source: trunk/gui/scripts/switch.tcl @ 2417

Last change on this file since 2417 was 2159, checked in by mmc, 13 years ago

Some minor fixes to make the builder work properly. Also, moved up
"wm withdraw" command in main.tcl to avoid getting a flash on the screen.
Changed the boolean control to use yes/no instead of "yes"/"no".

File size: 5.6 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: switch - on/off switch
3#
4#  This widget is used to control a (boolean) on/off value. 
5# It is just a wrapper around a button.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005  Purdue Research Foundation
9#
10#  See the file "license.terms" for information on usage and
11#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12# ======================================================================
13package require Itk
14
15itcl::class Rappture::Switch {
16    inherit itk::Widget
17
18    itk_option define -oncolor onColor Color "#00cc00"
19    itk_option define -state state State "normal"
20    itk_option define -showimage showimage Showimage "true"
21    itk_option define -showtext showtext Showtext "true"
22
23    constructor {args} { # defined below }
24    public method value {args}
25    public method updateText {}
26    private method _toggle {args}
27    private variable _value 0  ;# value for this widget
28}
29
30itk::usual Switch {
31    keep -cursor -background
32}
33
34# ----------------------------------------------------------------------
35# CONSTRUCTOR
36# ----------------------------------------------------------------------
37itcl::body Rappture::Switch::constructor {args} {
38
39    itk_component add button {
40        button $itk_interior.value \
41            -compound left \
42            -overrelief flat -relief flat -padx 3 -pady 0 -bd 0 \
43            -command [itcl::code $this _toggle]
44    } {
45        #rename -background -textbackground textBackground Background
46    }
47    pack $itk_component(button) -side left -expand yes -fill both
48    eval itk_initialize $args
49}
50
51# ----------------------------------------------------------------------
52# USAGE: value ?-check? ?<newval>?
53#
54# Clients use this to query/set the value for this widget.  With
55# no args, it returns the current value for the widget.  If the
56# <newval> is specified, it sets the value of the widget and
57# sends a <<Value>> event.  If the -check flag is included, the
58# new value is not actually applied, but just checked for correctness.
59# ----------------------------------------------------------------------
60itcl::body Rappture::Switch::value {args} {
61    set onlycheck 0
62    set i [lsearch -exact $args -check]
63    if {$i >= 0} {
64        set onlycheck 1
65        set args [lreplace $args $i $i]
66    }
67    if {[llength $args] == 1} {
68        set newval [lindex $args 0]
69        if {![string is boolean -strict $newval]} {
70            error "Should be a boolean value"
71        }
72        set newval [expr {($newval) ? 1 : 0}]
73        if {$onlycheck} {
74            return
75        }
76        set _value $newval
77        event generate $itk_component(hull) <<Value>>
78        updateText
79    } elseif {[llength $args] != 0} {
80        error "wrong # args: should be \"value ?-check? ?newval?\""
81    }
82    return [expr {($_value) ? "yes" : "no"}]
83}
84
85# ----------------------------------------------------------------------
86# _toggle
87#
88#        Use internally to convert the toggled button into the
89#        proper boolean format.  Yes, right now it's hardcoded to
90#        yes/no.  But in the future it could be some other text.
91#
92#        Can't use old "value" method because _value is already set
93#        be the widget and doesn't pass the value on the command line.
94#
95# ----------------------------------------------------------------------
96itcl::body Rappture::Switch::_toggle {} {
97    set _value [expr ($_value==0) ]
98    event generate $itk_component(hull) <<Value>>
99    updateText
100}
101
102itcl::body Rappture::Switch::updateText {} {
103    set image ""
104    set text ""
105    if { $_value } {
106        if {$itk_option(-showimage)} {
107            set image "[Rappture::icon cbon]"
108        }
109        if {$itk_option(-showtext)} {
110            set text "yes"
111        }
112    } else {
113        if {$itk_option(-showimage)} {
114            set image "[Rappture::icon cboff]"
115        }
116        if {$itk_option(-showtext)} {
117            set text "no"
118        }
119    }
120    $itk_component(button) configure -text $text -image $image
121}
122
123
124# ----------------------------------------------------------------------
125# CONFIGURATION OPTION: -oncolor
126# ----------------------------------------------------------------------
127itcl::configbody Rappture::Switch::oncolor {
128    #$itk_component(button) configure -selectcolor $itk_option(-oncolor)
129}
130
131# ----------------------------------------------------------------------
132# CONFIGURATION OPTION: -state
133# ----------------------------------------------------------------------
134itcl::configbody Rappture::Switch::state {
135    set valid {normal disabled}
136    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
137        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
138    }
139    $itk_component(button) configure -state $itk_option(-state)
140}
141
142# ----------------------------------------------------------------------
143# CONFIGURATION OPTION: -showimage
144# ----------------------------------------------------------------------
145itcl::configbody Rappture::Switch::showimage {
146    if {[string is boolean $itk_option(-showimage)] != 1} {
147        error "bad value \"$itk_option(-showimage)\": should be a boolean"
148    }
149    updateText
150}
151
152# ----------------------------------------------------------------------
153# CONFIGURATION OPTION: -showtext
154# ----------------------------------------------------------------------
155itcl::configbody Rappture::Switch::showtext {
156    if {[string is boolean $itk_option(-showtext)] != 1} {
157        error "bad value \"$itk_option(-showtext)\": should be a boolean"
158    }
159    updateText
160}
Note: See TracBrowser for help on using the repository browser.