Update ooo320-m1
[ooovba.git] / basctl / source / basicide / basobj3.cxx
bloba013c90f8c30f988f401261d85ac13dc9d54ab8d
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: basobj3.cxx,v $
10 * $Revision: 1.44 $
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>
41 #include <iderdll.hxx>
42 #include <iderdll2.hxx>
43 #include <basobj.hxx>
44 #include <basidesh.hxx>
45 #include <objdlg.hxx>
46 #include <bastypes.hxx>
47 #include <basdoc.hxx>
48 #include <basidesh.hrc>
50 #include <baside2.hxx>
51 #include <baside3.hxx>
52 #include <basicmod.hxx>
53 #include <localizationmgr.hxx>
54 #include "dlged.hxx"
55 #include <dlgeddef.hxx>
56 #include <comphelper/processfactory.hxx>
57 #ifndef _COM_SUN_STAR_SCRIPT_XLIBRYARYCONTAINER_HPP_
58 #include <com/sun/star/script/XLibraryContainer.hpp>
59 #endif
60 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
61 #include <com/sun/star/container/XNameContainer.hpp>
62 #include <xmlscript/xmldlg_imexp.hxx>
63 #include <rtl/uri.hxx>
64 #include <osl/process.h>
65 #include <osl/file.hxx>
67 using namespace comphelper;
68 using namespace ::com::sun::star;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::container;
73 //----------------------------------------------------------------------------
75 extern "C" {
76 SAL_DLLPUBLIC_EXPORT long basicide_handle_basic_error( void* pPtr )
78 return BasicIDE::HandleBasicError( (StarBASIC*)pPtr );
82 namespace BasicIDE
84 //----------------------------------------------------------------------------
86 SbMethod* CreateMacro( SbModule* pModule, const String& rMacroName )
88 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
89 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
90 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
92 if( pDispatcher )
94 pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
97 if ( pModule->GetMethods()->Find( rMacroName, SbxCLASS_METHOD ) )
98 return 0;
100 String aMacroName( rMacroName );
101 if ( aMacroName.Len() == 0 )
103 if ( !pModule->GetMethods()->Count() )
104 aMacroName = String( RTL_CONSTASCII_USTRINGPARAM( "Main" ) );
105 else
107 BOOL bValid = FALSE;
108 String aStdMacroText( RTL_CONSTASCII_USTRINGPARAM( "Macro" ) );
109 //String aStdMacroText( IDEResId( RID_STR_STDMACRONAME ) );
110 USHORT nMacro = 1;
111 while ( !bValid )
113 aMacroName = aStdMacroText;
114 aMacroName += String::CreateFromInt32( nMacro );
115 // Pruefen, ob vorhanden...
116 bValid = pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD ) ? FALSE : TRUE;
117 nMacro++;
122 ::rtl::OUString aOUSource( pModule->GetSource32() );
124 // Nicht zu viele Leerzeilen erzeugen...
125 sal_Int32 nSourceLen = aOUSource.getLength();
126 if ( nSourceLen > 2 )
128 const sal_Unicode* pStr = aOUSource.getStr();
129 if ( pStr[ nSourceLen - 1 ] != LINE_SEP )
130 aOUSource += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\n\n" ) );
131 else if ( pStr[ nSourceLen - 2 ] != LINE_SEP )
132 aOUSource += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\n" ) );
133 else if ( pStr[ nSourceLen - 3 ] == LINE_SEP )
134 aOUSource = aOUSource.copy( 0, nSourceLen-1 );
137 ::rtl::OUString aSubStr;
138 aSubStr = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Sub " ) );
139 aSubStr += aMacroName;
140 aSubStr += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\n\nEnd Sub" ) );
142 aOUSource += aSubStr;
144 // update module in library
145 ScriptDocument aDocument( ScriptDocument::NoDocument );
146 SbxObject* pParent = pModule->GetParent();
147 StarBASIC* pBasic = PTR_CAST(StarBASIC,pParent);
148 DBG_ASSERT(pBasic, "BasicIDE::CreateMacro: No Basic found!");
149 if ( pBasic )
151 BasicManager* pBasMgr = BasicIDE::FindBasicManager( pBasic );
152 DBG_ASSERT(pBasMgr, "BasicIDE::CreateMacro: No BasicManager found!");
153 if ( pBasMgr )
155 aDocument = ScriptDocument::getDocumentForBasicManager( pBasMgr );
156 OSL_ENSURE( aDocument.isValid(), "BasicIDE::CreateMacro: no document for the given BasicManager!" );
157 if ( aDocument.isValid() )
159 String aLibName = pBasic->GetName();
160 String aModName = pModule->GetName();
161 OSL_VERIFY( aDocument.updateModule( aLibName, aModName, aOUSource ) );
166 SbMethod* pMethod = (SbMethod*)pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD );
168 if( pDispatcher )
170 pDispatcher->Execute( SID_BASICIDE_UPDATEALLMODULESOURCES );
173 if ( aDocument.isAlive() )
174 BasicIDE::MarkDocumentModified( aDocument );
176 return pMethod;
179 //----------------------------------------------------------------------------
181 bool RenameDialog( Window* pErrorParent, const ScriptDocument& rDocument, const String& rLibName, const String& rOldName, const String& rNewName )
182 throw(ElementExistException, NoSuchElementException)
184 if ( !rDocument.hasDialog( rLibName, rOldName ) )
186 OSL_ENSURE( false, "BasicIDE::RenameDialog: old module name is invalid!" );
187 return false;
190 if ( rDocument.hasDialog( rLibName, rNewName ) )
192 ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_SBXNAMEALLREADYUSED2 ) ) );
193 aError.Execute();
194 return false;
197 // #i74440
198 if ( rNewName.Len() == 0 )
200 ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) );
201 aError.Execute();
202 return false;
205 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
206 IDEBaseWindow* pWin = pIDEShell ? pIDEShell->FindWindow( rDocument, rLibName, rOldName, BASICIDE_TYPE_DIALOG, FALSE ) : NULL;
207 Reference< XNameContainer > xExistingDialog;
208 if ( pWin )
209 xExistingDialog = ((DialogWindow*)pWin)->GetEditor()->GetDialog();
211 if ( xExistingDialog.is() )
212 LocalizationMgr::renameStringResourceIDs( rDocument, rLibName, rNewName, xExistingDialog );
214 if ( !rDocument.renameDialog( rLibName, rOldName, rNewName, xExistingDialog ) )
215 return false;
217 if ( pWin )
219 // set new name in window
220 pWin->SetName( rNewName );
222 // update property browser
223 ((DialogWindow*)pWin)->UpdateBrowser();
225 // update tabwriter
226 USHORT nId = (USHORT)(pIDEShell->GetIDEWindowTable()).GetKey( pWin );
227 DBG_ASSERT( nId, "No entry in Tabbar!" );
228 if ( nId )
230 BasicIDETabBar* pTabBar = (BasicIDETabBar*)pIDEShell->GetTabBar();
231 pTabBar->SetPageText( nId, rNewName );
232 pTabBar->Sort();
233 pTabBar->MakeVisible( pTabBar->GetCurPageId() );
236 return true;
239 //----------------------------------------------------------------------------
241 bool RemoveDialog( const ScriptDocument& rDocument, const String& rLibName, const String& rDlgName )
243 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
244 if ( pIDEShell )
246 DialogWindow* pDlgWin = pIDEShell->FindDlgWin( rDocument, rLibName, rDlgName, FALSE );
247 if( pDlgWin )
249 Reference< container::XNameContainer > xDialogModel = pDlgWin->GetDialog();
250 LocalizationMgr::removeResourceForDialog( rDocument, rLibName, rDlgName, xDialogModel );
254 return rDocument.removeDialog( rLibName, rDlgName );
257 //----------------------------------------------------------------------------
259 StarBASIC* FindBasic( const SbxVariable* pVar )
261 const SbxVariable* pSbx = pVar;
262 while ( pSbx && !pSbx->ISA( StarBASIC ) )
263 pSbx = pSbx->GetParent();
265 DBG_ASSERT( !pSbx || pSbx->ISA( StarBASIC ), "Find Basic: Kein Basic!" );
266 return (StarBASIC*)pSbx;
269 //----------------------------------------------------------------------------
271 BasicManager* FindBasicManager( StarBASIC* pLib )
273 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::AllWithApplication ) );
274 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
275 doc != aDocuments.end();
276 ++doc
279 BasicManager* pBasicMgr = doc->getBasicManager();
280 OSL_ENSURE( pBasicMgr, "BasicIDE::FindBasicManager: no basic manager for the document!" );
281 if ( !pBasicMgr )
282 continue;
284 Sequence< ::rtl::OUString > aLibNames( doc->getLibraryNames() );
285 sal_Int32 nLibCount = aLibNames.getLength();
286 const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
288 for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
290 StarBASIC* pL = pBasicMgr->GetLib( pLibNames[ i ] );
291 if ( pL == pLib )
292 return pBasicMgr;
295 return NULL;
298 //----------------------------------------------------------------------------
300 void MarkDocumentModified( const ScriptDocument& rDocument )
302 // Muss ja nicht aus einem Document kommen...
303 if ( rDocument.isApplication() )
305 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
306 if ( pIDEShell )
307 pIDEShell->SetAppBasicModified();
309 else
311 rDocument.setDocumentModified();
314 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
315 if ( pBindings )
317 pBindings->Invalidate( SID_SIGNATURE );
318 pBindings->Invalidate( SID_SAVEDOC );
319 pBindings->Update( SID_SAVEDOC );
322 // Objectcatalog updaten...
323 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
324 ObjectCatalog* pObjCatalog = pIDEShell ? pIDEShell->GetObjectCatalog() : 0;
325 if ( pObjCatalog )
326 pObjCatalog->UpdateEntries();
329 //----------------------------------------------------------------------------
331 void RunMethod( SbMethod* pMethod )
333 SbxValues aRes;
334 aRes.eType = SbxVOID;
335 pMethod->Get( aRes );
338 //----------------------------------------------------------------------------
340 void StopBasic()
342 StarBASIC::Stop();
343 BasicIDEShell* pShell = IDE_DLL()->GetShell();
344 if ( pShell )
346 IDEWindowTable& rWindows = pShell->GetIDEWindowTable();
347 IDEBaseWindow* pWin = rWindows.First();
348 while ( pWin )
350 // BasicStopped von Hand rufen, da das Stop-Notify ggf. sonst nicht
351 // durchkommen kann.
352 pWin->BasicStopped();
353 pWin = rWindows.Next();
356 BasicIDE::BasicStopped();
359 //----------------------------------------------------------------------------
361 void BasicStopped( BOOL* pbAppWindowDisabled,
362 BOOL* pbDispatcherLocked, USHORT* pnWaitCount,
363 SfxUInt16Item** ppSWActionCount, SfxUInt16Item** ppSWLockViewCount )
365 // Nach einem Error oder dem expliziten abbrechen des Basics muessen
366 // ggf. einige Locks entfernt werden...
368 if ( pbAppWindowDisabled )
369 *pbAppWindowDisabled = FALSE;
370 if ( pbDispatcherLocked )
371 *pbDispatcherLocked = FALSE;
372 if ( pnWaitCount )
373 *pnWaitCount = 0;
374 if ( ppSWActionCount )
375 *ppSWActionCount = 0;
376 if ( ppSWLockViewCount )
377 *ppSWLockViewCount = 0;
379 // AppWait ?
380 USHORT nWait = 0;
381 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
382 if( pIDEShell )
384 while ( pIDEShell->GetViewFrame()->GetWindow().IsWait() )
386 pIDEShell->GetViewFrame()->GetWindow().LeaveWait();
387 nWait++;
389 if ( pnWaitCount )
390 *pnWaitCount = nWait;
394 // Interactive = FALSE ?
395 if ( SFX_APP()->IsDispatcherLocked() )
397 SFX_APP()->LockDispatcher( FALSE );
398 if ( pbDispatcherLocked )
399 *pbDispatcherLocked = TRUE;
400 } */
402 Window* pDefParent = Application::GetDefDialogParent();
403 if ( pDefParent && !pDefParent->IsEnabled() )
405 pDefParent->Enable( TRUE );
406 if ( pbAppWindowDisabled )
407 *pbAppWindowDisabled = TRUE;
412 //----------------------------------------------------------------------------
414 void InvalidateDebuggerSlots()
416 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
417 if ( pBindings )
419 pBindings->Invalidate( SID_BASICSTOP );
420 pBindings->Update( SID_BASICSTOP );
421 pBindings->Invalidate( SID_BASICRUN );
422 pBindings->Update( SID_BASICRUN );
423 pBindings->Invalidate( SID_BASICCOMPILE );
424 pBindings->Update( SID_BASICCOMPILE );
425 pBindings->Invalidate( SID_BASICSTEPOVER );
426 pBindings->Update( SID_BASICSTEPOVER );
427 pBindings->Invalidate( SID_BASICSTEPINTO );
428 pBindings->Update( SID_BASICSTEPINTO );
429 pBindings->Invalidate( SID_BASICSTEPOUT );
430 pBindings->Update( SID_BASICSTEPOUT );
431 pBindings->Invalidate( SID_BASICIDE_TOGGLEBRKPNT );
432 pBindings->Update( SID_BASICIDE_TOGGLEBRKPNT );
433 pBindings->Invalidate( SID_BASICIDE_STAT_POS );
434 pBindings->Update( SID_BASICIDE_STAT_POS );
438 //----------------------------------------------------------------------------
440 long HandleBasicError( StarBASIC* pBasic )
442 BasicIDEDLL::Init();
443 BasicIDE::BasicStopped();
445 // no error output during macro choosing
446 if ( IDE_DLL()->GetExtraData()->ChoosingMacro() )
447 return 1;
448 if ( IDE_DLL()->GetExtraData()->ShellInCriticalSection() )
449 return 2;
451 long nRet = 0;
452 BasicIDEShell* pIDEShell = 0;
453 if ( SvtModuleOptions().IsBasicIDE() )
455 BasicManager* pBasMgr = BasicIDE::FindBasicManager( pBasic );
456 if ( pBasMgr )
458 BOOL bProtected = FALSE;
459 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
460 OSL_ENSURE( aDocument.isValid(), "BasicIDE::HandleBasicError: no document for the given BasicManager!" );
461 if ( aDocument.isValid() )
463 ::rtl::OUString aOULibName( pBasic->GetName() );
464 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
465 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
467 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
468 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
470 bProtected = TRUE;
475 if ( !bProtected )
477 pIDEShell = IDE_DLL()->GetShell();
478 if ( !pIDEShell )
480 SfxAllItemSet aArgs( SFX_APP()->GetPool() );
481 SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
482 SFX_APP()->ExecuteSlot( aRequest );
483 pIDEShell = IDE_DLL()->GetShell();
489 if ( pIDEShell )
490 nRet = pIDEShell->CallBasicErrorHdl( pBasic );
491 else
492 ErrorHandler::HandleError( StarBASIC::GetErrorCode() );
494 return nRet;
497 //----------------------------------------------------------------------------
499 SfxBindings* GetBindingsPtr()
501 SfxBindings* pBindings = NULL;
503 SfxViewFrame* pFrame = NULL;
504 BasicIDEDLL* pIDEDLL = IDE_DLL();
505 if ( pIDEDLL && pIDEDLL->GetShell() )
507 pFrame = pIDEDLL->GetShell()->GetViewFrame();
509 else
511 SfxViewFrame* pView = SfxViewFrame::GetFirst();
512 while ( pView )
514 SfxObjectShell* pObjShell = pView->GetObjectShell();
515 if ( pObjShell && pObjShell->ISA( BasicDocShell ) )
517 pFrame = pView;
518 break;
520 pView = SfxViewFrame::GetNext( *pView );
523 if ( pFrame != NULL )
524 pBindings = &pFrame->GetBindings();
526 return pBindings;
529 } //namespace BasicIDE
531 //----------------------------------------------------------------------------