home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _c73843d9c48270d859b0c1b70adf60ee < prev    next >
Encoding:
Text File  |  2004-06-01  |  6.3 KB  |  178 lines

  1. # search.pl
  2.  
  3. use Tk::LabEntry;
  4. use subs qw/search_flash_matches search_load_file search_text/;
  5. use vars qw/$TOP/;
  6.  
  7. sub search {
  8.  
  9.     # Create a top-level window with a text widget that allows you to load a
  10.     # file and highlight all instances of a given string.  A LabEntry widget
  11.     # is used to collect the file name and search string.
  12.  
  13.     my($demo) = @_;
  14.     $TOP = $MW->WidgetDemo(
  15.         -name     => $demo,
  16.         -text     =>'',
  17.         -title    => 'Text Demonstration - Search and Highlight',
  18.         -iconname => 'search',
  19.     );
  20.  
  21.     my $file_name = '';
  22.     my $file = $TOP->Frame;
  23.     my $fn = $file->LabEntry(-label => 'File Name:      ', -width => 40,
  24.         -labelPack => [qw/-side left -anchor w/],
  25.         -textvariable => \$file_name)->pack(qw/-side left/);
  26.     $fn->Subwidget('entry')->focus;
  27.     my $fn_button = $file->Button(-text => 'Load File');
  28.     $fn_button->pack(qw/-side left -pady 5 -padx 10/);
  29.  
  30.     my $search_string = '';
  31.     my $kind = 'exact';
  32.     my $string = $TOP->Frame;
  33.     my $ss = $string->LabEntry(-label => 'Search string:', -width => 40,
  34.         -labelPack => [qw/-side left -anchor w/],
  35.         -textvariable => \$search_string)->pack(qw/-side left/);
  36.     my $ss_button = $string->Button(-text => 'Highlight');
  37.     $ss_button->pack(qw/-side left -pady 5 -padx 10/);
  38.  
  39.     my $text = $TOP->Scrolled(qw/Text -setgrid true -scrollbars e/);
  40.  
  41.     my $subframe = $TOP->Frame;
  42.     my $exact  = $subframe->Radiobutton(-text => 'Exact match',
  43.                                         -variable => \$kind,
  44.                                         -value => 'exact');
  45.     my $regexp = $subframe->Radiobutton(-text => 'Regular expression',
  46.                                         -variable => \$kind,
  47.                                         -value => 'regexp');
  48.     $exact->pack(qw/-side left/, -fill => 'x');
  49.     $regexp->pack(qw/-side right/, -fill => 'x');
  50.  
  51.     $file->pack(qw/-side top -fill x/);
  52.     $string->pack(qw/-side top -fill x/);
  53.     $subframe->pack(qw/-side top -fill x/);
  54.     $text->pack(qw/-expand yes -fill both/);
  55.  
  56.     my $command =  sub {search_load_file $text, \$file_name, $ss};
  57.     $fn_button->configure(-command => $command);
  58.     $fn->bind('<Return>' => $command);
  59.  
  60.     $command = sub {search_text $text, \$search_string, 'search', $kind};
  61.     $ss_button->configure(-command => $command);
  62.     $ss->bind('<Return>' => $command);
  63.  
  64.     # Set up display styles for text highlighting.
  65.  
  66.     if ($TOP->depth > 1) {
  67.     search_flash_matches $text,
  68.             ['configure', 'search',
  69.                 -background => '#ce5555', -foreground => 'white'], 800,
  70.             ['configure', 'search',
  71.                 -background => undef,     -foreground => undef],   200;
  72.       } else {
  73.     search_flash_matches $text,
  74.             ['configure', 'search',
  75.                 -background => 'black',   -foreground => 'white'], 800,
  76.             ['configure', 'search',
  77.                -background => undef,      -foreground => undef],   200;
  78.       }
  79.  
  80.     $text->insert('0.0', 'This window demonstrates how to use the tagging facilities in text
  81. widgets to implement a searching mechanism.  First, type a file name
  82. in the top entry, then type <Return> or click on "Load File".  Then
  83. type a string in the lower entry and type <Return> or click on
  84. "Highlight".  This will cause all of the instances of the string to
  85. be tagged with the tag "search", and it will arrange for the tag\'s
  86. display attributes to change to make all of the strings blink.');
  87.  
  88.     $text->mark(qw/set insert 0.0/);
  89.  
  90. } # end search
  91.  
  92. sub search_flash_matches {
  93.  
  94.     # The procedure below is invoked repeatedly to invoke two commands at
  95.     # periodic intervals.  It normally reschedules itself after each execution
  96.     # but if an error occurs (e.g. because the window was deleted) then it
  97.     # doesn't reschedule itself.
  98.     # Arguments:
  99.     #
  100.     # w -       Text widget reference.
  101.     # cmd1 -    Reference to a list of tag options.
  102.     # sleep1 -    Ms to sleep after executing cmd1 before executing cmd2.
  103.     # cmd2 -    Reference to a list of tag options.
  104.     # sleep2 -    Ms to sleep after executing cmd2 before executing cmd1 again.
  105.  
  106.     my($w, $cmd1, $sleep1, $cmd2, $sleep2) = @_;
  107.  
  108.     $w->tag(@{$cmd1});
  109.     $w->after($sleep1,
  110.           [\&search_flash_matches, $w, $cmd2, $sleep2, $cmd1, $sleep1]);
  111.  
  112. } # end search_flash_matches
  113.  
  114. sub search_load_file {
  115.  
  116.     # The utility procedure below loads a file into a text widget, discarding
  117.     # the previous contents of the widget. Tags for the old widget are not
  118.     # affected, however.
  119.     # Arguments:
  120.     #
  121.     # w -    The window into which to load the file.  Must be a text widget.
  122.     # file -    Reference to the name of the file to load.  Must be readable.
  123.     # e -       Entry widget to get next focus.
  124.  
  125.     my ($w, $file, $e) = @_;
  126.  
  127.     my ($buf, $bytes) = ('', 0);
  128.  
  129.     if (not open(F, "<$$file")) {
  130.     $MW->Dialog(
  131.             -title  => 'File Not Found',
  132.             -text   => "$!: '$$file'",
  133.             -bitmap => 'error',
  134.         )->Show;
  135.     return;
  136.     }
  137.     $w->delete(qw/1.0 end/);
  138.     $bytes = read F, $buf, 10_000;    # after all, it IS just an example
  139.     $w->insert('end', $buf);
  140.     if ($bytes == 10000) {
  141.     $w->insert('end', "\n\n**************** File truncated at 10,000 bytes! ****************\n");
  142.     }
  143.     close F;
  144.  
  145.     $e->Subwidget('entry')->focus;
  146.  
  147. } # end search_load_file
  148.  
  149. sub search_text {
  150.  
  151.     # The utility procedure below searches for all instances of a given
  152.     # string in a text widget and applies a given tag to each instance found.
  153.     # Arguments:
  154.     #
  155.     # w -    The window in which to search.  Must be a text widget.
  156.     # string -    Reference to the string to search for.  The search is done
  157.     #           using exact matching only;  no special characters.
  158.     # tag -    Tag to apply to each instance of a matching string.
  159.  
  160.     my($w, $string, $tag, $kind) = @_;
  161.  
  162.     return unless ref($string) && length($$string);
  163.  
  164.     $w->tagRemove($tag, qw/0.0 end/);
  165.     my($current, $length) = ('1.0', 0);
  166.  
  167.     while (1) {
  168.     $current = $w->search(-count => \$length, "-$kind", $$string, $current, 'end');
  169.     last if not $current;
  170.     warn "Posn=$current count=$length\n",
  171.     $w->tagAdd($tag, $current, "$current + $length char");
  172.     $current = $w->index("$current + $length char");
  173.     }
  174.  
  175. } # end search_text
  176.  
  177. 1;
  178.