Update ooo320-m1
[ooovba.git] / basctl / source / basicide / bastype3.cxx
bloba372af86e572bfc327ba6b9cff7da8488cd0f0a0
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: bastype3.cxx,v $
10 * $Revision: 1.26 $
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"
35 #include <ide_pch.hxx>
37 #define _SI_NOSBXCONTROLS
39 #include <basic/sbx.hxx>
40 #include <bastype2.hxx>
41 #include <basobj.hxx>
42 #include <baside2.hrc>
43 #include <iderid.hxx>
44 #include <bastypes.hxx>
45 #include <basdoc.hxx>
46 #include <com/sun/star/script/XLibraryContainer.hpp>
47 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star;
53 SV_DECL_VARARR( EntryArray, SvLBoxEntry*, 4, 4 )
55 SV_IMPL_VARARR( EntryArray, SvLBoxEntry*);
58 void __EXPORT BasicTreeListBox::RequestingChilds( SvLBoxEntry* pEntry )
60 BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
61 ScriptDocument aDocument( aDesc.GetDocument() );
62 OSL_ENSURE( aDocument.isAlive(), "BasicTreeListBox::RequestingChilds: invalid document!" );
63 if ( !aDocument.isAlive() )
64 return;
66 LibraryLocation eLocation( aDesc.GetLocation() );
67 BasicEntryType eType( aDesc.GetType() );
69 if ( eType == OBJ_TYPE_DOCUMENT )
71 ImpCreateLibEntries( pEntry, aDocument, eLocation );
73 else if ( eType == OBJ_TYPE_LIBRARY )
75 String aLibName( aDesc.GetLibName() );
76 ::rtl::OUString aOULibName( aLibName );
78 // check password
79 BOOL bOK = TRUE;
80 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
81 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
83 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
84 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
86 String aPassword;
87 bOK = QueryPassword( xModLibContainer, aLibName, aPassword );
91 if ( bOK )
93 // load module library
94 BOOL bModLibLoaded = FALSE;
95 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
97 if ( !xModLibContainer->isLibraryLoaded( aOULibName ) )
99 EnterWait();
100 xModLibContainer->loadLibrary( aOULibName );
101 LeaveWait();
103 bModLibLoaded = xModLibContainer->isLibraryLoaded( aOULibName );
106 // load dialog library
107 BOOL bDlgLibLoaded = FALSE;
108 Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
109 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
111 if ( !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
113 EnterWait();
114 xDlgLibContainer->loadLibrary( aOULibName );
115 LeaveWait();
117 bDlgLibLoaded = xDlgLibContainer->isLibraryLoaded( aOULibName );
120 if ( bModLibLoaded || bDlgLibLoaded )
122 // create the sub entries
123 ImpCreateLibSubEntries( pEntry, aDocument, aLibName );
125 // exchange image
126 bool bDlgMode = ( nMode & BROWSEMODE_DIALOGS ) && !( nMode & BROWSEMODE_MODULES );
127 Image aImage( IDEResId( bDlgMode ? RID_IMG_DLGLIB : RID_IMG_MODLIB ) );
128 Image aImageHC( IDEResId( bDlgMode ? RID_IMG_DLGLIB_HC : RID_IMG_MODLIB_HC ) );
129 SetEntryBitmaps( pEntry, aImage, aImageHC );
131 else
133 DBG_ERROR( "BasicTreeListBox::RequestingChilds: Error loading library!" );
137 else if ( eType == OBJ_TYPE_DOCUMENT_OBJECTS
138 || eType == OBJ_TYPE_USERFORMS
139 || eType == OBJ_TYPE_NORMAL_MODULES
140 || eType == OBJ_TYPE_CLASS_MODULES )
142 String aLibName( aDesc.GetLibName() );
143 ImpCreateLibSubSubEntriesInVBAMode( pEntry, aDocument, aLibName );
145 else {
146 DBG_ERROR( "BasicTreeListBox::RequestingChilds: Unknown Type!" );
150 void __EXPORT BasicTreeListBox::ExpandedHdl()
152 SvLBoxEntry* pEntry = GetHdlEntry();
153 DBG_ASSERT( pEntry, "Was wurde zugeklappt?" );
154 // Die OnDemand erzeugten Childs loeschen,
155 // SubChilds werden automatisch geloescht.
156 if ( !IsExpanded( pEntry ) && pEntry->HasChildsOnDemand() )
158 SvLBoxEntry* pChild = FirstChild( pEntry );
159 while ( pChild )
161 GetModel()->Remove( pChild ); // Ruft auch den DTOR
162 pChild = FirstChild( pEntry );
167 void BasicTreeListBox::ScanAllEntries()
169 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
170 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
172 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
173 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
174 doc != aDocuments.end();
175 ++doc
178 if ( doc->isAlive() )
179 ScanEntry( *doc, LIBRARY_LOCATION_DOCUMENT );
183 SbxVariable* BasicTreeListBox::FindVariable( SvLBoxEntry* pEntry )
185 if ( !pEntry )
186 return 0;
188 String aLib, aModOrObj, aSubOrPropOrSObj, aPropOrSubInSObj;
189 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
190 EntryArray aEntries;
192 while ( pEntry )
194 USHORT nDepth = GetModel()->GetDepth( pEntry );
195 switch ( nDepth )
197 case 4:
198 case 3:
199 case 2:
200 case 1:
202 aEntries.C40_INSERT( SvLBoxEntry, pEntry, 0 );
204 break;
205 case 0:
207 aDocument = ((BasicDocumentEntry*)pEntry->GetUserData())->GetDocument();
209 break;
211 pEntry = GetParent( pEntry );
214 SbxVariable* pVar = 0;
215 bool bDocumentObjects = false;
216 if ( aEntries.Count() )
218 for ( USHORT n = 0; n < aEntries.Count(); n++ )
220 SvLBoxEntry* pLE = aEntries[n];
221 DBG_ASSERT( pLE, "Entrie im Array nicht gefunden" );
222 BasicEntry* pBE = (BasicEntry*)pLE->GetUserData();
223 DBG_ASSERT( pBE, "Keine Daten im Eintrag gefunden!" );
224 String aName( GetEntryText( pLE ) );
226 switch ( pBE->GetType() )
228 case OBJ_TYPE_LIBRARY:
230 BasicManager* pBasMgr = aDocument.getBasicManager();
231 if ( pBasMgr )
232 pVar = pBasMgr->GetLib( aName );
234 break;
235 case OBJ_TYPE_MODULE:
237 DBG_ASSERT( pVar && pVar->IsA( TYPE(StarBASIC) ), "FindVariable: Ungueltiges Basic" );
238 // extract the module name from the string like "Sheet1 (Example1)"
239 if( bDocumentObjects )
241 sal_uInt16 nIndex = 0;
242 aName = aName.GetToken( 0, ' ', nIndex );
244 pVar = ((StarBASIC*)pVar)->FindModule( aName );
246 break;
247 case OBJ_TYPE_METHOD:
249 DBG_ASSERT( pVar && ( (pVar->IsA( TYPE(SbModule) )) || (pVar->IsA( TYPE(SbxObject) )) ), "FindVariable: Ungueltiges Modul/Objekt" );
250 pVar = ((SbxObject*)pVar)->GetMethods()->Find( aName, SbxCLASS_METHOD );
252 break;
253 case OBJ_TYPE_DIALOG:
255 // sbx dialogs removed
257 break;
258 case OBJ_TYPE_DOCUMENT_OBJECTS:
259 bDocumentObjects = true;
260 case OBJ_TYPE_USERFORMS:
261 case OBJ_TYPE_NORMAL_MODULES:
262 case OBJ_TYPE_CLASS_MODULES:
264 // skip, to find the child entry.
265 continue;
267 default:
269 DBG_ERROR( "FindVariable: Unbekannter Typ!" );
270 pVar = 0;
272 break;
274 if ( !pVar )
275 break;
279 return pVar;
282 BasicEntryDescriptor BasicTreeListBox::GetEntryDescriptor( SvLBoxEntry* pEntry )
284 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
285 LibraryLocation eLocation = LIBRARY_LOCATION_UNKNOWN;
286 String aLibName;
287 String aLibSubName;
288 String aName;
289 String aMethodName;
290 BasicEntryType eType = OBJ_TYPE_UNKNOWN;
292 if ( !pEntry )
293 return BasicEntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
295 EntryArray aEntries;
297 while ( pEntry )
299 USHORT nDepth = GetModel()->GetDepth( pEntry );
300 switch ( nDepth )
302 case 4:
303 case 3:
304 case 2:
305 case 1:
307 aEntries.C40_INSERT( SvLBoxEntry, pEntry, 0 );
309 break;
310 case 0:
312 BasicDocumentEntry* pBasicDocumentEntry = (BasicDocumentEntry*)pEntry->GetUserData();
313 if ( pBasicDocumentEntry )
315 aDocument = pBasicDocumentEntry->GetDocument();
316 eLocation = pBasicDocumentEntry->GetLocation();
317 eType = OBJ_TYPE_DOCUMENT;
320 break;
322 pEntry = GetParent( pEntry );
325 if ( aEntries.Count() )
327 for ( USHORT n = 0; n < aEntries.Count(); n++ )
329 SvLBoxEntry* pLE = aEntries[n];
330 DBG_ASSERT( pLE, "Entrie im Array nicht gefunden" );
331 BasicEntry* pBE = (BasicEntry*)pLE->GetUserData();
332 DBG_ASSERT( pBE, "Keine Daten im Eintrag gefunden!" );
334 switch ( pBE->GetType() )
336 case OBJ_TYPE_LIBRARY:
338 aLibName = GetEntryText( pLE );
339 eType = pBE->GetType();
341 break;
342 case OBJ_TYPE_MODULE:
344 aName = GetEntryText( pLE );
345 eType = pBE->GetType();
347 break;
348 case OBJ_TYPE_METHOD:
350 aMethodName = GetEntryText( pLE );
351 eType = pBE->GetType();
353 break;
354 case OBJ_TYPE_DIALOG:
356 aName = GetEntryText( pLE );
357 eType = pBE->GetType();
359 break;
360 case OBJ_TYPE_DOCUMENT_OBJECTS:
361 case OBJ_TYPE_USERFORMS:
362 case OBJ_TYPE_NORMAL_MODULES:
363 case OBJ_TYPE_CLASS_MODULES:
365 aLibSubName = GetEntryText( pLE );
366 eType = pBE->GetType();
368 break;
369 default:
371 DBG_ERROR( "GetEntryDescriptor: Unbekannter Typ!" );
372 eType = OBJ_TYPE_UNKNOWN;
374 break;
377 if ( eType == OBJ_TYPE_UNKNOWN )
378 break;
382 return BasicEntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
385 USHORT BasicTreeListBox::ConvertType( BasicEntryType eType )
387 USHORT nType = OBJ_TYPE_UNKNOWN;
389 switch ( eType )
391 case OBJ_TYPE_DOCUMENT:
393 nType = BASICIDE_TYPE_SHELL;
395 break;
396 case OBJ_TYPE_LIBRARY:
398 nType = BASICIDE_TYPE_LIBRARY;
400 break;
401 case OBJ_TYPE_MODULE:
403 nType = BASICIDE_TYPE_MODULE;
405 break;
406 case OBJ_TYPE_DIALOG:
408 nType = BASICIDE_TYPE_DIALOG;
410 break;
411 case OBJ_TYPE_METHOD:
413 nType = BASICIDE_TYPE_METHOD;
415 break;
416 default: ;
419 return nType;
422 bool BasicTreeListBox::IsValidEntry( SvLBoxEntry* pEntry )
424 bool bIsValid = false;
426 BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
427 ScriptDocument aDocument( aDesc.GetDocument() );
428 LibraryLocation eLocation( aDesc.GetLocation() );
429 String aLibName( aDesc.GetLibName() );
430 String aName( aDesc.GetName() );
431 String aMethodName( aDesc.GetMethodName() );
432 BasicEntryType eType( aDesc.GetType() );
434 switch ( eType )
436 case OBJ_TYPE_DOCUMENT:
438 bIsValid = aDocument.isAlive()
439 && ( aDocument.isApplication()
440 || GetRootEntryName( aDocument, eLocation ) == GetEntryText( pEntry )
443 break;
444 case OBJ_TYPE_LIBRARY:
446 bIsValid = aDocument.hasLibrary( E_SCRIPTS, aLibName ) || aDocument.hasLibrary( E_DIALOGS, aLibName );
448 break;
449 case OBJ_TYPE_MODULE:
451 bIsValid = aDocument.hasModule( aLibName, aName );
453 break;
454 case OBJ_TYPE_DIALOG:
456 bIsValid = aDocument.hasDialog( aLibName, aName );
458 break;
459 case OBJ_TYPE_METHOD:
461 bIsValid = BasicIDE::HasMethod( aDocument, aLibName, aName, aMethodName );
463 break;
464 case OBJ_TYPE_DOCUMENT_OBJECTS:
465 case OBJ_TYPE_USERFORMS:
466 case OBJ_TYPE_NORMAL_MODULES:
467 case OBJ_TYPE_CLASS_MODULES:
469 bIsValid = true;
471 break;
472 default: ;
475 return bIsValid;
478 SbModule* BasicTreeListBox::FindModule( SvLBoxEntry* pEntry )
480 SbxVariable* pVar = FindVariable( pEntry );
481 if ( pVar && pVar->IsA( TYPE(SbModule ) ) )
482 return (SbModule*)pVar;
483 return 0;
486 SvLBoxEntry* BasicTreeListBox::FindRootEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
488 OSL_ENSURE( rDocument.isValid(), "BasicTreeListBox::FindRootEntry: invalid document!" );
489 ULONG nRootPos = 0;
490 SvLBoxEntry* pRootEntry = GetEntry( nRootPos );
491 while ( pRootEntry )
493 DBG_ASSERT( (((BasicEntry*)pRootEntry->GetUserData())->GetType() == OBJ_TYPE_DOCUMENT ), "Kein Shelleintrag?" );
494 BasicDocumentEntry* pBasicDocumentEntry = (BasicDocumentEntry*)pRootEntry->GetUserData();
495 if ( pBasicDocumentEntry && ( pBasicDocumentEntry->GetDocument() == rDocument ) && pBasicDocumentEntry->GetLocation() == eLocation )
496 return pRootEntry;
497 pRootEntry = GetEntry( ++nRootPos );
499 return 0;
502 String CreateMgrAndLibStr( const String& rMgrName, const String& rLibName )
504 String aName( '[' );
505 aName += rMgrName;
506 aName += String( RTL_CONSTASCII_USTRINGPARAM( "]." ) );
507 aName += rLibName;
508 return aName;