Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / toolkit / source / controls / accessiblecontrolcontext.cxx
blob7f830bc033a72edb5d68df27e64bb6fffb86b9c9
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 <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()
49 // nothing to do here, we have a late ctor
53 OAccessibleControlContext::~OAccessibleControlContext()
55 ensureDisposed();
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)
64 void OAccessibleControlContext::Init( const Reference< XAccessible >& _rxCreator )
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.set(xControl->getModel(), css::uno::UNO_QUERY);
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 );
86 OAccessibleControlContext* OAccessibleControlContext::create( const Reference< XAccessible >& _rxCreator )
88 OAccessibleControlContext* pNew = nullptr;
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;
102 void OAccessibleControlContext::startModelListening( )
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 );
111 void OAccessibleControlContext::stopModelListening( )
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 );
120 sal_Int32 SAL_CALL OAccessibleControlContext::getAccessibleChildCount( )
122 // we do not have children
123 return 0;
127 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleChild( sal_Int32 )
129 // we do not have children
130 throw IndexOutOfBoundsException();
134 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleParent( )
136 return Reference< XAccessible >();
140 sal_Int16 SAL_CALL OAccessibleControlContext::getAccessibleRole( )
142 return AccessibleRole::SHAPE;
146 OUString SAL_CALL OAccessibleControlContext::getAccessibleDescription( )
148 OContextEntryGuard aGuard( this );
149 return getModelStringProperty( "HelpText" );
153 OUString SAL_CALL OAccessibleControlContext::getAccessibleName( )
155 OContextEntryGuard aGuard( this );
156 return getModelStringProperty( "Name" );
160 Reference< XAccessibleRelationSet > SAL_CALL OAccessibleControlContext::getAccessibleRelationSet( )
162 return nullptr;
166 Reference< XAccessibleStateSet > SAL_CALL OAccessibleControlContext::getAccessibleStateSet( )
168 ::osl::MutexGuard aGuard( GetMutex() );
169 // no OContextEntryGuard here, as we do not want to throw an exception in case we're not alive anymore
171 ::utl::AccessibleStateSetHelper* pStateSet = nullptr;
172 if ( isAlive() )
174 // no own states, only the ones which are foreign controlled
175 pStateSet = new ::utl::AccessibleStateSetHelper( 0 );
177 else
178 { // only the DEFUNC state if we're already disposed
179 pStateSet = new ::utl::AccessibleStateSetHelper;
180 pStateSet->AddState( AccessibleStateType::DEFUNC );
182 return pStateSet;
186 void SAL_CALL OAccessibleControlContext::disposing( const EventObject& _rSource )
188 OSL_ENSURE( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ).get() == m_xControlModel.get(),
189 "OAccessibleControlContext::disposing: where did this come from?" );
191 stopModelListening( );
192 m_xControlModel.clear();
193 m_xModelPropsInfo.clear();
195 OAccessibleControlContext_Base::disposing();
199 OUString OAccessibleControlContext::getModelStringProperty( const sal_Char* _pPropertyName )
201 OUString sReturn;
204 if ( !m_xModelPropsInfo.is() && m_xControlModel.is() )
205 m_xModelPropsInfo = m_xControlModel->getPropertySetInfo();
207 OUString sPropertyName( OUString::createFromAscii( _pPropertyName ) );
208 if ( m_xModelPropsInfo.is() && m_xModelPropsInfo->hasPropertyByName( sPropertyName ) )
209 m_xControlModel->getPropertyValue( sPropertyName ) >>= sReturn;
211 catch( const Exception& )
213 OSL_FAIL( "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
215 return sReturn;
219 VclPtr< vcl::Window > OAccessibleControlContext::implGetWindow( Reference< awt::XWindow >* _pxUNOWindow ) const
221 Reference< awt::XControl > xControl( getAccessibleCreator(), UNO_QUERY );
222 Reference< awt::XWindow > xWindow;
223 if ( xControl.is() )
224 xWindow.set(xControl->getPeer(), css::uno::UNO_QUERY);
226 VclPtr< vcl::Window > pWindow = xWindow.is() ? VCLUnoHelper::GetWindow( xWindow ) : VclPtr< vcl::Window >();
228 if ( _pxUNOWindow )
229 *_pxUNOWindow = xWindow;
231 return pWindow;
235 awt::Rectangle OAccessibleControlContext::implGetBounds( )
237 SolarMutexGuard aSolarGuard;
238 // want to do some VCL stuff here ...
239 OContextEntryGuard aGuard( this );
241 OSL_FAIL( "OAccessibleControlContext::implGetBounds: performance issue: forced to calc the size myself!" );
242 // In design mode (and this is what this class is for), the surrounding shape (if any) should handle this call
243 // The problem is that in design mode, our size may not be correct (in the drawing layer, controls are
244 // positioned/sized for painting only), and that calculation of our position is expensive
246 // what we know (or can obtain from somewhere):
247 // * the PosSize of our peer, relative to its parent window
248 // * the parent window which the PosSize is relative to
249 // * our foreign controlled accessible parent
250 // from this info, we can determine the position of our peer relative to the foreign parent
252 // our control
253 Reference< awt::XWindow > xWindow;
254 VclPtr< vcl::Window > pVCLWindow = implGetWindow( &xWindow );
256 awt::Rectangle aBounds( 0, 0, 0, 0 );
257 if ( xWindow.is() )
259 // ugly, but .... though the XWindow has a getPosSize, it is impossible to determine the
260 // parent which this position/size is relative to. This means we must tunnel UNO and ask the
261 // implementation
262 vcl::Window* pVCLParent = pVCLWindow ? pVCLWindow->GetParent() : nullptr;
264 // the relative location of the window
265 ::Point aWindowRelativePos( 0, 0);
266 if ( pVCLWindow )
267 aWindowRelativePos = pVCLWindow->GetPosPixel();
269 // the screen position of the "window parent" of the control
270 ::Point aVCLParentScreenPos( 0, 0 );
271 if ( pVCLParent )
272 aVCLParentScreenPos = pVCLParent->GetPosPixel();
274 // now the size of the control
275 aBounds = xWindow->getPosSize();
277 // correct the pos
278 aBounds.X = aWindowRelativePos.X() + aVCLParentScreenPos.X();
279 aBounds.Y = aWindowRelativePos.Y() + aVCLParentScreenPos.Y();
282 return aBounds;
286 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleAtPoint( const awt::Point& /* _rPoint */ )
288 // no children at all
289 return nullptr;
293 void SAL_CALL OAccessibleControlContext::grabFocus( )
295 OSL_FAIL( "OAccessibleControlContext::grabFocus: !isFocusTraversable, but grabFocus!" );
299 sal_Int32 SAL_CALL OAccessibleControlContext::getForeground( )
301 SolarMutexGuard aSolarGuard;
302 // want to do some VCL stuff here ...
303 OContextEntryGuard aGuard( this );
305 VclPtr< vcl::Window > pWindow = implGetWindow();
306 Color nColor;
307 if ( pWindow )
309 if ( pWindow->IsControlForeground() )
310 nColor = pWindow->GetControlForeground();
311 else
313 vcl::Font aFont;
314 if ( pWindow->IsControlFont() )
315 aFont = pWindow->GetControlFont();
316 else
317 aFont = pWindow->GetFont();
318 nColor = aFont.GetColor();
321 return sal_Int32(nColor);
325 sal_Int32 SAL_CALL OAccessibleControlContext::getBackground( )
327 SolarMutexGuard aSolarGuard;
328 // want to do some VCL stuff here ...
329 OContextEntryGuard aGuard( this );
331 VclPtr< vcl::Window > pWindow = implGetWindow();
332 Color nColor;
333 if ( pWindow )
335 if ( pWindow->IsControlBackground() )
336 nColor = pWindow->GetControlBackground();
337 else
338 nColor = pWindow->GetBackground().GetColor();
341 return sal_Int32(nColor);
345 } //namespace toolkit
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */