Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / controls / accessiblecontrolcontext.cxx
blob761821bce601c40afc8f190746cb7c6029d110ee
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 <controls/accessiblecontrolcontext.hxx>
21 #include <com/sun/star/awt/XControl.hpp>
22 #include <com/sun/star/awt/XWindow.hpp>
23 #include <com/sun/star/beans/XPropertySet.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 <comphelper/accessiblecontexthelper.hxx>
30 #include <comphelper/diagnose_ex.hxx>
31 #include <vcl/window.hxx>
34 namespace toolkit
38 using ::comphelper::OContextEntryGuard;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::accessibility;
46 //= OAccessibleControlContext
49 OAccessibleControlContext::OAccessibleControlContext()
51 // nothing to do here, we have a late ctor
55 OAccessibleControlContext::~OAccessibleControlContext()
57 ensureDisposed();
61 void OAccessibleControlContext::Init( const Reference< XAccessible >& _rxCreator )
63 OContextEntryGuard aGuard( this );
65 // retrieve the model of the control
66 OSL_ENSURE( !m_xControlModel.is(), "OAccessibleControlContext::Init: already know a control model...!???" );
68 Reference< awt::XControl > xControl( _rxCreator, UNO_QUERY );
69 if ( xControl.is() )
70 m_xControlModel.set(xControl->getModel(), css::uno::UNO_QUERY);
71 OSL_ENSURE( m_xControlModel.is(), "OAccessibleControlContext::Init: invalid creator (no control, or control without model!" );
72 if ( !m_xControlModel.is() )
73 throw DisposedException(); // caught by the caller (the create method)
75 // start listening at the model
76 startModelListening();
78 // announce the XAccessible to our base class
79 OAccessibleControlContext_Base::lateInit( _rxCreator );
83 rtl::Reference<OAccessibleControlContext> OAccessibleControlContext::create( const Reference< XAccessible >& _rxCreator )
85 rtl::Reference<OAccessibleControlContext> pNew;
86 try
88 pNew = new OAccessibleControlContext;
89 pNew->Init( _rxCreator );
91 catch( const Exception& )
93 TOOLS_WARN_EXCEPTION( "toolkit", "OAccessibleControlContext::create: caught an exception from the late ctor!" );
95 return pNew;
99 void OAccessibleControlContext::startModelListening( )
101 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
102 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::startModelListening: invalid model!" );
103 if ( xModelComp.is() )
104 xModelComp->addEventListener( this );
108 void OAccessibleControlContext::stopModelListening( )
110 Reference< XComponent > xModelComp( m_xControlModel, UNO_QUERY );
111 OSL_ENSURE( xModelComp.is(), "OAccessibleControlContext::stopModelListening: invalid model!" );
112 if ( xModelComp.is() )
113 xModelComp->removeEventListener( this );
117 sal_Int64 SAL_CALL OAccessibleControlContext::getAccessibleChildCount( )
119 // we do not have children
120 return 0;
124 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleChild( sal_Int64 )
126 // we do not have children
127 throw IndexOutOfBoundsException();
131 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleParent( )
133 return Reference< XAccessible >();
137 sal_Int16 SAL_CALL OAccessibleControlContext::getAccessibleRole( )
139 return AccessibleRole::SHAPE;
143 OUString SAL_CALL OAccessibleControlContext::getAccessibleDescription( )
145 OContextEntryGuard aGuard( this );
146 return getModelStringProperty( "HelpText" );
150 OUString SAL_CALL OAccessibleControlContext::getAccessibleName( )
152 OContextEntryGuard aGuard( this );
153 return getModelStringProperty( "Name" );
157 Reference< XAccessibleRelationSet > SAL_CALL OAccessibleControlContext::getAccessibleRelationSet( )
159 return nullptr;
163 sal_Int64 SAL_CALL OAccessibleControlContext::getAccessibleStateSet( )
165 ::osl::MutexGuard aGuard( GetMutex() );
166 // no OContextEntryGuard here, as we do not want to throw an exception in case we're not alive anymore
168 sal_Int64 nStateSet = 0;
169 if ( isAlive() )
171 // no own states, only the ones which are foreign controlled
173 else
174 { // only the DEFUNC state if we're already disposed
175 nStateSet |= AccessibleStateType::DEFUNC;
177 return nStateSet;
181 void SAL_CALL OAccessibleControlContext::disposing( const EventObject& _rSource )
183 OSL_ENSURE( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ).get() == m_xControlModel.get(),
184 "OAccessibleControlContext::disposing: where did this come from?" );
186 stopModelListening( );
187 m_xControlModel.clear();
188 m_xModelPropsInfo.clear();
190 OAccessibleControlContext_Base::disposing();
194 OUString OAccessibleControlContext::getModelStringProperty( const char* _pPropertyName )
196 OUString sReturn;
199 if ( !m_xModelPropsInfo.is() && m_xControlModel.is() )
200 m_xModelPropsInfo = m_xControlModel->getPropertySetInfo();
202 OUString sPropertyName( OUString::createFromAscii( _pPropertyName ) );
203 if ( m_xModelPropsInfo.is() && m_xModelPropsInfo->hasPropertyByName( sPropertyName ) )
204 m_xControlModel->getPropertyValue( sPropertyName ) >>= sReturn;
206 catch( const Exception& )
208 TOOLS_WARN_EXCEPTION( "toolkit", "OAccessibleControlContext::getModelStringProperty" );
210 return sReturn;
214 vcl::Window* OAccessibleControlContext::implGetWindow( Reference< awt::XWindow >* _pxUNOWindow ) const
216 Reference< awt::XControl > xControl( getAccessibleCreator(), UNO_QUERY );
217 Reference< awt::XWindow > xWindow;
218 if ( xControl.is() )
219 xWindow.set(xControl->getPeer(), css::uno::UNO_QUERY);
221 vcl::Window* pWindow = xWindow.is() ? VCLUnoHelper::GetWindow( xWindow ) : nullptr;
223 if ( _pxUNOWindow )
224 *_pxUNOWindow = xWindow;
226 return pWindow;
230 awt::Rectangle OAccessibleControlContext::implGetBounds( )
232 SolarMutexGuard aSolarGuard;
233 // want to do some VCL stuff here ...
234 OContextEntryGuard aGuard( this );
236 OSL_FAIL( "OAccessibleControlContext::implGetBounds: performance issue: forced to calc the size myself!" );
237 // In design mode (and this is what this class is for), the surrounding shape (if any) should handle this call
238 // The problem is that in design mode, our size may not be correct (in the drawing layer, controls are
239 // positioned/sized for painting only), and that calculation of our position is expensive
241 // what we know (or can obtain from somewhere):
242 // * the PosSize of our peer, relative to its parent window
243 // * the parent window which the PosSize is relative to
244 // * our foreign controlled accessible parent
245 // from this info, we can determine the position of our peer relative to the foreign parent
247 // our control
248 Reference< awt::XWindow > xWindow;
249 VclPtr< vcl::Window > pVCLWindow = implGetWindow( &xWindow );
251 awt::Rectangle aBounds( 0, 0, 0, 0 );
252 if ( xWindow.is() )
254 // ugly, but... though the XWindow has a getPosSize, it is impossible to determine the
255 // parent which this position/size is relative to. This means we must tunnel UNO and ask the
256 // implementation
257 vcl::Window* pVCLParent = pVCLWindow ? pVCLWindow->GetParent() : nullptr;
259 // the relative location of the window
260 ::Point aWindowRelativePos( 0, 0);
261 if ( pVCLWindow )
262 aWindowRelativePos = pVCLWindow->GetPosPixel();
264 // the screen position of the "window parent" of the control
265 ::Point aVCLParentScreenPos( 0, 0 );
266 if ( pVCLParent )
267 aVCLParentScreenPos = pVCLParent->GetPosPixel();
269 // now the size of the control
270 aBounds = xWindow->getPosSize();
272 // correct the pos
273 aBounds.X = aWindowRelativePos.X() + aVCLParentScreenPos.X();
274 aBounds.Y = aWindowRelativePos.Y() + aVCLParentScreenPos.Y();
277 return aBounds;
281 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleAtPoint( const awt::Point& /* _rPoint */ )
283 // no children at all
284 return nullptr;
288 void SAL_CALL OAccessibleControlContext::grabFocus( )
290 OSL_FAIL( "OAccessibleControlContext::grabFocus: !isFocusTraversable, but grabFocus!" );
294 sal_Int32 SAL_CALL OAccessibleControlContext::getForeground( )
296 SolarMutexGuard aSolarGuard;
297 // want to do some VCL stuff here ...
298 OContextEntryGuard aGuard( this );
300 VclPtr< vcl::Window > pWindow = implGetWindow();
301 Color nColor;
302 if ( pWindow )
304 if ( pWindow->IsControlForeground() )
305 nColor = pWindow->GetControlForeground();
306 else
308 vcl::Font aFont;
309 if ( pWindow->IsControlFont() )
310 aFont = pWindow->GetControlFont();
311 else
312 aFont = pWindow->GetFont();
313 nColor = aFont.GetColor();
316 return sal_Int32(nColor);
320 sal_Int32 SAL_CALL OAccessibleControlContext::getBackground( )
322 SolarMutexGuard aSolarGuard;
323 // want to do some VCL stuff here ...
324 OContextEntryGuard aGuard( this );
326 VclPtr< vcl::Window > pWindow = implGetWindow();
327 Color nColor;
328 if ( pWindow )
330 if ( pWindow->IsControlBackground() )
331 nColor = pWindow->GetControlBackground();
332 else
333 nColor = pWindow->GetBackground().GetColor();
336 return sal_Int32(nColor);
340 } //namespace toolkit
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */