fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svx / source / accessibility / AccessibleControlShape.cxx
blob0f56193ba0d13f872c75bedd9e969dfea27ea63c
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 "svx/DescriptionGenerator.hxx"
23 #include <com/sun/star/drawing/XControlShape.hpp>
24 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
25 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
26 #include <com/sun/star/form/FormComponentType.hpp>
27 #include <com/sun/star/reflection/ProxyFactory.hpp>
28 #include <com/sun/star/container/XContainer.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <unotools/accessiblestatesethelper.hxx>
31 #include <svx/svdouno.hxx>
32 #include "svx/unoapi.hxx"
33 #include <svx/ShapeTypeHandler.hxx>
34 #include <svx/SvxShapeTypes.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
36 #include <comphelper/accessiblewrapper.hxx>
37 #include <svx/svdview.hxx>
38 #include <svx/svdpagv.hxx>
39 #include "svx/svdstr.hrc"
40 #include <algorithm>
42 using namespace ::accessibility;
43 using namespace ::com::sun::star::accessibility;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::awt;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::util;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::reflection;
50 using namespace ::com::sun::star::drawing;
51 using namespace ::com::sun::star::container;
53 //--------------------------------------------------------------------
54 namespace
56 //................................................................
57 const OUString& lcl_getNamePropertyName( )
59 static OUString s_sNamePropertyName( "Name" );
60 return s_sNamePropertyName;
62 //................................................................
63 const OUString& lcl_getDescPropertyName( )
65 static OUString s_sDescPropertyDesc( "HelpText" );
66 return s_sDescPropertyDesc;
68 //................................................................
69 const OUString& lcl_getLabelPropertyName( )
71 static OUString s_sLabelPropertyLabel( "Label" );
72 return s_sLabelPropertyLabel;
74 //................................................................
75 // return the property which should be used as AccessibleName
76 const OUString& lcl_getPreferredAccNameProperty( const Reference< XPropertySetInfo >& _rxPSI )
78 if ( _rxPSI.is() && _rxPSI->hasPropertyByName( lcl_getLabelPropertyName() ) )
79 return lcl_getLabelPropertyName();
80 else
81 return lcl_getNamePropertyName();
84 //................................................................
85 // determines whether or not a state which belongs to the inner context needs to be forwarded to the "composed"
86 // context
87 bool isComposedState( const sal_Int16 _nState )
89 return ( ( AccessibleStateType::INVALID != _nState )
90 && ( AccessibleStateType::DEFUNC != _nState )
91 && ( AccessibleStateType::ICONIFIED != _nState )
92 && ( AccessibleStateType::RESIZABLE != _nState )
93 && ( AccessibleStateType::SELECTABLE != _nState )
94 && ( AccessibleStateType::SHOWING != _nState )
95 && ( AccessibleStateType::MANAGES_DESCENDANTS != _nState )
96 && ( AccessibleStateType::VISIBLE != _nState )
100 //................................................................
101 /** determines whether the given control is in alive mode
103 inline bool isAliveMode( const Reference< XControl >& _rxControl )
105 OSL_PRECOND( _rxControl.is(), "AccessibleControlShape::isAliveMode: invalid control" );
106 return _rxControl.is() && !_rxControl->isDesignMode();
110 //=============================================================================
111 //= AccessibleControlShape
112 //=============================================================================
114 //-----------------------------------------------------------------------------
115 AccessibleControlShape::AccessibleControlShape (
116 const AccessibleShapeInfo& rShapeInfo,
117 const AccessibleShapeTreeInfo& rShapeTreeInfo)
118 : AccessibleShape (rShapeInfo, rShapeTreeInfo)
119 , m_bListeningForName( false )
120 , m_bListeningForDesc( false )
121 , m_bMultiplexingStates( false )
122 , m_bDisposeNativeContext( false )
123 , m_bWaitingForControl( false )
125 m_pChildManager = new comphelper::OWrappedAccessibleChildrenManager( comphelper::getProcessComponentContext() );
126 m_pChildManager->acquire();
128 osl_atomic_increment( &m_refCount );
130 m_pChildManager->setOwningAccessible( this );
132 osl_atomic_decrement( &m_refCount );
135 //-----------------------------------------------------------------------------
136 AccessibleControlShape::~AccessibleControlShape (void)
138 m_pChildManager->release();
139 m_pChildManager = NULL;
141 if ( m_xControlContextProxy.is() )
142 m_xControlContextProxy->setDelegator( NULL );
143 m_xControlContextProxy.clear();
144 m_xControlContextTypeAccess.clear();
145 m_xControlContextComponent.clear();
146 // this should remove the _only_ three "real" reference (means not delegated to
147 // ourself) to this proxy, and thus delete it
150 //-----------------------------------------------------------------------------
151 SdrObject* AccessibleControlShape::getSdrObject() const
153 return GetSdrObjectFromXShape (mxShape);
156 namespace {
157 Reference< XContainer > lcl_getControlContainer( const Window* _pWin, const SdrView* _pView )
159 Reference< XContainer > xReturn;
160 DBG_ASSERT( _pView, "lcl_getControlContainer: invalid view!" );
161 if ( _pView && _pView->GetSdrPageView())
163 xReturn = xReturn.query( _pView->GetSdrPageView()->GetControlContainer( *_pWin ) );
165 return xReturn;
169 //-----------------------------------------------------------------------------
170 void AccessibleControlShape::Init()
172 AccessibleShape::Init();
174 OSL_ENSURE( !m_xControlContextProxy.is(), "AccessibleControlShape::Init: already initialized!" );
177 // What we need to do here is merge the functionality of the AccessibleContext of our UNO control
178 // with our own AccessibleContext-related functionality.
180 // The problem is that we do not know the interfaces our "inner" context supports - this may be any
181 // XAccessibleXXX interface (or even any other) which makes sense for it.
183 // In theory, we could implement all possible interfaces ourself, and re-route all functionality to
184 // the inner context (except those we implement ourself, like XAccessibleComponent). But this is in no
185 // way future-proof - as soon as an inner context appears which implements an additional interface,
186 // we would need to adjust our implementation to support this new interface, too. Bad idea.
188 // The usual solution for such a problem is aggregation. Aggregation means using UNO's own meachnisms
189 // for merging an inner with an outer component, and get a component which behaves as it is exactly one.
190 // This is what XAggregation is for. Unfortunately, aggregation requires _exact_ control over the ref count
191 // of the inner object, which we do not have at all.
192 // Bad, too.
194 // But there is a solution: com.sun.star.reflection.ProxyFactory. This service is able to create a proxy
195 // for any component, which supports _exactly_ the same interfaces as the component. In addition, it can
196 // be aggregated, as by definition the proxy's ref count is exactly 1 when returned from the factory.
197 // Sounds better. Though this yields the problem of slightly degraded performance, it's the only solution
198 // I'm aware of at the moment .....
200 // get the control which belongs to our model (relative to our view)
201 const Window* pViewWindow = maShapeTreeInfo.GetWindow();
202 SdrUnoObj* pUnoObjectImpl = PTR_CAST( SdrUnoObj, getSdrObject() );
203 SdrView* pView = maShapeTreeInfo.GetSdrView();
204 OSL_ENSURE( pView && pViewWindow && pUnoObjectImpl, "AccessibleControlShape::Init: no view, or no view window, no SdrUnoObj!" );
206 if ( pView && pViewWindow && pUnoObjectImpl )
208 // .................................................................
209 // get the context of the control - it will be our "inner" context
210 m_xUnoControl = pUnoObjectImpl->GetUnoControl( *pView, *pViewWindow );
212 if ( !m_xUnoControl.is() )
214 // the control has not yet been created. Though speaking strictly, it is a bug that
215 // our instance here is created without an existing control (because an AccessibleControlShape
216 // is a representation of a view object, and can only live if the view it should represent
217 // is complete, which implies a living control), it's by far the easiest and most riskless way
218 // to fix this here in this class.
219 // Okay, we will add as listener to the control container where we expect our control to appear.
220 OSL_ENSURE( !m_bWaitingForControl, "AccessibleControlShape::Init: already waiting for the control!" );
222 Reference< XContainer > xControlContainer = lcl_getControlContainer( pViewWindow, maShapeTreeInfo.GetSdrView() );
223 OSL_ENSURE( xControlContainer.is(), "AccessibleControlShape::Init: unable to find my ControlContainer!" );
224 if ( xControlContainer.is() )
226 xControlContainer->addContainerListener( this );
227 m_bWaitingForControl = true;
230 else
232 Reference< XModeChangeBroadcaster > xControlModes( m_xUnoControl, UNO_QUERY );
233 Reference< XAccessible > xControlAccessible( xControlModes, UNO_QUERY );
234 Reference< XAccessibleContext > xNativeControlContext;
235 if ( xControlAccessible.is() )
236 xNativeControlContext = xControlAccessible->getAccessibleContext();
237 OSL_ENSURE( xNativeControlContext.is(), "AccessibleControlShape::Init: no AccessibleContext for the control!" );
238 m_aControlContext = WeakReference< XAccessibleContext >( xNativeControlContext );
240 // .................................................................
241 // add as listener to the context - we want to multiplex some states
242 if ( isAliveMode( m_xUnoControl ) && xNativeControlContext.is() )
243 { // (but only in alive mode)
244 startStateMultiplexing( );
247 // now that we have all information about our control, do some adjustments
248 adjustAccessibleRole();
249 initializeComposedState();
251 // some initialization for our child manager, which is used in alive mode only
252 if ( isAliveMode( m_xUnoControl ) )
254 Reference< XAccessibleStateSet > xStates( getAccessibleStateSet( ) );
255 OSL_ENSURE( xStates.is(), "AccessibleControlShape::AccessibleControlShape: no inner state set!" );
256 m_pChildManager->setTransientChildren( !xStates.is() || xStates->contains( AccessibleStateType::MANAGES_DESCENDANTS ) );
259 // .................................................................
260 // finally, aggregate a proxy for the control context
261 // first a factory for the proxy
262 Reference< XProxyFactory > xFactory = ProxyFactory::create( comphelper::getProcessComponentContext() );
263 // then the proxy itself
264 if ( xNativeControlContext.is() )
266 m_xControlContextProxy = xFactory->createProxy( xNativeControlContext );
267 m_xControlContextTypeAccess.set( xNativeControlContext, UNO_QUERY_THROW );
268 m_xControlContextComponent.set( xNativeControlContext, UNO_QUERY_THROW );
270 // aggregate the proxy
271 osl_atomic_increment( &m_refCount );
272 if ( m_xControlContextProxy.is() )
274 // At this point in time, the proxy has a ref count of exactly one - in m_xControlContextProxy.
275 // Remember to _not_ reset this member unles the delegator of the proxy has been reset, too!
276 m_xControlContextProxy->setDelegator( *this );
278 osl_atomic_decrement( &m_refCount );
280 m_bDisposeNativeContext = true;
282 // Finally, we need to add ourself as mode listener to the control. In case the mode switches,
283 // we need to dispose ourself.
284 xControlModes->addModeChangeListener( this );
289 catch( const Exception& )
291 OSL_FAIL( "AccessibleControlShape::Init: could not \"aggregate\" the controls XAccessibleContext!" );
295 //-----------------------------------------------------------------------------
296 Reference< XAccessibleContext > SAL_CALL AccessibleControlShape::getAccessibleContext(void) throw (RuntimeException)
298 return AccessibleShape::getAccessibleContext ();
302 //-----------------------------------------------------------------------------
303 void SAL_CALL AccessibleControlShape::grabFocus(void) throw (RuntimeException)
305 if ( !m_xUnoControl.is() || !isAliveMode( m_xUnoControl ) )
307 // in design mode, we simply forward the request to the base class
308 AccessibleShape::grabFocus();
310 else
312 Reference< XWindow > xWindow( m_xUnoControl, UNO_QUERY );
313 OSL_ENSURE( xWindow.is(), "AccessibleControlShape::grabFocus: invalid control!" );
314 if ( xWindow.is() )
315 xWindow->setFocus();
319 //-----------------------------------------------------------------------------
320 OUString SAL_CALL AccessibleControlShape::getImplementationName(void) throw (RuntimeException)
322 return OUString( "com.sun.star.comp.accessibility.AccessibleControlShape" );
325 //-----------------------------------------------------------------------------
326 OUString AccessibleControlShape::CreateAccessibleBaseName(void) throw (RuntimeException)
328 OUString sName;
330 ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
331 switch (nShapeType)
333 case DRAWING_CONTROL:
334 sName = "ControlShape";
335 break;
336 default:
337 sName = "UnknownAccessibleControlShape";
338 Reference< XShapeDescriptor > xDescriptor (mxShape, UNO_QUERY);
339 if (xDescriptor.is())
340 sName += ": " + xDescriptor->getShapeType();
343 return sName;
349 //--------------------------------------------------------------------
350 OUString
351 AccessibleControlShape::CreateAccessibleDescription (void)
352 throw (RuntimeException)
354 DescriptionGenerator aDG (mxShape);
355 ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
356 switch (nShapeType)
358 case DRAWING_CONTROL:
360 // check if we can obtain the "Desc" property from the model
361 OUString sDesc( getControlModelStringProperty( lcl_getDescPropertyName() ) );
362 if ( sDesc.isEmpty() )
363 { // no -> use the default
364 aDG.Initialize (STR_ObjNameSingulUno);
365 aDG.AddProperty ("ControlBackground", DescriptionGenerator::COLOR, "");
366 aDG.AddProperty ( "ControlBorder", DescriptionGenerator::INTEGER, "");
368 // ensure that we are listening to the Name property
369 m_bListeningForDesc = ensureListeningState( m_bListeningForDesc, true, lcl_getDescPropertyName() );
371 break;
373 default:
374 aDG.Initialize ("Unknown accessible control shape");
375 Reference< XShapeDescriptor > xDescriptor (mxShape, UNO_QUERY);
376 if (xDescriptor.is())
378 aDG.AppendString ("service name=");
379 aDG.AppendString (xDescriptor->getShapeType());
383 return aDG();
386 //--------------------------------------------------------------------
387 IMPLEMENT_FORWARD_REFCOUNT( AccessibleControlShape, AccessibleShape )
388 IMPLEMENT_GET_IMPLEMENTATION_ID( AccessibleControlShape )
390 //--------------------------------------------------------------------
391 void SAL_CALL AccessibleControlShape::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException)
393 ::osl::MutexGuard aGuard( maMutex );
395 // check if it is the name or the description
396 if ( _rEvent.PropertyName == lcl_getNamePropertyName()
397 || _rEvent.PropertyName == lcl_getLabelPropertyName() )
399 SetAccessibleName(
400 CreateAccessibleName(),
401 AccessibleContextBase::AutomaticallyCreated);
403 else if ( _rEvent.PropertyName == lcl_getDescPropertyName() )
405 SetAccessibleDescription(
406 CreateAccessibleDescription(),
407 AccessibleContextBase::AutomaticallyCreated);
409 #if OSL_DEBUG_LEVEL > 0
410 else
412 OSL_FAIL( "AccessibleControlShape::propertyChange: where did this come from?" );
414 #endif
417 //--------------------------------------------------------------------
418 Any SAL_CALL AccessibleControlShape::queryInterface( const Type& _rType ) throw (RuntimeException)
420 Any aReturn = AccessibleShape::queryInterface( _rType );
421 if ( !aReturn.hasValue() )
423 aReturn = AccessibleControlShape_Base::queryInterface( _rType );
424 if ( !aReturn.hasValue() && m_xControlContextProxy.is() )
425 aReturn = m_xControlContextProxy->queryAggregation( _rType );
427 return aReturn;
430 //--------------------------------------------------------------------
431 Sequence< Type > SAL_CALL AccessibleControlShape::getTypes() throw (RuntimeException)
433 Sequence< Type > aShapeTypes = AccessibleShape::getTypes();
434 Sequence< Type > aOwnTypes = AccessibleControlShape_Base::getTypes();
436 Sequence< Type > aAggregateTypes;
437 if ( m_xControlContextTypeAccess.is() )
438 aAggregateTypes = m_xControlContextTypeAccess->getTypes();
440 Sequence< Type > aAllTypes = comphelper::concatSequences( aShapeTypes, aOwnTypes, aAggregateTypes );
442 // remove duplicates
443 Type* pBegin = aAllTypes.getArray();
444 Type* pEnd = pBegin + aAllTypes.getLength();
445 while ( pBegin != pEnd )
447 Type aThisRoundType = *pBegin;
448 if ( ++pBegin != pEnd )
450 pEnd = ::std::remove( pBegin, pEnd, aThisRoundType );
451 // now all types between begin and (the old) end which equal aThisRoundType
452 // are moved behind the new end
455 aAllTypes.realloc( pEnd - aAllTypes.getArray() );
457 return aAllTypes;
460 //--------------------------------------------------------------------
461 void SAL_CALL AccessibleControlShape::notifyEvent( const AccessibleEventObject& _rEvent ) throw (RuntimeException)
463 if ( AccessibleEventId::STATE_CHANGED == _rEvent.EventId )
465 // multiplex this change
466 sal_Int16 nLostState( 0 ), nGainedState( 0 );
467 _rEvent.OldValue >>= nLostState;
468 _rEvent.NewValue >>= nGainedState;
470 // don't multiplex states which the inner context is not resposible for
471 if ( isComposedState( nLostState ) )
472 AccessibleShape::ResetState( nLostState );
474 if ( isComposedState( nGainedState ) )
475 AccessibleShape::SetState( nGainedState );
477 else
479 AccessibleEventObject aTranslatedEvent( _rEvent );
482 ::osl::MutexGuard aGuard( maMutex );
484 // let the child manager translate the event
485 aTranslatedEvent.Source = *this;
486 m_pChildManager->translateAccessibleEvent( _rEvent, aTranslatedEvent );
488 // see if any of these notifications affect our child manager
489 m_pChildManager->handleChildNotification( _rEvent );
492 FireEvent( aTranslatedEvent );
496 //--------------------------------------------------------------------
497 void SAL_CALL AccessibleControlShape::modeChanged( const ModeChangeEvent& _rSource ) throw (RuntimeException)
499 // did it come from our inner context (the real one, not it's proxy!)?
500 OSL_TRACE ("AccessibleControlShape::modeChanged");
501 Reference< XControl > xSource( _rSource.Source, UNO_QUERY ); // for faster compare
502 if ( xSource.get() == m_xUnoControl.get() )
504 // If our "pseudo-aggregated" inner context does not live anymore,
505 // we don't want to live, too. This is accomplished by asking our
506 // parent to replace this object with a new one. Disposing this
507 // object and sending notifications about the replacement are in
508 // the responsibility of our parent.
509 OSL_VERIFY( mpParent->ReplaceChild ( this, mxShape, mnIndex, maShapeTreeInfo ) );
511 #if OSL_DEBUG_LEVEL > 0
512 else
513 OSL_FAIL( "AccessibleControlShape::modeChanged: where did this come from?" );
514 #endif
517 //--------------------------------------------------------------------
518 void SAL_CALL AccessibleControlShape::disposing (const EventObject& _rSource) throw (RuntimeException)
520 AccessibleShape::disposing( _rSource );
523 //--------------------------------------------------------------------
524 bool AccessibleControlShape::ensureListeningState(
525 const bool _bCurrentlyListening, const bool _bNeedNewListening,
526 const OUString& _rPropertyName )
528 if ( ( _bCurrentlyListening == _bNeedNewListening ) || !ensureControlModelAccess() )
529 // nothing to do
530 return _bCurrentlyListening;
534 if ( !m_xModelPropsMeta.is() || m_xModelPropsMeta->hasPropertyByName( _rPropertyName ) )
536 // add or revoke as listener
537 if ( _bNeedNewListening )
538 m_xControlModel->addPropertyChangeListener( _rPropertyName, static_cast< XPropertyChangeListener* >( this ) );
539 else
540 m_xControlModel->removePropertyChangeListener( _rPropertyName, static_cast< XPropertyChangeListener* >( this ) );
542 else
543 OSL_FAIL( "AccessibleControlShape::ensureListeningState: this property does not exist at this model!" );
545 catch( const Exception& e )
547 (void)e; // make compiler happy
548 OSL_FAIL( "AccessibleControlShape::ensureListeningState: could not change the listening state!" );
551 return _bNeedNewListening;
554 //--------------------------------------------------------------------
555 sal_Int32 SAL_CALL AccessibleControlShape::getAccessibleChildCount( ) throw(RuntimeException)
557 if ( !m_xUnoControl.is() )
558 return 0;
559 else if ( !isAliveMode( m_xUnoControl ) )
560 // no special action required when in design mode
561 return AccessibleShape::getAccessibleChildCount( );
562 else
564 // in alive mode, we have the full control over our children - they are determined by the children
565 // of the context of our UNO control
566 Reference< XAccessibleContext > xControlContext( m_aControlContext );
567 OSL_ENSURE( xControlContext.is(), "AccessibleControlShape::getAccessibleChildCount: control context already dead! How this!" );
568 return xControlContext.is() ? xControlContext->getAccessibleChildCount() : 0;
572 //--------------------------------------------------------------------
573 Reference< XAccessible > SAL_CALL AccessibleControlShape::getAccessibleChild( sal_Int32 i ) throw(IndexOutOfBoundsException, RuntimeException)
575 Reference< XAccessible > xChild;
576 if ( !m_xUnoControl.is() )
578 throw IndexOutOfBoundsException();
580 if ( !isAliveMode( m_xUnoControl ) )
582 // no special action required when in design mode - let the base class handle this
583 xChild = AccessibleShape::getAccessibleChild( i );
585 else
587 // in alive mode, we have the full control over our children - they are determined by the children
588 // of the context of our UNO control
590 Reference< XAccessibleContext > xControlContext( m_aControlContext );
591 OSL_ENSURE( xControlContext.is(), "AccessibleControlShape::getAccessibleChildCount: control context already dead! How this!" );
592 if ( xControlContext.is() )
594 Reference< XAccessible > xInnerChild( xControlContext->getAccessibleChild( i ) );
595 OSL_ENSURE( xInnerChild.is(), "AccessibleControlShape::getAccessibleChild: control context returned nonsense!" );
596 if ( xInnerChild.is() )
598 // we need to wrap this inner child into an own implementation
599 xChild = m_pChildManager->getAccessibleWrapperFor( xInnerChild );
604 #if OSL_DEBUG_LEVEL > 0
605 sal_Int32 nChildIndex = -1;
606 Reference< XAccessibleContext > xContext;
607 if ( xChild.is() )
608 xContext = xChild->getAccessibleContext( );
609 if ( xContext.is() )
610 nChildIndex = xContext->getAccessibleIndexInParent( );
611 OSL_ENSURE( nChildIndex == i, "AccessibleControlShape::getAccessibleChild: index mismatch!" );
612 #endif
613 return xChild;
616 //--------------------------------------------------------------------
617 Reference< XAccessibleRelationSet > SAL_CALL AccessibleControlShape::getAccessibleRelationSet( ) throw (RuntimeException)
619 // TODO
620 return AccessibleShape::getAccessibleRelationSet( );
623 //--------------------------------------------------------------------
624 OUString AccessibleControlShape::CreateAccessibleName (void) throw (RuntimeException)
626 ensureControlModelAccess();
628 // check if we can obtain the "Name" resp. "Label" property from the model
629 const OUString& rAccNameProperty = lcl_getPreferredAccNameProperty( m_xModelPropsMeta );
631 OUString sName( getControlModelStringProperty( rAccNameProperty ) );
632 if ( sName.isEmpty() )
633 { // no -> use the default
634 sName = AccessibleShape::CreateAccessibleName();
637 // now that somebody first asked us for our name, ensure that we are listening to name changes on the model
638 m_bListeningForName = ensureListeningState( m_bListeningForName, true, lcl_getPreferredAccNameProperty( m_xModelPropsMeta ) );
640 return sName;
643 //--------------------------------------------------------------------
644 void SAL_CALL AccessibleControlShape::disposing (void)
646 // ensure we're not listening
647 m_bListeningForName = ensureListeningState( m_bListeningForName, false, lcl_getPreferredAccNameProperty( m_xModelPropsMeta ) );
648 m_bListeningForDesc = ensureListeningState( m_bListeningForDesc, false, lcl_getDescPropertyName() );
650 if ( m_bMultiplexingStates )
651 stopStateMultiplexing( );
653 // dispose the child cache/map
654 m_pChildManager->dispose();
656 // release the model
657 m_xControlModel.clear();
658 m_xModelPropsMeta.clear();
659 m_aControlContext = WeakReference< XAccessibleContext >();
661 // stop listening at the control container (should never be necessary here, but who knows ....)
662 if ( m_bWaitingForControl )
664 OSL_FAIL( "AccessibleControlShape::disposing: this should never happen!" );
665 Reference< XContainer > xContainer = lcl_getControlContainer( maShapeTreeInfo.GetWindow(), maShapeTreeInfo.GetSdrView() );
666 if ( xContainer.is() )
668 m_bWaitingForControl = false;
669 xContainer->removeContainerListener( this );
673 // forward the disposel to our inner context
674 if ( m_bDisposeNativeContext )
676 // don't listen for mode changes anymore
677 Reference< XModeChangeBroadcaster > xControlModes( m_xUnoControl, UNO_QUERY );
678 OSL_ENSURE( xControlModes.is(), "AccessibleControlShape::disposing: don't have an mode broadcaster anymore!" );
679 if ( xControlModes.is() )
680 xControlModes->removeModeChangeListener( this );
682 if ( m_xControlContextComponent.is() )
683 m_xControlContextComponent->dispose();
684 // do _not_ clear m_xControlContextProxy! This has to be done in the dtor for correct ref-count handling
686 // no need to dispose the proxy/inner context anymore
687 m_bDisposeNativeContext = false;
690 m_xUnoControl.clear();
692 // let the base do it's stuff
693 AccessibleShape::disposing();
696 //--------------------------------------------------------------------
697 sal_Bool AccessibleControlShape::ensureControlModelAccess() SAL_THROW(())
699 if ( m_xControlModel.is() )
700 return sal_True;
704 Reference< XControlShape > xShape( mxShape, UNO_QUERY );
705 if ( xShape.is() )
706 m_xControlModel = m_xControlModel.query( xShape->getControl() );
708 if ( m_xControlModel.is() )
709 m_xModelPropsMeta = m_xControlModel->getPropertySetInfo();
711 catch( const Exception& e )
713 (void)e; // make compiler happy
714 OSL_FAIL( "AccessibleControlShape::ensureControlModelAccess: caught an exception!" );
717 return m_xControlModel.is();
720 //--------------------------------------------------------------------
721 void AccessibleControlShape::startStateMultiplexing()
723 OSL_PRECOND( !m_bMultiplexingStates, "AccessibleControlShape::startStateMultiplexing: already multiplexing!" );
725 #if OSL_DEBUG_LEVEL > 0
726 // we should have a control, and it should be in alive mode
727 OSL_PRECOND( isAliveMode( m_xUnoControl ),
728 "AccessibleControlShape::startStateMultiplexing: should be done in alive mode only!" );
729 #endif
730 // we should have the native context of the control
731 Reference< XAccessibleEventBroadcaster > xBroadcaster( m_aControlContext.get(), UNO_QUERY );
732 OSL_ENSURE( xBroadcaster.is(), "AccessibleControlShape::startStateMultiplexing: no AccessibleEventBroadcaster on the native context!" );
734 if ( xBroadcaster.is() )
736 xBroadcaster->addAccessibleEventListener( this );
737 m_bMultiplexingStates = true;
741 //--------------------------------------------------------------------
742 void AccessibleControlShape::stopStateMultiplexing()
744 OSL_PRECOND( m_bMultiplexingStates, "AccessibleControlShape::stopStateMultiplexing: not multiplexing!" );
746 // we should have the native context of the control
747 Reference< XAccessibleEventBroadcaster > xBroadcaster( m_aControlContext.get(), UNO_QUERY );
748 OSL_ENSURE( xBroadcaster.is(), "AccessibleControlShape::stopStateMultiplexing: no AccessibleEventBroadcaster on the native context!" );
750 if ( xBroadcaster.is() )
752 xBroadcaster->removeAccessibleEventListener( this );
753 m_bMultiplexingStates = false;
757 //--------------------------------------------------------------------
758 OUString AccessibleControlShape::getControlModelStringProperty( const OUString& _rPropertyName ) const SAL_THROW(())
760 OUString sReturn;
763 if ( const_cast< AccessibleControlShape* >( this )->ensureControlModelAccess() )
765 if ( !m_xModelPropsMeta.is() || m_xModelPropsMeta->hasPropertyByName( _rPropertyName ) )
766 // ask only if a) the control does not have a PropertySetInfo object or b) it has, and the
767 // property in question is available
768 m_xControlModel->getPropertyValue( _rPropertyName ) >>= sReturn;
771 catch( const Exception& )
773 OSL_FAIL( "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
775 return sReturn;
778 //--------------------------------------------------------------------
779 void AccessibleControlShape::adjustAccessibleRole( )
781 // if we're in design mode, we are a simple SHAPE, in alive mode, we use the role of our inner context
782 if ( !isAliveMode( m_xUnoControl ) )
783 return;
785 // we're in alive mode -> determine the role of the inner context
786 Reference< XAccessibleContext > xNativeContext( m_aControlContext );
787 OSL_PRECOND( xNativeContext.is(), "AccessibleControlShape::adjustAccessibleRole: no inner context!" );
788 if ( xNativeContext.is() )
789 SetAccessibleRole( xNativeContext->getAccessibleRole( ) );
792 #ifdef DBG_UTIL
793 //--------------------------------------------------------------------
794 sal_Bool AccessibleControlShape::SetState( sal_Int16 _nState )
796 OSL_ENSURE( !isAliveMode( m_xUnoControl ) || !isComposedState( _nState ),
797 "AccessibleControlShape::SetState: a state which should be determined by the control context is set from outside!" );
798 return AccessibleShape::SetState( _nState );
800 #endif // DBG_UTIL
802 //--------------------------------------------------------------------
803 void AccessibleControlShape::initializeComposedState()
805 if ( !isAliveMode( m_xUnoControl ) )
806 // no action necessary for design mode
807 return;
809 // get our own state set implementation
810 ::utl::AccessibleStateSetHelper* pComposedStates =
811 static_cast< ::utl::AccessibleStateSetHelper* >( mxStateSet.get() );
812 OSL_PRECOND( pComposedStates,
813 "AccessibleControlShape::initializeComposedState: no composed set!" );
815 // we need to reset some states of the composed set, because they either do not apply
816 // for controls in alive mode, or are in the responsibility of the UNO-control, anyway
817 pComposedStates->RemoveState( AccessibleStateType::ENABLED ); // this is controlled by the UNO-control
818 pComposedStates->RemoveState( AccessibleStateType::SENSITIVE ); // this is controlled by the UNO-control
819 pComposedStates->RemoveState( AccessibleStateType::FOCUSABLE ); // this is controlled by the UNO-control
820 pComposedStates->RemoveState( AccessibleStateType::SELECTABLE ); // this does not hold for an alive UNO-control
821 #if OSL_DEBUG_LEVEL > 0
822 // now, only states which are not in the responsibility of the UNO control should be part of this state set
824 Sequence< sal_Int16 > aInitStates = pComposedStates->getStates();
825 for ( sal_Int32 i=0; i<aInitStates.getLength(); ++i )
826 OSL_ENSURE( !isComposedState( aInitStates.getConstArray()[i] ),
827 "AccessibleControlShape::initializeComposedState: invalid initial composed state (should be controlled by the UNO-control)!" );
829 #endif
831 // get my inner context
832 Reference< XAccessibleContext > xInnerContext( m_aControlContext );
833 OSL_PRECOND( xInnerContext.is(), "AccessibleControlShape::initializeComposedState: no inner context!" );
834 if ( xInnerContext.is() )
836 // get all states of the inner context
837 Reference< XAccessibleStateSet > xInnerStates( xInnerContext->getAccessibleStateSet() );
838 OSL_ENSURE( xInnerStates.is(), "AccessibleControlShape::initializeComposedState: no inner states!" );
839 Sequence< sal_Int16 > aInnerStates;
840 if ( xInnerStates.is() )
841 aInnerStates = xInnerStates->getStates();
843 // look which one are to be propagated to the composed context
844 const sal_Int16* pStates = aInnerStates.getConstArray();
845 const sal_Int16* pStatesEnd = pStates + aInnerStates.getLength();
846 for ( ; pStates != pStatesEnd; ++pStates )
848 if ( isComposedState( *pStates ) && !pComposedStates->contains( *pStates ) )
850 pComposedStates->AddState( *pStates );
856 void SAL_CALL AccessibleControlShape::elementInserted( const ::com::sun::star::container::ContainerEvent& _rEvent ) throw (::com::sun::star::uno::RuntimeException)
858 Reference< XContainer > xContainer( _rEvent.Source, UNO_QUERY );
859 Reference< XControl > xControl( _rEvent.Element, UNO_QUERY );
861 OSL_ENSURE( xContainer.is() && xControl.is(),
862 "AccessibleControlShape::elementInserted: invalid event description!" );
864 if ( !xControl.is() )
865 return;
867 ensureControlModelAccess();
869 Reference< XInterface > xNewNormalized( xControl->getModel(), UNO_QUERY );
870 Reference< XInterface > xMyModelNormalized( m_xControlModel, UNO_QUERY );
871 if ( xNewNormalized.get() && xMyModelNormalized.get() )
873 // now finally the control for the model we're responsible for has been inserted into the container
874 Reference< XInterface > xKeepAlive( *this );
876 // first, we're not interested in any more container events
877 if ( xContainer.is() )
879 xContainer->removeContainerListener( this );
880 m_bWaitingForControl = false;
883 // second, we need to replace ourself with a new version, which now can be based on the
884 // control
885 OSL_VERIFY( mpParent->ReplaceChild ( this, mxShape, mnIndex, maShapeTreeInfo ) );
889 void SAL_CALL AccessibleControlShape::elementRemoved( const ::com::sun::star::container::ContainerEvent& ) throw (::com::sun::star::uno::RuntimeException)
891 // not interested in
894 void SAL_CALL AccessibleControlShape::elementReplaced( const ::com::sun::star::container::ContainerEvent& ) throw (::com::sun::star::uno::RuntimeException)
896 // not interested in
899 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */