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 <macromgr.hxx>
21 #include <document.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>
29 #include <com/sun/star/container/XContainer.hpp>
30 #include <com/sun/star/script/XLibraryContainer.hpp>
32 using namespace ::com::sun::star
;
33 using ::com::sun::star::uno::Reference
;
38 * A simple container to keep track of cells that depend on basic modules
39 * changes. We don't check for duplicates at insertion time; instead, we
40 * remove duplicates at query time.
42 class ScUserMacroDepTracker
45 void addCell(const OUString
& rModuleName
, ScFormulaCell
* pCell
)
47 ModuleCellMap::iterator itr
= maCells
.find(rModuleName
);
48 if (itr
== maCells
.end())
50 pair
<ModuleCellMap::iterator
, bool> r
= maCells
.emplace(
51 rModuleName
, vector
<ScFormulaCell
*>());
59 itr
->second
.push_back(pCell
);
62 void removeCell(const ScFormulaCell
* pCell
)
64 for (auto& rEntry
: maCells
)
66 std::erase(rEntry
.second
, pCell
);
70 void getCellsByModule(const OUString
& rModuleName
, vector
<ScFormulaCell
*>& rCells
)
72 ModuleCellMap::iterator itr
= maCells
.find(rModuleName
);
73 if (itr
== maCells
.end())
76 vector
<ScFormulaCell
*>& rCellList
= itr
->second
;
79 std::sort(rCellList
.begin(), rCellList
.end());
80 auto last
= std::unique(rCellList
.begin(), rCellList
.end());
81 rCellList
.erase(last
, rCellList
.end());
83 // exception safe copy
84 vector
<ScFormulaCell
*> temp(rCellList
);
89 typedef std::unordered_map
<OUString
, vector
<ScFormulaCell
*>> ModuleCellMap
;
90 ModuleCellMap maCells
;
93 ScMacroManager::ScMacroManager(ScDocument
& rDoc
) :
94 mpDepTracker(new ScUserMacroDepTracker
),
99 ScMacroManager::~ScMacroManager()
103 typedef ::cppu::WeakImplHelper
< css::container::XContainerListener
> ContainerListenerHelper
;
105 class VBAProjectListener
: public ContainerListenerHelper
107 ScMacroManager
* mpMacroMgr
;
109 explicit VBAProjectListener( ScMacroManager
* pMacroMgr
) : mpMacroMgr( pMacroMgr
) {}
111 virtual void SAL_CALL
disposing( const lang::EventObject
& /*Source*/ ) override
{}
113 // XContainerListener
114 virtual void SAL_CALL
elementInserted( const container::ContainerEvent
& /*Event*/ ) override
{}
115 virtual void SAL_CALL
elementReplaced( const container::ContainerEvent
& Event
) override
117 OUString sModuleName
;
118 Event
.Accessor
>>= sModuleName
;
119 mpMacroMgr
->InitUserFuncData();
120 mpMacroMgr
->BroadcastModuleUpdate(sModuleName
);
122 virtual void SAL_CALL
elementRemoved( const container::ContainerEvent
& /*Event*/ ) override
{}
126 void ScMacroManager::InitUserFuncData()
128 // Clear unordered_map
129 mhFuncToVolatile
.clear();
130 OUString
sProjectName(u
"Standard"_ustr
);
132 Reference
< container::XContainer
> xModuleContainer
;
133 ScDocShell
* pShell
= mrDoc
.GetDocumentShell();
136 #if HAVE_FEATURE_SCRIPTING
137 const BasicManager
*pBasicManager
= pShell
->GetBasicManager();
138 if (!pBasicManager
->GetName().isEmpty())
140 sProjectName
= pBasicManager
->GetName();
145 Reference
< script::XLibraryContainer
> xLibraries( pShell
->GetBasicContainer(), uno::UNO_SET_THROW
);
146 xModuleContainer
.set( xLibraries
->getByName( sProjectName
), uno::UNO_QUERY_THROW
);
148 // remove old listener ( if there was one )
149 if ( mxContainerListener
.is() )
150 xModuleContainer
->removeContainerListener( mxContainerListener
);
152 mxContainerListener
= new VBAProjectListener( this );
153 xModuleContainer
->addContainerListener( mxContainerListener
);
155 catch (const uno::Exception
&)
160 void ScMacroManager::SetUserFuncVolatile( const OUString
& sName
, bool isVolatile
)
162 mhFuncToVolatile
[ sName
] = isVolatile
;
165 bool ScMacroManager::GetUserFuncVolatile( const OUString
& sName
)
167 NameBoolMap::iterator it
= mhFuncToVolatile
.find( sName
);
168 if ( it
== mhFuncToVolatile
.end() )
173 void ScMacroManager::AddDependentCell(const OUString
& aModuleName
, ScFormulaCell
* pCell
)
175 mpDepTracker
->addCell(aModuleName
, pCell
);
178 void ScMacroManager::RemoveDependentCell(const ScFormulaCell
* pCell
)
180 mpDepTracker
->removeCell(pCell
);
183 void ScMacroManager::BroadcastModuleUpdate(const OUString
& aModuleName
)
185 vector
<ScFormulaCell
*> aCells
;
186 mpDepTracker
->getCellsByModule(aModuleName
, aCells
);
187 for (ScFormulaCell
* pCell
: aCells
)
189 mrDoc
.PutInFormulaTree(pCell
); // for F9 recalc
191 // for recalc on cell value change. If the cell is not volatile, the
192 // cell stops listening right away after it gets re-interpreted.
193 mrDoc
.StartListeningArea(BCA_LISTEN_ALWAYS
, false, pCell
);
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */