Recent

Author Topic: TListBox and TStringList and LoadFromFile Data Display Issue  (Read 1941 times)

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #15 on: November 28, 2022, 07:16:24 pm »
Ps: Tested the "Linux only" on Windows  :P

Lol.. haha.. oh well I thought it should work seamlessly across platforms that is why I went with the captured data in a text file. So right now all it does when you click a button is load a text file on disk into the TListView so yes it should work on any platform the way it is right now. But once I figure out how to execute an external command through the code then I will have to put in checks as shown below. And then we have loads of fun!  :D

Code: Pascal  [Select][+][-]
  1. {$IFDEF Windows}
  2.      giveKodeZwergAHardTime();
  3.  {$ENDIF Windows}
  4.  
  5.   {$IFDEF Unix}  
  6.     {$IFDEF Darwin}
  7.       // Running on a Mac??
  8.     {$ENDIF Darwin}
  9.  
  10.     {$IFDEF Linux}
  11.       doNotGiveKodeZwergAHardTime();
  12.     {$ENDIF Linux}
  13.  
  14.     {$IFDEF FreeBSD}
  15.      // do something
  16.     {$ENDIF FreeBSD}
  17.  
  18.   {$ENDIF Unix}

The above was shamelessly lifted from https://wiki.freepascal.org/Executing_External_Programs
« Last Edit: November 28, 2022, 07:18:06 pm by Aruna »

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #16 on: November 28, 2022, 07:33:16 pm »
There are lots of comments in the main unit of the project.
Have fun.
Hello @wp I have a small request for you. That function you wrote would you mind commenting on things for me, please? Thank you.

Code: Pascal  [Select][+][-]
  1. function GetTextHeight(AFont: TFont): Integer;
  2. var
  3.   bmp: TBitmap;
  4.   tm: TLCLTextMetric; // FTW is a TextMetric (scratching my head here)
  5. begin
  6.   bmp := TBitmap.Create;
  7.   try
  8.     bmp.SetSize(1, 1);
  9.     bmp.Canvas.Font.Assign(AFont);
  10.     bmp.Canvas.GetTextMetrics(tm);
  11.     Result := abs(tm.Height);
  12.   finally
  13.     bmp.Free;
  14.   end;
  15. end;                        
« Last Edit: November 28, 2022, 07:38:27 pm by Aruna »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #17 on: November 28, 2022, 07:48:08 pm »
The function extracts the font height from the font metric data which is a record defined - I think - in unit Graphics. (there is another metric in units LCLIntf, LCLType). In order to fill this record you need a valid canvas to which the font under investigation is assigned. Controls have a valid canvas only during the paint cycle, and some widgetset even crash your application when the canvas is accessed outside the paint cycle. To avoid this I created a bitmap since it has a canvas that is always accessible. To allocate some memory for the bitmap I set its size to just 1x1 pixel - that's enough to get a valid canvas, and I don't do any painting so that its real size is unimportant. Then I assign the font to the canvas and extract the text metric. The font height usually is a negative number to indicate that it is measured in pixels (not 100% sure...) - therefore I use the abs() function to make it positive.

If you want to learn more about font metrics you are welcome to check out my "FontInspector": https://github.com/wp-xyz/LazSamples/tree/master/graphics/font%20inspector

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2065
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #18 on: November 28, 2022, 08:00:49 pm »
But once I figure out how to execute an external command through the code then I will have to put in checks as shown below. And then we have loads of fun!  :D
It is as easy as open a webpage and read it  8-)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #19 on: November 28, 2022, 08:02:49 pm »
The function extracts the font height from the font metric data which is a record defined - I think - in unit Graphics. (there is another metric in units LCLIntf, LCLType). In order to fill this record you need a valid canvas to which the font under investigation is assigned. Controls have a valid canvas only during the paint cycle, and some widgetset even crash your application when the canvas is accessed outside the paint cycle. To avoid this I created a bitmap since it has a canvas that is always accessible. To allocate some memory for the bitmap I set its size to just 1x1 pixel - that's enough to get a valid canvas, and I don't do any painting so that its real size is unimportant. Then I assign the font to the canvas and extract the text metric. The font height usually is a negative number to indicate that it is measured in pixels (not 100% sure...) - therefore I use the abs() function to make it positive.

If you want to learn more about font metrics you are welcome to check out my "FontInspector": https://github.com/wp-xyz/LazSamples/tree/master/graphics/font%20inspector
I was wondering about that 1 x 1 and that is when I thought I must ask you and now it makes a whole lot of sense. Very clever. Thank you very much I am constantly learning and this makes me very content and happy.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2065
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #20 on: November 28, 2022, 08:10:31 pm »
But once I figure out how to execute an external command through the code then I will have to put in checks as shown below. And then we have loads of fun!  :D
It is as easy as open a webpage and read it  8-)
Ohh, just now I see you already found that link.
Please open a new thread when you having questions about executing programs or want info about how-to for other platforms, I can just help for Windows systems  O:-)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018