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/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>
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")
40 , rDoc(*rData
.GetDocument())
41 , pDocShTables(nullptr)
42 , pDocInserter(nullptr)
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");
62 Init_Impl( bFromFile
);
65 ScInsertTableDlg::~ScInsertTableDlg()
70 void ScInsertTableDlg::dispose()
73 pDocShTables
->DoClose();
78 m_pBtnFromFile
.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 );
108 rDoc
.CreateValidTabName( aName
);
109 m_pEdName
->SetText( aName
);
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
)
126 m_pBtnFromFile
->Disable();
131 m_pBtnFromFile
->Check();
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();
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();
176 m_pBtnBrowse
->Enable();
177 m_pBtnLink
->Enable();
181 void ScInsertTableDlg::FillTables_Impl( ScDocument
* pSrcDoc
)
183 m_pLbTables
->SetUpdateMode( false );
184 m_pLbTables
->Clear();
188 SCTAB nCount
= pSrcDoc
->GetTableCount();
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
;
218 *pN
= m_pLbTables
->GetSelectEntryPos();
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
;
234 *pN
= m_pLbTables
->GetSelectEntryPos( nSelTabIndex
);
243 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg
, CountHdl_Impl
, Edit
&, void)
245 nTableCount
= static_cast<SCTAB
>(m_pNfCount
->GetValue());
249 rDoc
.CreateValidTabName( aName
);
250 m_pEdName
->SetText( aName
);
256 m_pEdName
->SetText(m_sSheetDotDotDot
);
257 m_pFtName
->Disable();
258 m_pEdName
->Disable();
264 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg
, ChoiceHdl_Impl
, Button
*, void)
266 if ( m_pBtnNew
->IsChecked() )
274 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg
, BrowseHdl_Impl
, Button
*, void)
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)
287 void ScInsertTableDlg::DoEnable_Impl()
289 if ( m_pBtnNew
->IsChecked() || ( pDocShTables
&& m_pLbTables
->GetSelectEntryCount() ) )
295 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg
, DoEnterHdl
, Button
*, void)
297 if(nTableCount
> 1 || ScDocument::ValidTabName(m_pEdName
->GetText()))
303 OUString
aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME
) );
304 (void)ScopedVclPtrInstance
<MessageDialog
>(this, aErrMsg
)->Execute();
308 IMPL_LINK_NOARG_TYPED(ScInsertTableDlg
, BrowseTimeoutHdl
, Timer
*, void)
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();
321 // ERRCTX_SFX_OPENDOC -> "Error loading document"
322 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, pMed
->GetName() );
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();
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
) );
348 pDocShTables
->DoClose();
349 aDocShTablesRef
.Clear();
350 pDocShTables
= nullptr;
352 FillTables_Impl( nullptr );
353 m_pFtPath
->SetText( EMPTY_OUSTRING
);
359 else if ( bMustClose
)
360 // execute slot FID_INS_TABLE_EXT and cancel file dialog
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */