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 <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>
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() )
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() );
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
) )
68 bOK
= QueryPassword( xModLibContainer
, aOULibName
, aPassword
);
74 // load module library
75 bool bModLibLoaded
= false;
76 if ( xModLibContainer
.is() && xModLibContainer
->hasByName( aOULibName
) )
78 if ( !xModLibContainer
->isLibraryLoaded( aOULibName
) )
81 xModLibContainer
->loadLibrary( aOULibName
);
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
) )
95 xDlgLibContainer
->loadLibrary( aOULibName
);
98 bDlgLibLoaded
= xDlgLibContainer
->isLibraryLoaded( aOULibName
);
101 if ( bModLibLoaded
|| bDlgLibLoaded
)
103 // create the sub entries
104 ImpCreateLibSubEntries( pEntry
, aDocument
, aOULibName
);
107 bool bDlgMode
= ( nMode
& BROWSEMODE_DIALOGS
) && !( nMode
& BROWSEMODE_MODULES
);
108 Image
aImage( IDEResId( bDlgMode
? RID_IMG_DLGLIB
: RID_IMG_MODLIB
) );
109 SetEntryBitmaps( pEntry
, aImage
);
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
);
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
);
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();
157 if ( doc
->isAlive() )
158 ScanEntry( *doc
, LIBRARY_LOCATION_DOCUMENT
);
162 SbxVariable
* TreeListBox::FindVariable( SvTreeListEntry
* pEntry
)
167 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
172 sal_uInt16 nDepth
= GetModel()->GetDepth( pEntry
);
180 aEntries
.push_front( pEntry
);
185 aDocument
= static_cast<DocumentEntry
*>(pEntry
->GetUserData())->GetDocument();
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
);
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 modul/object");
230 pVar
= static_cast<SbxObject
*>(pVar
)->GetMethods()->Find(aName
, SbxCLASS_METHOD
);
232 case OBJ_TYPE_DIALOG
:
233 // sbx dialogs removed
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.
243 OSL_FAIL( "FindVariable: Unbekannter Typ!" );
255 EntryDescriptor
TreeListBox::GetEntryDescriptor( SvTreeListEntry
* pEntry
)
257 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
258 LibraryLocation eLocation
= LIBRARY_LOCATION_UNKNOWN
;
260 OUString aLibSubName
;
262 OUString aMethodName
;
263 EntryType eType
= OBJ_TYPE_UNKNOWN
;
266 return EntryDescriptor( aDocument
, eLocation
, aLibName
, aLibSubName
, aName
, aMethodName
, eType
);
272 sal_uInt16 nDepth
= GetModel()->GetDepth( pEntry
);
280 aEntries
.push_front( pEntry
);
285 if (DocumentEntry
* pDocumentEntry
= static_cast<DocumentEntry
*>(pEntry
->GetUserData()))
287 aDocument
= pDocumentEntry
->GetDocument();
288 eLocation
= pDocumentEntry
->GetLocation();
289 eType
= OBJ_TYPE_DOCUMENT
;
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();
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: Unbekannter Typ!" );
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
).equals(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
, "Kein Shelleintrag?" );
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: */