1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: instbdlg.cxx,v $
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>
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>
64 #define ModalDialog( parent, id ) Dialog( parent, "insert-sheet.xml", id )
65 #define ErrorBox( this, bits, message ) ErrorBox (LAYOUT_THIS_WINDOW (this), bits, message)
66 #define SetDefDialogParent( this ) SetDefDialogParent (LAYOUT_THIS_WINDOW (this))
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
) ),
93 rDoc ( *rData
.GetDocument() ),
94 pDocShTables ( NULL
),
95 pDocInserter ( NULL
),
98 nTableCount (nTabCount
)
101 SetHelpId (SID_INSERT_TABLE
);
102 aFtPath
.SetText (EMPTY_STRING
);
103 #endif /* ENABLE_LAYOUT */
104 Init_Impl( bFromFile
);
108 //------------------------------------------------------------------------
110 __EXPORT
ScInsertTableDlg::~ScInsertTableDlg()
113 pDocShTables
->DoClose();
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
));
129 aNfCount
.SetText( String::CreateFromInt32(nTableCount
) );
130 aNfCount
.SetMax( MAXTAB
- rDoc
.GetTableCount() + 1 );
135 rDoc
.CreateValidTabName( aName
);
136 aEdName
.SetText( aName
);
140 String aName
=aFlTable
.GetText();
141 aName
.AppendAscii(RTL_CONSTASCII_STRINGPARAM("..."));
142 aEdName
.SetText( aName
);
147 bool bShared
= ( rViewData
.GetDocShell() ? rViewData
.GetDocShell()->IsDocShared() : false );
149 if ( !bFromFile
|| bShared
)
155 aBtnFromFile
.Disable();
160 aBtnFromFile
.Check();
163 aBrowseTimer
.SetTimeoutHdl( LINK( this, ScInsertTableDlg
, BrowseTimeoutHdl
) );
164 aBrowseTimer
.SetTimeout( 200 );
168 //------------------------------------------------------------------------
171 #define ModalDialog Dialog
172 #endif /* ENABLE_LAYOUT */
174 short __EXPORT
ScInsertTableDlg::Execute()
176 // set Parent of DocumentInserter and Doc-Manager
177 Window
* pOldDefParent
= Application::GetDefDialogParent();
178 Application::SetDefDialogParent( this );
180 if ( aBtnFromFile
.IsChecked() )
181 aBrowseTimer
.Start();
183 short nRet
= ModalDialog::Execute();
184 #undef SetDefDialogParent
185 Application::SetDefDialogParent( pOldDefParent
);
189 //------------------------------------------------------------------------
191 void ScInsertTableDlg::SetNewTable_Impl()
193 if (aBtnNew
.IsChecked() )
197 aLbTables
.Disable();
199 aBtnBrowse
.Disable();
210 //------------------------------------------------------------------------
212 void ScInsertTableDlg::SetFromTo_Impl()
214 if (aBtnFromFile
.IsChecked() )
222 aBtnBrowse
.Enable();
227 //------------------------------------------------------------------------
229 void ScInsertTableDlg::FillTables_Impl( ScDocument
* pSrcDoc
)
231 aLbTables
.SetUpdateMode( FALSE
);
236 SCTAB nCount
= pSrcDoc
->GetTableCount();
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
;
268 *pN
= aLbTables
.GetSelectEntryPos( 0 );
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
;
286 *pN
= aLbTables
.GetSelectEntryPos( nSelTabIndex
);
294 //------------------------------------------------------------------------
296 //------------------------------------------------------------------------
298 IMPL_LINK( ScInsertTableDlg
, CountHdl_Impl
, NumericField
*, EMPTYARG
)
300 nTableCount
= static_cast<SCTAB
>(aNfCount
.GetValue());
304 rDoc
.CreateValidTabName( aName
);
305 aEdName
.SetText( aName
);
311 String aName
=aFlTable
.GetText();
312 aName
.AppendAscii(RTL_CONSTASCII_STRINGPARAM("..."));
313 aEdName
.SetText( aName
);
322 //------------------------------------------------------------------------
323 IMPL_LINK( ScInsertTableDlg
, ChoiceHdl_Impl
, RadioButton
*, EMPTYARG
)
325 if ( aBtnNew
.IsChecked() )
334 //------------------------------------------------------------------------
336 IMPL_LINK( ScInsertTableDlg
, BrowseHdl_Impl
, PushButton
*, EMPTYARG
)
340 pDocInserter
= new ::sfx2::DocumentInserter(
341 0, String::CreateFromAscii( ScDocShell::Factory().GetShortName() ) );
342 pDocInserter
->StartExecuteModal( LINK( this, ScInsertTableDlg
, DialogClosedHdl
) );
346 //------------------------------------------------------------------------
348 IMPL_LINK( ScInsertTableDlg
, SelectHdl_Impl
, MultiListBox
*, EMPTYARG
)
354 //------------------------------------------------------------------------
356 void ScInsertTableDlg::DoEnable_Impl()
358 if ( aBtnNew
.IsChecked() || ( pDocShTables
&& aLbTables
.GetSelectEntryCount() ) )
364 IMPL_LINK( ScInsertTableDlg
, DoEnterHdl
, PushButton
*, EMPTYARG
)
366 if(nTableCount
> 1 || rDoc
.ValidTabName(aEdName
.GetText()))
372 String
aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME
) );
373 (void)ErrorBox( this,WinBits( WB_OK
| WB_DEF_OK
),aErrMsg
).Execute();
378 IMPL_LINK( ScInsertTableDlg
, BrowseTimeoutHdl
, Timer
*, EMPTYARG
)
381 BrowseHdl_Impl( &aBtnBrowse
);
385 IMPL_LINK( ScInsertTableDlg
, DialogClosedHdl
, sfx2::FileDialogHelper
*, _pFileDlg
)
387 if ( ERRCODE_NONE
== _pFileDlg
->GetError() )
389 SfxMedium
* pMed
= pDocInserter
->CreateMedium();
392 // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
393 SfxErrorContext
aEc( ERRCTX_SFX_OPENDOC
, pMed
->GetName() );
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();
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
) );
419 pDocShTables
->DoClose();
420 aDocShTablesRef
.Clear();
423 FillTables_Impl( NULL
);
424 aFtPath
.SetText( EMPTY_STRING
);
430 else if ( bMustClose
)
431 // execute slot FID_INS_TABLE_EXT and cancel file dialog
432 EndDialog( RET_CANCEL
);