Line data Source code
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 :
21 :
22 :
23 : // Global header
24 :
25 :
26 :
27 : #include <limits.h>
28 : #include <utility>
29 : #include <vector>
30 : #include <algorithm>
31 : #include <boost/ref.hpp>
32 : #include <osl/mutex.hxx>
33 : #include <vcl/window.hxx>
34 : #include <vcl/svapp.hxx>
35 : #include <comphelper/sequence.hxx>
36 : #include <com/sun/star/uno/Any.hxx>
37 : #include <com/sun/star/uno/Reference.hxx>
38 : #include <com/sun/star/awt/Point.hpp>
39 : #include <com/sun/star/awt/Rectangle.hpp>
40 : #include <com/sun/star/accessibility/AccessibleTextType.hpp>
41 :
42 :
43 :
44 : // Project-local header
45 :
46 :
47 :
48 : #include <editeng/editdata.hxx>
49 : #include <editeng/unopracc.hxx>
50 : #include "editeng/unoedprx.hxx"
51 : #include <editeng/AccessibleStaticTextBase.hxx>
52 : #include "editeng/AccessibleEditableTextPara.hxx"
53 :
54 :
55 : using namespace ::com::sun::star;
56 : using namespace ::com::sun::star::accessibility;
57 :
58 : /* TODO:
59 : =====
60 :
61 : - separate adapter functionality from AccessibleStaticText class
62 :
63 : - refactor common loops into templates, using mem_fun
64 :
65 : */
66 :
67 : namespace accessibility
68 : {
69 : typedef ::std::vector< beans::PropertyValue > PropertyValueVector;
70 :
71 : class PropertyValueEqualFunctor : public ::std::binary_function< beans::PropertyValue, beans::PropertyValue, bool >
72 : {
73 : public:
74 0 : PropertyValueEqualFunctor()
75 0 : {}
76 0 : bool operator() ( const beans::PropertyValue& lhs, const beans::PropertyValue& rhs ) const
77 : {
78 0 : return ( lhs.Name == rhs.Name && lhs.Value == rhs.Value );
79 : }
80 : };
81 : sal_Unicode cNewLine(0x0a);
82 :
83 :
84 : // Static Helper
85 :
86 :
87 0 : ESelection MakeSelection( sal_Int32 nStartPara, sal_Int32 nStartIndex,
88 : sal_Int32 nEndPara, sal_Int32 nEndIndex )
89 : {
90 : DBG_ASSERT(nStartPara >= 0 && nStartPara <= SAL_MAX_INT32 &&
91 : nStartIndex >= 0 && nStartIndex <= USHRT_MAX &&
92 : nEndPara >= 0 && nEndPara <= SAL_MAX_INT32 &&
93 : nEndIndex >= 0 && nEndIndex <= USHRT_MAX ,
94 : "AccessibleStaticTextBase_Impl::MakeSelection: index value overflow");
95 :
96 0 : return ESelection(nStartPara, nStartIndex, nEndPara, nEndIndex);
97 : }
98 :
99 :
100 :
101 : // AccessibleStaticTextBase_Impl declaration
102 :
103 :
104 :
105 :
106 : /** AccessibleStaticTextBase_Impl
107 :
108 : This class implements the AccessibleStaticTextBase
109 : functionality, mainly by forwarding the calls to an aggregated
110 : AccessibleEditableTextPara. As this is a therefore non-trivial
111 : adapter, factoring out the common functionality from
112 : AccessibleEditableTextPara might be a profitable future task.
113 : */
114 : class AccessibleStaticTextBase_Impl
115 : {
116 : friend class AccessibleStaticTextBase;
117 : public:
118 :
119 : // receive pointer to our frontend class and view window
120 : AccessibleStaticTextBase_Impl();
121 : ~AccessibleStaticTextBase_Impl();
122 :
123 : void SetEditSource( ::std::unique_ptr< SvxEditSource > && pEditSource );
124 :
125 5 : void SetEventSource( const uno::Reference< XAccessible >& rInterface )
126 : {
127 :
128 5 : mxThis = rInterface;
129 5 : }
130 :
131 : void SetOffset( const Point& );
132 :
133 : void Dispose();
134 :
135 : AccessibleEditableTextPara& GetParagraph( sal_Int32 nPara ) const;
136 : sal_Int32 GetParagraphCount() const;
137 :
138 0 : EPosition Index2Internal( sal_Int32 nFlatIndex ) const
139 : {
140 :
141 0 : return ImpCalcInternal( nFlatIndex, false );
142 : }
143 :
144 0 : EPosition Range2Internal( sal_Int32 nFlatIndex ) const
145 : {
146 :
147 0 : return ImpCalcInternal( nFlatIndex, true );
148 : }
149 :
150 : sal_Int32 Internal2Index( EPosition nEEIndex ) const;
151 :
152 : void CorrectTextSegment( TextSegment& aTextSegment,
153 : int nPara ) const;
154 :
155 : bool SetSelection( sal_Int32 nStartPara, sal_Int32 nStartIndex,
156 : sal_Int32 nEndPara, sal_Int32 nEndIndex );
157 : bool CopyText( sal_Int32 nStartPara, sal_Int32 nStartIndex,
158 : sal_Int32 nEndPara, sal_Int32 nEndIndex );
159 :
160 : Rectangle GetParagraphBoundingBox() const;
161 : bool RemoveLineBreakCount( sal_Int32& rIndex );
162 :
163 : private:
164 :
165 : EPosition ImpCalcInternal( sal_Int32 nFlatIndex, bool bExclusive ) const;
166 :
167 : // our frontend class (the one implementing the actual
168 : // interface). That's not necessarily the one containing the impl
169 : // pointer
170 : uno::Reference< XAccessible > mxThis;
171 :
172 : // implements our functionality, we're just an adapter (guarded by solar mutex)
173 : mutable AccessibleEditableTextPara* mpTextParagraph;
174 :
175 : uno::Reference< XAccessible > mxParagraph;
176 :
177 : // a wrapper for the text forwarders (guarded by solar mutex)
178 : mutable SvxEditSourceAdapter maEditSource;
179 :
180 : // guard for maOffset
181 : mutable ::osl::Mutex maMutex;
182 :
183 : /// our current offset to the containing shape/cell (guarded by maMutex)
184 : Point maOffset;
185 :
186 : };
187 :
188 :
189 :
190 : // AccessibleStaticTextBase_Impl implementation
191 :
192 :
193 :
194 5 : AccessibleStaticTextBase_Impl::AccessibleStaticTextBase_Impl() :
195 : mxThis( NULL ),
196 5 : mpTextParagraph( new AccessibleEditableTextPara(NULL) ),
197 : mxParagraph( mpTextParagraph ),
198 : maEditSource(),
199 : maMutex(),
200 10 : maOffset(0,0)
201 : {
202 :
203 : // TODO: this is still somewhat of a hack, all the more since
204 : // now the maTextParagraph has an empty parent reference set
205 5 : }
206 :
207 5 : AccessibleStaticTextBase_Impl::~AccessibleStaticTextBase_Impl()
208 : {
209 5 : }
210 :
211 5 : void AccessibleStaticTextBase_Impl::SetEditSource( ::std::unique_ptr< SvxEditSource > && pEditSource )
212 : {
213 :
214 5 : maEditSource.SetEditSource( std::move(pEditSource) );
215 5 : if( mpTextParagraph )
216 5 : mpTextParagraph->SetEditSource( &maEditSource );
217 5 : }
218 :
219 10 : void AccessibleStaticTextBase_Impl::SetOffset( const Point& rPoint )
220 : {
221 :
222 : // guard against non-atomic access to maOffset data structure
223 : {
224 10 : ::osl::MutexGuard aGuard( maMutex );
225 10 : maOffset = rPoint;
226 : }
227 :
228 10 : if( mpTextParagraph )
229 10 : mpTextParagraph->SetEEOffset( rPoint );
230 10 : }
231 :
232 5 : void AccessibleStaticTextBase_Impl::Dispose()
233 : {
234 :
235 : // we're the owner of the paragraph, so destroy it, too
236 5 : if( mpTextParagraph )
237 5 : mpTextParagraph->Dispose();
238 :
239 : // drop references
240 5 : mxParagraph = NULL;
241 5 : mxThis = NULL;
242 5 : mpTextParagraph = NULL;
243 5 : }
244 :
245 0 : AccessibleEditableTextPara& AccessibleStaticTextBase_Impl::GetParagraph( sal_Int32 nPara ) const
246 : {
247 :
248 0 : if( !mpTextParagraph )
249 0 : throw lang::DisposedException ("object has been already disposed", mxThis );
250 :
251 : // TODO: Have a different method on AccessibleEditableTextPara
252 : // that does not care about state changes
253 0 : mpTextParagraph->SetParagraphIndex( nPara );
254 :
255 0 : return *mpTextParagraph;
256 : }
257 :
258 0 : sal_Int32 AccessibleStaticTextBase_Impl::GetParagraphCount() const
259 : {
260 :
261 0 : if( !mpTextParagraph )
262 0 : return 0;
263 : else
264 0 : return mpTextParagraph->GetTextForwarder().GetParagraphCount();
265 : }
266 :
267 0 : sal_Int32 AccessibleStaticTextBase_Impl::Internal2Index( EPosition nEEIndex ) const
268 : {
269 : // XXX checks for overflow and returns maximum if so
270 0 : sal_Int32 aRes(0);
271 0 : for(sal_Int32 i=0; i<nEEIndex.nPara; ++i)
272 : {
273 0 : sal_Int32 nCount = GetParagraph(i).getCharacterCount();
274 0 : if (SAL_MAX_INT32 - aRes > nCount)
275 0 : return SAL_MAX_INT32;
276 0 : aRes += nCount;
277 : }
278 :
279 0 : if (SAL_MAX_INT32 - aRes > nEEIndex.nIndex)
280 0 : return SAL_MAX_INT32;
281 0 : return aRes + nEEIndex.nIndex;
282 : }
283 :
284 0 : void AccessibleStaticTextBase_Impl::CorrectTextSegment( TextSegment& aTextSegment,
285 : int nPara ) const
286 : {
287 : // Keep 'invalid' values at the TextSegment
288 0 : if( aTextSegment.SegmentStart != -1 &&
289 0 : aTextSegment.SegmentEnd != -1 )
290 : {
291 : // #112814# Correct TextSegment by paragraph offset
292 0 : sal_Int32 nOffset(0);
293 : int i;
294 0 : for(i=0; i<nPara; ++i)
295 0 : nOffset += GetParagraph(i).getCharacterCount();
296 :
297 0 : aTextSegment.SegmentStart += nOffset;
298 0 : aTextSegment.SegmentEnd += nOffset;
299 : }
300 0 : }
301 :
302 0 : EPosition AccessibleStaticTextBase_Impl::ImpCalcInternal( sal_Int32 nFlatIndex, bool bExclusive ) const
303 : {
304 :
305 0 : if( nFlatIndex < 0 )
306 : throw lang::IndexOutOfBoundsException("AccessibleStaticTextBase_Impl::Index2Internal: character index out of bounds",
307 0 : mxThis);
308 : // gratuitously accepting larger indices here, AccessibleEditableTextPara will throw eventually
309 :
310 : sal_Int32 nCurrPara, nCurrIndex, nParas, nCurrCount;
311 0 : for( nCurrPara=0, nParas=GetParagraphCount(), nCurrCount=0, nCurrIndex=0; nCurrPara<nParas; ++nCurrPara )
312 : {
313 0 : nCurrCount = GetParagraph( nCurrPara ).getCharacterCount();
314 0 : nCurrIndex += nCurrCount;
315 0 : if( nCurrIndex >= nFlatIndex )
316 : {
317 : // check overflow
318 : DBG_ASSERT(nCurrPara >= 0 && nCurrPara <= SAL_MAX_INT32 &&
319 : nFlatIndex - nCurrIndex + nCurrCount >= 0 && nFlatIndex - nCurrIndex + nCurrCount <= USHRT_MAX ,
320 : "AccessibleStaticTextBase_Impl::Index2Internal: index value overflow");
321 :
322 0 : return EPosition(nCurrPara, nFlatIndex - nCurrIndex + nCurrCount);
323 : }
324 : }
325 :
326 : // #102170# Allow one-past the end for ranges
327 0 : if( bExclusive && nCurrIndex == nFlatIndex )
328 : {
329 : // check overflow
330 : DBG_ASSERT(nCurrPara > 0 && nCurrPara <= SAL_MAX_INT32 &&
331 : nFlatIndex - nCurrIndex + nCurrCount >= 0 && nFlatIndex - nCurrIndex + nCurrCount <= USHRT_MAX ,
332 : "AccessibleStaticTextBase_Impl::Index2Internal: index value overflow");
333 :
334 0 : return EPosition(nCurrPara-1, nFlatIndex - nCurrIndex + nCurrCount);
335 : }
336 :
337 : // not found? Out of bounds
338 : throw lang::IndexOutOfBoundsException("AccessibleStaticTextBase_Impl::Index2Internal: character index out of bounds",
339 0 : mxThis);
340 : }
341 :
342 0 : bool AccessibleStaticTextBase_Impl::SetSelection( sal_Int32 nStartPara, sal_Int32 nStartIndex,
343 : sal_Int32 nEndPara, sal_Int32 nEndIndex )
344 : {
345 :
346 0 : if( !mpTextParagraph )
347 0 : return false;
348 :
349 : try
350 : {
351 0 : SvxEditViewForwarder& rCacheVF = mpTextParagraph->GetEditViewForwarder( true );
352 0 : return rCacheVF.SetSelection( MakeSelection(nStartPara, nStartIndex, nEndPara, nEndIndex) );
353 : }
354 0 : catch( const uno::RuntimeException& )
355 : {
356 0 : return false;
357 : }
358 : }
359 :
360 0 : bool AccessibleStaticTextBase_Impl::CopyText( sal_Int32 nStartPara, sal_Int32 nStartIndex,
361 : sal_Int32 nEndPara, sal_Int32 nEndIndex )
362 : {
363 :
364 0 : if( !mpTextParagraph )
365 0 : return false;
366 :
367 : try
368 : {
369 0 : SvxEditViewForwarder& rCacheVF = mpTextParagraph->GetEditViewForwarder( true );
370 0 : mpTextParagraph->GetTextForwarder(); // MUST be after GetEditViewForwarder(), see method docs
371 : bool aRetVal;
372 :
373 : // save current selection
374 0 : ESelection aOldSelection;
375 :
376 0 : rCacheVF.GetSelection( aOldSelection );
377 0 : rCacheVF.SetSelection( MakeSelection(nStartPara, nStartIndex, nEndPara, nEndIndex) );
378 0 : aRetVal = rCacheVF.Copy();
379 0 : rCacheVF.SetSelection( aOldSelection ); // restore
380 :
381 0 : return aRetVal;
382 : }
383 0 : catch( const uno::RuntimeException& )
384 : {
385 0 : return false;
386 : }
387 : }
388 :
389 0 : Rectangle AccessibleStaticTextBase_Impl::GetParagraphBoundingBox() const
390 : {
391 0 : Rectangle aRect;
392 0 : if( mpTextParagraph )
393 : {
394 0 : awt::Rectangle aAwtRect = mpTextParagraph->getBounds();
395 0 : aRect = Rectangle( Point( aAwtRect.X, aAwtRect.Y ), Size( aAwtRect.Width, aAwtRect.Height ) );
396 : }
397 : else
398 : {
399 0 : aRect.SetEmpty();
400 : }
401 0 : return aRect;
402 : }
403 : //the input argument is the index(including "\n" ) in the string.
404 : //the function will calculate the actual index(not including "\n") in the string.
405 : //and return true if the index is just at a "\n"
406 0 : bool AccessibleStaticTextBase_Impl::RemoveLineBreakCount( sal_Int32& rIndex )
407 : {
408 : // get the total char number inside the cell.
409 : sal_Int32 i, nCount, nParas;
410 0 : for( i=0, nCount=0, nParas=GetParagraphCount(); i<nParas; ++i )
411 0 : nCount += GetParagraph(i).getCharacterCount();
412 0 : nCount = nCount + (nParas-1);
413 0 : if( nCount == 0 && rIndex == 0) return false;
414 :
415 :
416 : sal_Int32 nCurrPara, nCurrCount;
417 0 : sal_Int32 nLineBreakPos = 0, nLineBreakCount = 0;
418 0 : sal_Int32 nParaCount = GetParagraphCount();
419 0 : for ( nCurrCount = 0, nCurrPara = 0; nCurrPara < nParaCount; nCurrPara++ )
420 : {
421 0 : nCurrCount += GetParagraph( nCurrPara ).getCharacterCount();
422 0 : nLineBreakPos = nCurrCount++;
423 0 : if ( rIndex == nLineBreakPos )
424 : {
425 0 : rIndex -= (++nLineBreakCount);//(++nLineBreakCount);
426 0 : if ( rIndex < 0)
427 : {
428 0 : rIndex = 0;
429 : }
430 : //if the index is at the last position of the last paragraph
431 : //there is no "\n" , so we should increase rIndex by 1 and return false.
432 0 : if ( (nCurrPara+1) == nParaCount )
433 : {
434 0 : rIndex++;
435 0 : return false;
436 : }
437 : else
438 : {
439 0 : return true;
440 : }
441 : }
442 0 : else if ( rIndex < nLineBreakPos )
443 : {
444 0 : rIndex -= nLineBreakCount;
445 0 : return false;
446 : }
447 : else
448 : {
449 0 : nLineBreakCount++;
450 : }
451 : }
452 0 : return false;
453 : }
454 :
455 :
456 : // AccessibleStaticTextBase implementation
457 :
458 5 : AccessibleStaticTextBase::AccessibleStaticTextBase( ::std::unique_ptr< SvxEditSource > && pEditSource ) :
459 5 : mpImpl( new AccessibleStaticTextBase_Impl() )
460 : {
461 5 : SolarMutexGuard aGuard;
462 :
463 5 : SetEditSource( std::move(pEditSource) );
464 5 : }
465 :
466 5 : AccessibleStaticTextBase::~AccessibleStaticTextBase()
467 : {
468 5 : }
469 :
470 5 : void AccessibleStaticTextBase::SetEditSource( ::std::unique_ptr< SvxEditSource > && pEditSource )
471 : {
472 : #ifdef DBG_UTIL
473 : // precondition: solar mutex locked
474 : DBG_TESTSOLARMUTEX();
475 :
476 : mpImpl->SetEditSource( std::move(pEditSource) );
477 :
478 : #else
479 5 : mpImpl->SetEditSource( std::move(pEditSource) );
480 : #endif
481 5 : }
482 :
483 5 : void AccessibleStaticTextBase::SetEventSource( const uno::Reference< XAccessible >& rInterface )
484 : {
485 5 : mpImpl->SetEventSource( rInterface );
486 :
487 5 : }
488 :
489 10 : void AccessibleStaticTextBase::SetOffset( const Point& rPoint )
490 : {
491 : #ifdef DBG_UTIL
492 : // precondition: solar mutex locked
493 : DBG_TESTSOLARMUTEX();
494 :
495 : mpImpl->SetOffset( rPoint );
496 :
497 : #else
498 10 : mpImpl->SetOffset( rPoint );
499 : #endif
500 10 : }
501 :
502 5 : void AccessibleStaticTextBase::Dispose()
503 : {
504 5 : mpImpl->Dispose();
505 :
506 5 : }
507 :
508 : // XAccessibleContext
509 8 : sal_Int32 SAL_CALL AccessibleStaticTextBase::getAccessibleChildCount() throw (uno::RuntimeException, std::exception)
510 : {
511 : // no children at all
512 8 : return 0;
513 : }
514 :
515 0 : uno::Reference< XAccessible > SAL_CALL AccessibleStaticTextBase::getAccessibleChild( sal_Int32 /*i*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
516 : {
517 : // no children at all
518 0 : return uno::Reference< XAccessible >();
519 : }
520 :
521 0 : uno::Reference< XAccessible > SAL_CALL AccessibleStaticTextBase::getAccessibleAtPoint( const awt::Point& /*_aPoint*/ ) throw (uno::RuntimeException, std::exception)
522 : {
523 : // no children at all
524 0 : return uno::Reference< XAccessible >();
525 : }
526 :
527 : // XAccessibleText
528 0 : sal_Int32 SAL_CALL AccessibleStaticTextBase::getCaretPosition() throw (uno::RuntimeException, std::exception)
529 : {
530 0 : SolarMutexGuard aGuard;
531 :
532 : sal_Int32 i, nPos, nParas;
533 0 : for( i=0, nPos=-1, nParas=mpImpl->GetParagraphCount(); i<nParas; ++i )
534 : {
535 0 : if( (nPos=mpImpl->GetParagraph(i).getCaretPosition()) != -1 )
536 0 : return nPos;
537 : }
538 :
539 0 : return nPos;
540 : }
541 :
542 0 : sal_Bool SAL_CALL AccessibleStaticTextBase::setCaretPosition( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
543 : {
544 0 : return setSelection(nIndex, nIndex);
545 : }
546 :
547 0 : sal_Unicode SAL_CALL AccessibleStaticTextBase::getCharacter( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
548 : {
549 0 : SolarMutexGuard aGuard;
550 :
551 0 : EPosition aPos( mpImpl->Index2Internal(nIndex) );
552 :
553 0 : return mpImpl->GetParagraph( aPos.nPara ).getCharacter( aPos.nIndex );
554 : }
555 :
556 0 : uno::Sequence< beans::PropertyValue > SAL_CALL AccessibleStaticTextBase::getCharacterAttributes( sal_Int32 nIndex, const ::com::sun::star::uno::Sequence< OUString >& aRequestedAttributes ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
557 : {
558 0 : SolarMutexGuard aGuard;
559 :
560 : //get the actual index without "\n"
561 0 : mpImpl->RemoveLineBreakCount( nIndex );
562 :
563 0 : EPosition aPos( mpImpl->Index2Internal(nIndex) );
564 :
565 0 : return mpImpl->GetParagraph( aPos.nPara ).getCharacterAttributes( aPos.nIndex, aRequestedAttributes );
566 : }
567 :
568 0 : awt::Rectangle SAL_CALL AccessibleStaticTextBase::getCharacterBounds( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
569 : {
570 0 : SolarMutexGuard aGuard;
571 :
572 : // #108900# Allow ranges for nIndex, as one-past-the-end
573 : // values are now legal, too.
574 0 : EPosition aPos( mpImpl->Range2Internal(nIndex) );
575 :
576 : // #i70916# Text in spread sheet cells return the wrong extents
577 0 : AccessibleEditableTextPara& rPara = mpImpl->GetParagraph( aPos.nPara );
578 0 : awt::Rectangle aParaBounds( rPara.getBounds() );
579 0 : awt::Rectangle aBounds( rPara.getCharacterBounds( aPos.nIndex ) );
580 0 : aBounds.X += aParaBounds.X;
581 0 : aBounds.Y += aParaBounds.Y;
582 :
583 0 : return aBounds;
584 : }
585 :
586 0 : sal_Int32 SAL_CALL AccessibleStaticTextBase::getCharacterCount() throw (uno::RuntimeException, std::exception)
587 : {
588 0 : SolarMutexGuard aGuard;
589 :
590 : sal_Int32 i, nCount, nParas;
591 0 : for( i=0, nCount=0, nParas=mpImpl->GetParagraphCount(); i<nParas; ++i )
592 0 : nCount += mpImpl->GetParagraph(i).getCharacterCount();
593 : //count on the number of "\n" which equals number of paragraphs decrease 1.
594 0 : nCount = nCount + (nParas-1);
595 0 : return nCount;
596 : }
597 :
598 0 : sal_Int32 SAL_CALL AccessibleStaticTextBase::getIndexAtPoint( const awt::Point& rPoint ) throw (uno::RuntimeException, std::exception)
599 : {
600 0 : SolarMutexGuard aGuard;
601 :
602 0 : const sal_Int32 nParas( mpImpl->GetParagraphCount() );
603 : sal_Int32 nIndex;
604 : int i;
605 0 : for( i=0; i<nParas; ++i )
606 : {
607 : // TODO: maybe exploit the fact that paragraphs are
608 : // ordered vertically for early exit
609 :
610 : // #i70916# Text in spread sheet cells return the wrong extents
611 0 : AccessibleEditableTextPara& rPara = mpImpl->GetParagraph( i );
612 0 : awt::Rectangle aParaBounds( rPara.getBounds() );
613 0 : awt::Point aPoint( rPoint );
614 0 : aPoint.X -= aParaBounds.X;
615 0 : aPoint.Y -= aParaBounds.Y;
616 :
617 : // #112814# Use correct index offset
618 0 : if ( ( nIndex = rPara.getIndexAtPoint( aPoint ) ) != -1 )
619 0 : return mpImpl->Internal2Index(EPosition(i, nIndex));
620 : }
621 :
622 0 : return -1;
623 : }
624 :
625 0 : OUString SAL_CALL AccessibleStaticTextBase::getSelectedText() throw (uno::RuntimeException, std::exception)
626 : {
627 0 : SolarMutexGuard aGuard;
628 :
629 0 : sal_Int32 nStart( getSelectionStart() );
630 0 : sal_Int32 nEnd( getSelectionEnd() );
631 :
632 : // #104481# Return the empty string for 'no selection'
633 0 : if( nStart < 0 || nEnd < 0 )
634 0 : return OUString();
635 :
636 0 : return getTextRange( nStart, nEnd );
637 : }
638 :
639 0 : sal_Int32 SAL_CALL AccessibleStaticTextBase::getSelectionStart() throw (uno::RuntimeException, std::exception)
640 : {
641 0 : SolarMutexGuard aGuard;
642 :
643 : sal_Int32 i, nPos, nParas;
644 0 : for( i=0, nPos=-1, nParas=mpImpl->GetParagraphCount(); i<nParas; ++i )
645 : {
646 0 : if( (nPos=mpImpl->GetParagraph(i).getSelectionStart()) != -1 )
647 0 : return nPos;
648 : }
649 :
650 0 : return nPos;
651 : }
652 :
653 0 : sal_Int32 SAL_CALL AccessibleStaticTextBase::getSelectionEnd() throw (uno::RuntimeException, std::exception)
654 : {
655 0 : SolarMutexGuard aGuard;
656 :
657 : sal_Int32 i, nPos, nParas;
658 0 : for( i=0, nPos=-1, nParas=mpImpl->GetParagraphCount(); i<nParas; ++i )
659 : {
660 0 : if( (nPos=mpImpl->GetParagraph(i).getSelectionEnd()) != -1 )
661 0 : return nPos;
662 : }
663 :
664 0 : return nPos;
665 : }
666 :
667 0 : sal_Bool SAL_CALL AccessibleStaticTextBase::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
668 : {
669 0 : SolarMutexGuard aGuard;
670 :
671 0 : EPosition aStartIndex( mpImpl->Range2Internal(nStartIndex) );
672 0 : EPosition aEndIndex( mpImpl->Range2Internal(nEndIndex) );
673 :
674 : return mpImpl->SetSelection( aStartIndex.nPara, aStartIndex.nIndex,
675 0 : aEndIndex.nPara, aEndIndex.nIndex );
676 : }
677 :
678 0 : OUString SAL_CALL AccessibleStaticTextBase::getText() throw (uno::RuntimeException, std::exception)
679 : {
680 0 : SolarMutexGuard aGuard;
681 :
682 : sal_Int32 i, nParas;
683 0 : OUString aRes;
684 0 : for( i=0, nParas=mpImpl->GetParagraphCount(); i<nParas; ++i )
685 0 : aRes += mpImpl->GetParagraph(i).getText();
686 :
687 0 : return aRes;
688 : }
689 :
690 0 : OUString SAL_CALL AccessibleStaticTextBase::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
691 : {
692 0 : SolarMutexGuard aGuard;
693 :
694 0 : if( nStartIndex > nEndIndex )
695 0 : ::std::swap(nStartIndex, nEndIndex);
696 : //if startindex equals endindex we will get nothing. So return an empty string directly.
697 0 : if ( nStartIndex == nEndIndex )
698 : {
699 0 : return OUString();
700 : }
701 0 : bool bStart = mpImpl->RemoveLineBreakCount( nStartIndex );
702 : //if the start index is just at a "\n", we need to begin from the next char
703 0 : if ( bStart )
704 : {
705 0 : nStartIndex++;
706 : }
707 : //we need to find out whether the previous position of the current endindex is at "\n" or not
708 : //if yes we need to mark it and add "\n" at the end of the result
709 0 : sal_Int32 nTemp = nEndIndex - 1;
710 0 : bool bEnd = mpImpl->RemoveLineBreakCount( nTemp );
711 0 : bool bTemp = mpImpl->RemoveLineBreakCount( nEndIndex );
712 : //if the below condition is true it indicates an empty paragraph with just a "\n"
713 : //so we need to set one "\n" flag to avoid duplication.
714 0 : if ( bStart && bEnd && ( nStartIndex == nEndIndex) )
715 : {
716 0 : bEnd = false;
717 : }
718 : //if the current endindex is at a "\n", we need to increase endindex by 1 to make sure
719 : //the char before "\n" is included. Because string returned by this function will not include
720 : //the char at the endindex.
721 0 : if ( bTemp )
722 : {
723 0 : nEndIndex++;
724 : }
725 0 : OUString aRes;
726 0 : EPosition aStartIndex( mpImpl->Range2Internal(nStartIndex) );
727 0 : EPosition aEndIndex( mpImpl->Range2Internal(nEndIndex) );
728 :
729 : // #102170# Special case: start and end paragraph are identical
730 0 : if( aStartIndex.nPara == aEndIndex.nPara )
731 : {
732 : //we don't return the string directly now for that we have to do some further process for "\n"
733 0 : aRes = mpImpl->GetParagraph( aStartIndex.nPara ).getTextRange( aStartIndex.nIndex, aEndIndex.nIndex );
734 : }
735 : else
736 : {
737 0 : sal_Int32 i( aStartIndex.nPara );
738 0 : aRes = mpImpl->GetParagraph(i).getTextRange( aStartIndex.nIndex,
739 0 : mpImpl->GetParagraph(i).getCharacterCount()/*-1*/);
740 0 : ++i;
741 :
742 : // paragraphs inbetween are fully included
743 0 : for( ; i<aEndIndex.nPara; ++i )
744 : {
745 0 : aRes += OUString(cNewLine);
746 0 : aRes += mpImpl->GetParagraph(i).getText();
747 : }
748 :
749 0 : if( i<=aEndIndex.nPara )
750 : {
751 : //if the below condition is mathed it means the endindex is at mid of the last paragraph
752 : //we need to add a "\n" before we add the last part of the string.
753 0 : if ( !bEnd && aEndIndex.nIndex )
754 : {
755 0 : aRes += OUString(cNewLine);
756 : }
757 0 : aRes += mpImpl->GetParagraph(i).getTextRange( 0, aEndIndex.nIndex );
758 : }
759 : }
760 : //According to the flag we marked before, we have to add "\n" at the beginning
761 : //or at the end of the result string.
762 0 : if ( bStart )
763 : {
764 0 : aRes = OUString(cNewLine) + aRes;
765 : }
766 0 : if ( bEnd )
767 : {
768 0 : aRes += OUString(cNewLine);
769 : }
770 0 : return aRes;
771 : }
772 :
773 0 : ::com::sun::star::accessibility::TextSegment SAL_CALL AccessibleStaticTextBase::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
774 : {
775 0 : SolarMutexGuard aGuard;
776 :
777 0 : bool bLineBreak = mpImpl->RemoveLineBreakCount( nIndex );
778 0 : EPosition aPos( mpImpl->Range2Internal(nIndex) );
779 :
780 0 : ::com::sun::star::accessibility::TextSegment aResult;
781 :
782 0 : if( AccessibleTextType::PARAGRAPH == aTextType )
783 : {
784 : // #106393# Special casing one behind last paragraph is
785 : // not necessary, since then, we return the content and
786 : // boundary of that last paragraph. Range2Internal is
787 : // tolerant against that, and returns the last paragraph
788 : // in aPos.nPara.
789 :
790 : // retrieve full text of the paragraph
791 0 : aResult.SegmentText = mpImpl->GetParagraph( aPos.nPara ).getText();
792 :
793 : // #112814# Adapt the start index with the paragraph offset
794 0 : aResult.SegmentStart = mpImpl->Internal2Index( EPosition( aPos.nPara, 0 ) );
795 0 : aResult.SegmentEnd = aResult.SegmentStart + aResult.SegmentText.getLength();
796 : }
797 0 : else if ( AccessibleTextType::ATTRIBUTE_RUN == aTextType )
798 : {
799 0 : SvxAccessibleTextAdapter& rTextForwarder = mpImpl->GetParagraph( aPos.nIndex ).GetTextForwarder();
800 : sal_Int32 nStartIndex, nEndIndex;
801 0 : if ( rTextForwarder.GetAttributeRun( nStartIndex, nEndIndex, aPos.nPara, aPos.nIndex, true ) )
802 : {
803 0 : aResult.SegmentText = getTextRange( nStartIndex, nEndIndex );
804 0 : aResult.SegmentStart = nStartIndex;
805 0 : aResult.SegmentEnd = nEndIndex;
806 : }
807 : }
808 : else
809 : {
810 : // No special handling required, forward to wrapped class
811 0 : aResult = mpImpl->GetParagraph( aPos.nPara ).getTextAtIndex( aPos.nIndex, aTextType );
812 :
813 : // #112814# Adapt the start index with the paragraph offset
814 0 : mpImpl->CorrectTextSegment( aResult, aPos.nPara );
815 0 : if ( bLineBreak )
816 : {
817 0 : aResult.SegmentText = OUString(cNewLine);
818 : }
819 : }
820 :
821 0 : return aResult;
822 : }
823 :
824 0 : ::com::sun::star::accessibility::TextSegment SAL_CALL AccessibleStaticTextBase::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
825 : {
826 0 : SolarMutexGuard aGuard;
827 :
828 0 : sal_Int32 nOldIdx = nIndex;
829 0 : bool bLineBreak = mpImpl->RemoveLineBreakCount( nIndex );
830 0 : EPosition aPos( mpImpl->Range2Internal(nIndex) );
831 :
832 0 : ::com::sun::star::accessibility::TextSegment aResult;
833 :
834 0 : if( AccessibleTextType::PARAGRAPH == aTextType )
835 : {
836 0 : if( aPos.nIndex == mpImpl->GetParagraph( aPos.nPara ).getCharacterCount() )
837 : {
838 : // #103589# Special casing one behind the last paragraph
839 0 : aResult.SegmentText = mpImpl->GetParagraph( aPos.nPara ).getText();
840 :
841 : // #112814# Adapt the start index with the paragraph offset
842 0 : aResult.SegmentStart = mpImpl->Internal2Index( EPosition( aPos.nPara, 0 ) );
843 : }
844 0 : else if( aPos.nPara > 0 )
845 : {
846 0 : aResult.SegmentText = mpImpl->GetParagraph( aPos.nPara - 1 ).getText();
847 :
848 : // #112814# Adapt the start index with the paragraph offset
849 0 : aResult.SegmentStart = mpImpl->Internal2Index( EPosition( aPos.nPara - 1, 0 ) );
850 : }
851 :
852 0 : aResult.SegmentEnd = aResult.SegmentStart + aResult.SegmentText.getLength();
853 : }
854 : else
855 : {
856 : // No special handling required, forward to wrapped class
857 0 : aResult = mpImpl->GetParagraph( aPos.nPara ).getTextBeforeIndex( aPos.nIndex, aTextType );
858 :
859 : // #112814# Adapt the start index with the paragraph offset
860 0 : mpImpl->CorrectTextSegment( aResult, aPos.nPara );
861 0 : if ( bLineBreak && (nOldIdx-1) >= 0)
862 : {
863 0 : aResult = getTextAtIndex( nOldIdx-1, aTextType );
864 : }
865 : }
866 :
867 0 : return aResult;
868 : }
869 :
870 0 : ::com::sun::star::accessibility::TextSegment SAL_CALL AccessibleStaticTextBase::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
871 : {
872 0 : SolarMutexGuard aGuard;
873 :
874 0 : sal_Int32 nTemp = nIndex+1;
875 0 : bool bLineBreak = mpImpl->RemoveLineBreakCount( nTemp );
876 0 : mpImpl->RemoveLineBreakCount( nIndex );
877 0 : EPosition aPos( mpImpl->Range2Internal(nIndex) );
878 :
879 0 : ::com::sun::star::accessibility::TextSegment aResult;
880 :
881 0 : if( AccessibleTextType::PARAGRAPH == aTextType )
882 : {
883 : // Special casing one behind the last paragraph is not
884 : // necessary, this case is invalid here for
885 : // getTextBehindIndex
886 0 : if( aPos.nPara + 1 < mpImpl->GetParagraphCount() )
887 : {
888 0 : aResult.SegmentText = mpImpl->GetParagraph( aPos.nPara + 1 ).getText();
889 :
890 : // #112814# Adapt the start index with the paragraph offset
891 0 : aResult.SegmentStart = mpImpl->Internal2Index( EPosition( aPos.nPara + 1, 0 ) );
892 0 : aResult.SegmentEnd = aResult.SegmentStart + aResult.SegmentText.getLength();
893 : }
894 : }
895 : else
896 : {
897 : // No special handling required, forward to wrapped class
898 0 : aResult = mpImpl->GetParagraph( aPos.nPara ).getTextBehindIndex( aPos.nIndex, aTextType );
899 :
900 : // #112814# Adapt the start index with the paragraph offset
901 0 : mpImpl->CorrectTextSegment( aResult, aPos.nPara );
902 0 : if ( bLineBreak )
903 : {
904 0 : aResult.SegmentText = OUString(cNewLine) + aResult.SegmentText;
905 : }
906 : }
907 :
908 0 : return aResult;
909 : }
910 :
911 0 : sal_Bool SAL_CALL AccessibleStaticTextBase::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
912 : {
913 0 : SolarMutexGuard aGuard;
914 :
915 0 : if( nStartIndex > nEndIndex )
916 0 : ::std::swap(nStartIndex, nEndIndex);
917 :
918 0 : EPosition aStartIndex( mpImpl->Range2Internal(nStartIndex) );
919 0 : EPosition aEndIndex( mpImpl->Range2Internal(nEndIndex) );
920 :
921 : return mpImpl->CopyText( aStartIndex.nPara, aStartIndex.nIndex,
922 0 : aEndIndex.nPara, aEndIndex.nIndex );
923 : }
924 :
925 : // XAccessibleTextAttributes
926 0 : uno::Sequence< beans::PropertyValue > AccessibleStaticTextBase::getDefaultAttributes( const uno::Sequence< OUString >& RequestedAttributes ) throw (uno::RuntimeException, std::exception)
927 : {
928 : // get the intersection of the default attributes of all paragraphs
929 :
930 0 : SolarMutexGuard aGuard;
931 :
932 : PropertyValueVector aDefAttrVec(
933 0 : comphelper::sequenceToContainer<PropertyValueVector>(mpImpl->GetParagraph( 0 ).getDefaultAttributes( RequestedAttributes )) );
934 :
935 0 : const sal_Int32 nParaCount = mpImpl->GetParagraphCount();
936 0 : for ( sal_Int32 nPara = 1; nPara < nParaCount; ++nPara )
937 : {
938 0 : uno::Sequence< beans::PropertyValue > aSeq = mpImpl->GetParagraph( nPara ).getDefaultAttributes( RequestedAttributes );
939 0 : PropertyValueVector aIntersectionVec;
940 :
941 0 : PropertyValueVector::const_iterator aEnd = aDefAttrVec.end();
942 0 : for ( PropertyValueVector::const_iterator aItr = aDefAttrVec.begin(); aItr != aEnd; ++aItr )
943 : {
944 0 : const beans::PropertyValue* pItr = aSeq.getConstArray();
945 0 : const beans::PropertyValue* pEnd = pItr + aSeq.getLength();
946 0 : const beans::PropertyValue* pFind = ::std::find_if( pItr, pEnd, ::std::bind2nd( PropertyValueEqualFunctor(), boost::cref( *aItr ) ) );
947 0 : if ( pFind != pEnd )
948 : {
949 0 : aIntersectionVec.push_back( *pFind );
950 : }
951 : }
952 :
953 0 : aDefAttrVec.swap( aIntersectionVec );
954 :
955 0 : if ( aDefAttrVec.empty() )
956 : {
957 0 : break;
958 : }
959 0 : }
960 :
961 0 : return comphelper::containerToSequence(aDefAttrVec);
962 : }
963 :
964 0 : uno::Sequence< beans::PropertyValue > SAL_CALL AccessibleStaticTextBase::getRunAttributes( sal_Int32 nIndex, const uno::Sequence< OUString >& RequestedAttributes ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
965 : {
966 : // get those default attributes of the paragraph, which are not part
967 : // of the intersection of all paragraphs and add them to the run attributes
968 :
969 0 : SolarMutexGuard aGuard;
970 :
971 0 : EPosition aPos( mpImpl->Index2Internal( nIndex ) );
972 0 : AccessibleEditableTextPara& rPara = mpImpl->GetParagraph( aPos.nPara );
973 0 : uno::Sequence< beans::PropertyValue > aDefAttrSeq = rPara.getDefaultAttributes( RequestedAttributes );
974 0 : uno::Sequence< beans::PropertyValue > aRunAttrSeq = rPara.getRunAttributes( aPos.nIndex, RequestedAttributes );
975 0 : uno::Sequence< beans::PropertyValue > aIntersectionSeq = getDefaultAttributes( RequestedAttributes );
976 0 : PropertyValueVector aDiffVec;
977 :
978 0 : const beans::PropertyValue* pDefAttr = aDefAttrSeq.getConstArray();
979 0 : const sal_Int32 nLength = aDefAttrSeq.getLength();
980 0 : for ( sal_Int32 i = 0; i < nLength; ++i )
981 : {
982 0 : const beans::PropertyValue* pItr = aIntersectionSeq.getConstArray();
983 0 : const beans::PropertyValue* pEnd = pItr + aIntersectionSeq.getLength();
984 0 : bool bNone = ::std::none_of( pItr, pEnd, ::std::bind2nd( PropertyValueEqualFunctor(), boost::cref( pDefAttr[i] ) ) );
985 0 : if ( bNone && pDefAttr[i].Handle != 0)
986 : {
987 0 : aDiffVec.push_back( pDefAttr[i] );
988 : }
989 : }
990 :
991 0 : return ::comphelper::concatSequences( aRunAttrSeq, comphelper::containerToSequence(aDiffVec) );
992 : }
993 :
994 0 : Rectangle AccessibleStaticTextBase::GetParagraphBoundingBox() const
995 : {
996 0 : return mpImpl->GetParagraphBoundingBox();
997 : }
998 :
999 : } // end of namespace accessibility
1000 :
1001 :
1002 :
1003 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|