stock browser in gtk2-perl

after searching all weekend to find a bug that was trivially squashed by
calling g_value_dup_boxed instead of g_value_get_boxed, i have just committed
some important updates to gtk2-perl-xs.

Gtk2/gtk-demo/ is a direct gtk2-perl port of the stock browser
demo from the Gtk+ source distribution.

the C version of this sample uses some sophisticated techniques, including
creating a custom boxed type to store the information about each stock item in
a column of the GtkListStore, and using custom data functions (via
gtk_tree_view_column_set_data_func, et al) to find out what to draw in the
treeview widget.

to get this sort of functionality, i added:

* G::Scalar -- new package corresponding to a boxed type GPERL_TYPE_SV at the
bindings level.

* GPerlCallback -- new object in the bindings to ease the use of perl
callbacks in situations which don't cleanly allow the use of GPerlClosure. 
it's a lot like a GClosure but doesn't require being called by something that
uses GValues (typically the case for things which just ask for function

the change that will be most visible to perl developers is the ability to do
things like this (excerpted from

   my $store = Gtk2::ListStore->new ('G::Scalar', 'G::String');
   foreach my $id (sort Gtk2::Stock->list_ids) {
       my $item = Gtk2::Stock->lookup ($id);

       my %info = ....

       my $iter = $store->append;

       # we're putting a hash reference directly into column 0!
       $store->set ($iter, 0, \%info, 1, $id);


   my $column = Gtk2::TreeViewColumn->new;
   $column->set_title ("Macro");

   my $cell_renderer = Gtk2::CellRendererPixbuf->new;
   $column->pack_start ($cell_renderer, FALSE);
   $column->set_cell_data_func ($cell_renderer, \&macro_set_func_pixbuf);
   $cell_renderer = Gtk2::CellRendererText->new;
   $column->pack_start ($cell_renderer, TRUE);
   $column->set_cell_data_func ($cell_renderer, \&macro_set_func_text);


  sub macro_set_func_text {
    my ($tree_column, $cell, $model, $iter) = @_;
    my ($info) = $model->get ($iter, 0);
    $cell->set (text => $info->{macro});
  sub macro_set_func_pixbuf {
    my ($tree_column, $cell, $model, $iter) = @_;
    my ($info) = $model->get ($iter, 0);
    $cell->set (pixbuf => $info->{small_icon});

personally, i think that's really cool.  =)

muppet <scott asofyet org>

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