xref: /AOO41X/main/svtools/source/edit/textdata.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_svtools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <svtools/textdata.hxx>
32*cdf0e10cSrcweir #include <textdat2.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir SV_IMPL_PTRARR( TextLines, TextLinePtr );
37*cdf0e10cSrcweir SV_IMPL_VARARR( TEWritingDirectionInfos, TEWritingDirectionInfo );
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir // -------------------------------------------------------------------------
41*cdf0e10cSrcweir // (+) class TextSelection
42*cdf0e10cSrcweir // -------------------------------------------------------------------------
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir TextSelection::TextSelection()
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir }
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir TextSelection::TextSelection( const TextPaM& rPaM ) :
49*cdf0e10cSrcweir 	maStartPaM( rPaM ), maEndPaM( rPaM )
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir TextSelection::TextSelection( const TextPaM& rStart, const TextPaM& rEnd ) :
54*cdf0e10cSrcweir 	maStartPaM( rStart ), maEndPaM( rEnd )
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir void TextSelection::Justify()
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	if ( maEndPaM < maStartPaM )
61*cdf0e10cSrcweir 	{
62*cdf0e10cSrcweir 		TextPaM aTemp( maStartPaM );
63*cdf0e10cSrcweir 		maStartPaM = maEndPaM;
64*cdf0e10cSrcweir 		maEndPaM = aTemp;
65*cdf0e10cSrcweir 	}
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir // -------------------------------------------------------------------------
70*cdf0e10cSrcweir // (+) class TETextPortionList
71*cdf0e10cSrcweir // -------------------------------------------------------------------------
72*cdf0e10cSrcweir TETextPortionList::TETextPortionList()
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir TETextPortionList::~TETextPortionList()
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir 	Reset();
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir void TETextPortionList::Reset()
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	for ( sal_uInt16 nPortion = 0; nPortion < Count(); nPortion++ )
84*cdf0e10cSrcweir 		delete GetObject( nPortion );
85*cdf0e10cSrcweir 	Remove( 0, Count() );
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir void TETextPortionList::DeleteFromPortion( sal_uInt16 nDelFrom )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	DBG_ASSERT( ( nDelFrom < Count() ) || ( (nDelFrom == 0) && (Count() == 0) ), "DeleteFromPortion: Out of range" );
91*cdf0e10cSrcweir 	for ( sal_uInt16 nP = nDelFrom; nP < Count(); nP++ )
92*cdf0e10cSrcweir 		delete GetObject( nP );
93*cdf0e10cSrcweir 	Remove( nDelFrom, Count()-nDelFrom );
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir sal_uInt16 TETextPortionList::FindPortion( sal_uInt16 nCharPos, sal_uInt16& nPortionStart, sal_Bool bPreferStartingPortion )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	// Bei nCharPos an Portion-Grenze wird die linke Portion gefunden
99*cdf0e10cSrcweir 	sal_uInt16 nTmpPos = 0;
100*cdf0e10cSrcweir 	for ( sal_uInt16 nPortion = 0; nPortion < Count(); nPortion++ )
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir 		TETextPortion* pPortion = GetObject( nPortion );
103*cdf0e10cSrcweir 		nTmpPos = nTmpPos + pPortion->GetLen();
104*cdf0e10cSrcweir 		if ( nTmpPos >= nCharPos )
105*cdf0e10cSrcweir 		{
106*cdf0e10cSrcweir             // take this one if we don't prefer the starting portion, or if it's the last one
107*cdf0e10cSrcweir             if ( ( nTmpPos != nCharPos ) || !bPreferStartingPortion || ( nPortion == Count() - 1 ) )
108*cdf0e10cSrcweir             {
109*cdf0e10cSrcweir 			    nPortionStart = nTmpPos - pPortion->GetLen();
110*cdf0e10cSrcweir 			    return nPortion;
111*cdf0e10cSrcweir             }
112*cdf0e10cSrcweir 		}
113*cdf0e10cSrcweir 	}
114*cdf0e10cSrcweir 	DBG_ERROR( "FindPortion: Nicht gefunden!" );
115*cdf0e10cSrcweir 	return ( Count() - 1 );
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir /*
119*cdf0e10cSrcweir sal_uInt16 TETextPortionList::GetPortionStartIndex( sal_uInt16 nPortion )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir 	sal_uInt16 nPos = 0;
122*cdf0e10cSrcweir 	for ( sal_uInt16 nP = 0; nP < nPortion; nP++ )
123*cdf0e10cSrcweir 	{
124*cdf0e10cSrcweir 		TETextPortion* pPortion = GetObject( nP );
125*cdf0e10cSrcweir 		nPos += pPortion->GetLen();
126*cdf0e10cSrcweir 	}
127*cdf0e10cSrcweir 	return nPos;
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir */
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir // -------------------------------------------------------------------------
133*cdf0e10cSrcweir // (+) class TEParaPortion
134*cdf0e10cSrcweir // -------------------------------------------------------------------------
135*cdf0e10cSrcweir TEParaPortion::TEParaPortion( TextNode* pN )
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir 	mpNode = pN;
138*cdf0e10cSrcweir 	mnInvalidPosStart = mnInvalidDiff = 0;
139*cdf0e10cSrcweir 	mbInvalid = sal_True;
140*cdf0e10cSrcweir 	mbSimple = sal_False;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir TEParaPortion::~TEParaPortion()
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir void TEParaPortion::MarkInvalid( sal_uInt16 nStart, short nDiff )
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir 	if ( mbInvalid == sal_False )
150*cdf0e10cSrcweir 	{
151*cdf0e10cSrcweir 		mnInvalidPosStart = ( nDiff >= 0 ) ? nStart : ( nStart + nDiff );
152*cdf0e10cSrcweir 		mnInvalidDiff = nDiff;
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir 	else
155*cdf0e10cSrcweir 	{
156*cdf0e10cSrcweir 		// Einfaches hintereinander tippen
157*cdf0e10cSrcweir 		if ( ( nDiff > 0 ) && ( mnInvalidDiff > 0 ) &&
158*cdf0e10cSrcweir 			 ( ( mnInvalidPosStart+mnInvalidDiff ) == nStart ) )
159*cdf0e10cSrcweir 		{
160*cdf0e10cSrcweir 			mnInvalidDiff = mnInvalidDiff + nDiff;
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 		// Einfaches hintereinander loeschen
163*cdf0e10cSrcweir 		else if ( ( nDiff < 0 ) && ( mnInvalidDiff < 0 ) && ( mnInvalidPosStart == nStart ) )
164*cdf0e10cSrcweir 		{
165*cdf0e10cSrcweir 			mnInvalidPosStart = mnInvalidPosStart + nDiff;
166*cdf0e10cSrcweir 			mnInvalidDiff = mnInvalidDiff + nDiff;
167*cdf0e10cSrcweir 		}
168*cdf0e10cSrcweir 		else
169*cdf0e10cSrcweir 		{
170*cdf0e10cSrcweir 			DBG_ASSERT( ( nDiff >= 0 ) || ( (nStart+nDiff) >= 0 ), "MarkInvalid: Diff out of Range" );
171*cdf0e10cSrcweir 			mnInvalidPosStart = Min( mnInvalidPosStart, (sal_uInt16) ( (nDiff < 0) ? nStart+nDiff : nDiff ) );
172*cdf0e10cSrcweir 			mnInvalidDiff = 0;
173*cdf0e10cSrcweir 			mbSimple = sal_False;
174*cdf0e10cSrcweir 		}
175*cdf0e10cSrcweir 	}
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     maWritingDirectionInfos.Remove( 0, maWritingDirectionInfos.Count() );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     mbInvalid = sal_True;
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir void TEParaPortion::MarkSelectionInvalid( sal_uInt16 nStart, sal_uInt16 /*nEnd*/ )
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	if ( mbInvalid == sal_False )
185*cdf0e10cSrcweir 	{
186*cdf0e10cSrcweir 		mnInvalidPosStart = nStart;
187*cdf0e10cSrcweir //		nInvalidPosEnd = nEnd;
188*cdf0e10cSrcweir 	}
189*cdf0e10cSrcweir 	else
190*cdf0e10cSrcweir 	{
191*cdf0e10cSrcweir 		mnInvalidPosStart = Min( mnInvalidPosStart, nStart );
192*cdf0e10cSrcweir //		nInvalidPosEnd = pNode->Len();
193*cdf0e10cSrcweir 	}
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     maWritingDirectionInfos.Remove( 0, maWritingDirectionInfos.Count() );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     mnInvalidDiff = 0;
198*cdf0e10cSrcweir 	mbInvalid = sal_True;
199*cdf0e10cSrcweir 	mbSimple = sal_False;
200*cdf0e10cSrcweir }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir sal_uInt16 TEParaPortion::GetLineNumber( sal_uInt16 nChar, sal_Bool bInclEnd )
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir 	for ( sal_uInt16 nLine = 0; nLine < maLines.Count(); nLine++ )
205*cdf0e10cSrcweir 	{
206*cdf0e10cSrcweir 		TextLine* pLine = maLines.GetObject( nLine );
207*cdf0e10cSrcweir 		if ( ( bInclEnd && ( pLine->GetEnd() >= nChar ) ) ||
208*cdf0e10cSrcweir 			 ( pLine->GetEnd() > nChar ) )
209*cdf0e10cSrcweir 		{
210*cdf0e10cSrcweir 			return nLine;
211*cdf0e10cSrcweir 		}
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 	// Dann sollte es am Ende der letzten Zeile sein!
215*cdf0e10cSrcweir 	DBG_ASSERT( nChar == maLines[ maLines.Count() - 1 ]->GetEnd(), "Index voll daneben!" );
216*cdf0e10cSrcweir 	DBG_ASSERT( !bInclEnd, "Zeile nicht gefunden: FindLine" );
217*cdf0e10cSrcweir 	return ( maLines.Count() - 1 );
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir void TEParaPortion::CorrectValuesBehindLastFormattedLine( sal_uInt16 nLastFormattedLine )
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir 	sal_uInt16 nLines = maLines.Count();
224*cdf0e10cSrcweir 	DBG_ASSERT( nLines, "CorrectPortionNumbersFromLine: Leere Portion?" );
225*cdf0e10cSrcweir 	if ( nLastFormattedLine < ( nLines - 1 ) )
226*cdf0e10cSrcweir 	{
227*cdf0e10cSrcweir 		const TextLine* pLastFormatted = maLines[ nLastFormattedLine ];
228*cdf0e10cSrcweir 		const TextLine* pUnformatted = maLines[ nLastFormattedLine+1 ];
229*cdf0e10cSrcweir 		short nPortionDiff = pUnformatted->GetStartPortion() - pLastFormatted->GetEndPortion();
230*cdf0e10cSrcweir 		short nTextDiff = pUnformatted->GetStart() - pLastFormatted->GetEnd();
231*cdf0e10cSrcweir 		nTextDiff++;	// LastFormatted->GetEnd() war incl. => 1 zuviel abgezogen!
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 		// Die erste unformatierte muss genau eine Portion hinter der letzten der
234*cdf0e10cSrcweir 		// formatierten beginnen:
235*cdf0e10cSrcweir 		// Wenn in der geaenderten Zeile eine Portion gesplittet wurde,
236*cdf0e10cSrcweir 		// kann nLastEnd > nNextStart sein!
237*cdf0e10cSrcweir 		short nPDiff = sal::static_int_cast< short >(-( nPortionDiff-1 ));
238*cdf0e10cSrcweir 		short nTDiff = sal::static_int_cast< short >(-( nTextDiff-1 ));
239*cdf0e10cSrcweir 		if ( nPDiff || nTDiff )
240*cdf0e10cSrcweir 		{
241*cdf0e10cSrcweir 			for ( sal_uInt16 nL = nLastFormattedLine+1; nL < nLines; nL++ )
242*cdf0e10cSrcweir 			{
243*cdf0e10cSrcweir 				TextLine* pLine = maLines[ nL ];
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 				pLine->GetStartPortion() = pLine->GetStartPortion() + nPDiff;
246*cdf0e10cSrcweir 				pLine->GetEndPortion() = pLine->GetEndPortion() + nPDiff;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 				pLine->GetStart() = pLine->GetStart() + nTDiff;
249*cdf0e10cSrcweir 				pLine->GetEnd() = pLine->GetEnd() + nTDiff;
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 				pLine->SetValid();
252*cdf0e10cSrcweir 			}
253*cdf0e10cSrcweir 		}
254*cdf0e10cSrcweir 	}
255*cdf0e10cSrcweir }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir // -------------------------------------------------------------------------
258*cdf0e10cSrcweir // (+) class TEParaPortions
259*cdf0e10cSrcweir // -------------------------------------------------------------------------
260*cdf0e10cSrcweir TEParaPortions::TEParaPortions()
261*cdf0e10cSrcweir {
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir TEParaPortions::~TEParaPortions()
265*cdf0e10cSrcweir {
266*cdf0e10cSrcweir 	Reset();
267*cdf0e10cSrcweir }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir void TEParaPortions::Reset()
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir 	TEParaPortions::iterator aIter( begin() );
272*cdf0e10cSrcweir 	while ( aIter != end() )
273*cdf0e10cSrcweir 		delete *aIter++;
274*cdf0e10cSrcweir 	clear();
275*cdf0e10cSrcweir }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir // -------------------------------------------------------------------------
278*cdf0e10cSrcweir // (+) class IdleFormatter
279*cdf0e10cSrcweir // -------------------------------------------------------------------------
280*cdf0e10cSrcweir IdleFormatter::IdleFormatter()
281*cdf0e10cSrcweir {
282*cdf0e10cSrcweir 	mpView = 0;
283*cdf0e10cSrcweir 	mnRestarts = 0;
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir IdleFormatter::~IdleFormatter()
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir 	mpView = 0;
289*cdf0e10cSrcweir }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir void IdleFormatter::DoIdleFormat( TextView* pV, sal_uInt16 nMaxRestarts )
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir 	mpView = pV;
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 	if ( IsActive() )
296*cdf0e10cSrcweir 		mnRestarts++;
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir 	if ( mnRestarts > nMaxRestarts )
299*cdf0e10cSrcweir 	{
300*cdf0e10cSrcweir 		mnRestarts = 0;
301*cdf0e10cSrcweir 		((Link&)GetTimeoutHdl()).Call( this );
302*cdf0e10cSrcweir 	}
303*cdf0e10cSrcweir 	else
304*cdf0e10cSrcweir 	{
305*cdf0e10cSrcweir 		Start();
306*cdf0e10cSrcweir 	}
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir void IdleFormatter::ForceTimeout()
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir 	if ( IsActive() )
312*cdf0e10cSrcweir 	{
313*cdf0e10cSrcweir 		Stop();
314*cdf0e10cSrcweir 		mnRestarts = 0;
315*cdf0e10cSrcweir 		((Link&)GetTimeoutHdl()).Call( this );
316*cdf0e10cSrcweir 	}
317*cdf0e10cSrcweir }
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir TYPEINIT1( TextHint, SfxSimpleHint );
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir TextHint::TextHint( sal_uLong Id ) : SfxSimpleHint( Id )
322*cdf0e10cSrcweir {
323*cdf0e10cSrcweir 	mnValue = 0;
324*cdf0e10cSrcweir }
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir TextHint::TextHint( sal_uLong Id, sal_uLong nValue ) : SfxSimpleHint( Id )
327*cdf0e10cSrcweir {
328*cdf0e10cSrcweir 	mnValue = nValue;
329*cdf0e10cSrcweir }
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir TEIMEInfos::TEIMEInfos( const TextPaM& rPos, const String& rOldTextAfterStartPos )
332*cdf0e10cSrcweir : aOldTextAfterStartPos( rOldTextAfterStartPos )
333*cdf0e10cSrcweir {
334*cdf0e10cSrcweir 	aPos = rPos;
335*cdf0e10cSrcweir 	nLen = 0;
336*cdf0e10cSrcweir 	bCursor = sal_True;
337*cdf0e10cSrcweir 	pAttribs = NULL;
338*cdf0e10cSrcweir 	bWasCursorOverwrite = sal_False;
339*cdf0e10cSrcweir }
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir TEIMEInfos::~TEIMEInfos()
342*cdf0e10cSrcweir {
343*cdf0e10cSrcweir 	delete[] pAttribs;
344*cdf0e10cSrcweir }
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir void TEIMEInfos::CopyAttribs( const sal_uInt16* pA, sal_uInt16 nL )
347*cdf0e10cSrcweir {
348*cdf0e10cSrcweir 	nLen = nL;
349*cdf0e10cSrcweir 	delete pAttribs;
350*cdf0e10cSrcweir 	pAttribs = new sal_uInt16[ nL ];
351*cdf0e10cSrcweir 	memcpy( pAttribs, pA, nL*sizeof(sal_uInt16) );
352*cdf0e10cSrcweir }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir void TEIMEInfos::DestroyAttribs()
355*cdf0e10cSrcweir {
356*cdf0e10cSrcweir 	delete pAttribs;
357*cdf0e10cSrcweir 	pAttribs = NULL;
358*cdf0e10cSrcweir     nLen = 0;
359*cdf0e10cSrcweir }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir 
362