Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sc / source / ui / miscdlgs / instbdlg.cxx
blobc8ef1a6b4bef6a67b54f0c1c9f78c1d08ade416c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sfx2/docfile.hxx>
23 #include <sfx2/docinsert.hxx>
24 #include <sfx2/filedlghelper.hxx>
25 #include <svtools/ehdl.hxx>
26 #include <svtools/sfxecode.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/weld.hxx>
30 #include <docsh.hxx>
31 #include <viewdata.hxx>
32 #include <globstr.hrc>
33 #include <scresid.hxx>
34 #include <instbdlg.hxx>
36 ScInsertTableDlg::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"))
58 m_sSheetDotDotDot = m_xEdName->get_text();
59 m_xLbTables->set_size_request(-1, m_xLbTables->get_height_rows(8));
60 Init_Impl(bFromFile);
63 ScInsertTableDlg::~ScInsertTableDlg()
65 if (pDocShTables)
66 pDocShTables->DoClose();
67 pDocInserter.reset();
70 void ScInsertTableDlg::Init_Impl( bool bFromFile )
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);
81 m_xNfCount->set_max(MAXTAB - rDoc.GetTableCount() + 1);
82 m_xNfCount->set_value(nTableCount);
84 if(nTableCount==1)
86 OUString aName;
87 rDoc.CreateValidTabName( aName );
88 m_xEdName->set_text( aName );
90 else
92 m_xEdName->set_text(m_sSheetDotDotDot);
93 m_xFtName->set_sensitive(false);
94 m_xEdName->set_sensitive(false);
97 bool bShared = rViewData.GetDocShell() && rViewData.GetDocShell()->IsDocShared();
99 if ( !bFromFile || bShared )
101 m_xBtnNew->set_active(true);
102 SetNewTable_Impl();
103 if ( bShared )
105 m_xBtnFromFile->set_sensitive(false);
108 else
110 m_xBtnFromFile->set_active(true);
111 SetFromTo_Impl();
113 aBrowseTimer.SetInvokeHandler( LINK( this, ScInsertTableDlg, BrowseTimeoutHdl ) );
114 aBrowseTimer.SetTimeout( 200 );
118 short ScInsertTableDlg::run()
120 if (m_xBtnFromFile->get_active())
121 aBrowseTimer.Start();
123 return GenericDialogController::run();
126 void ScInsertTableDlg::SetNewTable_Impl()
128 if (!m_xBtnNew->get_active() )
129 return;
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);
138 if(nTableCount==1)
140 m_xEdName->set_sensitive(true);
141 m_xFtName->set_sensitive(true);
145 void ScInsertTableDlg::SetFromTo_Impl()
147 if (m_xBtnFromFile->get_active() )
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);
160 void ScInsertTableDlg::FillTables_Impl( const ScDocument* pSrcDoc )
162 m_xLbTables->freeze();
163 m_xLbTables->clear();
165 if ( pSrcDoc )
167 SCTAB nCount = pSrcDoc->GetTableCount();
168 OUString aName;
170 for (SCTAB i=0; i<nCount; ++i)
172 pSrcDoc->GetName( i, aName );
173 m_xLbTables->append_text(aName);
177 m_xLbTables->thaw();
179 if (m_xLbTables->n_children() == 1)
180 m_xLbTables->select(0);
183 const OUString* ScInsertTableDlg::GetFirstTable( sal_uInt16* pN )
185 const OUString* pStr = nullptr;
187 if ( m_xBtnNew->get_active() )
189 aStrCurSelTable = m_xEdName->get_text();
190 pStr = &aStrCurSelTable;
192 else
194 std::vector<int> aRows(m_xLbTables->get_selected_rows());
195 if (nSelTabIndex < aRows.size())
197 aStrCurSelTable = m_xLbTables->get_text(aRows[0]);
198 pStr = &aStrCurSelTable;
199 if ( pN )
200 *pN = aRows[0];
201 nSelTabIndex = 1;
205 return pStr;
208 const OUString* ScInsertTableDlg::GetNextTable( sal_uInt16* pN )
210 if (m_xBtnNew->get_active())
211 return nullptr;
213 std::vector<int> aRows(m_xLbTables->get_selected_rows());
215 const OUString* pStr = nullptr;
216 if (nSelTabIndex < aRows.size())
218 aStrCurSelTable = m_xLbTables->get_text(aRows[nSelTabIndex]);
219 pStr = &aStrCurSelTable;
220 if ( pN )
221 *pN = aRows[nSelTabIndex];
222 nSelTabIndex++;
225 return pStr;
228 // Handler:
230 IMPL_LINK_NOARG(ScInsertTableDlg, CountHdl_Impl, weld::SpinButton&, void)
232 nTableCount = static_cast<SCTAB>(m_xNfCount->get_value());
233 if ( nTableCount==1)
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);
241 else
243 m_xEdName->set_text(m_sSheetDotDotDot);
244 m_xFtName->set_sensitive(false);
245 m_xEdName->set_sensitive(false);
248 DoEnable_Impl();
251 IMPL_LINK(ScInsertTableDlg, ChoiceHdl_Impl, weld::Toggleable&, rButton, void)
253 if (!rButton.get_active())
254 return;
256 if ( m_xBtnNew->get_active() )
257 SetNewTable_Impl();
258 else
259 SetFromTo_Impl();
261 DoEnable_Impl();
264 IMPL_LINK_NOARG(ScInsertTableDlg, BrowseHdl_Impl, weld::Button&, void)
266 pDocInserter.reset();
267 pDocInserter.reset( new ::sfx2::DocumentInserter(m_xDialog.get(), ScDocShell::Factory().GetFactoryName()) );
268 pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
271 IMPL_LINK_NOARG(ScInsertTableDlg, SelectHdl_Impl, weld::TreeView&, void)
273 DoEnable_Impl();
276 void ScInsertTableDlg::DoEnable_Impl()
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);
284 IMPL_LINK_NOARG(ScInsertTableDlg, DoEnterHdl, weld::Button&, void)
286 if (nTableCount > 1 || ScDocument::ValidTabName(m_xEdName->get_text()))
288 m_xDialog->response(RET_OK);
290 else
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();
299 IMPL_LINK_NOARG(ScInsertTableDlg, BrowseTimeoutHdl, Timer *, void)
301 bMustClose = true;
302 BrowseHdl_Impl(*m_xBtnBrowse);
305 IMPL_LINK( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
307 if ( ERRCODE_NONE == _pFileDlg->GetError() )
309 std::unique_ptr<SfxMedium> pMed = pDocInserter->CreateMedium();
310 if ( pMed )
312 // ERRCTX_SFX_OPENDOC -> "Error loading document"
313 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
315 if ( pDocShTables )
316 pDocShTables->DoClose(); // deletion is done when assigning to the reference
318 pMed->UseInteractionHandler( true ); // to enable the filter options dialog
320 pDocShTables = new ScDocShell;
321 aDocShTablesRef = pDocShTables;
324 weld::WaitObject aWait(m_xDialog.get());
325 pDocShTables->DoLoad(pMed.release());
328 ErrCode nErr = pDocShTables->GetErrorCode();
329 if ( nErr )
330 ErrorHandler::HandleError(nErr, m_xDialog.get()); // warnings, too
332 if ( !pDocShTables->GetError() ) // errors only
334 FillTables_Impl( &pDocShTables->GetDocument() );
335 m_xFtPath->set_label(pDocShTables->GetTitle(SFX_TITLE_FULLNAME));
337 else
339 pDocShTables->DoClose();
340 aDocShTablesRef.clear();
341 pDocShTables = nullptr;
343 FillTables_Impl( nullptr );
344 m_xFtPath->set_label(OUString());
348 DoEnable_Impl();
350 else if ( bMustClose )
351 // execute slot FID_INS_TABLE_EXT and cancel file dialog
352 m_xDialog->response(RET_CANCEL);
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */