xref: /AOO41X/main/comphelper/source/misc/accessibletexthelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // includes --------------------------------------------------------------
32*cdf0e10cSrcweir #include <comphelper/accessibletexthelper.hxx>
33*cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleTextType.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
35*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_TEXT_WORDTYPE_HPP_
36*cdf0e10cSrcweir #include <com/sun/star/i18n/WordType.hpp>
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir #include <com/sun/star/i18n/KCharacterType.hpp>
39*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
40*cdf0e10cSrcweir #include <com/sun/star/accessibility/TextSegment.hpp>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <algorithm>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //..............................................................................
45*cdf0e10cSrcweir namespace comphelper
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir //..............................................................................
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 	using namespace ::com::sun::star;
50*cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
51*cdf0e10cSrcweir 	using namespace ::com::sun::star::lang;
52*cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
53*cdf0e10cSrcweir 	using namespace ::com::sun::star::accessibility;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 	//==============================================================================
56*cdf0e10cSrcweir 	// OCommonAccessibleText
57*cdf0e10cSrcweir 	//==============================================================================
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	OCommonAccessibleText::OCommonAccessibleText()
60*cdf0e10cSrcweir 	{
61*cdf0e10cSrcweir 	}
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	OCommonAccessibleText::~OCommonAccessibleText()
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 	}
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	Reference < i18n::XBreakIterator > OCommonAccessibleText::implGetBreakIterator()
72*cdf0e10cSrcweir 	{
73*cdf0e10cSrcweir 		if ( !m_xBreakIter.is() )
74*cdf0e10cSrcweir 		{
75*cdf0e10cSrcweir 			Reference< lang::XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
76*cdf0e10cSrcweir 			if ( xMSF.is() )
77*cdf0e10cSrcweir 			{
78*cdf0e10cSrcweir 				m_xBreakIter = Reference< i18n::XBreakIterator >
79*cdf0e10cSrcweir 					( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.i18n.BreakIterator" ) ) ), UNO_QUERY );
80*cdf0e10cSrcweir 			}
81*cdf0e10cSrcweir 		}
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 		return m_xBreakIter;
84*cdf0e10cSrcweir 	}
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	Reference < i18n::XCharacterClassification > OCommonAccessibleText::implGetCharacterClassification()
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		if ( !m_xCharClass.is() )
91*cdf0e10cSrcweir 		{
92*cdf0e10cSrcweir 			Reference< lang::XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
93*cdf0e10cSrcweir 			if ( xMSF.is() )
94*cdf0e10cSrcweir 			{
95*cdf0e10cSrcweir 				m_xCharClass = Reference< i18n::XCharacterClassification >
96*cdf0e10cSrcweir 					( xMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.i18n.CharacterClassification" ) ) ), UNO_QUERY );
97*cdf0e10cSrcweir 			}
98*cdf0e10cSrcweir 		}
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 		return m_xCharClass;
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	sal_Bool OCommonAccessibleText::implIsValidBoundary( i18n::Boundary& rBoundary, sal_Int32 nLength )
106*cdf0e10cSrcweir 	{
107*cdf0e10cSrcweir 		return ( rBoundary.startPos >= 0 ) && ( rBoundary.startPos < nLength ) && ( rBoundary.endPos >= 0 ) && ( rBoundary.endPos <= nLength );
108*cdf0e10cSrcweir 	}
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	sal_Bool OCommonAccessibleText::implIsValidIndex( sal_Int32 nIndex, sal_Int32 nLength )
113*cdf0e10cSrcweir 	{
114*cdf0e10cSrcweir 		return ( nIndex >= 0 ) && ( nIndex < nLength );
115*cdf0e10cSrcweir 	}
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	sal_Bool OCommonAccessibleText::implIsValidRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex, sal_Int32 nLength )
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		return ( nStartIndex >= 0 ) && ( nStartIndex <= nLength ) && ( nEndIndex >= 0 ) && ( nEndIndex <= nLength );
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	void OCommonAccessibleText::implGetGlyphBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 		if ( implIsValidIndex( nIndex, sText.getLength() ) )
131*cdf0e10cSrcweir 		{
132*cdf0e10cSrcweir 			Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator();
133*cdf0e10cSrcweir 			if ( xBreakIter.is() )
134*cdf0e10cSrcweir 			{
135*cdf0e10cSrcweir 				sal_Int32 nCount = 1;
136*cdf0e10cSrcweir 				sal_Int32 nDone;
137*cdf0e10cSrcweir 				sal_Int32 nStartIndex = xBreakIter->previousCharacters( sText, nIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone );
138*cdf0e10cSrcweir 				if ( nDone != 0 )
139*cdf0e10cSrcweir 					nStartIndex = xBreakIter->nextCharacters( sText, nStartIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone );
140*cdf0e10cSrcweir 				sal_Int32 nEndIndex = xBreakIter->nextCharacters( sText, nStartIndex, implGetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nDone );
141*cdf0e10cSrcweir 				if ( nDone != 0 )
142*cdf0e10cSrcweir 				{
143*cdf0e10cSrcweir 					rBoundary.startPos = nStartIndex;
144*cdf0e10cSrcweir 					rBoundary.endPos = nEndIndex;
145*cdf0e10cSrcweir 				}
146*cdf0e10cSrcweir 			}
147*cdf0e10cSrcweir 		}
148*cdf0e10cSrcweir 		else
149*cdf0e10cSrcweir 		{
150*cdf0e10cSrcweir 			rBoundary.startPos = nIndex;
151*cdf0e10cSrcweir 			rBoundary.endPos = nIndex;
152*cdf0e10cSrcweir 		}
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 	sal_Bool OCommonAccessibleText::implGetWordBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex )
158*cdf0e10cSrcweir 	{
159*cdf0e10cSrcweir 		sal_Bool bWord = sal_False;
160*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 		if ( implIsValidIndex( nIndex, sText.getLength() ) )
163*cdf0e10cSrcweir 		{
164*cdf0e10cSrcweir 			Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator();
165*cdf0e10cSrcweir 			if ( xBreakIter.is() )
166*cdf0e10cSrcweir 			{
167*cdf0e10cSrcweir 				rBoundary = xBreakIter->getWordBoundary( sText, nIndex, implGetLocale(), i18n::WordType::ANY_WORD, sal_True );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 				// it's a word, if the first character is an alpha-numeric character
170*cdf0e10cSrcweir 				Reference< i18n::XCharacterClassification >	xCharClass = implGetCharacterClassification();
171*cdf0e10cSrcweir 				if ( xCharClass.is() )
172*cdf0e10cSrcweir 				{
173*cdf0e10cSrcweir 					sal_Int32 nType = xCharClass->getCharacterType( sText, rBoundary.startPos, implGetLocale() );
174*cdf0e10cSrcweir 					if ( ( nType & ( i18n::KCharacterType::LETTER | i18n::KCharacterType::DIGIT ) ) != 0 )
175*cdf0e10cSrcweir 						bWord = sal_True;
176*cdf0e10cSrcweir 				}
177*cdf0e10cSrcweir 			}
178*cdf0e10cSrcweir 		}
179*cdf0e10cSrcweir 		else
180*cdf0e10cSrcweir 		{
181*cdf0e10cSrcweir 			rBoundary.startPos = nIndex;
182*cdf0e10cSrcweir 			rBoundary.endPos = nIndex;
183*cdf0e10cSrcweir 		}
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 		return bWord;
186*cdf0e10cSrcweir 	}
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	void OCommonAccessibleText::implGetSentenceBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex )
191*cdf0e10cSrcweir 	{
192*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 		if ( implIsValidIndex( nIndex, sText.getLength() ) )
195*cdf0e10cSrcweir 		{
196*cdf0e10cSrcweir 			Locale aLocale = implGetLocale();
197*cdf0e10cSrcweir 			Reference < i18n::XBreakIterator > xBreakIter = implGetBreakIterator();
198*cdf0e10cSrcweir 			if ( xBreakIter.is() )
199*cdf0e10cSrcweir 			{
200*cdf0e10cSrcweir 				rBoundary.endPos = xBreakIter->endOfSentence( sText, nIndex, aLocale );
201*cdf0e10cSrcweir 				rBoundary.startPos = xBreakIter->beginOfSentence( sText, rBoundary.endPos, aLocale );
202*cdf0e10cSrcweir 			}
203*cdf0e10cSrcweir 		}
204*cdf0e10cSrcweir 		else
205*cdf0e10cSrcweir 		{
206*cdf0e10cSrcweir 			rBoundary.startPos = nIndex;
207*cdf0e10cSrcweir 			rBoundary.endPos = nIndex;
208*cdf0e10cSrcweir 		}
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	void OCommonAccessibleText::implGetParagraphBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex )
214*cdf0e10cSrcweir 	{
215*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 		if ( implIsValidIndex( nIndex, sText.getLength() ) )
218*cdf0e10cSrcweir 		{
219*cdf0e10cSrcweir 			rBoundary.startPos = 0;
220*cdf0e10cSrcweir 			rBoundary.endPos = sText.getLength();
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 			sal_Int32 nFound = sText.lastIndexOf( (sal_Unicode)'\n', nIndex );
223*cdf0e10cSrcweir 			if ( nFound != -1 )
224*cdf0e10cSrcweir 				rBoundary.startPos = nFound + 1;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 			nFound = sText.indexOf( (sal_Unicode)'\n', nIndex );
227*cdf0e10cSrcweir 			if ( nFound != -1 )
228*cdf0e10cSrcweir 				rBoundary.endPos = nFound + 1;
229*cdf0e10cSrcweir 		}
230*cdf0e10cSrcweir 		else
231*cdf0e10cSrcweir 		{
232*cdf0e10cSrcweir 			rBoundary.startPos = nIndex;
233*cdf0e10cSrcweir 			rBoundary.endPos = nIndex;
234*cdf0e10cSrcweir 		}
235*cdf0e10cSrcweir 	}
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 	void OCommonAccessibleText::implGetLineBoundary( i18n::Boundary& rBoundary, sal_Int32 nIndex )
240*cdf0e10cSrcweir 	{
241*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
242*cdf0e10cSrcweir 		sal_Int32 nLength = sText.getLength();
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 		if ( implIsValidIndex( nIndex, nLength ) || nIndex == nLength )
245*cdf0e10cSrcweir 		{
246*cdf0e10cSrcweir 			rBoundary.startPos = 0;
247*cdf0e10cSrcweir 			rBoundary.endPos = nLength;
248*cdf0e10cSrcweir 		}
249*cdf0e10cSrcweir 		else
250*cdf0e10cSrcweir 		{
251*cdf0e10cSrcweir 			rBoundary.startPos = nIndex;
252*cdf0e10cSrcweir 			rBoundary.endPos = nIndex;
253*cdf0e10cSrcweir 		}
254*cdf0e10cSrcweir 	}
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 	sal_Unicode OCommonAccessibleText::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
259*cdf0e10cSrcweir 	{
260*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 		if ( !implIsValidIndex( nIndex, sText.getLength() ) )
263*cdf0e10cSrcweir 			throw IndexOutOfBoundsException();
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 		return sText.getStr()[nIndex];
266*cdf0e10cSrcweir 	}
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 	sal_Int32 OCommonAccessibleText::getCharacterCount() throw (RuntimeException)
271*cdf0e10cSrcweir 	{
272*cdf0e10cSrcweir 		return implGetText().getLength();
273*cdf0e10cSrcweir 	}
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 	::rtl::OUString OCommonAccessibleText::getSelectedText() throw (RuntimeException)
278*cdf0e10cSrcweir 	{
279*cdf0e10cSrcweir 		::rtl::OUString sText;
280*cdf0e10cSrcweir 		sal_Int32 nStartIndex;
281*cdf0e10cSrcweir 		sal_Int32 nEndIndex;
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 		implGetSelection( nStartIndex, nEndIndex );
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 		try
286*cdf0e10cSrcweir 		{
287*cdf0e10cSrcweir 			sText = getTextRange( nStartIndex, nEndIndex );
288*cdf0e10cSrcweir 		}
289*cdf0e10cSrcweir 		catch ( IndexOutOfBoundsException& )
290*cdf0e10cSrcweir 		{
291*cdf0e10cSrcweir 		}
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir 		return sText;
294*cdf0e10cSrcweir 	}
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir 	sal_Int32 OCommonAccessibleText::getSelectionStart() throw (RuntimeException)
299*cdf0e10cSrcweir 	{
300*cdf0e10cSrcweir 		sal_Int32 nStartIndex;
301*cdf0e10cSrcweir 		sal_Int32 nEndIndex;
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir 		implGetSelection( nStartIndex, nEndIndex );
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 		return nStartIndex;
306*cdf0e10cSrcweir 	}
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir 	sal_Int32 OCommonAccessibleText::getSelectionEnd() throw (RuntimeException)
311*cdf0e10cSrcweir 	{
312*cdf0e10cSrcweir 		sal_Int32 nStartIndex;
313*cdf0e10cSrcweir 		sal_Int32 nEndIndex;
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir 		implGetSelection( nStartIndex, nEndIndex );
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir 		return nEndIndex;
318*cdf0e10cSrcweir 	}
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir 	::rtl::OUString OCommonAccessibleText::getText() throw (RuntimeException)
323*cdf0e10cSrcweir 	{
324*cdf0e10cSrcweir 		return implGetText();
325*cdf0e10cSrcweir 	}
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir 	::rtl::OUString OCommonAccessibleText::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
330*cdf0e10cSrcweir 	{
331*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 		if ( !implIsValidRange( nStartIndex, nEndIndex, sText.getLength() ) )
334*cdf0e10cSrcweir 			throw IndexOutOfBoundsException();
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 		sal_Int32 nMinIndex = ::std::min( nStartIndex, nEndIndex );
337*cdf0e10cSrcweir 		sal_Int32 nMaxIndex = ::std::max( nStartIndex, nEndIndex );
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 		return sText.copy( nMinIndex, nMaxIndex - nMinIndex );
340*cdf0e10cSrcweir 	}
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir     TextSegment OCommonAccessibleText::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (IndexOutOfBoundsException, IllegalArgumentException, RuntimeException)
345*cdf0e10cSrcweir 	{
346*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
347*cdf0e10cSrcweir 		sal_Int32 nLength = sText.getLength();
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir 		if ( !implIsValidIndex( nIndex, nLength ) && nIndex != nLength )
350*cdf0e10cSrcweir 			throw IndexOutOfBoundsException();
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 		i18n::Boundary aBoundary;
353*cdf0e10cSrcweir 		TextSegment aResult;
354*cdf0e10cSrcweir 		aResult.SegmentStart = -1;
355*cdf0e10cSrcweir 		aResult.SegmentEnd = -1;
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir 		switch ( aTextType )
358*cdf0e10cSrcweir 		{
359*cdf0e10cSrcweir 			case AccessibleTextType::CHARACTER:
360*cdf0e10cSrcweir 			{
361*cdf0e10cSrcweir 				if ( implIsValidIndex( nIndex, nLength ) )
362*cdf0e10cSrcweir 				{
363*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( nIndex, 1 );
364*cdf0e10cSrcweir 					aResult.SegmentStart = nIndex;
365*cdf0e10cSrcweir 					aResult.SegmentEnd = nIndex+1;
366*cdf0e10cSrcweir 				}
367*cdf0e10cSrcweir 			}
368*cdf0e10cSrcweir 			break;
369*cdf0e10cSrcweir 			case AccessibleTextType::GLYPH:
370*cdf0e10cSrcweir 			{
371*cdf0e10cSrcweir 				// get glyph at index
372*cdf0e10cSrcweir 				implGetGlyphBoundary( aBoundary, nIndex );
373*cdf0e10cSrcweir 				if ( implIsValidBoundary( aBoundary, nLength ) )
374*cdf0e10cSrcweir 			    {
375*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
376*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
377*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
378*cdf0e10cSrcweir 				}
379*cdf0e10cSrcweir 			}
380*cdf0e10cSrcweir 			break;
381*cdf0e10cSrcweir 			case AccessibleTextType::WORD:
382*cdf0e10cSrcweir 			{
383*cdf0e10cSrcweir 				// get word at index
384*cdf0e10cSrcweir 				sal_Bool bWord = implGetWordBoundary( aBoundary, nIndex );
385*cdf0e10cSrcweir 				if ( bWord && implIsValidBoundary( aBoundary, nLength ) )
386*cdf0e10cSrcweir 				{
387*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
388*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
389*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
390*cdf0e10cSrcweir 				}
391*cdf0e10cSrcweir 			}
392*cdf0e10cSrcweir 			break;
393*cdf0e10cSrcweir 			case AccessibleTextType::SENTENCE:
394*cdf0e10cSrcweir 			{
395*cdf0e10cSrcweir 				// get sentence at index
396*cdf0e10cSrcweir 				implGetSentenceBoundary( aBoundary, nIndex );
397*cdf0e10cSrcweir 				if ( implIsValidBoundary( aBoundary, nLength ) )
398*cdf0e10cSrcweir 				{
399*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
400*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
401*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
402*cdf0e10cSrcweir 				}
403*cdf0e10cSrcweir 			}
404*cdf0e10cSrcweir 			break;
405*cdf0e10cSrcweir 			case AccessibleTextType::PARAGRAPH:
406*cdf0e10cSrcweir 			{
407*cdf0e10cSrcweir 				// get paragraph at index
408*cdf0e10cSrcweir 				implGetParagraphBoundary( aBoundary, nIndex );
409*cdf0e10cSrcweir 				if ( implIsValidBoundary( aBoundary, nLength ) )
410*cdf0e10cSrcweir 				{
411*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
412*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
413*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
414*cdf0e10cSrcweir 				}
415*cdf0e10cSrcweir 			}
416*cdf0e10cSrcweir 			break;
417*cdf0e10cSrcweir 			case AccessibleTextType::LINE:
418*cdf0e10cSrcweir 			{
419*cdf0e10cSrcweir 				// get line at index
420*cdf0e10cSrcweir 				implGetLineBoundary( aBoundary, nIndex );
421*cdf0e10cSrcweir 				if ( implIsValidBoundary( aBoundary, nLength ) )
422*cdf0e10cSrcweir 				{
423*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
424*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
425*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
426*cdf0e10cSrcweir 				}
427*cdf0e10cSrcweir 			}
428*cdf0e10cSrcweir 			break;
429*cdf0e10cSrcweir             case AccessibleTextType::ATTRIBUTE_RUN:
430*cdf0e10cSrcweir             {
431*cdf0e10cSrcweir                 // TODO: implGetAttributeRunBoundary() (incompatible!)
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir                 aResult.SegmentText = sText;
434*cdf0e10cSrcweir                 aResult.SegmentStart = 0;
435*cdf0e10cSrcweir                 aResult.SegmentEnd = nLength;
436*cdf0e10cSrcweir             }
437*cdf0e10cSrcweir             break;
438*cdf0e10cSrcweir 			default:
439*cdf0e10cSrcweir 			{
440*cdf0e10cSrcweir 				// unknown text type
441*cdf0e10cSrcweir 			}
442*cdf0e10cSrcweir 		}
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir 		return aResult;
445*cdf0e10cSrcweir 	}
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir     TextSegment OCommonAccessibleText::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (IndexOutOfBoundsException, IllegalArgumentException, RuntimeException)
450*cdf0e10cSrcweir 	{
451*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
452*cdf0e10cSrcweir 		sal_Int32 nLength = sText.getLength();
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir 		if ( !implIsValidIndex( nIndex, nLength ) && nIndex != nLength )
455*cdf0e10cSrcweir 			throw IndexOutOfBoundsException();
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir 		i18n::Boundary aBoundary;
458*cdf0e10cSrcweir 		TextSegment aResult;
459*cdf0e10cSrcweir 		aResult.SegmentStart = -1;
460*cdf0e10cSrcweir 		aResult.SegmentEnd = -1;
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir 		switch ( aTextType )
463*cdf0e10cSrcweir 		{
464*cdf0e10cSrcweir 			case AccessibleTextType::CHARACTER:
465*cdf0e10cSrcweir 			{
466*cdf0e10cSrcweir 				if ( implIsValidIndex( nIndex - 1, nLength ) )
467*cdf0e10cSrcweir 				{
468*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( nIndex - 1, 1 );
469*cdf0e10cSrcweir 					aResult.SegmentStart = nIndex-1;
470*cdf0e10cSrcweir 					aResult.SegmentEnd = nIndex;
471*cdf0e10cSrcweir 				}
472*cdf0e10cSrcweir 			}
473*cdf0e10cSrcweir 			break;
474*cdf0e10cSrcweir 			case AccessibleTextType::GLYPH:
475*cdf0e10cSrcweir 			{
476*cdf0e10cSrcweir 				// get glyph at index
477*cdf0e10cSrcweir 				implGetGlyphBoundary( aBoundary, nIndex );
478*cdf0e10cSrcweir 				// get previous glyph
479*cdf0e10cSrcweir 				if ( aBoundary.startPos > 0 )
480*cdf0e10cSrcweir 				{
481*cdf0e10cSrcweir 					implGetGlyphBoundary( aBoundary, aBoundary.startPos - 1 );
482*cdf0e10cSrcweir 					if ( implIsValidBoundary( aBoundary, nLength ) )
483*cdf0e10cSrcweir 					{
484*cdf0e10cSrcweir 						aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
485*cdf0e10cSrcweir     					aResult.SegmentStart = aBoundary.startPos;
486*cdf0e10cSrcweir     					aResult.SegmentEnd = aBoundary.endPos;
487*cdf0e10cSrcweir 					}
488*cdf0e10cSrcweir 				}
489*cdf0e10cSrcweir 			}
490*cdf0e10cSrcweir 			break;
491*cdf0e10cSrcweir 			case AccessibleTextType::WORD:
492*cdf0e10cSrcweir 			{
493*cdf0e10cSrcweir 				// get word at index
494*cdf0e10cSrcweir 				implGetWordBoundary( aBoundary, nIndex );
495*cdf0e10cSrcweir 				// get previous word
496*cdf0e10cSrcweir 				sal_Bool bWord = sal_False;
497*cdf0e10cSrcweir 				while ( !bWord && aBoundary.startPos > 0 )
498*cdf0e10cSrcweir 					bWord = implGetWordBoundary( aBoundary, aBoundary.startPos - 1 );
499*cdf0e10cSrcweir 				if ( bWord && implIsValidBoundary( aBoundary, nLength ) )
500*cdf0e10cSrcweir 				{
501*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
502*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
503*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
504*cdf0e10cSrcweir 				}
505*cdf0e10cSrcweir 			}
506*cdf0e10cSrcweir 			break;
507*cdf0e10cSrcweir 			case AccessibleTextType::SENTENCE:
508*cdf0e10cSrcweir 			{
509*cdf0e10cSrcweir 				// get sentence at index
510*cdf0e10cSrcweir 				implGetSentenceBoundary( aBoundary, nIndex );
511*cdf0e10cSrcweir 				// get previous sentence
512*cdf0e10cSrcweir 				if ( aBoundary.startPos > 0 )
513*cdf0e10cSrcweir 				{
514*cdf0e10cSrcweir 					implGetSentenceBoundary( aBoundary, aBoundary.startPos - 1 );
515*cdf0e10cSrcweir 					if ( implIsValidBoundary( aBoundary, nLength ) )
516*cdf0e10cSrcweir 					{
517*cdf0e10cSrcweir 						aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
518*cdf0e10cSrcweir     					aResult.SegmentStart = aBoundary.startPos;
519*cdf0e10cSrcweir     					aResult.SegmentEnd = aBoundary.endPos;
520*cdf0e10cSrcweir 					}
521*cdf0e10cSrcweir 				}
522*cdf0e10cSrcweir 			}
523*cdf0e10cSrcweir 			break;
524*cdf0e10cSrcweir 			case AccessibleTextType::PARAGRAPH:
525*cdf0e10cSrcweir 			{
526*cdf0e10cSrcweir 				// get paragraph at index
527*cdf0e10cSrcweir 				implGetParagraphBoundary( aBoundary, nIndex );
528*cdf0e10cSrcweir 				// get previous paragraph
529*cdf0e10cSrcweir 				if ( aBoundary.startPos > 0 )
530*cdf0e10cSrcweir 				{
531*cdf0e10cSrcweir 					implGetParagraphBoundary( aBoundary, aBoundary.startPos - 1 );
532*cdf0e10cSrcweir 					if ( implIsValidBoundary( aBoundary, nLength ) )
533*cdf0e10cSrcweir 					{
534*cdf0e10cSrcweir 						aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
535*cdf0e10cSrcweir     					aResult.SegmentStart = aBoundary.startPos;
536*cdf0e10cSrcweir     					aResult.SegmentEnd = aBoundary.endPos;
537*cdf0e10cSrcweir 					}
538*cdf0e10cSrcweir 				}
539*cdf0e10cSrcweir 			}
540*cdf0e10cSrcweir 			break;
541*cdf0e10cSrcweir 			case AccessibleTextType::LINE:
542*cdf0e10cSrcweir 			{
543*cdf0e10cSrcweir 				// get line at index
544*cdf0e10cSrcweir 				implGetLineBoundary( aBoundary, nIndex );
545*cdf0e10cSrcweir 				// get previous line
546*cdf0e10cSrcweir 				if ( aBoundary.startPos > 0 )
547*cdf0e10cSrcweir 				{
548*cdf0e10cSrcweir 					implGetLineBoundary( aBoundary, aBoundary.startPos - 1 );
549*cdf0e10cSrcweir 					if ( implIsValidBoundary( aBoundary, nLength ) )
550*cdf0e10cSrcweir 					{
551*cdf0e10cSrcweir 						aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
552*cdf0e10cSrcweir     					aResult.SegmentStart = aBoundary.startPos;
553*cdf0e10cSrcweir     					aResult.SegmentEnd = aBoundary.endPos;
554*cdf0e10cSrcweir 					}
555*cdf0e10cSrcweir 				}
556*cdf0e10cSrcweir 			}
557*cdf0e10cSrcweir 			break;
558*cdf0e10cSrcweir             case AccessibleTextType::ATTRIBUTE_RUN:
559*cdf0e10cSrcweir             {
560*cdf0e10cSrcweir                 // TODO: implGetAttributeRunBoundary() (incompatible!)
561*cdf0e10cSrcweir             }
562*cdf0e10cSrcweir             break;
563*cdf0e10cSrcweir 			default:
564*cdf0e10cSrcweir 			{
565*cdf0e10cSrcweir 				// unknown text type
566*cdf0e10cSrcweir 			}
567*cdf0e10cSrcweir 		}
568*cdf0e10cSrcweir 
569*cdf0e10cSrcweir 		return aResult;
570*cdf0e10cSrcweir 	}
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir     TextSegment OCommonAccessibleText::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (IndexOutOfBoundsException, IllegalArgumentException, RuntimeException)
575*cdf0e10cSrcweir 	{
576*cdf0e10cSrcweir 		::rtl::OUString sText( implGetText() );
577*cdf0e10cSrcweir 		sal_Int32 nLength = sText.getLength();
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir 		if ( !implIsValidIndex( nIndex, nLength ) && nIndex != nLength )
580*cdf0e10cSrcweir 			throw IndexOutOfBoundsException();
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir 		i18n::Boundary aBoundary;
583*cdf0e10cSrcweir 		TextSegment aResult;
584*cdf0e10cSrcweir 		aResult.SegmentStart = -1;
585*cdf0e10cSrcweir 		aResult.SegmentEnd = -1;
586*cdf0e10cSrcweir 
587*cdf0e10cSrcweir 		switch ( aTextType )
588*cdf0e10cSrcweir 		{
589*cdf0e10cSrcweir 			case AccessibleTextType::CHARACTER:
590*cdf0e10cSrcweir 			{
591*cdf0e10cSrcweir 				if ( implIsValidIndex( nIndex + 1, nLength ) )
592*cdf0e10cSrcweir 			    {
593*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( nIndex + 1, 1 );
594*cdf0e10cSrcweir 					aResult.SegmentStart = nIndex+1;
595*cdf0e10cSrcweir 					aResult.SegmentEnd = nIndex+2;
596*cdf0e10cSrcweir 				}
597*cdf0e10cSrcweir 			}
598*cdf0e10cSrcweir 			break;
599*cdf0e10cSrcweir 			case AccessibleTextType::GLYPH:
600*cdf0e10cSrcweir 			{
601*cdf0e10cSrcweir 				// get glyph at index
602*cdf0e10cSrcweir 				implGetGlyphBoundary( aBoundary, nIndex );
603*cdf0e10cSrcweir 				// get next glyph
604*cdf0e10cSrcweir 				if ( aBoundary.endPos < nLength )
605*cdf0e10cSrcweir 				{
606*cdf0e10cSrcweir 					implGetGlyphBoundary( aBoundary, aBoundary.endPos );
607*cdf0e10cSrcweir 					if ( implIsValidBoundary( aBoundary, nLength ) )
608*cdf0e10cSrcweir 					{
609*cdf0e10cSrcweir 						aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
610*cdf0e10cSrcweir     					aResult.SegmentStart = aBoundary.startPos;
611*cdf0e10cSrcweir     					aResult.SegmentEnd = aBoundary.endPos;
612*cdf0e10cSrcweir 					}
613*cdf0e10cSrcweir 				}
614*cdf0e10cSrcweir 			}
615*cdf0e10cSrcweir 			break;
616*cdf0e10cSrcweir 			case AccessibleTextType::WORD:
617*cdf0e10cSrcweir 			{
618*cdf0e10cSrcweir 				// get word at index
619*cdf0e10cSrcweir 				implGetWordBoundary( aBoundary, nIndex );
620*cdf0e10cSrcweir 				// get next word
621*cdf0e10cSrcweir 				sal_Bool bWord = sal_False;
622*cdf0e10cSrcweir 				while ( !bWord && aBoundary.endPos < nLength )
623*cdf0e10cSrcweir 					bWord = implGetWordBoundary( aBoundary, aBoundary.endPos );
624*cdf0e10cSrcweir 				if ( bWord && implIsValidBoundary( aBoundary, nLength ) )
625*cdf0e10cSrcweir 				{
626*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
627*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
628*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
629*cdf0e10cSrcweir 				}
630*cdf0e10cSrcweir 			}
631*cdf0e10cSrcweir 			break;
632*cdf0e10cSrcweir 			case AccessibleTextType::SENTENCE:
633*cdf0e10cSrcweir 			{
634*cdf0e10cSrcweir 				// get sentence at index
635*cdf0e10cSrcweir 				implGetSentenceBoundary( aBoundary, nIndex );
636*cdf0e10cSrcweir 				// get next sentence
637*cdf0e10cSrcweir 				sal_Int32 nEnd = aBoundary.endPos;
638*cdf0e10cSrcweir 				sal_Int32 nI = aBoundary.endPos;
639*cdf0e10cSrcweir 				sal_Bool bFound = sal_False;
640*cdf0e10cSrcweir 				while ( !bFound && ++nI < nLength )
641*cdf0e10cSrcweir 				{
642*cdf0e10cSrcweir 					implGetSentenceBoundary( aBoundary, nI );
643*cdf0e10cSrcweir 					bFound = ( aBoundary.endPos > nEnd );
644*cdf0e10cSrcweir 				}
645*cdf0e10cSrcweir 				if ( bFound && implIsValidBoundary( aBoundary, nLength ) )
646*cdf0e10cSrcweir 				{
647*cdf0e10cSrcweir 					aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
648*cdf0e10cSrcweir 					aResult.SegmentStart = aBoundary.startPos;
649*cdf0e10cSrcweir 					aResult.SegmentEnd = aBoundary.endPos;
650*cdf0e10cSrcweir 				}
651*cdf0e10cSrcweir 			}
652*cdf0e10cSrcweir 			break;
653*cdf0e10cSrcweir 			case AccessibleTextType::PARAGRAPH:
654*cdf0e10cSrcweir 			{
655*cdf0e10cSrcweir 				// get paragraph at index
656*cdf0e10cSrcweir 				implGetParagraphBoundary( aBoundary, nIndex );
657*cdf0e10cSrcweir 				// get next paragraph
658*cdf0e10cSrcweir 				if ( aBoundary.endPos < nLength )
659*cdf0e10cSrcweir 				{
660*cdf0e10cSrcweir 					implGetParagraphBoundary( aBoundary, aBoundary.endPos );
661*cdf0e10cSrcweir 					if ( implIsValidBoundary( aBoundary, nLength ) )
662*cdf0e10cSrcweir 					{
663*cdf0e10cSrcweir 						aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
664*cdf0e10cSrcweir     					aResult.SegmentStart = aBoundary.startPos;
665*cdf0e10cSrcweir     					aResult.SegmentEnd = aBoundary.endPos;
666*cdf0e10cSrcweir 					}
667*cdf0e10cSrcweir 				}
668*cdf0e10cSrcweir 			}
669*cdf0e10cSrcweir 			break;
670*cdf0e10cSrcweir 			case AccessibleTextType::LINE:
671*cdf0e10cSrcweir 			{
672*cdf0e10cSrcweir 				// get line at index
673*cdf0e10cSrcweir 				implGetLineBoundary( aBoundary, nIndex );
674*cdf0e10cSrcweir 				// get next line
675*cdf0e10cSrcweir 				if ( aBoundary.endPos < nLength )
676*cdf0e10cSrcweir 				{
677*cdf0e10cSrcweir 					implGetLineBoundary( aBoundary, aBoundary.endPos );
678*cdf0e10cSrcweir 					if ( implIsValidBoundary( aBoundary, nLength ) )
679*cdf0e10cSrcweir 					{
680*cdf0e10cSrcweir 						aResult.SegmentText = sText.copy( aBoundary.startPos, aBoundary.endPos - aBoundary.startPos );
681*cdf0e10cSrcweir     					aResult.SegmentStart = aBoundary.startPos;
682*cdf0e10cSrcweir     					aResult.SegmentEnd = aBoundary.endPos;
683*cdf0e10cSrcweir 					}
684*cdf0e10cSrcweir 				}
685*cdf0e10cSrcweir 			}
686*cdf0e10cSrcweir 			break;
687*cdf0e10cSrcweir             case AccessibleTextType::ATTRIBUTE_RUN:
688*cdf0e10cSrcweir             {
689*cdf0e10cSrcweir                 // TODO: implGetAttributeRunBoundary() (incompatible!)
690*cdf0e10cSrcweir             }
691*cdf0e10cSrcweir             break;
692*cdf0e10cSrcweir 			default:
693*cdf0e10cSrcweir 			{
694*cdf0e10cSrcweir 				// unknown text type
695*cdf0e10cSrcweir 			}
696*cdf0e10cSrcweir 		}
697*cdf0e10cSrcweir 
698*cdf0e10cSrcweir 		return aResult;
699*cdf0e10cSrcweir 	}
700*cdf0e10cSrcweir 
701*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
702*cdf0e10cSrcweir     bool OCommonAccessibleText::implInitTextChangedEvent(
703*cdf0e10cSrcweir         const rtl::OUString& rOldString,
704*cdf0e10cSrcweir         const rtl::OUString& rNewString,
705*cdf0e10cSrcweir         ::com::sun::star::uno::Any& rDeleted,
706*cdf0e10cSrcweir         ::com::sun::star::uno::Any& rInserted) // throw()
707*cdf0e10cSrcweir     {
708*cdf0e10cSrcweir         sal_uInt32 nLenOld = rOldString.getLength();
709*cdf0e10cSrcweir         sal_uInt32 nLenNew = rNewString.getLength();
710*cdf0e10cSrcweir 
711*cdf0e10cSrcweir         // equal
712*cdf0e10cSrcweir         if ((0 == nLenOld) && (0 == nLenNew))
713*cdf0e10cSrcweir             return false;
714*cdf0e10cSrcweir 
715*cdf0e10cSrcweir         TextSegment aDeletedText;
716*cdf0e10cSrcweir         TextSegment aInsertedText;
717*cdf0e10cSrcweir 
718*cdf0e10cSrcweir         aDeletedText.SegmentStart = -1;
719*cdf0e10cSrcweir         aDeletedText.SegmentEnd = -1;
720*cdf0e10cSrcweir         aInsertedText.SegmentStart = -1;
721*cdf0e10cSrcweir         aInsertedText.SegmentEnd = -1;
722*cdf0e10cSrcweir 
723*cdf0e10cSrcweir         // insert only
724*cdf0e10cSrcweir         if ((0 == nLenOld) && (nLenNew > 0))
725*cdf0e10cSrcweir         {
726*cdf0e10cSrcweir             aInsertedText.SegmentStart = 0;
727*cdf0e10cSrcweir             aInsertedText.SegmentEnd = nLenNew;
728*cdf0e10cSrcweir             aInsertedText.SegmentText = rNewString.copy( aInsertedText.SegmentStart, aInsertedText.SegmentEnd - aInsertedText.SegmentStart );
729*cdf0e10cSrcweir 
730*cdf0e10cSrcweir             rInserted <<= aInsertedText;
731*cdf0e10cSrcweir             return true;
732*cdf0e10cSrcweir         }
733*cdf0e10cSrcweir 
734*cdf0e10cSrcweir         // delete only
735*cdf0e10cSrcweir         if ((nLenOld > 0) && (0 == nLenNew))
736*cdf0e10cSrcweir         {
737*cdf0e10cSrcweir             aDeletedText.SegmentStart = 0;
738*cdf0e10cSrcweir             aDeletedText.SegmentEnd = nLenOld;
739*cdf0e10cSrcweir             aDeletedText.SegmentText = rOldString.copy( aDeletedText.SegmentStart, aDeletedText.SegmentEnd - aDeletedText.SegmentStart );
740*cdf0e10cSrcweir 
741*cdf0e10cSrcweir             rDeleted <<= aDeletedText;
742*cdf0e10cSrcweir             return true;
743*cdf0e10cSrcweir         }
744*cdf0e10cSrcweir 
745*cdf0e10cSrcweir         const sal_Unicode* pFirstDiffOld = rOldString.getStr();
746*cdf0e10cSrcweir         const sal_Unicode* pLastDiffOld  = rOldString.getStr() + nLenOld;
747*cdf0e10cSrcweir         const sal_Unicode* pFirstDiffNew = rNewString.getStr();
748*cdf0e10cSrcweir         const sal_Unicode* pLastDiffNew  = rNewString.getStr() + nLenNew;
749*cdf0e10cSrcweir 
750*cdf0e10cSrcweir         // find first difference
751*cdf0e10cSrcweir         while ((*pFirstDiffOld == *pFirstDiffNew) &&
752*cdf0e10cSrcweir                (pFirstDiffOld  <  pLastDiffOld) &&
753*cdf0e10cSrcweir                (pFirstDiffNew  <  pLastDiffNew))
754*cdf0e10cSrcweir         {
755*cdf0e10cSrcweir             pFirstDiffOld++;
756*cdf0e10cSrcweir             pFirstDiffNew++;
757*cdf0e10cSrcweir         }
758*cdf0e10cSrcweir 
759*cdf0e10cSrcweir         // equality test
760*cdf0e10cSrcweir         if ((0 == *pFirstDiffOld) && (0 == *pFirstDiffNew))
761*cdf0e10cSrcweir             return false;
762*cdf0e10cSrcweir 
763*cdf0e10cSrcweir         // find last difference
764*cdf0e10cSrcweir         while ( ( pLastDiffOld > pFirstDiffOld) &&
765*cdf0e10cSrcweir                 ( pLastDiffNew > pFirstDiffNew) &&
766*cdf0e10cSrcweir                 (pLastDiffOld[-1]  == pLastDiffNew[-1]))
767*cdf0e10cSrcweir         {
768*cdf0e10cSrcweir             pLastDiffOld--;
769*cdf0e10cSrcweir             pLastDiffNew--;
770*cdf0e10cSrcweir         }
771*cdf0e10cSrcweir 
772*cdf0e10cSrcweir         if (pFirstDiffOld < pLastDiffOld)
773*cdf0e10cSrcweir         {
774*cdf0e10cSrcweir             aDeletedText.SegmentStart = pFirstDiffOld - rOldString.getStr();
775*cdf0e10cSrcweir             aDeletedText.SegmentEnd = pLastDiffOld  - rOldString.getStr();
776*cdf0e10cSrcweir             aDeletedText.SegmentText = rOldString.copy( aDeletedText.SegmentStart, aDeletedText.SegmentEnd - aDeletedText.SegmentStart );
777*cdf0e10cSrcweir 
778*cdf0e10cSrcweir             rDeleted <<= aDeletedText;
779*cdf0e10cSrcweir         }
780*cdf0e10cSrcweir 
781*cdf0e10cSrcweir         if (pFirstDiffNew < pLastDiffNew)
782*cdf0e10cSrcweir         {
783*cdf0e10cSrcweir             aInsertedText.SegmentStart = pFirstDiffNew - rNewString.getStr();
784*cdf0e10cSrcweir             aInsertedText.SegmentEnd = pLastDiffNew  - rNewString.getStr();
785*cdf0e10cSrcweir             aInsertedText.SegmentText = rNewString.copy( aInsertedText.SegmentStart, aInsertedText.SegmentEnd - aInsertedText.SegmentStart );
786*cdf0e10cSrcweir 
787*cdf0e10cSrcweir             rInserted <<= aInsertedText;
788*cdf0e10cSrcweir         }
789*cdf0e10cSrcweir         return true;
790*cdf0e10cSrcweir     }
791*cdf0e10cSrcweir 
792*cdf0e10cSrcweir 	//==============================================================================
793*cdf0e10cSrcweir 	// OAccessibleTextHelper
794*cdf0e10cSrcweir 	//==============================================================================
795*cdf0e10cSrcweir 
796*cdf0e10cSrcweir 	OAccessibleTextHelper::OAccessibleTextHelper()
797*cdf0e10cSrcweir 	{
798*cdf0e10cSrcweir 	}
799*cdf0e10cSrcweir 
800*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
801*cdf0e10cSrcweir 
802*cdf0e10cSrcweir 	OAccessibleTextHelper::OAccessibleTextHelper( IMutex* _pExternalLock )
803*cdf0e10cSrcweir 		:OAccessibleExtendedComponentHelper( _pExternalLock )
804*cdf0e10cSrcweir 	{
805*cdf0e10cSrcweir 	}
806*cdf0e10cSrcweir 
807*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
808*cdf0e10cSrcweir 	// XInterface
809*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
810*cdf0e10cSrcweir 
811*cdf0e10cSrcweir 	IMPLEMENT_FORWARD_XINTERFACE2( OAccessibleTextHelper, OAccessibleExtendedComponentHelper, OAccessibleTextHelper_Base )
812*cdf0e10cSrcweir 
813*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
814*cdf0e10cSrcweir 	// XTypeProvider
815*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
816*cdf0e10cSrcweir 
817*cdf0e10cSrcweir 	IMPLEMENT_FORWARD_XTYPEPROVIDER2( OAccessibleTextHelper, OAccessibleExtendedComponentHelper, OAccessibleTextHelper_Base )
818*cdf0e10cSrcweir 
819*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
820*cdf0e10cSrcweir 	// XAccessibleText
821*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
822*cdf0e10cSrcweir 
823*cdf0e10cSrcweir 	sal_Unicode OAccessibleTextHelper::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
824*cdf0e10cSrcweir 	{
825*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
826*cdf0e10cSrcweir 
827*cdf0e10cSrcweir 		return OCommonAccessibleText::getCharacter( nIndex );
828*cdf0e10cSrcweir 	}
829*cdf0e10cSrcweir 
830*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
831*cdf0e10cSrcweir 
832*cdf0e10cSrcweir 	sal_Int32 OAccessibleTextHelper::getCharacterCount() throw (RuntimeException)
833*cdf0e10cSrcweir 	{
834*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
835*cdf0e10cSrcweir 
836*cdf0e10cSrcweir 		return OCommonAccessibleText::getCharacterCount();
837*cdf0e10cSrcweir 	}
838*cdf0e10cSrcweir 
839*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
840*cdf0e10cSrcweir 
841*cdf0e10cSrcweir 	::rtl::OUString OAccessibleTextHelper::getSelectedText() throw (RuntimeException)
842*cdf0e10cSrcweir 	{
843*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
844*cdf0e10cSrcweir 
845*cdf0e10cSrcweir 		return OCommonAccessibleText::getSelectedText();
846*cdf0e10cSrcweir 	}
847*cdf0e10cSrcweir 
848*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
849*cdf0e10cSrcweir 
850*cdf0e10cSrcweir 	sal_Int32 OAccessibleTextHelper::getSelectionStart() throw (RuntimeException)
851*cdf0e10cSrcweir 	{
852*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
853*cdf0e10cSrcweir 
854*cdf0e10cSrcweir 		return OCommonAccessibleText::getSelectionStart();
855*cdf0e10cSrcweir 	}
856*cdf0e10cSrcweir 
857*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
858*cdf0e10cSrcweir 
859*cdf0e10cSrcweir 	sal_Int32 OAccessibleTextHelper::getSelectionEnd() throw (RuntimeException)
860*cdf0e10cSrcweir 	{
861*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
862*cdf0e10cSrcweir 
863*cdf0e10cSrcweir 		return OCommonAccessibleText::getSelectionEnd();
864*cdf0e10cSrcweir 	}
865*cdf0e10cSrcweir 
866*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
867*cdf0e10cSrcweir 
868*cdf0e10cSrcweir 	::rtl::OUString OAccessibleTextHelper::getText() throw (RuntimeException)
869*cdf0e10cSrcweir 	{
870*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
871*cdf0e10cSrcweir 
872*cdf0e10cSrcweir 		return OCommonAccessibleText::getText();
873*cdf0e10cSrcweir 	}
874*cdf0e10cSrcweir 
875*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
876*cdf0e10cSrcweir 
877*cdf0e10cSrcweir 	::rtl::OUString OAccessibleTextHelper::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
878*cdf0e10cSrcweir 	{
879*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
880*cdf0e10cSrcweir 
881*cdf0e10cSrcweir 		return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
882*cdf0e10cSrcweir 	}
883*cdf0e10cSrcweir 
884*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
885*cdf0e10cSrcweir 
886*cdf0e10cSrcweir     TextSegment OAccessibleTextHelper::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (IndexOutOfBoundsException, IllegalArgumentException, RuntimeException)
887*cdf0e10cSrcweir 	{
888*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
889*cdf0e10cSrcweir 
890*cdf0e10cSrcweir 		return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
891*cdf0e10cSrcweir 	}
892*cdf0e10cSrcweir 
893*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
894*cdf0e10cSrcweir 
895*cdf0e10cSrcweir     TextSegment OAccessibleTextHelper::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (IndexOutOfBoundsException, IllegalArgumentException, RuntimeException)
896*cdf0e10cSrcweir 	{
897*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
898*cdf0e10cSrcweir 
899*cdf0e10cSrcweir 		return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
900*cdf0e10cSrcweir 	}
901*cdf0e10cSrcweir 
902*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
903*cdf0e10cSrcweir 
904*cdf0e10cSrcweir     TextSegment OAccessibleTextHelper::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (IndexOutOfBoundsException, IllegalArgumentException, RuntimeException)
905*cdf0e10cSrcweir 	{
906*cdf0e10cSrcweir 		OExternalLockGuard aGuard( this );
907*cdf0e10cSrcweir 
908*cdf0e10cSrcweir 		return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
909*cdf0e10cSrcweir 	}
910*cdf0e10cSrcweir 
911*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
912*cdf0e10cSrcweir 
913*cdf0e10cSrcweir //..............................................................................
914*cdf0e10cSrcweir }	// namespace comphelper
915*cdf0e10cSrcweir //..............................................................................
916