~ubuntu-branches/ubuntu/gutsy/horae/gutsy

« back to all changes in this revision

Viewing changes to 0CPAN/Tk-Pod-0.9932/More.pm

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2006-12-28 12:36:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061228123648-9xnjr76wfthd92cq
Tags: 064-1
New upstream release, dropped dependency on libtk-filedialog-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package Tk::More;
2
 
 
3
 
use strict;
4
 
use vars qw($VERSION @ISA);
5
 
 
6
 
$VERSION = sprintf("%d.%02d", q$Revision: 5.1 $ =~ /(\d+)\.(\d+)/);
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
 
    $cw->Delegates('DEFAULT'   => $t,
96
 
                   'Search'    => 'SELF',
97
 
                   'ShowMatch' => 'SELF',
98
 
                   'Load'      => 'SELF',
99
 
                  );
100
 
 
101
 
    $cw->{DIRECTION} = "Next";
102
 
 
103
 
    $cw->ConfigSpecs(
104
 
                -insertofftime => [$t, qw(insertOffTime OffTime         0)], # no blinking
105
 
                -insertwidth   => [$t, qw(insertWidth   InsertWidth     0)], # invisible
106
 
                -padx          => [$t, qw(padX          Pad            5p)],
107
 
                -pady          => [$t, qw(padY          Pad            5p)],
108
 
                -searchcase    => ['PASSIVE', 'searchCase', 'SearchCase', 1],
109
 
                -helpcommand   => ['CALLBACK', undef, undef, undef],
110
 
                -background    => ['PASSIVE'],# XXX ignore -background, so optionAdd works.... still decide
111
 
                'DEFAULT'      => [$t]
112
 
                );
113
 
 
114
 
    $cw;
115
 
}
116
 
 
117
 
 
118
 
sub Search {
119
 
    my ($cw, $direction) = @_;
120
 
    $cw->{DIRECTION} = $direction;
121
 
    my $e = $cw->Subwidget('searchentry');
122
 
    $e->configure(-label => 'Search ' . ($direction eq 'Next'?'forward:':'backward:') );
123
 
    $e->configure(-relief=>'sunken',-state=>'normal');
124
 
    $e->selectionRange(0, "end");
125
 
    $e->focus;
126
 
}
127
 
 
128
 
sub SearchText {
129
 
    my ($cw, %args) = @_;
130
 
    my($t, $e) = ($cw->Subwidget('text'), $cw->Subwidget('searchentry'));
131
 
    $cw->{DIRECTION} = $args{-direction} if $args{-direction};
132
 
    my $searchterm;
133
 
    if (defined $args{-searchterm}) {
134
 
        $searchterm = $args{-searchterm};
135
 
        $ {$e->cget('-textvariable')} = $searchterm;
136
 
    } else {
137
 
        $e->historyAdd if ($e->can('historyAdd'));
138
 
        $searchterm = $e->get;
139
 
    }
140
 
    unless ($cw->search_text($t, $searchterm, 'search') ) {
141
 
        $cw->bell unless $args{-quiet};
142
 
    }
143
 
    $e->configure(-label=>'');
144
 
    $t->see('@0,0');
145
 
    $cw->ShowMatch($cw->{DIRECTION}, -firsttime => 1) unless $args{-onlymatch};
146
 
    $t->focus;
147
 
    $e->configure(-relief=>'flat', -state=>'disabled');
148
 
}
149
 
 
150
 
sub SearchTextEscape {
151
 
    my ($cw, %args) = @_;
152
 
    my($t, $e) = ($cw->Subwidget('text'), $cw->Subwidget('searchentry'));
153
 
    $e->configure(-label=>'');
154
 
    $t->focus;
155
 
    $e->configure(-relief=>'flat', -state=>'disabled');
156
 
}
157
 
 
158
 
sub ShowMatch {
159
 
    my ($cw, $method, %args) = @_;
160
 
    my $firsttime = $args{-firsttime};
161
 
 
162
 
    my $t = $cw->Subwidget('text');
163
 
    if ($cw->{DIRECTION} ne 'Next') {
164
 
        $method = 'Next' if $method eq 'Prev';
165
 
        $method = 'Prev' if $method eq 'Next';
166
 
    }
167
 
    my $cur = (($method eq 'Prev' && !$firsttime) ||
168
 
               ($method eq 'Next' &&  $firsttime)
169
 
               ? $t->index('@0,0')
170
 
               : $t->index('@0,'.$t->height));
171
 
    $method = "tag". $method . "range"; # $method: Next or Prev
172
 
    my @ins = $t->$method('search',$cur);
173
 
    unless (@ins) {
174
 
        # hack: Maybe the search was not performed yet? (e.g. after loading
175
 
        # a new page but with the same search term)
176
 
        my $e = $cw->Subwidget('searchentry');
177
 
        if (!defined $ {$e->cget('-textvariable')}) {
178
 
            return;
179
 
        }
180
 
        $cw->SearchText(-searchterm => $ {$e->cget('-textvariable')},
181
 
                        -onlymatch => 1);
182
 
        @ins = $t->$method('search',$cur);
183
 
        return if !@ins;
184
 
    }
185
 
    @ins = reverse @ins unless $method eq 'tagNextrange';
186
 
    $t->see($ins[0]);
187
 
    $ins[0];
188
 
}
189
 
 
190
 
# Load copied from TextUndo (xxx yy marks changes)
191
 
sub Load
192
 
{
193
 
 my ($text,$file) = @_;
194
 
 if (open(FILE,"<$file"))
195
 
  {
196
 
   $text->MainWindow->Busy;
197
 
   $text->SUPER::delete('1.0','end');
198
 
   #yy delete $text->{UNDO};
199
 
   while (<FILE>)
200
 
    {
201
 
     $text->SUPER::insert('end',$_);
202
 
    }
203
 
   close(FILE);
204
 
   #yy $text->{FILE} = $file;
205
 
   $text->markSet('insert', '@1,0');
206
 
   $text->MainWindow->Unbusy;
207
 
  }
208
 
 else
209
 
  {
210
 
   $text->messageBox(-message => "Cannot open $file: $!\n");
211
 
   die;
212
 
  }
213
 
}
214
 
 
215
 
# search_text copied from demo search.pl (modified)
216
 
sub search_text {
217
 
 
218
 
    # The utility procedure below searches for all instances of a given
219
 
    # string in a text widget and applies a given tag to each instance found.
220
 
    # Arguments:
221
 
    #
222
 
    # w -       The window in which to search.  Must be a text widget.
223
 
    # string -  string to search for.  The search is done
224
 
    #           using exact matching only;  no special characters.
225
 
    # tag -     Tag to apply to each instance of a matching string.
226
 
 
227
 
    my($w, $t, $string, $tag) = @_;
228
 
 
229
 
    return unless length($string);
230
 
 
231
 
    $w->tag('remove',  $tag, qw/0.0 end/);
232
 
    my($current, $length, $found) = ('1.0', 0, 0);
233
 
 
234
 
    my $insert = $w->index('insert');
235
 
    my @search_args = ('-regexp');
236
 
    push @search_args, '-nocase' unless ($w->cget('-searchcase'));
237
 
    eval {
238
 
        while (1) {
239
 
            $current = $w->search(@search_args, -count => \$length, '--', $string, $current, 'end');
240
 
            last if not $current;
241
 
            $found = 1;
242
 
            $w->tag('add', $tag, $current, "$current + $length char");
243
 
            $current = $w->index("$current + $length char");
244
 
        }
245
 
        $w->markSet('insert', $insert);
246
 
    };
247
 
    if ($@) {
248
 
        $w->messageBox(-icon => "error",
249
 
                       -message => $@,
250
 
                      );
251
 
    }
252
 
    $found;
253
 
} # end search_text
254
 
 
255
 
sub scroll {
256
 
    my($w,$t,$no,$unit) = @_;
257
 
    if ($unit =~ /^line/) {
258
 
        $t->yview('scroll', $no, 'units');
259
 
    } else {
260
 
        my($y1,$y2) = $t->yview;
261
 
        my $amount;
262
 
        if ($unit =~ /^halfpage/) {
263
 
            $amount = ($y2-$y1)/2;
264
 
        } elsif ($unit =~ /^page/) {
265
 
#           if ($no == -1) {
266
 
#               # loop until top-most line is invisible
267
 
#               my $inx = $t->index('@0,0');
268
 
#  my $i=0;
269
 
#               while ($t->bbox($inx)) {
270
 
#                   $t->yviewScroll(-1,'units');
271
 
#                   last if ($i++>1000);
272
 
#               }
273
 
#               goto XXX;
274
 
#           }
275
 
            $amount = ($y2-$y1);
276
 
        } else {
277
 
            die "Unknown unit $unit";
278
 
        }
279
 
#warn "$y1 $y2 $amount";
280
 
        $y1 += ($no * $amount);
281
 
        if ($no > 0) {
282
 
            $y1 = 1.0 if ($y1 > 1.0);
283
 
        } else {
284
 
            $y1 = 0.0 if ($y1 < 0.0);
285
 
        }
286
 
        $t->yviewMoveto($y1);
287
 
    }
288
 
    #XXX:
289
 
    Tk->break;
290
 
}
291
 
 
292
 
 
293
 
#package Tk::More::Status;
294
 
#
295
 
## Implement status bar
296
 
#
297
 
 
298
 
1;
299
 
 
300
 
__END__
301
 
 
302
 
=head1 NAME
303
 
 
304
 
Tk::More - a 'more' or 'less' like text widget
305
 
 
306
 
=head1 SYNOPSIS
307
 
 
308
 
    use Tk::More;
309
 
 
310
 
    $more = $parent->More(...text widget options ...);
311
 
    $more->Load(FILENAME);
312
 
 
313
 
=head1 DESCRIPTION
314
 
 
315
 
B<Tk::More> is a readonly text widget with additional key bindings as
316
 
found in UNI* command line tools C<more> or C<less>. As in C<more> an
317
 
additional status/command line is added at the bottom.
318
 
 
319
 
=head1 ADDITIONAL BINDINGS
320
 
 
321
 
=over 4
322
 
 
323
 
=item Key-g or Home
324
 
 
325
 
goto beginning of file
326
 
 
327
 
=item Key-G or End
328
 
 
329
 
goto end of file
330
 
 
331
 
=item Key-f or Next
332
 
 
333
 
forward screen
334
 
 
335
 
=item Key-b or Prior
336
 
 
337
 
backward screen
338
 
 
339
 
=item Key-k or Up
340
 
 
341
 
up one line
342
 
 
343
 
=item Key-j or Down
344
 
 
345
 
down one line
346
 
 
347
 
=item Key-/
348
 
 
349
 
search forward
350
 
 
351
 
=item Key-?
352
 
 
353
 
search backward
354
 
 
355
 
=item Key-n
356
 
 
357
 
find next match
358
 
 
359
 
=item Key-N
360
 
 
361
 
find previous match
362
 
 
363
 
=item Key-u
364
 
 
365
 
up half screen
366
 
 
367
 
=item Key-d
368
 
 
369
 
down half screen
370
 
 
371
 
=item Key-Return
372
 
 
373
 
down one line
374
 
 
375
 
=item Key-h
376
 
 
377
 
invoke help window
378
 
 
379
 
=back
380
 
 
381
 
=head1 BUGS
382
 
 
383
 
Besides that most of more bindings are not implemented. This bugs
384
 
me most (high to low priority):
385
 
 
386
 
* better status line implementation
387
 
 
388
 
* Cursor movement: up/down move displayed area regardless where
389
 
  insert cursor is
390
 
 
391
 
* add History, Load, Search (also as popup menu)
392
 
 
393
 
=head1 SEE ALSO
394
 
 
395
 
L<Tk::ROText|Tk::ROText>, L<more(1)>, L<tkmore>, L<less(1)>
396
 
 
397
 
=head1 AUTHOR
398
 
 
399
 
Achim Bohnet <F<ach@mpe.mpg.de>>
400
 
 
401
 
Currently maintained by Slaven Rezic <F<slaven@rezic.de>>.
402
 
 
403
 
Copyright (c) 1997-1998 Achim Bohnet. All rights reserved.  This program is
404
 
free software; you can redistribute it and/or modify it under the same
405
 
terms as Perl itself.
406
 
 
407
 
=cut