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 }
78 
80 {
81  bRenameTable = bFlag;
82  m_xEdTabName->set_sensitive(bFlag);
84 }
85 
87 {
88  if (mbEverEdited)
89  {
90  // Don't reset the name when the sheet name has ever been edited.
91  // But check the name, as this is also called for change of copy/move
92  // buttons and document listbox selection.
94  return;
95  }
96 
97  if (!m_xEdTabName->get_sensitive())
98  {
99  m_xEdTabName->set_text(OUString());
100  return;
101  }
102 
103  bool bVal = m_xBtnCopy->get_active();
104  if (bVal)
105  {
106  // copy
107  ScDocument* pDoc = GetSelectedDoc();
108  if (pDoc)
109  {
110  OUString aStr = maDefaultName;
111  pDoc->CreateValidTabName(aStr);
112  m_xEdTabName->set_text(aStr);
113  }
114  else
115  m_xEdTabName->set_text(maDefaultName);
116  }
117  else
118  {
119  // move
120  m_xEdTabName->set_text(maDefaultName);
121  }
122 
123  CheckNewTabName();
124 }
125 
127 {
128  const OUString aNewName = m_xEdTabName->get_text();
129  if (aNewName.isEmpty())
130  {
131  // New sheet name is empty. This is not good.
132  m_xFtWarn->show();
133  //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
134  m_xFtWarn->set_label(msStrTabNameEmpty);
135  m_xBtnOk->set_sensitive(false);
136  return;
137  }
138 
139  if (!ScDocument::ValidTabName(aNewName))
140  {
141  // New sheet name contains invalid characters.
142  m_xFtWarn->show();
143  //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
144  m_xFtWarn->set_label(msStrTabNameInvalid);
145  m_xBtnOk->set_sensitive(false);
146  return;
147  }
148 
149  bool bMoveInCurrentDoc = m_xBtnMove->get_active() && m_xLbDoc->get_active() == mnCurrentDocPos;
150  bool bFound = false;
151  const int nLast = m_xLbTable->n_children();
152  for (int i = 0; i < nLast && !bFound; ++i)
153  {
154  if (aNewName == m_xLbTable->get_text(i))
155  {
156  // Only for move within same document the same name is allowed.
157  if (!bMoveInCurrentDoc || maDefaultName != m_xEdTabName->get_text())
158  bFound = true;
159  }
160  }
161 
162  if (bFound)
163  {
164  m_xFtWarn->show();
165  //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
166  m_xFtWarn->set_label(msStrTabNameUsed);
167  m_xBtnOk->set_sensitive(false);
168  }
169  else
170  {
171  m_xFtWarn->hide();
172  //TODO m_xFtWarn->SetControlBackground();
173  m_xFtWarn->set_label(OUString());
174  m_xBtnOk->set_sensitive(true);
175  }
176 }
177 
179 {
180  return reinterpret_cast<ScDocument*>(m_xLbDoc->get_active_id().toUInt64());
181 }
182 
184 {
185  m_xBtnOk->connect_clicked(LINK(this, ScMoveTableDlg, OkHdl));
186  m_xLbDoc->connect_changed(LINK(this, ScMoveTableDlg, SelHdl));
187  m_xBtnCopy->connect_toggled(LINK(this, ScMoveTableDlg, CheckBtnHdl));
188  m_xEdTabName->connect_changed(LINK(this, ScMoveTableDlg, CheckNameHdl));
189  m_xBtnMove->set_active(true);
190  m_xBtnCopy->set_active(false);
191  m_xEdTabName->set_sensitive(false);
192  m_xFtWarn->hide();
193  InitDocListBox();
194  SelHdl(*m_xLbDoc);
196  {
197  m_xFtDoc->hide();
198  m_xLbDoc->hide();
199  }
200 }
201 
203 {
205  ScDocShell* pScSh = nullptr;
206  sal_uInt16 nSelPos = 0;
207  sal_uInt16 i = 0;
208 
209  m_xLbDoc->clear();
210  m_xLbDoc->freeze();
211 
212  while (pSh)
213  {
214  pScSh = dynamic_cast<ScDocShell*>(pSh);
215 
216  if (pScSh)
217  {
218  OUString aEntryName = pScSh->GetTitle();
219 
220  if (pScSh == SfxObjectShell::Current())
221  {
222  mnCurrentDocPos = nSelPos = i;
223  aEntryName += " " + msCurrentDoc;
224  }
225 
226  OUString sId(OUString::number(reinterpret_cast<sal_uInt64>(&pScSh->GetDocument())));
227  m_xLbDoc->insert(i, aEntryName, &sId, nullptr, nullptr);
228 
229  i++;
230  }
231  pSh = SfxObjectShell::GetNext(*pSh);
232  }
233 
234  m_xLbDoc->thaw();
235  m_xLbDoc->append_text(msNewDoc);
236  m_xLbDoc->set_active(nSelPos);
237 }
238 
239 // Handler:
240 
241 IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::ToggleButton&, rBtn, void)
242 {
243  if (&rBtn == m_xBtnCopy.get())
244  ResetRenameInput();
245 }
246 
248 {
249  const sal_Int32 nDocSel = m_xLbDoc->get_active();
250  const sal_Int32 nDocLast = m_xLbDoc->get_count() - 1;
251  const sal_Int32 nTabSel = m_xLbTable->get_selected_index();
252  const sal_Int32 nTabLast = m_xLbTable->n_children() - 1;
253 
254  nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
255  nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
256  bCopyTable = m_xBtnCopy->get_active();
257 
258  if (bCopyTable)
259  {
260  // Return an empty string when the new name is the same as the
261  // automatic name assigned by the document.
262  OUString aCopyName = maDefaultName;
263  ScDocument* pDoc = GetSelectedDoc();
264  if (pDoc)
265  pDoc->CreateValidTabName(aCopyName);
266  if (aCopyName == m_xEdTabName->get_text())
267  m_xEdTabName->set_text(OUString());
268  }
269  else
270  {
271  // Return an empty string, when the new name is the same as the
272  // original name.
273  if (maDefaultName == m_xEdTabName->get_text())
274  m_xEdTabName->set_text(OUString());
275  }
276 
277  m_xDialog->response(RET_OK);
278 }
279 
281 {
282  ScDocument* pDoc = GetSelectedDoc();
283  OUString aName;
284 
285  m_xLbTable->clear();
286  m_xLbTable->freeze();
287  if (pDoc)
288  {
289  SCTAB nLast = pDoc->GetTableCount() - 1;
290  for (SCTAB i = 0; i <= nLast; ++i)
291  {
292  pDoc->GetName(i, aName);
293  m_xLbTable->append_text(aName);
294  }
295  }
296  m_xLbTable->append_text(ScResId(STR_MOVE_TO_END));
297  m_xLbTable->thaw();
298  m_xLbTable->select(0);
299  ResetRenameInput();
300 }
301 
303 {
304  mbEverEdited = true;
305  CheckNewTabName();
306 }
307 
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPL_LINK_NOARG(ScMoveTableDlg, OkHdl, weld::Button &, void)
Definition: mvtabdlg.cxx:247
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:79
void CheckNewTabName()
Definition: mvtabdlg.cxx:126
void ResetRenameInput()
Definition: mvtabdlg.cxx:86
static SfxObjectShell * Current()
#define SC_DOC_NEW
Definition: document.hxx:238
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
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:312
void GetTabNameString(OUString &rString) const
Definition: mvtabdlg.cxx:67
OUString msStrTabNameUsed
Definition: mvtabdlg.hxx:51
OUString msStrTabNameEmpty
Definition: mvtabdlg.hxx:52
std::unique_ptr< weld::RadioButton > m_xBtnMove
Definition: mvtabdlg.hxx:64
IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::ToggleButton &, rBtn, void)
Definition: mvtabdlg.cxx:241
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:84
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:390
OUString ScResId(const char *pId)
Definition: scdll.cxx:88
virtual ~ScMoveTableDlg() override
Definition: mvtabdlg.cxx:65
void InitDocListBox()
Definition: mvtabdlg.cxx:202
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
bool bRenameTable
Definition: mvtabdlg.hxx:61
ScDocument * GetSelectedDoc()
Definition: mvtabdlg.cxx:178
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:333
OUString msNewDoc
Definition: mvtabdlg.hxx:49
OUString aName
RET_OK
Reference< XExecutableDialog > m_xDialog
const ScDocument & GetDocument() const
Definition: docsh.hxx:216
char aEntryName[20]
OUString sId
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:211
sal_Int16 SCTAB
Definition: types.hxx:23