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 <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>
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
, u
"modules/scalc/ui/insertsheet.ui"_ustr
, u
"InsertSheetDialog"_ustr
)
38 , aBrowseTimer("ScInsertTableDlg aBrowseTimer")
40 , rDoc(rData
.GetDocument())
43 , nTableCount(nTabCount
)
44 , m_xBtnBefore(m_xBuilder
->weld_radio_button(u
"before"_ustr
))
45 , m_xBtnNew(m_xBuilder
->weld_radio_button(u
"new"_ustr
))
46 , m_xBtnFromFile(m_xBuilder
->weld_radio_button(u
"fromfile"_ustr
))
47 , m_xFtCount(m_xBuilder
->weld_label(u
"countft"_ustr
))
48 , m_xNfCount(m_xBuilder
->weld_spin_button(u
"countnf"_ustr
))
49 , m_xFtName(m_xBuilder
->weld_label(u
"nameft"_ustr
))
50 , m_xEdName(m_xBuilder
->weld_entry(u
"nameed"_ustr
))
51 , m_xLbTables(m_xBuilder
->weld_tree_view(u
"tables"_ustr
))
52 , m_xFtPath(m_xBuilder
->weld_label(u
"path"_ustr
))
53 , m_xBtnBrowse(m_xBuilder
->weld_button(u
"browse"_ustr
))
54 , m_xBtnLink(m_xBuilder
->weld_check_button(u
"link"_ustr
))
55 , m_xBtnOk(m_xBuilder
->weld_button(u
"ok"_ustr
))
57 m_sSheetDotDotDot
= m_xEdName
->get_text();
58 m_xLbTables
->set_size_request(-1, m_xLbTables
->get_height_rows(8));
62 ScInsertTableDlg::~ScInsertTableDlg()
65 pDocShTables
->DoClose();
69 void ScInsertTableDlg::Init_Impl( bool bFromFile
)
71 m_xLbTables
->set_selection_mode(SelectionMode::Multiple
);
72 m_xBtnBrowse
->connect_clicked( LINK( this, ScInsertTableDlg
, BrowseHdl_Impl
) );
73 m_xBtnNew
->connect_toggled( LINK( this, ScInsertTableDlg
, ChoiceHdl_Impl
) );
74 m_xBtnFromFile
->connect_toggled( LINK( this, ScInsertTableDlg
, ChoiceHdl_Impl
) );
75 m_xLbTables
->connect_selection_changed(LINK(this, ScInsertTableDlg
, SelectHdl_Impl
));
76 m_xNfCount
->connect_value_changed( LINK( this, ScInsertTableDlg
, CountHdl_Impl
));
77 m_xBtnOk
->connect_clicked( LINK( this, ScInsertTableDlg
, DoEnterHdl
));
78 m_xBtnBefore
->set_active(true);
80 m_xNfCount
->set_max(MAXTAB
- rDoc
.GetTableCount() + 1);
81 m_xNfCount
->set_value(nTableCount
);
86 rDoc
.CreateValidTabName( aName
);
87 m_xEdName
->set_text( aName
);
91 m_xEdName
->set_text(m_sSheetDotDotDot
);
92 m_xFtName
->set_sensitive(false);
93 m_xEdName
->set_sensitive(false);
96 bool bShared
= rViewData
.GetDocShell() && rViewData
.GetDocShell()->IsDocShared();
98 if ( !bFromFile
|| bShared
)
100 m_xBtnNew
->set_active(true);
104 m_xBtnFromFile
->set_sensitive(false);
109 m_xBtnFromFile
->set_active(true);
112 aBrowseTimer
.SetInvokeHandler( LINK( this, ScInsertTableDlg
, BrowseTimeoutHdl
) );
113 aBrowseTimer
.SetTimeout( 200 );
117 short ScInsertTableDlg::run()
119 if (m_xBtnFromFile
->get_active())
120 aBrowseTimer
.Start();
122 return GenericDialogController::run();
125 void ScInsertTableDlg::SetNewTable_Impl()
127 if (!m_xBtnNew
->get_active() )
130 m_xNfCount
->set_sensitive(true);
131 m_xFtCount
->set_sensitive(true);
132 m_xLbTables
->set_sensitive(false);
133 m_xFtPath
->set_sensitive(false);
134 m_xBtnBrowse
->set_sensitive(false);
135 m_xBtnLink
->set_sensitive(false);
139 m_xEdName
->set_sensitive(true);
140 m_xFtName
->set_sensitive(true);
144 void ScInsertTableDlg::SetFromTo_Impl()
146 if (m_xBtnFromFile
->get_active() )
148 m_xEdName
->set_sensitive(false);
149 m_xFtName
->set_sensitive(false);
150 m_xFtCount
->set_sensitive(false);
151 m_xNfCount
->set_sensitive(false);
152 m_xLbTables
->set_sensitive(true);
153 m_xFtPath
->set_sensitive(true);
154 m_xBtnBrowse
->set_sensitive(true);
155 m_xBtnLink
->set_sensitive(true);
159 void ScInsertTableDlg::FillTables_Impl( const ScDocument
* pSrcDoc
)
161 m_xLbTables
->freeze();
162 m_xLbTables
->clear();
166 SCTAB nCount
= pSrcDoc
->GetTableCount();
169 for (SCTAB i
=0; i
<nCount
; ++i
)
171 pSrcDoc
->GetName( i
, aName
);
172 m_xLbTables
->append_text(aName
);
178 if (m_xLbTables
->n_children() == 1)
179 m_xLbTables
->select(0);
182 const OUString
* ScInsertTableDlg::GetFirstTable( sal_uInt16
* pN
)
184 const OUString
* pStr
= nullptr;
186 if ( m_xBtnNew
->get_active() )
188 aStrCurSelTable
= m_xEdName
->get_text();
189 pStr
= &aStrCurSelTable
;
193 std::vector
<int> aRows(m_xLbTables
->get_selected_rows());
194 if (nSelTabIndex
< aRows
.size())
196 aStrCurSelTable
= m_xLbTables
->get_text(aRows
[0]);
197 pStr
= &aStrCurSelTable
;
207 const OUString
* ScInsertTableDlg::GetNextTable( sal_uInt16
* pN
)
209 if (m_xBtnNew
->get_active())
212 std::vector
<int> aRows(m_xLbTables
->get_selected_rows());
214 const OUString
* pStr
= nullptr;
215 if (nSelTabIndex
< aRows
.size())
217 aStrCurSelTable
= m_xLbTables
->get_text(aRows
[nSelTabIndex
]);
218 pStr
= &aStrCurSelTable
;
220 *pN
= aRows
[nSelTabIndex
];
229 IMPL_LINK_NOARG(ScInsertTableDlg
, CountHdl_Impl
, weld::SpinButton
&, void)
231 nTableCount
= static_cast<SCTAB
>(m_xNfCount
->get_value());
235 rDoc
.CreateValidTabName( aName
);
236 m_xEdName
->set_text( aName
);
237 m_xFtName
->set_sensitive(true);
238 m_xEdName
->set_sensitive(true);
242 m_xEdName
->set_text(m_sSheetDotDotDot
);
243 m_xFtName
->set_sensitive(false);
244 m_xEdName
->set_sensitive(false);
250 IMPL_LINK(ScInsertTableDlg
, ChoiceHdl_Impl
, weld::Toggleable
&, rButton
, void)
252 if (!rButton
.get_active())
255 if ( m_xBtnNew
->get_active() )
263 IMPL_LINK_NOARG(ScInsertTableDlg
, BrowseHdl_Impl
, weld::Button
&, void)
265 pDocInserter
.reset();
266 pDocInserter
.reset( new ::sfx2::DocumentInserter(m_xDialog
.get(), ScDocShell::Factory().GetFactoryName()) );
267 pDocInserter
->StartExecuteModal( LINK( this, ScInsertTableDlg
, DialogClosedHdl
) );
270 IMPL_LINK_NOARG(ScInsertTableDlg
, SelectHdl_Impl
, weld::TreeView
&, void)
275 void ScInsertTableDlg::DoEnable_Impl()
277 if ( m_xBtnNew
->get_active() || ( pDocShTables
&& m_xLbTables
->count_selected_rows() ) )
278 m_xBtnOk
->set_sensitive(true);
280 m_xBtnOk
->set_sensitive(false);
283 IMPL_LINK_NOARG(ScInsertTableDlg
, DoEnterHdl
, weld::Button
&, void)
285 if (nTableCount
> 1 || ScDocument::ValidTabName(m_xEdName
->get_text()))
287 m_xDialog
->response(RET_OK
);
291 OUString
aErrMsg ( ScResId( STR_INVALIDTABNAME
) );
292 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(m_xDialog
.get(), VclMessageType::Warning
,
293 VclButtonsType::Ok
, aErrMsg
));
298 IMPL_LINK_NOARG(ScInsertTableDlg
, BrowseTimeoutHdl
, Timer
*, void)
301 BrowseHdl_Impl(*m_xBtnBrowse
);
304 IMPL_LINK( ScInsertTableDlg
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
, void )
306 if ( ERRCODE_NONE
== _pFileDlg
->GetError() )
308 std::unique_ptr
<SfxMedium
> pMed
= pDocInserter
->CreateMedium();
311 // ERRCTX_SFX_OPENDOC -> "Error loading document"
312 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, pMed
->GetName() );
315 pDocShTables
->DoClose(); // deletion is done when assigning to the reference
317 pMed
->UseInteractionHandler( true ); // to enable the filter options dialog
319 pDocShTables
= new ScDocShell
;
322 weld::WaitObject
aWait(m_xDialog
.get());
323 pDocShTables
->DoLoad(pMed
.release());
326 ErrCodeMsg nErr
= pDocShTables
->GetErrorCode();
328 ErrorHandler::HandleError(nErr
, m_xDialog
.get()); // warnings, too
330 if ( !pDocShTables
->GetErrorIgnoreWarning() ) // errors only
332 FillTables_Impl( &pDocShTables
->GetDocument() );
333 m_xFtPath
->set_label(pDocShTables
->GetTitle(SFX_TITLE_FULLNAME
));
337 pDocShTables
->DoClose();
338 pDocShTables
.clear();
340 FillTables_Impl( nullptr );
341 m_xFtPath
->set_label(OUString());
347 else if ( bMustClose
)
348 // execute slot FID_INS_TABLE_EXT and cancel file dialog
349 m_xDialog
->response(RET_CANCEL
);
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */