Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #undef SC_DLLIMPLEMENTATION
22 :
23 :
24 :
25 : #include <sfx2/app.hxx>
26 : #include <sfx2/docfile.hxx>
27 : #include <sfx2/docinsert.hxx>
28 : #include <sfx2/filedlghelper.hxx>
29 : #include <svtools/ehdl.hxx>
30 : #include <svtools/sfxecode.hxx>
31 : #include <vcl/msgbox.hxx>
32 :
33 : #include "global.hxx"
34 : #include "docsh.hxx"
35 : #include "viewdata.hxx"
36 : #include "scresid.hxx"
37 : #include "globstr.hrc"
38 :
39 : #define SC_INSTBDLG_CXX
40 : #include "instbdlg.hxx"
41 :
42 0 : ScInsertTableDlg::ScInsertTableDlg( Window* pParent, ScViewData& rData, SCTAB nTabCount, bool bFromFile )
43 : : ModalDialog(pParent, "InsertSheetDialog", "modules/scalc/ui/insertsheet.ui")
44 : , rViewData(rData)
45 0 : , rDoc(*rData.GetDocument())
46 : , pDocShTables(NULL)
47 : , pDocInserter(NULL)
48 : , bMustClose(false)
49 : , nSelTabIndex(0)
50 0 : , nTableCount(nTabCount)
51 : {
52 0 : get(m_pBtnBefore, "before");
53 0 : get(m_pBtnBehind, "after");
54 0 : get(m_pBtnNew, "new");
55 0 : get(m_pBtnFromFile, "fromfile");
56 0 : get(m_pFtCount, "countft");
57 0 : get(m_pNfCount, "countnf");
58 0 : get(m_pFtName, "nameft");
59 0 : get(m_pEdName, "nameed");
60 0 : m_sSheetDotDotDot = m_pEdName->GetText();
61 0 : get(m_pLbTables, "tables");
62 0 : m_pLbTables->SetDropDownLineCount(8);
63 0 : get(m_pFtPath, "path");
64 0 : get(m_pBtnBrowse, "browse");
65 0 : get(m_pBtnLink, "link");
66 0 : get(m_pBtnOk, "ok");
67 0 : Init_Impl( bFromFile );
68 0 : }
69 :
70 0 : ScInsertTableDlg::~ScInsertTableDlg()
71 : {
72 0 : if (pDocShTables)
73 0 : pDocShTables->DoClose();
74 0 : delete pDocInserter;
75 0 : }
76 :
77 0 : void ScInsertTableDlg::Init_Impl( bool bFromFile )
78 : {
79 0 : m_pLbTables->EnableMultiSelection(true);
80 0 : m_pBtnBrowse->SetClickHdl( LINK( this, ScInsertTableDlg, BrowseHdl_Impl ) );
81 0 : m_pBtnNew->SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
82 0 : m_pBtnFromFile->SetClickHdl( LINK( this, ScInsertTableDlg, ChoiceHdl_Impl ) );
83 0 : m_pLbTables->SetSelectHdl( LINK( this, ScInsertTableDlg, SelectHdl_Impl ) );
84 0 : m_pNfCount->SetModifyHdl( LINK( this, ScInsertTableDlg, CountHdl_Impl));
85 0 : m_pBtnOk->SetClickHdl( LINK( this, ScInsertTableDlg, DoEnterHdl ));
86 0 : m_pBtnBefore->Check();
87 :
88 0 : m_pNfCount->SetText( OUString::number(nTableCount) );
89 0 : m_pNfCount->SetMax( MAXTAB - rDoc.GetTableCount() + 1 );
90 :
91 0 : if(nTableCount==1)
92 : {
93 0 : OUString aName;
94 0 : rDoc.CreateValidTabName( aName );
95 0 : m_pEdName->SetText( aName );
96 : }
97 : else
98 : {
99 0 : m_pEdName->SetText(m_sSheetDotDotDot);
100 0 : m_pFtName->Disable();
101 0 : m_pEdName->Disable();
102 : }
103 :
104 0 : bool bShared = ( rViewData.GetDocShell() ? rViewData.GetDocShell()->IsDocShared() : false );
105 :
106 0 : if ( !bFromFile || bShared )
107 : {
108 0 : m_pBtnNew->Check();
109 0 : SetNewTable_Impl();
110 0 : if ( bShared )
111 : {
112 0 : m_pBtnFromFile->Disable();
113 : }
114 : }
115 : else
116 : {
117 0 : m_pBtnFromFile->Check();
118 0 : SetFromTo_Impl();
119 :
120 0 : aBrowseTimer.SetTimeoutHdl( LINK( this, ScInsertTableDlg, BrowseTimeoutHdl ) );
121 0 : aBrowseTimer.SetTimeout( 200 );
122 : }
123 0 : }
124 :
125 0 : short ScInsertTableDlg::Execute()
126 : {
127 : // set Parent of DocumentInserter and Doc-Manager
128 0 : Window* pOldDefParent = Application::GetDefDialogParent();
129 0 : Application::SetDefDialogParent( this );
130 :
131 0 : if ( m_pBtnFromFile->IsChecked() )
132 0 : aBrowseTimer.Start();
133 :
134 0 : short nRet = ModalDialog::Execute();
135 0 : Application::SetDefDialogParent( pOldDefParent );
136 0 : return nRet;
137 : }
138 :
139 0 : void ScInsertTableDlg::SetNewTable_Impl()
140 : {
141 0 : if (m_pBtnNew->IsChecked() )
142 : {
143 0 : m_pNfCount->Enable();
144 0 : m_pFtCount->Enable();
145 0 : m_pLbTables->Disable();
146 0 : m_pFtPath->Disable();
147 0 : m_pBtnBrowse->Disable();
148 0 : m_pBtnLink->Disable();
149 :
150 0 : if(nTableCount==1)
151 : {
152 0 : m_pEdName->Enable();
153 0 : m_pFtName->Enable();
154 : }
155 : }
156 0 : }
157 :
158 0 : void ScInsertTableDlg::SetFromTo_Impl()
159 : {
160 0 : if (m_pBtnFromFile->IsChecked() )
161 : {
162 0 : m_pEdName->Disable();
163 0 : m_pFtName->Disable();
164 0 : m_pFtCount->Disable();
165 0 : m_pNfCount->Disable();
166 0 : m_pLbTables->Enable();
167 0 : m_pFtPath->Enable();
168 0 : m_pBtnBrowse->Enable();
169 0 : m_pBtnLink->Enable();
170 : }
171 0 : }
172 :
173 0 : void ScInsertTableDlg::FillTables_Impl( ScDocument* pSrcDoc )
174 : {
175 0 : m_pLbTables->SetUpdateMode( false );
176 0 : m_pLbTables->Clear();
177 :
178 0 : if ( pSrcDoc )
179 : {
180 0 : SCTAB nCount = pSrcDoc->GetTableCount();
181 0 : OUString aName;
182 :
183 0 : for ( SCTAB i=0; i<nCount; i++ )
184 : {
185 0 : pSrcDoc->GetName( i, aName );
186 0 : m_pLbTables->InsertEntry( aName );
187 0 : }
188 : }
189 :
190 0 : m_pLbTables->SetUpdateMode( true );
191 :
192 0 : if(m_pLbTables->GetEntryCount()==1)
193 0 : m_pLbTables->SelectEntryPos(0);
194 0 : }
195 :
196 0 : const OUString* ScInsertTableDlg::GetFirstTable( sal_uInt16* pN )
197 : {
198 0 : const OUString* pStr = NULL;
199 :
200 0 : if ( m_pBtnNew->IsChecked() )
201 : {
202 0 : aStrCurSelTable = m_pEdName->GetText();
203 0 : pStr = &aStrCurSelTable;
204 : }
205 0 : else if ( nSelTabIndex < m_pLbTables->GetSelectEntryCount() )
206 : {
207 0 : aStrCurSelTable = m_pLbTables->GetSelectEntry( 0 );
208 0 : pStr = &aStrCurSelTable;
209 0 : if ( pN )
210 0 : *pN = m_pLbTables->GetSelectEntryPos( 0 );
211 0 : nSelTabIndex = 1;
212 : }
213 :
214 0 : return pStr;
215 : }
216 :
217 0 : const OUString* ScInsertTableDlg::GetNextTable( sal_uInt16* pN )
218 : {
219 0 : const OUString* pStr = NULL;
220 :
221 0 : if ( !m_pBtnNew->IsChecked() && nSelTabIndex < m_pLbTables->GetSelectEntryCount() )
222 : {
223 0 : aStrCurSelTable = m_pLbTables->GetSelectEntry( nSelTabIndex );
224 0 : pStr = &aStrCurSelTable;
225 0 : if ( pN )
226 0 : *pN = m_pLbTables->GetSelectEntryPos( nSelTabIndex );
227 0 : nSelTabIndex++;
228 : }
229 :
230 0 : return pStr;
231 : }
232 :
233 : // Handler:
234 :
235 :
236 0 : IMPL_LINK_NOARG(ScInsertTableDlg, CountHdl_Impl)
237 : {
238 0 : nTableCount = static_cast<SCTAB>(m_pNfCount->GetValue());
239 0 : if ( nTableCount==1)
240 : {
241 0 : OUString aName;
242 0 : rDoc.CreateValidTabName( aName );
243 0 : m_pEdName->SetText( aName );
244 0 : m_pFtName->Enable();
245 0 : m_pEdName->Enable();
246 : }
247 : else
248 : {
249 0 : m_pEdName->SetText(m_sSheetDotDotDot);
250 0 : m_pFtName->Disable();
251 0 : m_pEdName->Disable();
252 : }
253 :
254 0 : DoEnable_Impl();
255 0 : return 0;
256 : }
257 :
258 :
259 0 : IMPL_LINK_NOARG(ScInsertTableDlg, ChoiceHdl_Impl)
260 : {
261 0 : if ( m_pBtnNew->IsChecked() )
262 0 : SetNewTable_Impl();
263 : else
264 0 : SetFromTo_Impl();
265 :
266 0 : DoEnable_Impl();
267 0 : return 0;
268 : }
269 :
270 0 : IMPL_LINK_NOARG(ScInsertTableDlg, BrowseHdl_Impl)
271 : {
272 0 : if ( pDocInserter )
273 0 : delete pDocInserter;
274 : pDocInserter = new ::sfx2::DocumentInserter(
275 0 : OUString::createFromAscii( ScDocShell::Factory().GetShortName() ) );
276 0 : pDocInserter->StartExecuteModal( LINK( this, ScInsertTableDlg, DialogClosedHdl ) );
277 0 : return 0;
278 : }
279 :
280 0 : IMPL_LINK_NOARG(ScInsertTableDlg, SelectHdl_Impl)
281 : {
282 0 : DoEnable_Impl();
283 0 : return 0;
284 : }
285 :
286 0 : void ScInsertTableDlg::DoEnable_Impl()
287 : {
288 0 : if ( m_pBtnNew->IsChecked() || ( pDocShTables && m_pLbTables->GetSelectEntryCount() ) )
289 0 : m_pBtnOk->Enable();
290 : else
291 0 : m_pBtnOk->Disable();
292 0 : }
293 :
294 0 : IMPL_LINK_NOARG(ScInsertTableDlg, DoEnterHdl)
295 : {
296 0 : if(nTableCount > 1 || rDoc.ValidTabName(m_pEdName->GetText()))
297 : {
298 0 : EndDialog(RET_OK);
299 : }
300 : else
301 : {
302 0 : OUString aErrMsg ( ScGlobal::GetRscString( STR_INVALIDTABNAME ) );
303 0 : (void)ErrorBox( this,WinBits( WB_OK | WB_DEF_OK ),aErrMsg).Execute();
304 : }
305 0 : return 0;
306 : }
307 :
308 0 : IMPL_LINK_NOARG(ScInsertTableDlg, BrowseTimeoutHdl)
309 : {
310 0 : bMustClose = true;
311 0 : BrowseHdl_Impl(m_pBtnBrowse);
312 0 : return 0;
313 : }
314 :
315 0 : IMPL_LINK( ScInsertTableDlg, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
316 : {
317 0 : if ( ERRCODE_NONE == _pFileDlg->GetError() )
318 : {
319 0 : SfxMedium* pMed = pDocInserter->CreateMedium();
320 0 : if ( pMed )
321 : {
322 : // ERRCTX_SFX_OPENDOC -> "Fehler beim Laden des Dokumentes"
323 0 : SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
324 :
325 0 : if ( pDocShTables )
326 0 : pDocShTables->DoClose(); // delete passiert beim Zuweisen auf die Ref
327 :
328 0 : pMed->UseInteractionHandler( true ); // to enable the filter options dialog
329 :
330 0 : pDocShTables = new ScDocShell;
331 0 : aDocShTablesRef = pDocShTables;
332 :
333 0 : Pointer aOldPtr( GetPointer() );
334 0 : SetPointer( Pointer( POINTER_WAIT ) );
335 0 : pDocShTables->DoLoad( pMed );
336 0 : SetPointer( aOldPtr );
337 :
338 0 : sal_uLong nErr = pDocShTables->GetErrorCode();
339 0 : if ( nErr )
340 0 : ErrorHandler::HandleError( nErr ); // auch Warnings
341 :
342 0 : if ( !pDocShTables->GetError() ) // nur Errors
343 : {
344 0 : FillTables_Impl( pDocShTables->GetDocument() );
345 0 : m_pFtPath->SetText( pDocShTables->GetTitle( SFX_TITLE_FULLNAME ) );
346 : }
347 : else
348 : {
349 0 : pDocShTables->DoClose();
350 0 : aDocShTablesRef.Clear();
351 0 : pDocShTables = NULL;
352 :
353 0 : FillTables_Impl( NULL );
354 0 : m_pFtPath->SetText( EMPTY_OUSTRING );
355 0 : }
356 : }
357 :
358 0 : DoEnable_Impl();
359 : }
360 0 : else if ( bMustClose )
361 : // execute slot FID_INS_TABLE_EXT and cancel file dialog
362 0 : EndDialog( RET_CANCEL );
363 :
364 0 : return 0;
365 0 : }
366 :
367 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|