3 # This demonstration script creates a collection of widgets that
4 # allow you to load a file into a text widget, then perform searches
7 if {![info exists widgetDemo
]} {
8 error "This script should be run from the \"widget\" demo."
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.
19 # w - The window into which to load the file. Must be a
21 # file - The name of the file to load. Must be readable.
23 proc textLoadFile
{w
file} {
27 $w insert end
[read $f 10000]
33 # Search for all instances of a given string in a text widget and
34 # apply a given tag to each instance found.
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
49 set cur
[$w search
-count length
$string $cur end
]
53 $w tag add
$tag $cur "$cur + $length char"
54 set cur
[$w index
"$cur + $length char"]
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.
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
69 # sleep2 - Ms to sleep after executing cmd2 before executing cmd1 again.
71 proc textToggle
{cmd1 sleep1 cmd2 sleep2
} {
74 after $sleep1 [list textToggle
$cmd2 $sleep2 $cmd1 $sleep1]
81 wm title
$w "Text Demonstration - Search and Highlight"
82 wm iconname
$w "search"
85 ## See Code / Dismiss buttons
86 set btns
[addSeeDismiss
$w.buttons
$w]
87 pack $btns -side bottom
-fill x
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
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
124 textToggle
"$w.text tag configure search -background \
125 black -foreground white" 800 "$w.text tag configure \
126 search -background {} -foreground {}" 200
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