The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Gtk2Fu - GTK2 Forked Ultimate, a powerful layer on top of Gtk2. (forked from ugtk2.)

INTRODUCTION

gtk2-fu is a layer on top of perl gtk2, that brings power, simplicity and speed of development. It brings additional methods to ease the widget creation among other things. But the most important feature is that it brings you a lot of derivated methods from existing methods, that does exactly the same thing, except that ir returns the widget. Example :

  $window->set_title('foo');  #the normal method
  $window->set_title_('foo');  #the gtk2-fu derivated method

set_title_ does the same things as set_title, but it returns $window.

What's the advantage? you can chain actions ! Look at the 2 code examples below :

  # normal perl-gtk2
  my $window = Gtk2::Window->new();
  my $entry = Gtk2::Entry->new();
  $entry->set_text('foo');
  $entry->set_editable(0);
  $window->add($entry);

  # using gtk2-fu
  my $window = Gtk2::Window->new();
  $window->add(
    Gtk2::Entry->new()
      ->set_text_('foo')
      ->set_editable_(0)
  );

You could even get rid of the $window declaration. The gain can seem to you very minor, but in more complex cases, it allows you to create a complex GUI really easilly. You'll find a bigger example at the end of this documentation

In addition to this feature, gtk2-fu provides a toolbox of useful help methods to create widgets, ask things to the user, and misc other things.

HISTORY

gtk2-fu is a fork of ugtk2 (Ultimate gtk2) which is a port to gtk2 of ugtk (Ultimate gtk), which is an improvment of mygtk, which was the created by pixel at mandrakesoft, to basically address the same issues.

ugtk2 is great, but mandrakesoft specific, and not clean enough for me. The derivated methods that return the widget are written one by one by hand; ugtk2 goal is not to be exhaustive, but reduced to the need of mandrakesoft coders, so it is useless for external coders. In addition, the derivated methods are not object oriented, you need to call the explicitely. And a lot of helpers functions use specific things related to the MandrakeLinux installer, config tools, or Interactive.

OK now to the documentation :

DESCRIPTION

DERIVATED METHODS

Not all methods are wrapped so that they can be called with a trailing _ to return the widget. If you need one that is not yet done, mail me at <dams@gentoo.org>

implemented derivated methods

the derivated methods are implemented only for original methods that return nothing (void). Here is the list of gtk2 classes that have derivated methods implemented:

Glib::Object Gtk2::Object Gtk2::Widget Gtk2::Container Gtk2::Frame Gtk2::Window Gtk2::Box Gtk2::ButtonBox Gtk2::HButtonBox Gtk2::Paned Gtk2::Misc Gtk2::Label Gtk2::Entry Gtk2::ItemFactory

For instance, if you look for Gtk2::Frame in the documentation of gtk2 (http://developer.gnome.org/doc/API/2.0/gtk/GtkFrame.html) or perl-gtk2 (http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/Frame.html), you can see that the methods that return nothing (or void) are : set_label, set_label_widget, set_label_align, set_shadow_type, get_label_align. So, because Gtk2::Frame is in the list above, you can use set_label_, set_label_widget_, set_label_align_, set_shadow_type_, get_label_align_, which will do the same as the original methods, but return the frame.

METHODS for creation

These methods return a widget, ready to use. to use them you need to do :

  use Gtk2Fu qw(:create);
or 

  use Gtk2Fu qw(:all);
create_window()
 $widget = create_window($title, $type='toplevel', $resizable=1, $modal=0, $border_width=0, $delete_event=sub{})

Creates a window with specified properties.

create_entry()
  $entry = create_entry($text)

Creates an entry with a default text if provided.

create_full_menubar()
  $menubar = $window->create_full_menubar($menu_items=
                                          [
                                            [ "/_File" ,undef,0 ,0 ,"<Branch>" ],
                                            [ "/File/_New" ,"<control>N" ,\&callback,0 ,"<StockItem>" ,"gtk-new" ,"create a new file" ],
                                          ],
                                          $start_path='<main>');
  ($menubar, $factory) = $window->create_full_menubar($menu_items=
                                          [
                                            [ "/_File" ,undef,0 ,0 ,"<Branch>" ],
                                            [ "/File/_New" ,"<control>N" ,\&callback,0 ,"<StockItem>" ,"gtk-new" ,"create a new file" ],
                                          ],
                                          $start_path='<main>');

Creates a full menubar with menuitems of any type, and shortcuts. In scalar context, returns the menubar ready to be added in a box for instance. In list context, returns the menubar widget, and the factory, for advanced use. $start_path is optional.

create_scrolled_window()
  $viewport = $widget->create_scrolled_window($policy=['automatic','automatic'], $viewport_shadow)

Creates a scrolled viewport from a given $widget. Returns the viewport

METHODS for interaction with user

These methods create some widgets, launch a main loop, then return a value.

The ask_from_dialog methods display a dialog box and returns a value corresponding to the button clicked. The dialogs can take $options. $options is a HASHREF with keys/values tha can be :

stock => STRING

an icon for the dialog box

cancel => BOOLEAN

if set to 1, a cancel button will be added

width => INTEGER

set a specific width to the dialog box

width => INTEGER

set a specific height to the dialog box

transient => BOOLEAN

set the transient state of the dialog box

use_markup => BOOLEAN

use markup or not

ask_from_info_dialog()
  $widget = ask_from_info_dialog($title, $label, $options={});

Information dialog

ask_from_warn_dialog()
 $widget = ask_from_warn_dialog($title, $label, {});

Warning dialog

ask_from_err_dialog()
 $widget = ask_from_err_dialog($title, $label, {});

Error dialog

ask_from_dialog()
  $widget = ask_from_dialog($title, $label, {});

Normal dialog

toolbox METHODS

Packing helpers methods. The methods aim to replace the pack_start and pack_end methods.

gtkpack()
  $widget = gtkpack($box, $widget1, $widget2, ...)

Packs $widgets in $box, with expand set to 1 and fill set to 1

gtkpack_()
  $widget = gtkpack_($box, 0 => $widget1, 1 => $widget2 ...)

Packs $widgets in $box, with fill set to 1. For each widget, the boolean set the expand property in the packing.

gtkpack__()
  $widget = gtkpack__($box, $widget1, $widget2, ...)

Packs $widgets in $box, with expand set to 0 and fill set to 1

gtkpack2()
  $widget = gtkpack2($box, $widget1, $widget2, ...)

Packs $widgets in $box, with expand set to 1 and fill set to 0

gtkpack2_()
  $widget = gtkpack2_($box, 0 => $widget1, 1 => $widget2 ...)

Packs $widgets in $box, with fill set to 0. For each widget, the boolean set the expand property in the packing.

gtkpack2__()
  $widget = gtkpack2__($box, $widget1, $widget2, ...)

Packs $widgets in $box, with expand set to 0 and fill set to 0

miscellaneous METHODS

miscellaneous helpers methods.

gtkflush()

flushes the pending iterations

EXAMPLE

  my $w = create_window()
    ->signal_connect_(destroy => sub { Gtk2->main_quit })
    ->set_size_request_(600, 450)
    ->set_border_width_(5)
    ->add_(Gtk2::VBox->new(0,5)
           ->gtkpack_(0, new Gtk2::HBox(0, 0)
                      ->gtkpack_(1, my $darea = Gtk2::DrawingArea->new()
                                    ->signal_connect_(expose_event => \&draw_banner),
                                 0, new Gtk2::VBox(0, 0)
                                    ->gtkpack_(1, '', 0, $arrow_button)
                                ),
                      1, Gtk2::HPaned->new()
                      ->pack1_(new Gtk2::VBox(0, 5)
                               ->gtkpack_(0, new Gtk2::HBox(0, 5)
                                             ->gtkpack_(0, my $search_entry = Gtk2::Entry->new(),
                                                        0, my $search_ok = Gtk2::Button->new('search'),
                                                        0, my $search_label = Gtk2::Label->new(),
                                                       ),
                                          1, $slist->create_scrolled_window()
                                         ), 1, 0
                              )
                      ->pack2_($hpan2 = new Gtk2::VBox(0, 5)
                               ->gtkpack_(0, Gtk2::HBox->new(0, 5)
                                             ->gtkpack_(0, 'flag name :',
                                                        1, my $flag_name = Gtk2::Entry->new()
                                                       ),
                                          0, Gtk2::HBox->new(0, 5)
                                             ->gtkpack__(my $flag_M = Gtk2::CheckButton->new('')
                                                           ->set_sensitive_(0),
                                                         'set in make.defaults'),
                                          0, Gtk2::HBox->new(0, 5)
                                             ->gtkpack__(my $flag_U = Gtk2::CheckButton->new('')
                                                           ->set_sensitive_(0),
                                                         'set in use.defaults'),
                                          0, Gtk2::HBox->new(0, 5)
                                             ->gtkpack__(my $flag_C = Gtk2::CheckButton->new('')
                                                           ->set_sensitive_(0),
                                                         'set in make.conf'),
                                          0, Gtk2::Label->new('description :')
                                             ->set_alignment_(0, 0.5),
                                          1, Gtk2::Frame->new()
                                             ->set_shadow_type_('in')
                                             ->add_($flag_description)
                                         ),
                               1, 0)->set_position_(400),
                      0, Gtk2::HButtonBox->new()
                         ->set_layout_('end')
                         ->gtkpack(Gtk2::Button->new('Cancel')
                                     ->signal_connect_(clicked => sub { Gtk2->main_quit }),
                                   Gtk2::Button->new('OK')
                                     ->signal_connect_(clicked => sub { main_ok() })
                                  )
                     )
          )
    ->show_all_();
  $darea->size(-1, 38);

COPYRIGHT

Copyright (c) 2004-2005 Damien Krotkine. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 224:

You forgot a '=back' before '=head2'

Around line 260:

'=item' outside of any '=over'