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>
23 #include <bastype2.hxx>
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 <string_view>
29 #include <osl/diagnose.h>
30 #include <tools/debug.hxx>
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star
;
38 IMPL_LINK(SbTreeListBox
, RequestingChildrenHdl
, const weld::TreeIter
&, rEntry
, bool)
40 EntryDescriptor aDesc
= GetEntryDescriptor(&rEntry
);
41 const ScriptDocument
& aDocument
= aDesc
.GetDocument();
42 OSL_ENSURE( aDocument
.isAlive(), "basctl::TreeListBox::RequestingChildren: invalid document!" );
43 if (!aDocument
.isAlive())
46 LibraryLocation eLocation
= aDesc
.GetLocation();
47 EntryType eType
= aDesc
.GetType();
49 if ( eType
== OBJ_TYPE_DOCUMENT
)
51 ImpCreateLibEntries( rEntry
, aDocument
, eLocation
);
53 else if ( eType
== OBJ_TYPE_LIBRARY
)
55 const OUString
& aOULibName( aDesc
.GetLibName() );
59 Reference
< script::XLibraryContainer
> xModLibContainer( aDocument
.getLibraryContainer( E_SCRIPTS
) );
60 if ( xModLibContainer
.is() && xModLibContainer
->hasByName( aOULibName
) )
62 Reference
< script::XLibraryContainerPassword
> xPasswd( xModLibContainer
, UNO_QUERY
);
63 if ( xPasswd
.is() && xPasswd
->isLibraryPasswordProtected( aOULibName
) && !xPasswd
->isLibraryPasswordVerified( aOULibName
) )
66 bOK
= QueryPassword(m_pTopLevel
, xModLibContainer
, aOULibName
, aPassword
);
72 // load module library
73 bool bModLibLoaded
= false;
74 if ( xModLibContainer
.is() && xModLibContainer
->hasByName( aOULibName
) )
76 if ( !xModLibContainer
->isLibraryLoaded( aOULibName
) )
78 weld::WaitObject
aWait(m_pTopLevel
);
79 xModLibContainer
->loadLibrary( aOULibName
);
81 bModLibLoaded
= xModLibContainer
->isLibraryLoaded( aOULibName
);
84 // load dialog library
85 bool bDlgLibLoaded
= false;
86 Reference
< script::XLibraryContainer
> xDlgLibContainer
= aDocument
.getLibraryContainer( E_DIALOGS
);
87 if ( xDlgLibContainer
.is() && xDlgLibContainer
->hasByName( aOULibName
) )
89 if ( !xDlgLibContainer
->isLibraryLoaded( aOULibName
) )
91 weld::WaitObject
aWait(m_pTopLevel
);
92 xDlgLibContainer
->loadLibrary( aOULibName
);
94 bDlgLibLoaded
= xDlgLibContainer
->isLibraryLoaded( aOULibName
);
97 if ( bModLibLoaded
|| bDlgLibLoaded
)
99 // create the sub entries
100 ImpCreateLibSubEntries( rEntry
, aDocument
, aOULibName
);
103 const bool bDlgMode
= (nMode
& BrowseMode::Dialogs
) && !(nMode
& BrowseMode::Modules
);
104 auto const aImage(bDlgMode
? RID_BMP_DLGLIB
: RID_BMP_MODLIB
);
105 SetEntryBitmaps(rEntry
, aImage
);
109 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Error loading library!" );
113 else if ( eType
== OBJ_TYPE_DOCUMENT_OBJECTS
114 || eType
== OBJ_TYPE_USERFORMS
115 || eType
== OBJ_TYPE_NORMAL_MODULES
116 || eType
== OBJ_TYPE_CLASS_MODULES
)
118 const OUString
& aLibName( aDesc
.GetLibName() );
119 ImpCreateLibSubSubEntriesInVBAMode( rEntry
, aDocument
, aLibName
);
125 void SbTreeListBox::ScanAllEntries()
127 // instead of always freezing, freeze on the first add/remove, which keeps gtk
128 // from relayouting the tree if it's not necessary
129 m_bFreezeOnFirstAddRemove
= true;
131 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER
);
132 ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE
);
134 ScriptDocuments
aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted
) );
135 for (auto const& doc
: aDocuments
)
138 ScanEntry(doc
, LIBRARY_LOCATION_DOCUMENT
);
141 if (!m_bFreezeOnFirstAddRemove
)
142 m_xControl
->thaw(); // m_bFreezeOnFirstAddRemove was changed, so control was frozen
144 m_bFreezeOnFirstAddRemove
= false;
147 SbxVariable
* SbTreeListBox::FindVariable(const weld::TreeIter
* pEntry
)
152 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
153 std::unique_ptr
<weld::TreeIter
> xIter(m_xControl
->make_iterator(pEntry
));
154 std::vector
<std::pair
<Entry
*, OUString
>> aEntries
;
155 bool bValidIter
= true;
158 sal_uInt16 nDepth
= m_xControl
->get_iter_depth(*xIter
);
159 Entry
* pBE
= weld::fromId
<Entry
*>(m_xControl
->get_id(*xIter
));
167 aEntries
.emplace_back(pBE
, m_xControl
->get_text(*xIter
));
172 aDocument
= static_cast<DocumentEntry
*>(pBE
)->GetDocument();
176 bValidIter
= m_xControl
->iter_parent(*xIter
);
177 } while (bValidIter
);
179 SbxVariable
* pVar
= nullptr;
180 if (!aEntries
.empty())
182 std::reverse(aEntries
.begin(), aEntries
.end());
183 bool bDocumentObjects
= false;
184 for (const auto& pair
: aEntries
)
186 Entry
* pBE
= pair
.first
;
187 assert(pBE
&& "No data found in entry!");
188 OUString
aName(pair
.second
);
190 switch ( pBE
->GetType() )
192 case OBJ_TYPE_LIBRARY
:
193 if (BasicManager
* pBasMgr
= aDocument
.getBasicManager())
194 pVar
= pBasMgr
->GetLib( aName
);
196 case OBJ_TYPE_MODULE
:
197 DBG_ASSERT(dynamic_cast<StarBASIC
*>(pVar
), "FindVariable: invalid Basic");
202 // extract the module name from the string like "Sheet1 (Example1)"
203 if( bDocumentObjects
)
205 aName
= aName
.getToken( 0, ' ' );
207 pVar
= static_cast<StarBASIC
*>(pVar
)->FindModule( aName
);
209 case OBJ_TYPE_METHOD
:
210 DBG_ASSERT(dynamic_cast<SbxObject
*>(pVar
), "FindVariable: invalid module/object");
215 pVar
= static_cast<SbxObject
*>(pVar
)->GetMethods()->Find(aName
, SbxClassType::Method
);
217 case OBJ_TYPE_DIALOG
:
218 // sbx dialogs removed
220 case OBJ_TYPE_DOCUMENT_OBJECTS
:
221 bDocumentObjects
= true;
223 case OBJ_TYPE_USERFORMS
:
224 case OBJ_TYPE_NORMAL_MODULES
:
225 case OBJ_TYPE_CLASS_MODULES
:
226 // skip, to find the child entry.
229 OSL_FAIL( "FindVariable: unknown type" );
240 EntryDescriptor
SbTreeListBox::GetEntryDescriptor(const weld::TreeIter
* pEntry
)
242 ScriptDocument
aDocument( ScriptDocument::getApplicationScriptDocument() );
243 LibraryLocation eLocation
= LIBRARY_LOCATION_UNKNOWN
;
245 OUString aLibSubName
;
247 OUString aMethodName
;
248 EntryType eType
= OBJ_TYPE_UNKNOWN
;
251 return EntryDescriptor( aDocument
, eLocation
, aLibName
, aLibSubName
, aName
, aMethodName
, eType
);
253 std::vector
<std::pair
<Entry
*, OUString
>> aEntries
;
255 std::unique_ptr
<weld::TreeIter
> xIter(m_xControl
->make_iterator(pEntry
));
256 bool bValidIter
= true;
259 sal_uInt16 nDepth
= m_xControl
->get_iter_depth(*xIter
);
260 Entry
* pBE
= weld::fromId
<Entry
*>(m_xControl
->get_id(*xIter
));
268 aEntries
.emplace_back(pBE
, m_xControl
->get_text(*xIter
));
273 if (DocumentEntry
* pDocumentEntry
= static_cast<DocumentEntry
*>(pBE
))
275 aDocument
= pDocumentEntry
->GetDocument();
276 eLocation
= pDocumentEntry
->GetLocation();
277 eType
= OBJ_TYPE_DOCUMENT
;
282 bValidIter
= m_xControl
->iter_parent(*xIter
);
283 } while (bValidIter
);
285 if ( !aEntries
.empty() )
287 std::reverse(aEntries
.begin(), aEntries
.end());
288 for (const auto& pair
: aEntries
)
290 Entry
* pBE
= pair
.first
;
291 assert(pBE
&& "No data found in entry!");
293 switch ( pBE
->GetType() )
295 case OBJ_TYPE_LIBRARY
:
297 aLibName
= pair
.second
;
298 eType
= pBE
->GetType();
301 case OBJ_TYPE_MODULE
:
304 eType
= pBE
->GetType();
307 case OBJ_TYPE_METHOD
:
309 aMethodName
= pair
.second
;
310 eType
= pBE
->GetType();
313 case OBJ_TYPE_DIALOG
:
316 eType
= pBE
->GetType();
319 case OBJ_TYPE_DOCUMENT_OBJECTS
:
320 case OBJ_TYPE_USERFORMS
:
321 case OBJ_TYPE_NORMAL_MODULES
:
322 case OBJ_TYPE_CLASS_MODULES
:
324 aLibSubName
= pair
.second
;
325 eType
= pBE
->GetType();
330 OSL_FAIL( "GetEntryDescriptor: unknown type" );
331 eType
= OBJ_TYPE_UNKNOWN
;
336 if ( eType
== OBJ_TYPE_UNKNOWN
)
341 return EntryDescriptor( aDocument
, eLocation
, aLibName
, aLibSubName
, aName
, aMethodName
, eType
);
344 ItemType
SbTreeListBox::ConvertType (EntryType eType
)
348 case OBJ_TYPE_DOCUMENT
: return TYPE_SHELL
;
349 case OBJ_TYPE_LIBRARY
: return TYPE_LIBRARY
;
350 case OBJ_TYPE_MODULE
: return TYPE_MODULE
;
351 case OBJ_TYPE_DIALOG
: return TYPE_DIALOG
;
352 case OBJ_TYPE_METHOD
: return TYPE_METHOD
;
354 return static_cast<ItemType
>(OBJ_TYPE_UNKNOWN
);
358 bool SbTreeListBox::IsValidEntry(const weld::TreeIter
& rEntry
)
360 bool bIsValid
= false;
362 EntryDescriptor
aDesc(GetEntryDescriptor(&rEntry
));
363 const ScriptDocument
& aDocument( aDesc
.GetDocument() );
364 LibraryLocation
eLocation( aDesc
.GetLocation() );
365 const OUString
& aLibName( aDesc
.GetLibName() );
366 const OUString
& aName( aDesc
.GetName() );
367 const OUString
& aMethodName( aDesc
.GetMethodName() );
368 EntryType
eType( aDesc
.GetType() );
372 case OBJ_TYPE_DOCUMENT
:
374 bIsValid
= aDocument
.isAlive()
375 && (aDocument
.isApplication()
376 || GetRootEntryName(aDocument
, eLocation
) == m_xControl
->get_text(rEntry
));
379 case OBJ_TYPE_LIBRARY
:
381 bIsValid
= aDocument
.hasLibrary( E_SCRIPTS
, aLibName
) || aDocument
.hasLibrary( E_DIALOGS
, aLibName
);
384 case OBJ_TYPE_MODULE
:
386 bIsValid
= aDocument
.hasModule( aLibName
, aName
);
389 case OBJ_TYPE_DIALOG
:
391 bIsValid
= aDocument
.hasDialog( aLibName
, aName
);
394 case OBJ_TYPE_METHOD
:
396 bIsValid
= HasMethod( aDocument
, aLibName
, aName
, aMethodName
);
399 case OBJ_TYPE_DOCUMENT_OBJECTS
:
400 case OBJ_TYPE_USERFORMS
:
401 case OBJ_TYPE_NORMAL_MODULES
:
402 case OBJ_TYPE_CLASS_MODULES
:
413 SbModule
* SbTreeListBox::FindModule(const weld::TreeIter
* pEntry
)
415 return dynamic_cast<SbModule
*>(FindVariable(pEntry
));
418 bool SbTreeListBox::FindRootEntry( const ScriptDocument
& rDocument
, LibraryLocation eLocation
, weld::TreeIter
& rIter
)
420 OSL_ENSURE( rDocument
.isValid(), "basctl::TreeListBox::FindRootEntry: invalid document!" );
421 bool bValidIter
= m_xControl
->get_iter_first(rIter
);
424 DocumentEntry
* pBDEntry
= weld::fromId
<DocumentEntry
*>(m_xControl
->get_id(rIter
));
425 if (pBDEntry
&& pBDEntry
->GetDocument() == rDocument
&& pBDEntry
->GetLocation() == eLocation
)
427 bValidIter
= m_xControl
->iter_next_sibling(rIter
);
432 OUString
CreateMgrAndLibStr( std::u16string_view rMgrName
, std::u16string_view rLibName
)
434 return OUString::Concat("[") + rMgrName
+ "]." + rLibName
;
438 } // namespace basctl
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */