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 "osx/salinst.h"
22 #include "quartz/utils.h"
23 #include "a11ytextwrapper.h"
24 #include "a11ytextattributeswrapper.h"
27 #include <com/sun/star/accessibility/AccessibleTextType.hpp>
28 #include <com/sun/star/awt/Rectangle.hpp>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32 using namespace ::com::sun::star::accessibility;
33 using namespace ::com::sun::star::awt;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::uno;
37 // Wrapper for XAccessibleText, XAccessibleEditableText and XAccessibleMultiLineText
39 @implementation AquaA11yTextWrapper : NSObject
41 +(id)valueAttributeForElement:(AquaA11yWrapper *)wrapper {
42 return CreateNSString ( [ wrapper accessibleText ] -> getText() );
45 +(void)setValueAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value
52 +(id)numberOfCharactersAttributeForElement:(AquaA11yWrapper *)wrapper {
53 return [ NSNumber numberWithLong: [ wrapper accessibleText ] -> getCharacterCount() ];
56 +(id)selectedTextAttributeForElement:(AquaA11yWrapper *)wrapper {
57 return CreateNSString ( [ wrapper accessibleText ] -> getSelectedText() );
60 +(void)setSelectedTextAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value {
61 if ( [ wrapper accessibleEditableText ] ) {
62 NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
63 OUString newText = GetOUString ( (NSString *) value );
64 NSRange selectedTextRange = [ [ AquaA11yTextWrapper selectedTextRangeAttributeForElement: wrapper ] rangeValue ];
66 [ wrapper accessibleEditableText ] -> replaceText ( selectedTextRange.location, selectedTextRange.location + selectedTextRange.length, newText );
67 } catch ( const Exception & e ) {
74 +(id)selectedTextRangeAttributeForElement:(AquaA11yWrapper *)wrapper {
75 sal_Int32 start = [ wrapper accessibleText ] -> getSelectionStart();
76 sal_Int32 end = [ wrapper accessibleText ] -> getSelectionEnd();
78 return [ NSValue valueWithRange: NSMakeRange ( start, end - start ) ]; // true selection
80 long caretPos = [ wrapper accessibleText ] -> getCaretPosition();
81 if ( caretPos < 0 || caretPos > [ wrapper accessibleText ] -> getCharacterCount() ) {
84 return [ NSValue valueWithRange: NSMakeRange ( caretPos, 0 ) ]; // insertion point
88 +(void)setSelectedTextRangeAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value {
89 NSRange range = [ value rangeValue ];
91 [ wrapper accessibleText ] -> setSelection ( range.location, range.location + range.length );
92 } catch ( const Exception & e ) {
97 +(id)visibleCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper {
98 // the OOo a11y API returns only the visible portion...
99 return [ NSValue valueWithRange: NSMakeRange ( 0, [ wrapper accessibleText ] -> getCharacterCount() ) ];
102 +(void)setVisibleCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value
109 +(id)sharedTextUIElementsAttributeForElement:(AquaA11yWrapper *)wrapper
112 return [NSArray arrayWithObject:wrapper];
115 +(id)sharedCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper
118 return [ NSValue valueWithRange: NSMakeRange ( 0, [wrapper accessibleText]->getCharacterCount() ) ];
121 +(void)addAttributeNamesTo:(NSMutableArray *)attributeNames {
122 [ attributeNames addObjectsFromArray: [ AquaA11yTextWrapper specialAttributeNames ] ];
125 +(NSArray *)specialAttributeNames {
126 return [ NSArray arrayWithObjects:
127 NSAccessibilityValueAttribute,
128 NSAccessibilityNumberOfCharactersAttribute,
129 NSAccessibilitySelectedTextAttribute,
130 NSAccessibilitySelectedTextRangeAttribute,
131 NSAccessibilityVisibleCharacterRangeAttribute,
132 NSAccessibilitySharedTextUIElementsAttribute,
133 NSAccessibilitySharedCharacterRangeAttribute,
137 +(void)addParameterizedAttributeNamesTo:(NSMutableArray *)attributeNames {
138 [ attributeNames addObjectsFromArray: [ AquaA11yTextWrapper specialParameterizedAttributeNames ] ];
141 +(NSArray *)specialParameterizedAttributeNames {
142 return [ NSArray arrayWithObjects:
143 NSAccessibilityStringForRangeParameterizedAttribute,
144 NSAccessibilityAttributedStringForRangeParameterizedAttribute,
145 NSAccessibilityRangeForIndexParameterizedAttribute,
146 NSAccessibilityRangeForPositionParameterizedAttribute,
147 NSAccessibilityBoundsForRangeParameterizedAttribute,
148 NSAccessibilityStyleRangeForIndexParameterizedAttribute,
149 NSAccessibilityRTFForRangeParameterizedAttribute,
150 NSAccessibilityLineForIndexParameterizedAttribute,
151 NSAccessibilityRangeForLineParameterizedAttribute,
155 +(id)lineForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
156 NSNumber * lineNumber = nil;
158 sal_Int32 line = [ wrapper accessibleMultiLineText ] -> getLineNumberAtIndex ( (sal_Int32) [ index intValue ] );
159 lineNumber = [ NSNumber numberWithInt: line ];
160 } catch ( IndexOutOfBoundsException & e ) {
166 +(id)rangeForLineAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)line {
167 NSValue * range = nil;
169 TextSegment textSegment = [ wrapper accessibleMultiLineText ] -> getTextAtLineNumber ( [ line intValue ] );
170 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
171 } catch ( IndexOutOfBoundsException & e ) {
177 +(id)stringForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
178 int loc = [ range rangeValue ].location;
179 int len = [ range rangeValue ].length;
180 NSMutableString * textRange = [ [ NSMutableString alloc ] init ];
182 [ textRange appendString: CreateNSString ( [ wrapper accessibleText ] -> getTextRange ( loc, loc + len ) ) ];
183 } catch ( IndexOutOfBoundsException & e ) {
189 +(id)attributedStringForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
190 return [ AquaA11yTextAttributesWrapper createAttributedStringForElement: wrapper inOrigRange: range ];
193 +(id)rangeForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
194 NSValue * range = nil;
196 TextSegment textSegment = [ wrapper accessibleText ] -> getTextBeforeIndex ( [ index intValue ], AccessibleTextType::GLYPH );
197 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
198 } catch ( IndexOutOfBoundsException & e ) {
200 } catch ( IllegalArgumentException & e ) {
206 +(id)rangeForPositionAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)point {
207 NSValue * value = nil;
208 Point aPoint( [ AquaA11yUtil nsPointToVclPoint: point ]);
209 const Point screenPos = [ wrapper accessibleComponent ] -> getLocationOnScreen();
210 aPoint.X -= screenPos.X;
211 aPoint.Y -= screenPos.Y;
212 sal_Int32 index = [ wrapper accessibleText ] -> getIndexAtPoint( aPoint );
214 value = [ AquaA11yTextWrapper rangeForIndexAttributeForElement: wrapper forParameter: [ NSNumber numberWithLong: index ] ];
219 +(id)boundsForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
220 NSValue * rect = nil;
222 // TODO: this is ugly!!!
223 // the UNP-API can only return the bounds for a single character, not for a range
224 int loc = [ range rangeValue ].location;
225 int len = [ range rangeValue ].length;
226 int minx = 0x7fffffff, miny = 0x7fffffff, maxx = 0, maxy = 0;
227 for ( int i = 0; i < len; i++ ) {
228 Rectangle vclRect = [ wrapper accessibleText ] -> getCharacterBounds ( loc + i );
229 if ( vclRect.X < minx ) {
232 if ( vclRect.Y < miny ) {
235 if ( vclRect.Width + vclRect.X > maxx ) {
236 maxx = vclRect.Width + vclRect.X;
238 if ( vclRect.Height + vclRect.Y > maxy ) {
239 maxy = vclRect.Height + vclRect.Y;
242 if ( [ wrapper accessibleComponent ] ) {
243 // get location on screen (must be added since get CharacterBounds returns values relative to parent)
244 Point screenPos = [ wrapper accessibleComponent ] -> getLocationOnScreen();
245 Point pos ( minx + screenPos.X, miny + screenPos.Y );
246 Point size ( maxx - minx, maxy - miny );
247 NSValue * nsPos = [ AquaA11yUtil vclPointToNSPoint: pos ];
248 rect = [ NSValue valueWithRect: NSMakeRect ( [ nsPos pointValue ].x, [ nsPos pointValue ].y - size.Y, size.X, size.Y ) ];
249 //printf("Range: %s --- Rect: %s\n", [ NSStringFromRange ( [ range rangeValue ] ) UTF8String ], [ NSStringFromRect ( [ rect rectValue ] ) UTF8String ]);
251 } catch ( IndexOutOfBoundsException & e ) {
257 +(id)styleRangeForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
258 NSValue * range = nil;
260 TextSegment textSegment = [ wrapper accessibleText ] -> getTextAtIndex ( [ index intValue ], AccessibleTextType::ATTRIBUTE_RUN );
261 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
262 } catch ( IndexOutOfBoundsException & e ) {
264 } catch ( IllegalArgumentException & e ) {
270 +(id)rTFForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
271 NSData * rtfData = nil;
272 NSAttributedString * attrString = (NSAttributedString *) [ AquaA11yTextWrapper attributedStringForRangeAttributeForElement: wrapper forParameter: range ];
273 if ( attrString != nil ) {
275 rtfData = [ attrString RTFFromRange: [ range rangeValue ] documentAttributes: @{NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType} ];
276 } @catch ( NSException * e) {
283 +(BOOL)isAttributeSettable:(NSString *)attribute forElement:(AquaA11yWrapper *)wrapper {
284 BOOL isSettable = NO;
285 if ( [ attribute isEqualToString: NSAccessibilityValueAttribute ]
286 || [ attribute isEqualToString: NSAccessibilitySelectedTextAttribute ]
287 || [ attribute isEqualToString: NSAccessibilitySelectedTextRangeAttribute ]
288 || [ attribute isEqualToString: NSAccessibilityVisibleCharacterRangeAttribute ] ) {
289 if ( ! [ [ wrapper accessibilityAttributeValue: NSAccessibilityRoleAttribute ] isEqualToString: NSAccessibilityStaticTextRole ] ) {
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */