1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b3f79822SAndrew Rist * distributed with this work for additional information
6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10*b3f79822SAndrew Rist *
11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist *
13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17*b3f79822SAndrew Rist * specific language governing permissions and limitations
18*b3f79822SAndrew Rist * under the License.
19*b3f79822SAndrew Rist *
20*b3f79822SAndrew Rist *************************************************************/
21*b3f79822SAndrew Rist
22*b3f79822SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir #include <sfx2/objsh.hxx>
31cdf0e10cSrcweir
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include "addinlis.hxx"
34cdf0e10cSrcweir #include "miscuno.hxx" // SC_IMPL_SERVICE_INFO
35cdf0e10cSrcweir #include "document.hxx"
36cdf0e10cSrcweir #include "brdcst.hxx"
37cdf0e10cSrcweir #include "unoguard.hxx"
38cdf0e10cSrcweir #include "sc.hrc"
39cdf0e10cSrcweir
40cdf0e10cSrcweir using namespace com::sun::star;
41cdf0e10cSrcweir
42cdf0e10cSrcweir //------------------------------------------------------------------------
43cdf0e10cSrcweir
44cdf0e10cSrcweir //SMART_UNO_IMPLEMENTATION( ScAddInListener, UsrObject );
45cdf0e10cSrcweir
46cdf0e10cSrcweir SC_SIMPLE_SERVICE_INFO( ScAddInListener, "ScAddInListener", "stardiv.one.sheet.AddInListener" )
47cdf0e10cSrcweir
48cdf0e10cSrcweir //------------------------------------------------------------------------
49cdf0e10cSrcweir
50cdf0e10cSrcweir List ScAddInListener::aAllListeners;
51cdf0e10cSrcweir
52cdf0e10cSrcweir //------------------------------------------------------------------------
53cdf0e10cSrcweir
54cdf0e10cSrcweir // static
CreateListener(uno::Reference<sheet::XVolatileResult> xVR,ScDocument * pDoc)55cdf0e10cSrcweir ScAddInListener* ScAddInListener::CreateListener(
56cdf0e10cSrcweir uno::Reference<sheet::XVolatileResult> xVR, ScDocument* pDoc )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir ScAddInListener* pNew = new ScAddInListener( xVR, pDoc );
59cdf0e10cSrcweir
60cdf0e10cSrcweir pNew->acquire(); // for aAllListeners
61cdf0e10cSrcweir aAllListeners.Insert( pNew, LIST_APPEND );
62cdf0e10cSrcweir
63cdf0e10cSrcweir if ( xVR.is() )
64cdf0e10cSrcweir xVR->addResultListener( pNew ); // after at least 1 ref exists!
65cdf0e10cSrcweir
66cdf0e10cSrcweir return pNew;
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
ScAddInListener(uno::Reference<sheet::XVolatileResult> xVR,ScDocument * pDoc)69cdf0e10cSrcweir ScAddInListener::ScAddInListener( uno::Reference<sheet::XVolatileResult> xVR, ScDocument* pDoc ) :
70cdf0e10cSrcweir xVolRes( xVR )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir pDocs = new ScAddInDocs( 1, 1 );
73cdf0e10cSrcweir pDocs->Insert( pDoc );
74cdf0e10cSrcweir }
75cdf0e10cSrcweir
~ScAddInListener()76cdf0e10cSrcweir ScAddInListener::~ScAddInListener()
77cdf0e10cSrcweir {
78cdf0e10cSrcweir delete pDocs;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
81cdf0e10cSrcweir // static
Get(uno::Reference<sheet::XVolatileResult> xVR)82cdf0e10cSrcweir ScAddInListener* ScAddInListener::Get( uno::Reference<sheet::XVolatileResult> xVR )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir sheet::XVolatileResult* pComp = xVR.get();
85cdf0e10cSrcweir
86cdf0e10cSrcweir sal_uLong nCount = aAllListeners.Count();
87cdf0e10cSrcweir for (sal_uLong nPos=0; nPos<nCount; nPos++)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir ScAddInListener* pLst = (ScAddInListener*)aAllListeners.GetObject(nPos);
90cdf0e10cSrcweir if ( pComp == (sheet::XVolatileResult*)pLst->xVolRes.get() )
91cdf0e10cSrcweir return pLst;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir return NULL; // not found
94cdf0e10cSrcweir }
95cdf0e10cSrcweir
96cdf0e10cSrcweir //! move to some container object?
97cdf0e10cSrcweir // static
RemoveDocument(ScDocument * pDocumentP)98cdf0e10cSrcweir void ScAddInListener::RemoveDocument( ScDocument* pDocumentP )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir sal_uLong nPos = aAllListeners.Count();
101cdf0e10cSrcweir while (nPos)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir // loop backwards because elements are removed
104cdf0e10cSrcweir --nPos;
105cdf0e10cSrcweir ScAddInListener* pLst = (ScAddInListener*)aAllListeners.GetObject(nPos);
106cdf0e10cSrcweir ScAddInDocs* p = pLst->pDocs;
107cdf0e10cSrcweir sal_uInt16 nFoundPos;
108cdf0e10cSrcweir if ( p->Seek_Entry( pDocumentP, &nFoundPos ) )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir p->Remove( nFoundPos );
111cdf0e10cSrcweir if ( p->Count() == 0 )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir // this AddIn is no longer used
114cdf0e10cSrcweir // dont delete, just remove the ref for the list
115cdf0e10cSrcweir
116cdf0e10cSrcweir aAllListeners.Remove( nPos );
117cdf0e10cSrcweir
118cdf0e10cSrcweir if ( pLst->xVolRes.is() )
119cdf0e10cSrcweir pLst->xVolRes->removeResultListener( pLst );
120cdf0e10cSrcweir
121cdf0e10cSrcweir pLst->release(); // Ref for aAllListeners - pLst may be deleted here
122cdf0e10cSrcweir }
123cdf0e10cSrcweir }
124cdf0e10cSrcweir }
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir //------------------------------------------------------------------------
128cdf0e10cSrcweir
129cdf0e10cSrcweir // XResultListener
130cdf0e10cSrcweir
modified(const::com::sun::star::sheet::ResultEvent & aEvent)131cdf0e10cSrcweir void SAL_CALL ScAddInListener::modified( const ::com::sun::star::sheet::ResultEvent& aEvent )
132cdf0e10cSrcweir throw(::com::sun::star::uno::RuntimeException)
133cdf0e10cSrcweir {
134cdf0e10cSrcweir ScUnoGuard aGuard; //! or generate a UserEvent
135cdf0e10cSrcweir
136cdf0e10cSrcweir aResult = aEvent.Value; // store result
137cdf0e10cSrcweir
138cdf0e10cSrcweir if ( !HasListeners() )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir //! remove from list and removeListener, as in RemoveDocument ???
141cdf0e10cSrcweir
142cdf0e10cSrcweir #if 0
143cdf0e10cSrcweir //! this will crash if called before first StartListening !!!
144cdf0e10cSrcweir aAllListeners.Remove( this );
145cdf0e10cSrcweir if ( xVolRes.is() )
146cdf0e10cSrcweir xVolRes->removeResultListener( this );
147cdf0e10cSrcweir release(); // Ref for aAllListeners - this may be deleted here
148cdf0e10cSrcweir return;
149cdf0e10cSrcweir #endif
150cdf0e10cSrcweir }
151cdf0e10cSrcweir
152cdf0e10cSrcweir // notify document of changes
153cdf0e10cSrcweir
154cdf0e10cSrcweir Broadcast( ScHint( SC_HINT_DATACHANGED, ScAddress(), NULL ) );
155cdf0e10cSrcweir
156cdf0e10cSrcweir const ScDocument** ppDoc = (const ScDocument**) pDocs->GetData();
157cdf0e10cSrcweir sal_uInt16 nCount = pDocs->Count();
158cdf0e10cSrcweir for ( sal_uInt16 j=0; j<nCount; j++, ppDoc++ )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir ScDocument* pDoc = (ScDocument*)*ppDoc;
161cdf0e10cSrcweir pDoc->TrackFormulas();
162cdf0e10cSrcweir pDoc->GetDocumentShell()->Broadcast( SfxSimpleHint( FID_DATACHANGED ) );
163cdf0e10cSrcweir pDoc->ResetChanged( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB) );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
167cdf0e10cSrcweir // XEventListener
168cdf0e10cSrcweir
disposing(const::com::sun::star::lang::EventObject &)169cdf0e10cSrcweir void SAL_CALL ScAddInListener::disposing( const ::com::sun::star::lang::EventObject& /* Source */ )
170cdf0e10cSrcweir throw(::com::sun::star::uno::RuntimeException)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir // hold a ref so this is not deleted at removeResultListener
173cdf0e10cSrcweir uno::Reference<sheet::XResultListener> xRef( this );
174cdf0e10cSrcweir
175cdf0e10cSrcweir if ( xVolRes.is() )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir xVolRes->removeResultListener( this );
178cdf0e10cSrcweir xVolRes = NULL;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir }
181cdf0e10cSrcweir
182cdf0e10cSrcweir
183cdf0e10cSrcweir //------------------------------------------------------------------------
184cdf0e10cSrcweir
185cdf0e10cSrcweir
186cdf0e10cSrcweir
187