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

Last change on this file since 3330 was 3330, checked in by gah, 12 years ago

merge (by hand) with Rappture1.2 branch

File size: 6.9 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    set active [$_owner xml get $_path.active]
55    set inactive [$_owner xml get $_path.inactive]
56    #
57    # Create the widget and configure it properly based on other
58    # hints in the XML.
59    #
60    itk_component add element {
61        Rappture::PeriodicElement $itk_interior.element -editable yes
62    }
63    pack $itk_component(element) -expand yes -fill both
64    bind $itk_component(element) <<Value>> [itcl::code $this _newValue]
65
66    if { [llength $inactive] > 0 } {
67        $itk_component(element) element inactive $inactive
68    }
69    if { [llength $active] > 0 } {
70        $itk_component(element) element active $active
71    }           
72    if { $defval != "" } {
73        $itk_component(element) value $defval
74    }
75    eval itk_initialize $args
76}
77
78# ----------------------------------------------------------------------
79# DESTRUCTOR
80# ----------------------------------------------------------------------
81itcl::body Rappture::PeriodicElementEntry::destructor {} {
82    $_owner notify remove $this
83}
84
85# ----------------------------------------------------------------------
86# USAGE: value ?-check? ?<newval>?
87#
88# Clients use this to query/set the value for this widget.  With
89# no args, it returns the current value for the widget.  If the
90# <newval> is specified, it sets the value of the widget and
91# sends a <<Value>> event.  If the -check flag is included, the
92# new value is not actually applied, but just checked for correctness.
93# ----------------------------------------------------------------------
94itcl::body Rappture::PeriodicElementEntry::value {args} {
95    set onlycheck 0
96    set i [lsearch -exact $args -check]
97    if {$i >= 0} {
98        set onlycheck 1
99        set args [lreplace $args $i $i]
100    }
101
102    if {[llength $args] == 1} {
103        if {$onlycheck} {
104            # someday we may add validation...
105            return
106        }
107        set newval [lindex $args 0]
108        $itk_component(element) value $newval
109    } elseif {[llength $args] != 0} {
110        error "wrong # args: should be \"value ?-check? ?newval?\""
111    }
112
113    #
114    # Query the value and return.
115    #
116    set how [$_owner xml get $_path.returnvalue]
117    switch -- $how {
118        weight - number - name - symbol - all {
119            set how "-$how"
120        }
121        default {
122            set how "-name"
123        }
124    }
125    set str [$itk_component(element) element get $how]
126    return $str
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::PeriodicElementEntry::label {} {
136    set label [$_owner xml get $_path.about.label]
137    if {"" == $label} {
138        set label "Element"
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::PeriodicElementEntry::tooltip {} {
152    # query tooltip on-demand based on current element
153    return "@[itcl::code $this _tooltip]"
154}
155
156# ----------------------------------------------------------------------
157# USAGE: _newValue
158#
159# Invoked automatically whenever the value in the element changes.
160# Sends a <<Value>> event to notify clients of the change.
161# ----------------------------------------------------------------------
162itcl::body Rappture::PeriodicElementEntry::_newValue {} {
163    event generate $itk_component(hull) <<Value>>
164}
165
166# ----------------------------------------------------------------------
167# USAGE: _tooltip
168#
169# Returns the tooltip for this widget, given the current element in
170# the selector.  This is normally called by the Rappture::Tooltip
171# facility whenever it is about to pop up a tooltip for this widget.
172# ----------------------------------------------------------------------
173itcl::body Rappture::PeriodicElementEntry::_tooltip {} {
174    set tip [string trim [$_owner xml get $_path.about.description]]
175
176    # get the description for the current element, if there is one
177    set str [$itk_component(element) element get -all]
178    if {$_path != ""} {
179        set desc [$_owner xml get $_path.about.description]
180    }
181
182    if {[string length $str] > 0 && [string length $desc] > 0} {
183        append tip "\n\n$str:\n$desc"
184    }
185    return $tip
186}
187
188# ----------------------------------------------------------------------
189# CONFIGURATION OPTION: -state
190# ----------------------------------------------------------------------
191itcl::configbody Rappture::PeriodicElementEntry::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(element) configure -state $itk_option(-state)
197}
Note: See TracBrowser for help on using the repository browser.