Recent

Author Topic: [SOLVED] How to Draw a TCanvas on a GTK2 Window  (Read 2348 times)

aydın

  • Jr. Member
  • **
  • Posts: 77
[SOLVED] How to Draw a TCanvas on a GTK2 Window
« on: October 21, 2023, 09:54:05 pm »
Hi everyone!

For one of my projects, I'm not using the TForm in Lazarus LCL. Instead, I'm writing my own widgetset using pure API.

On Windows, I capture and draw the painting (WM_PAINT) event as follows:
Code: Pascal  [Select][+][-]
  1. Procedure Form_Paint(hwnd: THandle);
  2. Var
  3.   Context: HDC;
  4.   BufCanvas: TCanvas;
  5. Begin
  6.   Context := GetDC(hwnd);
  7.  
  8.   BufCanvas := TCanvas.Create;
  9.   BufCanvas.Handle := Context;
  10.  
  11.   {I make my drawings}
  12.  
  13.   //FIA(BufCanvas);
  14.   BufCanvas.Free;
  15.  
  16.   ReleaseDC(hwnd, Context);
  17. End;

To achieve the same in GTK2, you need to set up a drawing trigger as follows:
Code: Pascal  [Select][+][-]
  1. gtk_signal_connect(GTK_OBJECT(window), 'expose-event', G_CALLBACK(@Form_Paint), nil);

My question is, how can I draw the TCanvas I created on the GTK2 window?

Thank you in advance.
« Last Edit: October 23, 2023, 09:10:28 pm by aydın »
Lazarus 3.0 on Ubuntu 23.04 and Windows 11

aydın

  • Jr. Member
  • **
  • Posts: 77
Re: How to Draw a TCanvas on a GTK2 Window
« Reply #1 on: October 22, 2023, 09:48:31 am »
Hello again.

I've examined Lcl a bit, and although I don't fully understand its logic, I managed to create a working system.

After creating my window, I set up the trigger as follows:
Code: Pascal  [Select][+][-]
  1. gtk_signal_connect(GTK_OBJECT(PGtkWidget(Result)), 'expose-event', G_CALLBACK(@onDrawEvent),

And here is my drawing code:
Code: Pascal  [Select][+][-]
  1. function onDrawEvent(widget: PGtkWidget; event: PGdkEvent; user_data: gpointer): gboolean; cdecl;
  2. var
  3.   Bmp: TCanvas;
  4.   Context: HDC;
  5. begin
  6.   Context:= InterfaceBase.WidgetSet.GetDC(THandle(widget));
  7.  
  8.   Bmp:= TCanvas.Create;
  9.   Bmp.Handle:= Context;
  10.  
  11.   // Sample drawings
  12.   Bmp.Pen.Color:= clBlue;
  13.   Bmp.Pen.Width:= 2;
  14.   Bmp.Line(10, 10, 100, 100);
  15.  
  16.   // FIA(Bmp);
  17.   Bmp.Free;
  18.  
  19.   InterfaceBase.WidgetSet.ReleaseDC(THandle(widget), Context);
  20.  
  21.   Result:= True;
  22. end;

In fact, it's almost identical; I hadn't thought of converting PGtkWidget to THandle.

I hope it's useful to someone.
« Last Edit: October 22, 2023, 09:51:09 am by aydın »
Lazarus 3.0 on Ubuntu 23.04 and Windows 11

 

TinyPortal © 2005-2018