~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gcl-tk/demos-4.2/widget

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# the next line restarts using wish \
 
3
exec wish4.2 "$0" "$@"
 
4
 
 
5
# widget --
 
6
# This script demonstrates the various widgets provided by Tk,
 
7
# along with many of the features of the Tk toolkit.  This file
 
8
# only contains code to generate the main window for the
 
9
# application, which invokes individual demonstrations.  The
 
10
# code for the actual demonstrations is contained in separate
 
11
# ".tcl" files is this directory, which are sourced by this script
 
12
# as needed.
 
13
#
 
14
# SCCS: @(#) widget 1.21 96/10/04 17:09:34
 
15
 
 
16
eval destroy [winfo child .]
 
17
wm title . "Widget Demonstration"
 
18
 
 
19
#----------------------------------------------------------------
 
20
# The code below create the main window, consisting of a menu bar
 
21
# and a text widget that explains how to use the program, plus lists
 
22
# all of the demos as hypertext items.
 
23
#----------------------------------------------------------------
 
24
 
 
25
set font -*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*
 
26
frame .menuBar
 
27
pack .menuBar -side top -fill x
 
28
menubutton .menuBar.file -text File -menu .menuBar.file.m -underline 0
 
29
menu .menuBar.file.m
 
30
.menuBar.file.m add command -label "About ... " -command "aboutBox" \
 
31
    -underline 0 -accelerator "<F1>"
 
32
.menuBar.file.m add sep
 
33
.menuBar.file.m add command -label "Quit" -command "exit" -underline 0
 
34
pack .menuBar.file -side left
 
35
bind . <F1> aboutBox
 
36
 
 
37
frame .textFrame
 
38
scrollbar .s -orient vertical -command {.t yview} -highlightthickness 0 \
 
39
    -takefocus 1
 
40
pack .s -in .textFrame -side right -fill y -padx 1
 
41
text .t -yscrollcommand {.s set} -wrap word -width 60 -height 30 -font $font \
 
42
    -setgrid 1 -highlightthickness 0 -padx 4 -pady 2 -takefocus 0
 
43
pack .t -in .textFrame -expand y -fill both -padx 1
 
44
pack  .textFrame -expand yes -fill both -padx 1 -pady 2
 
45
 
 
46
frame .statusBar
 
47
label .statusBar.lab -text "   " -relief sunken -bd 1 \
 
48
    -font -*-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-* -anchor w
 
49
label .statusBar.foo -width 8 -relief sunken -bd 1 \
 
50
    -font -*-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-* -anchor w
 
51
pack .statusBar.lab -side left -padx 2 -expand yes -fill both
 
52
pack .statusBar.foo -side left -padx 2
 
53
pack .statusBar -side top -fill x -pady 2
 
54
 
 
55
# Create a bunch of tags to use in the text widget, such as those for
 
56
# section titles and demo descriptions.  Also define the bindings for
 
57
# tags.
 
58
 
 
59
.t tag configure title -font -*-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-*
 
60
 
 
61
# We put some "space" characters to the left and right of each demo description
 
62
# so that the descriptions are highlighted only when the mouse cursor
 
63
# is right over them (but not when the cursor is to their left or right)
 
64
#
 
65
.t tag configure demospace -lmargin1 1c -lmargin2 1c
 
66
 
 
67
 
 
68
if {[winfo depth .] == 1} {
 
69
    .t tag configure demo -lmargin1 1c -lmargin2 1c \
 
70
        -underline 1
 
71
    .t tag configure visited -lmargin1 1c -lmargin2 1c \
 
72
        -underline 1
 
73
    .t tag configure hot -background black -foreground white
 
74
} else {
 
75
    .t tag configure demo -lmargin1 1c -lmargin2 1c \
 
76
        -foreground blue -underline 1
 
77
    .t tag configure visited -lmargin1 1c -lmargin2 1c \
 
78
        -foreground #303080 -underline 1
 
79
    .t tag configure hot -foreground red -underline 1
 
80
}
 
81
.t tag bind demo <ButtonRelease-1> {
 
82
    invoke [.t index {@%x,%y}]
 
83
}
 
84
set lastLine ""
 
85
.t tag bind demo <Enter> {
 
86
    set lastLine [.t index {@%x,%y linestart}]
 
87
    .t tag add hot "$lastLine +1 chars" "$lastLine lineend -1 chars"
 
88
    .t config -cursor hand2
 
89
    showStatus [.t index {@%x,%y}]
 
90
}
 
91
.t tag bind demo <Leave> {
 
92
    .t tag remove hot 1.0 end
 
93
    .t config -cursor xterm
 
94
    .statusBar.lab config -text ""
 
95
}
 
96
.t tag bind demo <Motion> {
 
97
    set newLine [.t index {@%x,%y linestart}]
 
98
    if {[string compare $newLine $lastLine] != 0} {
 
99
        .t tag remove hot 1.0 end
 
100
        set lastLine $newLine
 
101
 
 
102
        set tags [.t tag names {@%x,%y}]
 
103
        set i [lsearch -glob $tags demo-*]
 
104
        if {$i >= 0} {
 
105
            .t tag add hot "$lastLine +1 chars" "$lastLine lineend -1 chars"
 
106
        }
 
107
    }
 
108
    showStatus [.t index {@%x,%y}]
 
109
}
 
110
 
 
111
# Create the text for the text widget.
 
112
 
 
113
.t insert end "Tk Widget Demonstrations\n" title
 
114
.t insert end {
 
115
This application provides a front end for several short scripts that demonstrate what you can do with Tk widgets.  Each of the numbered lines below describes a demonstration;  you can click on it to invoke the demonstration.  Once the demonstration window appears, you can click the "See Code" button to see the Tcl/Tk code that created the demonstration.  If you wish, you can edit the code and click the "Rerun Demo" button in the code window to reinvoke the demonstration with the modified code.
 
116
 
 
117
}
 
118
.t insert end "Labels, buttons, checkbuttons, and radiobuttons" title
 
119
.t insert end " \n " {demospace}
 
120
.t insert end "1. Labels (text and bitmaps)." {demo demo-label}
 
121
.t insert end " \n " {demospace}
 
122
.t insert end "2. Buttons." {demo demo-button}
 
123
.t insert end " \n " {demospace}
 
124
.t insert end "3. Checkbuttons (select any of a group)." {demo demo-check}
 
125
.t insert end " \n " {demospace}
 
126
.t insert end "4. Radiobuttons (select one of a group)." {demo demo-radio}
 
127
.t insert end " \n " {demospace}
 
128
.t insert end "5. A 15-puzzle game made out of buttons." {demo demo-puzzle}
 
129
.t insert end " \n " {demospace}
 
130
.t insert end "6. Iconic buttons that use bitmaps." {demo demo-icon}
 
131
.t insert end " \n " {demospace}
 
132
.t insert end "7. Two labels displaying images." {demo demo-image1}
 
133
.t insert end " \n " {demospace}
 
134
.t insert end "8. A simple user interface for viewing images." \
 
135
    {demo demo-image2}
 
136
.t insert end " \n " {demospace}
 
137
 
 
138
.t insert end \n {} "Listboxes" title
 
139
.t insert end " \n " {demospace}
 
140
.t insert end "1. 50 states." {demo demo-states}
 
141
.t insert end " \n " {demospace}
 
142
.t insert end "2. Colors: change the color scheme for the application." \
 
143
        {demo demo-colors}
 
144
.t insert end " \n " {demospace}
 
145
.t insert end "3. A collection of famous sayings." {demo demo-sayings}
 
146
.t insert end " \n " {demospace}
 
147
 
 
148
.t insert end \n {} "Entries" title
 
149
.t insert end " \n " {demospace}
 
150
.t insert end "1. Without scrollbars." {demo demo-entry1}
 
151
.t insert end " \n " {demospace}
 
152
.t insert end "2. With scrollbars." {demo demo-entry2}
 
153
.t insert end " \n " {demospace}
 
154
.t insert end "3. Simple Rolodex-like form." {demo demo-form}
 
155
.t insert end " \n " {demospace}
 
156
 
 
157
.t insert end \n {} "Text" title
 
158
.t insert end " \n " {demospace}
 
159
.t insert end "1. Basic editable text." {demo demo-text}
 
160
.t insert end " \n " {demospace}
 
161
.t insert end "2. Text display styles." {demo demo-style}
 
162
.t insert end " \n " {demospace}
 
163
.t insert end "3. Hypertext (tag bindings)." {demo demo-bind}
 
164
.t insert end " \n " {demospace}
 
165
.t insert end "4. A text widget with embedded windows." {demo demo-twind}
 
166
.t insert end " \n " {demospace}
 
167
.t insert end "5. A search tool built with a text widget." {demo demo-search}
 
168
.t insert end " \n " {demospace}
 
169
 
 
170
.t insert end \n {} "Canvases" title
 
171
.t insert end " \n " {demospace}
 
172
.t insert end "1. The canvas item types." {demo demo-items}
 
173
.t insert end " \n " {demospace}
 
174
.t insert end "2. A simple 2-D plot." {demo demo-plot}
 
175
.t insert end " \n " {demospace}
 
176
.t insert end "3. Text items in canvases." {demo demo-ctext}
 
177
.t insert end " \n " {demospace}
 
178
.t insert end "4. An editor for arrowheads on canvas lines." {demo demo-arrow}
 
179
.t insert end " \n " {demospace}
 
180
.t insert end "5. A ruler with adjustable tab stops." {demo demo-ruler}
 
181
.t insert end " \n " {demospace}
 
182
.t insert end "6. A building floor plan." {demo demo-floor}
 
183
.t insert end " \n " {demospace}
 
184
.t insert end "7. A simple scrollable canvas." {demo demo-cscroll}
 
185
.t insert end " \n " {demospace}
 
186
 
 
187
.t insert end \n {} "Scales" title
 
188
.t insert end " \n " {demospace}
 
189
.t insert end "1. Vertical scale." {demo demo-vscale}
 
190
.t insert end " \n " {demospace}
 
191
.t insert end "2. Horizontal scale." {demo demo-hscale}
 
192
.t insert end " \n " {demospace}
 
193
 
 
194
.t insert end \n {} "Menus" title
 
195
.t insert end " \n " {demospace}
 
196
.t insert end "1. A window containing several menus and cascades." \
 
197
        {demo demo-menu}
 
198
.t insert end " \n " {demospace}
 
199
 
 
200
.t insert end \n {} "Common Dialogs" title
 
201
.t insert end " \n " {demospace}
 
202
.t insert end "1. Message boxes." {demo demo-msgbox}
 
203
.t insert end " \n " {demospace}
 
204
.t insert end "2. File selection dialog." {demo demo-filebox}
 
205
.t insert end " \n " {demospace}
 
206
.t insert end "3. Color picker." {demo demo-clrpick}
 
207
.t insert end " \n " {demospace}
 
208
 
 
209
.t insert end \n {} "Miscellaneous" title
 
210
.t insert end " \n " {demospace}
 
211
.t insert end "1. The built-in bitmaps." {demo demo-bitmap}
 
212
.t insert end " \n " {demospace}
 
213
.t insert end "2. A dialog box with a local grab." {demo demo-dialog1}
 
214
.t insert end " \n " {demospace}
 
215
.t insert end "3. A dialog box with a global grab." {demo demo-dialog2}
 
216
.t insert end " \n " {demospace}
 
217
 
 
218
.t configure -state disabled
 
219
focus .s
 
220
 
 
221
# positionWindow --
 
222
# This procedure is invoked by most of the demos to position a
 
223
# new demo window.
 
224
#
 
225
# Arguments:
 
226
# w -           The name of the window to position.
 
227
 
 
228
proc positionWindow w {
 
229
    wm geometry $w +300+300
 
230
}
 
231
 
 
232
# showVars --
 
233
# Displays the values of one or more variables in a window, and
 
234
# updates the display whenever any of the variables changes.
 
235
#
 
236
# Arguments:
 
237
# w -           Name of new window to create for display.
 
238
# args -        Any number of names of variables.
 
239
 
 
240
proc showVars {w args} {
 
241
    catch {destroy $w}
 
242
    toplevel $w
 
243
    wm title $w "Variable values"
 
244
    label $w.title -text "Variable values:" -width 20 -anchor center \
 
245
            -font -Adobe-helvetica-medium-r-normal--*-180-*-*-*-*-*-*
 
246
    pack $w.title -side top -fill x
 
247
    set len 1
 
248
    foreach i $args {
 
249
        if {[string length $i] > $len} {
 
250
            set len [string length $i]
 
251
        }
 
252
    }
 
253
    foreach i $args {
 
254
        frame $w.$i
 
255
        label $w.$i.name -text "$i: " -width [expr $len + 2] -anchor w
 
256
        label $w.$i.value -textvar $i -anchor w
 
257
        pack $w.$i.name -side left
 
258
        pack $w.$i.value -side left -expand 1 -fill x
 
259
        pack $w.$i -side top -anchor w -fill x
 
260
    }
 
261
    button $w.ok -text OK -command "destroy $w"
 
262
    pack $w.ok -side bottom -pady 2
 
263
}
 
264
 
 
265
# invoke --
 
266
# This procedure is called when the user clicks on a demo description.
 
267
# It is responsible for invoking the demonstration.
 
268
#
 
269
# Arguments:
 
270
# index -       The index of the character that the user clicked on.
 
271
 
 
272
proc invoke index {
 
273
    global tk_library
 
274
    set tags [.t tag names $index]
 
275
    set i [lsearch -glob $tags demo-*]
 
276
    if {$i < 0} {
 
277
        return
 
278
    }
 
279
    set cursor [.t cget -cursor]
 
280
    .t configure -cursor watch
 
281
    update
 
282
    set demo [string range [lindex $tags $i] 5 end]
 
283
    uplevel [list source [file join $tk_library demos $demo.tcl]]
 
284
    update
 
285
    .t configure -cursor $cursor
 
286
 
 
287
    .t tag add visited "$index linestart +1 chars" "$index lineend -1 chars"
 
288
}
 
289
 
 
290
# showStatus --
 
291
#
 
292
#       Show the name of the demo program in the status bar. This procedure
 
293
#       is called when the user moves the cursor over a demo description.
 
294
#
 
295
proc showStatus index {
 
296
    global tk_library
 
297
    set tags [.t tag names $index]
 
298
    set i [lsearch -glob $tags demo-*]
 
299
    set cursor [.t cget -cursor]
 
300
    if {$i < 0} {
 
301
        .statusBar.lab config -text " "
 
302
        set newcursor xterm
 
303
    } else {
 
304
        set demo [string range [lindex $tags $i] 5 end]
 
305
        .statusBar.lab config -text "Run the \"$demo\" sample program"
 
306
        set newcursor hand2
 
307
    }
 
308
    if [string compare $cursor $newcursor] {
 
309
        .t config -cursor $newcursor
 
310
    }
 
311
}
 
312
 
 
313
 
 
314
# showCode --
 
315
# This procedure creates a toplevel window that displays the code for
 
316
# a demonstration and allows it to be edited and reinvoked.
 
317
#
 
318
# Arguments:
 
319
# w -           The name of the demonstration's window, which can be
 
320
#               used to derive the name of the file containing its code.
 
321
 
 
322
proc showCode w {
 
323
    global tk_library
 
324
    set file [string range $w 1 end].tcl
 
325
    if ![winfo exists .code] {
 
326
        toplevel .code
 
327
        frame .code.buttons
 
328
        pack .code.buttons -side bottom -fill x
 
329
        button .code.buttons.dismiss -text Dismiss -command "destroy .code"
 
330
        button .code.buttons.rerun -text "Rerun Demo" -command {
 
331
            eval [.code.text get 1.0 end]
 
332
        }
 
333
        pack .code.buttons.dismiss .code.buttons.rerun -side left \
 
334
            -expand 1 -pady 2
 
335
        frame .code.frame
 
336
        pack  .code.frame -expand yes -fill both -padx 1 -pady 1
 
337
        text .code.text -height 40 -wrap word\
 
338
            -xscrollcommand ".code.xscroll set" \
 
339
            -yscrollcommand ".code.yscroll set" \
 
340
            -setgrid 1 -highlightthickness 0 -pady 2 -padx 3
 
341
        scrollbar .code.xscroll -command ".code.text xview" \
 
342
            -highlightthickness 0 -orient horizontal
 
343
        scrollbar .code.yscroll -command ".code.text yview" \
 
344
            -highlightthickness 0 -orient vertical
 
345
 
 
346
        grid .code.text -in .code.frame -padx 1 -pady 1 \
 
347
            -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
 
348
        grid .code.yscroll -in .code.frame -padx 1 -pady 1 \
 
349
            -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
 
350
#       grid .code.xscroll -in .code.frame -padx 1 -pady 1 \
 
351
#           -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
 
352
        grid rowconfig    .code.frame 0 -weight 1 -minsize 0
 
353
        grid columnconfig .code.frame 0 -weight 1 -minsize 0
 
354
    } else {
 
355
        wm deiconify .code
 
356
        raise .code
 
357
    }
 
358
    wm title .code "Demo code: [file join $tk_library demos $file]"
 
359
    wm iconname .code $file
 
360
    set id [open [file join $tk_library demos $file]]
 
361
    .code.text delete 1.0 end
 
362
    .code.text insert 1.0 [read $id]
 
363
    .code.text mark set insert 1.0
 
364
    close $id
 
365
}
 
366
 
 
367
# aboutBox --
 
368
#
 
369
#       Pops up a message box with an "about" message
 
370
#
 
371
proc aboutBox {} {
 
372
    tk_messageBox -icon info -type ok -title "About Widget Demo" -message \
 
373
"Tk widget demonstration\n\n\
 
374
Copyright (c) 1996 Sun Microsystems, Inc."
 
375
}
 
376