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>
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>
29 
30 #include <global.hxx>
31 #include <docsh.hxx>
32 #include <viewdata.hxx>
33 #include <globstr.hrc>
34 #include <scresid.hxx>
35 #include <instbdlg.hxx>
36 
37 ScInsertTableDlg::ScInsertTableDlg(weld::Window* pParent, ScViewData& rData, SCTAB nTabCount, bool bFromFile)
38  : GenericDialogController(pParent, "modules/scalc/ui/insertsheet.ui", "InsertSheetDialog")
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 
71 void 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_clicked( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
76  m_xBtnFromFile->connect_clicked( 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;
88  rDoc.CreateValidTabName( aName );
89  m_xEdName->set_text( aName );
90  }
91  else
92  {
93  m_xEdName->set_text(m_sSheetDotDotDot);
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);
112  SetFromTo_Impl();
113 
114  aBrowseTimer.SetInvokeHandler( LINK( this, ScInsertTableDlg, BrowseTimeoutHdl ) );
115  aBrowseTimer.SetTimeout( 200 );
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 
184 const 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 
209 const 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 
253 {
254  if ( m_xBtnNew->get_active() )
255  SetNewTable_Impl();
256  else
257  SetFromTo_Impl();
258 
259  DoEnable_Impl();
260 }
261 
263 {
264  pDocInserter.reset();
265  pDocInserter.reset( new ::sfx2::DocumentInserter(m_xDialog.get(), ScDocShell::Factory().GetFactoryName()) );
266  pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
267 }
268 
270 {
271  DoEnable_Impl();
272 }
273 
275 {
276  if ( m_xBtnNew->get_active() || ( pDocShTables && m_xLbTables->count_selected_rows() ) )
277  m_xBtnOk->set_sensitive(true);
278  else
279  m_xBtnOk->set_sensitive(false);
280 }
281 
283 {
284  if (nTableCount > 1 || ScDocument::ValidTabName(m_xEdName->get_text()))
285  {
286  m_xDialog->response(RET_OK);
287  }
288  else
289  {
290  OUString aErrMsg ( ScResId( STR_INVALIDTABNAME ) );
291  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Warning,
292  VclButtonsType::Ok, aErrMsg));
293  xBox->run();
294  }
295 }
296 
297 IMPL_LINK_NOARG(ScInsertTableDlg, BrowseTimeoutHdl, Timer *, void)
298 {
299  bMustClose = true;
300  BrowseHdl_Impl(*m_xBtnBrowse);
301 }
302 
303 IMPL_LINK( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
304 {
305  if ( ERRCODE_NONE == _pFileDlg->GetError() )
306  {
307  std::unique_ptr<SfxMedium> pMed = pDocInserter->CreateMedium();
308  if ( pMed )
309  {
310  // ERRCTX_SFX_OPENDOC -> "Error loading document"
311  SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
312 
313  if ( pDocShTables )
314  pDocShTables->DoClose(); // deletion is done when assigning to the reference
315 
316  pMed->UseInteractionHandler( true ); // to enable the filter options dialog
317 
318  pDocShTables = new ScDocShell;
319  aDocShTablesRef = pDocShTables;
320 
321  {
322  weld::WaitObject aWait(m_xDialog.get());
323  pDocShTables->DoLoad(pMed.release());
324  }
325 
326  ErrCode nErr = pDocShTables->GetErrorCode();
327  if ( nErr )
328  ErrorHandler::HandleError(nErr, m_xDialog.get()); // warnings, too
329 
330  if ( !pDocShTables->GetError() ) // errors only
331  {
332  FillTables_Impl( &pDocShTables->GetDocument() );
333  m_xFtPath->set_label(pDocShTables->GetTitle(SFX_TITLE_FULLNAME));
334  }
335  else
336  {
337  pDocShTables->DoClose();
338  aDocShTablesRef.clear();
339  pDocShTables = nullptr;
340 
341  FillTables_Impl( nullptr );
342  m_xFtPath->set_label(EMPTY_OUSTRING);
343  }
344  }
345 
346  DoEnable_Impl();
347  }
348  else if ( bMustClose )
349  // execute slot FID_INS_TABLE_EXT and cancel file dialog
350  m_xDialog->response(RET_CANCEL);
351 }
352 
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPL_LINK_NOARG(ScInsertTableDlg, CountHdl_Impl, weld::SpinButton &, void)
Definition: instbdlg.cxx:231
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:354
std::unique_ptr< weld::Button > m_xBtnBrowse
Definition: instbdlg.hxx:75
#define EMPTY_OUSTRING
Definition: global.hxx:214
std::unique_ptr< weld::Label > m_xFtPath
Definition: instbdlg.hxx:74
OUString m_sSheetDotDotDot
Definition: instbdlg.hxx:63
void SetFromTo_Impl()
Definition: instbdlg.cxx:146
sal_uInt16 nSelTabIndex
Definition: instbdlg.hxx:60
std::unique_ptr< weld::RadioButton > m_xBtnFromFile
Definition: instbdlg.hxx:68
RET_CANCEL
ScInsertTableDlg(weld::Window *pParent, ScViewData &rViewData, SCTAB nTabCount, bool bFromFile)
Definition: instbdlg.cxx:37
std::unique_ptr< weld::RadioButton > m_xBtnBefore
Definition: instbdlg.hxx:65
const OUString * GetNextTable(sal_uInt16 *pN)
Definition: instbdlg.cxx:209
OUString aStrCurSelTable
Definition: instbdlg.hxx:61
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:314
std::unique_ptr< weld::Label > m_xFtCount
Definition: instbdlg.hxx:69
int nCount
bool IsDocShared() const
std::unique_ptr< weld::RadioButton > m_xBtnNew
Definition: instbdlg.hxx:67
void Init_Impl(bool bFromFile)
Definition: instbdlg.cxx:71
virtual short run() override
Definition: instbdlg.cxx:119
#define SFX_TITLE_FULLNAME
void DoEnable_Impl()
Definition: instbdlg.cxx:274
#define ERRCTX_SFX_OPENDOC
int i
void FillTables_Impl(const ScDocument *pSrcDoc)
Definition: instbdlg.cxx:161
static DialogMask HandleError(ErrCode nId, weld::Window *pParent=nullptr, DialogMask nMask=DialogMask::MAX)
std::unique_ptr< weld::TreeView > m_xLbTables
Definition: instbdlg.hxx:73
SC_DLLPUBLIC void CreateValidTabName(OUString &rName) const
Definition: document.cxx:392
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
virtual void Start() override
std::unique_ptr< weld::Button > m_xBtnOk
Definition: instbdlg.hxx:77
ScDocShell * pDocShTables
Definition: instbdlg.hxx:55
std::unique_ptr< sfx2::DocumentInserter > pDocInserter
Definition: instbdlg.hxx:56
const SCTAB MAXTAB
Definition: address.hxx:71
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetNewTable_Impl()
Definition: instbdlg.cxx:127
ScViewData & rViewData
Definition: instbdlg.hxx:53
const OUString * GetFirstTable(sal_uInt16 *pN)
Definition: instbdlg.cxx:184
std::unique_ptr< weld::SpinButton > m_xNfCount
Definition: instbdlg.hxx:70
#define ERRCODE_NONE
static SC_DLLPUBLIC bool ValidTabName(const OUString &rName)
Definition: document.cxx:335
OUString aName
RET_OK
Reference< XExecutableDialog > m_xDialog
ScDocument & rDoc
Definition: instbdlg.hxx:54
std::unique_ptr< weld::Entry > m_xEdName
Definition: instbdlg.hxx:72
virtual ~ScInsertTableDlg() override
Definition: instbdlg.cxx:64
void SetInvokeHandler(const Link< Timer *, void > &rLink)
IMPL_LINK(ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper *, _pFileDlg, void)
Definition: instbdlg.cxx:303
std::unique_ptr< weld::CheckButton > m_xBtnLink
Definition: instbdlg.hxx:76
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:213
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
sal_Int16 SCTAB
Definition: types.hxx:23
std::unique_ptr< weld::Label > m_xFtName
Definition: instbdlg.hxx:71
Timer aBrowseTimer
Definition: instbdlg.hxx:52