gtk2-perl / message for other developers



Hi,

I'd like to sum up the results of my developing on gtk2-perl of
the recent days, to underline the consequences on the other
developers.

Basically, before adding more widgets, I'm working on factorizing
existing code by using macros or functions, and ameliorating type
checking.


[*] Converting Perl objects to Gtk Objects

Previous situation was code like:

        (GtkWindow*) SvIV(SvRV(window))

Everywhere... not very good. Now, macros are provided:

        SvGtkWindow(window)

In addition to perform the same conversion and cast, it also does
automatic type checking, thanks to Daniel's "get_class" function
(through Gtk2/src/_Helpers.c:gtk2_perl_check_type); similar to
what was done by gtk-perl, if the class is not derived from
GtkWindow, Perl will die with:

FATAL: variable is not of type Gtk2::Window at <program.pl> line <line>.

I've substituted all occurrences of the old form in the CVS code,
with the new form. When adding widgets or methods, please do use
the macros.

Please note that Goran's choice for glib objects was to put them
with full name under the Gtk2 package (for example,
Gtk2::GObject), so we need to keep consistency for the type check
to work; thus I've had to rename "GSList" to "Gtk2::GSList" in
Gtk2/src/RadioButton.c:gtkperl_radio_button_get_group.

Because of that type check, the panedwindow.pl example doesn't
run anymore, seems it doesn't respect the prototypes :). The
other examples seem to work ok.


[*] Enums

I've made preliminary support for type checking on enums (like it
exists in current gtk-perl). Added value is that it enables the
shorter form 'toplevel' instead of Gtk2::Window::TOPLEVEL (the
longer form is still available). This has not yet been extended
to the other enums (I'll do that today hopefully), but it will
very probably be used like for casts from perl objects to gtk
objects, e.g. the developer will use ``SvGtkWindowType(type)''
that will check that type is a member of the GtkWindowType enum,
then give back the gint value of that type. If not, Perl dies:

FATAL: invalid GtkWindowType value topleel, expecting: toplevel / GTK_WINDOW_TOPLEVEL, popup / 
GTK_WINDOW_POPUP at <program.pl> line <line>.


[*] Constructors

In each class, we had very much code redundancy in the
constructors. Always the same 8 lines of code... bad. So now
instead of:

SV* gtkperl_button_new(char* class)
{
    SV *obj_ref, *obj;
    obj_ref = newSViv(0);
    obj = newSVrv(obj_ref, class);
    sv_setiv(obj, (IV) gtk_button_new());
    SvREADONLY_on(obj);
    return obj_ref;
}

Now we use:

SV* gtkperl_button_new(char* class)
{
    return gtk2_perl_new_object(gtk_button_new());
}

Better and more elegant, isn't it? The very interesting thing is
that thanks to Daniel's get_class function, we don't even need to
specify the class name to the function. Though, this is limited
to the GObject's hierarchy; thus, for simpler structs, we need to
use the other form of function call, it's just the same function
but specifying the classname; here's how we use it:

SV* gtkperl_color_selection_get_current_color(SV* colorsel)
{
    GdkColor* c = g_malloc0(sizeof(GdkColor));
    gtk_color_selection_get_current_color(SvGtkColorSelection(colorsel), c);
    return gtk2_perl_new_object_from_pointer(c, "Gtk2::Gdk::Color");
}

Beware, if you call the first form of function on a non-GObject,
get_class will crash trying to extract non-existing
object->g_type_instance. When unsure if an object is really a
GObject, you might verify the object hierarchy or the contents
of genscripts/castmacros-autogen.pl:@additional_not_objects (this
array is probably not exhaustive, though).

I've changed (by hand for safety) the object construction in the
whole source, please use these functions when you'll add widgets
or methods.

I've used the gtk2_perl prefix to not risk to mess with the rest
of the C namespace (C rulz), maybe Daniel you'll want to move
your get_class etc functions also to that naming scheme?


[*] Compiler options

I've pushed for the addition of -Wall -Werror compiler options,
because I think it gives more positive than negative. It made me
track down a number of real problems (mostly typos) in the
existing code, which is good. But actually it gives compilation
errors with perl 5.6.x; and Goran doesn't like it for some code;
so we only have -Wall -Wstrict-prototypes in the CVS. But Goran
provided a toggle with envvar, so I suggest the following form
for people using perl 5.8.x:

# GTK2_PERL_CFLAGS='-Wall -Wstrict-prototypes -Werror' perl compile-widgets.pl


Best regards,

-- 
Guillaume Cottenceau - http://people.mandrakesoft.com/~gc/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]