summaryrefslogtreecommitdiff
path: root/sw/source/core/access/accpara.cxx
blob: da6a6aa96785d98e599a2ebdf6c5bee8b5819a99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
 /*************************************************************************
 *
 *  $RCSfile: accpara.cxx,v $
 *
 *  $Revision: 1.14 $
 *
 *  last change: $Author: mib $ $Date: 2002-03-08 13:26:29 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifdef PRECOMPILED
#include "core_pch.hxx"
#endif
#ifndef _TXTFRM_HXX
#include <txtfrm.hxx>
#endif
#ifndef _NDTXT_HXX
#include <ndtxt.hxx>
#endif
#ifndef _PAM_HXX
#include <pam.hxx>
#endif
#ifndef _UNOOBJ_HXX
#include <unoobj.hxx>
#endif
#ifndef _CRSTATE_HXX
#include <crstate.hxx>
#endif
#ifndef _ACCMAP_HXX
#include <accmap.hxx>
#endif
#ifndef _CRSRSH_HXX
#include <crsrsh.hxx>
#endif


#pragma hdrstop

#ifndef _VOS_MUTEX_HXX_ //autogen
#include <vos/mutex.hxx>
#endif
#ifndef _SV_SVAPP_HXX //autogen
#include <vcl/svapp.hxx>
#endif
#ifndef _SV_WINDOW_HXX
#include <vcl/window.hxx>
#endif
#ifndef _RTL_USTRBUF_HXX_ 
#include <rtl/ustrbuf.hxx>
#endif

#ifndef _DRAFTS_COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLEROLE_HPP_
#include <drafts/com/sun/star/accessibility/AccessibleRole.hpp>
#endif
#ifndef _DRAFTS_COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLESTATETYPE_HPP_
#include <drafts/com/sun/star/accessibility/AccessibleStateType.hpp>
#endif
#ifndef _DRAFTS_COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLETEXTTYPE_HPP_
#include <drafts/com/sun/star/accessibility/AccessibleTextType.hpp>
#endif
#ifndef _DRAFTS_COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLEEVENTID_HPP_
#include <drafts/com/sun/star/accessibility/AccessibleEventId.hpp>
#endif

#ifndef _UTL_ACCESSIBLESTATESETHELPER_HXX_
#include <unotools/accessiblestatesethelper.hxx>
#endif
#ifndef _COM_SUN_STAR_I18N_WORDTYPE_HPP_
#include <com/sun/star/i18n/WordType.hpp>
#endif
#ifndef _COM_SUN_STAR_I18N_XBREAKITERATOR_HPP_
#include <com/sun/star/i18n/XBreakIterator.hpp>
#endif
#ifndef _COM_SUN_STAR_UNO_RUNTIMEEXCEPTION_HPP_
#include <com/sun/star/uno/RuntimeException.hpp>
#endif
#ifndef _COM_SUN_STAR_LANG_INDEXOUTOFBOUNDSEXCEPTION_HPP_
#include <com/sun/star/lang/INDEXOUTOFBOUNDSEXCEPTION.hpp>
#endif

#ifndef _BREAKIT_HXX
#include <breakit.hxx>
#endif

#ifndef _ACCPARA_HXX
#include "accpara.hxx"
#endif
#ifndef _ACCESS_HRC
#include "access.hrc"
#endif
#ifndef _ACCPORTIONS_HXX
#include "accportions.hxx"
#endif

using namespace ::com::sun::star::i18n;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::drafts::com::sun::star::accessibility;
using namespace ::rtl;

using ::com::sun::star::beans::PropertyValue;


const sal_Char sServiceName[] = "com.sun.star.text.AccessibleParagraphView";
const sal_Char sImplementationName[] = "SwAccessibleParagraph";
const xub_StrLen MAX_DESC_TEXT_LEN = 40;
const SwTxtNode* SwAccessibleParagraph::GetTxtNode() const
{
    const SwFrm* pFrm = GetFrm();
    DBG_ASSERT( pFrm->IsTxtFrm(), "The text frame has mutated!" );
    
    const SwTxtNode* pNode = static_cast<const SwTxtFrm*>(pFrm)->GetTxtNode();
    DBG_ASSERT( pNode != NULL, "A text frame without a text node." );

    return pNode;
}

OUString SwAccessibleParagraph::GetString()
{
    return GetPortionData().GetAccessibleString();
}

OUString SwAccessibleParagraph::GetDescription()
{
    const OUString& rText = GetString();

    // the description contains the first sentence up to
    // MAX_DESC_TEXT_LEN characters (including the next full word)
    Boundary aBound;
    if( rText.getLength() > 0 )
    {
        GetSentenceBoundary( aBound, rText, 0 );
        if( aBound.endPos > MAX_DESC_TEXT_LEN )
        {
            GetWordBoundary( aBound, rText, MAX_DESC_TEXT_LEN );
            aBound.startPos = 0;
        }
    }
    else
        GetEmptyBoundary( aBound );
    OUString sArg1( rText.copy( aBound.startPos, aBound.endPos ) );

    sal_Int16 nResId;
    if( IsHeading() )
    {
        nResId = STR_ACCESS_HEADING_DESC;
    }
    else
    {
        nResId = STR_ACCESS_PARAGRAPH_DESC;
    }

    return GetResource( nResId, &sArg1 );
}

sal_Bool SwAccessibleParagraph::GetSelection(
    sal_Int32& nStart, sal_Int32& nEnd)
{
    sal_Bool bRet = sal_False;
    nStart = -1;
    nEnd = -1;

    // get the selection, and test whether it affects our text node
    SwPaM* pCrsr = GetCrsr();
    if( pCrsr != NULL )
    {
        // get SwPosition for my node
        const SwTxtNode* pNode = GetTxtNode();
        ULONG nHere = pNode->GetIndex();

        // iterate over ring
        SwPaM* pRingStart = pCrsr;
        do
        {
            // ignore, if no mark
            if( pCrsr->HasMark() )
            {
                // check whether nHere is 'inside' pCrsr
                SwPosition* pStart = pCrsr->Start();
                SwPosition* pEnd = pCrsr->End();
                if( ( nHere >= pStart->nNode.GetIndex() ) &&
                    ( nHere <= pEnd->nNode.GetIndex() )      )
                {
                    // Yup, we are selected!
                    bRet = sal_True;
                    nStart = static_cast<sal_Int32>( 
                        ( nHere > pStart->nNode.GetIndex() ) ? 0 
                        : pStart->nContent.GetIndex() );
                    nEnd = static_cast<sal_Int32>( 
                        ( nHere < pEnd->nNode.GetIndex() ) ? pNode->Len()
                        : pEnd->nContent.GetIndex() );
                }
                // else: this PaM doesn't point to this paragraph
            }
            // else: this PaM is collapsed and doesn't select anything

            // next PaM in ring
            pCrsr = static_cast<SwPaM*>( pCrsr->GetNext() );
        }
        while( !bRet && (pCrsr != pRingStart) );
    }
    // else: nocursor -> no selection

    return bRet;
}

SwPaM* SwAccessibleParagraph::GetCrsr()
{
    // get the cursor shell; if we don't have any, we don't have a
    // cursor/selection either
    SwPaM* pCrsr = NULL;
    SwCrsrShell* pCrsrShell = SwAccessibleParagraph::GetCrsrShell();
    if( pCrsrShell != NULL )
    {
        // get the selection, and test whether it affects our text node
        pCrsr = pCrsrShell->GetCrsr( FALSE /* ??? */ );
    }

    return pCrsr;
}

SwCrsrShell* SwAccessibleParagraph::GetCrsrShell()
{
    // first, get the view shell
    DBG_ASSERT( GetMap() != NULL, "no map?" );
    ViewShell* pViewShell = GetMap()->GetShell();
    DBG_ASSERT( pViewShell != NULL, 
                "No view shell? Then what are you looking at?" );

    SwCrsrShell* pCrsrShell = NULL;

    // see if our view shell is a cursor shell
    if( pViewShell->ISA( SwCrsrShell ) )
    {
        pCrsrShell = static_cast<SwCrsrShell*>( pViewShell );
    }
    
    return pCrsrShell;
}

sal_Bool SwAccessibleParagraph::IsHeading() const
{
    const SwTxtNode *pTxtNd = GetTxtNode();
    return (pTxtNd->GetOutlineNum() && !pTxtNd->GetNum());
}

void SwAccessibleParagraph::SetStates(
        ::utl::AccessibleStateSetHelper& rStateSet )
{
    SwAccessibleContext::SetStates( rStateSet );

    // MULTILINE
    rStateSet.AddState( AccessibleStateType::MULTILINE );

    // SELECTABLE
    rStateSet.AddState( AccessibleStateType::SELECTABLE );

    // TODO: SELECTED
}

void SwAccessibleParagraph::_InvalidateContent( sal_Bool bVisibleDataFired )
{
    OUString sOldText( GetString() );

    ClearPortionData();

    const OUString& rText = GetString();

    if( rText != sOldText )
    {
        // The text is changed
        AccessibleEventObject aEvent;
        aEvent.EventId = AccessibleEventId::ACCESSIBLE_TEXT_EVENT;

        FireAccessibleEvent( aEvent );
    }
    else if( !bVisibleDataFired )
    {
        FireVisibleDataEvent();
    }
    
    sal_Bool bIsNowHeading = IsHeading();
    if( bIsNowHeading != bIsHeading || rText != sOldText )
    {
        bIsHeading = bIsNowHeading;

        OUString sNewDesc( GetDescription() );
        if( sNewDesc != sDesc )
        {
            // The text is changed
            AccessibleEventObject aEvent;
            aEvent.EventId = AccessibleEventId::ACCESSIBLE_DESCRIPTION_EVENT;
            aEvent.OldValue <<= sDesc;
            aEvent.NewValue <<= sNewDesc;
            sDesc = sNewDesc;

            FireAccessibleEvent( aEvent );
        }
    }
}


SwAccessibleParagraph::SwAccessibleParagraph( 
        SwAccessibleMap *pMap,
        sal_Int32 nPara,
        const SwTxtFrm *pTxtFrm	) :
    SwAccessibleContext( pMap, AccessibleRole::PARAGRAPH, pTxtFrm ),
    pPortionData( NULL )
{
    vos::OGuard aGuard(Application::GetSolarMutex());

    bIsHeading = IsHeading();
    sal_uInt16 nResId = bIsHeading ? STR_ACCESS_HEADING_NAME
                                   : STR_ACCESS_PARAGRAPH_NAME;
    OUString sArg( OUString::valueOf( nPara ) );
    SetName( GetResource( nResId, &sArg ) );
}

SwAccessibleParagraph::~SwAccessibleParagraph()
{
    delete pPortionData;
}

void SwAccessibleParagraph::UpdatePortionData()
    throw( RuntimeException )
{
    // obtain the text frame
    DBG_ASSERT( GetFrm() != NULL, "The text frame has vanished!" );
    DBG_ASSERT( GetFrm()->IsTxtFrm(), "The text frame has mutated!" );
    const SwTxtFrm* pFrm = static_cast<const SwTxtFrm*>( GetFrm() );

    // obtain the model string via the text node
    const String& rModelString = pFrm->GetTxtNode()->GetTxt();

    // build new portion data
    delete pPortionData;
    pPortionData = new SwAccessiblePortionData( rModelString );
    pFrm->VisitPortions( *pPortionData );

    DBG_ASSERT( pPortionData != NULL, "UpdatePortionData() failed" );
}


// text boundaries

void SwAccessibleParagraph::ClearPortionData()
{ 
    delete pPortionData; 
    pPortionData = NULL; 
}


void SwAccessibleParagraph::GetCharBoundary( 
    Boundary& rBound, 
    const OUString& rText,
    sal_Int32 nPos )
{
    rBound.startPos = nPos;
    rBound.endPos = nPos+1;
}

void SwAccessibleParagraph::GetWordBoundary(
    Boundary& rBound, 
    const OUString& rText,
    sal_Int32 nPos )
{
    GetPortionData().GetWordBoundary( rBound, nPos, GetTxtNode() );
}

void SwAccessibleParagraph::GetSentenceBoundary(
    Boundary& rBound, 
    const OUString& rText,
    sal_Int32 nPos )
{
    GetPortionData().GetSentenceBoundary( rBound, nPos, GetTxtNode() );
}

void SwAccessibleParagraph::GetLineBoundary(
    Boundary& rBound, 
    const OUString& rText,
    sal_Int32 nPos )
{
    GetPortionData().GetLineBoundary( rBound, nPos );
}

void SwAccessibleParagraph::GetParagraphBoundary(
    Boundary& rBound, 
    const OUString& rText,
    sal_Int32 nPos )
{
    rBound.startPos = 0;
    rBound.endPos = rText.getLength();
}

void SwAccessibleParagraph::GetEmptyBoundary( Boundary& rBound )
{
    rBound.startPos = 0;
    rBound.endPos = 0;
}


void SwAccessibleParagraph::GetTextBoundary( 
    Boundary& rBound, 
    const OUString& rText,
    sal_Int32 nPos,
    sal_Int16 nTextType )
    throw (
        IndexOutOfBoundsException, 
        RuntimeException)
{
    if( (nPos < 0) || (nPos >= rText.getLength()) )
        throw IndexOutOfBoundsException();

    switch( nTextType )
    {
        case AccessibleTextType::WORD:
            GetWordBoundary( rBound, rText, nPos );
            break;

        case AccessibleTextType::SENTENCE:
            GetSentenceBoundary( rBound, rText, nPos );
            break;

        case AccessibleTextType::PARAGRAPH:
            GetParagraphBoundary( rBound, rText, nPos );
            break;

        case AccessibleTextType::CHARACTER:
            GetCharBoundary( rBound, rText, nPos );
            break;

        case AccessibleTextType::LINE:
            GetLineBoundary( rBound, rText, nPos );
            break;

        default:
            // The specification asks us to return an empty string 
            // if the text type is no valid.
            GetEmptyBoundary( rBound );
            break;
    }
}

OUString SAL_CALL SwAccessibleParagraph::getAccessibleDescription (void)
        throw (::com::sun::star::uno::RuntimeException)
{
    vos::OGuard aGuard(Application::GetSolarMutex());

    CHECK_FOR_DEFUNC( XAccessibleContext );

    if( !sDesc.getLength() )
        sDesc = GetDescription();

    return sDesc;
}

Locale SAL_CALL SwAccessibleParagraph::getLocale (void)
        throw (::drafts::com::sun::star::accessibility::IllegalAccessibleComponentStateException, ::com::sun::star::uno::RuntimeException)
{
    vos::OGuard aGuard(Application::GetSolarMutex());

    SwTxtFrm *pTxtFrm = PTR_CAST( SwTxtFrm, GetFrm() );
    if( !pTxtFrm )
    {
        THROW_RUNTIME_EXCEPTION( XAccessibleContext, "internal error (no text frame)" );
    }

    const SwTxtNode *pTxtNd = pTxtFrm->GetTxtNode();
    Locale aLoc( pBreakIt->GetLocale( pTxtNd->GetLang( 0 ) ) );

    return aLoc;
}

OUString SAL_CALL SwAccessibleParagraph::getImplementationName()
        throw( RuntimeException )
{
    return OUString(RTL_CONSTASCII_USTRINGPARAM(sImplementationName));
}

sal_Bool SAL_CALL SwAccessibleParagraph::supportsService(
        const ::rtl::OUString& sTestServiceName)
    throw (::com::sun::star::uno::RuntimeException)
{
    return sTestServiceName.equalsAsciiL( sServiceName, sizeof(sServiceName)-1 );
}

Sequence< OUString > SAL_CALL SwAccessibleParagraph::getSupportedServiceNames()
        throw( ::com::sun::star::uno::RuntimeException )
{
    Sequence< OUString > aRet(1);
    OUString* pArray = aRet.getArray();
    pArray[0] = OUString( RTL_CONSTASCII_USTRINGPARAM(sServiceName) );
    return aRet;
}

//
//=====  XInterface  =======================================================
//

Any SwAccessibleParagraph::queryInterface( const Type& rType ) 
    throw (RuntimeException)
{
    Any aRet;
    if ( rType == ::getCppuType((Reference<XAccessibleText> *)0) )
    {
        Reference<XAccessibleText> aAccText = this;
        aRet <<= aAccText;
    }
    else if ( rType == ::getCppuType((Reference<XAccessibleEditableText> *)0) )
    {
        Reference<XAccessibleEditableText> aAccEditText = this;
        aRet <<= aAccEditText;
    }
    else
    {
        aRet = SwAccessibleContext::queryInterface(rType);
    }

    return aRet;
}


//
//=====  XAccesibleText  ===================================================
//

sal_Int32 SwAccessibleParagraph::getCaretPosition()
    throw (RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    sal_Int32 nRet = -1;

    // get the selection's point, and test whether it's in our node
    SwPaM* pCaret = GetCrsr();  // caret is first PaM in PaM-ring
    if( pCaret != NULL )
    {
        const SwTxtNode* pNode = GetTxtNode();

        // get SwPosition for my node
        ULONG nHere = pNode->GetIndex();

        // check whether the point points into 'our' node
        SwPosition* pPoint = pCaret->GetPoint();
        if( pNode->GetIndex() == pPoint->nNode.GetIndex() )
        {
            // Yes, it's us!
            nRet = GetPortionData().GetAccessiblePosition( 
                pPoint->nContent.GetIndex() );

            DBG_ASSERT( nRet >= 0, "invalid cursor?" );
            DBG_ASSERT( nRet <= GetPortionData().GetAccessibleString().
                                              getLength(), "invalid cursor?" );
        }
        // else: not in this paragraph
    }
    // else: no cursor -> no caret

    return nRet;
}

sal_Unicode SwAccessibleParagraph::getCharacter( sal_Int32 nIndex )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    OUString sText( GetString() );
    
    if( (nIndex >= 0) && (nIndex < sText.getLength()) )
    {
        return sText.getStr()[nIndex];
    }
    else
        throw IndexOutOfBoundsException();
}

Sequence<PropertyValue> SwAccessibleParagraph::getCharacterAttributes( sal_Int32 nIndex ) 
    throw (IndexOutOfBoundsException, RuntimeException)
{
    // HACK: dummy implementation
    Sequence<PropertyValue> aSeq;
    return aSeq;
}

com::sun::star::awt::Rectangle SwAccessibleParagraph::getCharacterBounds( 
    sal_Int32 nIndex ) 
    throw (IndexOutOfBoundsException, RuntimeException)
{
    vos::OGuard aGuard(Application::GetSolarMutex());
    CHECK_FOR_DEFUNC( XAccessibleContext ); // we have a frame?

    if( (nIndex < 0) || (nIndex >= GetString().getLength()) )
        throw IndexOutOfBoundsException();

    // get model position & prepare GetCharRect() arguments
    SwCrsrMoveState aMoveState;
    aMoveState.bRealHeight = TRUE;
    aMoveState.bRealWidth = TRUE;
    SwSpecialPos aSpecialPos;
    USHORT nPos = GetPortionData().FillSpecialPos( 
        nIndex, aSpecialPos, aMoveState.pSpecialPos );

    // call GetCharRect
    SwRect aCoreRect;
    SwTxtNode* pNode = const_cast<SwTxtNode*>( GetTxtNode() );
    SwIndex aIndex( pNode, nPos );
    SwPosition aPosition( *pNode, aIndex );
    GetFrm()->GetCharRect( aCoreRect, aPosition, &aMoveState );

    // translate core coordinates into accessibility coordinates
    Window *pWin = GetWindow();
    CHECK_FOR_WINDOW( XAccessibleComponent, pWin );
    aCoreRect -= GetBounds().TopLeft();
    MapMode aMapMode = pWin->GetMapMode();
    aMapMode.SetOrigin( Point() );
    Rectangle aScreenRect( pWin->LogicToPixel( aCoreRect.SVRect(), aMapMode ));

    // convert into AWT Rectangle
    return com::sun::star::awt::Rectangle( 
        aScreenRect.Left(), aScreenRect.Top(), 
        aScreenRect.GetWidth(), aScreenRect.GetHeight() );
}

sal_Int32 SwAccessibleParagraph::getCharacterCount()
    throw (RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    return GetString().getLength();
}

sal_Int32 SwAccessibleParagraph::getIndexAtPoint( const com::sun::star::awt::Point& rPoint )
    throw (RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());

    // construct SwPosition (where GetCrsrOfst() will put the result into)
    SwTxtNode* pNode = const_cast<SwTxtNode*>( GetTxtNode() );
    SwIndex aIndex( pNode, 0);
    SwPosition aPos( *pNode, aIndex );

    // construct Point (translate into layout coordinates)
    Window *pWin = GetWindow();
    CHECK_FOR_WINDOW( XAccessibleComponent, pWin );
    Point aPoint( rPoint.X, rPoint.Y );
    MapMode aMapMode = pWin->GetMapMode();
    aMapMode.SetOrigin( Point() );
    Point aCorePoint = pWin->PixelToLogic( aPoint, aMapMode );
    aCorePoint += GetBounds().TopLeft();

    // ask core for position
    DBG_ASSERT( GetFrm() != NULL, "The text frame has vanished!" );
    DBG_ASSERT( GetFrm()->IsTxtFrm(), "The text frame has mutated!" );
    const SwTxtFrm* pFrm = static_cast<const SwTxtFrm*>( GetFrm() );
    sal_Bool bSuccess = pFrm->GetCrsrOfst( &aPos, aCorePoint );

    return bSuccess ? 
        GetPortionData().GetAccessiblePosition( aPos.nContent.GetIndex() ) 
        : 0;
}

OUString SwAccessibleParagraph::getSelectedText() 
    throw (RuntimeException)
{
    vos::OGuard aGuard(Application::GetSolarMutex());
    CHECK_FOR_DEFUNC( XAccessibleContext ); // we have a frame?

    sal_Int32 nStart, nEnd;
    sal_Bool bSelected = GetSelection( nStart, nEnd );
    return bSelected ? GetString().copy( nStart, nEnd - nStart ) : OUString();
}

sal_Int32 SwAccessibleParagraph::getSelectionStart()
    throw (RuntimeException)
{
    vos::OGuard aGuard(Application::GetSolarMutex());
    CHECK_FOR_DEFUNC( XAccessibleContext ); // we have a frame?

    sal_Int32 nStart, nEnd;
    GetSelection( nStart, nEnd );
    return nStart;
}

sal_Int32 SwAccessibleParagraph::getSelectionEnd() 
    throw (RuntimeException)
{
    vos::OGuard aGuard(Application::GetSolarMutex());
    CHECK_FOR_DEFUNC( XAccessibleContext ); // we have a frame?

    sal_Int32 nStart, nEnd;
    GetSelection( nStart, nEnd );
    return nEnd;
}

sal_Bool SwAccessibleParagraph::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());

    // parameter checking
    sal_Int32 nLength = GetString().getLength();
    if ( (nStartIndex <= 0) ||
         (nStartIndex > nLength) ||
         (nEndIndex <= 0) ||
         (nEndIndex > nLength) )
    {
        throw IndexOutOfBoundsException();
    }

    sal_Bool bRet = sal_False;

    // get cursor shell
    SwCrsrShell* pCrsrShell = GetCrsrShell();
    if( pCrsrShell != NULL )
    {
        // create pam for selection
        SwTxtNode* pNode = const_cast<SwTxtNode*>( GetTxtNode() );
        SwIndex aIndex( pNode, GetPortionData().GetModelPosition(nStartIndex));
        SwPosition aStartPos( *pNode, aIndex );
        SwPaM aPaM( aStartPos );
        aPaM.SetMark();
        aPaM.GetPoint()->nContent = 
            GetPortionData().GetModelPosition(nEndIndex);

        // set PaM at cursor shell
        pCrsrShell->KillPams();
        pCrsrShell->SetSelection( aPaM );
        bRet = sal_True;
    }

    return bRet;
}

OUString SwAccessibleParagraph::getText() 
    throw (RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    return GetString();
}

OUString SwAccessibleParagraph::getTextRange( 
    sal_Int32 nStartIndex, sal_Int32 nEndIndex )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    OUString sText( GetString() );

    if ( (nStartIndex <= nEndIndex) &&
         (nStartIndex >= 0) &&
         (nEndIndex < sText.getLength()) )
    {
        return sText.copy(nStartIndex, nEndIndex-nStartIndex+1 );
    }
    else
        throw IndexOutOfBoundsException();
}

OUString SwAccessibleParagraph::getTextAtIndex( 
    sal_Int32 nIndex, sal_Int16 nTextType ) 
    throw (IndexOutOfBoundsException, RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    const OUString rText = GetString();

    Boundary aBound;
    GetTextBoundary( aBound, rText, nIndex, nTextType );

    DBG_ASSERT( aBound.startPos >= 0,               "illegal boundary" );
    DBG_ASSERT( aBound.startPos <= aBound.endPos,   "illegal boundary" );

    return rText.copy( aBound.startPos, aBound.endPos - aBound.startPos );
}

OUString SwAccessibleParagraph::getTextBeforeIndex( 
    sal_Int32 nIndex, sal_Int16 nTextType )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    const OUString rText = GetString();

    Boundary aBound;
    GetTextBoundary( aBound, rText, nIndex, nTextType );
    if( aBound.startPos > 0 )
        GetTextBoundary( aBound, rText, aBound.startPos-1, nTextType );
    else
        GetEmptyBoundary( aBound );

    return rText.copy( aBound.startPos, aBound.endPos - aBound.startPos );
}

OUString SwAccessibleParagraph::getTextBehindIndex( 
    sal_Int32 nIndex, sal_Int16 nTextType )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    CHECK_FOR_DEFUNC( XAccessibleContext );
    vos::OGuard aGuard(Application::GetSolarMutex());
    
    const OUString rText = GetString();

    Boundary aBound;
    GetTextBoundary( aBound, rText, nIndex, nTextType );
    if( aBound.endPos < (rText.getLength()-1) )
        GetTextBoundary( aBound, rText, aBound.endPos+1, nTextType );
    else
        GetEmptyBoundary( aBound );

    return rText.copy( aBound.startPos, aBound.endPos - aBound.startPos );
}

sal_Bool SwAccessibleParagraph::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    // HACK: dummy implementation
    return sal_False;
}


//
//=====  XAccesibleEditableText  ==========================================
//

sal_Bool SwAccessibleParagraph::cutText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    // HACK: dummy implementation
    return sal_False;
}

sal_Bool SwAccessibleParagraph::pasteText( sal_Int32 nIndex )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    // HACK: dummy implementation
    return sal_False;
}

sal_Bool SwAccessibleParagraph::deleteText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) 
    throw (IndexOutOfBoundsException, RuntimeException)
{
    return replaceText( nStartIndex, nEndIndex, OUString() );
}

sal_Bool SwAccessibleParagraph::insertText( const OUString& sText, sal_Int32 nIndex ) 
    throw (IndexOutOfBoundsException, RuntimeException)
{
    return replaceText( nIndex, nIndex, sText );
}

sal_Bool SwAccessibleParagraph::replaceText( 
    sal_Int32 nStartIndex, sal_Int32 nEndIndex, 
    const OUString& sReplacement ) 
    throw (IndexOutOfBoundsException, RuntimeException)
{
    vos::OGuard aGuard(Application::GetSolarMutex());
    CHECK_FOR_DEFUNC( XAccessibleContext );     // check for live frame

    const OUString& rText = GetString();

    if( (nStartIndex >= 0) &&
        (nEndIndex <= rText.getLength()) &&
        (nStartIndex <= nEndIndex) )
    {
        SwTxtNode* pNode = const_cast<SwTxtNode*>( GetTxtNode() );

        // create SwPosition for nStartIndex
        SwIndex aIndex( pNode, GetPortionData().GetModelPosition(nStartIndex));
        SwPosition aStartPos( *pNode, aIndex );

        // create SwPosition for nEndIndex
        SwPosition aEndPos( aStartPos );
        aEndPos.nContent = GetPortionData().GetModelPosition( nEndIndex );

        // now create XTextRange as helper and set string
        SwXTextRange::CreateTextRangeFromPosition(
            pNode->GetDoc(), aStartPos, &aEndPos)->setString( sReplacement );

        // delete portion data
        ClearPortionData();

        return sal_True;    // We always succeed! :-)
    }
    else
        throw IndexOutOfBoundsException();
}

sal_Bool SwAccessibleParagraph::setAttributes( sal_Int32 nStartIndex, sal_Int32 nEndIndex, 
                        const Sequence<PropertyValue>& aAttributeSet )
    throw (IndexOutOfBoundsException, RuntimeException)
{
    // HACK: dummy implementation
    return sal_False;
}

sal_Bool SwAccessibleParagraph::setText( const OUString& sText ) 
    throw (RuntimeException)
{
    return replaceText(0, GetString().getLength(), sText);
}