1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: textmarkuphelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sw.hxx"
33 #include <textmarkuphelper.hxx>
34 #include <accportions.hxx>
38 #include <comphelper/stlunosequence.hxx>
42 #include <com/sun/star/text/TextMarkupType.hpp>
43 #include <com/sun/star/accessibility/TextSegment.hpp>
48 using namespace com::sun::star
;
52 const SwWrongList
* getTextMarkupList( const SwTxtNode
& rTxtNode
,
53 const sal_Int32 nTextMarkupType
)
54 throw (::com::sun::star::lang::IllegalArgumentException
,
55 ::com::sun::star::uno::RuntimeException
)
57 const SwWrongList
* pTextMarkupList( 0 );
58 switch ( nTextMarkupType
)
60 case text::TextMarkupType::SPELLCHECK
:
62 pTextMarkupList
= rTxtNode
.GetWrong();
65 case text::TextMarkupType::PROOFREADING
:
67 // support not implemented yet
71 case text::TextMarkupType::SMARTTAG
:
73 // support not implemented yet
79 throw lang::IllegalArgumentException();
83 return pTextMarkupList
;
87 // implementation of calss <SwTextMarkupoHelper>
88 SwTextMarkupHelper::SwTextMarkupHelper( const SwTxtNode
& rTxtNode
,
89 const SwAccessiblePortionData
& rPortionData
)
90 : mrTxtNode( rTxtNode
),
91 mrPortionData( rPortionData
)
95 sal_Int32
SwTextMarkupHelper::getTextMarkupCount( sal_Int32 nTextMarkupType
)
96 throw (::com::sun::star::lang::IllegalArgumentException
,
97 ::com::sun::star::uno::RuntimeException
)
99 sal_Int32
nTextMarkupCount( 0 );
101 const SwWrongList
* pTextMarkupList
=
102 getTextMarkupList( mrTxtNode
, nTextMarkupType
);
103 if ( pTextMarkupList
)
105 nTextMarkupCount
= pTextMarkupList
->Count();
108 return nTextMarkupCount
;
110 ::com::sun::star::accessibility::TextSegment
111 SwTextMarkupHelper::getTextMarkup( sal_Int32 nTextMarkupIndex
,
112 sal_Int32 nTextMarkupType
)
113 throw (::com::sun::star::lang::IndexOutOfBoundsException
,
114 ::com::sun::star::lang::IllegalArgumentException
,
115 ::com::sun::star::uno::RuntimeException
)
117 if ( nTextMarkupIndex
>= getTextMarkupCount( nTextMarkupType
) ||
118 nTextMarkupIndex
< 0 )
120 throw lang::IndexOutOfBoundsException();
123 ::com::sun::star::accessibility::TextSegment aTextMarkupSegment
;
124 aTextMarkupSegment
.SegmentStart
= -1;
125 aTextMarkupSegment
.SegmentEnd
= -1;
127 const SwWrongList
* pTextMarkupList
=
128 getTextMarkupList( mrTxtNode
, nTextMarkupType
);
129 if ( pTextMarkupList
)
131 const SwWrongArea
* pTextMarkup
=
132 pTextMarkupList
->GetElement( static_cast<USHORT
>(nTextMarkupIndex
) );
135 const ::rtl::OUString rText
= mrPortionData
.GetAccessibleString();
136 const sal_Int32 nStartPos
=
137 mrPortionData
.GetAccessiblePosition( pTextMarkup
->mnPos
);
138 const sal_Int32 nEndPos
=
139 mrPortionData
.GetAccessiblePosition( pTextMarkup
->mnPos
+ pTextMarkup
->mnLen
);
140 aTextMarkupSegment
.SegmentText
= rText
.copy( nStartPos
, nEndPos
- nStartPos
);
141 aTextMarkupSegment
.SegmentStart
= nStartPos
;
142 aTextMarkupSegment
.SegmentEnd
= nEndPos
;
147 "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
151 return aTextMarkupSegment
;
154 ::com::sun::star::uno::Sequence
< ::com::sun::star::accessibility::TextSegment
>
155 SwTextMarkupHelper::getTextMarkupAtIndex( sal_Int32 nCharIndex
,
156 sal_Int32 nTextMarkupType
)
157 throw (::com::sun::star::lang::IndexOutOfBoundsException
,
158 ::com::sun::star::lang::IllegalArgumentException
,
159 ::com::sun::star::uno::RuntimeException
)
162 // value of <nCharIndex> is in range [0..length of accessible text)
164 const USHORT nCoreCharIndex
= mrPortionData
.GetModelPosition( nCharIndex
);
165 // Handling of portions with core length == 0 at the beginning of the
166 // paragraph - e.g. numbering portion.
167 if ( mrPortionData
.GetAccessiblePosition( nCoreCharIndex
) > nCharIndex
)
169 return uno::Sequence
< ::com::sun::star::accessibility::TextSegment
>();
172 ::std::vector
< ::com::sun::star::accessibility::TextSegment
> aTmpTextMarkups
;
173 const SwWrongList
* pTextMarkupList
=
174 getTextMarkupList( mrTxtNode
, nTextMarkupType
);
175 if ( pTextMarkupList
)
177 const ::rtl::OUString rText
= mrPortionData
.GetAccessibleString();
179 const USHORT nTextMarkupCount
= pTextMarkupList
->Count();
180 for ( USHORT nTextMarkupIdx
= 0; nTextMarkupIdx
< nTextMarkupCount
; ++nTextMarkupIdx
)
182 const SwWrongArea
* pTextMarkup
=
183 pTextMarkupList
->GetElement( static_cast<USHORT
>(nTextMarkupIdx
) );
185 "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
187 pTextMarkup
->mnPos
<= nCoreCharIndex
&&
188 nCoreCharIndex
< ( pTextMarkup
->mnPos
+ pTextMarkup
->mnLen
) )
190 const sal_Int32 nStartPos
=
191 mrPortionData
.GetAccessiblePosition( pTextMarkup
->mnPos
);
192 const sal_Int32 nEndPos
=
193 mrPortionData
.GetAccessiblePosition( pTextMarkup
->mnPos
+ pTextMarkup
->mnLen
);
194 ::com::sun::star::accessibility::TextSegment aTextMarkupSegment
;
195 aTextMarkupSegment
.SegmentText
= rText
.copy( nStartPos
, nEndPos
- nStartPos
);
196 aTextMarkupSegment
.SegmentStart
= nStartPos
;
197 aTextMarkupSegment
.SegmentEnd
= nEndPos
;
198 aTmpTextMarkups
.push_back( aTextMarkupSegment
);
203 uno::Sequence
< ::com::sun::star::accessibility::TextSegment
> aTextMarkups(
204 aTmpTextMarkups
.size() );
205 ::std::copy( aTmpTextMarkups
.begin(), aTmpTextMarkups
.end(),
206 ::comphelper::stl_begin( aTextMarkups
) );