1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/accessiblecomponenthelper.hxx>
21 #include <comphelper/accessiblecontexthelper.hxx>
22 #include <osl/diagnose.h>
23 #include <com/sun/star/accessibility/IllegalAccessibleComponentStateException.hpp>
24 #include <comphelper/accessibleeventnotifier.hxx>
25 #include <comphelper/solarmutex.hxx>
32 using namespace ::com::sun::star
;
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::com::sun::star::accessibility
;
37 OCommonAccessibleComponent::OCommonAccessibleComponent( )
38 :OCommonAccessibleComponent_Base( GetMutex() )
44 OCommonAccessibleComponent::~OCommonAccessibleComponent( )
46 // this ensures that the lock, which may be already destroyed as part of the derivee,
47 // is not used anymore
53 void SAL_CALL
OCommonAccessibleComponent::disposing()
55 // rhbz#1001768: de facto this class is locked by SolarMutex;
56 // do not lock m_Mutex because it may cause deadlock
57 osl::Guard
<SolarMutex
> aGuard(SolarMutex::get());
61 AccessibleEventNotifier::revokeClientNotifyDisposing( m_nClientId
, *this );
67 void SAL_CALL
OCommonAccessibleComponent::addAccessibleEventListener( const Reference
< XAccessibleEventListener
>& _rxListener
)
69 osl::Guard
<SolarMutex
> aGuard(SolarMutex::get());
70 // don't use the OContextEntryGuard - it will throw an exception if we're not alive
71 // anymore, while the most recent specification for XComponent states that we should
72 // silently ignore the call in such a situation
75 if ( _rxListener
.is() )
76 _rxListener
->disposing( EventObject( *this ) );
80 if ( _rxListener
.is() )
83 m_nClientId
= AccessibleEventNotifier::registerClient( );
85 AccessibleEventNotifier::addEventListener( m_nClientId
, _rxListener
);
90 void SAL_CALL
OCommonAccessibleComponent::removeAccessibleEventListener( const Reference
< XAccessibleEventListener
>& _rxListener
)
92 osl::Guard
<SolarMutex
> aGuard(SolarMutex::get());
93 // don't use the OContextEntryGuard - it will throw an exception if we're not alive
94 // anymore, while the most recent specification for XComponent states that we should
95 // silently ignore the call in such a situation
99 if ( !(_rxListener
.is() && m_nClientId
) )
102 sal_Int32 nListenerCount
= AccessibleEventNotifier::removeEventListener( m_nClientId
, _rxListener
);
103 if ( !nListenerCount
)
105 // no listeners anymore
106 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
107 // and at least to us not firing any events anymore, in case somebody calls
108 // NotifyAccessibleEvent, again
109 AccessibleEventNotifier::revokeClient( m_nClientId
);
115 void OCommonAccessibleComponent::NotifyAccessibleEvent( const sal_Int16 _nEventId
,
116 const Any
& _rOldValue
, const Any
& _rNewValue
, sal_Int32 nIndexHint
)
119 // if we don't have a client id for the notifier, then we don't have listeners, then
120 // we don't need to notify anything
123 // build an event object
124 AccessibleEventObject aEvent
;
125 aEvent
.Source
= *this;
126 aEvent
.EventId
= _nEventId
;
127 aEvent
.OldValue
= _rOldValue
;
128 aEvent
.NewValue
= _rNewValue
;
129 aEvent
.IndexHint
= nIndexHint
;
131 // let the notifier handle this event
132 AccessibleEventNotifier::addEvent( m_nClientId
, aEvent
);
136 bool OCommonAccessibleComponent::isAlive() const
138 return !rBHelper
.bDisposed
&& !rBHelper
.bInDispose
;
142 void OCommonAccessibleComponent::ensureAlive() const
145 throw DisposedException();
149 void OCommonAccessibleComponent::ensureDisposed( )
151 if ( !rBHelper
.bDisposed
)
153 OSL_ENSURE( 0 == m_refCount
, "OCommonAccessibleComponent::ensureDisposed: this method _has_ to be called from without your dtor only!" );
160 void OCommonAccessibleComponent::lateInit( const Reference
< XAccessible
>& _rxAccessible
)
162 m_aCreator
= _rxAccessible
;
166 Reference
< XAccessible
> OCommonAccessibleComponent::getAccessibleCreator( ) const
172 OUString SAL_CALL
OCommonAccessibleComponent::getAccessibleId( )
178 sal_Int64 SAL_CALL
OCommonAccessibleComponent::getAccessibleIndexInParent( )
180 OExternalLockGuard
aGuard( this );
182 // -1 for child not found/no parent (according to specification)
188 Reference
< XAccessibleContext
> xParentContext( implGetParentContext() );
190 // iterate over parent's children and search for this object
191 if ( xParentContext
.is() )
193 // our own XAccessible for comparing with the children of our parent
194 Reference
< XAccessible
> xCreator( m_aCreator
);
196 OSL_ENSURE( xCreator
.is(), "OCommonAccessibleComponent::getAccessibleIndexInParent: invalid creator!" );
197 // two ideas why this could be NULL:
198 // * nobody called our late ctor (init), so we never had a creator at all -> bad
199 // * the creator is already dead. In this case, we should have been disposed, and
200 // never survived the above OContextEntryGuard.
201 // in all other situations the creator should be non-NULL
205 sal_Int64 nChildCount
= xParentContext
->getAccessibleChildCount();
206 for ( sal_Int64 nChild
= 0; ( nChild
< nChildCount
) && ( -1 == nRet
); ++nChild
)
208 Reference
< XAccessible
> xChild( xParentContext
->getAccessibleChild( nChild
) );
209 if ( xChild
.get() == xCreator
.get() )
215 catch( const Exception
& )
217 OSL_FAIL( "OCommonAccessibleComponent::getAccessibleIndexInParent: caught an exception!" );
224 Locale SAL_CALL
OCommonAccessibleComponent::getLocale( )
226 // simply ask the parent
227 Reference
< XAccessible
> xParent
= getAccessibleParent();
228 Reference
< XAccessibleContext
> xParentContext
;
230 xParentContext
= xParent
->getAccessibleContext();
232 if ( !xParentContext
.is() )
233 throw IllegalAccessibleComponentStateException( OUString(), *this );
235 return xParentContext
->getLocale();
239 Reference
< XAccessibleContext
> OCommonAccessibleComponent::implGetParentContext()
241 Reference
< XAccessible
> xParent
= getAccessibleParent();
242 Reference
< XAccessibleContext
> xParentContext
;
244 xParentContext
= xParent
->getAccessibleContext();
245 return xParentContext
;
249 bool OCommonAccessibleComponent::containsPoint( const awt::Point
& _rPoint
)
251 OExternalLockGuard
aGuard( this );
252 awt::Rectangle
aBounds( implGetBounds() );
253 return ( _rPoint
.X
>= 0 )
254 && ( _rPoint
.Y
>= 0 )
255 && ( _rPoint
.X
< aBounds
.Width
)
256 && ( _rPoint
.Y
< aBounds
.Height
);
260 awt::Point
OCommonAccessibleComponent::getLocation( )
262 OExternalLockGuard
aGuard( this );
263 awt::Rectangle
aBounds( implGetBounds() );
264 return awt::Point( aBounds
.X
, aBounds
.Y
);
268 awt::Point
OCommonAccessibleComponent::getLocationOnScreen( )
270 OExternalLockGuard
aGuard( this );
272 awt::Point
aScreenLoc( 0, 0 );
274 Reference
< XAccessibleComponent
> xParentComponent( implGetParentContext(), UNO_QUERY
);
275 OSL_ENSURE( xParentComponent
.is(), "OCommonAccessibleComponent::getLocationOnScreen: no parent component!" );
276 if ( xParentComponent
.is() )
278 awt::Point
aParentScreenLoc( xParentComponent
->getLocationOnScreen() );
279 awt::Point
aOwnRelativeLoc( getLocation() );
280 aScreenLoc
.X
= aParentScreenLoc
.X
+ aOwnRelativeLoc
.X
;
281 aScreenLoc
.Y
= aParentScreenLoc
.Y
+ aOwnRelativeLoc
.Y
;
288 awt::Size
OCommonAccessibleComponent::getSize( )
290 OExternalLockGuard
aGuard( this );
291 awt::Rectangle
aBounds( implGetBounds() );
292 return awt::Size( aBounds
.Width
, aBounds
.Height
);
296 awt::Rectangle
OCommonAccessibleComponent::getBounds( )
298 OExternalLockGuard
aGuard( this );
299 return implGetBounds();
302 OAccessibleComponentHelper::OAccessibleComponentHelper( )
307 sal_Bool SAL_CALL
OAccessibleComponentHelper::containsPoint( const awt::Point
& _rPoint
)
309 return OCommonAccessibleComponent::containsPoint( _rPoint
);
313 awt::Point SAL_CALL
OAccessibleComponentHelper::getLocation( )
315 return OCommonAccessibleComponent::getLocation( );
319 awt::Point SAL_CALL
OAccessibleComponentHelper::getLocationOnScreen( )
321 return OCommonAccessibleComponent::getLocationOnScreen( );
325 awt::Size SAL_CALL
OAccessibleComponentHelper::getSize( )
327 return OCommonAccessibleComponent::getSize( );
331 awt::Rectangle SAL_CALL
OAccessibleComponentHelper::getBounds( )
333 return OCommonAccessibleComponent::getBounds( );
336 OAccessibleExtendedComponentHelper::OAccessibleExtendedComponentHelper( )
341 sal_Bool SAL_CALL
OAccessibleExtendedComponentHelper::containsPoint( const awt::Point
& _rPoint
)
343 return OCommonAccessibleComponent::containsPoint( _rPoint
);
347 awt::Point SAL_CALL
OAccessibleExtendedComponentHelper::getLocation( )
349 return OCommonAccessibleComponent::getLocation( );
353 awt::Point SAL_CALL
OAccessibleExtendedComponentHelper::getLocationOnScreen( )
355 return OCommonAccessibleComponent::getLocationOnScreen( );
359 awt::Size SAL_CALL
OAccessibleExtendedComponentHelper::getSize( )
361 return OCommonAccessibleComponent::getSize( );
365 awt::Rectangle SAL_CALL
OAccessibleExtendedComponentHelper::getBounds( )
367 return OCommonAccessibleComponent::getBounds( );
371 } // namespace comphelper
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */