source: trunk/gui/scripts/periodicelemententry.tcl @ 4735

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

add string trim to inputs

File size: 7.0 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: PeriodicElementEntry - widget for entering a choice of strings
4#
5#  This widget represents a <element> entry on a control panel.
6#  It is used to choose one of several mutually-exclusive strings.
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::PeriodicElementEntry {
17    inherit itk::Widget
18
19    itk_option define -state state State "normal"
20
21    constructor {owner path args} { # defined below }
22    destructor { # defined below }
23
24    public method value {args}
25
26    public method label {}
27    public method tooltip {}
28
29    protected method _newValue {}
30    protected method _tooltip {}
31
32    private variable _owner ""    ;# thing managing this control
33    private variable _path ""     ;# path in XML to this number
34}
35
36itk::usual PeriodicElementEntry {
37    keep -cursor -font
38    keep -foreground -background
39    keep -textforeground -textbackground
40    keep -selectbackground -selectforeground -selectborderwidth
41}
42
43# ----------------------------------------------------------------------
44# CONSTRUCTOR
45# ----------------------------------------------------------------------
46itcl::body Rappture::PeriodicElementEntry::constructor {owner path args} {
47    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
48        error "bad object \"$owner\": should be Rappture::ControlOwner"
49    }
50    set _owner $owner
51    set _path $path
52
53    set defval [string trim [$_owner xml get $_path.default]]
54    # Active and inactive are lists.  Don't need to trim.
55    set active [string trim [$_owner xml get $_path.active]]
56    set inactive [string trim [$_owner xml get $_path.inactive]]
57    #
58    # Create the widget and configure it properly based on other
59    # hints in the XML.
60    #
61    itk_component add element {
62        Rappture::PeriodicElement $itk_interior.element -editable yes
63    }
64    pack $itk_component(element) -expand yes -fill both
65    bind $itk_component(element) <<Value>> [itcl::code $this _newValue]
66
67    if { [llength $inactive] > 0 } {
68        $itk_component(element) element inactive $inactive
69    }
70    if { [llength $active] > 0 } {
71        $itk_component(element) element active $active
72    }           
73    if { $defval != "" } {
74        $itk_component(element) value $defval
75    }
76    eval itk_initialize $args
77}
78
79# ----------------------------------------------------------------------
80# DESTRUCTOR
81# ----------------------------------------------------------------------
82itcl::body Rappture::PeriodicElementEntry::destructor {} {
83    $_owner notify remove $this
84}
85
86# ----------------------------------------------------------------------
87# USAGE: value ?-check? ?<newval>?
88#
89# Clients use this to query/set the value for this widget.  With
90# no args, it returns the current value for the widget.  If the
91# <newval> is specified, it sets the value of the widget and
92# sends a <<Value>> event.  If the -check flag is included, the
93# new value is not actually applied, but just checked for correctness.
94# ----------------------------------------------------------------------
95itcl::body Rappture::PeriodicElementEntry::value {args} {
96    set onlycheck 0
97    set i [lsearch -exact $args -check]
98    if {$i >= 0} {
99        set onlycheck 1
100        set args [lreplace $args $i $i]
101    }
102
103    if {[llength $args] == 1} {
104        if {$onlycheck} {
105            # someday we may add validation...
106            return
107        }
108        set newval [lindex $args 0]
109        $itk_component(element) value $newval
110    } elseif {[llength $args] != 0} {
111        error "wrong # args: should be \"value ?-check? ?newval?\""
112    }
113
114    #
115    # Query the value and return.
116    #
117    set how [string trim [$_owner xml get $_path.returnvalue]]
118    switch -- $how {
119        weight - number - name - symbol - all {
120            set how "-$how"
121        }
122        default {
123            set how "-name"
124        }
125    }
126    set str [$itk_component(element) element get $how]
127    return $str
128}
129
130# ----------------------------------------------------------------------
131# USAGE: label
132#
133# Clients use this to query the label associated with this widget.
134# Reaches into the XML and pulls out the appropriate label string.
135# ----------------------------------------------------------------------
136itcl::body Rappture::PeriodicElementEntry::label {} {
137    set label [string trim [$_owner xml get $_path.about.label]]
138    if {"" == $label} {
139        set label "Element"
140    }
141    return $label
142}
143
144# ----------------------------------------------------------------------
145# USAGE: tooltip
146#
147# Clients use this to query the tooltip associated with this widget.
148# Reaches into the XML and pulls out the appropriate description
149# string.  Returns the string that should be used with the
150# Rappture::Tooltip facility.
151# ----------------------------------------------------------------------
152itcl::body Rappture::PeriodicElementEntry::tooltip {} {
153    # query tooltip on-demand based on current element
154    return "@[itcl::code $this _tooltip]"
155}
156
157# ----------------------------------------------------------------------
158# USAGE: _newValue
159#
160# Invoked automatically whenever the value in the element changes.
161# Sends a <<Value>> event to notify clients of the change.
162# ----------------------------------------------------------------------
163itcl::body Rappture::PeriodicElementEntry::_newValue {} {
164    event generate $itk_component(hull) <<Value>>
165}
166
167# ----------------------------------------------------------------------
168# USAGE: _tooltip
169#
170# Returns the tooltip for this widget, given the current element in
171# the selector.  This is normally called by the Rappture::Tooltip
172# facility whenever it is about to pop up a tooltip for this widget.
173# ----------------------------------------------------------------------
174itcl::body Rappture::PeriodicElementEntry::_tooltip {} {
175    set tip [string trim [$_owner xml get $_path.about.description]]
176
177    # get the description for the current element, if there is one
178    set str [$itk_component(element) element get -all]
179    if {$_path != ""} {
180        set desc [string trim [$_owner xml get $_path.about.description]]
181    }
182
183    if {[string length $str] > 0 && [string length $desc] > 0} {
184        append tip "\n\n$str:\n$desc"
185    }
186    return $tip
187}
188
189# ----------------------------------------------------------------------
190# CONFIGURATION OPTION: -state
191# ----------------------------------------------------------------------
192itcl::configbody Rappture::PeriodicElementEntry::state {
193    set valid {normal disabled}
194    if {[lsearch -exact $valid $itk_option(-state)] < 0} {
195        error "bad value \"$itk_option(-state)\": should be [join $valid {, }]"
196    }
197    $itk_component(element) configure -state $itk_option(-state)
198}
Note: See TracBrowser for help on using the repository browser.