Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / miscdlgs / instbdlg.cxx
blob0ed012d27afc26dedaba69a627ca12c0eca7da86
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 .
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>
35 #include "global.hxx"
36 #include "docsh.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")
48 , rViewData(rData)
49 , rDoc(*rData.GetDocument())
50 , pDocShTables(NULL)
51 , pDocInserter(NULL)
52 , bMustClose(false)
53 , nSelTabIndex(0)
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");
70 get(m_pBtnOk, "ok");
71 Init_Impl( bFromFile );
74 //------------------------------------------------------------------------
76 ScInsertTableDlg::~ScInsertTableDlg()
78 if (pDocShTables)
79 pDocShTables->DoClose();
80 delete pDocInserter;
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 );
99 if(nTableCount==1)
101 OUString aName;
102 rDoc.CreateValidTabName( aName );
103 m_pEdName->SetText( aName );
105 else
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 )
116 m_pBtnNew->Check();
117 SetNewTable_Impl();
118 if ( bShared )
120 m_pBtnFromFile->Disable();
123 else
125 m_pBtnFromFile->Check();
126 SetFromTo_Impl();
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 );
146 return nRet;
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();
162 if(nTableCount==1)
164 m_pEdName->Enable();
165 m_pFtName->Enable();
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();
181 m_pFtPath->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();
194 if ( pSrcDoc )
196 SCTAB nCount = pSrcDoc->GetTableCount();
197 OUString aName;
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;
227 if ( pN )
228 *pN = m_pLbTables->GetSelectEntryPos( 0 );
229 nSelTabIndex = 1;
232 return pStr;
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;
245 if ( pN )
246 *pN = m_pLbTables->GetSelectEntryPos( nSelTabIndex );
247 nSelTabIndex++;
250 return pStr;
254 //------------------------------------------------------------------------
255 // Handler:
256 //------------------------------------------------------------------------
258 IMPL_LINK_NOARG(ScInsertTableDlg, CountHdl_Impl)
260 nTableCount = static_cast<SCTAB>(m_pNfCount->GetValue());
261 if ( nTableCount==1)
263 OUString aName;
264 rDoc.CreateValidTabName( aName );
265 m_pEdName->SetText( aName );
266 m_pFtName->Enable();
267 m_pEdName->Enable();
269 else
271 m_pEdName->SetText(m_sSheetDotDotDot);
272 m_pFtName->Disable();
273 m_pEdName->Disable();
276 DoEnable_Impl();
277 return 0;
280 //------------------------------------------------------------------------
281 IMPL_LINK_NOARG(ScInsertTableDlg, ChoiceHdl_Impl)
283 if ( m_pBtnNew->IsChecked() )
284 SetNewTable_Impl();
285 else
286 SetFromTo_Impl();
288 DoEnable_Impl();
289 return 0;
292 //------------------------------------------------------------------------
294 IMPL_LINK_NOARG(ScInsertTableDlg, BrowseHdl_Impl)
296 if ( pDocInserter )
297 delete pDocInserter;
298 pDocInserter = new ::sfx2::DocumentInserter(
299 OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
300 pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
301 return 0;
304 //------------------------------------------------------------------------
306 IMPL_LINK_NOARG(ScInsertTableDlg, SelectHdl_Impl)
308 DoEnable_Impl();
309 return 0;
312 //------------------------------------------------------------------------
314 void ScInsertTableDlg::DoEnable_Impl()
316 if ( m_pBtnNew->IsChecked() || ( pDocShTables && m_pLbTables->GetSelectEntryCount() ) )
317 m_pBtnOk->Enable();
318 else
319 m_pBtnOk->Disable();
322 IMPL_LINK_NOARG(ScInsertTableDlg, DoEnterHdl)
324 if(nTableCount > 1 || rDoc.ValidTabName(m_pEdName->GetText()))
326 EndDialog(RET_OK);
328 else
330 OUString aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME ) );
331 (void)ErrorBox( this,WinBits( WB_OK | WB_DEF_OK ),aErrMsg).Execute();
333 return 0;
336 IMPL_LINK_NOARG(ScInsertTableDlg, BrowseTimeoutHdl)
338 bMustClose = true;
339 BrowseHdl_Impl(m_pBtnBrowse);
340 return 0;
343 IMPL_LINK( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
345 if ( ERRCODE_NONE == _pFileDlg->GetError() )
347 SfxMedium* pMed = pDocInserter->CreateMedium();
348 if ( pMed )
350 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
351 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
353 if ( pDocShTables )
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();
367 if ( nErr )
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 ) );
375 else
377 pDocShTables->DoClose();
378 aDocShTablesRef.Clear();
379 pDocShTables = NULL;
381 FillTables_Impl( NULL );
382 m_pFtPath->SetText( EMPTY_OUSTRING );
386 DoEnable_Impl();
388 else if ( bMustClose )
389 // execute slot FID_INS_TABLE_EXT and cancel file dialog
390 EndDialog( RET_CANCEL );
392 return 0;
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */