blib/lib/Acme/Gtk2/Ex/Builder.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 161 CC 8 SCAR 28.2
CC
8
Cov
0%
build
56.0
find
6.0
BEGIN
2.0
CRAP 16.8
1 use 5.010;
2 use strict;
3 use warnings;
4 package Acme::Gtk2::Ex::Builder;
5 BEGIN {
6 $Acme::Gtk2::Ex::Builder::VERSION = '0.008';
7 }
8 # ABSTRACT: Funny Gtk2 Interface Design Module
9  
10 use base qw( Exporter );
11  
12 our @EXPORT = qw(
13 build
14 contain
15 info
16 on
17 prop
18 set
19 widget
20 );
21  
22 sub find {
23 my $self = shift;
24 my $id = shift;
25 my $widget = shift;
26
27 if ($widget) {
28 $self->{_widget}{$id} = $widget;
29 }
30
31 return $self->{_widget}{$id};
32 }
33
34 sub _current {
35 my $self = shift;
36 my $up = shift || 0;
37 $self->{_current}[-1 - $up];
38 }
39
40 sub _current_push {
41 my $self = shift;
42 my $widget = shift;
43 push @{ $self->{_current} }, $widget;
44 }
45
46 sub _current_pop {
47 my $self = shift;
48 pop @{ $self->{_current} };
49 }
50
51 sub contain (&) { @_ }
52
53 sub build (&) {
54 my $code = shift;
55
56 my $self = bless {
57 _info => {},
58 _widget => {},
59 _current => [],
60 }, __PACKAGE__;
61
62 no strict 'subs';
63 no warnings 'redefine';
64
65 local *_widget = sub ($&) {
66 my $class = shift;
67 my $_code = shift;
68 my @params = @_;
69
70 my $widget;
71 if (ref($class) && $class->isa("Gtk2::Widget")) {
72 $widget = $class;
73 }
74 else {
75 $widget = "Gtk2::$class"->new(@params);
76 }
77
78 if ($self->_current && ref($self->_current) ne __PACKAGE__) {
79 given (ref $self->_current) {
80 when (/Gtk2::VBox|Gtk2::HBox/) {
81 $self->_current->pack_start($widget, 0, 0, 1);
82 }
83 default {
84 $self->_current->add($widget);
85 }
86 };
87 }
88
89 $self->_current_push( $widget );
90
91 local *_info = sub {
92 my $key = shift;
93 my @values = @_;
94
95 given ($key) {
96 when ('id') {
97 $self->find($values[0], $self->_current);
98 }
99 when ('packing') {
100 given (ref $self->_current(1)) {
101 when (/Gtk2::VBox|Gtk2::HBox/) {
102 $self->_current(1)->set_child_packing($self->_current, @values);
103 }
104 }
105 }
106 default {
107 }
108 }
109 $self->{_info}{$self->_current}{$key} = \@values;
110 };
111
112 local *_on = sub {
113 my $signal = shift;
114 my $_code = shift;
115 my $data = shift;
116
117 if ($self->_current) {
118 $self->_current->signal_connect( $signal => $_code, $data );
119 }
120 };
121
122 local *_set = sub {
123 my $attr = shift;
124 my @para = @_;
125
126 my $method = "set_$attr";
127 if ($self->_current) {
128 $self->_current->$method(@para);
129 }
130 };
131  
132 local *_prop = sub {
133 my $prop = shift;
134 my $value = shift;
135
136 my $method = "set";
137 if ($self->_current) {
138 $self->_current->$method($prop, $value);
139 }
140 };
141
142 $_code->() if defined $_code;
143 $self->_current_pop;
144 };
145
146 $code->();
147
148 return $self;
149 }
150
151 sub _warn {
152 my $syntax = shift;
153 sub { warn "you cannot call '$syntax' directly" };
154 }
155
156 *_widget = _warn 'widget';
157 *_info = _warn 'info';
158 *_on = _warn 'on';
159 *_set = _warn 'set';
160 *_prop = _warn 'prop';
161
162 sub widget { goto &_widget }
163 sub info { goto &_info }
164 sub on { goto &_on }
165 sub set { goto &_set }
166 sub prop { goto &_prop }
167  
168 1;
169  
170  
171 =pod
172  
173 =encoding utf-8
174  
175 =head1 NAME
176  
177 Acme::Gtk2::Ex::Builder - Funny Gtk2 Interface Design Module
178  
179 =head1 VERSION
180  
181 version 0.008
182  
183 =head1 SYNOPSIS
184  
185 use strict;
186 use warnings;
187 use Gtk2 -init;
188 use Acme::Gtk2::Ex::Builder;
189
190 my $app = build {
191 widget Window => contain {
192 info id => 'window';
193 set title => 'Awesome App';
194 set default_size => 200, 100;
195 set position => 'center';
196 on delete_event => sub { Gtk2->main_quit; };
197
198 widget Button => contain {
199 set label => 'Action';
200 on clicked => sub { say 'Seoul Perl Mongers!' };
201 };
202 };
203 };
204
205 $app->find('window')->show_all;
206 Gtk2->main;
207  
208 =head1 METHODS
209  
210 =head2 find
211  
212 Find and get widget by ID.
213 You can find widget only you set C<id> with C<info> function.
214  
215 my $app = build {
216 widget Window => contain {
217 info id => 'my-window';
218 };
219 };
220
221 my $window = $app->find('my-window');
222  
223 =head1 FUNCTIONS
224  
225 =head2 build
226  
227 This function acts like ordinary "new" method.
228 It is exported by default and returns L<Acme::Gtk2::Ex::Builder> object.
229 It can contains several C<widget> functions.
230  
231 my $app = build {
232 widget Window;
233 widget Dialog;
234 widget FileChooser;
235 widget VBox;
236 };
237  
238 =head2 widget
239  
240 This function creates the Gtk2 widget.
241 In fact when you use this, C<< Gtk2::XXX->new >> will be called.
242 See L<Gtk2> and Gtk2 API reference.
243  
244 Following code will call C<< Gtk2::Window->new >>.
245  
246 my $app = build {
247 widget Window;
248 };
249  
250 If you need more children widgets,
251 use C<contain>, then call C<widget> again and again.
252  
253 my $app = build {
254 widget Window contain => {
255 widget HBox => contain {
256 widget Button;
257 widget Button;
258 widget Button;
259 };
260 };
261 };
262  
263 If you have to use more parameters for constructor,
264 then specify additional parameters after the C<widget> block.
265 Following code create L<Gtk2::SimpleList> with
266 additional C<timestamp>, C<nick> and C<message> parameter.
267 See L<Gtk2> and Gtk2 API reference.
268  
269 use Gtk2;
270 use Gtk2::SimpleList; # Do NOT forgot!!
271
272 my $app = build {
273 widget SimpleList => contain {
274 info id => 'logviewer';
275 set headers_visible => FALSE;
276 set rules_hint => TRUE;
277 }, (
278 timestamp => 'markup',
279 nick => 'markup',
280 message => 'markup',
281 );
282 };
283  
284 It also supports prebuilt widget.
285  
286 my $prev_button = Gtk2::Button->new('Prev');
287 my $next_button = Gtk2::Button->new('Next');
288 my $quit_button = Gtk2::Button->new;
289
290 my $app = build {
291 widget VBox => contain {
292 widget HBox => contain {
293 widget $prev_button;
294 widget $next_button;
295 };
296 widget $next_button => contain {
297 info packing => TRUE, TRUE, 1, 'end';
298 set label => 'quit';
299 on clicked => \&quit_clicked;
300 }
301 };
302 };
303  
304 =head2 info
305  
306 This function sets additional information.
307 Since it is not realted to Gtk2 functions,
308 attributes, signal and properties,
309 so save anything what you want or need.
310  
311 Currently C<id> and C<packing> have some special meanings.
312 C<id> is used for C<widget> method to find widget.
313 C<packing> is used for L<Gtk2::VBox> and L<Gtk2::HBox>.
314  
315 my $app = build {
316 widget Window => contain {
317 info id => 'window';
318 set title => 'Seoul.pm irc log viewer';
319 };
320
321 widget HBox => contain {
322 info id => 'hbox';
323 info packing => TRUE, TRUE, 1, 'start';
324
325 widget ScrolledWindow => contain {
326 set policy => 'never', 'automatic';
327 };
328 };
329 };
330  
331 =head2 on
332  
333 This function connects signals for specified widget.
334 Actually it is same as C<< $widget->signal_connect >>.
335 See L<Gtk2> and Gtk2 API reference.
336  
337 my $app = build {
338 widget Window => contain {
339 on delete_event => sub { Gtk2->main_quit };
340 widget VBox => contain {
341 widget ToggleButton => contain {
342 set label => "show/hide";
343 on toggled => \&toggled;
344 };
345 widget Button => contain {
346 set label => 'Quit';
347 on clicked => sub { Gtk2->main_quit };
348 };
349 };
350 };
351 };
352  
353 =head2 set
354  
355 This function calls C<< $widget->set_KEY(VALUE) >> function
356 for specified widget.
357 See L<Gtk2> and Gtk2 API reference.
358  
359 my $app = build {
360 widget Window => contain {
361 set title => 'Awesome App';
362 set default_size => 200, 100;
363 set position => 'center';
364 };
365 };
366  
367 =head2 prop
368  
369 This function sets properties for specified widget.
370 Actually it is same as C<< $widget->set(KEY, VALUE) >>.
371 See L<Gtk2> and Gtk2 API reference.
372  
373 my $app = build {
374 widget Window => contain {
375 info id => 'window';
376 set position => 'center';
377 prop title => 'Window Example';
378 prop opacity => 0.8;
379 prop 'default-width' => 640;
380 prop 'default-height' => 480;
381 on delete_event => \&quit;
382 };
383 };
384  
385 =head2 contain
386  
387 This function is used to set attributes, set properties,
388 connect signal, add additional information or contain children widgets.
389  
390 my $app = build {
391 widget Window => contain {
392 info ...
393 set ...
394 prop ...
395 on ...
396 widget ...
397 };
398 };
399  
400 =head1 SEE ALSO
401  
402 The idea of this module is stealed from
403 L<Seoul.pm Perl Advent Calendar, 2010-12-24|http://advent.perl.kr/2010-12-24.html>.
404 I think L<Gtk2::Ex::Builder> will be released someday by the article's author.
405 But before the release, this module colud be helpful for you
406 who likes L<Gtk2> but too lazy to type all code by his/her own hands.
407  
408 =head1 AUTHOR
409  
410 Keedi Kim - κΉ€λ„ν˜• <keedi@cpan.org>
411  
412 =head1 COPYRIGHT AND LICENSE
413  
414 This software is copyright (c) 2011 by Keedi Kim.
415  
416 This is free software; you can redistribute it and/or modify it under
417 the same terms as the Perl 5 programming language system itself.
418  
419 =cut
420  
421  
422 __END__