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

Last change on this file since 5157 was 3647, checked in by gah, 11 years ago

add string trim to inputs

File size: 6.6 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: IntegerEntry - widget for entering integer values
4#
5#  This widget represents an <integer> entry on a control panel.
6#  It is used to enter integral values with no units.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15
16itcl::class Rappture::IntegerEntry {
17    inherit itk::Widget
18
19    itk_option define -state state State "normal"
20
21    constructor {owner path args} { # defined below }
22
23    public method value {args}
24
25    public method label {}
26    public method tooltip {}
27
28    protected method _newValue {}
29
30    private variable _owner ""    ;# thing managing this control
31    private variable _path ""     ;# path in XML to this number
32}
33
34itk::usual IntegerEntry {
35    keep -cursor -font
36    keep -foreground -background
37    keep -textbackground
38    keep -selectbackground -selectforeground -selectborderwidth
39}
40
41# ----------------------------------------------------------------------
42# CONSTRUCTOR
43# ----------------------------------------------------------------------
44itcl::body Rappture::IntegerEntry::constructor {owner path args} {
45    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
46        error "bad object \"$owner\": should be Rappture::ControlOwner"
47    }
48    set _owner $owner
49    set _path $path
50
51    #
52    # Create the widget and configure it properly based on other
53    # hints in the XML.
54    #
55    itk_component add spinner {
56        Rappture::Gauge $itk_interior.spinner -type integer -log $path
57    }
58    pack $itk_component(spinner) -expand yes -fill both
59    bind $itk_component(spinner) <<Value>> [itcl::code $this _newValue]
60
61    # if there are min/max values, plug them in.
62    set min [string trim [$_owner xml get $path.min]]
63    if {"" != $min} {
64        $itk_component(spinner) configure -minvalue $min
65    }
66
67    set max [string trim [$_owner xml get $path.max]]
68    if {"" != $max} {
69        $itk_component(spinner) configure -maxvalue $max
70    }
71
72    # if there is a color, use it for the min/max spectrum
73    set color [string trim [$_owner xml get $path.about.color]]
74    if {$color != "" && $min != "" && $max != ""} {
75        # For compatibility. If only one color use white for min
76        if {[llength $color] == 1} {
77            set color [list $min white $max $color]
78        }
79        $itk_component(spinner) configure \
80            -spectrum [Rappture::Spectrum ::\#auto $color]
81    }
82
83    # if the control has an icon, plug it in
84    set str [string trim [$_owner xml get $path.about.icon]]
85    if {$str != ""} {
86        $itk_component(spinner) configure -image \
87            [image create photo -data $str]
88    }
89
90    eval itk_initialize $args
91
92    #
93    # Assign the default value to this widget, if there is one.
94    #
95    set str [string trim [$_owner xml get $path.default]]
96    if {"" != $str != ""} {
97        $itk_component(spinner) value $str
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::IntegerEntry::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(spinner) value $newval
125        return $newval
126
127    } elseif {[llength $args] != 0} {
128        error "wrong # args: should be \"value ?-check? ?newval?\""
129    }
130
131    #
132    # Query the value and return.
133    #
134    return [$itk_component(spinner) value]
135}
136
137# ----------------------------------------------------------------------
138# USAGE: label
139#
140# Clients use this to query the label associated with this widget.
141# Reaches into the XML and pulls out the appropriate label string.
142# ----------------------------------------------------------------------
143itcl::body Rappture::IntegerEntry::label {} {
144    set label [string trim [$_owner xml get $_path.about.label]]
145    if {"" == $label} {
146        set label "Integer"
147    }
148    return $label
149}
150
151# ----------------------------------------------------------------------
152# USAGE: tooltip
153#
154# Clients use this to query the tooltip associated with this widget.
155# Reaches into the XML and pulls out the appropriate description
156# string.  Returns the string that should be used with the
157# Rappture::Tooltip facility.
158# ----------------------------------------------------------------------
159itcl::body Rappture::IntegerEntry::tooltip {} {
160    set str [string trim [$_owner xml get $_path.about.description]]
161    set min [string trim [$_owner xml get $_path.min]]
162    set max [string trim [$_owner xml get $_path.max]]
163
164    if {$min != "" || $max != ""} {
165        append str "\n\nEnter an integer"
166
167        if {$min != "" && $max != ""} {
168            append str " between $min and $max"
169        } elseif {$min != ""} {
170            append str " greater than $min"
171        } elseif {$max != ""} {
172            append str " less than $max"
173        }
174    }
175    return [string trim $str]
176}
177
178# ----------------------------------------------------------------------
179# USAGE: _newValue
180#
181# Invoked automatically whenever the value in the gauge changes.
182# Sends a <<Value>> event to notify clients of the change.
183# ----------------------------------------------------------------------
184itcl::body Rappture::IntegerEntry::_newValue {} {
185    event generate $itk_component(hull) <<Value>>
186}
187
188# ----------------------------------------------------------------------
189# CONFIGURATION OPTION: -state
190# ----------------------------------------------------------------------
191itcl::configbody Rappture::IntegerEntry::state {
192    set valid {normal disabled}
193    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
194        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
195    }
196    $itk_component(spinner) configure -state $itk_option(-state)
197}
Note: See TracBrowser for help on using the repository browser.