Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / osx / a11yfocustracker.cxx
blobdc42f3e30ff65831043bdfb30f9c8b7c6f0679ce
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 <vcl/svapp.hxx>
21 #include <vcl/window.hxx>
22 #include <vcl/toolbox.hxx>
23 #include <vcl/menu.hxx>
25 #include "osx/a11yfocustracker.hxx"
27 #include "documentfocuslistener.hxx"
29 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
30 #include <com/sun/star/accessibility/XAccessibleSelection.hpp>
31 #include <com/sun/star/accessibility/XAccessibleStateSet.hpp>
32 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
33 #include <com/sun/star/accessibility/AccessibleRole.hpp>
35 using namespace ::com::sun::star::accessibility;
36 using namespace ::com::sun::star::uno;
38 static inline vcl::Window *
39 getWindow(const ::VclSimpleEvent *pEvent)
41 return static_cast< const ::VclWindowEvent *> (pEvent)->GetWindow();
44 // callback function for Application::addEventListener
46 void AquaA11yFocusTracker::WindowEventHandler(void * pThis, VclSimpleEvent& rEvent)
48 AquaA11yFocusTracker *pFocusTracker = static_cast<AquaA11yFocusTracker *>(
49 pThis);
50 switch (rEvent.GetId())
52 case VclEventId::WindowPaint:
53 pFocusTracker-> toolbox_open_floater( getWindow(&rEvent) );
54 break;
55 case VclEventId::WindowGetFocus:
56 pFocusTracker->window_got_focus( getWindow(&rEvent) );
57 break;
58 case VclEventId::ObjectDying:
59 pFocusTracker->m_aDocumentWindowList.erase( getWindow(&rEvent) );
60 SAL_FALLTHROUGH;
61 case VclEventId::ToolboxHighlightOff:
62 pFocusTracker->toolbox_highlight_off( getWindow(&rEvent) );
63 break;
64 case VclEventId::ToolboxHighlight:
65 pFocusTracker->toolbox_highlight_on( getWindow(&rEvent) );
66 break;
67 case VclEventId::TabpageActivate:
68 pFocusTracker->tabpage_activated( getWindow(&rEvent) );
69 break;
70 case VclEventId::MenuHighlight:
71 // Inspired by code in WindowEventHandler in
72 // vcl/unx/gtk/a11y/atkutil.cxx, find out what kind of event
73 // it is to avoid blindly using a static_cast and crash,
74 // fdo#47275.
75 if( const VclMenuEvent* pMenuEvent = dynamic_cast < const VclMenuEvent* > (&rEvent) )
77 pFocusTracker->menu_highlighted( pMenuEvent );
79 else if( const VclAccessibleEvent* pAccEvent = dynamic_cast < const VclAccessibleEvent* > (&rEvent) )
81 Reference< XAccessible > xAccessible = pAccEvent->GetAccessible();
82 if( xAccessible.is() )
83 pFocusTracker->setFocusedObject( xAccessible );
85 break;
86 default:
87 break;
91 AquaA11yFocusTracker::AquaA11yFocusTracker() :
92 m_aWindowEventLink(this, WindowEventHandler),
93 m_xDocumentFocusListener(new DocumentFocusListener(*this))
95 Application::AddEventListener(m_aWindowEventLink);
96 window_got_focus(Application::GetFocusWindow());
99 AquaA11yFocusTracker::~AquaA11yFocusTracker() {}
101 void AquaA11yFocusTracker::setFocusedObject(const Reference< XAccessible >& xAccessible)
103 if( xAccessible != m_xFocusedObject )
105 m_xFocusedObject = xAccessible;
107 if( m_aFocusListener.is() )
108 m_aFocusListener->focusedObjectChanged(xAccessible);
112 void AquaA11yFocusTracker::notify_toolbox_item_focus(ToolBox *pToolBox)
114 Reference< XAccessible > xAccessible( pToolBox->GetAccessible() );
116 if( xAccessible.is() )
118 Reference< XAccessibleContext > xContext(xAccessible->getAccessibleContext());
120 if( xContext.is() )
122 ToolBox::ImplToolItems::size_type nPos = pToolBox->GetItemPos( pToolBox->GetHighlightItemId() );
123 if( nPos != ToolBox::ITEM_NOTFOUND )
124 setFocusedObject( xContext->getAccessibleChild( nPos ) );
125 //TODO: ToolBox::ImplToolItems::size_type -> sal_Int32!
130 void AquaA11yFocusTracker::toolbox_open_floater(vcl::Window *pWindow)
132 bool bToolboxFound = false;
133 bool bFloatingWindowFound = false;
134 vcl::Window * pFloatingWindow = nullptr;
135 while ( pWindow != nullptr ) {
136 if ( pWindow->GetType() == WindowType::TOOLBOX ) {
137 bToolboxFound = true;
138 } else if ( pWindow->GetType() == WindowType::FLOATINGWINDOW ) {
139 bFloatingWindowFound = true;
140 pFloatingWindow = pWindow;
142 pWindow = pWindow->GetParent();
144 if ( bToolboxFound && bFloatingWindowFound ) {
145 Reference < XAccessible > rxAccessible = pFloatingWindow -> GetAccessible();
146 if ( ! rxAccessible.is() ) {
147 return;
149 Reference < XAccessibleContext > rxContext = rxAccessible -> getAccessibleContext();
150 if ( ! rxContext.is() ) {
151 return;
153 if ( rxContext -> getAccessibleChildCount() > 0 ) {
154 Reference < XAccessible > rxAccessibleChild = rxContext -> getAccessibleChild( 0 );
155 if ( ! rxAccessibleChild.is() ) {
156 return;
158 setFocusedObject ( rxAccessibleChild );
163 void AquaA11yFocusTracker::toolbox_highlight_on(vcl::Window *pWindow)
165 // Make sure either the toolbox or its parent toolbox has the focus
166 if ( ! pWindow->HasFocus() )
168 ToolBox* pToolBoxParent = dynamic_cast< ToolBox * >( pWindow->GetParent() );
169 if ( ! pToolBoxParent || ! pToolBoxParent->HasFocus() )
170 return;
173 notify_toolbox_item_focus(static_cast <ToolBox *> (pWindow));
176 void AquaA11yFocusTracker::toolbox_highlight_off(vcl::Window *pWindow)
178 ToolBox* pToolBoxParent = dynamic_cast< ToolBox * >( pWindow->GetParent() );
180 // Notify when leaving sub toolboxes
181 if( pToolBoxParent && pToolBoxParent->HasFocus() )
182 notify_toolbox_item_focus( pToolBoxParent );
185 void AquaA11yFocusTracker::tabpage_activated(vcl::Window *pWindow)
187 Reference< XAccessible > xAccessible( pWindow->GetAccessible() );
189 if( xAccessible.is() )
191 Reference< XAccessibleSelection > xSelection(xAccessible->getAccessibleContext(), UNO_QUERY);
193 if( xSelection.is() )
194 setFocusedObject( xSelection->getSelectedAccessibleChild(0) );
198 void AquaA11yFocusTracker::menu_highlighted(const VclMenuEvent *pEvent)
200 Menu * pMenu = pEvent->GetMenu();
202 if( pMenu )
204 Reference< XAccessible > xAccessible( pMenu->GetAccessible() );
206 if( xAccessible.is() )
207 setFocusedObject( xAccessible );
211 void AquaA11yFocusTracker::window_got_focus(vcl::Window *pWindow)
213 // The menu bar is handled through VclEventId::MenuHighlightED
214 if( ! pWindow || !pWindow->IsReallyVisible() || pWindow->GetType() == WindowType::MENUBARWINDOW )
215 return;
217 // ToolBoxes are handled through VclEventId::ToolboxHighlight
218 if( pWindow->GetType() == WindowType::TOOLBOX )
219 return;
221 if( pWindow->GetType() == WindowType::TABCONTROL )
223 tabpage_activated( pWindow );
224 return;
227 Reference< XAccessible > xAccessible(pWindow->GetAccessible());
229 if( ! xAccessible.is() )
230 return;
232 Reference< XAccessibleContext > xContext = xAccessible->getAccessibleContext();
234 if( ! xContext.is() )
235 return;
237 Reference< XAccessibleStateSet > xStateSet = xContext->getAccessibleStateSet();
239 if( ! xStateSet.is() )
240 return;
242 /* the UNO ToolBox wrapper does not (yet?) support XAccessibleSelection, so we
243 * need to add listeners to the children instead of re-using the tabpage stuff
245 if( xStateSet->contains(AccessibleStateType::FOCUSED) && (pWindow->GetType() != WindowType::TREELISTBOX) )
247 setFocusedObject( xAccessible );
249 else
251 if( m_aDocumentWindowList.find(pWindow) == m_aDocumentWindowList.end() )
253 m_aDocumentWindowList.insert(pWindow);
254 m_xDocumentFocusListener->attachRecursive(xAccessible, xContext, xStateSet);
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */