Bump version to 24.04.3.4
[LibreOffice.git] / basctl / source / basicide / bastype3.cxx
blobd26ae83252f6d46cf4852a6fcfe2c26949c16840
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 .
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>
32 namespace basctl
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())
44 return false;
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() );
57 // check password
58 bool bOK = true;
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 ) )
65 OUString aPassword;
66 bOK = QueryPassword(m_pTopLevel, xModLibContainer, aOULibName, aPassword);
70 if ( bOK )
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 );
102 // exchange image
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);
107 else
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 );
122 return true;
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)
137 if ( doc.isAlive() )
138 ScanEntry(doc, LIBRARY_LOCATION_DOCUMENT);
141 if (!m_bFreezeOnFirstAddRemove)
142 m_xControl->thaw(); // m_bFreezeOnFirstAddRemove was changed, so control was frozen
143 else
144 m_bFreezeOnFirstAddRemove = false;
147 SbxVariable* SbTreeListBox::FindVariable(const weld::TreeIter* pEntry)
149 if ( !pEntry )
150 return nullptr;
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));
160 switch (nDepth)
162 case 4:
163 case 3:
164 case 2:
165 case 1:
167 aEntries.emplace_back(pBE, m_xControl->get_text(*xIter));
169 break;
170 case 0:
172 aDocument = static_cast<DocumentEntry*>(pBE)->GetDocument();
174 break;
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 );
195 break;
196 case OBJ_TYPE_MODULE:
197 DBG_ASSERT(dynamic_cast<StarBASIC*>(pVar), "FindVariable: invalid Basic");
198 if(!pVar)
200 break;
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 );
208 break;
209 case OBJ_TYPE_METHOD:
210 DBG_ASSERT(dynamic_cast<SbxObject*>(pVar), "FindVariable: invalid module/object");
211 if(!pVar)
213 break;
215 pVar = static_cast<SbxObject*>(pVar)->GetMethods()->Find(aName, SbxClassType::Method);
216 break;
217 case OBJ_TYPE_DIALOG:
218 // sbx dialogs removed
219 break;
220 case OBJ_TYPE_DOCUMENT_OBJECTS:
221 bDocumentObjects = true;
222 [[fallthrough]];
223 case OBJ_TYPE_USERFORMS:
224 case OBJ_TYPE_NORMAL_MODULES:
225 case OBJ_TYPE_CLASS_MODULES:
226 // skip, to find the child entry.
227 continue;
228 default:
229 OSL_FAIL( "FindVariable: unknown type" );
230 pVar = nullptr;
231 break;
233 if ( !pVar )
234 break;
237 return pVar;
240 EntryDescriptor SbTreeListBox::GetEntryDescriptor(const weld::TreeIter* pEntry)
242 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
243 LibraryLocation eLocation = LIBRARY_LOCATION_UNKNOWN;
244 OUString aLibName;
245 OUString aLibSubName;
246 OUString aName;
247 OUString aMethodName;
248 EntryType eType = OBJ_TYPE_UNKNOWN;
250 if ( !pEntry )
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));
261 switch (nDepth)
263 case 4:
264 case 3:
265 case 2:
266 case 1:
268 aEntries.emplace_back(pBE, m_xControl->get_text(*xIter));
270 break;
271 case 0:
273 if (DocumentEntry* pDocumentEntry = static_cast<DocumentEntry*>(pBE))
275 aDocument = pDocumentEntry->GetDocument();
276 eLocation = pDocumentEntry->GetLocation();
277 eType = OBJ_TYPE_DOCUMENT;
280 break;
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();
300 break;
301 case OBJ_TYPE_MODULE:
303 aName = pair.second;
304 eType = pBE->GetType();
306 break;
307 case OBJ_TYPE_METHOD:
309 aMethodName = pair.second;
310 eType = pBE->GetType();
312 break;
313 case OBJ_TYPE_DIALOG:
315 aName = pair.second;
316 eType = pBE->GetType();
318 break;
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();
327 break;
328 default:
330 OSL_FAIL( "GetEntryDescriptor: unknown type" );
331 eType = OBJ_TYPE_UNKNOWN;
333 break;
336 if ( eType == OBJ_TYPE_UNKNOWN )
337 break;
341 return EntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
344 ItemType SbTreeListBox::ConvertType (EntryType eType)
346 switch (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;
353 default:
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() );
370 switch ( eType )
372 case OBJ_TYPE_DOCUMENT:
374 bIsValid = aDocument.isAlive()
375 && (aDocument.isApplication()
376 || GetRootEntryName(aDocument, eLocation) == m_xControl->get_text(rEntry));
378 break;
379 case OBJ_TYPE_LIBRARY:
381 bIsValid = aDocument.hasLibrary( E_SCRIPTS, aLibName ) || aDocument.hasLibrary( E_DIALOGS, aLibName );
383 break;
384 case OBJ_TYPE_MODULE:
386 bIsValid = aDocument.hasModule( aLibName, aName );
388 break;
389 case OBJ_TYPE_DIALOG:
391 bIsValid = aDocument.hasDialog( aLibName, aName );
393 break;
394 case OBJ_TYPE_METHOD:
396 bIsValid = HasMethod( aDocument, aLibName, aName, aMethodName );
398 break;
399 case OBJ_TYPE_DOCUMENT_OBJECTS:
400 case OBJ_TYPE_USERFORMS:
401 case OBJ_TYPE_NORMAL_MODULES:
402 case OBJ_TYPE_CLASS_MODULES:
404 bIsValid = true;
406 break;
407 default: ;
410 return bIsValid;
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);
422 while (bValidIter)
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);
429 return false;
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: */