Avoid potential negative array index access to cached text.
[LibreOffice.git] / winaccessibility / source / UAccCOM / AccTextBase.cxx
bloba50cee9dd41c4aca192b4a7f6fd185cec4839270
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 // AccTextBase.cpp: implementation of the CAccTextBase class.
23 #include "stdafx.h"
25 #include "AccTextBase.h"
27 #include <rtl/ustrbuf.hxx>
28 #include <sal/log.hxx>
29 #include <vcl/accessibility/AccessibleTextAttributeHelper.hxx>
30 #include <vcl/svapp.hxx>
31 #include <o3tl/char16_t2wchar_t.hxx>
33 #include <com/sun/star/accessibility/AccessibleScrollType.hpp>
34 #include <com/sun/star/accessibility/AccessibleTextType.hpp>
35 #include <com/sun/star/accessibility/XAccessible.hpp>
36 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
37 #include <com/sun/star/accessibility/XAccessibleComponent.hpp>
38 #include <com/sun/star/accessibility/XAccessibleTextSelection.hpp>
39 #include "MAccessible.h"
41 using namespace css::accessibility;
42 using namespace css::uno;
44 namespace
46 sal_Int16 lcl_matchIA2TextBoundaryType(IA2TextBoundaryType boundaryType)
48 switch (boundaryType)
50 case IA2_TEXT_BOUNDARY_CHAR:
51 return com::sun::star::accessibility::AccessibleTextType::CHARACTER;
52 case IA2_TEXT_BOUNDARY_WORD:
53 return com::sun::star::accessibility::AccessibleTextType::WORD;
54 case IA2_TEXT_BOUNDARY_SENTENCE:
55 return com::sun::star::accessibility::AccessibleTextType::SENTENCE;
56 case IA2_TEXT_BOUNDARY_PARAGRAPH:
57 return com::sun::star::accessibility::AccessibleTextType::PARAGRAPH;
58 case IA2_TEXT_BOUNDARY_LINE:
59 return com::sun::star::accessibility::AccessibleTextType::LINE;
60 case IA2_TEXT_BOUNDARY_ALL:
61 // assert here, better handle it directly at call site
62 assert(false
63 && "No match for IA2_TEXT_BOUNDARY_ALL, handle at call site.");
64 break;
65 default:
66 break;
69 SAL_WARN("iacc2", "Unmatched text boundary type: " << boundaryType);
70 return -1;
75 // Construction/Destruction
78 CAccTextBase::CAccTextBase()
81 CAccTextBase::~CAccTextBase()
85 /**
86 * Get special selection.
87 * @param startOffset Start selection offset.
88 * @param endOffset End selection offset.
89 * @param success Variant to accept the result of if the method call is successful.
90 * @return Result.
92 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_addSelection(long startOffset, long endOffset)
94 SolarMutexGuard g;
96 try {
98 if(pUNOInterface == nullptr)
99 return E_FAIL;
101 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
103 Reference< XAccessibleTextSelection > pRExtension(pRContext,UNO_QUERY);
105 if( pRExtension.is() )
107 pRExtension->addSelection(0, startOffset, endOffset);
108 return S_OK;
110 else
112 pRXText->setSelection(startOffset, endOffset);
113 return S_OK;
116 } catch(...) { return E_FAIL; }
120 * Get special attributes.
121 * @param offset Offset.
122 * @param startOffset Variant to accept start offset.
123 * @param endOffset Variant to accept end offset.
124 * @param textAttributes Variant to accept attributes.
125 * @return Result.
127 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_attributes(long offset, long * startOffset, long * endOffset, BSTR * textAttributes)
129 SolarMutexGuard g;
131 try {
133 if (startOffset == nullptr || endOffset == nullptr || textAttributes == nullptr)
134 return E_INVALIDARG;
136 if(!pRXText.is())
138 return E_FAIL;
141 if (offset < 0 || offset > pRXText->getCharacterCount() )
142 return E_FAIL;
145 const OUString sAttrs = AccessibleTextAttributeHelper::GetIAccessible2TextAttributes(pRXText,
146 IA2AttributeType::TextAttributes,
147 offset, *startOffset, *endOffset);
149 if(*textAttributes)
150 SysFreeString(*textAttributes);
151 *textAttributes = SysAllocString(o3tl::toW(sAttrs.getStr()));
153 return S_OK;
155 } catch(...) { return E_FAIL; }
159 * Get caret position.
160 * @param offset Variant to accept caret offset.
161 * @return Result.
163 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_caretOffset(long * offset)
165 SolarMutexGuard g;
167 try {
169 if (offset == nullptr)
170 return E_INVALIDARG;
172 if(!pRXText.is())
174 *offset = 0;
175 return S_OK;
178 *offset = pRXText->getCaretPosition();
179 return S_OK;
181 } catch(...) { return E_FAIL; }
185 * Get character count.
186 * @param nCharacters Variant to accept character count.
187 * @return Result.
189 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_characterCount(long * nCharacters)
191 SolarMutexGuard g;
193 try {
195 if (nCharacters == nullptr)
196 return E_INVALIDARG;
198 if(!pRXText.is())
200 *nCharacters = 0;
201 return S_OK;
204 *nCharacters = pRXText->getCharacterCount();
205 return S_OK;
207 } catch(...) { return E_FAIL; }
211 * Get character extents.
212 * @param offset Offset.
213 * @param x Variant to accept x position.
214 * @param y Variant to accept y position.
215 * @param width Variant to accept width.
216 * @param Height Variant to accept height.
217 * @return Result.
219 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_characterExtents(long offset, IA2CoordinateType coordType, long * x, long * y, long * width, long * height)
221 SolarMutexGuard g;
223 try {
225 if (x == nullptr || height == nullptr || y == nullptr || width == nullptr)
226 return E_INVALIDARG;
228 if(!pRXText.is())
229 return E_FAIL;
231 if (offset < 0 || offset > pRXText->getCharacterCount())
232 return E_FAIL;
234 css::awt::Rectangle rectangle;
235 rectangle = pRXText->getCharacterBounds(offset);
237 //IA2Point aPoint;
238 css::awt::Point aPoint;
240 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
241 if( !pRContext.is() )
243 return E_FAIL;
245 Reference<XAccessibleComponent> pRComp(pRContext,UNO_QUERY);
246 if( pRComp.is() )
248 if(coordType == IA2_COORDTYPE_SCREEN_RELATIVE)
250 css::awt::Point pt = pRComp->getLocationOnScreen();
251 aPoint.X = pt.X;
252 aPoint.Y = pt.Y;
254 else if(coordType == IA2_COORDTYPE_PARENT_RELATIVE)
256 css::awt::Point pt = pRComp->getLocation();
257 aPoint.X = pt.X;
258 aPoint.Y = pt.Y;
261 rectangle.X = rectangle.X + aPoint.X;
262 rectangle.Y = rectangle.Y + aPoint.Y;
264 *x = rectangle.X;
265 *y = rectangle.Y;
267 // pRXText->getCharacterBounds() have different implement in different acc component
268 // But we need return the width/height == 1 for every component when offset == text length.
269 // So we ignore the return result of pRXText->getCharacterBounds() when offset == text length.
270 if (offset == pRXText->getCharacterCount())
272 *width = 1;
273 *height = 1;
275 else
277 *width = rectangle.Width;
278 *height = rectangle.Height;
281 return S_OK;
283 } catch(...) { return E_FAIL; }
287 * Get selections count.
288 * @param nSelections Variant to accept selections count.
289 * @return Result.
291 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_nSelections(long * nSelections)
293 SolarMutexGuard g;
295 try {
297 if (nSelections == nullptr)
298 return E_INVALIDARG;
300 if(pUNOInterface == nullptr)
302 *nSelections = 0;
303 return S_OK;
306 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
308 Reference< XAccessibleTextSelection > pRExtension(pRContext,UNO_QUERY);
310 if( pRExtension.is() )
312 *nSelections = pRExtension->getSelectedPortionCount();
313 return S_OK;
316 long iLength = pRXText->getSelectedText().getLength();
317 if( iLength> 0)
319 *nSelections = 1;
320 return S_OK;
323 *nSelections = 0;
324 return S_OK;
326 } catch(...) { return E_FAIL; }
330 * Get offset of some special point.
331 * @param x X position of one point.
332 * @param x Y position of one point.
333 * @param coordType Type.
334 * @param offset Variant to accept offset.
335 * @return Result.
337 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_offsetAtPoint(long x, long y, IA2CoordinateType coordType, long * offset)
339 SolarMutexGuard g;
341 try {
343 if (offset == nullptr)
344 return E_INVALIDARG;
346 if(!pRXText.is())
347 return E_FAIL;
349 css::awt::Point point;
350 point.X = x;
351 point.Y = y;
353 if (coordType == IA2_COORDTYPE_SCREEN_RELATIVE)
355 // convert from screen to local coordinates
356 Reference<XAccessibleContext> xContext = pUNOInterface->getAccessibleContext();
357 Reference<XAccessibleComponent> xComponent(xContext, UNO_QUERY);
358 if (!xComponent.is())
359 return S_FALSE;
361 css::awt::Point aObjectPos = xComponent->getLocationOnScreen();
362 point.X -= aObjectPos.X;
363 point.Y -= aObjectPos.Y;
366 *offset = pRXText->getIndexAtPoint(point);
367 return S_OK;
369 } catch(...) { return E_FAIL; }
373 * Get selection range.
374 * @param selection selection count.
375 * @param startOffset Variant to accept the start offset of special selection.
376 * @param endOffset Variant to accept the end offset of special selection.
377 * @return Result.
380 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_selection(long selectionIndex, long * startOffset, long * endOffset)
382 SolarMutexGuard g;
384 try {
386 if (startOffset == nullptr || endOffset == nullptr )
387 return E_INVALIDARG;
389 if(pUNOInterface == nullptr )
390 return E_FAIL;
392 long nSelection = 0;
393 get_nSelections(&nSelection);
395 if(selectionIndex >= nSelection || selectionIndex < 0 )
396 return E_FAIL;
398 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
400 Reference< XAccessibleTextSelection > pRExtension(pRContext,UNO_QUERY);
402 if( pRExtension.is() )
404 *startOffset = pRExtension->getSeletedPositionStart(selectionIndex);
405 *endOffset = pRExtension->getSeletedPositionEnd(selectionIndex);
406 return S_OK;
408 else if (pRXText->getSelectionEnd() > -1)
410 *startOffset = pRXText->getSelectionStart();
411 *endOffset = pRXText->getSelectionEnd();
412 return S_OK;
415 *startOffset = 0;
416 *endOffset = 0;
417 return E_FAIL;
419 } catch(...) { return E_FAIL; }
423 * Get special text.
424 * @param startOffset Start position of special range.
425 * @param endOffset End position of special range.
426 * @param text Variant to accept the text of special range.
427 * @return Result.
429 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_text(long startOffset, long endOffset, BSTR * text)
431 SolarMutexGuard g;
433 try {
435 if (text == nullptr)
436 return E_INVALIDARG;
438 if(!pRXText.is())
439 return E_FAIL;
441 if (endOffset < -1 || endOffset < startOffset )
443 return E_FAIL;
446 OUString ouStr;
447 if (endOffset == -1 )
449 long nLen=0;
450 if(SUCCEEDED(get_characterCount(&nLen)))
452 ouStr = pRXText->getTextRange(0, nLen);
455 else
457 ouStr = pRXText->getTextRange(startOffset, endOffset);
460 SysFreeString(*text);
461 *text = SysAllocString(o3tl::toW(ouStr.getStr()));
462 return S_OK;
464 } catch(...) { return E_FAIL; }
468 * Get special text before some position.
469 * @param offset Special position.
470 * @param boundaryType Boundary type.
471 * @param startOffset Variant to accept the start offset.
472 * @param endOffset Variant to accept the end offset.
473 * @param text Variant to accept the special text.
474 * @return Result.
476 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_textBeforeOffset(long offset, IA2TextBoundaryType boundaryType, long * startOffset, long * endOffset, BSTR * text)
478 SolarMutexGuard g;
480 try {
482 if (startOffset == nullptr || endOffset == nullptr || text == nullptr)
483 return E_INVALIDARG;
485 if(!pRXText.is())
486 return E_FAIL;
488 if (boundaryType == IA2_TEXT_BOUNDARY_ALL)
490 long nChar;
491 get_nCharacters( &nChar );
492 *startOffset = 0;
493 *endOffset = nChar;
494 return get_text(0, nChar, text);
497 const sal_Int16 nUnoBoundaryType = lcl_matchIA2TextBoundaryType(boundaryType);
498 if (nUnoBoundaryType < 0)
499 return E_FAIL;
501 TextSegment segment = pRXText->getTextBeforeIndex(offset, nUnoBoundaryType);
502 OUString ouStr = segment.SegmentText;
503 SysFreeString(*text);
504 *text = SysAllocString(o3tl::toW(ouStr.getStr()));
505 *startOffset = segment.SegmentStart;
506 *endOffset = segment.SegmentEnd;
508 return S_OK;
510 } catch(...) { return E_FAIL; }
514 * Get special text after some position.
515 * @param offset Special position.
516 * @param boundaryType Boundary type.
517 * @param startOffset Variant to accept the start offset.
518 * @param endOffset Variant to accept the end offset.
519 * @param text Variant to accept the special text.
520 * @return Result.
522 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_textAfterOffset(long offset, IA2TextBoundaryType boundaryType, long * startOffset, long * endOffset, BSTR * text)
524 SolarMutexGuard g;
526 try {
528 if (startOffset == nullptr || endOffset == nullptr || text == nullptr)
529 return E_INVALIDARG;
531 if(!pRXText.is())
532 return E_FAIL;
534 if (boundaryType == IA2_TEXT_BOUNDARY_ALL)
536 long nChar;
537 get_nCharacters( &nChar );
538 *startOffset = 0;
539 *endOffset = nChar;
540 return get_text(0, nChar, text);
543 const sal_Int16 nUnoBoundaryType = lcl_matchIA2TextBoundaryType(boundaryType);
544 if (nUnoBoundaryType < 0)
545 return E_FAIL;
547 TextSegment segment = pRXText->getTextBehindIndex(offset, nUnoBoundaryType);
548 OUString ouStr = segment.SegmentText;
549 SysFreeString(*text);
550 *text = SysAllocString(o3tl::toW(ouStr.getStr()));
551 *startOffset = segment.SegmentStart;
552 *endOffset = segment.SegmentEnd;
554 return S_OK;
556 } catch(...) { return E_FAIL; }
560 * Get special text at some position.
561 * @param offset Special position.
562 * @param boundaryType Boundary type.
563 * @param startOffset Variant to accept the start offset.
564 * @param endOffset Variant to accept the end offset.
565 * @param text Variant to accept the special text.
566 * @return Result.
568 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_textAtOffset(long offset, IA2TextBoundaryType boundaryType, long * startOffset, long * endOffset, BSTR * text)
570 SolarMutexGuard g;
572 try {
574 if (startOffset == nullptr || text == nullptr ||endOffset == nullptr)
575 return E_INVALIDARG;
577 if(!pRXText.is())
578 return E_FAIL;
580 if (boundaryType == IA2_TEXT_BOUNDARY_ALL)
582 long nChar;
583 get_nCharacters( &nChar );
584 *startOffset = 0;
585 *endOffset = nChar;
586 return get_text(0, nChar, text);
589 const sal_Int16 nUnoBoundaryType = lcl_matchIA2TextBoundaryType(boundaryType);
590 if (nUnoBoundaryType < 0)
591 return E_FAIL;
593 TextSegment segment = pRXText->getTextAtIndex(offset, nUnoBoundaryType);
594 OUString ouStr = segment.SegmentText;
595 SysFreeString(*text);
596 *text = SysAllocString(o3tl::toW(ouStr.getStr()));
597 *startOffset = segment.SegmentStart;
598 *endOffset = segment.SegmentEnd;
600 return S_OK;
602 } catch(...) { return E_FAIL; }
606 * Remove selection.
607 * @param selectionIndex Special selection index
608 * @param success Variant to accept the method called result.
609 * @return Result.
611 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::removeSelection(long selectionIndex)
613 SolarMutexGuard g;
615 try {
617 if(pUNOInterface == nullptr)
619 return E_FAIL;
622 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
624 Reference< XAccessibleTextSelection > pRExtension(pRContext,UNO_QUERY);
626 if( pRExtension.is() )
628 pRExtension->removeSelection(selectionIndex);
629 return S_OK;
631 else
633 pRXText->setSelection(0, 0);
634 return S_OK;
637 } catch(...) { return E_FAIL; }
641 * Set caret position.
642 * @param offset Special position.
643 * @param success Variant to accept the method called result.
644 * @return Result.
646 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::setCaretOffset(long offset)
648 SolarMutexGuard g;
650 try {
652 if(!pRXText.is())
653 return E_FAIL;
655 pRXText->setCaretPosition(offset);
657 return S_OK;
659 } catch(...) { return E_FAIL; }
663 * Set special selection.
664 * @param selectionIndex Special selection index.
665 * @param startOffset start position.
666 * @param endOffset end position.
667 * @param success Variant to accept the method called result.
668 * @return Result.
670 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::setSelection(long, long startOffset, long endOffset)
672 SolarMutexGuard g;
674 try {
676 if(!pRXText.is())
678 return E_FAIL;
681 pRXText->setSelection(startOffset, endOffset);
683 return S_OK;
685 } catch(...) { return E_FAIL; }
689 * Get characters count.
690 * @param nCharacters Variant to accept the characters count.
691 * @return Result.
693 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_nCharacters(long * nCharacters)
695 SolarMutexGuard g;
697 try {
699 if (nCharacters == nullptr)
700 return E_INVALIDARG;
702 if(!pRXText.is())
704 *nCharacters = 0;
705 return S_OK;
708 *nCharacters = pRXText->getCharacterCount();
710 return S_OK;
712 } catch(...) { return E_FAIL; }
715 // added by qiuhd, 2006/07/03, for direver 07/11
716 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_newText( IA2TextSegment *)
718 return E_NOTIMPL;
721 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::get_oldText( IA2TextSegment *)
723 return E_NOTIMPL;
727 * Scroll to special sub-string .
728 * @param startIndex Start index of sub string.
729 * @param endIndex End index of sub string.
730 * @return Result.
732 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::scrollSubstringToPoint(long, long, IA2CoordinateType, long, long )
734 return E_NOTIMPL;
737 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::scrollSubstringTo(long startIndex, long endIndex, IA2ScrollType type)
739 SolarMutexGuard g;
741 try {
743 if(!pRXText.is())
744 return E_FAIL;
746 AccessibleScrollType lUnoType;
748 switch(type)
750 case IA2_SCROLL_TYPE_TOP_LEFT:
751 lUnoType = AccessibleScrollType_SCROLL_TOP_LEFT;
752 break;
753 case IA2_SCROLL_TYPE_BOTTOM_RIGHT:
754 lUnoType = AccessibleScrollType_SCROLL_BOTTOM_RIGHT;
755 break;
756 case IA2_SCROLL_TYPE_TOP_EDGE:
757 lUnoType = AccessibleScrollType_SCROLL_TOP_EDGE;
758 break;
759 case IA2_SCROLL_TYPE_BOTTOM_EDGE:
760 lUnoType = AccessibleScrollType_SCROLL_BOTTOM_EDGE;
761 break;
762 case IA2_SCROLL_TYPE_LEFT_EDGE:
763 lUnoType = AccessibleScrollType_SCROLL_LEFT_EDGE;
764 break;
765 case IA2_SCROLL_TYPE_RIGHT_EDGE:
766 lUnoType = AccessibleScrollType_SCROLL_RIGHT_EDGE;
767 break;
768 case IA2_SCROLL_TYPE_ANYWHERE:
769 lUnoType = AccessibleScrollType_SCROLL_ANYWHERE;
770 break;
771 default:
772 return E_NOTIMPL;
775 if (pRXText->scrollSubstringTo(startIndex, endIndex, lUnoType))
776 return S_OK;
778 return E_NOTIMPL;
780 } catch(...) { return E_FAIL; }
784 * Put UNO interface.
785 * @param pXInterface UNO interface.
786 * @return Result.
788 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccTextBase::put_XInterface(hyper pXInterface)
790 // internal IUNOXWrapper - no mutex meeded
792 try {
794 CUNOXWrapper::put_XInterface(pXInterface);
795 //special query.
796 if(pUNOInterface == nullptr)
797 return E_FAIL;
798 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
799 if( !pRContext.is() )
801 return E_FAIL;
803 Reference<XAccessibleText> pRXI(pRContext,UNO_QUERY);
804 pRXText = pRXI;
805 return S_OK;
807 } catch(...) { return E_FAIL; }
810 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */