1 | # ---------------------------------------------------------------------- |
---|
2 | # RAPPTURE OBJECT: choice |
---|
3 | # |
---|
4 | # A set of mutually-exclusive choices, as in Model A or Model B. |
---|
5 | # Usually used as an input to a simulation. |
---|
6 | # |
---|
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 | # ====================================================================== |
---|
14 | |
---|
15 | object choice -extends base { |
---|
16 | palettes "Inputs" |
---|
17 | |
---|
18 | help http://rappture.org/wiki/rp_xml_ele_choice |
---|
19 | |
---|
20 | attr options -title "Options" -type choices -path option -tooltip "List of choices that will appear on the drop-down menu in your program." |
---|
21 | |
---|
22 | attr default -title "Default Value" -type string -path default -only input -tooltip "Sets the value that this input has when the program first starts. This value is used by default unless the user explicitly changes it." |
---|
23 | |
---|
24 | check options { |
---|
25 | if {[llength $attr(options)] < 2} { |
---|
26 | return [list warning "Should have at least two options for this choice, or it's not really a choice at all."] |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | check default { |
---|
31 | if {[llength $attr(options)] > 0} { |
---|
32 | if {[string length [string trim $attr(default)]] == 0} { |
---|
33 | return [list error "Must have a default value for this choice."] |
---|
34 | } |
---|
35 | |
---|
36 | # make sure that the default is really a choice |
---|
37 | set defval [string trim $attr(default)] |
---|
38 | set found 0 |
---|
39 | foreach rec $attr(options) { |
---|
40 | set label [string trim [lindex $rec 0]] |
---|
41 | set value [string trim [lindex $rec 1]] |
---|
42 | if {$defval eq $label || $defval eq $value} { |
---|
43 | set found 1 |
---|
44 | break |
---|
45 | } |
---|
46 | } |
---|
47 | if {!$found} { |
---|
48 | return [list error "Default value must be a label or value for one of the current choices."] |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | storage { |
---|
54 | private variable _val ;# current choice |
---|
55 | } |
---|
56 | |
---|
57 | import xml {xmlobj path} { |
---|
58 | attr import $xmlobj $path |
---|
59 | import_string [$xmlobj get $path.current] |
---|
60 | } |
---|
61 | |
---|
62 | export xml {xmlobj path} { |
---|
63 | $xmlobj put $path $_val |
---|
64 | } |
---|
65 | |
---|
66 | import string {val} { |
---|
67 | set _val $val |
---|
68 | } |
---|
69 | |
---|
70 | export string {var} { |
---|
71 | upvar $var v |
---|
72 | set v $_val |
---|
73 | } |
---|
74 | |
---|
75 | compare { |
---|
76 | return [string compare $_val $_val2] |
---|
77 | } |
---|
78 | } |
---|