bump product version to 4.1.6.2
[LibreOffice.git] / basctl / source / basicide / bastype3.cxx
blob54c5507c830c244924742d8b720b1d7b6a917a63
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 <basic/sbx.hxx>
23 #include <bastype2.hxx>
24 #include <basobj.hxx>
25 #include <baside2.hrc>
26 #include <iderid.hxx>
27 #include <bastypes.hxx>
28 #include <basdoc.hxx>
29 #include <com/sun/star/script/XLibraryContainer.hpp>
30 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
31 #include <deque>
32 #include <sfx2/docfac.hxx>
33 #include "svtools/treelistentry.hxx"
35 namespace basctl
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star;
42 typedef std::deque< SvTreeListEntry* > EntryArray;
45 void TreeListBox::RequestingChildren( SvTreeListEntry* pEntry )
47 EntryDescriptor aDesc = GetEntryDescriptor(pEntry);
48 ScriptDocument aDocument = aDesc.GetDocument();
49 OSL_ENSURE( aDocument.isAlive(), "basctl::TreeListBox::RequestingChildren: invalid document!" );
50 if ( !aDocument.isAlive() )
51 return;
53 LibraryLocation eLocation = aDesc.GetLocation();
54 EntryType eType = aDesc.GetType();
56 if ( eType == OBJ_TYPE_DOCUMENT )
58 ImpCreateLibEntries( pEntry, aDocument, eLocation );
60 else if ( eType == OBJ_TYPE_LIBRARY )
62 OUString aOULibName( aDesc.GetLibName() );
64 // check password
65 bool bOK = true;
66 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
67 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
69 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
70 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
72 OUString aPassword;
73 bOK = QueryPassword( xModLibContainer, aOULibName, aPassword );
77 if ( bOK )
79 // load module library
80 bool bModLibLoaded = false;
81 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
83 if ( !xModLibContainer->isLibraryLoaded( aOULibName ) )
85 EnterWait();
86 xModLibContainer->loadLibrary( aOULibName );
87 LeaveWait();
89 bModLibLoaded = xModLibContainer->isLibraryLoaded( aOULibName );
92 // load dialog library
93 bool bDlgLibLoaded = false;
94 Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
95 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
97 if ( !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
99 EnterWait();
100 xDlgLibContainer->loadLibrary( aOULibName );
101 LeaveWait();
103 bDlgLibLoaded = xDlgLibContainer->isLibraryLoaded( aOULibName );
106 if ( bModLibLoaded || bDlgLibLoaded )
108 // create the sub entries
109 ImpCreateLibSubEntries( pEntry, aDocument, aOULibName );
111 // exchange image
112 bool bDlgMode = ( nMode & BROWSEMODE_DIALOGS ) && !( nMode & BROWSEMODE_MODULES );
113 Image aImage( IDEResId( bDlgMode ? RID_IMG_DLGLIB : RID_IMG_MODLIB ) );
114 SetEntryBitmaps( pEntry, aImage );
116 else
118 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Error loading library!" );
122 else if ( eType == OBJ_TYPE_DOCUMENT_OBJECTS
123 || eType == OBJ_TYPE_USERFORMS
124 || eType == OBJ_TYPE_NORMAL_MODULES
125 || eType == OBJ_TYPE_CLASS_MODULES )
127 OUString aLibName( aDesc.GetLibName() );
128 ImpCreateLibSubSubEntriesInVBAMode( pEntry, aDocument, aLibName );
130 else {
131 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Unknown Type!" );
135 void TreeListBox::ExpandedHdl()
137 SvTreeListEntry* pEntry = GetHdlEntry();
138 DBG_ASSERT( pEntry, "Was wurde zugeklappt?" );
140 if ( !IsExpanded( pEntry ) && pEntry->HasChildrenOnDemand() )
142 SvTreeListEntry* pChild = FirstChild( pEntry );
143 while ( pChild )
145 GetModel()->Remove( pChild ); // does also call the DTOR
146 pChild = FirstChild( pEntry );
151 void TreeListBox::ScanAllEntries()
153 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
154 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
156 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
157 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
158 doc != aDocuments.end();
159 ++doc
162 if ( doc->isAlive() )
163 ScanEntry( *doc, LIBRARY_LOCATION_DOCUMENT );
167 SbxVariable* TreeListBox::FindVariable( SvTreeListEntry* pEntry )
169 if ( !pEntry )
170 return 0;
172 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
173 EntryArray aEntries;
175 while ( pEntry )
177 sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
178 switch ( nDepth )
180 case 4:
181 case 3:
182 case 2:
183 case 1:
185 aEntries.push_front( pEntry );
187 break;
188 case 0:
190 aDocument = static_cast<DocumentEntry*>(pEntry->GetUserData())->GetDocument();
192 break;
194 pEntry = GetParent( pEntry );
197 SbxVariable* pVar = 0;
198 bool bDocumentObjects = false;
199 if ( !aEntries.empty() )
201 for ( size_t n = 0; n < aEntries.size(); n++ )
203 SvTreeListEntry* pLE = aEntries[n];
204 DBG_ASSERT( pLE, "Can not find entry in array" );
205 Entry* pBE = static_cast<Entry*>(pLE->GetUserData());
206 DBG_ASSERT( pBE, "The data in the entry not found!" );
207 OUString aName( GetEntryText( pLE ) );
209 switch ( pBE->GetType() )
211 case OBJ_TYPE_LIBRARY:
212 if (BasicManager* pBasMgr = aDocument.getBasicManager())
213 pVar = pBasMgr->GetLib( aName );
214 break;
215 case OBJ_TYPE_MODULE:
216 DBG_ASSERT(dynamic_cast<StarBASIC*>(pVar), "FindVariable: invalid Basic");
217 if(!pVar)
219 break;
221 // extract the module name from the string like "Sheet1 (Example1)"
222 if( bDocumentObjects )
224 sal_Int32 nIndex = 0;
225 aName = aName.getToken( 0, ' ', nIndex );
227 pVar = static_cast<StarBASIC*>(pVar)->FindModule( aName );
228 break;
229 case OBJ_TYPE_METHOD:
230 DBG_ASSERT(dynamic_cast<SbxObject*>(pVar), "FindVariable: invalid modul/object");
231 if(!pVar)
233 break;
235 pVar = static_cast<SbxObject*>(pVar)->GetMethods()->Find(aName, SbxCLASS_METHOD);
236 break;
237 case OBJ_TYPE_DIALOG:
238 // sbx dialogs removed
239 break;
240 case OBJ_TYPE_DOCUMENT_OBJECTS:
241 bDocumentObjects = true;
242 case OBJ_TYPE_USERFORMS:
243 case OBJ_TYPE_NORMAL_MODULES:
244 case OBJ_TYPE_CLASS_MODULES:
245 // skip, to find the child entry.
246 continue;
247 default:
248 OSL_FAIL( "FindVariable: Unbekannter Typ!" );
249 pVar = 0;
250 break;
252 if ( !pVar )
253 break;
257 return pVar;
260 EntryDescriptor TreeListBox::GetEntryDescriptor( SvTreeListEntry* pEntry )
262 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
263 LibraryLocation eLocation = LIBRARY_LOCATION_UNKNOWN;
264 OUString aLibName;
265 OUString aLibSubName;
266 OUString aName;
267 OUString aMethodName;
268 EntryType eType = OBJ_TYPE_UNKNOWN;
270 if ( !pEntry )
271 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
273 EntryArray aEntries;
275 while ( pEntry )
277 sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
278 switch ( nDepth )
280 case 4:
281 case 3:
282 case 2:
283 case 1:
285 aEntries.push_front( pEntry );
287 break;
288 case 0:
290 if (DocumentEntry* pDocumentEntry = static_cast<DocumentEntry*>(pEntry->GetUserData()))
292 aDocument = pDocumentEntry->GetDocument();
293 eLocation = pDocumentEntry->GetLocation();
294 eType = OBJ_TYPE_DOCUMENT;
297 break;
299 pEntry = GetParent( pEntry );
302 if ( !aEntries.empty() )
304 for ( size_t n = 0; n < aEntries.size(); n++ )
306 SvTreeListEntry* pLE = aEntries[n];
307 DBG_ASSERT( pLE, "Entrie im Array nicht gefunden" );
308 Entry* pBE = static_cast<Entry*>(pLE->GetUserData());
309 DBG_ASSERT( pBE, "Keine Daten im Eintrag gefunden!" );
311 switch ( pBE->GetType() )
313 case OBJ_TYPE_LIBRARY:
315 aLibName = GetEntryText( pLE );
316 eType = pBE->GetType();
318 break;
319 case OBJ_TYPE_MODULE:
321 aName = GetEntryText( pLE );
322 eType = pBE->GetType();
324 break;
325 case OBJ_TYPE_METHOD:
327 aMethodName = GetEntryText( pLE );
328 eType = pBE->GetType();
330 break;
331 case OBJ_TYPE_DIALOG:
333 aName = GetEntryText( pLE );
334 eType = pBE->GetType();
336 break;
337 case OBJ_TYPE_DOCUMENT_OBJECTS:
338 case OBJ_TYPE_USERFORMS:
339 case OBJ_TYPE_NORMAL_MODULES:
340 case OBJ_TYPE_CLASS_MODULES:
342 aLibSubName = GetEntryText( pLE );
343 eType = pBE->GetType();
345 break;
346 default:
348 OSL_FAIL( "GetEntryDescriptor: Unbekannter Typ!" );
349 eType = OBJ_TYPE_UNKNOWN;
351 break;
354 if ( eType == OBJ_TYPE_UNKNOWN )
355 break;
359 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
362 ItemType TreeListBox::ConvertType (EntryType eType)
364 switch (eType)
366 case OBJ_TYPE_DOCUMENT: return TYPE_SHELL;
367 case OBJ_TYPE_LIBRARY: return TYPE_LIBRARY;
368 case OBJ_TYPE_MODULE: return TYPE_MODULE;
369 case OBJ_TYPE_DIALOG: return TYPE_DIALOG;
370 case OBJ_TYPE_METHOD: return TYPE_METHOD;
371 default:
372 return static_cast<ItemType>(OBJ_TYPE_UNKNOWN);
376 bool TreeListBox::IsValidEntry( SvTreeListEntry* pEntry )
378 bool bIsValid = false;
380 EntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
381 ScriptDocument aDocument( aDesc.GetDocument() );
382 LibraryLocation eLocation( aDesc.GetLocation() );
383 OUString aLibName( aDesc.GetLibName() );
384 OUString aName( aDesc.GetName() );
385 OUString aMethodName( aDesc.GetMethodName() );
386 EntryType eType( aDesc.GetType() );
388 switch ( eType )
390 case OBJ_TYPE_DOCUMENT:
392 bIsValid = aDocument.isAlive()
393 && (aDocument.isApplication()
394 || GetRootEntryName(aDocument, eLocation).equals(GetEntryText(pEntry)));
396 break;
397 case OBJ_TYPE_LIBRARY:
399 bIsValid = aDocument.hasLibrary( E_SCRIPTS, aLibName ) || aDocument.hasLibrary( E_DIALOGS, aLibName );
401 break;
402 case OBJ_TYPE_MODULE:
404 bIsValid = aDocument.hasModule( aLibName, aName );
406 break;
407 case OBJ_TYPE_DIALOG:
409 bIsValid = aDocument.hasDialog( aLibName, aName );
411 break;
412 case OBJ_TYPE_METHOD:
414 bIsValid = HasMethod( aDocument, aLibName, aName, aMethodName );
416 break;
417 case OBJ_TYPE_DOCUMENT_OBJECTS:
418 case OBJ_TYPE_USERFORMS:
419 case OBJ_TYPE_NORMAL_MODULES:
420 case OBJ_TYPE_CLASS_MODULES:
422 bIsValid = true;
424 break;
425 default: ;
428 return bIsValid;
431 SbModule* TreeListBox::FindModule( SvTreeListEntry* pEntry )
433 return dynamic_cast<SbModule*>(FindVariable(pEntry));
436 SvTreeListEntry* TreeListBox::FindRootEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
438 OSL_ENSURE( rDocument.isValid(), "basctl::TreeListBox::FindRootEntry: invalid document!" );
439 sal_uLong nRootPos = 0;
440 SvTreeListEntry* pRootEntry = GetEntry( nRootPos );
441 while ( pRootEntry )
443 DBG_ASSERT( static_cast<Entry*>(pRootEntry->GetUserData())->GetType() == OBJ_TYPE_DOCUMENT, "Kein Shelleintrag?" );
444 DocumentEntry* pBDEntry = static_cast<DocumentEntry*>(pRootEntry->GetUserData());
445 if (pBDEntry && pBDEntry->GetDocument() == rDocument && pBDEntry->GetLocation() == eLocation)
446 return pRootEntry;
447 pRootEntry = GetEntry( ++nRootPos );
449 return 0;
452 OUString CreateMgrAndLibStr( const OUString& rMgrName, const OUString& rLibName )
454 return "[" + rMgrName + "]." + rLibName;
458 } // namespace basctl
460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */