bump product version to 5.0.4.1
[LibreOffice.git] / basctl / source / basicide / macrodlg.cxx
blob23e2597c413f6b208ee0bfe5f627d7272b3c85d9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <macrodlg.hxx>
22 #include <macrodlg.hrc>
23 #include <basidesh.hxx>
24 #include <baside2.hrc>
26 #include <iderdll.hxx>
27 #include <iderdll2.hxx>
29 #include <moduldlg.hxx>
30 #include <basic/basmgr.hxx>
31 #include <basic/sbmeth.hxx>
32 #include <basic/sbmod.hxx>
34 #include <sfx2/dispatch.hxx>
35 #include <sfx2/minfitem.hxx>
36 #include <sfx2/request.hxx>
39 #include <map>
41 namespace basctl
44 using ::std::map;
45 using ::std::pair;
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
50 MacroChooser::MacroChooser( vcl::Window* pParnt, bool bCreateEntries )
51 : SfxModalDialog(pParnt, "BasicMacroDialog", "modules/BasicIDE/ui/basicmacrodialog.ui")
52 , bNewDelIsDel(true)
53 // the Sfx doesn't aske the BasicManger whether modified or not
54 // => start saving in case of a change without a into the BasicIDE.
55 , bForceStoreBasic(false)
56 , nMode(All)
58 get(m_pMacroNameEdit, "macronameedit");
59 get(m_pMacroFromTxT, "macrofromft");
60 get(m_pMacrosSaveInTxt, "macrotoft");
61 get(m_pBasicBox, "libraries");
62 get(m_pMacrosInTxt, "existingmacrosft");
63 m_aMacrosInTxtBaseStr = m_pMacrosInTxt->GetText();
64 get(m_pMacroBox, "macros");
65 get(m_pRunButton, "run");
66 get(m_pCloseButton, "close");
67 get(m_pAssignButton, "assign");
68 get(m_pEditButton, "edit");
69 get(m_pDelButton, "delete");
70 get(m_pOrganizeButton, "organize");
71 get(m_pNewLibButton, "newlibrary");
72 get(m_pNewModButton, "newmodule");
74 m_pMacroBox->SetSelectionMode( SINGLE_SELECTION );
75 m_pMacroBox->SetHighlightRange(); // select over the whole width
77 m_pRunButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
78 m_pCloseButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
79 m_pAssignButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
80 m_pEditButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
81 m_pDelButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
82 m_pOrganizeButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
84 // Buttons only for MacroChooser::Recording
85 m_pNewLibButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
86 m_pNewModButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
87 m_pNewLibButton->Hide(); // default
88 m_pNewModButton->Hide(); // default
89 m_pMacrosSaveInTxt->Hide(); // default
91 m_pMacrosInTxt->SetStyle( WB_NOMULTILINE | WB_PATHELLIPSIS );
93 m_pMacroNameEdit->SetModifyHdl( LINK( this, MacroChooser, EditModifyHdl ) );
95 m_pBasicBox->SetSelectHdl( LINK( this, MacroChooser, BasicSelectHdl ) );
97 m_pMacroBox->SetDoubleClickHdl( LINK( this, MacroChooser, MacroDoubleClickHdl ) );
98 m_pMacroBox->SetSelectHdl( LINK( this, MacroChooser, MacroSelectHdl ) );
100 m_pBasicBox->SetMode( BROWSEMODE_MODULES );
101 m_pBasicBox->SetStyle( WB_TABSTOP | WB_BORDER |
102 WB_HASLINES | WB_HASLINESATROOT |
103 WB_HASBUTTONS | WB_HASBUTTONSATROOT |
104 WB_HSCROLL );
106 if (SfxDispatcher* pDispatcher = GetDispatcher())
107 pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
109 if ( bCreateEntries )
110 m_pBasicBox->ScanAllEntries();
113 MacroChooser::~MacroChooser()
115 disposeOnce();
118 void MacroChooser::dispose()
120 if ( bForceStoreBasic )
122 SfxGetpApp()->SaveBasicAndDialogContainer();
123 bForceStoreBasic = false;
125 m_pMacroNameEdit.clear();
126 m_pMacroFromTxT.clear();
127 m_pMacrosSaveInTxt.clear();
128 m_pBasicBox.clear();
129 m_pMacrosInTxt.clear();
130 m_pMacroBox.clear();
131 m_pRunButton.clear();
132 m_pCloseButton.clear();
133 m_pAssignButton.clear();
134 m_pEditButton.clear();
135 m_pDelButton.clear();
136 m_pOrganizeButton.clear();
137 m_pNewLibButton.clear();
138 m_pNewModButton.clear();
139 SfxModalDialog::dispose();
142 void MacroChooser::StoreMacroDescription()
144 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(m_pBasicBox->FirstSelected());
145 OUString aMethodName;
146 SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
147 if ( pEntry )
148 aMethodName = m_pMacroBox->GetEntryText( pEntry );
149 else
150 aMethodName = m_pMacroNameEdit->GetText();
151 if ( !aMethodName.isEmpty() )
153 aDesc.SetMethodName( aMethodName );
154 aDesc.SetType( OBJ_TYPE_METHOD );
157 if (ExtraData* pData = basctl::GetExtraData())
158 pData->SetLastEntryDescriptor( aDesc );
161 void MacroChooser::RestoreMacroDescription()
163 EntryDescriptor aDesc;
164 if (Shell* pShell = GetShell())
166 if (BaseWindow* pCurWin = pShell->GetCurWindow())
167 aDesc = pCurWin->CreateEntryDescriptor();
169 else
171 if (ExtraData* pData = basctl::GetExtraData())
172 aDesc = pData->GetLastEntryDescriptor();
175 m_pBasicBox->SetCurrentEntry( aDesc );
177 OUString aLastMacro( aDesc.GetMethodName() );
178 if ( !aLastMacro.isEmpty() )
180 // find entry in macro box
181 SvTreeListEntry* pEntry = 0;
182 sal_uLong nPos = 0;
183 SvTreeListEntry* pE = m_pMacroBox->GetEntry( nPos );
184 while ( pE )
186 if ( m_pMacroBox->GetEntryText( pE ) == aLastMacro )
188 pEntry = pE;
189 break;
191 pE = m_pMacroBox->GetEntry( ++nPos );
194 if ( pEntry )
195 m_pMacroBox->SetCurEntry( pEntry );
196 else
198 m_pMacroNameEdit->SetText( aLastMacro );
199 m_pMacroNameEdit->SetSelection( Selection( 0, 0 ) );
204 short MacroChooser::Execute()
206 RestoreMacroDescription();
207 m_pRunButton->GrabFocus();
209 // #104198 Check if "wrong" document is active
210 SvTreeListEntry* pSelectedEntry = m_pBasicBox->GetCurEntry();
211 EntryDescriptor aDesc( m_pBasicBox->GetEntryDescriptor( pSelectedEntry ) );
212 const ScriptDocument& rSelectedDoc( aDesc.GetDocument() );
214 // App Basic is always ok, so only check if shell was found
215 if( rSelectedDoc.isDocument() && !rSelectedDoc.isActive() )
217 // Search for the right entry
218 sal_uLong nRootPos = 0;
219 SvTreeListEntry* pRootEntry = m_pBasicBox->GetEntry( nRootPos );
220 while( pRootEntry )
222 EntryDescriptor aCmpDesc( m_pBasicBox->GetEntryDescriptor( pRootEntry ) );
223 const ScriptDocument& rCmpDoc( aCmpDesc.GetDocument() );
224 if ( rCmpDoc.isDocument() && rCmpDoc.isActive() )
226 SvTreeListEntry* pEntry = pRootEntry;
227 SvTreeListEntry* pLastValid = pEntry;
228 while ( pEntry )
230 pLastValid = pEntry;
231 pEntry = m_pBasicBox->FirstChild( pEntry );
233 if( pLastValid )
234 m_pBasicBox->SetCurEntry( pLastValid );
236 pRootEntry = m_pBasicBox->GetEntry( ++nRootPos );
240 CheckButtons();
241 UpdateFields();
243 if ( StarBASIC::IsRunning() )
244 m_pCloseButton->GrabFocus();
246 vcl::Window* pPrevDlgParent = Application::GetDefDialogParent();
247 Application::SetDefDialogParent( this );
248 short nRet = ModalDialog::Execute();
249 // #57314# If the BasicIDE has been activated, don't reset the DefModalDialogParent to the inactive document.
250 if ( Application::GetDefDialogParent() == this )
251 Application::SetDefDialogParent( pPrevDlgParent );
252 return nRet;
256 void MacroChooser::EnableButton( Button& rButton, bool bEnable )
258 if ( bEnable )
260 if (nMode == ChooseOnly || nMode == Recording)
261 rButton.Enable(&rButton == m_pRunButton);
262 else
263 rButton.Enable();
265 else
266 rButton.Disable();
272 SbMethod* MacroChooser::GetMacro()
274 SbMethod* pMethod = 0;
275 SbModule* pModule = m_pBasicBox->FindModule( m_pBasicBox->GetCurEntry() );
276 if ( pModule )
278 SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
279 if ( pEntry )
281 OUString aMacroName( m_pMacroBox->GetEntryText( pEntry ) );
282 pMethod = static_cast<SbMethod*>(pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD ));
285 return pMethod;
290 void MacroChooser::DeleteMacro()
292 SbMethod* pMethod = GetMacro();
293 DBG_ASSERT( pMethod, "DeleteMacro: Kein Macro !" );
294 if ( pMethod && QueryDelMacro( pMethod->GetName(), this ) )
296 if (SfxDispatcher* pDispatcher = GetDispatcher())
297 pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
299 // mark current doc as modified:
300 StarBASIC* pBasic = FindBasic(pMethod);
301 assert(pBasic && "Basic?!");
302 BasicManager* pBasMgr = FindBasicManager( pBasic );
303 DBG_ASSERT( pBasMgr, "BasMgr?" );
304 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
305 if ( aDocument.isDocument() )
307 aDocument.setDocumentModified();
308 if (SfxBindings* pBindings = GetBindingsPtr())
309 pBindings->Invalidate( SID_SAVEDOC );
312 SbModule* pModule = pMethod->GetModule();
313 DBG_ASSERT( pModule, "DeleteMacro: Kein Modul?!" );
314 OUString aSource( pModule->GetSource32() );
315 sal_uInt16 nStart, nEnd;
316 pMethod->GetLineRange( nStart, nEnd );
317 pModule->GetMethods()->Remove( pMethod );
318 CutLines( aSource, nStart-1, nEnd-nStart+1, true );
319 pModule->SetSource32( aSource );
321 // update module in library
322 OUString aLibName = pBasic->GetName();
323 OUString aModName = pModule->GetName();
324 OSL_VERIFY( aDocument.updateModule( aLibName, aModName, aSource ) );
326 SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
327 DBG_ASSERT( pEntry, "DeleteMacro: Entry ?!" );
328 m_pMacroBox->GetModel()->Remove( pEntry );
329 bForceStoreBasic = true;
333 SbMethod* MacroChooser::CreateMacro()
335 SbMethod* pMethod = 0;
336 SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
337 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
338 ScriptDocument aDocument( aDesc.GetDocument() );
339 OSL_ENSURE( aDocument.isAlive(), "MacroChooser::CreateMacro: no document!" );
340 if ( !aDocument.isAlive() )
341 return NULL;
343 OUString aLibName( aDesc.GetLibName() );
345 if ( aLibName.isEmpty() )
346 aLibName = "Standard" ;
348 aDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
350 OUString aOULibName( aLibName );
351 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
352 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
353 xModLibContainer->loadLibrary( aOULibName );
354 Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ) );
355 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
356 xDlgLibContainer->loadLibrary( aOULibName );
358 BasicManager* pBasMgr = aDocument.getBasicManager();
359 StarBASIC* pBasic = pBasMgr ? pBasMgr->GetLib( aLibName ) : 0;
360 if ( pBasic )
362 SbModule* pModule = 0;
363 OUString aModName( aDesc.GetName() );
364 if ( !aModName.isEmpty() )
366 // extract the module name from the string like "Sheet1 (Example1)"
367 if( aDesc.GetLibSubName() == IDE_RESSTR(RID_STR_DOCUMENT_OBJECTS) )
369 sal_Int32 nIndex = 0;
370 aModName = aModName.getToken( 0, ' ', nIndex );
372 pModule = pBasic->FindModule( aModName );
374 else if ( pBasic->GetModules()->Count() )
375 pModule = static_cast<SbModule*>(pBasic->GetModules()->Get( 0 ));
377 if ( !pModule )
379 pModule = createModImpl( static_cast<vcl::Window*>( this ),
380 aDocument, *m_pBasicBox, aLibName, aModName );
383 OUString aSubName = m_pMacroNameEdit->GetText();
384 DBG_ASSERT( !pModule || !pModule->GetMethods()->Find( aSubName, SbxCLASS_METHOD ), "Macro existiert schon!" );
385 pMethod = pModule ? basctl::CreateMacro( pModule, aSubName ) : NULL;
388 return pMethod;
391 void MacroChooser::SaveSetCurEntry( SvTreeListBox& rBox, SvTreeListEntry* pEntry )
393 // the edit would be killed by the highlight otherwise:
395 OUString aSaveText( m_pMacroNameEdit->GetText() );
396 Selection aCurSel( m_pMacroNameEdit->GetSelection() );
398 rBox.SetCurEntry( pEntry );
399 m_pMacroNameEdit->SetText( aSaveText );
400 m_pMacroNameEdit->SetSelection( aCurSel );
403 void MacroChooser::CheckButtons()
405 SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
406 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
407 SvTreeListEntry* pMacroEntry = m_pMacroBox->FirstSelected();
408 SbMethod* pMethod = GetMacro();
410 // check, if corresponding libraries are readonly
411 bool bReadOnly = false;
412 sal_uInt16 nDepth = pCurEntry ? m_pBasicBox->GetModel()->GetDepth( pCurEntry ) : 0;
413 if ( nDepth == 1 || nDepth == 2 )
415 ScriptDocument aDocument( aDesc.GetDocument() );
416 OUString aOULibName( aDesc.GetLibName() );
417 Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
418 Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
419 if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
420 ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) )
422 bReadOnly = true;
426 if (nMode != Recording)
428 // Run...
429 bool bEnable = pMethod != nullptr;
430 if (nMode != ChooseOnly && StarBASIC::IsRunning())
431 bEnable = false;
432 EnableButton(*m_pRunButton, bEnable);
435 // organising still possible?
437 // Assign...
438 EnableButton(*m_pAssignButton, pMethod != nullptr);
440 // Edit...
441 EnableButton(*m_pEditButton, pMacroEntry != nullptr);
443 // Organizer...
444 EnableButton(*m_pOrganizeButton, !StarBASIC::IsRunning() && nMode == All);
446 // m_pDelButton->...
447 bool bProtected = m_pBasicBox->IsEntryProtected( pCurEntry );
448 bool bShare = ( aDesc.GetLocation() == LIBRARY_LOCATION_SHARE );
449 EnableButton(*m_pDelButton, !StarBASIC::IsRunning() && nMode == All && !bProtected && !bReadOnly && !bShare);
450 bool bPrev = bNewDelIsDel;
451 bNewDelIsDel = pMethod != nullptr;
452 if (bPrev != bNewDelIsDel && nMode == All)
454 OUString aBtnText( bNewDelIsDel ? IDEResId(RID_STR_BTNDEL).toString() : IDEResId(RID_STR_BTNNEW).toString() );
455 m_pDelButton->SetText( aBtnText );
458 if (nMode == Recording)
460 // save button
461 m_pRunButton->Enable(!bProtected && !bReadOnly && !bShare);
462 // new library button
463 m_pNewLibButton->Enable(!bShare);
464 // new module button
465 m_pNewModButton->Enable(!bProtected && !bReadOnly && !bShare);
471 IMPL_LINK_NOARG(MacroChooser, MacroDoubleClickHdl)
473 StoreMacroDescription();
474 if (nMode == Recording)
476 SbMethod* pMethod = GetMacro();
477 if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
478 return 0;
481 EndDialog(Macro_OkRun);
482 return 0;
485 IMPL_LINK( MacroChooser, MacroSelectHdl, SvTreeListBox *, pBox )
487 // Is also called if deselected!
488 // Two function calls in every SelectHdl because
489 // there's no separate DeselectHDL.
490 // So find out if select or deselect:
491 if ( pBox->IsSelected( pBox->GetHdlEntry() ) )
493 UpdateFields();
494 CheckButtons();
496 return 0;
499 IMPL_LINK( MacroChooser, BasicSelectHdl, SvTreeListBox *, pBox )
501 // Is also called if deselected!
502 // Two function calls in every SelectHdl because
503 // there's no separate DeselectHDL.
504 // So find out if select or deselect:
505 if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
506 return 0;
508 SbModule* pModule = m_pBasicBox->FindModule( m_pBasicBox->GetCurEntry() );
510 m_pMacroBox->Clear();
511 if ( pModule )
513 OUString aStr = m_aMacrosInTxtBaseStr;
514 aStr += " " ;
515 aStr += pModule->GetName();
517 m_pMacrosInTxt->SetText( aStr );
519 // The macros should be called in the same order that they
520 // are written down in the module.
522 map< sal_uInt16, SbMethod* > aMacros;
523 size_t nMacroCount = pModule->GetMethods()->Count();
524 for ( size_t iMeth = 0; iMeth < nMacroCount; iMeth++ )
526 SbMethod* pMethod = static_cast<SbMethod*>(pModule->GetMethods()->Get( iMeth ));
527 if( pMethod->IsHidden() )
528 continue;
529 DBG_ASSERT( pMethod, "Methode nicht gefunden! (NULL)" );
530 sal_uInt16 nStart, nEnd;
531 pMethod->GetLineRange( nStart, nEnd );
532 aMacros.insert( map< sal_uInt16, SbMethod*>::value_type( nStart, pMethod ) );
535 m_pMacroBox->SetUpdateMode(false);
536 for ( map< sal_uInt16, SbMethod* >::iterator it = aMacros.begin(); it != aMacros.end(); ++it )
537 m_pMacroBox->InsertEntry( (*it).second->GetName() );
538 m_pMacroBox->SetUpdateMode(true);
540 if ( m_pMacroBox->GetEntryCount() )
542 SvTreeListEntry* pEntry = m_pMacroBox->GetEntry( 0 );
543 DBG_ASSERT( pEntry, "Entry ?!" );
544 m_pMacroBox->SetCurEntry( pEntry );
548 UpdateFields();
549 CheckButtons();
550 return 0;
555 IMPL_LINK( MacroChooser, EditModifyHdl, Edit *, pEdit )
557 (void)pEdit;
559 // select the module in which the macro is put at Neu (new),
560 // if BasicManager or Lib is selecting
561 SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
562 if ( pCurEntry )
564 sal_uInt16 nDepth = m_pBasicBox->GetModel()->GetDepth( pCurEntry );
565 if ( ( nDepth == 1 ) && ( m_pBasicBox->IsEntryProtected( pCurEntry ) ) )
567 // then put to the respective Std-Lib...
568 SvTreeListEntry* pManagerEntry = m_pBasicBox->GetModel()->GetParent( pCurEntry );
569 pCurEntry = m_pBasicBox->GetModel()->FirstChild( pManagerEntry );
571 if ( nDepth < 2 )
573 SvTreeListEntry* pNewEntry = pCurEntry;
574 while ( pCurEntry && ( nDepth < 2 ) )
576 pCurEntry = m_pBasicBox->FirstChild( pCurEntry );
577 if ( pCurEntry )
579 pNewEntry = pCurEntry;
580 nDepth = m_pBasicBox->GetModel()->GetDepth( pCurEntry );
583 SaveSetCurEntry( *m_pBasicBox, pNewEntry );
585 if ( m_pMacroBox->GetEntryCount() )
587 OUString aEdtText( m_pMacroNameEdit->GetText() );
588 bool bFound = false;
589 for ( sal_uInt16 n = 0; n < m_pMacroBox->GetEntryCount(); n++ )
591 SvTreeListEntry* pEntry = m_pMacroBox->GetEntry( n );
592 DBG_ASSERT( pEntry, "Entry ?!" );
593 if ( m_pMacroBox->GetEntryText( pEntry ).equalsIgnoreAsciiCase( aEdtText ) )
595 SaveSetCurEntry(*m_pMacroBox, pEntry);
596 bFound = true;
597 break;
600 if ( !bFound )
602 SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
603 // if the entry exists ->Select ->Desription...
604 if ( pEntry )
605 m_pMacroBox->Select( pEntry, false );
610 CheckButtons();
611 return 0;
616 IMPL_LINK( MacroChooser, ButtonHdl, Button *, pButton )
618 // apart from New/Record the Description is done by LoseFocus
619 if (pButton == m_pRunButton)
621 StoreMacroDescription();
623 // #116444# check security settings before macro execution
624 if (nMode == All)
626 SbMethod* pMethod = GetMacro();
627 SbModule* pModule = pMethod ? pMethod->GetModule() : NULL;
628 StarBASIC* pBasic = pModule ? static_cast<StarBASIC*>(pModule->GetParent()) : NULL;
629 BasicManager* pBasMgr = pBasic ? FindBasicManager(pBasic) : NULL;
630 if ( pBasMgr )
632 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
633 if ( aDocument.isDocument() && !aDocument.allowMacros() )
635 ScopedVclPtrInstance<MessageDialog>::Create(this, IDEResId(RID_STR_CANNOTRUNMACRO), VCL_MESSAGE_WARNING)->Execute();
636 return 0;
640 else if (nMode == Recording )
642 if ( !IsValidSbxName(m_pMacroNameEdit->GetText()) )
644 ScopedVclPtrInstance<MessageDialog>::Create(this, IDEResId(RID_STR_BADSBXNAME))->Execute();
645 m_pMacroNameEdit->SetSelection( Selection( 0, m_pMacroNameEdit->GetText().getLength() ) );
646 m_pMacroNameEdit->GrabFocus();
647 return 0;
650 SbMethod* pMethod = GetMacro();
651 if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
652 return 0;
655 EndDialog(Macro_OkRun);
657 else if (pButton == m_pCloseButton)
659 StoreMacroDescription();
660 EndDialog(Macro_Close);
662 else if ((pButton == m_pEditButton) || (pButton == m_pDelButton))
664 SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
665 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
666 ScriptDocument aDocument( aDesc.GetDocument() );
667 DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
668 if ( !aDocument.isAlive() )
669 return 0;
670 BasicManager* pBasMgr = aDocument.getBasicManager();
671 OUString aLib( aDesc.GetLibName() );
672 OUString aMod( aDesc.GetName() );
673 // extract the module name from the string like "Sheet1 (Example1)"
674 if( aDesc.GetLibSubName() == IDE_RESSTR(RID_STR_DOCUMENT_OBJECTS) )
676 sal_Int32 nIndex = 0;
677 aMod = aMod.getToken( 0, ' ', nIndex );
679 OUString aSub( aDesc.GetMethodName() );
680 SfxMacroInfoItem aInfoItem( SID_BASICIDE_ARG_MACROINFO, pBasMgr, aLib, aMod, aSub, OUString() );
681 if (pButton == m_pEditButton)
683 SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
684 if ( pEntry )
685 aInfoItem.SetMethod( m_pMacroBox->GetEntryText( pEntry ) );
686 StoreMacroDescription();
687 SfxAllItemSet aArgs( SfxGetpApp()->GetPool() );
688 SfxRequest aRequest( SID_BASICIDE_APPEAR, SfxCallMode::SYNCHRON, aArgs );
689 SfxGetpApp()->ExecuteSlot( aRequest );
691 if (SfxDispatcher* pDispatcher = GetDispatcher())
692 pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SfxCallMode::ASYNCHRON, &aInfoItem, 0L );
693 EndDialog(Macro_Edit);
695 else
697 if ( bNewDelIsDel )
699 DeleteMacro();
700 if (SfxDispatcher* pDispatcher = GetDispatcher())
701 pDispatcher->Execute( SID_BASICIDE_UPDATEMODULESOURCE,
702 SfxCallMode::SYNCHRON, &aInfoItem, 0L );
703 CheckButtons();
704 UpdateFields();
705 //if ( m_pMacroBox->GetCurEntry() ) // OV-Bug ?
706 // m_pMacroBox->Select( m_pMacroBox->GetCurEntry() );
708 else
710 if ( !IsValidSbxName(m_pMacroNameEdit->GetText()) )
712 ScopedVclPtrInstance<MessageDialog>::Create(this, IDEResId(RID_STR_BADSBXNAME))->Execute();
713 m_pMacroNameEdit->SetSelection( Selection( 0, m_pMacroNameEdit->GetText().getLength() ) );
714 m_pMacroNameEdit->GrabFocus();
715 return 1;
717 SbMethod* pMethod = CreateMacro();
718 if ( pMethod )
720 aInfoItem.SetMethod( pMethod->GetName() );
721 aInfoItem.SetModule( pMethod->GetModule()->GetName() );
722 aInfoItem.SetLib( pMethod->GetModule()->GetParent()->GetName() );
723 SfxAllItemSet aArgs( SfxGetpApp()->GetPool() );
724 SfxRequest aRequest( SID_BASICIDE_APPEAR, SfxCallMode::SYNCHRON, aArgs );
725 SfxGetpApp()->ExecuteSlot( aRequest );
727 if (SfxDispatcher* pDispatcher = GetDispatcher())
728 pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SfxCallMode::ASYNCHRON, &aInfoItem, 0L );
729 StoreMacroDescription();
730 EndDialog(Macro_New);
735 else if (pButton == m_pAssignButton)
737 SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
738 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
739 ScriptDocument aDocument( aDesc.GetDocument() );
740 DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
741 if ( !aDocument.isAlive() )
742 return 0;
743 BasicManager* pBasMgr = aDocument.getBasicManager();
744 OUString aLib( aDesc.GetLibName() );
745 OUString aMod( aDesc.GetName() );
746 OUString aSub( m_pMacroNameEdit->GetText() );
747 SbMethod* pMethod = GetMacro();
748 DBG_ASSERT( pBasMgr, "BasMgr?" );
749 DBG_ASSERT( pMethod, "Method?" );
750 OUString aComment( GetInfo( pMethod ) );
751 SfxMacroInfoItem aItem( SID_MACROINFO, pBasMgr, aLib, aMod, aSub, aComment );
752 SfxAllItemSet Args( SfxGetpApp()->GetPool() );
753 SfxRequest aRequest( SID_CONFIG, SfxCallMode::SYNCHRON, Args );
754 aRequest.AppendItem( aItem );
755 SfxGetpApp()->ExecuteSlot( aRequest );
757 else if (pButton == m_pNewLibButton)
759 SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
760 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
761 ScriptDocument aDocument( aDesc.GetDocument() );
762 createLibImpl( static_cast<vcl::Window*>( this ), aDocument, NULL, m_pBasicBox );
764 else if (pButton == m_pNewModButton)
766 SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
767 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
768 ScriptDocument aDocument( aDesc.GetDocument() );
769 OUString aLibName( aDesc.GetLibName() );
770 OUString aModName;
771 createModImpl( static_cast<vcl::Window*>( this ), aDocument,
772 *m_pBasicBox, aLibName, aModName, true );
774 else if (pButton == m_pOrganizeButton)
776 StoreMacroDescription();
778 EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(m_pBasicBox->FirstSelected());
779 VclPtrInstance< OrganizeDialog > pDlg( this, 0, aDesc );
780 sal_uInt16 nRet = pDlg->Execute();
781 pDlg.reset();
783 if ( nRet ) // not only closed
785 EndDialog(Macro_Edit);
786 return 0;
789 Shell* pShell = GetShell();
790 if ( pShell && pShell->IsAppBasicModified() )
791 bForceStoreBasic = true;
793 m_pBasicBox->UpdateEntries();
795 return 0;
800 void MacroChooser::UpdateFields()
802 SvTreeListEntry* pMacroEntry = m_pMacroBox->GetCurEntry();
803 OUString aEmptyStr;
805 m_pMacroNameEdit->SetText( aEmptyStr );
806 if ( pMacroEntry )
807 m_pMacroNameEdit->SetText( m_pMacroBox->GetEntryText( pMacroEntry ) );
810 void MacroChooser::SetMode (Mode nM)
812 nMode = nM;
813 switch (nMode)
815 case All:
817 m_pRunButton->SetText(IDEResId(RID_STR_RUN).toString());
818 EnableButton(*m_pDelButton, true);
819 EnableButton(*m_pOrganizeButton, true);
820 break;
823 case ChooseOnly:
825 m_pRunButton->SetText(IDEResId(RID_STR_CHOOSE).toString());
826 EnableButton(*m_pDelButton, false);
827 EnableButton(*m_pOrganizeButton, false);
828 break;
831 case Recording:
833 m_pRunButton->SetText(IDEResId(RID_STR_RECORD).toString());
834 EnableButton(*m_pDelButton, false);
835 EnableButton(*m_pOrganizeButton, false);
837 m_pAssignButton->Hide();
838 m_pEditButton->Hide();
839 m_pDelButton->Hide();
840 m_pOrganizeButton->Hide();
841 m_pMacroFromTxT->Hide();
843 m_pNewLibButton->Show();
844 m_pNewModButton->Show();
845 m_pMacrosSaveInTxt->Show();
847 break;
850 CheckButtons();
853 OUString MacroChooser::GetInfo( SbxVariable* pVar )
855 OUString aComment;
856 SbxInfoRef xInfo = pVar->GetInfo();
857 if ( xInfo.Is() )
858 aComment = xInfo->GetComment();
859 return aComment;
863 } // namespace basctl
865 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */