Sign in Join

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

Mailing lists / gtk2 / 2008-03

Gtk callbacks connected directly to TObject

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

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

Andrew Haines

If the gtk2 widgetset is split and reworked, please keep in mind it is possible to have methods of object called directly without the need for a plain c function to be called first.

to call a method of an object now we would do something like this:

g_signal_connect(widget, 'signal-name', @mycallback, pointer(MyObject));

mydata can be an object.
which when triggers calls this:

procedure mycallback(widget: PGtkWdget; data: gpointer); cdecl;
var
  AObject: TFooObject;
begin
   AObject := TFooObject(data);
  AObject.SomeMethod(widget);
end;

using

g_signal_connect_data we can make this simpler:

procedure ConnectGtkSignalToObject(SignalWidget: PGtkWidget; TheSignal:
String; TheCallback: Pointer;
var
  AMethod: TMethod;
begin
  AMethod := TMethod(TheCallback);
  g_signal_connect_data(widget, 'signal-name', AMethod.code,
AMethod.data, nil, G_CONNECT_SWAPPED {or G_CONNECT_AFTER});
end;
ConnectGtkSignalToObject(MyGtkButton, 'some-signal',
@MyFooObjectInstance.SomeMethod);
TFooObject.SomeMethod(widget: PGtkWidget); cdecl;
begin
  // dosomething
end;
Doing it this way means that AMethod can be a virtual method which if
overridden will call the decendant's method as expected.

If there are multiple parameters in the callback such as a mouse event it would look like this:

TFooObject.MousePress(Button: gint; X, Y: gint; widget: PGtkWidget); cdecl;

as you can see the normally first parameter(the widget) has become the last parameter on all callbacks.

g_signal_connect_data must be used as opposed to
g_signal_connect_swapped because g_signal_connect_swapped does not give us the freedom to connect before or after the event.

http://library.gnome.org/devel/gobject/unstable/gobject-Signals.html#g-signal-connect-data

Regards,

Andrew

Reply by email