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>
30 using namespace ::com::sun::star::accessibility;
31 using namespace ::com::sun::star::awt;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::uno;
35 // Wrapper for XAccessibleText, XAccessibleEditableText and XAccessibleMultiLineText
37 @implementation AquaA11yTextWrapper : NSObject
39 +(id)valueAttributeForElement:(AquaA11yWrapper *)wrapper {
40 return CreateNSString ( [ wrapper accessibleText ] -> getText() );
43 +(void)setValueAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value
50 +(id)numberOfCharactersAttributeForElement:(AquaA11yWrapper *)wrapper {
51 return [ NSNumber numberWithLong: [ wrapper accessibleText ] -> getCharacterCount() ];
54 +(id)selectedTextAttributeForElement:(AquaA11yWrapper *)wrapper {
55 return CreateNSString ( [ wrapper accessibleText ] -> getSelectedText() );
58 +(void)setSelectedTextAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value {
59 if ( [ wrapper accessibleEditableText ] ) {
60 NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
61 OUString newText = GetOUString ( (NSString *) value );
62 NSRange selectedTextRange = [ [ AquaA11yTextWrapper selectedTextRangeAttributeForElement: wrapper ] rangeValue ];
64 [ wrapper accessibleEditableText ] -> replaceText ( selectedTextRange.location, selectedTextRange.location + selectedTextRange.length, newText );
65 } catch ( const Exception & e ) {
72 +(id)selectedTextRangeAttributeForElement:(AquaA11yWrapper *)wrapper {
73 sal_Int32 start = [ wrapper accessibleText ] -> getSelectionStart();
74 sal_Int32 end = [ wrapper accessibleText ] -> getSelectionEnd();
76 return [ NSValue valueWithRange: NSMakeRange ( start, end - start ) ]; // true selection
78 long caretPos = [ wrapper accessibleText ] -> getCaretPosition();
79 if ( caretPos < 0 || caretPos > [ wrapper accessibleText ] -> getCharacterCount() ) {
82 return [ NSValue valueWithRange: NSMakeRange ( caretPos, 0 ) ]; // insertion point
86 +(void)setSelectedTextRangeAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value {
87 NSRange range = [ value rangeValue ];
89 [ wrapper accessibleText ] -> setSelection ( range.location, range.location + range.length );
90 } catch ( const Exception & e ) {
95 +(id)visibleCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper {
96 // the OOo a11y API returns only the visible portion...
97 return [ NSValue valueWithRange: NSMakeRange ( 0, [ wrapper accessibleText ] -> getCharacterCount() ) ];
100 +(void)setVisibleCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value
107 +(id)sharedTextUIElementsAttributeForElement:(AquaA11yWrapper *)wrapper
110 return [NSArray arrayWithObject:wrapper];
113 +(id)sharedCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper
116 return [ NSValue valueWithRange: NSMakeRange ( 0, [wrapper accessibleText]->getCharacterCount() ) ];
119 +(void)addAttributeNamesTo:(NSMutableArray *)attributeNames {
120 [ attributeNames addObjectsFromArray: [ AquaA11yTextWrapper specialAttributeNames ] ];
123 +(NSArray *)specialAttributeNames {
124 return [ NSArray arrayWithObjects:
125 NSAccessibilityValueAttribute,
126 NSAccessibilityNumberOfCharactersAttribute,
127 NSAccessibilitySelectedTextAttribute,
128 NSAccessibilitySelectedTextRangeAttribute,
129 NSAccessibilityVisibleCharacterRangeAttribute,
130 NSAccessibilitySharedTextUIElementsAttribute,
131 NSAccessibilitySharedCharacterRangeAttribute,
135 +(void)addParameterizedAttributeNamesTo:(NSMutableArray *)attributeNames {
136 [ attributeNames addObjectsFromArray: [ AquaA11yTextWrapper specialParameterizedAttributeNames ] ];
139 +(NSArray *)specialParameterizedAttributeNames {
140 return [ NSArray arrayWithObjects:
141 NSAccessibilityStringForRangeParameterizedAttribute,
142 NSAccessibilityAttributedStringForRangeParameterizedAttribute,
143 NSAccessibilityRangeForIndexParameterizedAttribute,
144 NSAccessibilityRangeForPositionParameterizedAttribute,
145 NSAccessibilityBoundsForRangeParameterizedAttribute,
146 NSAccessibilityStyleRangeForIndexParameterizedAttribute,
147 NSAccessibilityRTFForRangeParameterizedAttribute,
148 NSAccessibilityLineForIndexParameterizedAttribute,
149 NSAccessibilityRangeForLineParameterizedAttribute,
153 +(id)lineForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
154 NSNumber * lineNumber = nil;
156 sal_Int32 line = [ wrapper accessibleMultiLineText ] -> getLineNumberAtIndex ( (sal_Int32) [ index intValue ] );
157 lineNumber = [ NSNumber numberWithInt: line ];
158 } catch ( IndexOutOfBoundsException & e ) {
164 +(id)rangeForLineAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)line {
165 NSValue * range = nil;
167 TextSegment textSegment = [ wrapper accessibleMultiLineText ] -> getTextAtLineNumber ( [ line intValue ] );
168 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
169 } catch ( IndexOutOfBoundsException & e ) {
175 +(id)stringForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
176 int loc = [ range rangeValue ].location;
177 int len = [ range rangeValue ].length;
178 NSMutableString * textRange = [ [ NSMutableString alloc ] init ];
180 [ textRange appendString: CreateNSString ( [ wrapper accessibleText ] -> getTextRange ( loc, loc + len ) ) ];
181 } catch ( IndexOutOfBoundsException & e ) {
187 +(id)attributedStringForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
188 return [ AquaA11yTextAttributesWrapper createAttributedStringForElement: wrapper inOrigRange: range ];
191 +(id)rangeForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
192 NSValue * range = nil;
194 TextSegment textSegment = [ wrapper accessibleText ] -> getTextBeforeIndex ( [ index intValue ], AccessibleTextType::GLYPH );
195 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
196 } catch ( IndexOutOfBoundsException & e ) {
198 } catch ( IllegalArgumentException & e ) {
204 +(id)rangeForPositionAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)point {
205 NSValue * value = nil;
206 Point aPoint( [ AquaA11yUtil nsPointToVclPoint: point ]);
207 const Point screenPos = [ wrapper accessibleComponent ] -> getLocationOnScreen();
208 aPoint.X -= screenPos.X;
209 aPoint.Y -= screenPos.Y;
210 sal_Int32 index = [ wrapper accessibleText ] -> getIndexAtPoint( aPoint );
212 value = [ AquaA11yTextWrapper rangeForIndexAttributeForElement: wrapper forParameter: [ NSNumber numberWithLong: index ] ];
217 +(id)boundsForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
218 NSValue * rect = nil;
220 // TODO: this is ugly!!!
221 // the UNP-API can only return the bounds for a single character, not for a range
222 int loc = [ range rangeValue ].location;
223 int len = [ range rangeValue ].length;
224 int minx = 0x7fffffff, miny = 0x7fffffff, maxx = 0, maxy = 0;
225 for ( int i = 0; i < len; i++ ) {
226 Rectangle vclRect = [ wrapper accessibleText ] -> getCharacterBounds ( loc + i );
227 if ( vclRect.X < minx ) {
230 if ( vclRect.Y < miny ) {
233 if ( vclRect.Width + vclRect.X > maxx ) {
234 maxx = vclRect.Width + vclRect.X;
236 if ( vclRect.Height + vclRect.Y > maxy ) {
237 maxy = vclRect.Height + vclRect.Y;
240 if ( [ wrapper accessibleComponent ] ) {
241 // get location on screen (must be added since get CharacterBounds returns values relative to parent)
242 Point screenPos = [ wrapper accessibleComponent ] -> getLocationOnScreen();
243 Point pos ( minx + screenPos.X, miny + screenPos.Y );
244 Point size ( maxx - minx, maxy - miny );
245 NSValue * nsPos = [ AquaA11yUtil vclPointToNSPoint: pos ];
246 rect = [ NSValue valueWithRect: NSMakeRect ( [ nsPos pointValue ].x, [ nsPos pointValue ].y - size.Y, size.X, size.Y ) ];
247 //printf("Range: %s --- Rect: %s\n", [ NSStringFromRange ( [ range rangeValue ] ) UTF8String ], [ NSStringFromRect ( [ rect rectValue ] ) UTF8String ]);
249 } catch ( IndexOutOfBoundsException & e ) {
255 +(id)styleRangeForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
256 NSValue * range = nil;
258 TextSegment textSegment = [ wrapper accessibleText ] -> getTextAtIndex ( [ index intValue ], AccessibleTextType::ATTRIBUTE_RUN );
259 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
260 } catch ( IndexOutOfBoundsException & e ) {
262 } catch ( IllegalArgumentException & e ) {
268 +(id)rTFForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
269 NSData * rtfData = nil;
270 NSAttributedString * attrString = (NSAttributedString *) [ AquaA11yTextWrapper attributedStringForRangeAttributeForElement: wrapper forParameter: range ];
271 if ( attrString != nil ) {
273 rtfData = [ attrString RTFFromRange: [ range rangeValue ] documentAttributes: nil ];
274 } @catch ( NSException * e) {
281 +(BOOL)isAttributeSettable:(NSString *)attribute forElement:(AquaA11yWrapper *)wrapper {
282 BOOL isSettable = NO;
283 if ( [ attribute isEqualToString: NSAccessibilityValueAttribute ]
284 || [ attribute isEqualToString: NSAccessibilitySelectedTextAttribute ]
285 || [ attribute isEqualToString: NSAccessibilitySelectedTextRangeAttribute ]
286 || [ attribute isEqualToString: NSAccessibilityVisibleCharacterRangeAttribute ] ) {
287 if ( ! [ [ wrapper accessibilityAttributeValue: NSAccessibilityRoleAttribute ] isEqualToString: NSAccessibilityStaticTextRole ] ) {
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */