Branch libreoffice-5-0-4
[LibreOffice.git] / toolkit / source / controls / accessiblecontrolcontext.cxx
blobbf8f38065672d6185e3d6d2331dd8c18d7d2bbe9
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 <toolkit/helper/externallock.hxx>
29 #include <vcl/window.hxx>
32 namespace toolkit
36 using ::comphelper::OContextEntryGuard;
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::accessibility;
44 //= OAccessibleControlContext
47 OAccessibleControlContext::OAccessibleControlContext()
48 : OAccessibleControlContext_Base(new VCLExternalSolarLock)
50 // nothing to do here, we have a late ctor
54 OAccessibleControlContext::~OAccessibleControlContext()
56 ensureDisposed();
60 IMPLEMENT_FORWARD_XINTERFACE3( OAccessibleControlContext, OAccessibleControlContext_Base, OAccessibleImplementationAccess, OAccessibleControlContext_IBase )
61 IMPLEMENT_FORWARD_XTYPEPROVIDER3( OAccessibleControlContext, OAccessibleControlContext_Base, OAccessibleImplementationAccess, OAccessibleControlContext_IBase )
62 // (order matters: the first is the class name, the second is the class doing the ref counting)
65 void OAccessibleControlContext::Init( const Reference< XAccessible >& _rxCreator )
67 OContextEntryGuard aGuard( this );
69 // retrieve the model of the control
70 OSL_ENSURE( !m_xControlModel.is(), "OAccessibleControlContext::Init: already know a control model....!???" );
72 Reference< awt::XControl > xControl( _rxCreator, UNO_QUERY );
73 if ( xControl.is() )
74 m_xControlModel.set(xControl->getModel(), css::uno::UNO_QUERY);
75 OSL_ENSURE( m_xControlModel.is(), "OAccessibleControlContext::Init: invalid creator (no control, or control without model!" );
76 if ( !m_xControlModel.is() )
77 throw DisposedException(); // caught by the caller (the create method)
79 // start listening at the model
80 startModelListening();
82 // announce the XAccessible to our base class
83 OAccessibleControlContext_Base::lateInit( _rxCreator );
87 OAccessibleControlContext* OAccessibleControlContext::create( const Reference< XAccessible >& _rxCreator )
89 OAccessibleControlContext* pNew = NULL;
90 try
92 pNew = new OAccessibleControlContext;
93 pNew->Init( _rxCreator );
95 catch( const Exception& )
97 OSL_FAIL( "OAccessibleControlContext::create: caught an exception from the late ctor!" );
99 return pNew;
103 void OAccessibleControlContext::startModelListening( )
105 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
106 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::startModelListening: invalid model!" );
107 if ( xModelComp.is() )
108 xModelComp->addEventListener( this );
112 void OAccessibleControlContext::stopModelListening( )
114 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
115 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::stopModelListening: invalid model!" );
116 if ( xModelComp.is() )
117 xModelComp->removeEventListener( this );
121 sal_Int32 SAL_CALL OAccessibleControlContext::getAccessibleChildCount( ) throw (RuntimeException, std::exception)
123 // we do not have children
124 return 0;
128 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleChild( sal_Int32 ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
130 // we do not have children
131 throw IndexOutOfBoundsException();
135 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleParent( ) throw (RuntimeException, std::exception)
137 OContextEntryGuard aGuard( this );
138 OSL_ENSURE( implGetForeignControlledParent().is(), "OAccessibleControlContext::getAccessibleParent: somebody forgot to set a parent!" );
139 // this parent of us is foreign controlled - somebody has to set it using the OAccessibleImplementationAccess
140 // class, before integrating our instance into an AccessibleDocumentModel
141 return implGetForeignControlledParent();
145 sal_Int16 SAL_CALL OAccessibleControlContext::getAccessibleRole( ) throw (RuntimeException, std::exception)
147 return AccessibleRole::SHAPE;
151 OUString SAL_CALL OAccessibleControlContext::getAccessibleDescription( ) throw (RuntimeException, std::exception)
153 OContextEntryGuard aGuard( this );
154 return getModelStringProperty( "HelpText" );
158 OUString SAL_CALL OAccessibleControlContext::getAccessibleName( ) throw (RuntimeException, std::exception)
160 OContextEntryGuard aGuard( this );
161 return getModelStringProperty( "Name" );
165 Reference< XAccessibleRelationSet > SAL_CALL OAccessibleControlContext::getAccessibleRelationSet( ) throw (RuntimeException, std::exception)
167 return NULL;
171 Reference< XAccessibleStateSet > SAL_CALL OAccessibleControlContext::getAccessibleStateSet( ) throw (RuntimeException, std::exception)
173 ::osl::MutexGuard aGuard( GetMutex() );
174 // no OContextEntryGuard here, as we do not want to throw an exception in case we're not alive anymore
176 ::utl::AccessibleStateSetHelper* pStateSet = NULL;
177 if ( isAlive() )
179 // no own states, only the ones which are foreign controlled
180 pStateSet = new ::utl::AccessibleStateSetHelper( implGetForeignControlledStates() );
182 else
183 { // only the DEFUNC state if we're already disposed
184 pStateSet = new ::utl::AccessibleStateSetHelper;
185 pStateSet->AddState( AccessibleStateType::DEFUNC );
187 return pStateSet;
191 void SAL_CALL OAccessibleControlContext::disposing( const EventObject&
192 #if OSL_DEBUG_LEVEL > 0
193 _rSource
194 #endif
195 ) throw ( RuntimeException, std::exception )
197 OSL_ENSURE( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ).get() == m_xControlModel.get(),
198 "OAccessibleControlContext::disposing: where did this come from?" );
200 stopModelListening( );
201 m_xControlModel.clear();
202 m_xModelPropsInfo.clear();
204 OAccessibleControlContext_Base::disposing();
208 OUString OAccessibleControlContext::getModelStringProperty( const sal_Char* _pPropertyName )
210 OUString sReturn;
213 if ( !m_xModelPropsInfo.is() && m_xControlModel.is() )
214 m_xModelPropsInfo = m_xControlModel->getPropertySetInfo();
216 OUString sPropertyName( OUString::createFromAscii( _pPropertyName ) );
217 if ( m_xModelPropsInfo.is() && m_xModelPropsInfo->hasPropertyByName( sPropertyName ) )
218 m_xControlModel->getPropertyValue( sPropertyName ) >>= sReturn;
220 catch( const Exception& )
222 OSL_FAIL( "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
224 return sReturn;
228 VclPtr< vcl::Window > OAccessibleControlContext::implGetWindow( Reference< awt::XWindow >* _pxUNOWindow ) const
230 Reference< awt::XControl > xControl( getAccessibleCreator(), UNO_QUERY );
231 Reference< awt::XWindow > xWindow;
232 if ( xControl.is() )
233 xWindow.set(xControl->getPeer(), css::uno::UNO_QUERY);
235 VclPtr< vcl::Window > pWindow = xWindow.is() ? VCLUnoHelper::GetWindow( xWindow ) : VclPtr< vcl::Window >();
237 if ( _pxUNOWindow )
238 *_pxUNOWindow = xWindow;
240 return pWindow;
244 awt::Rectangle OAccessibleControlContext::implGetBounds( ) throw (RuntimeException)
246 SolarMutexGuard aSolarGuard;
247 // want to do some VCL stuff here ...
248 OContextEntryGuard aGuard( this );
250 OSL_FAIL( "OAccessibleControlContext::implGetBounds: performance issue: forced to calc the size myself!" );
251 // In design mode (and this is what this class is for), the surrounding shape (if any) should handle this call
252 // The problem is that in design mode, our size may not be correct (in the drawing layer, controls are
253 // positioned/sized for painting only), and that calculation of our position is expensive
255 // what we know (or can obtain from somewhere):
256 // * the PosSize of our peer, relative to its parent window
257 // * the parent window which the PosSize is relative to
258 // * our foreign controlled accessible parent
259 // from this info, we can determine the position of our peer relative to the foreign parent
261 // our control
262 Reference< awt::XWindow > xWindow;
263 VclPtr< vcl::Window > pVCLWindow = implGetWindow( &xWindow );
265 awt::Rectangle aBounds( 0, 0, 0, 0 );
266 if ( xWindow.is() )
268 // ugly, but .... though the XWindow has a getPosSize, it is impossible to determine the
269 // parent which this position/size is relative to. This means we must tunnel UNO and ask the
270 // implementation
271 vcl::Window* pVCLParent = pVCLWindow ? pVCLWindow->GetParent() : NULL;
273 // the relative location of the window
274 ::Point aWindowRelativePos( 0, 0);
275 if ( pVCLWindow )
276 aWindowRelativePos = pVCLWindow->GetPosPixel();
278 // the screnn position of the "window parent" of the control
279 ::Point aVCLParentScreenPos( 0, 0 );
280 if ( pVCLParent )
281 aVCLParentScreenPos = pVCLParent->GetPosPixel();
283 // the screen position of the "accessible parent" of the control
284 Reference< XAccessible > xParentAcc( implGetForeignControlledParent() );
285 Reference< XAccessibleComponent > xParentAccComponent;
286 if ( xParentAcc.is() )
287 xParentAccComponent.set(xParentAcc->getAccessibleContext(), css::uno::UNO_QUERY);
288 awt::Point aAccParentScreenPos( 0, 0 );
289 if ( xParentAccComponent.is() )
290 aAccParentScreenPos = xParentAccComponent->getLocationOnScreen();
292 // now the size of the control
293 aBounds = xWindow->getPosSize();
295 // correct the pos
296 aBounds.X = aWindowRelativePos.X() + aVCLParentScreenPos.X() - aAccParentScreenPos.X;
297 aBounds.Y = aWindowRelativePos.Y() + aVCLParentScreenPos.Y() - aAccParentScreenPos.Y;
300 return aBounds;
304 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleAtPoint( const awt::Point& /* _rPoint */ ) throw (RuntimeException, std::exception)
306 // no children at all
307 return NULL;
311 void SAL_CALL OAccessibleControlContext::grabFocus( ) throw (RuntimeException, std::exception)
313 OSL_FAIL( "OAccessibleControlContext::grabFocus: !isFocusTraversable, but grabFocus!" );
317 sal_Int32 SAL_CALL OAccessibleControlContext::getForeground( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
319 SolarMutexGuard aSolarGuard;
320 // want to do some VCL stuff here ...
321 OContextEntryGuard aGuard( this );
323 VclPtr< vcl::Window > pWindow = implGetWindow();
324 sal_Int32 nColor = 0;
325 if ( pWindow )
327 if ( pWindow->IsControlForeground() )
328 nColor = pWindow->GetControlForeground().GetColor();
329 else
331 vcl::Font aFont;
332 if ( pWindow->IsControlFont() )
333 aFont = pWindow->GetControlFont();
334 else
335 aFont = pWindow->GetFont();
336 nColor = aFont.GetColor().GetColor();
339 return nColor;
343 sal_Int32 SAL_CALL OAccessibleControlContext::getBackground( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
345 SolarMutexGuard aSolarGuard;
346 // want to do some VCL stuff here ...
347 OContextEntryGuard aGuard( this );
349 VclPtr< vcl::Window > pWindow = implGetWindow();
350 sal_Int32 nColor = 0;
351 if ( pWindow )
353 if ( pWindow->IsControlBackground() )
354 nColor = pWindow->GetControlBackground().GetColor();
355 else
356 nColor = pWindow->GetBackground().GetColor().GetColor();
359 return nColor;
363 } //namespace toolkit
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */