LibreOffice Module sc (master) 1
mvtabdlg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#undef SC_DLLIMPLEMENTATION
21
22#include <mvtabdlg.hxx>
23#include <document.hxx>
24#include <docsh.hxx>
25#include <globstr.hrc>
26#include <scresid.hxx>
27#include <comphelper/lok.hxx>
28#include <utility>
29
30// tdf#96854 - remember last used option for copy/move sheet
32
33ScMoveTableDlg::ScMoveTableDlg(weld::Window* pParent, OUString aDefault)
34 : GenericDialogController(pParent, "modules/scalc/ui/movecopysheet.ui", "MoveCopySheetDialog")
35 , maDefaultName(std::move(aDefault))
36 , mnCurrentDocPos(0)
37 , nDocument(0)
38 , nTable(0)
39 , bCopyTable(false)
40 , bRenameTable(false)
41 , mbEverEdited(false)
42 , m_xBtnMove(m_xBuilder->weld_radio_button("move"))
43 , m_xBtnCopy(m_xBuilder->weld_radio_button("copy"))
44 , m_xFtDoc(m_xBuilder->weld_label("toDocumentLabel"))
45 , m_xLbDoc(m_xBuilder->weld_combo_box("toDocument"))
46 , m_xLbTable(m_xBuilder->weld_tree_view("insertBefore"))
47 , m_xEdTabName(m_xBuilder->weld_entry("newName"))
48 , m_xFtWarn(m_xBuilder->weld_label("newNameWarn"))
49 , m_xBtnOk(m_xBuilder->weld_button("ok"))
50 , m_xUnusedLabel(m_xBuilder->weld_label("warnunused"))
51 , m_xEmptyLabel(m_xBuilder->weld_label("warnempty"))
52 , m_xInvalidLabel(m_xBuilder->weld_label("warninvalid"))
53{
54 assert(m_xLbDoc->get_count() == 2);
55 msCurrentDoc = m_xLbDoc->get_text(0);
56 msNewDoc = m_xLbDoc->get_text(1);
57 m_xLbDoc->clear();
58 assert(m_xLbDoc->get_count() == 0);
59
60 m_xLbTable->set_size_request(-1, m_xLbTable->get_height_rows(8));
61
62 msStrTabNameUsed = m_xUnusedLabel->get_label();
63 msStrTabNameEmpty = m_xEmptyLabel->get_label();
65
66 Init();
67}
68
70
71void ScMoveTableDlg::GetTabNameString(OUString& rString) const
72{
73 rString = m_xEdTabName->get_text();
74}
75
77{
78 m_xBtnCopy->set_active(true);
79 m_xBtnMove->set_sensitive(false);
81}
82
84{
85 bRenameTable = bFlag;
86 m_xEdTabName->set_sensitive(bFlag);
88}
89
91{
92 if (mbEverEdited)
93 {
94 // Don't reset the name when the sheet name has ever been edited.
95 // But check the name, as this is also called for change of copy/move
96 // buttons and document listbox selection.
98 return;
99 }
100
101 if (!m_xEdTabName->get_sensitive())
102 {
103 m_xEdTabName->set_text(OUString());
104 return;
105 }
106
107 bool bVal = m_xBtnCopy->get_active();
108 if (bVal)
109 {
110 // copy
111 ScDocument* pDoc = GetSelectedDoc();
112 if (pDoc)
113 {
114 OUString aStr = maDefaultName;
116 m_xEdTabName->set_text(aStr);
117 }
118 else
119 m_xEdTabName->set_text(maDefaultName);
120 }
121 else
122 {
123 // move
124 m_xEdTabName->set_text(maDefaultName);
125 }
126
128}
129
131{
132 const OUString aNewName = m_xEdTabName->get_text();
133 if (aNewName.isEmpty())
134 {
135 // New sheet name is empty. This is not good.
136 m_xFtWarn->show();
137 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
138 m_xFtWarn->set_label(msStrTabNameEmpty);
139 m_xBtnOk->set_sensitive(false);
140 return;
141 }
142
143 if (!ScDocument::ValidTabName(aNewName))
144 {
145 // New sheet name contains invalid characters.
146 m_xFtWarn->show();
147 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
148 m_xFtWarn->set_label(msStrTabNameInvalid);
149 m_xBtnOk->set_sensitive(false);
150 return;
151 }
152
153 bool bMoveInCurrentDoc = m_xBtnMove->get_active() && m_xLbDoc->get_active() == mnCurrentDocPos;
154 bool bFound = false;
155 const int nLast = m_xLbTable->n_children();
156 for (int i = 0; i < nLast && !bFound; ++i)
157 {
158 if (aNewName == m_xLbTable->get_text(i))
159 {
160 // Only for move within same document the same name is allowed.
161 if (!bMoveInCurrentDoc || maDefaultName != m_xEdTabName->get_text())
162 bFound = true;
163 }
164 }
165
166 if (bFound)
167 {
168 m_xFtWarn->show();
169 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
170 m_xFtWarn->set_label(msStrTabNameUsed);
171 m_xBtnOk->set_sensitive(false);
172 }
173 else
174 {
175 m_xFtWarn->hide();
176 //TODO m_xFtWarn->SetControlBackground();
177 m_xFtWarn->set_label(OUString());
178 m_xBtnOk->set_sensitive(true);
179 }
180}
181
183{
184 return weld::fromId<ScDocument*>(m_xLbDoc->get_active_id());
185}
186
188{
189 m_xBtnOk->connect_clicked(LINK(this, ScMoveTableDlg, OkHdl));
190 m_xLbDoc->connect_changed(LINK(this, ScMoveTableDlg, SelHdl));
191 m_xBtnCopy->connect_toggled(LINK(this, ScMoveTableDlg, CheckBtnHdl));
192 m_xBtnMove->connect_toggled(LINK(this, ScMoveTableDlg, CheckBtnHdl));
193 m_xEdTabName->connect_changed(LINK(this, ScMoveTableDlg, CheckNameHdl));
194
195 // tdf#96854 - remember last used option for copy/move sheet
196 m_xBtnMove->set_active(!mbRememeberedCopy);
197 m_xBtnCopy->set_active(mbRememeberedCopy);
198 m_xEdTabName->set_sensitive(false);
199 m_xFtWarn->hide();
201 SelHdl(*m_xLbDoc);
203 {
204 m_xFtDoc->hide();
205 m_xLbDoc->hide();
206 }
208}
209
211{
213 ScDocShell* pScSh = nullptr;
214 sal_uInt16 nSelPos = 0;
215 sal_uInt16 i = 0;
216
217 m_xLbDoc->clear();
218 m_xLbDoc->freeze();
219
220 while (pSh)
221 {
222 pScSh = dynamic_cast<ScDocShell*>(pSh);
223
224 if (pScSh)
225 {
226 OUString aEntryName = pScSh->GetTitle();
227
228 if (pScSh == SfxObjectShell::Current())
229 {
230 mnCurrentDocPos = nSelPos = i;
231 aEntryName += " " + msCurrentDoc;
232 }
233
234 OUString sId(weld::toId(&pScSh->GetDocument()));
235 m_xLbDoc->insert(i, aEntryName, &sId, nullptr, nullptr);
236
237 i++;
238 }
239 pSh = SfxObjectShell::GetNext(*pSh);
240 }
241
242 m_xLbDoc->thaw();
243 m_xLbDoc->append_text(msNewDoc);
244 m_xLbDoc->set_active(nSelPos);
245}
246
248{
249 const bool bIsCopyActive = m_xBtnCopy->get_active();
250 // tdf#139464 Write "Copy" or "Move" on OK button
251 m_xBtnOk->set_label(bIsCopyActive ? m_xBtnCopy->get_label() : m_xBtnMove->get_label());
252 // tdf#96854 - remember last used option for copy/move sheet
253 mbRememeberedCopy = bIsCopyActive;
254}
255
256// Handler:
257
258IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::Toggleable&, rBtn, void)
259{
260 if (&rBtn == m_xBtnCopy.get())
261 ResetRenameInput();
262 SetOkBtnLabel();
263}
264
266{
267 const sal_Int32 nDocSel = m_xLbDoc->get_active();
268 const sal_Int32 nDocLast = m_xLbDoc->get_count() - 1;
269 const sal_Int32 nTabSel = m_xLbTable->get_selected_index();
270 const sal_Int32 nTabLast = m_xLbTable->n_children() - 1;
271
272 nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
273 nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
274 bCopyTable = m_xBtnCopy->get_active();
275
276 if (bCopyTable)
277 {
278 // Return an empty string when the new name is the same as the
279 // automatic name assigned by the document.
280 OUString aCopyName = maDefaultName;
281 ScDocument* pDoc = GetSelectedDoc();
282 if (pDoc)
283 pDoc->CreateValidTabName(aCopyName);
284 if (aCopyName == m_xEdTabName->get_text())
285 m_xEdTabName->set_text(OUString());
286 }
287 else
288 {
289 // Return an empty string, when the new name is the same as the
290 // original name.
291 if (maDefaultName == m_xEdTabName->get_text())
292 m_xEdTabName->set_text(OUString());
293 }
294
295 m_xDialog->response(RET_OK);
296}
297
299{
300 ScDocument* pDoc = GetSelectedDoc();
301 OUString aName;
302
303 m_xLbTable->clear();
304 m_xLbTable->freeze();
305 if (pDoc)
306 {
307 SCTAB nLast = pDoc->GetTableCount() - 1;
308 for (SCTAB i = 0; i <= nLast; ++i)
309 {
310 pDoc->GetName(i, aName);
311 m_xLbTable->append_text(aName);
312 }
313 }
314 m_xLbTable->append_text(ScResId(STR_MOVE_TO_END));
315 m_xLbTable->thaw();
316 m_xLbTable->select(0);
317 ResetRenameInput();
318}
319
321{
322 mbEverEdited = true;
323 CheckNewTabName();
324}
325
326/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SCTAB SC_TAB_APPEND
Definition: address.hxx:90
Reference< XExecutableDialog > m_xDialog
const ScDocument & GetDocument() const
Definition: docsh.hxx:219
static SC_DLLPUBLIC bool ValidTabName(const OUString &rName)
Definition: document.cxx:318
SC_DLLPUBLIC void CreateValidTabName(OUString &rName) const
Definition: document.cxx:375
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:204
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:297
OUString msStrTabNameInvalid
Definition: mvtabdlg.hxx:53
void EnableRenameTable(bool bFlag)
Definition: mvtabdlg.cxx:83
std::unique_ptr< weld::ComboBox > m_xLbDoc
Definition: mvtabdlg.hxx:70
void GetTabNameString(OUString &rString) const
Definition: mvtabdlg.cxx:71
std::unique_ptr< weld::Label > m_xFtWarn
Definition: mvtabdlg.hxx:73
void ResetRenameInput()
Definition: mvtabdlg.cxx:90
std::unique_ptr< weld::RadioButton > m_xBtnCopy
Definition: mvtabdlg.hxx:68
OUString msStrTabNameEmpty
Definition: mvtabdlg.hxx:52
sal_uInt16 mnCurrentDocPos
Definition: mvtabdlg.hxx:57
ScMoveTableDlg(weld::Window *pParent, OUString aDefault)
Definition: mvtabdlg.cxx:33
static bool mbRememeberedCopy
Definition: mvtabdlg.hxx:65
std::unique_ptr< weld::TreeView > m_xLbTable
Definition: mvtabdlg.hxx:71
std::unique_ptr< weld::Label > m_xUnusedLabel
Definition: mvtabdlg.hxx:75
void InitDocListBox()
Definition: mvtabdlg.cxx:210
OUString msCurrentDoc
Definition: mvtabdlg.hxx:48
void SetForceCopyTable()
Definition: mvtabdlg.cxx:76
ScDocument * GetSelectedDoc()
Definition: mvtabdlg.cxx:182
std::unique_ptr< weld::Button > m_xBtnOk
Definition: mvtabdlg.hxx:74
OUString msStrTabNameUsed
Definition: mvtabdlg.hxx:51
void SetOkBtnLabel()
Definition: mvtabdlg.cxx:247
const OUString maDefaultName
Definition: mvtabdlg.hxx:55
bool mbEverEdited
Definition: mvtabdlg.hxx:62
void CheckNewTabName()
Definition: mvtabdlg.cxx:130
std::unique_ptr< weld::Entry > m_xEdTabName
Definition: mvtabdlg.hxx:72
std::unique_ptr< weld::Label > m_xEmptyLabel
Definition: mvtabdlg.hxx:76
OUString msNewDoc
Definition: mvtabdlg.hxx:49
bool bRenameTable
Definition: mvtabdlg.hxx:61
std::unique_ptr< weld::Label > m_xFtDoc
Definition: mvtabdlg.hxx:69
std::unique_ptr< weld::RadioButton > m_xBtnMove
Definition: mvtabdlg.hxx:67
std::unique_ptr< weld::Label > m_xInvalidLabel
Definition: mvtabdlg.hxx:77
virtual ~ScMoveTableDlg() override
Definition: mvtabdlg.cxx:69
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetNext(const SfxObjectShell &rPrev, const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
OUString GetTitle(sal_uInt16 nMaxLen=0) const
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetFirst(const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
static SAL_WARN_UNUSED_RESULT SfxObjectShell * Current()
#define SC_DOC_NEW
Definition: document.hxx:248
OUString aName
aStr
IMPL_LINK_NOARG(ScMoveTableDlg, OkHdl, weld::Button &, void)
Definition: mvtabdlg.cxx:265
IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::Toggleable &, rBtn, void)
Definition: mvtabdlg.cxx:258
int i
OUString toId(const void *pValue)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
char aEntryName[20]
sal_Int16 SCTAB
Definition: types.hxx:22
OUString sId
RET_OK