Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-08

FP/GTK-GLib : Sending/Accessing Callback GPointer data ?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

T. Guilleminot

Hi,

I'd like to be able to pass/access variable(s ?) to/from the "data" part of the "g_signal_connect" function, eg :

    ...
    Procedure MyActionProcedure (widget : pGtkWidget ; data : gpointer)
cdecl;
    begin
      ... // Using data content here...
    end;
    ...
    g_signal_connect (G_OBJECT(pMyButton), 'button-release-event',
G_CALLBACK(@MyActionProcedure), MyData);
    // Passing information here
-------------------------------------------------------------------^^^^^^
    ...

However I'm unable to handle it once MyActionProcedure called.

- Does anyone have a *simple* example to do this ? I can only find C sample on the web.
- Is "data" able to store single or multiple (and different) variable ?
- If multiple variables possible, can they be of different types (integer, string...) ?

Thanks !
Tom

Reply by email

Marc Santhoff
to T. Guilleminot
T. Guilleminot wrote 25 lines
Hi, I'd like to be able to pass/access variable(s ?) to/from the "data" part of the "g_signal_connect" function, eg : ... Procedure MyActionProcedure (widget : pGtkWidget ; data : gpointer) cdecl; begin ... // Using data content here... end; ... g_signal_connect (G_OBJECT(pMyButton), 'button-release-event', G_CALLBACK(@MyActionProcedure), MyData); // Passing information here -------------------------------------------------------------------^^^^^^ ... However I'm unable to handle it once MyActionProcedure called. - Does anyone have a *simple* example to do this ? I can only find C sample on the web. - Is "data" able to store single or multiple (and different) variable ? - If multiple variables possible, can they be of different types (integer, string...) ?

You can pass inside the data-pointer what you like to, from my memory:

type
  TMyDataRec: record
    a: integer;
    b: string;
  end;
  TMyDataObj: class
    procedure DoSomething;
  end;
Procedure MyActionProcedure (widget : pGtkWidget ; data : gpointer)
cdecl;
begin
  { you have to know or check the type of data's content
    and cast it! }
  with data as TMyDataObj do
  begin
    DoSomething;

or

  if (TMyDataRec(data).a = 123) then
  begin
    ...

At the spot where you are passing the pointer the data has to be set up *before* connecting the callback.

var
  MyData: TMyDataRec;
  MyData2: TMyDataObj;
begin
MyData.a := 124;
g_signal_connect (G_OBJECT(pMyButton), 'button-release-event',
	 G_CALLBACK(@MyActionProcedure), MyData);
MyData2 := TMyDataObj.create;

g_signal_connect (G_OBJECT(pMyButton2), { <-- another button } 'button-release-event', G_CALLBACK(@MyActionProcedure), MyData2); { <-- another data item }

The only way to change the data item after assigning would be to hold a reference to it and set it on the fly (e.g. for remembering a state or the like).

HTH,
Marc

Reply by email

Marc Santhoff
to Marc Santhoff

Me again,

I forgot a general note:

If you can, do step back from using plain gtk+, especially the version 1.x.

That one is buggy and dead in sense of noone working at it anymore.

I had a really painful time using plain GTK+1.2 and if I had to decide which library and toolkit today, I'd pick Lazarus/LCL for sure.

You have been warned ;),
Marc

Reply by email