Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / miscdlgs / mvtabdlg.cxx
blob210830fa7188b2b27db84da8ba7d0e65652d78aa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #undef SC_DLLIMPLEMENTATION
22 #include <vcl/msgbox.hxx>
24 #include "mvtabdlg.hxx"
25 #include "document.hxx"
26 #include "docsh.hxx"
27 #include "miscdlgs.hrc"
28 #include "global.hxx"
29 #include "scresid.hxx"
30 #include "globstr.hrc"
32 ScMoveTableDlg::ScMoveTableDlg(vcl::Window* pParent, const OUString& rDefault)
34 : ModalDialog ( pParent, "MoveCopySheetDialog", "modules/scalc/ui/movecopysheet.ui" ),
35 maDefaultName( rDefault ),
36 mnCurrentDocPos( 0 ),
37 nDocument ( 0 ),
38 nTable ( 0 ),
39 bCopyTable ( false ),
40 bRenameTable( false ),
41 mbEverEdited( false )
43 get(pBtnMove, "move");
44 get(pBtnCopy, "copy");
45 get(pLbDoc, "toDocument");
47 assert(pLbDoc->GetEntryCount() == 2);
48 msCurrentDoc = pLbDoc->GetEntry(0);
49 msNewDoc = pLbDoc->GetEntry(1);
50 pLbDoc->Clear();
51 assert(pLbDoc->GetEntryCount() == 0);
53 get(pLbTable, "insertBefore");
54 pLbTable->set_height_request(pLbTable->GetTextHeight() * 8);
55 get(pEdTabName, "newName");
56 get(pFtWarn, "newNameWarn");
57 get(pBtnOk, "ok");
59 msStrTabNameUsed = get<FixedText>("warnunused")->GetText();
60 msStrTabNameEmpty = get<FixedText>("warnempty")->GetText();
61 msStrTabNameInvalid = get<FixedText>("warninvalid")->GetText();
63 Init();
66 ScMoveTableDlg::~ScMoveTableDlg()
68 disposeOnce();
71 void ScMoveTableDlg::dispose()
73 pBtnMove.clear();
74 pBtnCopy.clear();
75 pLbDoc.clear();
76 pLbTable.clear();
77 pEdTabName.clear();
78 pFtWarn.clear();
79 pBtnOk.clear();
80 ModalDialog::dispose();
83 void ScMoveTableDlg::GetTabNameString( OUString& rString ) const
85 rString = pEdTabName->GetText();
88 void ScMoveTableDlg::SetForceCopyTable()
90 pBtnCopy->Check();
91 pBtnMove->Disable();
92 pBtnCopy->Disable();
95 void ScMoveTableDlg::EnableRenameTable(bool bFlag)
97 bRenameTable = bFlag;
98 pEdTabName->Enable(bFlag);
99 ResetRenameInput();
102 void ScMoveTableDlg::ResetRenameInput()
104 if (mbEverEdited)
106 // Don't reset the name when the sheet name has ever been edited.
107 // But check the name, as this is also called for change of copy/move
108 // buttons and document listbox selection.
109 CheckNewTabName();
110 return;
113 if (!pEdTabName->IsEnabled())
115 pEdTabName->SetText(OUString());
116 return;
119 bool bVal = pBtnCopy->IsChecked();
120 if (bVal)
122 // copy
123 ScDocument* pDoc = GetSelectedDoc();
124 if (pDoc)
126 OUString aStr = maDefaultName;
127 pDoc->CreateValidTabName(aStr);
128 pEdTabName->SetText(aStr);
130 else
131 pEdTabName->SetText(maDefaultName);
133 else
134 // move
135 pEdTabName->SetText(maDefaultName);
137 CheckNewTabName();
140 void ScMoveTableDlg::CheckNewTabName()
142 const OUString aNewName = pEdTabName->GetText();
143 if (aNewName.isEmpty())
145 // New sheet name is empty. This is not good.
146 pFtWarn->Show();
147 pFtWarn->SetControlBackground(Color(COL_YELLOW));
148 pFtWarn->SetText(msStrTabNameEmpty);
149 pBtnOk->Disable();
150 return;
153 if (!ScDocument::ValidTabName(aNewName))
155 // New sheet name contains invalid characters.
156 pFtWarn->Show();
157 pFtWarn->SetControlBackground(Color(COL_YELLOW));
158 pFtWarn->SetText(msStrTabNameInvalid);
159 pBtnOk->Disable();
160 return;
163 bool bMoveInCurrentDoc = (pBtnMove->IsChecked() && IsCurrentDocSelected());
164 bool bFound = false;
165 const sal_Int32 nLast = pLbTable->GetEntryCount();
166 for ( sal_uInt16 i=0; i<nLast && !bFound; ++i )
168 if ( aNewName.equals(pLbTable->GetEntry(i)) )
170 // Only for move within same document the same name is allowed.
171 if (!bMoveInCurrentDoc || !maDefaultName.equals( pEdTabName->GetText()))
172 bFound = true;
176 if ( bFound )
178 pFtWarn->Show();
179 pFtWarn->SetControlBackground(Color(COL_YELLOW));
180 pFtWarn->SetText(msStrTabNameUsed);
181 pBtnOk->Disable();
183 else
185 pFtWarn->Hide();
186 pFtWarn->SetControlBackground();
187 pFtWarn->SetText(OUString());
188 pBtnOk->Enable();
192 ScDocument* ScMoveTableDlg::GetSelectedDoc()
194 sal_Int32 nPos = pLbDoc->GetSelectEntryPos();
195 return static_cast<ScDocument*>(pLbDoc->GetEntryData(nPos));
198 bool ScMoveTableDlg::IsCurrentDocSelected() const
200 return pLbDoc->GetSelectEntryPos() == mnCurrentDocPos;
203 void ScMoveTableDlg::Init()
205 pBtnOk->SetClickHdl ( LINK( this, ScMoveTableDlg, OkHdl ) );
206 pLbDoc->SetSelectHdl ( LINK( this, ScMoveTableDlg, SelHdl ) );
207 pBtnCopy->SetToggleHdl( LINK( this, ScMoveTableDlg, CheckBtnHdl ) );
208 pEdTabName->SetModifyHdl( LINK( this, ScMoveTableDlg, CheckNameHdl ) );
209 pBtnMove->Check();
210 pBtnCopy->Check( false );
211 pEdTabName->Enable(false);
212 pFtWarn->Hide();
213 InitDocListBox();
214 SelHdl( *pLbDoc.get() );
217 void ScMoveTableDlg::InitDocListBox()
219 SfxObjectShell* pSh = SfxObjectShell::GetFirst();
220 ScDocShell* pScSh = nullptr;
221 sal_uInt16 nSelPos = 0;
222 sal_uInt16 i = 0;
223 OUString aEntryName;
225 pLbDoc->Clear();
226 pLbDoc->SetUpdateMode( false );
228 while ( pSh )
230 pScSh = dynamic_cast<ScDocShell*>( pSh );
232 if ( pScSh )
234 aEntryName = pScSh->GetTitle();
236 if ( pScSh == SfxObjectShell::Current() )
238 mnCurrentDocPos = nSelPos = i;
239 aEntryName += " ";
240 aEntryName += msCurrentDoc;
243 pLbDoc->InsertEntry( aEntryName, i );
244 pLbDoc->SetEntryData( i, static_cast<void*>(&pScSh->GetDocument()) );
246 i++;
248 pSh = SfxObjectShell::GetNext( *pSh );
251 pLbDoc->SetUpdateMode( true );
252 pLbDoc->InsertEntry(msNewDoc);
253 pLbDoc->SelectEntryPos( nSelPos );
256 // Handler:
258 IMPL_LINK_TYPED( ScMoveTableDlg, CheckBtnHdl, RadioButton&, rBtn, void )
260 if (&rBtn == pBtnCopy)
261 ResetRenameInput();
264 IMPL_LINK_NOARG_TYPED(ScMoveTableDlg, OkHdl, Button*, void)
266 const sal_Int32 nDocSel = pLbDoc->GetSelectEntryPos();
267 const sal_Int32 nDocLast = pLbDoc->GetEntryCount()-1;
268 const sal_Int32 nTabSel = pLbTable->GetSelectEntryPos();
269 const sal_Int32 nTabLast = pLbTable->GetEntryCount()-1;
271 nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
272 nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
273 bCopyTable = pBtnCopy->IsChecked();
275 if (bCopyTable)
277 // Return an empty string when the new name is the same as the
278 // automatic name assigned by the document.
279 OUString aCopyName = maDefaultName;
280 ScDocument* pDoc = GetSelectedDoc();
281 if (pDoc)
282 pDoc->CreateValidTabName(aCopyName);
283 if (aCopyName == pEdTabName->GetText())
284 pEdTabName->SetText( OUString() );
286 else
288 // Return an empty string, when the new name is the same as the
289 // original name.
290 if (maDefaultName.equals(pEdTabName->GetText()))
291 pEdTabName->SetText(OUString());
294 EndDialog( RET_OK );
297 IMPL_LINK_TYPED( ScMoveTableDlg, SelHdl, ListBox&, rLb, void )
299 if ( &rLb == pLbDoc )
301 ScDocument* pDoc = GetSelectedDoc();
302 OUString aName;
304 pLbTable->Clear();
305 pLbTable->SetUpdateMode( false );
306 if ( pDoc )
308 SCTAB nLast = pDoc->GetTableCount()-1;
309 for ( SCTAB i=0; i<=nLast; i++ )
311 pDoc->GetName( i, aName );
312 pLbTable->InsertEntry( aName, static_cast<sal_uInt16>(i) );
315 pLbTable->InsertEntry( ScGlobal::GetRscString(STR_MOVE_TO_END) );
316 pLbTable->SetUpdateMode( true );
317 pLbTable->SelectEntryPos( 0 );
318 ResetRenameInput();
322 IMPL_LINK_TYPED( ScMoveTableDlg, CheckNameHdl, Edit&, rEdt, void )
324 if ( &rEdt == pEdTabName )
326 mbEverEdited = true;
327 CheckNewTabName();
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */