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
RevLine 
[3330]1# -*- mode: tcl; indent-tabs-mode: nil -*-
[13]2# ----------------------------------------------------------------------
3#  COMPONENT: switch - on/off switch
4#
[1076]5#  This widget is used to control a (boolean) on/off value. 
[1135]6# It is just a wrapper around a button.
[13]7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
[3177]9#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
[115]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]13# ======================================================================
14package require Itk
15
16itcl::class Rappture::Switch {
17    inherit itk::Widget
18
[1120]19    itk_option define -oncolor onColor Color "#00cc00"
[437]20    itk_option define -state state State "normal"
[1979]21    itk_option define -showimage showimage Showimage "true"
22    itk_option define -showtext showtext Showtext "true"
[3186]23    itk_option define -interactcommand interactCommand InteractCommand ""
[13]24
25    constructor {args} { # defined below }
26    public method value {args}
[3186]27
28    private method _updateText {}
[1135]29    private method _toggle {args}
[1076]30    private variable _value 0  ;# value for this widget
[13]31}
[1979]32
[13]33itk::usual Switch {
[1076]34    keep -cursor -background
[13]35}
36
37# ----------------------------------------------------------------------
38# CONSTRUCTOR
39# ----------------------------------------------------------------------
[995]40itcl::body Rappture::Switch::constructor {args} {
[13]41
[1135]42    itk_component add button {
[1929]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]
[13]47    } {
[1929]48        #rename -background -textbackground textBackground Background
[13]49    }
[1135]50    pack $itk_component(button) -side left -expand yes -fill both
[1076]51    eval itk_initialize $args
[13]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} {
[1929]67        set onlycheck 1
68        set args [lreplace $args $i $i]
[13]69    }
70    if {[llength $args] == 1} {
[1929]71        set newval [lindex $args 0]
[3510]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."
[1929]76        }
77        if {$onlycheck} {
78            return
79        }
80        set _value $newval
81        event generate $itk_component(hull) <<Value>>
[3186]82        _updateText
[13]83    } elseif {[llength $args] != 0} {
[1929]84        error "wrong # args: should be \"value ?-check? ?newval?\""
[13]85    }
[1076]86    return [expr {($_value) ? "yes" : "no"}]
[13]87}
88
[1135]89# ----------------------------------------------------------------------
[1979]90# _toggle
[1135]91#
[1979]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.
[1135]95#
[1979]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.
[1135]98#
99# ----------------------------------------------------------------------
100itcl::body Rappture::Switch::_toggle {} {
[3186]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    }
[1135]109}
110
[3186]111itcl::body Rappture::Switch::_updateText {} {
[1979]112    set image ""
113    set text ""
[1135]114    if { $_value } {
[1979]115        if {$itk_option(-showimage)} {
[3186]116            set image [Rappture::icon cbon]
[1979]117        }
118        if {$itk_option(-showtext)} {
[2159]119            set text "yes"
[1979]120        }
[1135]121    } else {
[1979]122        if {$itk_option(-showimage)} {
[3186]123            set image [Rappture::icon cboff]
[1979]124        }
125        if {$itk_option(-showtext)} {
[2159]126            set text "no"
[1979]127        }
[1135]128    }
[1979]129    $itk_component(button) configure -text $text -image $image
[1120]130}
[13]131
[1979]132
[13]133# ----------------------------------------------------------------------
[1076]134# CONFIGURATION OPTION: -oncolor
[13]135# ----------------------------------------------------------------------
[1076]136itcl::configbody Rappture::Switch::oncolor {
[3186]137    # does nothing now, but may come back soon
[13]138}
139
140# ----------------------------------------------------------------------
[437]141# CONFIGURATION OPTION: -state
142# ----------------------------------------------------------------------
143itcl::configbody Rappture::Switch::state {
144    set valid {normal disabled}
145    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
[1929]146        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
[437]147    }
[1135]148    $itk_component(button) configure -state $itk_option(-state)
[437]149}
[1979]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    }
[3186]158    _updateText
[1979]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    }
[3186]168    _updateText
[1979]169}
Note: See TracBrowser for help on using the repository browser.