[gnome-games] aisleriot: Make ClutterColor editable in property editor



commit 2899cb73a8a59ccc85bd5e2350cd00896494f88a
Author: Christian Persch <chpe gnome org>
Date:   Mon Jan 4 13:36:32 2010 +0100

    aisleriot: Make ClutterColor editable in property editor
    
    Implement GdkColor support in gtk+, re-import prop-editor, and add
    ClutterColor support on top.

 aisleriot/prop-editor.c |  161 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 157 insertions(+), 4 deletions(-)
---
diff --git a/aisleriot/prop-editor.c b/aisleriot/prop-editor.c
index 4a7841c..0051824 100644
--- a/aisleriot/prop-editor.c
+++ b/aisleriot/prop-editor.c
@@ -24,6 +24,9 @@
 
 #include "prop-editor.h"
 
+#ifdef HAVE_CLUTTER
+#include <clutter/clutter.h>
+#endif
 
 typedef struct
 {
@@ -594,12 +597,14 @@ pointer_changed (GObject *object, GParamSpec *pspec, gpointer data)
 }
 
 static gchar *
-object_label (GObject *obj)
+object_label (GObject *obj, GParamSpec *pspec)
 {
   const gchar *name;
 
   if (obj)
     name = g_type_name (G_TYPE_FROM_INSTANCE (obj));
+  else if (pspec)
+    name = g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec));
   else
     name = "unknown";
   return g_strdup_printf ("Object: %p (%s)", obj, name);
@@ -618,7 +623,7 @@ object_changed (GObject *object, GParamSpec *pspec, gpointer data)
   g_object_get (object, pspec->name, &obj, NULL);
   g_list_free (children);
 
-  str = object_label (obj);
+  str = object_label (obj, pspec);
   
   gtk_label_set_text (GTK_LABEL (label), str);
   gtk_widget_set_sensitive (button, G_IS_OBJECT (obj));
@@ -655,6 +660,125 @@ object_properties (GtkWidget *button,
     create_prop_editor (obj, 0);
 }
  
+static void
+color_modified (GtkColorButton *cb, gpointer data)
+{
+  ObjectProperty *p = data;
+  GdkColor color;
+
+  gtk_color_button_get_color (cb, &color);
+
+  if (is_child_property (p->spec))
+    {
+      GtkWidget *widget = GTK_WIDGET (p->obj);
+      GtkWidget *parent = gtk_widget_get_parent (widget);
+
+      gtk_container_child_set (GTK_CONTAINER (parent),
+			       widget, p->spec->name, &color, NULL);
+    }
+  else
+    g_object_set (p->obj, p->spec->name, &color, NULL);
+}
+
+static void
+color_changed (GObject *object, GParamSpec *pspec, gpointer data)
+{
+  GtkColorButton *cb = GTK_COLOR_BUTTON (data);
+  GValue val = { 0, };
+  GdkColor *color;
+  GdkColor cb_color;
+  
+  g_value_init (&val, GDK_TYPE_COLOR);
+  get_property_value (object, pspec, &val);
+
+  color = g_value_get_boxed (&val);
+  gtk_color_button_get_color (cb, &cb_color);
+
+  if (color != NULL && !gdk_color_equal (color, &cb_color))
+    {
+      block_controller (G_OBJECT (cb));
+      gtk_color_button_set_color (cb, color);
+      unblock_controller (G_OBJECT (cb));
+    }
+
+  g_value_unset (&val);
+}
+
+#ifdef HAVE_CLUTTER
+
+static void
+_clutter_color_from_gdk_color (ClutterColor *clutter_color,
+                               const GdkColor *gdk_color)
+{
+  clutter_color->red   = gdk_color->red   >> 8;
+  clutter_color->green = gdk_color->green >> 8;
+  clutter_color->blue  = gdk_color->blue  >> 8;
+  clutter_color->alpha = 0xff;
+}
+
+static void
+_clutter_color_to_gdk_color (GdkColor *gdk_color,
+                             const ClutterColor *clutter_color)
+{
+  gdk_color->red   = clutter_color->red   << 8;
+  gdk_color->green = clutter_color->green << 8;
+  gdk_color->blue  = clutter_color->blue  << 8;
+  gdk_color->pixel = 0;
+}
+
+static void
+cluttercolor_modified (GtkColorButton *cb, gpointer data)
+{
+  ObjectProperty *p = data;
+  GdkColor color;
+  ClutterColor ccolor;
+
+  gtk_color_button_get_color (cb, &color);
+  _clutter_color_from_gdk_color (&ccolor, &color);
+
+  if (is_child_property (p->spec))
+    {
+      GtkWidget *widget = GTK_WIDGET (p->obj);
+      GtkWidget *parent = gtk_widget_get_parent (widget);
+
+      gtk_container_child_set (GTK_CONTAINER (parent),
+			       widget, p->spec->name, &ccolor, NULL);
+    }
+  else
+    g_object_set (p->obj, p->spec->name, &ccolor, NULL);
+}
+
+static void
+cluttercolor_changed (GObject *object, GParamSpec *pspec, gpointer data)
+{
+  GtkColorButton *cb = GTK_COLOR_BUTTON (data);
+  GValue val = { 0, };
+  GdkColor cb_color;
+  ClutterColor *color;
+  ClutterColor cb_ccolor;
+
+  g_assert (G_PARAM_SPEC_VALUE_TYPE (pspec) == CLUTTER_TYPE_COLOR);
+
+  g_value_init (&val, CLUTTER_TYPE_COLOR);
+  get_property_value (object, pspec, &val);
+  color = g_value_get_boxed (&val);
+
+  gtk_color_button_get_color (cb, &cb_color);
+  _clutter_color_from_gdk_color (&cb_ccolor, &cb_color);
+
+  if (color != NULL && !clutter_color_equal (color, &cb_ccolor))
+    {
+      block_controller (G_OBJECT (cb));
+      _clutter_color_to_gdk_color (&cb_color, color);
+      gtk_color_button_set_color (cb, &cb_color);
+      unblock_controller (G_OBJECT (cb));
+    }
+
+  g_value_unset (&val);
+}
+
+#endif /* HAVE_CLUTTER */
+
 static GtkWidget *
 property_widget (GObject    *object, 
 		 GParamSpec *spec, 
@@ -882,6 +1006,35 @@ property_widget (GObject    *object,
 				 G_CALLBACK (object_changed),
 				 prop_edit, G_OBJECT (label));
     }
+  else if (type == G_TYPE_PARAM_BOXED &&
+           G_PARAM_SPEC_VALUE_TYPE (spec) == GDK_TYPE_COLOR)
+    {
+      prop_edit = gtk_color_button_new ();
+      
+      g_object_connect_property (object, spec, 
+				 G_CALLBACK (color_changed),
+				 prop_edit, G_OBJECT (prop_edit));
+      
+      if (can_modify)
+	connect_controller (G_OBJECT (prop_edit), "color-set",
+			    object, spec, G_CALLBACK (color_modified));
+    }
+#ifdef HAVE_CLUTTER
+  else if ((type == G_TYPE_PARAM_BOXED &&
+            G_PARAM_SPEC_VALUE_TYPE (spec) == CLUTTER_TYPE_COLOR) ||
+           type == CLUTTER_TYPE_PARAM_COLOR)
+    {
+      prop_edit = gtk_color_button_new ();
+      
+      g_object_connect_property (object, spec, 
+				 G_CALLBACK (cluttercolor_changed),
+				 prop_edit, G_OBJECT (prop_edit));
+      
+      if (can_modify)
+	connect_controller (G_OBJECT (prop_edit), "color-set",
+			    object, spec, G_CALLBACK (cluttercolor_modified));
+    }
+#endif /* HAVE_CLUTTER */
   else
     {  
       msg = g_strdup_printf ("uneditable property type: %s",
@@ -1105,7 +1258,7 @@ children_from_object (GObject *object)
 
       prop_edit = gtk_hbox_new (FALSE, 5);
 
-      str = object_label (object);
+      str = object_label (object, NULL);
       label = gtk_label_new (str);
       g_free (str);
       button = gtk_button_new_with_label ("Properties");
@@ -1160,7 +1313,7 @@ cells_from_object (GObject *object)
 
       prop_edit = gtk_hbox_new (FALSE, 5);
 
-      str = object_label (object);
+      str = object_label (object, NULL);
       label = gtk_label_new (str);
       g_free (str);
       button = gtk_button_new_with_label ("Properties");



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