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 
29 ScMoveTableDlg::ScMoveTableDlg(weld::Window* pParent, const OUString& rDefault)
30  : GenericDialogController(pParent, "modules/scalc/ui/movecopysheet.ui", "MoveCopySheetDialog")
31  , maDefaultName(rDefault)
32  , mnCurrentDocPos(0)
33  , nDocument(0)
34  , nTable(0)
35  , bCopyTable(false)
36  , bRenameTable(false)
37  , mbEverEdited(false)
38  , m_xBtnMove(m_xBuilder->weld_radio_button("move"))
39  , m_xBtnCopy(m_xBuilder->weld_radio_button("copy"))
40  , m_xFtDoc(m_xBuilder->weld_label("toDocumentLabel"))
41  , m_xLbDoc(m_xBuilder->weld_combo_box("toDocument"))
42  , m_xLbTable(m_xBuilder->weld_tree_view("insertBefore"))
43  , m_xEdTabName(m_xBuilder->weld_entry("newName"))
44  , m_xFtWarn(m_xBuilder->weld_label("newNameWarn"))
45  , m_xBtnOk(m_xBuilder->weld_button("ok"))
46  , m_xUnusedLabel(m_xBuilder->weld_label("warnunused"))
47  , m_xEmptyLabel(m_xBuilder->weld_label("warnempty"))
48  , m_xInvalidLabel(m_xBuilder->weld_label("warninvalid"))
49 {
50  assert(m_xLbDoc->get_count() == 2);
51  msCurrentDoc = m_xLbDoc->get_text(0);
52  msNewDoc = m_xLbDoc->get_text(1);
53  m_xLbDoc->clear();
54  assert(m_xLbDoc->get_count() == 0);
55 
56  m_xLbTable->set_size_request(-1, m_xLbTable->get_height_rows(8));
57 
58  msStrTabNameUsed = m_xUnusedLabel->get_label();
59  msStrTabNameEmpty = m_xEmptyLabel->get_label();
60  msStrTabNameInvalid = m_xInvalidLabel->get_label();
61 
62  Init();
63 }
64 
66 
67 void ScMoveTableDlg::GetTabNameString(OUString& rString) const
68 {
69  rString = m_xEdTabName->get_text();
70 }
71 
73 {
74  m_xBtnCopy->set_active(true);
75  m_xBtnMove->set_sensitive(false);
76  m_xBtnCopy->set_sensitive(false);
77  SetOkBtnLabel();
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;
112  pDoc->CreateValidTabName(aStr);
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 
124  CheckNewTabName();
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();
195  InitDocListBox();
196  SelHdl(*m_xLbDoc);
198  {
199  m_xFtDoc->hide();
200  m_xLbDoc->hide();
201  }
202  SetOkBtnLabel();
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 
251 IMPL_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: */
IMPL_LINK_NOARG(ScMoveTableDlg, OkHdl, weld::Button &, void)
Definition: mvtabdlg.cxx:258
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
OUString GetTitle(sal_uInt16 nMaxLen=0) const
std::unique_ptr< weld::ComboBox > m_xLbDoc
Definition: mvtabdlg.hxx:67
void EnableRenameTable(bool bFlag)
Definition: mvtabdlg.cxx:80
void CheckNewTabName()
Definition: mvtabdlg.cxx:127
void ResetRenameInput()
Definition: mvtabdlg.cxx:87
static SfxObjectShell * Current()
IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::Toggleable &, rBtn, void)
Definition: mvtabdlg.cxx:251
#define SC_DOC_NEW
Definition: document.hxx:247
std::unique_ptr< weld::RadioButton > m_xBtnCopy
Definition: mvtabdlg.hxx:65
std::unique_ptr< weld::TreeView > m_xLbTable
Definition: mvtabdlg.hxx:68
std::unique_ptr< weld::Entry > m_xEdTabName
Definition: mvtabdlg.hxx:69
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:315
void GetTabNameString(OUString &rString) const
Definition: mvtabdlg.cxx:67
void SetOkBtnLabel()
Definition: mvtabdlg.cxx:242
OUString msStrTabNameUsed
Definition: mvtabdlg.hxx:51
OUString msStrTabNameEmpty
Definition: mvtabdlg.hxx:52
std::unique_ptr< weld::RadioButton > m_xBtnMove
Definition: mvtabdlg.hxx:64
const OUString maDefaultName
Definition: mvtabdlg.hxx:55
std::unique_ptr< weld::Button > m_xBtnOk
Definition: mvtabdlg.hxx:71
const SCTAB SC_TAB_APPEND
Definition: address.hxx:90
int i
std::unique_ptr< weld::Label > m_xEmptyLabel
Definition: mvtabdlg.hxx:73
OUString msStrTabNameInvalid
Definition: mvtabdlg.hxx:53
OUString msCurrentDoc
Definition: mvtabdlg.hxx:48
sal_uInt16 mnCurrentDocPos
Definition: mvtabdlg.hxx:57
std::unique_ptr< weld::Label > m_xFtDoc
Definition: mvtabdlg.hxx:66
SC_DLLPUBLIC void CreateValidTabName(OUString &rName) const
Definition: document.cxx:393
virtual ~ScMoveTableDlg() override
Definition: mvtabdlg.cxx:65
void InitDocListBox()
Definition: mvtabdlg.cxx:205
std::unique_ptr< weld::Label > m_xUnusedLabel
Definition: mvtabdlg.hxx:72
std::unique_ptr< weld::Label > m_xInvalidLabel
Definition: mvtabdlg.hxx:74
void SetForceCopyTable()
Definition: mvtabdlg.cxx:72
bool mbEverEdited
Definition: mvtabdlg.hxx:62
OUString toId(const void *pValue)
bool bRenameTable
Definition: mvtabdlg.hxx:61
ScDocument * GetSelectedDoc()
Definition: mvtabdlg.cxx:179
static SfxObjectShell * GetFirst(const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
static SC_DLLPUBLIC bool ValidTabName(const OUString &rName)
Definition: document.cxx:336
OUString msNewDoc
Definition: mvtabdlg.hxx:49
OUString aName
RET_OK
Reference< XExecutableDialog > m_xDialog
const ScDocument & GetDocument() const
Definition: docsh.hxx:220
char aEntryName[20]
static SfxObjectShell * GetNext(const SfxObjectShell &rPrev, const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
ScMoveTableDlg(weld::Window *pParent, const OUString &rDefault)
Definition: mvtabdlg.cxx:29
std::unique_ptr< weld::Label > m_xFtWarn
Definition: mvtabdlg.hxx:70
aStr
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:215
sal_Int16 SCTAB
Definition: types.hxx:22
bool m_bDetectedRangeSegmentation false
OUString sId