Mercurial > audlegacy
comparison Plugins/Visualization/paranormal/pn/pnintegeroption.c @ 1507:0c5fdcf3f947 trunk
[svn] - incomplete stuff
author | nenolod |
---|---|
date | Sun, 06 Aug 2006 01:53:29 -0700 |
parents | |
children | 02841f72b897 |
comparison
equal
deleted
inserted
replaced
1506:2a8e193c07a6 | 1507:0c5fdcf3f947 |
---|---|
1 /* Paranormal - A highly customizable audio visualization library | |
2 * Copyright (C) 2001 Jamie Gennis <jgennis@mindspring.com> | |
3 * | |
4 * This library is free software; you can redistribute it and/or | |
5 * modify it under the terms of the GNU Library General Public | |
6 * License as published by the Free Software Foundation; either | |
7 * version 2 of the License, or (at your option) any later version. | |
8 * | |
9 * This library is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 * Library General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU Library General Public | |
15 * License along with this library; if not, write to the Free | |
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include <stdlib.h> | |
22 #include <limits.h> | |
23 #include <glib.h> | |
24 #include "pnintegeroption.h" | |
25 #include "pnxml.h" | |
26 #include "pnerror.h" | |
27 | |
28 static void pn_integer_option_class_init (PnIntegerOptionClass *class); | |
29 static void pn_integer_option_init (PnIntegerOption *integer_option, | |
30 PnIntegerOptionClass *class); | |
31 | |
32 /* PnUserObject methods */ | |
33 static void pn_integer_option_save_thyself (PnUserObject *user_object, | |
34 xmlNodePtr node); | |
35 static void pn_integer_option_load_thyself (PnUserObject *user_object, | |
36 xmlNodePtr node); | |
37 | |
38 static PnUserObjectClass *parent_class = NULL; | |
39 | |
40 GType | |
41 pn_integer_option_get_type (void) | |
42 { | |
43 static GType integer_option_type = 0; | |
44 | |
45 if (! integer_option_type) | |
46 { | |
47 static const GTypeInfo integer_option_info = | |
48 { | |
49 sizeof (PnIntegerOptionClass), | |
50 NULL, /* base_init */ | |
51 NULL, /* base_finalize */ | |
52 (GClassInitFunc) pn_integer_option_class_init, | |
53 NULL, /* class_finalize */ | |
54 NULL, /* class_data */ | |
55 sizeof (PnIntegerOption), | |
56 0, /* n_preallocs */ | |
57 (GInstanceInitFunc) pn_integer_option_init | |
58 }; | |
59 | |
60 /* FIXME: should this be dynamic? */ | |
61 integer_option_type = g_type_register_static (PN_TYPE_OPTION, | |
62 "PnIntegerOption", | |
63 &integer_option_info, | |
64 0); | |
65 } | |
66 return integer_option_type; | |
67 } | |
68 | |
69 static void | |
70 pn_integer_option_class_init (PnIntegerOptionClass *class) | |
71 { | |
72 PnObjectClass *object_class; | |
73 PnUserObjectClass *user_object_class; | |
74 PnOptionClass *option_class; | |
75 | |
76 parent_class = g_type_class_peek_parent (class); | |
77 | |
78 object_class = (PnObjectClass *) class; | |
79 user_object_class = (PnUserObjectClass *) class; | |
80 option_class = (PnOptionClass *) class; | |
81 | |
82 /* PnUserObject methods */ | |
83 user_object_class->save_thyself = pn_integer_option_save_thyself; | |
84 user_object_class->load_thyself = pn_integer_option_load_thyself; | |
85 | |
86 /* PnOption methods */ | |
87 /* FIXME: this needs to be uncommented when the widget is done */ | |
88 /* option_class->widget_type = PN_TYPE_INTEGER_OPTION_WIDGET; */ | |
89 } | |
90 | |
91 static void | |
92 pn_integer_option_init (PnIntegerOption *integer_option, | |
93 PnIntegerOptionClass *class) | |
94 { | |
95 integer_option->min = INT_MIN; | |
96 integer_option->max = INT_MAX; | |
97 } | |
98 | |
99 static void | |
100 pn_integer_option_save_thyself (PnUserObject *user_object, xmlNodePtr node) | |
101 { | |
102 PnIntegerOption *integer_option; | |
103 xmlNodePtr value_node; | |
104 gchar str[16]; | |
105 | |
106 g_return_if_fail (user_object != NULL); | |
107 g_return_if_fail (PN_IS_INTEGER_OPTION (user_object)); | |
108 g_return_if_fail (node != NULL); | |
109 | |
110 integer_option = (PnIntegerOption *) user_object; | |
111 | |
112 value_node = xmlNewChild (node, NULL, "Value", NULL); | |
113 sprintf (str, "%i", integer_option->value); | |
114 xmlNodeSetContent (value_node, str); | |
115 | |
116 if (parent_class->save_thyself) | |
117 parent_class->save_thyself (user_object, node); | |
118 } | |
119 | |
120 static void | |
121 pn_integer_option_load_thyself (PnUserObject *user_object, const xmlNodePtr node) | |
122 { | |
123 PnIntegerOption *integer_option; | |
124 xmlNodePtr integer_option_node; | |
125 gchar *val_str; | |
126 | |
127 g_return_if_fail (user_object != NULL); | |
128 g_return_if_fail (PN_IS_INTEGER_OPTION (user_object)); | |
129 g_return_if_fail (node != NULL); | |
130 | |
131 integer_option = (PnIntegerOption *) user_object; | |
132 | |
133 /* find the node for this class */ | |
134 for (integer_option_node = node->xmlChildrenNode; | |
135 integer_option_node; | |
136 integer_option_node = integer_option_node->next) | |
137 if (g_strcasecmp (integer_option_node->name, "Value") == 0) | |
138 break; | |
139 if (! integer_option_node) | |
140 { | |
141 pn_error ("unable to load a PnIntegerOption from xml node \"%s\"", node->name); | |
142 return; | |
143 } | |
144 | |
145 val_str = xmlNodeGetContent (integer_option_node); | |
146 | |
147 if (val_str) | |
148 pn_integer_option_set_value (integer_option, strtol (val_str, NULL, 0)); | |
149 else | |
150 { | |
151 pn_error ("invalid integer option value encountered at xml node \"%s\"", node->name); | |
152 return; | |
153 } | |
154 | |
155 if (parent_class->load_thyself) | |
156 parent_class->load_thyself (user_object, node); | |
157 } | |
158 | |
159 PnIntegerOption* | |
160 pn_integer_option_new (const gchar *name, const gchar *desc) | |
161 { | |
162 PnIntegerOption *integer_option; | |
163 | |
164 g_return_val_if_fail (name != NULL, NULL); | |
165 g_return_val_if_fail (desc != NULL, NULL); | |
166 | |
167 integer_option = (PnIntegerOption *) g_object_new (PN_TYPE_INTEGER_OPTION, NULL); | |
168 | |
169 pn_user_object_set_name (PN_USER_OBJECT (integer_option), name); | |
170 pn_user_object_set_description (PN_USER_OBJECT (integer_option), desc); | |
171 | |
172 return integer_option; | |
173 } | |
174 | |
175 void | |
176 pn_integer_option_set_value (PnIntegerOption *integer_option, gint value) | |
177 { | |
178 g_return_if_fail (integer_option != NULL); | |
179 g_return_if_fail (PN_IS_INTEGER_OPTION (integer_option)); | |
180 | |
181 integer_option->value = CLAMP (value, integer_option->min, integer_option->max); | |
182 } | |
183 | |
184 gint | |
185 pn_integer_option_get_value (PnIntegerOption *integer_option) | |
186 { | |
187 g_return_val_if_fail (integer_option != NULL, FALSE); | |
188 g_return_val_if_fail (PN_IS_INTEGER_OPTION (integer_option), FALSE); | |
189 | |
190 return integer_option->value; | |
191 } | |
192 | |
193 void | |
194 pn_integer_option_set_min (PnIntegerOption *integer_option, gint min) | |
195 { | |
196 g_return_if_fail (integer_option != NULL); | |
197 g_return_if_fail (PN_IS_INTEGER_OPTION (integer_option)); | |
198 | |
199 if (min > integer_option->max) | |
200 { | |
201 integer_option->min = integer_option->max; | |
202 integer_option->max = min; | |
203 } | |
204 else | |
205 integer_option->min = min; | |
206 | |
207 pn_integer_option_set_value (integer_option, integer_option->value); | |
208 } | |
209 | |
210 gint | |
211 pn_integer_option_get_min (PnIntegerOption *integer_option) | |
212 { | |
213 g_return_val_if_fail (integer_option != NULL, FALSE); | |
214 g_return_val_if_fail (PN_IS_INTEGER_OPTION (integer_option), FALSE); | |
215 | |
216 return integer_option->min; | |
217 } | |
218 | |
219 void | |
220 pn_integer_option_set_max (PnIntegerOption *integer_option, gint max) | |
221 { | |
222 g_return_if_fail (integer_option != NULL); | |
223 g_return_if_fail (PN_IS_INTEGER_OPTION (integer_option)); | |
224 | |
225 if (max < integer_option->min) | |
226 { | |
227 integer_option->max = integer_option->min; | |
228 integer_option->min = max; | |
229 } | |
230 else | |
231 integer_option->max = max; | |
232 | |
233 pn_integer_option_set_value (integer_option, integer_option->value); | |
234 } | |
235 | |
236 gint | |
237 pn_integer_option_get_max (PnIntegerOption *integer_option) | |
238 { | |
239 g_return_val_if_fail (integer_option != NULL, FALSE); | |
240 g_return_val_if_fail (PN_IS_INTEGER_OPTION (integer_option), FALSE); | |
241 | |
242 return integer_option->max; | |
243 } |