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