source: trunk/gui/scripts/integerentry.tcl @ 1277

Last change on this file since 1277 was 1075, checked in by mmh, 16 years ago

Rewritten color range code for integers and numbers.
examples/canvas rewritten.
examples/zoo/[integer|number]2 added

File size: 6.4 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: IntegerEntry - widget for entering integer values
3#
4#  This widget represents an <integer> entry on a control panel.
5#  It is used to enter integral values with no units.
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::IntegerEntry {
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 IntegerEntry {
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::IntegerEntry::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 spinner {
55        Rappture::Gauge $itk_interior.spinner -type integer
56    }
57    pack $itk_component(spinner) -expand yes -fill both
58    bind $itk_component(spinner) <<Value>> [itcl::code $this _newValue]
59
60    # if there are min/max values, plug them in.
61    set min [$_owner xml get $path.min]
62    if {"" != $min} {
63        $itk_component(spinner) configure -minvalue $min
64    }
65
66    set max [$_owner xml get $path.max]
67    if {"" != $max} {
68        $itk_component(spinner) configure -maxvalue $max
69    }
70
71    # if there is a color, use it for the min/max spectrum
72    set color [$_owner xml get $path.about.color]
73    if {$color != "" && $min != "" && $max != ""} {
74        # For compatibility. If only one color use white for min
75        if {[llength $color] == 1} {
76            set color [list $min white $max $color]
77        }
78        $itk_component(spinner) configure \
79            -spectrum [Rappture::Spectrum ::#auto $color]
80    }
81
82    # if the control has an icon, plug it in
83    set str [$_owner xml get $path.about.icon]
84    if {$str != ""} {
85        $itk_component(spinner) configure -image \
86            [image create photo -data $str]
87    }
88
89    eval itk_initialize $args
90
91    #
92    # Assign the default value to this widget, if there is one.
93    #
94    set str [$_owner xml get $path.default]
95    if {"" != $str != ""} { $itk_component(spinner) value $str }
96}
97
98# ----------------------------------------------------------------------
99# USAGE: value ?-check? ?<newval>?
100#
101# Clients use this to query/set the value for this widget.  With
102# no args, it returns the current value for the widget.  If the
103# <newval> is specified, it sets the value of the widget and
104# sends a <<Value>> event.  If the -check flag is included, the
105# new value is not actually applied, but just checked for correctness.
106# ----------------------------------------------------------------------
107itcl::body Rappture::IntegerEntry::value {args} {
108    set onlycheck 0
109    set i [lsearch -exact $args -check]
110    if {$i >= 0} {
111        set onlycheck 1
112        set args [lreplace $args $i $i]
113    }
114
115    if {[llength $args] == 1} {
116        if {$onlycheck} {
117            # someday we may add validation...
118            return
119        }
120        set newval [lindex $args 0]
121        $itk_component(spinner) value $newval
122        return $newval
123
124    } elseif {[llength $args] != 0} {
125        error "wrong # args: should be \"value ?-check? ?newval?\""
126    }
127
128    #
129    # Query the value and return.
130    #
131    return [$itk_component(spinner) value]
132}
133
134# ----------------------------------------------------------------------
135# USAGE: label
136#
137# Clients use this to query the label associated with this widget.
138# Reaches into the XML and pulls out the appropriate label string.
139# ----------------------------------------------------------------------
140itcl::body Rappture::IntegerEntry::label {} {
141    set label [$_owner xml get $_path.about.label]
142    if {"" == $label} {
143        set label "Integer"
144    }
145    return $label
146}
147
148# ----------------------------------------------------------------------
149# USAGE: tooltip
150#
151# Clients use this to query the tooltip associated with this widget.
152# Reaches into the XML and pulls out the appropriate description
153# string.  Returns the string that should be used with the
154# Rappture::Tooltip facility.
155# ----------------------------------------------------------------------
156itcl::body Rappture::IntegerEntry::tooltip {} {
157    set str [$_owner xml get $_path.about.description]
158
159    set min [$_owner xml get $_path.min]
160    set max [$_owner xml get $_path.max]
161
162    if {$min != "" || $max != ""} {
163        append str "\n\nEnter an integer"
164
165        if {$min != "" && $max != ""} {
166            append str " between $min and $max"
167        } elseif {$min != ""} {
168            append str " greater than $min"
169        } elseif {$max != ""} {
170            append str " less than $max"
171        }
172    }
173    return [string trim $str]
174}
175
176# ----------------------------------------------------------------------
177# USAGE: _newValue
178#
179# Invoked automatically whenever the value in the gauge changes.
180# Sends a <<Value>> event to notify clients of the change.
181# ----------------------------------------------------------------------
182itcl::body Rappture::IntegerEntry::_newValue {} {
183    event generate $itk_component(hull) <<Value>>
184}
185
186# ----------------------------------------------------------------------
187# CONFIGURATION OPTION: -state
188# ----------------------------------------------------------------------
189itcl::configbody Rappture::IntegerEntry::state {
190    set valid {normal disabled}
191    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
192        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
193    }
194    $itk_component(spinner) configure -state $itk_option(-state)
195}
Note: See TracBrowser for help on using the repository browser.