update dev300-m58
[ooovba.git] / sc / source / ui / miscdlgs / instbdlg.cxx
blob2b35ab1691a2312694f6cd9efcc531a871faf6d5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: instbdlg.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #undef SC_DLLIMPLEMENTATION
38 //------------------------------------------------------------------
40 #include <sfx2/app.hxx>
41 #include <sfx2/docfile.hxx>
42 #include <sfx2/docinsert.hxx>
43 #include <sfx2/filedlghelper.hxx>
44 #include <svtools/ehdl.hxx>
45 #include <svtools/sfxecode.hxx>
46 #include <vcl/msgbox.hxx>
48 #include "global.hxx"
49 #include "docsh.hxx"
50 #include "viewdata.hxx"
51 #include "scresid.hxx"
52 #include "instbdlg.hrc"
53 #include "globstr.hrc"
55 #define SC_INSTBDLG_CXX
56 #include "instbdlg.hxx"
58 #include <layout/layout-pre.hxx>
60 #if ENABLE_LAYOUT
61 #undef ScResId
62 #define ScResId(x) #x
63 #undef ModalDialog
64 #define ModalDialog( parent, id ) Dialog( parent, "insert-sheet.xml", id )
65 #undef ErrorBox
66 #define ErrorBox( this, bits, message ) ErrorBox (LAYOUT_THIS_WINDOW (this), bits, message)
67 #endif /* ENABLE_LAYOUT */
69 //==================================================================
71 ScInsertTableDlg::ScInsertTableDlg( Window* pParent, ScViewData& rData, SCTAB nTabCount, bool bFromFile )
73 : ModalDialog ( pParent, ScResId( RID_SCDLG_INSERT_TABLE ) ),
75 aBtnBefore ( this, ScResId( RB_BEFORE ) ),
76 aBtnBehind ( this, ScResId( RB_BEHIND ) ),
77 aFlPos ( this, ScResId( FL_POSITION ) ),
78 aBtnNew ( this, ScResId( RB_NEW ) ),
79 aBtnFromFile ( this, ScResId( RB_FROMFILE ) ),
80 aFtCount ( this, ScResId( FT_COUNT ) ),
81 aNfCount ( this, ScResId( NF_COUNT ) ),
82 aFtName ( this, ScResId( FT_NAME ) ),
83 aEdName ( this, ScResId( ED_TABNAME ) ),
84 aLbTables ( this, ScResId( LB_TABLES ) ),
85 aFtPath ( this, ScResId( FT_PATH ) ),
86 aBtnBrowse ( this, ScResId( BTN_BROWSE ) ),
87 aBtnLink ( this, ScResId( CB_LINK ) ),
88 aFlTable ( this, ScResId( FL_TABLE ) ),
89 aBtnOk ( this, ScResId( BTN_OK ) ),
90 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
91 aBtnHelp ( this, ScResId( BTN_HELP ) ),
92 rViewData ( rData ),
93 rDoc ( *rData.GetDocument() ),
94 pDocShTables ( NULL ),
95 pDocInserter ( NULL ),
96 bMustClose ( false ),
97 nSelTabIndex ( 0 ),
98 nTableCount (nTabCount)
100 #if ENABLE_LAYOUT
101 SetHelpId (SID_INSERT_TABLE);
102 aFtPath.SetText (EMPTY_STRING);
103 #endif /* ENABLE_LAYOUT */
104 Init_Impl( bFromFile );
105 FreeResource();
108 //------------------------------------------------------------------------
110 __EXPORT ScInsertTableDlg::~ScInsertTableDlg()
112 if (pDocShTables)
113 pDocShTables->DoClose();
114 delete pDocInserter;
117 //------------------------------------------------------------------------
119 void ScInsertTableDlg::Init_Impl( bool bFromFile )
121 aBtnBrowse .SetClickHdl( LINK( this, ScInsertTableDlg, BrowseHdl_Impl ) );
122 aBtnNew .SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
123 aBtnFromFile .SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
124 aLbTables .SetSelectHdl( LINK( this, ScInsertTableDlg, SelectHdl_Impl ) );
125 aNfCount .SetModifyHdl( LINK( this, ScInsertTableDlg, CountHdl_Impl));
126 aBtnOk .SetClickHdl( LINK( this, ScInsertTableDlg, DoEnterHdl ));
127 aBtnBefore.Check();
129 aNfCount.SetText( String::CreateFromInt32(nTableCount) );
130 aNfCount.SetMax( MAXTAB - rDoc.GetTableCount() + 1 );
132 if(nTableCount==1)
134 String aName;
135 rDoc.CreateValidTabName( aName );
136 aEdName.SetText( aName );
138 else
140 String aName=aFlTable.GetText();
141 aName.AppendAscii(RTL_CONSTASCII_STRINGPARAM("..."));
142 aEdName.SetText( aName );
143 aFtName.Disable();
144 aEdName.Disable();
147 bool bShared = ( rViewData.GetDocShell() ? rViewData.GetDocShell()->IsDocShared() : false );
149 if ( !bFromFile || bShared )
151 aBtnNew.Check();
152 SetNewTable_Impl();
153 if ( bShared )
155 aBtnFromFile.Disable();
158 else
160 aBtnFromFile.Check();
161 SetFromTo_Impl();
163 aBrowseTimer.SetTimeoutHdl( LINK( this, ScInsertTableDlg, BrowseTimeoutHdl ) );
164 aBrowseTimer.SetTimeout( 200 );
168 //------------------------------------------------------------------------
170 #if ENABLE_LAYOUT
171 #undef ModalDialog
172 #define ModalDialog Dialog
173 #endif /* ENABLE_LAYOUT */
175 short __EXPORT ScInsertTableDlg::Execute()
177 // set Parent of DocumentInserter and Doc-Manager
178 Window* pOldDefParent = Application::GetDefDialogParent();
179 Application::SetDefDialogParent( LAYOUT_THIS_WINDOW (this) );
181 if ( aBtnFromFile.IsChecked() )
182 aBrowseTimer.Start();
184 short nRet = ModalDialog::Execute();
185 Application::SetDefDialogParent( pOldDefParent );
186 return nRet;
189 //------------------------------------------------------------------------
191 void ScInsertTableDlg::SetNewTable_Impl()
193 if (aBtnNew.IsChecked() )
195 aNfCount .Enable();
196 aFtCount .Enable();
197 aLbTables .Disable();
198 aFtPath .Disable();
199 aBtnBrowse .Disable();
200 aBtnLink .Disable();
202 if(nTableCount==1)
204 aEdName.Enable();
205 aFtName.Enable();
210 //------------------------------------------------------------------------
212 void ScInsertTableDlg::SetFromTo_Impl()
214 if (aBtnFromFile.IsChecked() )
216 aEdName .Disable();
217 aFtName .Disable();
218 aFtCount .Disable();
219 aNfCount .Disable();
220 aLbTables .Enable();
221 aFtPath .Enable();
222 aBtnBrowse .Enable();
223 aBtnLink .Enable();
227 //------------------------------------------------------------------------
229 void ScInsertTableDlg::FillTables_Impl( ScDocument* pSrcDoc )
231 aLbTables.SetUpdateMode( FALSE );
232 aLbTables.Clear();
234 if ( pSrcDoc )
236 SCTAB nCount = pSrcDoc->GetTableCount();
237 String aName;
239 for ( SCTAB i=0; i<nCount; i++ )
241 pSrcDoc->GetName( i, aName );
242 aLbTables.InsertEntry( aName );
246 aLbTables.SetUpdateMode( TRUE );
248 if(aLbTables.GetEntryCount()==1)
249 aLbTables.SelectEntryPos(0);
252 //------------------------------------------------------------------------
254 const String* ScInsertTableDlg::GetFirstTable( USHORT* pN )
256 const String* pStr = NULL;
258 if ( aBtnNew.IsChecked() )
260 aStrCurSelTable = aEdName.GetText();
261 pStr = &aStrCurSelTable;
263 else if ( nSelTabIndex < aLbTables.GetSelectEntryCount() )
265 aStrCurSelTable = aLbTables.GetSelectEntry( 0 );
266 pStr = &aStrCurSelTable;
267 if ( pN )
268 *pN = aLbTables.GetSelectEntryPos( 0 );
269 nSelTabIndex = 1;
272 return pStr;
275 //------------------------------------------------------------------------
277 const String* ScInsertTableDlg::GetNextTable( USHORT* pN )
279 const String* pStr = NULL;
281 if ( !aBtnNew.IsChecked() && nSelTabIndex < aLbTables.GetSelectEntryCount() )
283 aStrCurSelTable = aLbTables.GetSelectEntry( nSelTabIndex );
284 pStr = &aStrCurSelTable;
285 if ( pN )
286 *pN = aLbTables.GetSelectEntryPos( nSelTabIndex );
287 nSelTabIndex++;
290 return pStr;
294 //------------------------------------------------------------------------
295 // Handler:
296 //------------------------------------------------------------------------
298 IMPL_LINK( ScInsertTableDlg, CountHdl_Impl, NumericField*, EMPTYARG )
300 nTableCount = static_cast<SCTAB>(aNfCount.GetValue());
301 if ( nTableCount==1)
303 String aName;
304 rDoc.CreateValidTabName( aName );
305 aEdName.SetText( aName );
306 aFtName.Enable();
307 aEdName.Enable();
309 else
311 String aName=aFlTable.GetText();
312 aName.AppendAscii(RTL_CONSTASCII_STRINGPARAM("..."));
313 aEdName.SetText( aName );
314 aFtName.Disable();
315 aEdName.Disable();
318 DoEnable_Impl();
319 return 0;
322 //------------------------------------------------------------------------
323 IMPL_LINK( ScInsertTableDlg, ChoiceHdl_Impl, RadioButton*, EMPTYARG )
325 if ( aBtnNew.IsChecked() )
326 SetNewTable_Impl();
327 else
328 SetFromTo_Impl();
330 DoEnable_Impl();
331 return 0;
334 //------------------------------------------------------------------------
336 IMPL_LINK( ScInsertTableDlg, BrowseHdl_Impl, PushButton*, EMPTYARG )
338 if ( pDocInserter )
339 delete pDocInserter;
340 pDocInserter = new ::sfx2::DocumentInserter(
341 0, String::CreateFromAscii( ScDocShell::Factory().GetShortName() ) );
342 pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
343 return 0;
346 //------------------------------------------------------------------------
348 IMPL_LINK( ScInsertTableDlg, SelectHdl_Impl, MultiListBox*, EMPTYARG )
350 DoEnable_Impl();
351 return 0;
354 //------------------------------------------------------------------------
356 void ScInsertTableDlg::DoEnable_Impl()
358 if ( aBtnNew.IsChecked() || ( pDocShTables && aLbTables.GetSelectEntryCount() ) )
359 aBtnOk.Enable();
360 else
361 aBtnOk.Disable();
364 IMPL_LINK( ScInsertTableDlg, DoEnterHdl, PushButton*, EMPTYARG )
366 if(nTableCount > 1 || rDoc.ValidTabName(aEdName.GetText()))
368 EndDialog(RET_OK);
370 else
372 String aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME ) );
373 (void)ErrorBox( this,WinBits( WB_OK | WB_DEF_OK ),aErrMsg).Execute();
375 return 0;
378 IMPL_LINK( ScInsertTableDlg, BrowseTimeoutHdl, Timer*, EMPTYARG )
380 bMustClose = true;
381 BrowseHdl_Impl( &aBtnBrowse );
382 return 0;
385 IMPL_LINK( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
387 if ( ERRCODE_NONE == _pFileDlg->GetError() )
389 SfxMedium* pMed = pDocInserter->CreateMedium();
390 if ( pMed )
392 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
393 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
395 if ( pDocShTables )
396 pDocShTables->DoClose(); // delete passiert beim Zuweisen auf die Ref
398 pMed->UseInteractionHandler( TRUE ); // to enable the filter options dialog
400 pDocShTables = new ScDocShell;
401 aDocShTablesRef = pDocShTables;
403 Pointer aOldPtr( GetPointer() );
404 SetPointer( Pointer( POINTER_WAIT ) );
405 pDocShTables->DoLoad( pMed );
406 SetPointer( aOldPtr );
408 ULONG nErr = pDocShTables->GetErrorCode();
409 if ( nErr )
410 ErrorHandler::HandleError( nErr ); // auch Warnings
412 if ( !pDocShTables->GetError() ) // nur Errors
414 FillTables_Impl( pDocShTables->GetDocument() );
415 aFtPath.SetText( pDocShTables->GetTitle( SFX_TITLE_FULLNAME ) );
417 else
419 pDocShTables->DoClose();
420 aDocShTablesRef.Clear();
421 pDocShTables = NULL;
423 FillTables_Impl( NULL );
424 aFtPath.SetText( EMPTY_STRING );
428 DoEnable_Impl();
430 else if ( bMustClose )
431 // execute slot FID_INS_TABLE_EXT and cancel file dialog
432 EndDialog( RET_CANCEL );
434 return 0;