[Gtk-sharp-list] Using g_object_set from Gtk#

Jonathan Morgan jonmmorgan at gmail.com
Fri Mar 17 18:21:57 EST 2006

On 3/17/06, Mike Kestner <mkestner at novell.com> wrote:
> On Thu, 2006-03-16 at 23:04 +1100, Jonathan Morgan wrote:
> > In Gtk+, g_object_set(obj, prop, value) can be used to set arbitrary
> > properties of an object, meaning that you can do things like replace
> > gtk_show_window with g_object_set(window, "visible", TRUE).
> >
> > Is there any (accessible) binding to this facility in Gtk#?  If not,
> > is there any way to do this in C# on any generic C# object (which
> > would be more generally useful).
> Closest thing is the protected GLib.Object.SetProperty method.  Gtk#
> intentionally hides as much of GLib.Object as possible, exposing its
> functionality in C# syntax (eg window.Visible = true;)
> > I would like this functionality because I am working on a Gtk binding
> > to Mercury, which I would like to have working with both Gtk# and
> > Gtk+, and it would simplify the work if I can specify things once as
> > calling object_set in Mercury, rather than once in C# and once in C.
> Not sure I follow.  Maybe if you can clarify what you are trying to do I
> could suggest alternative approaches?

Mercury compiles to both C and IL, and has foreign language interfaces
to both C# and C.    Any functions not written in Mercury must have
foreign language definitions for both the IL backend and the C
backend, otherwise it will not be able to compile to both.  One option
for making the binding is to write foreign language calls to both C#
and C that are equivalent (eg. in C gtk_show_widget, and in C#
Widget.show).  The other option, which is considerably less work, is
to write a Mercury predicate show_widget that calls
gtk.set_property(Widget, "visible", TRUE), and have set_property
defined in C as calling gtk_object_set, and in C# as some function
call along the lines of set_property(object, property_name, value),
which would have the effect of object.property_name = value (though
property_name would have to be converted from the form this_property
to ThisProperty, but that is trivial).

> > Also, is it possible to pass data to a Gtk# handler and have it passed
> > back to your callback via. the EventArgs parameter (as you can do with
> > the data parameter in Gtk+)?  This would be necessary for storing the
> > Mercury procedure to call from the C# callback I register.
> The Gtk# paradigm for the user_data parameter is to create your delegate
> from an instance method on a class and hold the data member in an
> instance field.

However, that is impossible for Mercury.  Quoting from the reference manual:

"The C# code from C# pragma foreign_proc declarations will be placed
in the bodies of static member functions of an automatically-generated
C# class. Since such C# code will become part of a static member
function, it must not refer to the this keyword.  It may however refer
to static member variables or static member functions declared with
pragma foreign_code."

The way I have implemented it in C is to register the callback with
the data parameter being the Mercury closure to call.  However, it
doesn't sound like this is possible with Gtk#.


More information about the Gtk-sharp-list mailing list