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 <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
;
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
*>(
50 switch (rEvent
.GetId())
52 case VclEventId::WindowPaint
:
53 pFocusTracker
-> toolbox_open_floater( getWindow(&rEvent
) );
55 case VclEventId::WindowGetFocus
:
56 pFocusTracker
->window_got_focus( getWindow(&rEvent
) );
58 case VclEventId::ObjectDying
:
59 pFocusTracker
->m_aDocumentWindowList
.erase( getWindow(&rEvent
) );
61 case VclEventId::ToolboxHighlightOff
:
62 pFocusTracker
->toolbox_highlight_off( getWindow(&rEvent
) );
64 case VclEventId::ToolboxHighlight
:
65 pFocusTracker
->toolbox_highlight_on( getWindow(&rEvent
) );
67 case VclEventId::TabpageActivate
:
68 pFocusTracker
->tabpage_activated( getWindow(&rEvent
) );
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,
75 if( const VclMenuEvent
* pMenuEvent
= dynamic_cast < const VclMenuEvent
* > (&rEvent
) )
77 pFocusTracker
->menu_highlighted( pMenuEvent
);
85 AquaA11yFocusTracker::AquaA11yFocusTracker() :
86 m_aWindowEventLink(this, WindowEventHandler
),
87 m_xDocumentFocusListener(new DocumentFocusListener(*this))
89 Application::AddEventListener(m_aWindowEventLink
);
90 window_got_focus(Application::GetFocusWindow());
93 AquaA11yFocusTracker::~AquaA11yFocusTracker() {}
95 void AquaA11yFocusTracker::setFocusedObject(const Reference
< XAccessible
>& xAccessible
)
97 if( xAccessible
!= m_xFocusedObject
)
99 m_xFocusedObject
= xAccessible
;
101 if( m_aFocusListener
.is() )
102 m_aFocusListener
->focusedObjectChanged(xAccessible
);
106 void AquaA11yFocusTracker::notify_toolbox_item_focus(ToolBox
*pToolBox
)
108 Reference
< XAccessible
> xAccessible( pToolBox
->GetAccessible() );
110 if( xAccessible
.is() )
112 Reference
< XAccessibleContext
> xContext(xAccessible
->getAccessibleContext());
116 ToolBox::ImplToolItems::size_type nPos
= pToolBox
->GetItemPos( pToolBox
->GetHighlightItemId() );
117 if( nPos
!= ToolBox::ITEM_NOTFOUND
)
118 setFocusedObject( xContext
->getAccessibleChild( nPos
) );
119 //TODO: ToolBox::ImplToolItems::size_type -> sal_Int32!
124 void AquaA11yFocusTracker::toolbox_open_floater(vcl::Window
*pWindow
)
126 bool bToolboxFound
= false;
127 bool bFloatingWindowFound
= false;
128 vcl::Window
* pFloatingWindow
= nullptr;
129 while ( pWindow
!= nullptr ) {
130 if ( pWindow
->GetType() == WindowType::TOOLBOX
) {
131 bToolboxFound
= true;
132 } else if ( pWindow
->GetType() == WindowType::FLOATINGWINDOW
) {
133 bFloatingWindowFound
= true;
134 pFloatingWindow
= pWindow
;
136 pWindow
= pWindow
->GetParent();
138 if ( bToolboxFound
&& bFloatingWindowFound
) {
139 Reference
< XAccessible
> rxAccessible
= pFloatingWindow
-> GetAccessible();
140 if ( ! rxAccessible
.is() ) {
143 Reference
< XAccessibleContext
> rxContext
= rxAccessible
-> getAccessibleContext();
144 if ( ! rxContext
.is() ) {
147 if ( rxContext
-> getAccessibleChildCount() > 0 ) {
148 Reference
< XAccessible
> rxAccessibleChild
= rxContext
-> getAccessibleChild( 0 );
149 if ( ! rxAccessibleChild
.is() ) {
152 setFocusedObject ( rxAccessibleChild
);
157 void AquaA11yFocusTracker::toolbox_highlight_on(vcl::Window
*pWindow
)
159 // Make sure either the toolbox or its parent toolbox has the focus
160 if ( ! pWindow
->HasFocus() )
162 ToolBox
* pToolBoxParent
= dynamic_cast< ToolBox
* >( pWindow
->GetParent() );
163 if ( ! pToolBoxParent
|| ! pToolBoxParent
->HasFocus() )
167 notify_toolbox_item_focus(static_cast <ToolBox
*> (pWindow
));
170 void AquaA11yFocusTracker::toolbox_highlight_off(vcl::Window
const *pWindow
)
172 ToolBox
* pToolBoxParent
= dynamic_cast< ToolBox
* >( pWindow
->GetParent() );
174 // Notify when leaving sub toolboxes
175 if( pToolBoxParent
&& pToolBoxParent
->HasFocus() )
176 notify_toolbox_item_focus( pToolBoxParent
);
179 void AquaA11yFocusTracker::tabpage_activated(vcl::Window
*pWindow
)
181 Reference
< XAccessible
> xAccessible( pWindow
->GetAccessible() );
183 if( xAccessible
.is() )
185 Reference
< XAccessibleSelection
> xSelection(xAccessible
->getAccessibleContext(), UNO_QUERY
);
187 if( xSelection
.is() )
188 setFocusedObject( xSelection
->getSelectedAccessibleChild(0) );
192 void AquaA11yFocusTracker::menu_highlighted(const VclMenuEvent
*pEvent
)
194 Menu
* pMenu
= pEvent
->GetMenu();
198 Reference
< XAccessible
> xAccessible( pMenu
->GetAccessible() );
200 if( xAccessible
.is() )
201 setFocusedObject( xAccessible
);
205 void AquaA11yFocusTracker::window_got_focus(vcl::Window
*pWindow
)
207 // The menu bar is handled through VclEventId::MenuHighlightED
208 if( ! pWindow
|| !pWindow
->IsReallyVisible() || pWindow
->GetType() == WindowType::MENUBARWINDOW
)
211 // ToolBoxes are handled through VclEventId::ToolboxHighlight
212 if( pWindow
->GetType() == WindowType::TOOLBOX
)
215 if( pWindow
->GetType() == WindowType::TABCONTROL
)
217 tabpage_activated( pWindow
);
221 Reference
< XAccessible
> xAccessible(pWindow
->GetAccessible());
223 if( ! xAccessible
.is() )
226 Reference
< XAccessibleContext
> xContext
= xAccessible
->getAccessibleContext();
228 if( ! xContext
.is() )
231 Reference
< XAccessibleStateSet
> xStateSet
= xContext
->getAccessibleStateSet();
233 if( ! xStateSet
.is() )
236 /* the UNO ToolBox wrapper does not (yet?) support XAccessibleSelection, so we
237 * need to add listeners to the children instead of re-using the tabpage stuff
239 if( xStateSet
->contains(AccessibleStateType::FOCUSED
) && (pWindow
->GetType() != WindowType::TREELISTBOX
) )
241 setFocusedObject( xAccessible
);
245 if( m_aDocumentWindowList
.insert(pWindow
).second
)
246 m_xDocumentFocusListener
->attachRecursive(xAccessible
, xContext
, xStateSet
);
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */