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/implbase1.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
;
36 using ::std::for_each
;
40 * A simple container to keep track of cells that depend on basic modules
41 * changes. We don't check for duplicates at insertion time; instead, we
42 * remove duplicates at query time.
44 class ScUserMacroDepTracker
47 void addCell(const OUString
& rModuleName
, ScFormulaCell
* pCell
)
49 ModuleCellMap::iterator itr
= maCells
.find(rModuleName
);
50 if (itr
== maCells
.end())
52 pair
<ModuleCellMap::iterator
, bool> r
= maCells
.insert(
53 ModuleCellMap::value_type(rModuleName
, list
<ScFormulaCell
*>()));
61 itr
->second
.push_back(pCell
);
64 void removeCell(ScFormulaCell
* pCell
)
66 ModuleCellMap::iterator itr
= maCells
.begin(), itrEnd
= maCells
.end();
67 for (; itr
!= itrEnd
; ++itr
)
68 itr
->second
.remove(pCell
);
71 void getCellsByModule(const OUString
& rModuleName
, list
<ScFormulaCell
*>& rCells
)
73 ModuleCellMap::iterator itr
= maCells
.find(rModuleName
);
74 if (itr
== maCells
.end())
77 list
<ScFormulaCell
*>& rCellList
= itr
->second
;
82 // exception safe copy
83 list
<ScFormulaCell
*> temp(rCellList
);
88 typedef std::unordered_map
<OUString
, list
<ScFormulaCell
*>, OUStringHash
> ModuleCellMap
;
89 ModuleCellMap maCells
;
92 ScMacroManager::ScMacroManager(ScDocument
* pDoc
) :
93 mpDepTracker(new ScUserMacroDepTracker
),
98 ScMacroManager::~ScMacroManager()
102 typedef ::cppu::WeakImplHelper1
< ::com::sun::star::container::XContainerListener
> ContainerListenerHelper
;
104 class VBAProjectListener
: public ContainerListenerHelper
106 ScMacroManager
* mpMacroMgr
;
108 VBAProjectListener( ScMacroManager
* pMacroMgr
) : mpMacroMgr( pMacroMgr
) {}
110 virtual void SAL_CALL
disposing( const lang::EventObject
& /*Source*/ ) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
{}
112 // XContainerListener
113 virtual void SAL_CALL
elementInserted( const container::ContainerEvent
& /*Event*/ ) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
{}
114 virtual void SAL_CALL
elementReplaced( const container::ContainerEvent
& Event
) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
116 OUString sModuleName
;
117 Event
.Accessor
>>= sModuleName
;
118 OSL_TRACE("VBAProjectListener::elementReplaced(%s)", OUStringToOString( sModuleName
, RTL_TEXTENCODING_UTF8
).getStr() );
119 mpMacroMgr
->InitUserFuncData();
120 mpMacroMgr
->BroadcastModuleUpdate(sModuleName
);
122 virtual void SAL_CALL
elementRemoved( const container::ContainerEvent
& /*Event*/ ) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
{}
126 void ScMacroManager::InitUserFuncData()
128 // Clear unordered_map
129 mhFuncToVolatile
.clear();
130 OUString
sProjectName("Standard");
132 Reference
< container::XContainer
> xModuleContainer
;
133 SfxObjectShell
* pShell
= mpDoc
->GetDocumentShell();
136 if (!pShell
->GetBasicManager()->GetName().isEmpty())
138 sProjectName
= pShell
->GetBasicManager()->GetName();
142 Reference
< script::XLibraryContainer
> xLibraries( pShell
->GetBasicContainer(), uno::UNO_QUERY_THROW
);
143 xModuleContainer
.set( xLibraries
->getByName( sProjectName
), uno::UNO_QUERY_THROW
);
145 if ( xModuleContainer
.is() )
147 // remove old listener ( if there was one )
148 if ( mxContainerListener
.is() )
149 xModuleContainer
->removeContainerListener( mxContainerListener
);
151 mxContainerListener
= new VBAProjectListener( this );
152 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(ScFormulaCell
* pCell
)
180 mpDepTracker
->removeCell(pCell
);
183 void ScMacroManager::BroadcastModuleUpdate(const OUString
& aModuleName
)
185 list
<ScFormulaCell
*> aCells
;
186 mpDepTracker
->getCellsByModule(aModuleName
, aCells
);
187 list
<ScFormulaCell
*>::iterator itr
= aCells
.begin(), itrEnd
= aCells
.end();
188 for (; itr
!= itrEnd
; ++itr
)
190 ScFormulaCell
* pCell
= *itr
;
191 mpDoc
->PutInFormulaTree(pCell
); // for F9 recalc
193 // for recalc on cell value change. If the cell is not volatile, the
194 // cell stops listening right away after it gets re-interpreted.
195 mpDoc
->StartListeningArea(BCA_LISTEN_ALWAYS
, false, pCell
);
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */