tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / vcl / source / edit / textdata.cxx
blobbf29c1443de79bba8ca6ab8f1c0bcf5ce1cf6b7a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 #include <osl/diagnose.h>
24 #include <cstddef>
26 #include <utility>
27 #include <vcl/textdata.hxx>
28 #include "textdat2.hxx"
31 TextSelection::TextSelection()
35 TextSelection::TextSelection( const TextPaM& rPaM ) :
36 maStartPaM( rPaM ), maEndPaM( rPaM )
40 TextSelection::TextSelection( const TextPaM& rStart, const TextPaM& rEnd ) :
41 maStartPaM( rStart ), maEndPaM( rEnd )
45 void TextSelection::Justify()
47 if ( maEndPaM < maStartPaM )
49 TextPaM aTemp( maStartPaM );
50 maStartPaM = maEndPaM;
51 maEndPaM = aTemp;
55 TETextPortionList::TETextPortionList()
59 TETextPortionList::~TETextPortionList()
61 Reset();
64 TETextPortion& TETextPortionList::operator[]( std::size_t nPos )
66 return maPortions[ nPos ];
69 std::vector<TETextPortion>::iterator TETextPortionList::begin()
71 return maPortions.begin();
74 std::vector<TETextPortion>::const_iterator TETextPortionList::begin() const
76 return maPortions.begin();
79 std::vector<TETextPortion>::iterator TETextPortionList::end()
81 return maPortions.end();
84 std::vector<TETextPortion>::const_iterator TETextPortionList::end() const
86 return maPortions.end();
89 bool TETextPortionList::empty() const
91 return maPortions.empty();
94 std::size_t TETextPortionList::size() const
96 return maPortions.size();
99 std::vector<TETextPortion>::iterator TETextPortionList::erase( const std::vector<TETextPortion>::iterator& aIter )
101 return maPortions.erase( aIter );
104 std::vector<TETextPortion>::iterator TETextPortionList::insert( const std::vector<TETextPortion>::iterator& aIter,
105 const TETextPortion& rTP )
107 return maPortions.insert( aIter, rTP );
110 void TETextPortionList::push_back( const TETextPortion& rTP )
112 maPortions.push_back( rTP );
115 void TETextPortionList::Reset()
117 maPortions.clear();
120 void TETextPortionList::DeleteFromPortion( std::size_t nDelFrom )
122 SAL_WARN_IF( ( nDelFrom >= maPortions.size() ) && ( (nDelFrom != 0) || (!maPortions.empty()) ), "vcl", "DeleteFromPortion: Out of range" );
123 maPortions.erase( maPortions.begin() + nDelFrom, maPortions.end() );
126 std::size_t TETextPortionList::FindPortion( sal_Int32 nCharPos, sal_Int32& nPortionStart, bool bPreferStartingPortion )
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++ )
132 TETextPortion& rPortion = maPortions[ nPortion ];
133 nTmpPos += rPortion.GetLen();
134 if ( nTmpPos >= nCharPos )
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 ) )
139 nPortionStart = nTmpPos - rPortion.GetLen();
140 return nPortion;
144 OSL_FAIL( "FindPortion: Not found!" );
145 return ( maPortions.size() - 1 );
148 TEParaPortion::TEParaPortion( TextNode* pN )
149 : mpNode {pN}
150 , mnInvalidPosStart {0}
151 , mnInvalidDiff {0}
152 , mbInvalid {true}
153 , mbSimple {false}
157 TEParaPortion::~TEParaPortion()
161 void TEParaPortion::MarkInvalid( sal_Int32 nStart, sal_Int32 nDiff )
163 if ( !mbInvalid )
165 mnInvalidPosStart = ( nDiff >= 0 ) ? nStart : ( nStart + nDiff );
166 mnInvalidDiff = nDiff;
168 else
170 // simple consecutive typing
171 if ( ( nDiff > 0 ) && ( mnInvalidDiff > 0 ) &&
172 ( ( mnInvalidPosStart+mnInvalidDiff ) == nStart ) )
174 mnInvalidDiff = mnInvalidDiff + nDiff;
176 // simple consecutive deleting
177 else if ( ( nDiff < 0 ) && ( mnInvalidDiff < 0 ) && ( mnInvalidPosStart == nStart ) )
179 mnInvalidPosStart = mnInvalidPosStart + nDiff;
180 mnInvalidDiff = mnInvalidDiff + nDiff;
182 else
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;
191 maWritingDirectionInfos.clear();
193 mbInvalid = true;
196 void TEParaPortion::MarkSelectionInvalid( sal_Int32 nStart )
198 if ( !mbInvalid )
200 mnInvalidPosStart = nStart;
202 else
204 mnInvalidPosStart = std::min( mnInvalidPosStart, nStart );
207 maWritingDirectionInfos.clear();
209 mnInvalidDiff = 0;
210 mbInvalid = true;
211 mbSimple = false;
214 std::vector<TextLine>::size_type TEParaPortion::GetLineNumber( sal_Int32 nChar, bool bInclEnd )
216 for ( std::vector<TextLine>::size_type nLine = 0; nLine < maLines.size(); nLine++ )
218 TextLine& rLine = maLines[ nLine ];
219 if ( ( bInclEnd && ( rLine.GetEnd() >= nChar ) ) ||
220 ( rLine.GetEnd() > nChar ) )
222 return nLine;
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 );
232 void TEParaPortion::CorrectValuesBehindLastFormattedLine( sal_uInt16 nLastFormattedLine )
234 sal_uInt16 nLines = maLines.size();
235 SAL_WARN_IF( !nLines, "vcl", "CorrectPortionNumbersFromLine: Empty portion?" );
236 if ( nLastFormattedLine >= ( nLines - 1 ) )
237 return;
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!
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;
253 for ( sal_uInt16 nL = nLastFormattedLine+1; nL < nLines; nL++ )
255 TextLine& rLine = maLines[ nL ];
257 rLine.SetStartPortion(rLine.GetStartPortion() + nPDiff);
258 rLine.SetEndPortion(rLine.GetEndPortion() + nPDiff);
260 rLine.SetStart(rLine.GetStart() + nTDiff);
261 rLine.SetEnd(rLine.GetEnd() + nTDiff);
263 rLine.SetValid();
267 TEParaPortions::~TEParaPortions()
271 IdleFormatter::IdleFormatter()
272 : Idle("vcl::TextEngine mpIdleFormatter")
273 , mpView(nullptr)
274 , mnRestarts(0)
276 SetPriority(TaskPriority::HIGH_IDLE);
279 IdleFormatter::~IdleFormatter()
281 mpView = nullptr;
284 void IdleFormatter::DoIdleFormat( TextView* pV, sal_uInt16 nMaxRestarts )
286 mpView = pV;
288 if ( IsActive() )
289 mnRestarts++;
291 if ( mnRestarts > nMaxRestarts )
293 mnRestarts = 0;
294 Invoke();
296 else
298 Start();
302 void IdleFormatter::ForceTimeout()
304 if ( IsActive() )
306 Stop();
307 mnRestarts = 0;
308 Invoke();
312 TextHint::TextHint( SfxHintId Id ) : SfxHint( Id ), mnValue(0)
316 TextHint::TextHint( SfxHintId Id, sal_Int32 nValue ) : SfxHint( Id ), mnValue(nValue)
320 TEIMEInfos::TEIMEInfos( const TextPaM& rPos, OUString _aOldTextAfterStartPos )
321 : aOldTextAfterStartPos(std::move(_aOldTextAfterStartPos))
322 , aPos(rPos)
323 , nLen(0)
324 , bWasCursorOverwrite(false)
328 TEIMEInfos::~TEIMEInfos()
332 void TEIMEInfos::CopyAttribs(const ExtTextInputAttr* pA, sal_Int32 nL)
334 nLen = nL;
335 pAttribs.reset( new ExtTextInputAttr[ nL ] );
336 memcpy( pAttribs.get(), pA, nL*sizeof(ExtTextInputAttr) );
339 void TEIMEInfos::DestroyAttribs()
341 pAttribs.reset();
342 nLen = 0;
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */