merge the formfield patch from ooo-build
[ooovba.git] / basctl / source / basicide / basobj2.cxx
blob67a3297f7471cb861dee6075069b5694d227b8a2
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: basobj2.cxx,v $
10 * $Revision: 1.37 $
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>
36 #include <vector>
37 #include <algorithm>
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>
42 #include <basobj.hxx>
43 #include <iderdll.hxx>
44 #include <iderdll2.hxx>
45 #include <iderid.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>
52 #include <basdoc.hxx>
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::container;
59 //----------------------------------------------------------------------------
61 extern "C" {
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 );
70 return pScriptURL;
72 SAL_DLLPUBLIC_EXPORT void basicide_macro_organizer( INT16 nTabId )
74 OSL_TRACE("in basicide_macro_organizer");
75 BasicIDE::Organize( nTabId );
79 namespace BasicIDE
81 //----------------------------------------------------------------------------
83 void Organize( INT16 tabId )
85 BasicIDEDLL::Init();
87 BasicEntryDescriptor aDesc;
88 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
89 if ( pIDEShell )
91 IDEBaseWindow* pCurWin = pIDEShell->GetCurWindow();
92 if ( pCurWin )
93 aDesc = pCurWin->CreateEntryDescriptor();
96 Window* pParent = Application::GetDefDialogParent();
97 OrganizeDialog* pDlg = new OrganizeDialog( pParent, tabId, aDesc );
98 pDlg->Execute();
99 delete pDlg;
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) == '_' ) );
112 if ( !bValid )
113 return FALSE;
115 return TRUE;
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 );
151 // merge both lists
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
157 // copy to sequence
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 ];
163 return aSeqLibNames;
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!" );
173 return false;
176 if ( rDocument.hasModule( rLibName, rNewName ) )
178 ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_SBXNAMEALLREADYUSED2 ) ) );
179 aError.Execute();
180 return false;
183 // #i74440
184 if ( rNewName.Len() == 0 )
186 ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) );
187 aError.Execute();
188 return false;
191 if ( !rDocument.renameModule( rLibName, rOldName, rNewName ) )
192 return false;
194 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
195 if ( pIDEShell )
197 IDEBaseWindow* pWin = pIDEShell->FindWindow( rDocument, rLibName, rOldName, BASICIDE_TYPE_MODULE, FALSE );
198 if ( pWin )
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 ) );
207 // update tabwriter
208 USHORT nId = (USHORT)(pIDEShell->GetIDEWindowTable()).GetKey( pWin );
209 DBG_ASSERT( nId, "No entry in Tabbar!" );
210 if ( nId )
212 BasicIDETabBar* pTabBar = (BasicIDETabBar*)pIDEShell->GetTabBar();
213 pTabBar->SetPageText( nId, rNewName );
214 pTabBar->Sort();
215 pTabBar->MakeVisible( pTabBar->GetCurPageId() );
219 return true;
222 //----------------------------------------------------------------------------
224 ::rtl::OUString ChooseMacro( const uno::Reference< frame::XModel >& rxLimitToDocument, BOOL bChooseOnly, const ::rtl::OUString& rMacroDesc )
226 (void)rMacroDesc;
228 BasicIDEDLL::Init();
230 IDE_DLL()->GetExtraData()->ChoosingMacro() = TRUE;
231 SFX_APP()->EnterBasicCall();
233 String aScriptURL;
234 BOOL bError = FALSE;
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() )
242 // Hack!
243 pChooser->SetMode( MACROCHOOSER_RECORDING );
245 short nRetValue = pChooser->Execute();
247 IDE_DLL()->GetExtraData()->ChoosingMacro() = FALSE;
249 switch ( nRetValue )
251 case MACRO_OK_RUN:
253 pMethod = pChooser->GetMacro();
254 if ( !pMethod && pChooser->GetMode() == MACROCHOOSER_RECORDING )
255 pMethod = pChooser->CreateMacro();
257 if ( pMethod )
259 SbModule* pModule = pMethod->GetModule();
260 DBG_ASSERT(pModule, "BasicIDE::ChooseMacro: No Module found!");
261 if ( pModule )
263 StarBASIC* pBasic = (StarBASIC*)pModule->GetParent();
264 DBG_ASSERT(pBasic, "BasicIDE::ChooseMacro: No Basic found!");
265 if ( pBasic )
267 BasicManager* pBasMgr = BasicIDE::FindBasicManager( pBasic );
268 DBG_ASSERT(pBasMgr, "BasicIDE::ChooseMacro: No BasicManager found!");
269 if ( pBasMgr )
271 // name
272 String aName;
273 aName += pBasic->GetName();
274 aName += '.';
275 aName += pModule->GetName();
276 aName += '.';
277 aName += pMethod->GetName();
279 // language
280 String aLanguage = String::CreateFromAscii("Basic");
282 // location
283 String aLocation;
284 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
285 if ( aDocument.isDocument() )
287 // document basic
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 );
298 if ( xContext.is() )
299 xScripts = xContext->getScriptContainer();
300 if ( xScripts.is() )
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() )
313 // error
314 bError = TRUE;
315 ErrorBox( NULL, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_ERRORCHOOSEMACRO ) ) ).Execute();
319 else
321 // application basic
322 aLocation = String::CreateFromAscii("application");
325 // script URL
326 if ( !bError )
328 aScriptURL = String::CreateFromAscii("vnd.sun.star.script:");
329 aScriptURL += aName;
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 );
346 break;
349 delete pChooser;
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;
363 // get module
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();
380 return aSeqMethods;
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();
395 if ( pMethods )
397 SbMethod* pMethod = (SbMethod*)pMethods->Find( rMethName, SbxCLASS_METHOD );
398 if ( pMethod )
399 bHasMethod = TRUE;
403 return bHasMethod;
405 } //namespace BasicIDE
406 //----------------------------------------------------------------------------