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