Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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/AccessibleRelationType.hpp>
25 : #include <com/sun/star/accessibility/AccessibleEventId.hpp>
26 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
27 : #include <com/sun/star/form/FormComponentType.hpp>
28 : #include <com/sun/star/reflection/ProxyFactory.hpp>
29 : #include <com/sun/star/container/XContainer.hpp>
30 : #include <com/sun/star/container/XChild.hpp>
31 : #include <comphelper/processfactory.hxx>
32 : #include <comphelper/property.hxx>
33 : #include <comphelper/types.hxx>
34 : #include <unotools/accessiblestatesethelper.hxx>
35 : #include <unotools/accessiblerelationsethelper.hxx>
36 : #include <svx/svdouno.hxx>
37 : #include "svx/unoapi.hxx"
38 : #include <svx/ShapeTypeHandler.hxx>
39 : #include <svx/SvxShapeTypes.hxx>
40 : #include <toolkit/helper/vclunohelper.hxx>
41 : #include <comphelper/accessiblewrapper.hxx>
42 : #include <svx/svdview.hxx>
43 : #include <svx/svdpagv.hxx>
44 : #include "svx/svdstr.hrc"
45 : #include <algorithm>
46 :
47 : using namespace ::accessibility;
48 : using namespace ::com::sun::star::accessibility;
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::awt;
51 : using namespace ::com::sun::star::beans;
52 : using namespace ::com::sun::star::util;
53 : using namespace ::com::sun::star::lang;
54 : using namespace ::com::sun::star::reflection;
55 : using namespace ::com::sun::star::drawing;
56 : using namespace ::com::sun::star::container;
57 :
58 : namespace
59 : {
60 0 : static OUString lcl_getNamePropertyName( )
61 : {
62 0 : return OUString( "Name" );
63 : }
64 0 : static OUString lcl_getDescPropertyName( )
65 : {
66 0 : return OUString( "HelpText" );
67 : }
68 0 : static OUString lcl_getLabelPropertyName( )
69 : {
70 0 : return OUString( "Label" );
71 : }
72 0 : static OUString lcl_getLabelControlPropertyName( )
73 : {
74 0 : return OUString("LabelControl");
75 : }
76 : // return the property which should be used as AccessibleName
77 0 : const OUString lcl_getPreferredAccNameProperty( const Reference< XPropertySetInfo >& _rxPSI )
78 : {
79 0 : if ( _rxPSI.is() && _rxPSI->hasPropertyByName( lcl_getLabelPropertyName() ) )
80 0 : return lcl_getLabelPropertyName();
81 : else
82 0 : return lcl_getNamePropertyName();
83 : }
84 :
85 : // determines whether or not a state which belongs to the inner context needs to be forwarded to the "composed"
86 : // context
87 0 : bool isComposedState( const sal_Int16 _nState )
88 : {
89 : return ( ( AccessibleStateType::INVALID != _nState )
90 0 : && ( AccessibleStateType::DEFUNC != _nState )
91 0 : && ( AccessibleStateType::ICONIFIED != _nState )
92 0 : && ( AccessibleStateType::RESIZABLE != _nState )
93 0 : && ( AccessibleStateType::SELECTABLE != _nState )
94 0 : && ( AccessibleStateType::SHOWING != _nState )
95 0 : && ( AccessibleStateType::MANAGES_DESCENDANTS != _nState )
96 0 : && ( AccessibleStateType::VISIBLE != _nState )
97 0 : );
98 : }
99 :
100 : /// determines whether the given control is in alive mode
101 0 : inline bool isAliveMode( const Reference< XControl >& _rxControl )
102 : {
103 : OSL_PRECOND( _rxControl.is(), "AccessibleControlShape::isAliveMode: invalid control" );
104 0 : return _rxControl.is() && !_rxControl->isDesignMode();
105 : }
106 : }
107 :
108 0 : AccessibleControlShape::AccessibleControlShape (
109 : const AccessibleShapeInfo& rShapeInfo,
110 : const AccessibleShapeTreeInfo& rShapeTreeInfo)
111 : : AccessibleShape (rShapeInfo, rShapeTreeInfo)
112 : , m_bListeningForName( false )
113 : , m_bListeningForDesc( false )
114 : , m_bMultiplexingStates( false )
115 : , m_bDisposeNativeContext( false )
116 0 : , m_bWaitingForControl( false )
117 : {
118 0 : m_pChildManager = new comphelper::OWrappedAccessibleChildrenManager( comphelper::getProcessComponentContext() );
119 0 : m_pChildManager->acquire();
120 :
121 0 : osl_atomic_increment( &m_refCount );
122 : {
123 0 : m_pChildManager->setOwningAccessible( this );
124 : }
125 0 : osl_atomic_decrement( &m_refCount );
126 0 : }
127 :
128 0 : AccessibleControlShape::~AccessibleControlShape()
129 : {
130 0 : m_pChildManager->release();
131 0 : m_pChildManager = NULL;
132 :
133 0 : if ( m_xControlContextProxy.is() )
134 0 : m_xControlContextProxy->setDelegator( NULL );
135 0 : m_xControlContextProxy.clear();
136 0 : m_xControlContextTypeAccess.clear();
137 0 : m_xControlContextComponent.clear();
138 : // this should remove the _only_ three "real" reference (means not delegated to
139 : // ourself) to this proxy, and thus delete it
140 0 : }
141 :
142 0 : SdrObject* AccessibleControlShape::getSdrObject() const
143 : {
144 0 : return GetSdrObjectFromXShape (mxShape);
145 : }
146 :
147 : namespace {
148 0 : Reference< XContainer > lcl_getControlContainer( const vcl::Window* _pWin, const SdrView* _pView )
149 : {
150 0 : Reference< XContainer > xReturn;
151 : DBG_ASSERT( _pView, "lcl_getControlContainer: invalid view!" );
152 0 : if ( _pView && _pView->GetSdrPageView())
153 : {
154 0 : xReturn.set(_pView->GetSdrPageView()->GetControlContainer( *_pWin ), css::uno::UNO_QUERY);
155 : }
156 0 : return xReturn;
157 : }
158 : }
159 :
160 0 : void AccessibleControlShape::Init()
161 : {
162 0 : AccessibleShape::Init();
163 :
164 : OSL_ENSURE( !m_xControlContextProxy.is(), "AccessibleControlShape::Init: already initialized!" );
165 : try
166 : {
167 : // What we need to do here is merge the functionality of the AccessibleContext of our UNO control
168 : // with our own AccessibleContext-related functionality.
169 :
170 : // The problem is that we do not know the interfaces our "inner" context supports - this may be any
171 : // XAccessibleXXX interface (or even any other) which makes sense for it.
172 :
173 : // In theory, we could implement all possible interfaces ourself, and re-route all functionality to
174 : // the inner context (except those we implement ourself, like XAccessibleComponent). But this is in no
175 : // way future-proof - as soon as an inner context appears which implements an additional interface,
176 : // we would need to adjust our implementation to support this new interface, too. Bad idea.
177 :
178 : // The usual solution for such a problem is aggregation. Aggregation means using UNO's own meachnisms
179 : // for merging an inner with an outer component, and get a component which behaves as it is exactly one.
180 : // This is what XAggregation is for. Unfortunately, aggregation requires _exact_ control over the ref count
181 : // of the inner object, which we do not have at all.
182 : // Bad, too.
183 :
184 : // But there is a solution: com.sun.star.reflection.ProxyFactory. This service is able to create a proxy
185 : // for any component, which supports _exactly_ the same interfaces as the component. In addition, it can
186 : // be aggregated, as by definition the proxy's ref count is exactly 1 when returned from the factory.
187 : // Sounds better. Though this yields the problem of slightly degraded performance, it's the only solution
188 : // I'm aware of at the moment .....
189 :
190 : // get the control which belongs to our model (relative to our view)
191 0 : const vcl::Window* pViewWindow = maShapeTreeInfo.GetWindow();
192 0 : SdrUnoObj* pUnoObjectImpl = PTR_CAST( SdrUnoObj, getSdrObject() );
193 0 : SdrView* pView = maShapeTreeInfo.GetSdrView();
194 : OSL_ENSURE( pView && pViewWindow && pUnoObjectImpl, "AccessibleControlShape::Init: no view, or no view window, no SdrUnoObj!" );
195 :
196 0 : if ( pView && pViewWindow && pUnoObjectImpl )
197 : {
198 : // get the context of the control - it will be our "inner" context
199 0 : m_xUnoControl = pUnoObjectImpl->GetUnoControl( *pView, *pViewWindow );
200 :
201 0 : if ( !m_xUnoControl.is() )
202 : {
203 : // the control has not yet been created. Though speaking strictly, it is a bug that
204 : // our instance here is created without an existing control (because an AccessibleControlShape
205 : // is a representation of a view object, and can only live if the view it should represent
206 : // is complete, which implies a living control), it's by far the easiest and most riskless way
207 : // to fix this here in this class.
208 : // Okay, we will add as listener to the control container where we expect our control to appear.
209 : OSL_ENSURE( !m_bWaitingForControl, "AccessibleControlShape::Init: already waiting for the control!" );
210 :
211 0 : Reference< XContainer > xControlContainer = lcl_getControlContainer( pViewWindow, maShapeTreeInfo.GetSdrView() );
212 : OSL_ENSURE( xControlContainer.is(), "AccessibleControlShape::Init: unable to find my ControlContainer!" );
213 0 : if ( xControlContainer.is() )
214 : {
215 0 : xControlContainer->addContainerListener( this );
216 0 : m_bWaitingForControl = true;
217 0 : }
218 : }
219 : else
220 : {
221 0 : Reference< XModeChangeBroadcaster > xControlModes( m_xUnoControl, UNO_QUERY );
222 0 : Reference< XAccessible > xControlAccessible( xControlModes, UNO_QUERY );
223 0 : Reference< XAccessibleContext > xNativeControlContext;
224 0 : if ( xControlAccessible.is() )
225 0 : xNativeControlContext = xControlAccessible->getAccessibleContext();
226 : OSL_ENSURE( xNativeControlContext.is(), "AccessibleControlShape::Init: no AccessibleContext for the control!" );
227 0 : m_aControlContext = WeakReference< XAccessibleContext >( xNativeControlContext );
228 :
229 : // add as listener to the context - we want to multiplex some states
230 0 : if ( isAliveMode( m_xUnoControl ) && xNativeControlContext.is() )
231 : { // (but only in alive mode)
232 0 : startStateMultiplexing( );
233 : }
234 :
235 : // now that we have all information about our control, do some adjustments
236 0 : adjustAccessibleRole();
237 0 : initializeComposedState();
238 :
239 : // some initialization for our child manager, which is used in alive mode only
240 0 : if ( isAliveMode( m_xUnoControl ) )
241 : {
242 0 : Reference< XAccessibleStateSet > xStates( getAccessibleStateSet( ) );
243 : OSL_ENSURE( xStates.is(), "AccessibleControlShape::AccessibleControlShape: no inner state set!" );
244 0 : m_pChildManager->setTransientChildren( !xStates.is() || xStates->contains( AccessibleStateType::MANAGES_DESCENDANTS ) );
245 : }
246 :
247 : // finally, aggregate a proxy for the control context
248 : // first a factory for the proxy
249 0 : Reference< XProxyFactory > xFactory = ProxyFactory::create( comphelper::getProcessComponentContext() );
250 : // then the proxy itself
251 0 : if ( xNativeControlContext.is() )
252 : {
253 0 : m_xControlContextProxy = xFactory->createProxy( xNativeControlContext );
254 0 : m_xControlContextTypeAccess.set( xNativeControlContext, UNO_QUERY_THROW );
255 0 : m_xControlContextComponent.set( xNativeControlContext, UNO_QUERY_THROW );
256 :
257 : // aggregate the proxy
258 0 : osl_atomic_increment( &m_refCount );
259 0 : if ( m_xControlContextProxy.is() )
260 : {
261 : // At this point in time, the proxy has a ref count of exactly one - in m_xControlContextProxy.
262 : // Remember to _not_ reset this member unles the delegator of the proxy has been reset, too!
263 0 : m_xControlContextProxy->setDelegator( *this );
264 : }
265 0 : osl_atomic_decrement( &m_refCount );
266 :
267 0 : m_bDisposeNativeContext = true;
268 :
269 : // Finally, we need to add ourself as mode listener to the control. In case the mode switches,
270 : // we need to dispose ourself.
271 0 : xControlModes->addModeChangeListener( this );
272 0 : }
273 : }
274 : }
275 : }
276 0 : catch( const Exception& )
277 : {
278 : OSL_FAIL( "AccessibleControlShape::Init: could not \"aggregate\" the controls XAccessibleContext!" );
279 : }
280 0 : }
281 :
282 0 : Reference< XAccessibleContext > SAL_CALL AccessibleControlShape::getAccessibleContext() throw (RuntimeException, std::exception)
283 : {
284 0 : return AccessibleShape::getAccessibleContext ();
285 : }
286 :
287 0 : void SAL_CALL AccessibleControlShape::grabFocus() throw (RuntimeException, std::exception)
288 : {
289 0 : if ( !m_xUnoControl.is() || !isAliveMode( m_xUnoControl ) )
290 : {
291 : // in design mode, we simply forward the request to the base class
292 0 : AccessibleShape::grabFocus();
293 : }
294 : else
295 : {
296 0 : Reference< XWindow > xWindow( m_xUnoControl, UNO_QUERY );
297 : OSL_ENSURE( xWindow.is(), "AccessibleControlShape::grabFocus: invalid control!" );
298 0 : if ( xWindow.is() )
299 0 : xWindow->setFocus();
300 : }
301 0 : }
302 :
303 0 : OUString SAL_CALL AccessibleControlShape::getImplementationName() throw (RuntimeException, std::exception)
304 : {
305 0 : return OUString( "com.sun.star.comp.accessibility.AccessibleControlShape" );
306 : }
307 :
308 0 : OUString AccessibleControlShape::CreateAccessibleBaseName() throw (RuntimeException)
309 : {
310 0 : OUString sName;
311 :
312 0 : ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
313 0 : switch (nShapeType)
314 : {
315 : case DRAWING_CONTROL:
316 0 : sName = "ControlShape";
317 0 : break;
318 : default:
319 0 : sName = "UnknownAccessibleControlShape";
320 0 : Reference< XShapeDescriptor > xDescriptor (mxShape, UNO_QUERY);
321 0 : if (xDescriptor.is())
322 0 : sName += ": " + xDescriptor->getShapeType();
323 : }
324 :
325 0 : return sName;
326 : }
327 :
328 :
329 :
330 :
331 : OUString
332 0 : AccessibleControlShape::CreateAccessibleDescription()
333 : throw (RuntimeException)
334 : {
335 0 : DescriptionGenerator aDG (mxShape);
336 0 : ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
337 0 : switch (nShapeType)
338 : {
339 : case DRAWING_CONTROL:
340 : {
341 : // check if we can obtain the "Desc" property from the model
342 0 : OUString sDesc( getControlModelStringProperty( lcl_getDescPropertyName() ) );
343 0 : if ( sDesc.isEmpty() )
344 : { // no -> use the default
345 0 : aDG.Initialize (STR_ObjNameSingulUno);
346 0 : aDG.AddProperty ("ControlBackground", DescriptionGenerator::COLOR, "");
347 0 : aDG.AddProperty ( "ControlBorder", DescriptionGenerator::INTEGER, "");
348 : }
349 : // ensure that we are listening to the Name property
350 0 : m_bListeningForDesc = ensureListeningState( m_bListeningForDesc, true, lcl_getDescPropertyName() );
351 : }
352 0 : break;
353 :
354 : default:
355 0 : aDG.Initialize ("Unknown accessible control shape");
356 0 : Reference< XShapeDescriptor > xDescriptor (mxShape, UNO_QUERY);
357 0 : if (xDescriptor.is())
358 : {
359 0 : aDG.AppendString ("service name=");
360 0 : aDG.AppendString (xDescriptor->getShapeType());
361 0 : }
362 : }
363 :
364 0 : return aDG();
365 : }
366 :
367 0 : IMPLEMENT_FORWARD_REFCOUNT( AccessibleControlShape, AccessibleShape )
368 0 : IMPLEMENT_GET_IMPLEMENTATION_ID( AccessibleControlShape )
369 :
370 0 : void SAL_CALL AccessibleControlShape::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException, std::exception)
371 : {
372 0 : ::osl::MutexGuard aGuard( maMutex );
373 :
374 : // check if it is the name or the description
375 0 : if ( _rEvent.PropertyName == lcl_getNamePropertyName()
376 0 : || _rEvent.PropertyName == lcl_getLabelPropertyName() )
377 : {
378 : SetAccessibleName(
379 0 : CreateAccessibleName(),
380 0 : AccessibleContextBase::AutomaticallyCreated);
381 : }
382 0 : else if ( _rEvent.PropertyName == lcl_getDescPropertyName() )
383 : {
384 : SetAccessibleDescription(
385 0 : CreateAccessibleDescription(),
386 0 : AccessibleContextBase::AutomaticallyCreated);
387 0 : }
388 : #if OSL_DEBUG_LEVEL > 0
389 : else
390 : {
391 : OSL_FAIL( "AccessibleControlShape::propertyChange: where did this come from?" );
392 : }
393 : #endif
394 0 : }
395 :
396 0 : Any SAL_CALL AccessibleControlShape::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
397 : {
398 0 : Any aReturn = AccessibleShape::queryInterface( _rType );
399 0 : if ( !aReturn.hasValue() )
400 : {
401 0 : aReturn = AccessibleControlShape_Base::queryInterface( _rType );
402 0 : if ( !aReturn.hasValue() && m_xControlContextProxy.is() )
403 0 : aReturn = m_xControlContextProxy->queryAggregation( _rType );
404 : }
405 0 : return aReturn;
406 : }
407 :
408 0 : Sequence< Type > SAL_CALL AccessibleControlShape::getTypes() throw (RuntimeException, std::exception)
409 : {
410 0 : Sequence< Type > aShapeTypes = AccessibleShape::getTypes();
411 0 : Sequence< Type > aOwnTypes = AccessibleControlShape_Base::getTypes();
412 :
413 0 : Sequence< Type > aAggregateTypes;
414 0 : if ( m_xControlContextTypeAccess.is() )
415 0 : aAggregateTypes = m_xControlContextTypeAccess->getTypes();
416 :
417 0 : Sequence< Type > aAllTypes = comphelper::concatSequences( aShapeTypes, aOwnTypes, aAggregateTypes );
418 :
419 : // remove duplicates
420 0 : Type* pBegin = aAllTypes.getArray();
421 0 : Type* pEnd = pBegin + aAllTypes.getLength();
422 0 : while ( pBegin != pEnd )
423 : {
424 0 : Type aThisRoundType = *pBegin;
425 0 : if ( ++pBegin != pEnd )
426 : {
427 0 : pEnd = ::std::remove( pBegin, pEnd, aThisRoundType );
428 : // now all types between begin and (the old) end which equal aThisRoundType
429 : // are moved behind the new end
430 : }
431 0 : }
432 0 : aAllTypes.realloc( pEnd - aAllTypes.getArray() );
433 :
434 0 : return aAllTypes;
435 : }
436 :
437 0 : void SAL_CALL AccessibleControlShape::notifyEvent( const AccessibleEventObject& _rEvent ) throw (RuntimeException, std::exception)
438 : {
439 0 : if ( AccessibleEventId::STATE_CHANGED == _rEvent.EventId )
440 : {
441 : // multiplex this change
442 0 : sal_Int16 nLostState( 0 ), nGainedState( 0 );
443 0 : _rEvent.OldValue >>= nLostState;
444 0 : _rEvent.NewValue >>= nGainedState;
445 :
446 : // don't multiplex states which the inner context is not resposible for
447 0 : if ( isComposedState( nLostState ) )
448 0 : AccessibleShape::ResetState( nLostState );
449 :
450 0 : if ( isComposedState( nGainedState ) )
451 0 : AccessibleShape::SetState( nGainedState );
452 : }
453 : else
454 : {
455 0 : AccessibleEventObject aTranslatedEvent( _rEvent );
456 :
457 : {
458 0 : ::osl::MutexGuard aGuard( maMutex );
459 :
460 : // let the child manager translate the event
461 0 : aTranslatedEvent.Source = *this;
462 0 : m_pChildManager->translateAccessibleEvent( _rEvent, aTranslatedEvent );
463 :
464 : // see if any of these notifications affect our child manager
465 0 : m_pChildManager->handleChildNotification( _rEvent );
466 : }
467 :
468 0 : FireEvent( aTranslatedEvent );
469 : }
470 0 : }
471 :
472 0 : void SAL_CALL AccessibleControlShape::modeChanged( const ModeChangeEvent& _rSource ) throw (RuntimeException, std::exception)
473 : {
474 : // did it come from our inner context (the real one, not it's proxy!)?
475 : OSL_TRACE ("AccessibleControlShape::modeChanged");
476 0 : Reference< XControl > xSource( _rSource.Source, UNO_QUERY ); // for faster compare
477 0 : if ( xSource.get() == m_xUnoControl.get() )
478 : {
479 : // If our "pseudo-aggregated" inner context does not live anymore,
480 : // we don't want to live, too. This is accomplished by asking our
481 : // parent to replace this object with a new one. Disposing this
482 : // object and sending notifications about the replacement are in
483 : // the responsibility of our parent.
484 0 : OSL_VERIFY( mpParent->ReplaceChild ( this, mxShape, mnIndex, maShapeTreeInfo ) );
485 0 : }
486 : #if OSL_DEBUG_LEVEL > 0
487 : else
488 : OSL_FAIL( "AccessibleControlShape::modeChanged: where did this come from?" );
489 : #endif
490 0 : }
491 :
492 0 : void SAL_CALL AccessibleControlShape::disposing (const EventObject& _rSource) throw (RuntimeException, std::exception)
493 : {
494 0 : AccessibleShape::disposing( _rSource );
495 0 : }
496 :
497 0 : bool AccessibleControlShape::ensureListeningState(
498 : const bool _bCurrentlyListening, const bool _bNeedNewListening,
499 : const OUString& _rPropertyName )
500 : {
501 0 : if ( ( _bCurrentlyListening == _bNeedNewListening ) || !ensureControlModelAccess() )
502 : // nothing to do
503 0 : return _bCurrentlyListening;
504 :
505 : try
506 : {
507 0 : if ( !m_xModelPropsMeta.is() || m_xModelPropsMeta->hasPropertyByName( _rPropertyName ) )
508 : {
509 : // add or revoke as listener
510 0 : if ( _bNeedNewListening )
511 0 : m_xControlModel->addPropertyChangeListener( _rPropertyName, static_cast< XPropertyChangeListener* >( this ) );
512 : else
513 0 : m_xControlModel->removePropertyChangeListener( _rPropertyName, static_cast< XPropertyChangeListener* >( this ) );
514 : }
515 : else
516 : OSL_FAIL( "AccessibleControlShape::ensureListeningState: this property does not exist at this model!" );
517 : }
518 0 : catch( const Exception& e )
519 : {
520 : (void)e; // make compiler happy
521 : OSL_FAIL( "AccessibleControlShape::ensureListeningState: could not change the listening state!" );
522 : }
523 :
524 0 : return _bNeedNewListening;
525 : }
526 :
527 0 : sal_Int32 SAL_CALL AccessibleControlShape::getAccessibleChildCount( ) throw(RuntimeException, std::exception)
528 : {
529 0 : if ( !m_xUnoControl.is() )
530 0 : return 0;
531 0 : else if ( !isAliveMode( m_xUnoControl ) )
532 : // no special action required when in design mode
533 0 : return AccessibleShape::getAccessibleChildCount( );
534 : else
535 : {
536 : // in alive mode, we have the full control over our children - they are determined by the children
537 : // of the context of our UNO control
538 0 : Reference< XAccessibleContext > xControlContext( m_aControlContext );
539 : OSL_ENSURE( xControlContext.is(), "AccessibleControlShape::getAccessibleChildCount: control context already dead! How this!" );
540 0 : return xControlContext.is() ? xControlContext->getAccessibleChildCount() : 0;
541 : }
542 : }
543 :
544 0 : Reference< XAccessible > SAL_CALL AccessibleControlShape::getAccessibleChild( sal_Int32 i ) throw(IndexOutOfBoundsException, RuntimeException, std::exception)
545 : {
546 0 : Reference< XAccessible > xChild;
547 0 : if ( !m_xUnoControl.is() )
548 : {
549 0 : throw IndexOutOfBoundsException();
550 : }
551 0 : if ( !isAliveMode( m_xUnoControl ) )
552 : {
553 : // no special action required when in design mode - let the base class handle this
554 0 : xChild = AccessibleShape::getAccessibleChild( i );
555 : }
556 : else
557 : {
558 : // in alive mode, we have the full control over our children - they are determined by the children
559 : // of the context of our UNO control
560 :
561 0 : Reference< XAccessibleContext > xControlContext( m_aControlContext );
562 : OSL_ENSURE( xControlContext.is(), "AccessibleControlShape::getAccessibleChildCount: control context already dead! How this!" );
563 0 : if ( xControlContext.is() )
564 : {
565 0 : Reference< XAccessible > xInnerChild( xControlContext->getAccessibleChild( i ) );
566 : OSL_ENSURE( xInnerChild.is(), "AccessibleControlShape::getAccessibleChild: control context returned nonsense!" );
567 0 : if ( xInnerChild.is() )
568 : {
569 : // we need to wrap this inner child into an own implementation
570 0 : xChild = m_pChildManager->getAccessibleWrapperFor( xInnerChild );
571 0 : }
572 0 : }
573 : }
574 :
575 : #if OSL_DEBUG_LEVEL > 0
576 : sal_Int32 nChildIndex = -1;
577 : Reference< XAccessibleContext > xContext;
578 : if ( xChild.is() )
579 : xContext = xChild->getAccessibleContext( );
580 : if ( xContext.is() )
581 : nChildIndex = xContext->getAccessibleIndexInParent( );
582 : OSL_ENSURE( nChildIndex == i, "AccessibleControlShape::getAccessibleChild: index mismatch!" );
583 : #endif
584 0 : return xChild;
585 : }
586 :
587 0 : Reference< XAccessibleRelationSet > SAL_CALL AccessibleControlShape::getAccessibleRelationSet( ) throw (RuntimeException, std::exception)
588 : {
589 0 : utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
590 0 : ensureControlModelAccess();
591 0 : AccessibleControlShape* pCtlAccShape = GetLabeledByControlShape();
592 0 : if(pCtlAccShape)
593 : {
594 0 : Reference < XAccessible > xAcc (pCtlAccShape->getAccessibleContext(), UNO_QUERY);
595 :
596 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > aSequence(1);
597 0 : aSequence[0] = xAcc;
598 0 : if( getAccessibleRole() == AccessibleRole::RADIO_BUTTON )
599 : {
600 0 : pRelationSetHelper->AddRelation( AccessibleRelation( AccessibleRelationType::MEMBER_OF, aSequence ) );
601 : }
602 : else
603 : {
604 0 : pRelationSetHelper->AddRelation( AccessibleRelation( AccessibleRelationType::LABELED_BY, aSequence ) );
605 0 : }
606 : }
607 0 : Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
608 0 : return xSet;
609 : }
610 :
611 0 : OUString AccessibleControlShape::CreateAccessibleName() throw (RuntimeException)
612 : {
613 0 : ensureControlModelAccess();
614 :
615 0 : OUString sName;
616 0 : if ( getAccessibleRole() != AccessibleRole::SHAPE
617 0 : && getAccessibleRole() != AccessibleRole::RADIO_BUTTON )
618 : {
619 0 : AccessibleControlShape* pCtlAccShape = GetLabeledByControlShape();
620 0 : if(pCtlAccShape)
621 : {
622 0 : sName = pCtlAccShape->CreateAccessibleName();
623 : }
624 : }
625 :
626 0 : if (sName.isEmpty())
627 : {
628 : // check if we can obtain the "Name" resp. "Label" property from the model
629 0 : const OUString& rAccNameProperty = lcl_getPreferredAccNameProperty( m_xModelPropsMeta );
630 0 : sName = getControlModelStringProperty( rAccNameProperty );
631 0 : if ( !sName.getLength() )
632 : { // no -> use the default
633 0 : sName = AccessibleShape::CreateAccessibleName();
634 0 : }
635 : }
636 :
637 : // now that somebody first asked us for our name, ensure that we are listening to name changes on the model
638 0 : m_bListeningForName = ensureListeningState( m_bListeningForName, true, lcl_getPreferredAccNameProperty( m_xModelPropsMeta ) );
639 :
640 0 : return sName;
641 : }
642 :
643 0 : void SAL_CALL AccessibleControlShape::disposing()
644 : {
645 : // ensure we're not listening
646 0 : m_bListeningForName = ensureListeningState( m_bListeningForName, false, lcl_getPreferredAccNameProperty( m_xModelPropsMeta ) );
647 0 : m_bListeningForDesc = ensureListeningState( m_bListeningForDesc, false, lcl_getDescPropertyName() );
648 :
649 0 : if ( m_bMultiplexingStates )
650 0 : stopStateMultiplexing( );
651 :
652 : // dispose the child cache/map
653 0 : m_pChildManager->dispose();
654 :
655 : // release the model
656 0 : m_xControlModel.clear();
657 0 : m_xModelPropsMeta.clear();
658 0 : m_aControlContext = WeakReference< XAccessibleContext >();
659 :
660 : // stop listening at the control container (should never be necessary here, but who knows ....)
661 0 : if ( m_bWaitingForControl )
662 : {
663 : OSL_FAIL( "AccessibleControlShape::disposing: this should never happen!" );
664 0 : Reference< XContainer > xContainer = lcl_getControlContainer( maShapeTreeInfo.GetWindow(), maShapeTreeInfo.GetSdrView() );
665 0 : if ( xContainer.is() )
666 : {
667 0 : m_bWaitingForControl = false;
668 0 : xContainer->removeContainerListener( this );
669 0 : }
670 : }
671 :
672 : // forward the disposel to our inner context
673 0 : if ( m_bDisposeNativeContext )
674 : {
675 : // don't listen for mode changes anymore
676 0 : Reference< XModeChangeBroadcaster > xControlModes( m_xUnoControl, UNO_QUERY );
677 : OSL_ENSURE( xControlModes.is(), "AccessibleControlShape::disposing: don't have an mode broadcaster anymore!" );
678 0 : if ( xControlModes.is() )
679 0 : xControlModes->removeModeChangeListener( this );
680 :
681 0 : if ( m_xControlContextComponent.is() )
682 0 : m_xControlContextComponent->dispose();
683 : // do _not_ clear m_xControlContextProxy! This has to be done in the dtor for correct ref-count handling
684 :
685 : // no need to dispose the proxy/inner context anymore
686 0 : m_bDisposeNativeContext = false;
687 : }
688 :
689 0 : m_xUnoControl.clear();
690 :
691 : // let the base do it's stuff
692 0 : AccessibleShape::disposing();
693 0 : }
694 :
695 0 : bool AccessibleControlShape::ensureControlModelAccess()
696 : {
697 0 : if ( m_xControlModel.is() )
698 0 : return true;
699 :
700 : try
701 : {
702 0 : Reference< XControlShape > xShape( mxShape, UNO_QUERY );
703 0 : if ( xShape.is() )
704 0 : m_xControlModel.set(xShape->getControl(), css::uno::UNO_QUERY);
705 :
706 0 : if ( m_xControlModel.is() )
707 0 : m_xModelPropsMeta = m_xControlModel->getPropertySetInfo();
708 : }
709 0 : catch( const Exception& e )
710 : {
711 : (void)e; // make compiler happy
712 : OSL_FAIL( "AccessibleControlShape::ensureControlModelAccess: caught an exception!" );
713 : }
714 :
715 0 : return m_xControlModel.is();
716 : }
717 :
718 0 : void AccessibleControlShape::startStateMultiplexing()
719 : {
720 : OSL_PRECOND( !m_bMultiplexingStates, "AccessibleControlShape::startStateMultiplexing: already multiplexing!" );
721 :
722 : #if OSL_DEBUG_LEVEL > 0
723 : // we should have a control, and it should be in alive mode
724 : OSL_PRECOND( isAliveMode( m_xUnoControl ),
725 : "AccessibleControlShape::startStateMultiplexing: should be done in alive mode only!" );
726 : #endif
727 : // we should have the native context of the control
728 0 : Reference< XAccessibleEventBroadcaster > xBroadcaster( m_aControlContext.get(), UNO_QUERY );
729 : OSL_ENSURE( xBroadcaster.is(), "AccessibleControlShape::startStateMultiplexing: no AccessibleEventBroadcaster on the native context!" );
730 :
731 0 : if ( xBroadcaster.is() )
732 : {
733 0 : xBroadcaster->addAccessibleEventListener( this );
734 0 : m_bMultiplexingStates = true;
735 0 : }
736 0 : }
737 :
738 0 : void AccessibleControlShape::stopStateMultiplexing()
739 : {
740 : OSL_PRECOND( m_bMultiplexingStates, "AccessibleControlShape::stopStateMultiplexing: not multiplexing!" );
741 :
742 : // we should have the native context of the control
743 0 : Reference< XAccessibleEventBroadcaster > xBroadcaster( m_aControlContext.get(), UNO_QUERY );
744 : OSL_ENSURE( xBroadcaster.is(), "AccessibleControlShape::stopStateMultiplexing: no AccessibleEventBroadcaster on the native context!" );
745 :
746 0 : if ( xBroadcaster.is() )
747 : {
748 0 : xBroadcaster->removeAccessibleEventListener( this );
749 0 : m_bMultiplexingStates = false;
750 0 : }
751 0 : }
752 :
753 0 : OUString AccessibleControlShape::getControlModelStringProperty( const OUString& _rPropertyName ) const
754 : {
755 0 : OUString sReturn;
756 : try
757 : {
758 0 : if ( const_cast< AccessibleControlShape* >( this )->ensureControlModelAccess() )
759 : {
760 0 : if ( !m_xModelPropsMeta.is() || m_xModelPropsMeta->hasPropertyByName( _rPropertyName ) )
761 : // ask only if a) the control does not have a PropertySetInfo object or b) it has, and the
762 : // property in question is available
763 0 : m_xControlModel->getPropertyValue( _rPropertyName ) >>= sReturn;
764 : }
765 : }
766 0 : catch( const Exception& )
767 : {
768 : OSL_FAIL( "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
769 : }
770 0 : return sReturn;
771 : }
772 :
773 0 : void AccessibleControlShape::adjustAccessibleRole( )
774 : {
775 : // if we're in design mode, we are a simple SHAPE, in alive mode, we use the role of our inner context
776 0 : if ( !isAliveMode( m_xUnoControl ) )
777 0 : return;
778 :
779 : // we're in alive mode -> determine the role of the inner context
780 0 : Reference< XAccessibleContext > xNativeContext( m_aControlContext );
781 : OSL_PRECOND( xNativeContext.is(), "AccessibleControlShape::adjustAccessibleRole: no inner context!" );
782 0 : if ( xNativeContext.is() )
783 0 : SetAccessibleRole( xNativeContext->getAccessibleRole( ) );
784 : }
785 :
786 : #ifdef DBG_UTIL
787 :
788 : bool AccessibleControlShape::SetState( sal_Int16 _nState )
789 : {
790 : OSL_ENSURE( !isAliveMode( m_xUnoControl ) || !isComposedState( _nState ),
791 : "AccessibleControlShape::SetState: a state which should be determined by the control context is set from outside!" );
792 : return AccessibleShape::SetState( _nState );
793 : }
794 : #endif // DBG_UTIL
795 :
796 0 : void AccessibleControlShape::initializeComposedState()
797 : {
798 0 : if ( !isAliveMode( m_xUnoControl ) )
799 : // no action necessary for design mode
800 0 : return;
801 :
802 : // get our own state set implementation
803 : ::utl::AccessibleStateSetHelper* pComposedStates =
804 0 : static_cast< ::utl::AccessibleStateSetHelper* >( mxStateSet.get() );
805 : OSL_PRECOND( pComposedStates,
806 : "AccessibleControlShape::initializeComposedState: no composed set!" );
807 :
808 : // we need to reset some states of the composed set, because they either do not apply
809 : // for controls in alive mode, or are in the responsibility of the UNO-control, anyway
810 0 : pComposedStates->RemoveState( AccessibleStateType::ENABLED ); // this is controlled by the UNO-control
811 0 : pComposedStates->RemoveState( AccessibleStateType::SENSITIVE ); // this is controlled by the UNO-control
812 0 : pComposedStates->RemoveState( AccessibleStateType::FOCUSABLE ); // this is controlled by the UNO-control
813 0 : pComposedStates->RemoveState( AccessibleStateType::SELECTABLE ); // this does not hold for an alive UNO-control
814 : #if OSL_DEBUG_LEVEL > 0
815 : // now, only states which are not in the responsibility of the UNO control should be part of this state set
816 : {
817 : Sequence< sal_Int16 > aInitStates = pComposedStates->getStates();
818 : for ( sal_Int32 i=0; i<aInitStates.getLength(); ++i )
819 : OSL_ENSURE( !isComposedState( aInitStates.getConstArray()[i] ),
820 : "AccessibleControlShape::initializeComposedState: invalid initial composed state (should be controlled by the UNO-control)!" );
821 : }
822 : #endif
823 :
824 : // get my inner context
825 0 : Reference< XAccessibleContext > xInnerContext( m_aControlContext );
826 : OSL_PRECOND( xInnerContext.is(), "AccessibleControlShape::initializeComposedState: no inner context!" );
827 0 : if ( xInnerContext.is() )
828 : {
829 : // get all states of the inner context
830 0 : Reference< XAccessibleStateSet > xInnerStates( xInnerContext->getAccessibleStateSet() );
831 : OSL_ENSURE( xInnerStates.is(), "AccessibleControlShape::initializeComposedState: no inner states!" );
832 0 : Sequence< sal_Int16 > aInnerStates;
833 0 : if ( xInnerStates.is() )
834 0 : aInnerStates = xInnerStates->getStates();
835 :
836 : // look which one are to be propagated to the composed context
837 0 : const sal_Int16* pStates = aInnerStates.getConstArray();
838 0 : const sal_Int16* pStatesEnd = pStates + aInnerStates.getLength();
839 0 : for ( ; pStates != pStatesEnd; ++pStates )
840 : {
841 0 : if ( isComposedState( *pStates ) && !pComposedStates->contains( *pStates ) )
842 : {
843 0 : pComposedStates->AddState( *pStates );
844 : }
845 0 : }
846 0 : }
847 : }
848 :
849 0 : void SAL_CALL AccessibleControlShape::elementInserted( const ::com::sun::star::container::ContainerEvent& _rEvent ) throw (::com::sun::star::uno::RuntimeException, std::exception)
850 : {
851 0 : Reference< XContainer > xContainer( _rEvent.Source, UNO_QUERY );
852 0 : Reference< XControl > xControl( _rEvent.Element, UNO_QUERY );
853 :
854 : OSL_ENSURE( xContainer.is() && xControl.is(),
855 : "AccessibleControlShape::elementInserted: invalid event description!" );
856 :
857 0 : if ( !xControl.is() )
858 0 : return;
859 :
860 0 : ensureControlModelAccess();
861 :
862 0 : Reference< XInterface > xNewNormalized( xControl->getModel(), UNO_QUERY );
863 0 : Reference< XInterface > xMyModelNormalized( m_xControlModel, UNO_QUERY );
864 0 : if ( xNewNormalized.get() && xMyModelNormalized.get() )
865 : {
866 : // now finally the control for the model we're responsible for has been inserted into the container
867 0 : Reference< XInterface > xKeepAlive( *this );
868 :
869 : // first, we're not interested in any more container events
870 0 : if ( xContainer.is() )
871 : {
872 0 : xContainer->removeContainerListener( this );
873 0 : m_bWaitingForControl = false;
874 : }
875 :
876 : // second, we need to replace ourself with a new version, which now can be based on the
877 : // control
878 0 : OSL_VERIFY( mpParent->ReplaceChild ( this, mxShape, mnIndex, maShapeTreeInfo ) );
879 0 : }
880 : }
881 :
882 0 : void SAL_CALL AccessibleControlShape::elementRemoved( const ::com::sun::star::container::ContainerEvent& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
883 : {
884 : // not interested in
885 0 : }
886 :
887 0 : void SAL_CALL AccessibleControlShape::elementReplaced( const ::com::sun::star::container::ContainerEvent& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
888 : {
889 : // not interested in
890 0 : }
891 :
892 0 : AccessibleControlShape* SAL_CALL AccessibleControlShape::GetLabeledByControlShape( )
893 : {
894 0 : if(m_xControlModel.is())
895 : {
896 0 : const OUString& rAccLabelControlProperty = lcl_getLabelControlPropertyName();
897 0 : Any sCtlLabelBy;
898 : // get the "label by" property value of the control
899 0 : if (::comphelper::hasProperty(rAccLabelControlProperty, m_xControlModel))
900 : {
901 0 : m_xControlModel->getPropertyValue( rAccLabelControlProperty ) >>= sCtlLabelBy;
902 0 : if( sCtlLabelBy.hasValue() )
903 : {
904 0 : Reference< XPropertySet > xAsSet (sCtlLabelBy, UNO_QUERY);
905 0 : AccessibleControlShape* pCtlAccShape = mpParent->GetAccControlShapeFromModel(xAsSet.get());
906 0 : return pCtlAccShape;
907 : }
908 0 : }
909 : }
910 0 : return NULL;
911 390 : }
912 :
913 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|