xref: /aoo42x/main/sc/source/ui/miscdlgs/instbdlg.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 #undef SC_DLLIMPLEMENTATION
32 
33 
34 
35 //------------------------------------------------------------------
36 
37 #include <sfx2/app.hxx>
38 #include <sfx2/docfile.hxx>
39 #include <sfx2/docinsert.hxx>
40 #include <sfx2/filedlghelper.hxx>
41 #include <svtools/ehdl.hxx>
42 #include <svtools/sfxecode.hxx>
43 #include <vcl/msgbox.hxx>
44 
45 #include "global.hxx"
46 #include "docsh.hxx"
47 #include "viewdata.hxx"
48 #include "scresid.hxx"
49 #include "instbdlg.hrc"
50 #include "globstr.hrc"
51 
52 #define SC_INSTBDLG_CXX
53 #include "instbdlg.hxx"
54 
55 #include <layout/layout-pre.hxx>
56 
57 #if ENABLE_LAYOUT
58 #undef ScResId
59 #define ScResId(x) #x
60 #undef ModalDialog
61 #define ModalDialog( parent, id ) Dialog( parent, "insert-sheet.xml", id )
62 #undef ErrorBox
63 #define ErrorBox( this, bits, message ) ErrorBox (LAYOUT_THIS_WINDOW (this), bits, message)
64 #endif /* ENABLE_LAYOUT */
65 
66 //==================================================================
67 
68 ScInsertTableDlg::ScInsertTableDlg( Window* pParent, ScViewData& rData, SCTAB nTabCount, bool bFromFile )
69 
70 	:	ModalDialog ( pParent, ScResId( RID_SCDLG_INSERT_TABLE ) ),
71 		//
72 		aFlPos			( this, ScResId( FL_POSITION ) ),
73 		aBtnBefore		( this, ScResId( RB_BEFORE ) ),
74 		aBtnBehind		( this, ScResId( RB_BEHIND ) ),
75 		aFlTable    	( this, ScResId( FL_TABLE ) ),
76 		aBtnNew   		( this, ScResId( RB_NEW ) ),
77 		aBtnFromFile    ( this, ScResId( RB_FROMFILE ) ),
78 		aFtCount		( this, ScResId( FT_COUNT ) ),
79 		aNfCount		( this, ScResId( NF_COUNT ) ),
80 		aFtName			( this, ScResId( FT_NAME ) ),
81 		aEdName			( this, ScResId( ED_TABNAME ) ),
82 		aLbTables		( this, ScResId( LB_TABLES ) ),
83 		aFtPath			( this, ScResId( FT_PATH ) ),
84 		aBtnBrowse		( this, ScResId( BTN_BROWSE ) ),
85 		aBtnLink		( this, ScResId( CB_LINK ) ),
86 		aBtnOk			( this, ScResId( BTN_OK ) ),
87 		aBtnCancel		( this, ScResId( BTN_CANCEL ) ),
88 		aBtnHelp		( this, ScResId( BTN_HELP ) ),
89 		rViewData		( rData ),
90 		rDoc			( *rData.GetDocument() ),
91 		pDocShTables	( NULL ),
92         pDocInserter    ( NULL ),
93         bMustClose      ( false ),
94 		nSelTabIndex	( 0 ),
95 		nTableCount		(nTabCount)
96 {
97 #if ENABLE_LAYOUT
98     SetHelpId (SID_INSERT_TABLE);
99     aFtPath.SetText (EMPTY_STRING);
100 #endif /* ENABLE_LAYOUT */
101     Init_Impl( bFromFile );
102 	FreeResource();
103 	aLbTables.SetAccessibleName(aBtnFromFile.GetText());
104 }
105 
106 //------------------------------------------------------------------------
107 
108 __EXPORT ScInsertTableDlg::~ScInsertTableDlg()
109 {
110 	if (pDocShTables)
111 		pDocShTables->DoClose();
112     delete pDocInserter;
113 }
114 
115 //------------------------------------------------------------------------
116 
117 void ScInsertTableDlg::Init_Impl( bool bFromFile )
118 {
119 	aBtnBrowse		.SetClickHdl( LINK( this, ScInsertTableDlg, BrowseHdl_Impl ) );
120 	aBtnNew			.SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
121 	aBtnFromFile	.SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
122 	aLbTables		.SetSelectHdl( LINK( this, ScInsertTableDlg, SelectHdl_Impl ) );
123 	aNfCount		.SetModifyHdl( LINK( this, ScInsertTableDlg, CountHdl_Impl));
124 	aBtnOk			.SetClickHdl( LINK( this, ScInsertTableDlg, DoEnterHdl ));
125 	aBtnBefore.Check();
126 
127 	aNfCount.SetText( String::CreateFromInt32(nTableCount) );
128 	aNfCount.SetMax( MAXTAB - rDoc.GetTableCount() + 1 );
129 
130 	if(nTableCount==1)
131 	{
132 		String aName;
133 		rDoc.CreateValidTabName( aName );
134 		aEdName.SetText( aName );
135 	}
136 	else
137 	{
138 		String aName=aFlTable.GetText();
139 		aName.AppendAscii(RTL_CONSTASCII_STRINGPARAM("..."));
140 		aEdName.SetText( aName );
141 		aFtName.Disable();
142 		aEdName.Disable();
143 	}
144 
145     bool bShared = ( rViewData.GetDocShell() ? rViewData.GetDocShell()->IsDocShared() : false );
146 
147     if ( !bFromFile || bShared )
148     {
149         aBtnNew.Check();
150         SetNewTable_Impl();
151         if ( bShared )
152         {
153             aBtnFromFile.Disable();
154         }
155     }
156     else
157     {
158         aBtnFromFile.Check();
159         SetFromTo_Impl();
160 
161         aBrowseTimer.SetTimeoutHdl( LINK( this, ScInsertTableDlg, BrowseTimeoutHdl ) );
162         aBrowseTimer.SetTimeout( 200 );
163     }
164 }
165 
166 //------------------------------------------------------------------------
167 
168 #if ENABLE_LAYOUT
169 #undef ModalDialog
170 #define ModalDialog Dialog
171 #endif /* ENABLE_LAYOUT */
172 
173 short __EXPORT ScInsertTableDlg::Execute()
174 {
175     // set Parent of DocumentInserter and Doc-Manager
176 	Window*	pOldDefParent = Application::GetDefDialogParent();
177 	Application::SetDefDialogParent( LAYOUT_THIS_WINDOW (this) );
178 
179     if ( aBtnFromFile.IsChecked() )
180         aBrowseTimer.Start();
181 
182     short nRet = ModalDialog::Execute();
183 	Application::SetDefDialogParent( pOldDefParent );
184 	return nRet;
185 }
186 
187 //------------------------------------------------------------------------
188 
189 void ScInsertTableDlg::SetNewTable_Impl()
190 {
191 	if (aBtnNew.IsChecked() )
192 	{
193 		aNfCount	.Enable();
194 		aFtCount	.Enable();
195 		aLbTables	.Disable();
196 		aFtPath		.Disable();
197 		aBtnBrowse	.Disable();
198 		aBtnLink	.Disable();
199 
200 		if(nTableCount==1)
201 		{
202 			aEdName.Enable();
203 			aFtName.Enable();
204 		}
205 	}
206 }
207 
208 //------------------------------------------------------------------------
209 
210 void ScInsertTableDlg::SetFromTo_Impl()
211 {
212 	if (aBtnFromFile.IsChecked() )
213 	{
214 		aEdName		.Disable();
215 		aFtName		.Disable();
216 		aFtCount	.Disable();
217 		aNfCount	.Disable();
218 		aLbTables	.Enable();
219 		aFtPath		.Enable();
220 		aBtnBrowse	.Enable();
221 		aBtnLink	.Enable();
222 	}
223 }
224 
225 //------------------------------------------------------------------------
226 
227 void ScInsertTableDlg::FillTables_Impl( ScDocument* pSrcDoc )
228 {
229 	aLbTables.SetUpdateMode( sal_False );
230 	aLbTables.Clear();
231 
232 	if ( pSrcDoc )
233 	{
234 		SCTAB nCount = pSrcDoc->GetTableCount();
235 		String aName;
236 
237 		for ( SCTAB i=0; i<nCount; i++ )
238 		{
239 			pSrcDoc->GetName( i, aName );
240 			aLbTables.InsertEntry( aName );
241 		}
242 	}
243 
244 	aLbTables.SetUpdateMode( sal_True );
245 
246 	if(aLbTables.GetEntryCount()==1)
247 		aLbTables.SelectEntryPos(0);
248 }
249 
250 //------------------------------------------------------------------------
251 
252 const String* ScInsertTableDlg::GetFirstTable( sal_uInt16* pN )
253 {
254 	const String* pStr = NULL;
255 
256 	if ( aBtnNew.IsChecked() )
257 	{
258 		aStrCurSelTable = aEdName.GetText();
259 		pStr = &aStrCurSelTable;
260 	}
261 	else if ( nSelTabIndex < aLbTables.GetSelectEntryCount() )
262 	{
263 		aStrCurSelTable = aLbTables.GetSelectEntry( 0 );
264 		pStr = &aStrCurSelTable;
265 		if ( pN )
266 			*pN = aLbTables.GetSelectEntryPos( 0 );
267 		nSelTabIndex = 1;
268 	}
269 
270 	return pStr;
271 }
272 
273 //------------------------------------------------------------------------
274 
275 const String* ScInsertTableDlg::GetNextTable( sal_uInt16* pN )
276 {
277 	const String* pStr = NULL;
278 
279 	if ( !aBtnNew.IsChecked() && nSelTabIndex < aLbTables.GetSelectEntryCount() )
280 	{
281 		aStrCurSelTable = aLbTables.GetSelectEntry( nSelTabIndex );
282 		pStr = &aStrCurSelTable;
283 		if ( pN )
284 			*pN = aLbTables.GetSelectEntryPos( nSelTabIndex );
285 		nSelTabIndex++;
286 	}
287 
288 	return pStr;
289 }
290 
291 
292 //------------------------------------------------------------------------
293 // Handler:
294 //------------------------------------------------------------------------
295 
296 IMPL_LINK( ScInsertTableDlg, CountHdl_Impl, NumericField*, EMPTYARG )
297 {
298     nTableCount = static_cast<SCTAB>(aNfCount.GetValue());
299 	if ( nTableCount==1)
300 	{
301 		String aName;
302 		rDoc.CreateValidTabName( aName );
303 		aEdName.SetText( aName );
304 		aFtName.Enable();
305 		aEdName.Enable();
306 	}
307 	else
308 	{
309 		String aName=aFlTable.GetText();
310 		aName.AppendAscii(RTL_CONSTASCII_STRINGPARAM("..."));
311 		aEdName.SetText( aName );
312 		aFtName.Disable();
313 		aEdName.Disable();
314 	}
315 
316 	DoEnable_Impl();
317 	return 0;
318 }
319 
320 //------------------------------------------------------------------------
321 IMPL_LINK( ScInsertTableDlg, ChoiceHdl_Impl, RadioButton*, EMPTYARG )
322 {
323 	if ( aBtnNew.IsChecked() )
324 		SetNewTable_Impl();
325 	else
326 		SetFromTo_Impl();
327 
328 	DoEnable_Impl();
329 	return 0;
330 }
331 
332 //------------------------------------------------------------------------
333 
334 IMPL_LINK( ScInsertTableDlg, BrowseHdl_Impl, PushButton*, EMPTYARG )
335 {
336     if ( pDocInserter )
337         delete pDocInserter;
338     pDocInserter = new ::sfx2::DocumentInserter(
339             0, String::CreateFromAscii( ScDocShell::Factory().GetShortName() ) );
340     pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
341     return 0;
342 }
343 
344 //------------------------------------------------------------------------
345 
346 IMPL_LINK( ScInsertTableDlg, SelectHdl_Impl, MultiListBox*, EMPTYARG )
347 {
348 	DoEnable_Impl();
349 	return 0;
350 }
351 
352 //------------------------------------------------------------------------
353 
354 void ScInsertTableDlg::DoEnable_Impl()
355 {
356 	if ( aBtnNew.IsChecked() || ( pDocShTables && aLbTables.GetSelectEntryCount() ) )
357 		aBtnOk.Enable();
358 	else
359 		aBtnOk.Disable();
360 }
361 
362 IMPL_LINK( ScInsertTableDlg, DoEnterHdl, PushButton*, EMPTYARG )
363 {
364 	if(nTableCount > 1 || rDoc.ValidTabName(aEdName.GetText()))
365 	{
366 		EndDialog(RET_OK);
367 	}
368 	else
369 	{
370 		String aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME ) );
371 		(void)ErrorBox( this,WinBits( WB_OK | WB_DEF_OK ),aErrMsg).Execute();
372 	}
373 	return 0;
374 }
375 
376 IMPL_LINK( ScInsertTableDlg, BrowseTimeoutHdl, Timer*, EMPTYARG )
377 {
378     bMustClose = true;
379     BrowseHdl_Impl( &aBtnBrowse );
380     return 0;
381 }
382 
383 IMPL_LINK( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
384 {
385     if ( ERRCODE_NONE == _pFileDlg->GetError() )
386     {
387         SfxMedium* pMed = pDocInserter->CreateMedium();
388         if ( pMed )
389         {
390             //  ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
391             SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
392 
393             if ( pDocShTables )
394                 pDocShTables->DoClose();        // delete passiert beim Zuweisen auf die Ref
395 
396             pMed->UseInteractionHandler( sal_True );    // to enable the filter options dialog
397 
398             pDocShTables = new ScDocShell;
399             aDocShTablesRef = pDocShTables;
400 
401             Pointer aOldPtr( GetPointer() );
402             SetPointer( Pointer( POINTER_WAIT ) );
403             pDocShTables->DoLoad( pMed );
404             SetPointer( aOldPtr );
405 
406             sal_uLong nErr = pDocShTables->GetErrorCode();
407             if ( nErr )
408                 ErrorHandler::HandleError( nErr );              // auch Warnings
409 
410             if ( !pDocShTables->GetError() )                    // nur Errors
411             {
412                 FillTables_Impl( pDocShTables->GetDocument() );
413                 aFtPath.SetText( pDocShTables->GetTitle( SFX_TITLE_FULLNAME ) );
414             }
415             else
416             {
417                 pDocShTables->DoClose();
418                 aDocShTablesRef.Clear();
419                 pDocShTables = NULL;
420 
421                 FillTables_Impl( NULL );
422                 aFtPath.SetText( EMPTY_STRING );
423             }
424         }
425 
426         DoEnable_Impl();
427     }
428     else if ( bMustClose )
429         // execute slot FID_INS_TABLE_EXT and cancel file dialog
430         EndDialog( RET_CANCEL );
431 
432     return 0;
433 }
434 
435