summaryrefslogtreecommitdiff
path: root/itcl/iwidgets3.0.0/generic/finddialog.itk
blob: 894d0db4fff32f35c1c33be26a046b3edba966d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#
# Finddialog
# ----------------------------------------------------------------------
# This class implements a dialog for searching text.  It prompts the
# user for a search string and the method of searching which includes
# case sensitive, regular expressions, backwards, and all.
#
# ----------------------------------------------------------------------
#  AUTHOR: Mark L. Ulferts              EMAIL: mulferts@austin.dsccc.com
#
#  @(#) RCS: $Id$
# ----------------------------------------------------------------------
#            Copyright (c) 1996 DSC Technologies Corporation
# ======================================================================
# Permission to use, copy, modify, distribute and license this software 
# and its documentation for any purpose, and without fee or written 
# agreement with DSC, is hereby granted, provided that the above copyright 
# notice appears in all copies and that both the copyright notice and 
# warranty disclaimer below appear in supporting documentation, and that 
# the names of DSC Technologies Corporation or DSC Communications 
# Corporation not be used in advertising or publicity pertaining to the 
# software without specific, written prior permission.
# 
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, 
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL 
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
# SOFTWARE.
# ======================================================================

#
# Usual options.
#
itk::usual Finddialog {
    keep -background -cursor -foreground -selectcolor
}

# ------------------------------------------------------------------
#                          IPRFINDDIALOG
# ------------------------------------------------------------------
class ::iwidgets::Finddialog {
    inherit iwidgets::Dialogshell

    constructor {args} {}

    itk_option define -selectcolor selectColor Background {} 
    itk_option define -clearcommand clearCommand Command {}
    itk_option define -matchcommand matchCommand Command {}
    itk_option define -patternbackground patternBackground Background \#707070
    itk_option define -patternforeground patternForeground Foreground White
    itk_option define -searchbackground searchBackground Background \#c4c4c4
    itk_option define -searchforeground searchForeground Foreground Black
    itk_option define -textwidget textWidget TextWidget {}

    public {
	method clear {}
	method find {}
    }

    protected {
	method _get {setting}
	method _textExists {}

	common _optionValues       ;# Current settings of check buttons.
	common _searchPoint        ;# Starting location for searches
	common _matchLen           ;# Matching pattern string length
    }
}

#
# Provide a lowercased access method for the ::finddialog class.
# 
proc ::iwidgets::finddialog {pathName args} {
    uplevel ::iwidgets::Finddialog $pathName $args
}

#
# Use option database to override default resources of base classes.
#
option add *Finddialog.title "Find" widgetDefault

# ------------------------------------------------------------------
#                            CONSTRUCTOR
# ------------------------------------------------------------------
body ::iwidgets::Finddialog::constructor {args} {
    #
    # Add the find pattern entryfield.
    #
    itk_component add pattern {
	iwidgets::Entryfield $itk_interior.pattern -labeltext "Find:"
    } 
    bind [$itk_component(pattern) component entry] \
	    <Return> "[code $this invoke]; break"
    
    #
    # Add the find all checkbutton.
    #
    itk_component add all {
	checkbutton $itk_interior.all \
	    -variable [scope _optionValues($this-all)] \
	    -text "All"
    }

    #
    # Add the case consideration checkbutton.
    #
    itk_component add case {
	checkbutton $itk_interior.case \
	    -variable [scope _optionValues($this-case)] \
	    -text "Consider Case"
    }

    #
    # Add the regular expression checkbutton.
    #
    itk_component add regexp {
	checkbutton $itk_interior.regexp \
	    -variable [scope _optionValues($this-regexp)] \
	    -text "Use Regular Expression"
    }

    #
    # Add the find backwards checkbutton.
    #
    itk_component add backwards {
	checkbutton $itk_interior.backwards \
	    -variable [scope _optionValues($this-backwards)] \
	    -text "Find Backwards"
    }

    #
    # Add the find, clear, and close buttons, making find be the default.
    #
    add Find -text Find -command [code $this find]
    add Clear -text Clear -command [code $this clear]
    add Close -text Close -command [code $this deactivate 0]

    default Find

    #
    # Use the grid to layout the components.
    #
    grid $itk_component(pattern) -row 0 -column 0 \
	-padx 10 -pady 10 -columnspan 4 -sticky ew
    grid $itk_component(all) -row 1 -column 0
    grid $itk_component(case) -row 1 -column 1
    grid $itk_component(regexp) -row 1 -column 2
    grid $itk_component(backwards) -row 1 -column 3

    grid columnconfigure $itk_interior 0 -weight 1
    grid columnconfigure $itk_interior 1 -weight 1
    grid columnconfigure $itk_interior 2 -weight 1
    grid columnconfigure $itk_interior 3 -weight 1

    #
    # Initialize all the configuration options.
    #
    eval itk_initialize $args
}

# ------------------------------------------------------------------
#                             OPTIONS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# OPTION: -clearcommand
#
# Specifies a command to be invoked following a clear operation. 
# The command is meant to be a means of notification that the
# clear has taken place and allow other actions to take place such
# as disabling a find again menu.
# ------------------------------------------------------------------
configbody iwidgets::Finddialog::clearcommand {}

# ------------------------------------------------------------------
# OPTION: -matchcommand
#
# Specifies a command to be invoked following a find operation. 
# The command is called with a match point as an argument.  Should
# a match not be found the match point is {}.
# ------------------------------------------------------------------
configbody iwidgets::Finddialog::matchcommand {}

# ------------------------------------------------------------------
# OPTION: -patternbackground
#
# Specifies the background color of the text matching the search
# pattern.  It may have any of the forms accepted by Tk_GetColor.
# ------------------------------------------------------------------
configbody iwidgets::Finddialog::patternbackground {}

# ------------------------------------------------------------------
# OPTION: -patternforeground
#
# Specifies the foreground color of the pattern matching a search
# operation.  It may have any of the forms accepted by Tk_GetColor.
# ------------------------------------------------------------------
configbody iwidgets::Finddialog::patternforeground {}

# ------------------------------------------------------------------
# OPTION: -searchforeground
#
# Specifies the foreground color of the line containing the matching
# pattern from a search operation.  It may have any of the forms 
# accepted by Tk_GetColor.
# ------------------------------------------------------------------
configbody iwidgets::Finddialog::searchforeground {}

# ------------------------------------------------------------------
# OPTION: -searchbackground
#
# Specifies the background color of the line containing the matching
# pattern from a search operation.  It may have any of the forms 
# accepted by Tk_GetColor.
# ------------------------------------------------------------------
configbody iwidgets::Finddialog::searchbackground {}

# ------------------------------------------------------------------
# OPTION: -textwidget
#
# Specifies the scrolledtext or text widget to be searched.
# ------------------------------------------------------------------
configbody iwidgets::Finddialog::textwidget {
    if {$itk_option(-textwidget) != {}} {
	set _searchPoint($itk_option(-textwidget)) 1.0
    }
}

# ------------------------------------------------------------------
#                            METHODS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# PUBLIC METHOD: clear 
#
# Clear the pattern entryfield and the indicators.  
# ------------------------------------------------------------------
body ::iwidgets::Finddialog::clear {} {
    $itk_component(pattern) clear

    if {[_textExists]} {
	set _searchPoint($itk_option(-textwidget)) 1.0

	$itk_option(-textwidget) tag remove search-line 1.0 end
	$itk_option(-textwidget) tag remove search-pattern 1.0 end
    }

    if {$itk_option(-clearcommand) != {}} {
	eval $itk_option(-clearcommand)
    }
}

# ------------------------------------------------------------------
# PUBLIC METHOD: find
#
# Search for a specific text string in the text widget given by
# the -textwidget option.  Should this option not be set to an
# existing widget, then a quick exit is made. 
# ------------------------------------------------------------------
body ::iwidgets::Finddialog::find {} {
    if {! [_textExists]} {
	return
    }

    #
    # Clear any existing indicators in the text widget.
    #
    $itk_option(-textwidget) tag remove search-line 1.0 end
    $itk_option(-textwidget) tag remove search-pattern 1.0 end

    #
    # Make sure the search pattern isn't just blank.  If so, skip this.
    #
    set pattern [_get pattern]

    if {[string trim $pattern] == ""} {
	return
    }

    #
    # After clearing out any old highlight indicators from a previous
    # search, we'll be building our search command piece-meal based on 
    # the current settings of the checkbuttons in the find dialog.  The
    # first we'll add is a variable to catch the count of the length
    # of the string matching the pattern.
    #
    set precmd "$itk_option(-textwidget) search \
	    -count [list [scope _matchLen($this)]]"

    if {! [_get case]} {
	append precmd " -nocase"
    }

    if {[_get regexp]} {
	append precmd " -regexp"
    } else {
	append precmd " -exact"
    }

    #
    # If we are going to find all matches, then the start point for
    # the search will be the beginning of the text; otherwise, we'll
    # use the last known starting point +/- a character depending on
    # the direction.
    #
    if {[_get all]} {
	set _searchPoint($itk_option(-textwidget)) 1.0
    } else {
	if {[_get backwards]} {
	    append precmd " -backwards"
	} else {
	    append precmd " -forwards"
	}
    }

    #
    # Get the pattern to be matched and add it to the search command.
    # Since it may contain embedded spaces, we'll wrap it in a list.
    #
    append precmd " [list $pattern]"    

    #
    # If the search is for all matches, then we'll be performing the 
    # search until no more matches are found; otherwise, we'll break
    # out of the loop after one search.
    #
    while {1} {
	if {[_get all]} {
	    set postcmd " $_searchPoint($itk_option(-textwidget)) end"

	} else {
	    set postcmd " $_searchPoint($itk_option(-textwidget))"
	}

	#
	# Create the final search command out of the pre and post parts
	# and evaluate it which returns the location of the matching string.
	#
	set cmd {}
	append cmd $precmd $postcmd

	if {[catch {eval $cmd} matchPoint] != 0} {
	    set _searchPoint($itk_option(-textwidget)) 1.0
	    return {}
	}

	#
	# If a match exists, then we'll make this spot be the new starting
	# position.  Then we'll tag the line and the pattern in the line.
	# The foreground and background settings will lite these positions
	# in the text widget up.
	#
	if {$matchPoint != {}} {
	    set _searchPoint($itk_option(-textwidget)) $matchPoint 

	    $itk_option(-textwidget) tag add search-line \
	      "$_searchPoint($itk_option(-textwidget)) linestart" \
		"$_searchPoint($itk_option(-textwidget))" 
	    $itk_option(-textwidget) tag add search-line \
	      "$_searchPoint($itk_option(-textwidget)) + \
               $_matchLen($this) chars" \
	      "$_searchPoint($itk_option(-textwidget)) lineend"
	    $itk_option(-textwidget) tag add search-pattern \
	       $_searchPoint($itk_option(-textwidget)) \
		"$_searchPoint($itk_option(-textwidget)) + \
                 $_matchLen($this) chars"
	}

	#
	# Set the search point for the next time through to be one
	# character more or less from the current search point based
	# on the direction.
	#
	if {[_get all] || ! [_get backwards]} {
	    set _searchPoint($itk_option(-textwidget)) \
		[$itk_option(-textwidget) index \
		     "$_searchPoint($itk_option(-textwidget)) + 1c"]
	} else {
	    set _searchPoint($itk_option(-textwidget)) \
		[$itk_option(-textwidget) index \
		     "$_searchPoint($itk_option(-textwidget)) - 1c"]
	}

	#
	# If this isn't a find all operation or we didn't get a match, exit.
	#
	if {(! [_get all]) || ($matchPoint == {})} {
	    break
	}
    }

    #
    # Configure the colors for the search-line and search-pattern.
    #
    $itk_option(-textwidget) tag configure search-line \
	    -foreground $itk_option(-searchforeground)
    $itk_option(-textwidget) tag configure search-line \
	    -background $itk_option(-searchbackground)
    $itk_option(-textwidget) tag configure search-pattern \
	    -background $itk_option(-patternbackground)
    $itk_option(-textwidget) tag configure search-pattern \
	    -foreground $itk_option(-patternforeground)

    #
    # Adjust the view to be the last matched position.
    #
    if {$matchPoint != {}} {
	$itk_option(-textwidget) see $matchPoint
    }

    #
    # There may be multiple matches of the pattern on a single line,
    # so we'll set the tag priorities such that the pattern tag is higher.
    #
    $itk_option(-textwidget) tag raise search-pattern search-line

    #
    # If a match command is defined, then call it with the match point.
    #
    if {$itk_option(-matchcommand) != {}} {
	[subst $itk_option(-matchcommand)] $matchPoint
    }

    #
    # Return the match point to the caller so they know if we found 
    # anything and if so where
    #
    return $matchPoint
}

# ------------------------------------------------------------------
# PROTECTED METHOD: _get setting
#
# Get the current value for the pattern, case, regexp, or backwards.
# ------------------------------------------------------------------
body ::iwidgets::Finddialog::_get {setting} {
    switch $setting {
	pattern {
	    return [$itk_component(pattern) get]
	}
	case {
	    return $_optionValues($this-case)
	}
	regexp {
	    return $_optionValues($this-regexp)
	}
	backwards {
	    return $_optionValues($this-backwards)
	}
	all {
	    return $_optionValues($this-all)
	}
	default {
	    error "bad get setting: \"$setting\", should be pattern,\
		    case, regexp, backwards, or all"
	}
    }
}

# ------------------------------------------------------------------
# PROTECTED METHOD: _textExists
#
# Check the validity of the text widget option.  Does it exist and
# is it of the class Text or Scrolledtext.
# ------------------------------------------------------------------
body ::iwidgets::Finddialog::_textExists {} {
    if {$itk_option(-textwidget) == {}} {
	return 0
    }

    if {! [winfo exists $itk_option(-textwidget)]} {
	error "bad finddialog text widget value: \"$itk_option(-textwidget)\",\
               the widget doesn't exist"
    }

    if {([winfo class $itk_option(-textwidget)] != "Text") &&
	([itcl::find objects -isa iwidgets::Scrolledtext *::$itk_option(-textwidget)] == "")} {
	error "bad finddialog text widget value: \"$itk_option(-textwidget)\",\
               must be of the class Text or based on Scrolledtext"
    }

    return 1
}