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

Last change on this file since 3510 was 3510, checked in by gah, 11 years ago

start of string trim fixes for gauge and switch (boolean)

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