1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: basobj2.cxx,v $
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_basctl.hxx"
34 #include <ide_pch.hxx>
38 #include <basic/sbx.hxx>
39 #include <svtools/moduleoptions.hxx>
40 #include <com/sun/star/document/XEmbeddedScripts.hpp>
41 #include <com/sun/star/document/XScriptInvocationContext.hpp>
43 #include <iderdll.hxx>
44 #include <iderdll2.hxx>
46 #include <macrodlg.hxx>
47 #include <moduldlg.hxx>
48 #include <basidesh.hxx>
49 #include <basidesh.hrc>
50 #include <baside2.hxx>
51 #include <basicmod.hxx>
54 using namespace ::com::sun::star
;
55 using namespace ::com::sun::star::uno
;
56 using namespace ::com::sun::star::container
;
59 //----------------------------------------------------------------------------
62 SAL_DLLPUBLIC_EXPORT rtl_uString
* basicide_choose_macro( void* pOnlyInDocument_AsXModel
, BOOL bChooseOnly
, rtl_uString
* pMacroDesc
)
64 ::rtl::OUString
aMacroDesc( pMacroDesc
);
65 Reference
< frame::XModel
> aDocument( static_cast< frame::XModel
* >( pOnlyInDocument_AsXModel
) );
66 ::rtl::OUString aScriptURL
= BasicIDE::ChooseMacro( aDocument
, bChooseOnly
, aMacroDesc
);
67 rtl_uString
* pScriptURL
= aScriptURL
.pData
;
68 rtl_uString_acquire( pScriptURL
);
72 SAL_DLLPUBLIC_EXPORT
void basicide_macro_organizer( INT16 nTabId
)
74 OSL_TRACE("in basicide_macro_organizer");
75 BasicIDE::Organize( nTabId
);
81 //----------------------------------------------------------------------------
83 void Organize( INT16 tabId
)
87 BasicEntryDescriptor aDesc
;
88 BasicIDEShell
* pIDEShell
= IDE_DLL()->GetShell();
91 IDEBaseWindow
* pCurWin
= pIDEShell
->GetCurWindow();
93 aDesc
= pCurWin
->CreateEntryDescriptor();
96 Window
* pParent
= Application::GetDefDialogParent();
97 OrganizeDialog
* pDlg
= new OrganizeDialog( pParent
, tabId
, aDesc
);
102 //----------------------------------------------------------------------------
104 BOOL
IsValidSbxName( const String
& rName
)
106 for ( USHORT nChar
= 0; nChar
< rName
.Len(); nChar
++ )
108 BOOL bValid
= ( ( rName
.GetChar(nChar
) >= 'A' && rName
.GetChar(nChar
) <= 'Z' ) ||
109 ( rName
.GetChar(nChar
) >= 'a' && rName
.GetChar(nChar
) <= 'z' ) ||
110 ( rName
.GetChar(nChar
) >= '0' && rName
.GetChar(nChar
) <= '9' && nChar
) ||
111 ( rName
.GetChar(nChar
) == '_' ) );
118 static BOOL
StringCompareLessThan( const String
& rStr1
, const String
& rStr2
)
120 return (rStr1
.CompareIgnoreCaseToAscii( rStr2
) == COMPARE_LESS
);
123 //----------------------------------------------------------------------------
125 Sequence
< ::rtl::OUString
> GetMergedLibraryNames( const Reference
< script::XLibraryContainer
>& xModLibContainer
, const Reference
< script::XLibraryContainer
>& xDlgLibContainer
)
127 // create a sorted list of module library names
128 ::std::vector
<String
> aModLibList
;
129 if ( xModLibContainer
.is() )
131 Sequence
< ::rtl::OUString
> aModLibNames
= xModLibContainer
->getElementNames();
132 sal_Int32 nModLibCount
= aModLibNames
.getLength();
133 const ::rtl::OUString
* pModLibNames
= aModLibNames
.getConstArray();
134 for ( sal_Int32 i
= 0 ; i
< nModLibCount
; i
++ )
135 aModLibList
.push_back( pModLibNames
[ i
] );
136 ::std::sort( aModLibList
.begin() , aModLibList
.end() , StringCompareLessThan
);
139 // create a sorted list of dialog library names
140 ::std::vector
<String
> aDlgLibList
;
141 if ( xDlgLibContainer
.is() )
143 Sequence
< ::rtl::OUString
> aDlgLibNames
= xDlgLibContainer
->getElementNames();
144 sal_Int32 nDlgLibCount
= aDlgLibNames
.getLength();
145 const ::rtl::OUString
* pDlgLibNames
= aDlgLibNames
.getConstArray();
146 for ( sal_Int32 i
= 0 ; i
< nDlgLibCount
; i
++ )
147 aDlgLibList
.push_back( pDlgLibNames
[ i
] );
148 ::std::sort( aDlgLibList
.begin() , aDlgLibList
.end() , StringCompareLessThan
);
152 ::std::vector
<String
> aLibList( aModLibList
.size() + aDlgLibList
.size() );
153 ::std::merge( aModLibList
.begin(), aModLibList
.end(), aDlgLibList
.begin(), aDlgLibList
.end(), aLibList
.begin(), StringCompareLessThan
);
154 ::std::vector
<String
>::iterator aIterEnd
= ::std::unique( aLibList
.begin(), aLibList
.end() ); // move unique elements to the front
155 aLibList
.erase( aIterEnd
, aLibList
.end() ); // remove duplicates
158 sal_Int32 nLibCount
= aLibList
.size();
159 Sequence
< ::rtl::OUString
> aSeqLibNames( nLibCount
);
160 for ( sal_Int32 i
= 0 ; i
< nLibCount
; i
++ )
161 aSeqLibNames
.getArray()[ i
] = aLibList
[ i
];
166 //----------------------------------------------------------------------------
168 bool RenameModule( Window
* pErrorParent
, const ScriptDocument
& rDocument
, const String
& rLibName
, const String
& rOldName
, const String
& rNewName
)
170 if ( !rDocument
.hasModule( rLibName
, rOldName
) )
172 OSL_ENSURE( false, "BasicIDE::RenameModule: old module name is invalid!" );
176 if ( rDocument
.hasModule( rLibName
, rNewName
) )
178 ErrorBox
aError( pErrorParent
, WB_OK
| WB_DEF_OK
, String( IDEResId( RID_STR_SBXNAMEALLREADYUSED2
) ) );
184 if ( rNewName
.Len() == 0 )
186 ErrorBox
aError( pErrorParent
, WB_OK
| WB_DEF_OK
, String( IDEResId( RID_STR_BADSBXNAME
) ) );
191 if ( !rDocument
.renameModule( rLibName
, rOldName
, rNewName
) )
194 BasicIDEShell
* pIDEShell
= IDE_DLL()->GetShell();
197 IDEBaseWindow
* pWin
= pIDEShell
->FindWindow( rDocument
, rLibName
, rOldName
, BASICIDE_TYPE_MODULE
, FALSE
);
200 // set new name in window
201 pWin
->SetName( rNewName
);
203 // set new module in module window
204 ModulWindow
* pModWin
= (ModulWindow
*)pWin
;
205 pModWin
->SetSbModule( (SbModule
*)pModWin
->GetBasic()->FindModule( rNewName
) );
208 USHORT nId
= (USHORT
)(pIDEShell
->GetIDEWindowTable()).GetKey( pWin
);
209 DBG_ASSERT( nId
, "No entry in Tabbar!" );
212 BasicIDETabBar
* pTabBar
= (BasicIDETabBar
*)pIDEShell
->GetTabBar();
213 pTabBar
->SetPageText( nId
, rNewName
);
215 pTabBar
->MakeVisible( pTabBar
->GetCurPageId() );
222 //----------------------------------------------------------------------------
224 ::rtl::OUString
ChooseMacro( const uno::Reference
< frame::XModel
>& rxLimitToDocument
, BOOL bChooseOnly
, const ::rtl::OUString
& rMacroDesc
)
230 IDE_DLL()->GetExtraData()->ChoosingMacro() = TRUE
;
231 SFX_APP()->EnterBasicCall();
235 SbMethod
* pMethod
= NULL
;
237 MacroChooser
* pChooser
= new MacroChooser( NULL
, TRUE
);
238 if ( bChooseOnly
|| !SvtModuleOptions().IsBasicIDE() )
239 pChooser
->SetMode( MACROCHOOSER_CHOOSEONLY
);
241 if ( !bChooseOnly
&& rxLimitToDocument
.is() )
243 pChooser
->SetMode( MACROCHOOSER_RECORDING
);
245 short nRetValue
= pChooser
->Execute();
247 IDE_DLL()->GetExtraData()->ChoosingMacro() = FALSE
;
253 pMethod
= pChooser
->GetMacro();
254 if ( !pMethod
&& pChooser
->GetMode() == MACROCHOOSER_RECORDING
)
255 pMethod
= pChooser
->CreateMacro();
259 SbModule
* pModule
= pMethod
->GetModule();
260 DBG_ASSERT(pModule
, "BasicIDE::ChooseMacro: No Module found!");
263 StarBASIC
* pBasic
= (StarBASIC
*)pModule
->GetParent();
264 DBG_ASSERT(pBasic
, "BasicIDE::ChooseMacro: No Basic found!");
267 BasicManager
* pBasMgr
= BasicIDE::FindBasicManager( pBasic
);
268 DBG_ASSERT(pBasMgr
, "BasicIDE::ChooseMacro: No BasicManager found!");
273 aName
+= pBasic
->GetName();
275 aName
+= pModule
->GetName();
277 aName
+= pMethod
->GetName();
280 String aLanguage
= String::CreateFromAscii("Basic");
284 ScriptDocument
aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr
) );
285 if ( aDocument
.isDocument() )
288 aLocation
= String::CreateFromAscii("document");
290 if ( rxLimitToDocument
.is() )
292 uno::Reference
< frame::XModel
> xLimitToDocument( rxLimitToDocument
);
294 uno::Reference
< document::XEmbeddedScripts
> xScripts( rxLimitToDocument
, UNO_QUERY
);
295 if ( !xScripts
.is() )
296 { // the document itself does not support embedding scripts
297 uno::Reference
< document::XScriptInvocationContext
> xContext( rxLimitToDocument
, UNO_QUERY
);
299 xScripts
= xContext
->getScriptContainer();
301 { // but it is able to refer to a document which actually does support this
302 xLimitToDocument
.set( xScripts
, UNO_QUERY
);
303 if ( !xLimitToDocument
.is() )
305 OSL_ENSURE( false, "BasicIDE::ChooseMacro: a script container which is no document!?" );
306 xLimitToDocument
= rxLimitToDocument
;
311 if ( xLimitToDocument
!= aDocument
.getDocument() )
315 ErrorBox( NULL
, WB_OK
| WB_DEF_OK
, String( IDEResId( RID_STR_ERRORCHOOSEMACRO
) ) ).Execute();
322 aLocation
= String::CreateFromAscii("application");
328 aScriptURL
= String::CreateFromAscii("vnd.sun.star.script:");
330 aScriptURL
+= String::CreateFromAscii("?language=");
331 aScriptURL
+= aLanguage
;
332 aScriptURL
+= String::CreateFromAscii("&location=");
333 aScriptURL
+= aLocation
;
340 if ( pMethod
&& !rxLimitToDocument
.is() )
342 pMethod
->AddRef(); // festhalten, bis Event abgearbeitet.
343 Application::PostUserEvent( LINK( IDE_DLL()->GetExtraData(), BasicIDEData
, ExecuteMacroEvent
), pMethod
);
351 SFX_APP()->LeaveBasicCall();
353 return ::rtl::OUString( aScriptURL
);
356 //----------------------------------------------------------------------------
358 Sequence
< ::rtl::OUString
> GetMethodNames( const ScriptDocument
& rDocument
, const String
& rLibName
, const String
& rModName
)
359 throw(NoSuchElementException
)
361 Sequence
< ::rtl::OUString
> aSeqMethods
;
364 ::rtl::OUString aOUSource
;
365 if ( rDocument
.getModule( rLibName
, rModName
, aOUSource
) )
367 SbModuleRef xModule
= new SbModule( rModName
);
368 xModule
->SetSource32( aOUSource
);
369 USHORT nCount
= xModule
->GetMethods()->Count();
370 aSeqMethods
.realloc( nCount
);
372 for ( USHORT i
= 0; i
< nCount
; i
++ )
374 SbMethod
* pMethod
= (SbMethod
*)xModule
->GetMethods()->Get( i
);
375 DBG_ASSERT( pMethod
, "Method not found! (NULL)" );
376 aSeqMethods
.getArray()[ i
] = pMethod
->GetName();
383 //----------------------------------------------------------------------------
385 BOOL
HasMethod( const ScriptDocument
& rDocument
, const String
& rLibName
, const String
& rModName
, const String
& rMethName
)
387 BOOL bHasMethod
= FALSE
;
389 ::rtl::OUString aOUSource
;
390 if ( rDocument
.hasModule( rLibName
, rModName
) && rDocument
.getModule( rLibName
, rModName
, aOUSource
) )
392 SbModuleRef xModule
= new SbModule( rModName
);
393 xModule
->SetSource32( aOUSource
);
394 SbxArray
* pMethods
= xModule
->GetMethods();
397 SbMethod
* pMethod
= (SbMethod
*)pMethods
->Find( rMethName
, SbxCLASS_METHOD
);
405 } //namespace BasicIDE
406 //----------------------------------------------------------------------------