Update ooo320-m1
[ooovba.git] / basctl / source / basicide / macrodlg.cxx
blob4a97e4a76e15472de6f87e99b5aeb6d44affcdd0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macrodlg.cxx,v $
10 * $Revision: 1.38 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basctl.hxx"
34 #include <memory>
36 #include <ide_pch.hxx>
39 #include <macrodlg.hxx>
40 #include <macrodlg.hrc>
41 #include <basidesh.hrc>
42 #include <basidesh.hxx>
43 #include <baside2.hrc> // ID's fuer Imagese
44 #include <basobj.hxx>
45 #include <baside3.hxx>
47 #include <iderdll.hxx>
48 #include <iderdll2.hxx>
49 #include <iderid.hxx>
51 #include <moduldlg.hxx>
52 #include <basic/sbx.hxx>
54 #include <bastypes.hxx>
55 #include <sbxitem.hxx>
56 #include <sfx2/minfitem.hxx>
58 #ifndef _COM_SUN_STAR_SCRIPT_XLIBRYARYCONTAINER2_HPP_
59 #include <com/sun/star/script/XLibraryContainer2.hpp>
60 #endif
61 #include <com/sun/star/document/MacroExecMode.hpp>
63 using namespace ::com::sun::star;
64 using namespace ::com::sun::star::uno;
67 DECLARE_LIST( MacroList, SbMethod* )
69 MacroChooser::MacroChooser( Window* pParnt, BOOL bCreateEntries ) :
70 SfxModalDialog( pParnt, IDEResId( RID_MACROCHOOSER ) ),
71 aMacroNameTxt( this, IDEResId( RID_TXT_MACRONAME ) ),
72 aMacroNameEdit( this, IDEResId( RID_ED_MACRONAME ) ),
73 aMacroFromTxT( this, IDEResId( RID_TXT_MACROFROM ) ),
74 aMacrosSaveInTxt( this, IDEResId( RID_TXT_SAVEMACRO ) ),
75 aBasicBox( this, IDEResId( RID_CTRL_LIB ) ),
76 aMacrosInTxt( this, IDEResId( RID_TXT_MACROSIN ) ),
77 aMacroBox( this, IDEResId( RID_CTRL_MACRO ) ),
78 aRunButton( this, IDEResId( RID_PB_RUN ) ),
79 aCloseButton( this, IDEResId( RID_PB_CLOSE ) ),
80 aAssignButton( this, IDEResId( RID_PB_ASSIGN ) ),
81 aEditButton( this, IDEResId( RID_PB_EDIT ) ),
82 aNewDelButton( this, IDEResId( RID_PB_DEL ) ),
83 aOrganizeButton( this, IDEResId( RID_PB_ORG ) ),
84 aHelpButton( this, IDEResId( RID_PB_HELP ) ),
85 aNewLibButton( this, IDEResId( RID_PB_NEWLIB ) ),
86 aNewModButton( this, IDEResId( RID_PB_NEWMOD ) )
88 FreeResource();
90 nMode = MACROCHOOSER_ALL;
91 bNewDelIsDel = TRUE;
93 // Der Sfx fragt den BasicManager nicht, ob modified
94 // => Speichern anschmeissen, wenn Aenderung, aber kein Sprung in
95 // die BasicIDE.
96 bForceStoreBasic = FALSE;
98 aMacrosInTxtBaseStr = aMacrosInTxt.GetText();
100 aMacroBox.SetSelectionMode( SINGLE_SELECTION );
101 aMacroBox.SetHighlightRange(); // ueber ganze Breite selektieren
103 aRunButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
104 aCloseButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
105 aAssignButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
106 aEditButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
107 aNewDelButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
108 aOrganizeButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
110 // Buttons only for MACROCHOOSER_RECORDING
111 aNewLibButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
112 aNewModButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
113 aNewLibButton.Hide(); // default
114 aNewModButton.Hide(); // default
115 aMacrosSaveInTxt.Hide(); // default
117 aMacrosInTxt.SetStyle( WB_NOMULTILINE | WB_PATHELLIPSIS );
119 aMacroNameEdit.SetModifyHdl( LINK( this, MacroChooser, EditModifyHdl ) );
121 aBasicBox.SetSelectHdl( LINK( this, MacroChooser, BasicSelectHdl ) );
123 aMacroBox.SetDoubleClickHdl( LINK( this, MacroChooser, MacroDoubleClickHdl ) );
124 aMacroBox.SetSelectHdl( LINK( this, MacroChooser, MacroSelectHdl ) );
126 aBasicBox.SetMode( BROWSEMODE_MODULES );
127 aBasicBox.SetWindowBits( WB_HASLINES | WB_HASLINESATROOT |
128 WB_HASBUTTONS | WB_HASBUTTONSATROOT |
129 WB_HSCROLL );
131 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
132 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
133 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
134 if( pDispatcher )
136 pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
139 if ( bCreateEntries )
140 aBasicBox.ScanAllEntries();
143 MacroChooser::~MacroChooser()
145 if ( bForceStoreBasic )
146 SFX_APP()->SaveBasicAndDialogContainer();
149 void MacroChooser::StoreMacroDescription()
151 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( aBasicBox.FirstSelected() ) );
152 String aMethodName;
153 SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
154 if ( pEntry )
155 aMethodName = aMacroBox.GetEntryText( pEntry );
156 else
157 aMethodName = aMacroNameEdit.GetText();
158 if ( aMethodName.Len() )
160 aDesc.SetMethodName( aMethodName );
161 aDesc.SetType( OBJ_TYPE_METHOD );
164 BasicIDEData* pData = IDE_DLL()->GetExtraData();
165 if ( pData )
166 pData->SetLastEntryDescriptor( aDesc );
169 void MacroChooser::RestoreMacroDescription()
171 BasicEntryDescriptor aDesc;
172 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
173 if ( pIDEShell )
175 IDEBaseWindow* pCurWin = pIDEShell->GetCurWindow();
176 if ( pCurWin )
177 aDesc = pCurWin->CreateEntryDescriptor();
179 else
181 BasicIDEData* pData = IDE_DLL()->GetExtraData();
182 if ( pData )
183 aDesc = pData->GetLastEntryDescriptor();
186 aBasicBox.SetCurrentEntry( aDesc );
188 String aLastMacro( aDesc.GetMethodName() );
189 if ( aLastMacro.Len() )
191 // find entry in macro box
192 SvLBoxEntry* pEntry = 0;
193 ULONG nPos = 0;
194 SvLBoxEntry* pE = aMacroBox.GetEntry( nPos );
195 while ( pE )
197 if ( aMacroBox.GetEntryText( pE ) == aLastMacro )
199 pEntry = pE;
200 break;
202 pE = aMacroBox.GetEntry( ++nPos );
205 if ( pEntry )
206 aMacroBox.SetCurEntry( pEntry );
207 else
209 aMacroNameEdit.SetText( aLastMacro );
210 aMacroNameEdit.SetSelection( Selection( 0, 0 ) );
215 short __EXPORT MacroChooser::Execute()
217 RestoreMacroDescription();
218 aRunButton.GrabFocus();
220 // #104198 Check if "wrong" document is active
221 SvLBoxEntry* pSelectedEntry = aBasicBox.GetCurEntry();
222 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pSelectedEntry ) );
223 const ScriptDocument& rSelectedDoc( aDesc.GetDocument() );
225 // App Basic is always ok, so only check if shell was found
226 if( rSelectedDoc.isDocument() && !rSelectedDoc.isActive() )
228 // Search for the right entry
229 ULONG nRootPos = 0;
230 SvLBoxEntry* pRootEntry = aBasicBox.GetEntry( nRootPos );
231 while( pRootEntry )
233 BasicEntryDescriptor aCmpDesc( aBasicBox.GetEntryDescriptor( pRootEntry ) );
234 const ScriptDocument& rCmpDoc( aCmpDesc.GetDocument() );
235 if ( rCmpDoc.isDocument() && rCmpDoc.isActive() )
237 SvLBoxEntry* pEntry = pRootEntry;
238 SvLBoxEntry* pLastValid = pEntry;
239 while ( pEntry )
241 pLastValid = pEntry;
242 pEntry = aBasicBox.FirstChild( pEntry );
244 if( pLastValid )
245 aBasicBox.SetCurEntry( pLastValid );
247 pRootEntry = aBasicBox.GetEntry( ++nRootPos );
251 CheckButtons();
252 UpdateFields();
254 if ( StarBASIC::IsRunning() )
255 aCloseButton.GrabFocus();
257 Window* pPrevDlgParent = Application::GetDefDialogParent();
258 Application::SetDefDialogParent( this );
259 short nRet = ModalDialog::Execute();
260 // #57314# Wenn die BasicIDE aktiviert wurde, dann nicht den DefModalDialogParent auf das inaktive Dokument zuruecksetzen.
261 if ( Application::GetDefDialogParent() == this )
262 Application::SetDefDialogParent( pPrevDlgParent );
263 return nRet;
267 void MacroChooser::EnableButton( Button& rButton, BOOL bEnable )
269 if ( bEnable )
271 if ( nMode == MACROCHOOSER_CHOOSEONLY || nMode == MACROCHOOSER_RECORDING )
273 // Nur der RunButton kann enabled werden
274 if ( &rButton == &aRunButton )
275 rButton.Enable();
276 else
277 rButton.Disable();
279 else
280 rButton.Enable();
282 else
283 rButton.Disable();
289 SbMethod* MacroChooser::GetMacro()
291 SbMethod* pMethod = 0;
292 SbModule* pModule = aBasicBox.FindModule( aBasicBox.GetCurEntry() );
293 if ( pModule )
295 SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
296 if ( pEntry )
298 String aMacroName( aMacroBox.GetEntryText( pEntry ) );
299 pMethod = (SbMethod*)pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD );
302 return pMethod;
307 void MacroChooser::DeleteMacro()
309 SbMethod* pMethod = GetMacro();
310 DBG_ASSERT( pMethod, "DeleteMacro: Kein Macro !" );
311 if ( pMethod && QueryDelMacro( pMethod->GetName(), this ) )
313 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
314 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
315 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
316 if( pDispatcher )
318 pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
321 // Aktuelles Doc als geaendert markieren:
322 StarBASIC* pBasic = BasicIDE::FindBasic( pMethod );
323 DBG_ASSERT( pBasic, "Basic?!" );
324 BasicManager* pBasMgr = BasicIDE::FindBasicManager( pBasic );
325 DBG_ASSERT( pBasMgr, "BasMgr?" );
326 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
327 if ( aDocument.isDocument() ) // Muss ja nicht aus einem Document kommen...
329 aDocument.setDocumentModified();
330 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
331 if ( pBindings )
332 pBindings->Invalidate( SID_SAVEDOC );
335 SbModule* pModule = pMethod->GetModule();
336 DBG_ASSERT( pModule, "DeleteMacro: Kein Modul?!" );
337 ::rtl::OUString aSource( pModule->GetSource32() );
338 USHORT nStart, nEnd;
339 pMethod->GetLineRange( nStart, nEnd );
340 pModule->GetMethods()->Remove( pMethod );
341 CutLines( aSource, nStart-1, nEnd-nStart+1, TRUE );
342 pModule->SetSource32( aSource );
344 // update module in library
345 String aLibName = pBasic->GetName();
346 String aModName = pModule->GetName();
347 OSL_VERIFY( aDocument.updateModule( aLibName, aModName, aSource ) );
349 SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
350 DBG_ASSERT( pEntry, "DeleteMacro: Entry ?!" );
351 aMacroBox.GetModel()->Remove( pEntry );
352 bForceStoreBasic = TRUE;
356 SbMethod* MacroChooser::CreateMacro()
358 SbMethod* pMethod = 0;
359 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
360 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
361 ScriptDocument aDocument( aDesc.GetDocument() );
362 OSL_ENSURE( aDocument.isAlive(), "MacroChooser::CreateMacro: no document!" );
363 if ( !aDocument.isAlive() )
364 return NULL;
366 String aLibName( aDesc.GetLibName() );
368 if ( !aLibName.Len() )
369 aLibName = String::CreateFromAscii( "Standard" );
371 aDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
373 ::rtl::OUString aOULibName( aLibName );
374 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
375 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
376 xModLibContainer->loadLibrary( aOULibName );
377 Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ) );
378 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
379 xDlgLibContainer->loadLibrary( aOULibName );
381 BasicManager* pBasMgr = aDocument.getBasicManager();
382 StarBASIC* pBasic = pBasMgr ? pBasMgr->GetLib( aLibName ) : 0;
383 if ( pBasic )
385 SbModule* pModule = 0;
386 String aModName( aDesc.GetName() );
387 if ( aModName.Len() )
389 // extract the module name from the string like "Sheet1 (Example1)"
390 if( aDesc.GetLibSubName().Equals( String( IDEResId( RID_STR_DOCUMENT_OBJECTS ) ) ) )
392 sal_uInt16 nIndex = 0;
393 aModName = aModName.GetToken( 0, ' ', nIndex );
395 pModule = pBasic->FindModule( aModName );
397 else if ( pBasic->GetModules()->Count() )
398 pModule = (SbModule*)pBasic->GetModules()->Get( 0 );
400 if ( !pModule )
402 pModule = createModImpl( static_cast<Window*>( this ),
403 aDocument, aBasicBox, aLibName, aModName );
406 String aSubName = aMacroNameEdit.GetText();
407 DBG_ASSERT( !pModule || !pModule->GetMethods()->Find( aSubName, SbxCLASS_METHOD ), "Macro existiert schon!" );
408 pMethod = pModule ? BasicIDE::CreateMacro( pModule, aSubName ) : NULL;
411 return pMethod;
414 void MacroChooser::SaveSetCurEntry( SvTreeListBox& rBox, SvLBoxEntry* pEntry )
416 // Durch das Highlight wird das Edit sonst platt gemacht:
418 String aSaveText( aMacroNameEdit.GetText() );
419 Selection aCurSel( aMacroNameEdit.GetSelection() );
421 rBox.SetCurEntry( pEntry );
422 aMacroNameEdit.SetText( aSaveText );
423 aMacroNameEdit.SetSelection( aCurSel );
426 void MacroChooser::CheckButtons()
428 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
429 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
430 SvLBoxEntry* pMacroEntry = aMacroBox.FirstSelected();
431 SbMethod* pMethod = GetMacro();
433 // check, if corresponding libraries are readonly
434 BOOL bReadOnly = FALSE;
435 USHORT nDepth = pCurEntry ? aBasicBox.GetModel()->GetDepth( pCurEntry ) : 0;
436 if ( nDepth == 1 || nDepth == 2 )
438 ScriptDocument aDocument( aDesc.GetDocument() );
439 ::rtl::OUString aOULibName( aDesc.GetLibName() );
440 Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
441 Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
442 if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
443 ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) )
445 bReadOnly = TRUE;
449 if ( nMode != MACROCHOOSER_RECORDING )
451 // Run...
452 BOOL bEnable = pMethod ? TRUE : FALSE;
453 if ( ( nMode != MACROCHOOSER_CHOOSEONLY ) && StarBASIC::IsRunning() )
454 bEnable = FALSE;
455 EnableButton( aRunButton, bEnable );
458 // Organisieren immer moeglich ?
460 // Assign...
461 EnableButton( aAssignButton, pMethod ? TRUE : FALSE );
463 // Edit...
464 EnableButton( aEditButton, pMacroEntry ? TRUE : FALSE );
466 // aOrganizeButton
467 EnableButton( aOrganizeButton, !StarBASIC::IsRunning() && ( nMode == MACROCHOOSER_ALL ));
469 // aNewDelButton....
470 bool bProtected = aBasicBox.IsEntryProtected( pCurEntry );
471 bool bShare = ( aDesc.GetLocation() == LIBRARY_LOCATION_SHARE );
472 EnableButton( aNewDelButton,
473 !StarBASIC::IsRunning() && ( nMode == MACROCHOOSER_ALL ) && !bProtected && !bReadOnly && !bShare );
474 BOOL bPrev = bNewDelIsDel;
475 bNewDelIsDel = pMethod ? TRUE : FALSE;
476 if ( ( bPrev != bNewDelIsDel ) && ( nMode == MACROCHOOSER_ALL ) )
478 String aBtnText( bNewDelIsDel ? IDEResId( RID_STR_BTNDEL) : IDEResId( RID_STR_BTNNEW ) );
479 aNewDelButton.SetText( aBtnText );
482 if ( nMode == MACROCHOOSER_RECORDING )
484 // save button
485 if ( !bProtected && !bReadOnly && !bShare )
486 aRunButton.Enable();
487 else
488 aRunButton.Disable();
490 // new library button
491 if ( !bShare )
492 aNewLibButton.Enable();
493 else
494 aNewLibButton.Disable();
496 // new module button
497 if ( !bProtected && !bReadOnly && !bShare )
498 aNewModButton.Enable();
499 else
500 aNewModButton.Disable();
506 IMPL_LINK_INLINE_START( MacroChooser, MacroDoubleClickHdl, SvTreeListBox *, EMPTYARG )
508 StoreMacroDescription();
509 if ( nMode == MACROCHOOSER_RECORDING )
511 SbMethod* pMethod = GetMacro();
512 if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
513 return 0;
516 EndDialog( MACRO_OK_RUN );
517 return 0;
519 IMPL_LINK_INLINE_END( MacroChooser, MacroDoubleClickHdl, SvTreeListBox *, EMPTYARG )
521 IMPL_LINK( MacroChooser, MacroSelectHdl, SvTreeListBox *, pBox )
523 // Wird auch gerufen, wenn Deselektiert!
524 // 2 Funktionsaufrufe in jedem SelectHdl, nur weil Olli
525 // keinen separatren DeselctHdl einfuehren wollte:
526 // Also: Feststellen, ob Select oder Deselect:
527 if ( pBox->IsSelected( pBox->GetHdlEntry() ) )
529 UpdateFields();
530 CheckButtons();
532 return 0;
535 IMPL_LINK( MacroChooser, BasicSelectHdl, SvTreeListBox *, pBox )
537 static String aSpaceStr = String::CreateFromAscii(" ");
539 // Wird auch gerufen, wenn Deselektiert!
540 // 2 Funktionsaufrufe in jedem SelectHdl, nur weil Olli
541 // keinen separatren DeselctHdl einfuehren wollte:
542 // Also: Feststellen, ob Select oder Deselect:
543 if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
544 return 0;
546 SbModule* pModule = aBasicBox.FindModule( aBasicBox.GetCurEntry() );
548 aMacroBox.Clear();
549 if ( pModule )
551 String aStr = aMacrosInTxtBaseStr;
552 aStr += aSpaceStr;
553 aStr += pModule->GetName();
555 aMacrosInTxt.SetText( aStr );
557 // Die Macros sollen in der Reihenfolge angezeigt werden,
558 // wie sie im Modul stehen.
559 MacroList aMacros;
560 USHORT nMacros = pModule->GetMethods()->Count();
561 USHORT nMethod;
562 for ( nMethod = 0; nMethod < nMacros; nMethod++ )
564 SbMethod* pMethod = (SbMethod*)pModule->GetMethods()->Get( nMethod );
565 DBG_ASSERT( pMethod, "Methode nicht gefunden! (NULL)" );
566 ULONG nPos = LIST_APPEND;
567 // Eventuell weiter vorne ?
568 USHORT nStart, nEnd;
569 pMethod->GetLineRange( nStart, nEnd );
570 for ( ULONG n = 0; n < aMacros.Count(); n++ )
572 USHORT nS, nE;
573 SbMethod* pM = aMacros.GetObject( n );
574 DBG_ASSERT( pM, "Macro nicht in Liste ?!" );
575 pM->GetLineRange( nS, nE );
576 if ( nS > nStart )
578 nPos = n;
579 break;
582 aMacros.Insert( pMethod, nPos );
585 aMacroBox.SetUpdateMode( FALSE );
586 for ( nMethod = 0; nMethod < nMacros; nMethod++ )
587 aMacroBox.InsertEntry( aMacros.GetObject( nMethod )->GetName() );
588 aMacroBox.SetUpdateMode( TRUE );
590 if ( aMacroBox.GetEntryCount() )
592 SvLBoxEntry* pEntry = aMacroBox.GetEntry( 0 );
593 DBG_ASSERT( pEntry, "Entry ?!" );
594 aMacroBox.SetCurEntry( pEntry );
598 UpdateFields();
599 CheckButtons();
600 return 0;
605 IMPL_LINK( MacroChooser, EditModifyHdl, Edit *, pEdit )
607 (void)pEdit;
609 // Das Modul, in dem bei Neu das Macro landet, selektieren,
610 // wenn BasicManager oder Lib selektiert.
611 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
612 if ( pCurEntry )
614 USHORT nDepth = aBasicBox.GetModel()->GetDepth( pCurEntry );
615 if ( ( nDepth == 1 ) && ( aBasicBox.IsEntryProtected( pCurEntry ) ) )
617 // Dann auf die entsprechende Std-Lib stellen...
618 SvLBoxEntry* pManagerEntry = aBasicBox.GetModel()->GetParent( pCurEntry );
619 pCurEntry = aBasicBox.GetModel()->FirstChild( pManagerEntry );
621 if ( nDepth < 2 )
623 SvLBoxEntry* pNewEntry = pCurEntry;
624 while ( pCurEntry && ( nDepth < 2 ) )
626 pCurEntry = aBasicBox.FirstChild( pCurEntry );
627 if ( pCurEntry )
629 pNewEntry = pCurEntry;
630 nDepth = aBasicBox.GetModel()->GetDepth( pCurEntry );
633 SaveSetCurEntry( aBasicBox, pNewEntry );
635 if ( aMacroBox.GetEntryCount() )
637 String aEdtText( aMacroNameEdit.GetText() );
638 BOOL bFound = FALSE;
639 for ( USHORT n = 0; n < aMacroBox.GetEntryCount(); n++ )
641 SvLBoxEntry* pEntry = aMacroBox.GetEntry( n );
642 DBG_ASSERT( pEntry, "Entry ?!" );
643 if ( aMacroBox.GetEntryText( pEntry ).CompareIgnoreCaseToAscii( aEdtText ) == COMPARE_EQUAL )
645 SaveSetCurEntry( aMacroBox, pEntry );
646 bFound = TRUE;
647 break;
650 if ( !bFound )
652 SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
653 // Wenn es den Eintrag gibt ->Select ->Desription...
654 if ( pEntry )
655 aMacroBox.Select( pEntry, FALSE );
660 CheckButtons();
661 return 0;
666 IMPL_LINK( MacroChooser, ButtonHdl, Button *, pButton )
668 // ausser bei New/Record wird die Description durch LoseFocus uebernommen.
669 if ( pButton == &aRunButton )
671 StoreMacroDescription();
673 // #116444# check security settings before macro execution
674 if ( nMode == MACROCHOOSER_ALL )
676 SbMethod* pMethod = GetMacro();
677 SbModule* pModule = pMethod ? pMethod->GetModule() : NULL;
678 StarBASIC* pBasic = pModule ? (StarBASIC*)pModule->GetParent() : NULL;
679 BasicManager* pBasMgr = pBasic ? BasicIDE::FindBasicManager( pBasic ) : NULL;
680 if ( pBasMgr )
682 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
683 if ( aDocument.isDocument() && !aDocument.allowMacros() )
685 WarningBox( this, WB_OK, String( IDEResId( RID_STR_CANNOTRUNMACRO ) ) ).Execute();
686 return 0;
690 else if ( nMode == MACROCHOOSER_RECORDING )
692 BOOL bValid = BasicIDE::IsValidSbxName( aMacroNameEdit.GetText() );
693 if ( !bValid )
695 ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
696 aMacroNameEdit.SetSelection( Selection( 0, aMacroNameEdit.GetText().Len() ) );
697 aMacroNameEdit.GrabFocus();
698 return 0;
701 SbMethod* pMethod = GetMacro();
702 if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
703 return 0;
706 EndDialog( MACRO_OK_RUN );
708 else if ( pButton == &aCloseButton )
710 StoreMacroDescription();
711 EndDialog( MACRO_CLOSE );
713 else if ( ( pButton == &aEditButton ) || ( pButton == &aNewDelButton ) )
715 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
716 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
717 ScriptDocument aDocument( aDesc.GetDocument() );
718 DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
719 if ( !aDocument.isAlive() )
720 return 0;
721 BasicManager* pBasMgr = aDocument.getBasicManager();
722 String aLib( aDesc.GetLibName() );
723 String aMod( aDesc.GetName() );
724 // extract the module name from the string like "Sheet1 (Example1)"
725 if( aDesc.GetLibSubName().Equals( String( IDEResId( RID_STR_DOCUMENT_OBJECTS ) ) ) )
727 sal_uInt16 nIndex = 0;
728 aMod = aMod.GetToken( 0, ' ', nIndex );
730 String aSub( aDesc.GetMethodName() );
731 SfxMacroInfoItem aInfoItem( SID_BASICIDE_ARG_MACROINFO, pBasMgr, aLib, aMod, aSub, String() );
732 if ( pButton == &aEditButton )
734 SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
735 if ( pEntry )
736 aInfoItem.SetMethod( aMacroBox.GetEntryText( pEntry ) );
737 StoreMacroDescription();
738 SfxAllItemSet aArgs( SFX_APP()->GetPool() );
739 SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
740 SFX_APP()->ExecuteSlot( aRequest );
742 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
743 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
744 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
745 if( pDispatcher )
746 pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
747 EndDialog( MACRO_EDIT );
749 else
751 if ( bNewDelIsDel )
753 DeleteMacro();
754 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
755 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
756 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
757 if( pDispatcher )
759 pDispatcher->Execute( SID_BASICIDE_UPDATEMODULESOURCE,
760 SFX_CALLMODE_SYNCHRON, &aInfoItem, 0L );
762 CheckButtons();
763 UpdateFields();
764 //if ( aMacroBox.GetCurEntry() ) // OV-Bug ?
765 // aMacroBox.Select( aMacroBox.GetCurEntry() );
767 else
769 BOOL bValid = BasicIDE::IsValidSbxName( aMacroNameEdit.GetText() );
770 if ( !bValid )
772 ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
773 aMacroNameEdit.SetSelection( Selection( 0, aMacroNameEdit.GetText().Len() ) );
774 aMacroNameEdit.GrabFocus();
775 return 1;
777 SbMethod* pMethod = CreateMacro();
778 if ( pMethod )
780 aInfoItem.SetMethod( pMethod->GetName() );
781 aInfoItem.SetModule( pMethod->GetModule()->GetName() );
782 aInfoItem.SetLib( pMethod->GetModule()->GetParent()->GetName() );
783 SfxAllItemSet aArgs( SFX_APP()->GetPool() );
784 SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
785 SFX_APP()->ExecuteSlot( aRequest );
787 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
788 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
789 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
790 if ( pDispatcher )
791 pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
792 StoreMacroDescription();
793 EndDialog( MACRO_NEW );
799 else if ( pButton == &aAssignButton )
801 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
802 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
803 ScriptDocument aDocument( aDesc.GetDocument() );
804 DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
805 if ( !aDocument.isAlive() )
806 return 0;
807 BasicManager* pBasMgr = aDocument.getBasicManager();
808 String aLib( aDesc.GetLibName() );
809 String aMod( aDesc.GetName() );
810 String aSub( aMacroNameEdit.GetText() );
811 SbMethod* pMethod = GetMacro();
812 DBG_ASSERT( pBasMgr, "BasMgr?" );
813 DBG_ASSERT( pMethod, "Method?" );
814 String aComment( GetInfo( pMethod ) );
815 SfxMacroInfoItem aItem( SID_MACROINFO, pBasMgr, aLib, aMod, aSub, aComment );
816 SfxAllItemSet Args( SFX_APP()->GetPool() );
817 SfxRequest aRequest( SID_CONFIG, SFX_CALLMODE_SYNCHRON, Args );
818 aRequest.AppendItem( aItem );
819 SFX_APP()->ExecuteSlot( aRequest );
821 else if ( pButton == &aNewLibButton )
823 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
824 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
825 ScriptDocument aDocument( aDesc.GetDocument() );
826 createLibImpl( static_cast<Window*>( this ), aDocument, NULL, &aBasicBox );
828 else if ( pButton == &aNewModButton )
830 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
831 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
832 ScriptDocument aDocument( aDesc.GetDocument() );
833 String aLibName( aDesc.GetLibName() );
834 String aModName;
835 createModImpl( static_cast<Window*>( this ), aDocument,
836 aBasicBox, aLibName, aModName, true );
838 else if ( pButton == &aOrganizeButton )
840 StoreMacroDescription();
842 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( aBasicBox.FirstSelected() ) );
843 OrganizeDialog* pDlg = new OrganizeDialog( this, 0, aDesc );
844 USHORT nRet = pDlg->Execute();
845 delete pDlg;
847 if ( nRet ) // Nicht einfach nur geschlossen
849 EndDialog( MACRO_EDIT );
850 return 0;
853 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
854 if ( pIDEShell && pIDEShell->IsAppBasicModified() )
855 bForceStoreBasic = TRUE;
857 aBasicBox.UpdateEntries();
859 return 0;
864 void MacroChooser::UpdateFields()
866 SvLBoxEntry* pMacroEntry = aMacroBox.GetCurEntry();
867 String aEmptyStr;
869 aMacroNameEdit.SetText( aEmptyStr );
870 if ( pMacroEntry )
871 aMacroNameEdit.SetText( aMacroBox.GetEntryText( pMacroEntry ) );
874 void MacroChooser::SetMode( USHORT nM )
876 nMode = nM;
877 if ( nMode == MACROCHOOSER_ALL )
879 aRunButton.SetText( String( IDEResId( RID_STR_RUN ) ) );
880 EnableButton( aNewDelButton, TRUE );
881 EnableButton( aOrganizeButton, TRUE );
883 else if ( nMode == MACROCHOOSER_CHOOSEONLY )
885 aRunButton.SetText( String( IDEResId( RID_STR_CHOOSE ) ) );
886 EnableButton( aNewDelButton, FALSE );
887 EnableButton( aOrganizeButton, FALSE );
889 else if ( nMode == MACROCHOOSER_RECORDING )
891 aRunButton.SetText( String( IDEResId( RID_STR_RECORD ) ) );
892 EnableButton( aNewDelButton, FALSE );
893 EnableButton( aOrganizeButton, FALSE );
895 aAssignButton.Hide();
896 aEditButton.Hide();
897 aNewDelButton.Hide();
898 aOrganizeButton.Hide();
899 aMacroFromTxT.Hide();
901 aNewLibButton.Show();
902 aNewModButton.Show();
903 aMacrosSaveInTxt.Show();
905 Point aHelpPos = aHelpButton.GetPosPixel();
906 Point aHelpPosLogic = PixelToLogic( aHelpPos, MapMode(MAP_APPFONT) );
907 aHelpPosLogic.Y() -= 34;
908 aHelpPos = LogicToPixel( aHelpPosLogic, MapMode(MAP_APPFONT) );
909 aHelpButton.SetPosPixel( aHelpPos );
911 CheckButtons();
914 String MacroChooser::GetInfo( SbxVariable* pVar )
916 String aComment;
917 SbxInfoRef xInfo = pVar->GetInfo();
918 if ( xInfo.Is() )
919 aComment = xInfo->GetComment();
920 return aComment;