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
:
302 case OBJ_TYPE_DIALOG
:
305 eType
= pBE
->GetType();
308 case OBJ_TYPE_METHOD
:
310 aMethodName
= pair
.second
;
311 eType
= pBE
->GetType();
314 case OBJ_TYPE_DOCUMENT_OBJECTS
:
315 case OBJ_TYPE_USERFORMS
:
316 case OBJ_TYPE_NORMAL_MODULES
:
317 case OBJ_TYPE_CLASS_MODULES
:
319 aLibSubName
= pair
.second
;
320 eType
= pBE
->GetType();
325 OSL_FAIL( "GetEntryDescriptor: unknown type" );
326 eType
= OBJ_TYPE_UNKNOWN
;
331 if ( eType
== OBJ_TYPE_UNKNOWN
)
336 return EntryDescriptor(std::move(aDocument
), eLocation
, aLibName
,
337 aLibSubName
, aName
, aMethodName
, eType
);
340 SbxItemType
SbTreeListBox::ConvertType (EntryType eType
)
344 case OBJ_TYPE_DOCUMENT
: return SBX_TYPE_SHELL
;
345 case OBJ_TYPE_LIBRARY
: return SBX_TYPE_LIBRARY
;
346 case OBJ_TYPE_MODULE
: return SBX_TYPE_MODULE
;
347 case OBJ_TYPE_DIALOG
: return SBX_TYPE_DIALOG
;
348 case OBJ_TYPE_METHOD
: return SBX_TYPE_METHOD
;
350 return static_cast<SbxItemType
>(OBJ_TYPE_UNKNOWN
);
354 bool SbTreeListBox::IsValidEntry(const weld::TreeIter
& rEntry
)
356 bool bIsValid
= false;
358 EntryDescriptor
aDesc(GetEntryDescriptor(&rEntry
));
359 const ScriptDocument
& aDocument( aDesc
.GetDocument() );
360 LibraryLocation
eLocation( aDesc
.GetLocation() );
361 const OUString
& aLibName( aDesc
.GetLibName() );
362 const OUString
& aName( aDesc
.GetName() );
363 const OUString
& aMethodName( aDesc
.GetMethodName() );
364 EntryType
eType( aDesc
.GetType() );
368 case OBJ_TYPE_DOCUMENT
:
370 bIsValid
= aDocument
.isAlive()
371 && (aDocument
.isApplication()
372 || GetRootEntryName(aDocument
, eLocation
) == m_xControl
->get_text(rEntry
));
375 case OBJ_TYPE_LIBRARY
:
377 bIsValid
= aDocument
.hasLibrary( E_SCRIPTS
, aLibName
) || aDocument
.hasLibrary( E_DIALOGS
, aLibName
);
380 case OBJ_TYPE_MODULE
:
382 bIsValid
= aDocument
.hasModule( aLibName
, aName
);
385 case OBJ_TYPE_DIALOG
:
387 bIsValid
= aDocument
.hasDialog( aLibName
, aName
);
390 case OBJ_TYPE_METHOD
:
392 bIsValid
= HasMethod( aDocument
, aLibName
, aName
, aMethodName
);
395 case OBJ_TYPE_DOCUMENT_OBJECTS
:
396 case OBJ_TYPE_USERFORMS
:
397 case OBJ_TYPE_NORMAL_MODULES
:
398 case OBJ_TYPE_CLASS_MODULES
:
409 SbModule
* SbTreeListBox::FindModule(const weld::TreeIter
* pEntry
)
411 return dynamic_cast<SbModule
*>(FindVariable(pEntry
));
414 bool SbTreeListBox::FindRootEntry( const ScriptDocument
& rDocument
, LibraryLocation eLocation
, weld::TreeIter
& rIter
)
416 OSL_ENSURE( rDocument
.isValid(), "basctl::TreeListBox::FindRootEntry: invalid document!" );
417 bool bValidIter
= m_xControl
->get_iter_first(rIter
);
420 DocumentEntry
* pBDEntry
= weld::fromId
<DocumentEntry
*>(m_xControl
->get_id(rIter
));
421 if (pBDEntry
&& pBDEntry
->GetDocument() == rDocument
&& pBDEntry
->GetLocation() == eLocation
)
423 bValidIter
= m_xControl
->iter_next_sibling(rIter
);
428 OUString
CreateMgrAndLibStr( std::u16string_view rMgrName
, std::u16string_view rLibName
)
430 return OUString::Concat("[") + rMgrName
+ "]." + rLibName
;
434 } // namespace basctl
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */