source: branches/blt4/gui/scripts/booleanentry.tcl @ 1646

Last change on this file since 1646 was 1646, checked in by gah, 14 years ago
File size: 6.0 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    itk_component add -protected vframe {
51        frame $itk_interior.vframe
52    }
53
54    # if the control has an icon, plug it in
55    set icon [$_owner xml get $path.about.icon]
56    if {$icon != ""} {
57        itk_component add icon {
58            set icon [image create picture -data $icon]
59            set w [image width $icon]
60            set h [image height $icon]
61            canvas $itk_component(vframe).icon -height $h -width $w -borderwidth 0 -highlightthickness 0
62        } {
63            usual
64            ignore -highlightthickness -highlightbackground -highlightcolor
65        }
66        set c $itk_component(icon)
67        $c create image [expr {0.5*$w}] [expr {0.5*$h}] \
68            -anchor center -image $icon
69        pack $itk_component(icon) -fill x -side left
70    }
71   
72    #
73    # Create the widget and configure it properly based on other
74    # hints in the XML.
75    #
76    itk_component add switch {
77        set color [$_owner xml get $path.about.color]
78        if {$color != ""} {
79            Rappture::Switch $itk_component(vframe).switch -oncolor $color
80        } else {
81            Rappture::Switch $itk_component(vframe).switch
82        }
83    }
84
85    bind $itk_component(switch) <<Value>> [itcl::code $this _newValue]
86    pack $itk_component(switch) -fill x -side left
87    pack $itk_component(vframe) -side left -expand yes -fill both
88    eval itk_initialize $args
89
90    #
91    # Assign the default value to this widget, if there is one.
92    #
93    set str [$_owner xml get $path.default]
94    if {"" != $str} {
95        $itk_component(switch) value $str
96    } else {
97        $itk_component(switch) value off
98    }
99}
100
101# ----------------------------------------------------------------------
102# USAGE: value ?-check? ?<newval>?
103#
104# Clients use this to query/set the value for this widget.  With
105# no args, it returns the current value for the widget.  If the
106# <newval> is specified, it sets the value of the widget and
107# sends a <<Value>> event.  If the -check flag is included, the
108# new value is not actually applied, but just checked for correctness.
109# ----------------------------------------------------------------------
110itcl::body Rappture::BooleanEntry::value {args} {
111    set onlycheck 0
112    set i [lsearch -exact $args -check]
113    if {$i >= 0} {
114        set onlycheck 1
115        set args [lreplace $args $i $i]
116    }
117
118    if {[llength $args] == 1} {
119        if {$onlycheck} {
120            # someday we may add validation...
121            return
122        }
123        set newval [lindex $args 0]
124        $itk_component(switch) value $newval
125        event generate $itk_component(hull) <<Value>>
126        return $newval
127
128    } elseif {[llength $args] != 0} {
129        error "wrong # args: should be \"value ?-check? ?newval?\""
130    }
131
132    #
133    # Query the value and return.
134    #
135    return [$itk_component(switch) value]
136}
137
138# ----------------------------------------------------------------------
139# USAGE: label
140#
141# Clients use this to query the label associated with this widget.
142# Reaches into the XML and pulls out the appropriate label string.
143# ----------------------------------------------------------------------
144itcl::body Rappture::BooleanEntry::label {} {
145    set label [$_owner xml get $_path.about.label]
146    if {"" == $label} {
147        set label "Boolean"
148    }
149    return $label
150}
151
152# ----------------------------------------------------------------------
153# USAGE: tooltip
154#
155# Clients use this to query the tooltip associated with this widget.
156# Reaches into the XML and pulls out the appropriate description
157# string.  Returns the string that should be used with the
158# Rappture::Tooltip facility.
159# ----------------------------------------------------------------------
160itcl::body Rappture::BooleanEntry::tooltip {} {
161    set str [$_owner xml get $_path.about.description]
162    append str "\n\nClick to turn on/off"
163    return [string trim $str]
164}
165
166# ----------------------------------------------------------------------
167# USAGE: _newValue
168#
169# Invoked automatically whenever the value in the gauge changes.
170# Sends a <<Value>> event to notify clients of the change.
171# ----------------------------------------------------------------------
172itcl::body Rappture::BooleanEntry::_newValue {} {
173    event generate $itk_component(hull) <<Value>>
174}
175
176# ----------------------------------------------------------------------
177# CONFIGURATION OPTION: -state
178# ----------------------------------------------------------------------
179itcl::configbody Rappture::BooleanEntry::state {
180    set valid {normal disabled}
181    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
182        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
183    }
184    $itk_component(switch) configure -state $itk_option(-state)
185}
Note: See TracBrowser for help on using the repository browser.