LibreOffice Module winaccessibility (master) 1
msaaservice_impl.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
20#include <rtl/ref.hxx>
21#include <sal/log.hxx>
26
27#include <com/sun/star/lang/XServiceInfo.hpp>
28#include <com/sun/star/accessibility/XMSAAService.hpp>
29#include <com/sun/star/accessibility/AccessibleRole.hpp>
30
31#include <com/sun/star/awt/XExtendedToolkit.hpp>
32#include <vcl/svapp.hxx>
33#include <vcl/window.hxx>
34
35#include <prewin.h>
36#include <postwin.h>
37
38using namespace ::com::sun::star; // for odk interfaces
39using namespace ::com::sun::star::uno; // for basic types
40using namespace ::com::sun::star::accessibility;
41
42using namespace ::com::sun::star::awt;
43
45
46namespace my_sc_impl
47{
48
49namespace {
50
51class MSAAServiceImpl : public ::cppu::WeakImplHelper<
52 XMSAAService, lang::XServiceInfo >
53{
54private:
56
57public:
58 MSAAServiceImpl ();
59
60 // XComponent - as used by VCL to lifecycle manage this bridge.
61 virtual void SAL_CALL dispose() override;
62 virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& ) override
63 { /* dummy */ }
64 virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& ) override
65 { /* dummy */ }
66
67 // XMSAAService
68 virtual sal_Int64 SAL_CALL getAccObjectPtr(
69 sal_Int64 hWnd, sal_Int64 lParam, sal_Int64 wParam) override;
70 virtual void SAL_CALL handleWindowOpened(sal_Int64) override;
71
72 // XServiceInfo
73 virtual OUString SAL_CALL getImplementationName() override;
74 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
75 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
76};
77
78}
79
85sal_Int64 MSAAServiceImpl::getAccObjectPtr(
86 sal_Int64 hWnd, sal_Int64 lParam, sal_Int64 wParam)
87{
88 // tdf#155794: this must complete without taking SolarMutex
89
90 if (!m_pTopWindowListener.is())
91 {
92 return 0;
93 }
94 return m_pTopWindowListener->GetMSComPtr(hWnd, lParam, wParam);
95}
96
103void MSAAServiceImpl::handleWindowOpened(sal_Int64 nAcc)
104{
106
107 SAL_INFO( "iacc2", "Window opened " << nAcc );
108
109 if (m_pTopWindowListener.is() && nAcc)
110 {
111 m_pTopWindowListener->HandleWindowOpened(
112 static_cast<css::accessibility::XAccessible*>(
113 reinterpret_cast<void*>(nAcc)));
114 }
115}
116
117OUString MSAAServiceImpl::getImplementationName()
118{
119 return "com.sun.star.accessibility.my_sc_implementation.MSAAService";
120}
121
127sal_Bool MSAAServiceImpl::supportsService( OUString const & serviceName )
128{
129 return cppu::supportsService(this, serviceName);
130}
131
137Sequence< OUString > MSAAServiceImpl::getSupportedServiceNames()
138{
139 return { "com.sun.star.accessibility.MSAAService" };
140}
141
142static void AccessBridgeHandleExistingWindow(const Reference< XMSAAService > &xAccMgr,
143 vcl::Window *pWindow, bool bShow)
144{
145 if ( pWindow )
146 {
147 css::uno::Reference< css::accessibility::XAccessible > xAccessible;
148
149 SAL_INFO( "iacc2", "Decide whether to register existing window with IAccessible2" );
150
151 // Test for combo box - drop down floating windows first
152 vcl::Window * pParentWindow = pWindow->GetParent();
153
154 if ( pParentWindow )
155 {
156 try
157 {
158 // The parent window of a combo box floating window should have the role COMBO_BOX
159 css::uno::Reference< css::accessibility::XAccessible > xParentAccessible(pParentWindow->GetAccessible());
160 if ( xParentAccessible.is() )
161 {
162 css::uno::Reference< css::accessibility::XAccessibleContext > xParentAC( xParentAccessible->getAccessibleContext() );
163 if ( xParentAC.is() && (css::accessibility::AccessibleRole::COMBO_BOX == xParentAC->getAccessibleRole()) )
164 {
165 // O.k. - this is a combo box floating window corresponding to the child of role LIST of the parent.
166 // Let's not rely on a specific child order, just search for the child with the role LIST
167 sal_Int64 nCount = xParentAC->getAccessibleChildCount();
168 for (sal_Int64 n = 0; (n < nCount) && !xAccessible.is(); n++)
169 {
170 css::uno::Reference< css::accessibility::XAccessible > xChild = xParentAC->getAccessibleChild(n);
171 if ( xChild.is() )
172 {
173 css::uno::Reference< css::accessibility::XAccessibleContext > xChildAC = xChild->getAccessibleContext();
174 if ( xChildAC.is() && (css::accessibility::AccessibleRole::LIST == xChildAC->getAccessibleRole()) )
175 {
176 xAccessible = xChild;
177 }
178 }
179 }
180 }
181 }
182 }
183 catch (css::uno::RuntimeException const&)
184 {
185 // Ignore show events that throw DisposedExceptions in getAccessibleContext(),
186 // but keep revoking these windows in hide(s).
187 if (bShow)
188 return;
189 }
190 }
191
192 // We have to rely on the fact that Window::GetAccessible()->getAccessibleContext() returns a valid XAccessibleContext
193 // also for other menus than menubar or toplevel popup window. Otherwise we had to traverse the hierarchy to find the
194 // context object to this menu floater. This makes the call to Window->IsMenuFloatingWindow() obsolete.
195 if ( ! xAccessible.is() )
196 xAccessible = pWindow->GetAccessible();
197
198 assert( xAccMgr.is() );
199 if ( xAccessible.is() )
200 {
201 xAccMgr->handleWindowOpened(
202 reinterpret_cast<sal_Int64>(xAccessible.get()));
203 SAL_INFO( "iacc2", "Decide whether to register existing window with IAccessible2" );
204 }
205 }
206}
207
208/*
209 * Setup and notify the OS of Accessible peers for all existing windows.
210 */
211static void AccessBridgeUpdateOldTopWindows( const Reference< XMSAAService > &xAccMgr )
212{
213 sal_uInt16 nTopWindowCount = static_cast<sal_uInt16>(Application::GetTopWindowCount());
214
215 for ( sal_uInt16 i = 0; i < nTopWindowCount; i++ )
216 {
217 vcl::Window* pTopWindow = Application::GetTopWindow( i );
218 css::uno::Reference< css::accessibility::XAccessible > xAccessible = pTopWindow->GetAccessible();
219 if ( xAccessible.is() )
220 {
221 css::uno::Reference< css::accessibility::XAccessibleContext > xAC( xAccessible->getAccessibleContext() );
222 if ( xAC.is())
223 {
224 if ( !xAC->getAccessibleName().isEmpty() )
225 AccessBridgeHandleExistingWindow( xAccMgr, pTopWindow, true );
226 }
227 }
228 }
229}
230
231MSAAServiceImpl::MSAAServiceImpl()
232{
233 Reference< XExtendedToolkit > xToolkit(Application::GetVCLToolkit(), UNO_QUERY);
234
235 if( xToolkit.is() )
236 {
238 Reference<XTopWindowListener> const xRef(m_pTopWindowListener);
239 xToolkit->addTopWindowListener( xRef );
240 SAL_INFO( "iacc2", "successfully connected to the toolkit event hose" );
241 }
242 else
243 SAL_WARN( "iacc2", "No VCL toolkit interface to listen to for events");
244}
245
246void MSAAServiceImpl::dispose()
247{
249
250 // As all folders and streams contain references to their parents,
251 // we must remove these references so that they will be deleted when
252 // the hash_map of the root folder is cleared, releasing all subfolders
253 // and substreams which in turn release theirs, etc. When xRootFolder is
254 // released when this destructor completes, the folder tree should be
255 // deleted fully (and automagically).
256 m_pTopWindowListener.clear();
257}
258
259extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
261 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
262{
263 Reference< XMSAAService > xAccMgr( new MSAAServiceImpl() );
264
266
267 SAL_INFO("iacc2", "Created new IAccessible2 service impl.");
268
269 xAccMgr->acquire();
270 return xAccMgr.get();
271}
272
273}
274
275/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AccEventListener is the general event listener for all top windows.
static tools::Long GetTopWindowCount()
static vcl::Window * GetTopWindow(tools::Long nIndex)
static css::uno::Reference< css::awt::XToolkit > GetVCLToolkit()
vcl::Window * GetParent() const
css::uno::Reference< css::accessibility::XAccessible > GetAccessible(bool bCreate=true)
int nCount
sal_Int64 n
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
rtl::Reference< AccTopWindowListener > m_pTopWindowListener
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * winaccessibility_MSAAServiceImpl_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
static void AccessBridgeHandleExistingWindow(const Reference< XMSAAService > &xAccMgr, vcl::Window *pWindow, bool bShow)
static void AccessBridgeUpdateOldTopWindows(const Reference< XMSAAService > &xAccMgr)
void dispose()
unsigned char sal_Bool