lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / accessibility / source / extended / AccessibleGridControlBase.cxx
blob5a870ff41814aed99331cf13b6d9ec32db33d378
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 <extended/AccessibleGridControlBase.hxx>
21 #include <vcl/accessibletable.hxx>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <sal/types.h>
25 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
26 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
27 #include <com/sun/star/accessibility/IllegalAccessibleComponentStateException.hpp>
28 #include <unotools/accessiblerelationsethelper.hxx>
29 #include <sal/log.hxx>
31 using ::com::sun::star::uno::Sequence;
32 using ::com::sun::star::uno::Any;
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::accessibility;
36 using namespace ::comphelper;
37 using namespace ::vcl;
38 using namespace ::vcl::table;
41 namespace accessibility {
43 using namespace com::sun::star::accessibility::AccessibleStateType;
46 AccessibleGridControlBase::AccessibleGridControlBase(
47 const css::uno::Reference< css::accessibility::XAccessible >& rxParent,
48 ::vcl::table::IAccessibleTable& rTable,
49 ::vcl::table::AccessibleTableControlObjType eObjType ) :
50 AccessibleGridControlImplHelper( m_aMutex ),
51 m_xParent( rxParent ),
52 m_aTable( rTable),
53 m_eObjType( eObjType ),
54 m_aName( rTable.GetAccessibleObjectName( eObjType, 0, 0 ) ),
55 m_aDescription( rTable.GetAccessibleObjectDescription( eObjType ) ),
56 m_aClientId(0)
60 AccessibleGridControlBase::~AccessibleGridControlBase()
62 if( isAlive() )
64 // increment ref count to prevent double call of Dtor
65 osl_atomic_increment( &m_refCount );
66 dispose();
70 void SAL_CALL AccessibleGridControlBase::disposing()
72 SolarMutexGuard g;
74 if ( getClientId( ) )
76 AccessibleEventNotifier::TClientId nId( getClientId( ) );
77 setClientId( 0 );
78 AccessibleEventNotifier::revokeClientNotifyDisposing( nId, *this );
81 m_xParent = nullptr;
82 //m_aTable = NULL;
85 // css::accessibility::XAccessibleContext
87 css::uno::Reference< css::accessibility::XAccessible > SAL_CALL AccessibleGridControlBase::getAccessibleParent()
89 SolarMutexGuard g;
91 ensureIsAlive();
92 return m_xParent;
95 sal_Int32 SAL_CALL AccessibleGridControlBase::getAccessibleIndexInParent()
97 SolarMutexGuard g;
99 ensureIsAlive();
101 // -1 for child not found/no parent (according to specification)
102 sal_Int32 nRet = -1;
104 css::uno::Reference< uno::XInterface > xMeMyselfAndI( static_cast< css::accessibility::XAccessibleContext* >( this ), uno::UNO_QUERY );
106 // iterate over parent's children and search for this object
107 if( m_xParent.is() )
109 css::uno::Reference< css::accessibility::XAccessibleContext >
110 xParentContext( m_xParent->getAccessibleContext() );
111 if( xParentContext.is() )
113 css::uno::Reference< uno::XInterface > xChild;
115 sal_Int32 nChildCount = xParentContext->getAccessibleChildCount();
116 for( sal_Int32 nChild = 0; nChild < nChildCount; ++nChild )
118 xChild.set(xParentContext->getAccessibleChild( nChild ), css::uno::UNO_QUERY);
119 if ( xMeMyselfAndI.get() == xChild.get() )
121 nRet = nChild;
122 break;
127 return nRet;
130 OUString SAL_CALL AccessibleGridControlBase::getAccessibleDescription()
132 SolarMutexGuard g;
134 ensureIsAlive();
135 return m_aDescription;
138 OUString SAL_CALL AccessibleGridControlBase::getAccessibleName()
140 SolarMutexGuard g;
142 ensureIsAlive();
143 return m_aName;
146 css::uno::Reference< css::accessibility::XAccessibleRelationSet > SAL_CALL
147 AccessibleGridControlBase::getAccessibleRelationSet()
149 SolarMutexGuard g;
151 ensureIsAlive();
152 // GridControl does not have relations.
153 return new utl::AccessibleRelationSetHelper;
156 css::uno::Reference< css::accessibility::XAccessibleStateSet > SAL_CALL
157 AccessibleGridControlBase::getAccessibleStateSet()
159 SolarMutexGuard aSolarGuard;
161 // don't check whether alive -> StateSet may contain DEFUNC
162 return implCreateStateSetHelper();
165 lang::Locale SAL_CALL AccessibleGridControlBase::getLocale()
167 SolarMutexGuard g;
169 ensureIsAlive();
170 if( m_xParent.is() )
172 css::uno::Reference< css::accessibility::XAccessibleContext >
173 xParentContext( m_xParent->getAccessibleContext() );
174 if( xParentContext.is() )
175 return xParentContext->getLocale();
177 throw IllegalAccessibleComponentStateException();
180 // css::accessibility::XAccessibleComponent
182 sal_Bool SAL_CALL AccessibleGridControlBase::containsPoint( const awt::Point& rPoint )
184 return tools::Rectangle( Point(), getBoundingBox().GetSize() ).IsInside( VCLPoint( rPoint ) );
187 awt::Rectangle SAL_CALL AccessibleGridControlBase::getBounds()
189 return AWTRectangle( getBoundingBox() );
192 awt::Point SAL_CALL AccessibleGridControlBase::getLocation()
194 return AWTPoint( getBoundingBox().TopLeft() );
197 awt::Point SAL_CALL AccessibleGridControlBase::getLocationOnScreen()
199 return AWTPoint( getBoundingBoxOnScreen().TopLeft() );
202 awt::Size SAL_CALL AccessibleGridControlBase::getSize()
204 return AWTSize( getBoundingBox().GetSize() );
207 // css::accessibility::XAccessibleEventBroadcaster
209 void SAL_CALL AccessibleGridControlBase::addAccessibleEventListener(
210 const css::uno::Reference< css::accessibility::XAccessibleEventListener>& _rxListener )
212 if ( _rxListener.is() )
214 SolarMutexGuard g;
216 if ( !getClientId( ) )
217 setClientId( AccessibleEventNotifier::registerClient( ) );
219 AccessibleEventNotifier::addEventListener( getClientId( ), _rxListener );
223 void SAL_CALL AccessibleGridControlBase::removeAccessibleEventListener(
224 const css::uno::Reference< css::accessibility::XAccessibleEventListener>& _rxListener )
226 if( _rxListener.is() && getClientId( ) )
228 SolarMutexGuard g;
230 sal_Int32 nListenerCount = AccessibleEventNotifier::removeEventListener( getClientId( ), _rxListener );
231 if ( !nListenerCount )
233 // no listeners anymore
234 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
235 // and at least to us not firing any events anymore, in case somebody calls
236 // NotifyAccessibleEvent, again
237 AccessibleEventNotifier::TClientId nId( getClientId( ) );
238 setClientId( 0 );
239 AccessibleEventNotifier::revokeClient( nId );
244 // XTypeProvider
246 Sequence< sal_Int8 > SAL_CALL AccessibleGridControlBase::getImplementationId()
248 return css::uno::Sequence<sal_Int8>();
251 // XServiceInfo
253 sal_Bool SAL_CALL AccessibleGridControlBase::supportsService(
254 const OUString& rServiceName )
256 return cppu::supportsService(this, rServiceName);
259 Sequence< OUString > SAL_CALL AccessibleGridControlBase::getSupportedServiceNames()
261 return { "com.sun.star.accessibility.AccessibleContext" };
263 // internal virtual methods
265 bool AccessibleGridControlBase::implIsShowing()
267 bool bShowing = false;
268 if( m_xParent.is() )
270 css::uno::Reference< css::accessibility::XAccessibleComponent >
271 xParentComp( m_xParent->getAccessibleContext(), uno::UNO_QUERY );
272 if( xParentComp.is() )
273 bShowing = implGetBoundingBox().IsOver(
274 VCLRectangle( xParentComp->getBounds() ) );
276 return bShowing;
279 ::utl::AccessibleStateSetHelper* AccessibleGridControlBase::implCreateStateSetHelper()
281 ::utl::AccessibleStateSetHelper*
282 pStateSetHelper = new ::utl::AccessibleStateSetHelper;
284 if( isAlive() )
286 // SHOWING done with m_xParent
287 if( implIsShowing() )
288 pStateSetHelper->AddState( AccessibleStateType::SHOWING );
289 // GridControl fills StateSet with states depending on object type
290 m_aTable.FillAccessibleStateSet( *pStateSetHelper, getType() );
292 else
293 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
294 return pStateSetHelper;
297 // internal helper methods
299 bool AccessibleGridControlBase::isAlive() const
301 ::osl::MutexGuard g(m_aMutex); // guards rBHelper members
302 return !rBHelper.bDisposed && !rBHelper.bInDispose;
305 void AccessibleGridControlBase::ensureIsAlive() const
307 if( !isAlive() )
308 throw lang::DisposedException();
311 tools::Rectangle AccessibleGridControlBase::getBoundingBox()
313 SolarMutexGuard aSolarGuard;
314 ensureIsAlive();
315 tools::Rectangle aRect = implGetBoundingBox();
316 if ( aRect.Left() == 0 && aRect.Top() == 0 && aRect.Right() == 0 && aRect.Bottom() == 0 )
318 SAL_WARN( "accessibility", "rectangle doesn't exist" );
320 return aRect;
323 tools::Rectangle AccessibleGridControlBase::getBoundingBoxOnScreen()
325 SolarMutexGuard aSolarGuard;
326 ensureIsAlive();
327 tools::Rectangle aRect = implGetBoundingBoxOnScreen();
328 if ( aRect.Left() == 0 && aRect.Top() == 0 && aRect.Right() == 0 && aRect.Bottom() == 0 )
330 SAL_WARN( "accessibility", "rectangle doesn't exist" );
332 return aRect;
335 void AccessibleGridControlBase::commitEvent(
336 sal_Int16 _nEventId, const Any& _rNewValue, const Any& _rOldValue )
338 SolarMutexGuard g;
340 if ( !getClientId( ) )
341 // if we don't have a client id for the notifier, then we don't have listeners, then
342 // we don't need to notify anything
343 return;
345 // build an event object
346 AccessibleEventObject aEvent;
347 aEvent.Source = *this;
348 aEvent.EventId = _nEventId;
349 aEvent.OldValue = _rOldValue;
350 aEvent.NewValue = _rNewValue;
352 // let the notifier handle this event
354 AccessibleEventNotifier::addEvent( getClientId( ), aEvent );
357 sal_Int16 SAL_CALL AccessibleGridControlBase::getAccessibleRole()
359 ensureIsAlive();
360 sal_Int16 nRole = AccessibleRole::UNKNOWN;
361 switch ( m_eObjType )
363 case TCTYPE_ROWHEADERCELL:
364 nRole = AccessibleRole::ROW_HEADER;
365 break;
366 case TCTYPE_COLUMNHEADERCELL:
367 nRole = AccessibleRole::COLUMN_HEADER;
368 break;
369 case TCTYPE_COLUMNHEADERBAR:
370 case TCTYPE_ROWHEADERBAR:
371 case TCTYPE_TABLE:
372 nRole = AccessibleRole::TABLE;
373 break;
374 case TCTYPE_TABLECELL:
375 nRole = AccessibleRole::TABLE_CELL;
376 break;
377 case TCTYPE_GRIDCONTROL:
378 nRole = AccessibleRole::PANEL;
379 break;
381 return nRole;
384 css::uno::Reference<css::accessibility::XAccessible > SAL_CALL AccessibleGridControlBase::getAccessibleAtPoint( const css::awt::Point& )
386 return nullptr;
389 sal_Int32 SAL_CALL AccessibleGridControlBase::getForeground( )
391 SolarMutexGuard aSolarGuard;
393 ensureIsAlive();
395 Color nColor;
396 vcl::Window* pInst = m_aTable.GetWindowInstance();
397 if ( pInst )
399 if ( pInst->IsControlForeground() )
400 nColor = pInst->GetControlForeground();
401 else
403 vcl::Font aFont;
404 if ( pInst->IsControlFont() )
405 aFont = pInst->GetControlFont();
406 else
407 aFont = pInst->GetFont();
408 nColor = aFont.GetColor();
411 return sal_Int32(nColor);
414 sal_Int32 SAL_CALL AccessibleGridControlBase::getBackground( )
416 SolarMutexGuard aSolarGuard;
418 ensureIsAlive();
419 Color nColor;
420 vcl::Window* pInst = m_aTable.GetWindowInstance();
421 if ( pInst )
423 if ( pInst->IsControlBackground() )
424 nColor = pInst->GetControlBackground();
425 else
426 nColor = pInst->GetBackground().GetColor();
428 return sal_Int32(nColor);
432 GridControlAccessibleElement::GridControlAccessibleElement( const css::uno::Reference< css::accessibility::XAccessible >& rxParent,
433 ::vcl::table::IAccessibleTable& rTable,
434 ::vcl::table::AccessibleTableControlObjType eObjType )
435 :AccessibleGridControlBase( rxParent, rTable, eObjType )
439 // XInterface
440 IMPLEMENT_FORWARD_XINTERFACE2( GridControlAccessibleElement, AccessibleGridControlBase, GridControlAccessibleElement_Base)
442 // XTypeProvider
443 IMPLEMENT_FORWARD_XTYPEPROVIDER2( GridControlAccessibleElement, AccessibleGridControlBase, GridControlAccessibleElement_Base )
445 // css::accessibility::XAccessible
447 css::uno::Reference< css::accessibility::XAccessibleContext > SAL_CALL GridControlAccessibleElement::getAccessibleContext()
449 SolarMutexGuard g;
451 ensureIsAlive();
452 return this;
455 GridControlAccessibleElement::~GridControlAccessibleElement( )
460 } // namespace accessibility
463 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */