Re: Creating custom widget in Perl



On 02/19/01 dLux wrote:
I cannot find info about how to create custom widgets in perl. I saw the
only available example (the source code of Gtk::ColorSelectButton), but

Check also Gtk/samples/widget.pl in the sources.
Some of the perl gimp plugins also use register_subtype.

there are something that are not clear:

- Why UNIVERSAL::can("Gtk::Button","new") is undef? (In other words: Why
  do I need to write my own constructor?

$ perl -MGtk=-init -e 'print UNIVERSAL::can("Gtk::Button","new"), "\n"'
CODE(0x815775c)

You don't need to create your own constructor, just use the new method
in the Gtk::Widget package or the one in the Gtk::Object package.
Having a new constructor in your package makes for cleaner code, IMHO.

- Why does the author use Gtk::Widget->new($pkg,@_) in the constructor,
  not Gtk::Button->new?

Gtk::Button->new() creates a Gtk::Button widget. Gtk::Widget->new()
is used to create instances of subtypes (Gtk::Widget is a base virtual
class, you can't create Gtk::Widget objects...).

- What is Gtk::Button->register_subtype?

register_subtype() is a method in the Gtk::Object package, it will
register a new object type within the Gtk type system.

        Gtk::Button->register_subtype("Gtk::FancyButton");
        Gtk::Window->register_subtype("Gtk::FancyWindow");

will get you this class hierarchy:

        Gtk::Widget
        \...
        |       Gtk::Button
        |       \
        |        + Gtk::FancyButton
        \
         Gtk::Window
         \
          + Gtk::FancyWindow

Hope the ascii art makes sense:-)

- Why we need GTK_CLASS_INIT and GTK_OBJECT_INIT?

If you don't need them, you probably don't need to register
a subtype. Anyway:

GTK_CLASS_INIT() can be used to do perl-type initialization
(for example adding signals or args to a class). This gets called once.

GTK_OBJECT_INIT?() is used for object-specific initialization and
gets called for every object you create in that package.

- What are the "args" good for?

They are used to store data in an object and retreive it with a
simple set/get interface. There is type information stored with the
data as well as access restrictions (read, write, read-write)...
Since we use access functions, the data can also be built on the fly,
using the GTK_OBJECT_SET_ARG()/GTK_OBJECT_GET_ARG() functions.

- What are the usable types for the "args"? I want (for example) use
  an array of integes type. Where I can find info about the
  available types?

From gtktypeutils.c:

    { GTK_TYPE_NONE,            "void" },
    { GTK_TYPE_CHAR,            "gchar" },
    { GTK_TYPE_UCHAR,           "guchar" },
    { GTK_TYPE_BOOL,            "gboolean" },
    { GTK_TYPE_INT,             "gint" },
    { GTK_TYPE_UINT,            "guint" },
    { GTK_TYPE_LONG,            "glong" },
    { GTK_TYPE_ULONG,           "gulong" },
    { GTK_TYPE_FLOAT,           "gfloat" },
    { GTK_TYPE_DOUBLE,          "gdouble" },
    { GTK_TYPE_STRING,          "GtkString" },

For the next three entries you should be able to use the actual
enum/flag/boxed name ("GtkWindowType", "GtkJustification", ...):

    { GTK_TYPE_ENUM,            "GtkEnum" },
    { GTK_TYPE_FLAGS,           "GtkFlags" },
    { GTK_TYPE_BOXED,           "GtkBoxed" },

"GtkObject" works too for any GtkObject derived type (widgets etac.).

Composite perl types are not supported (well, to some extent, see the
"points" arg in Gnome::CanvasLine), but you should be able to easily
fake them in perl with a string... Some support for this may be added
in the future, though...

lupus

-- 
-----------------------------------------------------------------
lupus debian org                                     debian/rules
lupus ximian com                             Monkeys do it better




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