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 <basic/sbx.hxx>
23 #include <bastype2.hxx>
25 #include <baside2.hrc>
27 #include <bastypes.hxx>
29 #include <com/sun/star/script/XLibraryContainer.hpp>
30 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
32 #include <sfx2/docfac.hxx>
33 #include "svtools/treelistentry.hxx"
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() )
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() );
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
) )
73 bOK
= QueryPassword( xModLibContainer
, aOULibName
, aPassword
);
79 // load module library
80 bool bModLibLoaded
= false;
81 if ( xModLibContainer
.is() && xModLibContainer
->hasByName( aOULibName
) )
83 if ( !xModLibContainer
->isLibraryLoaded( aOULibName
) )
86 xModLibContainer
->loadLibrary( aOULibName
);
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
) )
100 xDlgLibContainer
->loadLibrary( aOULibName
);
103 bDlgLibLoaded
= xDlgLibContainer
->isLibraryLoaded( aOULibName
);
106 if ( bModLibLoaded
|| bDlgLibLoaded
)
108 // create the sub entries
109 ImpCreateLibSubEntries( pEntry
, aDocument
, aOULibName
);
112 bool bDlgMode
= ( nMode
& BROWSEMODE_DIALOGS
) && !( nMode
& BROWSEMODE_MODULES
);
113 Image
aImage( IDEResId( bDlgMode
? RID_IMG_DLGLIB
: RID_IMG_MODLIB
) );
114 SetEntryBitmaps( pEntry
, aImage
);
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
);
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
);
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();
162 if ( doc
->isAlive() )
163 ScanEntry( *doc
, LIBRARY_LOCATION_DOCUMENT
);
167 SbxVariable
* TreeListBox::FindVariable( SvTreeListEntry
* pEntry
)
172 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
177 sal_uInt16 nDepth
= GetModel()->GetDepth( pEntry
);
185 aEntries
.push_front( pEntry
);
190 aDocument
= static_cast<DocumentEntry
*>(pEntry
->GetUserData())->GetDocument();
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
);
215 case OBJ_TYPE_MODULE
:
216 DBG_ASSERT(dynamic_cast<StarBASIC
*>(pVar
), "FindVariable: invalid Basic");
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
);
229 case OBJ_TYPE_METHOD
:
230 DBG_ASSERT(dynamic_cast<SbxObject
*>(pVar
), "FindVariable: invalid modul/object");
235 pVar
= static_cast<SbxObject
*>(pVar
)->GetMethods()->Find(aName
, SbxCLASS_METHOD
);
237 case OBJ_TYPE_DIALOG
:
238 // sbx dialogs removed
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.
248 OSL_FAIL( "FindVariable: Unbekannter Typ!" );
260 EntryDescriptor
TreeListBox::GetEntryDescriptor( SvTreeListEntry
* pEntry
)
262 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
263 LibraryLocation eLocation
= LIBRARY_LOCATION_UNKNOWN
;
265 OUString aLibSubName
;
267 OUString aMethodName
;
268 EntryType eType
= OBJ_TYPE_UNKNOWN
;
271 return EntryDescriptor( aDocument
, eLocation
, aLibName
, aLibSubName
, aName
, aMethodName
, eType
);
277 sal_uInt16 nDepth
= GetModel()->GetDepth( pEntry
);
285 aEntries
.push_front( pEntry
);
290 if (DocumentEntry
* pDocumentEntry
= static_cast<DocumentEntry
*>(pEntry
->GetUserData()))
292 aDocument
= pDocumentEntry
->GetDocument();
293 eLocation
= pDocumentEntry
->GetLocation();
294 eType
= OBJ_TYPE_DOCUMENT
;
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();
319 case OBJ_TYPE_MODULE
:
321 aName
= GetEntryText( pLE
);
322 eType
= pBE
->GetType();
325 case OBJ_TYPE_METHOD
:
327 aMethodName
= GetEntryText( pLE
);
328 eType
= pBE
->GetType();
331 case OBJ_TYPE_DIALOG
:
333 aName
= GetEntryText( pLE
);
334 eType
= pBE
->GetType();
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();
348 OSL_FAIL( "GetEntryDescriptor: Unbekannter Typ!" );
349 eType
= OBJ_TYPE_UNKNOWN
;
354 if ( eType
== OBJ_TYPE_UNKNOWN
)
359 return EntryDescriptor( aDocument
, eLocation
, aLibName
, aLibSubName
, aName
, aMethodName
, eType
);
362 ItemType
TreeListBox::ConvertType (EntryType 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
;
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() );
390 case OBJ_TYPE_DOCUMENT
:
392 bIsValid
= aDocument
.isAlive()
393 && (aDocument
.isApplication()
394 || GetRootEntryName(aDocument
, eLocation
).equals(GetEntryText(pEntry
)));
397 case OBJ_TYPE_LIBRARY
:
399 bIsValid
= aDocument
.hasLibrary( E_SCRIPTS
, aLibName
) || aDocument
.hasLibrary( E_DIALOGS
, aLibName
);
402 case OBJ_TYPE_MODULE
:
404 bIsValid
= aDocument
.hasModule( aLibName
, aName
);
407 case OBJ_TYPE_DIALOG
:
409 bIsValid
= aDocument
.hasDialog( aLibName
, aName
);
412 case OBJ_TYPE_METHOD
:
414 bIsValid
= HasMethod( aDocument
, aLibName
, aName
, aMethodName
);
417 case OBJ_TYPE_DOCUMENT_OBJECTS
:
418 case OBJ_TYPE_USERFORMS
:
419 case OBJ_TYPE_NORMAL_MODULES
:
420 case OBJ_TYPE_CLASS_MODULES
:
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
);
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
)
447 pRootEntry
= GetEntry( ++nRootPos
);
452 OUString
CreateMgrAndLibStr( const OUString
& rMgrName
, const OUString
& rLibName
)
454 return "[" + rMgrName
+ "]." + rLibName
;
458 } // namespace basctl
460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */