bump product version to 6.3.0.0.beta1
[LibreOffice.git] / toolkit / source / controls / accessiblecontrolcontext.cxx
bloba72f7694ebf8804dd915b65205bf02fae2613148
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/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <vcl/svapp.hxx>
27 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
28 #include <com/sun/star/accessibility/AccessibleRole.hpp>
29 #include <toolkit/helper/vclunohelper.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()
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 = nullptr;
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( )
123 // we do not have children
124 return 0;
128 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleChild( sal_Int32 )
130 // we do not have children
131 throw IndexOutOfBoundsException();
135 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleParent( )
137 return Reference< XAccessible >();
141 sal_Int16 SAL_CALL OAccessibleControlContext::getAccessibleRole( )
143 return AccessibleRole::SHAPE;
147 OUString SAL_CALL OAccessibleControlContext::getAccessibleDescription( )
149 OContextEntryGuard aGuard( this );
150 return getModelStringProperty( "HelpText" );
154 OUString SAL_CALL OAccessibleControlContext::getAccessibleName( )
156 OContextEntryGuard aGuard( this );
157 return getModelStringProperty( "Name" );
161 Reference< XAccessibleRelationSet > SAL_CALL OAccessibleControlContext::getAccessibleRelationSet( )
163 return nullptr;
167 Reference< XAccessibleStateSet > SAL_CALL OAccessibleControlContext::getAccessibleStateSet( )
169 ::osl::MutexGuard aGuard( GetMutex() );
170 // no OContextEntryGuard here, as we do not want to throw an exception in case we're not alive anymore
172 ::utl::AccessibleStateSetHelper* pStateSet = nullptr;
173 if ( isAlive() )
175 // no own states, only the ones which are foreign controlled
176 pStateSet = new ::utl::AccessibleStateSetHelper( 0 );
178 else
179 { // only the DEFUNC state if we're already disposed
180 pStateSet = new ::utl::AccessibleStateSetHelper;
181 pStateSet->AddState( AccessibleStateType::DEFUNC );
183 return pStateSet;
187 void SAL_CALL OAccessibleControlContext::disposing( const EventObject& _rSource )
189 OSL_ENSURE( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ).get() == m_xControlModel.get(),
190 "OAccessibleControlContext::disposing: where did this come from?" );
192 stopModelListening( );
193 m_xControlModel.clear();
194 m_xModelPropsInfo.clear();
196 OAccessibleControlContext_Base::disposing();
200 OUString OAccessibleControlContext::getModelStringProperty( const sal_Char* _pPropertyName )
202 OUString sReturn;
205 if ( !m_xModelPropsInfo.is() && m_xControlModel.is() )
206 m_xModelPropsInfo = m_xControlModel->getPropertySetInfo();
208 OUString sPropertyName( OUString::createFromAscii( _pPropertyName ) );
209 if ( m_xModelPropsInfo.is() && m_xModelPropsInfo->hasPropertyByName( sPropertyName ) )
210 m_xControlModel->getPropertyValue( sPropertyName ) >>= sReturn;
212 catch( const Exception& )
214 OSL_FAIL( "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
216 return sReturn;
220 VclPtr< vcl::Window > OAccessibleControlContext::implGetWindow( Reference< awt::XWindow >* _pxUNOWindow ) const
222 Reference< awt::XControl > xControl( getAccessibleCreator(), UNO_QUERY );
223 Reference< awt::XWindow > xWindow;
224 if ( xControl.is() )
225 xWindow.set(xControl->getPeer(), css::uno::UNO_QUERY);
227 VclPtr< vcl::Window > pWindow = xWindow.is() ? VCLUnoHelper::GetWindow( xWindow ) : VclPtr< vcl::Window >();
229 if ( _pxUNOWindow )
230 *_pxUNOWindow = xWindow;
232 return pWindow;
236 awt::Rectangle OAccessibleControlContext::implGetBounds( )
238 SolarMutexGuard aSolarGuard;
239 // want to do some VCL stuff here ...
240 OContextEntryGuard aGuard( this );
242 OSL_FAIL( "OAccessibleControlContext::implGetBounds: performance issue: forced to calc the size myself!" );
243 // In design mode (and this is what this class is for), the surrounding shape (if any) should handle this call
244 // The problem is that in design mode, our size may not be correct (in the drawing layer, controls are
245 // positioned/sized for painting only), and that calculation of our position is expensive
247 // what we know (or can obtain from somewhere):
248 // * the PosSize of our peer, relative to its parent window
249 // * the parent window which the PosSize is relative to
250 // * our foreign controlled accessible parent
251 // from this info, we can determine the position of our peer relative to the foreign parent
253 // our control
254 Reference< awt::XWindow > xWindow;
255 VclPtr< vcl::Window > pVCLWindow = implGetWindow( &xWindow );
257 awt::Rectangle aBounds( 0, 0, 0, 0 );
258 if ( xWindow.is() )
260 // ugly, but .... though the XWindow has a getPosSize, it is impossible to determine the
261 // parent which this position/size is relative to. This means we must tunnel UNO and ask the
262 // implementation
263 vcl::Window* pVCLParent = pVCLWindow ? pVCLWindow->GetParent() : nullptr;
265 // the relative location of the window
266 ::Point aWindowRelativePos( 0, 0);
267 if ( pVCLWindow )
268 aWindowRelativePos = pVCLWindow->GetPosPixel();
270 // the screen position of the "window parent" of the control
271 ::Point aVCLParentScreenPos( 0, 0 );
272 if ( pVCLParent )
273 aVCLParentScreenPos = pVCLParent->GetPosPixel();
275 // now the size of the control
276 aBounds = xWindow->getPosSize();
278 // correct the pos
279 aBounds.X = aWindowRelativePos.X() + aVCLParentScreenPos.X();
280 aBounds.Y = aWindowRelativePos.Y() + aVCLParentScreenPos.Y();
283 return aBounds;
287 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleAtPoint( const awt::Point& /* _rPoint */ )
289 // no children at all
290 return nullptr;
294 void SAL_CALL OAccessibleControlContext::grabFocus( )
296 OSL_FAIL( "OAccessibleControlContext::grabFocus: !isFocusTraversable, but grabFocus!" );
300 sal_Int32 SAL_CALL OAccessibleControlContext::getForeground( )
302 SolarMutexGuard aSolarGuard;
303 // want to do some VCL stuff here ...
304 OContextEntryGuard aGuard( this );
306 VclPtr< vcl::Window > pWindow = implGetWindow();
307 Color nColor;
308 if ( pWindow )
310 if ( pWindow->IsControlForeground() )
311 nColor = pWindow->GetControlForeground();
312 else
314 vcl::Font aFont;
315 if ( pWindow->IsControlFont() )
316 aFont = pWindow->GetControlFont();
317 else
318 aFont = pWindow->GetFont();
319 nColor = aFont.GetColor();
322 return sal_Int32(nColor);
326 sal_Int32 SAL_CALL OAccessibleControlContext::getBackground( )
328 SolarMutexGuard aSolarGuard;
329 // want to do some VCL stuff here ...
330 OContextEntryGuard aGuard( this );
332 VclPtr< vcl::Window > pWindow = implGetWindow();
333 Color nColor;
334 if ( pWindow )
336 if ( pWindow->IsControlBackground() )
337 nColor = pWindow->GetControlBackground();
338 else
339 nColor = pWindow->GetBackground().GetColor();
342 return sal_Int32(nColor);
346 } //namespace toolkit
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */