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