[fpc-pascal] FP/GTK-GLib : Sending/Accessing Callback GPointer data ?
Marc Santhoff
M.Santhoff at t-online.de
Wed Aug 6 19:39:50 CEST 2008
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
More information about the fpc-pascal
mailing list