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 ].get();
66 }
67 
68 std::vector<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::begin()
69 {
70  return maPortions.begin();
71 }
72 
73 std::vector<std::unique_ptr<TETextPortion>>::const_iterator TETextPortionList::begin() const
74 {
75  return maPortions.begin();
76 }
77 
78 std::vector<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::end()
79 {
80  return maPortions.end();
81 }
82 
83 std::vector<std::unique_ptr<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<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::erase( const std::vector<std::unique_ptr<TETextPortion>>::iterator& aIter )
99 {
100  return maPortions.erase( aIter );
101 }
102 
103 std::vector<std::unique_ptr<TETextPortion>>::iterator TETextPortionList::insert( const std::vector<std::unique_ptr<TETextPortion>>::iterator& aIter,
104  std::unique_ptr<TETextPortion> pTP )
105 {
106  return maPortions.insert( aIter, std::move(pTP) );
107 }
108 
109 void TETextPortionList::push_back( std::unique_ptr<TETextPortion> pTP )
110 {
111  maPortions.push_back( std::move(pTP) );
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* pPortion = maPortions[ nPortion ].get();
132  nTmpPos += pPortion->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 - pPortion->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  {
237  const TextLine& rLastFormatted = maLines[ nLastFormattedLine ];
238  const TextLine& rUnformatted = maLines[ nLastFormattedLine+1 ];
239  std::ptrdiff_t nPortionDiff = rUnformatted.GetStartPortion() - rLastFormatted.GetEndPortion();
240  sal_Int32 nTextDiff = rUnformatted.GetStart() - rLastFormatted.GetEnd();
241  nTextDiff++; // LastFormatted.GetEnd() was inclusive => subtracted one too much!
242 
243  // The first unformatted one has to start exactly one portion past the last
244  // formatted one.
245  // If a portion got split in the changed row, nLastEnd could be > nNextStart!
246  std::ptrdiff_t nPDiff = -( nPortionDiff-1 );
247  const sal_Int32 nTDiff = -( nTextDiff-1 );
248  if ( nPDiff || nTDiff )
249  {
250  for ( sal_uInt16 nL = nLastFormattedLine+1; nL < nLines; nL++ )
251  {
252  TextLine& rLine = maLines[ nL ];
253 
254  rLine.SetStartPortion(rLine.GetStartPortion() + nPDiff);
255  rLine.SetEndPortion(rLine.GetEndPortion() + nPDiff);
256 
257  rLine.SetStart(rLine.GetStart() + nTDiff);
258  rLine.SetEnd(rLine.GetEnd() + nTDiff);
259 
260  rLine.SetValid();
261  }
262  }
263  }
264 }
265 
267 {
268 }
269 
271  : mpView(nullptr)
272  , mnRestarts(0)
273 {
275 }
276 
278 {
279  mpView = nullptr;
280 }
281 
282 void IdleFormatter::DoIdleFormat( TextView* pV, sal_uInt16 nMaxRestarts )
283 {
284  mpView = pV;
285 
286  if ( IsActive() )
287  mnRestarts++;
288 
289  if ( mnRestarts > nMaxRestarts )
290  {
291  mnRestarts = 0;
292  Invoke();
293  }
294  else
295  {
296  Start();
297  }
298 }
299 
301 {
302  if ( IsActive() )
303  {
304  Stop();
305  mnRestarts = 0;
306  Invoke();
307  }
308 }
309 
311 {
312 }
313 
314 TextHint::TextHint( SfxHintId Id, sal_uLong nValue ) : SfxHint( Id ), mnValue(nValue)
315 {
316 }
317 
318 TEIMEInfos::TEIMEInfos( const TextPaM& rPos, const OUString& rOldTextAfterStartPos )
319  : aOldTextAfterStartPos(rOldTextAfterStartPos)
320  , aPos(rPos)
321  , nLen(0)
322  , bWasCursorOverwrite(false)
323 {
324 }
325 
327 {
328 }
329 
330 void TEIMEInfos::CopyAttribs(const ExtTextInputAttr* pA, sal_Int32 nL)
331 {
332  nLen = nL;
333  pAttribs.reset( new ExtTextInputAttr[ nL ] );
334  memcpy( pAttribs.get(), pA, nL*sizeof(ExtTextInputAttr) );
335 }
336 
338 {
339  pAttribs.reset();
340  nLen = 0;
341 }
342 
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~IdleFormatter() override
Definition: textdata.cxx:277
std::size_t GetStartPortion() const
Definition: textdat2.hxx:144
TETextPortion * operator[](std::size_t nPos)
Definition: textdata.cxx:63
SfxHintId
void CopyAttribs(const ExtTextInputAttr *pA, sal_Int32 nL)
Definition: textdata.cxx:330
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:103
TextView * mpView
Definition: textdat2.hxx:251
std::vector< std::unique_ptr< TETextPortion > >::iterator begin()
Definition: textdata.cxx:68
TextHint(SfxHintId nId)
Definition: textdata.cxx:310
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:90
sal_Int32 mnInvalidPosStart
Definition: textdat2.hxx:179
std::vector< std::unique_ptr< TETextPortion > >::iterator end()
Definition: textdata.cxx:78
std::vector< TextLine > maLines
Definition: textdat2.hxx:175
void CorrectValuesBehindLastFormattedLine(sal_uInt16 nLastFormattedLine)
Definition: textdata.cxx:231
TEParaPortion(TextNode *pNode)
Definition: textdata.cxx:147
virtual void Start() override
Activates the timer task.
Definition: idle.cxx:34
void ForceTimeout()
Definition: textdata.cxx:300
bool empty() const
Definition: textdata.cxx:88
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:160
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:195
void DoIdleFormat(TextView *pV, sal_uInt16 nMaxRestarts)
Definition: textdata.cxx:282
void SetEnd(sal_Int32 n)
Definition: textdat2.hxx:140
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:337
void SetStartPortion(std::size_t n)
Definition: textdat2.hxx:143
void Stop()
Definition: scheduler.cxx:593
#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:109
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:318
sal_Int32 GetStart() const
Definition: textdat2.hxx:138
ExtTextInputAttr
void SetPriority(TaskPriority ePriority)
Definition: scheduler.cxx:600
std::vector< std::unique_ptr< TETextPortion > >::iterator erase(const std::vector< std::unique_ptr< TETextPortion >>::iterator &aIter)
Definition: textdata.cxx:98
std::size_t GetEndPortion() const
Definition: textdat2.hxx:147
void SetValid()
Definition: textdat2.hxx:154
const double mnValue