1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <osx/salinst.h>
21 #include <osx/a11ylistener.hxx>
22 #include <osx/a11yfactory.h>
23 #include <osx/a11yfocustracker.hxx>
24 #include <osx/a11ywrapper.h>
25 
26 #include "a11ytextwrapper.h"
27 
28 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
29 #include <com/sun/star/accessibility/AccessibleRole.hpp>
30 #include <com/sun/star/accessibility/AccessibleTableModelChange.hpp>
31 #include <com/sun/star/accessibility/AccessibleTableModelChangeType.hpp>
32 
33 using namespace ::com::sun::star::accessibility;
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::uno;
37 
getTableNotification(const AccessibleEventObject & aEvent)38 static NSString * getTableNotification( const AccessibleEventObject& aEvent )
39 {
40     AccessibleTableModelChange aChange;
41     NSString * notification = nil;
42 
43     if( (aEvent.NewValue >>= aChange) &&
44         ( AccessibleTableModelChangeType::INSERT == aChange.Type || AccessibleTableModelChangeType::DELETE == aChange.Type ) &&
45         aChange.FirstRow != aChange.LastRow )
46     {
47         notification = NSAccessibilityRowCountChangedNotification;
48     }
49 
50     return notification;
51 }
52 
AquaA11yEventListener(id wrapperObject,sal_Int16 role)53 AquaA11yEventListener::AquaA11yEventListener(id wrapperObject, sal_Int16 role) : m_wrapperObject(wrapperObject), m_role(role)
54 {
55     [ m_wrapperObject retain ];
56 }
57 
~AquaA11yEventListener()58 AquaA11yEventListener::~AquaA11yEventListener()
59 {
60     [ m_wrapperObject release ];
61 }
62 
63 void SAL_CALL
disposing(const EventObject &)64 AquaA11yEventListener::disposing( const EventObject& )
65 {
66     [ AquaA11yFactory removeFromWrapperRepositoryFor: [ static_cast<AquaA11yWrapper *>(m_wrapperObject) accessibleContext ] ];
67 }
68 
69 void SAL_CALL
notifyEvent(const AccessibleEventObject & aEvent)70 AquaA11yEventListener::notifyEvent( const AccessibleEventObject& aEvent )
71 {
72     NSString * notification = nil;
73     id element = m_wrapperObject;
74     ::css::awt::Rectangle bounds;
75 
76     // TODO: NSAccessibilityValueChanged, NSAccessibilitySelectedRowsChangedNotification
77     switch( aEvent.EventId )
78     {
79         case AccessibleEventId::ACTIVE_DESCENDANT_CHANGED:
80             if( m_role != AccessibleRole::LIST ) {
81                 Reference< XAccessible > xAccessible;
82                 if( aEvent.NewValue >>= xAccessible )
83                     TheAquaA11yFocusTracker::get().setFocusedObject( xAccessible );
84             }
85             break;
86 
87         case AccessibleEventId::NAME_CHANGED:
88             notification = NSAccessibilityTitleChangedNotification;
89             break;
90 
91         case AccessibleEventId::CHILD:
92             // only needed for tooltips (says Apple)
93             if ( m_role == AccessibleRole::TOOL_TIP ) {
94                 if(aEvent.NewValue.hasValue()) {
95                     notification = NSAccessibilityCreatedNotification;
96                 } else if(aEvent.OldValue.hasValue()) {
97                     notification = NSAccessibilityUIElementDestroyedNotification;
98                 }
99             }
100             break;
101 
102         case AccessibleEventId::INVALIDATE_ALL_CHILDREN:
103             // TODO: deprecate or remember all children
104             break;
105 
106         case AccessibleEventId::BOUNDRECT_CHANGED:
107             bounds = [ element accessibleComponent ] -> getBounds();
108             if ( m_oldBounds.X != 0 && ( bounds.X != m_oldBounds.X || bounds.Y != m_oldBounds.Y ) ) {
109                 NSAccessibilityPostNotification(element, NSAccessibilityMovedNotification); // post directly since both cases can happen simultaneously
110             }
111             if ( m_oldBounds.X != 0 && ( bounds.Width != m_oldBounds.Width || bounds.Height != m_oldBounds.Height ) ) {
112                 NSAccessibilityPostNotification(element, NSAccessibilityResizedNotification); // post directly since both cases can happen simultaneously
113             }
114             m_oldBounds = bounds;
115             break;
116 
117         case AccessibleEventId::SELECTION_CHANGED:
118             notification = NSAccessibilitySelectedChildrenChangedNotification;
119             break;
120 
121         case AccessibleEventId::TEXT_SELECTION_CHANGED:
122             notification = NSAccessibilitySelectedTextChangedNotification;
123             break;
124 
125         case AccessibleEventId::TABLE_MODEL_CHANGED:
126             notification = getTableNotification(aEvent);
127             break;
128 
129         case AccessibleEventId::CARET_CHANGED:
130             notification = NSAccessibilitySelectedTextChangedNotification;
131             break;
132 
133         case AccessibleEventId::TEXT_CHANGED:
134             notification = NSAccessibilityValueChangedNotification;
135             break;
136 
137         default:
138             break;
139     }
140 
141     if( nil != notification )
142         NSAccessibilityPostNotification(element, notification);
143 }
144 
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
146