LibreOffice Module toolkit (master) 1
accessiblecontrolcontext.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
31#include <vcl/window.hxx>
32
33
34namespace toolkit
35{
36
37
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;
44
45
46 //= OAccessibleControlContext
47
48
50 {
51 // nothing to do here, we have a late ctor
52 }
53
54
56 {
57 ensureDisposed();
58 }
59
60
61 void OAccessibleControlContext::Init( const Reference< XAccessible >& _rxCreator )
62 {
63 OContextEntryGuard aGuard( this );
64
65 // retrieve the model of the control
66 OSL_ENSURE( !m_xControlModel.is(), "OAccessibleControlContext::Init: already know a control model...!???" );
67
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)
74
75 // start listening at the model
77
78 // announce the XAccessible to our base class
79 OAccessibleControlContext_Base::lateInit( _rxCreator );
80 }
81
82
84 {
86 try
87 {
89 pNew->Init( _rxCreator );
90 }
91 catch( const Exception& )
92 {
93 TOOLS_WARN_EXCEPTION( "toolkit", "OAccessibleControlContext::create: caught an exception from the late ctor!" );
94 }
95 return pNew;
96 }
97
98
100 {
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 );
105 }
106
107
109 {
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 );
114 }
115
116
118 {
119 // we do not have children
120 return 0;
121 }
122
123
124 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleChild( sal_Int64 )
125 {
126 // we do not have children
127 throw IndexOutOfBoundsException();
128 }
129
130
131 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleParent( )
132 {
133 return Reference< XAccessible >();
134 }
135
136
138 {
139 return AccessibleRole::SHAPE;
140 }
141
142
144 {
145 OContextEntryGuard aGuard( this );
146 return getModelStringProperty( "HelpText" );
147 }
148
149
151 {
152 OContextEntryGuard aGuard( this );
153 return getModelStringProperty( "Name" );
154 }
155
156
157 Reference< XAccessibleRelationSet > SAL_CALL OAccessibleControlContext::getAccessibleRelationSet( )
158 {
159 return nullptr;
160 }
161
162
164 {
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
167
168 sal_Int64 nStateSet = 0;
169 if ( isAlive() )
170 {
171 // no own states, only the ones which are foreign controlled
172 }
173 else
174 { // only the DEFUNC state if we're already disposed
175 nStateSet |= AccessibleStateType::DEFUNC;
176 }
177 return nStateSet;
178 }
179
180
181 void SAL_CALL OAccessibleControlContext::disposing( const EventObject& _rSource )
182 {
183 OSL_ENSURE( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ).get() == m_xControlModel.get(),
184 "OAccessibleControlContext::disposing: where did this come from?" );
185
187 m_xControlModel.clear();
188 m_xModelPropsInfo.clear();
189
190 OAccessibleControlContext_Base::disposing();
191 }
192
193
194 OUString OAccessibleControlContext::getModelStringProperty( const char* _pPropertyName )
195 {
196 OUString sReturn;
197 try
198 {
199 if ( !m_xModelPropsInfo.is() && m_xControlModel.is() )
200 m_xModelPropsInfo = m_xControlModel->getPropertySetInfo();
201
202 OUString sPropertyName( OUString::createFromAscii( _pPropertyName ) );
203 if ( m_xModelPropsInfo.is() && m_xModelPropsInfo->hasPropertyByName( sPropertyName ) )
204 m_xControlModel->getPropertyValue( sPropertyName ) >>= sReturn;
205 }
206 catch( const Exception& )
207 {
208 TOOLS_WARN_EXCEPTION( "toolkit", "OAccessibleControlContext::getModelStringProperty" );
209 }
210 return sReturn;
211 }
212
213
214 vcl::Window* OAccessibleControlContext::implGetWindow( Reference< awt::XWindow >* _pxUNOWindow ) const
215 {
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);
220
221 vcl::Window* pWindow = xWindow.is() ? VCLUnoHelper::GetWindow( xWindow ) : nullptr;
222
223 if ( _pxUNOWindow )
224 *_pxUNOWindow = xWindow;
225
226 return pWindow;
227 }
228
229
231 {
232 SolarMutexGuard aSolarGuard;
233 // want to do some VCL stuff here ...
234 OContextEntryGuard aGuard( this );
235
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
240
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
246
247 // our control
248 Reference< awt::XWindow > xWindow;
249 VclPtr< vcl::Window > pVCLWindow = implGetWindow( &xWindow );
250
251 awt::Rectangle aBounds( 0, 0, 0, 0 );
252 if ( xWindow.is() )
253 {
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;
258
259 // the relative location of the window
260 ::Point aWindowRelativePos( 0, 0);
261 if ( pVCLWindow )
262 aWindowRelativePos = pVCLWindow->GetPosPixel();
263
264 // the screen position of the "window parent" of the control
265 ::Point aVCLParentScreenPos( 0, 0 );
266 if ( pVCLParent )
267 aVCLParentScreenPos = pVCLParent->GetPosPixel();
268
269 // now the size of the control
270 aBounds = xWindow->getPosSize();
271
272 // correct the pos
273 aBounds.X = aWindowRelativePos.X() + aVCLParentScreenPos.X();
274 aBounds.Y = aWindowRelativePos.Y() + aVCLParentScreenPos.Y();
275 }
276
277 return aBounds;
278 }
279
280
281 Reference< XAccessible > SAL_CALL OAccessibleControlContext::getAccessibleAtPoint( const awt::Point& /* _rPoint */ )
282 {
283 // no children at all
284 return nullptr;
285 }
286
287
289 {
290 OSL_FAIL( "OAccessibleControlContext::grabFocus: !isFocusTraversable, but grabFocus!" );
291 }
292
293
295 {
296 SolarMutexGuard aSolarGuard;
297 // want to do some VCL stuff here ...
298 OContextEntryGuard aGuard( this );
299
301 Color nColor;
302 if ( pWindow )
303 {
304 if ( pWindow->IsControlForeground() )
305 nColor = pWindow->GetControlForeground();
306 else
307 {
308 vcl::Font aFont;
309 if ( pWindow->IsControlFont() )
310 aFont = pWindow->GetControlFont();
311 else
312 aFont = pWindow->GetFont();
313 nColor = aFont.GetColor();
314 }
315 }
316 return sal_Int32(nColor);
317 }
318
319
321 {
322 SolarMutexGuard aSolarGuard;
323 // want to do some VCL stuff here ...
324 OContextEntryGuard aGuard( this );
325
327 Color nColor;
328 if ( pWindow )
329 {
330 if ( pWindow->IsControlBackground() )
331 nColor = pWindow->GetControlBackground();
332 else
333 nColor = pWindow->GetBackground().GetColor();
334 }
335
336 return sal_Int32(nColor);
337 }
338
339
340} //namespace toolkit
341
342
343/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr tools::Long Y() const
constexpr tools::Long X() const
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
void Init(const css::uno::Reference< css::accessibility::XAccessible > &_rxCreator)
late ctor
virtual sal_Int32 SAL_CALL getForeground() override
virtual sal_Int64 SAL_CALL getAccessibleStateSet() override
virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleChild(sal_Int64 i) override
virtual sal_Int64 SAL_CALL getAccessibleChildCount() override
virtual OUString SAL_CALL getAccessibleName() override
css::uno::Reference< css::beans::XPropertySetInfo > m_xModelPropsInfo
virtual void SAL_CALL grabFocus() override
static rtl::Reference< OAccessibleControlContext > create(const css::uno::Reference< css::accessibility::XAccessible > &_rxCreator)
creates an accessible context for a uno control
virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleParent() override
virtual OUString SAL_CALL getAccessibleDescription() override
virtual css::uno::Reference< css::accessibility::XAccessibleRelationSet > SAL_CALL getAccessibleRelationSet() override
OUString getModelStringProperty(const char *_pPropertyName)
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual sal_Int32 SAL_CALL getBackground() override
virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleAtPoint(const css::awt::Point &aPoint) override
virtual css::awt::Rectangle implGetBounds() override
vcl::Window * implGetWindow(css::uno::Reference< css::awt::XWindow > *_pxUNOWindow=nullptr) const
css::uno::Reference< css::beans::XPropertySet > m_xControlModel
virtual sal_Int16 SAL_CALL getAccessibleRole() override
const Color & GetColor() const
virtual Point GetPosPixel() const
#define TOOLS_WARN_EXCEPTION(area, stream)
@ Exception
bool isAlive()