bump product version to 5.0.4.1
[LibreOffice.git] / basctl / source / basicide / bastype2.cxx
blobde255910e212bce50624d0891a7e20804ad8cee0
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 .
21 #include <bastypes.hxx>
22 #include <bastype2.hxx>
23 #include <baside2.hrc>
24 #include <iderid.hxx>
25 #include <tools/urlobj.hxx>
26 #include <tools/diagnose_ex.h>
27 #include <svtools/imagemgr.hxx>
28 #include <svtools/treelistentry.hxx>
29 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
30 #include <com/sun/star/frame/ModuleManager.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <sfx2/dispatch.hxx>
33 #include <vcl/builderfactory.hxx>
35 #include <initializer_list>
37 #include <com/sun/star/script/ModuleType.hpp>
38 #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/container/XNamed.hpp>
42 namespace basctl
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star;
48 void ModuleInfoHelper::getObjectName( const uno::Reference< container::XNameContainer >& rLib, const OUString& rModName, OUString& rObjName )
50 try
52 uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( rLib, uno::UNO_QUERY );
53 if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( rModName ) )
55 script::ModuleInfo aModuleInfo = xVBAModuleInfo->getModuleInfo( rModName );
56 uno::Any aObject( aModuleInfo.ModuleObject );
57 uno::Reference< lang::XServiceInfo > xServiceInfo( aObject, uno::UNO_QUERY );
58 if( xServiceInfo.is() && xServiceInfo->supportsService( "ooo.vba.excel.Worksheet" ) )
60 uno::Reference< container::XNamed > xNamed( aObject, uno::UNO_QUERY );
61 if( xNamed.is() )
62 rObjName = xNamed->getName();
66 catch(const uno::Exception& )
71 sal_Int32 ModuleInfoHelper::getModuleType( const uno::Reference< container::XNameContainer >& rLib, const OUString& rModName )
73 sal_Int32 nType = script::ModuleType::NORMAL;
74 uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( rLib, uno::UNO_QUERY );
75 if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( rModName ) )
77 script::ModuleInfo aModuleInfo = xVBAModuleInfo->getModuleInfo( rModName );
78 nType = aModuleInfo.ModuleType;
80 return nType;
83 Entry::~Entry()
84 { }
86 DocumentEntry::DocumentEntry (
87 ScriptDocument const& rDocument,
88 LibraryLocation eLocation,
89 EntryType eType
90 ) :
91 Entry(eType),
92 m_aDocument(rDocument),
93 m_eLocation(eLocation)
95 OSL_ENSURE( m_aDocument.isValid(), "DocumentEntry::DocumentEntry: illegal document!" );
98 DocumentEntry::~DocumentEntry()
99 { }
101 LibEntry::LibEntry (
102 ScriptDocument const& rDocument,
103 LibraryLocation eLocation,
104 OUString const& rLibName,
105 EntryType eType
107 DocumentEntry(rDocument, eLocation, eType),
108 m_aLibName(rLibName)
111 LibEntry::~LibEntry()
114 EntryDescriptor::EntryDescriptor () :
115 m_aDocument(ScriptDocument::getApplicationScriptDocument()),
116 m_eLocation(LIBRARY_LOCATION_UNKNOWN),
117 m_eType(OBJ_TYPE_UNKNOWN)
120 EntryDescriptor::EntryDescriptor (
121 ScriptDocument const& rDocument,
122 LibraryLocation eLocation,
123 OUString const& rLibName,
124 OUString const& rLibSubName,
125 OUString const& rName,
126 EntryType eType
128 m_aDocument(rDocument),
129 m_eLocation(eLocation),
130 m_aLibName(rLibName),
131 m_aLibSubName(rLibSubName),
132 m_aName(rName),
133 m_eType(eType)
135 OSL_ENSURE( m_aDocument.isValid(), "EntryDescriptor::EntryDescriptor: invalid document!" );
138 EntryDescriptor::EntryDescriptor (
139 ScriptDocument const& rDocument,
140 LibraryLocation eLocation,
141 OUString const& rLibName,
142 OUString const& rLibSubName,
143 OUString const& rName,
144 OUString const& rMethodName,
145 EntryType eType
147 m_aDocument(rDocument),
148 m_eLocation(eLocation),
149 m_aLibName(rLibName),
150 m_aLibSubName(rLibSubName),
151 m_aName(rName),
152 m_aMethodName(rMethodName),
153 m_eType(eType)
155 OSL_ENSURE( m_aDocument.isValid(), "EntryDescriptor::EntryDescriptor: invalid document!" );
158 EntryDescriptor::~EntryDescriptor()
161 bool EntryDescriptor::operator == (EntryDescriptor const& rDesc) const
163 return
164 m_aDocument == rDesc.m_aDocument &&
165 m_eLocation == rDesc.m_eLocation &&
166 m_aLibName == rDesc.m_aLibName &&
167 m_aLibSubName == rDesc.m_aLibSubName &&
168 m_aName == rDesc.m_aName &&
169 m_aMethodName == rDesc.m_aMethodName &&
170 m_eType == rDesc.m_eType;
174 // TreeListBox
178 TreeListBox::TreeListBox (vcl::Window* pParent, ResId const& rRes)
179 : SvTreeListBox( pParent, IDEResId( sal::static_int_cast<sal_uInt16>( rRes.GetId() ) ) )
180 , m_aNotifier( *this )
182 Init();
185 TreeListBox::TreeListBox (vcl::Window* pParent, WinBits nStyle)
186 : SvTreeListBox(pParent, nStyle)
187 , m_aNotifier( *this )
189 Init();
192 void TreeListBox::Init()
194 SetNodeDefaultImages();
195 SetSelectionMode( SINGLE_SELECTION );
196 nMode = 0xFF; // everything
199 VCL_BUILDER_DECL_FACTORY(TreeListBox)
201 WinBits nWinBits = WB_TABSTOP;
202 OString sBorder = VclBuilder::extractCustomProperty(rMap);
203 if (!sBorder.isEmpty())
204 nWinBits |= WB_BORDER;
205 rRet = VclPtr<TreeListBox>::Create(pParent, nWinBits);
208 TreeListBox::~TreeListBox ()
210 disposeOnce();
213 void TreeListBox::dispose()
215 m_aNotifier.dispose();
217 // destroy user data
218 SvTreeListEntry* pEntry = First();
219 while ( pEntry )
221 delete static_cast<Entry*>( pEntry->GetUserData() );
222 pEntry->SetUserData( NULL );
223 pEntry = Next( pEntry );
225 SvTreeListBox::dispose();
228 void TreeListBox::ScanEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
230 OSL_ENSURE( rDocument.isAlive(), "TreeListBox::ScanEntry: illegal document!" );
231 if ( !rDocument.isAlive() )
232 return;
234 // can be called multiple times for updating!
236 // actually test if basic's in the tree already?!
237 SetUpdateMode(false);
239 // level 1: BasicManager (application, document, ...)
240 SvTreeListEntry* pDocumentRootEntry = FindRootEntry( rDocument, eLocation );
241 if ( pDocumentRootEntry && IsExpanded( pDocumentRootEntry ) )
242 ImpCreateLibEntries( pDocumentRootEntry, rDocument, eLocation );
243 if ( !pDocumentRootEntry )
245 OUString aRootName( GetRootEntryName( rDocument, eLocation ) );
246 Image aImage;
247 GetRootEntryBitmaps( rDocument, aImage );
248 std::unique_ptr<Entry> e(new DocumentEntry(rDocument, eLocation));
249 AddEntry(
250 aRootName,
251 aImage,
252 0, true, &e);
255 SetUpdateMode(true);
258 void TreeListBox::ImpCreateLibEntries( SvTreeListEntry* pDocumentRootEntry, const ScriptDocument& rDocument, LibraryLocation eLocation )
260 // get a sorted list of library names
261 Sequence< OUString > aLibNames( rDocument.getLibraryNames() );
262 sal_Int32 nLibCount = aLibNames.getLength();
263 const OUString* pLibNames = aLibNames.getConstArray();
265 for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
267 OUString aLibName = pLibNames[ i ];
269 if ( eLocation == rDocument.getLibraryLocation( aLibName ) )
271 // check, if the module library is loaded
272 bool bModLibLoaded = false;
273 OUString aOULibName( aLibName );
274 Reference< script::XLibraryContainer > xModLibContainer( rDocument.getLibraryContainer( E_SCRIPTS ) );
275 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryLoaded( aOULibName ) )
276 bModLibLoaded = true;
278 // check, if the dialog library is loaded
279 bool bDlgLibLoaded = false;
280 Reference< script::XLibraryContainer > xDlgLibContainer( rDocument.getLibraryContainer( E_DIALOGS ) );
281 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryLoaded( aOULibName ) )
282 bDlgLibLoaded = true;
284 bool bLoaded = bModLibLoaded || bDlgLibLoaded;
286 // if only one of the libraries is loaded, load also the other
287 if ( bLoaded )
289 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
290 xModLibContainer->loadLibrary( aOULibName );
292 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
293 xDlgLibContainer->loadLibrary( aOULibName );
296 // create tree list box entry
297 sal_uInt16 nId;
298 if ( ( nMode & BROWSEMODE_DIALOGS ) && !( nMode & BROWSEMODE_MODULES ) )
299 nId = bLoaded ? RID_IMG_DLGLIB : RID_IMG_DLGLIBNOTLOADED;
300 else
301 nId = bLoaded ? RID_IMG_MODLIB : RID_IMG_MODLIBNOTLOADED;
302 SvTreeListEntry* pLibRootEntry = FindEntry( pDocumentRootEntry, aLibName, OBJ_TYPE_LIBRARY );
303 if ( pLibRootEntry )
305 SetEntryBitmaps( pLibRootEntry, Image( IDEResId( nId ) ) );
306 if ( IsExpanded( pLibRootEntry ) )
307 ImpCreateLibSubEntries( pLibRootEntry, rDocument, aLibName );
309 else
311 std::unique_ptr<Entry> e(new Entry(OBJ_TYPE_LIBRARY));
312 AddEntry(
313 aLibName,
314 Image( IDEResId( nId ) ),
315 pDocumentRootEntry, true, &e);
321 void TreeListBox::ImpCreateLibSubEntries( SvTreeListEntry* pLibRootEntry, const ScriptDocument& rDocument, const OUString& rLibName )
323 // modules
324 if ( nMode & BROWSEMODE_MODULES )
326 Reference< script::XLibraryContainer > xModLibContainer( rDocument.getLibraryContainer( E_SCRIPTS ) );
328 if ( xModLibContainer.is() && xModLibContainer->hasByName( rLibName ) && xModLibContainer->isLibraryLoaded( rLibName ) )
332 if( rDocument.isInVBAMode() )
333 ImpCreateLibSubEntriesInVBAMode( pLibRootEntry, rDocument, rLibName );
334 else
336 // get a sorted list of module names
337 Sequence< OUString > aModNames = rDocument.getObjectNames( E_SCRIPTS, rLibName );
338 sal_Int32 nModCount = aModNames.getLength();
339 const OUString* pModNames = aModNames.getConstArray();
341 for ( sal_Int32 i = 0 ; i < nModCount ; i++ )
343 OUString aModName = pModNames[ i ];
344 SvTreeListEntry* pModuleEntry = FindEntry( pLibRootEntry, aModName, OBJ_TYPE_MODULE );
345 if ( !pModuleEntry )
347 std::unique_ptr<Entry> e(new Entry(OBJ_TYPE_MODULE));
348 pModuleEntry = AddEntry(
349 aModName,
350 Image( IDEResId( RID_IMG_MODULE ) ),
351 pLibRootEntry, false, &e);
354 // methods
355 if ( nMode & BROWSEMODE_SUBS )
357 Sequence< OUString > aNames = GetMethodNames( rDocument, rLibName, aModName );
358 sal_Int32 nCount = aNames.getLength();
359 const OUString* pNames = aNames.getConstArray();
361 for ( sal_Int32 j = 0 ; j < nCount ; j++ )
363 OUString aName = pNames[ j ];
364 SvTreeListEntry* pEntry = FindEntry( pModuleEntry, aName, OBJ_TYPE_METHOD );
365 if ( !pEntry )
367 std::unique_ptr<Entry> e(
368 new Entry(OBJ_TYPE_METHOD));
369 pEntry = AddEntry(
370 aName,
371 Image( IDEResId( RID_IMG_MACRO ) ),
372 pModuleEntry, false, &e);
379 catch ( const container::NoSuchElementException& )
381 DBG_UNHANDLED_EXCEPTION();
386 // dialogs
387 if ( nMode & BROWSEMODE_DIALOGS )
389 Reference< script::XLibraryContainer > xDlgLibContainer( rDocument.getLibraryContainer( E_DIALOGS ) );
391 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( rLibName ) && xDlgLibContainer->isLibraryLoaded( rLibName ) )
395 // get a sorted list of dialog names
396 Sequence< OUString > aDlgNames( rDocument.getObjectNames( E_DIALOGS, rLibName ) );
397 sal_Int32 nDlgCount = aDlgNames.getLength();
398 const OUString* pDlgNames = aDlgNames.getConstArray();
400 for ( sal_Int32 i = 0 ; i < nDlgCount ; i++ )
402 OUString aDlgName = pDlgNames[ i ];
403 SvTreeListEntry* pDialogEntry = FindEntry( pLibRootEntry, aDlgName, OBJ_TYPE_DIALOG );
404 if ( !pDialogEntry )
406 std::unique_ptr<Entry> e(new Entry(OBJ_TYPE_DIALOG));
407 pDialogEntry = AddEntry(
408 aDlgName,
409 Image( IDEResId( RID_IMG_DIALOG ) ),
410 pLibRootEntry, false, &e);
414 catch (const container::NoSuchElementException& )
416 DBG_UNHANDLED_EXCEPTION();
422 void TreeListBox::ImpCreateLibSubEntriesInVBAMode( SvTreeListEntry* pLibRootEntry, const ScriptDocument& rDocument, const OUString& rLibName )
424 auto const aEntries = {
425 std::make_pair( OBJ_TYPE_DOCUMENT_OBJECTS, IDE_RESSTR(RID_STR_DOCUMENT_OBJECTS) ),
426 std::make_pair( OBJ_TYPE_USERFORMS, IDE_RESSTR(RID_STR_USERFORMS) ),
427 std::make_pair( OBJ_TYPE_NORMAL_MODULES, IDE_RESSTR(RID_STR_NORMAL_MODULES) ),
428 std::make_pair( OBJ_TYPE_CLASS_MODULES, IDE_RESSTR(RID_STR_CLASS_MODULES) ) };
429 for( auto const & iter: aEntries )
431 EntryType eType = iter.first;
432 OUString const & aEntryName = iter.second;
433 SvTreeListEntry* pLibSubRootEntry = FindEntry( pLibRootEntry, aEntryName, eType );
434 if( pLibSubRootEntry )
436 SetEntryBitmaps( pLibSubRootEntry, Image( IDEResId( RID_IMG_MODLIB ) ) );
437 if ( IsExpanded( pLibSubRootEntry ) )
438 ImpCreateLibSubSubEntriesInVBAMode( pLibSubRootEntry, rDocument, rLibName );
440 else
442 std::unique_ptr<Entry> e(new Entry(eType));
443 AddEntry(
444 aEntryName,
445 Image( IDEResId( RID_IMG_MODLIB ) ),
446 pLibRootEntry, true, &e);
451 void TreeListBox::ImpCreateLibSubSubEntriesInVBAMode( SvTreeListEntry* pLibSubRootEntry, const ScriptDocument& rDocument, const OUString& rLibName )
453 uno::Reference< container::XNameContainer > xLib = rDocument.getOrCreateLibrary( E_SCRIPTS, rLibName );
454 if( !xLib.is() )
455 return;
459 // get a sorted list of module names
460 Sequence< OUString > aModNames = rDocument.getObjectNames( E_SCRIPTS, rLibName );
461 sal_Int32 nModCount = aModNames.getLength();
462 const OUString* pModNames = aModNames.getConstArray();
464 EntryDescriptor aDesc( GetEntryDescriptor( pLibSubRootEntry ) );
465 EntryType eCurrentType( aDesc.GetType() );
467 for ( sal_Int32 i = 0 ; i < nModCount ; i++ )
469 OUString aModName = pModNames[ i ];
470 EntryType eType = OBJ_TYPE_UNKNOWN;
471 switch( ModuleInfoHelper::getModuleType( xLib, aModName ) )
473 case script::ModuleType::DOCUMENT:
474 eType = OBJ_TYPE_DOCUMENT_OBJECTS;
475 break;
476 case script::ModuleType::FORM:
477 eType = OBJ_TYPE_USERFORMS;
478 break;
479 case script::ModuleType::NORMAL:
480 eType = OBJ_TYPE_NORMAL_MODULES;
481 break;
482 case script::ModuleType::CLASS:
483 eType = OBJ_TYPE_CLASS_MODULES;
484 break;
486 if( eType != eCurrentType )
487 continue;
489 // display a nice friendly name in the ObjectModule tab,
490 // combining the objectname and module name, e.g. Sheet1 ( Financials )
491 OUString aEntryName = aModName;
492 if( eType == OBJ_TYPE_DOCUMENT_OBJECTS )
494 OUString sObjName;
495 ModuleInfoHelper::getObjectName( xLib, aModName, sObjName );
496 if( !sObjName.isEmpty() )
498 aEntryName += " (" + sObjName + ")";
501 SvTreeListEntry* pModuleEntry = FindEntry( pLibSubRootEntry, aEntryName, OBJ_TYPE_MODULE );
502 if ( !pModuleEntry )
504 std::unique_ptr<Entry> e(new Entry(OBJ_TYPE_MODULE));
505 pModuleEntry = AddEntry(
506 aEntryName,
507 Image( IDEResId( RID_IMG_MODULE ) ),
508 pLibSubRootEntry, false, &e);
511 // methods
512 if ( nMode & BROWSEMODE_SUBS )
514 Sequence< OUString > aNames = GetMethodNames( rDocument, rLibName, aModName );
515 sal_Int32 nCount = aNames.getLength();
516 const OUString* pNames = aNames.getConstArray();
518 for ( sal_Int32 j = 0 ; j < nCount ; j++ )
520 OUString aName = pNames[ j ];
521 SvTreeListEntry* pEntry = FindEntry( pModuleEntry, aName, OBJ_TYPE_METHOD );
522 if ( !pEntry )
524 std::unique_ptr<Entry> e(new Entry(OBJ_TYPE_METHOD));
525 pEntry = AddEntry(
526 aName,
527 Image( IDEResId( RID_IMG_MACRO ) ),
528 pModuleEntry, false, &e);
534 catch ( const container::NoSuchElementException& )
536 DBG_UNHANDLED_EXCEPTION();
540 SvTreeListEntry* TreeListBox::ImpFindEntry( SvTreeListEntry* pParent, const OUString& rText )
542 sal_uLong nRootPos = 0;
543 SvTreeListEntry* pEntry = pParent ? FirstChild( pParent ) : GetEntry( nRootPos );
544 while ( pEntry )
546 if ( rText.equals(GetEntryText( pEntry )) )
547 return pEntry;
549 pEntry = pParent ? NextSibling( pEntry ) : GetEntry( ++nRootPos );
551 return 0;
554 void TreeListBox::onDocumentCreated( const ScriptDocument& /*_rDocument*/ )
556 UpdateEntries();
559 void TreeListBox::onDocumentOpened( const ScriptDocument& /*_rDocument*/ )
561 UpdateEntries();
564 void TreeListBox::onDocumentSave( const ScriptDocument& /*_rDocument*/ )
566 // not interested in
569 void TreeListBox::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ )
571 // not interested in
574 void TreeListBox::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ )
576 // not interested in
579 void TreeListBox::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ )
581 UpdateEntries();
584 void TreeListBox::onDocumentClosed( const ScriptDocument& rDocument )
586 UpdateEntries();
587 // The document is not yet actually deleted, so we need to remove its entry
588 // manually.
589 RemoveEntry(rDocument);
592 void TreeListBox::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ )
594 // not interested in
597 void TreeListBox::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ )
599 // not interested in
602 void TreeListBox::UpdateEntries()
604 EntryDescriptor aCurDesc( GetEntryDescriptor( FirstSelected() ) );
606 // removing the invalid entries
607 SvTreeListEntry* pLastValid = 0;
608 SvTreeListEntry* pEntry = First();
609 while ( pEntry )
611 if ( IsValidEntry( pEntry ) )
612 pLastValid = pEntry;
613 else
614 RemoveEntry(pEntry);
615 pEntry = pLastValid ? Next( pLastValid ) : First();
618 ScanAllEntries();
620 SetCurrentEntry( aCurDesc );
623 // Removes the entry from the tree.
624 void TreeListBox::RemoveEntry (SvTreeListEntry* pEntry)
626 // removing the associated user data
627 delete static_cast<Entry*>(pEntry->GetUserData());
628 // removing the entry
629 GetModel()->Remove( pEntry );
632 // Removes the entry of rDocument.
633 void TreeListBox::RemoveEntry (ScriptDocument const& rDocument)
635 // finding the entry of rDocument
636 for (SvTreeListEntry* pEntry = First(); pEntry; pEntry = Next(pEntry))
637 if (rDocument == GetEntryDescriptor(pEntry).GetDocument())
639 RemoveEntry(pEntry);
640 break;
644 SvTreeListEntry* TreeListBox::CloneEntry( SvTreeListEntry* pSource )
646 SvTreeListEntry* pNew = SvTreeListBox::CloneEntry( pSource );
647 Entry* pUser = static_cast<Entry*>(pSource->GetUserData());
649 DBG_ASSERT( pUser, "User-Daten?!" );
650 DBG_ASSERT( pUser->GetType() != OBJ_TYPE_DOCUMENT, "TreeListBox::CloneEntry: document?!" );
652 Entry* pNewUser = new Entry( *pUser );
653 pNew->SetUserData( pNewUser );
654 return pNew;
657 SvTreeListEntry* TreeListBox::FindEntry( SvTreeListEntry* pParent, const OUString& rText, EntryType eType )
659 sal_uLong nRootPos = 0;
660 SvTreeListEntry* pEntry = pParent ? FirstChild( pParent ) : GetEntry( nRootPos );
661 while ( pEntry )
663 Entry* pBasicEntry = static_cast<Entry*>(pEntry->GetUserData());
664 DBG_ASSERT( pBasicEntry, "FindEntry: no Entry ?!" );
665 if ( ( pBasicEntry->GetType() == eType ) && ( rText.equals(GetEntryText( pEntry )) ) )
666 return pEntry;
668 pEntry = pParent ? NextSibling( pEntry ) : GetEntry( ++nRootPos );
670 return 0;
673 bool TreeListBox::ExpandingHdl()
675 // expanding or collapsing?
676 bool bOK = true;
677 if ( GetModel()->GetDepth( GetHdlEntry() ) == 1 )
679 SvTreeListEntry* pCurEntry = GetCurEntry();
680 EntryDescriptor aDesc( GetEntryDescriptor( pCurEntry ) );
681 ScriptDocument aDocument( aDesc.GetDocument() );
682 OSL_ENSURE( aDocument.isAlive(), "TreeListBox::ExpandingHdl: no document, or document is dead!" );
683 if ( aDocument.isAlive() )
685 OUString aLibName( aDesc.GetLibName() );
686 OUString aLibSubName( aDesc.GetLibSubName() );
687 OUString aName( aDesc.GetName() );
688 OUString aMethodName( aDesc.GetMethodName() );
690 if ( !aLibName.isEmpty() && aLibSubName.isEmpty() && aName.isEmpty() && aMethodName.isEmpty() )
692 // check password, if library is password protected and not verified
693 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
694 if ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) )
696 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
697 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aLibName ) && !xPasswd->isLibraryPasswordVerified( aLibName ) )
699 OUString aPassword;
700 bOK = QueryPassword( xModLibContainer, aLibName, aPassword );
706 return bOK;
709 bool TreeListBox::IsEntryProtected( SvTreeListEntry* pEntry )
711 bool bProtected = false;
712 if ( pEntry && ( GetModel()->GetDepth( pEntry ) == 1 ) )
714 EntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
715 ScriptDocument aDocument( aDesc.GetDocument() );
716 OSL_ENSURE( aDocument.isAlive(), "TreeListBox::IsEntryProtected: no document, or document is dead!" );
717 if ( aDocument.isAlive() )
719 OUString aOULibName( aDesc.GetLibName() );
720 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
721 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
723 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
724 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
726 bProtected = true;
731 return bProtected;
734 SvTreeListEntry* TreeListBox::AddEntry(
735 OUString const& rText,
736 const Image& rImage,
737 SvTreeListEntry* pParent,
738 bool bChildrenOnDemand,
739 std::unique_ptr<Entry> * aUserData
742 assert(aUserData != 0);
743 SvTreeListEntry* p = InsertEntry(
744 rText, rImage, rImage, pParent, bChildrenOnDemand, TREELIST_APPEND,
745 aUserData->get()
747 aUserData->release();
748 return p;
751 void TreeListBox::SetEntryBitmaps( SvTreeListEntry * pEntry, const Image& rImage )
753 SetExpandedEntryBmp( pEntry, rImage );
754 SetCollapsedEntryBmp( pEntry, rImage );
757 LibraryType TreeListBox::GetLibraryType() const
759 LibraryType eType = LIBRARY_TYPE_ALL;
760 if ( ( nMode & BROWSEMODE_MODULES ) && !( nMode & BROWSEMODE_DIALOGS ) )
761 eType = LIBRARY_TYPE_MODULE;
762 else if ( !( nMode & BROWSEMODE_MODULES ) && ( nMode & BROWSEMODE_DIALOGS ) )
763 eType = LIBRARY_TYPE_DIALOG;
764 return eType;
767 OUString TreeListBox::GetRootEntryName( const ScriptDocument& rDocument, LibraryLocation eLocation ) const
769 return rDocument.getTitle( eLocation, GetLibraryType() );
772 void TreeListBox::GetRootEntryBitmaps( const ScriptDocument& rDocument, Image& rImage )
774 OSL_ENSURE( rDocument.isValid(), "TreeListBox::GetRootEntryBitmaps: illegal document!" );
775 if ( !rDocument.isValid() )
776 return;
778 if ( rDocument.isDocument() )
780 OUString sFactoryURL;
781 Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
782 Reference< frame::XModuleManager2 > xModuleManager( frame::ModuleManager::create(xContext) );
785 OUString sModule( xModuleManager->identify( rDocument.getDocument() ) );
786 Reference< container::XNameAccess > xModuleConfig( xModuleManager, UNO_QUERY );
787 if ( xModuleConfig.is() )
789 Sequence< beans::PropertyValue > aModuleDescr;
790 xModuleConfig->getByName( sModule ) >>= aModuleDescr;
791 sal_Int32 nCount = aModuleDescr.getLength();
792 const beans::PropertyValue* pModuleDescr = aModuleDescr.getConstArray();
793 for ( sal_Int32 i = 0; i < nCount; ++i )
795 if ( pModuleDescr[ i ].Name == "ooSetupFactoryEmptyDocumentURL" )
797 pModuleDescr[ i ].Value >>= sFactoryURL;
798 break;
803 catch( const Exception& )
805 DBG_UNHANDLED_EXCEPTION();
808 if ( !sFactoryURL.isEmpty() )
810 rImage = SvFileInformationManager::GetFileImage( INetURLObject( sFactoryURL ), false );
812 else
814 // default icon
815 rImage = Image( IDEResId( RID_IMG_DOCUMENT ) );
818 else
820 rImage = Image( IDEResId( RID_IMG_INSTALLATION ) );
824 void TreeListBox::SetCurrentEntry (EntryDescriptor& rDesc)
826 SvTreeListEntry* pCurEntry = 0;
827 EntryDescriptor aDesc = rDesc;
828 if ( aDesc.GetType() == OBJ_TYPE_UNKNOWN )
830 aDesc = EntryDescriptor(
831 ScriptDocument::getApplicationScriptDocument(),
832 LIBRARY_LOCATION_USER, "Standard",
833 OUString(), ".", OBJ_TYPE_UNKNOWN
836 ScriptDocument aDocument = aDesc.GetDocument();
837 OSL_ENSURE( aDocument.isValid(), "TreeListBox::SetCurrentEntry: invalid document!" );
838 LibraryLocation eLocation = aDesc.GetLocation();
839 SvTreeListEntry* pRootEntry = FindRootEntry( aDocument, eLocation );
840 if ( pRootEntry )
842 pCurEntry = pRootEntry;
843 OUString aLibName( aDesc.GetLibName() );
844 if ( !aLibName.isEmpty() )
846 Expand( pRootEntry );
847 SvTreeListEntry* pLibEntry = FindEntry( pRootEntry, aLibName, OBJ_TYPE_LIBRARY );
848 if ( pLibEntry )
850 pCurEntry = pLibEntry;
851 OUString aLibSubName( aDesc.GetLibSubName() );
852 if( !aLibSubName.isEmpty() )
854 Expand( pLibEntry );
855 SvTreeListEntry* pLibSubEntry = ImpFindEntry( pLibEntry, aLibSubName );
856 if( pLibSubEntry )
858 pCurEntry = pLibSubEntry;
861 OUString aName( aDesc.GetName() );
862 if ( !aName.isEmpty() )
864 Expand( pCurEntry );
865 EntryType eType = OBJ_TYPE_MODULE;
866 if ( aDesc.GetType() == OBJ_TYPE_DIALOG )
867 eType = OBJ_TYPE_DIALOG;
868 SvTreeListEntry* pEntry = FindEntry( pCurEntry, aName, eType );
869 if ( pEntry )
871 pCurEntry = pEntry;
872 OUString aMethodName( aDesc.GetMethodName() );
873 if ( !aMethodName.isEmpty() )
875 Expand( pEntry );
876 SvTreeListEntry* pSubEntry = FindEntry( pEntry, aMethodName, OBJ_TYPE_METHOD );
877 if ( pSubEntry )
879 pCurEntry = pSubEntry;
881 else
883 pSubEntry = FirstChild( pEntry );
884 if ( pSubEntry )
885 pCurEntry = pSubEntry;
889 else
891 pEntry = FirstChild( pLibEntry );
892 if ( pEntry )
893 pCurEntry = pEntry;
897 else
899 pLibEntry = FirstChild( pRootEntry );
900 if ( pLibEntry )
901 pCurEntry = pLibEntry;
905 else
907 pRootEntry = First();
908 if ( pRootEntry )
909 pCurEntry = pRootEntry;
912 SetCurEntry( pCurEntry );
915 void TreeListBox::MouseButtonDown( const MouseEvent& rMEvt )
917 SvTreeListBox::MouseButtonDown( rMEvt );
918 if ( rMEvt.IsLeft() && ( rMEvt.GetClicks() == 2 ) )
920 OpenCurrent();
924 void TreeListBox::KeyInput( const KeyEvent& rEvt )
926 if ( rEvt.GetKeyCode() == KEY_RETURN && OpenCurrent() )
928 return;
930 SvTreeListBox::KeyInput( rEvt );
933 bool TreeListBox::OpenCurrent()
935 EntryDescriptor aDesc = GetEntryDescriptor(GetCurEntry());
936 switch (aDesc.GetType())
938 case OBJ_TYPE_METHOD:
939 case OBJ_TYPE_MODULE:
940 case OBJ_TYPE_DIALOG:
941 if (SfxDispatcher* pDispatcher = GetDispatcher())
943 SbxItem aSbxItem(
944 SID_BASICIDE_ARG_SBX, aDesc.GetDocument(),
945 aDesc.GetLibName(), aDesc.GetName(), aDesc.GetMethodName(),
946 ConvertType(aDesc.GetType())
948 pDispatcher->Execute(
949 SID_BASICIDE_SHOWSBX,
950 SfxCallMode::SYNCHRON, &aSbxItem, 0L
952 return true;
954 break;
956 default:
957 break;
959 return false;
962 } // namespace basctl
964 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */