1 | |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | |
---|
4 | |
---|
5 | # COMPONENT: PushButton - widget for entering a choice of strings |
---|
6 | # |
---|
7 | # This widget represents a <choice> entry on a control panel. |
---|
8 | # It is used to choose one of several mutually-exclusive strings. |
---|
9 | # ====================================================================== |
---|
10 | # AUTHOR: Michael McLennan, Purdue University |
---|
11 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
12 | # |
---|
13 | # See the file "license.terms" for information on usage and |
---|
14 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
15 | # ====================================================================== |
---|
16 | package require Itk |
---|
17 | |
---|
18 | option add *PushButton.width 16 widgetDefault |
---|
19 | option add *PushButton.height 16 widgetDefault |
---|
20 | |
---|
21 | itcl::class Rappture::PushButton { |
---|
22 | inherit itk::Widget |
---|
23 | |
---|
24 | itk_option define -variable variable Variable "normal" |
---|
25 | itk_option define -command command Command "normal" |
---|
26 | itk_option define -width width Width "normal" |
---|
27 | itk_option define -height height Height "normal" |
---|
28 | itk_option define -onvalue onValue OnValue "normal" |
---|
29 | itk_option define -offvalue offValue OffValue "normal" |
---|
30 | |
---|
31 | constructor {args} { |
---|
32 | # defined below |
---|
33 | } |
---|
34 | destructor { |
---|
35 | # defined below |
---|
36 | } |
---|
37 | |
---|
38 | public method invoke {} |
---|
39 | public method deselect {} |
---|
40 | public method select {} |
---|
41 | public method toggle {} |
---|
42 | public method disable {} |
---|
43 | public method enable {} |
---|
44 | |
---|
45 | protected method _fixValue {args} |
---|
46 | |
---|
47 | private variable _state 0 |
---|
48 | private variable _enabled 1 |
---|
49 | public variable command ""; # Command to be invoked |
---|
50 | private variable _variable ""; # Variable to be set. |
---|
51 | public variable onimage ""; # Image displayed when selected |
---|
52 | public variable offimage ""; # Image displayed when deselected. |
---|
53 | public variable disabledimage ""; # Image displayed when deselected. |
---|
54 | public variable onvalue "1"; # Value set when selected. |
---|
55 | public variable offvalue "0"; # Value set when deselected. |
---|
56 | } |
---|
57 | |
---|
58 | itk::usual PushButton { |
---|
59 | keep -cursor -font |
---|
60 | keep -foreground -background |
---|
61 | } |
---|
62 | |
---|
63 | # ---------------------------------------------------------------------- |
---|
64 | # CONSTRUCTOR |
---|
65 | # ---------------------------------------------------------------------- |
---|
66 | itcl::body Rappture::PushButton::constructor {args} { |
---|
67 | itk_component add button { |
---|
68 | label $itk_interior.button -borderwidth 1 -relief sunken |
---|
69 | } { |
---|
70 | usual |
---|
71 | ignore -padx -pady -relief -borderwidth -background |
---|
72 | } |
---|
73 | bind $itk_component(button) <ButtonPress> \ |
---|
74 | [itcl::code $this invoke] |
---|
75 | pack $itk_component(button) -expand yes -fill both -ipadx 1 -ipady 1 |
---|
76 | eval itk_initialize $args |
---|
77 | deselect |
---|
78 | } |
---|
79 | |
---|
80 | itcl::body Rappture::PushButton::invoke {} { |
---|
81 | if { !$_enabled } { |
---|
82 | return |
---|
83 | } |
---|
84 | toggle |
---|
85 | if { $command != "" } { |
---|
86 | uplevel \#0 $command |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | itcl::body Rappture::PushButton::toggle {} { |
---|
91 | if { !$_enabled } { |
---|
92 | return |
---|
93 | } |
---|
94 | set _state [expr !$_state] |
---|
95 | if { $_state } { |
---|
96 | select |
---|
97 | } else { |
---|
98 | deselect |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | itcl::body Rappture::PushButton::disable {} { |
---|
103 | if { $_enabled } { |
---|
104 | set _enabled [expr !$_enabled] |
---|
105 | $itk_component(button) configure -relief raise \ |
---|
106 | -image $disabledimage -bg grey85 |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | itcl::body Rappture::PushButton::enable {} { |
---|
111 | if { !$_enabled } { |
---|
112 | set _enabled [expr !$_enabled] |
---|
113 | _fixValue |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | # ---------------------------------------------------------------------- |
---|
118 | # USAGE: _fixValue ?<name1> <name2> <op>? |
---|
119 | # |
---|
120 | # Invoked automatically whenever the -variable associated with this |
---|
121 | # widget is modified. Copies the value to the current settings for |
---|
122 | # the widget. |
---|
123 | # ---------------------------------------------------------------------- |
---|
124 | itcl::body Rappture::PushButton::_fixValue {args} { |
---|
125 | if {"" == $itk_option(-variable)} { |
---|
126 | return |
---|
127 | } |
---|
128 | upvar #0 $itk_option(-variable) var |
---|
129 | if { $var != "" && [string is boolean $var] } { |
---|
130 | set var [expr "$var == 1"] |
---|
131 | } |
---|
132 | if { $var == $onvalue } { |
---|
133 | set _state 1 |
---|
134 | $itk_component(button) configure -relief sunken \ |
---|
135 | -image $onimage -bg white |
---|
136 | } else { |
---|
137 | set _state 0 |
---|
138 | $itk_component(button) configure -relief raise \ |
---|
139 | -image $offimage -bg grey85 |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | itcl::body Rappture::PushButton::select {} { |
---|
144 | if { !$_enabled } { |
---|
145 | return |
---|
146 | } |
---|
147 | upvar #0 $_variable state |
---|
148 | set state $onvalue |
---|
149 | } |
---|
150 | |
---|
151 | itcl::body Rappture::PushButton::deselect {} { |
---|
152 | if { !$_enabled } { |
---|
153 | return |
---|
154 | } |
---|
155 | upvar #0 $_variable state |
---|
156 | set state $offvalue |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | # ---------------------------------------------------------------------- |
---|
161 | # CONFIGURE: -variable |
---|
162 | # ---------------------------------------------------------------------- |
---|
163 | itcl::configbody Rappture::PushButton::variable { |
---|
164 | if {"" != $_variable} { |
---|
165 | upvar #0 $_variable var |
---|
166 | trace remove variable var write [itcl::code $this _fixValue] |
---|
167 | } |
---|
168 | set _variable $itk_option(-variable) |
---|
169 | |
---|
170 | if {"" != $_variable} { |
---|
171 | upvar #0 $_variable var |
---|
172 | trace add variable var write [itcl::code $this _fixValue] |
---|
173 | |
---|
174 | # sync to the current value of this variable |
---|
175 | if {[info exists var]} { |
---|
176 | _fixValue |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | # ---------------------------------------------------------------------- |
---|
182 | # CONFIGURE: -width |
---|
183 | # ---------------------------------------------------------------------- |
---|
184 | itcl::configbody Rappture::PushButton::width { |
---|
185 | $itk_component(button) configure -width $itk_option(-width) |
---|
186 | } |
---|
187 | |
---|
188 | # ---------------------------------------------------------------------- |
---|
189 | # CONFIGURE: -height |
---|
190 | # ---------------------------------------------------------------------- |
---|
191 | itcl::configbody Rappture::PushButton::height { |
---|
192 | set _height $itk_option(-height) |
---|
193 | $itk_component(button) configure -height $itk_option(-height) |
---|
194 | } |
---|
195 | |
---|