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
, "MoveCopySheetDialog", "modules/scalc/ui/movecopysheet.ui" ),
42 maDefaultName( rDefault
),
47 bRenameTable( 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);
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");
66 msStrTabNameUsed
= get
<FixedText
>("warnunused")->GetText();
67 msStrTabNameEmpty
= get
<FixedText
>("warnempty")->GetText();
68 msStrTabNameInvalid
= get
<FixedText
>("warninvalid")->GetText();
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);
101 void ScMoveTableDlg::EnableCopyTable(sal_Bool bFlag
)
109 void ScMoveTableDlg::EnableRenameTable(sal_Bool bFlag
)
111 bRenameTable
= bFlag
;
112 pEdTabName
->Enable(bFlag
);
116 void ScMoveTableDlg::ResetRenameInput()
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.
127 if (!pEdTabName
->IsEnabled())
129 pEdTabName
->SetText(OUString());
133 bool bVal
= pBtnCopy
->IsChecked();
137 ScDocument
* pDoc
= GetSelectedDoc();
140 OUString aStr
= maDefaultName
;
141 pDoc
->CreateValidTabName(aStr
);
142 pEdTabName
->SetText(aStr
);
145 pEdTabName
->SetText(maDefaultName
);
149 pEdTabName
->SetText(maDefaultName
);
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
);
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
);
177 bool bMoveInCurrentDoc
= (pBtnMove
->IsChecked() && IsCurrentDocSelected());
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()))
192 pFtWarn
->Show(sal_True
);
193 pFtWarn
->SetControlBackground(Color(COL_YELLOW
));
194 pFtWarn
->SetText(msStrTabNameUsed
);
199 pFtWarn
->Hide(sal_True
);
200 pFtWarn
->SetControlBackground();
201 pFtWarn
->SetText(OUString());
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
);
233 //------------------------------------------------------------------------
235 void ScMoveTableDlg::InitDocListBox()
237 SfxObjectShell
* pSh
= SfxObjectShell::GetFirst();
238 ScDocShell
* pScSh
= NULL
;
239 sal_uInt16 nSelPos
= 0;
244 pLbDoc
->SetUpdateMode( false );
248 pScSh
= PTR_CAST( ScDocShell
, pSh
);
252 aEntryName
= pScSh
->GetTitle();
254 if ( pScSh
== SfxObjectShell::Current() )
256 mnCurrentDocPos
= nSelPos
= i
;
258 aEntryName
+= msCurrentDoc
;
261 pLbDoc
->InsertEntry( aEntryName
, i
);
262 pLbDoc
->SetEntryData( i
, (void*)pScSh
->GetDocument() );
266 pSh
= SfxObjectShell::GetNext( *pSh
);
269 pLbDoc
->SetUpdateMode( sal_True
);
270 pLbDoc
->InsertEntry(msNewDoc
);
271 pLbDoc
->SelectEntryPos( nSelPos
);
274 //------------------------------------------------------------------------
277 IMPL_LINK( ScMoveTableDlg
, CheckBtnHdl
, void *, pBtn
)
279 if (pBtn
== pBtnCopy
)
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();
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();
303 pDoc
->CreateValidTabName(aCopyName
);
304 if (aCopyName
== pEdTabName
->GetText())
305 pEdTabName
->SetText( OUString() );
309 // Return an empty string, when the new name is the same as the
311 if (maDefaultName
.equals(pEdTabName
->GetText()))
312 pEdTabName
->SetText(OUString());
320 IMPL_LINK( ScMoveTableDlg
, SelHdl
, ListBox
*, pLb
)
324 ScDocument
* pDoc
= GetSelectedDoc();
328 pLbTable
->SetUpdateMode( false );
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 );
347 IMPL_LINK( ScMoveTableDlg
, CheckNameHdl
, Edit
*, pEdt
)
349 if ( pEdt
== pEdTabName
)
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */