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