Recent

Author Topic: Can I make a TMemo size itself to the text it contains ?  (Read 9920 times)

Pabdym

  • New Member
  • *
  • Posts: 21
Can I make a TMemo size itself to the text it contains ?
« on: January 03, 2013, 12:36:01 am »
Hello,
I autosize the height as follows :
Code: [Select]
procedure TForm1.Memo1Change(Sender: TObject);
var
    LineHeight : Integer;
    DC         :  HDC;
    SaveFont   :  HFont;
    Metrics    :  TTextMetric;
begin
    DC := GetDC (memo1.Handle );
    SaveFont := SelectObject ( DC, memo1.Font.Handle );
    GetTextMetrics (DC, Metrics);
    SelectObject (DC, SaveFont);
    ReleaseDC ( memo1.Handle, DC );
    LineHeight := Metrics.tmHeight;
    memo1.height := (memo1.Lines.Count +1) * LineHeight;
end;
OK with Windows... but with Linux autosize doesn't work.
Why ?
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
 with Memo1 do begin
  Width  := 150;
  Height := 90;
  Lines.Text := 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
  Showmessage(IntToStr(Lines.Count));
 end;
end;
  • Windows : Result is 19
  • Linux : Result is 1 (?)
Is it a bug ?
Regards.



« Last Edit: January 03, 2013, 12:37:42 am by Pabdym »
Laz 1.1-38979 fpc-2.6.1-20121007-win32
Laz 1.0.4-0_amd64  fpc_2.6.0-120824_amd64 (x86_64-linux-gtk2)  [2013-01-02]

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #1 on: January 03, 2013, 03:16:39 am »
This is because you have no line breaks in your code

This is just one long string that is interpted as 1 line
 Lines.Text := 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

I too am using lazarus in linux enviroment and same is for me.

I if you copy and paste your code into the properties of StringList in properties you will see that it paste as one long string,  thefore you need line break.

Hope this helps
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Stephano

  • New Member
  • *
  • Posts: 32
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #2 on: January 03, 2013, 09:24:31 am »
It looks like a bug. What is the output of Memo1.Lines[ii] for all lines in Windows?

For a workaround, try:
Memo1.Clear;
Memo1.Lines.Add('Your text');

Pabdym

  • New Member
  • *
  • Posts: 21
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #3 on: January 03, 2013, 10:36:27 am »
Tanks for your answers.

This is because you have no line breaks in your code.
The code is the same in Windows and in Linux. How to explain this difference of results : Windows -> 19 and Linux -> 1 ?
I believed that Memo.lines.count sent back the number of lines after shaping of the contents. It is the case for Windows. Not for Linux ? Nevertheless the display is correct under Linux. If I increase the memo.height without modifying its width, I see well 19 lines. If it is not a bug, then how do we obtain the number of lines after the "made automatic formatting" ?

It looks like a bug. What is the output of Memo1.Lines for all lines in Windows?
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  with Memo1 do begin
  Width  := 150;
  Height := 90;
  Lines.Text := 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i : Integer;
begin
  with Memo1 do
    for i := 0 to Lines.Count -1 do showmessage(Lines[i]);
end;
Result correct in Windows :
  • i:= 0; ->  Lorem ipsum dolor sit
  • i:= 1; ->  amet, consectetur
  • i:= 2; ->  adipisicing elit, sed do
  • i:= 3;  -> eiusmod tempor
  • ...
Sorry for the bad quality of my English.
Regards.
« Last Edit: January 03, 2013, 11:38:48 am by Pabdym »
Laz 1.1-38979 fpc-2.6.1-20121007-win32
Laz 1.0.4-0_amd64  fpc_2.6.0-120824_amd64 (x86_64-linux-gtk2)  [2013-01-02]

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #4 on: January 03, 2013, 05:38:35 pm »
Are you using the same values for WordWrap and ScrollBars on Win and Linux?
The behaviour in Linux suggests that you have at least a horizontal scrollbar there (and I guess you don't in Windows).

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #5 on: January 03, 2013, 06:12:57 pm »
On Windows here Memo1.Lines.Count reports 1, the same as Linux.
Are you using the DelimitedText property?
How are you expecting your long single line assigned to the Text property will be parsed into multiple lines by the memo?

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #6 on: January 03, 2013, 07:40:58 pm »
Quote
How are you expecting your long single line assigned to the Text property will be parsed into multiple lines by the memo?

I too wonder the same thing, as I stated there were no linebreak, only coma delimited long string
and even using DelimitedText property? only returns 4 lines, not 19

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Pabdym

  • New Member
  • *
  • Posts: 21
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #7 on: January 04, 2013, 11:13:12 am »
Hello,
Thanks for your help.
I  declared a suspicion of bug.
http://bugs.freepascal.org/view.php?id=23573.
Please look at the attchments on the Lazarus bugtracker. I forgot to specify that I use Ubuntu 10.04  (not Ubuntu 12.04) with a Lazarus 1.04.
Regards.
Laz 1.1-38979 fpc-2.6.1-20121007-win32
Laz 1.0.4-0_amd64  fpc_2.6.0-120824_amd64 (x86_64-linux-gtk2)  [2013-01-02]

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #8 on: January 04, 2013, 12:32:59 pm »
I don't have Linux so I can't test this, but maybe as a workaround you could use this code.

Memo1.WordWrap:= False;
Memo1.Text:= YourLongString;
Memo1.WordWrap:= True;
Lazarus Trunk / fpc 2.6.2 / Win32

Stephano

  • New Member
  • *
  • Posts: 32
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #9 on: January 04, 2013, 12:40:33 pm »
howardpc reports a result of 1 under Windows.

Can somebody else check it under Windows?

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #10 on: January 04, 2013, 01:09:04 pm »
howardpc reports a result of 1 under Windows.

I also have Memo1.Lines.Count = 1 on Windows, which is to be expected when Scrollbars := ssNone and WordWrap := False;
See bugreport.

Bart

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #11 on: January 04, 2013, 01:23:20 pm »
Hello,
Thanks for your help.
I  declared a suspicion of bug.
http://bugs.freepascal.org/view.php?id=23573.
Please look at the attchments on the Lazarus bugtracker. I forgot to specify that I use Ubuntu 10.04  (not Ubuntu 12.04) with a Lazarus 1.04.
Regards.

If you need an imediate solution, you can try SynMemo.

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #12 on: January 04, 2013, 01:57:45 pm »
See notes in the bugtracker.
The way the widgetset handles WordWrap is different under win32 and GTK2/Linux.
Under GTK2/Linux, WordWrap only has a visual effect on the memo, it does not break the lines
Under Windows the lines do break. The OS seems to handle it this way.

So this probably cannot be fixed.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #13 on: January 04, 2013, 02:18:48 pm »
Quote from the docs on TCustomMemo:

Quote
The logical lines (paragraphs) in <var>Lines</var> do not always match the displayed lines.
When WordWrap is True, every paragraph can wrap into multiple display lines.

So this behaviour is also documented...

I closed the bugreport, since this behaviour cannot be altered, and it is documented.

Bart
« Last Edit: January 04, 2013, 02:20:21 pm by Bart »

Pabdym

  • New Member
  • *
  • Posts: 21
Re: Can I make a TMemo size itself to the text it contains ?
« Reply #14 on: January 04, 2013, 02:55:36 pm »
OK.

Thus  to refer to my earlier question, the answer  is "No, I cannot make a TMemo size itself to the text it contains with Linux" ... with this methodologiy %)

Thanks for your comments today, everyone. Regards
« Last Edit: January 04, 2013, 04:12:07 pm by Pabdym »
Laz 1.1-38979 fpc-2.6.1-20121007-win32
Laz 1.0.4-0_amd64  fpc_2.6.0-120824_amd64 (x86_64-linux-gtk2)  [2013-01-02]

 

TinyPortal © 2005-2018