| 1 | package Tk::SearchDialog; | ||
| 2 | my $RCSRevKey = '$Revision: 0.43 $'; | ||
| 3 | $RCSRevKey =~ /Revision: (.*?) /; | ||
| 4 | $VERSION=$1; | ||
| 5 | use vars qw( $VERSION ); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | SearchDialog.pm--Search Dialog Widget for Perl/Tk Text. | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Tk::SearchDialog; | ||
| 14 | |||
| 15 | my $dialog = $mw -> SearchDialog; | ||
| 16 | my @searchoptions = $dialog -> Show; | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | The Tk::SearchDialog widget opens a dialog window that allows | ||
| 21 | entry of search and/or replacement text, and the selection | ||
| 22 | of search options. | ||
| 23 | |||
| 24 | The SearchDialog returns a list of ($option, $value) pairs (see below) | ||
| 25 | when the user clicks the "Search!" button, and undef if the user | ||
| 26 | clicks the "Cancel" button. | ||
| 27 | |||
| 28 | =head1 SEARCH OPTIONS | ||
| 29 | |||
| 30 | The SearchDialog returns a list with the following search | ||
| 31 | specifications. All specifications are string scalar values. The | ||
| 32 | '-option*' options are set to '1' when selected, and '0' or undef if | ||
| 33 | not selected. Labels and titles are read-only. | ||
| 34 | |||
| 35 | =head2 -searchstring | ||
| 36 | |||
| 37 | =head2 -replacestring | ||
| 38 | |||
| 39 | =head2 -optioncase | ||
| 40 | |||
| 41 | =head2 -optionregex | ||
| 42 | |||
| 43 | =head2 -optionbackward | ||
| 44 | |||
| 45 | =head2 -optionquery | ||
| 46 | |||
| 47 | =head2 -entrylabel | ||
| 48 | |||
| 49 | =head2 -replacelabel | ||
| 50 | |||
| 51 | =head2 -optiontitle | ||
| 52 | |||
| 53 | =head2 -optcaselabel | ||
| 54 | |||
| 55 | =head2 -optregexlabel | ||
| 56 | |||
| 57 | =head2 -optbackwardlabel | ||
| 58 | |||
| 59 | =head2 -optquerylabel | ||
| 60 | |||
| 61 | =head2 -searchlabel | ||
| 62 | |||
| 63 | =head2 -cancellable | ||
| 64 | |||
| 65 | =head2 -accept | ||
| 66 | |||
| 67 | =head1 BUGS | ||
| 68 | |||
| 69 | Allow all option defaults to be set by the calling module. Should | ||
| 70 | have some entry validation. | ||
| 71 | |||
| 72 | =head1 VERSION INFORMATION | ||
| 73 | |||
| 74 | $Id: SearchDialog.pm,v 0.43 2002/08/22 16:34:50 kiesling Exp $ | ||
| 75 | |||
| 76 | Author: Robert Allan Kiesling <rkiesling@earthlink.net> | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | |||
| 81 | use Tk qw(Ev); | ||
| 82 | use Carp; | ||
| 83 | use Tk::widgets qw( LabEntry Button Frame Listbox Scrollbar ); | ||
| 84 | use base qw(Tk::Toplevel); | ||
| 85 | |||
| 86 | Construct Tk::Widget 'SearchDialog'; | ||
| 87 | |||
| 88 | my $defaultfont="*-helvetica-medium-r-*-*-12-*"; | ||
| 89 | |||
| 90 | sub Cancel { | ||
| 91 | shift -> withdraw; | ||
| 92 | return undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub Accept { | ||
| 96 | my ($w, $args) = @_; | ||
| 97 | $w -> {'Configure'}{'-accept'} = '1'; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub Populate { | ||
| 101 | my ($w, $args) = @_; | ||
| 102 | require Tk::Label; | ||
| 103 | require Tk::Entry; | ||
| 104 | require Tk::Frame; | ||
| 105 | require Tk::Checkbutton; | ||
| 106 | |||
| 107 | $w -> SUPER::Populate( $args ); | ||
| 108 | |||
| 109 | $w -> ConfigSpecs(-searchstring => ['PASSIVE', undef, undef, ''], | ||
| 110 | -replacestring => ['PASSIVE', undef, undef, ''], | ||
| 111 | -optioncase => ['PASSIVE', undef, undef, ''], | ||
| 112 | -optionregex => ['PASSIVE', undef, undef, ''], | ||
| 113 | -optionbackward => ['PASSIVE', undef, undef, ''], | ||
| 114 | -optionquery => ['PASSIVE', undef, undef, ''], | ||
| 115 | -entrylabel => ['PASSIVE', undef, undef, | ||
| 116 | "Search Pattern: "], | ||
| 117 | -replacelabel => ['PASSIVE', undef, undef, | ||
| 118 | "Replace String: "], | ||
| 119 | -optiontitle => ['PASSIVE', undef, undef, | ||
| 120 | "Search Options:" ], | ||
| 121 | -optcaselabel => ['PASSIVE', undef, undef, | ||
| 122 | "Case Sensitive" ], | ||
| 123 | -optregexlabel => ['PASSIVE', undef, undef, | ||
| 124 | "Regular Expression" ], | ||
| 125 | -optbackwardlabel => ['PASSIVE', undef, undef, | ||
| 126 | "Search Backward" ], | ||
| 127 | -optquerylabel => ['PASSIVE', undef, undef, | ||
| 128 | "Replace without Asking" ], | ||
| 129 | -searchlabel => ['PASSIVE', undef, undef, | ||
| 130 | "Search!" ], | ||
| 131 | -cancellabel => ['PASSIVE', undef, undef, | ||
| 132 | "Cancel" ], | ||
| 133 | -accept => ['PASSIVE', undef, undef, undef ] | ||
| 134 | ); | ||
| 135 | |||
| 136 | my $f = $w -> Component( Frame => 'entryframe', | ||
| 137 | -container => 0, -relief => 'groove', | ||
| 138 | -borderwidth => 3 ); | ||
| 139 | # search pattern | ||
| 140 | my $s = $w->Component( Label => 'searchlabel', | ||
| 141 | -textvariable => \$w->{'Configure'}{'-entrylabel'}, | ||
| 142 | -font => $defaultfont | ||
| 143 | ); | ||
| 144 | $s->grid( -in => $f, -column => 1, -row => 1, -padx => 5, -pady => 5 ); | ||
| 145 | my $searchpat = $w -> Component( Entry => 'searchstring', | ||
| 146 | -width => 30, | ||
| 147 | -textvariable => \$w -> {'Configure'}{'-searchstring'}); | ||
| 148 | $searchpat->grid( -in => $f, -column => 2, -row => 1, -padx => 5, | ||
| 149 | -pady => 5 ); | ||
| 150 | |||
| 151 | my $s1 = $w->Component( Label => 'replacelabel', | ||
| 152 | -textvariable => \$w -> {'Configure'}{'-replacelabel'}, | ||
| 153 | -font => $defaultfont | ||
| 154 | ); | ||
| 155 | $s1->grid( -in => $f, -column => 1, -row => 2, -padx => 5, -pady => 5 ); | ||
| 156 | my $replace = $w -> Component( Entry => 'replacestring', | ||
| 157 | -width => 30, | ||
| 158 | -textvariable => \$w -> {'Configure'}{'-replacestring'}); | ||
| 159 | $replace -> grid( -in => $f, -column => 2, -row => 2, -padx => 5, | ||
| 160 | -pady => 5, -columnspan => 3 ); | ||
| 161 | $f -> grid( -in => $w, -row => 1, -columnspan => 5, -sticky => 'ew', | ||
| 162 | -ipady => 5 ); | ||
| 163 | |||
| 164 | my $s2 = $w -> Component( Label => 'optionsTitle', | ||
| 165 | -textvariable => \$w -> {'Configure'}{'-optiontitle'}, | ||
| 166 | -font => $defaultfont ); | ||
| 167 | $s2 -> grid( -in => $w, -row => 3, -column => 1, -sticky => 'w', | ||
| 168 | -padx => 5, -pady => 5 ); | ||
| 169 | my $case = $w -> Component( Checkbutton => 'optcase', | ||
| 170 | -textvariable => \$w->{'Configure'}{'-optcaselabel'}, | ||
| 171 | -font => $defaultfont, | ||
| 172 | -variable => \$w -> {'Configure'}{'-optioncase'} ); | ||
| 173 | $case -> grid( -in => $w, -row => 4, -column => 1, -sticky => 'w', | ||
| 174 | -padx => 5, -pady => 5 ); | ||
| 175 | my $regex = $w -> Component( Checkbutton => 'optregex', | ||
| 176 | -textvariable => \$w -> {'Configure'}{'-optregexlabel'}, | ||
| 177 | -font => $defaultfont, | ||
| 178 | -variable => \$w -> {'Configure'}{'-optionregex'} ); | ||
| 179 | $regex -> grid( -in => $w, -row => 5, -column => 1, -sticky => 'w', | ||
| 180 | -padx => 2, -pady => 2 ); | ||
| 181 | my $backward = $w -> Component( Checkbutton => 'optbackward', | ||
| 182 | -textvariable => \$w -> {'Configure'}{'-optbackwardlabel'}, | ||
| 183 | -font => $defaultfont, | ||
| 184 | -variable => \$w -> {'Configure'}{'-optionbackward'} ); | ||
| 185 | $backward -> grid( -in => $w, -row => 4, -column => 2, -sticky => 'w', | ||
| 186 | -padx => 2, -pady => 2 ); | ||
| 187 | my $query = $w -> Component( Checkbutton => 'optquery', | ||
| 188 | -textvariable => \$w -> {'Configure'}{'-optquerylabel'}, | ||
| 189 | -font => $defaultfont, | ||
| 190 | -variable => \$w -> {'Configure'}{'-optionbackward'} ); | ||
| 191 | $query-> grid( -in => $w, -row => 5, -column => 2, -sticky => 'w', | ||
| 192 | -padx => 2, -pady => 2 ); | ||
| 193 | my $s3 = $w -> Component( Label => 'blank', | ||
| 194 | -text => '', | ||
| 195 | -font => $defaultfont ); | ||
| 196 | $s3 -> grid( -in => $w, -row => 6, -column => 1 ); | ||
| 197 | |||
| 198 | my $f1 = $w -> Component( Frame => 'buttonframe', | ||
| 199 | -container => 0, -relief => 'groove', | ||
| 200 | -borderwidth => 3 ); | ||
| 201 | my $ab = $f1 -> Component ( Button => 'searchacceptbutton', | ||
| 202 | -textvariable => \$w -> {'Configure'}{'-searchlabel'}, | ||
| 203 | -font => $defaultfont, | ||
| 204 | -default => 'active', | ||
| 205 | -command => ['Accept', $w]) | ||
| 206 | -> grid( -in => $f1, -column => 2, -row => 1, -padx => 10, -pady => 5 ); | ||
| 207 | my $cb = $f1 -> Component ( Button => 'cancelbutton', | ||
| 208 | -textvariable => \$w -> {'Configure'}{'-cancellabel'}, | ||
| 209 | -font => $defaultfont, | ||
| 210 | -default => 'normal', | ||
| 211 | -command => ['Cancel', $w]) | ||
| 212 | -> grid( -in => $f1, -column => 4, -row => 1, -padx => 20, -pady => 2 ); | ||
| 213 | $f1 -> grid( -in => $w, -row => 7, -columnspan => 5, -sticky => 'ew' ); | ||
| 214 | |||
| 215 | return $w; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub Show { | ||
| 219 | my ($w, $args) = @_; | ||
| 220 | $w -> waitVariable( \$w -> {'Configure'}{'-accept'} ); | ||
| 221 | $w -> withdraw; | ||
| 222 | return %{$w -> {'Configure'}}; | ||
| 223 | } | ||
| 224 | |||
| 225 | 1; | ||
| 226 | __END__; |