| 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 "atkwrapper.hxx" |
| 21 | #include "atktextattributes.hxx" |
| 22 | #include <algorithm> |
| 23 | |
| 24 | #include <osl/diagnose.h> |
| 25 | #include <rtl/character.hxx> |
| 26 | |
| 27 | #include <com/sun/star/accessibility/AccessibleScrollType.hpp> |
| 28 | #include <com/sun/star/accessibility/AccessibleTextType.hpp> |
| 29 | #include <com/sun/star/accessibility/TextSegment.hpp> |
| 30 | #include <com/sun/star/accessibility/XAccessibleMultiLineText.hpp> |
| 31 | #include <com/sun/star/accessibility/XAccessibleText.hpp> |
| 32 | #include <com/sun/star/accessibility/XAccessibleTextAttributes.hpp> |
| 33 | #include <com/sun/star/accessibility/XAccessibleTextMarkup.hpp> |
| 34 | #include <com/sun/star/lang/NoSupportException.hpp> |
| 35 | #include <com/sun/star/text/TextMarkupType.hpp> |
| 36 | |
| 37 | using namespace ::com::sun::star; |
| 38 | |
| 39 | static sal_Int16 |
| 40 | text_type_from_boundary(AtkTextBoundary boundary_type) |
| 41 | { |
| 42 | switch(boundary_type) |
| 43 | { |
| 44 | case ATK_TEXT_BOUNDARY_CHAR: |
| 45 | return accessibility::AccessibleTextType::CHARACTER; |
| 46 | case ATK_TEXT_BOUNDARY_WORD_START: |
| 47 | case ATK_TEXT_BOUNDARY_WORD_END: |
| 48 | return accessibility::AccessibleTextType::WORD; |
| 49 | case ATK_TEXT_BOUNDARY_SENTENCE_START: |
| 50 | case ATK_TEXT_BOUNDARY_SENTENCE_END: |
| 51 | return accessibility::AccessibleTextType::SENTENCE; |
| 52 | case ATK_TEXT_BOUNDARY_LINE_START: |
| 53 | case ATK_TEXT_BOUNDARY_LINE_END: |
| 54 | return accessibility::AccessibleTextType::LINE; |
| 55 | default: |
| 56 | return -1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /*****************************************************************************/ |
| 61 | |
| 62 | static accessibility::AccessibleScrollType |
| 63 | scroll_type_from_scroll_type(AtkScrollType type) |
| 64 | { |
| 65 | switch(type) |
| 66 | { |
| 67 | case ATK_SCROLL_TOP_LEFT: |
| 68 | return accessibility::AccessibleScrollType_SCROLL_TOP_LEFT; |
| 69 | case ATK_SCROLL_BOTTOM_RIGHT: |
| 70 | return accessibility::AccessibleScrollType_SCROLL_BOTTOM_RIGHT; |
| 71 | case ATK_SCROLL_TOP_EDGE: |
| 72 | return accessibility::AccessibleScrollType_SCROLL_TOP_EDGE; |
| 73 | case ATK_SCROLL_BOTTOM_EDGE: |
| 74 | return accessibility::AccessibleScrollType_SCROLL_BOTTOM_EDGE; |
| 75 | case ATK_SCROLL_LEFT_EDGE: |
| 76 | return accessibility::AccessibleScrollType_SCROLL_LEFT_EDGE; |
| 77 | case ATK_SCROLL_RIGHT_EDGE: |
| 78 | return accessibility::AccessibleScrollType_SCROLL_RIGHT_EDGE; |
| 79 | case ATK_SCROLL_ANYWHERE: |
| 80 | return accessibility::AccessibleScrollType_SCROLL_ANYWHERE; |
| 81 | default: |
| 82 | throw lang::NoSupportException(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /*****************************************************************************/ |
| 87 | |
| 88 | static gchar * |
| 89 | adjust_boundaries( css::uno::Reference<css::accessibility::XAccessibleText> const & pText, |
| 90 | accessibility::TextSegment const & rTextSegment, |
| 91 | AtkTextBoundary boundary_type, |
| 92 | gint * start_offset, gint * end_offset ) |
| 93 | { |
| 94 | accessibility::TextSegment aTextSegment; |
| 95 | OUString aString; |
| 96 | gint start = 0, end = 0; |
| 97 | |
| 98 | if( !rTextSegment.SegmentText.isEmpty() ) |
| 99 | { |
| 100 | switch(boundary_type) |
| 101 | { |
| 102 | case ATK_TEXT_BOUNDARY_CHAR: |
| 103 | if ((rTextSegment.SegmentEnd - rTextSegment.SegmentStart) == 1 |
| 104 | && rtl::isSurrogate(rTextSegment.SegmentText[0])) |
| 105 | return nullptr; |
| 106 | [[fallthrough]]; |
| 107 | case ATK_TEXT_BOUNDARY_LINE_START: |
| 108 | case ATK_TEXT_BOUNDARY_LINE_END: |
| 109 | case ATK_TEXT_BOUNDARY_SENTENCE_START: |
| 110 | start = rTextSegment.SegmentStart; |
| 111 | end = rTextSegment.SegmentEnd; |
| 112 | aString = rTextSegment.SegmentText; |
| 113 | break; |
| 114 | |
| 115 | // the OOo break iterator behaves as SENTENCE_START |
| 116 | case ATK_TEXT_BOUNDARY_SENTENCE_END: |
| 117 | start = rTextSegment.SegmentStart; |
| 118 | end = rTextSegment.SegmentEnd; |
| 119 | |
| 120 | if( start > 0 ) |
| 121 | --start; |
| 122 | if( end > 0 && end < pText->getCharacterCount() - 1 ) |
| 123 | --end; |
| 124 | |
| 125 | aString = pText->getTextRange(start, end); |
| 126 | break; |
| 127 | |
| 128 | case ATK_TEXT_BOUNDARY_WORD_START: |
| 129 | start = rTextSegment.SegmentStart; |
| 130 | |
| 131 | // Determine the start index of the next segment |
| 132 | aTextSegment = pText->getTextBehindIndex(rTextSegment.SegmentEnd, |
| 133 | text_type_from_boundary(boundary_type)); |
| 134 | if( !aTextSegment.SegmentText.isEmpty() ) |
| 135 | end = aTextSegment.SegmentStart; |
| 136 | else |
| 137 | end = pText->getCharacterCount(); |
| 138 | |
| 139 | aString = pText->getTextRange(start, end); |
| 140 | break; |
| 141 | |
| 142 | case ATK_TEXT_BOUNDARY_WORD_END: |
| 143 | end = rTextSegment.SegmentEnd; |
| 144 | |
| 145 | // Determine the end index of the previous segment |
| 146 | aTextSegment = pText->getTextBeforeIndex(rTextSegment.SegmentStart, |
| 147 | text_type_from_boundary(boundary_type)); |
| 148 | if( !aTextSegment.SegmentText.isEmpty() ) |
| 149 | start = aTextSegment.SegmentEnd; |
| 150 | else |
| 151 | start = 0; |
| 152 | |
| 153 | aString = pText->getTextRange(start, end); |
| 154 | break; |
| 155 | |
| 156 | default: |
| 157 | return nullptr; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | *start_offset = start; |
| 162 | *end_offset = end; |
| 163 | |
| 164 | return OUStringToGChar(aString); |
| 165 | } |
| 166 | |
| 167 | /*****************************************************************************/ |
| 168 | |
| 169 | /// @throws uno::RuntimeException |
| 170 | static css::uno::Reference<css::accessibility::XAccessibleText> |
| 171 | getText( AtkText *pText ) |
| 172 | { |
| 173 | AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); |
| 174 | if( pWrap ) |
| 175 | { |
| 176 | if( !pWrap->mpText.is() ) |
| 177 | { |
| 178 | pWrap->mpText.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 179 | } |
| 180 | |
| 181 | return pWrap->mpText; |
| 182 | } |
| 183 | |
| 184 | return css::uno::Reference<css::accessibility::XAccessibleText>(); |
| 185 | } |
| 186 | |
| 187 | /*****************************************************************************/ |
| 188 | |
| 189 | /// @throws uno::RuntimeException |
| 190 | static css::uno::Reference<css::accessibility::XAccessibleTextMarkup> |
| 191 | getTextMarkup( AtkText *pText ) |
| 192 | { |
| 193 | AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); |
| 194 | if( pWrap ) |
| 195 | { |
| 196 | if( !pWrap->mpTextMarkup.is() ) |
| 197 | { |
| 198 | pWrap->mpTextMarkup.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 199 | } |
| 200 | |
| 201 | return pWrap->mpTextMarkup; |
| 202 | } |
| 203 | |
| 204 | return css::uno::Reference<css::accessibility::XAccessibleTextMarkup>(); |
| 205 | } |
| 206 | |
| 207 | /*****************************************************************************/ |
| 208 | |
| 209 | /// @throws uno::RuntimeException |
| 210 | static css::uno::Reference<css::accessibility::XAccessibleTextAttributes> |
| 211 | getTextAttributes( AtkText *pText ) |
| 212 | { |
| 213 | AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); |
| 214 | if( pWrap ) |
| 215 | { |
| 216 | if( !pWrap->mpTextAttributes.is() ) |
| 217 | { |
| 218 | pWrap->mpTextAttributes.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 219 | } |
| 220 | |
| 221 | return pWrap->mpTextAttributes; |
| 222 | } |
| 223 | |
| 224 | return css::uno::Reference<css::accessibility::XAccessibleTextAttributes>(); |
| 225 | } |
| 226 | |
| 227 | /*****************************************************************************/ |
| 228 | |
| 229 | /// @throws uno::RuntimeException |
| 230 | static css::uno::Reference<css::accessibility::XAccessibleMultiLineText> |
| 231 | getMultiLineText( AtkText *pText ) |
| 232 | { |
| 233 | AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pText ); |
| 234 | if( pWrap ) |
| 235 | { |
| 236 | if( !pWrap->mpMultiLineText.is() ) |
| 237 | { |
| 238 | pWrap->mpMultiLineText.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 239 | } |
| 240 | |
| 241 | return pWrap->mpMultiLineText; |
| 242 | } |
| 243 | |
| 244 | return css::uno::Reference<css::accessibility::XAccessibleMultiLineText>(); |
| 245 | } |
| 246 | |
| 247 | /*****************************************************************************/ |
| 248 | |
| 249 | extern "C" { |
| 250 | |
| 251 | static gchar * |
| 252 | text_wrapper_get_text (AtkText *text, |
| 253 | gint start_offset, |
| 254 | gint end_offset) |
| 255 | { |
| 256 | gchar * ret = nullptr; |
| 257 | |
| 258 | g_return_val_if_fail( (end_offset == -1) || (end_offset >= start_offset), nullptr ); |
| 259 | |
| 260 | try { |
| 261 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 262 | = getText( text ); |
| 263 | if( pText.is() ) |
| 264 | { |
| 265 | OUString aText; |
| 266 | sal_Int32 n = pText->getCharacterCount(); |
| 267 | |
| 268 | if( start_offset < n ) |
| 269 | { |
| 270 | if( -1 == end_offset ) |
| 271 | aText = pText->getTextRange(start_offset, n - start_offset); |
| 272 | else |
| 273 | aText = pText->getTextRange(start_offset, end_offset); |
| 274 | } |
| 275 | |
| 276 | ret = g_strdup( OUStringToOString(aText, RTL_TEXTENCODING_UTF8 ).getStr() ); |
| 277 | } |
| 278 | } |
| 279 | catch(const uno::Exception&) { |
| 280 | g_warning( "Exception in getText()" ); |
| 281 | } |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static gchar * |
| 287 | text_wrapper_get_text_after_offset (AtkText *text, |
| 288 | gint offset, |
| 289 | AtkTextBoundary boundary_type, |
| 290 | gint *start_offset, |
| 291 | gint *end_offset) |
| 292 | { |
| 293 | try { |
| 294 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 295 | = getText( text ); |
| 296 | if( pText.is() ) |
| 297 | { |
| 298 | accessibility::TextSegment aTextSegment = pText->getTextBehindIndex(offset, text_type_from_boundary(boundary_type)); |
| 299 | return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); |
| 300 | } |
| 301 | } |
| 302 | catch(const uno::Exception&) { |
| 303 | g_warning( "Exception in get_text_after_offset()" ); |
| 304 | } |
| 305 | |
| 306 | return nullptr; |
| 307 | } |
| 308 | |
| 309 | static gchar * |
| 310 | text_wrapper_get_text_at_offset (AtkText *text, |
| 311 | gint offset, |
| 312 | AtkTextBoundary boundary_type, |
| 313 | gint *start_offset, |
| 314 | gint *end_offset) |
| 315 | { |
| 316 | try { |
| 317 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 318 | = getText( text ); |
| 319 | if( pText.is() ) |
| 320 | { |
| 321 | /* If the user presses the 'End' key, the caret will be placed behind the last character, |
| 322 | * which is the same index as the first character of the next line. In atk the magic offset |
| 323 | * '-2' is used to cover this special case. |
| 324 | */ |
| 325 | if ( |
| 326 | -2 == offset && |
| 327 | (ATK_TEXT_BOUNDARY_LINE_START == boundary_type || |
| 328 | ATK_TEXT_BOUNDARY_LINE_END == boundary_type) |
| 329 | ) |
| 330 | { |
| 331 | css::uno::Reference< |
| 332 | css::accessibility::XAccessibleMultiLineText> pMultiLineText |
| 333 | = getMultiLineText( text ); |
| 334 | if( pMultiLineText.is() ) |
| 335 | { |
| 336 | accessibility::TextSegment aTextSegment = pMultiLineText->getTextAtLineWithCaret(); |
| 337 | return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | accessibility::TextSegment aTextSegment = pText->getTextAtIndex(offset, text_type_from_boundary(boundary_type)); |
| 342 | return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); |
| 343 | } |
| 344 | } |
| 345 | catch(const uno::Exception&) { |
| 346 | g_warning( "Exception in get_text_at_offset()" ); |
| 347 | } |
| 348 | |
| 349 | return nullptr; |
| 350 | } |
| 351 | |
| 352 | static gunichar |
| 353 | text_wrapper_get_character_at_offset (AtkText *text, |
| 354 | gint offset) |
| 355 | { |
| 356 | gint start, end; |
| 357 | gunichar uc = 0xFFFFFFFF; |
| 358 | |
| 359 | gchar * char_as_string = |
| 360 | text_wrapper_get_text_at_offset(text, offset, ATK_TEXT_BOUNDARY_CHAR, |
| 361 | &start, &end); |
| 362 | if( char_as_string ) |
| 363 | { |
| 364 | uc = g_utf8_get_char( char_as_string ); |
| 365 | g_free( char_as_string ); |
| 366 | } |
| 367 | |
| 368 | return uc; |
| 369 | } |
| 370 | |
| 371 | static gchar * |
| 372 | text_wrapper_get_text_before_offset (AtkText *text, |
| 373 | gint offset, |
| 374 | AtkTextBoundary boundary_type, |
| 375 | gint *start_offset, |
| 376 | gint *end_offset) |
| 377 | { |
| 378 | try { |
| 379 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 380 | = getText( text ); |
| 381 | if( pText.is() ) |
| 382 | { |
| 383 | accessibility::TextSegment aTextSegment = pText->getTextBeforeIndex(offset, text_type_from_boundary(boundary_type)); |
| 384 | return adjust_boundaries(pText, aTextSegment, boundary_type, start_offset, end_offset); |
| 385 | } |
| 386 | } |
| 387 | catch(const uno::Exception&) { |
| 388 | g_warning( "Exception in text_before_offset()" ); |
| 389 | } |
| 390 | |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
| 394 | static gint |
| 395 | text_wrapper_get_caret_offset (AtkText *text) |
| 396 | { |
| 397 | gint offset = -1; |
| 398 | |
| 399 | try { |
| 400 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 401 | = getText( text ); |
| 402 | if( pText.is() ) |
| 403 | offset = pText->getCaretPosition(); |
| 404 | } |
| 405 | catch(const uno::Exception&) { |
| 406 | g_warning( "Exception in getCaretPosition()" ); |
| 407 | } |
| 408 | |
| 409 | return offset; |
| 410 | } |
| 411 | |
| 412 | static gboolean |
| 413 | text_wrapper_set_caret_offset (AtkText *text, |
| 414 | gint offset) |
| 415 | { |
| 416 | try { |
| 417 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 418 | = getText( text ); |
| 419 | if( pText.is() ) |
| 420 | return pText->setCaretPosition( offset ); |
| 421 | } |
| 422 | catch(const uno::Exception&) { |
| 423 | g_warning( "Exception in setCaretPosition()" ); |
| 424 | } |
| 425 | |
| 426 | return FALSE; |
| 427 | } |
| 428 | |
| 429 | // #i92232# |
| 430 | static AtkAttributeSet* |
| 431 | handle_text_markup_as_run_attribute( css::uno::Reference<css::accessibility::XAccessibleTextMarkup> const & pTextMarkup, |
| 432 | const gint nTextMarkupType, |
| 433 | const gint offset, |
| 434 | AtkAttributeSet* pSet, |
| 435 | gint *start_offset, |
| 436 | gint *end_offset ) |
| 437 | { |
| 438 | const gint nTextMarkupCount( pTextMarkup->getTextMarkupCount( nTextMarkupType ) ); |
| 439 | for ( gint nTextMarkupIndex = 0; |
| 440 | nTextMarkupIndex < nTextMarkupCount; |
| 441 | ++nTextMarkupIndex ) |
| 442 | { |
| 443 | accessibility::TextSegment aTextSegment = |
| 444 | pTextMarkup->getTextMarkup( nTextMarkupIndex, nTextMarkupType ); |
| 445 | const gint nStartOffsetTextMarkup = aTextSegment.SegmentStart; |
| 446 | const gint nEndOffsetTextMarkup = aTextSegment.SegmentEnd; |
| 447 | if ( nStartOffsetTextMarkup <= offset ) |
| 448 | { |
| 449 | if ( offset < nEndOffsetTextMarkup ) |
| 450 | { |
| 451 | // text markup at <offset> |
| 452 | *start_offset = ::std::max( *start_offset, |
| 453 | nStartOffsetTextMarkup ); |
| 454 | *end_offset = ::std::min( *end_offset, |
| 455 | nEndOffsetTextMarkup ); |
| 456 | switch ( nTextMarkupType ) |
| 457 | { |
| 458 | case css::text::TextMarkupType::SPELLCHECK: |
| 459 | { |
| 460 | pSet = attribute_set_prepend_misspelled( pSet ); |
| 461 | } |
| 462 | break; |
| 463 | case css::text::TextMarkupType::TRACK_CHANGE_INSERTION: |
| 464 | { |
| 465 | pSet = attribute_set_prepend_tracked_change_insertion( pSet ); |
| 466 | } |
| 467 | break; |
| 468 | case css::text::TextMarkupType::TRACK_CHANGE_DELETION: |
| 469 | { |
| 470 | pSet = attribute_set_prepend_tracked_change_deletion( pSet ); |
| 471 | } |
| 472 | break; |
| 473 | case css::text::TextMarkupType::TRACK_CHANGE_FORMATCHANGE: |
| 474 | { |
| 475 | pSet = attribute_set_prepend_tracked_change_formatchange( pSet ); |
| 476 | } |
| 477 | break; |
| 478 | default: |
| 479 | { |
| 480 | OSL_ASSERT( false ); |
| 481 | } |
| 482 | } |
| 483 | break; // no further iteration needed. |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | *start_offset = ::std::max( *start_offset, |
| 488 | nEndOffsetTextMarkup ); |
| 489 | // continue iteration. |
| 490 | } |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | *end_offset = ::std::min( *end_offset, |
| 495 | nStartOffsetTextMarkup ); |
| 496 | break; // no further iteration. |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return pSet; |
| 501 | } |
| 502 | |
| 503 | static AtkAttributeSet * |
| 504 | text_wrapper_get_run_attributes( AtkText *text, |
| 505 | gint offset, |
| 506 | gint *start_offset, |
| 507 | gint *end_offset) |
| 508 | { |
| 509 | AtkAttributeSet *pSet = nullptr; |
| 510 | |
| 511 | try { |
| 512 | bool bOffsetsAreValid = false; |
| 513 | |
| 514 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 515 | = getText( text ); |
| 516 | if( pText.is()) |
| 517 | { |
| 518 | uno::Sequence< beans::PropertyValue > aAttributeList; |
| 519 | |
| 520 | css::uno::Reference<css::accessibility::XAccessibleTextAttributes> |
| 521 | pTextAttributes = getTextAttributes( text ); |
| 522 | if(pTextAttributes.is()) // Text attributes are available for paragraphs only |
| 523 | { |
| 524 | aAttributeList = pTextAttributes->getRunAttributes( offset, uno::Sequence< OUString > () ); |
| 525 | } |
| 526 | else // For other text objects use character attributes |
| 527 | { |
| 528 | aAttributeList = pText->getCharacterAttributes( offset, uno::Sequence< OUString > () ); |
| 529 | } |
| 530 | |
| 531 | pSet = attribute_set_new_from_property_values( aAttributeList, true, text ); |
| 532 | // #i100938# |
| 533 | // - always provide start_offset and end_offset |
| 534 | { |
| 535 | accessibility::TextSegment aTextSegment = |
| 536 | pText->getTextAtIndex(offset, accessibility::AccessibleTextType::ATTRIBUTE_RUN); |
| 537 | |
| 538 | *start_offset = aTextSegment.SegmentStart; |
| 539 | // #i100938# |
| 540 | // Do _not_ increment the end_offset provide by <accessibility::TextSegment> instance |
| 541 | *end_offset = aTextSegment.SegmentEnd; |
| 542 | bOffsetsAreValid = true; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // Special handling for misspelled text |
| 547 | // #i92232# |
| 548 | // - add special handling for tracked changes and refactor the |
| 549 | // corresponding code for handling misspelled text. |
| 550 | css::uno::Reference<css::accessibility::XAccessibleTextMarkup> |
| 551 | pTextMarkup = getTextMarkup( text ); |
| 552 | if( pTextMarkup.is() ) |
| 553 | { |
| 554 | // Get attribute run here if it hasn't been done before |
| 555 | if (!bOffsetsAreValid && pText.is()) |
| 556 | { |
| 557 | accessibility::TextSegment aAttributeTextSegment = |
| 558 | pText->getTextAtIndex(offset, accessibility::AccessibleTextType::ATTRIBUTE_RUN); |
| 559 | *start_offset = aAttributeTextSegment.SegmentStart; |
| 560 | *end_offset = aAttributeTextSegment.SegmentEnd; |
| 561 | } |
| 562 | // handle misspelled text |
| 563 | pSet = handle_text_markup_as_run_attribute( |
| 564 | pTextMarkup, |
| 565 | css::text::TextMarkupType::SPELLCHECK, |
| 566 | offset, pSet, start_offset, end_offset ); |
| 567 | // handle tracked changes |
| 568 | pSet = handle_text_markup_as_run_attribute( |
| 569 | pTextMarkup, |
| 570 | css::text::TextMarkupType::TRACK_CHANGE_INSERTION, |
| 571 | offset, pSet, start_offset, end_offset ); |
| 572 | pSet = handle_text_markup_as_run_attribute( |
| 573 | pTextMarkup, |
| 574 | css::text::TextMarkupType::TRACK_CHANGE_DELETION, |
| 575 | offset, pSet, start_offset, end_offset ); |
| 576 | pSet = handle_text_markup_as_run_attribute( |
| 577 | pTextMarkup, |
| 578 | css::text::TextMarkupType::TRACK_CHANGE_FORMATCHANGE, |
| 579 | offset, pSet, start_offset, end_offset ); |
| 580 | } |
| 581 | } |
| 582 | catch(const uno::Exception&){ |
| 583 | |
| 584 | g_warning( "Exception in get_run_attributes()" ); |
| 585 | |
| 586 | if( pSet ) |
| 587 | { |
| 588 | atk_attribute_set_free( pSet ); |
| 589 | pSet = nullptr; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return pSet; |
| 594 | } |
| 595 | |
| 596 | /*****************************************************************************/ |
| 597 | |
| 598 | static AtkAttributeSet * |
| 599 | text_wrapper_get_default_attributes( AtkText *text ) |
| 600 | { |
| 601 | AtkAttributeSet *pSet = nullptr; |
| 602 | |
| 603 | try { |
| 604 | css::uno::Reference<css::accessibility::XAccessibleTextAttributes> |
| 605 | pTextAttributes = getTextAttributes( text ); |
| 606 | if( pTextAttributes.is() ) |
| 607 | { |
| 608 | uno::Sequence< beans::PropertyValue > aAttributeList = |
| 609 | pTextAttributes->getDefaultAttributes( uno::Sequence< OUString > () ); |
| 610 | |
| 611 | pSet = attribute_set_new_from_property_values( aAttributeList, false, text ); |
| 612 | } |
| 613 | } |
| 614 | catch(const uno::Exception&) { |
| 615 | |
| 616 | g_warning( "Exception in get_default_attributes()" ); |
| 617 | |
| 618 | if( pSet ) |
| 619 | { |
| 620 | atk_attribute_set_free( pSet ); |
| 621 | pSet = nullptr; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return pSet; |
| 626 | } |
| 627 | |
| 628 | /*****************************************************************************/ |
| 629 | |
| 630 | static void |
| 631 | text_wrapper_get_character_extents( AtkText *text, |
| 632 | gint offset, |
| 633 | gint *x, |
| 634 | gint *y, |
| 635 | gint *width, |
| 636 | gint *height, |
| 637 | AtkCoordType coords ) |
| 638 | { |
| 639 | *x = *y = *width = *height = -1; |
| 640 | |
| 641 | try { |
| 642 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 643 | = getText( text ); |
| 644 | if( pText.is() ) |
| 645 | { |
| 646 | awt::Rectangle aRect = pText->getCharacterBounds( offset ); |
| 647 | |
| 648 | gint origin_x = 0; |
| 649 | gint origin_y = 0; |
| 650 | |
| 651 | if (coords == ATK_XY_SCREEN || coords == ATK_XY_WINDOW) |
| 652 | { |
| 653 | g_return_if_fail( ATK_IS_COMPONENT( text ) ); |
| 654 | gint nWidth = -1; |
| 655 | gint nHeight = -1; |
| 656 | atk_component_get_extents(ATK_COMPONENT(text), &origin_x, &origin_y, &nWidth, &nHeight, coords); |
| 657 | } |
| 658 | |
| 659 | *x = aRect.X + origin_x; |
| 660 | *y = aRect.Y + origin_y; |
| 661 | *width = aRect.Width; |
| 662 | *height = aRect.Height; |
| 663 | } |
| 664 | } |
| 665 | catch(const uno::Exception&) { |
| 666 | g_warning( "Exception in getCharacterBounds" ); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | static gint |
| 671 | text_wrapper_get_character_count (AtkText *text) |
| 672 | { |
| 673 | gint rv = 0; |
| 674 | |
| 675 | try { |
| 676 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 677 | = getText( text ); |
| 678 | if( pText.is() ) |
| 679 | rv = pText->getCharacterCount(); |
| 680 | } |
| 681 | catch(const uno::Exception&) { |
| 682 | g_warning( "Exception in getCharacterCount" ); |
| 683 | } |
| 684 | |
| 685 | return rv; |
| 686 | } |
| 687 | |
| 688 | static gint |
| 689 | text_wrapper_get_offset_at_point (AtkText *text, |
| 690 | gint x, |
| 691 | gint y, |
| 692 | AtkCoordType coords) |
| 693 | { |
| 694 | try { |
| 695 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 696 | = getText( text ); |
| 697 | if( pText.is() ) |
| 698 | { |
| 699 | gint origin_x = 0; |
| 700 | gint origin_y = 0; |
| 701 | |
| 702 | if (coords == ATK_XY_SCREEN || coords == ATK_XY_WINDOW) |
| 703 | { |
| 704 | g_return_val_if_fail( ATK_IS_COMPONENT( text ), -1 ); |
| 705 | gint nWidth = -1; |
| 706 | gint nHeight = -1; |
| 707 | atk_component_get_extents(ATK_COMPONENT(text), &origin_x, &origin_y, &nWidth, &nHeight, coords); |
| 708 | } |
| 709 | |
| 710 | return pText->getIndexAtPoint( awt::Point(x - origin_x, y - origin_y) ); |
| 711 | } |
| 712 | } |
| 713 | catch(const uno::Exception&) { |
| 714 | g_warning( "Exception in getIndexAtPoint" ); |
| 715 | } |
| 716 | |
| 717 | return -1; |
| 718 | } |
| 719 | |
| 720 | // FIXME: the whole series of selections API is problematic ... |
| 721 | |
| 722 | static gint |
| 723 | text_wrapper_get_n_selections (AtkText *text) |
| 724 | { |
| 725 | gint rv = 0; |
| 726 | |
| 727 | try { |
| 728 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 729 | = getText( text ); |
| 730 | if( pText.is() ) |
| 731 | rv = ( pText->getSelectionEnd() > pText->getSelectionStart() ) ? 1 : 0; |
| 732 | } |
| 733 | catch(const uno::Exception&) { |
| 734 | g_warning( "Exception in getSelectionEnd() or getSelectionStart()" ); |
| 735 | } |
| 736 | |
| 737 | return rv; |
| 738 | } |
| 739 | |
| 740 | static gchar * |
| 741 | text_wrapper_get_selection (AtkText *text, |
| 742 | gint selection_num, |
| 743 | gint *start_offset, |
| 744 | gint *end_offset) |
| 745 | { |
| 746 | g_return_val_if_fail( selection_num == 0, FALSE ); |
| 747 | |
| 748 | try { |
| 749 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 750 | = getText( text ); |
| 751 | if( pText.is() ) |
| 752 | { |
| 753 | *start_offset = pText->getSelectionStart(); |
| 754 | *end_offset = pText->getSelectionEnd(); |
| 755 | |
| 756 | return OUStringToGChar( pText->getSelectedText() ); |
| 757 | } |
| 758 | } |
| 759 | catch(const uno::Exception&) { |
| 760 | g_warning( "Exception in getSelectionEnd(), getSelectionStart() or getSelectedText()" ); |
| 761 | } |
| 762 | |
| 763 | return nullptr; |
| 764 | } |
| 765 | |
| 766 | static gboolean |
| 767 | text_wrapper_add_selection (AtkText *text, |
| 768 | gint start_offset, |
| 769 | gint end_offset) |
| 770 | { |
| 771 | // FIXME: can we try to be more compatible by expanding an |
| 772 | // existing adjacent selection ? |
| 773 | |
| 774 | try { |
| 775 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 776 | = getText( text ); |
| 777 | if( pText.is() ) |
| 778 | return pText->setSelection( start_offset, end_offset ); // ? |
| 779 | } |
| 780 | catch(const uno::Exception&) { |
| 781 | g_warning( "Exception in setSelection()" ); |
| 782 | } |
| 783 | |
| 784 | return FALSE; |
| 785 | } |
| 786 | |
| 787 | static gboolean |
| 788 | text_wrapper_remove_selection (AtkText *text, |
| 789 | gint selection_num) |
| 790 | { |
| 791 | g_return_val_if_fail( selection_num == 0, FALSE ); |
| 792 | |
| 793 | try { |
| 794 | css::uno::Reference<css::accessibility::XAccessibleText> xText = getText(text); |
| 795 | if (!xText.is()) |
| 796 | return false; |
| 797 | |
| 798 | const sal_Int32 nCaretPos = xText->getCaretPosition(); |
| 799 | return xText->setSelection(nCaretPos, nCaretPos); |
| 800 | } |
| 801 | catch(const uno::Exception&) { |
| 802 | g_warning( "Exception in setSelection()" ); |
| 803 | } |
| 804 | |
| 805 | return FALSE; |
| 806 | } |
| 807 | |
| 808 | static gboolean |
| 809 | text_wrapper_set_selection (AtkText *text, |
| 810 | gint selection_num, |
| 811 | gint start_offset, |
| 812 | gint end_offset) |
| 813 | { |
| 814 | g_return_val_if_fail( selection_num == 0, FALSE ); |
| 815 | |
| 816 | try { |
| 817 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 818 | = getText( text ); |
| 819 | if( pText.is() ) |
| 820 | return pText->setSelection( start_offset, end_offset ); |
| 821 | } |
| 822 | catch(const uno::Exception&) { |
| 823 | g_warning( "Exception in setSelection()" ); |
| 824 | } |
| 825 | |
| 826 | return FALSE; |
| 827 | } |
| 828 | |
| 829 | static gboolean |
| 830 | text_wrapper_scroll_substring_to(AtkText *text, |
| 831 | gint start_offset, |
| 832 | gint end_offset, |
| 833 | AtkScrollType scroll_type) |
| 834 | { |
| 835 | try { |
| 836 | css::uno::Reference<css::accessibility::XAccessibleText> pText |
| 837 | = getText( text ); |
| 838 | |
| 839 | if( pText.is() ) |
| 840 | return pText->scrollSubstringTo( start_offset, end_offset, |
| 841 | scroll_type_from_scroll_type( scroll_type ) ); |
| 842 | } |
| 843 | catch(const uno::Exception&) { |
| 844 | g_warning( "Exception in scrollSubstringTo()" ); |
| 845 | } |
| 846 | |
| 847 | return FALSE; |
| 848 | } |
| 849 | |
| 850 | } // extern "C" |
| 851 | |
| 852 | void |
| 853 | textIfaceInit (gpointer iface_, gpointer) |
| 854 | { |
| 855 | auto const iface = static_cast<AtkTextIface *>(iface_); |
| 856 | g_return_if_fail (iface != nullptr); |
| 857 | |
| 858 | iface->get_text = text_wrapper_get_text; |
| 859 | iface->get_character_at_offset = text_wrapper_get_character_at_offset; |
| 860 | iface->get_text_before_offset = text_wrapper_get_text_before_offset; |
| 861 | iface->get_text_at_offset = text_wrapper_get_text_at_offset; |
| 862 | iface->get_text_after_offset = text_wrapper_get_text_after_offset; |
| 863 | iface->get_caret_offset = text_wrapper_get_caret_offset; |
| 864 | iface->set_caret_offset = text_wrapper_set_caret_offset; |
| 865 | iface->get_character_count = text_wrapper_get_character_count; |
| 866 | iface->get_n_selections = text_wrapper_get_n_selections; |
| 867 | iface->get_selection = text_wrapper_get_selection; |
| 868 | iface->add_selection = text_wrapper_add_selection; |
| 869 | iface->remove_selection = text_wrapper_remove_selection; |
| 870 | iface->set_selection = text_wrapper_set_selection; |
| 871 | iface->get_run_attributes = text_wrapper_get_run_attributes; |
| 872 | iface->get_default_attributes = text_wrapper_get_default_attributes; |
| 873 | iface->get_character_extents = text_wrapper_get_character_extents; |
| 874 | iface->get_offset_at_point = text_wrapper_get_offset_at_point; |
| 875 | iface->scroll_substring_to = text_wrapper_scroll_substring_to; |
| 876 | } |
| 877 | |
| 878 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |