1
#----------------------------------------------------------------
2
# Miscellaneous procedures
4
#----------------------------------------------------------------
6
#----------------------------------------------------------------
8
#----------------------------------------------------------------
10
if [catch {expr $tk_priv(new_tcltk) == 0 || $tk_priv(new_tcltk) == 1}] {
11
set tk_priv(new_tcltk) 0
12
if [regexp "(\[0-9\]+\.\[0-9\]+)" $tk_patchLevel cur] {
14
set tk_priv(new_tcltk) 1
22
proc retrieve-filename {path} {
23
set divs [split $path /]
24
return [lindex $divs [expr [llength $divs] - 1]]
33
return [format "%02d:%02d" [expr $sec / 60] [expr $sec % 60]]
36
return [format "-%02d:%02d" [expr $sec / 60] [expr $sec % 60]]
42
# only numerical key and some controls are available for input.
44
proc numeric-bind {w} {
46
if {"%A" != "" && [regexp "\[0-9\]+" %A]} {
49
} elseif {"%K" == "Return"} {
54
bind $w <Key-Delete> {tk_entryBackspace %W; tk_entrySeeCaret %W}
55
bind $w <Key-BackSpace> {tk_entryBackspace %W; tk_entrySeeCaret %W}
56
bind $w <Control-Key-h> {tk_entryBackspace %W; tk_entrySeeCaret %W}
57
bind $w <Control-Key-d> {%W delete sel.first sel.last; tk_entrySeeCaret %W}
58
bind $w <Control-Key-u> {%W delete 0 end}
64
proc my-listbox {w title size {dohoriz 1} {multiple 0}} {
67
label $w.label -text $title -relief flat
68
pack $w.label -side top -fill x -anchor w
69
scrollbar $w.yscr -command "$w.list yview"
70
pack $w.yscr -side right -fill y
71
if {$tk_priv(new_tcltk)} {
72
regexp "(\[0-9\]+)x(\[0-9\])" $size foo width height
73
set lopt [list -width $width -height $height]
75
lappend lopt -selectmode multiple
78
set lopt [list -geometry $size]
81
scrollbar $w.xscr -command "$w.list xview" -orient horizontal
82
pack $w.xscr -side bottom -fill x
83
eval listbox $w.list -relief sunken -setgrid yes $lopt\
84
[list -yscroll "$w.yscr set"]\
85
[list -xscroll "$w.xscr set"]
87
eval listbox $w.list -relief sunken -setgrid yes $lopt\
88
[list -yscroll "$w.yscr set"]
90
pack $w.list -side left -fill both -expand yes
94
#----------------------------------------------------------------
96
#----------------------------------------------------------------
98
proc my-dialog {w title defbtn canbtn buttons} {
99
toplevel $w -class Dialog
101
wm iconname $w $title
103
label $w.title -text $title -relief raised -bd 1
104
pack $w.title -side top -fill x
106
frame $w.f -relief raised -bd 1
107
pack $w.f -side top -fill both
109
frame $w.buttons -relief raised -bd 1
110
pack $w.buttons -side bottom -fill both
112
foreach but $buttons {
113
button $w.buttons.c$i -text [lindex $but 0] -command [lindex $but 1]
114
if {$defbtn != "" && $i == $defbtn} {
115
frame $w.buttons.default -relief sunken -bd 1
116
raise $w.buttons.c$i $w.buttons.default
117
pack $w.buttons.default -side left -expand 1\
119
pack $w.buttons.c$i -in $w.buttons.default -padx 2m -pady 2m \
121
bind $w <Return> "$w.buttons.c$i flash; $w.buttons.c$i invoke"
123
pack $w.buttons.c$i -side left -expand 1 \
124
-padx 3m -pady 3m -ipadx 2m -ipady 1m
125
if {$canbtn != "" && $i == $canbtn} {
126
bind $w <Key-Escape> "$w.buttons.c$i flash; $w.buttons.c$i invoke"
135
#----------------------------------------------------------------
136
# warning/question dialog
137
#----------------------------------------------------------------
139
if {$tk_priv(new_tcltk)} {
140
proc my-message-dialog {w title text bitmap defbtn canbtn args} {
142
return [eval tk_dialog [list $w $title $text $bitmap $defbtn] $args]
145
proc my-message-dialog {w title text bitmap defbtn canbtn args} {
151
lappend butlist [list $i "set tk_priv(button) $num; destroy $w"]
154
set f [my-dialog $w $title $defbtn $canbtn $butlist]
156
message $f.msg -width 3i -text $text
157
pack $f.msg -side right -expand 1 -fill both -padx 5m -pady 5m
159
label $f.bitmap -bitmap $bitmap
160
pack $f.bitmap -side left -padx 5m -pady 5m
162
set tk_priv(button) 0
166
set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
167
- [winfo vrootx [winfo parent $w]]]
168
set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
169
- [winfo vrooty [winfo parent $w]]]
178
return $tk_priv(button)
182
proc warning {message} {
183
my-message-dialog .warning "Warning" $message warning 0 0 { OK }
186
proc error {message} {
187
my-message-dialog .error "Error" $message error 0 0 { OK }
190
proc information {message} {
191
my-message-dialog .info "Information" $message info 0 0 { OK }
194
proc question {message {defrc 1}} {
203
return [expr ![my-message-dialog .yesno "Question" $message question\
204
$defbtn $canbtn "Yes" "No"]]
207
#----------------------------------------------------------------
208
# get the root file name from full path
209
#----------------------------------------------------------------
211
proc rootname {path} {
214
} elseif [regexp "\[^/\]+$" $path base] {
216
} elseif [regexp "(\[^/\]+)/$" $path rest base] {
223
#----------------------------------------------------------------
224
# pseudo random routine without TclX
225
#----------------------------------------------------------------
227
set pseudo_random [catch {random 1}]
228
set pseudo_random_next -1
229
proc my-random {max} {
230
global pseudo_random pseudo_random_next
231
if {$pseudo_random} {
232
set pseudo_random_next [expr $pseudo_random_next * 1103515245 + 12345]
233
return [expr ($pseudo_random_next/65536) % $max]
234
# Or, use bash's random routine instead...
235
# return [expr [exec bash -c {echo $RANDOM}] % $max]
240
proc init-random {num} {
241
global pseudo_random pseudo_random_next
242
if {$pseudo_random} {
243
set pseudo_random_next $num