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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Tom__G

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Marc Santhoff
Am Mittwoch, den 06.08.2008, 12:15 +0200 schrieb 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...) ?

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal