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 .
21 #include "vcl/svapp.hxx"
22 #include "vcl/window.hxx"
23 #include "vcl/toolbox.hxx"
24 #include "vcl/menu.hxx"
26 #include "aqua/aqua11yfocustracker.hxx"
28 #include "documentfocuslistener.hxx"
30 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
31 #include <com/sun/star/accessibility/XAccessibleSelection.hpp>
32 #include <com/sun/star/accessibility/XAccessibleStateSet.hpp>
33 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
34 #include <com/sun/star/accessibility/AccessibleRole.hpp>
36 using namespace ::com::sun::star::accessibility
;
37 using namespace ::com::sun::star::uno
;
39 //------------------------------------------------------------------------------
41 static inline Window
*
42 getWindow(const ::VclSimpleEvent
*pEvent
)
44 return static_cast< const ::VclWindowEvent
*> (pEvent
)->GetWindow();
48 //------------------------------------------------------------------------------
50 // callback function for Application::addEventListener
52 long AquaA11yFocusTracker::WindowEventHandler(AquaA11yFocusTracker
*pFocusTracker
, ::VclSimpleEvent
const *pEvent
)
54 switch (pEvent
->GetId())
56 case VCLEVENT_WINDOW_PAINT
:
57 pFocusTracker
-> toolbox_open_floater( getWindow(pEvent
) );
59 case VCLEVENT_WINDOW_GETFOCUS
:
60 pFocusTracker
->window_got_focus( getWindow(pEvent
) );
62 case VCLEVENT_OBJECT_DYING
:
63 pFocusTracker
->m_aDocumentWindowList
.erase( getWindow(pEvent
) );
64 // intentional pass through ..
65 case VCLEVENT_TOOLBOX_HIGHLIGHTOFF
:
66 pFocusTracker
->toolbox_highlight_off( getWindow(pEvent
) );
68 case VCLEVENT_TOOLBOX_HIGHLIGHT
:
69 pFocusTracker
->toolbox_highlight_on( getWindow(pEvent
) );
71 case VCLEVENT_TABPAGE_ACTIVATE
:
72 pFocusTracker
->tabpage_activated( getWindow(pEvent
) );
74 case VCLEVENT_MENU_HIGHLIGHT
:
75 // Inspired by code in WindowEventHandler in
76 // vcl/unx/gtk/a11y/atkutil.cxx, find out what kind of event
77 // it is to avoid blindly using a static_cast and crash,
79 if( const VclMenuEvent
* pMenuEvent
= dynamic_cast < const VclMenuEvent
* > (pEvent
) )
81 pFocusTracker
->menu_highlighted( pMenuEvent
);
83 else if( const VclAccessibleEvent
* pAccEvent
= dynamic_cast < const VclAccessibleEvent
* > (pEvent
) )
85 Reference
< XAccessible
> xAccessible
= pAccEvent
->GetAccessible();
86 if( xAccessible
.is() )
87 pFocusTracker
->setFocusedObject( xAccessible
);
97 //------------------------------------------------------------------------------
99 AquaA11yFocusTracker::AquaA11yFocusTracker() :
100 m_aWindowEventLink(this, (PSTUB
) WindowEventHandler
),
101 m_xDocumentFocusListener(new DocumentFocusListener(*this))
103 Application::AddEventListener(m_aWindowEventLink
);
104 window_got_focus(Application::GetFocusWindow());
107 //------------------------------------------------------------------------------
109 void AquaA11yFocusTracker::setFocusedObject(const Reference
< XAccessible
>& xAccessible
)
111 if( xAccessible
!= m_xFocusedObject
)
113 m_xFocusedObject
= xAccessible
;
115 if( m_aFocusListener
.is() )
116 m_aFocusListener
->focusedObjectChanged(xAccessible
);
120 //------------------------------------------------------------------------------
122 void AquaA11yFocusTracker::notify_toolbox_item_focus(ToolBox
*pToolBox
)
124 Reference
< XAccessible
> xAccessible( pToolBox
->GetAccessible() );
126 if( xAccessible
.is() )
128 Reference
< XAccessibleContext
> xContext(xAccessible
->getAccessibleContext());
132 sal_Int32 nPos
= pToolBox
->GetItemPos( pToolBox
->GetHighlightItemId() );
133 if( nPos
!= TOOLBOX_ITEM_NOTFOUND
)
134 setFocusedObject( xContext
->getAccessibleChild( nPos
) );
139 //------------------------------------------------------------------------------
141 void AquaA11yFocusTracker::toolbox_open_floater(Window
*pWindow
)
143 bool bToolboxFound
= false;
144 bool bFloatingWindowFound
= false;
145 Window
* pFloatingWindow
= NULL
;
146 while ( pWindow
!= NULL
) {
147 if ( pWindow
->GetType() == WINDOW_TOOLBOX
) {
148 bToolboxFound
= true;
149 } else if ( pWindow
->GetType() == WINDOW_FLOATINGWINDOW
) {
150 bFloatingWindowFound
= true;
151 pFloatingWindow
= pWindow
;
153 pWindow
= pWindow
->GetParent();
155 if ( bToolboxFound
&& bFloatingWindowFound
) {
156 Reference
< XAccessible
> rxAccessible
= pFloatingWindow
-> GetAccessible();
157 if ( ! rxAccessible
.is() ) {
160 Reference
< XAccessibleContext
> rxContext
= rxAccessible
-> getAccessibleContext();
161 if ( ! rxContext
.is() ) {
164 if ( rxContext
-> getAccessibleChildCount() > 0 ) {
165 Reference
< XAccessible
> rxAccessibleChild
= rxContext
-> getAccessibleChild( 0 );
166 if ( ! rxAccessibleChild
.is() ) {
169 setFocusedObject ( rxAccessibleChild
);
174 //------------------------------------------------------------------------------
176 void AquaA11yFocusTracker::toolbox_highlight_on(Window
*pWindow
)
178 // Make sure either the toolbox or its parent toolbox has the focus
179 if ( ! pWindow
->HasFocus() )
181 ToolBox
* pToolBoxParent
= dynamic_cast< ToolBox
* >( pWindow
->GetParent() );
182 if ( ! pToolBoxParent
|| ! pToolBoxParent
->HasFocus() )
186 notify_toolbox_item_focus(static_cast <ToolBox
*> (pWindow
));
189 //------------------------------------------------------------------------------
191 void AquaA11yFocusTracker::toolbox_highlight_off(Window
*pWindow
)
193 ToolBox
* pToolBoxParent
= dynamic_cast< ToolBox
* >( pWindow
->GetParent() );
195 // Notify when leaving sub toolboxes
196 if( pToolBoxParent
&& pToolBoxParent
->HasFocus() )
197 notify_toolbox_item_focus( pToolBoxParent
);
200 //------------------------------------------------------------------------------
202 void AquaA11yFocusTracker::tabpage_activated(Window
*pWindow
)
204 Reference
< XAccessible
> xAccessible( pWindow
->GetAccessible() );
206 if( xAccessible
.is() )
208 Reference
< XAccessibleSelection
> xSelection(xAccessible
->getAccessibleContext(), UNO_QUERY
);
210 if( xSelection
.is() )
211 setFocusedObject( xSelection
->getSelectedAccessibleChild(0) );
215 //------------------------------------------------------------------------------
217 void AquaA11yFocusTracker::menu_highlighted(const VclMenuEvent
*pEvent
)
219 Menu
* pMenu
= pEvent
->GetMenu();
223 Reference
< XAccessible
> xAccessible( pMenu
->GetAccessible() );
225 if( xAccessible
.is() )
226 setFocusedObject( xAccessible
);
230 //------------------------------------------------------------------------------
232 void AquaA11yFocusTracker::window_got_focus(Window
*pWindow
)
234 // The menu bar is handled through VCLEVENT_MENU_HIGHLIGHTED
235 if( ! pWindow
|| !pWindow
->IsReallyVisible() || pWindow
->GetType() == WINDOW_MENUBARWINDOW
)
238 // ToolBoxes are handled through VCLEVENT_TOOLBOX_HIGHLIGHT
239 if( pWindow
->GetType() == WINDOW_TOOLBOX
)
242 if( pWindow
->GetType() == WINDOW_TABCONTROL
)
244 tabpage_activated( pWindow
);
248 Reference
< XAccessible
> xAccessible(pWindow
->GetAccessible());
250 if( ! xAccessible
.is() )
253 Reference
< XAccessibleContext
> xContext
= xAccessible
->getAccessibleContext();
255 if( ! xContext
.is() )
258 Reference
< XAccessibleStateSet
> xStateSet
= xContext
->getAccessibleStateSet();
260 if( ! xStateSet
.is() )
263 /* the UNO ToolBox wrapper does not (yet?) support XAccessibleSelection, so we
264 * need to add listeners to the children instead of re-using the tabpage stuff
266 if( xStateSet
->contains(AccessibleStateType::FOCUSED
) && (pWindow
->GetType() != WINDOW_TREELISTBOX
) )
268 setFocusedObject( xAccessible
);
272 if( m_aDocumentWindowList
.find(pWindow
) == m_aDocumentWindowList
.end() )
274 m_aDocumentWindowList
.insert(pWindow
);
275 m_xDocumentFocusListener
->attachRecursive(xAccessible
, xContext
, xStateSet
);
277 #ifdef ENABLE_TRACING
279 fprintf(stderr
, "Window %p already in the list\n", pWindow
);
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */