Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / miscdlgs / mvtabdlg.cxx
blob7c169ba531fba7bfcb1f08e353332e2ad3d579c4
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 .
21 #undef SC_DLLIMPLEMENTATION
25 //------------------------------------------------------------------
27 #include <vcl/msgbox.hxx>
29 #include "mvtabdlg.hxx"
30 #include "document.hxx"
31 #include "docsh.hxx"
32 #include "miscdlgs.hrc"
33 #include "global.hxx"
34 #include "scresid.hxx"
35 #include "globstr.hrc"
37 //==================================================================
39 ScMoveTableDlg::ScMoveTableDlg(Window* pParent, const OUString& rDefault)
41 : ModalDialog ( pParent, "MoveCopySheetDialog", "modules/scalc/ui/movecopysheet.ui" ),
42 maDefaultName( rDefault ),
43 mnCurrentDocPos( 0 ),
44 nDocument ( 0 ),
45 nTable ( 0 ),
46 bCopyTable ( false ),
47 bRenameTable( false ),
48 mbEverEdited( false )
50 get(pBtnMove, "move");
51 get(pBtnCopy, "copy");
52 get(pLbDoc, "toDocument");
54 assert(pLbDoc->GetEntryCount() == 2);
55 msCurrentDoc = pLbDoc->GetEntry(0);
56 msNewDoc = pLbDoc->GetEntry(1);
57 pLbDoc->Clear();
58 assert(pLbDoc->GetEntryCount() == 0);
60 get(pLbTable, "insertBefore");
61 pLbTable->set_height_request(pLbTable->GetTextHeight() * 8);
62 get(pEdTabName, "newName");
63 get(pFtWarn, "newNameWarn");
64 get(pBtnOk, "ok");
66 msStrTabNameUsed = get<FixedText>("warnunused")->GetText();
67 msStrTabNameEmpty = get<FixedText>("warnempty")->GetText();
68 msStrTabNameInvalid = get<FixedText>("warninvalid")->GetText();
70 Init();
73 //------------------------------------------------------------------------
75 ScMoveTableDlg::~ScMoveTableDlg()
79 //------------------------------------------------------------------------
81 sal_uInt16 ScMoveTableDlg::GetSelectedDocument () const { return nDocument; }
83 SCTAB ScMoveTableDlg::GetSelectedTable () const { return nTable; }
85 bool ScMoveTableDlg::GetCopyTable () const { return bCopyTable; }
87 bool ScMoveTableDlg::GetRenameTable () const { return bRenameTable; }
89 void ScMoveTableDlg::GetTabNameString( OUString& rString ) const
91 rString = pEdTabName->GetText();
94 void ScMoveTableDlg::SetForceCopyTable()
96 pBtnCopy->Check(true);
97 pBtnMove->Disable();
98 pBtnCopy->Disable();
101 void ScMoveTableDlg::EnableCopyTable(sal_Bool bFlag)
103 if(bFlag)
104 pBtnCopy->Enable();
105 else
106 pBtnCopy->Disable();
109 void ScMoveTableDlg::EnableRenameTable(sal_Bool bFlag)
111 bRenameTable = bFlag;
112 pEdTabName->Enable(bFlag);
113 ResetRenameInput();
116 void ScMoveTableDlg::ResetRenameInput()
118 if (mbEverEdited)
120 // Don't reset the name when the sheet name has ever been edited.
121 // But check the name, as this is also called for change of copy/move
122 // buttons and document listbox selection.
123 CheckNewTabName();
124 return;
127 if (!pEdTabName->IsEnabled())
129 pEdTabName->SetText(OUString());
130 return;
133 bool bVal = pBtnCopy->IsChecked();
134 if (bVal)
136 // copy
137 ScDocument* pDoc = GetSelectedDoc();
138 if (pDoc)
140 OUString aStr = maDefaultName;
141 pDoc->CreateValidTabName(aStr);
142 pEdTabName->SetText(aStr);
144 else
145 pEdTabName->SetText(maDefaultName);
147 else
148 // move
149 pEdTabName->SetText(maDefaultName);
151 CheckNewTabName();
154 void ScMoveTableDlg::CheckNewTabName()
156 const OUString aNewName = pEdTabName->GetText();
157 if (aNewName.isEmpty())
159 // New sheet name is empty. This is not good.
160 pFtWarn->Show(sal_True);
161 pFtWarn->SetControlBackground(Color(COL_YELLOW));
162 pFtWarn->SetText(msStrTabNameEmpty);
163 pBtnOk->Disable();
164 return;
167 if (!ScDocument::ValidTabName(aNewName))
169 // New sheet name contains invalid characters.
170 pFtWarn->Show(sal_True);
171 pFtWarn->SetControlBackground(Color(COL_YELLOW));
172 pFtWarn->SetText(msStrTabNameInvalid);
173 pBtnOk->Disable();
174 return;
177 bool bMoveInCurrentDoc = (pBtnMove->IsChecked() && IsCurrentDocSelected());
178 bool bFound = false;
179 sal_uInt16 nLast = pLbTable->GetEntryCount() - 1;
180 for ( sal_uInt16 i=0; i<=nLast && !bFound; ++i )
182 if ( aNewName.equals(pLbTable->GetEntry(i)) )
184 // Only for move within same document the same name is allowed.
185 if (!bMoveInCurrentDoc || !maDefaultName.equals( pEdTabName->GetText()))
186 bFound = true;
190 if ( bFound )
192 pFtWarn->Show(sal_True);
193 pFtWarn->SetControlBackground(Color(COL_YELLOW));
194 pFtWarn->SetText(msStrTabNameUsed);
195 pBtnOk->Disable();
197 else
199 pFtWarn->Hide(sal_True);
200 pFtWarn->SetControlBackground();
201 pFtWarn->SetText(OUString());
202 pBtnOk->Enable();
206 ScDocument* ScMoveTableDlg::GetSelectedDoc()
208 sal_uInt16 nPos = pLbDoc->GetSelectEntryPos();
209 return static_cast<ScDocument*>(pLbDoc->GetEntryData(nPos));
212 bool ScMoveTableDlg::IsCurrentDocSelected() const
214 return pLbDoc->GetSelectEntryPos() == mnCurrentDocPos;
217 //------------------------------------------------------------------------
219 void ScMoveTableDlg::Init()
221 pBtnOk->SetClickHdl ( LINK( this, ScMoveTableDlg, OkHdl ) );
222 pLbDoc->SetSelectHdl ( LINK( this, ScMoveTableDlg, SelHdl ) );
223 pBtnCopy->SetToggleHdl( LINK( this, ScMoveTableDlg, CheckBtnHdl ) );
224 pEdTabName->SetModifyHdl( LINK( this, ScMoveTableDlg, CheckNameHdl ) );
225 pBtnMove->Check( true );
226 pBtnCopy->Check( false );
227 pEdTabName->Enable(false);
228 pFtWarn->Hide(sal_True);
229 InitDocListBox();
230 SelHdl( pLbDoc );
233 //------------------------------------------------------------------------
235 void ScMoveTableDlg::InitDocListBox()
237 SfxObjectShell* pSh = SfxObjectShell::GetFirst();
238 ScDocShell* pScSh = NULL;
239 sal_uInt16 nSelPos = 0;
240 sal_uInt16 i = 0;
241 OUString aEntryName;
243 pLbDoc->Clear();
244 pLbDoc->SetUpdateMode( false );
246 while ( pSh )
248 pScSh = PTR_CAST( ScDocShell, pSh );
250 if ( pScSh )
252 aEntryName = pScSh->GetTitle();
254 if ( pScSh == SfxObjectShell::Current() )
256 mnCurrentDocPos = nSelPos = i;
257 aEntryName += " ";
258 aEntryName += msCurrentDoc;
261 pLbDoc->InsertEntry( aEntryName, i );
262 pLbDoc->SetEntryData( i, (void*)pScSh->GetDocument() );
264 i++;
266 pSh = SfxObjectShell::GetNext( *pSh );
269 pLbDoc->SetUpdateMode( sal_True );
270 pLbDoc->InsertEntry(msNewDoc);
271 pLbDoc->SelectEntryPos( nSelPos );
274 //------------------------------------------------------------------------
275 // Handler:
277 IMPL_LINK( ScMoveTableDlg, CheckBtnHdl, void *, pBtn )
279 if (pBtn == pBtnCopy)
280 ResetRenameInput();
282 return 0;
285 IMPL_LINK_NOARG(ScMoveTableDlg, OkHdl)
287 sal_uInt16 nDocSel = pLbDoc->GetSelectEntryPos();
288 sal_uInt16 nDocLast = pLbDoc->GetEntryCount()-1;
289 sal_uInt16 nTabSel = pLbTable->GetSelectEntryPos();
290 sal_uInt16 nTabLast = pLbTable->GetEntryCount()-1;
292 nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
293 nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
294 bCopyTable = pBtnCopy->IsChecked();
296 if (bCopyTable)
298 // Return an empty string when the new name is the same as the
299 // automatic name assigned by the document.
300 OUString aCopyName = maDefaultName;
301 ScDocument* pDoc = GetSelectedDoc();
302 if (pDoc)
303 pDoc->CreateValidTabName(aCopyName);
304 if (aCopyName == pEdTabName->GetText())
305 pEdTabName->SetText( OUString() );
307 else
309 // Return an empty string, when the new name is the same as the
310 // original name.
311 if (maDefaultName.equals(pEdTabName->GetText()))
312 pEdTabName->SetText(OUString());
315 EndDialog( RET_OK );
317 return 0;
320 IMPL_LINK( ScMoveTableDlg, SelHdl, ListBox *, pLb )
322 if ( pLb == pLbDoc )
324 ScDocument* pDoc = GetSelectedDoc();
325 OUString aName;
327 pLbTable->Clear();
328 pLbTable->SetUpdateMode( false );
329 if ( pDoc )
331 SCTAB nLast = pDoc->GetTableCount()-1;
332 for ( SCTAB i=0; i<=nLast; i++ )
334 pDoc->GetName( i, aName );
335 pLbTable->InsertEntry( aName, static_cast<sal_uInt16>(i) );
338 pLbTable->InsertEntry( ScGlobal::GetRscString(STR_MOVE_TO_END) );
339 pLbTable->SetUpdateMode( sal_True );
340 pLbTable->SelectEntryPos( 0 );
341 ResetRenameInput();
344 return 0;
347 IMPL_LINK( ScMoveTableDlg, CheckNameHdl, Edit *, pEdt )
349 if ( pEdt == pEdTabName )
351 mbEverEdited = true;
352 CheckNewTabName();
355 return 0;
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */