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