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 
28 ScMoveTableDlg::ScMoveTableDlg(weld::Window* pParent, const OUString& rDefault)
29  : GenericDialogController(pParent, "modules/scalc/ui/movecopysheet.ui", "MoveCopySheetDialog")
30  , maDefaultName(rDefault)
31  , mnCurrentDocPos(0)
32  , nDocument(0)
33  , nTable(0)
34  , bCopyTable(false)
35  , bRenameTable(false)
36  , mbEverEdited(false)
37  , m_xBtnMove(m_xBuilder->weld_radio_button("move"))
38  , m_xBtnCopy(m_xBuilder->weld_radio_button("copy"))
39  , m_xLbDoc(m_xBuilder->weld_combo_box("toDocument"))
40  , m_xLbTable(m_xBuilder->weld_tree_view("insertBefore"))
41  , m_xEdTabName(m_xBuilder->weld_entry("newName"))
42  , m_xFtWarn(m_xBuilder->weld_label("newNameWarn"))
43  , m_xBtnOk(m_xBuilder->weld_button("ok"))
44  , m_xUnusedLabel(m_xBuilder->weld_label("warnunused"))
45  , m_xEmptyLabel(m_xBuilder->weld_label("warnempty"))
46  , m_xInvalidLabel(m_xBuilder->weld_label("warninvalid"))
47 {
48  assert(m_xLbDoc->get_count() == 2);
49  msCurrentDoc = m_xLbDoc->get_text(0);
50  msNewDoc = m_xLbDoc->get_text(1);
51  m_xLbDoc->clear();
52  assert(m_xLbDoc->get_count() == 0);
53 
54  m_xLbTable->set_size_request(-1, m_xLbTable->get_height_rows(8));
55 
56  msStrTabNameUsed = m_xUnusedLabel->get_label();
57  msStrTabNameEmpty = m_xEmptyLabel->get_label();
58  msStrTabNameInvalid = m_xInvalidLabel->get_label();
59 
60  Init();
61 }
62 
64 {
65 }
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);
195 }
196 
198 {
200  ScDocShell* pScSh = nullptr;
201  sal_uInt16 nSelPos = 0;
202  sal_uInt16 i = 0;
203 
204  m_xLbDoc->clear();
205  m_xLbDoc->freeze();
206 
207  while ( pSh )
208  {
209  pScSh = dynamic_cast<ScDocShell*>( pSh );
210 
211  if ( pScSh )
212  {
213  OUString aEntryName = pScSh->GetTitle();
214 
215  if ( pScSh == SfxObjectShell::Current() )
216  {
217  mnCurrentDocPos = nSelPos = i;
218  aEntryName += " " + msCurrentDoc;
219  }
220 
221  OUString sId(OUString::number(reinterpret_cast<sal_uInt64>(&pScSh->GetDocument())));
222  m_xLbDoc->insert(i, aEntryName, &sId, nullptr, nullptr);
223 
224  i++;
225  }
226  pSh = SfxObjectShell::GetNext( *pSh );
227  }
228 
229  m_xLbDoc->thaw();
230  m_xLbDoc->append_text(msNewDoc);
231  m_xLbDoc->set_active(nSelPos);
232 }
233 
234 // Handler:
235 
236 IMPL_LINK( ScMoveTableDlg, CheckBtnHdl, weld::ToggleButton&, rBtn, void )
237 {
238  if (&rBtn == m_xBtnCopy.get())
239  ResetRenameInput();
240 }
241 
243 {
244  const sal_Int32 nDocSel = m_xLbDoc->get_active();
245  const sal_Int32 nDocLast = m_xLbDoc->get_count() - 1;
246  const sal_Int32 nTabSel = m_xLbTable->get_selected_index();
247  const sal_Int32 nTabLast = m_xLbTable->n_children() - 1;
248 
249  nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
250  nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
251  bCopyTable = m_xBtnCopy->get_active();
252 
253  if (bCopyTable)
254  {
255  // Return an empty string when the new name is the same as the
256  // automatic name assigned by the document.
257  OUString aCopyName = maDefaultName;
258  ScDocument* pDoc = GetSelectedDoc();
259  if (pDoc)
260  pDoc->CreateValidTabName(aCopyName);
261  if (aCopyName == m_xEdTabName->get_text())
262  m_xEdTabName->set_text(OUString());
263  }
264  else
265  {
266  // Return an empty string, when the new name is the same as the
267  // original name.
268  if (maDefaultName == m_xEdTabName->get_text())
269  m_xEdTabName->set_text(OUString());
270  }
271 
272  m_xDialog->response(RET_OK);
273 }
274 
276 {
277  ScDocument* pDoc = GetSelectedDoc();
278  OUString aName;
279 
280  m_xLbTable->clear();
281  m_xLbTable->freeze();
282  if ( pDoc )
283  {
284  SCTAB nLast = pDoc->GetTableCount()-1;
285  for (SCTAB i = 0; i <= nLast; ++i)
286  {
287  pDoc->GetName(i, aName);
288  m_xLbTable->append_text(aName);
289  }
290  }
291  m_xLbTable->append_text(ScResId(STR_MOVE_TO_END));
292  m_xLbTable->thaw();
293  m_xLbTable->select(0);
294  ResetRenameInput();
295 }
296 
298 {
299  mbEverEdited = true;
300  CheckNewTabName();
301 }
302 
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPL_LINK_NOARG(ScMoveTableDlg, OkHdl, weld::Button &, void)
Definition: mvtabdlg.cxx:242
OUString GetTitle(sal_uInt16 nMaxLen=0) const
std::unique_ptr< weld::ComboBox > m_xLbDoc
Definition: mvtabdlg.hxx:66
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:67
std::unique_ptr< weld::Entry > m_xEdTabName
Definition: mvtabdlg.hxx:68
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:313
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:236
const OUString maDefaultName
Definition: mvtabdlg.hxx:55
std::unique_ptr< weld::Button > m_xBtnOk
Definition: mvtabdlg.hxx:70
const SCTAB SC_TAB_APPEND
Definition: address.hxx:84
int i
std::unique_ptr< weld::Label > m_xEmptyLabel
Definition: mvtabdlg.hxx:72
OUString msStrTabNameInvalid
Definition: mvtabdlg.hxx:53
OUString msCurrentDoc
Definition: mvtabdlg.hxx:48
sal_uInt16 mnCurrentDocPos
Definition: mvtabdlg.hxx:57
SC_DLLPUBLIC void CreateValidTabName(OUString &rName) const
Definition: document.cxx:391
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
virtual ~ScMoveTableDlg() override
Definition: mvtabdlg.cxx:63
void InitDocListBox()
Definition: mvtabdlg.cxx:197
std::unique_ptr< weld::Label > m_xUnusedLabel
Definition: mvtabdlg.hxx:71
std::unique_ptr< weld::Label > m_xInvalidLabel
Definition: mvtabdlg.hxx:73
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:334
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:28
std::unique_ptr< weld::Label > m_xFtWarn
Definition: mvtabdlg.hxx:69
aStr
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:212
sal_Int16 SCTAB
Definition: types.hxx:23