LibreOffice Module vcl (master)  1
textdata.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 #include <osl/diagnose.h>
23 
24 #include <cstddef>
25 
26 #include <vcl/textdata.hxx>
27 #include <vcl/window.hxx>
28 #include "textdat2.hxx"
29 
30 
32 {
33 }
34 
36  maStartPaM( rPaM ), maEndPaM( rPaM )
37 {
38 }
39 
40 TextSelection::TextSelection( const TextPaM& rStart, const TextPaM& rEnd ) :
41  maStartPaM( rStart ), maEndPaM( rEnd )
42 {
43 }
44 
46 {
47  if ( maEndPaM < maStartPaM )
48  {
49  TextPaM aTemp( maStartPaM );
51  maEndPaM = aTemp;
52  }
53 }
54 
56 {
57 }
58 
60 {
61  Reset();
62 }
63 
65 {
66  return maPortions[ nPos ].get();
67 }
68 
69 std::vector<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::begin()
70 {
71  return maPortions.begin();
72 }
73 
74 std::vector<std::unique_ptr<TETextPortion>>::const_iterator TETextPortionList::begin() const
75 {
76  return maPortions.begin();
77 }
78 
79 std::vector<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::end()
80 {
81  return maPortions.end();
82 }
83 
84 std::vector<std::unique_ptr<TETextPortion>>::const_iterator TETextPortionList::end() const
85 {
86  return maPortions.end();
87 }
88 
90 {
91  return maPortions.empty();
92 }
93 
94 std::size_t TETextPortionList::size() const
95 {
96  return maPortions.size();
97 }
98 
99 std::vector<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::erase( const std::vector<std::unique_ptr<TETextPortion>>::iterator& aIter )
100 {
101  return maPortions.erase( aIter );
102 }
103 
104 std::vector<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::insert( const std::vector<std::unique_ptr<TETextPortion>>::iterator& aIter,
105  std::unique_ptr<TETextPortion> pTP )
106 {
107  return maPortions.insert( aIter, std::move(pTP) );
108 }
109 
110 void TETextPortionList::push_back( std::unique_ptr<TETextPortion> pTP )
111 {
112  maPortions.push_back( std::move(pTP) );
113 }
114 
116 {
117  maPortions.clear();
118 }
119 
120 void TETextPortionList::DeleteFromPortion( std::size_t nDelFrom )
121 {
122  SAL_WARN_IF( ( nDelFrom >= maPortions.size() ) && ( (nDelFrom != 0) || (!maPortions.empty()) ), "vcl", "DeleteFromPortion: Out of range" );
123  maPortions.erase( maPortions.begin() + nDelFrom, maPortions.end() );
124 }
125 
126 std::size_t TETextPortionList::FindPortion( sal_Int32 nCharPos, sal_Int32& nPortionStart, bool bPreferStartingPortion )
127 {
128  // find left portion at nCharPos at portion border
129  sal_Int32 nTmpPos = 0;
130  for ( std::size_t nPortion = 0; nPortion < maPortions.size(); nPortion++ )
131  {
132  TETextPortion* pPortion = maPortions[ nPortion ].get();
133  nTmpPos += pPortion->GetLen();
134  if ( nTmpPos >= nCharPos )
135  {
136  // take this one if we don't prefer the starting portion, or if it's the last one
137  if ( ( nTmpPos != nCharPos ) || !bPreferStartingPortion || ( nPortion == maPortions.size() - 1 ) )
138  {
139  nPortionStart = nTmpPos - pPortion->GetLen();
140  return nPortion;
141  }
142  }
143  }
144  OSL_FAIL( "FindPortion: Not found!" );
145  return ( maPortions.size() - 1 );
146 }
147 
149  : mpNode {pN}
150  , mnInvalidPosStart {0}
151  , mnInvalidDiff {0}
152  , mbInvalid {true}
153  , mbSimple {false}
154 {
155 }
156 
158 {
159 }
160 
161 void TEParaPortion::MarkInvalid( sal_Int32 nStart, sal_Int32 nDiff )
162 {
163  if ( !mbInvalid )
164  {
165  mnInvalidPosStart = ( nDiff >= 0 ) ? nStart : ( nStart + nDiff );
166  mnInvalidDiff = nDiff;
167  }
168  else
169  {
170  // simple consecutive typing
171  if ( ( nDiff > 0 ) && ( mnInvalidDiff > 0 ) &&
172  ( ( mnInvalidPosStart+mnInvalidDiff ) == nStart ) )
173  {
174  mnInvalidDiff = mnInvalidDiff + nDiff;
175  }
176  // simple consecutive deleting
177  else if ( ( nDiff < 0 ) && ( mnInvalidDiff < 0 ) && ( mnInvalidPosStart == nStart ) )
178  {
180  mnInvalidDiff = mnInvalidDiff + nDiff;
181  }
182  else
183  {
184  SAL_WARN_IF( ( nDiff < 0 ) && ( (nStart+nDiff) < 0 ), "vcl", "MarkInvalid: Diff out of Range" );
185  mnInvalidPosStart = std::min( mnInvalidPosStart, nDiff < 0 ? nStart+nDiff : nDiff );
186  mnInvalidDiff = 0;
187  mbSimple = false;
188  }
189  }
190 
191  maWritingDirectionInfos.clear();
192 
193  mbInvalid = true;
194 }
195 
196 void TEParaPortion::MarkSelectionInvalid( sal_Int32 nStart )
197 {
198  if ( !mbInvalid )
199  {
200  mnInvalidPosStart = nStart;
201  }
202  else
203  {
204  mnInvalidPosStart = std::min( mnInvalidPosStart, nStart );
205  }
206 
207  maWritingDirectionInfos.clear();
208 
209  mnInvalidDiff = 0;
210  mbInvalid = true;
211  mbSimple = false;
212 }
213 
214 std::vector<TextLine>::size_type TEParaPortion::GetLineNumber( sal_Int32 nChar, bool bInclEnd )
215 {
216  for ( std::vector<TextLine>::size_type nLine = 0; nLine < maLines.size(); nLine++ )
217  {
218  TextLine& rLine = maLines[ nLine ];
219  if ( ( bInclEnd && ( rLine.GetEnd() >= nChar ) ) ||
220  ( rLine.GetEnd() > nChar ) )
221  {
222  return nLine;
223  }
224  }
225 
226  // Then it should be at the end of the last line
227  OSL_ENSURE(nChar == maLines.back().GetEnd(), "wrong Index");
228  OSL_ENSURE(!bInclEnd, "Line not found: FindLine");
229  return ( maLines.size() - 1 );
230 }
231 
232 void TEParaPortion::CorrectValuesBehindLastFormattedLine( sal_uInt16 nLastFormattedLine )
233 {
234  sal_uInt16 nLines = maLines.size();
235  SAL_WARN_IF( !nLines, "vcl", "CorrectPortionNumbersFromLine: Empty portion?" );
236  if ( nLastFormattedLine >= ( nLines - 1 ) )
237  return;
238 
239  const TextLine& rLastFormatted = maLines[ nLastFormattedLine ];
240  const TextLine& rUnformatted = maLines[ nLastFormattedLine+1 ];
241  std::ptrdiff_t nPortionDiff = rUnformatted.GetStartPortion() - rLastFormatted.GetEndPortion();
242  sal_Int32 nTextDiff = rUnformatted.GetStart() - rLastFormatted.GetEnd();
243  nTextDiff++; // LastFormatted.GetEnd() was inclusive => subtracted one too much!
244 
245  // The first unformatted one has to start exactly one portion past the last
246  // formatted one.
247  // If a portion got split in the changed row, nLastEnd could be > nNextStart!
248  std::ptrdiff_t nPDiff = -( nPortionDiff-1 );
249  const sal_Int32 nTDiff = -( nTextDiff-1 );
250  if ( !(nPDiff || nTDiff) )
251  return;
252 
253  for ( sal_uInt16 nL = nLastFormattedLine+1; nL < nLines; nL++ )
254  {
255  TextLine& rLine = maLines[ nL ];
256 
257  rLine.SetStartPortion(rLine.GetStartPortion() + nPDiff);
258  rLine.SetEndPortion(rLine.GetEndPortion() + nPDiff);
259 
260  rLine.SetStart(rLine.GetStart() + nTDiff);
261  rLine.SetEnd(rLine.GetEnd() + nTDiff);
262 
263  rLine.SetValid();
264  }
265 }
266 
268 {
269 }
270 
272  : mpView(nullptr)
273  , mnRestarts(0)
274 {
276 }
277 
279 {
280  mpView = nullptr;
281 }
282 
283 void IdleFormatter::DoIdleFormat( TextView* pV, sal_uInt16 nMaxRestarts )
284 {
285  mpView = pV;
286 
287  if ( IsActive() )
288  mnRestarts++;
289 
290  if ( mnRestarts > nMaxRestarts )
291  {
292  mnRestarts = 0;
293  Invoke();
294  }
295  else
296  {
297  Start();
298  }
299 }
300 
302 {
303  if ( IsActive() )
304  {
305  Stop();
306  mnRestarts = 0;
307  Invoke();
308  }
309 }
310 
312 {
313 }
314 
315 TextHint::TextHint( SfxHintId Id, sal_uLong nValue ) : SfxHint( Id ), mnValue(nValue)
316 {
317 }
318 
319 TEIMEInfos::TEIMEInfos( const TextPaM& rPos, const OUString& rOldTextAfterStartPos )
320  : aOldTextAfterStartPos(rOldTextAfterStartPos)
321  , aPos(rPos)
322  , nLen(0)
323  , bWasCursorOverwrite(false)
324 {
325 }
326 
328 {
329 }
330 
331 void TEIMEInfos::CopyAttribs(const ExtTextInputAttr* pA, sal_Int32 nL)
332 {
333  nLen = nL;
334  pAttribs.reset( new ExtTextInputAttr[ nL ] );
335  memcpy( pAttribs.get(), pA, nL*sizeof(ExtTextInputAttr) );
336 }
337 
339 {
340  pAttribs.reset();
341  nLen = 0;
342 }
343 
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~IdleFormatter() override
Definition: textdata.cxx:278
std::size_t GetStartPortion() const
Definition: textdat2.hxx:144
TETextPortion * operator[](std::size_t nPos)
Definition: textdata.cxx:64
SfxHintId
void CopyAttribs(const ExtTextInputAttr *pA, sal_Int32 nL)
Definition: textdata.cxx:331
std::vector< std::unique_ptr< TETextPortion > >::iterator insert(const std::vector< std::unique_ptr< TETextPortion >>::iterator &aIter, std::unique_ptr< TETextPortion > pTP)
Definition: textdata.cxx:104
TextView * mpView
Definition: textdat2.hxx:251
std::vector< std::unique_ptr< TETextPortion > >::iterator begin()
Definition: textdata.cxx:69
TextHint(SfxHintId nId)
Definition: textdata.cxx:311
AlgAtomPtr mpNode
std::unique_ptr< ExtTextInputAttr[]> pAttribs
Definition: textdata.hxx:130
sal_uIntPtr sal_uLong
Important idle events to be run before processing drawing events.
void SetStart(sal_Int32 n)
Definition: textdat2.hxx:137
sal_Int32 mnInvalidDiff
Definition: textdat2.hxx:180
void SetEndPortion(std::size_t n)
Definition: textdat2.hxx:146
bool IsActive() const
Definition: task.hxx:91
sal_Int32 mnInvalidPosStart
Definition: textdat2.hxx:179
std::vector< std::unique_ptr< TETextPortion > >::iterator end()
Definition: textdata.cxx:79
std::vector< TextLine > maLines
Definition: textdat2.hxx:175
void CorrectValuesBehindLastFormattedLine(sal_uInt16 nLastFormattedLine)
Definition: textdata.cxx:232
TEParaPortion(TextNode *pNode)
Definition: textdata.cxx:148
virtual void Start() override
Activates the timer task.
Definition: idle.cxx:34
void ForceTimeout()
Definition: textdata.cxx:301
bool empty() const
Definition: textdata.cxx:89
sal_Int32 GetEnd() const
Definition: textdat2.hxx:141
sal_Int32 nLen
Definition: textdata.hxx:132
sal_Int32 & GetLen()
Definition: textdat2.hxx:65
void MarkInvalid(sal_Int32 nStart, sal_Int32 nDiff)
Definition: textdata.cxx:161
sal_uInt16 mnRestarts
Definition: textdat2.hxx:252
std::vector< std::unique_ptr< TETextPortion > > maPortions
Definition: textdat2.hxx:75
virtual void Invoke() override
Calls the maInvokeHandler with the parameter this.
Definition: timer.cxx:73
void MarkSelectionInvalid(sal_Int32 nStart)
Definition: textdata.cxx:196
void DoIdleFormat(TextView *pV, sal_uInt16 nMaxRestarts)
Definition: textdata.cxx:283
void SetEnd(sal_Int32 n)
Definition: textdat2.hxx:140
std::vector< TextLine >::size_type GetLineNumber(sal_Int32 nIndex, bool bInclEnd)
Definition: textdata.cxx:214
std::size_t FindPortion(sal_Int32 nCharPos, sal_Int32 &rPortionStart, bool bPreferStartingPortion=false)
Definition: textdata.cxx:126
void DeleteFromPortion(std::size_t nDelFrom)
Definition: textdata.cxx:120
void DestroyAttribs()
Definition: textdata.cxx:338
void SetStartPortion(std::size_t n)
Definition: textdat2.hxx:143
void Stop()
Definition: scheduler.cxx:587
#define SAL_WARN_IF(condition, area, stream)
TextPaM maEndPaM
Definition: textdata.hxx:84
std::vector< TEWritingDirectionInfo > maWritingDirectionInfos
Definition: textdat2.hxx:177
TextPaM maStartPaM
Definition: textdata.hxx:83
void push_back(std::unique_ptr< TETextPortion > pTP)
Definition: textdata.cxx:110
void Justify()
Definition: textdata.cxx:45
std::size_t size() const
Definition: textdata.cxx:94
UnoViewSharedPtr mpView
TEIMEInfos(const TextPaM &rPos, const OUString &rOldTextAfterStartPos)
Definition: textdata.cxx:319
sal_Int32 GetStart() const
Definition: textdat2.hxx:138
ExtTextInputAttr
void SetPriority(TaskPriority ePriority)
Definition: scheduler.cxx:594
std::vector< std::unique_ptr< TETextPortion > >::iterator erase(const std::vector< std::unique_ptr< TETextPortion >>::iterator &aIter)
Definition: textdata.cxx:99
std::size_t GetEndPortion() const
Definition: textdat2.hxx:147
void SetValid()
Definition: textdat2.hxx:154
const double mnValue