1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 #include <sfx2/docfac.hxx>
30 #include <svtools/treelistentry.hxx>
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() )
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() );
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
) )
70 bOK
= QueryPassword( xModLibContainer
, aOULibName
, aPassword
);
76 // load module library
77 bool bModLibLoaded
= false;
78 if ( xModLibContainer
.is() && xModLibContainer
->hasByName( aOULibName
) )
80 if ( !xModLibContainer
->isLibraryLoaded( aOULibName
) )
83 xModLibContainer
->loadLibrary( aOULibName
);
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
) )
97 xDlgLibContainer
->loadLibrary( aOULibName
);
100 bDlgLibLoaded
= xDlgLibContainer
->isLibraryLoaded( aOULibName
);
103 if ( bModLibLoaded
|| bDlgLibLoaded
)
105 // create the sub entries
106 ImpCreateLibSubEntries( pEntry
, aDocument
, aOULibName
);
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
);
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
);
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
);
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();
158 if ( doc
->isAlive() )
159 ScanEntry( *doc
, LIBRARY_LOCATION_DOCUMENT
);
163 SbxVariable
* TreeListBox::FindVariable( SvTreeListEntry
* pEntry
)
168 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
173 sal_uInt16 nDepth
= GetModel()->GetDepth( pEntry
);
181 aEntries
.push_front( pEntry
);
186 aDocument
= static_cast<DocumentEntry
*>(pEntry
->GetUserData())->GetDocument();
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
);
210 case OBJ_TYPE_MODULE
:
211 DBG_ASSERT(dynamic_cast<StarBASIC
*>(pVar
), "FindVariable: invalid Basic");
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
);
224 case OBJ_TYPE_METHOD
:
225 DBG_ASSERT(dynamic_cast<SbxObject
*>(pVar
), "FindVariable: invalid module/object");
230 pVar
= static_cast<SbxObject
*>(pVar
)->GetMethods()->Find(aName
, SbxClassType::Method
);
232 case OBJ_TYPE_DIALOG
:
233 // sbx dialogs removed
235 case OBJ_TYPE_DOCUMENT_OBJECTS
:
236 bDocumentObjects
= true;
238 case OBJ_TYPE_USERFORMS
:
239 case OBJ_TYPE_NORMAL_MODULES
:
240 case OBJ_TYPE_CLASS_MODULES
:
241 // skip, to find the child entry.
244 OSL_FAIL( "FindVariable: unknown type" );
256 EntryDescriptor
TreeListBox::GetEntryDescriptor( SvTreeListEntry
* pEntry
)
258 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
259 LibraryLocation eLocation
= LIBRARY_LOCATION_UNKNOWN
;
261 OUString aLibSubName
;
263 OUString aMethodName
;
264 EntryType eType
= OBJ_TYPE_UNKNOWN
;
267 return EntryDescriptor( aDocument
, eLocation
, aLibName
, aLibSubName
, aName
, aMethodName
, eType
);
273 sal_uInt16 nDepth
= GetModel()->GetDepth( pEntry
);
281 aEntries
.push_front( pEntry
);
286 if (DocumentEntry
* pDocumentEntry
= static_cast<DocumentEntry
*>(pEntry
->GetUserData()))
288 aDocument
= pDocumentEntry
->GetDocument();
289 eLocation
= pDocumentEntry
->GetLocation();
290 eType
= OBJ_TYPE_DOCUMENT
;
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();
314 case OBJ_TYPE_MODULE
:
316 aName
= GetEntryText( pLE
);
317 eType
= pBE
->GetType();
320 case OBJ_TYPE_METHOD
:
322 aMethodName
= GetEntryText( pLE
);
323 eType
= pBE
->GetType();
326 case OBJ_TYPE_DIALOG
:
328 aName
= GetEntryText( pLE
);
329 eType
= pBE
->GetType();
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();
343 OSL_FAIL( "GetEntryDescriptor: unknown type" );
344 eType
= OBJ_TYPE_UNKNOWN
;
349 if ( eType
== OBJ_TYPE_UNKNOWN
)
354 return EntryDescriptor( aDocument
, eLocation
, aLibName
, aLibSubName
, aName
, aMethodName
, eType
);
357 ItemType
TreeListBox::ConvertType (EntryType 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
;
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() );
385 case OBJ_TYPE_DOCUMENT
:
387 bIsValid
= aDocument
.isAlive()
388 && (aDocument
.isApplication()
389 || GetRootEntryName(aDocument
, eLocation
) == GetEntryText(pEntry
));
392 case OBJ_TYPE_LIBRARY
:
394 bIsValid
= aDocument
.hasLibrary( E_SCRIPTS
, aLibName
) || aDocument
.hasLibrary( E_DIALOGS
, aLibName
);
397 case OBJ_TYPE_MODULE
:
399 bIsValid
= aDocument
.hasModule( aLibName
, aName
);
402 case OBJ_TYPE_DIALOG
:
404 bIsValid
= aDocument
.hasDialog( aLibName
, aName
);
407 case OBJ_TYPE_METHOD
:
409 bIsValid
= HasMethod( aDocument
, aLibName
, aName
, aMethodName
);
412 case OBJ_TYPE_DOCUMENT_OBJECTS
:
413 case OBJ_TYPE_USERFORMS
:
414 case OBJ_TYPE_NORMAL_MODULES
:
415 case OBJ_TYPE_CLASS_MODULES
:
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
);
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
)
442 pRootEntry
= GetEntry( ++nRootPos
);
447 OUString
CreateMgrAndLibStr( const OUString
& rMgrName
, const OUString
& rLibName
)
449 return "[" + rMgrName
+ "]." + rLibName
;
453 } // namespace basctl
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */