[metacity] theme: move integer, float and color constants to MetaThemeMetacity



commit 79fc442f5e0c267f01440fa25ce1c148415dbe32
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Thu Jan 28 13:29:59 2016 +0200

    theme: move integer, float and color constants to MetaThemeMetacity

 libmetacity/meta-theme-metacity.c |  208 +++++++++++++++++++++++++++++++++++-
 libmetacity/meta-theme-metacity.h |   28 +++++
 po/POTFILES.in                    |    1 +
 src/Makefile.am                   |    2 +-
 src/ui/theme-parser.c             |   24 ++---
 src/ui/theme-private.h            |   33 ------
 src/ui/theme.c                    |  214 +------------------------------------
 7 files changed, 251 insertions(+), 259 deletions(-)
---
diff --git a/libmetacity/meta-theme-metacity.c b/libmetacity/meta-theme-metacity.c
index baad9c2..8714035 100644
--- a/libmetacity/meta-theme-metacity.c
+++ b/libmetacity/meta-theme-metacity.c
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2001 Havoc Pennington
  * Copyright (C) 2016 Alberts Muktupāvels
  *
  * This program is free software: you can redistribute it and/or modify
@@ -17,21 +18,226 @@
 
 #include "config.h"
 
+#include <glib/gi18n-lib.h>
+
+#include "meta-theme.h"
 #include "meta-theme-metacity.h"
 
 struct _MetaThemeMetacity
 {
-  MetaThemeImpl parent;
+  MetaThemeImpl  parent;
+
+  GHashTable    *integers;
+  GHashTable    *floats;
+  GHashTable    *colors;
 };
 
 G_DEFINE_TYPE (MetaThemeMetacity, meta_theme_metacity, META_TYPE_THEME_IMPL)
 
+static gboolean
+first_uppercase (const gchar *str)
+{
+  return g_ascii_isupper (*str);
+}
+
+static void
+meta_theme_metacity_dispose (GObject *object)
+{
+  MetaThemeMetacity *metacity;
+
+  metacity = META_THEME_METACITY (object);
+
+  g_clear_pointer (&metacity->integers, g_hash_table_destroy);
+  g_clear_pointer (&metacity->floats, g_hash_table_destroy);
+  g_clear_pointer (&metacity->colors, g_hash_table_destroy);
+
+  G_OBJECT_CLASS (meta_theme_metacity_parent_class)->dispose (object);
+}
+
 static void
 meta_theme_metacity_class_init (MetaThemeMetacityClass *metacity_class)
 {
+  GObjectClass *object_class;
+
+  object_class = G_OBJECT_CLASS (metacity_class);
+
+  object_class->dispose = meta_theme_metacity_dispose;
 }
 
 static void
 meta_theme_metacity_init (MetaThemeMetacity *metacity)
 {
 }
+
+gboolean
+meta_theme_metacity_define_int (MetaThemeMetacity  *metacity,
+                                const gchar        *name,
+                                gint                value,
+                                GError            **error)
+{
+  if (metacity->integers == NULL)
+    {
+      metacity->integers = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                  g_free, NULL);
+    }
+
+  if (!first_uppercase (name))
+    {
+      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
+                   _("User-defined constants must begin with a capital letter; '%s' does not"),
+                   name);
+
+      return FALSE;
+    }
+
+  if (g_hash_table_lookup_extended (metacity->integers, name, NULL, NULL))
+    {
+      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
+                   _("Constant '%s' has already been defined"), name);
+
+      return FALSE;
+    }
+
+  g_hash_table_insert (metacity->integers, g_strdup (name),
+                       GINT_TO_POINTER (value));
+
+  return TRUE;
+}
+
+gboolean
+meta_theme_metacity_lookup_int (MetaThemeMetacity *metacity,
+                                const gchar       *name,
+                                gint              *value)
+{
+  gpointer tmp;
+
+  *value = 0;
+
+  if (metacity->integers == NULL)
+    return FALSE;
+
+  if (!g_hash_table_lookup_extended (metacity->integers, name, NULL, &tmp))
+    return FALSE;
+
+  *value = GPOINTER_TO_INT (tmp);
+
+  return TRUE;
+}
+
+gboolean
+meta_theme_metacity_define_float (MetaThemeMetacity  *metacity,
+                                  const gchar        *name,
+                                  gdouble             value,
+                                  GError            **error)
+{
+  gdouble *d;
+
+  if (metacity->floats == NULL)
+    {
+      metacity->floats = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                g_free, g_free);
+    }
+
+  if (!first_uppercase (name))
+    {
+      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
+                   _("User-defined constants must begin with a capital letter; '%s' does not"),
+                   name);
+
+      return FALSE;
+    }
+
+  if (g_hash_table_lookup_extended (metacity->floats, name, NULL, NULL))
+    {
+      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
+                   _("Constant '%s' has already been defined"), name);
+
+      return FALSE;
+    }
+
+  d = g_new (gdouble, 1);
+
+  *d = value;
+
+  g_hash_table_insert (metacity->floats, g_strdup (name), d);
+
+  return TRUE;
+}
+
+gboolean
+meta_theme_metacity_lookup_float (MetaThemeMetacity *metacity,
+                                  const gchar       *name,
+                                  gdouble           *value)
+{
+  gdouble *d;
+
+  *value = 0.0;
+
+  if (metacity->floats == NULL)
+    return FALSE;
+
+  d = g_hash_table_lookup (metacity->floats, name);
+
+  if (!d)
+    return FALSE;
+
+  *value = *d;
+
+  return TRUE;
+}
+
+gboolean
+meta_theme_metacity_define_color (MetaThemeMetacity  *metacity,
+                                  const gchar        *name,
+                                  const gchar        *value,
+                                  GError            **error)
+{
+  if (metacity->colors == NULL)
+    {
+      metacity->colors = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                g_free, g_free);
+    }
+
+  if (!first_uppercase (name))
+    {
+      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
+                   _("User-defined constants must begin with a capital letter; '%s' does not"),
+                   name);
+
+      return FALSE;
+    }
+
+  if (g_hash_table_lookup_extended (metacity->colors, name, NULL, NULL))
+    {
+      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
+                   _("Constant '%s' has already been defined"), name);
+
+      return FALSE;
+    }
+
+  g_hash_table_insert (metacity->colors, g_strdup (name), g_strdup (value));
+
+  return TRUE;
+}
+
+gboolean
+meta_theme_metacity_lookup_color (MetaThemeMetacity  *metacity,
+                                  const gchar        *name,
+                                  gchar             **value)
+{
+  gchar *result;
+
+  *value = NULL;
+
+  if (metacity->colors == NULL)
+    return FALSE;
+
+  result = g_hash_table_lookup (metacity->colors, name);
+
+  if (!result)
+    return FALSE;
+
+  *value = result;
+
+  return TRUE;
+}
diff --git a/libmetacity/meta-theme-metacity.h b/libmetacity/meta-theme-metacity.h
index d1f057d..269acf9 100644
--- a/libmetacity/meta-theme-metacity.h
+++ b/libmetacity/meta-theme-metacity.h
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2001 Havoc Pennington
  * Copyright (C) 2016 Alberts Muktupāvels
  *
  * This program is free software: you can redistribute it and/or modify
@@ -26,6 +27,33 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (MetaThemeMetacity, meta_theme_metacity,
                       META, THEME_METACITY, MetaThemeImpl)
 
+gboolean meta_theme_metacity_define_int   (MetaThemeMetacity  *metacity,
+                                           const gchar        *name,
+                                           gint                value,
+                                           GError            **error);
+
+gboolean meta_theme_metacity_lookup_int   (MetaThemeMetacity  *metacity,
+                                           const gchar        *name,
+                                           gint               *value);
+
+gboolean meta_theme_metacity_define_float (MetaThemeMetacity  *metacity,
+                                           const gchar        *name,
+                                           gdouble             value,
+                                           GError            **error);
+
+gboolean meta_theme_metacity_lookup_float (MetaThemeMetacity  *metacity,
+                                           const gchar        *name,
+                                           gdouble            *value);
+
+gboolean meta_theme_metacity_define_color (MetaThemeMetacity  *metacity,
+                                           const gchar        *name,
+                                           const gchar        *value,
+                                           GError            **error);
+
+gboolean meta_theme_metacity_lookup_color (MetaThemeMetacity  *metacity,
+                                           const gchar        *name,
+                                           gchar             **value);
+
 G_END_DECLS
 
 #endif
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 34ecd38..e56b905 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -2,6 +2,7 @@
 # Please keep this file sorted alphabetically.
 libmetacity/meta-color-spec.c
 libmetacity/meta-gradient-spec.c
+libmetacity/meta-theme-metacity.c
 src/50-metacity-navigation.xml.in
 src/50-metacity-system.xml.in
 src/50-metacity-windows.xml.in
diff --git a/src/Makefile.am b/src/Makefile.am
index b99492f..38bfa26 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -146,7 +146,7 @@ convert_DATA = metacity-schemas.convert
 bin_PROGRAMS=metacity metacity-theme-viewer
 
 metacity_LDADD= METACITY_LIBS@ libmetacity-private.la $(top_builddir)/libmetacity/libmetacity.la
-metacity_theme_viewer_LDADD= @METACITY_LIBS@ libmetacity-private.la
+metacity_theme_viewer_LDADD= @METACITY_LIBS@ libmetacity-private.la 
$(top_builddir)/libmetacity/libmetacity.la
 
 testboxes_SOURCES=include/util.h core/util.c include/boxes.h core/boxes.c core/testboxes.c
 testgradient_SOURCES=../libmetacity/meta-gradient.h ../libmetacity/meta-gradient.c ui/testgradient.c
diff --git a/src/ui/theme-parser.c b/src/ui/theme-parser.c
index e51d874..f990652 100644
--- a/src/ui/theme-parser.c
+++ b/src/ui/theme-parser.c
@@ -22,6 +22,7 @@
 #include <config.h>
 #include "theme-private.h"
 #include "util.h"
+#include <libmetacity/meta-theme-metacity.h>
 #include <string.h>
 #include <stdlib.h>
 
@@ -565,7 +566,7 @@ parse_positive_integer (const char          *str,
   /* Is str a constant? */
 
   if (META_THEME_ALLOWS (theme, META_THEME_UBIQUITOUS_CONSTANTS) &&
-      meta_theme_lookup_int_constant (theme, str, &j))
+      meta_theme_metacity_lookup_int (META_THEME_METACITY (theme->impl), str, &j))
     {
       /* Yes. */
       l = j;
@@ -813,7 +814,8 @@ parse_color (MetaTheme *theme,
   char* referent;
 
   if (META_THEME_ALLOWS (theme, META_THEME_COLOR_CONSTANTS) &&
-      meta_theme_lookup_color_constant (theme, str, &referent))
+      meta_theme_metacity_lookup_color (META_THEME_METACITY (theme->impl),
+                                        str, &referent))
     {
       if (referent)
         return meta_color_spec_new_from_string (referent, err);
@@ -906,10 +908,8 @@ parse_toplevel_element (GMarkupParseContext  *context,
               if (!parse_double (value, &dval, context, error))
                 return;
 
-              if (!meta_theme_define_float_constant (info->theme,
-                                                     name,
-                                                     dval,
-                                                     error))
+              if (!meta_theme_metacity_define_float (META_THEME_METACITY (info->theme->impl),
+                                                     name, dval, error))
                 {
                   add_context_to_error (error, context);
                   return;
@@ -920,10 +920,8 @@ parse_toplevel_element (GMarkupParseContext  *context,
               if (!parse_positive_integer (value, &ival, context, info->theme, error))
                 return;
 
-              if (!meta_theme_define_int_constant (info->theme,
-                                                   name,
-                                                   ival,
-                                                   error))
+              if (!meta_theme_metacity_define_int (META_THEME_METACITY (info->theme->impl),
+                                                   name, ival, error))
                 {
                   add_context_to_error (error, context);
                   return;
@@ -932,10 +930,8 @@ parse_toplevel_element (GMarkupParseContext  *context,
         }
       else
         {
-          if (!meta_theme_define_color_constant (info->theme,
-                                                 name,
-                                                 value,
-                                                 error))
+          if (!meta_theme_metacity_define_color (META_THEME_METACITY (info->theme->impl),
+                                                 name, value, error))
             {
               add_context_to_error (error, context);
               return;
diff --git a/src/ui/theme-private.h b/src/ui/theme-private.h
index d472d6b..fb616e6 100644
--- a/src/ui/theme-private.h
+++ b/src/ui/theme-private.h
@@ -647,16 +647,6 @@ struct _MetaTheme
 
   PangoFontDescription *titlebar_font;
 
-  /** Symbol table of integer constants. */
-  GHashTable *integer_constants;
-  /** Symbol table of float constants. */
-  GHashTable *float_constants;
-  /**
-   * Symbol table of colour constants (hex triples, and triples
-   * plus alpha).
-   * */
-  GHashTable *color_constants;
-
   GHashTable *images_by_filename;
   GHashTable *layouts_by_name;
   GHashTable *draw_op_lists_by_name;
@@ -745,29 +735,6 @@ void                   meta_theme_insert_style_set             (MetaTheme
                                                                 const char                  *name,
                                                                 MetaFrameStyleSet           *style_set);
 
-gboolean               meta_theme_define_int_constant          (MetaTheme                   *theme,
-                                                                const char                  *name,
-                                                                int                          value,
-                                                                GError                     **error);
-gboolean               meta_theme_lookup_int_constant          (MetaTheme                   *theme,
-                                                                const char                  *name,
-                                                                int                         *value);
-gboolean               meta_theme_define_float_constant        (MetaTheme                   *theme,
-                                                                const char                  *name,
-                                                                double                       value,
-                                                                GError                     **error);
-gboolean               meta_theme_lookup_float_constant        (MetaTheme                   *theme,
-                                                                const char                  *name,
-                                                                double                      *value);
-
-gboolean               meta_theme_define_color_constant        (MetaTheme                   *theme,
-                                                                const char                  *name,
-                                                                const char                  *value,
-                                                                GError                     **error);
-gboolean               meta_theme_lookup_color_constant        (MetaTheme                   *theme,
-                                                                const char                  *name,
-                                                                char                       **value);
-
 PangoFontDescription  *meta_gtk_widget_get_font_desc           (GtkWidget                   *widget,
                                                                 double                       scale,
                                                                 const PangoFontDescription  *override);
diff --git a/src/ui/theme.c b/src/ui/theme.c
index c78a085..6499b31 100644
--- a/src/ui/theme.c
+++ b/src/ui/theme.c
@@ -2212,13 +2212,15 @@ meta_theme_replace_constants (MetaTheme   *theme,
 
       if (t->type == POS_TOKEN_VARIABLE)
         {
-          if (meta_theme_lookup_int_constant (theme, t->d.v.name, &ival))
+          if (meta_theme_metacity_lookup_int (META_THEME_METACITY (theme->impl),
+                                              t->d.v.name, &ival))
             {
               g_free (t->d.v.name);
               t->type = POS_TOKEN_INT;
               t->d.i.val = ival;
             }
-          else if (meta_theme_lookup_float_constant (theme, t->d.v.name, &dval))
+          else if (meta_theme_metacity_lookup_float (META_THEME_METACITY (theme->impl),
+                                                     t->d.v.name, &dval))
             {
               g_free (t->d.v.name);
               t->type = POS_TOKEN_DOUBLE;
@@ -4979,13 +4981,6 @@ meta_theme_free (MetaTheme *theme)
   if (theme->titlebar_font)
     pango_font_description_free (theme->titlebar_font);
 
-  if (theme->integer_constants)
-    g_hash_table_destroy (theme->integer_constants);
-  if (theme->float_constants)
-    g_hash_table_destroy (theme->float_constants);
-  if (theme->color_constants)
-    g_hash_table_destroy (theme->color_constants);
-
   g_hash_table_destroy (theme->images_by_filename);
   g_hash_table_destroy (theme->layouts_by_name);
   g_hash_table_destroy (theme->draw_op_lists_by_name);
@@ -5541,207 +5536,6 @@ meta_theme_insert_style_set    (MetaTheme         *theme,
   g_hash_table_replace (theme->style_sets_by_name, g_strdup (name), style_set);
 }
 
-static gboolean
-first_uppercase (const char *str)
-{
-  return g_ascii_isupper (*str);
-}
-
-gboolean
-meta_theme_define_int_constant (MetaTheme   *theme,
-                                const char  *name,
-                                int          value,
-                                GError     **error)
-{
-  if (theme->integer_constants == NULL)
-    theme->integer_constants = g_hash_table_new_full (g_str_hash,
-                                                      g_str_equal,
-                                                      g_free,
-                                                      NULL);
-
-  if (!first_uppercase (name))
-    {
-      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
-                   _("User-defined constants must begin with a capital letter; \"%s\" does not"),
-                   name);
-      return FALSE;
-    }
-
-  if (g_hash_table_lookup_extended (theme->integer_constants, name, NULL, NULL))
-    {
-      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
-                   _("Constant \"%s\" has already been defined"),
-                   name);
-
-      return FALSE;
-    }
-
-  g_hash_table_insert (theme->integer_constants,
-                       g_strdup (name),
-                       GINT_TO_POINTER (value));
-
-  return TRUE;
-}
-
-gboolean
-meta_theme_lookup_int_constant (MetaTheme   *theme,
-                                const char  *name,
-                                int         *value)
-{
-  gpointer old_value;
-
-  *value = 0;
-
-  if (theme->integer_constants == NULL)
-    return FALSE;
-
-  if (g_hash_table_lookup_extended (theme->integer_constants,
-                                    name, NULL, &old_value))
-    {
-      *value = GPOINTER_TO_INT (old_value);
-      return TRUE;
-    }
-  else
-    {
-      return FALSE;
-    }
-}
-
-gboolean
-meta_theme_define_float_constant (MetaTheme   *theme,
-                                  const char  *name,
-                                  double       value,
-                                  GError     **error)
-{
-  double *d;
-
-  if (theme->float_constants == NULL)
-    theme->float_constants = g_hash_table_new_full (g_str_hash,
-                                                    g_str_equal,
-                                                    g_free,
-                                                    g_free);
-
-  if (!first_uppercase (name))
-    {
-      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
-                   _("User-defined constants must begin with a capital letter; \"%s\" does not"),
-                   name);
-      return FALSE;
-    }
-
-  if (g_hash_table_lookup_extended (theme->float_constants, name, NULL, NULL))
-    {
-      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
-                   _("Constant \"%s\" has already been defined"),
-                   name);
-
-      return FALSE;
-    }
-
-  d = g_new (double, 1);
-  *d = value;
-
-  g_hash_table_insert (theme->float_constants,
-                       g_strdup (name), d);
-
-  return TRUE;
-}
-
-gboolean
-meta_theme_lookup_float_constant (MetaTheme   *theme,
-                                  const char  *name,
-                                  double      *value)
-{
-  double *d;
-
-  *value = 0.0;
-
-  if (theme->float_constants == NULL)
-    return FALSE;
-
-  d = g_hash_table_lookup (theme->float_constants, name);
-
-  if (d)
-    {
-      *value = *d;
-      return TRUE;
-    }
-  else
-    {
-      return FALSE;
-    }
-}
-
-gboolean
-meta_theme_define_color_constant (MetaTheme   *theme,
-                                  const char  *name,
-                                  const char  *value,
-                                  GError     **error)
-{
-  if (theme->color_constants == NULL)
-    theme->color_constants = g_hash_table_new_full (g_str_hash,
-                                                    g_str_equal,
-                                                    g_free,
-                                                    NULL);
-
-  if (!first_uppercase (name))
-    {
-      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
-                   _("User-defined constants must begin with a capital letter; \"%s\" does not"),
-                   name);
-      return FALSE;
-    }
-
-  if (g_hash_table_lookup_extended (theme->color_constants, name, NULL, NULL))
-    {
-      g_set_error (error, META_THEME_ERROR, META_THEME_ERROR_FAILED,
-                   _("Constant \"%s\" has already been defined"),
-                   name);
-
-      return FALSE;
-    }
-
-  g_hash_table_insert (theme->color_constants,
-                       g_strdup (name),
-                       g_strdup (value));
-
-  return TRUE;
-}
-
-/**
- * Looks up a colour constant.
- *
- * \param theme  the theme containing the constant
- * \param name  the name of the constant
- * \param value  [out] the string representation of the colour, or NULL if it
- *               doesn't exist
- * \return  TRUE if it exists, FALSE otherwise
- */
-gboolean
-meta_theme_lookup_color_constant (MetaTheme   *theme,
-                                  const char  *name,
-                                  char       **value)
-{
-  char *result;
-
-  *value = NULL;
-
-  if (theme->color_constants == NULL)
-    return FALSE;
-
-  result = g_hash_table_lookup (theme->color_constants, name);
-
-  if (result)
-    {
-      *value = result;
-      return TRUE;
-    }
-  else
-    {
-      return FALSE;
-    }
-}
-
 PangoFontDescription*
 meta_gtk_widget_get_font_desc (GtkWidget *widget,
                                double     scale,


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