1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: addinlis.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 #include <tools/debug.hxx>
37 #include <sfx2/objsh.hxx>
40 #include "addinlis.hxx"
41 #include "miscuno.hxx" // SC_IMPL_SERVICE_INFO
42 #include "document.hxx"
44 #include "unoguard.hxx"
47 using namespace com::sun::star
;
49 //------------------------------------------------------------------------
51 //SMART_UNO_IMPLEMENTATION( ScAddInListener, UsrObject );
53 SC_SIMPLE_SERVICE_INFO( ScAddInListener
, "ScAddInListener", "stardiv.one.sheet.AddInListener" )
55 //------------------------------------------------------------------------
57 List
ScAddInListener::aAllListeners
;
59 //------------------------------------------------------------------------
62 ScAddInListener
* ScAddInListener::CreateListener(
63 uno::Reference
<sheet::XVolatileResult
> xVR
, ScDocument
* pDoc
)
65 ScAddInListener
* pNew
= new ScAddInListener( xVR
, pDoc
);
67 pNew
->acquire(); // for aAllListeners
68 aAllListeners
.Insert( pNew
, LIST_APPEND
);
71 xVR
->addResultListener( pNew
); // after at least 1 ref exists!
76 ScAddInListener::ScAddInListener( uno::Reference
<sheet::XVolatileResult
> xVR
, ScDocument
* pDoc
) :
79 pDocs
= new ScAddInDocs( 1, 1 );
80 pDocs
->Insert( pDoc
);
83 ScAddInListener::~ScAddInListener()
89 ScAddInListener
* ScAddInListener::Get( uno::Reference
<sheet::XVolatileResult
> xVR
)
91 sheet::XVolatileResult
* pComp
= xVR
.get();
93 ULONG nCount
= aAllListeners
.Count();
94 for (ULONG nPos
=0; nPos
<nCount
; nPos
++)
96 ScAddInListener
* pLst
= (ScAddInListener
*)aAllListeners
.GetObject(nPos
);
97 if ( pComp
== (sheet::XVolatileResult
*)pLst
->xVolRes
.get() )
100 return NULL
; // not found
103 //! move to some container object?
105 void ScAddInListener::RemoveDocument( ScDocument
* pDocumentP
)
107 ULONG nPos
= aAllListeners
.Count();
110 // loop backwards because elements are removed
112 ScAddInListener
* pLst
= (ScAddInListener
*)aAllListeners
.GetObject(nPos
);
113 ScAddInDocs
* p
= pLst
->pDocs
;
115 if ( p
->Seek_Entry( pDocumentP
, &nFoundPos
) )
117 p
->Remove( nFoundPos
);
118 if ( p
->Count() == 0 )
120 // this AddIn is no longer used
121 // dont delete, just remove the ref for the list
123 aAllListeners
.Remove( nPos
);
125 if ( pLst
->xVolRes
.is() )
126 pLst
->xVolRes
->removeResultListener( pLst
);
128 pLst
->release(); // Ref for aAllListeners - pLst may be deleted here
134 //------------------------------------------------------------------------
138 void SAL_CALL
ScAddInListener::modified( const ::com::sun::star::sheet::ResultEvent
& aEvent
)
139 throw(::com::sun::star::uno::RuntimeException
)
141 ScUnoGuard aGuard
; //! or generate a UserEvent
143 aResult
= aEvent
.Value
; // store result
145 if ( !HasListeners() )
147 //! remove from list and removeListener, as in RemoveDocument ???
150 //! this will crash if called before first StartListening !!!
151 aAllListeners
.Remove( this );
153 xVolRes
->removeResultListener( this );
154 release(); // Ref for aAllListeners - this may be deleted here
159 // notify document of changes
161 Broadcast( ScHint( SC_HINT_DATACHANGED
, ScAddress(), NULL
) );
163 const ScDocument
** ppDoc
= (const ScDocument
**) pDocs
->GetData();
164 USHORT nCount
= pDocs
->Count();
165 for ( USHORT j
=0; j
<nCount
; j
++, ppDoc
++ )
167 ScDocument
* pDoc
= (ScDocument
*)*ppDoc
;
168 pDoc
->TrackFormulas();
169 pDoc
->GetDocumentShell()->Broadcast( SfxSimpleHint( FID_DATACHANGED
) );
170 pDoc
->ResetChanged( ScRange(0,0,0,MAXCOL
,MAXROW
,MAXTAB
) );
176 void SAL_CALL
ScAddInListener::disposing( const ::com::sun::star::lang::EventObject
& /* Source */ )
177 throw(::com::sun::star::uno::RuntimeException
)
179 // hold a ref so this is not deleted at removeResultListener
180 uno::Reference
<sheet::XResultListener
> xRef( this );
184 xVolRes
->removeResultListener( this );
190 //------------------------------------------------------------------------