File Coverage

blib/lib/Gtk2/Ex/Utils.pm
Criterion Covered Total %
statement 17 19 89.4
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Gtk2::Ex::Utils;
2             ###############################################################################
3             # Gtk2::Ex::Utils - Extra Gtk2 Utilities for working with Gnome2/Gtk2 in Perl.
4             # Copyright (C) 2005 Open Door Software Inc.
5             #
6             # This library is free software; you can redistribute it and/or
7             # modify it under the terms of the GNU Lesser General Public
8             # License as published by the Free Software Foundation; either
9             # version 2.1 of the License, or (at your option) any later version.
10             #
11             # This library is distributed in the hope that it will be useful,
12             # but WITHOUT ANY WARRANTY; without even the implied warranty of
13             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14             # Lesser General Public License for more details.
15             #
16             # You should have received a copy of the GNU Lesser General Public
17             # License along with this library; if not, write to the Free Software
18             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19             ###############################################################################
20 1     1   38284 use strict;
  1         2  
  1         47  
21              
22             BEGIN {
23 1     1   6 use Exporter;
  1         2  
  1         48  
24 1     1   5 use vars qw( $VERSION @ISA @EXPORT_OK %EXPORT_TAGS );
  1         6  
  1         148  
25 1     1   2 $VERSION = '0.09';
26 1         26 @ISA = qw( Exporter );
27 1         2 @EXPORT_OK = qw( );
28 1         3 $EXPORT_TAGS{all} = [ qw( ) ];
29 1         2 $EXPORT_TAGS{main} = [ qw( ) ];
30 1         8 $EXPORT_TAGS{format} = [ qw( ) ];
31 1         23 $EXPORT_TAGS{create} = [ qw( ) ];
32             }
33              
34 1     1   818 use Gtk2;
  0            
  0            
35             use Gtk2::Ex::Constants qw( :all );
36              
37             =head1 NAME
38              
39             Gtk2::Ex::Utils - Extra Gtk2 Utilities for working with Gnome2/Gtk2 in Perl.
40              
41             =head1 SYNOPSIS
42              
43             use Gtk2::Ex::Utils qw( :main );
44              
45             # do stuff
46             ...
47              
48             # Update the UI and react to pending events
49             process_pending_events();
50              
51             # do more stuff
52             ...
53              
54             # Exit the program with a value of 255 for some reason
55             process_main_exit( 255 );
56              
57             =head1 DESCRIPTION
58              
59             This module provides simple utility functions useful for Gnome2/Gtk2 Perl
60             programming.
61              
62             =head1 EXPORT TAGS
63              
64             =over
65              
66             :all :main :alter :create
67              
68             =back
69              
70             =head1 FUNCTIONS BY TAG
71              
72             =head2 B<:main>
73              
74             =item B ( )
75              
76             For all pending events, run through the main loop once. Useful for long
77             processes to update the user interface.
78              
79             =cut
80              
81             push( @EXPORT_OK, 'process_pending_events' );
82             push( @{ $EXPORT_TAGS{all} }, 'process_pending_events' );
83             push( @{ $EXPORT_TAGS{main} }, 'process_pending_events' );
84             sub process_pending_events {
85             while ( events_pending Gtk2 ) {
86             main_iteration Gtk2;
87             }
88             }
89              
90             =item B ( [ EXIT_VALUE ] )
91              
92             This will quit the main event loop after all pending events have been
93             given a run through the main loop one last time. Once the UI work is
94             done, exit with the value given or zero. Should the exit value passed
95             be the string 'no-exit', the function will return TRUE instead of
96             exiting.
97              
98             =cut
99              
100             push( @EXPORT_OK, 'process_main_exit' );
101             push( @{ $EXPORT_TAGS{all} }, 'process_main_exit' );
102             push( @{ $EXPORT_TAGS{main} }, 'process_main_exit' );
103             sub process_main_exit {
104             my $exit_value = $_[0] || '0';
105             while ( events_pending Gtk2 ) {
106             main_iteration Gtk2;
107             }
108             main_quit Gtk2;
109             unless ( $exit_value =~ /^no\-exit$/i ) {
110             $exit_value = '0' unless $exit_value =~ m!^\d+$!;
111             exit( $exit_value );
112             }
113             return( TRUE );
114             }
115              
116             =head2 :alter
117              
118             =item DOUBLE = B ( DOUBLE )
119              
120             Used with Gtk2 progress bars to ensure a given value is within the 0.00
121             to 1.00 bounds for valid percentages. This function will modify invalid
122             values appropriately to either 0.00 or 1.00 should the value be out of
123             bounds.
124              
125             =cut
126              
127             push( @EXPORT_OK, 'force_progress_bounds' );
128             push( @{ $EXPORT_TAGS{all} }, 'force_progress_bounds' );
129             push( @{ $EXPORT_TAGS{alter} }, 'force_progress_bounds' );
130             sub force_progress_bounds {
131             my $frac = $_[0] || return ( 0.00 );
132             return( ( not $frac ) ? '0.00' :
133             ( ( $frac > 1.00 ) ? 1.00 :
134             ( ( $frac < 0.00 ) ? '0.00' : $frac ) ) );
135             }
136              
137             =item Gtk2::Label = B ( Gtk2::Label )
138              
139             Given a Gtk2::Label will center the alignment, left justify the text,
140             make the label selectable and make the label wrap lines.
141              
142             =cut
143              
144             push( @EXPORT_OK, 'make_label_wrap_left_centred' );
145             push( @{ $EXPORT_TAGS{all} }, 'make_label_wrap_left_centred' );
146             push( @{ $EXPORT_TAGS{alter} }, 'make_label_wrap_left_centred' );
147             sub make_label_wrap_left_centred {
148             my $label = $_[0] || return();
149             $label->set_line_wrap( TRUE );
150             $label->set_justify( J_LEFT );
151             $label->set_alignment( A_CENTER, A_MIDDLE );
152             $label->set_selectable( TRUE );
153             return( $label );
154             }
155             push( @EXPORT_OK, 'make_label_wrap_left_centered' );
156             push( @{ $EXPORT_TAGS{all} }, 'make_label_wrap_left_centered' );
157             push( @{ $EXPORT_TAGS{allter} }, 'make_label_wrap_left_centered' );
158             sub make_label_wrap_left_centered {
159             my $label = $_[0] || return();
160             $label->set_line_wrap( TRUE );
161             $label->set_justify( J_LEFT );
162             $label->set_alignment( A_CENTER, A_MIDDLE );
163             $label->set_selectable( TRUE );
164             return( $label );
165             }
166              
167             =head2 :create
168              
169             =item Gtk2::Button = B ( ICON, STRING )
170              
171             This will create a new Gtk2::Button, a Gtk2::Image and a label then
172             pack the image and label into an hbox inside the button. The label
173             is new_with_mnemonic and the ICON given can be one of the following
174             types: a stock-id string, the path to an image file, a Gtk2::Image
175             object or a Gtk2::Gdk::Pixbuf object. The button has references to
176             the three components as follows: $button->{HBOX}, $button->{LABEL}
177             and $button->{IMAGE}.
178              
179             =cut
180              
181             push( @EXPORT_OK, 'create_mnemonic_icon_button' );
182             push( @{ $EXPORT_TAGS{all} }, 'create_mnemonic_icon_button' );
183             push( @{ $EXPORT_TAGS{create} }, 'create_mnemonic_icon_button' );
184             sub create_mnemonic_icon_button {
185             my ( $icon, $text ) = ( @_ );
186             my $button = new Gtk2::Button;
187             my $hbox = new Gtk2::HBox ( FALSE, 0 );
188             $button->{HBOX} = $hbox;
189             $button->add( $hbox );
190             if ( $icon ) {
191             my $image = undef;
192             if ( ref( $icon ) eq "Gtk2::Gdk::Pixbuf" ) {
193             $image = new_from_pixbuf Gtk2::Image ( $icon );
194             } elsif ( ref( $icon ) eq "Gtk2::Image" ) {
195             $image = $icon;
196             } elsif ( -f $icon ) {
197             $image = new_from_file Gtk2::Image ( $icon );
198             } elsif ( $icon =~ /^gtk\-/ ) {
199             $image = new_from_stock Gtk2::Image ( $icon, 'menu' );
200             }
201             unless ( not $image ) {
202             $button->{IMAGE} = $image;
203             $hbox->pack_start( $image, PACK_ZERO, PAD_WIDGET );
204             $image->set_alignment( A_LEFT, A_MIDDLE );
205             }
206             }
207             my $label = new_with_mnemonic Gtk2::Label ( $text );
208             $button->{LABEL} = $label;
209             $hbox->pack_start( $label, PACK_FILL, PAD_WIDGET );
210             $label->set_mnemonic_widget( $button );
211             $label->set_justify( J_RIGHT );
212             $label->set_alignment( A_RIGHT, A_MIDDLE );
213             return( $button );
214             }
215              
216              
217             1;
218              
219             __END__