1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * Rappture 2.0 Choice Object Source |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Derrick Kearney, Purdue University |
---|
7 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
8 | * |
---|
9 | * See the file "license.terms" for information on usage and |
---|
10 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
11 | * ====================================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | #include "RpChoice.h" |
---|
15 | |
---|
16 | using namespace Rappture; |
---|
17 | |
---|
18 | Choice::Choice ( |
---|
19 | const char *path, |
---|
20 | const char *val |
---|
21 | ) |
---|
22 | : Object (), |
---|
23 | _options (NULL) |
---|
24 | { |
---|
25 | this->path(path); |
---|
26 | this->label(""); |
---|
27 | this->desc(""); |
---|
28 | this->def(val); |
---|
29 | this->cur(val); |
---|
30 | } |
---|
31 | |
---|
32 | Choice::Choice ( |
---|
33 | const char *path, |
---|
34 | const char *val, |
---|
35 | const char *label, |
---|
36 | const char *desc |
---|
37 | ) |
---|
38 | : Object (), |
---|
39 | _options (NULL) |
---|
40 | { |
---|
41 | this->path(path); |
---|
42 | this->label(label); |
---|
43 | this->desc(desc); |
---|
44 | this->def(val); |
---|
45 | this->cur(val); |
---|
46 | } |
---|
47 | |
---|
48 | // copy constructor |
---|
49 | Choice::Choice ( const Choice& o ) |
---|
50 | : Object(o) |
---|
51 | { |
---|
52 | this->def(o.def()); |
---|
53 | this->cur(o.cur()); |
---|
54 | |
---|
55 | // need to copy _options |
---|
56 | } |
---|
57 | |
---|
58 | // default destructor |
---|
59 | Choice::~Choice () |
---|
60 | { |
---|
61 | // clean up dynamic memory |
---|
62 | // unallocate the _options? |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | /**********************************************************************/ |
---|
67 | // METHOD: addOption() |
---|
68 | /// Add an option value to the object |
---|
69 | /** |
---|
70 | * Add an option value to the object. Currently all |
---|
71 | * labels must be unique. |
---|
72 | */ |
---|
73 | |
---|
74 | Choice& |
---|
75 | Choice::addOption( |
---|
76 | const char *label, |
---|
77 | const char *desc, |
---|
78 | const char *val) |
---|
79 | { |
---|
80 | option *p = NULL; |
---|
81 | |
---|
82 | p = new option; |
---|
83 | if (!p) { |
---|
84 | // raise error and exit |
---|
85 | } |
---|
86 | |
---|
87 | p->label(label); |
---|
88 | p->desc(desc); |
---|
89 | p->val(val); |
---|
90 | |
---|
91 | if (_options == NULL) { |
---|
92 | _options = Rp_ChainCreate(); |
---|
93 | if (_options == NULL) { |
---|
94 | // raise error and exit |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | Rp_ChainAppend(_options,p); |
---|
99 | |
---|
100 | return *this; |
---|
101 | } |
---|
102 | |
---|
103 | /**********************************************************************/ |
---|
104 | // METHOD: delOption() |
---|
105 | /// Delete an option value from the object |
---|
106 | /** |
---|
107 | * Delete an option value from the object. |
---|
108 | */ |
---|
109 | |
---|
110 | Choice& |
---|
111 | Choice::delOption(const char *label) |
---|
112 | { |
---|
113 | if (label == NULL) { |
---|
114 | return *this; |
---|
115 | } |
---|
116 | |
---|
117 | if (_options == NULL) { |
---|
118 | return *this; |
---|
119 | } |
---|
120 | |
---|
121 | option *p = NULL; |
---|
122 | const char *plabel = NULL; |
---|
123 | Rp_ChainLink *l = NULL; |
---|
124 | |
---|
125 | // traverse the list looking for the matching option |
---|
126 | l = Rp_ChainFirstLink(_options); |
---|
127 | while (l != NULL) { |
---|
128 | p = (option *) Rp_ChainGetValue(l); |
---|
129 | plabel = p->label(); |
---|
130 | if ((*label == *plabel) && (strcmp(plabel,label) == 0)) { |
---|
131 | // we found matching entry, remove it |
---|
132 | if (p) { |
---|
133 | delete p; |
---|
134 | Rp_ChainDeleteLink(_options,l); |
---|
135 | } |
---|
136 | break; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | return *this; |
---|
142 | } |
---|
143 | |
---|
144 | /**********************************************************************/ |
---|
145 | // METHOD: xml() |
---|
146 | /// view this object's xml |
---|
147 | /** |
---|
148 | * View this object as an xml element returned as text. |
---|
149 | */ |
---|
150 | |
---|
151 | const char * |
---|
152 | Choice::xml(size_t indent, size_t tabstop) |
---|
153 | { |
---|
154 | size_t l1width = indent + tabstop; |
---|
155 | size_t l2width = indent + (2*tabstop); |
---|
156 | size_t l3width = indent + (3*tabstop); |
---|
157 | const char *sp = ""; |
---|
158 | |
---|
159 | Path p(path()); |
---|
160 | _tmpBuf.clear(); |
---|
161 | |
---|
162 | _tmpBuf.appendf( |
---|
163 | "%7$*4$s<choice id='%1$s'>\n\ |
---|
164 | %7$*5$s<about>\n\ |
---|
165 | %7$*6$s<label>%2$s</label>\n\ |
---|
166 | %7$*6$s<description>%3$s</description>\n\ |
---|
167 | %7$*5$s</about>\n", |
---|
168 | p.id(),label(),desc(),indent,l1width,l2width,sp); |
---|
169 | |
---|
170 | Rp_ChainLink *l = NULL; |
---|
171 | l = Rp_ChainFirstLink(_options); |
---|
172 | while (l != NULL) { |
---|
173 | option *op = (option *)Rp_ChainGetValue(l); |
---|
174 | _tmpBuf.appendf( |
---|
175 | "%7$*4$s<option>\n\ |
---|
176 | %7$*5$s<about>\n\ |
---|
177 | %7$*6$s<label>%1$s</label>\n\ |
---|
178 | %7$*6$s<description>%2$s</description>\n\ |
---|
179 | %7$*5$s</about>\n\ |
---|
180 | %7$*5$s<value>%3$s</value>\n\ |
---|
181 | %7$*4$s</option>\n", |
---|
182 | op->label(),op->desc(),op->val(),l1width,l2width,l3width,sp); |
---|
183 | l = Rp_ChainNextLink(l); |
---|
184 | } |
---|
185 | |
---|
186 | _tmpBuf.appendf( |
---|
187 | "%5$*4$s<default>%1$s</default>\n\ |
---|
188 | %5$*4$s<current>%2$s</current>\n\ |
---|
189 | %5$*3$s</choice>", |
---|
190 | def(),cur(),indent,l1width,sp); |
---|
191 | |
---|
192 | return _tmpBuf.bytes(); |
---|
193 | } |
---|
194 | |
---|
195 | /**********************************************************************/ |
---|
196 | // METHOD: is() |
---|
197 | /// what kind of object is this |
---|
198 | /** |
---|
199 | * return hex value telling what kind of object this is. |
---|
200 | */ |
---|
201 | |
---|
202 | const int |
---|
203 | Choice::is() const |
---|
204 | { |
---|
205 | // return "choi" in hex |
---|
206 | return 0x63686F69; |
---|
207 | } |
---|
208 | |
---|
209 | // -------------------------------------------------------------------- // |
---|
210 | |
---|