Branch data 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 : : #include <memory>
21 : : #include <vcl/msgbox.hxx>
22 : :
23 : : #include <macrodlg.hxx>
24 : : #include <macrodlg.hrc>
25 : : #include <basidesh.hrc>
26 : : #include <basidesh.hxx>
27 : : #include <baside2.hrc> // ID's for Images
28 : : #include <basobj.hxx>
29 : : #include <baside3.hxx>
30 : :
31 : : #include <iderdll.hxx>
32 : : #include <iderdll2.hxx>
33 : : #include <iderid.hxx>
34 : :
35 : : #include <moduldlg.hxx>
36 : : #include <basic/basmgr.hxx>
37 : : #include <basic/sbmeth.hxx>
38 : : #include <basic/sbmod.hxx>
39 : : #include <basic/sbx.hxx>
40 : :
41 : : #include <bastypes.hxx>
42 : : #include <sbxitem.hxx>
43 : : #include <sfx2/app.hxx>
44 : : #include <sfx2/dispatch.hxx>
45 : : #include <sfx2/minfitem.hxx>
46 : : #include <sfx2/request.hxx>
47 : :
48 : : #include <com/sun/star/script/XLibraryContainer2.hpp>
49 : : #include <com/sun/star/document/MacroExecMode.hpp>
50 : :
51 : : #include <map>
52 : : using ::std::map;
53 : : using ::std::pair;
54 : :
55 : : using namespace ::com::sun::star;
56 : : using namespace ::com::sun::star::uno;
57 : :
58 : 0 : MacroChooser::MacroChooser( Window* pParnt, bool bCreateEntries ) :
59 : : SfxModalDialog( pParnt, IDEResId( RID_MACROCHOOSER ) ),
60 : : aMacroNameTxt( this, IDEResId( RID_TXT_MACRONAME ) ),
61 : : aMacroNameEdit( this, IDEResId( RID_ED_MACRONAME ) ),
62 : : aMacroFromTxT( this, IDEResId( RID_TXT_MACROFROM ) ),
63 : : aMacrosSaveInTxt( this, IDEResId( RID_TXT_SAVEMACRO ) ),
64 : : aBasicBox( this, IDEResId( RID_CTRL_LIB ) ),
65 : : aMacrosInTxt( this, IDEResId( RID_TXT_MACROSIN ) ),
66 : : aMacroBox( this, IDEResId( RID_CTRL_MACRO ) ),
67 : : aRunButton( this, IDEResId( RID_PB_RUN ) ),
68 : : aCloseButton( this, IDEResId( RID_PB_CLOSE ) ),
69 : : aAssignButton( this, IDEResId( RID_PB_ASSIGN ) ),
70 : : aEditButton( this, IDEResId( RID_PB_EDIT ) ),
71 : : aNewDelButton( this, IDEResId( RID_PB_DEL ) ),
72 : : aOrganizeButton( this, IDEResId( RID_PB_ORG ) ),
73 : : aHelpButton( this, IDEResId( RID_PB_HELP ) ),
74 : : aNewLibButton( this, IDEResId( RID_PB_NEWLIB ) ),
75 : 0 : aNewModButton( this, IDEResId( RID_PB_NEWMOD ) )
76 : : {
77 : 0 : FreeResource();
78 : :
79 : 0 : nMode = MACROCHOOSER_ALL;
80 : 0 : bNewDelIsDel = true;
81 : :
82 : : // the Sfx doesn't aske the BasicManger whether modified or not
83 : : // => start saving in case of a change without a into the BasicIDE.
84 : 0 : bForceStoreBasic = false;
85 : :
86 : 0 : aMacrosInTxtBaseStr = aMacrosInTxt.GetText();
87 : :
88 : 0 : aMacroBox.SetSelectionMode( SINGLE_SELECTION );
89 : 0 : aMacroBox.SetHighlightRange(); // select over the whole width
90 : :
91 : 0 : aRunButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
92 : 0 : aCloseButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
93 : 0 : aAssignButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
94 : 0 : aEditButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
95 : 0 : aNewDelButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
96 : 0 : aOrganizeButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
97 : :
98 : : // Buttons only for MACROCHOOSER_RECORDING
99 : 0 : aNewLibButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
100 : 0 : aNewModButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
101 : 0 : aNewLibButton.Hide(); // default
102 : 0 : aNewModButton.Hide(); // default
103 : 0 : aMacrosSaveInTxt.Hide(); // default
104 : :
105 : 0 : aMacrosInTxt.SetStyle( WB_NOMULTILINE | WB_PATHELLIPSIS );
106 : :
107 : 0 : aMacroNameEdit.SetModifyHdl( LINK( this, MacroChooser, EditModifyHdl ) );
108 : :
109 : 0 : aBasicBox.SetSelectHdl( LINK( this, MacroChooser, BasicSelectHdl ) );
110 : :
111 : 0 : aMacroBox.SetDoubleClickHdl( LINK( this, MacroChooser, MacroDoubleClickHdl ) );
112 : 0 : aMacroBox.SetSelectHdl( LINK( this, MacroChooser, MacroSelectHdl ) );
113 : :
114 : 0 : aBasicBox.SetMode( BROWSEMODE_MODULES );
115 : : aBasicBox.SetStyle( WB_TABSTOP | WB_BORDER |
116 : : WB_HASLINES | WB_HASLINESATROOT |
117 : : WB_HASBUTTONS | WB_HASBUTTONSATROOT |
118 : 0 : WB_HSCROLL );
119 : :
120 : 0 : BasicIDEShell* pIDEShell = BasicIDEGlobals::GetShell();
121 : 0 : SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
122 : 0 : SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
123 : 0 : if( pDispatcher )
124 : : {
125 : 0 : pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
126 : : }
127 : :
128 : 0 : if ( bCreateEntries )
129 : 0 : aBasicBox.ScanAllEntries();
130 : 0 : }
131 : :
132 : 0 : MacroChooser::~MacroChooser()
133 : : {
134 : 0 : if ( bForceStoreBasic )
135 : 0 : SFX_APP()->SaveBasicAndDialogContainer();
136 : 0 : }
137 : :
138 : 0 : void MacroChooser::StoreMacroDescription()
139 : : {
140 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( aBasicBox.FirstSelected() ) );
141 : 0 : String aMethodName;
142 : 0 : SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
143 : 0 : if ( pEntry )
144 : 0 : aMethodName = aMacroBox.GetEntryText( pEntry );
145 : : else
146 : 0 : aMethodName = aMacroNameEdit.GetText();
147 : 0 : if ( aMethodName.Len() )
148 : : {
149 : 0 : aDesc.SetMethodName( aMethodName );
150 : 0 : aDesc.SetType( OBJ_TYPE_METHOD );
151 : : }
152 : :
153 : 0 : BasicIDEData* pData = BasicIDEGlobals::GetExtraData();
154 : 0 : if ( pData )
155 : 0 : pData->SetLastEntryDescriptor( aDesc );
156 : 0 : }
157 : :
158 : 0 : void MacroChooser::RestoreMacroDescription()
159 : : {
160 : 0 : BasicEntryDescriptor aDesc;
161 : 0 : BasicIDEShell* pIDEShell = BasicIDEGlobals::GetShell();
162 : 0 : if ( pIDEShell )
163 : : {
164 : 0 : IDEBaseWindow* pCurWin = pIDEShell->GetCurWindow();
165 : 0 : if ( pCurWin )
166 : 0 : aDesc = pCurWin->CreateEntryDescriptor();
167 : : }
168 : : else
169 : : {
170 : 0 : BasicIDEData* pData = BasicIDEGlobals::GetExtraData();
171 : 0 : if ( pData )
172 : 0 : aDesc = pData->GetLastEntryDescriptor();
173 : : }
174 : :
175 : 0 : aBasicBox.SetCurrentEntry( aDesc );
176 : :
177 : 0 : String aLastMacro( aDesc.GetMethodName() );
178 : 0 : if ( aLastMacro.Len() )
179 : : {
180 : : // find entry in macro box
181 : 0 : SvLBoxEntry* pEntry = 0;
182 : 0 : sal_uLong nPos = 0;
183 : 0 : SvLBoxEntry* pE = aMacroBox.GetEntry( nPos );
184 : 0 : while ( pE )
185 : : {
186 : 0 : if ( aMacroBox.GetEntryText( pE ) == aLastMacro )
187 : : {
188 : 0 : pEntry = pE;
189 : 0 : break;
190 : : }
191 : 0 : pE = aMacroBox.GetEntry( ++nPos );
192 : : }
193 : :
194 : 0 : if ( pEntry )
195 : 0 : aMacroBox.SetCurEntry( pEntry );
196 : : else
197 : : {
198 : 0 : aMacroNameEdit.SetText( aLastMacro );
199 : 0 : aMacroNameEdit.SetSelection( Selection( 0, 0 ) );
200 : : }
201 : 0 : }
202 : 0 : }
203 : :
204 : 0 : short MacroChooser::Execute()
205 : : {
206 : 0 : RestoreMacroDescription();
207 : 0 : aRunButton.GrabFocus();
208 : :
209 : : // #104198 Check if "wrong" document is active
210 : 0 : SvLBoxEntry* pSelectedEntry = aBasicBox.GetCurEntry();
211 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pSelectedEntry ) );
212 : 0 : const ScriptDocument& rSelectedDoc( aDesc.GetDocument() );
213 : :
214 : : // App Basic is always ok, so only check if shell was found
215 : 0 : if( rSelectedDoc.isDocument() && !rSelectedDoc.isActive() )
216 : : {
217 : : // Search for the right entry
218 : 0 : sal_uLong nRootPos = 0;
219 : 0 : SvLBoxEntry* pRootEntry = aBasicBox.GetEntry( nRootPos );
220 : 0 : while( pRootEntry )
221 : : {
222 : 0 : BasicEntryDescriptor aCmpDesc( aBasicBox.GetEntryDescriptor( pRootEntry ) );
223 : 0 : const ScriptDocument& rCmpDoc( aCmpDesc.GetDocument() );
224 : 0 : if ( rCmpDoc.isDocument() && rCmpDoc.isActive() )
225 : : {
226 : 0 : SvLBoxEntry* pEntry = pRootEntry;
227 : 0 : SvLBoxEntry* pLastValid = pEntry;
228 : 0 : while ( pEntry )
229 : : {
230 : 0 : pLastValid = pEntry;
231 : 0 : pEntry = aBasicBox.FirstChild( pEntry );
232 : : }
233 : 0 : if( pLastValid )
234 : 0 : aBasicBox.SetCurEntry( pLastValid );
235 : : }
236 : 0 : pRootEntry = aBasicBox.GetEntry( ++nRootPos );
237 : 0 : }
238 : : }
239 : :
240 : 0 : CheckButtons();
241 : 0 : UpdateFields();
242 : :
243 : 0 : if ( StarBASIC::IsRunning() )
244 : 0 : aCloseButton.GrabFocus();
245 : :
246 : 0 : Window* pPrevDlgParent = Application::GetDefDialogParent();
247 : 0 : Application::SetDefDialogParent( this );
248 : 0 : short nRet = ModalDialog::Execute();
249 : : // #57314# If the BasicIDE has been activated, don't reset the DefModalDialogParent to the inactive document.
250 : 0 : if ( Application::GetDefDialogParent() == this )
251 : 0 : Application::SetDefDialogParent( pPrevDlgParent );
252 : 0 : return nRet;
253 : : }
254 : :
255 : :
256 : 0 : void MacroChooser::EnableButton( Button& rButton, bool bEnable )
257 : : {
258 : 0 : if ( bEnable )
259 : : {
260 : 0 : if ( nMode == MACROCHOOSER_CHOOSEONLY || nMode == MACROCHOOSER_RECORDING )
261 : : {
262 : 0 : if ( &rButton == &aRunButton )
263 : 0 : rButton.Enable();
264 : : else
265 : 0 : rButton.Disable();
266 : : }
267 : : else
268 : 0 : rButton.Enable();
269 : : }
270 : : else
271 : 0 : rButton.Disable();
272 : 0 : }
273 : :
274 : :
275 : :
276 : :
277 : 0 : SbMethod* MacroChooser::GetMacro()
278 : : {
279 : 0 : SbMethod* pMethod = 0;
280 : 0 : SbModule* pModule = aBasicBox.FindModule( aBasicBox.GetCurEntry() );
281 : 0 : if ( pModule )
282 : : {
283 : 0 : SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
284 : 0 : if ( pEntry )
285 : : {
286 : 0 : String aMacroName( aMacroBox.GetEntryText( pEntry ) );
287 : 0 : pMethod = (SbMethod*)pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD );
288 : : }
289 : : }
290 : 0 : return pMethod;
291 : : }
292 : :
293 : :
294 : :
295 : 0 : void MacroChooser::DeleteMacro()
296 : : {
297 : 0 : SbMethod* pMethod = GetMacro();
298 : : DBG_ASSERT( pMethod, "DeleteMacro: Kein Macro !" );
299 : 0 : if ( pMethod && QueryDelMacro( pMethod->GetName(), this ) )
300 : : {
301 : 0 : BasicIDEShell* pIDEShell = BasicIDEGlobals::GetShell();
302 : 0 : SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
303 : 0 : SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
304 : 0 : if( pDispatcher )
305 : : {
306 : 0 : pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
307 : : }
308 : :
309 : : // mark current doc as modified:
310 : 0 : StarBASIC* pBasic = BasicIDE::FindBasic( pMethod );
311 : : DBG_ASSERT( pBasic, "Basic?!" );
312 : 0 : BasicManager* pBasMgr = BasicIDE::FindBasicManager( pBasic );
313 : : DBG_ASSERT( pBasMgr, "BasMgr?" );
314 : 0 : ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
315 : 0 : if ( aDocument.isDocument() )
316 : : {
317 : 0 : aDocument.setDocumentModified();
318 : 0 : SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
319 : 0 : if ( pBindings )
320 : 0 : pBindings->Invalidate( SID_SAVEDOC );
321 : : }
322 : :
323 : 0 : SbModule* pModule = pMethod->GetModule();
324 : : DBG_ASSERT( pModule, "DeleteMacro: Kein Modul?!" );
325 : 0 : ::rtl::OUString aSource( pModule->GetSource32() );
326 : : sal_uInt16 nStart, nEnd;
327 : 0 : pMethod->GetLineRange( nStart, nEnd );
328 : 0 : pModule->GetMethods()->Remove( pMethod );
329 : 0 : CutLines( aSource, nStart-1, nEnd-nStart+1, true );
330 : 0 : pModule->SetSource32( aSource );
331 : :
332 : : // update module in library
333 : 0 : String aLibName = pBasic->GetName();
334 : 0 : String aModName = pModule->GetName();
335 : 0 : OSL_VERIFY( aDocument.updateModule( aLibName, aModName, aSource ) );
336 : :
337 : 0 : SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
338 : : DBG_ASSERT( pEntry, "DeleteMacro: Entry ?!" );
339 : 0 : aMacroBox.GetModel()->Remove( pEntry );
340 : 0 : bForceStoreBasic = true;
341 : : }
342 : 0 : }
343 : :
344 : 0 : SbMethod* MacroChooser::CreateMacro()
345 : : {
346 : 0 : SbMethod* pMethod = 0;
347 : 0 : SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
348 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
349 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
350 : : OSL_ENSURE( aDocument.isAlive(), "MacroChooser::CreateMacro: no document!" );
351 : 0 : if ( !aDocument.isAlive() )
352 : 0 : return NULL;
353 : :
354 : 0 : String aLibName( aDesc.GetLibName() );
355 : :
356 : 0 : if ( !aLibName.Len() )
357 : 0 : aLibName = rtl::OUString("Standard");
358 : :
359 : 0 : aDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
360 : :
361 : 0 : ::rtl::OUString aOULibName( aLibName );
362 : 0 : Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
363 : 0 : if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
364 : 0 : xModLibContainer->loadLibrary( aOULibName );
365 : 0 : Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ) );
366 : 0 : if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
367 : 0 : xDlgLibContainer->loadLibrary( aOULibName );
368 : :
369 : 0 : BasicManager* pBasMgr = aDocument.getBasicManager();
370 : 0 : StarBASIC* pBasic = pBasMgr ? pBasMgr->GetLib( aLibName ) : 0;
371 : 0 : if ( pBasic )
372 : : {
373 : 0 : SbModule* pModule = 0;
374 : 0 : String aModName( aDesc.GetName() );
375 : 0 : if ( aModName.Len() )
376 : : {
377 : : // extract the module name from the string like "Sheet1 (Example1)"
378 : 0 : if( aDesc.GetLibSubName() == IDE_RESSTR(RID_STR_DOCUMENT_OBJECTS) )
379 : : {
380 : 0 : sal_uInt16 nIndex = 0;
381 : 0 : aModName = aModName.GetToken( 0, ' ', nIndex );
382 : : }
383 : 0 : pModule = pBasic->FindModule( aModName );
384 : : }
385 : 0 : else if ( pBasic->GetModules()->Count() )
386 : 0 : pModule = (SbModule*)pBasic->GetModules()->Get( 0 );
387 : :
388 : 0 : if ( !pModule )
389 : : {
390 : : pModule = createModImpl( static_cast<Window*>( this ),
391 : 0 : aDocument, aBasicBox, aLibName, aModName );
392 : : }
393 : :
394 : 0 : String aSubName = aMacroNameEdit.GetText();
395 : : DBG_ASSERT( !pModule || !pModule->GetMethods()->Find( aSubName, SbxCLASS_METHOD ), "Macro existiert schon!" );
396 : 0 : pMethod = pModule ? BasicIDE::CreateMacro( pModule, aSubName ) : NULL;
397 : : }
398 : :
399 : 0 : return pMethod;
400 : : }
401 : :
402 : 0 : void MacroChooser::SaveSetCurEntry( SvTreeListBox& rBox, SvLBoxEntry* pEntry )
403 : : {
404 : : // the edit would be killed by the highlight otherwise:
405 : :
406 : 0 : String aSaveText( aMacroNameEdit.GetText() );
407 : 0 : Selection aCurSel( aMacroNameEdit.GetSelection() );
408 : :
409 : 0 : rBox.SetCurEntry( pEntry );
410 : 0 : aMacroNameEdit.SetText( aSaveText );
411 : 0 : aMacroNameEdit.SetSelection( aCurSel );
412 : 0 : }
413 : :
414 : 0 : void MacroChooser::CheckButtons()
415 : : {
416 : 0 : SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
417 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
418 : 0 : SvLBoxEntry* pMacroEntry = aMacroBox.FirstSelected();
419 : 0 : SbMethod* pMethod = GetMacro();
420 : :
421 : : // check, if corresponding libraries are readonly
422 : 0 : bool bReadOnly = false;
423 : 0 : sal_uInt16 nDepth = pCurEntry ? aBasicBox.GetModel()->GetDepth( pCurEntry ) : 0;
424 : 0 : if ( nDepth == 1 || nDepth == 2 )
425 : : {
426 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
427 : 0 : ::rtl::OUString aOULibName( aDesc.GetLibName() );
428 : 0 : Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
429 : 0 : Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
430 : 0 : if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
431 : 0 : ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) )
432 : : {
433 : 0 : bReadOnly = true;
434 : 0 : }
435 : : }
436 : :
437 : 0 : if ( nMode != MACROCHOOSER_RECORDING )
438 : : {
439 : : // Run...
440 : 0 : bool bEnable = pMethod ? true : false;
441 : 0 : if ( ( nMode != MACROCHOOSER_CHOOSEONLY ) && StarBASIC::IsRunning() )
442 : 0 : bEnable = false;
443 : 0 : EnableButton( aRunButton, bEnable );
444 : : }
445 : :
446 : : // organising still possible?
447 : :
448 : : // Assign...
449 : 0 : EnableButton( aAssignButton, pMethod ? true : false );
450 : :
451 : : // Edit...
452 : 0 : EnableButton( aEditButton, pMacroEntry ? true : false );
453 : :
454 : : // aOrganizeButton
455 : 0 : EnableButton( aOrganizeButton, !StarBASIC::IsRunning() && ( nMode == MACROCHOOSER_ALL ));
456 : :
457 : : // aNewDelButton....
458 : 0 : bool bProtected = aBasicBox.IsEntryProtected( pCurEntry );
459 : 0 : bool bShare = ( aDesc.GetLocation() == LIBRARY_LOCATION_SHARE );
460 : : EnableButton( aNewDelButton,
461 : 0 : !StarBASIC::IsRunning() && ( nMode == MACROCHOOSER_ALL ) && !bProtected && !bReadOnly && !bShare );
462 : 0 : bool bPrev = bNewDelIsDel;
463 : 0 : bNewDelIsDel = pMethod ? true : false;
464 : 0 : if ( ( bPrev != bNewDelIsDel ) && ( nMode == MACROCHOOSER_ALL ) )
465 : : {
466 : 0 : String aBtnText( bNewDelIsDel ? IDEResId( RID_STR_BTNDEL) : IDEResId( RID_STR_BTNNEW ) );
467 : 0 : aNewDelButton.SetText( aBtnText );
468 : : }
469 : :
470 : 0 : if ( nMode == MACROCHOOSER_RECORDING )
471 : : {
472 : : // save button
473 : 0 : if ( !bProtected && !bReadOnly && !bShare )
474 : 0 : aRunButton.Enable();
475 : : else
476 : 0 : aRunButton.Disable();
477 : :
478 : : // new library button
479 : 0 : if ( !bShare )
480 : 0 : aNewLibButton.Enable();
481 : : else
482 : 0 : aNewLibButton.Disable();
483 : :
484 : : // new module button
485 : 0 : if ( !bProtected && !bReadOnly && !bShare )
486 : 0 : aNewModButton.Enable();
487 : : else
488 : 0 : aNewModButton.Disable();
489 : 0 : }
490 : 0 : }
491 : :
492 : :
493 : :
494 : 0 : IMPL_LINK_NOARG_INLINE_START(MacroChooser, MacroDoubleClickHdl)
495 : : {
496 : 0 : StoreMacroDescription();
497 : 0 : if ( nMode == MACROCHOOSER_RECORDING )
498 : : {
499 : 0 : SbMethod* pMethod = GetMacro();
500 : 0 : if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
501 : 0 : return 0;
502 : : }
503 : :
504 : 0 : EndDialog( MACRO_OK_RUN );
505 : 0 : return 0;
506 : : }
507 : 0 : IMPL_LINK_NOARG_INLINE_END(MacroChooser, MacroDoubleClickHdl)
508 : :
509 : 0 : IMPL_LINK( MacroChooser, MacroSelectHdl, SvTreeListBox *, pBox )
510 : : {
511 : : // Is also called if deselected!
512 : : // Two function calls in every SelectHdl because
513 : : // there's no separate DeselectHDL.
514 : : // So find out if select or deselect:
515 : 0 : if ( pBox->IsSelected( pBox->GetHdlEntry() ) )
516 : : {
517 : 0 : UpdateFields();
518 : 0 : CheckButtons();
519 : : }
520 : 0 : return 0;
521 : : }
522 : :
523 : 0 : IMPL_LINK( MacroChooser, BasicSelectHdl, SvTreeListBox *, pBox )
524 : : {
525 : : // Is also called if deselected!
526 : : // Two function calls in every SelectHdl because
527 : : // there's no separate DeselectHDL.
528 : : // So find out if select or deselect:
529 : 0 : if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
530 : 0 : return 0;
531 : :
532 : 0 : SbModule* pModule = aBasicBox.FindModule( aBasicBox.GetCurEntry() );
533 : :
534 : 0 : aMacroBox.Clear();
535 : 0 : if ( pModule )
536 : : {
537 : 0 : String aStr = aMacrosInTxtBaseStr;
538 : 0 : aStr += rtl::OUString(" ");
539 : 0 : aStr += pModule->GetName();
540 : :
541 : 0 : aMacrosInTxt.SetText( aStr );
542 : :
543 : : // The macros should be called in the same order that they
544 : : // are written down in the module.
545 : :
546 : 0 : map< sal_uInt16, SbMethod* > aMacros;
547 : 0 : size_t nMacroCount = pModule->GetMethods()->Count();
548 : 0 : for ( size_t iMeth = 0; iMeth < nMacroCount; iMeth++ )
549 : : {
550 : 0 : SbMethod* pMethod = (SbMethod*)pModule->GetMethods()->Get( iMeth );
551 : 0 : if( pMethod->IsHidden() )
552 : 0 : continue;
553 : : DBG_ASSERT( pMethod, "Methode nicht gefunden! (NULL)" );
554 : : sal_uInt16 nStart, nEnd;
555 : 0 : pMethod->GetLineRange( nStart, nEnd );
556 : 0 : aMacros.insert( map< sal_uInt16, SbMethod*>::value_type( nStart, pMethod ) );
557 : : }
558 : :
559 : 0 : aMacroBox.SetUpdateMode(false);
560 : 0 : for ( map< sal_uInt16, SbMethod* >::iterator it = aMacros.begin(); it != aMacros.end(); ++it )
561 : 0 : aMacroBox.InsertEntry( (*it).second->GetName() );
562 : 0 : aMacroBox.SetUpdateMode(true);
563 : :
564 : 0 : if ( aMacroBox.GetEntryCount() )
565 : : {
566 : 0 : SvLBoxEntry* pEntry = aMacroBox.GetEntry( 0 );
567 : : DBG_ASSERT( pEntry, "Entry ?!" );
568 : 0 : aMacroBox.SetCurEntry( pEntry );
569 : 0 : }
570 : : }
571 : :
572 : 0 : UpdateFields();
573 : 0 : CheckButtons();
574 : 0 : return 0;
575 : : }
576 : :
577 : :
578 : :
579 : 0 : IMPL_LINK( MacroChooser, EditModifyHdl, Edit *, pEdit )
580 : : {
581 : : (void)pEdit;
582 : :
583 : : // select the module in which the macro is put at Neu (new),
584 : : // if BasicManager or Lib is selecting
585 : 0 : SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
586 : 0 : if ( pCurEntry )
587 : : {
588 : 0 : sal_uInt16 nDepth = aBasicBox.GetModel()->GetDepth( pCurEntry );
589 : 0 : if ( ( nDepth == 1 ) && ( aBasicBox.IsEntryProtected( pCurEntry ) ) )
590 : : {
591 : : // then put to the respective Std-Lib...
592 : 0 : SvLBoxEntry* pManagerEntry = aBasicBox.GetModel()->GetParent( pCurEntry );
593 : 0 : pCurEntry = aBasicBox.GetModel()->FirstChild( pManagerEntry );
594 : : }
595 : 0 : if ( nDepth < 2 )
596 : : {
597 : 0 : SvLBoxEntry* pNewEntry = pCurEntry;
598 : 0 : while ( pCurEntry && ( nDepth < 2 ) )
599 : : {
600 : 0 : pCurEntry = aBasicBox.FirstChild( pCurEntry );
601 : 0 : if ( pCurEntry )
602 : : {
603 : 0 : pNewEntry = pCurEntry;
604 : 0 : nDepth = aBasicBox.GetModel()->GetDepth( pCurEntry );
605 : : }
606 : : }
607 : 0 : SaveSetCurEntry( aBasicBox, pNewEntry );
608 : : }
609 : 0 : if ( aMacroBox.GetEntryCount() )
610 : : {
611 : 0 : String aEdtText( aMacroNameEdit.GetText() );
612 : 0 : bool bFound = false;
613 : 0 : for ( sal_uInt16 n = 0; n < aMacroBox.GetEntryCount(); n++ )
614 : : {
615 : 0 : SvLBoxEntry* pEntry = aMacroBox.GetEntry( n );
616 : : DBG_ASSERT( pEntry, "Entry ?!" );
617 : 0 : if ( aMacroBox.GetEntryText( pEntry ).CompareIgnoreCaseToAscii( aEdtText ) == COMPARE_EQUAL )
618 : : {
619 : 0 : SaveSetCurEntry( aMacroBox, pEntry );
620 : 0 : bFound = true;
621 : 0 : break;
622 : : }
623 : : }
624 : 0 : if ( !bFound )
625 : : {
626 : 0 : SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
627 : : // if the entry exists ->Select ->Desription...
628 : 0 : if ( pEntry )
629 : 0 : aMacroBox.Select( pEntry, false );
630 : 0 : }
631 : : }
632 : : }
633 : :
634 : 0 : CheckButtons();
635 : 0 : return 0;
636 : : }
637 : :
638 : :
639 : :
640 : 0 : IMPL_LINK( MacroChooser, ButtonHdl, Button *, pButton )
641 : : {
642 : : // apart from New/Record the Description is done by LoseFocus
643 : 0 : if ( pButton == &aRunButton )
644 : : {
645 : 0 : StoreMacroDescription();
646 : :
647 : : // #116444# check security settings before macro execution
648 : 0 : if ( nMode == MACROCHOOSER_ALL )
649 : : {
650 : 0 : SbMethod* pMethod = GetMacro();
651 : 0 : SbModule* pModule = pMethod ? pMethod->GetModule() : NULL;
652 : 0 : StarBASIC* pBasic = pModule ? (StarBASIC*)pModule->GetParent() : NULL;
653 : 0 : BasicManager* pBasMgr = pBasic ? BasicIDE::FindBasicManager( pBasic ) : NULL;
654 : 0 : if ( pBasMgr )
655 : : {
656 : 0 : ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
657 : 0 : if ( aDocument.isDocument() && !aDocument.allowMacros() )
658 : : {
659 : 0 : WarningBox( this, WB_OK, String( IDEResId( RID_STR_CANNOTRUNMACRO ) ) ).Execute();
660 : 0 : return 0;
661 : 0 : }
662 : : }
663 : : }
664 : 0 : else if ( nMode == MACROCHOOSER_RECORDING )
665 : : {
666 : 0 : if ( !BasicIDE::IsValidSbxName(aMacroNameEdit.GetText()) )
667 : : {
668 : 0 : ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
669 : 0 : aMacroNameEdit.SetSelection( Selection( 0, aMacroNameEdit.GetText().Len() ) );
670 : 0 : aMacroNameEdit.GrabFocus();
671 : 0 : return 0;
672 : : }
673 : :
674 : 0 : SbMethod* pMethod = GetMacro();
675 : 0 : if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
676 : 0 : return 0;
677 : : }
678 : :
679 : 0 : EndDialog( MACRO_OK_RUN );
680 : : }
681 : 0 : else if ( pButton == &aCloseButton )
682 : : {
683 : 0 : StoreMacroDescription();
684 : 0 : EndDialog( MACRO_CLOSE );
685 : : }
686 : 0 : else if ( ( pButton == &aEditButton ) || ( pButton == &aNewDelButton ) )
687 : : {
688 : 0 : SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
689 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
690 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
691 : : DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
692 : 0 : if ( !aDocument.isAlive() )
693 : 0 : return 0;
694 : 0 : BasicManager* pBasMgr = aDocument.getBasicManager();
695 : 0 : String aLib( aDesc.GetLibName() );
696 : 0 : String aMod( aDesc.GetName() );
697 : : // extract the module name from the string like "Sheet1 (Example1)"
698 : 0 : if( aDesc.GetLibSubName() == IDE_RESSTR(RID_STR_DOCUMENT_OBJECTS) )
699 : : {
700 : 0 : sal_uInt16 nIndex = 0;
701 : 0 : aMod = aMod.GetToken( 0, ' ', nIndex );
702 : : }
703 : 0 : String aSub( aDesc.GetMethodName() );
704 : 0 : SfxMacroInfoItem aInfoItem( SID_BASICIDE_ARG_MACROINFO, pBasMgr, aLib, aMod, aSub, String() );
705 : 0 : if ( pButton == &aEditButton )
706 : : {
707 : 0 : SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
708 : 0 : if ( pEntry )
709 : 0 : aInfoItem.SetMethod( aMacroBox.GetEntryText( pEntry ) );
710 : 0 : StoreMacroDescription();
711 : 0 : SfxAllItemSet aArgs( SFX_APP()->GetPool() );
712 : 0 : SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
713 : 0 : SFX_APP()->ExecuteSlot( aRequest );
714 : :
715 : 0 : BasicIDEShell* pIDEShell = BasicIDEGlobals::GetShell();
716 : 0 : SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
717 : 0 : SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
718 : 0 : if( pDispatcher )
719 : 0 : pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
720 : 0 : EndDialog( MACRO_EDIT );
721 : : }
722 : : else
723 : : {
724 : 0 : if ( bNewDelIsDel )
725 : : {
726 : 0 : DeleteMacro();
727 : 0 : BasicIDEShell* pIDEShell = BasicIDEGlobals::GetShell();
728 : 0 : SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
729 : 0 : SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
730 : 0 : if( pDispatcher )
731 : : {
732 : : pDispatcher->Execute( SID_BASICIDE_UPDATEMODULESOURCE,
733 : 0 : SFX_CALLMODE_SYNCHRON, &aInfoItem, 0L );
734 : : }
735 : 0 : CheckButtons();
736 : 0 : UpdateFields();
737 : : //if ( aMacroBox.GetCurEntry() ) // OV-Bug ?
738 : : // aMacroBox.Select( aMacroBox.GetCurEntry() );
739 : : }
740 : : else
741 : : {
742 : 0 : if ( !BasicIDE::IsValidSbxName(aMacroNameEdit.GetText()) )
743 : : {
744 : 0 : ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
745 : 0 : aMacroNameEdit.SetSelection( Selection( 0, aMacroNameEdit.GetText().Len() ) );
746 : 0 : aMacroNameEdit.GrabFocus();
747 : 0 : return 1;
748 : : }
749 : 0 : SbMethod* pMethod = CreateMacro();
750 : 0 : if ( pMethod )
751 : : {
752 : 0 : aInfoItem.SetMethod( pMethod->GetName() );
753 : 0 : aInfoItem.SetModule( pMethod->GetModule()->GetName() );
754 : 0 : aInfoItem.SetLib( pMethod->GetModule()->GetParent()->GetName() );
755 : 0 : SfxAllItemSet aArgs( SFX_APP()->GetPool() );
756 : 0 : SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
757 : 0 : SFX_APP()->ExecuteSlot( aRequest );
758 : :
759 : 0 : BasicIDEShell* pIDEShell = BasicIDEGlobals::GetShell();
760 : 0 : SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
761 : 0 : SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
762 : 0 : if ( pDispatcher )
763 : 0 : pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
764 : 0 : StoreMacroDescription();
765 : 0 : EndDialog( MACRO_NEW );
766 : : }
767 : : }
768 : 0 : }
769 : : }
770 : :
771 : 0 : else if ( pButton == &aAssignButton )
772 : : {
773 : 0 : SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
774 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
775 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
776 : : DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
777 : 0 : if ( !aDocument.isAlive() )
778 : 0 : return 0;
779 : 0 : BasicManager* pBasMgr = aDocument.getBasicManager();
780 : 0 : String aLib( aDesc.GetLibName() );
781 : 0 : String aMod( aDesc.GetName() );
782 : 0 : String aSub( aMacroNameEdit.GetText() );
783 : 0 : SbMethod* pMethod = GetMacro();
784 : : DBG_ASSERT( pBasMgr, "BasMgr?" );
785 : : DBG_ASSERT( pMethod, "Method?" );
786 : 0 : String aComment( GetInfo( pMethod ) );
787 : 0 : SfxMacroInfoItem aItem( SID_MACROINFO, pBasMgr, aLib, aMod, aSub, aComment );
788 : 0 : SfxAllItemSet Args( SFX_APP()->GetPool() );
789 : 0 : SfxRequest aRequest( SID_CONFIG, SFX_CALLMODE_SYNCHRON, Args );
790 : 0 : aRequest.AppendItem( aItem );
791 : 0 : SFX_APP()->ExecuteSlot( aRequest );
792 : : }
793 : 0 : else if ( pButton == &aNewLibButton )
794 : : {
795 : 0 : SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
796 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
797 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
798 : 0 : createLibImpl( static_cast<Window*>( this ), aDocument, NULL, &aBasicBox );
799 : : }
800 : 0 : else if ( pButton == &aNewModButton )
801 : : {
802 : 0 : SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
803 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
804 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
805 : 0 : String aLibName( aDesc.GetLibName() );
806 : 0 : String aModName;
807 : : createModImpl( static_cast<Window*>( this ), aDocument,
808 : 0 : aBasicBox, aLibName, aModName, true );
809 : : }
810 : 0 : else if ( pButton == &aOrganizeButton )
811 : : {
812 : 0 : StoreMacroDescription();
813 : :
814 : 0 : BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( aBasicBox.FirstSelected() ) );
815 : 0 : OrganizeDialog* pDlg = new OrganizeDialog( this, 0, aDesc );
816 : 0 : sal_uInt16 nRet = pDlg->Execute();
817 : 0 : delete pDlg;
818 : :
819 : 0 : if ( nRet ) // not only closed
820 : : {
821 : 0 : EndDialog( MACRO_EDIT );
822 : 0 : return 0;
823 : : }
824 : :
825 : 0 : BasicIDEShell* pIDEShell = BasicIDEGlobals::GetShell();
826 : 0 : if ( pIDEShell && pIDEShell->IsAppBasicModified() )
827 : 0 : bForceStoreBasic = true;
828 : :
829 : 0 : aBasicBox.UpdateEntries();
830 : : }
831 : 0 : return 0;
832 : : }
833 : :
834 : :
835 : :
836 : 0 : void MacroChooser::UpdateFields()
837 : : {
838 : 0 : SvLBoxEntry* pMacroEntry = aMacroBox.GetCurEntry();
839 : 0 : String aEmptyStr;
840 : :
841 : 0 : aMacroNameEdit.SetText( aEmptyStr );
842 : 0 : if ( pMacroEntry )
843 : 0 : aMacroNameEdit.SetText( aMacroBox.GetEntryText( pMacroEntry ) );
844 : 0 : }
845 : :
846 : 0 : void MacroChooser::SetMode( sal_uInt16 nM )
847 : : {
848 : 0 : nMode = nM;
849 : 0 : if ( nMode == MACROCHOOSER_ALL )
850 : : {
851 : 0 : aRunButton.SetText( String( IDEResId( RID_STR_RUN ) ) );
852 : 0 : EnableButton( aNewDelButton, true );
853 : 0 : EnableButton( aOrganizeButton, true );
854 : : }
855 : 0 : else if ( nMode == MACROCHOOSER_CHOOSEONLY )
856 : : {
857 : 0 : aRunButton.SetText( String( IDEResId( RID_STR_CHOOSE ) ) );
858 : 0 : EnableButton( aNewDelButton, false );
859 : 0 : EnableButton( aOrganizeButton, false );
860 : : }
861 : 0 : else if ( nMode == MACROCHOOSER_RECORDING )
862 : : {
863 : 0 : aRunButton.SetText( String( IDEResId( RID_STR_RECORD ) ) );
864 : 0 : EnableButton( aNewDelButton, false );
865 : 0 : EnableButton( aOrganizeButton, false );
866 : :
867 : 0 : aAssignButton.Hide();
868 : 0 : aEditButton.Hide();
869 : 0 : aNewDelButton.Hide();
870 : 0 : aOrganizeButton.Hide();
871 : 0 : aMacroFromTxT.Hide();
872 : :
873 : 0 : aNewLibButton.Show();
874 : 0 : aNewModButton.Show();
875 : 0 : aMacrosSaveInTxt.Show();
876 : :
877 : 0 : Point aHelpPos = aHelpButton.GetPosPixel();
878 : 0 : Point aHelpPosLogic = PixelToLogic( aHelpPos, MapMode(MAP_APPFONT) );
879 : 0 : aHelpPosLogic.Y() -= 34;
880 : 0 : aHelpPos = LogicToPixel( aHelpPosLogic, MapMode(MAP_APPFONT) );
881 : 0 : aHelpButton.SetPosPixel( aHelpPos );
882 : : }
883 : 0 : CheckButtons();
884 : 0 : }
885 : :
886 : 0 : String MacroChooser::GetInfo( SbxVariable* pVar )
887 : : {
888 : 0 : String aComment;
889 : 0 : SbxInfoRef xInfo = pVar->GetInfo();
890 : 0 : if ( xInfo.Is() )
891 : 0 : aComment = xInfo->GetComment();
892 : 0 : return aComment;
893 : : }
894 : :
895 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|