Update tk to version 8.5.11
[msysgit.git] / mingw / lib / tk8.5 / demos / search.tcl
blob9f44e16e57bc2a4290d3553bffef8c95461dd003
1 # search.tcl --
3 # This demonstration script creates a collection of widgets that
4 # allow you to load a file into a text widget, then perform searches
5 # on that file.
7 if {![info exists widgetDemo]} {
8 error "This script should be run from the \"widget\" demo."
11 package require Tk
13 # textLoadFile --
14 # This procedure below loads a file into a text widget, discarding
15 # the previous contents of the widget. Tags for the old widget are
16 # not affected, however.
18 # Arguments:
19 # w - The window into which to load the file. Must be a
20 # text widget.
21 # file - The name of the file to load. Must be readable.
23 proc textLoadFile {w file} {
24 set f [open $file]
25 $w delete 1.0 end
26 while {![eof $f]} {
27 $w insert end [read $f 10000]
29 close $f
32 # textSearch --
33 # Search for all instances of a given string in a text widget and
34 # apply a given tag to each instance found.
36 # Arguments:
37 # w - The window in which to search. Must be a text widget.
38 # string - The string to search for. The search is done using
39 # exact matching only; no special characters.
40 # tag - Tag to apply to each instance of a matching string.
42 proc textSearch {w string tag} {
43 $w tag remove search 0.0 end
44 if {$string == ""} {
45 return
47 set cur 1.0
48 while 1 {
49 set cur [$w search -count length $string $cur end]
50 if {$cur == ""} {
51 break
53 $w tag add $tag $cur "$cur + $length char"
54 set cur [$w index "$cur + $length char"]
58 # textToggle --
59 # This procedure is invoked repeatedly to invoke two commands at
60 # periodic intervals. It normally reschedules itself after each
61 # execution but if an error occurs (e.g. because the window was
62 # deleted) then it doesn't reschedule itself.
64 # Arguments:
65 # cmd1 - Command to execute when procedure is called.
66 # sleep1 - Ms to sleep after executing cmd1 before executing cmd2.
67 # cmd2 - Command to execute in the *next* invocation of this
68 # procedure.
69 # sleep2 - Ms to sleep after executing cmd2 before executing cmd1 again.
71 proc textToggle {cmd1 sleep1 cmd2 sleep2} {
72 catch {
73 eval $cmd1
74 after $sleep1 [list textToggle $cmd2 $sleep2 $cmd1 $sleep1]
78 set w .search
79 catch {destroy $w}
80 toplevel $w
81 wm title $w "Text Demonstration - Search and Highlight"
82 wm iconname $w "search"
83 positionWindow $w
85 ## See Code / Dismiss buttons
86 set btns [addSeeDismiss $w.buttons $w]
87 pack $btns -side bottom -fill x
89 frame $w.file
90 label $w.file.label -text "File name:" -width 13 -anchor w
91 entry $w.file.entry -width 40 -textvariable fileName
92 button $w.file.button -text "Load File" \
93 -command "textLoadFile $w.text \$fileName"
94 pack $w.file.label $w.file.entry -side left
95 pack $w.file.button -side left -pady 5 -padx 10
96 bind $w.file.entry <Return> "
97 textLoadFile $w.text \$fileName
98 focus $w.string.entry
100 focus $w.file.entry
102 frame $w.string
103 label $w.string.label -text "Search string:" -width 13 -anchor w
104 entry $w.string.entry -width 40 -textvariable searchString
105 button $w.string.button -text "Highlight" \
106 -command "textSearch $w.text \$searchString search"
107 pack $w.string.label $w.string.entry -side left
108 pack $w.string.button -side left -pady 5 -padx 10
109 bind $w.string.entry <Return> "textSearch $w.text \$searchString search"
111 text $w.text -yscrollcommand "$w.scroll set" -setgrid true
112 scrollbar $w.scroll -command "$w.text yview"
113 pack $w.file $w.string -side top -fill x
114 pack $w.scroll -side right -fill y
115 pack $w.text -expand yes -fill both
117 # Set up display styles for text highlighting.
119 if {[winfo depth $w] > 1} {
120 textToggle "$w.text tag configure search -background \
121 #ce5555 -foreground white" 800 "$w.text tag configure \
122 search -background {} -foreground {}" 200
123 } else {
124 textToggle "$w.text tag configure search -background \
125 black -foreground white" 800 "$w.text tag configure \
126 search -background {} -foreground {}" 200
128 $w.text insert 1.0 \
129 {This window demonstrates how to use the tagging facilities in text
130 widgets to implement a searching mechanism. First, type a file name
131 in the top entry, then type <Return> or click on "Load File". Then
132 type a string in the lower entry and type <Return> or click on
133 "Load File". This will cause all of the instances of the string to
134 be tagged with the tag "search", and it will arrange for the tag's
135 display attributes to change to make all of the strings blink.}
136 $w.text mark set insert 0.0
138 set fileName ""
139 set searchString ""