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
30ScMoveTableDlg::ScMoveTableDlg(weld::Window* pParent, OUString aDefault)
31 : GenericDialogController(pParent, "modules/scalc/ui/movecopysheet.ui", "MoveCopySheetDialog")
32 , maDefaultName(std::move(aDefault))
33 , mnCurrentDocPos(0)
34 , nDocument(0)
35 , nTable(0)
36 , bCopyTable(false)
37 , bRenameTable(false)
38 , mbEverEdited(false)
39 , m_xBtnMove(m_xBuilder->weld_radio_button("move"))
40 , m_xBtnCopy(m_xBuilder->weld_radio_button("copy"))
41 , m_xFtDoc(m_xBuilder->weld_label("toDocumentLabel"))
42 , m_xLbDoc(m_xBuilder->weld_combo_box("toDocument"))
43 , m_xLbTable(m_xBuilder->weld_tree_view("insertBefore"))
44 , m_xEdTabName(m_xBuilder->weld_entry("newName"))
45 , m_xFtWarn(m_xBuilder->weld_label("newNameWarn"))
46 , m_xBtnOk(m_xBuilder->weld_button("ok"))
47 , m_xUnusedLabel(m_xBuilder->weld_label("warnunused"))
48 , m_xEmptyLabel(m_xBuilder->weld_label("warnempty"))
49 , m_xInvalidLabel(m_xBuilder->weld_label("warninvalid"))
50{
51 assert(m_xLbDoc->get_count() == 2);
52 msCurrentDoc = m_xLbDoc->get_text(0);
53 msNewDoc = m_xLbDoc->get_text(1);
54 m_xLbDoc->clear();
55 assert(m_xLbDoc->get_count() == 0);
56
57 m_xLbTable->set_size_request(-1, m_xLbTable->get_height_rows(8));
58
59 msStrTabNameUsed = m_xUnusedLabel->get_label();
60 msStrTabNameEmpty = m_xEmptyLabel->get_label();
62
63 Init();
64}
65
67
68void ScMoveTableDlg::GetTabNameString(OUString& rString) const
69{
70 rString = m_xEdTabName->get_text();
71}
72
74{
75 m_xBtnCopy->set_active(true);
76 m_xBtnMove->set_sensitive(false);
78}
79
81{
82 bRenameTable = bFlag;
83 m_xEdTabName->set_sensitive(bFlag);
85}
86
88{
89 if (mbEverEdited)
90 {
91 // Don't reset the name when the sheet name has ever been edited.
92 // But check the name, as this is also called for change of copy/move
93 // buttons and document listbox selection.
95 return;
96 }
97
98 if (!m_xEdTabName->get_sensitive())
99 {
100 m_xEdTabName->set_text(OUString());
101 return;
102 }
103
104 bool bVal = m_xBtnCopy->get_active();
105 if (bVal)
106 {
107 // copy
108 ScDocument* pDoc = GetSelectedDoc();
109 if (pDoc)
110 {
111 OUString aStr = maDefaultName;
113 m_xEdTabName->set_text(aStr);
114 }
115 else
116 m_xEdTabName->set_text(maDefaultName);
117 }
118 else
119 {
120 // move
121 m_xEdTabName->set_text(maDefaultName);
122 }
123
125}
126
128{
129 const OUString aNewName = m_xEdTabName->get_text();
130 if (aNewName.isEmpty())
131 {
132 // New sheet name is empty. This is not good.
133 m_xFtWarn->show();
134 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
135 m_xFtWarn->set_label(msStrTabNameEmpty);
136 m_xBtnOk->set_sensitive(false);
137 return;
138 }
139
140 if (!ScDocument::ValidTabName(aNewName))
141 {
142 // New sheet name contains invalid characters.
143 m_xFtWarn->show();
144 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
145 m_xFtWarn->set_label(msStrTabNameInvalid);
146 m_xBtnOk->set_sensitive(false);
147 return;
148 }
149
150 bool bMoveInCurrentDoc = m_xBtnMove->get_active() && m_xLbDoc->get_active() == mnCurrentDocPos;
151 bool bFound = false;
152 const int nLast = m_xLbTable->n_children();
153 for (int i = 0; i < nLast && !bFound; ++i)
154 {
155 if (aNewName == m_xLbTable->get_text(i))
156 {
157 // Only for move within same document the same name is allowed.
158 if (!bMoveInCurrentDoc || maDefaultName != m_xEdTabName->get_text())
159 bFound = true;
160 }
161 }
162
163 if (bFound)
164 {
165 m_xFtWarn->show();
166 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
167 m_xFtWarn->set_label(msStrTabNameUsed);
168 m_xBtnOk->set_sensitive(false);
169 }
170 else
171 {
172 m_xFtWarn->hide();
173 //TODO m_xFtWarn->SetControlBackground();
174 m_xFtWarn->set_label(OUString());
175 m_xBtnOk->set_sensitive(true);
176 }
177}
178
180{
181 return weld::fromId<ScDocument*>(m_xLbDoc->get_active_id());
182}
183
185{
186 m_xBtnOk->connect_clicked(LINK(this, ScMoveTableDlg, OkHdl));
187 m_xLbDoc->connect_changed(LINK(this, ScMoveTableDlg, SelHdl));
188 m_xBtnCopy->connect_toggled(LINK(this, ScMoveTableDlg, CheckBtnHdl));
189 m_xBtnMove->connect_toggled(LINK(this, ScMoveTableDlg, CheckBtnHdl));
190 m_xEdTabName->connect_changed(LINK(this, ScMoveTableDlg, CheckNameHdl));
191 m_xBtnMove->set_active(true);
192 m_xBtnCopy->set_active(false);
193 m_xEdTabName->set_sensitive(false);
194 m_xFtWarn->hide();
196 SelHdl(*m_xLbDoc);
198 {
199 m_xFtDoc->hide();
200 m_xLbDoc->hide();
201 }
203}
204
206{
208 ScDocShell* pScSh = nullptr;
209 sal_uInt16 nSelPos = 0;
210 sal_uInt16 i = 0;
211
212 m_xLbDoc->clear();
213 m_xLbDoc->freeze();
214
215 while (pSh)
216 {
217 pScSh = dynamic_cast<ScDocShell*>(pSh);
218
219 if (pScSh)
220 {
221 OUString aEntryName = pScSh->GetTitle();
222
223 if (pScSh == SfxObjectShell::Current())
224 {
225 mnCurrentDocPos = nSelPos = i;
226 aEntryName += " " + msCurrentDoc;
227 }
228
229 OUString sId(weld::toId(&pScSh->GetDocument()));
230 m_xLbDoc->insert(i, aEntryName, &sId, nullptr, nullptr);
231
232 i++;
233 }
234 pSh = SfxObjectShell::GetNext(*pSh);
235 }
236
237 m_xLbDoc->thaw();
238 m_xLbDoc->append_text(msNewDoc);
239 m_xLbDoc->set_active(nSelPos);
240}
241
243{
244 // tdf#139464 Write "Copy" or "Move" on OK button
245 m_xBtnOk->set_label(m_xBtnCopy->get_active() ? m_xBtnCopy->get_label()
246 : m_xBtnMove->get_label());
247}
248
249// Handler:
250
251IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::Toggleable&, rBtn, void)
252{
253 if (&rBtn == m_xBtnCopy.get())
254 ResetRenameInput();
255 SetOkBtnLabel();
256}
257
259{
260 const sal_Int32 nDocSel = m_xLbDoc->get_active();
261 const sal_Int32 nDocLast = m_xLbDoc->get_count() - 1;
262 const sal_Int32 nTabSel = m_xLbTable->get_selected_index();
263 const sal_Int32 nTabLast = m_xLbTable->n_children() - 1;
264
265 nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
266 nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
267 bCopyTable = m_xBtnCopy->get_active();
268
269 if (bCopyTable)
270 {
271 // Return an empty string when the new name is the same as the
272 // automatic name assigned by the document.
273 OUString aCopyName = maDefaultName;
274 ScDocument* pDoc = GetSelectedDoc();
275 if (pDoc)
276 pDoc->CreateValidTabName(aCopyName);
277 if (aCopyName == m_xEdTabName->get_text())
278 m_xEdTabName->set_text(OUString());
279 }
280 else
281 {
282 // Return an empty string, when the new name is the same as the
283 // original name.
284 if (maDefaultName == m_xEdTabName->get_text())
285 m_xEdTabName->set_text(OUString());
286 }
287
288 m_xDialog->response(RET_OK);
289}
290
292{
293 ScDocument* pDoc = GetSelectedDoc();
294 OUString aName;
295
296 m_xLbTable->clear();
297 m_xLbTable->freeze();
298 if (pDoc)
299 {
300 SCTAB nLast = pDoc->GetTableCount() - 1;
301 for (SCTAB i = 0; i <= nLast; ++i)
302 {
303 pDoc->GetName(i, aName);
304 m_xLbTable->append_text(aName);
305 }
306 }
307 m_xLbTable->append_text(ScResId(STR_MOVE_TO_END));
308 m_xLbTable->thaw();
309 m_xLbTable->select(0);
310 ResetRenameInput();
311}
312
314{
315 mbEverEdited = true;
316 CheckNewTabName();
317}
318
319/* 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:220
static SC_DLLPUBLIC bool ValidTabName(const OUString &rName)
Definition: document.cxx:338
SC_DLLPUBLIC void CreateValidTabName(OUString &rName) const
Definition: document.cxx:395
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:217
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:317
OUString msStrTabNameInvalid
Definition: mvtabdlg.hxx:53
void EnableRenameTable(bool bFlag)
Definition: mvtabdlg.cxx:80
std::unique_ptr< weld::ComboBox > m_xLbDoc
Definition: mvtabdlg.hxx:67
void GetTabNameString(OUString &rString) const
Definition: mvtabdlg.cxx:68
std::unique_ptr< weld::Label > m_xFtWarn
Definition: mvtabdlg.hxx:70
void ResetRenameInput()
Definition: mvtabdlg.cxx:87
std::unique_ptr< weld::RadioButton > m_xBtnCopy
Definition: mvtabdlg.hxx:65
OUString msStrTabNameEmpty
Definition: mvtabdlg.hxx:52
sal_uInt16 mnCurrentDocPos
Definition: mvtabdlg.hxx:57
ScMoveTableDlg(weld::Window *pParent, OUString aDefault)
Definition: mvtabdlg.cxx:30
std::unique_ptr< weld::TreeView > m_xLbTable
Definition: mvtabdlg.hxx:68
std::unique_ptr< weld::Label > m_xUnusedLabel
Definition: mvtabdlg.hxx:72
void InitDocListBox()
Definition: mvtabdlg.cxx:205
OUString msCurrentDoc
Definition: mvtabdlg.hxx:48
void SetForceCopyTable()
Definition: mvtabdlg.cxx:73
ScDocument * GetSelectedDoc()
Definition: mvtabdlg.cxx:179
std::unique_ptr< weld::Button > m_xBtnOk
Definition: mvtabdlg.hxx:71
OUString msStrTabNameUsed
Definition: mvtabdlg.hxx:51
void SetOkBtnLabel()
Definition: mvtabdlg.cxx:242
const OUString maDefaultName
Definition: mvtabdlg.hxx:55
bool mbEverEdited
Definition: mvtabdlg.hxx:62
void CheckNewTabName()
Definition: mvtabdlg.cxx:127
std::unique_ptr< weld::Entry > m_xEdTabName
Definition: mvtabdlg.hxx:69
std::unique_ptr< weld::Label > m_xEmptyLabel
Definition: mvtabdlg.hxx:73
OUString msNewDoc
Definition: mvtabdlg.hxx:49
bool bRenameTable
Definition: mvtabdlg.hxx:61
std::unique_ptr< weld::Label > m_xFtDoc
Definition: mvtabdlg.hxx:66
std::unique_ptr< weld::RadioButton > m_xBtnMove
Definition: mvtabdlg.hxx:64
std::unique_ptr< weld::Label > m_xInvalidLabel
Definition: mvtabdlg.hxx:74
virtual ~ScMoveTableDlg() override
Definition: mvtabdlg.cxx:66
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:258
IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::Toggleable &, rBtn, void)
Definition: mvtabdlg.cxx:251
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