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 // Related tdf#158914: explicitly call autorelease selector
43 // CreateNSString() is not a getter. It expects the caller to
44 // release the returned string.
45 return [ CreateNSString ( [ wrapper accessibleText ] -> getText() ) autorelease ];
48 +(void)setValueAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value
55 +(id)numberOfCharactersAttributeForElement:(AquaA11yWrapper *)wrapper {
56 return [ NSNumber numberWithLong: [ wrapper accessibleText ] -> getCharacterCount() ];
59 +(id)selectedTextAttributeForElement:(AquaA11yWrapper *)wrapper {
60 // Related tdf#158914: explicitly call autorelease selector
61 // CreateNSString() is not a getter. It expects the caller to
62 // release the returned string.
63 return [ CreateNSString ( [ wrapper accessibleText ] -> getSelectedText() ) autorelease ];
66 +(void)setSelectedTextAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value {
67 if ( [ wrapper accessibleEditableText ] ) {
68 NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
69 OUString newText = GetOUString ( static_cast<NSString *>(value) );
70 NSRange selectedTextRange = [ [ AquaA11yTextWrapper selectedTextRangeAttributeForElement: wrapper ] rangeValue ];
72 [ wrapper accessibleEditableText ] -> replaceText ( selectedTextRange.location, selectedTextRange.location + selectedTextRange.length, newText );
73 } catch ( const Exception & ) {
80 +(id)selectedTextRangeAttributeForElement:(AquaA11yWrapper *)wrapper {
81 sal_Int32 start = [ wrapper accessibleText ] -> getSelectionStart();
82 sal_Int32 end = [ wrapper accessibleText ] -> getSelectionEnd();
84 return [ NSValue valueWithRange: NSMakeRange ( start, end - start ) ]; // true selection
86 sal_Int32 caretPos = [ wrapper accessibleText ] -> getCaretPosition();
87 if ( caretPos < 0 || caretPos > [ wrapper accessibleText ] -> getCharacterCount() ) {
90 return [ NSValue valueWithRange: NSMakeRange ( caretPos, 0 ) ]; // insertion point
94 +(void)setSelectedTextRangeAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value {
95 NSRange range = [ value rangeValue ];
97 [ wrapper accessibleText ] -> setSelection ( range.location, range.location + range.length );
98 } catch ( const Exception & ) {
103 +(id)visibleCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper {
104 // the OOo a11y API returns only the visible portion...
105 return [ NSValue valueWithRange: NSMakeRange ( 0, [ wrapper accessibleText ] -> getCharacterCount() ) ];
108 +(void)setVisibleCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper to:(id)value
115 +(id)sharedTextUIElementsAttributeForElement:(AquaA11yWrapper *)wrapper
117 return [NSArray arrayWithObject:wrapper];
120 +(id)sharedCharacterRangeAttributeForElement:(AquaA11yWrapper *)wrapper
122 return [ NSValue valueWithRange: NSMakeRange ( 0, [wrapper accessibleText]->getCharacterCount() ) ];
125 +(void)addAttributeNamesTo:(NSMutableArray *)attributeNames {
126 [ attributeNames addObjectsFromArray: [ AquaA11yTextWrapper specialAttributeNames ] ];
129 +(NSArray *)specialAttributeNames {
130 return [ NSArray arrayWithObjects:
131 NSAccessibilityValueAttribute,
132 NSAccessibilityNumberOfCharactersAttribute,
133 NSAccessibilitySelectedTextAttribute,
134 NSAccessibilitySelectedTextRangeAttribute,
135 NSAccessibilityVisibleCharacterRangeAttribute,
136 NSAccessibilitySharedTextUIElementsAttribute,
137 NSAccessibilitySharedCharacterRangeAttribute,
141 +(void)addParameterizedAttributeNamesTo:(NSMutableArray *)attributeNames {
142 [ attributeNames addObjectsFromArray: [ AquaA11yTextWrapper specialParameterizedAttributeNames ] ];
145 +(NSArray *)specialParameterizedAttributeNames {
146 return [ NSArray arrayWithObjects:
147 NSAccessibilityStringForRangeParameterizedAttribute,
148 NSAccessibilityAttributedStringForRangeParameterizedAttribute,
149 NSAccessibilityRangeForIndexParameterizedAttribute,
150 NSAccessibilityRangeForPositionParameterizedAttribute,
151 NSAccessibilityBoundsForRangeParameterizedAttribute,
152 NSAccessibilityStyleRangeForIndexParameterizedAttribute,
153 NSAccessibilityRTFForRangeParameterizedAttribute,
154 NSAccessibilityLineForIndexParameterizedAttribute,
155 NSAccessibilityRangeForLineParameterizedAttribute,
159 +(id)lineForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
160 NSNumber * lineNumber = nil;
162 sal_Int32 line = [ wrapper accessibleMultiLineText ] -> getLineNumberAtIndex ( static_cast<sal_Int32>([ index intValue ]) );
163 lineNumber = [ NSNumber numberWithInt: line ];
164 } catch ( IndexOutOfBoundsException & ) {
170 +(id)rangeForLineAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)line {
171 NSValue * range = nil;
173 TextSegment textSegment = [ wrapper accessibleMultiLineText ] -> getTextAtLineNumber ( [ line intValue ] );
174 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
175 } catch ( IndexOutOfBoundsException & ) {
181 +(id)stringForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
182 int loc = [ range rangeValue ].location;
183 int len = [ range rangeValue ].length;
184 NSMutableString * textRange = [ NSMutableString string ];
186 // Related tdf#158914: explicitly call autorelease selector
187 // CreateNSString() is not a getter. It expects the caller to
188 // release the returned string.
189 [ textRange appendString: [ CreateNSString ( [ wrapper accessibleText ] -> getTextRange ( loc, loc + len ) ) autorelease ] ];
190 } catch ( IndexOutOfBoundsException & ) {
196 +(id)attributedStringForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
197 return [ AquaA11yTextAttributesWrapper createAttributedStringForElement: wrapper inOrigRange: range ];
200 +(id)rangeForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
201 NSValue * range = nil;
203 TextSegment textSegment = [ wrapper accessibleText ] -> getTextBeforeIndex ( [ index intValue ], AccessibleTextType::GLYPH );
204 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
205 } catch ( IndexOutOfBoundsException & ) {
207 } catch ( IllegalArgumentException & ) {
213 +(id)rangeForPositionAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)point {
214 NSValue * value = nil;
215 css::awt::Point aPoint( [ AquaA11yUtil nsPointToVclPoint: point ]);
216 const css::awt::Point screenPos = [ wrapper accessibleComponent ] -> getLocationOnScreen();
217 aPoint.X -= screenPos.X;
218 aPoint.Y -= screenPos.Y;
219 sal_Int32 index = [ wrapper accessibleText ] -> getIndexAtPoint( aPoint );
221 value = [ AquaA11yTextWrapper rangeForIndexAttributeForElement: wrapper forParameter: [ NSNumber numberWithLong: index ] ];
226 +(id)boundsForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
227 NSValue * rect = nil;
229 // TODO: this is ugly!!!
230 // the UNP-API can only return the bounds for a single character, not for a range
231 int loc = [ range rangeValue ].location;
232 int len = [ range rangeValue ].length;
233 int minx = 0x7fffffff, miny = 0x7fffffff, maxx = 0, maxy = 0;
234 for ( int i = 0; i < len; i++ ) {
235 Rectangle vclRect = [ wrapper accessibleText ] -> getCharacterBounds ( loc + i );
236 if ( vclRect.X < minx ) {
239 if ( vclRect.Y < miny ) {
242 if ( vclRect.Width + vclRect.X > maxx ) {
243 maxx = vclRect.Width + vclRect.X;
245 if ( vclRect.Height + vclRect.Y > maxy ) {
246 maxy = vclRect.Height + vclRect.Y;
249 if ( [ wrapper accessibleComponent ] ) {
250 // get location on screen (must be added since get CharacterBounds returns values relative to parent)
251 css::awt::Point screenPos = [ wrapper accessibleComponent ] -> getLocationOnScreen();
252 css::awt::Point pos ( minx + screenPos.X, miny + screenPos.Y );
253 css::awt::Point size ( maxx - minx, maxy - miny );
254 NSValue * nsPos = [ AquaA11yUtil vclPointToNSPoint: pos ];
255 rect = [ NSValue valueWithRect: NSMakeRect ( [ nsPos pointValue ].x, [ nsPos pointValue ].y - size.Y, size.X, size.Y ) ];
256 //printf("Range: %s --- Rect: %s\n", [ NSStringFromRange ( [ range rangeValue ] ) UTF8String ], [ NSStringFromRect ( [ rect rectValue ] ) UTF8String ]);
258 } catch ( IndexOutOfBoundsException & ) {
264 +(id)styleRangeForIndexAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)index {
265 NSValue * range = nil;
267 TextSegment textSegment = [ wrapper accessibleText ] -> getTextAtIndex ( [ index intValue ], AccessibleTextType::ATTRIBUTE_RUN );
268 range = [ NSValue valueWithRange: NSMakeRange ( textSegment.SegmentStart, textSegment.SegmentEnd - textSegment.SegmentStart ) ];
269 } catch ( IndexOutOfBoundsException & ) {
271 } catch ( IllegalArgumentException & ) {
277 +(id)rTFForRangeAttributeForElement:(AquaA11yWrapper *)wrapper forParameter:(id)range {
278 NSData * rtfData = nil;
279 NSAttributedString * attrString = static_cast<NSAttributedString *>([ AquaA11yTextWrapper attributedStringForRangeAttributeForElement: wrapper forParameter: range ]);
280 if ( attrString != nil ) {
282 rtfData = [ attrString RTFFromRange: [ range rangeValue ] documentAttributes: @{NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType} ];
283 } @catch ( NSException *) {
290 +(BOOL)isAttributeSettable:(NSString *)attribute forElement:(AquaA11yWrapper *)wrapper {
291 bool isSettable = false;
292 if ( [ attribute isEqualToString: NSAccessibilityValueAttribute ]
293 || [ attribute isEqualToString: NSAccessibilitySelectedTextAttribute ]
294 || [ attribute isEqualToString: NSAccessibilitySelectedTextRangeAttribute ]
295 || [ attribute isEqualToString: NSAccessibilityVisibleCharacterRangeAttribute ] ) {
296 if ( ! [ [ wrapper accessibilityAttributeValue: NSAccessibilityRoleAttribute ] isEqualToString: NSAccessibilityStaticTextRole ] ) {
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */