Acme::Gtk2::Ex::Builder - Funny Gtk2 Interface Design Module
version 0.008
use strict; use warnings; use Gtk2 -init; use Acme::Gtk2::Ex::Builder; my $app = build { widget Window => contain { info id => 'window'; set title => 'Awesome App'; set default_size => 200, 100; set position => 'center'; on delete_event => sub { Gtk2->main_quit; }; widget Button => contain { set label => 'Action'; on clicked => sub { say 'Seoul Perl Mongers!' }; }; }; }; $app->find('window')->show_all; Gtk2->main;
Find and get widget by ID. You can find widget only you set id with info function.
id
info
my $app = build { widget Window => contain { info id => 'my-window'; }; }; my $window = $app->find('my-window');
This function acts like ordinary "new" method. It is exported by default and returns Acme::Gtk2::Ex::Builder object. It can contains several widget functions.
widget
my $app = build { widget Window; widget Dialog; widget FileChooser; widget VBox; };
This function creates the Gtk2 widget. In fact when you use this, Gtk2::XXX->new will be called. See Gtk2 and Gtk2 API reference.
Gtk2::XXX->new
Following code will call Gtk2::Window->new.
Gtk2::Window->new
my $app = build { widget Window; };
If you need more children widgets, use contain, then call widget again and again.
contain
my $app = build { widget Window contain => { widget HBox => contain { widget Button; widget Button; widget Button; }; }; };
If you have to use more parameters for constructor, then specify additional parameters after the widget block. Following code create Gtk2::SimpleList with additional timestamp, nick and message parameter. See Gtk2 and Gtk2 API reference.
timestamp
nick
message
use Gtk2; use Gtk2::SimpleList; # Do NOT forgot!! my $app = build { widget SimpleList => contain { info id => 'logviewer'; set headers_visible => FALSE; set rules_hint => TRUE; }, ( timestamp => 'markup', nick => 'markup', message => 'markup', ); };
It also supports prebuilt widget.
my $prev_button = Gtk2::Button->new('Prev'); my $next_button = Gtk2::Button->new('Next'); my $quit_button = Gtk2::Button->new; my $app = build { widget VBox => contain { widget HBox => contain { widget $prev_button; widget $next_button; }; widget $next_button => contain { info packing => TRUE, TRUE, 1, 'end'; set label => 'quit'; on clicked => \&quit_clicked; } }; };
This function sets additional information. Since it is not realted to Gtk2 functions, attributes, signal and properties, so save anything what you want or need.
Currently id and packing have some special meanings. id is used for widget method to find widget. packing is used for Gtk2::VBox and Gtk2::HBox.
packing
my $app = build { widget Window => contain { info id => 'window'; set title => 'Seoul.pm irc log viewer'; }; widget HBox => contain { info id => 'hbox'; info packing => TRUE, TRUE, 1, 'start'; widget ScrolledWindow => contain { set policy => 'never', 'automatic'; }; }; };
This function connects signals for specified widget. Actually it is same as $widget->signal_connect. See Gtk2 and Gtk2 API reference.
$widget->signal_connect
my $app = build { widget Window => contain { on delete_event => sub { Gtk2->main_quit }; widget VBox => contain { widget ToggleButton => contain { set label => "show/hide"; on toggled => \&toggled; }; widget Button => contain { set label => 'Quit'; on clicked => sub { Gtk2->main_quit }; }; }; }; };
This function calls $widget->set_KEY(VALUE) function for specified widget. See Gtk2 and Gtk2 API reference.
$widget->set_KEY(VALUE)
my $app = build { widget Window => contain { set title => 'Awesome App'; set default_size => 200, 100; set position => 'center'; }; };
This function sets properties for specified widget. Actually it is same as $widget->set(KEY, VALUE). See Gtk2 and Gtk2 API reference.
$widget->set(KEY, VALUE)
my $app = build { widget Window => contain { info id => 'window'; set position => 'center'; prop title => 'Window Example'; prop opacity => 0.8; prop 'default-width' => 640; prop 'default-height' => 480; on delete_event => \&quit; }; };
This function is used to set attributes, set properties, connect signal, add additional information or contain children widgets.
my $app = build { widget Window => contain { info ... set ... prop ... on ... widget ... }; };
The idea of this module is stealed from Seoul.pm Perl Advent Calendar, 2010-12-24. I think Gtk2::Ex::Builder will be released someday by the article's author. But before the release, this module colud be helpful for you who likes Gtk2 but too lazy to type all code by his/her own hands.
Keedi Kim - 김도형 <keedi@cpan.org>
This software is copyright (c) 2011 by Keedi Kim.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Acme::Gtk2::Ex::Builder, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Acme::Gtk2::Ex::Builder
CPAN shell
perl -MCPAN -e shell install Acme::Gtk2::Ex::Builder
For more information on module installation, please visit the detailed CPAN module installation guide.