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

Last change on this file since 1444 was 1342, checked in by gah, 15 years ago

preliminary HQ output from molvisviewer; unexpand tabs; all jpeg generation at 100%

File size: 4.3 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
21    constructor {args} { # defined below }
22    public method value {args}
23    public method updateText {}
24    private method _toggle {args}
25    private variable _value 0  ;# value for this widget
26}
27                                                                               
28itk::usual Switch {
29    keep -cursor -background
30}
31
32# ----------------------------------------------------------------------
33# CONSTRUCTOR
34# ----------------------------------------------------------------------
35itcl::body Rappture::Switch::constructor {args} {
36
37    itk_component add button {
38        button $itk_interior.value \
39            -compound left \
40            -overrelief flat -relief flat -padx 3 -pady 0 -bd 0 \
41            -command [itcl::code $this _toggle]
42    } {
43        #rename -background -textbackground textBackground Background
44    }
45    pack $itk_component(button) -side left -expand yes -fill both
46    eval itk_initialize $args
47}
48
49# ----------------------------------------------------------------------
50# USAGE: value ?-check? ?<newval>?
51#
52# Clients use this to query/set the value for this widget.  With
53# no args, it returns the current value for the widget.  If the
54# <newval> is specified, it sets the value of the widget and
55# sends a <<Value>> event.  If the -check flag is included, the
56# new value is not actually applied, but just checked for correctness.
57# ----------------------------------------------------------------------
58itcl::body Rappture::Switch::value {args} {
59    set onlycheck 0
60    set i [lsearch -exact $args -check]
61    if {$i >= 0} {
62        set onlycheck 1
63        set args [lreplace $args $i $i]
64    }
65    if {[llength $args] == 1} {
66        set newval [lindex $args 0]
67        if {![string is boolean -strict $newval]} {
68            error "Should be a boolean value"
69        }
70        set newval [expr {($newval) ? 1 : 0}]
71        if {$onlycheck} {
72            return
73        }
74        set _value $newval
75        event generate $itk_component(hull) <<Value>>
76        updateText
77    } elseif {[llength $args] != 0} {
78        error "wrong # args: should be \"value ?-check? ?newval?\""
79    }
80    return [expr {($_value) ? "yes" : "no"}]
81}
82
83# ----------------------------------------------------------------------
84# _toggle
85#
86#       Use internally to convert the toggled button into the
87#       proper boolean format.  Yes, right now it's hardcoded to
88#       yes/no.  But in the future it could be some other text.
89#
90#       Can't use old "value" method because _value is already set
91#       be the widget and doesn't pass the value on the command line.
92#
93# ----------------------------------------------------------------------
94itcl::body Rappture::Switch::_toggle {} {
95    set _value [expr ($_value==0) ]
96    event generate $itk_component(hull) <<Value>>
97    updateText
98}
99
100itcl::body Rappture::Switch::updateText {} {
101    if { $_value } {
102        $itk_component(button) configure -text "yes" \
103            -image [Rappture::icon cbon]
104    } else {
105        $itk_component(button) configure -text "no" \
106            -image [Rappture::icon cboff]
107    }
108}
109   
110
111# ----------------------------------------------------------------------
112# CONFIGURATION OPTION: -oncolor
113# ----------------------------------------------------------------------
114itcl::configbody Rappture::Switch::oncolor {
115    #$itk_component(button) configure -selectcolor $itk_option(-oncolor)
116}
117
118# ----------------------------------------------------------------------
119# CONFIGURATION OPTION: -state
120# ----------------------------------------------------------------------
121itcl::configbody Rappture::Switch::state {
122    set valid {normal disabled}
123    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
124        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
125    }
126    $itk_component(button) configure -state $itk_option(-state)
127}
Note: See TracBrowser for help on using the repository browser.