Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / miscdlgs / instbdlg.cxx
blob4594b3fe2adc4b153da89d75765dd43fa628ba7c
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/app.hxx>
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/docinsert.hxx>
25 #include <sfx2/filedlghelper.hxx>
26 #include <svtools/ehdl.hxx>
27 #include <svtools/sfxecode.hxx>
28 #include <vcl/msgbox.hxx>
30 #include "global.hxx"
31 #include "docsh.hxx"
32 #include "viewdata.hxx"
33 #include "scresid.hxx"
34 #include "globstr.hrc"
35 #include "instbdlg.hxx"
37 ScInsertTableDlg::ScInsertTableDlg( vcl::Window* pParent, ScViewData& rData, SCTAB nTabCount, bool bFromFile )
38 : ModalDialog(pParent, "InsertSheetDialog", "modules/scalc/ui/insertsheet.ui")
39 , rViewData(rData)
40 , rDoc(*rData.GetDocument())
41 , pDocShTables(nullptr)
42 , pDocInserter(nullptr)
43 , bMustClose(false)
44 , nSelTabIndex(0)
45 , nTableCount(nTabCount)
47 get(m_pBtnBefore, "before");
48 get(m_pBtnBehind, "after");
49 get(m_pBtnNew, "new");
50 get(m_pBtnFromFile, "fromfile");
51 get(m_pFtCount, "countft");
52 get(m_pNfCount, "countnf");
53 get(m_pFtName, "nameft");
54 get(m_pEdName, "nameed");
55 m_sSheetDotDotDot = m_pEdName->GetText();
56 get(m_pLbTables, "tables");
57 m_pLbTables->SetDropDownLineCount(8);
58 get(m_pFtPath, "path");
59 get(m_pBtnBrowse, "browse");
60 get(m_pBtnLink, "link");
61 get(m_pBtnOk, "ok");
62 Init_Impl( bFromFile );
65 ScInsertTableDlg::~ScInsertTableDlg()
67 disposeOnce();
70 void ScInsertTableDlg::dispose()
72 if (pDocShTables)
73 pDocShTables->DoClose();
74 delete pDocInserter;
75 m_pBtnBefore.clear();
76 m_pBtnBehind.clear();
77 m_pBtnNew.clear();
78 m_pBtnFromFile.clear();
79 m_pFtCount.clear();
80 m_pNfCount.clear();
81 m_pFtName.clear();
82 m_pEdName.clear();
83 m_pLbTables.clear();
84 m_pFtPath.clear();
85 m_pBtnBrowse.clear();
86 m_pBtnLink.clear();
87 m_pBtnOk.clear();
88 ModalDialog::dispose();
91 void ScInsertTableDlg::Init_Impl( bool bFromFile )
93 m_pLbTables->EnableMultiSelection(true);
94 m_pBtnBrowse->SetClickHdl( LINK( this, ScInsertTableDlg, BrowseHdl_Impl ) );
95 m_pBtnNew->SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
96 m_pBtnFromFile->SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
97 m_pLbTables->SetSelectHdl( LINK( this, ScInsertTableDlg, SelectHdl_Impl ) );
98 m_pNfCount->SetModifyHdl( LINK( this, ScInsertTableDlg, CountHdl_Impl));
99 m_pBtnOk->SetClickHdl( LINK( this, ScInsertTableDlg, DoEnterHdl ));
100 m_pBtnBefore->Check();
102 m_pNfCount->SetText( OUString::number(nTableCount) );
103 m_pNfCount->SetMax( MAXTAB - rDoc.GetTableCount() + 1 );
105 if(nTableCount==1)
107 OUString aName;
108 rDoc.CreateValidTabName( aName );
109 m_pEdName->SetText( aName );
111 else
113 m_pEdName->SetText(m_sSheetDotDotDot);
114 m_pFtName->Disable();
115 m_pEdName->Disable();
118 bool bShared = rViewData.GetDocShell() && rViewData.GetDocShell()->IsDocShared();
120 if ( !bFromFile || bShared )
122 m_pBtnNew->Check();
123 SetNewTable_Impl();
124 if ( bShared )
126 m_pBtnFromFile->Disable();
129 else
131 m_pBtnFromFile->Check();
132 SetFromTo_Impl();
134 aBrowseTimer.SetTimeoutHdl( LINK( this, ScInsertTableDlg, BrowseTimeoutHdl ) );
135 aBrowseTimer.SetTimeout( 200 );
139 short ScInsertTableDlg::Execute()
141 if ( m_pBtnFromFile->IsChecked() )
142 aBrowseTimer.Start();
144 return ModalDialog::Execute();
147 void ScInsertTableDlg::SetNewTable_Impl()
149 if (m_pBtnNew->IsChecked() )
151 m_pNfCount->Enable();
152 m_pFtCount->Enable();
153 m_pLbTables->Disable();
154 m_pFtPath->Disable();
155 m_pBtnBrowse->Disable();
156 m_pBtnLink->Disable();
158 if(nTableCount==1)
160 m_pEdName->Enable();
161 m_pFtName->Enable();
166 void ScInsertTableDlg::SetFromTo_Impl()
168 if (m_pBtnFromFile->IsChecked() )
170 m_pEdName->Disable();
171 m_pFtName->Disable();
172 m_pFtCount->Disable();
173 m_pNfCount->Disable();
174 m_pLbTables->Enable();
175 m_pFtPath->Enable();
176 m_pBtnBrowse->Enable();
177 m_pBtnLink->Enable();
181 void ScInsertTableDlg::FillTables_Impl( ScDocument* pSrcDoc )
183 m_pLbTables->SetUpdateMode( false );
184 m_pLbTables->Clear();
186 if ( pSrcDoc )
188 SCTAB nCount = pSrcDoc->GetTableCount();
189 OUString aName;
191 for ( SCTAB i=0; i<nCount; i++ )
193 pSrcDoc->GetName( i, aName );
194 m_pLbTables->InsertEntry( aName );
198 m_pLbTables->SetUpdateMode( true );
200 if(m_pLbTables->GetEntryCount()==1)
201 m_pLbTables->SelectEntryPos(0);
204 const OUString* ScInsertTableDlg::GetFirstTable( sal_uInt16* pN )
206 const OUString* pStr = nullptr;
208 if ( m_pBtnNew->IsChecked() )
210 aStrCurSelTable = m_pEdName->GetText();
211 pStr = &aStrCurSelTable;
213 else if ( nSelTabIndex < m_pLbTables->GetSelectEntryCount() )
215 aStrCurSelTable = m_pLbTables->GetSelectEntry();
216 pStr = &aStrCurSelTable;
217 if ( pN )
218 *pN = m_pLbTables->GetSelectEntryPos();
219 nSelTabIndex = 1;
222 return pStr;
225 const OUString* ScInsertTableDlg::GetNextTable( sal_uInt16* pN )
227 const OUString* pStr = nullptr;
229 if ( !m_pBtnNew->IsChecked() && nSelTabIndex < m_pLbTables->GetSelectEntryCount() )
231 aStrCurSelTable = m_pLbTables->GetSelectEntry( nSelTabIndex );
232 pStr = &aStrCurSelTable;
233 if ( pN )
234 *pN = m_pLbTables->GetSelectEntryPos( nSelTabIndex );
235 nSelTabIndex++;
238 return pStr;
241 // Handler:
243 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg, CountHdl_Impl, Edit&, void)
245 nTableCount = static_cast<SCTAB>(m_pNfCount->GetValue());
246 if ( nTableCount==1)
248 OUString aName;
249 rDoc.CreateValidTabName( aName );
250 m_pEdName->SetText( aName );
251 m_pFtName->Enable();
252 m_pEdName->Enable();
254 else
256 m_pEdName->SetText(m_sSheetDotDotDot);
257 m_pFtName->Disable();
258 m_pEdName->Disable();
261 DoEnable_Impl();
264 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg, ChoiceHdl_Impl, Button*, void)
266 if ( m_pBtnNew->IsChecked() )
267 SetNewTable_Impl();
268 else
269 SetFromTo_Impl();
271 DoEnable_Impl();
274 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg, BrowseHdl_Impl, Button*, void)
276 delete pDocInserter;
277 pDocInserter = new ::sfx2::DocumentInserter(
278 OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
279 pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
282 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg, SelectHdl_Impl, ListBox&, void)
284 DoEnable_Impl();
287 void ScInsertTableDlg::DoEnable_Impl()
289 if ( m_pBtnNew->IsChecked() || ( pDocShTables && m_pLbTables->GetSelectEntryCount() ) )
290 m_pBtnOk->Enable();
291 else
292 m_pBtnOk->Disable();
295 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg, DoEnterHdl, Button*, void)
297 if(nTableCount > 1 || ScDocument::ValidTabName(m_pEdName->GetText()))
299 EndDialog(RET_OK);
301 else
303 OUString aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME ) );
304 (void)ScopedVclPtrInstance<MessageDialog>(this, aErrMsg)->Execute();
308 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg, BrowseTimeoutHdl, Timer *, void)
310 bMustClose = true;
311 BrowseHdl_Impl(m_pBtnBrowse);
314 IMPL_LINK_TYPED( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg, void )
316 if ( ERRCODE_NONE == _pFileDlg->GetError() )
318 SfxMedium* pMed = pDocInserter->CreateMedium();
319 if ( pMed )
321 // ERRCTX_SFX_OPENDOC -> "Error loading document"
322 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
324 if ( pDocShTables )
325 pDocShTables->DoClose(); // deletion is done when assigning to the reference
327 pMed->UseInteractionHandler( true ); // to enable the filter options dialog
329 pDocShTables = new ScDocShell;
330 aDocShTablesRef = pDocShTables;
332 Pointer aOldPtr( GetPointer() );
333 SetPointer( Pointer( PointerStyle::Wait ) );
334 pDocShTables->DoLoad( pMed );
335 SetPointer( aOldPtr );
337 sal_uLong nErr = pDocShTables->GetErrorCode();
338 if ( nErr )
339 ErrorHandler::HandleError( nErr ); // warnings, too
341 if ( !pDocShTables->GetError() ) // errors only
343 FillTables_Impl( &pDocShTables->GetDocument() );
344 m_pFtPath->SetText( pDocShTables->GetTitle( SFX_TITLE_FULLNAME ) );
346 else
348 pDocShTables->DoClose();
349 aDocShTablesRef.Clear();
350 pDocShTables = nullptr;
352 FillTables_Impl( nullptr );
353 m_pFtPath->SetText( EMPTY_OUSTRING );
357 DoEnable_Impl();
359 else if ( bMustClose )
360 // execute slot FID_INS_TABLE_EXT and cancel file dialog
361 EndDialog();
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */