lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / accessibility / source / extended / AccessibleBrowseBoxBase.cxx
blob9759737c07591bd43a79ba1de3b87710a6b6cfeb
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/AccessibleBrowseBoxBase.hxx>
21 #include <vcl/accessibletableprovider.hxx>
22 #include <cppuhelper/supportsservice.hxx>
24 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
25 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
26 #include <com/sun/star/accessibility/IllegalAccessibleComponentStateException.hpp>
27 #include <unotools/accessiblerelationsethelper.hxx>
28 #include <vcl/svapp.hxx>
29 #include <sal/log.hxx>
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::Sequence;
34 using ::com::sun::star::uno::Any;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::accessibility;
38 using namespace ::comphelper;
39 using namespace ::svt;
42 namespace accessibility {
44 using namespace com::sun::star::accessibility::AccessibleStateType;
47 // Ctor/Dtor/disposing
49 AccessibleBrowseBoxBase::AccessibleBrowseBoxBase(
50 const css::uno::Reference< css::accessibility::XAccessible >& rxParent,
51 ::vcl::IAccessibleTableProvider& rBrowseBox,
52 const css::uno::Reference< css::awt::XWindow >& _xFocusWindow,
53 ::vcl::AccessibleBrowseBoxObjType eObjType ) :
54 AccessibleBrowseBoxImplHelper( m_aMutex ),
55 mxParent( rxParent ),
56 mpBrowseBox( &rBrowseBox ),
57 m_xFocusWindow(_xFocusWindow),
58 maName( rBrowseBox.GetAccessibleObjectName( eObjType ) ),
59 maDescription( rBrowseBox.GetAccessibleObjectDescription( eObjType ) ),
60 meObjType( eObjType ),
61 m_aClientId(0)
63 if ( m_xFocusWindow.is() )
64 m_xFocusWindow->addFocusListener( this );
67 AccessibleBrowseBoxBase::AccessibleBrowseBoxBase(
68 const css::uno::Reference< css::accessibility::XAccessible >& rxParent,
69 ::vcl::IAccessibleTableProvider& rBrowseBox,
70 const css::uno::Reference< css::awt::XWindow >& _xFocusWindow,
71 ::vcl::AccessibleBrowseBoxObjType eObjType,
72 const OUString& rName,
73 const OUString& rDescription ) :
74 AccessibleBrowseBoxImplHelper( m_aMutex ),
75 mxParent( rxParent ),
76 mpBrowseBox( &rBrowseBox ),
77 m_xFocusWindow(_xFocusWindow),
78 maName( rName ),
79 maDescription( rDescription ),
80 meObjType( eObjType ),
81 m_aClientId(0)
83 if ( m_xFocusWindow.is() )
84 m_xFocusWindow->addFocusListener( this );
87 AccessibleBrowseBoxBase::~AccessibleBrowseBoxBase()
89 if( isAlive() )
91 // increment ref count to prevent double call of Dtor
92 osl_atomic_increment( &m_refCount );
93 dispose();
97 void SAL_CALL AccessibleBrowseBoxBase::disposing()
99 ::osl::MutexGuard aGuard( getMutex() );
100 if ( m_xFocusWindow.is() )
102 SolarMutexGuard aSolarGuard;
103 m_xFocusWindow->removeFocusListener( this );
106 if ( getClientId( ) )
108 AccessibleEventNotifier::TClientId nId( getClientId( ) );
109 setClientId( 0 );
110 AccessibleEventNotifier::revokeClientNotifyDisposing( nId, *this );
113 mxParent = nullptr;
114 mpBrowseBox = nullptr;
117 // css::accessibility::XAccessibleContext
119 Reference< css::accessibility::XAccessible > SAL_CALL AccessibleBrowseBoxBase::getAccessibleParent()
121 ::osl::MutexGuard aGuard( getMutex() );
122 ensureIsAlive();
123 return mxParent;
126 sal_Int32 SAL_CALL AccessibleBrowseBoxBase::getAccessibleIndexInParent()
128 ::osl::MutexGuard aGuard( getMutex() );
129 ensureIsAlive();
131 // -1 for child not found/no parent (according to specification)
132 sal_Int32 nRet = -1;
134 css::uno::Reference< uno::XInterface > xMeMyselfAndI( static_cast< css::accessibility::XAccessibleContext* >( this ), uno::UNO_QUERY );
136 // iterate over parent's children and search for this object
137 if( mxParent.is() )
139 css::uno::Reference< css::accessibility::XAccessibleContext >
140 xParentContext( mxParent->getAccessibleContext() );
141 if( xParentContext.is() )
143 css::uno::Reference< uno::XInterface > xChild;
145 sal_Int32 nChildCount = xParentContext->getAccessibleChildCount();
146 for( sal_Int32 nChild = 0; nChild < nChildCount; ++nChild )
148 xChild.set(xParentContext->getAccessibleChild( nChild ), css::uno::UNO_QUERY);
150 if ( xMeMyselfAndI.get() == xChild.get() )
152 nRet = nChild;
153 break;
158 return nRet;
161 OUString SAL_CALL AccessibleBrowseBoxBase::getAccessibleDescription()
163 ::osl::MutexGuard aGuard( getMutex() );
164 ensureIsAlive();
165 return maDescription;
168 OUString SAL_CALL AccessibleBrowseBoxBase::getAccessibleName()
170 ::osl::MutexGuard aGuard( getMutex() );
171 ensureIsAlive();
172 return maName;
175 Reference< css::accessibility::XAccessibleRelationSet > SAL_CALL
176 AccessibleBrowseBoxBase::getAccessibleRelationSet()
178 ::osl::MutexGuard aGuard( getMutex() );
179 ensureIsAlive();
180 // BrowseBox does not have relations.
181 return new utl::AccessibleRelationSetHelper;
184 Reference< css::accessibility::XAccessibleStateSet > SAL_CALL
185 AccessibleBrowseBoxBase::getAccessibleStateSet()
187 SolarMethodGuard aGuard( getMutex() );
188 // don't check whether alive -> StateSet may contain DEFUNC
189 return implCreateStateSetHelper();
192 lang::Locale SAL_CALL AccessibleBrowseBoxBase::getLocale()
194 ::osl::MutexGuard aGuard( getMutex() );
195 ensureIsAlive();
196 if( mxParent.is() )
198 css::uno::Reference< css::accessibility::XAccessibleContext >
199 xParentContext( mxParent->getAccessibleContext() );
200 if( xParentContext.is() )
201 return xParentContext->getLocale();
203 throw IllegalAccessibleComponentStateException();
206 // css::accessibility::XAccessibleComponent
208 sal_Bool SAL_CALL AccessibleBrowseBoxBase::containsPoint( const css::awt::Point& rPoint )
210 return tools::Rectangle( Point(), getBoundingBox().GetSize() ).IsInside( VCLPoint( rPoint ) );
213 awt::Rectangle SAL_CALL AccessibleBrowseBoxBase::getBounds()
215 return AWTRectangle( getBoundingBox() );
218 awt::Point SAL_CALL AccessibleBrowseBoxBase::getLocation()
220 return AWTPoint( getBoundingBox().TopLeft() );
223 awt::Point SAL_CALL AccessibleBrowseBoxBase::getLocationOnScreen()
225 return AWTPoint( getBoundingBoxOnScreen().TopLeft() );
228 awt::Size SAL_CALL AccessibleBrowseBoxBase::getSize()
230 return AWTSize( getBoundingBox().GetSize() );
233 void SAL_CALL AccessibleBrowseBoxBase::focusGained( const css::awt::FocusEvent& )
235 com::sun::star::uno::Any aFocused;
236 com::sun::star::uno::Any aEmpty;
237 aFocused <<= FOCUSED;
239 commitEvent(AccessibleEventId::STATE_CHANGED,aFocused,aEmpty);
243 void SAL_CALL AccessibleBrowseBoxBase::focusLost( const css::awt::FocusEvent& )
245 com::sun::star::uno::Any aFocused;
246 com::sun::star::uno::Any aEmpty;
247 aFocused <<= FOCUSED;
249 commitEvent(AccessibleEventId::STATE_CHANGED,aEmpty,aFocused);
251 // css::accessibility::XAccessibleEventBroadcaster
253 void SAL_CALL AccessibleBrowseBoxBase::addAccessibleEventListener(
254 const css::uno::Reference< css::accessibility::XAccessibleEventListener>& _rxListener )
256 if ( _rxListener.is() )
258 ::osl::MutexGuard aGuard( getMutex() );
259 if ( !getClientId( ) )
260 setClientId( AccessibleEventNotifier::registerClient( ) );
262 AccessibleEventNotifier::addEventListener( getClientId( ), _rxListener );
266 void SAL_CALL AccessibleBrowseBoxBase::removeAccessibleEventListener(
267 const css::uno::Reference< css::accessibility::XAccessibleEventListener>& _rxListener )
269 if( _rxListener.is() && getClientId( ) )
271 ::osl::MutexGuard aGuard( getMutex() );
272 sal_Int32 nListenerCount = AccessibleEventNotifier::removeEventListener( getClientId( ), _rxListener );
273 if ( !nListenerCount )
275 // no listeners anymore
276 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
277 // and at least to us not firing any events anymore, in case somebody calls
278 // NotifyAccessibleEvent, again
280 AccessibleEventNotifier::TClientId nId( getClientId( ) );
281 setClientId( 0 );
282 AccessibleEventNotifier::revokeClient( nId );
287 // XTypeProvider
289 Sequence< sal_Int8 > SAL_CALL AccessibleBrowseBoxBase::getImplementationId()
291 return css::uno::Sequence<sal_Int8>();
294 // XServiceInfo
296 sal_Bool SAL_CALL AccessibleBrowseBoxBase::supportsService(
297 const OUString& rServiceName )
299 return cppu::supportsService(this, rServiceName);
302 Sequence< OUString > SAL_CALL AccessibleBrowseBoxBase::getSupportedServiceNames()
304 const OUString aServiceName( "com.sun.star.accessibility.AccessibleContext" );
305 return Sequence< OUString >( &aServiceName, 1 );
308 // other public methods
310 void AccessibleBrowseBoxBase::setAccessibleName( const OUString& rName )
312 ::osl::ClearableMutexGuard aGuard( getMutex() );
313 Any aOld;
314 aOld <<= maName;
315 maName = rName;
317 aGuard.clear();
319 commitEvent(
320 AccessibleEventId::NAME_CHANGED,
321 uno::Any( maName ),
322 aOld );
325 void AccessibleBrowseBoxBase::setAccessibleDescription( const OUString& rDescription )
327 ::osl::ClearableMutexGuard aGuard( getMutex() );
328 Any aOld;
329 aOld <<= maDescription;
330 maDescription = rDescription;
332 aGuard.clear();
334 commitEvent(
335 AccessibleEventId::DESCRIPTION_CHANGED,
336 uno::Any( maDescription ),
337 aOld );
340 // internal virtual methods
342 bool AccessibleBrowseBoxBase::implIsShowing()
344 bool bShowing = false;
345 if( mxParent.is() )
347 css::uno::Reference< css::accessibility::XAccessibleComponent >
348 xParentComp( mxParent->getAccessibleContext(), uno::UNO_QUERY );
349 if( xParentComp.is() )
350 bShowing = implGetBoundingBox().IsOver(
351 VCLRectangle( xParentComp->getBounds() ) );
353 return bShowing;
356 ::utl::AccessibleStateSetHelper* AccessibleBrowseBoxBase::implCreateStateSetHelper()
358 ::utl::AccessibleStateSetHelper*
359 pStateSetHelper = new ::utl::AccessibleStateSetHelper;
361 if( isAlive() )
363 // SHOWING done with mxParent
364 if( implIsShowing() )
365 pStateSetHelper->AddState( AccessibleStateType::SHOWING );
366 // BrowseBox fills StateSet with states depending on object type
367 mpBrowseBox->FillAccessibleStateSet( *pStateSetHelper, getType() );
369 else
370 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
372 return pStateSetHelper;
375 // internal helper methods
377 bool AccessibleBrowseBoxBase::isAlive() const
379 return !rBHelper.bDisposed && !rBHelper.bInDispose && mpBrowseBox;
382 void AccessibleBrowseBoxBase::ensureIsAlive() const
384 if( !isAlive() )
385 throw lang::DisposedException();
388 tools::Rectangle AccessibleBrowseBoxBase::getBoundingBox()
390 SolarMethodGuard aGuard(getMutex());
391 ensureIsAlive();
393 tools::Rectangle aRect = implGetBoundingBox();
394 if ( aRect.Left() == 0 && aRect.Top() == 0 && aRect.Right() == 0 && aRect.Bottom() == 0 )
396 SAL_WARN( "accessibility", "rectangle doesn't exist" );
398 return aRect;
401 tools::Rectangle AccessibleBrowseBoxBase::getBoundingBoxOnScreen()
403 SolarMethodGuard aGuard(getMutex());
404 ensureIsAlive();
406 tools::Rectangle aRect = implGetBoundingBoxOnScreen();
407 if ( aRect.Left() == 0 && aRect.Top() == 0 && aRect.Right() == 0 && aRect.Bottom() == 0 )
409 SAL_WARN( "accessibility", "rectangle doesn't exist" );
411 return aRect;
414 void AccessibleBrowseBoxBase::commitEvent(
415 sal_Int16 _nEventId, const Any& _rNewValue, const Any& _rOldValue )
417 ::osl::ClearableMutexGuard aGuard( getMutex() );
418 if ( !getClientId( ) )
419 // if we don't have a client id for the notifier, then we don't have listeners, then
420 // we don't need to notify anything
421 return;
423 // build an event object
424 AccessibleEventObject aEvent;
425 aEvent.Source = *this;
426 aEvent.EventId = _nEventId;
427 aEvent.OldValue = _rOldValue;
428 aEvent.NewValue = _rNewValue;
430 // let the notifier handle this event
432 AccessibleEventNotifier::addEvent( getClientId( ), aEvent );
435 sal_Int16 SAL_CALL AccessibleBrowseBoxBase::getAccessibleRole()
437 osl::MutexGuard aGuard( getMutex() );
438 ensureIsAlive();
439 sal_Int16 nRole = AccessibleRole::UNKNOWN;
440 switch ( meObjType )
442 case vcl::BBTYPE_ROWHEADERCELL:
443 nRole = AccessibleRole::ROW_HEADER;
444 break;
445 case vcl::BBTYPE_COLUMNHEADERCELL:
446 nRole = AccessibleRole::COLUMN_HEADER;
447 break;
448 case vcl::BBTYPE_COLUMNHEADERBAR:
449 case vcl::BBTYPE_ROWHEADERBAR:
450 case vcl::BBTYPE_TABLE:
451 nRole = AccessibleRole::TABLE;
452 break;
453 case vcl::BBTYPE_TABLECELL:
454 nRole = AccessibleRole::TABLE_CELL;
455 break;
456 case vcl::BBTYPE_BROWSEBOX:
457 nRole = AccessibleRole::PANEL;
458 break;
459 case vcl::BBTYPE_CHECKBOXCELL:
460 nRole = AccessibleRole::CHECK_BOX;
461 break;
463 return nRole;
466 Reference<XAccessible > SAL_CALL AccessibleBrowseBoxBase::getAccessibleAtPoint( const css::awt::Point& )
468 return nullptr;
471 void SAL_CALL AccessibleBrowseBoxBase::disposing( const css::lang::EventObject& )
473 m_xFocusWindow = nullptr;
476 sal_Int32 SAL_CALL AccessibleBrowseBoxBase::getForeground( )
478 SolarMethodGuard aGuard(getMutex());
479 ensureIsAlive();
481 Color nColor;
482 vcl::Window* pInst = mpBrowseBox->GetWindowInstance();
483 if ( pInst )
485 if ( pInst->IsControlForeground() )
486 nColor = pInst->GetControlForeground();
487 else
489 vcl::Font aFont;
490 if ( pInst->IsControlFont() )
491 aFont = pInst->GetControlFont();
492 else
493 aFont = pInst->GetFont();
494 nColor = aFont.GetColor();
498 return sal_Int32(nColor);
501 sal_Int32 SAL_CALL AccessibleBrowseBoxBase::getBackground( )
503 SolarMethodGuard aGuard(getMutex());
504 ensureIsAlive();
506 Color nColor;
507 vcl::Window* pInst = mpBrowseBox->GetWindowInstance();
508 if ( pInst )
510 if ( pInst->IsControlBackground() )
511 nColor = pInst->GetControlBackground();
512 else
513 nColor = pInst->GetBackground().GetColor();
516 return sal_Int32(nColor);
520 // XInterface
521 IMPLEMENT_FORWARD_XINTERFACE2( BrowseBoxAccessibleElement, AccessibleBrowseBoxBase, BrowseBoxAccessibleElement_Base )
523 // XTypeProvider
524 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BrowseBoxAccessibleElement, AccessibleBrowseBoxBase, BrowseBoxAccessibleElement_Base )
526 // css::accessibility::XAccessible
528 Reference< css::accessibility::XAccessibleContext > SAL_CALL BrowseBoxAccessibleElement::getAccessibleContext()
530 osl::MutexGuard aGuard( getMutex() );
531 ensureIsAlive();
532 return this;
536 BrowseBoxAccessibleElement::BrowseBoxAccessibleElement( const css::uno::Reference< css::accessibility::XAccessible >& rxParent, ::vcl::IAccessibleTableProvider& rBrowseBox,
537 const css::uno::Reference< css::awt::XWindow >& _xFocusWindow, ::vcl::AccessibleBrowseBoxObjType eObjType )
538 :AccessibleBrowseBoxBase( rxParent, rBrowseBox, _xFocusWindow, eObjType )
543 BrowseBoxAccessibleElement::BrowseBoxAccessibleElement( const css::uno::Reference< css::accessibility::XAccessible >& rxParent, ::vcl::IAccessibleTableProvider& rBrowseBox,
544 const css::uno::Reference< css::awt::XWindow >& _xFocusWindow, ::vcl::AccessibleBrowseBoxObjType eObjType,
545 const OUString& rName, const OUString& rDescription )
546 :AccessibleBrowseBoxBase( rxParent, rBrowseBox, _xFocusWindow, eObjType, rName, rDescription )
551 BrowseBoxAccessibleElement::~BrowseBoxAccessibleElement( )
556 } // namespace accessibility
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */