summaryrefslogtreecommitdiff
path: root/src/DrawResources/DrawTK.tcl
blob: ea523a431cb1b1d12776297300de8b3c90209a34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#
# TK features for Draw
#

# reload bindings
if { [info exists tk_library] } {
    set version [split [info tclversion] "."]
    set major [lindex ${version} 0]
    set minor [lindex ${version} 1]
    if { (${major} > 8) || (${major} >= 8 && ${minor} >= 4) } {
        #source $tk_library/tk.tcl
    } else {
        source $tk_library/tk.tcl
    }
}

wm geometry . +10+10

frame .mbar -relief raised -bd 2
pack .mbar -side top -fill x
focus .mbar

set theMenus("") ""
set Draw_MenuIndex 0

proc addmenuitem {menu options} {

    global theMenus Draw_MenuIndex
    if {![info exists theMenus($menu)]} {
	incr Draw_MenuIndex
	set m .mbar.m$Draw_MenuIndex.menu
	menubutton .mbar.m$Draw_MenuIndex -text $menu -menu $m
	pack .mbar.m$Draw_MenuIndex -side left
	menu $m
	set theMenus($menu) $m
    } else {set m $theMenus($menu)}

    eval $m add $options
}

proc addmenu {menu submenu {command ""}} {
    if {$command == ""} {set command $submenu}
    addmenuitem $menu "command -label $submenu -command {$command}"
}

#################################
# Menus definition
#################################

# the file menu

addmenu File datadir vdatadir
addmenu File restore vrestore
addmenu File source  vsource
addmenu File exit    

# the view menu

addmenu Views axo   {smallview AXON}
addmenu Views top   {smallview +X+Y}
addmenu Views front {smallview +X+Z}
addmenu Views left  {smallview +Y+Z}
addmenu Views 2d    {smallview -2D-}
addmenuitem Views   "separator"
addmenu Views mu4
addmenu Views av2d
addmenu Views axo
addmenu Views pers

# the display menu

addmenu Display fit   "fit; repaint"
addmenu Display 2dfit "2dfit; repaint"
addmenu Display clear
addmenu Display 2dclear


#################################
# Modal dialog box
# add OK, help,  cancel buttons  
#################################

proc modaldialog {box okproc {helpproc ""} {cancelproc ""}} {
    wm geometry $box +10+60

    button $box.ok -text ok -command "$okproc ; destroy $box"
    pack $box.ok -side left
    button $box.ko -text Cancel -command "$cancelproc ; destroy $box"
    pack $box.ko -side right
    if {$helpproc != ""} {
	button $box.help -text Help -command $helpproc
	pack $box.help -side right
    }
    grab set $box
}

##############################
#
# dialbox command arg1 val1 arg2 val2 ...
#
##############################

proc dialbox args {
    
    set com [lindex $args 0]

    toplevel .d
    wm title .d $com

    # com will be the command
    set com "eval $com"

    # create entries for the arguments
    set n [llength $args]

    for {set i 1} {$i < $n} {incr i 2} {

	frame .d.a$i
	label .d.a$i.l -text [lindex $args $i]
	entry .d.a$i.e -relief sunken
	.d.a$i.e insert end [lindex $args [expr $i+1]]
	pack .d.a$i.l -side left
	pack .d.a$i.e -side right
	pack .d.a$i -side top -fill x

	append com { [} ".d.a$i.e get" {]}
    }
    append com ";repaint"

    modaldialog .d $com "help [lindex $args 0]"
}


####################################
# Modal get file
#  select a file and launch a command
#  - file is the original value
#  - okproc is the OK procedure, 
#    it will be called with the filename
#  - title is the box title
#  - filter is called on each subfile
#  - Buttons are added in the dialbox, if none it is created
####################################

proc retyes {file} {return 1}

proc getfile {file okproc title {filter "retyes"} {box ""}} {

    if {$box == ""} {
	set box ".s"
	toplevel .s
    }
    wm title $box $title

    # The text entry at the top
    frame  $box.d
    entry  $box.d.e -relief sunken -width 40
    $box.d.e insert end $file
    button $box.d.s -text scan -command "filescan $filter $box"
    pack   $box.d.e -side left
    pack   $box.d.s -side right
    pack   $box.d   -side top

    # The list box with the files
    frame $box.f
    listbox $box.f.l -relief sunken -yscrollcommand "$box.f.s set"
    scrollbar $box.f.s -relief sunken -command "$box.f.l yview"
    pack $box.f.l $box.f.s -side left -fill y
    pack $box.f -side top

    filescan $filter $box

    bind $box.f.l <Double-Button-1> "fileclick $box $filter $okproc"
    
    modaldialog $box [concat $okproc " \[" $box.d.e "get\]"]
}

# when double click
proc fileclick {box filter okproc} {
    filescan $filter $box [selection get]
    set f [$box.d.e get]
    if {! [file isdirectory $f]} {
	destroy $box
	$okproc $f
    }
}

proc filescan {filter box {subfile ""}} {

    set s [$box.d.e get]
    if {$s == "."} {set s [pwd]/}

    $box.d.e delete 0 end
    if {$subfile != ""} {
	if {$subfile == ".."} {
	    set s [file dirname [file dirname $s]]/
	} else {
	    set s [file dirname $s]/$subfile
	}
    }
    $box.d.e insert end $s

    # list directories
    $box.f.l delete 0 end
    $box.f.l insert end ".."
    if [file isdirectory $s] {
	set d $s
	if {![string match */ $s]} {append s "/"}
    } else {
	set d [file dirname $s]
    }
    foreach f [glob -nocomplain $d/*] {
	if [$filter $f] {
	    set x [file tail $f]
	    if [file isdirectory $f] {append x "/"}
	    $box.f.l insert end $x
	}
    }
}


#################################
# File menu procedures
#################################

#
# dialog box for datadir
#

proc isdir {f} {return [file isdirectory $f]}

proc sdatadir {d} {
    global Draw_DataDir
    set Draw_DataDir $d
}

proc vdatadir {} {
    global Draw_DataDir
    toplevel .s
    frame .s.t
    button .s.t.d -text data -command {
	.s.d.e delete 0 end
	.s.d.e insert end $env(WBCONTAINER)/data/
	filescan isdir .s
    }
    pack .s.t.d -side left
    pack .s.t -side top
    getfile $Draw_DataDir sdatadir "Data Directory" isdir .s
}

proc notild {f} {return [expr ! [string match *~ $f]]}

proc rresto {f} {
    if {! [file isdirectory $f]} {
	uplevel \#0 "brestore $f [file tail $f]"
	repaint
    }
}

proc vrestore {} {
    global Draw_DataDir 
    getfile $Draw_DataDir rresto "Restore" notild
}


proc ssour {f} {
    global Draw_Source
    set Draw_Source $f
    if {! [file isdirectory $f]} {
	uplevel \#0 "source $f"
    }
}

set Draw_Source [pwd]
proc vsource {} {
    global Draw_Source 
    getfile $Draw_Source  ssour "Source" notild
}