Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / winaccessibility / source / service / msaaservice_impl.cxx
blob15959e2b4c837cd92fa7b6bb8586bd6699bc5b90
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 <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>
35 #include <prewin.h>
36 #include <postwin.h>
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>
46 namespace my_sc_impl
49 static Sequence< OUString > getSupportedServiceNames_MSAAServiceImpl()
51 Sequence< OUString > seqNames { "com.sun.star.accessibility.MSAAService" };
52 return seqNames;
55 static OUString getImplementationName_MSAAServiceImpl()
57 return "com.sun.star.accessibility.my_sc_implementation.MSAAService";
60 class MSAAServiceImpl : public ::cppu::WeakImplHelper<
61 XMSAAService, lang::XServiceInfo >
63 private:
64 rtl::Reference<AccTopWindowListener> m_pTopWindowListener;
66 public:
67 MSAAServiceImpl ();
69 // XComponent - as used by VCL to lifecycle manage this bridge.
70 virtual void SAL_CALL dispose() override;
71 virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& ) override
72 { /* dummy */ }
73 virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& ) override
74 { /* dummy */ }
76 // XMSAAService
77 virtual sal_Int64 SAL_CALL getAccObjectPtr(
78 sal_Int64 hWnd, sal_Int64 lParam, sal_Int64 wParam) override;
79 virtual void SAL_CALL handleWindowOpened(sal_Int64) override;
81 // XServiceInfo
82 virtual OUString SAL_CALL getImplementationName() override;
83 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
84 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
87 /**
88 * Implementation of getAccObjectPtr.
89 * @param
90 * @return Com interface.
92 sal_Int64 MSAAServiceImpl::getAccObjectPtr(
93 sal_Int64 hWnd, sal_Int64 lParam, sal_Int64 wParam)
95 SolarMutexGuard g;
97 if (!m_pTopWindowListener.is())
99 return 0;
101 return m_pTopWindowListener->GetMSComPtr(hWnd, lParam, wParam);
105 * Implementation of handleWindowOpened, the method will be invoked when a
106 * top window is opened and AT starts up.
107 * @param
108 * @return
110 void MSAAServiceImpl::handleWindowOpened(sal_Int64 nAcc)
112 SolarMutexGuard g;
114 SAL_INFO( "iacc2", "Window opened " << nAcc );
116 if (m_pTopWindowListener.is() && nAcc)
118 m_pTopWindowListener->HandleWindowOpened(
119 static_cast<css::accessibility::XAccessible*>(
120 reinterpret_cast<void*>(nAcc)));
124 OUString MSAAServiceImpl::getImplementationName()
126 return getImplementationName_MSAAServiceImpl();
130 * Implementation of XServiceInfo, return support service name.
131 * @param Service name.
132 * @return If the service name is supported.
134 sal_Bool MSAAServiceImpl::supportsService( OUString const & serviceName )
136 return cppu::supportsService(this, serviceName);
140 * Implementation of XServiceInfo, return all service names.
141 * @param.
142 * @return service name sequence.
144 Sequence< OUString > MSAAServiceImpl::getSupportedServiceNames()
146 return getSupportedServiceNames_MSAAServiceImpl();
149 static void AccessBridgeHandleExistingWindow(const Reference< XMSAAService > &xAccMgr,
150 vcl::Window *pWindow, bool bShow)
152 if ( pWindow )
154 css::uno::Reference< css::accessibility::XAccessible > xAccessible;
156 SAL_INFO( "iacc2", "Decide whether to register existing window with IAccessible2" );
158 // Test for combo box - drop down floating windows first
159 vcl::Window * pParentWindow = pWindow->GetParent();
161 if ( pParentWindow )
165 // The parent window of a combo box floating window should have the role COMBO_BOX
166 css::uno::Reference< css::accessibility::XAccessible > xParentAccessible(pParentWindow->GetAccessible());
167 if ( xParentAccessible.is() )
169 css::uno::Reference< css::accessibility::XAccessibleContext > xParentAC( xParentAccessible->getAccessibleContext() );
170 if ( xParentAC.is() && (css::accessibility::AccessibleRole::COMBO_BOX == xParentAC->getAccessibleRole()) )
172 // O.k. - this is a combo box floating window corresponding to the child of role LIST of the parent.
173 // Let's not rely on a specific child order, just search for the child with the role LIST
174 sal_Int32 nCount = xParentAC->getAccessibleChildCount();
175 for ( sal_Int32 n = 0; (n < nCount) && !xAccessible.is(); n++)
177 css::uno::Reference< css::accessibility::XAccessible > xChild = xParentAC->getAccessibleChild(n);
178 if ( xChild.is() )
180 css::uno::Reference< css::accessibility::XAccessibleContext > xChildAC = xChild->getAccessibleContext();
181 if ( xChildAC.is() && (css::accessibility::AccessibleRole::LIST == xChildAC->getAccessibleRole()) )
183 xAccessible = xChild;
190 catch (css::uno::RuntimeException const&)
192 // Ignore show events that throw DisposedExceptions in getAccessibleContext(),
193 // but keep revoking these windows in hide(s).
194 if (bShow)
195 return;
199 // We have to rely on the fact that Window::GetAccessible()->getAccessibleContext() returns a valid XAccessibleContext
200 // also for other menus than menubar or toplevel popup window. Otherwise we had to traverse the hierarchy to find the
201 // context object to this menu floater. This makes the call to Window->IsMenuFloatingWindow() obsolete.
202 if ( ! xAccessible.is() )
203 xAccessible = pWindow->GetAccessible();
205 assert( xAccMgr.is() );
206 if ( xAccessible.is() )
208 xAccMgr->handleWindowOpened(
209 reinterpret_cast<sal_Int64>(xAccessible.get()));
210 SAL_INFO( "iacc2", "Decide whether to register existing window with IAccessible2" );
216 * Setup and notify the OS of Accessible peers for all existing windows.
218 static void AccessBridgeUpdateOldTopWindows( const Reference< XMSAAService > &xAccMgr )
220 sal_uInt16 nTopWindowCount = static_cast<sal_uInt16>(Application::GetTopWindowCount());
222 for ( sal_uInt16 i = 0; i < nTopWindowCount; i++ )
224 vcl::Window* pTopWindow = Application::GetTopWindow( i );
225 css::uno::Reference< css::accessibility::XAccessible > xAccessible = pTopWindow->GetAccessible();
226 if ( xAccessible.is() )
228 css::uno::Reference< css::accessibility::XAccessibleContext > xAC( xAccessible->getAccessibleContext() );
229 if ( xAC.is())
231 if ( !xAC->getAccessibleName().isEmpty() )
232 AccessBridgeHandleExistingWindow( xAccMgr, pTopWindow, true );
239 * Static method that can create an entity of our MSAA Service
240 * @param xContext No use here.
241 * @return The object interface.
243 static Reference< XInterface > create_MSAAServiceImpl( Reference< XComponentContext > const & /*xContext*/ )
245 Reference< XMSAAService > xAccMgr( new MSAAServiceImpl() );
247 AccessBridgeUpdateOldTopWindows( xAccMgr );
249 SAL_INFO("iacc2", "Created new IAccessible2 service impl.");
251 return xAccMgr;
254 MSAAServiceImpl::MSAAServiceImpl()
256 Reference< XExtendedToolkit > xToolkit(Application::GetVCLToolkit(), UNO_QUERY);
258 if( xToolkit.is() )
260 m_pTopWindowListener.set(new AccTopWindowListener());
261 Reference<XTopWindowListener> const xRef(m_pTopWindowListener.get());
262 xToolkit->addTopWindowListener( xRef );
263 SAL_INFO( "iacc2", "successfully connected to the toolkit event hose" );
265 else
266 SAL_WARN( "iacc2", "No VCL toolkit interface to listen to for events");
269 void MSAAServiceImpl::dispose()
271 SolarMutexGuard g;
273 // As all folders and streams contain references to their parents,
274 // we must remove these references so that they will be deleted when
275 // the hash_map of the root folder is cleared, releasing all subfolders
276 // and substreams which in turn release theirs, etc. When xRootFolder is
277 // released when this destructor completes, the folder tree should be
278 // deleted fully (and automagically).
279 m_pTopWindowListener.clear();
284 /* shared lib exports implemented without helpers in service_impl1.cxx */
285 namespace my_sc_impl
287 static struct ::cppu::ImplementationEntry s_component_entries [] =
290 create_MSAAServiceImpl, getImplementationName_MSAAServiceImpl,
291 getSupportedServiceNames_MSAAServiceImpl,
292 ::cppu::createSingleComponentFactory,
293 nullptr, 0
295 { nullptr, nullptr, nullptr, nullptr, nullptr, 0 }
299 extern "C"
301 SAL_DLLPUBLIC_EXPORT void * iacc2_component_getFactory(
302 sal_Char const * implName, lang::XMultiServiceFactory * xMgr,
303 registry::XRegistryKey * xRegistry )
305 return ::cppu::component_getFactoryHelper(
306 implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries );
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */