File: checkbutton.tcl

package info (click to toggle)
amsn 0.98.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,876 kB
  • ctags: 10,292
  • sloc: tcl: 117,923; ansic: 32,173; cpp: 17,387; xml: 6,643; objc: 1,251; sh: 667; makefile: 544; perl: 215; python: 126
file content (317 lines) | stat: -rw-r--r-- 8,620 bytes parent folder | download | duplicates (4)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package require snit

snit::widgetadaptor pixmapcheckbutton {

	typevariable widgetlist
	typevariable loadimagecommand

	typevariable blank

	typevariable normal
	typevariable hover
	typevariable disabled

	typevariable normalpress
	typevariable hoverpress
	typevariable disabledpress

	variable selected

	option -activeforeground -configuremethod SetActiveForeground
	option -activefg -configuremethod SetActiveForeground
	option -command
	option -font -configuremethod SetFont
	option -foreground -configuremethod SetForeground
	option -fg -configuremethod SetForeground
	option -indicatoron -default 1 -configuremethod SetIndicatorOn
	option -offvalue
	option -onvalue
	option -selected
	option -state -default normal -configuremethod SetState
	option -takefocus -default 1
	option -text -configuremethod SetText
	option -variable


	typeconstructor {
		$type SetImageCommand "$type loadimage"
		$type reloadimages 1
	}

	constructor { args } {
		lappend widgetlist $self
		installhull using canvas -bg white -relief flat -highlightthickness 0

		$hull create image 0 0 -image $normal -activeimage $hover -disabledimage $disabled -anchor nw -tag button
		$hull create text 0 0 -anchor w -tag label
		$hull coords label [image width $normal] [expr [image height $normal] / 2]
		set selected "no"
		$self configurelist $args
		$self SetState -state $options(-state)
		$self configure -selected "no"
		$self UpdateVariable 0

		$hull configure -width [expr [$self MeasureText $options(-text) w] + [image width $normal]]
		
		set ih [image height $normal]
		set th [$self MeasureText $options(-text) h]
		if { $ih > $th } {
			$hull configure -height $ih
		} else {
			$hull configure -height $th
		}

		bind $self <Enter> "$self ButtonHovered"
		bind $self <Leave> "$self ButtonUnhovered"
		bind $self <Button-1> "$self toggle"
		bind $self <KeyPress-space> "$self toggle"
		bind $self <KeyPress-plus> "$self select"
		bind $self <KeyPress-equal> "$self select"
		bind $self <KeyPress-minus> "$self deselect"
	}

	#////////////////////////////////////////////////////////////////
	# PUBLIC COMMANDS						/
	# deselect							/
	# flash								/
	# invoke								/
	# select								/
	# toggle								/
	#////////////////////////////////////////////////////////////////
	method toggle { } {
		if { $options(-state) != "disabled" } {
			switch $options(-selected) {
				no {
					$self select
				}
				yes {
					$self deselect
				}
			}
		}
	}

	method invoke { } {
		if { $options(-state) != "disabled" } {
			eval $options(-command)
		}
	}

	method flash { } {
		if { $options(-state) != "disabled" } {
			after 250 $self toggle
			after 500 $self toggle
			after 750 $self toggle
			after 1000 $self toggle
		}
	}

	method select { } {
		$self configure -selected "yes"
		if { $options(-state) != "disabled" } {
			if { $options(-variable) != "" } {
				$self UpdateVariable $options(-onvalue)
			}
			$hull itemconfigure button -image $normalpress -disabledimage $disabledpress
			$self invoke
		}
	}

	method deselect { } {
		$self configure -selected "no"
		if { $options(-state) != "disabled" } {
			if { $options(-variable) != "" } {
				$self UpdateVariable $options(-offvalue)
			}
			$hull itemconfigure button -image $normal -disabledimage $disabled
		}
	}

	#////////////////////////////////////////////////////////
	# PRIVATE COMMANDS					/
	#////////////////////////////////////////////////////////
	method ButtonHovered { } {
		if { $options(-state) != "disabled" } {
			if { $options(-selected) == "yes" } {
				$hull itemconfigure button -image $hoverpress
			} else {
				$hull itemconfigure button -image $hover
			}
		}
	}
	
	method ButtonUnhovered { } {
		if { $options(-state) != "disabled" } {
			if { $options(-selected) == "yes" } {
				$hull itemconfigure button -image $normalpress
			} else {
				$hull itemconfigure button -image $normal
			}
		}
	}
	
	method SetFont { option value } {
		set options(-font) $value
		$hull itemconfigure label -font $value
	}
	
	method SetActiveForeground { option value } {
		set options(-activeforeground) $value
		set options(-activefg) $value
		$hull itemconfigure label -activefill $value
	}
	
	method SetForeground { option value } {
		set options(-foreground) $value
		set options(-fg) $value
		$hull itemconfigure label -fill $value
	}
	
	method SetIndicatorOn { option value } {
		set options(-indicatoron) $value
		if { !$value } {
			$hull delete button
			$hull coords label 0 [expr [image height $normal] / 2]
		} else {
			switch $options(-selected) {
				yes {
					$hull create image 0 0 -image $normalpress -disabledimage $disabledpress -anchor nw -tag button
				}
				no {
					$hull create image 0 0 -image $normal -disabledimage $disabled -anchor nw -tag button
				}
			}
			$hull coords label [image width $normal] [expr [image height $normal] / 2]
			switch $options(-state) {
				disabled { $hull itemconfigure button -state disabled }
				default { $hull itemconfigure button -state normal }
			}
		}
	}
	
	method SetState { option value } {
		set options(-state) $value
		switch $value {
			normal {
				switch $options(-selected) {
					yes { $hull itemconfigure button -image $normalpress } 
					no { $hull itemconfigure button -image $normal }
				}
			}
			active {
				switch $options(-selected) {
					yes { $hull itemconfigure button -image $hoverpress } 
					no { $hull itemconfigure button -image $hover } 
				}
			}
			disabled {
				$hull itemconfigure button -state disabled
			}
		}
		
	}
	
	method SetText { option value } {
		set options(-text) $value
		$hull itemconfigure label -text $value
		$hull configure -width [expr [$self MeasureText $options(-text) w] + [image width $normal]]
		
		set ih [image height $normal]
		set th [$self MeasureText $options(-text) h]
		if { $ih > $th } {
			$hull configure -height $ih
		} else {
			$hull configure -height $th
		}
	}

	method UpdateVariable { value } {
		uplevel #0 "set $options(-variable) $value"
	}

	method MeasureText { text dimension } {
		if { $dimension == "w" } {
			if { $options(-font) != "" } {
				set idx [expr [string first "\n" $text] - 1] 
				if { $idx < 0 } { set idx end }
				set m [font measure $options(-font) -displayof $self [string range $text \
					0 $idx]]
				return $m
			} else {
				set idx [expr [string first "\n" $text] - 1] 
				if { $idx < 0 } { set idx end }
				set f [font create -family helvetica -size 12 -weight normal]
				set m [font measure $f -displayof $self [string range $text \
					0 $idx]]
				font delete $f
				return $m
			}
		} elseif { $dimension == "h" } {
			if { $options(-font) != "" } {
				#Get number of lines
				set n [$self NumberLines $text]
				#Multiply font size by no. lines and add gap between lines * (no. lines - 1).
				return [expr $n * [font configure $options(-font) -size] + (($n - 1) * 7)]
			} else {
				#Get number of lines
				set n [$self NumberLines $text]
				#Multiply font size by no. lines and add gap between lines * (no. lines - 1).
				return [expr ($n * 12) + (($n - 1) * 7)]
			}
		}
	}

	method NumberLines { string } {
		for { set n 0; set idx 0 } { [string first "\n" $string $idx] != -1} { incr n 1 } {
					set idx [expr [string first "\n" $string $idx] +1]
				}
		if { $n < 1 } { set n 1 }
		return $n
	}

	#/////////////////////////////////////////////////////
	
	#/////////////////////////////////////////////////////
	# Image (re)loading methods
	# 
	# 
	# 
	#/////////////////////////////////////////////////////
	
	typemethod loadimage { imagename } {
		return [image create photo -file $imagename.gif]
	}
	
	typemethod SetImageCommand { command } {
		set loadimagecommand $command
	}
	
	typemethod reloadimages { init } {

		set normal [eval "$loadimagecommand check"]
		set hover [eval "$loadimagecommand checkhover"]
		set disabled [eval "$loadimagecommand checkdisabled"]

		set normalpress [eval "$loadimagecommand checkpress"]
		set hoverpress [eval "$loadimagecommand checkhoverpress"]
		set disabledpress [eval "$loadimagecommand checkdisabledpress"]

		if { !$init } {
			foreach widget $widgetlist {
				if { [$widget cget -selected] == "no" } {
					::hull1$widget itemconfigure button -image $normal -disabledimage $disabled
				} else {
					::hull1$widget itemconfigure button -image $normalpress -disabledimage $disabledpress
				}
			$widget configure -width [expr [$widget MeasureText [$widget cget -text] w] + [image width $normal]]
			set ih [image height $normal]
			set th [$self MeasureText $options(-text) h]
			if { $ih > $th } {
				$hull configure -height $ih
			} else {
				$hull configure -height $th
			}
			}
		}
	}
}