tdf#151001: Add support for EXPAND function
[LibreOffice.git] / svx / source / accessibility / AccessibleControlShape.cxx
blob1933d9b0b6a4b5578ca8d319991775d8c9b3182f
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 <svx/AccessibleControlShape.hxx>
21 #include <svx/AccessibleShapeInfo.hxx>
22 #include <DescriptionGenerator.hxx>
23 #include <com/sun/star/awt/XWindow.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/drawing/XControlShape.hpp>
26 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
27 #include <com/sun/star/accessibility/AccessibleRole.hpp>
28 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
29 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
30 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
31 #include <com/sun/star/reflection/ProxyFactory.hpp>
32 #include <com/sun/star/util/XModeChangeBroadcaster.hpp>
33 #include <com/sun/star/container/XContainer.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <comphelper/property.hxx>
36 #include <unotools/accessiblerelationsethelper.hxx>
37 #include <svx/IAccessibleParent.hxx>
38 #include <svx/svdouno.hxx>
39 #include <svx/ShapeTypeHandler.hxx>
40 #include <svx/SvxShapeTypes.hxx>
41 #include <comphelper/accessiblewrapper.hxx>
42 #include <svx/svdview.hxx>
43 #include <svx/svdpagv.hxx>
44 #include <svx/strings.hrc>
45 #include <vcl/svapp.hxx>
46 #include <vcl/window.hxx>
47 #include <sal/log.hxx>
48 #include <tools/debug.hxx>
49 #include <comphelper/diagnose_ex.hxx>
51 using namespace ::accessibility;
52 using namespace ::com::sun::star::accessibility;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::awt;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::util;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::reflection;
59 using namespace ::com::sun::star::drawing;
60 using namespace ::com::sun::star::container;
62 namespace
64 constexpr OUString NAME_PROPERTY_NAME = u"Name"_ustr;
65 constexpr OUString DESC_PROPERTY_NAME = u"HelpText"_ustr;
66 constexpr OUString LABEL_PROPERTY_NAME = u"Label"_ustr;
67 constexpr OUString LABEL_CONTROL_PROPERTY_NAME = u"LabelControl"_ustr;
69 // return the property which should be used as AccessibleName
70 const OUString & lcl_getPreferredAccNameProperty( const Reference< XPropertySetInfo >& _rxPSI )
72 if ( _rxPSI.is() && _rxPSI->hasPropertyByName( LABEL_PROPERTY_NAME ) )
73 return LABEL_PROPERTY_NAME;
74 else
75 return NAME_PROPERTY_NAME;
78 // determines whether or not a state which belongs to the inner context needs to be forwarded to the "composed"
79 // context
80 bool isComposedState( const sal_Int64 _nState )
82 return ( ( AccessibleStateType::INVALID != _nState )
83 && ( AccessibleStateType::DEFUNC != _nState )
84 && ( AccessibleStateType::ICONIFIED != _nState )
85 && ( AccessibleStateType::RESIZABLE != _nState )
86 && ( AccessibleStateType::SELECTABLE != _nState )
87 && ( AccessibleStateType::SHOWING != _nState )
88 && ( AccessibleStateType::MANAGES_DESCENDANTS != _nState )
89 && ( AccessibleStateType::VISIBLE != _nState )
93 /// determines whether the given control is in alive mode
94 bool isAliveMode( const Reference< XControl >& _rxControl )
96 OSL_PRECOND( _rxControl.is(), "AccessibleControlShape::isAliveMode: invalid control" );
97 return _rxControl.is() && !_rxControl->isDesignMode();
101 AccessibleControlShape::AccessibleControlShape (
102 const AccessibleShapeInfo& rShapeInfo,
103 const AccessibleShapeTreeInfo& rShapeTreeInfo)
104 : AccessibleShape (rShapeInfo, rShapeTreeInfo)
105 , m_bListeningForName( false )
106 , m_bListeningForDesc( false )
107 , m_bMultiplexingStates( false )
108 , m_bDisposeNativeContext( false )
109 , m_bWaitingForControl( false )
111 m_pChildManager = new comphelper::OWrappedAccessibleChildrenManager( comphelper::getProcessComponentContext() );
113 osl_atomic_increment( &m_refCount );
115 m_pChildManager->setOwningAccessible( this );
117 osl_atomic_decrement( &m_refCount );
120 AccessibleControlShape::~AccessibleControlShape()
122 m_pChildManager.clear();
124 if ( m_xControlContextProxy.is() )
125 m_xControlContextProxy->setDelegator( nullptr );
126 m_xControlContextProxy.clear();
127 m_xControlContextTypeAccess.clear();
128 m_xControlContextComponent.clear();
129 // this should remove the _only_ three "real" reference (means not delegated to
130 // ourself) to this proxy, and thus delete it
133 namespace {
134 Reference< XContainer > lcl_getControlContainer( const OutputDevice* _pWin, const SdrView* _pView )
136 Reference< XContainer > xReturn;
137 DBG_ASSERT( _pView, "lcl_getControlContainer: invalid view!" );
138 if ( _pView && _pView->GetSdrPageView())
140 xReturn.set(_pView->GetSdrPageView()->GetControlContainer( *_pWin ), css::uno::UNO_QUERY);
142 return xReturn;
146 void AccessibleControlShape::Init()
148 AccessibleShape::Init();
150 OSL_ENSURE( !m_xControlContextProxy.is(), "AccessibleControlShape::Init: already initialized!" );
153 // What we need to do here is merge the functionality of the AccessibleContext of our UNO control
154 // with our own AccessibleContext-related functionality.
156 // The problem is that we do not know the interfaces our "inner" context supports - this may be any
157 // XAccessibleXXX interface (or even any other) which makes sense for it.
159 // In theory, we could implement all possible interfaces ourself, and re-route all functionality to
160 // the inner context (except those we implement ourself, like XAccessibleComponent). But this is in no
161 // way future-proof - as soon as an inner context appears which implements an additional interface,
162 // we would need to adjust our implementation to support this new interface, too. Bad idea.
164 // The usual solution for such a problem is aggregation. Aggregation means using UNO's own mechanism
165 // for merging an inner with an outer component, and get a component which behaves as it is exactly one.
166 // This is what XAggregation is for. Unfortunately, aggregation requires _exact_ control over the ref count
167 // of the inner object, which we do not have at all.
168 // Bad, too.
170 // But there is a solution: com.sun.star.reflection.ProxyFactory. This service is able to create a proxy
171 // for any component, which supports _exactly_ the same interfaces as the component. In addition, it can
172 // be aggregated, as by definition the proxy's ref count is exactly 1 when returned from the factory.
173 // Sounds better. Though this yields the problem of slightly degraded performance, it's the only solution
174 // I'm aware of at the moment...
176 // get the control which belongs to our model (relative to our view)
177 const vcl::Window* pViewWindow = maShapeTreeInfo.GetWindow();
178 SdrUnoObj* pUnoObjectImpl = dynamic_cast<SdrUnoObj*>(SdrObject::getSdrObjectFromXShape(mxShape));
179 SdrView* pView = maShapeTreeInfo.GetSdrView();
180 OSL_ENSURE( pView && pViewWindow && pUnoObjectImpl, "AccessibleControlShape::Init: no view, or no view window, no SdrUnoObj!" );
182 if ( pView && pViewWindow && pUnoObjectImpl )
184 // get the context of the control - it will be our "inner" context
185 m_xUnoControl = pUnoObjectImpl->GetUnoControl( *pView, *pViewWindow->GetOutDev() );
187 if ( !m_xUnoControl.is() )
189 // the control has not yet been created. Though speaking strictly, it is a bug that
190 // our instance here is created without an existing control (because an AccessibleControlShape
191 // is a representation of a view object, and can only live if the view it should represent
192 // is complete, which implies a living control), it's by far the easiest and most riskless way
193 // to fix this here in this class.
194 // Okay, we will add as listener to the control container where we expect our control to appear.
195 OSL_ENSURE( !m_bWaitingForControl, "AccessibleControlShape::Init: already waiting for the control!" );
197 Reference< XContainer > xControlContainer = lcl_getControlContainer( pViewWindow->GetOutDev(), maShapeTreeInfo.GetSdrView() );
198 OSL_ENSURE( xControlContainer.is(), "AccessibleControlShape::Init: unable to find my ControlContainer!" );
199 if ( xControlContainer.is() )
201 xControlContainer->addContainerListener( this );
202 m_bWaitingForControl = true;
205 else
207 Reference< XModeChangeBroadcaster > xControlModes( m_xUnoControl, UNO_QUERY );
208 Reference< XAccessible > xControlAccessible( xControlModes, UNO_QUERY );
209 Reference< XAccessibleContext > xNativeControlContext;
210 if ( xControlAccessible.is() )
211 xNativeControlContext = xControlAccessible->getAccessibleContext();
212 OSL_ENSURE( xNativeControlContext.is(), "AccessibleControlShape::Init: no AccessibleContext for the control!" );
213 m_aControlContext = WeakReference< XAccessibleContext >( xNativeControlContext );
215 // add as listener to the context - we want to multiplex some states
216 if ( isAliveMode( m_xUnoControl ) && xNativeControlContext.is() )
217 { // (but only in alive mode)
218 startStateMultiplexing( );
221 // now that we have all information about our control, do some adjustments
222 adjustAccessibleRole();
223 initializeComposedState();
225 // some initialization for our child manager, which is used in alive mode only
226 if ( isAliveMode( m_xUnoControl ) )
228 sal_Int64 nStates( getAccessibleStateSet( ) );
229 m_pChildManager->setTransientChildren( nStates & AccessibleStateType::MANAGES_DESCENDANTS );
232 // finally, aggregate a proxy for the control context
233 // first a factory for the proxy
234 Reference< XProxyFactory > xFactory = ProxyFactory::create( comphelper::getProcessComponentContext() );
235 // then the proxy itself
236 if ( xNativeControlContext.is() )
238 m_xControlContextProxy = xFactory->createProxy( xNativeControlContext );
239 m_xControlContextTypeAccess.set( xNativeControlContext, UNO_QUERY_THROW );
240 m_xControlContextComponent.set( xNativeControlContext, UNO_QUERY_THROW );
242 // aggregate the proxy
243 osl_atomic_increment( &m_refCount );
244 if ( m_xControlContextProxy.is() )
246 // At this point in time, the proxy has a ref count of exactly one - in m_xControlContextProxy.
247 // Remember to _not_ reset this member unless the delegator of the proxy has been reset, too!
248 m_xControlContextProxy->setDelegator( *this );
250 osl_atomic_decrement( &m_refCount );
252 m_bDisposeNativeContext = true;
254 // Finally, we need to add ourself as mode listener to the control. In case the mode switches,
255 // we need to dispose ourself.
256 xControlModes->addModeChangeListener( this );
261 catch( const Exception& )
263 OSL_FAIL( "AccessibleControlShape::Init: could not \"aggregate\" the controls XAccessibleContext!" );
267 void SAL_CALL AccessibleControlShape::grabFocus()
269 if ( !m_xUnoControl.is() || !isAliveMode( m_xUnoControl ) )
271 // in design mode, we simply forward the request to the base class
272 AccessibleShape::grabFocus();
274 else
276 Reference< XWindow > xWindow( m_xUnoControl, UNO_QUERY );
277 OSL_ENSURE( xWindow.is(), "AccessibleControlShape::grabFocus: invalid control!" );
278 if ( xWindow.is() )
279 xWindow->setFocus();
283 OUString SAL_CALL AccessibleControlShape::getImplementationName()
285 return u"com.sun.star.comp.accessibility.AccessibleControlShape"_ustr;
288 OUString AccessibleControlShape::CreateAccessibleBaseName()
290 OUString sName;
292 ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
293 switch (nShapeType)
295 case DRAWING_CONTROL:
296 sName = "ControlShape";
297 break;
298 default:
299 sName = "UnknownAccessibleControlShape";
300 if (mxShape.is())
301 sName += ": " + mxShape->getShapeType();
304 return sName;
307 OUString
308 AccessibleControlShape::CreateAccessibleDescription()
310 DescriptionGenerator aDG (mxShape);
311 ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
312 switch (nShapeType)
314 case DRAWING_CONTROL:
316 // check if we can obtain the "Desc" property from the model
317 OUString sDesc( getControlModelStringProperty( DESC_PROPERTY_NAME ) );
318 if ( sDesc.isEmpty() )
319 { // no -> use the default
320 aDG.Initialize (STR_ObjNameSingulUno);
321 aDG.AddProperty (u"ControlBackground"_ustr, DescriptionGenerator::PropertyType::Color);
322 aDG.AddProperty ( u"ControlBorder"_ustr, DescriptionGenerator::PropertyType::Integer);
324 // ensure that we are listening to the Name property
325 m_bListeningForDesc = ensureListeningState( m_bListeningForDesc, true, DESC_PROPERTY_NAME );
327 break;
329 default:
330 aDG.Initialize (u"Unknown accessible control shape");
331 if (mxShape.is())
333 aDG.AppendString (u"service name=");
334 aDG.AppendString (mxShape->getShapeType());
338 return aDG();
341 IMPLEMENT_FORWARD_REFCOUNT( AccessibleControlShape, AccessibleShape )
342 IMPLEMENT_GET_IMPLEMENTATION_ID( AccessibleControlShape )
344 void SAL_CALL AccessibleControlShape::propertyChange( const PropertyChangeEvent& _rEvent )
346 ::osl::MutexGuard aGuard( m_aMutex );
348 // check if it is the name or the description
349 if ( _rEvent.PropertyName == NAME_PROPERTY_NAME
350 || _rEvent.PropertyName == LABEL_PROPERTY_NAME )
352 SetAccessibleName(
353 CreateAccessibleName(),
354 AccessibleContextBase::AutomaticallyCreated);
356 else if ( _rEvent.PropertyName == DESC_PROPERTY_NAME )
358 SetAccessibleDescription(
359 CreateAccessibleDescription(),
360 AccessibleContextBase::AutomaticallyCreated);
362 #if OSL_DEBUG_LEVEL > 0
363 else
365 OSL_FAIL( "AccessibleControlShape::propertyChange: where did this come from?" );
367 #endif
370 Any SAL_CALL AccessibleControlShape::queryInterface( const Type& _rType )
372 Any aReturn = AccessibleShape::queryInterface( _rType );
373 if ( !aReturn.hasValue() )
375 aReturn = AccessibleControlShape_Base::queryInterface( _rType );
376 if ( !aReturn.hasValue() && m_xControlContextProxy.is() )
377 aReturn = m_xControlContextProxy->queryAggregation( _rType );
379 return aReturn;
382 Sequence< Type > SAL_CALL AccessibleControlShape::getTypes()
384 Sequence< Type > aShapeTypes = AccessibleShape::getTypes();
385 Sequence< Type > aOwnTypes = AccessibleControlShape_Base::getTypes();
387 Sequence< Type > aAggregateTypes;
388 if ( m_xControlContextTypeAccess.is() )
389 aAggregateTypes = m_xControlContextTypeAccess->getTypes();
391 // remove duplicates
392 return comphelper::combineSequences(comphelper::concatSequences( aShapeTypes, aOwnTypes), aAggregateTypes );
395 void SAL_CALL AccessibleControlShape::notifyEvent( const AccessibleEventObject& _rEvent )
397 if ( AccessibleEventId::STATE_CHANGED == _rEvent.EventId )
399 // multiplex this change
400 sal_Int64 nLostState( 0 ), nGainedState( 0 );
401 _rEvent.OldValue >>= nLostState;
402 _rEvent.NewValue >>= nGainedState;
404 // don't multiplex states which the inner context is not responsible for
405 if ( isComposedState( nLostState ) )
406 AccessibleShape::ResetState( nLostState );
408 if ( isComposedState( nGainedState ) )
409 AccessibleShape::SetState( nGainedState );
411 else
413 AccessibleEventObject aTranslatedEvent( _rEvent );
416 ::osl::MutexGuard aGuard( m_aMutex );
418 // let the child manager translate the event
419 aTranslatedEvent.Source = *this;
420 m_pChildManager->translateAccessibleEvent( _rEvent, aTranslatedEvent );
422 // see if any of these notifications affect our child manager
423 m_pChildManager->handleChildNotification( _rEvent );
426 FireEvent( aTranslatedEvent );
430 void SAL_CALL AccessibleControlShape::modeChanged(const ModeChangeEvent& rSource)
432 // did it come from our inner context (the real one, not it's proxy!)?
433 SAL_INFO("sw.uno", "AccessibleControlShape::modeChanged");
434 Reference<XControl> xSource(rSource.Source, UNO_QUERY); // for faster compare
435 if(xSource.get() != m_xUnoControl.get())
437 SAL_WARN("sw.uno", "AccessibleControlShape::modeChanged: where did this come from?");
438 return;
440 SolarMutexGuard g;
441 // If our "pseudo-aggregated" inner context does not live anymore,
442 // we don't want to live, too. This is accomplished by asking our
443 // parent to replace this object with a new one. Disposing this
444 // object and sending notifications about the replacement are in
445 // the responsibility of our parent.
446 const bool bReplaced = mpParent->ReplaceChild(this, mxShape, 0, maShapeTreeInfo);
447 SAL_WARN_IF(!bReplaced, "sw.uno", "AccessibleControlShape::modeChanged: replacing ourselves away did fail");
450 void SAL_CALL AccessibleControlShape::disposing (const EventObject& _rSource)
452 AccessibleShape::disposing( _rSource );
455 bool AccessibleControlShape::ensureListeningState(
456 const bool _bCurrentlyListening, const bool _bNeedNewListening,
457 const OUString& _rPropertyName )
459 if ( ( _bCurrentlyListening == _bNeedNewListening ) || !ensureControlModelAccess() )
460 // nothing to do
461 return _bCurrentlyListening;
465 if ( !m_xModelPropsMeta.is() || m_xModelPropsMeta->hasPropertyByName( _rPropertyName ) )
467 // add or revoke as listener
468 if ( _bNeedNewListening )
469 m_xControlModel->addPropertyChangeListener( _rPropertyName, static_cast< XPropertyChangeListener* >( this ) );
470 else
471 m_xControlModel->removePropertyChangeListener( _rPropertyName, static_cast< XPropertyChangeListener* >( this ) );
473 else
474 OSL_FAIL( "AccessibleControlShape::ensureListeningState: this property does not exist at this model!" );
476 catch( const Exception& )
478 OSL_FAIL( "AccessibleControlShape::ensureListeningState: could not change the listening state!" );
481 return _bNeedNewListening;
484 sal_Int64 SAL_CALL AccessibleControlShape::getAccessibleChildCount( )
486 if ( !m_xUnoControl.is() )
487 return 0;
488 else if ( !isAliveMode( m_xUnoControl ) )
489 // no special action required when in design mode
490 return AccessibleShape::getAccessibleChildCount( );
491 else
493 // in alive mode, we have the full control over our children - they are determined by the children
494 // of the context of our UNO control
495 Reference< XAccessibleContext > xControlContext( m_aControlContext );
496 OSL_ENSURE( xControlContext.is(), "AccessibleControlShape::getAccessibleChildCount: control context already dead! How this!" );
497 return xControlContext.is() ? xControlContext->getAccessibleChildCount() : 0;
501 Reference< XAccessible > SAL_CALL AccessibleControlShape::getAccessibleChild( sal_Int64 i )
503 Reference< XAccessible > xChild;
504 if ( !m_xUnoControl.is() )
506 throw IndexOutOfBoundsException();
508 if ( !isAliveMode( m_xUnoControl ) )
510 // no special action required when in design mode - let the base class handle this
511 xChild = AccessibleShape::getAccessibleChild( i );
513 else
515 // in alive mode, we have the full control over our children - they are determined by the children
516 // of the context of our UNO control
518 Reference< XAccessibleContext > xControlContext( m_aControlContext );
519 OSL_ENSURE( xControlContext.is(), "AccessibleControlShape::getAccessibleChild: control context already dead! How this!" );
520 if ( xControlContext.is() )
522 Reference< XAccessible > xInnerChild( xControlContext->getAccessibleChild( i ) );
523 OSL_ENSURE( xInnerChild.is(), "AccessibleControlShape::getAccessibleChild: control context returned nonsense!" );
524 if ( xInnerChild.is() )
526 // we need to wrap this inner child into an own implementation
527 xChild = m_pChildManager->getAccessibleWrapperFor( xInnerChild );
532 #if OSL_DEBUG_LEVEL > 0
533 sal_Int64 nChildIndex = -1;
534 Reference< XAccessibleContext > xContext;
535 if ( xChild.is() )
536 xContext = xChild->getAccessibleContext( );
537 if ( xContext.is() )
538 nChildIndex = xContext->getAccessibleIndexInParent( );
539 SAL_WARN_IF( nChildIndex != i, "svx", "AccessibleControlShape::getAccessibleChild: index mismatch,"
540 " nChildIndex=" << nChildIndex << " vs i=" << i );
541 #endif
542 return xChild;
545 Reference< XAccessibleRelationSet > SAL_CALL AccessibleControlShape::getAccessibleRelationSet( )
547 rtl::Reference<utl::AccessibleRelationSetHelper> pRelationSetHelper = new utl::AccessibleRelationSetHelper;
548 ensureControlModelAccess();
549 AccessibleControlShape* pCtlAccShape = GetLabeledByControlShape();
550 if(pCtlAccShape)
552 Reference < XAccessible > xAcc (pCtlAccShape->getAccessibleContext(), UNO_QUERY);
554 css::uno::Sequence<css::uno::Reference<XAccessible>> aSequence { xAcc };
555 if( getAccessibleRole() == AccessibleRole::RADIO_BUTTON )
557 pRelationSetHelper->AddRelation( AccessibleRelation( AccessibleRelationType_MEMBER_OF, aSequence ) );
559 else
561 pRelationSetHelper->AddRelation( AccessibleRelation( AccessibleRelationType_LABELED_BY, aSequence ) );
564 return pRelationSetHelper;
567 OUString AccessibleControlShape::CreateAccessibleName()
569 ensureControlModelAccess();
571 OUString sName;
572 sal_Int16 aAccessibleRole = getAccessibleRole();
573 if ( aAccessibleRole != AccessibleRole::SHAPE
574 && aAccessibleRole != AccessibleRole::RADIO_BUTTON )
576 AccessibleControlShape* pCtlAccShape = GetLabeledByControlShape();
577 if(pCtlAccShape)
579 sName = pCtlAccShape->CreateAccessibleName();
583 if (sName.isEmpty())
585 // check if we can obtain the "Name" resp. "Label" property from the model
586 const OUString aAccNameProperty = lcl_getPreferredAccNameProperty( m_xModelPropsMeta );
587 sName = getControlModelStringProperty( aAccNameProperty );
588 if ( !sName.getLength() )
589 { // no -> use the default
590 sName = AccessibleShape::CreateAccessibleName();
594 // now that somebody first asked us for our name, ensure that we are listening to name changes on the model
595 m_bListeningForName = ensureListeningState( m_bListeningForName, true, lcl_getPreferredAccNameProperty( m_xModelPropsMeta ) );
597 return sName;
600 void SAL_CALL AccessibleControlShape::disposing()
602 // ensure we're not listening
603 m_bListeningForName = ensureListeningState( m_bListeningForName, false, lcl_getPreferredAccNameProperty( m_xModelPropsMeta ) );
604 m_bListeningForDesc = ensureListeningState( m_bListeningForDesc, false, DESC_PROPERTY_NAME );
606 if ( m_bMultiplexingStates )
607 stopStateMultiplexing( );
609 // dispose the child cache/map
610 m_pChildManager->dispose();
612 // release the model
613 m_xControlModel.clear();
614 m_xModelPropsMeta.clear();
615 m_aControlContext = WeakReference< XAccessibleContext >();
617 // stop listening at the control container (should never be necessary here, but who knows...)
618 if ( m_bWaitingForControl )
620 OSL_FAIL( "AccessibleControlShape::disposing: this should never happen!" );
621 if (auto pWindow = maShapeTreeInfo.GetWindow())
623 Reference< XContainer > xContainer = lcl_getControlContainer( pWindow->GetOutDev(), maShapeTreeInfo.GetSdrView() );
624 if ( xContainer.is() )
626 m_bWaitingForControl = false;
627 xContainer->removeContainerListener( this );
632 // forward the disposal to our inner context
633 if ( m_bDisposeNativeContext )
635 // don't listen for mode changes anymore
636 Reference< XModeChangeBroadcaster > xControlModes( m_xUnoControl, UNO_QUERY );
637 OSL_ENSURE( xControlModes.is(), "AccessibleControlShape::disposing: don't have a mode broadcaster anymore!" );
638 if ( xControlModes.is() )
639 xControlModes->removeModeChangeListener( this );
641 if ( m_xControlContextComponent.is() )
642 m_xControlContextComponent->dispose();
643 // do _not_ clear m_xControlContextProxy! This has to be done in the dtor for correct ref-count handling
645 // no need to dispose the proxy/inner context anymore
646 m_bDisposeNativeContext = false;
649 m_xUnoControl.clear();
651 // let the base do its stuff
652 AccessibleShape::disposing();
655 bool AccessibleControlShape::ensureControlModelAccess()
657 if ( m_xControlModel.is() )
658 return true;
662 Reference< XControlShape > xShape( mxShape, UNO_QUERY );
663 if ( xShape.is() )
664 m_xControlModel.set(xShape->getControl(), css::uno::UNO_QUERY);
666 if ( m_xControlModel.is() )
667 m_xModelPropsMeta = m_xControlModel->getPropertySetInfo();
669 catch( const Exception& )
671 TOOLS_WARN_EXCEPTION( "svx", "AccessibleControlShape::ensureControlModelAccess" );
674 return m_xControlModel.is();
677 void AccessibleControlShape::startStateMultiplexing()
679 OSL_PRECOND( !m_bMultiplexingStates, "AccessibleControlShape::startStateMultiplexing: already multiplexing!" );
681 #if OSL_DEBUG_LEVEL > 0
682 // we should have a control, and it should be in alive mode
683 OSL_PRECOND( isAliveMode( m_xUnoControl ),
684 "AccessibleControlShape::startStateMultiplexing: should be done in alive mode only!" );
685 #endif
686 // we should have the native context of the control
687 Reference< XAccessibleEventBroadcaster > xBroadcaster( m_aControlContext.get(), UNO_QUERY );
688 OSL_ENSURE( xBroadcaster.is(), "AccessibleControlShape::startStateMultiplexing: no AccessibleEventBroadcaster on the native context!" );
690 if ( xBroadcaster.is() )
692 xBroadcaster->addAccessibleEventListener( this );
693 m_bMultiplexingStates = true;
697 void AccessibleControlShape::stopStateMultiplexing()
699 OSL_PRECOND( m_bMultiplexingStates, "AccessibleControlShape::stopStateMultiplexing: not multiplexing!" );
701 // we should have the native context of the control
702 Reference< XAccessibleEventBroadcaster > xBroadcaster( m_aControlContext.get(), UNO_QUERY );
703 OSL_ENSURE( xBroadcaster.is(), "AccessibleControlShape::stopStateMultiplexing: no AccessibleEventBroadcaster on the native context!" );
705 if ( xBroadcaster.is() )
707 xBroadcaster->removeAccessibleEventListener( this );
708 m_bMultiplexingStates = false;
712 OUString AccessibleControlShape::getControlModelStringProperty( const OUString& _rPropertyName ) const
714 OUString sReturn;
717 if ( const_cast< AccessibleControlShape* >( this )->ensureControlModelAccess() )
719 if ( !m_xModelPropsMeta.is() || m_xModelPropsMeta->hasPropertyByName( _rPropertyName ) )
720 // ask only if a) the control does not have a PropertySetInfo object or b) it has, and the
721 // property in question is available
722 m_xControlModel->getPropertyValue( _rPropertyName ) >>= sReturn;
725 catch( const Exception& )
727 TOOLS_WARN_EXCEPTION( "svx", "OAccessibleControlContext::getModelStringProperty" );
729 return sReturn;
732 void AccessibleControlShape::adjustAccessibleRole( )
734 // if we're in design mode, we are a simple SHAPE, in alive mode, we use the role of our inner context
735 if ( !isAliveMode( m_xUnoControl ) )
736 return;
738 // we're in alive mode -> determine the role of the inner context
739 Reference< XAccessibleContext > xNativeContext( m_aControlContext );
740 OSL_PRECOND( xNativeContext.is(), "AccessibleControlShape::adjustAccessibleRole: no inner context!" );
741 if ( xNativeContext.is() )
742 SetAccessibleRole( xNativeContext->getAccessibleRole( ) );
745 #ifdef DBG_UTIL
747 bool AccessibleControlShape::SetState( sal_Int64 _nState )
749 OSL_ENSURE( !isAliveMode( m_xUnoControl ) || !isComposedState( _nState ),
750 "AccessibleControlShape::SetState: a state which should be determined by the control context is set from outside!" );
751 return AccessibleShape::SetState( _nState );
753 #endif // DBG_UTIL
755 void AccessibleControlShape::initializeComposedState()
757 if ( !isAliveMode( m_xUnoControl ) )
758 // no action necessary for design mode
759 return;
761 // we need to reset some states of the composed set, because they either do not apply
762 // for controls in alive mode, or are in the responsibility of the UNO-control, anyway
763 mnStateSet &= ~AccessibleStateType::ENABLED; // this is controlled by the UNO-control
764 mnStateSet &= ~AccessibleStateType::SENSITIVE; // this is controlled by the UNO-control
765 mnStateSet &= ~AccessibleStateType::FOCUSABLE; // this is controlled by the UNO-control
766 mnStateSet &= ~AccessibleStateType::SELECTABLE; // this does not hold for an alive UNO-control
768 // get my inner context
769 Reference< XAccessibleContext > xInnerContext( m_aControlContext );
770 OSL_PRECOND( xInnerContext.is(), "AccessibleControlShape::initializeComposedState: no inner context!" );
771 if ( !xInnerContext.is() )
772 return;
774 // get all states of the inner context
775 sal_Int64 nInnerStates( xInnerContext->getAccessibleStateSet() );
777 // look which one are to be propagated to the composed context
778 for ( int i = 0; i < 63; ++i )
780 sal_Int64 nState = sal_Int64(1) << i;
781 if ( (nInnerStates & nState) && isComposedState( nState ) )
783 mnStateSet |= nState;
788 void SAL_CALL AccessibleControlShape::elementInserted( const css::container::ContainerEvent& _rEvent )
790 Reference< XContainer > xContainer( _rEvent.Source, UNO_QUERY );
791 Reference< XControl > xControl( _rEvent.Element, UNO_QUERY );
793 OSL_ENSURE( xContainer.is() && xControl.is(),
794 "AccessibleControlShape::elementInserted: invalid event description!" );
796 if ( !xControl.is() )
797 return;
799 ensureControlModelAccess();
801 Reference< XInterface > xNewNormalized( xControl->getModel(), UNO_QUERY );
802 Reference< XInterface > xMyModelNormalized( m_xControlModel, UNO_QUERY );
803 if ( !(xNewNormalized && xMyModelNormalized) )
804 return;
806 // now finally the control for the model we're responsible for has been inserted into the container
807 Reference< XInterface > xKeepAlive( *this );
809 // first, we're not interested in any more container events
810 if ( xContainer.is() )
812 xContainer->removeContainerListener( this );
813 m_bWaitingForControl = false;
816 // second, we need to replace ourself with a new version, which now can be based on the
817 // control
818 OSL_VERIFY( mpParent->ReplaceChild ( this, mxShape, 0, maShapeTreeInfo ) );
821 void SAL_CALL AccessibleControlShape::elementRemoved( const css::container::ContainerEvent& )
823 // not interested in
826 void SAL_CALL AccessibleControlShape::elementReplaced( const css::container::ContainerEvent& )
828 // not interested in
831 AccessibleControlShape* AccessibleControlShape::GetLabeledByControlShape( )
833 if(m_xControlModel.is())
835 Any sCtlLabelBy;
836 // get the "label by" property value of the control
837 if (::comphelper::hasProperty(LABEL_CONTROL_PROPERTY_NAME, m_xControlModel))
839 sCtlLabelBy = m_xControlModel->getPropertyValue(LABEL_CONTROL_PROPERTY_NAME);
840 if( sCtlLabelBy.hasValue() )
842 Reference< XPropertySet > xAsSet (sCtlLabelBy, UNO_QUERY);
843 AccessibleControlShape* pCtlAccShape = mpParent->GetAccControlShapeFromModel(xAsSet.get());
844 return pCtlAccShape;
848 return nullptr;
851 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */