3 # Create a top-level window that illustrates how you can bind
4 # Tcl commands to regions of text in a text widget.
7 # w - Name to use for new top-level window.
9 proc mkTextBind
{{w .bindings
}} {
13 wm title
$w "Text Demonstration - Tag Bindings"
14 wm iconname
$w "Text Bindings"
15 button $w.ok
-text OK
-command "destroy $w"
16 text $w.t
-relief raised
-bd 2 -yscrollcommand "$w.s set" -setgrid true
\
17 -width 60 -height 28 \
18 -font "-Adobe-Helvetica-Bold-R-Normal-*-120-*"
19 scrollbar $w.s
-relief flat
-command "$w.t yview"
20 pack append $w $w.ok
{bottom fillx
} $w.s
{right filly
} $w.t
{expand fill
}
22 # Set up display styles
24 if {[winfo screendepth
$w] > 4} {
25 set bold
"-foreground red"
26 set normal
"-foreground {}"
28 set bold
"-foreground white -background black"
29 set normal
"-foreground {} -background {}"
32 The same tag mechanism that controls display styles in
text
33 widgets can also be used to associate
Tcl commands with regions
34 of
text, so that mouse or keyboard actions on the
text cause
35 particular
Tcl commands to be invoked. For example
, in the
36 text below the descriptions of the
canvas demonstrations have
37 been tagged. When you move the mouse over a demo description
38 the description lights up
, and when you press
button 3 over a
39 description then that particular demonstration is invoked.
41 This demo
package contains a number of demonstrations of Tk's
42 canvas widgets. Here are brief descriptions of some of the
43 demonstrations that are available
:
47 {1. Samples of all the different types of items that can be
48 created in
canvas widgets.
} d1
49 insertWithTags
$w.t
\n\n
51 {2. A simple two-dimensional plot that allows you to adjust
52 the positions of the data points.
} d2
53 insertWithTags
$w.t
\n\n
55 {3. Anchoring and justification modes
for text items.
} d3
56 insertWithTags
$w.t
\n\n
58 {4. An editor
for arrow-head shapes
for line items.
} d4
59 insertWithTags
$w.t
\n\n
61 {5. A ruler with facilities
for editing tab stops.
} d5
62 insertWithTags
$w.t
\n\n
64 {6. A
grid that demonstrates how canvases can be scrolled.
} d6
66 foreach tag
{d1 d2 d3 d4 d5 d6
} {
67 $w.t tag
bind $tag <Any-Enter
> "$w.t tag configure $tag $bold"
68 $w.t tag
bind $tag <Any-Leave
> "$w.t tag configure $tag $normal"
70 $w.t tag
bind d1
<3> mkItems
71 $w.t tag
bind d2
<3> mkPlot
72 $w.t tag
bind d3
<3> mkCanvText
73 $w.t tag
bind d4
<3> mkArrow
74 $w.t tag
bind d5
<3> mkRuler
75 $w.t tag
bind d6
<3> mkScroll
77 $w.t mark
set insert
0.0
78 bind $w <Any-Enter
> "focus $w.t"
81 # The procedure below inserts text into a given text widget and
82 # applies one or more tags to that text. The arguments are:
84 # w Window in which to insert
85 # text Text to insert (it's inserted at the "insert" mark)
86 # args One or more tags to apply to text. If this is empty
87 # then all tags are removed from the text.
89 proc insertWithTags
{w
text args
} {
90 set start
[$w index insert
]
91 $w insert insert
$text
92 foreach tag
[$w tag names
$start] {
93 $w tag remove
$tag $start insert
96 $w tag add
$i $start insert