From fe743caa958de9c3894548fe84d2c0a341d773ea Mon Sep 17 00:00:00 2001 From: zeljko Date: Mon, 4 Apr 2011 07:53:38 +0000 Subject: [PATCH] Qt: remove redundant line breaks when reading from lfm.Patch by Vladimir Zhirov issue #18278. git-svn-id: trunk@30168 - --- lcl/interfaces/qt/qtprivate.pp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lcl/interfaces/qt/qtprivate.pp b/lcl/interfaces/qt/qtprivate.pp index 99f6e45e63..77a2030cfa 100644 --- a/lcl/interfaces/qt/qtprivate.pp +++ b/lcl/interfaces/qt/qtprivate.pp @@ -89,6 +89,7 @@ type private FTextChanged: Boolean; // Inform TQtMemoStrings about change in TextChange event FStringList: TStringList; // Holds the lines to show + FHasTrailingLineBreak: Boolean; // Indicates whether lines have trailing line break FOwner: TWinControl; // Lazarus Control Owning MemoStrings procedure InternalUpdate; procedure ExternalUpdate(var AStr: WideString; @@ -138,7 +139,7 @@ begin if W <> '' then SetInternalText(UTF16ToUTF8(W)) else - FStringList.Text := ''; + SetInternalText(''); FTextChanged := False; end; @@ -189,18 +190,26 @@ var begin Result := FStringList.Text; - // remove trailing line break - TextLen := Length(Result); - if (TextLen > 0) and (Result[TextLen] = #10) then - Dec(TextLen); - if (TextLen > 0) and (Result[TextLen] = #13) then - Dec(TextLen); - SetLength(Result, TextLen); + // Since TStringList.Text automatically adds line break to the last line, + // we should remove it if original text does not contain it + if not FHasTrailingLineBreak then + begin + TextLen := Length(Result); + if (TextLen > 0) and (Result[TextLen] = #10) then + Dec(TextLen); + if (TextLen > 0) and (Result[TextLen] = #13) then + Dec(TextLen); + SetLength(Result, TextLen); + end; end; procedure TQtMemoStrings.SetInternalText(const Value: string); +var + TextLen: Integer; begin - FStringList.Text := Value + LineEnding; + TextLen := Length(Value); + FHasTrailingLineBreak := (TextLen > 0) and (Value[TextLen] in [#13, #10]); + FStringList.Text := Value; end; {------------------------------------------------------------------------------ @@ -295,6 +304,7 @@ begin WriteLn('TQtMemoStrings.Create Unspecified owner'); {$endif} FStringList := TStringList.Create; + FHasTrailingLineBreak := False; FOwner := TheOwner; end;