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 .
21 #undef SC_DLLIMPLEMENTATION
25 //------------------------------------------------------------------
27 #include <sfx2/app.hxx>
28 #include <sfx2/docfile.hxx>
29 #include <sfx2/docinsert.hxx>
30 #include <sfx2/filedlghelper.hxx>
31 #include <svtools/ehdl.hxx>
32 #include <svtools/sfxecode.hxx>
33 #include <vcl/msgbox.hxx>
37 #include "viewdata.hxx"
38 #include "scresid.hxx"
39 #include "globstr.hrc"
41 #define SC_INSTBDLG_CXX
42 #include "instbdlg.hxx"
44 //==================================================================
46 ScInsertTableDlg::ScInsertTableDlg( Window
* pParent
, ScViewData
& rData
, SCTAB nTabCount
, bool bFromFile
)
47 : ModalDialog(pParent
, "InsertSheetDialog", "modules/scalc/ui/insertsheet.ui")
49 , rDoc(*rData
.GetDocument())
54 , nTableCount(nTabCount
)
56 get(m_pBtnBefore
, "before");
57 get(m_pBtnBehind
, "after");
58 get(m_pBtnNew
, "new");
59 get(m_pBtnFromFile
, "fromfile");
60 get(m_pFtCount
, "countft");
61 get(m_pNfCount
, "countnf");
62 get(m_pFtName
, "nameft");
63 get(m_pEdName
, "nameed");
64 m_sSheetDotDotDot
= m_pEdName
->GetText();
65 get(m_pLbTables
, "tables");
66 m_pLbTables
->SetDropDownLineCount(8);
67 get(m_pFtPath
, "path");
68 get(m_pBtnBrowse
, "browse");
69 get(m_pBtnLink
, "link");
71 Init_Impl( bFromFile
);
74 //------------------------------------------------------------------------
76 ScInsertTableDlg::~ScInsertTableDlg()
79 pDocShTables
->DoClose();
83 //------------------------------------------------------------------------
85 void ScInsertTableDlg::Init_Impl( bool bFromFile
)
87 m_pLbTables
->EnableMultiSelection(true);
88 m_pBtnBrowse
->SetClickHdl( LINK( this, ScInsertTableDlg
, BrowseHdl_Impl
) );
89 m_pBtnNew
->SetClickHdl( LINK( this, ScInsertTableDlg
, ChoiceHdl_Impl
) );
90 m_pBtnFromFile
->SetClickHdl( LINK( this, ScInsertTableDlg
, ChoiceHdl_Impl
) );
91 m_pLbTables
->SetSelectHdl( LINK( this, ScInsertTableDlg
, SelectHdl_Impl
) );
92 m_pNfCount
->SetModifyHdl( LINK( this, ScInsertTableDlg
, CountHdl_Impl
));
93 m_pBtnOk
->SetClickHdl( LINK( this, ScInsertTableDlg
, DoEnterHdl
));
94 m_pBtnBefore
->Check();
96 m_pNfCount
->SetText( OUString::number(nTableCount
) );
97 m_pNfCount
->SetMax( MAXTAB
- rDoc
.GetTableCount() + 1 );
102 rDoc
.CreateValidTabName( aName
);
103 m_pEdName
->SetText( aName
);
107 m_pEdName
->SetText(m_sSheetDotDotDot
);
108 m_pFtName
->Disable();
109 m_pEdName
->Disable();
112 bool bShared
= ( rViewData
.GetDocShell() ? rViewData
.GetDocShell()->IsDocShared() : false );
114 if ( !bFromFile
|| bShared
)
120 m_pBtnFromFile
->Disable();
125 m_pBtnFromFile
->Check();
128 aBrowseTimer
.SetTimeoutHdl( LINK( this, ScInsertTableDlg
, BrowseTimeoutHdl
) );
129 aBrowseTimer
.SetTimeout( 200 );
133 //------------------------------------------------------------------------
135 short ScInsertTableDlg::Execute()
137 // set Parent of DocumentInserter and Doc-Manager
138 Window
* pOldDefParent
= Application::GetDefDialogParent();
139 Application::SetDefDialogParent( this );
141 if ( m_pBtnFromFile
->IsChecked() )
142 aBrowseTimer
.Start();
144 short nRet
= ModalDialog::Execute();
145 Application::SetDefDialogParent( pOldDefParent
);
149 //------------------------------------------------------------------------
151 void ScInsertTableDlg::SetNewTable_Impl()
153 if (m_pBtnNew
->IsChecked() )
155 m_pNfCount
->Enable();
156 m_pFtCount
->Enable();
157 m_pLbTables
->Disable();
158 m_pFtPath
->Disable();
159 m_pBtnBrowse
->Disable();
160 m_pBtnLink
->Disable();
170 //------------------------------------------------------------------------
172 void ScInsertTableDlg::SetFromTo_Impl()
174 if (m_pBtnFromFile
->IsChecked() )
176 m_pEdName
->Disable();
177 m_pFtName
->Disable();
178 m_pFtCount
->Disable();
179 m_pNfCount
->Disable();
180 m_pLbTables
->Enable();
182 m_pBtnBrowse
->Enable();
183 m_pBtnLink
->Enable();
187 //------------------------------------------------------------------------
189 void ScInsertTableDlg::FillTables_Impl( ScDocument
* pSrcDoc
)
191 m_pLbTables
->SetUpdateMode( false );
192 m_pLbTables
->Clear();
196 SCTAB nCount
= pSrcDoc
->GetTableCount();
199 for ( SCTAB i
=0; i
<nCount
; i
++ )
201 pSrcDoc
->GetName( i
, aName
);
202 m_pLbTables
->InsertEntry( aName
);
206 m_pLbTables
->SetUpdateMode( sal_True
);
208 if(m_pLbTables
->GetEntryCount()==1)
209 m_pLbTables
->SelectEntryPos(0);
212 //------------------------------------------------------------------------
214 const OUString
* ScInsertTableDlg::GetFirstTable( sal_uInt16
* pN
)
216 const OUString
* pStr
= NULL
;
218 if ( m_pBtnNew
->IsChecked() )
220 aStrCurSelTable
= m_pEdName
->GetText();
221 pStr
= &aStrCurSelTable
;
223 else if ( nSelTabIndex
< m_pLbTables
->GetSelectEntryCount() )
225 aStrCurSelTable
= m_pLbTables
->GetSelectEntry( 0 );
226 pStr
= &aStrCurSelTable
;
228 *pN
= m_pLbTables
->GetSelectEntryPos( 0 );
235 //------------------------------------------------------------------------
237 const OUString
* ScInsertTableDlg::GetNextTable( sal_uInt16
* pN
)
239 const OUString
* pStr
= NULL
;
241 if ( !m_pBtnNew
->IsChecked() && nSelTabIndex
< m_pLbTables
->GetSelectEntryCount() )
243 aStrCurSelTable
= m_pLbTables
->GetSelectEntry( nSelTabIndex
);
244 pStr
= &aStrCurSelTable
;
246 *pN
= m_pLbTables
->GetSelectEntryPos( nSelTabIndex
);
254 //------------------------------------------------------------------------
256 //------------------------------------------------------------------------
258 IMPL_LINK_NOARG(ScInsertTableDlg
, CountHdl_Impl
)
260 nTableCount
= static_cast<SCTAB
>(m_pNfCount
->GetValue());
264 rDoc
.CreateValidTabName( aName
);
265 m_pEdName
->SetText( aName
);
271 m_pEdName
->SetText(m_sSheetDotDotDot
);
272 m_pFtName
->Disable();
273 m_pEdName
->Disable();
280 //------------------------------------------------------------------------
281 IMPL_LINK_NOARG(ScInsertTableDlg
, ChoiceHdl_Impl
)
283 if ( m_pBtnNew
->IsChecked() )
292 //------------------------------------------------------------------------
294 IMPL_LINK_NOARG(ScInsertTableDlg
, BrowseHdl_Impl
)
298 pDocInserter
= new ::sfx2::DocumentInserter(
299 OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
300 pDocInserter
->StartExecuteModal( LINK( this, ScInsertTableDlg
, DialogClosedHdl
) );
304 //------------------------------------------------------------------------
306 IMPL_LINK_NOARG(ScInsertTableDlg
, SelectHdl_Impl
)
312 //------------------------------------------------------------------------
314 void ScInsertTableDlg::DoEnable_Impl()
316 if ( m_pBtnNew
->IsChecked() || ( pDocShTables
&& m_pLbTables
->GetSelectEntryCount() ) )
322 IMPL_LINK_NOARG(ScInsertTableDlg
, DoEnterHdl
)
324 if(nTableCount
> 1 || rDoc
.ValidTabName(m_pEdName
->GetText()))
330 OUString
aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME
) );
331 (void)ErrorBox( this,WinBits( WB_OK
| WB_DEF_OK
),aErrMsg
).Execute();
336 IMPL_LINK_NOARG(ScInsertTableDlg
, BrowseTimeoutHdl
)
339 BrowseHdl_Impl(m_pBtnBrowse
);
343 IMPL_LINK( ScInsertTableDlg
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
345 if ( ERRCODE_NONE
== _pFileDlg
->GetError() )
347 SfxMedium
* pMed
= pDocInserter
->CreateMedium();
350 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
351 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, pMed
->GetName() );
354 pDocShTables
->DoClose(); // delete passiert beim Zuweisen auf die Ref
356 pMed
->UseInteractionHandler( sal_True
); // to enable the filter options dialog
358 pDocShTables
= new ScDocShell
;
359 aDocShTablesRef
= pDocShTables
;
361 Pointer
aOldPtr( GetPointer() );
362 SetPointer( Pointer( POINTER_WAIT
) );
363 pDocShTables
->DoLoad( pMed
);
364 SetPointer( aOldPtr
);
366 sal_uLong nErr
= pDocShTables
->GetErrorCode();
368 ErrorHandler::HandleError( nErr
); // auch Warnings
370 if ( !pDocShTables
->GetError() ) // nur Errors
372 FillTables_Impl( pDocShTables
->GetDocument() );
373 m_pFtPath
->SetText( pDocShTables
->GetTitle( SFX_TITLE_FULLNAME
) );
377 pDocShTables
->DoClose();
378 aDocShTablesRef
.Clear();
381 FillTables_Impl( NULL
);
382 m_pFtPath
->SetText( EMPTY_OUSTRING
);
388 else if ( bMustClose
)
389 // execute slot FID_INS_TABLE_EXT and cancel file dialog
390 EndDialog( RET_CANCEL
);
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */