Update ooo320-m1
[ooovba.git] / sfx2 / source / control / macrconf.cxx
blob84c9f7feb5874c1606d6e87b31938ef76715766d
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: macrconf.cxx,v $
10 * $Revision: 1.25.142.1 $
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_sfx2.hxx"
33 #include <basic/sbstar.hxx>
34 #include <basic/basmgr.hxx>
35 #ifndef _SBX_HXX //autogen
36 #include <basic/sbx.hxx>
37 #endif
38 #include <svtools/intitem.hxx>
39 #include <basic/sbmeth.hxx>
40 #include <basic/sbmod.hxx>
41 #ifndef _BASIC_SBUNO_HXX
42 #include <basic/sbuno.hxx>
43 #endif
45 #include <osl/mutex.hxx>
47 #include <com/sun/star/script/XEngine.hpp>
48 #include <com/sun/star/document/MacroExecMode.hpp>
50 #ifndef GCC
51 #endif
53 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
54 #include <comphelper/processfactory.hxx>
55 #endif
56 #include <unotools/intlwrapper.hxx>
58 #include <sfx2/msgpool.hxx>
59 #include <sfx2/macrconf.hxx>
60 #include "sfxbasic.hxx"
61 #include <sfx2/sfx.hrc>
62 #include <sfx2/app.hxx>
63 #include <sfx2/objsh.hxx>
64 #include <sfx2/dispatch.hxx>
65 #include "minfitem.hxx"
66 #include "imgmgr.hxx"
67 #include <sfx2/evntconf.hxx>
68 #include <sfx2/docfile.hxx>
69 #include <sfx2/genlink.hxx>
70 #include <sfx2/viewfrm.hxx>
71 #include <appdata.hxx>
72 #include "objshimp.hxx"
73 #include <sfx2/request.hxx>
75 static const sal_uInt16 nCompatVersion = 2;
76 static const sal_uInt16 nVersion = 3;
78 // Static member
79 SfxMacroConfig* SfxMacroConfig::pMacroConfig = NULL;
81 SfxMacroConfig* SfxMacroConfig::GetOrCreate()
83 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
84 if ( !pMacroConfig )
85 pMacroConfig = new SfxMacroConfig;
86 return pMacroConfig;
89 void SfxMacroConfig::Release_Impl()
91 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
92 DELETEZ( pMacroConfig );
95 //==========================================================================
97 struct SfxMacroConfig_Impl
99 SfxMacroInfoArr_Impl aArr;
100 sal_uInt32 nEventId;
101 sal_Bool bWaitingForCallback;
103 SfxMacroConfig_Impl()
104 : nEventId( 0 )
105 , bWaitingForCallback( sal_False )
109 //==========================================================================
111 SbMethod* SfxQueryMacro_Impl( BasicManager* pMgr , const String& rMacro,
112 const String &rLibName, const String& rModule )
114 IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
115 const CollatorWrapper* pCollator = aIntlWrapper.getCollator();
116 sal_uInt16 nLibCount = pMgr->GetLibCount();
117 for ( sal_uInt16 nLib = 0; nLib < nLibCount; ++nLib )
119 if ( COMPARE_EQUAL == pCollator->compareString( pMgr->GetLibName( nLib ), rLibName ) )
121 StarBASIC* pLib = pMgr->GetLib( nLib );
122 if( !pLib )
124 pMgr->LoadLib( nLib );
125 pLib = pMgr->GetLib( nLib );
128 if( pLib )
130 sal_uInt16 nModCount = pLib->GetModules()->Count();
131 for( sal_uInt16 nMod = 0; nMod < nModCount; ++nMod )
133 SbModule* pMod = (SbModule*)pLib->GetModules()->Get( nMod );
134 if ( pMod && COMPARE_EQUAL == pCollator->compareString( pMod->GetName(), rModule ) )
136 SbMethod* pMethod = (SbMethod*)pMod->Find( rMacro, SbxCLASS_METHOD );
137 if( pMethod )
138 return pMethod;
144 return 0;
147 SbMethod* SfxQueryMacro( BasicManager* pMgr , const String& rMacro )
149 sal_uInt16 nLast = 0;
150 String aMacro = rMacro;
151 String aLibName = aMacro.GetToken( 0, '.', nLast );
152 String aModule = aMacro.GetToken( 0, '.', nLast );
153 aMacro.Erase( 0, nLast );
155 return SfxQueryMacro_Impl( pMgr, aMacro, aLibName, aModule );
158 ErrCode SfxCallMacro( BasicManager* pMgr, const String& rCode,
159 SbxArray *pArgs, SbxValue *pRet )
161 ErrCode nErr;
162 SfxApplication *pApp = SFX_APP();
163 pApp->EnterBasicCall();
164 SbMethod* pMethod = SfxQueryMacro( pMgr, rCode );
165 if ( pMethod )
167 if ( pArgs )
168 pMethod->SetParameters( pArgs );
169 nErr = pMethod->Call( pRet );
171 else
172 nErr = ERRCODE_BASIC_PROC_UNDEFINED;
174 pApp->LeaveBasicCall();
175 return nErr;
178 //==========================================================================
180 SfxMacroInfo::SfxMacroInfo( const String& rURL ) :
181 pHelpText(0),
182 nRefCnt(0),
183 bAppBasic(TRUE),
184 nSlotId(0),
185 pSlot(0)
187 if ( rURL.CompareToAscii( "macro:", 6 ) == COMPARE_EQUAL )
189 String aTmp = rURL.Copy( 6 );
190 if ( aTmp.GetTokenCount('/') > 3 )
192 // 'macro:///lib.mod.proc(args)' => Macro via App-BASIC-Mgr
193 // 'macro://[docname|.]/lib.mod.proc(args)' => Macro via zugehoerigen Doc-BASIC-Mgr
194 if ( aTmp.CompareToAscii("///", 3 ) != COMPARE_EQUAL )
195 bAppBasic = FALSE;
196 aTmp = rURL.GetToken( 3, '/' );
197 if ( aTmp.GetTokenCount('.') == 3 )
199 aLibName = aTmp.GetToken( 0, '.' );
200 aModuleName = aTmp.GetToken( 1, '.' );
201 aMethodName = aTmp.GetToken( 2, '.' );
203 // Remove arguments to be compatible
204 aMethodName.SearchAndReplaceAscii( "()", String(), sal::static_int_cast< xub_StrLen >(std::max( aMethodName.Len()-2, 0 )));
208 DBG_ASSERT( aLibName.Len() && aModuleName.Len() && aMethodName.Len(), "Wrong macro URL!" );
210 else
211 aMethodName = rURL;
214 SfxMacroInfo::SfxMacroInfo( bool _bAppBasic ) :
215 pHelpText(0),
216 nRefCnt(0),
217 bAppBasic(_bAppBasic),
218 nSlotId(0),
219 pSlot(0)
222 //==========================================================================
224 SfxMacroInfo::SfxMacroInfo(bool _bAppBasic, const String& rLibName,
225 const String& rModuleName, const String& rMethodName) :
226 pHelpText(0),
227 nRefCnt(0),
228 bAppBasic(_bAppBasic),
229 aLibName(rLibName),
230 aModuleName(rModuleName),
231 aMethodName(rMethodName),
232 nSlotId(0),
233 pSlot(0)
237 //==========================================================================
239 SfxMacroInfo::SfxMacroInfo(bool _bAppBasic, const String& rQualifiedName )
240 : pHelpText(0),
241 nRefCnt(0),
242 bAppBasic(_bAppBasic),
243 nSlotId(0),
244 pSlot(0)
246 sal_uInt16 nCount = rQualifiedName.GetTokenCount('.');
247 aMethodName = rQualifiedName.GetToken( nCount-1, '.' );
248 if ( nCount > 1 )
249 aModuleName = rQualifiedName.GetToken( nCount-2, '.' );
250 if ( nCount > 2 )
251 aLibName = rQualifiedName.GetToken( 0, '.' );
254 //==========================================================================
256 SfxMacroInfo::SfxMacroInfo(SfxMacroInfo& rOther) :
257 pHelpText(0),
258 nRefCnt(0),
259 bAppBasic(rOther.bAppBasic),
260 aLibName(rOther.aLibName),
261 aModuleName(rOther.aModuleName),
262 aMethodName(rOther.aMethodName),
263 nSlotId(rOther.nSlotId),
264 pSlot(0)
267 //==========================================================================
269 SfxMacroInfo::~SfxMacroInfo()
271 delete pSlot;
272 delete pHelpText;
275 //==========================================================================
277 sal_Bool SfxMacroInfo::operator==(const SfxMacroInfo& rOther) const
279 if ( GetQualifiedName() == rOther.GetQualifiedName() &&
280 bAppBasic == rOther.bAppBasic )
281 return sal_True;
282 else
283 return sal_False;
286 //==========================================================================
288 String SfxMacroInfo::GetMacroName() const
290 String aMacroName = aMethodName;
291 aMacroName += '(';
292 aMacroName += aLibName;
293 aMacroName += '.';
294 aMacroName += aModuleName;
295 aMacroName += ')';
296 return aMacroName;
299 //==========================================================================
301 String SfxMacroInfo::GetQualifiedName() const
303 String aMacroName;
304 if( aMacroName.Len() || aLibName.Len() )
306 // Altes Format
307 aMacroName = aLibName;
308 aMacroName += '.';
309 aMacroName += aModuleName;
310 aMacroName += '.';
313 // Wg. ::com::sun::star::script::JavaScript kein Zerlegen des Strings mehr
314 aMacroName += aMethodName;
315 return aMacroName;
318 String SfxMacroInfo::GetFullQualifiedName() const
320 // Liefert nur Unsinn, wenn f"ur ein ::com::sun::star::script::JavaScript aufgerufen !
321 String aRet;
322 if ( bAppBasic )
323 aRet = SFX_APP()->GetName();
324 aRet += '.';
325 aRet += GetQualifiedName();
326 return aRet;
329 String SfxMacroInfo::GetURL() const
331 if ( !aLibName.Len() )
332 return aMethodName;
334 // 'macro:///lib.mod.proc(args)' => Macro via App-BASIC-Mgr
335 // 'macro://[docname|.]/lib.mod.proc(args)' => Macro via zugehoerigen Doc-BASIC-Mgr
336 // 'macro://obj.method(args)' => Object via App-BASIC-Mgr
337 String aURL( String::CreateFromAscii("macro://") );
338 if ( !bAppBasic )
339 aURL += '.';
340 aURL += '/';
341 aURL += aLibName;
342 aURL += '.';
343 aURL += aModuleName;
344 aURL += '.';
345 aURL += aMethodName;
346 aURL += String::CreateFromAscii("()");
348 return aURL;
351 //==========================================================================
353 BasicManager* SfxMacroInfo::GetBasicManager() const
355 if (bAppBasic)
357 return SFX_APP()->GetBasicManager();
359 else
361 SfxObjectShell *pCurrDocShell = SfxObjectShell::Current();
362 return pCurrDocShell ? pCurrDocShell->GetBasicManager() :
363 SFX_APP()->GetBasicManager();
367 //==========================================================================
369 String SfxMacroInfo::GetBasicName() const
371 if (bAppBasic)
373 return SFX_APP()->GetName();
375 else
377 SfxObjectShell *pCurrDocShell = SfxObjectShell::Current();
378 if ( pCurrDocShell )
379 return pCurrDocShell->GetTitle();
380 else
381 return SFX_APP()->GetName();
385 String SfxMacroInfo::GetHelpText() const
387 if ( pHelpText )
388 return *pHelpText;
389 return String();
392 String SfxMacroConfig::RequestHelp( sal_uInt16 nId )
394 SfxMacroInfo *pInfo = SFX_APP()->GetMacroConfig()->GetMacroInfo( nId );
395 if ( !pInfo )
396 return String();
398 if ( !pInfo->pHelpText )
400 SbMethod *pMethod =
401 SfxQueryMacro_Impl( pInfo->GetBasicManager(), pInfo->aMethodName,
402 pInfo->aLibName, pInfo->aModuleName );
403 if ( pMethod && pMethod->GetInfo() )
404 pInfo->pHelpText = new String( pMethod->GetInfo()->GetComment() );
407 return pInfo->GetHelpText();
410 void SfxMacroInfo::SetHelpText( const String& rName )
412 if ( !pHelpText )
413 pHelpText = new String;
414 *pHelpText = rName;
417 //==========================================================================
419 SvStream& operator >> (SvStream& rStream, SfxMacroInfo& rInfo)
421 sal_uInt16 nAppBasic, nFileVersion;
422 String aDocName;
424 rStream >> nFileVersion;
425 if ( nVersion < nCompatVersion )
427 // In der 1.Version ohne Versionskennung
428 nAppBasic = nVersion;
429 nFileVersion = 1;
430 rStream.ReadByteString(aDocName,RTL_TEXTENCODING_UTF8);
431 rStream.ReadByteString(rInfo.aLibName,RTL_TEXTENCODING_UTF8);
432 rStream.ReadByteString(rInfo.aModuleName,RTL_TEXTENCODING_UTF8);
433 rStream.ReadByteString(rInfo.aMethodName,RTL_TEXTENCODING_UTF8);
435 else
437 String aInput;
438 rStream >> nAppBasic;
439 rStream.ReadByteString(aDocName,RTL_TEXTENCODING_UTF8); // Vorsicht: kann bei AppName Unsinn sein!
440 rStream.ReadByteString(rInfo.aLibName,RTL_TEXTENCODING_UTF8);
441 rStream.ReadByteString(rInfo.aModuleName,RTL_TEXTENCODING_UTF8);
442 rStream.ReadByteString(aInput,RTL_TEXTENCODING_UTF8);
444 if ( nFileVersion == nCompatVersion )
445 rInfo.aMethodName = aInput;
446 else
448 sal_uInt16 nCount = aInput.GetTokenCount('.');
449 rInfo.aMethodName = aInput.GetToken( nCount-1, '.' );
450 if ( nCount > 1 )
451 rInfo.aModuleName = aInput.GetToken( nCount-2, '.' );
452 if ( nCount > 2 )
453 rInfo.aLibName = aInput.GetToken( 0, '.' );
457 rInfo.bAppBasic = (sal_Bool) nAppBasic;
458 return rStream;
461 int SfxMacroInfo::Load( SvStream& rStream )
463 rStream >> (*this);
464 nSlotId = SFX_APP()->GetMacroConfig()->GetSlotId(this);
465 return 0;
468 //==========================================================================
470 SvStream& operator << (SvStream& rStream, const SfxMacroInfo& rInfo)
472 if ( rInfo.bAppBasic )
474 rStream << nVersion
475 << (sal_uInt16) rInfo.bAppBasic;
476 rStream.WriteByteString(rInfo.GetBasicName(),RTL_TEXTENCODING_UTF8);
477 rStream.WriteByteString(rInfo.aLibName,RTL_TEXTENCODING_UTF8);
478 rStream.WriteByteString(rInfo.aModuleName,RTL_TEXTENCODING_UTF8);
479 rStream.WriteByteString(rInfo.aMethodName,RTL_TEXTENCODING_UTF8);
481 else
483 rStream << nVersion
484 << (sal_uInt16) rInfo.bAppBasic;
485 rStream.WriteByteString(SFX_APP()->GetName(),RTL_TEXTENCODING_UTF8);
486 rStream.WriteByteString(rInfo.aLibName,RTL_TEXTENCODING_UTF8);
487 rStream.WriteByteString(rInfo.aModuleName,RTL_TEXTENCODING_UTF8);
488 rStream.WriteByteString(rInfo.aMethodName,RTL_TEXTENCODING_UTF8);
491 return rStream;
494 sal_Bool SfxMacroInfo::Compare( const SvxMacro& rMacro ) const
496 String aName = rMacro.GetLibName();
497 aName += '.';
498 aName += rMacro.GetMacName();
499 if ( GetFullQualifiedName() == aName )
500 return sal_True;
501 return sal_False;
504 //==========================================================================
506 SfxMacroConfig::SfxMacroConfig()
508 pImp = new SfxMacroConfig_Impl;
511 //==========================================================================
513 SfxMacroConfig::~SfxMacroConfig()
515 if ( pImp->nEventId )
516 Application::RemoveUserEvent( pImp->nEventId );
517 delete pImp;
520 //==========================================================================
522 SFX_STATE_STUB( SfxApplication, MacroState_Impl )
523 SFX_EXEC_STUB( SfxApplication, MacroExec_Impl )
525 sal_uInt16 SfxMacroConfig::GetSlotId(SfxMacroInfoPtr pInfo)
527 sal_uInt16 nCount = pImp->aArr.Count(); // Macro suchen
528 sal_uInt16 i;
529 for (i=0; i<nCount; i++)
530 if ((*(pImp->aArr)[i]) == (*pInfo))
531 break;
533 if (i == nCount)
534 { // Macro noch unbekannt
535 nCount = aIdArray.Count();
536 sal_uInt16 n;
537 for (n=0; n<nCount; n++) // freie SlotId suchen
538 if (aIdArray[n] > SID_MACRO_START + n)
539 break;
541 sal_uInt16 nNewSlotId = SID_MACRO_START + n;
542 if ( nNewSlotId > SID_MACRO_END )
543 return 0;
544 aIdArray.Insert( SID_MACRO_START + n, n );
546 SfxSlot *pNewSlot = new SfxSlot;
547 pNewSlot->nSlotId = SID_MACRO_START + n;
548 pNewSlot->nGroupId = 0;
549 pNewSlot->nFlags = SFX_SLOT_ASYNCHRON;
550 pNewSlot->nMasterSlotId = 0;
551 pNewSlot->nValue = 0;
552 pNewSlot->fnExec = SFX_STUB_PTR(SfxApplication,MacroExec_Impl);
553 pNewSlot->fnState = SFX_STUB_PTR(SfxApplication,MacroState_Impl);
554 pNewSlot->pType = 0; HACK(SFX_TYPE(SfxVoidItem))
555 pNewSlot->pName = pNewSlot->pMethodName = U2S(pInfo->aMethodName).getStr();
556 pNewSlot->pLinkedSlot = 0;
557 pNewSlot->nArgDefCount = 0;
558 pNewSlot->pFirstArgDef = 0;
559 pNewSlot->pUnoName = 0;
561 if (nCount)
563 SfxSlot *pSlot = (pImp->aArr)[0]->pSlot;
564 pNewSlot->pNextSlot = pSlot->pNextSlot;
565 pSlot->pNextSlot = pNewSlot;
567 else
568 pNewSlot->pNextSlot = pNewSlot;
570 // Macro uebernehmen
571 SfxMacroInfoPtr pNewInfo = new SfxMacroInfo(*pInfo);
572 pNewInfo->nSlotId = SID_MACRO_START + n;
573 pImp->aArr.Insert(pNewInfo,n);
574 pNewInfo->pSlot = pNewSlot;
575 pInfo->nSlotId = pNewInfo->nSlotId;
576 pNewInfo->nRefCnt++;
578 else
580 pInfo->nSlotId = (pImp->aArr)[i]->nSlotId;
581 (pImp->aArr)[i]->nRefCnt++;
584 return pInfo->nSlotId;
587 //==========================================================================
589 void SfxMacroConfig::ReleaseSlotId(sal_uInt16 nId)
591 DBG_ASSERT( IsMacroSlot( nId ), "SlotId ist kein Macro!");
593 sal_uInt16 nCount = pImp->aArr.Count();
594 for (sal_uInt16 i=0; i<nCount; i++)
596 SfxMacroInfo *pInfo = (pImp->aArr)[i];
597 if (pInfo->nSlotId == nId)
599 pInfo->nRefCnt--;
600 if (pInfo->nRefCnt == 0)
602 // Slot wird nicht mehr referenziert, also holen
603 SfxSlot *pSlot = pInfo->pSlot;
605 // Slot aus der Verkettung rausnehmen
606 while (pSlot->pNextSlot != pInfo->pSlot)
607 pSlot = (SfxSlot*) pSlot->pNextSlot;
608 pSlot->pNextSlot = pInfo->pSlot->pNextSlot;
610 // Slot selbst kurz schlie\sen
611 pSlot = pInfo->pSlot;
612 pSlot->pNextSlot = pSlot;
614 // MacroInfo aus Array entfernen, damit sie kein Unheil
615 // anrichten kann
616 pImp->aArr.Remove(i);
618 // SlotId wieder freigeben
619 sal_uInt16 nIdCount = aIdArray.Count();
620 for (sal_uInt16 n=0; n<nIdCount; n++)
622 if (aIdArray[n] == nId)
624 aIdArray.Remove(n);
625 break;
629 // Sofern nicht die Applikation heruntergefahren wird, mu\s
630 // der Slot asynchron gel"oscht werden, falls er in seinem
631 // eigenen Execute abgeschossen wird!
632 if ( !SFX_APP()->Get_Impl()->bInQuit )
633 pImp->nEventId = Application::PostUserEvent( LINK(this, SfxMacroConfig, EventHdl_Impl), pInfo );
634 else
635 EventHdl_Impl( pInfo );
637 return;
641 DBG_ERROR("Macro-SlotId nicht gefunden!");
644 //==========================================================================
646 void SfxMacroConfig::RegisterSlotId(sal_uInt16 nId)
648 DBG_ASSERT( IsMacroSlot( nId ), "SlotId ist kein Macro!");
650 sal_uInt16 nCount = pImp->aArr.Count();
651 for (sal_uInt16 i=0; i<nCount; i++)
653 if ((pImp->aArr)[i]->nSlotId == nId)
655 (pImp->aArr)[i]->nRefCnt++;
656 return;
660 DBG_ERROR("Macro-SlotId nicht gefunden!");
663 //==========================================================================
665 SfxMacroInfo* SfxMacroConfig::GetMacroInfo(sal_uInt16 nId) const
667 sal_uInt16 nCount = pImp->aArr.Count();
668 for (sal_uInt16 i=0; i<nCount; i++)
669 if ((pImp->aArr)[i]->nSlotId == nId)
670 return (pImp->aArr)[i];
672 return 0;
675 //==========================================================================
677 const SfxMacroInfo* SfxMacroConfig::GetMacroInfo_Impl( const SvxMacro *pMacro ) const
679 sal_uInt16 nCount = pImp->aArr.Count();
680 for (sal_uInt16 i=0; i<nCount; i++)
681 if ((pImp->aArr)[i]->Compare(*pMacro) )
682 return (pImp->aArr)[i];
683 return 0;
686 //==========================================================================
688 sal_Bool SfxMacroConfig::ExecuteMacro( sal_uInt16 nId, const String& rArgs ) const
690 const SfxMacroInfo* pInfo = GetMacroInfo( nId );
691 if ( !pInfo )
692 return sal_False;
694 SfxObjectShell* pSh = SfxObjectShell::Current();
696 SvxMacro aMacro( pInfo->GetQualifiedName(), pInfo->GetBasicName(), STARBASIC );
697 sal_Bool bRet = ExecuteMacro( pSh, &aMacro, rArgs );
699 // Release, da im Dispatcher-Execute ein Register gemacht wurde
700 ((SfxMacroConfig*)this)->ReleaseSlotId( nId );
701 return bRet;
704 sal_Bool SfxMacroConfig::ExecuteMacro( SfxObjectShell *pSh, const SvxMacro* pMacro, const String& /*rArgs*/ ) const
706 SfxApplication *pApp = SFX_APP();
708 // Name des Macros oder Scripts bzw. ScriptCode
709 String aCode( pMacro->GetMacName() );
710 ErrCode nErr = ERRCODE_NONE;
712 // Ist es ein Basic-Macro ?
713 ScriptType eSType = pMacro->GetScriptType();
714 sal_Bool bIsBasic = eSType == STARBASIC;
715 sal_Bool bIsStarScript = ( eSType == EXTENDED_STYPE && pMacro->GetLibName().SearchAscii( "StarScript" ) != STRING_NOTFOUND );
716 sal_Bool bIsBasicLibBased = bIsBasic || bIsStarScript || !pSh;
718 if ( bIsBasicLibBased )
720 pApp->EnterBasicCall();
721 BasicManager *pAppMgr = SFX_APP()->GetBasicManager();
722 if( bIsBasic )
724 // BasicManager von Document?
725 BasicManager *pMgr = pSh ? pSh->GetBasicManager() : NULL;
727 // Da leider der Name zwischendurch h"aufig gewechselt hat ...
728 if( SFX_APP()->GetName() == pMacro->GetLibName() ||
729 pMacro->GetLibName().EqualsAscii("StarDesktop") )
730 pMgr = pAppMgr;
731 else if ( pMgr == pAppMgr )
732 pMgr = NULL;
734 if ( pSh && pMgr && pMgr != pAppMgr )
736 if ( !pSh->AdjustMacroMode( String() ) )
737 return sal_False;
740 if ( pSh && pMgr && pMgr == pAppMgr )
742 ::com::sun::star::uno::Any aOldThisComponent = pAppMgr->SetGlobalUNOConstant( "ThisComponent", makeAny( pSh->GetModel() ) );
743 nErr = Call( 0, aCode, pMgr );
744 pAppMgr->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
746 else if ( pMgr )
747 nErr = Call( 0, aCode, pMgr );
748 else
749 nErr = SbxERR_NO_METHOD;
753 pApp->LeaveBasicCall();
755 else
757 nErr = SbxERR_NO_METHOD;
760 return ( nErr == ERRCODE_NONE );
763 sal_Bool SfxMacroConfig::CheckMacro( SfxObjectShell *pSh, const SvxMacro* pMacro ) const
765 SfxApplication *pApp = SFX_APP();
767 // Name des Macros oder Scripts bzw. ScriptCode
768 String aCode( pMacro->GetMacName() );
769 ErrCode nErr = ERRCODE_NONE;
771 // BasicManager von Document oder Application
772 pApp->EnterBasicCall();
773 BasicManager *pAppMgr = SFX_APP()->GetBasicManager();
774 BasicManager *pMgr = pSh ? pSh->GetBasicManager() : NULL;
776 // Da leider der Name zwischendurch h"aufig gewechselt hat ...
777 if( SFX_APP()->GetName() == pMacro->GetLibName() ||
778 pMacro->GetLibName().EqualsAscii("StarDesktop") )
779 pMgr = pAppMgr;
780 else if ( pMgr == pAppMgr )
781 pMgr = NULL;
783 if ( !pMgr || !SfxQueryMacro( pMgr, aCode ) )
784 nErr = SbxERR_NO_METHOD;
785 pApp->LeaveBasicCall();
786 return ( nErr == ERRCODE_NONE );
789 //==========================================================================
791 sal_Bool SfxMacroConfig::CheckMacro( sal_uInt16 nId ) const
793 const SfxMacroInfo* pInfo = GetMacroInfo( nId );
794 if ( !pInfo )
795 return sal_False;
797 // Basic nur initialisieren, wenn default nicht ::com::sun::star::script::JavaScript; dann mu\s
798 // in IsBasic() sowieso das Basic angelegt werden
799 SfxObjectShell* pSh = SfxObjectShell::Current();
801 SfxApplication *pApp = SFX_APP();
802 pApp->EnterBasicCall();
804 // BasicManager von Document oder Application
805 BasicManager *pAppMgr = SFX_APP()->GetBasicManager();
806 BasicManager *pMgr = pSh ? pSh->GetBasicManager() : NULL;
808 if( SFX_APP()->GetName() == pInfo->GetBasicName() )
809 pMgr = SFX_APP()->GetBasicManager();
810 else if ( pMgr == pAppMgr )
811 pMgr = NULL;
813 String aFull( pInfo->GetQualifiedName() );
814 sal_Bool bIsBasic = pMgr ? IsBasic( 0, aFull, pMgr ) : sal_False;
815 pApp->LeaveBasicCall();
816 return bIsBasic;
819 //==========================================================================
821 IMPL_LINK( SfxMacroConfig, CallbackHdl_Impl, SfxMacroConfig*, pConfig )
823 (void)pConfig; // unused
824 pImp->bWaitingForCallback = sal_False;
825 return 0;
828 IMPL_LINK( SfxMacroConfig, EventHdl_Impl, SfxMacroInfo*, pInfo )
830 delete pInfo;
831 pImp->nEventId = 0;
832 return 0;
835 sal_Bool SfxMacroConfig::IsBasic(
836 SbxObject* /*pVCtrl*/,
837 const String& rCode,
838 BasicManager* pMgr )
840 sal_Bool bFound;
841 SFX_APP()->EnterBasicCall();
842 bFound = SfxQueryMacro( pMgr, rCode ) != 0;
843 SFX_APP()->LeaveBasicCall();
844 return bFound;
847 ErrCode SfxMacroConfig::Call(
848 SbxObject* /*pVCtrl*/,
849 const String& rCode,
850 BasicManager* pMgr,
851 SbxArray *pArgs,
852 SbxValue *pRet )
854 SfxApplication *pApp = SFX_APP();
855 pApp->EnterBasicCall();
856 SbMethod* pMethod = SfxQueryMacro( pMgr, rCode );
857 ErrCode nErr = 0;
858 if( pMethod )
860 if ( pArgs )
861 pMethod->SetParameters( pArgs );
862 nErr = pMethod->Call( pRet );
864 else
865 nErr = ERRCODE_BASIC_PROC_UNDEFINED;
867 pApp->LeaveBasicCall();
868 return nErr;
872 sal_Bool SfxMacroConfig::IsMacroSlot( sal_uInt16 nId )
874 return ( nId >= SID_MACRO_START && nId <= SID_MACRO_END );