Resolves: tdf#162093 TableRef item specifier may occur standalone
[LibreOffice.git] / sc / source / core / tool / addinlis.cxx
blobb7548a78dec717ba978be1fcf4c494b6772e572f
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 <sfx2/objsh.hxx>
21 #include <utility>
22 #include <vcl/svapp.hxx>
24 #include <addinlis.hxx>
25 #include <miscuno.hxx>
26 #include <document.hxx>
27 #include <docsh.hxx>
28 #include <brdcst.hxx>
30 #include <com/sun/star/sheet/XVolatileResult.hpp>
32 using namespace com::sun::star;
34 SC_SIMPLE_SERVICE_INFO( ScAddInListener, u"ScAddInListener"_ustr, u"stardiv.one.sheet.AddInListener"_ustr )
36 ::std::vector<rtl::Reference<ScAddInListener>> ScAddInListener::aAllListeners;
38 ScAddInListener* ScAddInListener::CreateListener(
39 const uno::Reference<sheet::XVolatileResult>& xVR, ScDocument* pDoc )
41 rtl::Reference<ScAddInListener> xNew = new ScAddInListener( xVR, pDoc );
43 aAllListeners.push_back( xNew );
45 if ( xVR.is() )
46 xVR->addResultListener( xNew ); // after at least 1 ref exists!
48 return xNew.get();
51 ScAddInListener::ScAddInListener( uno::Reference<sheet::XVolatileResult> xVR, ScDocument* pDoc ) :
52 xVolRes(std::move( xVR )),
53 pDocs( new ScAddInDocs )
55 pDocs->insert( pDoc );
58 ScAddInListener::~ScAddInListener()
62 ScAddInListener* ScAddInListener::Get( const uno::Reference<sheet::XVolatileResult>& xVR )
64 ScAddInListener* pLst = nullptr;
65 sheet::XVolatileResult* pComp = xVR.get();
67 for (auto const& listener : aAllListeners)
69 if ( pComp == listener->xVolRes.get() )
71 pLst = listener.get();
72 break;
75 return pLst;
78 //TODO: move to some container object?
79 void ScAddInListener::RemoveDocument( ScDocument* pDocumentP )
81 auto iter = aAllListeners.begin();
82 while(iter != aAllListeners.end())
84 ScAddInDocs* p = (*iter)->pDocs.get();
85 ScAddInDocs::iterator iter2 = p->find( pDocumentP );
86 if( iter2 != p->end() )
88 p->erase( iter2 );
89 if ( p->empty() )
91 if ( (*iter)->xVolRes.is() )
92 (*iter)->xVolRes->removeResultListener( *iter );
94 iter = aAllListeners.erase( iter );
95 continue;
98 ++iter;
102 // XResultListener
104 void SAL_CALL ScAddInListener::modified( const css::sheet::ResultEvent& aEvent )
106 SolarMutexGuard aGuard; //TODO: or generate a UserEvent
108 aResult = aEvent.Value; // store result
110 // notify document of changes
112 Broadcast( ScHint(SfxHintId::ScDataChanged, ScAddress()) );
114 for (auto const& pDoc : *pDocs)
116 pDoc->TrackFormulas();
117 pDoc->GetDocumentShell()->Broadcast( SfxHint( SfxHintId::ScDataChanged ) );
121 // XEventListener
123 void SAL_CALL ScAddInListener::disposing( const css::lang::EventObject& /* Source */ )
125 // hold a ref so this is not deleted at removeResultListener
126 uno::Reference<sheet::XResultListener> xKeepAlive( this );
128 if ( xVolRes.is() )
130 xVolRes->removeResultListener( this );
131 xVolRes = nullptr;
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */