bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / miscdlgs / mvtabdlg.cxx
blob65a1c9107742484e533a8734b1f364dbfaf47031
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, ScResId( RID_SCDLG_MOVETAB ) ),
43 aFlAction ( this, ScResId( FL_ACTION ) ),
44 aBtnMove ( this, ScResId( BTN_MOVE ) ),
45 aBtnCopy ( this, ScResId( BTN_COPY ) ),
46 aFlLocation ( this, ScResId( FL_LOCATION ) ),
47 aFtDoc ( this, ScResId( FT_DEST ) ),
48 aLbDoc ( this, ScResId( LB_DEST ) ),
49 aFtTable ( this, ScResId( FT_INSERT ) ),
50 aLbTable ( this, ScResId( LB_INSERT ) ),
51 aFlName ( this, ScResId( FL_NAME ) ),
52 aFtTabName ( this, ScResId( FT_TABNAME ) ),
53 aEdTabName ( this, ScResId( ED_INPUT ) ),
54 aFtWarn ( this, ScResId( FT_TABNAME_WARN ) ),
55 aBtnOk ( this, ScResId( BTN_OK ) ),
56 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
57 aBtnHelp ( this, ScResId( BTN_HELP ) ),
59 maStrTabNameUsed( SC_RESSTR(STR_TABNAME_WARN_USED) ),
60 maStrTabNameEmpty( SC_RESSTR(STR_TABNAME_WARN_EMPTY) ),
61 maStrTabNameInvalid( SC_RESSTR(STR_TABNAME_WARN_INVALID) ),
63 maDefaultName( rDefault ),
64 mnCurrentDocPos( 0 ),
65 nDocument ( 0 ),
66 nTable ( 0 ),
67 bCopyTable ( false ),
68 bRenameTable( false ),
69 mbEverEdited( false )
71 Init();
72 FreeResource();
75 //------------------------------------------------------------------------
77 ScMoveTableDlg::~ScMoveTableDlg()
81 //------------------------------------------------------------------------
83 sal_uInt16 ScMoveTableDlg::GetSelectedDocument () const { return nDocument; }
85 SCTAB ScMoveTableDlg::GetSelectedTable () const { return nTable; }
87 bool ScMoveTableDlg::GetCopyTable () const { return bCopyTable; }
89 bool ScMoveTableDlg::GetRenameTable () const { return bRenameTable; }
91 void ScMoveTableDlg::GetTabNameString( OUString& rString ) const
93 rString = aEdTabName.GetText();
96 void ScMoveTableDlg::SetForceCopyTable()
98 aBtnCopy.Check(true);
99 aBtnMove.Disable();
100 aBtnCopy.Disable();
103 void ScMoveTableDlg::EnableCopyTable(sal_Bool bFlag)
105 if(bFlag)
106 aBtnCopy.Enable();
107 else
108 aBtnCopy.Disable();
111 void ScMoveTableDlg::EnableRenameTable(sal_Bool bFlag)
113 bRenameTable = bFlag;
114 aEdTabName.Enable(bFlag);
115 aFtTabName.Enable(bFlag);
116 ResetRenameInput();
119 void ScMoveTableDlg::ResetRenameInput()
121 if (mbEverEdited)
123 // Don't reset the name when the sheet name has ever been edited.
124 // But check the name, as this is also called for change of copy/move
125 // buttons and document listbox selection.
126 CheckNewTabName();
127 return;
130 if (!aEdTabName.IsEnabled())
132 aEdTabName.SetText(String());
133 return;
136 bool bVal = aBtnCopy.IsChecked();
137 if (bVal)
139 // copy
140 ScDocument* pDoc = GetSelectedDoc();
141 if (pDoc)
143 OUString aStr = maDefaultName;
144 pDoc->CreateValidTabName(aStr);
145 aEdTabName.SetText(aStr);
147 else
148 aEdTabName.SetText(maDefaultName);
150 else
151 // move
152 aEdTabName.SetText(maDefaultName);
154 CheckNewTabName();
157 void ScMoveTableDlg::CheckNewTabName()
159 const OUString aNewName = aEdTabName.GetText();
160 if (aNewName.isEmpty())
162 // New sheet name is empty. This is not good.
163 aFtWarn.SetText(maStrTabNameEmpty);
164 aFtWarn.Show();
165 aBtnOk.Disable();
166 return;
169 if (!ScDocument::ValidTabName(aNewName))
171 // New sheet name contains invalid characters.
172 aFtWarn.SetText(maStrTabNameInvalid);
173 aFtWarn.Show();
174 aBtnOk.Disable();
175 return;
178 bool bMoveInCurrentDoc = (aBtnMove.IsChecked() && IsCurrentDocSelected());
179 bool bFound = false;
180 sal_uInt16 nLast = aLbTable.GetEntryCount() - 1;
181 for ( sal_uInt16 i=0; i<=nLast && !bFound; ++i )
183 if ( aNewName.equals(aLbTable.GetEntry(i)) )
185 // Only for move within same document the same name is allowed.
186 if (!bMoveInCurrentDoc || !maDefaultName.equals( aEdTabName.GetText()))
187 bFound = true;
191 if ( bFound )
193 aFtWarn.SetText(maStrTabNameUsed);
194 aFtWarn.Show();
195 aBtnOk.Disable();
197 else
199 aFtWarn.Hide();
200 aBtnOk.Enable();
204 ScDocument* ScMoveTableDlg::GetSelectedDoc()
206 sal_uInt16 nPos = aLbDoc.GetSelectEntryPos();
207 return static_cast<ScDocument*>(aLbDoc.GetEntryData(nPos));
210 bool ScMoveTableDlg::IsCurrentDocSelected() const
212 return aLbDoc.GetSelectEntryPos() == mnCurrentDocPos;
215 //------------------------------------------------------------------------
217 void ScMoveTableDlg::Init()
219 aBtnOk.SetClickHdl ( LINK( this, ScMoveTableDlg, OkHdl ) );
220 aLbDoc.SetSelectHdl ( LINK( this, ScMoveTableDlg, SelHdl ) );
221 aBtnCopy.SetToggleHdl( LINK( this, ScMoveTableDlg, CheckBtnHdl ) );
222 aEdTabName.SetModifyHdl( LINK( this, ScMoveTableDlg, CheckNameHdl ) );
223 aBtnMove.Check( true );
224 aBtnCopy.Check( false );
225 aEdTabName.Enable(false);
226 aFtWarn.SetControlBackground( Color( COL_YELLOW ) );
227 aFtWarn.Hide();
228 InitDocListBox();
229 SelHdl( &aLbDoc );
232 //------------------------------------------------------------------------
234 void ScMoveTableDlg::InitDocListBox()
236 SfxObjectShell* pSh = SfxObjectShell::GetFirst();
237 ScDocShell* pScSh = NULL;
238 sal_uInt16 nSelPos = 0;
239 sal_uInt16 i = 0;
240 String aEntryName;
242 aLbDoc.Clear();
243 aLbDoc.SetUpdateMode( false );
245 while ( pSh )
247 pScSh = PTR_CAST( ScDocShell, pSh );
249 if ( pScSh )
251 aEntryName = pScSh->GetTitle();
253 if ( pScSh == SfxObjectShell::Current() )
255 mnCurrentDocPos = nSelPos = i;
256 aEntryName += sal_Unicode( ' ' );
257 aEntryName += String( ScResId( STR_CURRENTDOC ) );
260 aLbDoc.InsertEntry( aEntryName, i );
261 aLbDoc.SetEntryData( i, (void*)pScSh->GetDocument() );
263 i++;
265 pSh = SfxObjectShell::GetNext( *pSh );
268 aLbDoc.SetUpdateMode( sal_True );
269 aLbDoc.InsertEntry( String( ScResId( STR_NEWDOC ) ) );
270 aLbDoc.SelectEntryPos( nSelPos );
273 //------------------------------------------------------------------------
274 // Handler:
276 IMPL_LINK( ScMoveTableDlg, CheckBtnHdl, void *, pBtn )
278 if (pBtn == &aBtnCopy)
279 ResetRenameInput();
281 return 0;
284 IMPL_LINK_NOARG(ScMoveTableDlg, OkHdl)
286 sal_uInt16 nDocSel = aLbDoc.GetSelectEntryPos();
287 sal_uInt16 nDocLast = aLbDoc.GetEntryCount()-1;
288 sal_uInt16 nTabSel = aLbTable.GetSelectEntryPos();
289 sal_uInt16 nTabLast = aLbTable.GetEntryCount()-1;
291 nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
292 nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
293 bCopyTable = aBtnCopy.IsChecked();
295 if (bCopyTable)
297 // Return an empty string when the new name is the same as the
298 // automatic name assigned by the document.
299 OUString aCopyName = maDefaultName;
300 ScDocument* pDoc = GetSelectedDoc();
301 if (pDoc)
302 pDoc->CreateValidTabName(aCopyName);
303 if (aCopyName == OUString(aEdTabName.GetText()))
304 aEdTabName.SetText( OUString() );
306 else
308 // Return an empty string, when the new name is the same as the
309 // original name.
310 if (maDefaultName.equals(aEdTabName.GetText()))
311 aEdTabName.SetText(OUString());
314 EndDialog( RET_OK );
316 return 0;
319 IMPL_LINK( ScMoveTableDlg, SelHdl, ListBox *, pLb )
321 if ( pLb == &aLbDoc )
323 ScDocument* pDoc = GetSelectedDoc();
324 OUString aName;
326 aLbTable.Clear();
327 aLbTable.SetUpdateMode( false );
328 if ( pDoc )
330 SCTAB nLast = pDoc->GetTableCount()-1;
331 for ( SCTAB i=0; i<=nLast; i++ )
333 pDoc->GetName( i, aName );
334 aLbTable.InsertEntry( aName, static_cast<sal_uInt16>(i) );
337 aLbTable.InsertEntry( ScGlobal::GetRscString(STR_MOVE_TO_END) );
338 aLbTable.SetUpdateMode( sal_True );
339 aLbTable.SelectEntryPos( 0 );
340 ResetRenameInput();
343 return 0;
346 IMPL_LINK( ScMoveTableDlg, CheckNameHdl, Edit *, pEdt )
348 if ( pEdt == &aEdTabName )
350 mbEverEdited = true;
351 CheckNewTabName();
354 return 0;
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */