/src/libreoffice/sc/source/ui/docshell/macromgr.cxx
Line | Count | Source |
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 <macromgr.hxx> |
21 | | #include <document.hxx> |
22 | | #include <docsh.hxx> |
23 | | #include <basic/basmgr.hxx> |
24 | | #include <cppuhelper/implbase.hxx> |
25 | | #include <sfx2/objsh.hxx> |
26 | | #include <formulacell.hxx> |
27 | | #include <config_features.h> |
28 | | #include <vector> |
29 | | #include <com/sun/star/container/XContainer.hpp> |
30 | | #include <com/sun/star/script/XLibraryContainer.hpp> |
31 | | |
32 | | using namespace ::com::sun::star; |
33 | | using ::com::sun::star::uno::Reference; |
34 | | |
35 | | /** |
36 | | * A simple container to keep track of cells that depend on basic modules |
37 | | * changes. We don't check for duplicates at insertion time; instead, we |
38 | | * remove duplicates at query time. |
39 | | */ |
40 | | class ScUserMacroDepTracker |
41 | | { |
42 | | public: |
43 | | void addCell(const OUString& rModuleName, ScFormulaCell* pCell) |
44 | 0 | { |
45 | 0 | ModuleCellMap::iterator itr = maCells.find(rModuleName); |
46 | 0 | if (itr == maCells.end()) |
47 | 0 | { |
48 | 0 | std::pair<ModuleCellMap::iterator, bool> r = maCells.emplace( |
49 | 0 | rModuleName, std::vector<ScFormulaCell*>()); |
50 | |
|
51 | 0 | if (!r.second) |
52 | | // insertion failed. |
53 | 0 | return; |
54 | | |
55 | 0 | itr = r.first; |
56 | 0 | } |
57 | 0 | itr->second.push_back(pCell); |
58 | 0 | } |
59 | | |
60 | | void removeCell(const ScFormulaCell* pCell) |
61 | 76 | { |
62 | 76 | for (auto& rEntry : maCells) |
63 | 0 | { |
64 | 0 | std::erase(rEntry.second, pCell); |
65 | 0 | } |
66 | 76 | } |
67 | | |
68 | | void getCellsByModule(const OUString& rModuleName, std::vector<ScFormulaCell*>& rCells) |
69 | 0 | { |
70 | 0 | ModuleCellMap::iterator itr = maCells.find(rModuleName); |
71 | 0 | if (itr == maCells.end()) |
72 | 0 | return; |
73 | | |
74 | 0 | std::vector<ScFormulaCell*>& rCellList = itr->second; |
75 | | |
76 | | // Remove duplicates. |
77 | 0 | std::sort(rCellList.begin(), rCellList.end()); |
78 | 0 | auto last = std::unique(rCellList.begin(), rCellList.end()); |
79 | 0 | rCellList.erase(last, rCellList.end()); |
80 | | |
81 | | // exception safe copy |
82 | 0 | std::vector<ScFormulaCell*> temp(rCellList); |
83 | 0 | rCells.swap(temp); |
84 | 0 | } |
85 | | |
86 | | private: |
87 | | typedef std::unordered_map<OUString, std::vector<ScFormulaCell*>> ModuleCellMap; |
88 | | ModuleCellMap maCells; |
89 | | }; |
90 | | |
91 | | ScMacroManager::ScMacroManager(ScDocument& rDoc) : |
92 | 28 | mpDepTracker(new ScUserMacroDepTracker), |
93 | 28 | mrDoc(rDoc) |
94 | 28 | { |
95 | 28 | } |
96 | | |
97 | | ScMacroManager::~ScMacroManager() |
98 | 28 | { |
99 | 28 | } |
100 | | |
101 | | typedef ::cppu::WeakImplHelper< css::container::XContainerListener > ContainerListenerHelper; |
102 | | |
103 | | class VBAProjectListener : public ContainerListenerHelper |
104 | | { |
105 | | ScMacroManager* mpMacroMgr; |
106 | | public: |
107 | 0 | explicit VBAProjectListener( ScMacroManager* pMacroMgr ) : mpMacroMgr( pMacroMgr ) {} |
108 | | // XEventListener |
109 | 0 | virtual void SAL_CALL disposing( const lang::EventObject& /*Source*/ ) override {} |
110 | | |
111 | | // XContainerListener |
112 | 0 | virtual void SAL_CALL elementInserted( const container::ContainerEvent& /*Event*/ ) override {} |
113 | | virtual void SAL_CALL elementReplaced( const container::ContainerEvent& Event ) override |
114 | 0 | { |
115 | 0 | OUString sModuleName; |
116 | 0 | Event.Accessor >>= sModuleName; |
117 | 0 | mpMacroMgr->InitUserFuncData(); |
118 | 0 | mpMacroMgr->BroadcastModuleUpdate(sModuleName); |
119 | 0 | } |
120 | 0 | virtual void SAL_CALL elementRemoved( const container::ContainerEvent& /*Event*/ ) override {} |
121 | | |
122 | | }; |
123 | | |
124 | | void ScMacroManager::InitUserFuncData() |
125 | 0 | { |
126 | | // Clear unordered_map |
127 | 0 | mhFuncToVolatile.clear(); |
128 | 0 | OUString sProjectName(u"Standard"_ustr); |
129 | |
|
130 | 0 | Reference< container::XContainer > xModuleContainer; |
131 | 0 | ScDocShell* pShell = mrDoc.GetDocumentShell(); |
132 | 0 | if (!pShell) |
133 | 0 | return; |
134 | | #if HAVE_FEATURE_SCRIPTING |
135 | | const BasicManager *pBasicManager = pShell->GetBasicManager(); |
136 | | if (!pBasicManager->GetName().isEmpty()) |
137 | | { |
138 | | sProjectName = pBasicManager->GetName(); |
139 | | } |
140 | | #endif |
141 | 0 | try |
142 | 0 | { |
143 | 0 | Reference< script::XLibraryContainer > xLibraries( pShell->GetBasicContainer(), uno::UNO_SET_THROW ); |
144 | 0 | xModuleContainer.set( xLibraries->getByName( sProjectName ), uno::UNO_QUERY_THROW ); |
145 | | |
146 | | // remove old listener ( if there was one ) |
147 | 0 | if ( mxContainerListener.is() ) |
148 | 0 | xModuleContainer->removeContainerListener( mxContainerListener ); |
149 | | // Create listener |
150 | 0 | mxContainerListener = new VBAProjectListener( this ); |
151 | 0 | xModuleContainer->addContainerListener( mxContainerListener ); |
152 | 0 | } |
153 | 0 | catch (const uno::Exception&) |
154 | 0 | { |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | | void ScMacroManager::SetUserFuncVolatile( const OUString& sName, bool isVolatile ) |
159 | 0 | { |
160 | 0 | mhFuncToVolatile[ sName ] = isVolatile; |
161 | 0 | } |
162 | | |
163 | | bool ScMacroManager::GetUserFuncVolatile( const OUString& sName ) |
164 | 0 | { |
165 | 0 | NameBoolMap::iterator it = mhFuncToVolatile.find( sName ); |
166 | 0 | if ( it == mhFuncToVolatile.end() ) |
167 | 0 | return false; |
168 | 0 | return it->second; |
169 | 0 | } |
170 | | |
171 | | void ScMacroManager::AddDependentCell(const OUString& aModuleName, ScFormulaCell* pCell) |
172 | 0 | { |
173 | 0 | mpDepTracker->addCell(aModuleName, pCell); |
174 | 0 | } |
175 | | |
176 | | void ScMacroManager::RemoveDependentCell(const ScFormulaCell* pCell) |
177 | 76 | { |
178 | 76 | mpDepTracker->removeCell(pCell); |
179 | 76 | } |
180 | | |
181 | | void ScMacroManager::BroadcastModuleUpdate(const OUString& aModuleName) |
182 | 0 | { |
183 | 0 | std::vector<ScFormulaCell*> aCells; |
184 | 0 | mpDepTracker->getCellsByModule(aModuleName, aCells); |
185 | 0 | for (ScFormulaCell* pCell : aCells) |
186 | 0 | { |
187 | 0 | mrDoc.PutInFormulaTree(pCell); // for F9 recalc |
188 | | |
189 | | // for recalc on cell value change. If the cell is not volatile, the |
190 | | // cell stops listening right away after it gets re-interpreted. |
191 | 0 | mrDoc.StartListeningArea(BCA_LISTEN_ALWAYS, false, pCell); |
192 | 0 | } |
193 | 0 | } |
194 | | |
195 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |