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