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