Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / extended / AccessibleBrowseBoxBase.cxx
blob7427feac6b9a496a9a2df17e42f6e18b86a1ff1e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "accessibility/extended/AccessibleBrowseBoxBase.hxx"
21 #include <svtools/accessibletableprovider.hxx>
22 #include <comphelper/servicehelper.hxx>
23 #include <cppuhelper/supportsservice.hxx>
25 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
26 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
27 #include <unotools/accessiblerelationsethelper.hxx>
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Sequence;
33 using ::com::sun::star::uno::Any;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::accessibility;
37 using namespace ::comphelper;
38 using namespace ::svt;
43 namespace accessibility {
45 using namespace com::sun::star::accessibility::AccessibleStateType;
48 // Ctor/Dtor/disposing --------------------------------------------------------
50 AccessibleBrowseBoxBase::AccessibleBrowseBoxBase(
51 const Reference< XAccessible >& rxParent,
52 IAccessibleTableProvider& rBrowseBox,
53 const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >& _xFocusWindow,
54 AccessibleBrowseBoxObjType eObjType ) :
55 AccessibleBrowseBoxImplHelper( m_aMutex ),
56 mxParent( rxParent ),
57 mpBrowseBox( &rBrowseBox ),
58 m_xFocusWindow(_xFocusWindow),
59 maName( rBrowseBox.GetAccessibleObjectName( eObjType ) ),
60 maDescription( rBrowseBox.GetAccessibleObjectDescription( eObjType ) ),
61 meObjType( eObjType ),
62 m_aClientId(0)
64 if ( m_xFocusWindow.is() )
65 m_xFocusWindow->addFocusListener( this );
68 AccessibleBrowseBoxBase::AccessibleBrowseBoxBase(
69 const Reference< XAccessible >& rxParent,
70 IAccessibleTableProvider& rBrowseBox,
71 const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >& _xFocusWindow,
72 AccessibleBrowseBoxObjType eObjType,
73 const OUString& rName,
74 const OUString& rDescription ) :
75 AccessibleBrowseBoxImplHelper( m_aMutex ),
76 mxParent( rxParent ),
77 mpBrowseBox( &rBrowseBox ),
78 m_xFocusWindow(_xFocusWindow),
79 maName( rName ),
80 maDescription( rDescription ),
81 meObjType( eObjType ),
82 m_aClientId(0)
84 if ( m_xFocusWindow.is() )
85 m_xFocusWindow->addFocusListener( this );
88 AccessibleBrowseBoxBase::~AccessibleBrowseBoxBase()
90 if( isAlive() )
92 // increment ref count to prevent double call of Dtor
93 osl_atomic_increment( &m_refCount );
94 dispose();
98 void SAL_CALL AccessibleBrowseBoxBase::disposing()
100 ::osl::MutexGuard aGuard( getOslMutex() );
101 if ( m_xFocusWindow.is() )
103 SolarMutexGuard aSolarGuard;
104 m_xFocusWindow->removeFocusListener( this );
107 if ( getClientId( ) )
109 AccessibleEventNotifier::TClientId nId( getClientId( ) );
110 setClientId( 0 );
111 AccessibleEventNotifier::revokeClientNotifyDisposing( nId, *this );
114 mxParent = NULL;
115 mpBrowseBox = NULL;
118 // XAccessibleContext ---------------------------------------------------------
120 Reference< XAccessible > SAL_CALL AccessibleBrowseBoxBase::getAccessibleParent()
121 throw ( uno::RuntimeException, std::exception )
123 ::osl::MutexGuard aGuard( getOslMutex() );
124 ensureIsAlive();
125 return mxParent;
128 sal_Int32 SAL_CALL AccessibleBrowseBoxBase::getAccessibleIndexInParent()
129 throw ( uno::RuntimeException, std::exception )
131 ::osl::MutexGuard aGuard( getOslMutex() );
132 ensureIsAlive();
134 // -1 for child not found/no parent (according to specification)
135 sal_Int32 nRet = -1;
137 Reference< uno::XInterface > xMeMyselfAndI( static_cast< XAccessibleContext* >( this ), uno::UNO_QUERY );
139 // iterate over parent's children and search for this object
140 if( mxParent.is() )
142 Reference< XAccessibleContext >
143 xParentContext( mxParent->getAccessibleContext() );
144 if( xParentContext.is() )
146 Reference< uno::XInterface > xChild;
148 sal_Int32 nChildCount = xParentContext->getAccessibleChildCount();
149 for( sal_Int32 nChild = 0; nChild < nChildCount; ++nChild )
151 xChild.set(xParentContext->getAccessibleChild( nChild ), css::uno::UNO_QUERY);
153 if ( xMeMyselfAndI.get() == xChild.get() )
155 nRet = nChild;
156 break;
161 return nRet;
164 OUString SAL_CALL AccessibleBrowseBoxBase::getAccessibleDescription()
165 throw ( uno::RuntimeException, std::exception )
167 ::osl::MutexGuard aGuard( getOslMutex() );
168 ensureIsAlive();
169 return maDescription;
172 OUString SAL_CALL AccessibleBrowseBoxBase::getAccessibleName()
173 throw ( uno::RuntimeException, std::exception )
175 ::osl::MutexGuard aGuard( getOslMutex() );
176 ensureIsAlive();
177 return maName;
180 Reference< XAccessibleRelationSet > SAL_CALL
181 AccessibleBrowseBoxBase::getAccessibleRelationSet()
182 throw ( uno::RuntimeException, std::exception )
184 ensureIsAlive();
185 // BrowseBox does not have relations.
186 return new utl::AccessibleRelationSetHelper;
189 Reference< XAccessibleStateSet > SAL_CALL
190 AccessibleBrowseBoxBase::getAccessibleStateSet()
191 throw ( uno::RuntimeException, std::exception )
193 SolarMutexGuard aSolarGuard;
194 ::osl::MutexGuard aGuard( getOslMutex() );
195 // don't check whether alive -> StateSet may contain DEFUNC
196 return implCreateStateSetHelper();
199 lang::Locale SAL_CALL AccessibleBrowseBoxBase::getLocale()
200 throw ( IllegalAccessibleComponentStateException, uno::RuntimeException, std::exception )
202 ::osl::MutexGuard aGuard( getOslMutex() );
203 ensureIsAlive();
204 if( mxParent.is() )
206 Reference< XAccessibleContext >
207 xParentContext( mxParent->getAccessibleContext() );
208 if( xParentContext.is() )
209 return xParentContext->getLocale();
211 throw IllegalAccessibleComponentStateException();
214 // XAccessibleComponent -------------------------------------------------------
216 sal_Bool SAL_CALL AccessibleBrowseBoxBase::containsPoint( const awt::Point& rPoint )
217 throw ( uno::RuntimeException, std::exception )
219 return Rectangle( Point(), getBoundingBox().GetSize() ).IsInside( VCLPoint( rPoint ) );
222 awt::Rectangle SAL_CALL AccessibleBrowseBoxBase::getBounds()
223 throw ( uno::RuntimeException, std::exception )
225 return AWTRectangle( getBoundingBox() );
228 awt::Point SAL_CALL AccessibleBrowseBoxBase::getLocation()
229 throw ( uno::RuntimeException, std::exception )
231 return AWTPoint( getBoundingBox().TopLeft() );
234 awt::Point SAL_CALL AccessibleBrowseBoxBase::getLocationOnScreen()
235 throw ( uno::RuntimeException, std::exception )
237 return AWTPoint( getBoundingBoxOnScreen().TopLeft() );
240 awt::Size SAL_CALL AccessibleBrowseBoxBase::getSize()
241 throw ( uno::RuntimeException, std::exception )
243 return AWTSize( getBoundingBox().GetSize() );
246 void SAL_CALL AccessibleBrowseBoxBase::focusGained( const ::com::sun::star::awt::FocusEvent& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
248 com::sun::star::uno::Any aFocused;
249 com::sun::star::uno::Any aEmpty;
250 aFocused <<= FOCUSED;
252 commitEvent(AccessibleEventId::STATE_CHANGED,aFocused,aEmpty);
256 void SAL_CALL AccessibleBrowseBoxBase::focusLost( const ::com::sun::star::awt::FocusEvent& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
258 com::sun::star::uno::Any aFocused;
259 com::sun::star::uno::Any aEmpty;
260 aFocused <<= FOCUSED;
262 commitEvent(AccessibleEventId::STATE_CHANGED,aEmpty,aFocused);
264 // XAccessibleEventBroadcaster ------------------------------------------------
266 void SAL_CALL AccessibleBrowseBoxBase::addAccessibleEventListener(
267 const Reference< XAccessibleEventListener>& _rxListener )
268 throw ( uno::RuntimeException, std::exception )
270 if ( _rxListener.is() )
272 ::osl::MutexGuard aGuard( getOslMutex() );
273 if ( !getClientId( ) )
274 setClientId( AccessibleEventNotifier::registerClient( ) );
276 AccessibleEventNotifier::addEventListener( getClientId( ), _rxListener );
280 void SAL_CALL AccessibleBrowseBoxBase::removeAccessibleEventListener(
281 const Reference< XAccessibleEventListener>& _rxListener )
282 throw ( uno::RuntimeException, std::exception )
284 if( _rxListener.is() && getClientId( ) )
286 ::osl::MutexGuard aGuard( getOslMutex() );
287 sal_Int32 nListenerCount = AccessibleEventNotifier::removeEventListener( getClientId( ), _rxListener );
288 if ( !nListenerCount )
290 // no listeners anymore
291 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
292 // and at least to us not firing any events anymore, in case somebody calls
293 // NotifyAccessibleEvent, again
295 AccessibleEventNotifier::TClientId nId( getClientId( ) );
296 setClientId( 0 );
297 AccessibleEventNotifier::revokeClient( nId );
302 // XTypeProvider --------------------------------------------------------------
304 Sequence< sal_Int8 > SAL_CALL AccessibleBrowseBoxBase::getImplementationId()
305 throw ( uno::RuntimeException, std::exception )
307 return css::uno::Sequence<sal_Int8>();
310 // XServiceInfo ---------------------------------------------------------------
312 sal_Bool SAL_CALL AccessibleBrowseBoxBase::supportsService(
313 const OUString& rServiceName )
314 throw ( uno::RuntimeException, std::exception )
316 return cppu::supportsService(this, rServiceName);
319 Sequence< OUString > SAL_CALL AccessibleBrowseBoxBase::getSupportedServiceNames()
320 throw ( uno::RuntimeException, std::exception )
322 const OUString aServiceName( "com.sun.star.accessibility.AccessibleContext" );
323 return Sequence< OUString >( &aServiceName, 1 );
326 // other public methods -------------------------------------------------------
328 void AccessibleBrowseBoxBase::setAccessibleName( const OUString& rName )
330 ::osl::ClearableMutexGuard aGuard( getOslMutex() );
331 Any aOld;
332 aOld <<= maName;
333 maName = rName;
335 aGuard.clear();
337 commitEvent(
338 AccessibleEventId::NAME_CHANGED,
339 uno::makeAny( maName ),
340 aOld );
343 void AccessibleBrowseBoxBase::setAccessibleDescription( const OUString& rDescription )
345 ::osl::ClearableMutexGuard aGuard( getOslMutex() );
346 Any aOld;
347 aOld <<= maDescription;
348 maDescription = rDescription;
350 aGuard.clear();
352 commitEvent(
353 AccessibleEventId::DESCRIPTION_CHANGED,
354 uno::makeAny( maDescription ),
355 aOld );
358 // internal virtual methods ---------------------------------------------------
360 bool AccessibleBrowseBoxBase::implIsShowing()
362 bool bShowing = false;
363 if( mxParent.is() )
365 Reference< XAccessibleComponent >
366 xParentComp( mxParent->getAccessibleContext(), uno::UNO_QUERY );
367 if( xParentComp.is() )
368 bShowing = implGetBoundingBox().IsOver(
369 VCLRectangle( xParentComp->getBounds() ) );
371 return bShowing;
374 ::utl::AccessibleStateSetHelper* AccessibleBrowseBoxBase::implCreateStateSetHelper()
376 ::utl::AccessibleStateSetHelper*
377 pStateSetHelper = new ::utl::AccessibleStateSetHelper;
379 if( isAlive() )
381 // SHOWING done with mxParent
382 if( implIsShowing() )
383 pStateSetHelper->AddState( AccessibleStateType::SHOWING );
384 // BrowseBox fills StateSet with states depending on object type
385 mpBrowseBox->FillAccessibleStateSet( *pStateSetHelper, getType() );
387 else
388 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
390 return pStateSetHelper;
393 // internal helper methods ----------------------------------------------------
395 bool AccessibleBrowseBoxBase::isAlive() const
397 return !rBHelper.bDisposed && !rBHelper.bInDispose && mpBrowseBox;
400 void AccessibleBrowseBoxBase::ensureIsAlive() const
401 throw ( lang::DisposedException )
403 if( !isAlive() )
404 throw lang::DisposedException();
407 Rectangle AccessibleBrowseBoxBase::getBoundingBox()
408 throw ( lang::DisposedException )
410 SolarMutexGuard aSolarGuard;
411 ::osl::MutexGuard aGuard( getOslMutex() );
412 ensureIsAlive();
413 Rectangle aRect = implGetBoundingBox();
414 if ( 0 == aRect.Left() && 0 == aRect.Top() && 0 == aRect.Right() && 0 == aRect.Bottom() )
416 SAL_WARN( "accessibility", "rectangle doesn't exist" );
418 return aRect;
421 Rectangle AccessibleBrowseBoxBase::getBoundingBoxOnScreen()
422 throw ( lang::DisposedException )
424 SolarMutexGuard aSolarGuard;
425 ::osl::MutexGuard aGuard( getOslMutex() );
426 ensureIsAlive();
427 Rectangle aRect = implGetBoundingBoxOnScreen();
428 if ( 0 == aRect.Left() && 0 == aRect.Top() && 0 == aRect.Right() && 0 == aRect.Bottom() )
430 SAL_WARN( "accessibility", "rectangle doesn't exist" );
432 return aRect;
435 void AccessibleBrowseBoxBase::commitEvent(
436 sal_Int16 _nEventId, const Any& _rNewValue, const Any& _rOldValue )
438 ::osl::ClearableMutexGuard aGuard( getOslMutex() );
439 if ( !getClientId( ) )
440 // if we don't have a client id for the notifier, then we don't have listeners, then
441 // we don't need to notify anything
442 return;
444 // build an event object
445 AccessibleEventObject aEvent;
446 aEvent.Source = *this;
447 aEvent.EventId = _nEventId;
448 aEvent.OldValue = _rOldValue;
449 aEvent.NewValue = _rNewValue;
451 // let the notifier handle this event
453 AccessibleEventNotifier::addEvent( getClientId( ), aEvent );
456 sal_Int16 SAL_CALL AccessibleBrowseBoxBase::getAccessibleRole()
457 throw ( uno::RuntimeException, std::exception )
459 ensureIsAlive();
460 sal_Int16 nRole = AccessibleRole::UNKNOWN;
461 switch ( meObjType )
463 case BBTYPE_ROWHEADERCELL:
464 nRole = AccessibleRole::ROW_HEADER;
465 break;
466 case BBTYPE_COLUMNHEADERCELL:
467 nRole = AccessibleRole::COLUMN_HEADER;
468 break;
469 case BBTYPE_COLUMNHEADERBAR:
470 case BBTYPE_ROWHEADERBAR:
471 case BBTYPE_TABLE:
472 nRole = AccessibleRole::TABLE;
473 break;
474 case BBTYPE_TABLECELL:
475 nRole = AccessibleRole::TABLE_CELL;
476 break;
477 case BBTYPE_BROWSEBOX:
478 nRole = AccessibleRole::PANEL;
479 break;
480 case BBTYPE_CHECKBOXCELL:
481 nRole = AccessibleRole::CHECK_BOX;
482 break;
484 return nRole;
487 Reference<XAccessible > SAL_CALL AccessibleBrowseBoxBase::getAccessibleAtPoint( const ::com::sun::star::awt::Point& )
488 throw ( uno::RuntimeException, std::exception )
490 return NULL;
493 void SAL_CALL AccessibleBrowseBoxBase::disposing( const ::com::sun::star::lang::EventObject& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
495 m_xFocusWindow = NULL;
498 sal_Int32 SAL_CALL AccessibleBrowseBoxBase::getForeground( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
500 SolarMutexGuard aSolarGuard;
501 ::osl::MutexGuard aGuard( getOslMutex() );
502 ensureIsAlive();
504 sal_Int32 nColor = 0;
505 vcl::Window* pInst = mpBrowseBox->GetWindowInstance();
506 if ( pInst )
508 if ( pInst->IsControlForeground() )
509 nColor = pInst->GetControlForeground().GetColor();
510 else
512 vcl::Font aFont;
513 if ( pInst->IsControlFont() )
514 aFont = pInst->GetControlFont();
515 else
516 aFont = pInst->GetFont();
517 nColor = aFont.GetColor().GetColor();
521 return nColor;
524 sal_Int32 SAL_CALL AccessibleBrowseBoxBase::getBackground( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
526 SolarMutexGuard aSolarGuard;
527 ::osl::MutexGuard aGuard( getOslMutex() );
528 ensureIsAlive();
529 sal_Int32 nColor = 0;
530 vcl::Window* pInst = mpBrowseBox->GetWindowInstance();
531 if ( pInst )
533 if ( pInst->IsControlBackground() )
534 nColor = pInst->GetControlBackground().GetColor();
535 else
536 nColor = pInst->GetBackground().GetColor().GetColor();
539 return nColor;
543 // XInterface -----------------------------------------------------------------
544 IMPLEMENT_FORWARD_XINTERFACE2( BrowseBoxAccessibleElement, AccessibleBrowseBoxBase, BrowseBoxAccessibleElement_Base )
546 // XTypeProvider --------------------------------------------------------------
547 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BrowseBoxAccessibleElement, AccessibleBrowseBoxBase, BrowseBoxAccessibleElement_Base )
549 // XAccessible ----------------------------------------------------------------
551 Reference< XAccessibleContext > SAL_CALL BrowseBoxAccessibleElement::getAccessibleContext() throw ( uno::RuntimeException, std::exception )
553 ensureIsAlive();
554 return this;
558 BrowseBoxAccessibleElement::BrowseBoxAccessibleElement( const Reference< XAccessible >& rxParent, IAccessibleTableProvider& rBrowseBox,
559 const Reference< awt::XWindow >& _xFocusWindow, AccessibleBrowseBoxObjType eObjType )
560 :AccessibleBrowseBoxBase( rxParent, rBrowseBox, _xFocusWindow, eObjType )
565 BrowseBoxAccessibleElement::BrowseBoxAccessibleElement( const Reference< XAccessible >& rxParent, IAccessibleTableProvider& rBrowseBox,
566 const Reference< awt::XWindow >& _xFocusWindow, AccessibleBrowseBoxObjType eObjType,
567 const OUString& rName, const OUString& rDescription )
568 :AccessibleBrowseBoxBase( rxParent, rBrowseBox, _xFocusWindow, eObjType, rName, rDescription )
573 BrowseBoxAccessibleElement::~BrowseBoxAccessibleElement( )
579 } // namespace accessibility
583 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */