Glade Custom Widgets

From LSDevLinux
Jump to: navigation, search

Glade and GTK# are a powerful combination. One that we are using for our Mono re-implementation of WorldPad. It's quite possible to build a very functional application using just the standard widgets provided by GTK, but sometimes specific functionality is needed which is not found in the regular set of widgets.

In this instance, a "custom widget" can be inserted into a Glade interface. This is easy to do, but filling in the custom widget with something useful is a bit more tricky.

How do we implement the custom widget?

In our case, we needed a custom colour picking widget. (Note that GTK does have a colour selection button, but we wanted something simpler, to more closely follow WorldPad). All that is needed is to stick a custom widget from the "GTK+ Additional" set into your window or dialog. You can then set a few generic string and integer properties that can be used when the widget is instantiated.

If you have custom controls in your glade file, an XMLCustomWidgetHandler should be registered and will be called when you run load the Glade file. This function accepts a few parameters, most usefully the name of the widget and the properties that you may have defined in Glade.

To register the handler, create a method (we called ours on_custom_widget_found()*) and set the static property Glade.XML.CustomWidgetHandler to a new XMLCustomWidgetHandler constructed with the above method as a parameter

In our program, we simply took the name of the widget and used it to decide which widget to create and set up**. The widget is then returned by the function, et voila! Any widget you can construct using GTK# can appear in your Glade application.

*Gtk.Widget on_custom_widget_found(XML xml, string func_name, string name, string string1, string string2, int int1, int int2)

**Each widget was constructed, had an event attached, and was displayed using Show().