bump product version to 4.1.6.2
[LibreOffice.git] / toolkit / source / controls / accessiblecontrolcontext.cxx
blob7be5732e17993cd9c2f6bed8202ec137cc22105e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <toolkit/controls/accessiblecontrolcontext.hxx>
21 #include <unotools/accessiblestatesethelper.hxx>
22 #include <com/sun/star/awt/XControl.hpp>
23 #include <com/sun/star/awt/XWindow.hpp>
24 #include <vcl/svapp.hxx>
25 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
26 #include <com/sun/star/accessibility/AccessibleRole.hpp>
27 #include <toolkit/helper/vclunohelper.hxx>
28 #include <vcl/window.hxx>
30 //........................................................................
31 namespace toolkit
33 //........................................................................
35 using ::comphelper::OContextEntryGuard;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::accessibility;
42 //====================================================================
43 //= OAccessibleControlContext
44 //====================================================================
45 //--------------------------------------------------------------------
46 OAccessibleControlContext::OAccessibleControlContext()
47 :OAccessibleControlContext_Base( )
49 // nothing to do here, we have a late ctor
52 //--------------------------------------------------------------------
53 OAccessibleControlContext::~OAccessibleControlContext()
55 ensureDisposed();
58 //--------------------------------------------------------------------
59 IMPLEMENT_FORWARD_XINTERFACE3( OAccessibleControlContext, OAccessibleControlContext_Base, OAccessibleImplementationAccess, OAccessibleControlContext_IBase )
60 IMPLEMENT_FORWARD_XTYPEPROVIDER3( OAccessibleControlContext, OAccessibleControlContext_Base, OAccessibleImplementationAccess, OAccessibleControlContext_IBase )
61 // (order matters: the first is the class name, the second is the class doing the ref counting)
63 //--------------------------------------------------------------------
64 void OAccessibleControlContext::Init( const Reference< XAccessible >& _rxCreator ) SAL_THROW( ( Exception ) )
66 OContextEntryGuard aGuard( this );
68 // retrieve the model of the control
69 OSL_ENSURE( !m_xControlModel.is(), "OAccessibleControlContext::Init: already know a control model....!???" );
71 Reference< awt::XControl > xControl( _rxCreator, UNO_QUERY );
72 if ( xControl.is() )
73 m_xControlModel = m_xControlModel.query( xControl->getModel() );
74 OSL_ENSURE( m_xControlModel.is(), "OAccessibleControlContext::Init: invalid creator (no control, or control without model!" );
75 if ( !m_xControlModel.is() )
76 throw DisposedException(); // caught by the caller (the create method)
78 // start listening at the model
79 startModelListening();
81 // announce the XAccessible to our base class
82 OAccessibleControlContext_Base::lateInit( _rxCreator );
85 //--------------------------------------------------------------------
86 OAccessibleControlContext* OAccessibleControlContext::create( const Reference< XAccessible >& _rxCreator ) SAL_THROW( ( ) )
88 OAccessibleControlContext* pNew = NULL;
89 try
91 pNew = new OAccessibleControlContext;
92 pNew->Init( _rxCreator );
94 catch( const Exception& )
96 OSL_FAIL( "OAccessibleControlContext::create: caught an exception from the late ctor!" );
98 return pNew;
101 //--------------------------------------------------------------------
102 void OAccessibleControlContext::startModelListening( ) SAL_THROW( ( Exception ) )
104 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
105 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::startModelListening: invalid model!" );
106 if ( xModelComp.is() )
107 xModelComp->addEventListener( this );
110 //--------------------------------------------------------------------
111 void OAccessibleControlContext::stopModelListening( ) SAL_THROW( ( Exception ) )
113 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
114 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::stopModelListening: invalid model!" );
115 if ( xModelComp.is() )
116 xModelComp->removeEventListener( this );
119 //--------------------------------------------------------------------
120 sal_Int32 SAL_CALL OAccessibleControlContext::getAccessibleChildCount( ) throw (RuntimeException)
122 // we do not have children
123 return 0;
126 //--------------------------------------------------------------------
127 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleChild( sal_Int32 ) throw (IndexOutOfBoundsException, RuntimeException)
129 // we do not have children
130 throw IndexOutOfBoundsException();
133 //--------------------------------------------------------------------
134 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleParent( ) throw (RuntimeException)
136 OContextEntryGuard aGuard( this );
137 OSL_ENSURE( implGetForeignControlledParent().is(), "OAccessibleControlContext::getAccessibleParent: somebody forgot to set a parent!" );
138 // this parent of us is foreign controlled - somebody has to set it using the OAccessibleImplementationAccess
139 // class, before integrating our instance into an AccessibleDocumentModel
140 return implGetForeignControlledParent();
143 //--------------------------------------------------------------------
144 sal_Int16 SAL_CALL OAccessibleControlContext::getAccessibleRole( ) throw (RuntimeException)
146 return AccessibleRole::SHAPE;
149 //--------------------------------------------------------------------
150 OUString SAL_CALL OAccessibleControlContext::getAccessibleDescription( ) throw (RuntimeException)
152 OContextEntryGuard aGuard( this );
153 return getModelStringProperty( "HelpText" );
156 //--------------------------------------------------------------------
157 OUString SAL_CALL OAccessibleControlContext::getAccessibleName( ) throw (RuntimeException)
159 OContextEntryGuard aGuard( this );
160 return getModelStringProperty( "Name" );
163 //--------------------------------------------------------------------
164 Reference< XAccessibleRelationSet > SAL_CALL OAccessibleControlContext::getAccessibleRelationSet( ) throw (RuntimeException)
166 return NULL;
169 //--------------------------------------------------------------------
170 Reference< XAccessibleStateSet > SAL_CALL OAccessibleControlContext::getAccessibleStateSet( ) throw (RuntimeException)
172 ::osl::MutexGuard aGuard( GetMutex() );
173 // no OContextEntryGuard here, as we do not want to throw an exception in case we're not alive anymore
175 ::utl::AccessibleStateSetHelper* pStateSet = NULL;
176 if ( isAlive() )
178 // no own states, only the ones which are foreign controlled
179 pStateSet = new ::utl::AccessibleStateSetHelper( implGetForeignControlledStates() );
181 else
182 { // only the DEFUNC state if we're already disposed
183 pStateSet = new ::utl::AccessibleStateSetHelper;
184 pStateSet->AddState( AccessibleStateType::DEFUNC );
186 return pStateSet;
189 //--------------------------------------------------------------------
190 void SAL_CALL OAccessibleControlContext::disposing( const EventObject&
191 #if OSL_DEBUG_LEVEL > 0
192 _rSource
193 #endif
194 ) throw ( RuntimeException )
196 OSL_ENSURE( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ).get() == m_xControlModel.get(),
197 "OAccessibleControlContext::disposing: where did this come from?" );
199 stopModelListening( );
200 m_xControlModel.clear();
201 m_xModelPropsInfo.clear();
203 OAccessibleControlContext_Base::disposing();
206 //--------------------------------------------------------------------
207 OUString OAccessibleControlContext::getModelStringProperty( const sal_Char* _pPropertyName )
209 OUString sReturn;
212 if ( !m_xModelPropsInfo.is() && m_xControlModel.is() )
213 m_xModelPropsInfo = m_xControlModel->getPropertySetInfo();
215 OUString sPropertyName( OUString::createFromAscii( _pPropertyName ) );
216 if ( m_xModelPropsInfo.is() && m_xModelPropsInfo->hasPropertyByName( sPropertyName ) )
217 m_xControlModel->getPropertyValue( sPropertyName ) >>= sReturn;
219 catch( const Exception& )
221 OSL_FAIL( "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
223 return sReturn;
226 //--------------------------------------------------------------------
227 Window* OAccessibleControlContext::implGetWindow( Reference< awt::XWindow >* _pxUNOWindow ) const
229 Reference< awt::XControl > xControl( getAccessibleCreator(), UNO_QUERY );
230 Reference< awt::XWindow > xWindow;
231 if ( xControl.is() )
232 xWindow = xWindow.query( xControl->getPeer() );
234 Window* pWindow = xWindow.is() ? VCLUnoHelper::GetWindow( xWindow ) : NULL;
236 if ( _pxUNOWindow )
237 *_pxUNOWindow = xWindow;
238 return pWindow;
241 //--------------------------------------------------------------------
242 awt::Rectangle SAL_CALL OAccessibleControlContext::implGetBounds( ) throw (RuntimeException)
244 SolarMutexGuard aSolarGuard;
245 // want to do some VCL stuff here ...
246 OContextEntryGuard aGuard( this );
248 OSL_FAIL( "OAccessibleControlContext::implGetBounds: performance issue: forced to calc the size myself!" );
249 // In design mode (and this is what this class is for), the surrounding shape (if any) should handle this call
250 // The problem is that in design mode, our size may not be correct (in the drawing layer, controls are
251 // positioned/sized for painting only), and that calculation of our position is expensive
253 // what we know (or can obtain from somewhere):
254 // * the PosSize of our peer, relative to it's parent window
255 // * the parent window which the PosSize is relative to
256 // * our foreign controlled accessible parent
257 // from this info, we can determine the position of our peer relative to the foreign parent
259 // our control
260 Reference< awt::XWindow > xWindow;
261 Window* pVCLWindow = implGetWindow( &xWindow );
263 awt::Rectangle aBounds( 0, 0, 0, 0 );
264 if ( xWindow.is() )
266 // ugly, but .... though the XWindow has a getPosSize, it is impossible to determine the
267 // parent which this position/size is relative to. This means we must tunnel UNO and ask the
268 // implementation
269 Window* pVCLParent = pVCLWindow ? pVCLWindow->GetParent() : NULL;
271 // the relative location of the window
272 ::Point aWindowRelativePos( 0, 0);
273 if ( pVCLWindow )
274 aWindowRelativePos = pVCLWindow->GetPosPixel();
276 // the screnn position of the "window parent" of the control
277 ::Point aVCLParentScreenPos( 0, 0 );
278 if ( pVCLParent )
279 aVCLParentScreenPos = pVCLParent->GetPosPixel();
281 // the screen position of the "accessible parent" of the control
282 Reference< XAccessible > xParentAcc( implGetForeignControlledParent() );
283 Reference< XAccessibleComponent > xParentAccComponent;
284 if ( xParentAcc.is() )
285 xParentAccComponent = xParentAccComponent.query( xParentAcc->getAccessibleContext() );
286 awt::Point aAccParentScreenPos( 0, 0 );
287 if ( xParentAccComponent.is() )
288 aAccParentScreenPos = xParentAccComponent->getLocationOnScreen();
290 // now the size of the control
291 aBounds = xWindow->getPosSize();
293 // correct the pos
294 aBounds.X = aWindowRelativePos.X() + aVCLParentScreenPos.X() - aAccParentScreenPos.X;
295 aBounds.Y = aWindowRelativePos.Y() + aVCLParentScreenPos.Y() - aAccParentScreenPos.Y;
298 return aBounds;
301 //--------------------------------------------------------------------
302 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleAtPoint( const awt::Point& /* _rPoint */ ) throw (RuntimeException)
304 // no children at all
305 return NULL;
308 //--------------------------------------------------------------------
309 void SAL_CALL OAccessibleControlContext::grabFocus( ) throw (RuntimeException)
311 OSL_FAIL( "OAccessibleControlContext::grabFocus: !isFocusTraversable, but grabFocus!" );
314 //--------------------------------------------------------------------
315 Any SAL_CALL OAccessibleControlContext::getAccessibleKeyBinding( ) throw (RuntimeException)
317 // we do not have any key bindings to activate a UNO control in design mode
318 return Any();
321 //--------------------------------------------------------------------
322 sal_Int32 SAL_CALL OAccessibleControlContext::getForeground( ) throw (::com::sun::star::uno::RuntimeException)
324 SolarMutexGuard aSolarGuard;
325 // want to do some VCL stuff here ...
326 OContextEntryGuard aGuard( this );
328 Window* pWindow = implGetWindow( );
329 sal_Int32 nColor = 0;
330 if ( pWindow )
332 if ( pWindow->IsControlForeground() )
333 nColor = pWindow->GetControlForeground().GetColor();
334 else
336 Font aFont;
337 if ( pWindow->IsControlFont() )
338 aFont = pWindow->GetControlFont();
339 else
340 aFont = pWindow->GetFont();
341 nColor = aFont.GetColor().GetColor();
344 return nColor;
347 //--------------------------------------------------------------------
348 sal_Int32 SAL_CALL OAccessibleControlContext::getBackground( ) throw (::com::sun::star::uno::RuntimeException)
350 SolarMutexGuard aSolarGuard;
351 // want to do some VCL stuff here ...
352 OContextEntryGuard aGuard( this );
354 Window* pWindow = implGetWindow( );
355 sal_Int32 nColor = 0;
356 if ( pWindow )
358 if ( pWindow->IsControlBackground() )
359 nColor = pWindow->GetControlBackground().GetColor();
360 else
361 nColor = pWindow->GetBackground().GetColor().GetColor();
364 return nColor;
367 //........................................................................
368 } //namespace toolkit
369 //........................................................................
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */