[monet/monet-xml] mn-config: use a hash table to store the widget drawing operations



commit 67d53ab83e40e1e8233c1fa3381ee8afc39bf0d4
Author: Thomas Wood <thos gnome org>
Date:   Sat Sep 4 23:27:00 2010 +0100

    mn-config: use a hash table to store the widget drawing operations

 monet-gtk/style.c |    6 ++--
 monet/mn-config.c |   75 +++++++++++++++++++++++++---------------------------
 monet/mn-config.h |   27 ++++++++-----------
 monet/mn-style.c  |   35 +++++++++++++++----------
 monet/mn-style.h  |   20 +++++++-------
 5 files changed, 81 insertions(+), 82 deletions(-)
---
diff --git a/monet-gtk/style.c b/monet-gtk/style.c
index 52808c9..b47d3ee 100644
--- a/monet-gtk/style.c
+++ b/monet-gtk/style.c
@@ -278,7 +278,7 @@ monet_gtk_draw_box (GtkStyle      *style,
   MnFlags mn_flags;
   MnStyle *mn_style;
   gint int_w, int_h;
-  MnWidget mn_widget;
+  const gchar *mn_widget;
 
   monet_params_init (style, window, state_type, widget,
                      &mn_style, &cr, &mn_state, &mn_flags, &mn_palette);
@@ -290,7 +290,7 @@ monet_gtk_draw_box (GtkStyle      *style,
       height = int_h;
     }
 
-  mn_widget = MN_WIDGET_INVALID;
+  mn_widget = NULL;
 
   if (DETAIL ("button"))
     {
@@ -313,7 +313,7 @@ monet_gtk_draw_box (GtkStyle      *style,
       mn_widget = MN_TOOL_BAR;
     }
 
-  if (mn_widget != MN_WIDGET_INVALID)
+  if (mn_widget != NULL)
     {
       mn_style_paint_widget (mn_style, mn_widget, cr, x, y, width, height,
                              mn_palette, mn_state, mn_flags);
diff --git a/monet/mn-config.c b/monet/mn-config.c
index 549b3ed..44fa4ec 100644
--- a/monet/mn-config.c
+++ b/monet/mn-config.c
@@ -73,7 +73,7 @@ mn_config_free_drawing_ops (MnDrawingOps *ops)
 }
 
 static void
-mn_config_free_ops (GSList *ops)
+mn_config_free_ops (gchar *key, GSList *ops, gpointer user_data)
 {
   g_slist_foreach (ops, (GFunc) mn_config_free_drawing_ops, NULL);
   g_slist_free (ops);
@@ -83,13 +83,10 @@ static void
 mn_config_finalize (GObject *object)
 {
   MnConfig *config = MN_CONFIG (object);
-  gint widget, state;
 
-  for (widget = 0; widget < MN_WIDGET_LAST; widget++)
-    for (state = 0; state < 4; state++)
-      {
-        mn_config_free_ops (config->widget_ops[widget][state]);
-      }
+  g_hash_table_foreach (config->widget_ops, (GHFunc)mn_config_free_ops, NULL);
+  g_hash_table_destroy (config->widget_ops);
+  config->widget_ops = NULL;
 
   G_OBJECT_CLASS (mn_config_parent_class)->finalize (object);
 }
@@ -109,6 +106,9 @@ static void
 mn_config_init (MnConfig *self)
 {
   self->priv = CONFIG_PRIVATE (self);
+
+  self->widget_ops = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                            g_free, NULL);
 }
 
 MnConfig *
@@ -323,19 +323,28 @@ add_circle (MnConfig *config,
   config->priv->current_ops->ops = g_slist_append (config->priv->current_ops->ops, op);
 }
 
-static MnState
-monet_state_from_string (const gchar *string)
+const gchar *
+mn_state_to_string (MnState state)
 {
-  switch (string[0])
+  switch (state)
     {
-      case 'n': return MN_STATE_NORMAL;
-      case 'h': return MN_STATE_HOVER;
-      case 'd': return MN_STATE_DISABLED;
-      case 'a': return MN_STATE_ACTIVE;
-      default: return MN_STATE_NORMAL;
+    case MN_STATE_NORMAL: return NULL;
+    case MN_STATE_HOVER: return "hover";
+    case MN_STATE_DISABLED: return "disabled";
+    case MN_STATE_ACTIVE: return "active";
+    default: return NULL;
     }
 }
 
+gchar*
+mn_create_index (const gchar *widget,
+                 const gchar *state)
+{
+  return g_strconcat (widget,
+                      (state) ? "#" : NULL, state,
+                      NULL);
+}
+
 static MnFlags
 monet_flags_from_string (const gchar *string)
 {
@@ -370,7 +379,7 @@ monet_start_element (GMarkupParseContext  *context,
   MnConfig *config = (MnConfig *) user_data;
   MnConfigPrivate *priv = config->priv;
   GError *err = NULL;
-  gint i, widget;
+  gint i;
 
   if (!strcmp (element_name, "monet"))
     {
@@ -381,11 +390,12 @@ monet_start_element (GMarkupParseContext  *context,
   if (!strcmp (element_name, "widget"))
     {
       const gchar *type = NULL;
+      const gchar *state = NULL;
+      gchar *idex;
       MnDrawingOps *ops;
-      MnState wstate;
+      GSList *list;
       MnFlags wflags;
 
-      wstate = MN_STATE_NORMAL;
       wflags = 0;
 
       /* inspect the attributes */
@@ -395,7 +405,7 @@ monet_start_element (GMarkupParseContext  *context,
             type = attribute_values[i];
 
           else if (!strcmp (attribute_names[i], "state"))
-            wstate = monet_state_from_string (attribute_values[i]);
+            state = attribute_values[i];
 
           else if (!strcmp (attribute_names[i], "flags"))
             wflags = monet_flags_from_string (attribute_values[i]);
@@ -409,32 +419,19 @@ monet_start_element (GMarkupParseContext  *context,
           return;
         }
 
-      if (!strcmp (type, "button"))
-        widget = MN_BUTTON;
-      else if (!strcmp (type, "entry"))
-        widget = MN_ENTRY;
-      else if (!strcmp (type, "check"))
-        widget = MN_CHECK;
-      else if (!strcmp (type, "radio"))
-        widget = MN_RADIO;
-      else if (!strcmp (type, "menu"))
-        widget = MN_MENU;
-      else if (!strcmp (type, "menu-item"))
-        widget = MN_MENU_ITEM;
-      else if (!strcmp (type, "menu-bar"))
-        widget = MN_MENU_BAR;
-      else if (!strcmp (type, "tool-bar"))
-        widget = MN_TOOL_BAR;
+      idex = mn_create_index (type, state);
+
+      list = g_hash_table_lookup (config->widget_ops, idex);
 
       /* add the drawing operations for this widget and state */
       ops = g_new0 (MnDrawingOps, 1);
       ops->flags = wflags;
 
-      priv->current_ops = ops;
+      list = g_slist_prepend (list, ops);
 
-      config->widget_ops[widget][wstate] =
-        g_slist_prepend (config->widget_ops[widget][wstate], ops);
+      g_hash_table_insert (config->widget_ops, idex, list);
 
+      priv->current_ops = ops;
     }
   else if (!strcmp (element_name, "gradient"))
     add_pattern (config, attribute_names, attribute_values, FALSE);
@@ -447,7 +444,7 @@ monet_start_element (GMarkupParseContext  *context,
   else if (!strcmp (element_name, "line"))
     add_line (config, attribute_names, attribute_values);
   else if (!strcmp (element_name, "circle"))
-    add_circle (config, attribute_names, attribute_names);
+    add_circle (config, attribute_names, attribute_values);
 }
 
 static void
diff --git a/monet/mn-config.h b/monet/mn-config.h
index 67a5536..bba10d8 100644
--- a/monet/mn-config.h
+++ b/monet/mn-config.h
@@ -61,20 +61,14 @@ typedef struct
   GSList  *ops;
 } MnDrawingOps;
 
-typedef enum
-{
-  MN_WIDGET_INVALID = -1,
-  MN_BUTTON,
-  MN_ENTRY,
-  MN_CHECK,
-  MN_RADIO,
-  MN_MENU,
-  MN_MENU_ITEM,
-  MN_MENU_BAR,
-  MN_TOOL_BAR,
-
-  MN_WIDGET_LAST
-} MnWidget;
+#define MN_BUTTON "button"
+#define MN_ENTRY  "entry"
+#define MN_CHECK  "check"
+#define MN_RADIO  "radio"
+#define MN_MENU   "menu"
+#define MN_MENU_ITEM "menu-item"
+#define MN_MENU_BAR "menu-bar"
+#define MN_TOOL_BAR "tool-bar"
 
 struct _MnConfig
 {
@@ -83,7 +77,7 @@ struct _MnConfig
   MnConfigPrivate *priv;
 
   /*< private >*/
-  GSList *widget_ops[MN_WIDGET_LAST][4];
+  GHashTable *widget_ops;
 };
 
 struct _MnConfigClass
@@ -145,7 +139,8 @@ MnConfig* mn_config_new (void);
 gboolean mn_config_load_from_file (MnConfig     *config,
                                    const gchar  *file,
                                    GError      **err);
-
+gchar * mn_create_index (const gchar *widget, const gchar *state);
+const gchar * mn_state_to_string (MnState state);
 G_END_DECLS
 
 #endif /* MN_CONFIG_H */
diff --git a/monet/mn-style.c b/monet/mn-style.c
index b94330c..2e34817 100644
--- a/monet/mn-style.c
+++ b/monet/mn-style.c
@@ -311,27 +311,34 @@ mn_style_draw_ops (MnStyle   *style,
 }
 
 void
-mn_style_paint_widget (MnStyle   *style,
-                       MnWidget   widget,
-                       cairo_t   *cr,
-                       gdouble    x,
-                       gdouble    y,
-                       gdouble    width,
-                       gdouble    height,
-                       MnPalette *colors,
-                       MnState    state,
-                       MnFlags    flags)
+mn_style_paint_widget (MnStyle     *style,
+                       const gchar *widget,
+                       cairo_t     *cr,
+                       gdouble      x,
+                       gdouble      y,
+                       gdouble      width,
+                       gdouble      height,
+                       MnPalette   *colors,
+                       MnState      state,
+                       MnFlags      flags)
 {
   GSList *widget_ops;
   GSList *l;
   GSList *drawing_ops = NULL;
   GSList *default_ops = NULL;
+  gchar *idex;
+  const gchar *state_str;
 
-  widget_ops = style->priv->config->widget_ops[widget][state];
+  state_str = mn_state_to_string (state);
+  idex = mn_create_index (widget, state_str);
+
+  widget_ops = g_hash_table_lookup (style->priv->config->widget_ops, idex);
+
+  g_free (idex);
 
   /* if no ops were found for the request state, try the default state */
   if (!widget_ops)
-    widget_ops = style->priv->config->widget_ops[widget][MN_STATE_NORMAL];
+    widget_ops = g_hash_table_lookup (style->priv->config->widget_ops, widget);
 
   /* find an ops withe the requested flags */
   for (l = widget_ops; l; l = g_slist_next (l))
@@ -357,7 +364,7 @@ mn_style_paint_widget (MnStyle   *style,
   else if (default_ops)
     mn_style_draw_ops (style, default_ops, cr, x, y, width, height, colors);
   else
-    g_warning ("No drawing could be found for widget type %d in state %d",
-               widget, state);
+    g_warning ("No drawing could be found for widget type %s in state %s",
+               widget, state_str);
 
 }
diff --git a/monet/mn-style.h b/monet/mn-style.h
index fa82832..877a78e 100644
--- a/monet/mn-style.h
+++ b/monet/mn-style.h
@@ -77,16 +77,16 @@ gboolean mn_style_load_config    (MnStyle      *style,
                                   GError      **error);
 
 
-void mn_style_paint_widget (MnStyle   *style,
-                            MnWidget   widget,
-                            cairo_t   *cr,
-                            gdouble    x,
-                            gdouble    y,
-                            gdouble    width,
-                            gdouble    height,
-                            MnPalette *colors,
-                            MnState    state,
-                            MnFlags    flags);
+void mn_style_paint_widget (MnStyle     *style,
+                            const gchar *widget,
+                            cairo_t     *cr,
+                            gdouble      x,
+                            gdouble      y,
+                            gdouble      width,
+                            gdouble      height,
+                            MnPalette   *colors,
+                            MnState      state,
+                            MnFlags      flags);
 
 
 G_END_DECLS



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]