xref: /AOO41X/main/forms/source/component/EventThread.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir #include "EventThread.hxx"
31*cdf0e10cSrcweir #include <comphelper/guarding.hxx>
32*cdf0e10cSrcweir #include <tools/debug.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir //.........................................................................
35*cdf0e10cSrcweir namespace frm
36*cdf0e10cSrcweir {
37*cdf0e10cSrcweir //.........................................................................
38*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39*cdf0e10cSrcweir using namespace ::com::sun::star::awt;
40*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir DBG_NAME( OComponentEventThread )
43*cdf0e10cSrcweir OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper* pCompImpl ) :
44*cdf0e10cSrcweir 	m_pCompImpl( pCompImpl )
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir 	DBG_CTOR( OComponentEventThread, NULL );
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 	increment(m_refCount);
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 	// Eine Referenz des Controls halten
51*cdf0e10cSrcweir 	{
52*cdf0e10cSrcweir 		InterfaceRef xIFace(static_cast<XWeak*>(pCompImpl));
53*cdf0e10cSrcweir 		query_interface(xIFace, m_xComp);
54*cdf0e10cSrcweir 	}
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	// und uns an dem Control anmelden
57*cdf0e10cSrcweir 	{
58*cdf0e10cSrcweir 		Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
59*cdf0e10cSrcweir 		m_xComp->addEventListener( xEvtLstnr );
60*cdf0e10cSrcweir 	}
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	decrement(m_refCount);
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir OComponentEventThread::~OComponentEventThread()
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	DBG_DTOR( OComponentEventThread, NULL );
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	DBG_ASSERT( m_aEvents.size() == 0,
70*cdf0e10cSrcweir 		"OComponentEventThread::~OComponentEventThread: Kein dispose gerufen?" );
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     impl_clearEventQueue();
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir Any SAL_CALL OComponentEventThread::queryInterface(const Type& _rType) throw (RuntimeException)
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir 	Any aReturn;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	aReturn = OWeakObject::queryInterface(_rType);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	if (!aReturn.hasValue())
82*cdf0e10cSrcweir 		aReturn = ::cppu::queryInterface(_rType,
83*cdf0e10cSrcweir 			static_cast<XEventListener*>(this)
84*cdf0e10cSrcweir 		);
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	return aReturn;
87*cdf0e10cSrcweir }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir void OComponentEventThread::impl_clearEventQueue()
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir 	while ( m_aEvents.size() )
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir 		delete *m_aEvents.begin();
94*cdf0e10cSrcweir         m_aEvents.erase( m_aEvents.begin() );
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir 	m_aControls.erase( m_aControls.begin(), m_aControls.end() );
97*cdf0e10cSrcweir 	m_aFlags.erase( m_aFlags.begin(), m_aFlags.end() );
98*cdf0e10cSrcweir }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir void OComponentEventThread::disposing( const EventObject& evt ) throw ( ::com::sun::star::uno::RuntimeException)
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir 	if( evt.Source == m_xComp )
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir 		::osl::MutexGuard aGuard( m_aMutex );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 		// Event-Listener abmelden
107*cdf0e10cSrcweir 		Reference<XEventListener>  xEvtLstnr = static_cast<XEventListener*>(this);
108*cdf0e10cSrcweir 		m_xComp->removeEventListener( xEvtLstnr );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 		// Event-Queue loeschen
111*cdf0e10cSrcweir         impl_clearEventQueue();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		// Das Control loslassen und pCompImpl auf 0 setzen, damit der
114*cdf0e10cSrcweir 		// Thread weiss, dass er sich beenden soll.
115*cdf0e10cSrcweir 		m_xComp = 0;
116*cdf0e10cSrcweir 		m_pCompImpl = 0;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		// Den Thread aufwecken und beenden.
119*cdf0e10cSrcweir 		m_aCond.set();
120*cdf0e10cSrcweir 		terminate();
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir void OComponentEventThread::addEvent( const EventObject* _pEvt, sal_Bool bFlag )
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	Reference<XControl>  xTmp;
127*cdf0e10cSrcweir 	addEvent( _pEvt, xTmp, bFlag );
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir void OComponentEventThread::addEvent( const EventObject* _pEvt,
131*cdf0e10cSrcweir 								   const Reference<XControl>& rControl,
132*cdf0e10cSrcweir 								   sal_Bool bFlag )
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	// Daten in die Queue stellen
137*cdf0e10cSrcweir 	m_aEvents.push_back( cloneEvent( _pEvt ) );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	Reference<XWeak>		xWeakControl(rControl, UNO_QUERY);
140*cdf0e10cSrcweir 	Reference<XAdapter>	xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
141*cdf0e10cSrcweir 	m_aControls.push_back( xControlAdapter );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	m_aFlags.push_back( bFlag );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	// Thread aufwecken
146*cdf0e10cSrcweir 	m_aCond.set();
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir //---------------------------------------------------------------------
150*cdf0e10cSrcweir //--- 22.08.01 15:48:15 -----------------------------------------------
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir void OComponentEventThread::implStarted( )
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir 	acquire( );
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir //---------------------------------------------------------------------
158*cdf0e10cSrcweir //--- 22.08.01 15:48:16 -----------------------------------------------
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir void OComponentEventThread::implTerminated( )
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir 	release( );
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir //---------------------------------------------------------------------
166*cdf0e10cSrcweir //--- 22.08.01 15:47:31 -----------------------------------------------
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir void SAL_CALL OComponentEventThread::kill()
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir 	OComponentEventThread_TBASE::kill();
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	implTerminated( );
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir //---------------------------------------------------------------------
176*cdf0e10cSrcweir //--- 22.08.01 15:47:33 -----------------------------------------------
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir void SAL_CALL OComponentEventThread::onTerminated()
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir 	OComponentEventThread_TBASE::onTerminated();
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	implTerminated( );
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir void OComponentEventThread::run()
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir 	implStarted( );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 	// uns selbst festhalten, damit wir nicht geloescht werden,
190*cdf0e10cSrcweir 	// wenn zwischendrinne mal ein dispose gerufen wird.
191*cdf0e10cSrcweir 	InterfaceRef xThis(static_cast<XWeak*>(this));
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	do
194*cdf0e10cSrcweir 	{
195*cdf0e10cSrcweir 		::osl::MutexGuard aGuard(m_aMutex);
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 		while( m_aEvents.size() > 0 )
198*cdf0e10cSrcweir 		{
199*cdf0e10cSrcweir 			// Das Control holen und festhalten, damit es waehrend des
200*cdf0e10cSrcweir 			// actionPerformed nicht geloescht werden kann.
201*cdf0e10cSrcweir 			Reference<XComponent>  xComp = m_xComp;
202*cdf0e10cSrcweir 			::cppu::OComponentHelper *pCompImpl = m_pCompImpl;
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir             ThreadEvents::iterator firstEvent( m_aEvents.begin() );
205*cdf0e10cSrcweir 			EventObject* pEvt = *firstEvent;
206*cdf0e10cSrcweir             m_aEvents.erase( firstEvent );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir             ThreadObjects::iterator firstControl( m_aControls.begin() );
209*cdf0e10cSrcweir 			Reference<XAdapter> xControlAdapter = *firstControl;
210*cdf0e10cSrcweir             m_aControls.erase( firstControl );
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir             ThreadBools::iterator firstFlag( m_aFlags.begin() );
213*cdf0e10cSrcweir 			sal_Bool bFlag = *firstFlag;
214*cdf0e10cSrcweir             m_aFlags.erase( firstFlag );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 			{
217*cdf0e10cSrcweir 				MutexRelease aReleaseOnce(m_aMutex);
218*cdf0e10cSrcweir 				// Weil ein queryHardRef eine Exception schmeissen kann sollte
219*cdf0e10cSrcweir 				// es nicht bei gelocktem Mutex aufgerufen werden.
220*cdf0e10cSrcweir 				Reference<XControl>  xControl;
221*cdf0e10cSrcweir 				if ( xControlAdapter.is() )
222*cdf0e10cSrcweir 					query_interface(xControlAdapter->queryAdapted(), xControl);
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 				if( xComp.is() )
225*cdf0e10cSrcweir 					processEvent( pCompImpl, pEvt, xControl, bFlag );
226*cdf0e10cSrcweir 			}
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 			delete pEvt;
229*cdf0e10cSrcweir 		};
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 		// Nach einem dispose kennen wir das Control nicht mehr. Dann darf
232*cdf0e10cSrcweir 		// auch nicht gewartet werden.
233*cdf0e10cSrcweir 		if( !m_xComp.is() )
234*cdf0e10cSrcweir 			return;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 		// Warte-Bedingung zuruecksetzen
237*cdf0e10cSrcweir 		m_aCond.reset();
238*cdf0e10cSrcweir 		{
239*cdf0e10cSrcweir 			MutexRelease aReleaseOnce(m_aMutex);
240*cdf0e10cSrcweir 			// und warten ... falls nicht zwischenzeitlich doch noch ein
241*cdf0e10cSrcweir 			// Event eingetroffen ist.
242*cdf0e10cSrcweir 			m_aCond.wait();
243*cdf0e10cSrcweir 		}
244*cdf0e10cSrcweir 	}
245*cdf0e10cSrcweir 	while( sal_True );
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir //.........................................................................
249*cdf0e10cSrcweir }	// namespace frm
250*cdf0e10cSrcweir //.........................................................................
251*cdf0e10cSrcweir 
252