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 .
20 #undef SC_DLLIMPLEMENTATION
22 #include <vcl/msgbox.hxx>
24 #include "mvtabdlg.hxx"
25 #include "document.hxx"
27 #include "miscdlgs.hrc"
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
),
40 bRenameTable( 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);
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");
59 msStrTabNameUsed
= get
<FixedText
>("warnunused")->GetText();
60 msStrTabNameEmpty
= get
<FixedText
>("warnempty")->GetText();
61 msStrTabNameInvalid
= get
<FixedText
>("warninvalid")->GetText();
66 ScMoveTableDlg::~ScMoveTableDlg()
71 void ScMoveTableDlg::dispose()
80 ModalDialog::dispose();
83 void ScMoveTableDlg::GetTabNameString( OUString
& rString
) const
85 rString
= pEdTabName
->GetText();
88 void ScMoveTableDlg::SetForceCopyTable()
95 void ScMoveTableDlg::EnableRenameTable(bool bFlag
)
98 pEdTabName
->Enable(bFlag
);
102 void ScMoveTableDlg::ResetRenameInput()
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.
113 if (!pEdTabName
->IsEnabled())
115 pEdTabName
->SetText(OUString());
119 bool bVal
= pBtnCopy
->IsChecked();
123 ScDocument
* pDoc
= GetSelectedDoc();
126 OUString aStr
= maDefaultName
;
127 pDoc
->CreateValidTabName(aStr
);
128 pEdTabName
->SetText(aStr
);
131 pEdTabName
->SetText(maDefaultName
);
135 pEdTabName
->SetText(maDefaultName
);
140 void ScMoveTableDlg::CheckNewTabName()
142 const OUString aNewName
= pEdTabName
->GetText();
143 if (aNewName
.isEmpty())
145 // New sheet name is empty. This is not good.
147 pFtWarn
->SetControlBackground(Color(COL_YELLOW
));
148 pFtWarn
->SetText(msStrTabNameEmpty
);
153 if (!ScDocument::ValidTabName(aNewName
))
155 // New sheet name contains invalid characters.
157 pFtWarn
->SetControlBackground(Color(COL_YELLOW
));
158 pFtWarn
->SetText(msStrTabNameInvalid
);
163 bool bMoveInCurrentDoc
= (pBtnMove
->IsChecked() && IsCurrentDocSelected());
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()))
179 pFtWarn
->SetControlBackground(Color(COL_YELLOW
));
180 pFtWarn
->SetText(msStrTabNameUsed
);
186 pFtWarn
->SetControlBackground();
187 pFtWarn
->SetText(OUString());
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
) );
210 pBtnCopy
->Check( false );
211 pEdTabName
->Enable(false);
214 SelHdl( *pLbDoc
.get() );
217 void ScMoveTableDlg::InitDocListBox()
219 SfxObjectShell
* pSh
= SfxObjectShell::GetFirst();
220 ScDocShell
* pScSh
= nullptr;
221 sal_uInt16 nSelPos
= 0;
226 pLbDoc
->SetUpdateMode( false );
230 pScSh
= dynamic_cast<ScDocShell
*>( pSh
);
234 aEntryName
= pScSh
->GetTitle();
236 if ( pScSh
== SfxObjectShell::Current() )
238 mnCurrentDocPos
= nSelPos
= i
;
240 aEntryName
+= msCurrentDoc
;
243 pLbDoc
->InsertEntry( aEntryName
, i
);
244 pLbDoc
->SetEntryData( i
, static_cast<void*>(&pScSh
->GetDocument()) );
248 pSh
= SfxObjectShell::GetNext( *pSh
);
251 pLbDoc
->SetUpdateMode( true );
252 pLbDoc
->InsertEntry(msNewDoc
);
253 pLbDoc
->SelectEntryPos( nSelPos
);
258 IMPL_LINK_TYPED( ScMoveTableDlg
, CheckBtnHdl
, RadioButton
&, rBtn
, void )
260 if (&rBtn
== pBtnCopy
)
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();
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();
282 pDoc
->CreateValidTabName(aCopyName
);
283 if (aCopyName
== pEdTabName
->GetText())
284 pEdTabName
->SetText( OUString() );
288 // Return an empty string, when the new name is the same as the
290 if (maDefaultName
.equals(pEdTabName
->GetText()))
291 pEdTabName
->SetText(OUString());
297 IMPL_LINK_TYPED( ScMoveTableDlg
, SelHdl
, ListBox
&, rLb
, void )
299 if ( &rLb
== pLbDoc
)
301 ScDocument
* pDoc
= GetSelectedDoc();
305 pLbTable
->SetUpdateMode( false );
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 );
322 IMPL_LINK_TYPED( ScMoveTableDlg
, CheckNameHdl
, Edit
&, rEdt
, void )
324 if ( &rEdt
== pEdTabName
)
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */