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

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