LibreOffice Module sc (master) 1
instbdlg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#undef SC_DLLIMPLEMENTATION
21
22#include <sfx2/docfile.hxx>
23#include <sfx2/docinsert.hxx>
25#include <svtools/ehdl.hxx>
26#include <svtools/sfxecode.hxx>
27#include <vcl/svapp.hxx>
28#include <vcl/weld.hxx>
29
30#include <docsh.hxx>
31#include <viewdata.hxx>
32#include <globstr.hrc>
33#include <scresid.hxx>
34#include <instbdlg.hxx>
35
36ScInsertTableDlg::ScInsertTableDlg(weld::Window* pParent, ScViewData& rData, SCTAB nTabCount, bool bFromFile)
37 : GenericDialogController(pParent, "modules/scalc/ui/insertsheet.ui", "InsertSheetDialog")
38 , aBrowseTimer("ScInsertTableDlg aBrowseTimer")
39 , rViewData(rData)
40 , rDoc(rData.GetDocument())
41 , pDocShTables(nullptr)
42 , bMustClose(false)
43 , nSelTabIndex(0)
44 , nTableCount(nTabCount)
45 , m_xBtnBefore(m_xBuilder->weld_radio_button("before"))
46 , m_xBtnNew(m_xBuilder->weld_radio_button("new"))
47 , m_xBtnFromFile(m_xBuilder->weld_radio_button("fromfile"))
48 , m_xFtCount(m_xBuilder->weld_label("countft"))
49 , m_xNfCount(m_xBuilder->weld_spin_button("countnf"))
50 , m_xFtName(m_xBuilder->weld_label("nameft"))
51 , m_xEdName(m_xBuilder->weld_entry("nameed"))
52 , m_xLbTables(m_xBuilder->weld_tree_view("tables"))
53 , m_xFtPath(m_xBuilder->weld_label("path"))
54 , m_xBtnBrowse(m_xBuilder->weld_button("browse"))
55 , m_xBtnLink(m_xBuilder->weld_check_button("link"))
56 , m_xBtnOk(m_xBuilder->weld_button("ok"))
57{
58 m_sSheetDotDotDot = m_xEdName->get_text();
59 m_xLbTables->set_size_request(-1, m_xLbTables->get_height_rows(8));
60 Init_Impl(bFromFile);
61}
62
64{
65 if (pDocShTables)
67 pDocInserter.reset();
68}
69
70void ScInsertTableDlg::Init_Impl( bool bFromFile )
71{
72 m_xLbTables->set_selection_mode(SelectionMode::Multiple);
73 m_xBtnBrowse->connect_clicked( LINK( this, ScInsertTableDlg, BrowseHdl_Impl ) );
74 m_xBtnNew->connect_toggled( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
75 m_xBtnFromFile->connect_toggled( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
76 m_xLbTables->connect_changed( LINK( this, ScInsertTableDlg, SelectHdl_Impl ) );
77 m_xNfCount->connect_value_changed( LINK( this, ScInsertTableDlg, CountHdl_Impl));
78 m_xBtnOk->connect_clicked( LINK( this, ScInsertTableDlg, DoEnterHdl ));
79 m_xBtnBefore->set_active(true);
80
81 m_xNfCount->set_max(MAXTAB - rDoc.GetTableCount() + 1);
82 m_xNfCount->set_value(nTableCount);
83
84 if(nTableCount==1)
85 {
86 OUString aName;
88 m_xEdName->set_text( aName );
89 }
90 else
91 {
93 m_xFtName->set_sensitive(false);
94 m_xEdName->set_sensitive(false);
95 }
96
97 bool bShared = rViewData.GetDocShell() && rViewData.GetDocShell()->IsDocShared();
98
99 if ( !bFromFile || bShared )
100 {
101 m_xBtnNew->set_active(true);
103 if ( bShared )
104 {
105 m_xBtnFromFile->set_sensitive(false);
106 }
107 }
108 else
109 {
110 m_xBtnFromFile->set_active(true);
112
113 aBrowseTimer.SetInvokeHandler( LINK( this, ScInsertTableDlg, BrowseTimeoutHdl ) );
115 }
116}
117
119{
120 if (m_xBtnFromFile->get_active())
122
123 return GenericDialogController::run();
124}
125
127{
128 if (!m_xBtnNew->get_active() )
129 return;
130
131 m_xNfCount->set_sensitive(true);
132 m_xFtCount->set_sensitive(true);
133 m_xLbTables->set_sensitive(false);
134 m_xFtPath->set_sensitive(false);
135 m_xBtnBrowse->set_sensitive(false);
136 m_xBtnLink->set_sensitive(false);
137
138 if(nTableCount==1)
139 {
140 m_xEdName->set_sensitive(true);
141 m_xFtName->set_sensitive(true);
142 }
143}
144
146{
147 if (m_xBtnFromFile->get_active() )
148 {
149 m_xEdName->set_sensitive(false);
150 m_xFtName->set_sensitive(false);
151 m_xFtCount->set_sensitive(false);
152 m_xNfCount->set_sensitive(false);
153 m_xLbTables->set_sensitive(true);
154 m_xFtPath->set_sensitive(true);
155 m_xBtnBrowse->set_sensitive(true);
156 m_xBtnLink->set_sensitive(true);
157 }
158}
159
161{
162 m_xLbTables->freeze();
163 m_xLbTables->clear();
164
165 if ( pSrcDoc )
166 {
167 SCTAB nCount = pSrcDoc->GetTableCount();
168 OUString aName;
169
170 for (SCTAB i=0; i<nCount; ++i)
171 {
172 pSrcDoc->GetName( i, aName );
173 m_xLbTables->append_text(aName);
174 }
175 }
176
177 m_xLbTables->thaw();
178
179 if (m_xLbTables->n_children() == 1)
180 m_xLbTables->select(0);
181}
182
183const OUString* ScInsertTableDlg::GetFirstTable( sal_uInt16* pN )
184{
185 const OUString* pStr = nullptr;
186
187 if ( m_xBtnNew->get_active() )
188 {
189 aStrCurSelTable = m_xEdName->get_text();
190 pStr = &aStrCurSelTable;
191 }
192 else
193 {
194 std::vector<int> aRows(m_xLbTables->get_selected_rows());
195 if (nSelTabIndex < aRows.size())
196 {
197 aStrCurSelTable = m_xLbTables->get_text(aRows[0]);
198 pStr = &aStrCurSelTable;
199 if ( pN )
200 *pN = aRows[0];
201 nSelTabIndex = 1;
202 }
203 }
204
205 return pStr;
206}
207
208const OUString* ScInsertTableDlg::GetNextTable( sal_uInt16* pN )
209{
210 if (m_xBtnNew->get_active())
211 return nullptr;
212
213 std::vector<int> aRows(m_xLbTables->get_selected_rows());
214
215 const OUString* pStr = nullptr;
216 if (nSelTabIndex < aRows.size())
217 {
218 aStrCurSelTable = m_xLbTables->get_text(aRows[nSelTabIndex]);
219 pStr = &aStrCurSelTable;
220 if ( pN )
221 *pN = aRows[nSelTabIndex];
222 nSelTabIndex++;
223 }
224
225 return pStr;
226}
227
228// Handler:
229
231{
232 nTableCount = static_cast<SCTAB>(m_xNfCount->get_value());
233 if ( nTableCount==1)
234 {
235 OUString aName;
236 rDoc.CreateValidTabName( aName );
237 m_xEdName->set_text( aName );
238 m_xFtName->set_sensitive(true);
239 m_xEdName->set_sensitive(true);
240 }
241 else
242 {
243 m_xEdName->set_text(m_sSheetDotDotDot);
244 m_xFtName->set_sensitive(false);
245 m_xEdName->set_sensitive(false);
246 }
247
248 DoEnable_Impl();
249}
250
251IMPL_LINK(ScInsertTableDlg, ChoiceHdl_Impl, weld::Toggleable&, rButton, void)
252{
253 if (!rButton.get_active())
254 return;
255
256 if ( m_xBtnNew->get_active() )
257 SetNewTable_Impl();
258 else
259 SetFromTo_Impl();
260
261 DoEnable_Impl();
262}
263
265{
266 pDocInserter.reset();
267 pDocInserter.reset( new ::sfx2::DocumentInserter(m_xDialog.get(), ScDocShell::Factory().GetFactoryName()) );
268 pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
269}
270
272{
273 DoEnable_Impl();
274}
275
277{
278 if ( m_xBtnNew->get_active() || ( pDocShTables && m_xLbTables->count_selected_rows() ) )
279 m_xBtnOk->set_sensitive(true);
280 else
281 m_xBtnOk->set_sensitive(false);
282}
283
285{
286 if (nTableCount > 1 || ScDocument::ValidTabName(m_xEdName->get_text()))
287 {
288 m_xDialog->response(RET_OK);
289 }
290 else
291 {
292 OUString aErrMsg ( ScResId( STR_INVALIDTABNAME ) );
293 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Warning,
294 VclButtonsType::Ok, aErrMsg));
295 xBox->run();
296 }
297}
298
299IMPL_LINK_NOARG(ScInsertTableDlg, BrowseTimeoutHdl, Timer *, void)
300{
301 bMustClose = true;
302 BrowseHdl_Impl(*m_xBtnBrowse);
303}
304
305IMPL_LINK( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
306{
307 if ( ERRCODE_NONE == _pFileDlg->GetError() )
308 {
309 std::unique_ptr<SfxMedium> pMed = pDocInserter->CreateMedium();
310 if ( pMed )
311 {
312 // ERRCTX_SFX_OPENDOC -> "Error loading document"
313 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
314
315 if ( pDocShTables )
316 pDocShTables->DoClose(); // deletion is done when assigning to the reference
317
318 pMed->UseInteractionHandler( true ); // to enable the filter options dialog
319
320 pDocShTables = new ScDocShell;
321 aDocShTablesRef = pDocShTables;
322
323 {
324 weld::WaitObject aWait(m_xDialog.get());
325 pDocShTables->DoLoad(pMed.release());
326 }
327
328 ErrCode nErr = pDocShTables->GetErrorCode();
329 if ( nErr )
330 ErrorHandler::HandleError(nErr, m_xDialog.get()); // warnings, too
331
332 if ( !pDocShTables->GetError() ) // errors only
333 {
334 FillTables_Impl( &pDocShTables->GetDocument() );
335 m_xFtPath->set_label(pDocShTables->GetTitle(SFX_TITLE_FULLNAME));
336 }
337 else
338 {
339 pDocShTables->DoClose();
340 aDocShTablesRef.clear();
341 pDocShTables = nullptr;
342
343 FillTables_Impl( nullptr );
344 m_xFtPath->set_label(OUString());
345 }
346 }
347
348 DoEnable_Impl();
349 }
350 else if ( bMustClose )
351 // execute slot FID_INS_TABLE_EXT and cancel file dialog
352 m_xDialog->response(RET_CANCEL);
353}
354
355/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SCTAB MAXTAB
Definition: address.hxx:70
Reference< XExecutableDialog > m_xDialog
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
static DialogMask HandleError(ErrCode nId, weld::Window *pParent=nullptr, DialogMask nMask=DialogMask::MAX)
static SC_DLLPUBLIC bool ValidTabName(const OUString &rName)
Definition: document.cxx:318
SC_DLLPUBLIC void CreateValidTabName(OUString &rName) const
Definition: document.cxx:375
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:204
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:297
std::unique_ptr< weld::Label > m_xFtPath
Definition: instbdlg.hxx:73
void DoEnable_Impl()
Definition: instbdlg.cxx:276
OUString m_sSheetDotDotDot
Definition: instbdlg.hxx:63
std::unique_ptr< weld::Entry > m_xEdName
Definition: instbdlg.hxx:71
void SetFromTo_Impl()
Definition: instbdlg.cxx:145
OUString aStrCurSelTable
Definition: instbdlg.hxx:61
std::unique_ptr< weld::Label > m_xFtCount
Definition: instbdlg.hxx:68
std::unique_ptr< weld::CheckButton > m_xBtnLink
Definition: instbdlg.hxx:75
std::unique_ptr< weld::RadioButton > m_xBtnNew
Definition: instbdlg.hxx:66
void Init_Impl(bool bFromFile)
Definition: instbdlg.cxx:70
sal_uInt16 nSelTabIndex
Definition: instbdlg.hxx:60
ScViewData & rViewData
Definition: instbdlg.hxx:53
std::unique_ptr< weld::TreeView > m_xLbTables
Definition: instbdlg.hxx:72
virtual ~ScInsertTableDlg() override
Definition: instbdlg.cxx:63
ScDocShell * pDocShTables
Definition: instbdlg.hxx:55
std::unique_ptr< sfx2::DocumentInserter > pDocInserter
Definition: instbdlg.hxx:56
std::unique_ptr< weld::Label > m_xFtName
Definition: instbdlg.hxx:70
void SetNewTable_Impl()
Definition: instbdlg.cxx:126
std::unique_ptr< weld::Button > m_xBtnBrowse
Definition: instbdlg.hxx:74
ScDocument & rDoc
Definition: instbdlg.hxx:54
virtual short run() override
Definition: instbdlg.cxx:118
std::unique_ptr< weld::Button > m_xBtnOk
Definition: instbdlg.hxx:76
std::unique_ptr< weld::RadioButton > m_xBtnBefore
Definition: instbdlg.hxx:65
const OUString * GetNextTable(sal_uInt16 *pN)
Definition: instbdlg.cxx:208
void FillTables_Impl(const ScDocument *pSrcDoc)
Definition: instbdlg.cxx:160
Timer aBrowseTimer
Definition: instbdlg.hxx:52
ScInsertTableDlg(weld::Window *pParent, ScViewData &rViewData, SCTAB nTabCount, bool bFromFile)
Definition: instbdlg.cxx:36
std::unique_ptr< weld::RadioButton > m_xBtnFromFile
Definition: instbdlg.hxx:67
const OUString * GetFirstTable(sal_uInt16 *pN)
Definition: instbdlg.cxx:183
std::unique_ptr< weld::SpinButton > m_xNfCount
Definition: instbdlg.hxx:69
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:354
bool IsDocShared() const
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void Start(bool bStartTimer=true) override
int nCount
#define ERRCODE_NONE
IMPL_LINK(ScInsertTableDlg, ChoiceHdl_Impl, weld::Toggleable &, rButton, void)
Definition: instbdlg.cxx:251
IMPL_LINK_NOARG(ScInsertTableDlg, CountHdl_Impl, weld::SpinButton &, void)
Definition: instbdlg.cxx:230
OUString aName
int i
#define SFX_TITLE_FULLNAME
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
#define ERRCTX_SFX_OPENDOC
sal_Int16 SCTAB
Definition: types.hxx:22
RET_OK
RET_CANCEL