LibreOffice Module basctl (master) 1
bastype3.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <basic/basmgr.hxx>
21#include <basic/sbmod.hxx>
22#include <basobj.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>
31
32namespace basctl
33{
34
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star;
37
38IMPL_LINK(SbTreeListBox, RequestingChildrenHdl, const weld::TreeIter&, rEntry, bool)
39{
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())
44 return false;
45
46 LibraryLocation eLocation = aDesc.GetLocation();
47 EntryType eType = aDesc.GetType();
48
49 if ( eType == OBJ_TYPE_DOCUMENT )
50 {
51 ImpCreateLibEntries( rEntry, aDocument, eLocation );
52 }
53 else if ( eType == OBJ_TYPE_LIBRARY )
54 {
55 const OUString& aOULibName( aDesc.GetLibName() );
56
57 // check password
58 bool bOK = true;
60 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
61 {
62 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
63 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
64 {
65 OUString aPassword;
66 bOK = QueryPassword(m_pTopLevel, xModLibContainer, aOULibName, aPassword);
67 }
68 }
69
70 if ( bOK )
71 {
72 // load module library
73 bool bModLibLoaded = false;
74 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
75 {
76 if ( !xModLibContainer->isLibraryLoaded( aOULibName ) )
77 {
78 weld::WaitObject aWait(m_pTopLevel);
79 xModLibContainer->loadLibrary( aOULibName );
80 }
81 bModLibLoaded = xModLibContainer->isLibraryLoaded( aOULibName );
82 }
83
84 // load dialog library
85 bool bDlgLibLoaded = false;
87 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
88 {
89 if ( !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
90 {
91 weld::WaitObject aWait(m_pTopLevel);
92 xDlgLibContainer->loadLibrary( aOULibName );
93 }
94 bDlgLibLoaded = xDlgLibContainer->isLibraryLoaded( aOULibName );
95 }
96
97 if ( bModLibLoaded || bDlgLibLoaded )
98 {
99 // create the sub entries
100 ImpCreateLibSubEntries( rEntry, aDocument, aOULibName );
101
102 // exchange image
103 const bool bDlgMode = (nMode & BrowseMode::Dialogs) && !(nMode & BrowseMode::Modules);
104 auto const aImage(bDlgMode ? OUString(RID_BMP_DLGLIB) : OUString(RID_BMP_MODLIB));
105 SetEntryBitmaps(rEntry, aImage);
106 }
107 else
108 {
109 OSL_FAIL( "basctl::TreeListBox::RequestingChildren: Error loading library!" );
110 }
111 }
112 }
117 {
118 const OUString& aLibName( aDesc.GetLibName() );
119 ImpCreateLibSubSubEntriesInVBAMode( rEntry, aDocument, aLibName );
120 }
121
122 return true;
123}
124
126{
127 // instead of always freezing, freeze on the first add/remove, which keeps gtk
128 // from relayouting the tree if it's not necessary
130
133
135 for (auto const& doc : aDocuments)
136 {
137 if ( doc.isAlive() )
139 }
140
142 m_xControl->thaw(); // m_bFreezeOnFirstAddRemove was changed, so control was frozen
143 else
145}
146
148{
149 if ( !pEntry )
150 return nullptr;
151
153 std::unique_ptr<weld::TreeIter> xIter(m_xControl->make_iterator(pEntry));
154 std::vector<std::pair<Entry*, OUString>> aEntries;
155 bool bValidIter = true;
156 do
157 {
158 sal_uInt16 nDepth = m_xControl->get_iter_depth(*xIter);
159 Entry* pBE = weld::fromId<Entry*>(m_xControl->get_id(*xIter));
160 switch (nDepth)
161 {
162 case 4:
163 case 3:
164 case 2:
165 case 1:
166 {
167 aEntries.emplace_back(pBE, m_xControl->get_text(*xIter));
168 }
169 break;
170 case 0:
171 {
172 aDocument = static_cast<DocumentEntry*>(pBE)->GetDocument();
173 }
174 break;
175 }
176 bValidIter = m_xControl->iter_parent(*xIter);
177 } while (bValidIter);
178
179 SbxVariable* pVar = nullptr;
180 if (!aEntries.empty())
181 {
182 std::reverse(aEntries.begin(), aEntries.end());
183 bool bDocumentObjects = false;
184 for (const auto& pair : aEntries)
185 {
186 Entry* pBE = pair.first;
187 assert(pBE && "No data found in entry!");
188 OUString aName(pair.second);
189
190 switch ( pBE->GetType() )
191 {
192 case OBJ_TYPE_LIBRARY:
193 if (BasicManager* pBasMgr = aDocument.getBasicManager())
194 pVar = pBasMgr->GetLib( aName );
195 break;
196 case OBJ_TYPE_MODULE:
197 DBG_ASSERT(dynamic_cast<StarBASIC*>(pVar), "FindVariable: invalid Basic");
198 if(!pVar)
199 {
200 break;
201 }
202 // extract the module name from the string like "Sheet1 (Example1)"
203 if( bDocumentObjects )
204 {
205 aName = aName.getToken( 0, ' ' );
206 }
207 pVar = static_cast<StarBASIC*>(pVar)->FindModule( aName );
208 break;
209 case OBJ_TYPE_METHOD:
210 DBG_ASSERT(dynamic_cast<SbxObject*>(pVar), "FindVariable: invalid module/object");
211 if(!pVar)
212 {
213 break;
214 }
215 pVar = static_cast<SbxObject*>(pVar)->GetMethods()->Find(aName, SbxClassType::Method);
216 break;
217 case OBJ_TYPE_DIALOG:
218 // sbx dialogs removed
219 break;
221 bDocumentObjects = true;
222 [[fallthrough]];
226 // skip, to find the child entry.
227 continue;
228 default:
229 OSL_FAIL( "FindVariable: unknown type" );
230 pVar = nullptr;
231 break;
232 }
233 if ( !pVar )
234 break;
235 }
236 }
237 return pVar;
238}
239
241{
244 OUString aLibName;
245 OUString aLibSubName;
246 OUString aName;
247 OUString aMethodName;
249
250 if ( !pEntry )
251 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
252
253 std::vector<std::pair<Entry*, OUString>> aEntries;
254
255 std::unique_ptr<weld::TreeIter> xIter(m_xControl->make_iterator(pEntry));
256 bool bValidIter = true;
257 do
258 {
259 sal_uInt16 nDepth = m_xControl->get_iter_depth(*xIter);
260 Entry* pBE = weld::fromId<Entry*>(m_xControl->get_id(*xIter));
261 switch (nDepth)
262 {
263 case 4:
264 case 3:
265 case 2:
266 case 1:
267 {
268 aEntries.emplace_back(pBE, m_xControl->get_text(*xIter));
269 }
270 break;
271 case 0:
272 {
273 if (DocumentEntry* pDocumentEntry = static_cast<DocumentEntry*>(pBE))
274 {
275 aDocument = pDocumentEntry->GetDocument();
276 eLocation = pDocumentEntry->GetLocation();
278 }
279 }
280 break;
281 }
282 bValidIter = m_xControl->iter_parent(*xIter);
283 } while (bValidIter);
284
285 if ( !aEntries.empty() )
286 {
287 std::reverse(aEntries.begin(), aEntries.end());
288 for (const auto& pair : aEntries)
289 {
290 Entry* pBE = pair.first;
291 assert(pBE && "No data found in entry!");
292
293 switch ( pBE->GetType() )
294 {
295 case OBJ_TYPE_LIBRARY:
296 {
297 aLibName = pair.second;
298 eType = pBE->GetType();
299 }
300 break;
301 case OBJ_TYPE_MODULE:
302 {
303 aName = pair.second;
304 eType = pBE->GetType();
305 }
306 break;
307 case OBJ_TYPE_METHOD:
308 {
309 aMethodName = pair.second;
310 eType = pBE->GetType();
311 }
312 break;
313 case OBJ_TYPE_DIALOG:
314 {
315 aName = pair.second;
316 eType = pBE->GetType();
317 }
318 break;
323 {
324 aLibSubName = pair.second;
325 eType = pBE->GetType();
326 }
327 break;
328 default:
329 {
330 OSL_FAIL( "GetEntryDescriptor: unknown type" );
332 }
333 break;
334 }
335
336 if ( eType == OBJ_TYPE_UNKNOWN )
337 break;
338 }
339 }
340
341 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
342}
343
345{
346 switch (eType)
347 {
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;
353 default:
354 return static_cast<ItemType>(OBJ_TYPE_UNKNOWN);
355 }
356}
357
359{
360 bool bIsValid = false;
361
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() );
369
370 switch ( eType )
371 {
373 {
374 bIsValid = aDocument.isAlive()
375 && (aDocument.isApplication()
376 || GetRootEntryName(aDocument, eLocation) == m_xControl->get_text(rEntry));
377 }
378 break;
379 case OBJ_TYPE_LIBRARY:
380 {
381 bIsValid = aDocument.hasLibrary( E_SCRIPTS, aLibName ) || aDocument.hasLibrary( E_DIALOGS, aLibName );
382 }
383 break;
384 case OBJ_TYPE_MODULE:
385 {
386 bIsValid = aDocument.hasModule( aLibName, aName );
387 }
388 break;
389 case OBJ_TYPE_DIALOG:
390 {
391 bIsValid = aDocument.hasDialog( aLibName, aName );
392 }
393 break;
394 case OBJ_TYPE_METHOD:
395 {
396 bIsValid = HasMethod( aDocument, aLibName, aName, aMethodName );
397 }
398 break;
403 {
404 bIsValid = true;
405 }
406 break;
407 default: ;
408 }
409
410 return bIsValid;
411}
412
414{
415 return dynamic_cast<SbModule*>(FindVariable(pEntry));
416}
417
419{
420 OSL_ENSURE( rDocument.isValid(), "basctl::TreeListBox::FindRootEntry: invalid document!" );
421 bool bValidIter = m_xControl->get_iter_first(rIter);
422 while (bValidIter)
423 {
424 DocumentEntry* pBDEntry = weld::fromId<DocumentEntry*>(m_xControl->get_id(rIter));
425 if (pBDEntry && pBDEntry->GetDocument() == rDocument && pBDEntry->GetLocation() == eLocation)
426 return true;
427 bValidIter = m_xControl->iter_next_sibling(rIter);
428 }
429 return false;
430}
431
432OUString CreateMgrAndLibStr( std::u16string_view rMgrName, std::u16string_view rLibName )
433{
434 return OUString::Concat("[") + rMgrName + "]." + rLibName;
435}
436
437
438} // namespace basctl
439
440/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScriptDocument aDocument
Definition: basobj2.cxx:194
virtual SbxVariable * Find(const OUString &, SbxClassType)
LibraryLocation GetLocation() const
Definition: bastype2.hxx:100
ScriptDocument const & GetDocument() const
Definition: bastype2.hxx:99
const OUString & GetMethodName() const
Definition: bastype2.hxx:155
LibraryLocation GetLocation() const
Definition: bastype2.hxx:150
ScriptDocument const & GetDocument() const
Definition: bastype2.hxx:149
EntryType GetType() const
Definition: bastype2.hxx:158
const OUString & GetLibName() const
Definition: bastype2.hxx:152
const OUString & GetName() const
Definition: bastype2.hxx:154
EntryType GetType() const
Definition: bastype2.hxx:82
EntryDescriptor GetEntryDescriptor(const weld::TreeIter *pEntry)
Definition: bastype3.cxx:240
bool FindRootEntry(const ScriptDocument &rDocument, LibraryLocation eLocation, weld::TreeIter &rIter)
Definition: bastype3.cxx:418
OUString GetRootEntryName(const ScriptDocument &rDocument, LibraryLocation eLocation) const
Definition: bastype2.cxx:678
std::unique_ptr< weld::TreeView > m_xControl
Definition: bastype2.hxx:177
SbxVariable * FindVariable(const weld::TreeIter *pEntry)
Definition: bastype3.cxx:147
bool IsValidEntry(const weld::TreeIter &rEntry)
Definition: bastype3.cxx:358
void ScanEntry(const ScriptDocument &rDocument, LibraryLocation eLocation)
Definition: bastype2.cxx:186
SbModule * FindModule(const weld::TreeIter *pEntry)
Definition: bastype3.cxx:413
static ItemType ConvertType(EntryType eType)
Definition: bastype3.cxx:344
encapsulates a document which contains Basic scripts and dialogs
css::uno::Reference< css::script::XLibraryContainer > getLibraryContainer(LibraryContainerType _eType) const
returns the Basic or Dialog library container of the document
@ DocumentsSorted
real documents only, sorted lexicographically by their title (using the sys locale's default collator...
static ScriptDocuments getAllScriptDocuments(ScriptDocumentList _eListType)
returns the set of ScriptDocument instances, one for each open document which contains Basic/Dialog c...
static const ScriptDocument & getApplicationScriptDocument()
returns a reference to a shared ScriptDocument instance which operates on the application-wide script...
bool isAlive() const
determines whether the document instance is alive
bool isValid() const
determines whether the document is actually able to contain Basic/Dialog libraries
#define DBG_ASSERT(sCon, aError)
ScXMLEditAttributeMap::Entry const aEntries[]
DocumentType eType
OUString aName
OUString CreateMgrAndLibStr(std::u16string_view rMgrName, std::u16string_view rLibName)
Definition: bastype3.cxx:432
bool HasMethod(ScriptDocument const &rDocument, OUString const &rLibName, OUString const &rModName, OUString const &rMethName)
Definition: basobj2.cxx:402
bool QueryPassword(weld::Widget *pDialogParent, const Reference< script::XLibraryContainer > &xLibContainer, const OUString &rLibName, OUString &rPassword, bool bRepeat, bool bNewTitle)
Definition: bastypes.cxx:779
@ LIBRARY_LOCATION_USER
@ LIBRARY_LOCATION_UNKNOWN
@ LIBRARY_LOCATION_SHARE
@ LIBRARY_LOCATION_DOCUMENT
IMPL_LINK(AccessibleDialogWindow, WindowEventListener, VclWindowEvent &, rEvent, void)
std::vector< ScriptDocument > ScriptDocuments
@ OBJ_TYPE_UNKNOWN
Definition: bastype2.hxx:52
@ OBJ_TYPE_USERFORMS
Definition: bastype2.hxx:59
@ OBJ_TYPE_LIBRARY
Definition: bastype2.hxx:54
@ OBJ_TYPE_NORMAL_MODULES
Definition: bastype2.hxx:60
@ OBJ_TYPE_DIALOG
Definition: bastype2.hxx:56
@ OBJ_TYPE_CLASS_MODULES
Definition: bastype2.hxx:61
@ OBJ_TYPE_DOCUMENT
Definition: bastype2.hxx:53
@ OBJ_TYPE_MODULE
Definition: bastype2.hxx:55
@ OBJ_TYPE_METHOD
Definition: bastype2.hxx:57
@ OBJ_TYPE_DOCUMENT_OBJECTS
Definition: bastype2.hxx:58
ItemType
Definition: sbxitem.hxx:28
@ TYPE_MODULE
Definition: sbxitem.hxx:32
@ TYPE_METHOD
Definition: sbxitem.hxx:34
@ TYPE_LIBRARY
Definition: sbxitem.hxx:31
@ TYPE_SHELL
Definition: sbxitem.hxx:30
@ TYPE_DIALOG
Definition: sbxitem.hxx:33