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 $w.ok -side bottom -fill x
21
pack $w.s -side right -fill y
22
pack $w.t -expand yes -fill both
24
# Set up display styles
26
if {[tk colormodel $w] == "color"} {
27
set bold "-foreground red"
28
set normal "-foreground {}"
30
set bold "-foreground white -background black"
31
set normal "-foreground {} -background {}"
34
The same tag mechanism that controls display styles in text
35
widgets can also be used to associate Tcl commands with regions
36
of text, so that mouse or keyboard actions on the text cause
37
particular Tcl commands to be invoked. For example, in the
38
text below the descriptions of the canvas demonstrations have
39
been tagged. When you move the mouse over a demo description
40
the description lights up, and when you press button 3 over a
41
description then that particular demonstration is invoked.
43
This demo package contains a number of demonstrations of Tk's
44
canvas widgets. Here are brief descriptions of some of the
45
demonstrations that are available:
49
{1. Samples of all the different types of items that can be
50
created in canvas widgets.} d1
51
insertWithTags $w.t \n\n
53
{2. A simple two-dimensional plot that allows you to adjust
54
the positions of the data points.} d2
55
insertWithTags $w.t \n\n
57
{3. Anchoring and justification modes for text items.} d3
58
insertWithTags $w.t \n\n
60
{4. An editor for arrow-head shapes for line items.} d4
61
insertWithTags $w.t \n\n
63
{5. A ruler with facilities for editing tab stops.} d5
64
insertWithTags $w.t \n\n
66
{6. A grid that demonstrates how canvases can be scrolled.} d6
68
foreach tag {d1 d2 d3 d4 d5 d6} {
69
$w.t tag bind $tag <Any-Enter> "$w.t tag configure $tag $bold"
70
$w.t tag bind $tag <Any-Leave> "$w.t tag configure $tag $normal"
72
$w.t tag bind d1 <3> mkItems
73
$w.t tag bind d2 <3> mkPlot
74
$w.t tag bind d3 <3> mkCanvText
75
$w.t tag bind d4 <3> mkArrow
76
$w.t tag bind d5 <3> mkRuler
77
$w.t tag bind d6 <3> mkScroll
79
$w.t mark set insert 0.0
80
bind $w <Any-Enter> "focus $w.t"
83
# The procedure below inserts text into a given text widget and
84
# applies one or more tags to that text. The arguments are:
86
# w Window in which to insert
87
# text Text to insert (it's inserted at the "insert" mark)
88
# args One or more tags to apply to text. If this is empty
89
# then all tags are removed from the text.
91
proc insertWithTags {w text args} {
92
set start [$w index insert]
93
$w insert insert $text
94
foreach tag [$w tag names $start] {
95
$w tag remove $tag $start insert
98
$w tag add $i $start insert