Recent

Author Topic: [Solved] Question about widgesets of Linux and Windows, for dummies, like me  (Read 4475 times)

devEric69

  • Hero Member
  • *****
  • Posts: 648
I have this example of Delphi-Windows code:

Code: Pascal  [Select][+][-]
  1. DrawText(grdIni.Canvas.Handle, PChar(grdIni.Cells[ACol, ARow]), -1, Rect, DT_NOPREFIX or DT_WORDBREAK);

I want it to be cross-platforms. So, I code:

Code: Pascal  [Select][+][-]
  1. {$If defined (Windows)}
  2.     DrawText(grdIni.Canvas.Handle, PChar(grdIni.Cells[ACol, ARow]), -1, Rect, DT_NOPREFIX or DT_WORDBREAK);
  3. {$ElseIf defined (Unix)}
  4.      // ???
  5. {$EndIf}


My question is for those who have a great general knowledge of *nix (Debian, MacOS, Arch, ...) and their respective drawing interfaces.


[off topic \on]
What I know: under Windows, things are simple: there is only the GDI, and his successor the GDI+. Under the *nix, apparently, everything works with X11, and there are various drawing APIs over X11: GTK, QT,  CARBON, even fpGUI, etc, and LCLIntf Windows.pp (for Windows\GDI).
Of course, things would be easier if there was only one widgetset, but that's the way it is. I know that GNOME and KDE have decided to get together to write, with the best of their ideas, and with the best of their code, a common desktop (probably an API common to the latter), for which they hope it will be the future reference under Linux. But that's a long way.
[off topic \off]


• My first question is: if I code for XFCE (which says that it's a\the minimalist widgeset over X11, based on GTK), does this choice of porting towards *nix increase the chances that an application will work on most *nix?
• And my second question is: how is the best, for coding the GTK Drawtext call, for example? Something like this (knowing the GDI, I admit that I have a bias to find the solution that most closely resembles it, ie with HDC, etc):

Code: Pascal  [Select][+][-]
  1. TGtk2Widgetset.CreateThemeServices.DrawText(.../...);

I know this message is confusing, but my mind is not clear with the different drawing APIs under *nix.

Any links towards coding examples, with a lot of Linux-GTK calls (and if there's a mix with the Windows-GDI equivalent, it would be ideal), are welcome :) .
« Last Edit: December 17, 2019, 04:39:29 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Question about widgesets of Linux, for dummies, like me
« Reply #1 on: December 17, 2019, 01:34:51 pm »
question is: how is the best
Code: Pascal  [Select][+][-]
  1. Canvas.TextOut(...); // already crossplatform
  2. Canvas.TextRect(...); // and cross-widgetset

Everything else is either non-portable and/or doesn't worth efforts (imo) for a task like rendering Grid cells.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Question about widgesets of Linux, for dummies, like me
« Reply #2 on: December 17, 2019, 02:13:03 pm »
Thank you for the answer ("rendering Grid cells" is only one example, to illustrate).

So, if I understood correctly, the logic of Free Pascal is to have made a TCanvas (whith objects - TPen, TBrush - and methods - Draw, ...), which is a cross-platform or cross-widgetset adapter.

@Sash: I see you're on XFCE. Is it to be "more" sure, that if your application is running under a minimalist widgeset, then it will probably run also well everywhere on *nix?
And if the answer is rather yes, if the TCanvas is not enough to do a complicated stuff, do you sometimes need to make specific calls like "TGtk2Widgetset.CreateThemeServices.DrawText(.../...);"?
« Last Edit: December 17, 2019, 02:16:32 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9870
  • Debugger - SynEdit - and more
    • wiki
Re: Question about widgesets of Linux, for dummies, like me
« Reply #3 on: December 17, 2019, 03:04:27 pm »
Without any idea what you need.... hard to say.

Maybe LCLIntf.ExtUTF8Out

function ExtUTF8Out(DC: HDC; X, Y: Integer; Options: Longint; Rect: PRect; Str: PChar; Count: Longint; Dx: PInteger): Boolean;

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Question about widgesets of Linux, for dummies, like me
« Reply #4 on: December 17, 2019, 03:42:08 pm »
Quote
Without any idea what you need.... hard to say.

Indeed, I don't know, myself, what I'm exactly looking for %), except perhaps the locations where concrete calls are switched according to this or that Widgetset target, when calling drawing routines :D .

So, when drawing, I'll use the TCanvas first, then I'll use the WidgetSet methods of LCLIntf ( which have many calls like "Widgeset.DrawThisThing(...)"; ), and which provides the right WidgetSet object created by the "GetDefaultLCLWidgetType" function).
I have seen and understood the basics, concerning how Lazarus remains independent first of a platform, and use the correct Widgeset when drawing, with the same code on the surface  ==> now, I still have to read code from these "strategic" points.

Thank you.
« Last Edit: December 17, 2019, 03:45:23 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Question about widgesets of Linux, for dummies, like me
« Reply #5 on: December 17, 2019, 04:00:05 pm »
@devEric69 : XFCE is a Desktop Environment (DE), it's rather a software subdistribution set. Actual drawing depends on widgetsets (and underlying libs) like gtk or qt. So yes, everything compiled for gtk should run on any binary compatible Linux. And if you use gtk api calls it should be compilable with all gtk DEs on "*nixes" like BSD and whatever else is supported by Lazarus.

In my normal life, I'm rather concerned how it will look on Windows, so I prefer to to have standard controls and
keep my `paintings` at bare minimum level.

Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Question about widgesets of Linux and Windows, for dummies, like me
« Reply #6 on: December 17, 2019, 04:12:17 pm »
Okay, thanks for the clarification. I know I'm "fuzzy" about the boundaries between the different versions of *nix, and the APIs available (gtk, qt, ...), and even between a distribution and its subdistributions sets (from a graphical capabilities point of view). Hence my simplistic assumptions due to lack of knowledge....
« Last Edit: December 17, 2019, 04:15:25 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Question about widgesets of Linux and Windows, for dummies, like me
« Reply #7 on: December 17, 2019, 04:15:51 pm »
Hi!

Keep all your drawings "invisible" until they are done: Use inetrnal drawing surfaces. And at last draw your final bitmap (or whatever) on a visible component with a canvas: an image,  a paintbox, a label or a form - even a stringGrid owns a canvas. I know only some rare cases, where the canvas had a problem. So the same code for different system should not be a problem.

Winni


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: Question about widgesets of Linux and Windows, for dummies, like me
« Reply #8 on: December 17, 2019, 04:18:19 pm »
DevEric69, just use LCL and you don't need to worry about widgetset specific stuff. It is mostly compatible with Delphi VCL which was clearly designed to be cross-platform although it came true only with Kylix/CLX, and now with LCL.
LCLIntf may be needed when porting a Windows app, otherwise not.
To study how the widgetset mapping works, see directory lcl/interfaces.

Remember, Lazarus IDE is done with LCL. Lazarus sources work as a good example code.
« Last Edit: December 17, 2019, 04:23:24 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Question about widgesets of Linux and Windows, for dummies, like me
« Reply #9 on: December 17, 2019, 04:38:39 pm »
Quote
To study how the widgetset mapping works, see directory lcl/interfaces.

I noticed: that's indeed what intrigues me.

"@All", thank you all very much for your advices.
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

wp

  • Hero Member
  • *****
  • Posts: 11916
The attached screenshot was taken from Manjaro Linux/XFCE with qt5 widgetset, and the same output is obtained by gtk2. The text in the left paintbox was written by Canvas.TextRect, and the text in the right paintbox was written by the handle function of your 1st post - code below. You see in the right paintbox: This kind of "windows-style" programming works also in Linux even though the Windows unit does not exist. The trick is to "use" units LCLIntf and LCLType - they make these funtions available (sometimes you need also LCLProc and LMessages (the latter instead of Messages)). If you want to see which ones of these functions are available for the widgetset of your choice go to folder lcl/interfaces of your Lazarus installation, find the folder of that widgetset and open the file <widgetset>winapi.inc (e.g. gtk2winapi.inc for gtk2) or <widgetset>winapih.inc, and study the code.

Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf, LCLType;
  3.  
  4. { TForm1 }
  5.  
  6. const
  7.    s = 'Lorem & ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
  8.  
  9. procedure TForm1.PaintBox1Paint(Sender: TObject);
  10. var
  11.   ts: TTextStyle;
  12.   R: TRect;
  13. begin
  14.    R := Rect(0, 0, Paintbox1.Width, Paintbox1.Height);
  15.    ts := Paintbox1.Canvas.TextStyle;
  16.    ts.Wordbreak := true;
  17.    ts.SingleLine := false;
  18.    ts.ShowPrefix := false;
  19.    Paintbox1.Canvas.TextRect(R, 0, 0, s, ts);
  20. end;
  21.  
  22. procedure TForm1.PaintBox2Paint(Sender: TObject);
  23. var
  24.   flags: Integer;
  25.   R: TRect;
  26. begin
  27.    R := Rect(0, 0, Paintbox2.Width, Paintbox2.Height);
  28.    flags := DT_NOPREFIX or DT_WORDBREAK;
  29.    DrawText(Paintbox2.Canvas.Handle, PChar(s), Length(s), R, flags);
  30. end;

 

TinyPortal © 2005-2018