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

Last change on this file since 115 was 115, checked in by mmc, 19 years ago

Updated all copyright notices.

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