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