bump product version to 5.0.4.1
[LibreOffice.git] / basctl / source / basicide / bastype3.cxx
blob24122eaefaf44a89c1088f8c386719af488a9d0b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <basic/basmgr.hxx>
21 #include <basic/sbmod.hxx>
22 #include <bastype2.hxx>
23 #include <baside2.hrc>
24 #include <bastypes.hxx>
25 #include <com/sun/star/script/XLibraryContainer.hpp>
26 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
27 #include <sfx2/docfac.hxx>
28 #include <svtools/treelistentry.hxx>
30 namespace basctl
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star;
37 typedef std::deque< SvTreeListEntry* > EntryArray;
40 void TreeListBox::RequestingChildren( SvTreeListEntry* pEntry )
42 EntryDescriptor aDesc = GetEntryDescriptor(pEntry);
43 ScriptDocument aDocument = aDesc.GetDocument();
44 OSL_ENSURE( aDocument.isAlive(), "basctl::TreeListBox::RequestingChildren: invalid document!" );
45 if ( !aDocument.isAlive() )
46 return;
48 LibraryLocation eLocation = aDesc.GetLocation();
49 EntryType eType = aDesc.GetType();
51 if ( eType == OBJ_TYPE_DOCUMENT )
53 ImpCreateLibEntries( pEntry, aDocument, eLocation );
55 else if ( eType == OBJ_TYPE_LIBRARY )
57 OUString aOULibName( aDesc.GetLibName() );
59 // check password
60 bool bOK = true;
61 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
62 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
64 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
65 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
67 OUString aPassword;
68 bOK = QueryPassword( xModLibContainer, aOULibName, aPassword );
72 if ( bOK )
74 // load module library
75 bool bModLibLoaded = false;
76 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
78 if ( !xModLibContainer->isLibraryLoaded( aOULibName ) )
80 EnterWait();
81 xModLibContainer->loadLibrary( aOULibName );
82 LeaveWait();
84 bModLibLoaded = xModLibContainer->isLibraryLoaded( aOULibName );
87 // load dialog library
88 bool bDlgLibLoaded = false;
89 Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
90 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
92 if ( !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
94 EnterWait();
95 xDlgLibContainer->loadLibrary( aOULibName );
96 LeaveWait();
98 bDlgLibLoaded = xDlgLibContainer->isLibraryLoaded( aOULibName );
101 if ( bModLibLoaded || bDlgLibLoaded )
103 // create the sub entries
104 ImpCreateLibSubEntries( pEntry, aDocument, aOULibName );
106 // exchange image
107 bool bDlgMode = ( nMode & BROWSEMODE_DIALOGS ) && !( nMode & BROWSEMODE_MODULES );
108 Image aImage( IDEResId( bDlgMode ? RID_IMG_DLGLIB : RID_IMG_MODLIB ) );
109 SetEntryBitmaps( pEntry, aImage );
111 else
113 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Error loading library!" );
117 else if ( eType == OBJ_TYPE_DOCUMENT_OBJECTS
118 || eType == OBJ_TYPE_USERFORMS
119 || eType == OBJ_TYPE_NORMAL_MODULES
120 || eType == OBJ_TYPE_CLASS_MODULES )
122 OUString aLibName( aDesc.GetLibName() );
123 ImpCreateLibSubSubEntriesInVBAMode( pEntry, aDocument, aLibName );
125 else {
126 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Unknown Type!" );
130 void TreeListBox::ExpandedHdl()
132 SvTreeListEntry* pEntry = GetHdlEntry();
133 DBG_ASSERT( pEntry, "Was wurde zugeklappt?" );
135 if ( !IsExpanded( pEntry ) && pEntry->HasChildrenOnDemand() )
137 SvTreeListEntry* pChild = FirstChild( pEntry );
138 while ( pChild )
140 GetModel()->Remove( pChild ); // does also call the DTOR
141 pChild = FirstChild( pEntry );
146 void TreeListBox::ScanAllEntries()
148 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
149 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
151 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
152 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
153 doc != aDocuments.end();
154 ++doc
157 if ( doc->isAlive() )
158 ScanEntry( *doc, LIBRARY_LOCATION_DOCUMENT );
162 SbxVariable* TreeListBox::FindVariable( SvTreeListEntry* pEntry )
164 if ( !pEntry )
165 return 0;
167 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
168 EntryArray aEntries;
170 while ( pEntry )
172 sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
173 switch ( nDepth )
175 case 4:
176 case 3:
177 case 2:
178 case 1:
180 aEntries.push_front( pEntry );
182 break;
183 case 0:
185 aDocument = static_cast<DocumentEntry*>(pEntry->GetUserData())->GetDocument();
187 break;
189 pEntry = GetParent( pEntry );
192 SbxVariable* pVar = 0;
193 if ( !aEntries.empty() )
195 bool bDocumentObjects = false;
196 for ( size_t n = 0; n < aEntries.size(); n++ )
198 SvTreeListEntry* pLE = aEntries[n];
199 assert(pLE && "Can not find entry in array");
200 Entry* pBE = static_cast<Entry*>(pLE->GetUserData());
201 assert(pBE && "The data in the entry not found!");
202 OUString aName( GetEntryText( pLE ) );
204 switch ( pBE->GetType() )
206 case OBJ_TYPE_LIBRARY:
207 if (BasicManager* pBasMgr = aDocument.getBasicManager())
208 pVar = pBasMgr->GetLib( aName );
209 break;
210 case OBJ_TYPE_MODULE:
211 DBG_ASSERT(dynamic_cast<StarBASIC*>(pVar), "FindVariable: invalid Basic");
212 if(!pVar)
214 break;
216 // extract the module name from the string like "Sheet1 (Example1)"
217 if( bDocumentObjects )
219 sal_Int32 nIndex = 0;
220 aName = aName.getToken( 0, ' ', nIndex );
222 pVar = static_cast<StarBASIC*>(pVar)->FindModule( aName );
223 break;
224 case OBJ_TYPE_METHOD:
225 DBG_ASSERT(dynamic_cast<SbxObject*>(pVar), "FindVariable: invalid modul/object");
226 if(!pVar)
228 break;
230 pVar = static_cast<SbxObject*>(pVar)->GetMethods()->Find(aName, SbxCLASS_METHOD);
231 break;
232 case OBJ_TYPE_DIALOG:
233 // sbx dialogs removed
234 break;
235 case OBJ_TYPE_DOCUMENT_OBJECTS:
236 bDocumentObjects = true;
237 case OBJ_TYPE_USERFORMS:
238 case OBJ_TYPE_NORMAL_MODULES:
239 case OBJ_TYPE_CLASS_MODULES:
240 // skip, to find the child entry.
241 continue;
242 default:
243 OSL_FAIL( "FindVariable: Unbekannter Typ!" );
244 pVar = 0;
245 break;
247 if ( !pVar )
248 break;
252 return pVar;
255 EntryDescriptor TreeListBox::GetEntryDescriptor( SvTreeListEntry* pEntry )
257 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
258 LibraryLocation eLocation = LIBRARY_LOCATION_UNKNOWN;
259 OUString aLibName;
260 OUString aLibSubName;
261 OUString aName;
262 OUString aMethodName;
263 EntryType eType = OBJ_TYPE_UNKNOWN;
265 if ( !pEntry )
266 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
268 EntryArray aEntries;
270 while ( pEntry )
272 sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
273 switch ( nDepth )
275 case 4:
276 case 3:
277 case 2:
278 case 1:
280 aEntries.push_front( pEntry );
282 break;
283 case 0:
285 if (DocumentEntry* pDocumentEntry = static_cast<DocumentEntry*>(pEntry->GetUserData()))
287 aDocument = pDocumentEntry->GetDocument();
288 eLocation = pDocumentEntry->GetLocation();
289 eType = OBJ_TYPE_DOCUMENT;
292 break;
294 pEntry = GetParent( pEntry );
297 if ( !aEntries.empty() )
299 for ( size_t n = 0; n < aEntries.size(); n++ )
301 SvTreeListEntry* pLE = aEntries[n];
302 DBG_ASSERT( pLE, "Entrie im Array nicht gefunden" );
303 Entry* pBE = static_cast<Entry*>(pLE->GetUserData());
304 DBG_ASSERT( pBE, "Keine Daten im Eintrag gefunden!" );
306 switch ( pBE->GetType() )
308 case OBJ_TYPE_LIBRARY:
310 aLibName = GetEntryText( pLE );
311 eType = pBE->GetType();
313 break;
314 case OBJ_TYPE_MODULE:
316 aName = GetEntryText( pLE );
317 eType = pBE->GetType();
319 break;
320 case OBJ_TYPE_METHOD:
322 aMethodName = GetEntryText( pLE );
323 eType = pBE->GetType();
325 break;
326 case OBJ_TYPE_DIALOG:
328 aName = GetEntryText( pLE );
329 eType = pBE->GetType();
331 break;
332 case OBJ_TYPE_DOCUMENT_OBJECTS:
333 case OBJ_TYPE_USERFORMS:
334 case OBJ_TYPE_NORMAL_MODULES:
335 case OBJ_TYPE_CLASS_MODULES:
337 aLibSubName = GetEntryText( pLE );
338 eType = pBE->GetType();
340 break;
341 default:
343 OSL_FAIL( "GetEntryDescriptor: Unbekannter Typ!" );
344 eType = OBJ_TYPE_UNKNOWN;
346 break;
349 if ( eType == OBJ_TYPE_UNKNOWN )
350 break;
354 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
357 ItemType TreeListBox::ConvertType (EntryType eType)
359 switch (eType)
361 case OBJ_TYPE_DOCUMENT: return TYPE_SHELL;
362 case OBJ_TYPE_LIBRARY: return TYPE_LIBRARY;
363 case OBJ_TYPE_MODULE: return TYPE_MODULE;
364 case OBJ_TYPE_DIALOG: return TYPE_DIALOG;
365 case OBJ_TYPE_METHOD: return TYPE_METHOD;
366 default:
367 return static_cast<ItemType>(OBJ_TYPE_UNKNOWN);
371 bool TreeListBox::IsValidEntry( SvTreeListEntry* pEntry )
373 bool bIsValid = false;
375 EntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
376 ScriptDocument aDocument( aDesc.GetDocument() );
377 LibraryLocation eLocation( aDesc.GetLocation() );
378 OUString aLibName( aDesc.GetLibName() );
379 OUString aName( aDesc.GetName() );
380 OUString aMethodName( aDesc.GetMethodName() );
381 EntryType eType( aDesc.GetType() );
383 switch ( eType )
385 case OBJ_TYPE_DOCUMENT:
387 bIsValid = aDocument.isAlive()
388 && (aDocument.isApplication()
389 || GetRootEntryName(aDocument, eLocation).equals(GetEntryText(pEntry)));
391 break;
392 case OBJ_TYPE_LIBRARY:
394 bIsValid = aDocument.hasLibrary( E_SCRIPTS, aLibName ) || aDocument.hasLibrary( E_DIALOGS, aLibName );
396 break;
397 case OBJ_TYPE_MODULE:
399 bIsValid = aDocument.hasModule( aLibName, aName );
401 break;
402 case OBJ_TYPE_DIALOG:
404 bIsValid = aDocument.hasDialog( aLibName, aName );
406 break;
407 case OBJ_TYPE_METHOD:
409 bIsValid = HasMethod( aDocument, aLibName, aName, aMethodName );
411 break;
412 case OBJ_TYPE_DOCUMENT_OBJECTS:
413 case OBJ_TYPE_USERFORMS:
414 case OBJ_TYPE_NORMAL_MODULES:
415 case OBJ_TYPE_CLASS_MODULES:
417 bIsValid = true;
419 break;
420 default: ;
423 return bIsValid;
426 SbModule* TreeListBox::FindModule( SvTreeListEntry* pEntry )
428 return dynamic_cast<SbModule*>(FindVariable(pEntry));
431 SvTreeListEntry* TreeListBox::FindRootEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
433 OSL_ENSURE( rDocument.isValid(), "basctl::TreeListBox::FindRootEntry: invalid document!" );
434 sal_uLong nRootPos = 0;
435 SvTreeListEntry* pRootEntry = GetEntry( nRootPos );
436 while ( pRootEntry )
438 DBG_ASSERT( static_cast<Entry*>(pRootEntry->GetUserData())->GetType() == OBJ_TYPE_DOCUMENT, "Kein Shelleintrag?" );
439 DocumentEntry* pBDEntry = static_cast<DocumentEntry*>(pRootEntry->GetUserData());
440 if (pBDEntry && pBDEntry->GetDocument() == rDocument && pBDEntry->GetLocation() == eLocation)
441 return pRootEntry;
442 pRootEntry = GetEntry( ++nRootPos );
444 return 0;
447 OUString CreateMgrAndLibStr( const OUString& rMgrName, const OUString& rLibName )
449 return "[" + rMgrName + "]." + rLibName;
453 } // namespace basctl
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */