File Coverage

blib/lib/Tk/Gpack.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Tk::Gpack ;
2            
3 1     1   31813 use Exporter ;
  1         2  
  1         46  
4 1     1   411 use Tk::Widget ;
  0            
  0            
5             our @ISA = qw(Exporter Tk::Widget) ;
6             our @EXPORT = qw(gpack xpack spack tpack gplace xplace splace tplace ggrid xgrid sgrid tgrid gunderline _packinate _gridinate _placinate) ;
7             our $VERSION = '0.7' ;
8            
9             package Tk ; # Gleefully pollute the root namespace.
10             Exporter::import qw(Tk::Gpack gpack xpack spack tpack gplace xplace splace tplace ggrid xgrid sgrid tgrid gunderline _packinate _gridinate _placinate);
11            
12             package Tk::Gpack ;
13            
14             sub gpack { # Group Pack
15             ###########
16             my @tp = @_ ; # To pack
17             my $count = 0 ;
18             foreach (@tp) {
19             if ($count % 2) { # If odd
20             $tp[$count - 1]->pack(_packinate($tp[$count])) ;
21             }
22             $count++ ;
23             }
24             }
25            
26             sub tpack { # Target Pack, group pack in a target
27             ###########
28             my $self = shift ;
29             my @tp = @_ ; # To pack
30             my $count = 0 ;
31             foreach (@tp) {
32             if ($count % 2) { # If odd
33             $tp[$count - 1]->pack(_packinate($tp[$count]), -in => $self) ;
34             }
35             $count++ ;
36             }
37             }
38            
39             sub xpack { # Expand Pack
40             ###########
41             my $self = shift ;
42             my $string = shift ;
43             my @options = @_ ;
44             push @options, _packinate($string) ;
45             $self->pack(@options) ;
46             }
47            
48             sub spack { # self pack, assume the object has a data configspec called -geometry
49             #############
50             my $self = shift ;
51             my @options = @_ ;
52             my $string = $self->cget('-geometry') ;
53             $self->xpack($string, @options) ;
54             }
55            
56             sub ggrid { # Group Grid
57             ###########
58             my @tg = @_ ; # To grid
59             my $count = 0 ;
60             foreach (@tg) {
61             if ($count % 2) { # If odd
62             $tg[$count - 1]->grid(_gridinate($tg[$count])) ;
63             }
64             $count++ ;
65             }
66             }
67            
68             sub tgrid { # Group Grid
69             ###########
70             my $self = shift ;
71             my @tg = @_ ; # To grid
72             my $count = 0 ;
73             foreach (@tg) {
74             if ($count % 2) { # If odd
75             $tg[$count - 1]->grid(_gridinate($tg[$count]), -in => $self) ;
76             }
77             $count++ ;
78             }
79             }
80            
81             sub xgrid { # Expand Grid
82             ###########
83             my $self = shift ;
84             my $string = shift ;
85             my @options = @_ ;
86             push @options, _gridinate($string) ;
87             $self->grid(@options) ;
88             }
89            
90             sub sgrid { # self pack, assume the object has a data configspec called -geometry
91             #############
92             my $self = shift ;
93             my @options = @_ ;
94             my $string = $self->cget('-geometry') ;
95             $self->xgrid($string, @options) ;
96             }
97            
98             sub gunderline { # Group underline
99             #################
100             my @tu = @_ ; # too underline
101             my $count = 0 ;
102             foreach (@tu) {
103             if ($count % 2) { # If odd
104             $tu[$count - 1]->configure("-underline" => $tu[$count]) ;
105             }
106             $count++ ;
107             }
108             }
109            
110             sub gplace { # Group place
111             ###########
112             my @tp = @_ ; # To place
113             my $count = 0 ;
114             foreach (@tp) {
115             if ($count % 2) { # If odd
116             $tp[$count - 1]->place(_placinate($tp[$count])) ;
117             }
118             $count++ ;
119             }
120             }
121            
122             sub tplace { # Target Place
123             ###########
124             my $self = shift ;
125             my @tp = @_ ; # To place
126             my $count = 0 ;
127             foreach (@tp) {
128             if ($count % 2) { # If odd
129             $tp[$count - 1]->place(_placinate($tp[$count]), -in => $self) ;
130             }
131             $count++ ;
132             }
133             }
134            
135             sub xplace { # Expand place
136             ###########
137             my $self = shift ;
138             my $string = shift ;
139             my @options = @_ ;
140             push @options, _placinate($string) ;
141             $self->place(@options) ;
142             }
143            
144             sub splace { # self place
145             #############
146             my $self = shift ;
147             my @options = @_ ;
148             my $string = $self->cget('-geometry') ;
149             $self->xplace($string, @options) ;
150             }
151            
152             sub _placinate {
153             ################
154             # -padx and -pady are now ony effective to a single character.
155             my $stringin = shift ;
156             my @stringout ;
157             my $foo = 0 ;
158             ################### Switches
159             my $a = "-anchor" ;
160             my $h = "-height" ;
161             my $w = "-width" ;
162             my $x = "-x" ;
163             my $y = "-y" ;
164             ########################
165             my @chars = reverse(split(//, $stringin)) ; # Read backwards
166             my @buf = () ;
167             foreach(@chars) {
168             if ($_ =~ /[0-9]/) {
169             unshift @buf, $_ ;
170             next ;
171             } else {
172             if ($_ =~ /w/ && scalar(@buf)) { # a -width
173             my $n = join "", @buf ;
174             @buf = () ;
175             push @stringout, ($w => $n) ;
176             next ;
177             } elsif ($_ =~ /h/ && scalar(@buf)) { # -height
178             my $n = join "", @buf ;
179             @buf = () ;
180             push @stringout, ($h => $n) ;
181             next ;
182             } elsif ($_ =~ /x/ && scalar(@buf)) { # -x
183             my $n = join "", @buf ;
184             @buf = () ;
185             push @stringout, ($x => $n) ;
186             next ;
187             } elsif ($_ =~ /y/ && scalar(@buf)) { # -y
188             my $n = join "", @buf ;
189             @buf = () ;
190             push @stringout, ($y => $n) ;
191             next ;
192             } elsif ($_ =~ /a/ && scalar(@buf)) { # -anchor
193             my $n = join "", @buf ;
194             @buf = () ;
195             push @stringout, ($a => $n) ;
196             next ;
197             } else {
198             unshift @buf, $_ ; # Should only be characters preceding an "a"
199             }
200             }
201             }
202             warn @stringout if $foo ;
203             return @stringout ;
204             }
205            
206            
207             sub _packinate {
208             ###############
209             # -padx and -pady are now ony effective to a single character.
210             #
211             my $string = shift ;
212             # warn $string ;
213             my $foo = 0 ;
214             #################### Switches
215             my $x1 = "-expand" ;
216             my $s1 = "-side" ;
217             my $a = "-anchor" ;
218             my $f = "-fill" ;
219             my $X = "-padx" ;
220             my $Y = "-pady" ;
221             #################### Values
222             my $c = "center" ;
223             my $l = "left" ;
224             my $r = "right" ;
225             my $t = "top" ;
226             my $n = "n" ;
227             my $s2 = "s" ;
228             my $e = "e" ;
229             my $w = "w" ;
230             my $y = "y" ;
231             my $x2 = "x" ;
232             my $b1 = "both" ;
233             my $b2 = "bottom" ;
234             my @chars = split(//,$string) ;
235             ####################
236             my $last ;
237             my $count = 0 ;
238             foreach (@chars) { # single characters.
239             if (s/a/$a/) { }
240             elsif (s/f/$f/) { }
241             elsif (s/X/$X/) { }
242             elsif (s/Y/$Y/) { }
243             elsif (s/c/$c/) { }
244             elsif (s/l/$l/) { }
245             elsif (s/r/$r/) { }
246             elsif (s/t/$t/) { }
247             elsif (s/n/$n/) { }
248             elsif (s/e/$e/) { }
249             elsif (s/w/$w/) { }
250             elsif (s/y/$y/) { $foo = 1 ; }
251             elsif ($_ =~ /x/) {if ($last =~ /$f/) {$_ = $x2 ; } else {$_ = $x1 ; } }
252             elsif ($_ =~ /s/) {if ($last =~ /$a/) { $_ = $s2 ; } else {$_ = $s1 ; } }
253             elsif ($_ =~ /b/) {if ($last =~ /$s1/) { $_ = $b2 ; } else {$_ = $b1 ; } }
254             ##########
255             $chars[$count] = $_ ;
256             $last = $_ ;
257             $count++ ;
258             }
259             #
260             $count = 0 ;
261             my @vals ;
262             foreach (@chars) {
263             if ($count % 2) { # If odd
264             push @vals, ($chars[$count - 1] => $chars[$count]) ;
265             }
266             $count++ ;
267             }
268             warn @vals if $foo ;
269             return @vals ;
270             }
271            
272             sub _gridinate {
273             ###############
274             # Untested
275             my $string = shift ;
276             my $row = $string ;
277             my $col = $string ;
278             my $sticky = $string ;
279             my @vals ;
280            
281             $row =~ s/.*r([0-9]+).*/$1/ ; # Keep the numbers that previously followed "r"
282             $col =~ s/.*c([0-9]+).*/$1/ ; #
283             $sticky =~ s/([cr][0-9]+)//g ;# delete all other possible pairs
284             if ($sticky =~ /s/) {
285             $sticky =~ s/s(...)/$1/ ; # allow for sw se etc.
286             $sticky =~ s/^s// ;
287             push @vals, ("-sticky" => $sticky) ;
288             }
289             unshift @vals, ("-row" => $row) ;
290             unshift @vals, ("-column" => $col) ;
291             # warn "$row $col $sticky" ;
292             return @vals ;
293             }
294            
295             1 ;
296            
297             ################## END OF CODE #####################################
298            
299             =head1 NAME
300            
301             Tk::Gpack - Abbreviated geometry arguments for pack, grid and place geometry managers.
302            
303             =head1 DESCRIPTION
304            
305             This module exports four functions for each of the different geometry mananers into the Tk namespace.
306             These functions provide a variety of styles for controlling the indevidual geometry of one,
307             or bulk groups of widgets. Each geometry manager has a series of single letter abbreviations
308             allowing a significant reduction in code, while remaining fairly intuitive.
309            
310             =head1 SYNOPSIS
311            
312             use Tk::Gpack ;
313            
314             gpack, ggrid, and gplace are group packers, they recieve an even numbered list of alternating widgets and abbreviations.
315            
316             gpack($one, 'slan' $two, 'sran' $three, 'slanx1fb') ; # group pack
317             ggrid($one, 'r25c10', $two, 'c9r15', $three, 'c1r1se' ) ; # group grid
318             gplace($one, 'w40h40x120y120anw', $two, 'x40y40ase', $three, 'aww20h20x25y140') ; # group placer
319            
320             tpack, tgrid, and tplace are target packers, and use exactly the same format except they take a preceding target widget, (typically a frame) which will be automatically be used in conjunction with the -in => argument.
321            
322             tpack($FRAME1, $one, 'slan' $two, 'sran' $three, 'slanx1fb') ; # target pack
323             tgrid($TOPLEVEL1, $one, 'r25c10', $two, 'c9r15', $three, 'c1r1se' ) ; # target grid
324             tplace($MW, $one, 'w40h40x120y120anw', $two, 'x40y40ase', $three, 'aww20h20x25y140') ; # target placer
325            
326             xpack xgrid and xplace are expand packers, and used inline as a direct replacement to pack grid and place. The first string passed is the abbreviation string, while anything remaining will be parsed as the standard verbose options.
327            
328             $one->xpack('slan', -in => $FRAME1) ; # expand pack
329             $two->xgrid('r4c4sw', -in => $TOPLEVEL2) ; # expand grid
330             $three->xplace('x20y20aw', -in => $MW) ; # expand place
331            
332             spack sgrid and splace are self packers, they assume that an abbreviation is embedded in the widget as an option called '-geometry'. You must be using derived widgets for this to work, and have defined a configspec '-geometry'. The self packers perform the same as xpack in that they permit additional verbose option pairs to be passed which will be appended to the expansion of the embedded abbreviation. If you are using a default widget geometry as shown below, you can still override it by simply using xpack in place of spack. (spack won't take the abbreviation as an argument) This is particularly handly for templated code. To use spack splace and sgrid do the following:
333            
334             package DerivedButton ;
335             ...
336             sub Populate {
337             $self->ConfigSpecs(-geometry => ['PASSIVE', 'data', 'Data', 'slan']) ; # <------ Abbreviation
338             }
339             #!/usr/bin/perl -w
340             use Tk ;
341             ...
342             my $DButton = $mw->DerivedButton()->spack(-in => $foo) ;
343            
344             Obviously this last example is not complete. Once you've built a derived widget it should make sense though.
345            
346             =head1 DETAILS
347            
348             The abbreviations are fairly intuitive. All supported options are represented by a single character. For the pack geometry manager all passed values are also single characters. For grid and place passed values may be multiple characters. Numeric arguments for grid and place are variable length integers for example. There are a few redundant characters, but they work as expected.
349            
350             NOT ALL OPTIONS TRANSLATE, in this version. (And probably for quite a few versions to come) But the most used ones do. Please review the following translation lists to see How things are supported at this time.
351            
352             =head1 SUPPORTED TRANSLATIONS
353            
354             # OPTIONS pack()
355             ###################
356             x = '-expand'
357             s = '-side'
358             a = '-anchor'
359             f = '-fill'
360             X = '-padx'
361             Y = '-pady'
362            
363             # VALUES pack()
364             ####################
365             c = 'center'
366             l = 'left'
367             r = 'right'
368             t = 'top'
369             n = 'n'
370             s = 's'
371             e = 'e'
372             w = 'w'
373             y = 'y'
374             x = 'x'
375             b = 'both'
376             b = 'bottom'
377            
378             # OPTIONS grid()
379             ####################
380             r = '-row'
381             c = '-column'
382             s = '-sticky'
383            
384             # VALUES grid()
385             ####################
386             n = 'n'
387             s = 's'
388             e = 'e'
389             w = 'w'
390            
391             # OPTIONS place()
392             ####################
393             w = '-width'
394             h = '-height'
395             x = '-x'
396             y = '-y'
397             a = '-anchor'
398            
399             # VALUES place()
400             ####################
401             n = 'n'
402             ne = 'ne'
403             nw = 'nw'
404             s = 's'
405             se = 'se'
406             sw = 'sw'
407             e = 'e'
408             w = 'w'
409            
410             =head1 INSTALLATION
411            
412             To install this module type the following:
413            
414             perl Makefile.PL
415             make
416             make install
417            
418             =head1 DEPENDENCIES
419            
420             use Tk ; # (duh)
421            
422             Not all options currently supported. I've been using this for a while now, and it
423             seems to work OK.
424            
425             =head1 TODO
426            
427             Add more supported options. Tighten up some of the code.
428            
429             =head1 COPYRIGHT AND LICENCE
430            
431             Copyright (C) 2005 IT Operators (http://www.itoperators.com)
432            
433             This library is free software; you can redistribute it and/or modify
434             it under the same terms as Perl itself, either Perl version 5.8.6 or,
435             at your option, any later version of Perl 5 you may have available.
436            
437             =cut
438