1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
32 #include "miscdlgs.hrc"
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
),
68 bRenameTable( false ),
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()
103 void ScMoveTableDlg::EnableCopyTable(sal_Bool bFlag
)
111 void ScMoveTableDlg::EnableRenameTable(sal_Bool bFlag
)
113 bRenameTable
= bFlag
;
114 aEdTabName
.Enable(bFlag
);
115 aFtTabName
.Enable(bFlag
);
119 void ScMoveTableDlg::ResetRenameInput()
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.
130 if (!aEdTabName
.IsEnabled())
132 aEdTabName
.SetText(String());
136 bool bVal
= aBtnCopy
.IsChecked();
140 ScDocument
* pDoc
= GetSelectedDoc();
143 OUString aStr
= maDefaultName
;
144 pDoc
->CreateValidTabName(aStr
);
145 aEdTabName
.SetText(aStr
);
148 aEdTabName
.SetText(maDefaultName
);
152 aEdTabName
.SetText(maDefaultName
);
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
);
169 if (!ScDocument::ValidTabName(aNewName
))
171 // New sheet name contains invalid characters.
172 aFtWarn
.SetText(maStrTabNameInvalid
);
178 bool bMoveInCurrentDoc
= (aBtnMove
.IsChecked() && IsCurrentDocSelected());
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()))
193 aFtWarn
.SetText(maStrTabNameUsed
);
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
) );
232 //------------------------------------------------------------------------
234 void ScMoveTableDlg::InitDocListBox()
236 SfxObjectShell
* pSh
= SfxObjectShell::GetFirst();
237 ScDocShell
* pScSh
= NULL
;
238 sal_uInt16 nSelPos
= 0;
243 aLbDoc
.SetUpdateMode( false );
247 pScSh
= PTR_CAST( ScDocShell
, pSh
);
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() );
265 pSh
= SfxObjectShell::GetNext( *pSh
);
268 aLbDoc
.SetUpdateMode( sal_True
);
269 aLbDoc
.InsertEntry( String( ScResId( STR_NEWDOC
) ) );
270 aLbDoc
.SelectEntryPos( nSelPos
);
273 //------------------------------------------------------------------------
276 IMPL_LINK( ScMoveTableDlg
, CheckBtnHdl
, void *, pBtn
)
278 if (pBtn
== &aBtnCopy
)
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();
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();
302 pDoc
->CreateValidTabName(aCopyName
);
303 if (aCopyName
== OUString(aEdTabName
.GetText()))
304 aEdTabName
.SetText( OUString() );
308 // Return an empty string, when the new name is the same as the
310 if (maDefaultName
.equals(aEdTabName
.GetText()))
311 aEdTabName
.SetText(OUString());
319 IMPL_LINK( ScMoveTableDlg
, SelHdl
, ListBox
*, pLb
)
321 if ( pLb
== &aLbDoc
)
323 ScDocument
* pDoc
= GetSelectedDoc();
327 aLbTable
.SetUpdateMode( false );
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 );
346 IMPL_LINK( ScMoveTableDlg
, CheckNameHdl
, Edit
*, pEdt
)
348 if ( pEdt
== &aEdTabName
)
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */