Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / toolkit / source / controls / accessiblecontrolcontext.cxx
blob7cf45ec18fecd7dac91dac792cd863b8cc721972
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 <com/sun/star/lang/IndexOutOfBoundsException.hpp>
25 #include <vcl/svapp.hxx>
26 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
27 #include <com/sun/star/accessibility/AccessibleRole.hpp>
28 #include <toolkit/helper/vclunohelper.hxx>
29 #include <toolkit/helper/externallock.hxx>
30 #include <vcl/window.hxx>
33 namespace toolkit
37 using ::comphelper::OContextEntryGuard;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::accessibility;
45 //= OAccessibleControlContext
48 OAccessibleControlContext::OAccessibleControlContext()
49 : OAccessibleControlContext_Base(new VCLExternalSolarLock)
51 // nothing to do here, we have a late ctor
55 OAccessibleControlContext::~OAccessibleControlContext()
57 ensureDisposed();
61 IMPLEMENT_FORWARD_XINTERFACE3( OAccessibleControlContext, OAccessibleControlContext_Base, OAccessibleImplementationAccess, OAccessibleControlContext_IBase )
62 IMPLEMENT_FORWARD_XTYPEPROVIDER3( OAccessibleControlContext, OAccessibleControlContext_Base, OAccessibleImplementationAccess, OAccessibleControlContext_IBase )
63 // (order matters: the first is the class name, the second is the class doing the ref counting)
66 void OAccessibleControlContext::Init( const Reference< XAccessible >& _rxCreator )
68 OContextEntryGuard aGuard( this );
70 // retrieve the model of the control
71 OSL_ENSURE( !m_xControlModel.is(), "OAccessibleControlContext::Init: already know a control model....!???" );
73 Reference< awt::XControl > xControl( _rxCreator, UNO_QUERY );
74 if ( xControl.is() )
75 m_xControlModel.set(xControl->getModel(), css::uno::UNO_QUERY);
76 OSL_ENSURE( m_xControlModel.is(), "OAccessibleControlContext::Init: invalid creator (no control, or control without model!" );
77 if ( !m_xControlModel.is() )
78 throw DisposedException(); // caught by the caller (the create method)
80 // start listening at the model
81 startModelListening();
83 // announce the XAccessible to our base class
84 OAccessibleControlContext_Base::lateInit( _rxCreator );
88 OAccessibleControlContext* OAccessibleControlContext::create( const Reference< XAccessible >& _rxCreator )
90 OAccessibleControlContext* pNew = nullptr;
91 try
93 pNew = new OAccessibleControlContext;
94 pNew->Init( _rxCreator );
96 catch( const Exception& )
98 OSL_FAIL( "OAccessibleControlContext::create: caught an exception from the late ctor!" );
100 return pNew;
104 void OAccessibleControlContext::startModelListening( )
106 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
107 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::startModelListening: invalid model!" );
108 if ( xModelComp.is() )
109 xModelComp->addEventListener( this );
113 void OAccessibleControlContext::stopModelListening( )
115 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
116 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::stopModelListening: invalid model!" );
117 if ( xModelComp.is() )
118 xModelComp->removeEventListener( this );
122 sal_Int32 SAL_CALL OAccessibleControlContext::getAccessibleChildCount( )
124 // we do not have children
125 return 0;
129 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleChild( sal_Int32 )
131 // we do not have children
132 throw IndexOutOfBoundsException();
136 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleParent( )
138 OContextEntryGuard aGuard( this );
139 OSL_ENSURE( implGetForeignControlledParent().is(), "OAccessibleControlContext::getAccessibleParent: somebody forgot to set a parent!" );
140 // this parent of us is foreign controlled - somebody has to set it using the OAccessibleImplementationAccess
141 // class, before integrating our instance into an AccessibleDocumentModel
142 return implGetForeignControlledParent();
146 sal_Int16 SAL_CALL OAccessibleControlContext::getAccessibleRole( )
148 return AccessibleRole::SHAPE;
152 OUString SAL_CALL OAccessibleControlContext::getAccessibleDescription( )
154 OContextEntryGuard aGuard( this );
155 return getModelStringProperty( "HelpText" );
159 OUString SAL_CALL OAccessibleControlContext::getAccessibleName( )
161 OContextEntryGuard aGuard( this );
162 return getModelStringProperty( "Name" );
166 Reference< XAccessibleRelationSet > SAL_CALL OAccessibleControlContext::getAccessibleRelationSet( )
168 return nullptr;
172 Reference< XAccessibleStateSet > SAL_CALL OAccessibleControlContext::getAccessibleStateSet( )
174 ::osl::MutexGuard aGuard( GetMutex() );
175 // no OContextEntryGuard here, as we do not want to throw an exception in case we're not alive anymore
177 ::utl::AccessibleStateSetHelper* pStateSet = nullptr;
178 if ( isAlive() )
180 // no own states, only the ones which are foreign controlled
181 pStateSet = new ::utl::AccessibleStateSetHelper( implGetForeignControlledStates() );
183 else
184 { // only the DEFUNC state if we're already disposed
185 pStateSet = new ::utl::AccessibleStateSetHelper;
186 pStateSet->AddState( AccessibleStateType::DEFUNC );
188 return pStateSet;
192 void SAL_CALL OAccessibleControlContext::disposing( const EventObject& _rSource )
194 OSL_ENSURE( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ).get() == m_xControlModel.get(),
195 "OAccessibleControlContext::disposing: where did this come from?" );
197 stopModelListening( );
198 m_xControlModel.clear();
199 m_xModelPropsInfo.clear();
201 OAccessibleControlContext_Base::disposing();
205 OUString OAccessibleControlContext::getModelStringProperty( const sal_Char* _pPropertyName )
207 OUString sReturn;
210 if ( !m_xModelPropsInfo.is() && m_xControlModel.is() )
211 m_xModelPropsInfo = m_xControlModel->getPropertySetInfo();
213 OUString sPropertyName( OUString::createFromAscii( _pPropertyName ) );
214 if ( m_xModelPropsInfo.is() && m_xModelPropsInfo->hasPropertyByName( sPropertyName ) )
215 m_xControlModel->getPropertyValue( sPropertyName ) >>= sReturn;
217 catch( const Exception& )
219 OSL_FAIL( "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
221 return sReturn;
225 VclPtr< vcl::Window > OAccessibleControlContext::implGetWindow( Reference< awt::XWindow >* _pxUNOWindow ) const
227 Reference< awt::XControl > xControl( getAccessibleCreator(), UNO_QUERY );
228 Reference< awt::XWindow > xWindow;
229 if ( xControl.is() )
230 xWindow.set(xControl->getPeer(), css::uno::UNO_QUERY);
232 VclPtr< vcl::Window > pWindow = xWindow.is() ? VCLUnoHelper::GetWindow( xWindow ) : VclPtr< vcl::Window >();
234 if ( _pxUNOWindow )
235 *_pxUNOWindow = xWindow;
237 return pWindow;
241 awt::Rectangle OAccessibleControlContext::implGetBounds( )
243 SolarMutexGuard aSolarGuard;
244 // want to do some VCL stuff here ...
245 OContextEntryGuard aGuard( this );
247 OSL_FAIL( "OAccessibleControlContext::implGetBounds: performance issue: forced to calc the size myself!" );
248 // In design mode (and this is what this class is for), the surrounding shape (if any) should handle this call
249 // The problem is that in design mode, our size may not be correct (in the drawing layer, controls are
250 // positioned/sized for painting only), and that calculation of our position is expensive
252 // what we know (or can obtain from somewhere):
253 // * the PosSize of our peer, relative to its parent window
254 // * the parent window which the PosSize is relative to
255 // * our foreign controlled accessible parent
256 // from this info, we can determine the position of our peer relative to the foreign parent
258 // our control
259 Reference< awt::XWindow > xWindow;
260 VclPtr< vcl::Window > pVCLWindow = implGetWindow( &xWindow );
262 awt::Rectangle aBounds( 0, 0, 0, 0 );
263 if ( xWindow.is() )
265 // ugly, but .... though the XWindow has a getPosSize, it is impossible to determine the
266 // parent which this position/size is relative to. This means we must tunnel UNO and ask the
267 // implementation
268 vcl::Window* pVCLParent = pVCLWindow ? pVCLWindow->GetParent() : nullptr;
270 // the relative location of the window
271 ::Point aWindowRelativePos( 0, 0);
272 if ( pVCLWindow )
273 aWindowRelativePos = pVCLWindow->GetPosPixel();
275 // the screen position of the "window parent" of the control
276 ::Point aVCLParentScreenPos( 0, 0 );
277 if ( pVCLParent )
278 aVCLParentScreenPos = pVCLParent->GetPosPixel();
280 // the screen position of the "accessible parent" of the control
281 Reference< XAccessible > xParentAcc( implGetForeignControlledParent() );
282 Reference< XAccessibleComponent > xParentAccComponent;
283 if ( xParentAcc.is() )
284 xParentAccComponent.set(xParentAcc->getAccessibleContext(), css::uno::UNO_QUERY);
285 awt::Point aAccParentScreenPos( 0, 0 );
286 if ( xParentAccComponent.is() )
287 aAccParentScreenPos = xParentAccComponent->getLocationOnScreen();
289 // now the size of the control
290 aBounds = xWindow->getPosSize();
292 // correct the pos
293 aBounds.X = aWindowRelativePos.X() + aVCLParentScreenPos.X() - aAccParentScreenPos.X;
294 aBounds.Y = aWindowRelativePos.Y() + aVCLParentScreenPos.Y() - aAccParentScreenPos.Y;
297 return aBounds;
301 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleAtPoint( const awt::Point& /* _rPoint */ )
303 // no children at all
304 return nullptr;
308 void SAL_CALL OAccessibleControlContext::grabFocus( )
310 OSL_FAIL( "OAccessibleControlContext::grabFocus: !isFocusTraversable, but grabFocus!" );
314 sal_Int32 SAL_CALL OAccessibleControlContext::getForeground( )
316 SolarMutexGuard aSolarGuard;
317 // want to do some VCL stuff here ...
318 OContextEntryGuard aGuard( this );
320 VclPtr< vcl::Window > pWindow = implGetWindow();
321 sal_Int32 nColor = 0;
322 if ( pWindow )
324 if ( pWindow->IsControlForeground() )
325 nColor = pWindow->GetControlForeground().GetColor();
326 else
328 vcl::Font aFont;
329 if ( pWindow->IsControlFont() )
330 aFont = pWindow->GetControlFont();
331 else
332 aFont = pWindow->GetFont();
333 nColor = aFont.GetColor().GetColor();
336 return nColor;
340 sal_Int32 SAL_CALL OAccessibleControlContext::getBackground( )
342 SolarMutexGuard aSolarGuard;
343 // want to do some VCL stuff here ...
344 OContextEntryGuard aGuard( this );
346 VclPtr< vcl::Window > pWindow = implGetWindow();
347 sal_Int32 nColor = 0;
348 if ( pWindow )
350 if ( pWindow->IsControlBackground() )
351 nColor = pWindow->GetControlBackground().GetColor();
352 else
353 nColor = pWindow->GetBackground().GetColor().GetColor();
356 return nColor;
360 } //namespace toolkit
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */