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 "textdat2.hxx"
28 
29 
31 {
32 }
33 
35  maStartPaM( rPaM ), maEndPaM( rPaM )
36 {
37 }
38 
39 TextSelection::TextSelection( const TextPaM& rStart, const TextPaM& rEnd ) :
40  maStartPaM( rStart ), maEndPaM( rEnd )
41 {
42 }
43 
45 {
46  if ( maEndPaM < maStartPaM )
47  {
48  TextPaM aTemp( maStartPaM );
50  maEndPaM = aTemp;
51  }
52 }
53 
55 {
56 }
57 
59 {
60  Reset();
61 }
62 
64 {
65  return maPortions[ nPos ];
66 }
67 
68 std::vector<TETextPortion>::iterator TETextPortionList::begin()
69 {
70  return maPortions.begin();
71 }
72 
73 std::vector<TETextPortion>::const_iterator TETextPortionList::begin() const
74 {
75  return maPortions.begin();
76 }
77 
78 std::vector<TETextPortion>::iterator TETextPortionList::end()
79 {
80  return maPortions.end();
81 }
82 
83 std::vector<TETextPortion>::const_iterator TETextPortionList::end() const
84 {
85  return maPortions.end();
86 }
87 
89 {
90  return maPortions.empty();
91 }
92 
93 std::size_t TETextPortionList::size() const
94 {
95  return maPortions.size();
96 }
97 
98 std::vector<TETextPortion>::iterator TETextPortionList::erase( const std::vector<TETextPortion>::iterator& aIter )
99 {
100  return maPortions.erase( aIter );
101 }
102 
103 std::vector<TETextPortion>::iterator TETextPortionList::insert( const std::vector<TETextPortion>::iterator& aIter,
104  const TETextPortion& rTP )
105 {
106  return maPortions.insert( aIter, rTP );
107 }
108 
110 {
111  maPortions.push_back( rTP );
112 }
113 
115 {
116  maPortions.clear();
117 }
118 
119 void TETextPortionList::DeleteFromPortion( std::size_t nDelFrom )
120 {
121  SAL_WARN_IF( ( nDelFrom >= maPortions.size() ) && ( (nDelFrom != 0) || (!maPortions.empty()) ), "vcl", "DeleteFromPortion: Out of range" );
122  maPortions.erase( maPortions.begin() + nDelFrom, maPortions.end() );
123 }
124 
125 std::size_t TETextPortionList::FindPortion( sal_Int32 nCharPos, sal_Int32& nPortionStart, bool bPreferStartingPortion )
126 {
127  // find left portion at nCharPos at portion border
128  sal_Int32 nTmpPos = 0;
129  for ( std::size_t nPortion = 0; nPortion < maPortions.size(); nPortion++ )
130  {
131  TETextPortion& rPortion = maPortions[ nPortion ];
132  nTmpPos += rPortion.GetLen();
133  if ( nTmpPos >= nCharPos )
134  {
135  // take this one if we don't prefer the starting portion, or if it's the last one
136  if ( ( nTmpPos != nCharPos ) || !bPreferStartingPortion || ( nPortion == maPortions.size() - 1 ) )
137  {
138  nPortionStart = nTmpPos - rPortion.GetLen();
139  return nPortion;
140  }
141  }
142  }
143  OSL_FAIL( "FindPortion: Not found!" );
144  return ( maPortions.size() - 1 );
145 }
146 
148  : mpNode {pN}
149  , mnInvalidPosStart {0}
150  , mnInvalidDiff {0}
151  , mbInvalid {true}
152  , mbSimple {false}
153 {
154 }
155 
157 {
158 }
159 
160 void TEParaPortion::MarkInvalid( sal_Int32 nStart, sal_Int32 nDiff )
161 {
162  if ( !mbInvalid )
163  {
164  mnInvalidPosStart = ( nDiff >= 0 ) ? nStart : ( nStart + nDiff );
165  mnInvalidDiff = nDiff;
166  }
167  else
168  {
169  // simple consecutive typing
170  if ( ( nDiff > 0 ) && ( mnInvalidDiff > 0 ) &&
171  ( ( mnInvalidPosStart+mnInvalidDiff ) == nStart ) )
172  {
173  mnInvalidDiff = mnInvalidDiff + nDiff;
174  }
175  // simple consecutive deleting
176  else if ( ( nDiff < 0 ) && ( mnInvalidDiff < 0 ) && ( mnInvalidPosStart == nStart ) )
177  {
179  mnInvalidDiff = mnInvalidDiff + nDiff;
180  }
181  else
182  {
183  SAL_WARN_IF( ( nDiff < 0 ) && ( (nStart+nDiff) < 0 ), "vcl", "MarkInvalid: Diff out of Range" );
184  mnInvalidPosStart = std::min( mnInvalidPosStart, nDiff < 0 ? nStart+nDiff : nDiff );
185  mnInvalidDiff = 0;
186  mbSimple = false;
187  }
188  }
189 
190  maWritingDirectionInfos.clear();
191 
192  mbInvalid = true;
193 }
194 
195 void TEParaPortion::MarkSelectionInvalid( sal_Int32 nStart )
196 {
197  if ( !mbInvalid )
198  {
199  mnInvalidPosStart = nStart;
200  }
201  else
202  {
203  mnInvalidPosStart = std::min( mnInvalidPosStart, nStart );
204  }
205 
206  maWritingDirectionInfos.clear();
207 
208  mnInvalidDiff = 0;
209  mbInvalid = true;
210  mbSimple = false;
211 }
212 
213 std::vector<TextLine>::size_type TEParaPortion::GetLineNumber( sal_Int32 nChar, bool bInclEnd )
214 {
215  for ( std::vector<TextLine>::size_type nLine = 0; nLine < maLines.size(); nLine++ )
216  {
217  TextLine& rLine = maLines[ nLine ];
218  if ( ( bInclEnd && ( rLine.GetEnd() >= nChar ) ) ||
219  ( rLine.GetEnd() > nChar ) )
220  {
221  return nLine;
222  }
223  }
224 
225  // Then it should be at the end of the last line
226  OSL_ENSURE(nChar == maLines.back().GetEnd(), "wrong Index");
227  OSL_ENSURE(!bInclEnd, "Line not found: FindLine");
228  return ( maLines.size() - 1 );
229 }
230 
231 void TEParaPortion::CorrectValuesBehindLastFormattedLine( sal_uInt16 nLastFormattedLine )
232 {
233  sal_uInt16 nLines = maLines.size();
234  SAL_WARN_IF( !nLines, "vcl", "CorrectPortionNumbersFromLine: Empty portion?" );
235  if ( nLastFormattedLine >= ( nLines - 1 ) )
236  return;
237 
238  const TextLine& rLastFormatted = maLines[ nLastFormattedLine ];
239  const TextLine& rUnformatted = maLines[ nLastFormattedLine+1 ];
240  std::ptrdiff_t nPortionDiff = rUnformatted.GetStartPortion() - rLastFormatted.GetEndPortion();
241  sal_Int32 nTextDiff = rUnformatted.GetStart() - rLastFormatted.GetEnd();
242  nTextDiff++; // LastFormatted.GetEnd() was inclusive => subtracted one too much!
243 
244  // The first unformatted one has to start exactly one portion past the last
245  // formatted one.
246  // If a portion got split in the changed row, nLastEnd could be > nNextStart!
247  std::ptrdiff_t nPDiff = -( nPortionDiff-1 );
248  const sal_Int32 nTDiff = -( nTextDiff-1 );
249  if ( !(nPDiff || nTDiff) )
250  return;
251 
252  for ( sal_uInt16 nL = nLastFormattedLine+1; nL < nLines; nL++ )
253  {
254  TextLine& rLine = maLines[ nL ];
255 
256  rLine.SetStartPortion(rLine.GetStartPortion() + nPDiff);
257  rLine.SetEndPortion(rLine.GetEndPortion() + nPDiff);
258 
259  rLine.SetStart(rLine.GetStart() + nTDiff);
260  rLine.SetEnd(rLine.GetEnd() + nTDiff);
261 
262  rLine.SetValid();
263  }
264 }
265 
267 {
268 }
269 
271  : Idle("vcl::TextEngine mpIdleFormatter")
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_Int32 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:145
SfxHintId
void CopyAttribs(const ExtTextInputAttr *pA, sal_Int32 nL)
Definition: textdata.cxx:331
TextView * mpView
Definition: textdat2.hxx:252
std::vector< TETextPortion >::iterator erase(const std::vector< TETextPortion >::iterator &aIter)
Definition: textdata.cxx:98
TextHint(SfxHintId nId)
Definition: textdata.cxx:311
AlgAtomPtr mpNode
std::unique_ptr< ExtTextInputAttr[]> pAttribs
Definition: textdata.hxx:129
Important idle events to be run before processing drawing events.
void SetStart(sal_Int32 n)
Definition: textdat2.hxx:138
sal_Int32 mnInvalidDiff
Definition: textdat2.hxx:181
void SetEndPortion(std::size_t n)
Definition: textdat2.hxx:147
std::vector< TETextPortion >::iterator end()
Definition: textdata.cxx:78
void push_back(const TETextPortion &aTP)
Definition: textdata.cxx:109
bool IsActive() const
Definition: task.hxx:101
sal_Int32 mnInvalidPosStart
Definition: textdat2.hxx:180
std::vector< TETextPortion > maPortions
Definition: textdat2.hxx:76
std::vector< TextLine > maLines
Definition: textdat2.hxx:176
void CorrectValuesBehindLastFormattedLine(sal_uInt16 nLastFormattedLine)
Definition: textdata.cxx:231
TEParaPortion(TextNode *pNode)
Definition: textdata.cxx:147
void ForceTimeout()
Definition: textdata.cxx:301
bool empty() const
Definition: textdata.cxx:88
sal_Int32 GetEnd() const
Definition: textdat2.hxx:142
sal_Int32 nLen
Definition: textdata.hxx:131
sal_Int32 & GetLen()
Definition: textdat2.hxx:65
void MarkInvalid(sal_Int32 nStart, sal_Int32 nDiff)
Definition: textdata.cxx:160
sal_uInt16 mnRestarts
Definition: textdat2.hxx:253
TETextPortion & operator[](std::size_t nPos)
Definition: textdata.cxx:63
virtual void Invoke() override
Calls the maInvokeHandler with the parameter this.
Definition: timer.cxx:73
void MarkSelectionInvalid(sal_Int32 nStart)
Definition: textdata.cxx:195
sal_uInt32 Id
void DoIdleFormat(TextView *pV, sal_uInt16 nMaxRestarts)
Definition: textdata.cxx:283
void SetEnd(sal_Int32 n)
Definition: textdat2.hxx:141
std::vector< TextLine >::size_type GetLineNumber(sal_Int32 nIndex, bool bInclEnd)
Definition: textdata.cxx:213
std::size_t FindPortion(sal_Int32 nCharPos, sal_Int32 &rPortionStart, bool bPreferStartingPortion=false)
Definition: textdata.cxx:125
void DeleteFromPortion(std::size_t nDelFrom)
Definition: textdata.cxx:119
void DestroyAttribs()
Definition: textdata.cxx:338
void SetStartPortion(std::size_t n)
Definition: textdat2.hxx:144
std::vector< TETextPortion >::iterator begin()
Definition: textdata.cxx:68
void Stop()
Definition: scheduler.cxx:590
#define SAL_WARN_IF(condition, area, stream)
TextPaM maEndPaM
Definition: textdata.hxx:83
std::vector< TEWritingDirectionInfo > maWritingDirectionInfos
Definition: textdat2.hxx:178
TextPaM maStartPaM
Definition: textdata.hxx:82
void Justify()
Definition: textdata.cxx:44
std::size_t size() const
Definition: textdata.cxx:93
UnoViewSharedPtr mpView
TEIMEInfos(const TextPaM &rPos, const OUString &rOldTextAfterStartPos)
Definition: textdata.cxx:319
virtual void Start(bool bStartTimer=true) override
Schedules the task for execution.
Definition: idle.cxx:34
sal_Int32 GetStart() const
Definition: textdat2.hxx:139
ExtTextInputAttr
void SetPriority(TaskPriority ePriority)
Definition: scheduler.cxx:597
std::vector< TETextPortion >::iterator insert(const std::vector< TETextPortion >::iterator &aIter, const TETextPortion &rTP)
Definition: textdata.cxx:103
std::size_t GetEndPortion() const
Definition: textdat2.hxx:148
An idle is a timer to be scheduled immediately.
Definition: idle.hxx:34
void SetValid()
Definition: textdat2.hxx:155
const double mnValue