File Coverage

blib/lib/Hints/X.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Hints::X;
2              
3 1     1   634 use strict;
  1         1  
  1         38  
4 1     1   5 use vars qw/$VERSION/;
  1         2  
  1         54  
5 1     1   1414 use Tk;
  0            
  0            
6              
7             $VERSION = '0.02';
8              
9             =head1 NAME
10              
11             Hints::X - Perl extension for dialog for showing hints from hints databases
12              
13             =head1 SYNOPSIS
14              
15             use Tk;
16             use Hints;
17             use Hints::X;
18              
19             my $mw = new Tk;
20              
21             my $hints = new Hints;
22             $hints->load_from_file('my.hints');
23              
24             my $xhints = new Hints::X (-hints => $hints, -mw => $mw);
25             $xhints->show;
26              
27             =head1 DESCRIPTION
28              
29             This module use Hints(3) module for showing its database in X dialog.
30             For X interface is Perl/Tk used.
31              
32             =head1 THE HINTS::X CLASS
33              
34             =head2 new
35              
36             Constructor create dialog with database and controls. You must specify
37             Hints(3) instance for handling hints database and widget of Tk main window.
38              
39             my $xhints = new Hints::X (-hints => $hints, -mw => $mw);
40              
41             =cut
42              
43             sub new {
44             my $class = shift;
45             my %params = @_;
46             my $obj = bless { }, $class;
47             $obj->{hints} = $params{-hints} if $params{-hints};
48             $obj->{mw} = $params{-mw} if $params{-mw};
49             return undef unless $obj->{hints} and $obj->{mw};
50             $obj->create_window;
51             return $obj;
52             }
53              
54             sub create_window {
55             my $obj = shift;
56              
57             $obj->{w} = $obj->{mw}->Toplevel;
58             $obj->{w}->withdraw;
59             $obj->{w}->geometry($obj->default_geometry);
60             $obj->{w}->resizable(0,0);
61             $obj->{w}->title('Hints');
62             $obj->{w}->iconname('Hints');
63             $obj->{w}->client('hints');
64             $obj->{current} = "???";
65              
66             my $f = $obj->{w}->Frame()->pack(-side => 'right', -fill => 'y');
67             $f->Button(-text => 'Previous', -command => sub { $obj->previous; })
68             ->pack(-side => 'top', -expand => 'y', -fill => 'x');
69             $f->Button(-text => 'Random', -command => sub { $obj->random; })
70             ->pack(-side => 'top', -expand => 'y', -fill => 'x');
71             $f->Button(-text => 'Next', -command => sub { $obj->next; })
72             ->pack(-side => 'top', -expand => 'y', -fill => 'x');
73              
74             $f = $obj->{w}->Frame(-relief => 'ridge', -borderwidth => 2,
75             -background => 'white')
76             ->pack(-side => 'left', -expand => 'y', -fill => 'both',
77             -padx => 5, -pady => 5);
78             $f->Label(-textvariable => \$obj->{current}, -wraplength => 360,
79             -justify => 'left', -background => 'white')
80             ->pack(-fill => 'both', -expand => 'y');
81              
82             $obj->random;
83             }
84              
85             =head2 show
86              
87             Show window with hints.
88              
89             $xhints->show;
90              
91             =cut
92              
93             sub show {
94             my $obj = shift;
95              
96             $obj->create_window unless Exists($obj->{w});
97             $obj->{w}->deiconify;
98             $obj->{w}->raise;
99             }
100              
101             =head2 hide
102              
103             Hide window with hints.
104              
105             $xhints->hide;
106              
107             =cut
108              
109             sub hide {
110             my $obj = shift;
111              
112             $obj->{w}->withdraw;
113             }
114              
115             =head2 showed
116              
117             Is window with hints open and visible?
118              
119             do_something() if $xhints->showed;
120              
121             =cut
122              
123             sub showed {
124             my $obj = shift;
125             return Exists($obj->{w});
126             }
127              
128             =head2 geometry
129              
130             Wrapper for Tk::Widget geometry method.
131              
132             my $geom = $xhints->geometry;
133              
134             =cut
135              
136             sub geometry {
137             my $obj = shift;
138             return $obj->{w}->geometry(@_);
139             }
140              
141             =head2 default_geometry
142              
143             Defaults values for C.
144              
145             $xhints->geometry($xhints->default_geometry);
146              
147             =cut
148              
149             sub default_geometry {
150             my $obj = shift;
151             return "480x120";
152             }
153              
154             sub random {
155             my $obj = shift;
156             $obj->{current} = $obj->{hints}->random;
157             }
158              
159             sub previous {
160             my $obj = shift;
161              
162             $obj->{current} = $obj->{hints}->backward;
163             }
164              
165             sub next {
166             my $obj = shift;
167              
168             $obj->{current} = $obj->{hints}->forward;
169             }
170              
171             sub DESTROY {
172             my $obj = shift;
173              
174             $obj->{w}->destroy if Exists($obj->{w});
175             }
176              
177             1;
178              
179             __END__