impress: don't exit textbox editing when new slide was added
[LibreOffice.git] / vcl / osx / a11yfactory.mm
blob797a7492d391ca7fbff078edcb6cb0f18b865f77
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  */
21 #include <osx/salinst.h>
22 #include <osx/a11yfactory.h>
23 #include <osx/a11yfocustracker.hxx>
25 #include "a11yfocuslistener.hxx"
26 #include "a11yrolehelper.h"
27 #include "a11ywrapperbutton.h"
28 #include "a11ywrapperstatictext.h"
29 #include "a11ywrappertextarea.h"
30 #include "a11ywrappercheckbox.h"
31 #include "a11ywrappercombobox.h"
32 #include "a11ywrappergroup.h"
33 #include "a11ywrapperlist.h"
34 #include "a11ywrapperradiobutton.h"
35 #include "a11ywrapperradiogroup.h"
36 #include "a11ywrapperrow.h"
37 #include "a11ywrapperscrollarea.h"
38 #include "a11ywrapperscrollbar.h"
39 #include "a11ywrappersplitter.h"
40 #include "a11ywrappertabgroup.h"
41 #include "a11ywrappertoolbar.h"
42 #include "a11ytablewrapper.h"
44 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
46 using namespace ::com::sun::star::accessibility;
47 using namespace ::com::sun::star::uno;
49 static bool enabled = false; 
51 @implementation AquaA11yFactory : NSObject
53 #pragma mark -
54 #pragma mark Wrapper Repository
56 +(NSMutableDictionary *)allWrapper {
57     static NSMutableDictionary * mdAllWrapper = nil;
58     if ( mdAllWrapper == nil ) {
59         mdAllWrapper = [ [ [ NSMutableDictionary alloc ] init ] retain ];
60         // initialize keyboard focus tracker
61         rtl::Reference< AquaA11yFocusListener > listener( AquaA11yFocusListener::get() );
62         TheAquaA11yFocusTracker::get().setFocusListener(listener);
63         enabled = true;      
64     }
65     return mdAllWrapper;
68 +(NSValue *)keyForAccessibleContext: (Reference < XAccessibleContext >) rxAccessibleContext {
69     return [ NSValue valueWithPointer: rxAccessibleContext.get() ];
72 +(NSValue *)keyForAccessibleContextAsRadioGroup: (Reference < XAccessibleContext >) rxAccessibleContext {
73     return [ NSValue valueWithPointer: ( rxAccessibleContext.get() + 2 ) ];
76 +(AquaA11yWrapper *)wrapperForAccessible: (Reference < XAccessible >) rxAccessible {
77     if ( rxAccessible.is() ) {
78         Reference< XAccessibleContext > xAccessibleContext = rxAccessible->getAccessibleContext();
79         if( xAccessibleContext.is() ) {
80             return [ AquaA11yFactory wrapperForAccessibleContext: xAccessibleContext ];
81         }
82     }
83     return nil;
86 +(AquaA11yWrapper *)wrapperForAccessibleContext: (Reference < XAccessibleContext >) rxAccessibleContext {
87     return [ AquaA11yFactory wrapperForAccessibleContext: rxAccessibleContext createIfNotExists: YES asRadioGroup: NO ];
90 +(AquaA11yWrapper *)wrapperForAccessibleContext: (Reference < XAccessibleContext >) rxAccessibleContext createIfNotExists:(BOOL) bCreate {
91     return [ AquaA11yFactory wrapperForAccessibleContext: rxAccessibleContext createIfNotExists: bCreate asRadioGroup: NO ];
94 +(AquaA11yWrapper *)wrapperForAccessibleContext: (Reference < XAccessibleContext >) rxAccessibleContext createIfNotExists:(BOOL) bCreate asRadioGroup:(BOOL) asRadioGroup{
95     NSMutableDictionary * dAllWrapper = [ AquaA11yFactory allWrapper ];
96     NSValue * nKey = nil;
97     if ( asRadioGroup ) {
98         nKey = [ AquaA11yFactory keyForAccessibleContextAsRadioGroup: rxAccessibleContext ];
99     } else {
100         nKey = [ AquaA11yFactory keyForAccessibleContext: rxAccessibleContext ];
101     }
102     AquaA11yWrapper * aWrapper = static_cast<AquaA11yWrapper *>([ dAllWrapper objectForKey: nKey ]);
103     if ( aWrapper != nil ) {
104         [ aWrapper retain ];
105     } else if ( bCreate ) {
106         NSString * nativeRole = [ AquaA11yRoleHelper getNativeRoleFrom: rxAccessibleContext.get() ];
107         // TODO: reflection
108         if ( [ nativeRole isEqualToString: NSAccessibilityButtonRole ] ) {
109             aWrapper = [ [ AquaA11yWrapperButton alloc ] initWithAccessibleContext: rxAccessibleContext ];
110         } else if ( [ nativeRole isEqualToString: NSAccessibilityTextAreaRole ] ) {
111             aWrapper = [ [ AquaA11yWrapperTextArea alloc ] initWithAccessibleContext: rxAccessibleContext ];
112         } else if ( [ nativeRole isEqualToString: NSAccessibilityStaticTextRole ] ) {
113             aWrapper = [ [ AquaA11yWrapperStaticText alloc ] initWithAccessibleContext: rxAccessibleContext ];
114         } else if ( [ nativeRole isEqualToString: NSAccessibilityComboBoxRole ] ) {
115             aWrapper = [ [ AquaA11yWrapperComboBox alloc ] initWithAccessibleContext: rxAccessibleContext ];
116         } else if ( [ nativeRole isEqualToString: NSAccessibilityGroupRole ] ) {
117             aWrapper = [ [ AquaA11yWrapperGroup alloc ] initWithAccessibleContext: rxAccessibleContext ];
118         } else if ( [ nativeRole isEqualToString: NSAccessibilityToolbarRole ] ) {
119             aWrapper = [ [ AquaA11yWrapperToolbar alloc ] initWithAccessibleContext: rxAccessibleContext ];
120         } else if ( [ nativeRole isEqualToString: NSAccessibilityScrollAreaRole ] ) {
121             aWrapper = [ [ AquaA11yWrapperScrollArea alloc ] initWithAccessibleContext: rxAccessibleContext ];
122         } else if ( [ nativeRole isEqualToString: NSAccessibilityTabGroupRole ] ) {
123             aWrapper = [ [ AquaA11yWrapperTabGroup alloc ] initWithAccessibleContext: rxAccessibleContext ];
124         } else if ( [ nativeRole isEqualToString: NSAccessibilityScrollBarRole ] ) {
125             aWrapper = [ [ AquaA11yWrapperScrollBar alloc ] initWithAccessibleContext: rxAccessibleContext ];
126         } else if ( [ nativeRole isEqualToString: NSAccessibilityCheckBoxRole ] ) {
127             aWrapper = [ [ AquaA11yWrapperCheckBox alloc ] initWithAccessibleContext: rxAccessibleContext ];
128         } else if ( [ nativeRole isEqualToString: NSAccessibilityRadioGroupRole ] ) {
129             aWrapper = [ [ AquaA11yWrapperRadioGroup alloc ] initWithAccessibleContext: rxAccessibleContext ];
130         } else if ( [ nativeRole isEqualToString: NSAccessibilityRadioButtonRole ] ) {
131             aWrapper = [ [ AquaA11yWrapperRadioButton alloc ] initWithAccessibleContext: rxAccessibleContext ];
132         } else if ( [ nativeRole isEqualToString: NSAccessibilityRowRole ] ) {
133             aWrapper = [ [ AquaA11yWrapperRow alloc ] initWithAccessibleContext: rxAccessibleContext ];
134         } else if ( [ nativeRole isEqualToString: NSAccessibilityListRole ] ) {
135             aWrapper = [ [ AquaA11yWrapperList alloc ] initWithAccessibleContext: rxAccessibleContext ];
136         } else if ( [ nativeRole isEqualToString: NSAccessibilitySplitterRole ] ) {
137             aWrapper = [ [ AquaA11yWrapperSplitter alloc ] initWithAccessibleContext: rxAccessibleContext ];
138         } else if ( [ nativeRole isEqualToString: NSAccessibilityTableRole ] ) {
139             aWrapper = [ [ AquaA11yTableWrapper alloc ] initWithAccessibleContext: rxAccessibleContext ];
140         } else {
141             aWrapper = [ [ AquaA11yWrapper alloc ] initWithAccessibleContext: rxAccessibleContext ];
142         }
143         [ nativeRole release ];
144         [ aWrapper setActsAsRadioGroup: asRadioGroup ];
145         #if 0
146         /* #i102033# NSAccessibility does not seemt to know an equivalent for transient children.
147            That means we need to cache this, else e.g. tree list boxes are not accessible (moreover
148            it crashes by notifying dead objects - which would seemt o be another bug)
149            
150            FIXME:
151            Unfortunately this can increase memory consumption drastically until the non transient parent
152            is destroyed and finally all the transients are released.
153         */
154         if ( ! rxAccessibleContext -> getAccessibleStateSet() -> contains ( AccessibleStateType::TRANSIENT ) )
155         #endif
156         {
157             [ dAllWrapper setObject: aWrapper forKey: nKey ];
158             /* fdo#67410: Accessibility notifications are not delivered on NSView subclasses that do not
159                "reasonably" participate in NSView hierarchy (perhaps the only important point is
160                that the view is a transitive subview of the NSWindow's content view, but I
161                did not try to verify that).
163                So let the superview-subviews relationship mirror the AXParent-AXChildren relationship.
164             */
165             id parent = [aWrapper accessibilityAttributeValue:NSAccessibilityParentAttribute];
166             if (parent) {
167                 if ([parent isKindOfClass:[NSView class]]) {
168                     // SAL_DEBUG("Wrapper INIT: " << [[aWrapper description] UTF8String] << " ==> " << [[parent description] UTF8String]);
169                     NSView *parentView = static_cast<NSView *>(parent);
170                     [parentView addSubview:aWrapper positioned:NSWindowBelow relativeTo:nil];
171                 } else if ([parent isKindOfClass:NSClassFromString(@"SalFrameWindow")]) {
172                     NSWindow *window = static_cast<NSWindow *>(parent);
173                     NSView *salView = [window contentView];
174                     // SAL_DEBUG("Wrapper INIT SAL: " << [[aWrapper description] UTF8String] << " ==> " << [[salView description] UTF8String]);
175                     [salView addSubview:aWrapper positioned:NSWindowBelow relativeTo:nil];
176                 } else {
177                     // SAL_DEBUG("Wrapper INIT: !! " << [[aWrapper description] UTF8String] << " !==>! " << [[parent description] UTF8String] << "!!");
178                 }
179             } else {
180                 // SAL_DEBUG("Wrapper INIT: " << [[aWrapper description] UTF8String] << " ==> NO PARENT");
181             }
182         }
183     }
184     return aWrapper;
187 +(void)insertIntoWrapperRepository: (NSView *) viewElement forAccessibleContext: (Reference < XAccessibleContext >) rxAccessibleContext {
188     NSMutableDictionary * dAllWrapper = [ AquaA11yFactory allWrapper ];
189     [ dAllWrapper setObject: viewElement forKey: [ AquaA11yFactory keyForAccessibleContext: rxAccessibleContext ] ];
192 +(void)removeFromWrapperRepositoryFor: (css::uno::Reference < css::accessibility::XAccessibleContext >) rxAccessibleContext {
193     // TODO: when RADIO_BUTTON search for associated RadioGroup-wrapper and delete that as well
194     AquaA11yWrapper * theWrapper = [ AquaA11yFactory wrapperForAccessibleContext: rxAccessibleContext createIfNotExists: NO ];
195     if ( theWrapper != nil ) {
196         if (![theWrapper isKindOfClass:NSClassFromString(@"SalFrameView")]) {
197             [theWrapper removeFromSuperview];
198         }
199         [ [ AquaA11yFactory allWrapper ] removeObjectForKey: [ AquaA11yFactory keyForAccessibleContext: rxAccessibleContext ] ];
200         [ theWrapper release ];
201     }
204 +(void)registerView: (NSView *) theView {
205     if ( enabled && [ theView isKindOfClass: [ AquaA11yWrapper class ] ] ) {
206         // insertIntoWrapperRepository gets called from SalFrameView itself to bootstrap the bridge initially
207         [ static_cast<AquaA11yWrapper *>(theView) accessibleContext ];
208     }
211 +(void)revokeView: (NSView *) theView {
212     if ( enabled && [ theView isKindOfClass: [ AquaA11yWrapper class ] ] ) {
213         [ AquaA11yFactory removeFromWrapperRepositoryFor: [ static_cast<AquaA11yWrapper *>(theView) accessibleContext ] ];
214     }
217 @end
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */