1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <rtl/ref.hxx>
21 #include <sal/log.hxx>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppuhelper/factory.hxx>
24 #include <cppuhelper/implementationentry.hxx>
25 #include <cppuhelper/supportsservice.hxx>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/accessibility/XMSAAService.hpp>
29 #include <com/sun/star/accessibility/AccessibleRole.hpp>
31 #include <com/sun/star/awt/XExtendedToolkit.hpp>
32 #include <vcl/svapp.hxx>
33 #include <vcl/window.hxx>
38 using namespace ::com::sun::star
; // for odk interfaces
39 using namespace ::com::sun::star::uno
; // for basic types
40 using namespace ::com::sun::star::accessibility
;
42 using namespace ::com::sun::star::awt
;
44 #include <AccTopWindowListener.hxx>
51 class MSAAServiceImpl
: public ::cppu::WeakImplHelper
<
52 XMSAAService
, lang::XServiceInfo
>
55 rtl::Reference
<AccTopWindowListener
> m_pTopWindowListener
;
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
64 virtual void SAL_CALL
removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& ) override
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
;
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
;
81 * Implementation of getAccObjectPtr.
83 * @return Com interface.
85 sal_Int64
MSAAServiceImpl::getAccObjectPtr(
86 sal_Int64 hWnd
, sal_Int64 lParam
, sal_Int64 wParam
)
88 // tdf#155794: this must complete without taking SolarMutex
90 if (!m_pTopWindowListener
.is())
94 return m_pTopWindowListener
->GetMSComPtr(hWnd
, lParam
, wParam
);
98 * Implementation of handleWindowOpened, the method will be invoked when a
99 * top window is opened and AT starts up.
103 void MSAAServiceImpl::handleWindowOpened(sal_Int64 nAcc
)
107 SAL_INFO( "iacc2", "Window opened " << nAcc
);
109 if (m_pTopWindowListener
.is() && nAcc
)
111 m_pTopWindowListener
->HandleWindowOpened(
112 static_cast<css::accessibility::XAccessible
*>(
113 reinterpret_cast<void*>(nAcc
)));
117 OUString
MSAAServiceImpl::getImplementationName()
119 return "com.sun.star.accessibility.my_sc_implementation.MSAAService";
123 * Implementation of XServiceInfo, return support service name.
124 * @param Service name.
125 * @return If the service name is supported.
127 sal_Bool
MSAAServiceImpl::supportsService( OUString
const & serviceName
)
129 return cppu::supportsService(this, serviceName
);
133 * Implementation of XServiceInfo, return all service names.
135 * @return service name sequence.
137 Sequence
< OUString
> MSAAServiceImpl::getSupportedServiceNames()
139 return { "com.sun.star.accessibility.MSAAService" };
142 static void AccessBridgeHandleExistingWindow(const Reference
< XMSAAService
> &xAccMgr
,
143 vcl::Window
*pWindow
, bool bShow
)
147 css::uno::Reference
< css::accessibility::XAccessible
> xAccessible
;
149 SAL_INFO( "iacc2", "Decide whether to register existing window with IAccessible2" );
151 // Test for combo box - drop down floating windows first
152 vcl::Window
* pParentWindow
= pWindow
->GetParent();
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() )
162 css::uno::Reference
< css::accessibility::XAccessibleContext
> xParentAC( xParentAccessible
->getAccessibleContext() );
163 if ( xParentAC
.is() && (css::accessibility::AccessibleRole::COMBO_BOX
== xParentAC
->getAccessibleRole()) )
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
++)
170 css::uno::Reference
< css::accessibility::XAccessible
> xChild
= xParentAC
->getAccessibleChild(n
);
173 css::uno::Reference
< css::accessibility::XAccessibleContext
> xChildAC
= xChild
->getAccessibleContext();
174 if ( xChildAC
.is() && (css::accessibility::AccessibleRole::LIST
== xChildAC
->getAccessibleRole()) )
176 xAccessible
= xChild
;
183 catch (css::uno::RuntimeException
const&)
185 // Ignore show events that throw DisposedExceptions in getAccessibleContext(),
186 // but keep revoking these windows in hide(s).
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();
198 assert( xAccMgr
.is() );
199 if ( xAccessible
.is() )
201 xAccMgr
->handleWindowOpened(
202 reinterpret_cast<sal_Int64
>(xAccessible
.get()));
203 SAL_INFO( "iacc2", "Decide whether to register existing window with IAccessible2" );
209 * Setup and notify the OS of Accessible peers for all existing windows.
211 static void AccessBridgeUpdateOldTopWindows( const Reference
< XMSAAService
> &xAccMgr
)
213 sal_uInt16 nTopWindowCount
= static_cast<sal_uInt16
>(Application::GetTopWindowCount());
215 for ( sal_uInt16 i
= 0; i
< nTopWindowCount
; i
++ )
217 vcl::Window
* pTopWindow
= Application::GetTopWindow( i
);
218 css::uno::Reference
< css::accessibility::XAccessible
> xAccessible
= pTopWindow
->GetAccessible();
219 if ( xAccessible
.is() )
221 css::uno::Reference
< css::accessibility::XAccessibleContext
> xAC( xAccessible
->getAccessibleContext() );
224 if ( !xAC
->getAccessibleName().isEmpty() )
225 AccessBridgeHandleExistingWindow( xAccMgr
, pTopWindow
, true );
231 MSAAServiceImpl::MSAAServiceImpl()
233 Reference
< XExtendedToolkit
> xToolkit(Application::GetVCLToolkit(), UNO_QUERY
);
237 m_pTopWindowListener
.set(new AccTopWindowListener());
238 Reference
<XTopWindowListener
> const xRef(m_pTopWindowListener
);
239 xToolkit
->addTopWindowListener( xRef
);
240 SAL_INFO( "iacc2", "successfully connected to the toolkit event hose" );
243 SAL_WARN( "iacc2", "No VCL toolkit interface to listen to for events");
246 void MSAAServiceImpl::dispose()
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();
259 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
260 winaccessibility_MSAAServiceImpl_get_implementation(
261 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
263 Reference
< XMSAAService
> xAccMgr( new MSAAServiceImpl() );
265 AccessBridgeUpdateOldTopWindows( xAccMgr
);
267 SAL_INFO("iacc2", "Created new IAccessible2 service impl.");
270 return xAccMgr
.get();
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */