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 <mvtabdlg.hxx>
23 #include <document.hxx>
25 #include <globstr.hrc>
26 #include <scresid.hxx>
27 #include <comphelper/lok.hxx>
29 #include <tabvwsh.hxx>
31 ScMoveTableDlg::ScMoveTableDlg(weld::Window
* pParent
, OUString aDefault
)
32 : GenericDialogController(pParent
, u
"modules/scalc/ui/movecopysheet.ui"_ustr
,
33 u
"MoveCopySheetDialog"_ustr
)
34 , maDefaultName(std::move(aDefault
))
41 , m_xBtnMove(m_xBuilder
->weld_radio_button(u
"move"_ustr
))
42 , m_xBtnCopy(m_xBuilder
->weld_radio_button(u
"copy"_ustr
))
43 , m_xFtDoc(m_xBuilder
->weld_label(u
"toDocumentLabel"_ustr
))
44 , m_xLbDoc(m_xBuilder
->weld_combo_box(u
"toDocument"_ustr
))
45 , m_xLbTable(m_xBuilder
->weld_tree_view(u
"insertBefore"_ustr
))
46 , m_xEdTabName(m_xBuilder
->weld_entry(u
"newName"_ustr
))
47 , m_xFtWarn(m_xBuilder
->weld_label(u
"newNameWarn"_ustr
))
48 , m_xBtnOk(m_xBuilder
->weld_button(u
"ok"_ustr
))
49 , m_xUnusedLabel(m_xBuilder
->weld_label(u
"warnunused"_ustr
))
50 , m_xEmptyLabel(m_xBuilder
->weld_label(u
"warnempty"_ustr
))
51 , m_xInvalidLabel(m_xBuilder
->weld_label(u
"warninvalid"_ustr
))
53 assert(m_xLbDoc
->get_count() == 2);
54 msCurrentDoc
= m_xLbDoc
->get_text(0);
55 msNewDoc
= m_xLbDoc
->get_text(1);
57 assert(m_xLbDoc
->get_count() == 0);
59 m_xLbTable
->set_size_request(-1, m_xLbTable
->get_height_rows(8));
61 msStrTabNameUsed
= m_xUnusedLabel
->get_label();
62 msStrTabNameEmpty
= m_xEmptyLabel
->get_label();
63 msStrTabNameInvalid
= m_xInvalidLabel
->get_label();
68 ScMoveTableDlg::~ScMoveTableDlg() {}
70 void ScMoveTableDlg::GetTabNameString(OUString
& rString
) const
72 rString
= m_xEdTabName
->get_text();
75 void ScMoveTableDlg::SetForceCopyTable()
77 m_xBtnCopy
->set_active(true);
78 m_xBtnMove
->set_sensitive(false);
82 void ScMoveTableDlg::EnableRenameTable(bool bFlag
)
85 m_xEdTabName
->set_sensitive(bFlag
);
89 void ScMoveTableDlg::ResetRenameInput()
93 // Don't reset the name when the sheet name has ever been edited.
94 // But check the name, as this is also called for change of copy/move
95 // buttons and document listbox selection.
100 if (!m_xEdTabName
->get_sensitive())
102 m_xEdTabName
->set_text(OUString());
106 bool bVal
= m_xBtnCopy
->get_active();
110 ScDocument
* pDoc
= GetSelectedDoc();
113 OUString aStr
= maDefaultName
;
114 pDoc
->CreateValidTabName(aStr
);
115 m_xEdTabName
->set_text(aStr
);
118 m_xEdTabName
->set_text(maDefaultName
);
123 m_xEdTabName
->set_text(maDefaultName
);
129 void ScMoveTableDlg::CheckNewTabName()
131 const OUString aNewName
= m_xEdTabName
->get_text();
132 if (aNewName
.isEmpty())
134 // New sheet name is empty. This is not good.
136 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
137 m_xFtWarn
->set_label(msStrTabNameEmpty
);
138 m_xBtnOk
->set_sensitive(false);
142 if (!ScDocument::ValidTabName(aNewName
))
144 // New sheet name contains invalid characters.
146 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
147 m_xFtWarn
->set_label(msStrTabNameInvalid
);
148 m_xBtnOk
->set_sensitive(false);
152 bool bMoveInCurrentDoc
= m_xBtnMove
->get_active() && m_xLbDoc
->get_active() == mnCurrentDocPos
;
154 const int nLast
= m_xLbTable
->n_children();
155 for (int i
= 0; i
< nLast
&& !bFound
; ++i
)
157 if (aNewName
== m_xLbTable
->get_text(i
))
159 // Only for move within same document the same name is allowed.
160 if (!bMoveInCurrentDoc
|| maDefaultName
!= m_xEdTabName
->get_text())
168 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
169 m_xFtWarn
->set_label(msStrTabNameUsed
);
170 m_xBtnOk
->set_sensitive(false);
175 //TODO m_xFtWarn->SetControlBackground();
176 m_xFtWarn
->set_label(OUString());
177 m_xBtnOk
->set_sensitive(true);
181 ScDocument
* ScMoveTableDlg::GetSelectedDoc()
183 return weld::fromId
<ScDocument
*>(m_xLbDoc
->get_active_id());
186 void ScMoveTableDlg::Init()
188 m_xBtnOk
->connect_clicked(LINK(this, ScMoveTableDlg
, OkHdl
));
189 m_xLbDoc
->connect_changed(LINK(this, ScMoveTableDlg
, SelHdl
));
190 m_xBtnCopy
->connect_toggled(LINK(this, ScMoveTableDlg
, CheckBtnHdl
));
191 m_xBtnMove
->connect_toggled(LINK(this, ScMoveTableDlg
, CheckBtnHdl
));
192 m_xEdTabName
->connect_changed(LINK(this, ScMoveTableDlg
, CheckNameHdl
));
194 // tdf#96854 - remember last used option for copy/move sheet
195 bool bIsCopyActive
= false;
196 if (ScTabViewShell
* pViewSh
= ScTabViewShell::GetActiveViewShell())
197 bIsCopyActive
= pViewSh
->GetViewData().GetOptions().GetOption(VOPT_COPY_SHEET
);
198 m_xBtnMove
->set_active(!bIsCopyActive
);
199 m_xBtnCopy
->set_active(bIsCopyActive
);
200 m_xEdTabName
->set_sensitive(false);
204 if (comphelper::LibreOfficeKit::isActive())
212 void ScMoveTableDlg::InitDocListBox()
214 SfxObjectShell
* pSh
= SfxObjectShell::GetFirst();
215 ScDocShell
* pScSh
= nullptr;
216 sal_uInt16 nSelPos
= 0;
224 pScSh
= dynamic_cast<ScDocShell
*>(pSh
);
228 OUString aEntryName
= pScSh
->GetTitle();
230 if (pScSh
== SfxObjectShell::Current())
232 mnCurrentDocPos
= nSelPos
= i
;
233 aEntryName
+= " " + msCurrentDoc
;
236 OUString
sId(weld::toId(&pScSh
->GetDocument()));
237 m_xLbDoc
->insert(i
, aEntryName
, &sId
, nullptr, nullptr);
241 pSh
= SfxObjectShell::GetNext(*pSh
);
245 m_xLbDoc
->append_text(msNewDoc
);
246 m_xLbDoc
->set_active(nSelPos
);
249 void ScMoveTableDlg::SetOkBtnLabel()
251 const bool bIsCopyActive
= m_xBtnCopy
->get_active();
252 // tdf#139464 Write "Copy" or "Move" on OK button
253 m_xBtnOk
->set_label(bIsCopyActive
? m_xBtnCopy
->get_label() : m_xBtnMove
->get_label());
254 // tdf#96854 - remember last used option for copy/move sheet
255 if (ScTabViewShell
* pScViewShell
= ScTabViewShell::GetActiveViewShell())
257 ScViewOptions
aViewOpt(pScViewShell
->GetViewData().GetOptions());
258 aViewOpt
.SetOption(VOPT_COPY_SHEET
, bIsCopyActive
);
259 pScViewShell
->GetViewData().SetOptions(aViewOpt
);
265 IMPL_LINK(ScMoveTableDlg
, CheckBtnHdl
, weld::Toggleable
&, rBtn
, void)
267 if (&rBtn
== m_xBtnCopy
.get())
272 IMPL_LINK_NOARG(ScMoveTableDlg
, OkHdl
, weld::Button
&, void)
274 const sal_Int32 nDocSel
= m_xLbDoc
->get_active();
275 const sal_Int32 nDocLast
= m_xLbDoc
->get_count() - 1;
276 const sal_Int32 nTabSel
= m_xLbTable
->get_selected_index();
277 const sal_Int32 nTabLast
= m_xLbTable
->n_children() - 1;
279 nDocument
= (nDocSel
!= nDocLast
) ? nDocSel
: SC_DOC_NEW
;
280 nTable
= (nTabSel
!= nTabLast
) ? static_cast<SCTAB
>(nTabSel
) : SC_TAB_APPEND
;
281 bCopyTable
= m_xBtnCopy
->get_active();
285 // Return an empty string when the new name is the same as the
286 // automatic name assigned by the document.
287 OUString aCopyName
= maDefaultName
;
288 ScDocument
* pDoc
= GetSelectedDoc();
290 pDoc
->CreateValidTabName(aCopyName
);
291 if (aCopyName
== m_xEdTabName
->get_text())
292 m_xEdTabName
->set_text(OUString());
296 // Return an empty string, when the new name is the same as the
298 if (maDefaultName
== m_xEdTabName
->get_text())
299 m_xEdTabName
->set_text(OUString());
302 m_xDialog
->response(RET_OK
);
305 IMPL_LINK_NOARG(ScMoveTableDlg
, SelHdl
, weld::ComboBox
&, void)
307 ScDocument
* pDoc
= GetSelectedDoc();
311 m_xLbTable
->freeze();
314 SCTAB nLast
= pDoc
->GetTableCount() - 1;
315 for (SCTAB i
= 0; i
<= nLast
; ++i
)
317 pDoc
->GetName(i
, aName
);
318 m_xLbTable
->append_text(aName
);
321 m_xLbTable
->append_text(ScResId(STR_MOVE_TO_END
));
323 m_xLbTable
->select(0);
327 IMPL_LINK_NOARG(ScMoveTableDlg
, CheckNameHdl
, weld::Entry
&, void)
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */