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 <osl/diagnose.h>
26 #include <sfx2/objsh.hxx>
27 #include "formulacell.hxx"
28 #include <com/sun/star/container/XContainer.hpp>
32 using namespace ::com::sun::star
;
33 using ::com::sun::star::uno::RuntimeException
;
34 using ::com::sun::star::uno::Reference
;
39 * A simple container to keep track of cells that depend on basic modules
40 * changes. We don't check for duplicates at insertion time; instead, we
41 * remove duplicates at query time.
43 class ScUserMacroDepTracker
46 void addCell(const OUString
& rModuleName
, ScFormulaCell
* pCell
)
48 ModuleCellMap::iterator itr
= maCells
.find(rModuleName
);
49 if (itr
== maCells
.end())
51 pair
<ModuleCellMap::iterator
, bool> r
= maCells
.insert(
52 ModuleCellMap::value_type(rModuleName
, list
<ScFormulaCell
*>()));
60 itr
->second
.push_back(pCell
);
63 void removeCell(ScFormulaCell
* pCell
)
65 ModuleCellMap::iterator itr
= maCells
.begin(), itrEnd
= maCells
.end();
66 for (; itr
!= itrEnd
; ++itr
)
67 itr
->second
.remove(pCell
);
70 void getCellsByModule(const OUString
& rModuleName
, list
<ScFormulaCell
*>& rCells
)
72 ModuleCellMap::iterator itr
= maCells
.find(rModuleName
);
73 if (itr
== maCells
.end())
76 list
<ScFormulaCell
*>& rCellList
= itr
->second
;
81 // exception safe copy
82 list
<ScFormulaCell
*> temp(rCellList
);
87 typedef std::unordered_map
<OUString
, list
<ScFormulaCell
*>, OUStringHash
> ModuleCellMap
;
88 ModuleCellMap maCells
;
91 ScMacroManager::ScMacroManager(ScDocument
* pDoc
) :
92 mpDepTracker(new ScUserMacroDepTracker
),
97 ScMacroManager::~ScMacroManager()
101 typedef ::cppu::WeakImplHelper
< css::container::XContainerListener
> ContainerListenerHelper
;
103 class VBAProjectListener
: public ContainerListenerHelper
105 ScMacroManager
* mpMacroMgr
;
107 explicit VBAProjectListener( ScMacroManager
* pMacroMgr
) : mpMacroMgr( pMacroMgr
) {}
109 virtual void SAL_CALL
disposing( const lang::EventObject
& /*Source*/ ) throw(RuntimeException
, std::exception
) override
{}
111 // XContainerListener
112 virtual void SAL_CALL
elementInserted( const container::ContainerEvent
& /*Event*/ ) throw(RuntimeException
, std::exception
) override
{}
113 virtual void SAL_CALL
elementReplaced( const container::ContainerEvent
& Event
) throw(RuntimeException
, std::exception
) override
115 OUString sModuleName
;
116 Event
.Accessor
>>= sModuleName
;
117 OSL_TRACE("VBAProjectListener::elementReplaced(%s)", OUStringToOString( sModuleName
, RTL_TEXTENCODING_UTF8
).getStr() );
118 mpMacroMgr
->InitUserFuncData();
119 mpMacroMgr
->BroadcastModuleUpdate(sModuleName
);
121 virtual void SAL_CALL
elementRemoved( const container::ContainerEvent
& /*Event*/ ) throw(RuntimeException
, std::exception
) override
{}
125 void ScMacroManager::InitUserFuncData()
127 // Clear unordered_map
128 mhFuncToVolatile
.clear();
129 OUString
sProjectName("Standard");
131 Reference
< container::XContainer
> xModuleContainer
;
132 SfxObjectShell
* pShell
= mpDoc
->GetDocumentShell();
135 if (!pShell
->GetBasicManager()->GetName().isEmpty())
137 sProjectName
= pShell
->GetBasicManager()->GetName();
141 Reference
< script::XLibraryContainer
> xLibraries( pShell
->GetBasicContainer(), uno::UNO_QUERY_THROW
);
142 xModuleContainer
.set( xLibraries
->getByName( sProjectName
), uno::UNO_QUERY_THROW
);
144 if ( xModuleContainer
.is() )
146 // remove old listener ( if there was one )
147 if ( mxContainerListener
.is() )
148 xModuleContainer
->removeContainerListener( mxContainerListener
);
150 mxContainerListener
= new VBAProjectListener( this );
151 xModuleContainer
->addContainerListener( mxContainerListener
);
154 catch (const uno::Exception
&)
159 void ScMacroManager::SetUserFuncVolatile( const OUString
& sName
, bool isVolatile
)
161 mhFuncToVolatile
[ sName
] = isVolatile
;
164 bool ScMacroManager::GetUserFuncVolatile( const OUString
& sName
)
166 NameBoolMap::iterator it
= mhFuncToVolatile
.find( sName
);
167 if ( it
== mhFuncToVolatile
.end() )
172 void ScMacroManager::AddDependentCell(const OUString
& aModuleName
, ScFormulaCell
* pCell
)
174 mpDepTracker
->addCell(aModuleName
, pCell
);
177 void ScMacroManager::RemoveDependentCell(ScFormulaCell
* pCell
)
179 mpDepTracker
->removeCell(pCell
);
182 void ScMacroManager::BroadcastModuleUpdate(const OUString
& aModuleName
)
184 list
<ScFormulaCell
*> aCells
;
185 mpDepTracker
->getCellsByModule(aModuleName
, aCells
);
186 list
<ScFormulaCell
*>::iterator itr
= aCells
.begin(), itrEnd
= aCells
.end();
187 for (; itr
!= itrEnd
; ++itr
)
189 ScFormulaCell
* pCell
= *itr
;
190 mpDoc
->PutInFormulaTree(pCell
); // for F9 recalc
192 // for recalc on cell value change. If the cell is not volatile, the
193 // cell stops listening right away after it gets re-interpreted.
194 mpDoc
->StartListeningArea(BCA_LISTEN_ALWAYS
, false, pCell
);
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */