merge the formfield patch from ooo-build
[ooovba.git] / forms / source / component / EventThread.cxx
blobc92a66d91782a5bc6f0a4417d86056866310dd05
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: EventThread.cxx,v $
10 * $Revision: 1.10 $
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_forms.hxx"
33 #include "EventThread.hxx"
34 #include <comphelper/guarding.hxx>
35 #include <tools/debug.hxx>
37 //.........................................................................
38 namespace frm
40 //.........................................................................
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::awt;
43 using namespace ::com::sun::star::lang;
45 DBG_NAME( OComponentEventThread )
46 OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper* pCompImpl ) :
47 m_pCompImpl( pCompImpl )
49 DBG_CTOR( OComponentEventThread, NULL );
51 increment(m_refCount);
53 // Eine Referenz des Controls halten
55 InterfaceRef xIFace(static_cast<XWeak*>(pCompImpl));
56 query_interface(xIFace, m_xComp);
59 // und uns an dem Control anmelden
61 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
62 m_xComp->addEventListener( xEvtLstnr );
65 decrement(m_refCount);
68 OComponentEventThread::~OComponentEventThread()
70 DBG_DTOR( OComponentEventThread, NULL );
72 DBG_ASSERT( m_aEvents.size() == 0,
73 "OComponentEventThread::~OComponentEventThread: Kein dispose gerufen?" );
75 impl_clearEventQueue();
78 Any SAL_CALL OComponentEventThread::queryInterface(const Type& _rType) throw (RuntimeException)
80 Any aReturn;
82 aReturn = OWeakObject::queryInterface(_rType);
84 if (!aReturn.hasValue())
85 aReturn = ::cppu::queryInterface(_rType,
86 static_cast<XEventListener*>(this)
89 return aReturn;
92 void OComponentEventThread::impl_clearEventQueue()
94 while ( m_aEvents.size() )
96 delete *m_aEvents.begin();
97 m_aEvents.erase( m_aEvents.begin() );
99 m_aControls.erase( m_aControls.begin(), m_aControls.end() );
100 m_aFlags.erase( m_aFlags.begin(), m_aFlags.end() );
103 void OComponentEventThread::disposing( const EventObject& evt ) throw ( ::com::sun::star::uno::RuntimeException)
105 if( evt.Source == m_xComp )
107 ::osl::MutexGuard aGuard( m_aMutex );
109 // Event-Listener abmelden
110 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
111 m_xComp->removeEventListener( xEvtLstnr );
113 // Event-Queue loeschen
114 impl_clearEventQueue();
116 // Das Control loslassen und pCompImpl auf 0 setzen, damit der
117 // Thread weiss, dass er sich beenden soll.
118 m_xComp = 0;
119 m_pCompImpl = 0;
121 // Den Thread aufwecken und beenden.
122 m_aCond.set();
123 terminate();
127 void OComponentEventThread::addEvent( const EventObject* _pEvt, sal_Bool bFlag )
129 Reference<XControl> xTmp;
130 addEvent( _pEvt, xTmp, bFlag );
133 void OComponentEventThread::addEvent( const EventObject* _pEvt,
134 const Reference<XControl>& rControl,
135 sal_Bool bFlag )
137 ::osl::MutexGuard aGuard( m_aMutex );
139 // Daten in die Queue stellen
140 m_aEvents.push_back( cloneEvent( _pEvt ) );
142 Reference<XWeak> xWeakControl(rControl, UNO_QUERY);
143 Reference<XAdapter> xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
144 m_aControls.push_back( xControlAdapter );
146 m_aFlags.push_back( bFlag );
148 // Thread aufwecken
149 m_aCond.set();
152 //---------------------------------------------------------------------
153 //--- 22.08.01 15:48:15 -----------------------------------------------
155 void OComponentEventThread::implStarted( )
157 acquire( );
160 //---------------------------------------------------------------------
161 //--- 22.08.01 15:48:16 -----------------------------------------------
163 void OComponentEventThread::implTerminated( )
165 release( );
168 //---------------------------------------------------------------------
169 //--- 22.08.01 15:47:31 -----------------------------------------------
171 void SAL_CALL OComponentEventThread::kill()
173 OComponentEventThread_TBASE::kill();
175 implTerminated( );
178 //---------------------------------------------------------------------
179 //--- 22.08.01 15:47:33 -----------------------------------------------
181 void SAL_CALL OComponentEventThread::onTerminated()
183 OComponentEventThread_TBASE::onTerminated();
185 implTerminated( );
188 void OComponentEventThread::run()
190 implStarted( );
192 // uns selbst festhalten, damit wir nicht geloescht werden,
193 // wenn zwischendrinne mal ein dispose gerufen wird.
194 InterfaceRef xThis(static_cast<XWeak*>(this));
198 ::osl::MutexGuard aGuard(m_aMutex);
200 while( m_aEvents.size() > 0 )
202 // Das Control holen und festhalten, damit es waehrend des
203 // actionPerformed nicht geloescht werden kann.
204 Reference<XComponent> xComp = m_xComp;
205 ::cppu::OComponentHelper *pCompImpl = m_pCompImpl;
207 ThreadEvents::iterator firstEvent( m_aEvents.begin() );
208 EventObject* pEvt = *firstEvent;
209 m_aEvents.erase( firstEvent );
211 ThreadObjects::iterator firstControl( m_aControls.begin() );
212 Reference<XAdapter> xControlAdapter = *firstControl;
213 m_aControls.erase( firstControl );
215 ThreadBools::iterator firstFlag( m_aFlags.begin() );
216 sal_Bool bFlag = *firstFlag;
217 m_aFlags.erase( firstFlag );
220 MutexRelease aReleaseOnce(m_aMutex);
221 // Weil ein queryHardRef eine Exception schmeissen kann sollte
222 // es nicht bei gelocktem Mutex aufgerufen werden.
223 Reference<XControl> xControl;
224 if ( xControlAdapter.is() )
225 query_interface(xControlAdapter->queryAdapted(), xControl);
227 if( xComp.is() )
228 processEvent( pCompImpl, pEvt, xControl, bFlag );
231 delete pEvt;
234 // Nach einem dispose kennen wir das Control nicht mehr. Dann darf
235 // auch nicht gewartet werden.
236 if( !m_xComp.is() )
237 return;
239 // Warte-Bedingung zuruecksetzen
240 m_aCond.reset();
242 MutexRelease aReleaseOnce(m_aMutex);
243 // und warten ... falls nicht zwischenzeitlich doch noch ein
244 // Event eingetroffen ist.
245 m_aCond.wait();
248 while( sal_True );
251 //.........................................................................
252 } // namespace frm
253 //.........................................................................