blib/lib/Tk/EntryDialog.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package EntryDialog;
2 $VERSION=0.10;
3 1 ▶use vars qw($VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Tk::EntryDialog - Dialog widget with text entry.
8  
9 =head1 SYNOPSIS
10  
11 use Tk;
12 use Tk::EntryDialog;
13  
14 $d = $w -> EntryDialog ( -font => '*-helvetica-medium-r-*-*-12-*',
15 -title => 'Text Entry',
16 -textlabel => 'Please enter your text:',
17 -defaultentry => 'Text in entry widget' );
18 $d -> WaitForInput;
19  
20 =head1 DESCRIPTION
21  
22 The -font option defaults to *-helvetica-medium-r-*-*-12-*.
23 The -defaultentry option supplies the default text in the Entry
24 widget.
25  
26 The -textlabel option prints the text of its argument in label above
27 the text entry box.
28  
29 After WaitForEntry is called, clicking on the 'Accept' button or
30 pressing Enter in the text entry widget, closes the dialog and returns
31 the text in the entry box.
32  
33 The WaitForEntry method does not destroy the dialog window. Instead
34 WaitForEntry unmaps the dialog box from the display. To de-allocate
35 the widget, you must explicitly call $w -> destroy or $w -> DESTROY.
36  
37 Refer to the Tk::options man page for a description of options
38 common to all Perl/Tk widgets.
39  
40 Example:
41  
42 use Tk;
43 use Tk::EntryDialog;
44  
45 my $w = new MainWindow;
46  
47 my $b = $w -> Button (-text => 'Dialog',
48 -command => sub{&show_dialog($w)}) -> pack;
49  
50 sub show_dialog {
51 my ($w) = @_;
52 my $e;
53 if (not defined $e) {
54 $e = $w -> EntryDialog (-title => 'Enter Text');
55 $e -> configure (-defaultentry => 'Default text');
56 $e -> configure (-textlabel => 'Please enter your text:');
57 }
58 my $resp = $e -> WaitForInput;
59 print "$resp\n";
60 $e -> configure (-textlabel => '');
61 $e -> configure (-defaultentry => 'New entry without label.');
62 my $resp = $e -> WaitForInput;
63 print "$resp\n";
64 return $resp;
65 }
66  
67 MainLoop;
68  
69 =head1 VERSION
70  
71 $Revision: 0.10 $
72  
73 Licensed for free distribution under the terms of the
74 Perl Artistic License.
75  
76 Written by Robert Allan Kiesling <rkiesling@earthlink.net>.
77  
78 Dave Scheck <cds033@email.mot.com> provided the input for the
79 -textlabel option.
80  
81 =cut
82  
83 0 ▶use Tk qw(Ev);
Subroutine BEGIN: called CC=1 SCAR=2.6
84 use strict;
85 use Carp;
86 use base qw(Tk::Toplevel);
87 use Tk::widgets qw(Entry Button);
88  
89 Construct Tk::Widget 'EntryDialog';
90  
91 sub Accept {$_[0]->{Configure}{-accept} += 1}
92  
93 sub Cancel {
94 $_[0] -> {Configure}{-defaultentry} = '';
95 $_[0] -> {Configure}{-accept} += 1;
96 }
97  
98 sub textlabel {
99 my $w = $_[0];
100 my $text = $_[1];
101 if (defined $text and length ($text)) {
102 my $l1 = $w->Component (Label => 'textlabel',
103 -textvariable => \$w->{Configure}{-textlabel},
104 -font => $w -> {Configure}{-font});
105 $l1->grid( -column => 1, -row => 1, -padx => 5, -pady => 5,
106 -sticky => 'ew', -columnspan => 5 );
107 $w->Advertise('textlabel' => $l1);
108  
109 $w -> Subwidget ('entry') ->
110 grid ( -column => 1, -row => 2, -padx => 5, -pady => 5,
111 -sticky => 'ew', -columnspan => 5 );
112 $w -> Subwidget ('acceptbutton') ->
113 grid( -column => 2, -row => 3, -padx => 5, -pady => 5,
114 -sticky => 'new' );
115 $w -> Subwidget ('cancelbutton') ->
116 grid ( -column => 4, -row => 3, -padx => 5, -pady => 5,
117 -sticky => 'new' );
118 } else {
119 $w -> Subwidget ('textlabel') -> destroy if
120 defined $w -> Subwidget ('textlabel');
121 }
122 }
123  
124 sub Populate {
125 my ($w,$args) = @_;
126 require Tk::Button;
127 require Tk::Toplevel;
128 require Tk::Label;
129 require Tk::Entry;
130 $w->SUPER::Populate($args);
131  
132 $w->ConfigSpecs( -font => ['CHILDREN',undef,undef,
133 '*-helvetica-medium-r-*-*-12-*'],
134 -defaultentry => ['PASSIVE',undef,undef,''],
135 -textlabel => ['METHOD',undef,undef,''],
136 -accept => ['PASSIVE',undef,undef,0] );
137  
138 my $row = 1;
139 $w -> withdraw;
140
141 $row++ if (defined $args->{-textlabel} and length ($args->{-textlabel}));
142  
143 my $e1 = $w -> Component (Entry => 'entry',
144 -textvariable => \$w->{Configure}{-defaultentry});
145 $e1 -> grid ( -column => 1, -row => $row++, -padx => 5, -pady => 5,
146 -sticky => 'ew', -columnspan => 5 );
147 $w -> Advertise ('entry' => $e1);
148 $e1 -> bind ('<Return>', sub {$w -> Accept});
149 my $b1 = $w -> Component (Button => 'acceptbutton',
150 -text => 'Accept',
151 -default => 'active' );
152 $b1->grid( -column => 2, -row => $row, -padx => 5, -pady => 5, -sticky => 'new' );
153 $b1 -> bind ('<Button-1>', sub {$w -> Accept});
154 $b1->focus;
155 my $b2 = $w -> Component (Button => 'cancelbutton',
156 -text => 'Cancel',
157 -command => sub{$w -> Cancel},
158 -default => 'normal' );
159 $b2->grid( -column => 4, -row => $row, -padx => 5, -pady => 5, -sticky => 'new' );
160  
161 $w -> bind ('<Escape>', sub {$w -> withdraw});
162  
163 return $w;
164 }
165  
166 sub WaitForInput {
167 my ($w, @args) = @_;
168 $w -> Popup (@args);
169 $w -> waitVariable(\$w->{Configure}{-accept});
170 $w -> withdraw;
171 return $w -> {Configure}{-defaultentry};
172 }
173  
174 1;
↑ Summary