Mercurial > pidgin.yaz
comparison finch/libgnt/gntutils.c @ 16125:5f204f55af09
Add a utility function to create widgets from an XML description.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sun, 15 Apr 2007 05:49:27 +0000 |
parents | 2c81ebc7bf0b |
children | 8410511f4dbb |
comparison
equal
deleted
inserted
replaced
16124:ab3f93232a2d | 16125:5f204f55af09 |
---|---|
1 #include "gntbutton.h" | |
2 #include "gntcheckbox.h" | |
3 #include "gntcombobox.h" | |
4 #include "gntentry.h" | |
5 #include "gntlabel.h" | |
6 #include "gntline.h" | |
7 #include "gnttextview.h" | |
8 #include "gnttree.h" | |
1 #include "gntutils.h" | 9 #include "gntutils.h" |
2 #include "gnttree.h" | 10 #include "gntwindow.h" |
3 | 11 |
12 #include "config.h" | |
13 | |
14 #include <stdarg.h> | |
4 #include <stdlib.h> | 15 #include <stdlib.h> |
5 #include <string.h> | 16 #include <string.h> |
17 | |
18 #ifndef NO_LIBXML | |
19 #include <libxml/parser.h> | |
20 #include <libxml/tree.h> | |
21 #endif | |
6 | 22 |
7 #include "config.h" | 23 #include "config.h" |
8 | 24 |
9 void gnt_util_get_text_bound(const char *text, int *width, int *height) | 25 void gnt_util_get_text_bound(const char *text, int *width, int *height) |
10 { | 26 { |
182 BindingView bv = {hash, GNT_TREE(tree)}; | 198 BindingView bv = {hash, GNT_TREE(tree)}; |
183 | 199 |
184 gnt_tree_set_compare_func(bv.tree, (GCompareFunc)g_utf8_collate); | 200 gnt_tree_set_compare_func(bv.tree, (GCompareFunc)g_utf8_collate); |
185 g_hash_table_foreach(klass->actions, add_action, &bv); | 201 g_hash_table_foreach(klass->actions, add_action, &bv); |
186 g_hash_table_foreach(klass->bindings, add_binding, &bv); | 202 g_hash_table_foreach(klass->bindings, add_binding, &bv); |
187 gnt_tree_adjust_columns(bv.tree); | 203 if (GNT_TREE(tree)->list == NULL) { |
204 gnt_widget_destroy(tree); | |
205 tree = NULL; | |
206 } else | |
207 gnt_tree_adjust_columns(bv.tree); | |
188 g_hash_table_destroy(hash); | 208 g_hash_table_destroy(hash); |
189 | 209 |
190 return tree; | 210 return tree; |
191 } | 211 } |
192 | 212 |
213 #ifndef NO_LIBXML | |
214 static GntWidget * | |
215 gnt_widget_from_xmlnode(xmlNode *node, GntWidget **data[]) | |
216 { | |
217 GntWidget *widget = NULL; | |
218 char *name; | |
219 char *id, *prop, *content; | |
220 int val; | |
221 | |
222 if (node == NULL || node->name == NULL || node->type != XML_ELEMENT_NODE) | |
223 return NULL; | |
224 | |
225 name = (char*)node->name; | |
226 content = (char*)xmlNodeGetContent(node); | |
227 if (strcmp(name + 1, "window") == 0 || strcmp(name + 1, "box") == 0) { | |
228 xmlNode *ch; | |
229 char *title; | |
230 gboolean vert = (*name == 'v'); | |
231 | |
232 if (name[1] == 'w') | |
233 widget = gnt_window_box_new(FALSE, vert); | |
234 else | |
235 widget = gnt_box_new(FALSE, vert); | |
236 | |
237 title = (char*)xmlGetProp(node, (xmlChar*)"title"); | |
238 if (title) { | |
239 gnt_box_set_title(GNT_BOX(widget), title); | |
240 xmlFree(title); | |
241 } | |
242 | |
243 prop = (char*)xmlGetProp(node, (xmlChar*)"fill"); | |
244 if (prop) { | |
245 if (sscanf(prop, "%d", &val) == 1) | |
246 gnt_box_set_fill(GNT_BOX(widget), !!val); | |
247 xmlFree(prop); | |
248 } | |
249 | |
250 prop = (char*)xmlGetProp(node, (xmlChar*)"align"); | |
251 if (prop) { | |
252 if (sscanf(prop, "%d", &val) == 1) | |
253 gnt_box_set_alignment(GNT_BOX(widget), val); | |
254 xmlFree(prop); | |
255 } | |
256 | |
257 prop = (char*)xmlGetProp(node, (xmlChar*)"pad"); | |
258 if (prop) { | |
259 if (sscanf(prop, "%d", &val) == 1) | |
260 gnt_box_set_pad(GNT_BOX(widget), val); | |
261 xmlFree(prop); | |
262 } | |
263 | |
264 for (ch = node->children; ch; ch=ch->next) | |
265 gnt_box_add_widget(GNT_BOX(widget), gnt_widget_from_xmlnode(ch, data)); | |
266 } else if (strcmp(name, "button") == 0) { | |
267 widget = gnt_button_new(content); | |
268 } else if (strcmp(name, "label") == 0) { | |
269 widget = gnt_label_new(content); | |
270 } else if (strcmp(name, "entry") == 0) { | |
271 widget = gnt_entry_new(content); | |
272 } else if (strcmp(name, "combobox") == 0) { | |
273 widget = gnt_combo_box_new(); | |
274 } else if (strcmp(name, "checkbox") == 0) { | |
275 widget = gnt_check_box_new(content); | |
276 } else if (strcmp(name, "tree") == 0) { | |
277 widget = gnt_tree_new(); | |
278 } else if (strcmp(name, "textview") == 0) { | |
279 widget = gnt_text_view_new(); | |
280 } else if (strcmp(name + 1, "line") == 0) { | |
281 widget = gnt_line_new(*name == 'v'); | |
282 } | |
283 | |
284 xmlFree(content); | |
285 | |
286 if (widget == NULL) { | |
287 g_printerr("Invalid widget name %s\n", name); | |
288 return NULL; | |
289 } | |
290 | |
291 id = (char*)xmlGetProp(node, (xmlChar*)"id"); | |
292 if (id) { | |
293 int i; | |
294 sscanf(id, "%d", &i); | |
295 *data[i] = widget; | |
296 xmlFree(id); | |
297 } | |
298 | |
299 prop = (char*)xmlGetProp(node, (xmlChar*)"border"); | |
300 if (prop) { | |
301 int val; | |
302 if (sscanf(prop, "%d", &val) == 1) { | |
303 if (val) | |
304 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_NO_BORDER); | |
305 else | |
306 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER); | |
307 } | |
308 xmlFree(prop); | |
309 } | |
310 | |
311 prop = (char*)xmlGetProp(node, (xmlChar*)"shadow"); | |
312 if (prop) { | |
313 int val; | |
314 if (sscanf(prop, "%d", &val) == 1) { | |
315 if (val) | |
316 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_NO_BORDER); | |
317 else | |
318 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER); | |
319 } | |
320 xmlFree(prop); | |
321 } | |
322 | |
323 return widget; | |
324 } | |
325 #endif | |
326 | |
327 void gnt_util_parse_widgets(const char *string, int num, ...) | |
328 { | |
329 #ifndef NO_LIBXML | |
330 xmlParserCtxtPtr ctxt; | |
331 xmlDocPtr doc; | |
332 xmlNodePtr node; | |
333 va_list list; | |
334 GntWidget ***data; | |
335 int id; | |
336 | |
337 ctxt = xmlNewParserCtxt(); | |
338 doc = xmlCtxtReadDoc(ctxt, (xmlChar*)string, NULL, NULL, XML_PARSE_NOBLANKS); | |
339 | |
340 data = g_new0(GntWidget **, num); | |
341 | |
342 va_start(list, num); | |
343 for (id = 0; id < num; id++) | |
344 data[id] = va_arg(list, gpointer); | |
345 | |
346 node = xmlDocGetRootElement(doc); | |
347 gnt_widget_from_xmlnode(node, data); | |
348 | |
349 xmlFreeDoc(doc); | |
350 xmlCleanupParser(); | |
351 va_end(list); | |
352 g_free(data); | |
353 #endif | |
354 } | |
355 |