Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / docshell / macromgr.cxx
blob263ed4e617e423352a621ac4c2cc843d86a5eba7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sfx2/objsh.hxx>
26 #include "formulacell.hxx"
27 #include <com/sun/star/container/XContainer.hpp>
29 #include <list>
31 using namespace ::com::sun::star;
32 using ::com::sun::star::uno::RuntimeException;
33 using ::com::sun::star::uno::Reference;
34 using ::boost::unordered_map;
35 using ::std::list;
36 using ::std::for_each;
37 using ::std::pair;
39 /**
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
46 public:
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*>()));
55 if (!r.second)
56 // insertion failed.
57 return;
59 itr = r.first;
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())
75 return;
77 list<ScFormulaCell*>& rCellList = itr->second;
79 // Remove duplicates.
80 rCellList.sort();
81 rCellList.unique();
82 // exception safe copy
83 list<ScFormulaCell*> temp(rCellList);
84 rCells.swap(temp);
87 private:
88 typedef boost::unordered_map<OUString, list<ScFormulaCell*>, OUStringHash> ModuleCellMap;
89 ModuleCellMap maCells;
93 ScMacroManager::ScMacroManager(ScDocument* pDoc) :
94 mpDepTracker(new ScUserMacroDepTracker),
95 mpDoc(pDoc)
99 ScMacroManager::~ScMacroManager()
103 typedef ::cppu::WeakImplHelper1< ::com::sun::star::container::XContainerListener > ContainerListenerHelper;
105 class VBAProjectListener : public ContainerListenerHelper
107 ScMacroManager* mpMacroMgr;
108 public:
109 VBAProjectListener( ScMacroManager* pMacroMgr ) : mpMacroMgr( pMacroMgr ) {}
110 // XEventListener
111 virtual void SAL_CALL disposing( const lang::EventObject& /*Source*/ ) throw(RuntimeException, std::exception) SAL_OVERRIDE {}
113 // XContainerListener
114 virtual void SAL_CALL elementInserted( const container::ContainerEvent& /*Event*/ ) throw(RuntimeException, std::exception) SAL_OVERRIDE {}
115 virtual void SAL_CALL elementReplaced( const container::ContainerEvent& Event ) throw(RuntimeException, std::exception) SAL_OVERRIDE
117 OUString sModuleName;
118 Event.Accessor >>= sModuleName;
119 OSL_TRACE("VBAProjectListener::elementReplaced(%s)", OUStringToOString( sModuleName, RTL_TEXTENCODING_UTF8 ).getStr() );
120 mpMacroMgr->InitUserFuncData();
121 mpMacroMgr->BroadcastModuleUpdate(sModuleName);
123 virtual void SAL_CALL elementRemoved( const container::ContainerEvent& /*Event*/ ) throw(RuntimeException, std::exception) SAL_OVERRIDE {}
127 void ScMacroManager::InitUserFuncData()
129 // Clear boost::unordered_map
130 mhFuncToVolatile.clear();
131 OUString sProjectName("Standard");
133 Reference< container::XContainer > xModuleContainer;
134 SfxObjectShell* pShell = mpDoc->GetDocumentShell();
135 if (pShell && !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 );
149 // Create listener
150 mxContainerListener = new VBAProjectListener( this );
151 xModuleContainer->addContainerListener( mxContainerListener );
154 catch( 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() )
168 return false;
169 return it->second;
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, pCell);
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */