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