1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*24acc546SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*24acc546SAndrew Rist * distributed with this work for additional information
6*24acc546SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*24acc546SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*24acc546SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*24acc546SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist * software distributed under the License is distributed on an
15*24acc546SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist * KIND, either express or implied. See the License for the
17*24acc546SAndrew Rist * specific language governing permissions and limitations
18*24acc546SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*24acc546SAndrew Rist *************************************************************/
21*24acc546SAndrew Rist
22*24acc546SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir #include "EventThread.hxx"
27cdf0e10cSrcweir #include <comphelper/guarding.hxx>
28cdf0e10cSrcweir #include <tools/debug.hxx>
29cdf0e10cSrcweir
30cdf0e10cSrcweir //.........................................................................
31cdf0e10cSrcweir namespace frm
32cdf0e10cSrcweir {
33cdf0e10cSrcweir //.........................................................................
34cdf0e10cSrcweir using namespace ::com::sun::star::uno;
35cdf0e10cSrcweir using namespace ::com::sun::star::awt;
36cdf0e10cSrcweir using namespace ::com::sun::star::lang;
37cdf0e10cSrcweir
DBG_NAME(OComponentEventThread)38cdf0e10cSrcweir DBG_NAME( OComponentEventThread )
39cdf0e10cSrcweir OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper* pCompImpl ) :
40cdf0e10cSrcweir m_pCompImpl( pCompImpl )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir DBG_CTOR( OComponentEventThread, NULL );
43cdf0e10cSrcweir
44cdf0e10cSrcweir increment(m_refCount);
45cdf0e10cSrcweir
46cdf0e10cSrcweir // Eine Referenz des Controls halten
47cdf0e10cSrcweir {
48cdf0e10cSrcweir InterfaceRef xIFace(static_cast<XWeak*>(pCompImpl));
49cdf0e10cSrcweir query_interface(xIFace, m_xComp);
50cdf0e10cSrcweir }
51cdf0e10cSrcweir
52cdf0e10cSrcweir // und uns an dem Control anmelden
53cdf0e10cSrcweir {
54cdf0e10cSrcweir Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
55cdf0e10cSrcweir m_xComp->addEventListener( xEvtLstnr );
56cdf0e10cSrcweir }
57cdf0e10cSrcweir
58cdf0e10cSrcweir decrement(m_refCount);
59cdf0e10cSrcweir }
60cdf0e10cSrcweir
~OComponentEventThread()61cdf0e10cSrcweir OComponentEventThread::~OComponentEventThread()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir DBG_DTOR( OComponentEventThread, NULL );
64cdf0e10cSrcweir
65cdf0e10cSrcweir DBG_ASSERT( m_aEvents.size() == 0,
66cdf0e10cSrcweir "OComponentEventThread::~OComponentEventThread: Kein dispose gerufen?" );
67cdf0e10cSrcweir
68cdf0e10cSrcweir impl_clearEventQueue();
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
queryInterface(const Type & _rType)71cdf0e10cSrcweir Any SAL_CALL OComponentEventThread::queryInterface(const Type& _rType) throw (RuntimeException)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir Any aReturn;
74cdf0e10cSrcweir
75cdf0e10cSrcweir aReturn = OWeakObject::queryInterface(_rType);
76cdf0e10cSrcweir
77cdf0e10cSrcweir if (!aReturn.hasValue())
78cdf0e10cSrcweir aReturn = ::cppu::queryInterface(_rType,
79cdf0e10cSrcweir static_cast<XEventListener*>(this)
80cdf0e10cSrcweir );
81cdf0e10cSrcweir
82cdf0e10cSrcweir return aReturn;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
impl_clearEventQueue()85cdf0e10cSrcweir void OComponentEventThread::impl_clearEventQueue()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir while ( m_aEvents.size() )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir delete *m_aEvents.begin();
90cdf0e10cSrcweir m_aEvents.erase( m_aEvents.begin() );
91cdf0e10cSrcweir }
92cdf0e10cSrcweir m_aControls.erase( m_aControls.begin(), m_aControls.end() );
93cdf0e10cSrcweir m_aFlags.erase( m_aFlags.begin(), m_aFlags.end() );
94cdf0e10cSrcweir }
95cdf0e10cSrcweir
disposing(const EventObject & evt)96cdf0e10cSrcweir void OComponentEventThread::disposing( const EventObject& evt ) throw ( ::com::sun::star::uno::RuntimeException)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir if( evt.Source == m_xComp )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex );
101cdf0e10cSrcweir
102cdf0e10cSrcweir // Event-Listener abmelden
103cdf0e10cSrcweir Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
104cdf0e10cSrcweir m_xComp->removeEventListener( xEvtLstnr );
105cdf0e10cSrcweir
106cdf0e10cSrcweir // Event-Queue loeschen
107cdf0e10cSrcweir impl_clearEventQueue();
108cdf0e10cSrcweir
109cdf0e10cSrcweir // Das Control loslassen und pCompImpl auf 0 setzen, damit der
110cdf0e10cSrcweir // Thread weiss, dass er sich beenden soll.
111cdf0e10cSrcweir m_xComp = 0;
112cdf0e10cSrcweir m_pCompImpl = 0;
113cdf0e10cSrcweir
114cdf0e10cSrcweir // Den Thread aufwecken und beenden.
115cdf0e10cSrcweir m_aCond.set();
116cdf0e10cSrcweir terminate();
117cdf0e10cSrcweir }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
addEvent(const EventObject * _pEvt,sal_Bool bFlag)120cdf0e10cSrcweir void OComponentEventThread::addEvent( const EventObject* _pEvt, sal_Bool bFlag )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir Reference<XControl> xTmp;
123cdf0e10cSrcweir addEvent( _pEvt, xTmp, bFlag );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir
addEvent(const EventObject * _pEvt,const Reference<XControl> & rControl,sal_Bool bFlag)126cdf0e10cSrcweir void OComponentEventThread::addEvent( const EventObject* _pEvt,
127cdf0e10cSrcweir const Reference<XControl>& rControl,
128cdf0e10cSrcweir sal_Bool bFlag )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex );
131cdf0e10cSrcweir
132cdf0e10cSrcweir // Daten in die Queue stellen
133cdf0e10cSrcweir m_aEvents.push_back( cloneEvent( _pEvt ) );
134cdf0e10cSrcweir
135cdf0e10cSrcweir Reference<XWeak> xWeakControl(rControl, UNO_QUERY);
136cdf0e10cSrcweir Reference<XAdapter> xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
137cdf0e10cSrcweir m_aControls.push_back( xControlAdapter );
138cdf0e10cSrcweir
139cdf0e10cSrcweir m_aFlags.push_back( bFlag );
140cdf0e10cSrcweir
141cdf0e10cSrcweir // Thread aufwecken
142cdf0e10cSrcweir m_aCond.set();
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir //---------------------------------------------------------------------
146cdf0e10cSrcweir //--- 22.08.01 15:48:15 -----------------------------------------------
147cdf0e10cSrcweir
implStarted()148cdf0e10cSrcweir void OComponentEventThread::implStarted( )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir acquire( );
151cdf0e10cSrcweir }
152cdf0e10cSrcweir
153cdf0e10cSrcweir //---------------------------------------------------------------------
154cdf0e10cSrcweir //--- 22.08.01 15:48:16 -----------------------------------------------
155cdf0e10cSrcweir
implTerminated()156cdf0e10cSrcweir void OComponentEventThread::implTerminated( )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir release( );
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
161cdf0e10cSrcweir //---------------------------------------------------------------------
162cdf0e10cSrcweir //--- 22.08.01 15:47:31 -----------------------------------------------
163cdf0e10cSrcweir
kill()164cdf0e10cSrcweir void SAL_CALL OComponentEventThread::kill()
165cdf0e10cSrcweir {
166cdf0e10cSrcweir OComponentEventThread_TBASE::kill();
167cdf0e10cSrcweir
168cdf0e10cSrcweir implTerminated( );
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
171cdf0e10cSrcweir //---------------------------------------------------------------------
172cdf0e10cSrcweir //--- 22.08.01 15:47:33 -----------------------------------------------
173cdf0e10cSrcweir
onTerminated()174cdf0e10cSrcweir void SAL_CALL OComponentEventThread::onTerminated()
175cdf0e10cSrcweir {
176cdf0e10cSrcweir OComponentEventThread_TBASE::onTerminated();
177cdf0e10cSrcweir
178cdf0e10cSrcweir implTerminated( );
179cdf0e10cSrcweir }
180cdf0e10cSrcweir
run()181cdf0e10cSrcweir void OComponentEventThread::run()
182cdf0e10cSrcweir {
183cdf0e10cSrcweir implStarted( );
184cdf0e10cSrcweir
185cdf0e10cSrcweir // uns selbst festhalten, damit wir nicht geloescht werden,
186cdf0e10cSrcweir // wenn zwischendrinne mal ein dispose gerufen wird.
187cdf0e10cSrcweir InterfaceRef xThis(static_cast<XWeak*>(this));
188cdf0e10cSrcweir
189cdf0e10cSrcweir do
190cdf0e10cSrcweir {
191cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex);
192cdf0e10cSrcweir
193cdf0e10cSrcweir while( m_aEvents.size() > 0 )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir // Das Control holen und festhalten, damit es waehrend des
196cdf0e10cSrcweir // actionPerformed nicht geloescht werden kann.
197cdf0e10cSrcweir Reference<XComponent> xComp = m_xComp;
198cdf0e10cSrcweir ::cppu::OComponentHelper *pCompImpl = m_pCompImpl;
199cdf0e10cSrcweir
200cdf0e10cSrcweir ThreadEvents::iterator firstEvent( m_aEvents.begin() );
201cdf0e10cSrcweir EventObject* pEvt = *firstEvent;
202cdf0e10cSrcweir m_aEvents.erase( firstEvent );
203cdf0e10cSrcweir
204cdf0e10cSrcweir ThreadObjects::iterator firstControl( m_aControls.begin() );
205cdf0e10cSrcweir Reference<XAdapter> xControlAdapter = *firstControl;
206cdf0e10cSrcweir m_aControls.erase( firstControl );
207cdf0e10cSrcweir
208cdf0e10cSrcweir ThreadBools::iterator firstFlag( m_aFlags.begin() );
209cdf0e10cSrcweir sal_Bool bFlag = *firstFlag;
210cdf0e10cSrcweir m_aFlags.erase( firstFlag );
211cdf0e10cSrcweir
212cdf0e10cSrcweir {
213cdf0e10cSrcweir MutexRelease aReleaseOnce(m_aMutex);
214cdf0e10cSrcweir // Weil ein queryHardRef eine Exception schmeissen kann sollte
215cdf0e10cSrcweir // es nicht bei gelocktem Mutex aufgerufen werden.
216cdf0e10cSrcweir Reference<XControl> xControl;
217cdf0e10cSrcweir if ( xControlAdapter.is() )
218cdf0e10cSrcweir query_interface(xControlAdapter->queryAdapted(), xControl);
219cdf0e10cSrcweir
220cdf0e10cSrcweir if( xComp.is() )
221cdf0e10cSrcweir processEvent( pCompImpl, pEvt, xControl, bFlag );
222cdf0e10cSrcweir }
223cdf0e10cSrcweir
224cdf0e10cSrcweir delete pEvt;
225cdf0e10cSrcweir };
226cdf0e10cSrcweir
227cdf0e10cSrcweir // Nach einem dispose kennen wir das Control nicht mehr. Dann darf
228cdf0e10cSrcweir // auch nicht gewartet werden.
229cdf0e10cSrcweir if( !m_xComp.is() )
230cdf0e10cSrcweir return;
231cdf0e10cSrcweir
232cdf0e10cSrcweir // Warte-Bedingung zuruecksetzen
233cdf0e10cSrcweir m_aCond.reset();
234cdf0e10cSrcweir {
235cdf0e10cSrcweir MutexRelease aReleaseOnce(m_aMutex);
236cdf0e10cSrcweir // und warten ... falls nicht zwischenzeitlich doch noch ein
237cdf0e10cSrcweir // Event eingetroffen ist.
238cdf0e10cSrcweir m_aCond.wait();
239cdf0e10cSrcweir }
240cdf0e10cSrcweir }
241cdf0e10cSrcweir while( sal_True );
242cdf0e10cSrcweir }
243cdf0e10cSrcweir
244cdf0e10cSrcweir //.........................................................................
245cdf0e10cSrcweir } // namespace frm
246cdf0e10cSrcweir //.........................................................................
247cdf0e10cSrcweir
248