xref: /AOO41X/main/basctl/source/basicide/basobj2.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basctl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <ide_pch.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "basobj.hxx"
34*cdf0e10cSrcweir #include "iderdll.hxx"
35*cdf0e10cSrcweir #include "iderdll2.hxx"
36*cdf0e10cSrcweir #include "iderid.hxx"
37*cdf0e10cSrcweir #include "macrodlg.hxx"
38*cdf0e10cSrcweir #include "moduldlg.hxx"
39*cdf0e10cSrcweir #include "basidesh.hxx"
40*cdf0e10cSrcweir #include "basidesh.hrc"
41*cdf0e10cSrcweir #include "baside2.hxx"
42*cdf0e10cSrcweir #include "basicmod.hxx"
43*cdf0e10cSrcweir #include "basdoc.hxx"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedScripts.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/document/XScriptInvocationContext.hpp>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <basic/sbx.hxx>
49*cdf0e10cSrcweir #include <framework/documentundoguard.hxx>
50*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
51*cdf0e10cSrcweir #include <unotools/moduleoptions.hxx>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include <vector>
54*cdf0e10cSrcweir #include <algorithm>
55*cdf0e10cSrcweir #include <memory>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using namespace ::com::sun::star;
58*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
59*cdf0e10cSrcweir using namespace ::com::sun::star::container;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir //----------------------------------------------------------------------------
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir extern "C" {
65*cdf0e10cSrcweir     SAL_DLLPUBLIC_EXPORT rtl_uString* basicide_choose_macro( void* pOnlyInDocument_AsXModel, sal_Bool bChooseOnly, rtl_uString* pMacroDesc )
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		::rtl::OUString aMacroDesc( pMacroDesc );
68*cdf0e10cSrcweir         Reference< frame::XModel > aDocument( static_cast< frame::XModel* >( pOnlyInDocument_AsXModel ) );
69*cdf0e10cSrcweir 		::rtl::OUString aScriptURL = BasicIDE::ChooseMacro( aDocument, bChooseOnly, aMacroDesc );
70*cdf0e10cSrcweir 		rtl_uString* pScriptURL = aScriptURL.pData;
71*cdf0e10cSrcweir         rtl_uString_acquire( pScriptURL );
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         return pScriptURL;
74*cdf0e10cSrcweir 	}
75*cdf0e10cSrcweir     SAL_DLLPUBLIC_EXPORT void basicide_macro_organizer( sal_Int16 nTabId )
76*cdf0e10cSrcweir     {
77*cdf0e10cSrcweir         OSL_TRACE("in basicide_macro_organizer");
78*cdf0e10cSrcweir         BasicIDE::Organize( nTabId );
79*cdf0e10cSrcweir     }
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir namespace BasicIDE
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir //----------------------------------------------------------------------------
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir void Organize( sal_Int16 tabId )
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     BasicIDEDLL::Init();
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     BasicEntryDescriptor aDesc;
91*cdf0e10cSrcweir     BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
92*cdf0e10cSrcweir     if ( pIDEShell )
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         IDEBaseWindow* pCurWin = pIDEShell->GetCurWindow();
95*cdf0e10cSrcweir         if ( pCurWin )
96*cdf0e10cSrcweir             aDesc = pCurWin->CreateEntryDescriptor();
97*cdf0e10cSrcweir     }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	Window* pParent = Application::GetDefDialogParent();
100*cdf0e10cSrcweir 	OrganizeDialog* pDlg = new OrganizeDialog( pParent, tabId, aDesc );
101*cdf0e10cSrcweir 	pDlg->Execute();
102*cdf0e10cSrcweir 	delete pDlg;
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir //----------------------------------------------------------------------------
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir sal_Bool IsValidSbxName( const String& rName )
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir 	for ( sal_uInt16 nChar = 0; nChar < rName.Len(); nChar++ )
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		sal_Bool bValid = (	( rName.GetChar(nChar) >= 'A' && rName.GetChar(nChar) <= 'Z' ) ||
112*cdf0e10cSrcweir 						( rName.GetChar(nChar) >= 'a' && rName.GetChar(nChar) <= 'z' ) ||
113*cdf0e10cSrcweir 						( rName.GetChar(nChar) >= '0' && rName.GetChar(nChar) <= '9' && nChar ) ||
114*cdf0e10cSrcweir 						( rName.GetChar(nChar) == '_' ) );
115*cdf0e10cSrcweir 		if ( !bValid )
116*cdf0e10cSrcweir 			return sal_False;
117*cdf0e10cSrcweir 	}
118*cdf0e10cSrcweir 	return sal_True;
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir static sal_Bool StringCompareLessThan( const String& rStr1, const String& rStr2 )
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     return (rStr1.CompareIgnoreCaseToAscii( rStr2 ) == COMPARE_LESS);
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir //----------------------------------------------------------------------------
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir Sequence< ::rtl::OUString > GetMergedLibraryNames( const Reference< script::XLibraryContainer >& xModLibContainer, const Reference< script::XLibraryContainer >& xDlgLibContainer )
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir     // create a sorted list of module library names
131*cdf0e10cSrcweir     ::std::vector<String> aModLibList;
132*cdf0e10cSrcweir     if ( xModLibContainer.is() )
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aModLibNames = xModLibContainer->getElementNames();
135*cdf0e10cSrcweir 		sal_Int32 nModLibCount = aModLibNames.getLength();
136*cdf0e10cSrcweir 		const ::rtl::OUString* pModLibNames = aModLibNames.getConstArray();
137*cdf0e10cSrcweir         for ( sal_Int32 i = 0 ; i < nModLibCount ; i++ )
138*cdf0e10cSrcweir 			aModLibList.push_back( pModLibNames[ i ] );
139*cdf0e10cSrcweir         ::std::sort( aModLibList.begin() , aModLibList.end() , StringCompareLessThan );
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     // create a sorted list of dialog library names
143*cdf0e10cSrcweir 	::std::vector<String> aDlgLibList;
144*cdf0e10cSrcweir     if ( xDlgLibContainer.is() )
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aDlgLibNames = xDlgLibContainer->getElementNames();
147*cdf0e10cSrcweir 		sal_Int32 nDlgLibCount = aDlgLibNames.getLength();
148*cdf0e10cSrcweir 		const ::rtl::OUString* pDlgLibNames = aDlgLibNames.getConstArray();
149*cdf0e10cSrcweir         for ( sal_Int32 i = 0 ; i < nDlgLibCount ; i++ )
150*cdf0e10cSrcweir 			aDlgLibList.push_back( pDlgLibNames[ i ] );
151*cdf0e10cSrcweir         ::std::sort( aDlgLibList.begin() , aDlgLibList.end() , StringCompareLessThan );
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     // merge both lists
155*cdf0e10cSrcweir     ::std::vector<String> aLibList( aModLibList.size() + aDlgLibList.size() );
156*cdf0e10cSrcweir     ::std::merge( aModLibList.begin(), aModLibList.end(), aDlgLibList.begin(), aDlgLibList.end(), aLibList.begin(), StringCompareLessThan );
157*cdf0e10cSrcweir     ::std::vector<String>::iterator aIterEnd = ::std::unique( aLibList.begin(), aLibList.end() );  // move unique elements to the front
158*cdf0e10cSrcweir     aLibList.erase( aIterEnd, aLibList.end() ); // remove duplicates
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     // copy to sequence
161*cdf0e10cSrcweir 	sal_Int32 nLibCount = aLibList.size();
162*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSeqLibNames( nLibCount );
163*cdf0e10cSrcweir     for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
164*cdf0e10cSrcweir         aSeqLibNames.getArray()[ i ] = aLibList[ i ];
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     return aSeqLibNames;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir //----------------------------------------------------------------------------
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir bool RenameModule( Window* pErrorParent, const ScriptDocument& rDocument, const String& rLibName, const String& rOldName, const String& rNewName )
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir     if ( !rDocument.hasModule( rLibName, rOldName ) )
174*cdf0e10cSrcweir     {
175*cdf0e10cSrcweir         OSL_ENSURE( false, "BasicIDE::RenameModule: old module name is invalid!" );
176*cdf0e10cSrcweir         return false;
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     if ( rDocument.hasModule( rLibName, rNewName ) )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir         ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_SBXNAMEALLREADYUSED2 ) ) );
182*cdf0e10cSrcweir         aError.Execute();
183*cdf0e10cSrcweir 		return false;
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	// #i74440
187*cdf0e10cSrcweir 	if ( rNewName.Len() == 0 )
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) );
190*cdf0e10cSrcweir         aError.Execute();
191*cdf0e10cSrcweir 		return false;
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     if ( !rDocument.renameModule( rLibName, rOldName, rNewName ) )
195*cdf0e10cSrcweir         return false;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 	BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
198*cdf0e10cSrcweir 	if ( pIDEShell )
199*cdf0e10cSrcweir 	{
200*cdf0e10cSrcweir 		IDEBaseWindow* pWin = pIDEShell->FindWindow( rDocument, rLibName, rNewName, BASICIDE_TYPE_MODULE, sal_True );
201*cdf0e10cSrcweir 		if ( pWin )
202*cdf0e10cSrcweir 		{
203*cdf0e10cSrcweir 			// set new name in window
204*cdf0e10cSrcweir             pWin->SetName( rNewName );
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir             // set new module in module window
207*cdf0e10cSrcweir             ModulWindow* pModWin = (ModulWindow*)pWin;
208*cdf0e10cSrcweir             pModWin->SetSbModule( (SbModule*)pModWin->GetBasic()->FindModule( rNewName ) );
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 			// update tabwriter
211*cdf0e10cSrcweir 			sal_uInt16 nId = (sal_uInt16)(pIDEShell->GetIDEWindowTable()).GetKey( pWin );
212*cdf0e10cSrcweir 			DBG_ASSERT( nId, "No entry in Tabbar!" );
213*cdf0e10cSrcweir 			if ( nId )
214*cdf0e10cSrcweir 			{
215*cdf0e10cSrcweir 				BasicIDETabBar*	pTabBar = (BasicIDETabBar*)pIDEShell->GetTabBar();
216*cdf0e10cSrcweir 				pTabBar->SetPageText( nId, rNewName );
217*cdf0e10cSrcweir 				pTabBar->Sort();
218*cdf0e10cSrcweir 				pTabBar->MakeVisible( pTabBar->GetCurPageId() );
219*cdf0e10cSrcweir 			}
220*cdf0e10cSrcweir 		}
221*cdf0e10cSrcweir 	}
222*cdf0e10cSrcweir     return true;
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir //----------------------------------------------------------------------------
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir namespace
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir     struct MacroExecutionData
231*cdf0e10cSrcweir     {
232*cdf0e10cSrcweir         ScriptDocument  aDocument;
233*cdf0e10cSrcweir         SbMethodRef     xMethod;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         MacroExecutionData()
236*cdf0e10cSrcweir             :aDocument( ScriptDocument::NoDocument )
237*cdf0e10cSrcweir             ,xMethod( NULL )
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir         }
240*cdf0e10cSrcweir     };
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     class MacroExecution
243*cdf0e10cSrcweir     {
244*cdf0e10cSrcweir     public:
245*cdf0e10cSrcweir         DECL_STATIC_LINK( MacroExecution, ExecuteMacroEvent, MacroExecutionData* );
246*cdf0e10cSrcweir     };
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     IMPL_STATIC_LINK( MacroExecution, ExecuteMacroEvent, MacroExecutionData*, i_pData )
250*cdf0e10cSrcweir     {
251*cdf0e10cSrcweir         (void)pThis;
252*cdf0e10cSrcweir         ENSURE_OR_RETURN( i_pData, "wrong MacroExecutionData", 0L );
253*cdf0e10cSrcweir         // take ownership of the data
254*cdf0e10cSrcweir         ::std::auto_ptr< MacroExecutionData > pData( i_pData );
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir         DBG_ASSERT( pData->xMethod->GetParent()->GetFlags() & SBX_EXTSEARCH, "Kein EXTSEARCH!" );
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir         // in case this is a document-local macro, try to protect the document's Undo Manager from
259*cdf0e10cSrcweir         // flawed scripts
260*cdf0e10cSrcweir         ::std::auto_ptr< ::framework::DocumentUndoGuard > pUndoGuard;
261*cdf0e10cSrcweir         if ( pData->aDocument.isDocument() )
262*cdf0e10cSrcweir             pUndoGuard.reset( new ::framework::DocumentUndoGuard( pData->aDocument.getDocument() ) );
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir         BasicIDE::RunMethod( pData->xMethod );
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir         return 1L;
267*cdf0e10cSrcweir     }
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir //----------------------------------------------------------------------------
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir ::rtl::OUString ChooseMacro( const uno::Reference< frame::XModel >& rxLimitToDocument, sal_Bool bChooseOnly, const ::rtl::OUString& rMacroDesc )
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir 	(void)rMacroDesc;
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 	BasicIDEDLL::Init();
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	IDE_DLL()->GetExtraData()->ChoosingMacro() = sal_True;
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     String aScriptURL;
281*cdf0e10cSrcweir     sal_Bool bError = sal_False;
282*cdf0e10cSrcweir     SbMethod* pMethod = NULL;
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir     ::std::auto_ptr< MacroChooser > pChooser( new MacroChooser( NULL, sal_True ) );
285*cdf0e10cSrcweir 	if ( bChooseOnly || !SvtModuleOptions().IsBasicIDE() )
286*cdf0e10cSrcweir         pChooser->SetMode( MACROCHOOSER_CHOOSEONLY );
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir     if ( !bChooseOnly && rxLimitToDocument.is() )
289*cdf0e10cSrcweir         // Hack!
290*cdf0e10cSrcweir         pChooser->SetMode( MACROCHOOSER_RECORDING );
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 	short nRetValue = pChooser->Execute();
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	IDE_DLL()->GetExtraData()->ChoosingMacro() = sal_False;
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 	switch ( nRetValue )
297*cdf0e10cSrcweir 	{
298*cdf0e10cSrcweir 		case MACRO_OK_RUN:
299*cdf0e10cSrcweir 		{
300*cdf0e10cSrcweir 			pMethod = pChooser->GetMacro();
301*cdf0e10cSrcweir             if ( !pMethod && pChooser->GetMode() == MACROCHOOSER_RECORDING )
302*cdf0e10cSrcweir                 pMethod = pChooser->CreateMacro();
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir             if ( !pMethod )
305*cdf0e10cSrcweir                 break;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir             SbModule* pModule = pMethod->GetModule();
308*cdf0e10cSrcweir             ENSURE_OR_BREAK( pModule, "BasicIDE::ChooseMacro: No Module found!" );
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir             StarBASIC* pBasic = (StarBASIC*)pModule->GetParent();
311*cdf0e10cSrcweir 			ENSURE_OR_BREAK( pBasic, "BasicIDE::ChooseMacro: No Basic found!" );
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir             BasicManager* pBasMgr = BasicIDE::FindBasicManager( pBasic );
314*cdf0e10cSrcweir 			ENSURE_OR_BREAK( pBasMgr, "BasicIDE::ChooseMacro: No BasicManager found!" );
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir             // name
317*cdf0e10cSrcweir             String aName;
318*cdf0e10cSrcweir 			aName += pBasic->GetName();
319*cdf0e10cSrcweir 			aName += '.';
320*cdf0e10cSrcweir 			aName += pModule->GetName();
321*cdf0e10cSrcweir 			aName += '.';
322*cdf0e10cSrcweir 			aName += pMethod->GetName();
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir             // language
325*cdf0e10cSrcweir             String aLanguage = String::CreateFromAscii("Basic");
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir             // location
328*cdf0e10cSrcweir             String aLocation;
329*cdf0e10cSrcweir 			ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
330*cdf0e10cSrcweir             if ( aDocument.isDocument() )
331*cdf0e10cSrcweir             {
332*cdf0e10cSrcweir                 // document basic
333*cdf0e10cSrcweir                 aLocation = String::CreateFromAscii("document");
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir                 if ( rxLimitToDocument.is() )
336*cdf0e10cSrcweir                 {
337*cdf0e10cSrcweir                     uno::Reference< frame::XModel > xLimitToDocument( rxLimitToDocument );
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir                     uno::Reference< document::XEmbeddedScripts > xScripts( rxLimitToDocument, UNO_QUERY );
340*cdf0e10cSrcweir                     if ( !xScripts.is() )
341*cdf0e10cSrcweir                     {   // the document itself does not support embedding scripts
342*cdf0e10cSrcweir                         uno::Reference< document::XScriptInvocationContext > xContext( rxLimitToDocument, UNO_QUERY );
343*cdf0e10cSrcweir                         if ( xContext.is() )
344*cdf0e10cSrcweir                             xScripts = xContext->getScriptContainer();
345*cdf0e10cSrcweir                         if ( xScripts.is() )
346*cdf0e10cSrcweir                         {   // but it is able to refer to a document which actually does support this
347*cdf0e10cSrcweir                             xLimitToDocument.set( xScripts, UNO_QUERY );
348*cdf0e10cSrcweir                             if ( !xLimitToDocument.is() )
349*cdf0e10cSrcweir                             {
350*cdf0e10cSrcweir                                 OSL_ENSURE( false, "BasicIDE::ChooseMacro: a script container which is no document!?" );
351*cdf0e10cSrcweir                                 xLimitToDocument = rxLimitToDocument;
352*cdf0e10cSrcweir                             }
353*cdf0e10cSrcweir                         }
354*cdf0e10cSrcweir                     }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir                     if ( xLimitToDocument != aDocument.getDocument() )
357*cdf0e10cSrcweir                     {
358*cdf0e10cSrcweir                         // error
359*cdf0e10cSrcweir                         bError = sal_True;
360*cdf0e10cSrcweir                         ErrorBox( NULL, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_ERRORCHOOSEMACRO ) ) ).Execute();
361*cdf0e10cSrcweir                     }
362*cdf0e10cSrcweir                 }
363*cdf0e10cSrcweir             }
364*cdf0e10cSrcweir             else
365*cdf0e10cSrcweir             {
366*cdf0e10cSrcweir                 // application basic
367*cdf0e10cSrcweir                 aLocation = String::CreateFromAscii("application");
368*cdf0e10cSrcweir             }
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir             // script URL
371*cdf0e10cSrcweir             if ( !bError )
372*cdf0e10cSrcweir             {
373*cdf0e10cSrcweir                 aScriptURL = String::CreateFromAscii("vnd.sun.star.script:");
374*cdf0e10cSrcweir                 aScriptURL += aName;
375*cdf0e10cSrcweir                 aScriptURL += String::CreateFromAscii("?language=");
376*cdf0e10cSrcweir                 aScriptURL += aLanguage;
377*cdf0e10cSrcweir                 aScriptURL += String::CreateFromAscii("&location=");
378*cdf0e10cSrcweir                 aScriptURL += aLocation;
379*cdf0e10cSrcweir             }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir 			if ( !rxLimitToDocument.is() )
382*cdf0e10cSrcweir 			{
383*cdf0e10cSrcweir                 MacroExecutionData* pExecData = new MacroExecutionData;
384*cdf0e10cSrcweir                 pExecData->aDocument = aDocument;
385*cdf0e10cSrcweir                 pExecData->xMethod = pMethod;   // keep alive until the event has been processed
386*cdf0e10cSrcweir 				Application::PostUserEvent( STATIC_LINK( NULL, MacroExecution, ExecuteMacroEvent ), pExecData );
387*cdf0e10cSrcweir 			}
388*cdf0e10cSrcweir 		}
389*cdf0e10cSrcweir 		break;
390*cdf0e10cSrcweir 	}
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir     return aScriptURL;
393*cdf0e10cSrcweir }
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir //----------------------------------------------------------------------------
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir Sequence< ::rtl::OUString > GetMethodNames( const ScriptDocument& rDocument, const String& rLibName, const String& rModName )
398*cdf0e10cSrcweir 	throw(NoSuchElementException )
399*cdf0e10cSrcweir {
400*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aSeqMethods;
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir 	// get module
403*cdf0e10cSrcweir     ::rtl::OUString aOUSource;
404*cdf0e10cSrcweir     if ( rDocument.getModule( rLibName, rModName, aOUSource ) )
405*cdf0e10cSrcweir     {
406*cdf0e10cSrcweir 	    SbModuleRef xModule = new SbModule( rModName );
407*cdf0e10cSrcweir 	    xModule->SetSource32( aOUSource );
408*cdf0e10cSrcweir 	    sal_uInt16 nCount = xModule->GetMethods()->Count();
409*cdf0e10cSrcweir 		sal_uInt16 nRealCount = nCount;
410*cdf0e10cSrcweir 	    for ( sal_uInt16 i = 0; i < nCount; i++ )
411*cdf0e10cSrcweir 	    {
412*cdf0e10cSrcweir 		    SbMethod* pMethod = (SbMethod*)xModule->GetMethods()->Get( i );
413*cdf0e10cSrcweir 			if( pMethod->IsHidden() )
414*cdf0e10cSrcweir 				--nRealCount;
415*cdf0e10cSrcweir 	    }
416*cdf0e10cSrcweir         aSeqMethods.realloc( nRealCount );
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 		sal_uInt16 iTarget = 0;
419*cdf0e10cSrcweir 	    for ( sal_uInt16 i = 0 ; i < nCount; ++i )
420*cdf0e10cSrcweir 	    {
421*cdf0e10cSrcweir 		    SbMethod* pMethod = (SbMethod*)xModule->GetMethods()->Get( i );
422*cdf0e10cSrcweir 			if( pMethod->IsHidden() )
423*cdf0e10cSrcweir 				continue;
424*cdf0e10cSrcweir 		    DBG_ASSERT( pMethod, "Method not found! (NULL)" );
425*cdf0e10cSrcweir 		    aSeqMethods.getArray()[ iTarget++ ] = pMethod->GetName();
426*cdf0e10cSrcweir 	    }
427*cdf0e10cSrcweir     }
428*cdf0e10cSrcweir 
429*cdf0e10cSrcweir 	return aSeqMethods;
430*cdf0e10cSrcweir }
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir //----------------------------------------------------------------------------
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir sal_Bool HasMethod( const ScriptDocument& rDocument, const String& rLibName, const String& rModName, const String& rMethName )
435*cdf0e10cSrcweir {
436*cdf0e10cSrcweir     sal_Bool bHasMethod = sal_False;
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir     ::rtl::OUString aOUSource;
439*cdf0e10cSrcweir     if ( rDocument.hasModule( rLibName, rModName ) && rDocument.getModule( rLibName, rModName, aOUSource ) )
440*cdf0e10cSrcweir     {
441*cdf0e10cSrcweir         SbModuleRef xModule = new SbModule( rModName );
442*cdf0e10cSrcweir         xModule->SetSource32( aOUSource );
443*cdf0e10cSrcweir         SbxArray* pMethods = xModule->GetMethods();
444*cdf0e10cSrcweir         if ( pMethods )
445*cdf0e10cSrcweir         {
446*cdf0e10cSrcweir             SbMethod* pMethod = (SbMethod*)pMethods->Find( rMethName, SbxCLASS_METHOD );
447*cdf0e10cSrcweir             if ( pMethod && !pMethod->IsHidden() )
448*cdf0e10cSrcweir                 bHasMethod = sal_True;
449*cdf0e10cSrcweir         }
450*cdf0e10cSrcweir     }
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir     return bHasMethod;
453*cdf0e10cSrcweir }
454*cdf0e10cSrcweir } //namespace BasicIDE
455*cdf0e10cSrcweir //----------------------------------------------------------------------------
456