More.pm untested

filter: Statementstmt 0.0% 0 / 220 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 327 CC 33 SCAR 36.8
CC
33
Cov
0%
ShowMatch
90.0
SearchText
56.0
scroll
56.0
CRAP 39.5
1 package Tk::More;
2  
3 use strict;
4 use vars qw($VERSION @ISA);
5  
6 $VERSION = '5.09';
7  
8 use Tk qw(Ev);
9 use Tk::Derived;
10 use Tk::Frame;
11 @ISA = qw(Tk::Derived Tk::Frame);
12  
13 Construct Tk::Widget 'More';
14  
15 sub Populate {
16 my ($cw, $args) = @_;
17  
18 require Tk::ROText;
19 require Tk::LabEntry;
20  
21 $cw->SUPER::Populate($args);
22  
23 my $Entry = 'LabEntry';
24 my @Entry_args;
25 if (eval { die "Not yet";
26 require Tk::HistEntry;
27 Tk::HistEntry->VERSION(0.37);
28 1;
29 }) {
30 $Entry = 'HistEntry';
31 } else {
32 @Entry_args = (-labelPack=>[-side =>'left']);
33 }
34  
35 my $search;
36 my $e = $cw->$Entry(
37 @Entry_args,
38 -textvariable => \$search,
39 -relief => 'flat',
40 -state => 'disabled',
41 )->pack(-side=>'bottom', -fill => 'x', -expand=>'no');
42 $cw->Advertise('searchentry' => $e);
43  
44 my $t = $cw->ROText(-cursor=>undef)->pack(-fill => 'both' , -expand => 'yes');
45 $cw->Advertise('text' => $t);
46 $t->tagConfigure('search', -foreground => 'red');
47  
48 # reorder bindings: private widget bindings first
49 $t->bindtags([$t, grep { $_ ne $t->PathName } $t->bindtags]);
50  
51 $t->bind('<Key-slash>', [$cw, 'Search', 'Next']);
52 $t->bind('<Key-question>', [$cw, 'Search', 'Prev']);
53 $t->bind('<Key-n>', [$cw, 'ShowMatch', 'Next']);
54 $t->bind('<Key-N>', [$cw, 'ShowMatch', 'Prev']);
55  
56 $t->bind('<Key-g>', $t->bind(ref($t),'<Control-Home>'));
57 $t->bind('<Key-G>', $t->bind(ref($t),'<Control-End>'));
58 $t->bind('<Home>', $t->bind('<Key-g>'));
59 $t->bind('<End>', $t->bind('<Key-G>'));
60  
61 $t->bind('<Key-j>', [$cw, 'scroll', $t, 1, 'line']);
62 $t->bind('<Down>', [$cw, 'scroll', $t, 1, 'line']);
63 $t->bind('<Key-k>', [$cw, 'scroll', $t, -1, 'line']);
64 $t->bind('<Up>', [$cw, 'scroll', $t, -1, 'line']);
65  
66 $t->bind('<Key-f>', [$cw, 'scroll', $t, 1, 'page']);
67 $t->bind('<Next>', [$cw, 'scroll', $t, 1, 'page']);
68 $t->bind('<Key-b>', [$cw, 'scroll', $t, -1, 'page']);
69 $t->bind('<Prior>', [$cw, 'scroll', $t, -1, 'page']);
70  
71 $t->bind('<Right>', [sub {
72 return if ($_[1] =~ /(Alt|Meta)-/);
73 $t->xview('scroll', 1, 'units'); Tk->break;
74 }, Ev('s')]);
75 $t->bind('<Left>', [sub {
76 return if ($_[1] =~ /(Alt|Meta)-/);
77 $t->xview('scroll', -1, 'units'); Tk->break;
78 }, Ev('s')]);
79  
80 $t->bind('<Return>', ['yview', 'scroll', 1, 'units']);
81 $t->bind('<Key-d>', [$cw, 'scroll', $t, 1, 'halfpage']);
82 $t->bind('<Key-u>', [$cw, 'scroll', $t, -1, 'halfpage']);
83  
84 $t->bind('<Key-h>', sub { $cw->Callback(-helpcommand => $t) });
85  
86 $e->bind('<Return>',[$cw, 'SearchText']);
87 $e->bind('<Escape>',[$cw, 'SearchTextEscape']);
88  
89 foreach my $mod (qw(Alt Meta)) {
90 foreach my $key (qw(n N g G j k f b d u h)) {
91 $t->bind("<$mod-Key-$key>" => \&Tk::NoOp);
92 }
93 }
94  
95 # This was formerly possible, but is now invalid:
96 delete $args->{-font} if !defined $args->{-font};
97  
98 $cw->Delegates('DEFAULT' => $t,
99 'Search' => 'SELF',
100 'ShowMatch' => 'SELF',
101 'Load' => 'SELF',
102 'LoadFH' => 'SELF',
103 'AddQuitBindings' => 'SELF',
104 );
105  
106 $cw->{DIRECTION} = "Next";
107  
108 $cw->ConfigSpecs(
109 -insertofftime => [$t, qw(insertOffTime OffTime 0)], # no blinking
110 -insertwidth => [$t, qw(insertWidth InsertWidth 0)], # invisible
111 -padx => [$t, qw(padX Pad 5p)],
112 -pady => [$t, qw(padY Pad 5p)],
113 -searchcase => ['PASSIVE', 'searchCase', 'SearchCase', 1],
114 -helpcommand => ['CALLBACK', undef, undef, undef],
115 -background => ['PASSIVE'],# XXX ignore -background, so optionAdd works.... still decide
116 -font => [$t, 'fixedFont', 'FixedFont', 'Courier 10'],
117 'DEFAULT' => [$t]
118 );
119  
120 $cw;
121 }
122  
123  
124 sub Search {
125 my ($cw, $direction) = @_;
126 $cw->{DIRECTION} = $direction;
127 my $e = $cw->Subwidget('searchentry');
128 $e->configure(-label => 'Search ' . ($direction eq 'Next'?'forward:':'backward:') );
129 $e->configure(-relief=>'sunken',-state=>'normal');
130 $e->selectionRange(0, "end");
131 $e->focus;
132 }
133  
134 sub SearchText {
135 my ($cw, %args) = @_;
136 my($t, $e) = ($cw->Subwidget('text'), $cw->Subwidget('searchentry'));
137 $cw->{DIRECTION} = $args{-direction} if $args{-direction};
138 my $searchterm;
139 if (defined $args{-searchterm}) {
140 $searchterm = $args{-searchterm};
141 $ {$e->cget('-textvariable')} = $searchterm;
142 } else {
143 $e->historyAdd if ($e->can('historyAdd'));
144 $searchterm = $e->get;
145 }
146 unless ($cw->search_text($t, $searchterm, 'search') ) {
147 $cw->bell unless $args{-quiet};
148 }
149 $e->configure(-label=>'');
150 $t->see('@0,0');
151 $cw->ShowMatch($cw->{DIRECTION}, -firsttime => 1) unless $args{-onlymatch};
152 $t->focus;
153 $e->configure(-relief=>'flat', -state=>'disabled');
154 }
155  
156 sub SearchTextEscape {
157 my ($cw, %args) = @_;
158 my($t, $e) = ($cw->Subwidget('text'), $cw->Subwidget('searchentry'));
159 $e->configure(-label=>'');
160 $t->focus;
161 $e->configure(-relief=>'flat', -state=>'disabled');
162 }
163  
164 sub ShowMatch {
165 my ($cw, $method, %args) = @_;
166 my $firsttime = $args{-firsttime};
167  
168 my $t = $cw->Subwidget('text');
169 if ($cw->{DIRECTION} ne 'Next') {
170 $method = 'Next' if $method eq 'Prev';
171 $method = 'Prev' if $method eq 'Next';
172 }
173 my $cur = (($method eq 'Prev' && !$firsttime) ||
174 ($method eq 'Next' && $firsttime)
175 ? $t->index('@0,0')
176 : $t->index('@0,'.$t->height));
177 $method = "tag". $method . "range"; # $method: Next or Prev
178 my @ins = $t->$method('search',$cur);
179 unless (@ins) {
180 # hack: Maybe the search was not performed yet? (e.g. after loading
181 # a new page but with the same search term)
182 my $e = $cw->Subwidget('searchentry');
183 if (!defined $ {$e->cget('-textvariable')}) {
184 return;
185 }
186 $cw->SearchText(-searchterm => $ {$e->cget('-textvariable')},
187 -onlymatch => 1);
188 @ins = $t->$method('search',$cur);
189 return if !@ins;
190 }
191 @ins = reverse @ins unless $method eq 'tagNextrange';
192 $t->see($ins[0]);
193 $ins[0];
194 }
195  
196 # Load copied from TextUndo (xxx yy marks changes)
197 sub Load
198 {
199 my ($text,$file,%args) = @_;
200 if (open(FILE,"<$file"))
201 {
202 $text->LoadFH(\*FILE,%args);
203 close(FILE);
204 }
205 else
206 {
207 $text->messageBox(-message => "Cannot open $file: $!\n");
208 die;
209 }
210 }
211  
212 sub LoadFH
213 {
214 my ($text,$fh,%args) = @_;
215 my $encoding = delete $args{-encoding};
216 die "Unhandled arguments: " . join(" ", %args) if %args;
217 if ($encoding)
218 {
219 binmode $fh, ":encoding($encoding)";
220 }
221 $text->MainWindow->Busy;
222 $text->delete('1.0','end');
223 #yy delete $text->{UNDO};
224 while (<$fh>)
225 {
226 $text->insert('end',$_);
227 }
228 #yy $text->{FILE} = $file;
229 $text->markSet('insert', '@1,0');
230 $text->MainWindow->Unbusy;
231 }
232  
233 # search_text copied from demo search.pl (modified)
234 sub search_text {
235  
236 # The utility procedure below searches for all instances of a given
237 # string in a text widget and applies a given tag to each instance found.
238 # Arguments:
239 #
240 # w - The window in which to search. Must be a text widget.
241 # string - string to search for. The search is done
242 # using exact matching only; no special characters.
243 # tag - Tag to apply to each instance of a matching string.
244  
245 my($w, $t, $string, $tag) = @_;
246  
247 return unless length($string);
248  
249 $w->tag('remove', $tag, qw/0.0 end/);
250 my($current, $length, $found) = ('1.0', 0, 0);
251  
252 my $insert = $w->index('insert');
253 my @search_args = ('-regexp');
254 push @search_args, '-nocase' unless ($w->cget('-searchcase'));
255 eval {
256 while (1) {
257 $current = $w->search(@search_args, -count => \$length, '--', $string, $current, 'end');
258 last if not $current;
259 $found = 1;
260 $w->tag('add', $tag, $current, "$current + $length char");
261 $current = $w->index("$current + $length char");
262 }
263 $w->markSet('insert', $insert);
264 };
265 if ($@) {
266 $w->messageBox(-icon => "error",
267 -message => $@,
268 );
269 }
270 $found;
271 } # end search_text
272  
273 sub scroll {
274 my($w,$t,$no,$unit) = @_;
275 if ($unit =~ /^line/) {
276 $t->yview('scroll', $no, 'units');
277 } else {
278 my($y1,$y2) = $t->yview;
279 my $amount;
280 if ($unit =~ /^halfpage/) {
281 $amount = ($y2-$y1)/2;
282 } elsif ($unit =~ /^page/) {
283 # if ($no == -1) {
284 # # loop until top-most line is invisible
285 # my $inx = $t->index('@0,0');
286 # my $i=0;
287 # while ($t->bbox($inx)) {
288 # $t->yviewScroll(-1,'units');
289 # last if ($i++>1000);
290 # }
291 # goto XXX;
292 # }
293 $amount = ($y2-$y1);
294 } else {
295 die "Unknown unit $unit";
296 }
297 #warn "$y1 $y2 $amount";
298 $y1 += ($no * $amount);
299 if ($no > 0) {
300 $y1 = 1.0 if ($y1 > 1.0);
301 } else {
302 $y1 = 0.0 if ($y1 < 0.0);
303 }
304 $t->yviewMoveto($y1);
305 }
306 #XXX:
307 Tk->break;
308 }
309  
310 sub AddQuitBindings {
311 my($more) = @_;
312 $more->bind("<q>" => sub { $more->toplevel->destroy });
313 $more->bind("<Control-q>" => sub { $more->toplevel->destroy });
314 }
315  
316 #package Tk::More::Status;
317 #
318 ## Implement status bar
319 #
320  
321 1;
322  
323 __END__