source: trunk/gui/scripts/booleanentry.tcl @ 738

Last change on this file since 738 was 437, checked in by mmc, 18 years ago

Added a new <enable> parameter to all inputs. Controls can now be
enabled/disabled based on the status of other controls. If a group
is disabled, it disappears entirely. If a parameter is enabled to
a hard-coded "off" value, then it acts like a hidden (secret)
parameter.

File size: 5.5 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: BooleanEntry - widget for entering boolean values
3#
4#  This widget represents a <boolean> entry on a control panel.
5#  It is used to enter yes/no or on/off values.
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::BooleanEntry {
16    inherit itk::Widget
17
18    itk_option define -state state State "normal"
19
20    constructor {owner path args} { # defined below }
21
22    public method value {args}
23
24    public method label {}
25    public method tooltip {}
26
27    protected method _newValue {}
28
29    private variable _owner ""    ;# thing managing this control
30    private variable _path ""     ;# path in XML to this number
31}
32
33itk::usual BooleanEntry {
34    keep -cursor -font
35    keep -foreground -background
36    keep -textbackground
37    keep -selectbackground -selectforeground -selectborderwidth
38}
39
40# ----------------------------------------------------------------------
41# CONSTRUCTOR
42# ----------------------------------------------------------------------
43itcl::body Rappture::BooleanEntry::constructor {owner path args} {
44    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
45        error "bad object \"$owner\": should be Rappture::ControlOwner"
46    }
47    set _owner $owner
48    set _path $path
49
50    #
51    # Create the widget and configure it properly based on other
52    # hints in the XML.
53    #
54    itk_component add switch {
55        Rappture::Switch $itk_interior.switch
56    }
57    pack $itk_component(switch) -expand yes -fill both
58    bind $itk_component(switch) <<Value>> [itcl::code $this _newValue]
59
60    set color [$_owner xml get $path.about.color]
61    if {$color != ""} {
62        $itk_component(switch) configure -oncolor $color
63    }
64
65    # if the control has an icon, plug it in
66    set str [$_owner xml get $path.about.icon]
67    if {$str != ""} {
68        $itk_component(switch) configure -onimage \
69            [image create photo -data $str]
70    }
71
72    eval itk_initialize $args
73
74    #
75    # Assign the default value to this widget, if there is one.
76    #
77    set str [$_owner xml get $path.default]
78    if {"" != $str} {
79        $itk_component(switch) value $str
80    } else {
81        $itk_component(switch) value off
82    }
83}
84
85# ----------------------------------------------------------------------
86# USAGE: value ?-check? ?<newval>?
87#
88# Clients use this to query/set the value for this widget.  With
89# no args, it returns the current value for the widget.  If the
90# <newval> is specified, it sets the value of the widget and
91# sends a <<Value>> event.  If the -check flag is included, the
92# new value is not actually applied, but just checked for correctness.
93# ----------------------------------------------------------------------
94itcl::body Rappture::BooleanEntry::value {args} {
95    set onlycheck 0
96    set i [lsearch -exact $args -check]
97    if {$i >= 0} {
98        set onlycheck 1
99        set args [lreplace $args $i $i]
100    }
101
102    if {[llength $args] == 1} {
103        if {$onlycheck} {
104            # someday we may add validation...
105            return
106        }
107        set newval [lindex $args 0]
108        $itk_component(switch) value $newval
109        return $newval
110
111    } elseif {[llength $args] != 0} {
112        error "wrong # args: should be \"value ?-check? ?newval?\""
113    }
114
115    #
116    # Query the value and return.
117    #
118    return [$itk_component(switch) value]
119}
120
121# ----------------------------------------------------------------------
122# USAGE: label
123#
124# Clients use this to query the label associated with this widget.
125# Reaches into the XML and pulls out the appropriate label string.
126# ----------------------------------------------------------------------
127itcl::body Rappture::BooleanEntry::label {} {
128    set label [$_owner xml get $_path.about.label]
129    if {"" == $label} {
130        set label "Boolean"
131    }
132    return $label
133}
134
135# ----------------------------------------------------------------------
136# USAGE: tooltip
137#
138# Clients use this to query the tooltip associated with this widget.
139# Reaches into the XML and pulls out the appropriate description
140# string.  Returns the string that should be used with the
141# Rappture::Tooltip facility.
142# ----------------------------------------------------------------------
143itcl::body Rappture::BooleanEntry::tooltip {} {
144    set str [$_owner xml get $_path.about.description]
145    append str "\n\nClick to turn on/off"
146    return [string trim $str]
147}
148
149# ----------------------------------------------------------------------
150# USAGE: _newValue
151#
152# Invoked automatically whenever the value in the gauge changes.
153# Sends a <<Value>> event to notify clients of the change.
154# ----------------------------------------------------------------------
155itcl::body Rappture::BooleanEntry::_newValue {} {
156    event generate $itk_component(hull) <<Value>>
157}
158
159# ----------------------------------------------------------------------
160# CONFIGURATION OPTION: -state
161# ----------------------------------------------------------------------
162itcl::configbody Rappture::BooleanEntry::state {
163    set valid {normal disabled}
164    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
165        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
166    }
167    $itk_component(switch) configure -state $itk_option(-state)
168}
Note: See TracBrowser for help on using the repository browser.