Bump version to 6.0-36
[LibreOffice.git] / basctl / source / basicide / bastype3.cxx
blobdfaeddfa287017d6fc61e18b7168186000fdbcf3
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 <strings.hrc>
24 #include <bitmaps.hlst>
25 #include <bastypes.hxx>
26 #include <com/sun/star/script/XLibraryContainer.hpp>
27 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
28 #include <deque>
29 #include <sfx2/docfac.hxx>
30 #include <svtools/treelistentry.hxx>
32 namespace basctl
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star;
39 typedef std::deque< SvTreeListEntry* > EntryArray;
42 void TreeListBox::RequestingChildren( SvTreeListEntry* pEntry )
44 EntryDescriptor aDesc = GetEntryDescriptor(pEntry);
45 ScriptDocument aDocument = aDesc.GetDocument();
46 OSL_ENSURE( aDocument.isAlive(), "basctl::TreeListBox::RequestingChildren: invalid document!" );
47 if ( !aDocument.isAlive() )
48 return;
50 LibraryLocation eLocation = aDesc.GetLocation();
51 EntryType eType = aDesc.GetType();
53 if ( eType == OBJ_TYPE_DOCUMENT )
55 ImpCreateLibEntries( pEntry, aDocument, eLocation );
57 else if ( eType == OBJ_TYPE_LIBRARY )
59 OUString aOULibName( aDesc.GetLibName() );
61 // check password
62 bool bOK = true;
63 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
64 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
66 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
67 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
69 OUString aPassword;
70 bOK = QueryPassword( xModLibContainer, aOULibName, aPassword );
74 if ( bOK )
76 // load module library
77 bool bModLibLoaded = false;
78 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
80 if ( !xModLibContainer->isLibraryLoaded( aOULibName ) )
82 EnterWait();
83 xModLibContainer->loadLibrary( aOULibName );
84 LeaveWait();
86 bModLibLoaded = xModLibContainer->isLibraryLoaded( aOULibName );
89 // load dialog library
90 bool bDlgLibLoaded = false;
91 Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
92 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
94 if ( !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
96 EnterWait();
97 xDlgLibContainer->loadLibrary( aOULibName );
98 LeaveWait();
100 bDlgLibLoaded = xDlgLibContainer->isLibraryLoaded( aOULibName );
103 if ( bModLibLoaded || bDlgLibLoaded )
105 // create the sub entries
106 ImpCreateLibSubEntries( pEntry, aDocument, aOULibName );
108 // exchange image
109 const bool bDlgMode = (nMode & BrowseMode::Dialogs) && !(nMode & BrowseMode::Modules);
110 Image aImage(StockImage::Yes, bDlgMode ? OUStringLiteral(RID_BMP_DLGLIB) : OUStringLiteral(RID_BMP_MODLIB));
111 SetEntryBitmaps( pEntry, aImage );
113 else
115 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Error loading library!" );
119 else if ( eType == OBJ_TYPE_DOCUMENT_OBJECTS
120 || eType == OBJ_TYPE_USERFORMS
121 || eType == OBJ_TYPE_NORMAL_MODULES
122 || eType == OBJ_TYPE_CLASS_MODULES )
124 OUString aLibName( aDesc.GetLibName() );
125 ImpCreateLibSubSubEntriesInVBAMode( pEntry, aDocument, aLibName );
127 else {
128 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Unknown Type!" );
132 void TreeListBox::ExpandedHdl()
134 SvTreeListEntry* pEntry = GetHdlEntry();
135 assert(pEntry && "What was collapsed?");
136 if ( !IsExpanded( pEntry ) && pEntry->HasChildrenOnDemand() )
138 SvTreeListEntry* pChild = FirstChild( pEntry );
139 while ( pChild )
141 GetModel()->Remove( pChild ); // does also call the DTOR
142 pChild = FirstChild( pEntry );
147 void TreeListBox::ScanAllEntries()
149 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
150 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
152 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
153 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
154 doc != aDocuments.end();
155 ++doc
158 if ( doc->isAlive() )
159 ScanEntry( *doc, LIBRARY_LOCATION_DOCUMENT );
163 SbxVariable* TreeListBox::FindVariable( SvTreeListEntry* pEntry )
165 if ( !pEntry )
166 return nullptr;
168 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
169 EntryArray aEntries;
171 while ( pEntry )
173 sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
174 switch ( nDepth )
176 case 4:
177 case 3:
178 case 2:
179 case 1:
181 aEntries.push_front( pEntry );
183 break;
184 case 0:
186 aDocument = static_cast<DocumentEntry*>(pEntry->GetUserData())->GetDocument();
188 break;
190 pEntry = GetParent( pEntry );
193 SbxVariable* pVar = nullptr;
194 if ( !aEntries.empty() )
196 bool bDocumentObjects = false;
197 for (SvTreeListEntry* pLE : aEntries)
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 module/object");
226 if(!pVar)
228 break;
230 pVar = static_cast<SbxObject*>(pVar)->GetMethods()->Find(aName, SbxClassType::Method);
231 break;
232 case OBJ_TYPE_DIALOG:
233 // sbx dialogs removed
234 break;
235 case OBJ_TYPE_DOCUMENT_OBJECTS:
236 bDocumentObjects = true;
237 SAL_FALLTHROUGH;
238 case OBJ_TYPE_USERFORMS:
239 case OBJ_TYPE_NORMAL_MODULES:
240 case OBJ_TYPE_CLASS_MODULES:
241 // skip, to find the child entry.
242 continue;
243 default:
244 OSL_FAIL( "FindVariable: unknown type" );
245 pVar = nullptr;
246 break;
248 if ( !pVar )
249 break;
253 return pVar;
256 EntryDescriptor TreeListBox::GetEntryDescriptor( SvTreeListEntry* pEntry )
258 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
259 LibraryLocation eLocation = LIBRARY_LOCATION_UNKNOWN;
260 OUString aLibName;
261 OUString aLibSubName;
262 OUString aName;
263 OUString aMethodName;
264 EntryType eType = OBJ_TYPE_UNKNOWN;
266 if ( !pEntry )
267 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
269 EntryArray aEntries;
271 while ( pEntry )
273 sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
274 switch ( nDepth )
276 case 4:
277 case 3:
278 case 2:
279 case 1:
281 aEntries.push_front( pEntry );
283 break;
284 case 0:
286 if (DocumentEntry* pDocumentEntry = static_cast<DocumentEntry*>(pEntry->GetUserData()))
288 aDocument = pDocumentEntry->GetDocument();
289 eLocation = pDocumentEntry->GetLocation();
290 eType = OBJ_TYPE_DOCUMENT;
293 break;
295 pEntry = GetParent( pEntry );
298 if ( !aEntries.empty() )
300 for (SvTreeListEntry* pLE : aEntries)
302 assert(pLE && "Entry not found in array");
303 Entry* pBE = static_cast<Entry*>(pLE->GetUserData());
304 assert(pBE && "No data found in entry!");
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: unknown type" );
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) == 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, "No shell entry?" );
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 nullptr;
447 OUString CreateMgrAndLibStr( const OUString& rMgrName, const OUString& rLibName )
449 return "[" + rMgrName + "]." + rLibName;
453 } // namespace basctl
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */