Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / extended / AccessibleGridControlBase.cxx
blobe523ec1c9eed359e2be24ecbc82451bc0144f070
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/AccessibleGridControlBase.hxx"
21 #include <svtools/accessibletable.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;
39 using namespace ::svt::table;
44 namespace accessibility {
46 using namespace com::sun::star::accessibility::AccessibleStateType;
49 AccessibleGridControlBase::AccessibleGridControlBase(
50 const Reference< XAccessible >& rxParent,
51 svt::table::IAccessibleTable& rTable,
52 AccessibleTableControlObjType eObjType ) :
53 AccessibleGridControlImplHelper( m_aMutex ),
54 m_xParent( rxParent ),
55 m_aTable( rTable),
56 m_eObjType( eObjType ),
57 m_aName( rTable.GetAccessibleObjectName( eObjType, 0, 0 ) ),
58 m_aDescription( rTable.GetAccessibleObjectDescription( eObjType ) ),
59 m_aClientId(0)
63 AccessibleGridControlBase::~AccessibleGridControlBase()
65 if( isAlive() )
67 // increment ref count to prevent double call of Dtor
68 osl_atomic_increment( &m_refCount );
69 dispose();
73 void SAL_CALL AccessibleGridControlBase::disposing()
75 SolarMutexGuard g;
77 if ( getClientId( ) )
79 AccessibleEventNotifier::TClientId nId( getClientId( ) );
80 setClientId( 0 );
81 AccessibleEventNotifier::revokeClientNotifyDisposing( nId, *this );
84 m_xParent = NULL;
85 //m_aTable = NULL;
88 // XAccessibleContext ---------------------------------------------------------
90 Reference< XAccessible > SAL_CALL AccessibleGridControlBase::getAccessibleParent()
91 throw ( uno::RuntimeException, std::exception )
93 SolarMutexGuard g;
95 ensureIsAlive();
96 return m_xParent;
99 sal_Int32 SAL_CALL AccessibleGridControlBase::getAccessibleIndexInParent()
100 throw ( uno::RuntimeException, std::exception )
102 SolarMutexGuard g;
104 ensureIsAlive();
106 // -1 for child not found/no parent (according to specification)
107 sal_Int32 nRet = -1;
109 Reference< uno::XInterface > xMeMyselfAndI( static_cast< XAccessibleContext* >( this ), uno::UNO_QUERY );
111 // iterate over parent's children and search for this object
112 if( m_xParent.is() )
114 Reference< XAccessibleContext >
115 xParentContext( m_xParent->getAccessibleContext() );
116 if( xParentContext.is() )
118 Reference< uno::XInterface > xChild;
120 sal_Int32 nChildCount = xParentContext->getAccessibleChildCount();
121 for( sal_Int32 nChild = 0; nChild < nChildCount; ++nChild )
123 xChild.set(xParentContext->getAccessibleChild( nChild ), css::uno::UNO_QUERY);
124 if ( xMeMyselfAndI.get() == xChild.get() )
126 nRet = nChild;
127 break;
132 return nRet;
135 OUString SAL_CALL AccessibleGridControlBase::getAccessibleDescription()
136 throw ( uno::RuntimeException, std::exception )
138 SolarMutexGuard g;
140 ensureIsAlive();
141 return m_aDescription;
144 OUString SAL_CALL AccessibleGridControlBase::getAccessibleName()
145 throw ( uno::RuntimeException, std::exception )
147 SolarMutexGuard g;
149 ensureIsAlive();
150 return m_aName;
153 Reference< XAccessibleRelationSet > SAL_CALL
154 AccessibleGridControlBase::getAccessibleRelationSet()
155 throw ( uno::RuntimeException, std::exception )
157 SolarMutexGuard g;
159 ensureIsAlive();
160 // GridControl does not have relations.
161 return new utl::AccessibleRelationSetHelper;
164 Reference< XAccessibleStateSet > SAL_CALL
165 AccessibleGridControlBase::getAccessibleStateSet()
166 throw ( uno::RuntimeException, std::exception )
168 SolarMutexGuard aSolarGuard;
170 // don't check whether alive -> StateSet may contain DEFUNC
171 return implCreateStateSetHelper();
174 lang::Locale SAL_CALL AccessibleGridControlBase::getLocale()
175 throw ( IllegalAccessibleComponentStateException, uno::RuntimeException, std::exception )
177 SolarMutexGuard g;
179 ensureIsAlive();
180 if( m_xParent.is() )
182 Reference< XAccessibleContext >
183 xParentContext( m_xParent->getAccessibleContext() );
184 if( xParentContext.is() )
185 return xParentContext->getLocale();
187 throw IllegalAccessibleComponentStateException();
190 // XAccessibleComponent -------------------------------------------------------
192 sal_Bool SAL_CALL AccessibleGridControlBase::containsPoint( const awt::Point& rPoint )
193 throw ( uno::RuntimeException, std::exception )
195 return Rectangle( Point(), getBoundingBox().GetSize() ).IsInside( VCLPoint( rPoint ) );
198 awt::Rectangle SAL_CALL AccessibleGridControlBase::getBounds()
199 throw ( uno::RuntimeException, std::exception )
201 return AWTRectangle( getBoundingBox() );
204 awt::Point SAL_CALL AccessibleGridControlBase::getLocation()
205 throw ( uno::RuntimeException, std::exception )
207 return AWTPoint( getBoundingBox().TopLeft() );
210 awt::Point SAL_CALL AccessibleGridControlBase::getLocationOnScreen()
211 throw ( uno::RuntimeException, std::exception )
213 return AWTPoint( getBoundingBoxOnScreen().TopLeft() );
216 awt::Size SAL_CALL AccessibleGridControlBase::getSize()
217 throw ( uno::RuntimeException, std::exception )
219 return AWTSize( getBoundingBox().GetSize() );
222 // XAccessibleEventBroadcaster ------------------------------------------------
224 void SAL_CALL AccessibleGridControlBase::addAccessibleEventListener(
225 const Reference< XAccessibleEventListener>& _rxListener )
226 throw ( uno::RuntimeException, std::exception )
228 if ( _rxListener.is() )
230 SolarMutexGuard g;
232 if ( !getClientId( ) )
233 setClientId( AccessibleEventNotifier::registerClient( ) );
235 AccessibleEventNotifier::addEventListener( getClientId( ), _rxListener );
239 void SAL_CALL AccessibleGridControlBase::removeAccessibleEventListener(
240 const Reference< XAccessibleEventListener>& _rxListener )
241 throw ( uno::RuntimeException, std::exception )
243 if( _rxListener.is() && getClientId( ) )
245 SolarMutexGuard g;
247 sal_Int32 nListenerCount = AccessibleEventNotifier::removeEventListener( getClientId( ), _rxListener );
248 if ( !nListenerCount )
250 // no listeners anymore
251 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
252 // and at least to us not firing any events anymore, in case somebody calls
253 // NotifyAccessibleEvent, again
254 AccessibleEventNotifier::TClientId nId( getClientId( ) );
255 setClientId( 0 );
256 AccessibleEventNotifier::revokeClient( nId );
261 // XTypeProvider --------------------------------------------------------------
263 Sequence< sal_Int8 > SAL_CALL AccessibleGridControlBase::getImplementationId()
264 throw ( uno::RuntimeException, std::exception )
266 return css::uno::Sequence<sal_Int8>();
269 // XServiceInfo ---------------------------------------------------------------
271 sal_Bool SAL_CALL AccessibleGridControlBase::supportsService(
272 const OUString& rServiceName )
273 throw ( uno::RuntimeException, std::exception )
275 return cppu::supportsService(this, rServiceName);
278 Sequence< OUString > SAL_CALL AccessibleGridControlBase::getSupportedServiceNames()
279 throw ( uno::RuntimeException, std::exception )
281 const OUString aServiceName( "com.sun.star.accessibility.AccessibleContext" );
282 return Sequence< OUString >( &aServiceName, 1 );
284 // internal virtual methods ---------------------------------------------------
286 bool AccessibleGridControlBase::implIsShowing()
288 bool bShowing = false;
289 if( m_xParent.is() )
291 Reference< XAccessibleComponent >
292 xParentComp( m_xParent->getAccessibleContext(), uno::UNO_QUERY );
293 if( xParentComp.is() )
294 bShowing = implGetBoundingBox().IsOver(
295 VCLRectangle( xParentComp->getBounds() ) );
297 return bShowing;
300 ::utl::AccessibleStateSetHelper* AccessibleGridControlBase::implCreateStateSetHelper()
302 ::utl::AccessibleStateSetHelper*
303 pStateSetHelper = new ::utl::AccessibleStateSetHelper;
305 if( isAlive() )
307 // SHOWING done with m_xParent
308 if( implIsShowing() )
309 pStateSetHelper->AddState( AccessibleStateType::SHOWING );
310 // GridControl fills StateSet with states depending on object type
311 m_aTable.FillAccessibleStateSet( *pStateSetHelper, getType() );
313 else
314 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
315 return pStateSetHelper;
318 // internal helper methods ----------------------------------------------------
320 bool AccessibleGridControlBase::isAlive() const
322 ::osl::MutexGuard g(m_aMutex); // guards rBHelper members
323 return !rBHelper.bDisposed && !rBHelper.bInDispose;
326 void AccessibleGridControlBase::ensureIsAlive() const
327 throw ( lang::DisposedException )
329 if( !isAlive() )
330 throw lang::DisposedException();
333 Rectangle AccessibleGridControlBase::getBoundingBox()
334 throw ( lang::DisposedException )
336 SolarMutexGuard aSolarGuard;
337 ensureIsAlive();
338 Rectangle aRect = implGetBoundingBox();
339 if ( 0 == aRect.Left() && 0 == aRect.Top() && 0 == aRect.Right() && 0 == aRect.Bottom() )
341 SAL_WARN( "accessibility", "rectangle doesn't exist" );
343 return aRect;
346 Rectangle AccessibleGridControlBase::getBoundingBoxOnScreen()
347 throw ( lang::DisposedException )
349 SolarMutexGuard aSolarGuard;
350 ensureIsAlive();
351 Rectangle aRect = implGetBoundingBoxOnScreen();
352 if ( 0 == aRect.Left() && 0 == aRect.Top() && 0 == aRect.Right() && 0 == aRect.Bottom() )
354 SAL_WARN( "accessibility", "rectangle doesn't exist" );
356 return aRect;
359 void AccessibleGridControlBase::commitEvent(
360 sal_Int16 _nEventId, const Any& _rNewValue, const Any& _rOldValue )
362 SolarMutexGuard g;
364 if ( !getClientId( ) )
365 // if we don't have a client id for the notifier, then we don't have listeners, then
366 // we don't need to notify anything
367 return;
369 // build an event object
370 AccessibleEventObject aEvent;
371 aEvent.Source = *this;
372 aEvent.EventId = _nEventId;
373 aEvent.OldValue = _rOldValue;
374 aEvent.NewValue = _rNewValue;
376 // let the notifier handle this event
378 AccessibleEventNotifier::addEvent( getClientId( ), aEvent );
381 sal_Int16 SAL_CALL AccessibleGridControlBase::getAccessibleRole()
382 throw ( uno::RuntimeException, std::exception )
384 ensureIsAlive();
385 sal_Int16 nRole = AccessibleRole::UNKNOWN;
386 switch ( m_eObjType )
388 case TCTYPE_ROWHEADERCELL:
389 nRole = AccessibleRole::ROW_HEADER;
390 break;
391 case TCTYPE_COLUMNHEADERCELL:
392 nRole = AccessibleRole::COLUMN_HEADER;
393 break;
394 case TCTYPE_COLUMNHEADERBAR:
395 case TCTYPE_ROWHEADERBAR:
396 case TCTYPE_TABLE:
397 nRole = AccessibleRole::TABLE;
398 break;
399 case TCTYPE_TABLECELL:
400 nRole = AccessibleRole::TABLE_CELL;
401 break;
402 case TCTYPE_GRIDCONTROL:
403 nRole = AccessibleRole::PANEL;
404 break;
406 return nRole;
409 Reference<XAccessible > SAL_CALL AccessibleGridControlBase::getAccessibleAtPoint( const ::com::sun::star::awt::Point& )
410 throw ( uno::RuntimeException, std::exception )
412 return NULL;
415 sal_Int32 SAL_CALL AccessibleGridControlBase::getForeground( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
417 SolarMutexGuard aSolarGuard;
419 ensureIsAlive();
421 sal_Int32 nColor = 0;
422 vcl::Window* pInst = m_aTable.GetWindowInstance();
423 if ( pInst )
425 if ( pInst->IsControlForeground() )
426 nColor = pInst->GetControlForeground().GetColor();
427 else
429 vcl::Font aFont;
430 if ( pInst->IsControlFont() )
431 aFont = pInst->GetControlFont();
432 else
433 aFont = pInst->GetFont();
434 nColor = aFont.GetColor().GetColor();
437 return nColor;
440 sal_Int32 SAL_CALL AccessibleGridControlBase::getBackground( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
442 SolarMutexGuard aSolarGuard;
444 ensureIsAlive();
445 sal_Int32 nColor = 0;
446 vcl::Window* pInst = m_aTable.GetWindowInstance();
447 if ( pInst )
449 if ( pInst->IsControlBackground() )
450 nColor = pInst->GetControlBackground().GetColor();
451 else
452 nColor = pInst->GetBackground().GetColor().GetColor();
454 return nColor;
458 GridControlAccessibleElement::GridControlAccessibleElement( const Reference< XAccessible >& rxParent,
459 IAccessibleTable& rTable,
460 AccessibleTableControlObjType eObjType )
461 :AccessibleGridControlBase( rxParent, rTable, eObjType )
465 // XInterface -----------------------------------------------------------------
466 IMPLEMENT_FORWARD_XINTERFACE2( GridControlAccessibleElement, AccessibleGridControlBase, GridControlAccessibleElement_Base)
468 // XTypeProvider --------------------------------------------------------------
469 IMPLEMENT_FORWARD_XTYPEPROVIDER2( GridControlAccessibleElement, AccessibleGridControlBase, GridControlAccessibleElement_Base )
471 // XAccessible ----------------------------------------------------------------
473 Reference< XAccessibleContext > SAL_CALL GridControlAccessibleElement::getAccessibleContext() throw ( uno::RuntimeException, std::exception )
475 SolarMutexGuard g;
477 ensureIsAlive();
478 return this;
481 GridControlAccessibleElement::~GridControlAccessibleElement( )
487 } // namespace accessibility
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */