diff options
author | Mikael Hallendal <micke@imendio.com> | 2005-01-05 19:00:16 +0000 |
---|---|---|
committer | Mikael Hallendal <hallski@src.gnome.org> | 2005-01-05 19:00:16 +0000 |
commit | 64efa369bbb71d6025b4b5e8c20295a4a1da6c62 (patch) | |
tree | 6075c2b2113469fc82bad6f374e8ec6cf1593ef1 /src/glade-catalog.c | |
parent | 3eee46a64fa999d7e39b0c7cf4275d71c6c351be (diff) | |
download | glade-64efa369bbb71d6025b4b5e8c20295a4a1da6c62.tar.gz |
- Removed need for the palette file, loads all .catalog files in catalog
2005-01-05 Mikael Hallendal <micke@imendio.com>
* src/glade-catalog.[ch]:
- Removed need for the palette file, loads all .catalog files in
catalog directory.
- New catalog format where a catalog is one per library, for example
gtk+.catalog. Each catalog contains one or more widget groups.
Widget class definitions has been separated from the grouping which
means that you can have a widget in two different groups.
* src/glade-palette.c:
- Updated to add widget groups rather than catalogs to the option
menu.
(glade_palette_widget_table_create),
(glade_palette_append_widget_group):
- Renamed from glade_palette_append_catalog.
(glade_palette_new):
* src/glade-palette.h:
- Made glade_palette_append_widget_group internal.
* src/glade-types.h:
- Added GladeWidgetGroup.
* src/glade-widget-class.c:
(glade_widget_class_new):
- Reimplemented to instead take an GladeXmlNode node and load the
widget class from there.
* src/glade-widget-class.h:
- Removed glade_widget_class_new_from_node for which the implementation
was removed over a year ago.
* src/glade.h:
- Moved some GladeCatalog specific XML tags to glade-catalog.c instead.
* src/main.c: (glade_init):
- Give a warning if no catalog files where found instead of silently
exit.
* widgets/Makefile.am:
* widgets/gtk+.catalog:
- Added gtk+.catalog
Diffstat (limited to 'src/glade-catalog.c')
-rw-r--r-- | src/glade-catalog.c | 402 |
1 files changed, 260 insertions, 142 deletions
diff --git a/src/glade-catalog.c b/src/glade-catalog.c index bc779bec..8cdaa8d5 100644 --- a/src/glade-catalog.c +++ b/src/glade-catalog.c @@ -1,6 +1,7 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* * Copyright (C) 2001 Ximian, Inc. + * Copyright (C) 2004 Imendio AB * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as @@ -29,212 +30,329 @@ #include <sys/types.h> #include <sys/stat.h> -#include <glib/gdir.h> +#include <glib.h> #include "glade.h" #include "glade-catalog.h" #include "glade-widget-class.h" -#define GLADE_TAG_PALETTE "GladePalette" +#define GLADE_TAG_CATALOG "GladeCatalog" +#define GLADE_TAG_WIDGETS "GladeWidgets" +#define GLADE_TAG_WIDGET_GROUP "GladeWidgetGroup" -/** - * glade_catalog_delete: - * @catalog: - * - * Frees @catalog and its associated memory. - */ -void -glade_catalog_delete (GladeCatalog *catalog) +struct _GladeCatalog { - GList *list; + gchar *library; - if (catalog == NULL) - return; + gchar *name; + + GList *widget_groups; + GList *widget_classes; +}; + +struct _GladeWidgetGroup { + gchar *name; + gchar *title; + GList *widget_classes; +}; + +static GladeCatalog * catalog_load (const char *filename); +static gboolean catalog_load_widgets (GladeCatalog *catalog, + GladeXmlNode *widgets_node); +static gboolean catalog_load_group (GladeCatalog *catalog, + GladeXmlNode *group_node); + +void widget_group_free (GladeWidgetGroup *group); - g_free (catalog->title); - for (list = catalog->widget_classes; list; list = list->next) - glade_widget_class_free (GLADE_WIDGET_CLASS (list->data)); - g_list_free (catalog->widget_classes); - g_free (catalog); -} static GladeCatalog * -glade_catalog_load (const char *base_catalog_filename) +catalog_load (const char *filename) { - GladeWidgetClass *widget_class = NULL; - GladeXmlContext *context = NULL; - GladeXmlNode *root = NULL; - GladeXmlNode *widget_node = NULL; - GladeXmlDoc *doc = NULL; - GladeCatalog *catalog = NULL; - char *name = NULL; - char *generic_name = NULL; - char *palette_name = NULL; - char *catalog_filename = NULL; - char *base_filename = NULL; - char *base_library = NULL; - GList *last_widget_class = NULL; - - catalog_filename = g_strdup_printf ("%s%c%s", glade_catalogs_dir, G_DIR_SEPARATOR, base_catalog_filename); - if (catalog_filename == NULL) - { - g_critical (_("Not enough memory.")); - goto lblError; - } + GladeCatalog *catalog; + GladeXmlContext *context; + GladeXmlDoc *doc; + GladeXmlNode *root; + GladeXmlNode *node; + g_print ("Loading catalog: %s\n", filename); /* get the context & root node of the catalog file */ - context = glade_xml_context_new_from_path (catalog_filename, NULL, GLADE_TAG_CATALOG); - if (!context) + context = glade_xml_context_new_from_path (filename, + NULL, GLADE_TAG_CATALOG); + if (!context) { - g_warning (_("Impossible to open the catalog [%s]."), catalog_filename); - goto lblError; + g_warning ("Couldn't load catalog [%s].", filename); + return NULL; } - doc = glade_xml_context_get_doc (context); + doc = glade_xml_context_get_doc (context); root = glade_xml_doc_get_root (doc); - catalog = g_new0 (GladeCatalog, 1); + if (!glade_xml_node_verify (root, GLADE_TAG_CATALOG)) + { + g_warning ("Catalog root node is not '%s', skipping %s", + GLADE_TAG_CATALOG, filename); + glade_xml_context_free (context); + return NULL; + } - last_widget_class = NULL; + catalog = g_new0 (GladeCatalog, 1); - /* read the title of this catalog */ - catalog->title = glade_xml_get_property_string_required (root, "Title", catalog_filename); + catalog->name = glade_xml_get_property_string (root, "name"); + if (!catalog->name) + { + g_warning ("Couldn't find required property 'name' in catalog root node"); + g_free (catalog); + glade_xml_context_free (context); + return NULL; + } + + catalog->library = glade_xml_get_property_string (root, "library"); - if (!glade_xml_node_verify (root, GLADE_TAG_CATALOG)) + node = glade_xml_node_get_children (root); + for (; node; node = glade_xml_node_next (node)) { - g_warning (_("The root node of [%s] has a name different from %s."), catalog_filename, GLADE_TAG_CATALOG); - goto lblError; + const gchar *node_name; + + node_name = glade_xml_node_get_name (node); + if (strcmp (node_name, GLADE_TAG_WIDGETS) == 0) + { + catalog_load_widgets (catalog, node); + } + else if (strcmp (node_name, GLADE_TAG_WIDGET_GROUP) == 0) + { + catalog_load_group (catalog, node); + } + else + continue; } - /* get the library to be used by this catalog (if any) */ - base_library = glade_xml_get_property_string (root, "library"); + catalog->widget_groups = g_list_reverse (catalog->widget_groups); + + glade_xml_context_free (context); + + return catalog; +} + +static gboolean +catalog_load_widgets (GladeCatalog *catalog, GladeXmlNode *widgets_node) +{ + GladeXmlNode *node; - /* build all the GladeWidgetClass'es associated with this catalog */ - widget_node = glade_xml_node_get_children (root); - for (; widget_node; widget_node = glade_xml_node_next (widget_node)) + node = glade_xml_node_get_children (widgets_node); + for (; node; node = glade_xml_node_next (node)) { - char *base_widget_class_library = NULL; + const gchar *node_name; + GladeWidgetClass *widget_class; - if (!glade_xml_node_verify (widget_node, GLADE_TAG_GLADE_WIDGET)) + node_name = glade_xml_node_get_name (node); + if (strcmp (node_name, GLADE_TAG_GLADE_WIDGET_CLASS) != 0) continue; + + widget_class = glade_widget_class_new_from_node (node, + catalog->library); - name = glade_xml_get_property_string_required (widget_node, "name", catalog_filename); - if (!name) - continue; + catalog->widget_classes = g_list_prepend (catalog->widget_classes, + widget_class); + } + + return TRUE; +} + +static gboolean +catalog_load_group (GladeCatalog *catalog, GladeXmlNode *group_node) +{ + GladeWidgetGroup *group; + GladeXmlNode *node; + + group = g_new0 (GladeWidgetGroup, 1); + + group->name = glade_xml_get_property_string (group_node, "name"); + if (!group->name) + { + g_warning ("Required property 'name' not found in group node"); + + widget_group_free (group); + return FALSE; + } + + group->title = glade_xml_get_property_string (group_node, "title"); + if (!group->title) + { + g_warning ("Required property 'title' not found in group node"); + + widget_group_free (group); + return FALSE; + } + + g_print ("Loading widget group: %s\n", group->title); + + group->widget_classes = NULL; + + node = glade_xml_node_get_children (group_node); + for (; node; node = glade_xml_node_next (node)) + { + const gchar *node_name; + GladeWidgetClass *widget_class; + gchar *name; - /* get the specific library to the widget class, if any */ - base_widget_class_library = glade_xml_get_property_string (widget_node, "library"); - generic_name = glade_xml_get_property_string (widget_node, "generic_name"); - palette_name = glade_xml_get_property_string (widget_node, "palette_name"); + node_name = glade_xml_node_get_name (node); - base_filename = glade_xml_get_property_string (widget_node, "filename"); + if (strcmp (node_name, GLADE_TAG_GLADE_WIDGET_CLASS) != 0) + continue; - widget_class = glade_widget_class_new (name, generic_name, palette_name, base_filename, - base_widget_class_library ? base_widget_class_library : base_library); - if (widget_class) + name = glade_xml_get_property_string (node, "name"); + if (!name) { - last_widget_class = g_list_append (last_widget_class, widget_class); - - if (last_widget_class->next != NULL) - last_widget_class = last_widget_class->next; - else - catalog->widget_classes = last_widget_class; + g_warning ("Couldn't find required property on %s", + GLADE_TAG_GLADE_WIDGET_CLASS); + continue; } + widget_class = glade_widget_class_get_by_name (name); + if (!widget_class) + { + g_warning ("Tried to include undefined widget class '%s' in a widget group", name); + g_free (name); + continue; + } g_free (name); - g_free (generic_name); - g_free (palette_name); - g_free (base_filename); - g_free (base_widget_class_library); + + group->widget_classes = g_list_prepend (group->widget_classes, + widget_class); } - glade_xml_context_free (context); - g_free (catalog_filename); - g_free (base_library); - return catalog; + group->widget_classes = g_list_reverse (group->widget_classes); -lblError: - glade_xml_context_free (context); - g_free (catalog_filename); - g_free (catalog); - g_free (base_library); - return NULL; + catalog->widget_groups = g_list_prepend (catalog->widget_groups, + group); + + return TRUE; } -/** - * glade_catalog_load_all: - * - * TODO: write me - * - * Returns: - */ +void +widget_group_free (GladeWidgetGroup *group) +{ + g_return_if_fail (group != NULL); + + g_free (group->name); + g_free (group->title); + + /* The actual widget classes will be free elsewhere */ + g_list_free (group->widget_classes); +} + GList * glade_catalog_load_all (void) { - gchar *filename = g_build_filename (glade_catalogs_dir, "glade-palette.xml", NULL); - GladeXmlContext *context; - GladeXmlNode *root; - GladeXmlNode *xml_catalogs; - GladeXmlDoc *doc; - GList *catalogs = NULL; + GDir *dir; + GError *error; + const gchar *filename; + GList *catalogs; GladeCatalog *catalog; - - context = glade_xml_context_new_from_path (filename, NULL, GLADE_TAG_PALETTE); - if (context == NULL) + + /* Read all files in catalog dir */ + dir = g_dir_open (glade_catalogs_dir, 0, &error); + if (!dir) { - g_critical ("Unable to open %s.\n", filename); + g_warning ("Failed to open catalog directory: %s", + error->message); return NULL; } - doc = glade_xml_context_get_doc (context); - root = glade_xml_doc_get_root (doc); - xml_catalogs = glade_xml_node_get_children (root); - while (xml_catalogs != NULL) { - char *name = glade_xml_get_property_string_required (xml_catalogs, "filename", NULL); + catalogs = NULL; + while ((filename = g_dir_read_name (dir))) + { + gchar *catalog_filename; - if (!strcmp(glade_xml_node_get_name (xml_catalogs), GLADE_TAG_CATALOG)) - { - catalog = glade_catalog_load (name); - if (catalog) - catalogs = g_list_append (catalogs, catalog); - else - g_warning ("Unable to open the catalog %s.\n", name); - } - else - g_warning ("The palette file \"glade-palette.xml\" has " - "a node with name %s instead of " GLADE_TAG_CATALOG "\n", name); + if (!g_str_has_suffix (filename, ".catalog")) + continue; - xml_catalogs = glade_xml_node_next (xml_catalogs); - g_free (name); + catalog_filename = g_build_filename (glade_catalogs_dir, + filename, NULL); + catalog = catalog_load (catalog_filename); + g_free (catalog_filename); + + if (catalog) + catalogs = g_list_append (catalogs, catalog); + else + g_warning ("Unable to open the catalog file %s.\n", + filename); } - glade_xml_context_free (context); - g_free (filename); + g_dir_close (dir); + return catalogs; } -/** - * glade_catalog_get_title: - * @catalog: a #GladeCatalog - * - * Returns: a pointer to the title of @catalog - */ -const char * -glade_catalog_get_title (GladeCatalog *catalog) +const gchar * +glade_catalog_get_name (GladeCatalog *catalog) { g_return_val_if_fail (catalog != NULL, NULL); - return catalog->title; + + return catalog->name; +} + +GList * +glade_catalog_get_widget_groups (GladeCatalog *catalog) +{ + g_return_val_if_fail (catalog != NULL, NULL); + + return catalog->widget_groups; } -/** - * glade_catalog_get_widget_classes: - * @catalog: a #GladeCatalog - * - * Returns: a #GList containing the widget classes in @catalog - */ GList * glade_catalog_get_widget_classes (GladeCatalog *catalog) { g_return_val_if_fail (catalog != NULL, NULL); + return catalog->widget_classes; } + +void +glade_catalog_free (GladeCatalog *catalog) +{ + GList *list; + + if (catalog == NULL) + return; + + g_free (catalog->name); + + for (list = catalog->widget_classes; list; list = list->next) + glade_widget_class_free (GLADE_WIDGET_CLASS (list->data)); + + g_list_free (catalog->widget_classes); + + for (list = catalog->widget_groups; list; list = list->next) + widget_group_free (GLADE_WIDGET_GROUP (list->data)); + + g_list_free (catalog->widget_groups); + + + g_free (catalog); +} +const gchar * +glade_widget_group_get_name (GladeWidgetGroup *group) +{ + g_return_val_if_fail (group != NULL, NULL); + + return group->name; +} + +const gchar * +glade_widget_group_get_title (GladeWidgetGroup *group) +{ + g_return_val_if_fail (group != NULL, NULL); + + return group->title; +} + +GList * +glade_widget_group_get_widget_classes (GladeWidgetGroup *group) +{ + g_return_val_if_fail (group != NULL, NULL); + + return group->widget_classes; +} + + |