[Glade-users] custom handlers in libglade and gtk builder

Gerald Britton gerald.britton at gmail.com
Fri May 1 11:37:18 EDT 2009


OK.  Thanks to your help before, I've been able to define custom
objects and use them in both glade-3 and GtkBuilder.  Now, I'm working
on using these new types to replace the old custom widgets.  These old
widgets make us of the set_custom_handler function.  The custom
handler that is set by that function, in the current code, on the
value of the field "Creation Function:" in the definition of the
custom object in glade.  So today, I have something like:

        <widget class="Custom" id="lon_entry">
          <property name="visible">True</property>
          <property name="creation_function">ValidatableMaskedEntry</property>
          <property name="int1">0</property>
          <property name="int2">0</property>
          <property name="last_modification_time">Fri, 23 Mar 2007
20:18:43 GMT</property>
        </widget>

Then, I have this code:

from gtk.glade import set_custom_handler

def get_custom_handler(glade, function_name, widget_name,
                       str1, str2, int1, int2):
    if function_name == 'ValidatableMaskedEntry':
        return ValidatableMaskedEntry()
    if function_name == 'StyledTextEditor':
        return StyledTextEditor()

set_custom_handler(get_custom_handler)

Which, as you can see, returns one of two possible "handlers"
depending on the value of the "creation function" field.

In my progress so far towards converting these to GtkBuilder, I've
constructed a custom widget like this:

          <object class="ValidatableMaskedEntry" id="val_mask1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="invisible_char">&#x25CF;</property>
            <property name="text" translatable="yes">valmask</property>
          </object>

and built a python class to handle it:

import gobject
import gtk

class ValidatableMaskedEntry(gtk.Entry):
       __gtype_name__ = 'ValidatableMaskedEntry'

       def __init__(self):
               gtk.Entry.__init__(self)

After setting up the proper env vars and starting glade-3, I can use
my new custom object.  Similarly, at run time, I include the class
definition before calling the add_from_file method on my new glade
file.  At this point, all is well.

So, what I need to grasp is what the set_custom_handler function does
in the old gtk,glade world how to accomplish the same thing in the
GtkBuilder world, while reusing as much existing code as I can.
Unfortunately, I found the docs on set_custom_handler a little on the
terse side.  They state:

"This gtk.glade.set_custom_handler() function allows you to override
the default behaviour when a Custom widget is found in an interface. "

which is great but where is the default behavior defined?

On Fri, May 1, 2009 at 11:07 AM, Tristan Van Berkom
<tristan.van.berkom at gmail.com> wrote:
> Hi,
>   GtkBuilder does not support this method of inserting custom objects
> in the interface, instead GTK+ now allows all widgets to be loadable
> with builder and the way GtkBuilder builds your objects can be customized
> using the GtkBuilder interface.
>
> We assume here that if you are not introducing a real type, if you only
> had a function that created stock widgets manually and returned that;
> you can easily replace that by adding manually built widgets to a known
> container in the project.
>
> With Glade, we have for a long time supported getting your widget
> types into the palette and thats been improving over the years,
> the documentation is pretty up to date but its a little terse (its not
> a tutorial):
>  http://glade.gnome.org/docs/
>
> Keep in mind that its possible to mimic your widget type completely
> by just writing up a few lines of xml, while the docs describe lots of
> things you can do with a plugin library, usually most of that is unneeded.
> Also remember that if you provide the actual type at runtime to Glade
> in the form of a library, Glade will introspect your custom widgets properties
> and signals etc - if you fake the type in the catalog you'll have to define
> properties for it manually in the Glade catalog.
>
> Cheers,
>          -Tristan
>
> On Fri, May 1, 2009 at 10:36 AM, Gerald Britton
> <gerald.britton at gmail.com> wrote:
>> Hi -- I'm converting a project from libglade to gtkbuilder and I've
>> run into a snag with custom objects.  In the code I'm working on there
>> is the line:
>>
>>    from gtk.glade import set_custom_handler
>>
>> followed by a function definition:
>>
>>    def get_custom_handler(glade, function_name, widget_name,
>>                       str1, str2, int1, int2)
>>
>>    ...
>>
>> and
>>
>>    set_custom_handler(get_custom_handler)
>>
>> I need to know how to do the same sort of thing in the gtkbuilder
>> world.  So, how does one set up custom handlers in the gtk.builder
>> world?
>>
>> --
>> Gerald Britton
>> _______________________________________________
>> Glade-users maillist  -  Glade-users at lists.ximian.com
>> http://lists.ximian.com/mailman/listinfo/glade-users
>>
>



-- 
Gerald Britton


More information about the Glade-users mailing list