1507
|
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 <ctype.h>
|
|
22 #include <glib.h>
|
|
23 #include "pnlistoption.h"
|
|
24 #include "pnxml.h"
|
|
25 #include "pnerror.h"
|
|
26
|
|
27 static void pn_list_option_class_init (PnListOptionClass *class);
|
|
28 static void pn_list_option_init (PnListOption *list_option,
|
|
29 PnListOptionClass *class);
|
|
30
|
|
31 /* PnUserObject methods */
|
|
32 static void pn_list_option_save_thyself (PnUserObject *user_object,
|
|
33 xmlNodePtr node);
|
|
34 static void pn_list_option_load_thyself (PnUserObject *user_object,
|
|
35 xmlNodePtr node);
|
|
36
|
|
37 static PnUserObjectClass *parent_class = NULL;
|
|
38
|
|
39 GType
|
|
40 pn_list_option_get_type (void)
|
|
41 {
|
|
42 static GType list_option_type = 0;
|
|
43
|
|
44 if (! list_option_type)
|
|
45 {
|
|
46 static const GTypeInfo list_option_info =
|
|
47 {
|
|
48 sizeof (PnListOptionClass),
|
|
49 NULL, /* base_init */
|
|
50 NULL, /* base_finalize */
|
|
51 (GClassInitFunc) pn_list_option_class_init,
|
|
52 NULL, /* class_finalize */
|
|
53 NULL, /* class_data */
|
|
54 sizeof (PnListOption),
|
|
55 0, /* n_preallocs */
|
|
56 (GInstanceInitFunc) pn_list_option_init
|
|
57 };
|
|
58
|
|
59 /* FIXME: should this be dynamic? */
|
|
60 list_option_type = g_type_register_static (PN_TYPE_OPTION,
|
|
61 "PnListOption",
|
|
62 &list_option_info,
|
|
63 0);
|
|
64 }
|
|
65 return list_option_type;
|
|
66 }
|
|
67
|
|
68 static void
|
|
69 pn_list_option_class_init (PnListOptionClass *class)
|
|
70 {
|
|
71 PnObjectClass *object_class;
|
|
72 PnUserObjectClass *user_object_class;
|
|
73 PnOptionClass *option_class;
|
|
74
|
|
75 parent_class = g_type_class_peek_parent (class);
|
|
76
|
|
77 object_class = (PnObjectClass *) class;
|
|
78 user_object_class = (PnUserObjectClass *) class;
|
|
79 option_class = (PnOptionClass *) class;
|
|
80
|
|
81 /* PnUserObject methods */
|
|
82 user_object_class->save_thyself = pn_list_option_save_thyself;
|
|
83 user_object_class->load_thyself = pn_list_option_load_thyself;
|
|
84
|
|
85 /* PnOption methods */
|
|
86 /* FIXME: this needs to be uncommented when the widget is done */
|
|
87 /* option_class->widget_type = PN_TYPE_LIST_OPTION_WIDGET; */
|
|
88 }
|
|
89
|
|
90 static void
|
|
91 pn_list_option_init (PnListOption *list_option, PnListOptionClass *class)
|
|
92 {
|
|
93 list_option->items = g_array_new (FALSE, FALSE, sizeof (const gchar *));
|
|
94 list_option->index = -1;
|
|
95 }
|
|
96
|
|
97 static void
|
|
98 pn_list_option_save_thyself (PnUserObject *user_object, xmlNodePtr node)
|
|
99 {
|
|
100 PnListOption *list_option;
|
|
101 xmlNodePtr value_node;
|
|
102
|
|
103 g_return_if_fail (user_object != NULL);
|
|
104 g_return_if_fail (PN_IS_LIST_OPTION (user_object));
|
|
105 g_return_if_fail (node != NULL);
|
|
106
|
|
107 list_option = (PnListOption *) user_object;
|
|
108
|
|
109 value_node = xmlNewChild (node, NULL, "Value", NULL);
|
|
110 xmlNodeSetContent (value_node, g_array_index (list_option->items, const gchar *, list_option->index));
|
|
111
|
|
112 if (parent_class->save_thyself)
|
|
113 parent_class->save_thyself (user_object, node);
|
|
114 }
|
|
115
|
|
116 static void
|
|
117 pn_list_option_load_thyself (PnUserObject *user_object, const xmlNodePtr node)
|
|
118 {
|
|
119 PnListOption *list_option;
|
|
120 xmlNodePtr list_option_node;
|
|
121 gchar *val_str;
|
|
122 guint i;
|
|
123
|
|
124 g_return_if_fail (user_object != NULL);
|
|
125 g_return_if_fail (PN_IS_LIST_OPTION (user_object));
|
|
126 g_return_if_fail (node != NULL);
|
|
127
|
|
128 list_option = (PnListOption *) user_object;
|
|
129
|
|
130 /* find the node for this class */
|
|
131 for (list_option_node = node->xmlChildrenNode;
|
|
132 list_option_node;
|
|
133 list_option_node = list_option_node->next)
|
|
134 if (g_strcasecmp (list_option_node->name, "Value") == 0)
|
|
135 break;
|
|
136 if (! list_option_node)
|
|
137 {
|
|
138 pn_error ("unable to load a PnListOption from xml node \"%s\"", node->name);
|
|
139 return;
|
|
140 }
|
|
141
|
|
142 val_str = xmlNodeGetContent (list_option_node);
|
|
143 if (! val_str)
|
|
144 goto done;
|
|
145
|
|
146 for (i=0; i < list_option->items->len; i++)
|
|
147 if (g_strcasecmp (val_str, g_array_index (list_option->items, const gchar *, i)) == 0)
|
|
148 {
|
|
149 list_option->index = i;
|
|
150 goto done;
|
|
151 }
|
|
152
|
|
153 pn_error ("invalid list option value encountered at xml node \"%s\"", node->name);
|
|
154 return;
|
|
155
|
|
156 done:
|
|
157 if (parent_class->load_thyself)
|
|
158 parent_class->load_thyself (user_object, node);
|
|
159 }
|
|
160
|
|
161 PnListOption*
|
|
162 pn_list_option_new (const gchar *name, const gchar *desc)
|
|
163 {
|
|
164 PnListOption *list_option;
|
|
165
|
|
166 g_return_val_if_fail (name != NULL, NULL);
|
|
167 g_return_val_if_fail (desc != NULL, NULL);
|
|
168
|
|
169 list_option = (PnListOption *) g_object_new (PN_TYPE_LIST_OPTION, NULL);
|
|
170
|
|
171 pn_user_object_set_name (PN_USER_OBJECT (list_option), name);
|
|
172 pn_user_object_set_description (PN_USER_OBJECT (list_option), desc);
|
|
173
|
|
174 return list_option;
|
|
175 }
|
|
176
|
|
177 void
|
|
178 pn_list_option_add_item (PnListOption *list_option, const gchar *item)
|
|
179 {
|
|
180 g_return_if_fail (list_option != NULL);
|
|
181 g_return_if_fail (PN_IS_LIST_OPTION (list_option));
|
|
182 g_return_if_fail (item != NULL);
|
|
183
|
|
184 g_array_append_val (list_option->items, item);
|
|
185
|
|
186 if (list_option->index < 0)
|
|
187 list_option->index = 0;
|
|
188 }
|
|
189
|
|
190 void
|
|
191 pn_list_option_set_index (PnListOption *list_option, guint index)
|
|
192 {
|
|
193 g_return_if_fail (list_option != NULL);
|
|
194 g_return_if_fail (PN_IS_LIST_OPTION (list_option));
|
|
195 g_return_if_fail (index < list_option->items->len);
|
|
196
|
|
197 list_option->index = index;
|
|
198 }
|
|
199
|
|
200 gint
|
|
201 pn_list_option_get_index (PnListOption *list_option)
|
|
202 {
|
|
203 g_return_val_if_fail (list_option != NULL, FALSE);
|
|
204 g_return_val_if_fail (PN_IS_LIST_OPTION (list_option), FALSE);
|
|
205
|
|
206 return list_option->index;
|
|
207 }
|