[gtk+/wip/colorchooser-v2: 39/77] Misc cleanups



commit 2981f6cd9c3b615f4995eb65539cf13238f06026
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Feb 4 00:02:49 2012 -0500

    Misc cleanups

 gtk/gtkcolorchooser.c       |   14 +-
 gtk/gtkcolorchooserwidget.c |  159 +++++++++++++-----------
 gtk/gtkcoloreditor.c        |  266 +++++++++++++++++++----------------------
 gtk/gtkcolorplane.c         |  156 +++++++++++-------------
 gtk/gtkcolorscale.c         |  278 +++++++++++++++++++++----------------------
 gtk/gtkcolorswatch.c        |  163 ++++++++++++-------------
 gtk/gtkcolorswatch.h        |    6 +
 7 files changed, 509 insertions(+), 533 deletions(-)
---
diff --git a/gtk/gtkcolorchooser.c b/gtk/gtkcolorchooser.c
index 153180b..f13e99d 100644
--- a/gtk/gtkcolorchooser.c
+++ b/gtk/gtkcolorchooser.c
@@ -114,6 +114,13 @@ gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
                   1, G_TYPE_STRING);
 }
 
+void
+_gtk_color_chooser_color_activated (GtkColorChooser *chooser,
+                                    const GdkRGBA   *color)
+{
+  g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
+}
+
 /**
  * gtk_color_chooser_get_rgba:
  * @chooser: a #GtkColorChooser
@@ -149,13 +156,6 @@ gtk_color_chooser_set_rgba (GtkColorChooser *chooser,
   GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color);
 }
 
-void
-_gtk_color_chooser_color_activated (GtkColorChooser *chooser,
-                                    const GdkRGBA   *color)
-{
-  g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
-}
-
 /**
  * gtk_color_chooser_get_use_alpha:
  * @chooser: a #GtkColorChooser
diff --git a/gtk/gtkcolorchooserwidget.c b/gtk/gtkcolorchooserwidget.c
index b35d938..44da55e 100644
--- a/gtk/gtkcolorchooserwidget.c
+++ b/gtk/gtkcolorchooserwidget.c
@@ -24,7 +24,6 @@
 #include "gtkcolorchooserwidget.h"
 #include "gtkcoloreditor.h"
 #include "gtkcolorswatch.h"
-#include "gtkcolorutils.h"
 #include "gtkbox.h"
 #include "gtkgrid.h"
 #include "gtklabel.h"
@@ -64,6 +63,7 @@ struct _GtkColorChooserWidgetPrivate
 {
   GtkWidget *palette;
   GtkWidget *editor;
+  GtkSizeGroup *size_group;
 
   GtkWidget *colors;
   GtkWidget *grays;
@@ -71,9 +71,8 @@ struct _GtkColorChooserWidgetPrivate
 
   GtkWidget *button;
   GtkColorSwatch *current;
-  gboolean use_alpha;
 
-  GtkSizeGroup *size_group;
+  gboolean use_alpha;
 
   GSettings *settings;
 };
@@ -100,10 +99,12 @@ select_swatch (GtkColorChooserWidget *cc,
 
   if (cc->priv->current == swatch)
     return;
+
   if (cc->priv->current != NULL)
     gtk_color_swatch_set_selected (cc->priv->current, FALSE);
   gtk_color_swatch_set_selected (swatch, TRUE);
   cc->priv->current = swatch;
+
   gtk_color_swatch_get_rgba (swatch, &color);
   g_settings_set (cc->priv->settings, "selected-color", "(bdddd)",
                   TRUE, color.red, color.green, color.blue, color.alpha);
@@ -111,26 +112,6 @@ select_swatch (GtkColorChooserWidget *cc,
   g_object_notify (G_OBJECT (cc), "rgba");
 }
 
-static void save_custom_colors (GtkColorChooserWidget *cc);
-
-static void
-button_activate (GtkColorSwatch        *swatch,
-                 GtkColorChooserWidget *cc)
-{
-  GdkRGBA color;
-
-  /* somewhat random, makes the hairline nicely visible */
-  color.red = 0.75;
-  color.green = 0.25;
-  color.blue = 0.25;
-  color.alpha = 1.0;
-
-  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc->priv->editor), &color);
-
-  gtk_widget_hide (cc->priv->palette);
-  gtk_widget_show (cc->priv->editor);
-}
-
 static void
 swatch_activate (GtkColorSwatch        *swatch,
                  GtkColorChooserWidget *cc)
@@ -163,7 +144,8 @@ swatch_selected (GtkColorSwatch        *swatch,
 }
 
 static void
-connect_swatch_signals (GtkWidget *p, gpointer data)
+connect_swatch_signals (GtkWidget *p,
+                        gpointer   data)
 {
   g_signal_connect (p, "activate", G_CALLBACK (swatch_activate), data);
   g_signal_connect (p, "customize", G_CALLBACK (swatch_customize), data);
@@ -171,17 +153,23 @@ connect_swatch_signals (GtkWidget *p, gpointer data)
 }
 
 static void
-connect_button_signals (GtkWidget *p, gpointer data)
+button_activate (GtkColorSwatch        *swatch,
+                 GtkColorChooserWidget *cc)
 {
-  g_signal_connect (p, "activate", G_CALLBACK (button_activate), data);
+  /* somewhat random, makes the hairline nicely visible */
+  GdkRGBA color = { 0.75, 0.25, 0.25, 1.0 };
+
+  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc->priv->editor), &color);
+
+  gtk_widget_hide (cc->priv->palette);
+  gtk_widget_show (cc->priv->editor);
 }
 
 static void
-connect_custom_signals (GtkWidget *p, gpointer data)
+connect_button_signals (GtkWidget *p,
+                        gpointer   data)
 {
-  connect_swatch_signals (p, data);
-  g_signal_connect_swapped (p, "notify::color",
-                            G_CALLBACK (save_custom_colors), data);
+  g_signal_connect (p, "activate", G_CALLBACK (button_activate), data);
 }
 
 static void
@@ -200,10 +188,8 @@ save_custom_colors (GtkColorChooserWidget *cc)
     {
       i++;
       if (gtk_color_swatch_get_rgba (GTK_COLOR_SWATCH (child), &color))
-        {
-          g_variant_builder_add (&builder, "(dddd)",
-                                 color.red, color.green, color.blue, color.alpha);
-       }
+        g_variant_builder_add (&builder, "(dddd)",
+                               color.red, color.green, color.blue, color.alpha);
     }
 
   variant = g_variant_builder_end (&builder);
@@ -211,6 +197,64 @@ save_custom_colors (GtkColorChooserWidget *cc)
 }
 
 static void
+connect_custom_signals (GtkWidget *p,
+                        gpointer   data)
+{
+  connect_swatch_signals (p, data);
+  g_signal_connect_swapped (p, "notify::rgba",
+                            G_CALLBACK (save_custom_colors), data);
+}
+
+static void
+gtk_color_chooser_widget_set_use_alpha (GtkColorChooserWidget *cc,
+                                        gboolean               use_alpha)
+{
+  GtkWidget *grids[3];
+  gint i;
+  GList *children, *l;
+  GtkWidget *swatch;
+
+  cc->priv->use_alpha = use_alpha;
+  gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (cc->priv->editor), use_alpha);
+
+  grids[0] = cc->priv->colors;
+  grids[1] = cc->priv->grays;
+  grids[2] = cc->priv->custom;
+
+  for (i = 0; i < 3; i++)
+    {
+      children = gtk_container_get_children (GTK_CONTAINER (grids[i]));
+      for (l = children; l; l = l->next)
+        {
+          swatch = l->data;
+          gtk_color_swatch_set_use_alpha (GTK_COLOR_SWATCH (swatch), use_alpha);
+        }
+      g_list_free (children);
+    }
+
+  gtk_widget_queue_draw (GTK_WIDGET (cc));
+}
+
+static void
+gtk_color_chooser_widget_set_show_editor (GtkColorChooserWidget *cc,
+                                          gboolean               show_editor)
+{
+  if (show_editor)
+    {
+      GdkRGBA color = { 0.75, 0.25, 0.25, 1.0 };
+
+      if (cc->priv->current)
+        gtk_color_swatch_get_rgba (cc->priv->current, &color);
+      gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc->priv->editor), &color);
+    }
+
+  gtk_widget_set_visible (cc->priv->editor, show_editor);
+  gtk_widget_set_visible (cc->priv->palette, !show_editor);
+}
+
+/* UI construction {{{1 */
+
+static void
 gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
 {
   GtkWidget *grid;
@@ -290,7 +334,6 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
   for (i = 0; i < 9; i++)
     {
        gdk_rgba_parse (&color, default_grayscale[i]);
-       color.alpha = 1.0;
 
        p = gtk_color_swatch_new ();
        connect_swatch_signals (p, cc);
@@ -378,6 +421,8 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
   gtk_size_group_add_widget (cc->priv->size_group, alignment);
 }
 
+/* GObject implementation {{{1 */
+
 static void
 gtk_color_chooser_widget_get_property (GObject    *object,
                                        guint       prop_id,
@@ -410,44 +455,6 @@ gtk_color_chooser_widget_get_property (GObject    *object,
 }
 
 static void
-gtk_color_chooser_widget_set_use_alpha (GtkColorChooserWidget *cc,
-                                        gboolean               use_alpha)
-{
-  GtkWidget *grids[3];
-  gint i;
-  GList *children, *l;
-  GtkWidget *swatch;
-
-  cc->priv->use_alpha = use_alpha;
-  gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (cc->priv->editor), use_alpha);
-
-  grids[0] = cc->priv->colors;
-  grids[1] = cc->priv->grays;
-  grids[2] = cc->priv->custom;
-
-  for (i = 0; i < 3; i++)
-    {
-      children = gtk_container_get_children (GTK_CONTAINER (grids[i]));
-      for (l = children; l; l = l->next)
-        {
-          swatch = l->data;
-          gtk_color_swatch_set_use_alpha (GTK_COLOR_SWATCH (swatch), use_alpha);
-        }
-      g_list_free (children);
-    }
-
-  gtk_widget_queue_draw (GTK_WIDGET (cc));
-}
-
-static void
-gtk_color_chooser_widget_set_show_editor (GtkColorChooserWidget *cc,
-                                          gboolean               show_editor)
-{
-  gtk_widget_set_visible (cc->priv->editor, show_editor);
-  gtk_widget_set_visible (cc->priv->palette, !show_editor);
-}
-
-static void
 gtk_color_chooser_widget_set_property (GObject      *object,
                                        guint         prop_id,
                                        const GValue *value,
@@ -514,6 +521,8 @@ gtk_color_chooser_widget_class_init (GtkColorChooserWidgetClass *class)
   g_type_class_add_private (object_class, sizeof (GtkColorChooserWidgetPrivate));
 }
 
+/* GtkColorChooser implementation {{{1 */
+
 static void
 gtk_color_chooser_widget_get_rgba (GtkColorChooser *chooser,
                                    GdkRGBA         *color)
@@ -615,6 +624,8 @@ gtk_color_chooser_widget_iface_init (GtkColorChooserInterface *iface)
   iface->set_rgba = gtk_color_chooser_widget_set_rgba;
 }
 
+/* Public API {{{1 */
+
 /**
  * gtk_color_chooser_widget_new:
  *
@@ -629,3 +640,5 @@ gtk_color_chooser_widget_new (void)
 {
   return g_object_new (GTK_TYPE_COLOR_CHOOSER_WIDGET, NULL);
 }
+
+/* vim:set foldmethod=marker: */
diff --git a/gtk/gtkcoloreditor.c b/gtk/gtkcoloreditor.c
index a34ee36..8a9c21c 100644
--- a/gtk/gtkcoloreditor.c
+++ b/gtk/gtkcoloreditor.c
@@ -30,16 +30,15 @@
 
 #include "config.h"
 
-#include "gtkcolorchooserprivate.h"
 #include "gtkcoloreditor.h"
+
+#include "gtkcolorchooserprivate.h"
 #include "gtkcolorplane.h"
 #include "gtkcolorscale.h"
 #include "gtkcolorswatch.h"
 #include "gtkcolorutils.h"
 #include "gtkgrid.h"
 #include "gtkorientable.h"
-#include "gtkaspectframe.h"
-#include "gtkdrawingarea.h"
 #include "gtkentry.h"
 #include "gtkoverlay.h"
 #include "gtkadjustment.h"
@@ -75,7 +74,7 @@ struct _GtkColorEditorPrivate
   GtkAdjustment *a_adj;
 
   guint text_changed : 1;
-  guint use_alpha   : 1;
+  guint use_alpha    : 1;
 };
 
 enum
@@ -101,21 +100,15 @@ scale_round (gdouble value, gdouble scale)
 }
 
 static void
-update_entry (GtkColorEditor *editor)
+entry_set_rgba (GtkColorEditor *editor,
+                const GdkRGBA  *color)
 {
   gchar *text;
-  gdouble h, s, v;
-  gdouble r, g, b;
-
-  h = gtk_adjustment_get_value (editor->priv->h_adj);
-  s = gtk_adjustment_get_value (editor->priv->s_adj);
-  v = gtk_adjustment_get_value (editor->priv->v_adj);
-  gtk_hsv_to_rgb (h, s, v, &r, &g, &b);
 
   text = g_strdup_printf ("#%02X%02X%02X",
-                          scale_round (r, 255),
-                          scale_round (g, 255),
-                          scale_round (b, 255));
+                          scale_round (color->red, 255),
+                          scale_round (color->green, 255),
+                          scale_round (color->blue, 255));
   gtk_entry_set_text (GTK_ENTRY (editor->priv->entry), text);
   editor->priv->text_changed = FALSE;
   g_free (text);
@@ -164,16 +157,20 @@ static void
 hsv_changed (GtkColorEditor *editor)
 {
   GdkRGBA color;
-  gdouble h, s, v;
+  gdouble h, s, v, a;
 
   h = gtk_adjustment_get_value (editor->priv->h_adj);
   s = gtk_adjustment_get_value (editor->priv->s_adj);
   v = gtk_adjustment_get_value (editor->priv->v_adj);
+  a = gtk_adjustment_get_value (editor->priv->a_adj);
+
   gtk_hsv_to_rgb (h, s, v, &color.red, &color.green, &color.blue);
-  color.alpha = gtk_adjustment_get_value (editor->priv->a_adj);
-  update_entry (editor);
+  color.alpha = a;
+
   gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (editor->priv->swatch), &color);
   gtk_color_scale_set_rgba (GTK_COLOR_SCALE (editor->priv->a_slider), &color);
+  entry_set_rgba (editor, &color);
+
   g_object_notify (G_OBJECT (editor), "rgba");
 }
 
@@ -300,7 +297,8 @@ get_child_position (GtkOverlay     *overlay,
 }
 
 static void
-value_changed (GtkAdjustment *a, GtkAdjustment *as)
+value_changed (GtkAdjustment *a,
+               GtkAdjustment *as)
 {
   gdouble scale;
 
@@ -311,7 +309,8 @@ value_changed (GtkAdjustment *a, GtkAdjustment *as)
 }
 
 static GtkAdjustment *
-scaled_adjustment (GtkAdjustment *a, gdouble scale)
+scaled_adjustment (GtkAdjustment *a,
+                   gdouble        scale)
 {
   GtkAdjustment *as;
 
@@ -329,7 +328,9 @@ scaled_adjustment (GtkAdjustment *a, gdouble scale)
 }
 
 static gboolean
-popup_draw (GtkWidget *popup, cairo_t *cr, GtkColorEditor *editor)
+popup_draw (GtkWidget      *popup,
+            cairo_t        *cr,
+            GtkColorEditor *editor)
 {
   GtkStyleContext *context;
   gint width, height;
@@ -344,14 +345,40 @@ popup_draw (GtkWidget *popup, cairo_t *cr, GtkColorEditor *editor)
   return FALSE;
 }
 
+static GtkWidget *
+create_popup (GtkColorEditor *editor,
+              GtkWidget      *attach,
+              GtkWidget      *contents)
+{
+  GtkWidget *popup;
+
+  popup = gtk_alignment_new (0.5, 0.5, 0, 0);
+  gtk_alignment_set_padding (GTK_ALIGNMENT (popup), 12, 12, 12, 12);
+  gtk_style_context_add_class (gtk_widget_get_style_context (popup), GTK_STYLE_CLASS_TOOLTIP);
+
+  gtk_container_add (GTK_CONTAINER (popup), contents);
+
+  gtk_widget_show_all (popup);
+  gtk_widget_hide (popup);
+  gtk_widget_set_no_show_all (popup, TRUE);
+
+  g_signal_connect (popup, "draw", G_CALLBACK (popup_draw), editor);
+
+  gtk_overlay_add_overlay (GTK_OVERLAY (editor->priv->overlay), popup);
+  g_signal_connect (attach, "popup-menu", G_CALLBACK (popup_edit), editor);
+
+  return popup;
+}
+
 static void
 gtk_color_editor_init (GtkColorEditor *editor)
 {
   GtkWidget *grid;
-  GtkWidget *box;
   GtkWidget *slider;
   GtkWidget *alignment;
-  GtkAdjustment *adj;
+  GtkWidget *entry;
+  GtkWidget *swatch;
+  GtkAdjustment *h_adj, *s_adj, *v_adj, *a_adj;
   GdkRGBA transparent = { 0, 0, 0, 0 };
 
   editor->priv = G_TYPE_INSTANCE_GET_PRIVATE (editor,
@@ -359,60 +386,53 @@ gtk_color_editor_init (GtkColorEditor *editor)
                                               GtkColorEditorPrivate);
   editor->priv->use_alpha = TRUE;
 
-  editor->priv->h_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
-  editor->priv->s_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
-  editor->priv->v_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
-  editor->priv->a_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
-  g_object_ref_sink (editor->priv->h_adj);
-  g_object_ref_sink (editor->priv->s_adj);
-  g_object_ref_sink (editor->priv->v_adj);
-  g_object_ref_sink (editor->priv->a_adj);
-  g_signal_connect_swapped (editor->priv->h_adj, "value-changed",
-                            G_CALLBACK (hsv_changed), editor);
-  g_signal_connect_swapped (editor->priv->s_adj, "value-changed",
-                            G_CALLBACK (hsv_changed), editor);
-  g_signal_connect_swapped (editor->priv->a_adj, "value-changed",
-                            G_CALLBACK (hsv_changed), editor);
-  g_signal_connect_swapped (editor->priv->v_adj, "value-changed",
-                            G_CALLBACK (hsv_changed), editor);
+  editor->priv->h_adj = h_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
+  editor->priv->s_adj = s_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
+  editor->priv->v_adj = v_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
+  editor->priv->a_adj = a_adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
 
-  gtk_widget_push_composite_child ();
+  g_object_ref_sink (h_adj);
+  g_object_ref_sink (s_adj);
+  g_object_ref_sink (v_adj);
+  g_object_ref_sink (a_adj);
 
-  editor->priv->overlay = gtk_overlay_new ();
-  gtk_widget_override_background_color (editor->priv->overlay, 0, &transparent);
-  editor->priv->grid = grid = gtk_grid_new ();
-  gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
-  gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
+  g_signal_connect_swapped (h_adj, "value-changed", G_CALLBACK (hsv_changed), editor);
+  g_signal_connect_swapped (s_adj, "value-changed", G_CALLBACK (hsv_changed), editor);
+  g_signal_connect_swapped (v_adj, "value-changed", G_CALLBACK (hsv_changed), editor);
+  g_signal_connect_swapped (a_adj, "value-changed", G_CALLBACK (hsv_changed), editor);
+
+  gtk_widget_push_composite_child ();
 
-  editor->priv->swatch = gtk_color_swatch_new ();
-  gtk_widget_set_sensitive (editor->priv->swatch, FALSE);
-  gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (editor->priv->swatch), 2, 2, 2, 2);
+  /* Construct the main UI */
+  editor->priv->swatch = swatch = gtk_color_swatch_new ();
+  gtk_widget_set_sensitive (swatch, FALSE);
+  gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (swatch), 2, 2, 2, 2);
 
-  editor->priv->entry = gtk_entry_new ();
-  g_signal_connect (editor->priv->entry, "activate",
-                    G_CALLBACK (entry_apply), editor);
-  g_signal_connect (editor->priv->entry, "notify::text",
-                    G_CALLBACK (entry_text_changed), editor);
-  g_signal_connect (editor->priv->entry, "focus-out-event",
-                    G_CALLBACK (entry_focus_out), editor);
+  editor->priv->entry = entry = gtk_entry_new ();
+  g_signal_connect (entry, "activate", G_CALLBACK (entry_apply), editor);
+  g_signal_connect (entry, "notify::text", G_CALLBACK (entry_text_changed), editor);
+  g_signal_connect (entry, "focus-out-event", G_CALLBACK (entry_focus_out), editor);
 
-  editor->priv->h_slider = slider = gtk_color_scale_new (editor->priv->h_adj,
-                                                         GTK_COLOR_SCALE_HUE);
+  editor->priv->h_slider = slider = gtk_color_scale_new (h_adj, GTK_COLOR_SCALE_HUE);
   gtk_orientable_set_orientation (GTK_ORIENTABLE (slider), GTK_ORIENTATION_VERTICAL);
   if (gtk_widget_get_direction (slider) == GTK_TEXT_DIR_RTL)
-    gtk_style_context_add_class (gtk_widget_get_style_context (slider), GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
+    gtk_style_context_add_class (gtk_widget_get_style_context (slider),
+                                 GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
   else
-    gtk_style_context_add_class (gtk_widget_get_style_context (slider), GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
+    gtk_style_context_add_class (gtk_widget_get_style_context (slider),
+                                 GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
+
+  editor->priv->sv_plane = gtk_color_plane_new (h_adj, s_adj, v_adj);
+  gtk_widget_set_size_request (editor->priv->sv_plane, 300, 300);
 
-  editor->priv->a_slider = slider = gtk_color_scale_new (editor->priv->a_adj,
-                                                         GTK_COLOR_SCALE_ALPHA);
+  editor->priv->a_slider = slider = gtk_color_scale_new (a_adj, GTK_COLOR_SCALE_ALPHA);
   gtk_orientable_set_orientation (GTK_ORIENTABLE (slider), GTK_ORIENTATION_HORIZONTAL);
-  gtk_style_context_add_class (gtk_widget_get_style_context (slider), GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
+  gtk_style_context_add_class (gtk_widget_get_style_context (slider),
+                               GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
 
-  editor->priv->sv_plane = gtk_color_plane_new (editor->priv->h_adj,
-                                                editor->priv->s_adj,
-                                                editor->priv->v_adj);
-  gtk_widget_set_size_request (editor->priv->sv_plane, 300, 300);
+  editor->priv->grid = grid = gtk_grid_new ();
+  gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
+  gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
 
   gtk_grid_attach (GTK_GRID (grid), editor->priv->swatch,   1, 0, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), editor->priv->entry,    2, 0, 1, 1);
@@ -425,96 +445,60 @@ gtk_color_editor_init (GtkColorEditor *editor)
    */
   alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 30, 30);
-  gtk_container_add (GTK_CONTAINER (editor->priv->overlay), alignment);
   gtk_container_add (GTK_CONTAINER (alignment), grid);
 
-  editor->priv->sv_popup = gtk_alignment_new (0.5, 0.5, 0, 0);
-  gtk_style_context_add_class (gtk_widget_get_style_context (editor->priv->sv_popup),
-                               GTK_STYLE_CLASS_TOOLTIP);
-  gtk_alignment_set_padding (GTK_ALIGNMENT (editor->priv->sv_popup), 12, 12, 12, 12);
+  editor->priv->overlay = gtk_overlay_new ();
+  gtk_widget_override_background_color (editor->priv->overlay, 0, &transparent);
+  gtk_container_add (GTK_CONTAINER (editor->priv->overlay), alignment);
+
+  /* Construct the sv popup */
+  editor->priv->s_entry = entry = gtk_spin_button_new (scaled_adjustment (s_adj, 100), 1, 0);
+  g_signal_connect (entry, "key-press-event", G_CALLBACK (popup_key_press), editor);
+
+  editor->priv->v_entry = entry = gtk_spin_button_new (scaled_adjustment (v_adj, 100), 1, 0);
+  g_signal_connect (entry, "key-press-event", G_CALLBACK (popup_key_press), editor);
+
   grid = gtk_grid_new ();
   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
-  adj = scaled_adjustment (editor->priv->s_adj, 100);
-  editor->priv->s_entry = gtk_spin_button_new (adj, 1, 0);
-  g_signal_connect (editor->priv->s_entry, "key-press-event",
-                    G_CALLBACK (popup_key_press), editor);
 
-  adj = scaled_adjustment (editor->priv->v_adj, 100);
-  editor->priv->v_entry = gtk_spin_button_new (adj, 1, 0);
-  g_signal_connect (editor->priv->v_entry, "key-press-event",
-                    G_CALLBACK (popup_key_press), editor);
-
-  gtk_container_add (GTK_CONTAINER (editor->priv->sv_popup), grid);
   gtk_grid_attach (GTK_GRID (grid), gtk_label_new (C_("Color channel", "S")), 0, 0, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), editor->priv->s_entry, 1, 0, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), gtk_label_new (C_("Color channel", "V")), 0, 1, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), editor->priv->v_entry, 1, 1, 1, 1);
-  gtk_widget_show_all (editor->priv->sv_popup);
-  gtk_widget_hide (editor->priv->sv_popup);
-  gtk_widget_set_no_show_all (editor->priv->sv_popup, TRUE);
-
-  gtk_overlay_add_overlay (GTK_OVERLAY (editor->priv->overlay), editor->priv->sv_popup);
-
-  g_signal_connect (editor->priv->sv_popup, "draw",
-                    G_CALLBACK (popup_draw), editor);
-  g_signal_connect (editor->priv->sv_plane, "popup-menu",
-                    G_CALLBACK (popup_edit), editor);
-
-  editor->priv->h_popup = gtk_alignment_new (0.5, 0.5, 0, 0);
-  gtk_style_context_add_class (gtk_widget_get_style_context (editor->priv->h_popup),
-                               GTK_STYLE_CLASS_TOOLTIP);
-  gtk_alignment_set_padding (GTK_ALIGNMENT (editor->priv->h_popup), 12, 12, 12, 12);
-  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
-  adj = scaled_adjustment (editor->priv->h_adj, 100);
-  editor->priv->h_entry = gtk_spin_button_new (adj, 1, 0);
-  g_signal_connect (editor->priv->h_entry, "key-press-event",
-                    G_CALLBACK (popup_key_press), editor);
-  gtk_container_add (GTK_CONTAINER (editor->priv->h_popup), box);
-  gtk_container_add (GTK_CONTAINER (box), gtk_label_new (C_("Color channel", "H")));
-  gtk_container_add (GTK_CONTAINER (box), editor->priv->h_entry);
-  gtk_widget_show_all (editor->priv->h_popup);
-  gtk_widget_hide (editor->priv->h_popup);
-  gtk_widget_set_no_show_all (editor->priv->h_popup, TRUE);
-
-  gtk_overlay_add_overlay (GTK_OVERLAY (editor->priv->overlay), editor->priv->h_popup);
-
-  g_signal_connect (editor->priv->h_popup, "draw",
-                    G_CALLBACK (popup_draw), editor);
-  g_signal_connect (editor->priv->h_slider, "popup-menu",
-                    G_CALLBACK (popup_edit), editor);
-
-  editor->priv->a_popup = gtk_alignment_new (0.5, 0.5, 0, 0);
-  gtk_style_context_add_class (gtk_widget_get_style_context (editor->priv->a_popup),
-                               GTK_STYLE_CLASS_TOOLTIP);
-  gtk_alignment_set_padding (GTK_ALIGNMENT (editor->priv->a_popup), 12, 12, 12, 12);
+
+  editor->priv->sv_popup = create_popup (editor, editor->priv->sv_plane, grid);
+
+  /* Construct the h popup */
+  editor->priv->h_entry = entry = gtk_spin_button_new (scaled_adjustment (h_adj, 100), 1, 0);
+  g_signal_connect (entry, "key-press-event", G_CALLBACK (popup_key_press), editor);
+
+  grid = gtk_grid_new ();
+  gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
+
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (C_("Color channel", "H")), 0, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), editor->priv->h_entry, 1, 0, 1, 1);
+
+  editor->priv->h_popup = create_popup (editor, editor->priv->h_slider, grid);
+
+  /* Construct the a popup */
+  editor->priv->a_entry = entry = gtk_spin_button_new (scaled_adjustment (a_adj, 100), 1, 0);
+  g_signal_connect (entry, "key-press-event", G_CALLBACK (popup_key_press), editor);
+
   grid = gtk_grid_new ();
   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
-  adj = scaled_adjustment (editor->priv->a_adj, 100);
-  editor->priv->a_entry = gtk_spin_button_new (adj, 1, 0);
-  g_signal_connect (editor->priv->a_entry, "key-press-event",
-                    G_CALLBACK (popup_key_press), editor);
-  gtk_container_add (GTK_CONTAINER (editor->priv->a_popup), grid);
+
   gtk_grid_attach (GTK_GRID (grid), gtk_label_new (C_("Color channel", "A")), 0, 0, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), editor->priv->a_entry, 1, 0, 1, 1);
-  gtk_widget_show_all (editor->priv->a_popup);
-  gtk_widget_hide (editor->priv->a_popup);
-  gtk_widget_set_no_show_all (editor->priv->a_popup, TRUE);
-
-  gtk_overlay_add_overlay (GTK_OVERLAY (editor->priv->overlay), editor->priv->a_popup);
 
-  g_signal_connect (editor->priv->a_popup, "draw",
-                    G_CALLBACK (popup_draw), editor);
-  g_signal_connect (editor->priv->a_slider, "popup-menu",
-                    G_CALLBACK (popup_edit), editor);
+  editor->priv->a_popup = create_popup (editor, editor->priv->a_slider, grid);
 
-  g_signal_connect (editor->priv->overlay, "get-child-position",
-                    G_CALLBACK (get_child_position), editor);
-  g_signal_connect (editor, "notify::visible",
-                    G_CALLBACK (dismiss_current_popup), NULL);
+  /* Hook up popup positioning */
+  g_signal_connect (editor->priv->overlay, "get-child-position", G_CALLBACK (get_child_position), editor);
+  g_signal_connect (editor, "notify::visible", G_CALLBACK (dismiss_current_popup), NULL);
 
   gtk_widget_show_all (editor->priv->overlay);
-
   gtk_container_add (GTK_CONTAINER (editor), editor->priv->overlay);
+
   gtk_widget_pop_composite_child ();
 }
 
@@ -635,12 +619,10 @@ gtk_color_editor_set_rgba (GtkColorChooser *chooser,
   gtk_adjustment_set_value (editor->priv->s_adj, s);
   gtk_adjustment_set_value (editor->priv->v_adj, v);
   gtk_adjustment_set_value (editor->priv->a_adj, color->alpha);
+
   gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (editor->priv->swatch), color);
   gtk_color_scale_set_rgba (GTK_COLOR_SCALE (editor->priv->a_slider), color);
-
-  update_entry (editor);
-
-  gtk_widget_queue_draw (GTK_WIDGET (editor));
+  entry_set_rgba (editor, color);
 
   g_object_notify (G_OBJECT (editor), "rgba");
 }
diff --git a/gtk/gtkcolorplane.c b/gtk/gtkcolorplane.c
index 2e7b2a0..ab50610 100644
--- a/gtk/gtkcolorplane.c
+++ b/gtk/gtkcolorplane.c
@@ -21,10 +21,7 @@
 
 #include "gtkcolorplane.h"
 #include "gtkcolorutils.h"
-#include "gtkcontainer.h"
 #include "gtkaccessible.h"
-#include "gtkwindow.h"
-#include "gtkbutton.h"
 #include "gtkintl.h"
 
 struct _GtkColorPlanePrivate
@@ -34,15 +31,32 @@ struct _GtkColorPlanePrivate
   GtkAdjustment *v_adj;
 
   cairo_surface_t *surface;
-  gint x, y;
   gboolean in_drag;
 };
 
 G_DEFINE_TYPE (GtkColorPlane, gtk_color_plane, GTK_TYPE_DRAWING_AREA)
 
+static void
+sv_to_xy (GtkColorPlane *plane,
+          gint          *x,
+          gint          *y)
+{
+  gdouble s, v;
+  gint width, height;
+
+  width = gtk_widget_get_allocated_width (GTK_WIDGET (plane));
+  height = gtk_widget_get_allocated_height (GTK_WIDGET (plane));
+
+  s = gtk_adjustment_get_value (plane->priv->s_adj);
+  v = gtk_adjustment_get_value (plane->priv->v_adj);
+
+  *x = CLAMP (width * v, 0, width - 1);
+  *y = CLAMP (height * (1 - s), 0, height - 1);
+}
+
 static gboolean
-sv_draw (GtkWidget *widget,
-         cairo_t   *cr)
+plane_draw (GtkWidget *widget,
+            cairo_t   *cr)
 {
   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
   gint x, y;
@@ -51,8 +65,7 @@ sv_draw (GtkWidget *widget,
   cairo_set_source_surface (cr, plane->priv->surface, 0, 0);
   cairo_paint (cr);
 
-  x = plane->priv->x;
-  y = plane->priv->y;
+  sv_to_xy (plane, &x, &y);
   width = gtk_widget_get_allocated_width (widget);
   height = gtk_widget_get_allocated_height (widget);
 
@@ -83,7 +96,7 @@ sv_draw (GtkWidget *widget,
 }
 
 static void
-create_sv_surface (GtkColorPlane *plane)
+create_surface (GtkColorPlane *plane)
 {
   GtkWidget *widget = GTK_WIDGET (plane);
   cairo_t *cr;
@@ -148,30 +161,11 @@ create_sv_surface (GtkColorPlane *plane)
   g_free (data);
 }
 
-static void
-hsv_to_xy (GtkColorPlane *plane)
-{
-  gdouble s, v;
-  gint width, height;
-
-  width = gtk_widget_get_allocated_width (GTK_WIDGET (plane));
-  height = gtk_widget_get_allocated_height (GTK_WIDGET (plane));
-
-  s = gtk_adjustment_get_value (plane->priv->s_adj);
-  v = gtk_adjustment_get_value (plane->priv->v_adj);
-  plane->priv->x = CLAMP (width * v, 0, width - 1);
-  plane->priv->y = CLAMP (height * (1 - s), 0, height - 1);
-}
-
 static gboolean
-sv_configure (GtkWidget         *widget,
-              GdkEventConfigure *event)
+plane_configure (GtkWidget         *widget,
+                 GdkEventConfigure *event)
 {
-  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
-
-  create_sv_surface (plane);
-  hsv_to_xy (plane);
-
+  create_surface (GTK_COLOR_PLANE (widget));
   return TRUE;
 }
 
@@ -197,37 +191,45 @@ set_cross_grab (GtkWidget *widget,
 }
 
 static gboolean
-sv_grab_broken (GtkWidget          *widget,
-                GdkEventGrabBroken *event)
+plane_grab_broken (GtkWidget          *widget,
+                   GdkEventGrabBroken *event)
 {
-  GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
+  GTK_COLOR_PLANE (widget)->priv->in_drag = FALSE;
+  return TRUE;
+}
 
-  plane->priv->in_drag = FALSE;
+static void
+h_changed (GtkColorPlane *plane)
+{
+  create_surface (plane);
+  gtk_widget_queue_draw (GTK_WIDGET (plane));
+}
 
-  return TRUE;
+static void
+sv_changed (GtkColorPlane *plane)
+{
+  gtk_widget_queue_draw (GTK_WIDGET (plane));
 }
 
 static void
-sv_update_color (GtkColorPlane *plane,
-                 gint           x,
-                 gint           y)
+update_color (GtkColorPlane *plane,
+              gint           x,
+              gint           y)
 {
   GtkWidget *widget = GTK_WIDGET (plane);
   gdouble s, v;
 
-  plane->priv->x = x;
-  plane->priv->y = y;
-
   s = CLAMP (1 - y * (1.0 / gtk_widget_get_allocated_height (widget)), 0, 1);
   v = CLAMP (x * (1.0 / gtk_widget_get_allocated_width (widget)), 0, 1);
   gtk_adjustment_set_value (plane->priv->s_adj, s);
   gtk_adjustment_set_value (plane->priv->v_adj, v);
+
   gtk_widget_queue_draw (widget);
 }
 
 static gboolean
-sv_button_press (GtkWidget      *widget,
-                 GdkEventButton *event)
+plane_button_press (GtkWidget      *widget,
+                    GdkEventButton *event)
 {
   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
 
@@ -245,15 +247,15 @@ sv_button_press (GtkWidget      *widget,
 
   plane->priv->in_drag = TRUE;
   set_cross_grab (widget, gdk_event_get_device ((GdkEvent*)event), event->time);
-  sv_update_color (plane, event->x, event->y);
+  update_color (plane, event->x, event->y);
   gtk_widget_grab_focus (widget);
 
   return TRUE;
 }
 
 static gboolean
-sv_button_release (GtkWidget      *widget,
-                   GdkEventButton *event)
+plane_button_release (GtkWidget      *widget,
+                      GdkEventButton *event)
 {
   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
 
@@ -262,15 +264,15 @@ sv_button_release (GtkWidget      *widget,
 
   plane->priv->in_drag = FALSE;
 
-  sv_update_color (plane, event->x, event->y);
+  update_color (plane, event->x, event->y);
   gdk_device_ungrab (gdk_event_get_device ((GdkEvent *) event), event->time);
 
   return TRUE;
 }
 
 static gboolean
-sv_motion (GtkWidget      *widget,
-           GdkEventMotion *event)
+plane_motion_notify (GtkWidget      *widget,
+                     GdkEventMotion *event)
 {
   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
 
@@ -278,26 +280,12 @@ sv_motion (GtkWidget      *widget,
     return FALSE;
 
   gdk_event_request_motions (event);
-  sv_update_color (plane, event->x, event->y);
+  update_color (plane, event->x, event->y);
 
   return TRUE;
 }
 
 static void
-h_changed (GtkColorPlane *plane)
-{
-  create_sv_surface (plane);
-  gtk_widget_queue_draw (GTK_WIDGET (plane));
-}
-
-static void
-sv_changed (GtkColorPlane *plane)
-{
-  hsv_to_xy (plane);
-  gtk_widget_queue_draw (GTK_WIDGET (plane));
-}
-
-static void
 sv_move (GtkColorPlane *plane,
          gdouble        ds,
          gdouble        dv)
@@ -354,13 +342,12 @@ error:
 }
 
 static gboolean
-sv_key_press (GtkWidget      *widget,
-              GdkEventKey    *event)
+plane_key_press (GtkWidget   *widget,
+                 GdkEventKey *event)
 {
   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
   gdouble step;
 
-  /* FIXME: turn into bindings */
   if ((event->state & GDK_MOD1_MASK) != 0)
     step = 0.1;
   else
@@ -392,6 +379,7 @@ gtk_color_plane_init (GtkColorPlane *plane)
   plane->priv = G_TYPE_INSTANCE_GET_PRIVATE (plane,
                                              GTK_TYPE_COLOR_PLANE,
                                              GtkColorPlanePrivate);
+
   gtk_widget_set_can_focus (GTK_WIDGET (plane), TRUE);
   gtk_widget_set_events (GTK_WIDGET (plane), GDK_KEY_PRESS_MASK
                                              | GDK_BUTTON_PRESS_MASK
@@ -407,11 +395,13 @@ gtk_color_plane_init (GtkColorPlane *plane)
 }
 
 static void
-sv_finalize (GObject *object)
+plane_finalize (GObject *object)
 {
   GtkColorPlane *plane = GTK_COLOR_PLANE (object);
 
-  cairo_surface_destroy (plane->priv->surface);
+  if (plane->priv->surface)
+    cairo_surface_destroy (plane->priv->surface);
+
   g_clear_object (&plane->priv->h_adj);
   g_clear_object (&plane->priv->s_adj);
   g_clear_object (&plane->priv->v_adj);
@@ -425,15 +415,15 @@ gtk_color_plane_class_init (GtkColorPlaneClass *class)
   GObjectClass *object_class = G_OBJECT_CLASS (class);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
 
-  object_class->finalize = sv_finalize;
+  object_class->finalize = plane_finalize;
 
-  widget_class->draw = sv_draw;
-  widget_class->configure_event = sv_configure;
-  widget_class->button_press_event = sv_button_press;
-  widget_class->button_release_event = sv_button_release;
-  widget_class->motion_notify_event = sv_motion;
-  widget_class->grab_broken_event = sv_grab_broken;
-  widget_class->key_press_event = sv_key_press;
+  widget_class->draw = plane_draw;
+  widget_class->configure_event = plane_configure;
+  widget_class->button_press_event = plane_button_press;
+  widget_class->button_release_event = plane_button_release;
+  widget_class->motion_notify_event = plane_motion_notify;
+  widget_class->grab_broken_event = plane_grab_broken;
+  widget_class->key_press_event = plane_key_press;
 
   g_type_class_add_private (class, sizeof (GtkColorPlanePrivate));
 }
@@ -447,16 +437,12 @@ gtk_color_plane_new (GtkAdjustment *h_adj,
 
   plane = (GtkColorPlane *) g_object_new (GTK_TYPE_COLOR_PLANE, NULL);
 
-
   plane->priv->h_adj = g_object_ref_sink (h_adj);
   plane->priv->s_adj = g_object_ref_sink (s_adj);
   plane->priv->v_adj = g_object_ref_sink (v_adj);
-  g_signal_connect_swapped (plane->priv->h_adj, "value-changed",
-                            G_CALLBACK (h_changed), plane);
-  g_signal_connect_swapped (plane->priv->s_adj, "value-changed",
-                            G_CALLBACK (sv_changed), plane);
-  g_signal_connect_swapped (plane->priv->v_adj, "value-changed",
-                            G_CALLBACK (sv_changed), plane);
+  g_signal_connect_swapped (h_adj, "value-changed", G_CALLBACK (h_changed), plane);
+  g_signal_connect_swapped (s_adj, "value-changed", G_CALLBACK (sv_changed), plane);
+  g_signal_connect_swapped (v_adj, "value-changed", G_CALLBACK (sv_changed), plane);
 
   return (GtkWidget *)plane;
 }
diff --git a/gtk/gtkcolorscale.c b/gtk/gtkcolorscale.c
index e7ca4bf..c1cc660 100644
--- a/gtk/gtkcolorscale.c
+++ b/gtk/gtkcolorscale.c
@@ -20,10 +20,12 @@
 #include "config.h"
 
 #include "gtkcolorscale.h"
+
 #include "gtkcolorutils.h"
 #include "gtkorientable.h"
 #include "gtkstylecontext.h"
 #include "gtkaccessible.h"
+#include "gtkprivate.h"
 #include "gtkintl.h"
 
 struct _GtkColorScalePrivate
@@ -34,6 +36,12 @@ struct _GtkColorScalePrivate
   GtkColorScaleType type;
 };
 
+enum
+{
+  PROP_ZERO,
+  PROP_SCALE_TYPE
+};
+
 G_DEFINE_TYPE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
 
 static cairo_pattern_t *
@@ -58,19 +66,11 @@ get_checkered_pattern (void)
 }
 
 static void
-create_h_surface (GtkColorScale *scale)
+create_surface (GtkColorScale *scale)
 {
   GtkWidget *widget = GTK_WIDGET (scale);
-  cairo_t *cr;
   cairo_surface_t *surface;
-  gint width, height, stride;
-  cairo_surface_t *tmp;
-  guint red, green, blue;
-  guint32 *data, *p;
-  gdouble h;
-  gdouble r, g, b;
-  gdouble f;
-  gint x, y;
+  gint width, height;
 
   if (!gtk_widget_get_realized (widget))
     return;
@@ -78,7 +78,8 @@ create_h_surface (GtkColorScale *scale)
   width = gtk_widget_get_allocated_width (widget);
   height = gtk_widget_get_allocated_height (widget);
 
-  if (width != scale->priv->width ||
+  if (!scale->priv->surface ||
+      width != scale->priv->width ||
       height != scale->priv->height)
     {
       surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
@@ -96,108 +97,77 @@ create_h_surface (GtkColorScale *scale)
   if (width == 1 || height == 1)
     return;
 
-  stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
-
-  data = g_malloc (height * stride);
-
-  f = 1.0 / (height - 1);
-  for (y = 0; y < height; y++)
+  if (scale->priv->type == GTK_COLOR_SCALE_HUE)
     {
-      h = CLAMP (y * f, 0.0, 1.0);
-      p = data + y * (stride / 4);
-      for (x = 0; x < width; x++)
+      cairo_t *cr;
+      gint stride;
+      cairo_surface_t *tmp;
+      guint red, green, blue;
+      guint32 *data, *p;
+      gdouble h;
+      gdouble r, g, b;
+      gdouble f;
+      gint x, y;
+
+      stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
+
+      data = g_malloc (height * stride);
+
+      f = 1.0 / (height - 1);
+      for (y = 0; y < height; y++)
         {
-          gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
-          red = CLAMP (r * 255, 0, 255);
-          green = CLAMP (g * 255, 0, 255);
-          blue = CLAMP (b * 255, 0, 255);
-          p[x] = (red << 16) | (green << 8) | blue;
+          h = CLAMP (y * f, 0.0, 1.0);
+          p = data + y * (stride / 4);
+          for (x = 0; x < width; x++)
+            {
+              gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
+              red = CLAMP (r * 255, 0, 255);
+              green = CLAMP (g * 255, 0, 255);
+              blue = CLAMP (b * 255, 0, 255);
+              p[x] = (red << 16) | (green << 8) | blue;
+            }
         }
-    }
-
-  tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
-                                             width, height, stride);
-  cr = cairo_create (surface);
-
-  cairo_set_source_surface (cr, tmp, 0, 0);
-  cairo_paint (cr);
 
-  cairo_destroy (cr);
-  cairo_surface_destroy (tmp);
-  g_free (data);
-}
-
-static void
-create_a_surface (GtkColorScale *scale)
-{
-  GtkWidget *widget = GTK_WIDGET (scale);
-  cairo_t *cr;
-  cairo_surface_t *surface;
-  cairo_pattern_t *pattern;
-  cairo_matrix_t matrix;
-  GdkRGBA *color;
-  gint width, height;
+      tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
+                                                 width, height, stride);
+      cr = cairo_create (surface);
 
-  if (!gtk_widget_get_realized (widget))
-    return;
+      cairo_set_source_surface (cr, tmp, 0, 0);
+      cairo_paint (cr);
 
-  width = gtk_widget_get_allocated_width (widget);
-  height = gtk_widget_get_allocated_height (widget);
-
-  if (!scale->priv->surface ||
-      width != scale->priv->width ||
-      height != scale->priv->height)
-    {
-      surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
-                                                   CAIRO_CONTENT_COLOR,
-                                                   width, height);
-      if (scale->priv->surface)
-        cairo_surface_destroy (scale->priv->surface);
-      scale->priv->surface = surface;
-      scale->priv->width = width;
-      scale->priv->height = height;
+      cairo_destroy (cr);
+      cairo_surface_destroy (tmp);
+      g_free (data);
     }
-  else
-    return;
-
-  if (width == 1 || height == 1)
-    return;
+  else if (scale->priv->type == GTK_COLOR_SCALE_ALPHA)
+    {
+      cairo_t *cr;
+      cairo_pattern_t *pattern;
+      cairo_matrix_t matrix;
+      GdkRGBA *color;
 
-  cr = cairo_create (surface);
+      cr = cairo_create (surface);
 
-  cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
-  cairo_paint (cr);
-  cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
+      cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
+      cairo_paint (cr);
+      cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
 
-  pattern = get_checkered_pattern ();
-  cairo_matrix_init_scale (&matrix, 0.125, 0.125);
-  cairo_pattern_set_matrix (pattern, &matrix);
-  cairo_mask (cr, pattern);
-  cairo_pattern_destroy (pattern);
+      pattern = get_checkered_pattern ();
+      cairo_matrix_init_scale (&matrix, 0.125, 0.125);
+      cairo_pattern_set_matrix (pattern, &matrix);
+      cairo_mask (cr, pattern);
+      cairo_pattern_destroy (pattern);
 
-  color = &scale->priv->color;
+      color = &scale->priv->color;
 
-  pattern = cairo_pattern_create_linear (0, 0, width, 0);
-  cairo_pattern_add_color_stop_rgba (pattern, 0, color->red, color->green, color->blue, 0);
-  cairo_pattern_add_color_stop_rgba (pattern, width, color->red, color->green, color->blue, 1);
-  cairo_set_source (cr, pattern);
-  cairo_paint (cr);
-  cairo_pattern_destroy (pattern);
+      pattern = cairo_pattern_create_linear (0, 0, width, 0);
+      cairo_pattern_add_color_stop_rgba (pattern, 0, color->red, color->green, color->blue, 0);
+      cairo_pattern_add_color_stop_rgba (pattern, width, color->red, color->green, color->blue, 1);
+      cairo_set_source (cr, pattern);
+      cairo_paint (cr);
+      cairo_pattern_destroy (pattern);
 
-  cairo_destroy (cr);
-}
-
-static void
-create_surface (GtkColorScale *scale)
-{
-  switch (scale->priv->type)
-    {
-    case GTK_COLOR_SCALE_HUE:
-      create_h_surface (scale);
-      break;
-    case GTK_COLOR_SCALE_ALPHA:
-      create_a_surface (scale);
-      break;
+      cairo_destroy (cr);
     }
 }
 
@@ -264,8 +234,6 @@ gtk_color_scale_init (GtkColorScale *scale)
   scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
                                              GTK_TYPE_COLOR_SCALE,
                                              GtkColorScalePrivate);
-
-  scale->priv->type = GTK_COLOR_SCALE_HUE;
 }
 
 static void
@@ -280,46 +248,31 @@ scale_finalize (GObject *object)
 }
 
 static void
-gtk_color_scale_class_init (GtkColorScaleClass *class)
+scale_get_property (GObject    *object,
+                    guint       prop_id,
+                    GValue     *value,
+                    GParamSpec *pspec)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (class);
-  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
-
-  object_class->finalize = scale_finalize;
-
-  widget_class->draw = scale_draw;
-
-  g_type_class_add_private (class, sizeof (GtkColorScalePrivate));
-}
+  GtkColorScale *scale = GTK_COLOR_SCALE (object);
 
-void
-gtk_color_scale_set_rgba (GtkColorScale *scale,
-                          const GdkRGBA *color)
-{
-  scale->priv->color.red = color->red;
-  scale->priv->color.green = color->green;
-  scale->priv->color.blue = color->blue;
-  scale->priv->color.alpha = color->alpha;
-  if (scale->priv->surface)
+  switch (prop_id)
     {
-      cairo_surface_destroy (scale->priv->surface);
-      scale->priv->surface = NULL;
+    case PROP_SCALE_TYPE:
+      g_value_set_int (value, scale->priv->type);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
     }
-  create_surface (scale);
-  gtk_widget_queue_draw (GTK_WIDGET (scale));
 }
 
 static void
-gtk_color_scale_set_type (GtkColorScale     *scale,
-                          GtkColorScaleType  type)
+scale_set_type (GtkColorScale     *scale,
+                GtkColorScaleType  type)
 {
   AtkObject *atk_obj;
 
   scale->priv->type = type;
-  cairo_surface_destroy (scale->priv->surface);
-  scale->priv->surface = NULL;
-  create_surface (scale);
-  gtk_widget_queue_draw (GTK_WIDGET (scale));
 
   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (scale));
   if (GTK_IS_ACCESSIBLE (atk_obj))
@@ -332,17 +285,62 @@ gtk_color_scale_set_type (GtkColorScale     *scale,
     }
 }
 
+static void
+scale_set_property (GObject      *object,
+                    guint         prop_id,
+                    const GValue *value,
+                    GParamSpec   *pspec)
+{
+  GtkColorScale *scale = GTK_COLOR_SCALE (object);
+
+  switch (prop_id)
+    {
+    case PROP_SCALE_TYPE:
+      scale_set_type (scale, (GtkColorScaleType)g_value_get_int (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_color_scale_class_init (GtkColorScaleClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+
+  object_class->finalize = scale_finalize;
+  object_class->get_property = scale_get_property;
+  object_class->set_property = scale_set_property;
+
+  widget_class->draw = scale_draw;
+
+  g_object_class_install_property (object_class, PROP_SCALE_TYPE,
+      g_param_spec_int ("scale-type", P_("Scale type"), P_("Scale type"),
+                        0, 1, 0,
+                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+  g_type_class_add_private (class, sizeof (GtkColorScalePrivate));
+}
+
+void
+gtk_color_scale_set_rgba (GtkColorScale *scale,
+                          const GdkRGBA *color)
+{
+  scale->priv->color = *color;
+  scale->priv->width = -1; /* force surface refresh */
+  create_surface (scale);
+  gtk_widget_queue_draw (GTK_WIDGET (scale));
+}
+
 GtkWidget *
 gtk_color_scale_new (GtkAdjustment     *adjustment,
                      GtkColorScaleType  type)
 {
-  GtkWidget *scale;
-
-  scale = (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SCALE,
-                                      "adjustment", adjustment,
-                                      "draw-value", FALSE,
-                                      NULL);
-  gtk_color_scale_set_type (GTK_COLOR_SCALE (scale), type);
-
-  return scale;
+  return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SCALE,
+                                     "adjustment", adjustment,
+                                     "draw-value", FALSE,
+                                     "scale-type", type,
+                                     NULL);
 }
diff --git a/gtk/gtkcolorswatch.c b/gtk/gtkcolorswatch.c
index 60973d3..8e4b38d 100644
--- a/gtk/gtkcolorswatch.c
+++ b/gtk/gtkcolorswatch.c
@@ -20,6 +20,7 @@
 #include "config.h"
 
 #include "gtkcolorswatch.h"
+
 #include "gtkroundedboxprivate.h"
 #include "gtkdnd.h"
 #include "gtkicontheme.h"
@@ -27,7 +28,6 @@
 #include "gtkmenu.h"
 #include "gtkmenuitem.h"
 #include "gtkmenushell.h"
-#include "gtkbindings.h"
 #include "gtkprivate.h"
 #include "gtkintl.h"
 
@@ -78,16 +78,6 @@ gtk_color_swatch_init (GtkColorSwatch *swatch)
   swatch->priv->use_alpha = TRUE;
 }
 
-static void
-swatch_finalize (GObject *object)
-{
-  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
-
-  g_free (swatch->priv->icon);
-
-  G_OBJECT_CLASS (gtk_color_swatch_parent_class)->finalize (object);
-}
-
 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
 
 static cairo_pattern_t *
@@ -237,7 +227,7 @@ drag_set_color_icon (GdkDragContext *context,
   gdk_cairo_set_source_rgba (cr, color);
   cairo_paint (cr);
 
-  cairo_surface_set_device_offset (surface, -2, -2);
+  cairo_surface_set_device_offset (surface, -4, -4);
   gtk_drag_set_icon_surface (context, surface);
 
   cairo_destroy (cr);
@@ -316,52 +306,6 @@ swatch_drag_data_received (GtkWidget        *widget,
 }
 
 static void
-swatch_get_property (GObject    *object,
-                     guint       prop_id,
-                     GValue     *value,
-                     GParamSpec *pspec)
-{
-  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
-  GdkRGBA color;
-
-  switch (prop_id)
-    {
-    case PROP_RGBA:
-      gtk_color_swatch_get_rgba (swatch, &color);
-      g_value_set_boxed (value, &color);
-      break;
-    case PROP_SELECTED:
-      g_value_set_boolean (value, swatch->priv->selected);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-    }
-}
-
-static void
-swatch_set_property (GObject      *object,
-                     guint         prop_id,
-                     const GValue *value,
-                     GParamSpec   *pspec)
-{
-  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
-
-  switch (prop_id)
-    {
-    case PROP_RGBA:
-      gtk_color_swatch_set_rgba (swatch, g_value_get_boxed (value));
-      break;
-    case PROP_SELECTED:
-      gtk_color_swatch_set_selected (swatch, g_value_get_boolean (value));
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-    }
-}
-
-static void
 swatch_get_preferred_width (GtkWidget *widget,
                             gint      *min,
                             gint      *nat)
@@ -403,8 +347,8 @@ swatch_key_press (GtkWidget   *widget,
 }
 
 static gboolean
-swatch_enter (GtkWidget        *widget,
-              GdkEventCrossing *event)
+swatch_enter_notify (GtkWidget        *widget,
+                     GdkEventCrossing *event)
 {
   GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
   swatch->priv->contains_pointer = TRUE;
@@ -412,8 +356,8 @@ swatch_enter (GtkWidget        *widget,
 }
 
 static gboolean
-swatch_leave (GtkWidget        *widget,
-              GdkEventCrossing *event)
+swatch_leave_notify (GtkWidget        *widget,
+                     GdkEventCrossing *event)
 {
   GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
   swatch->priv->contains_pointer = FALSE;
@@ -497,7 +441,7 @@ swatch_button_press (GtkWidget      *widget,
 {
   GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
 
-  gtk_widget_grab_focus (GTK_WIDGET (swatch));
+  gtk_widget_grab_focus (widget);
 
   if (gdk_event_triggers_context_menu ((GdkEvent *) event) &&
       swatch->priv->has_color)
@@ -521,8 +465,6 @@ swatch_button_release (GtkWidget      *widget,
 {
   GtkColorSwatch *swatch = GTK_COLOR_SWATCH (widget);
 
-  gtk_widget_grab_focus (GTK_WIDGET (swatch));
-
   if (event->button == GDK_BUTTON_PRIMARY &&
       swatch->priv->contains_pointer)
     {
@@ -542,12 +484,70 @@ swatch_button_release (GtkWidget      *widget,
 }
 
 static gboolean
-swatch_menu (GtkWidget *swatch)
+swatch_popup_menu (GtkWidget *swatch)
 {
   do_popup (swatch, NULL);
   return TRUE;
 }
 
+/* GObject implementation {{{1 */
+
+static void
+swatch_get_property (GObject    *object,
+                     guint       prop_id,
+                     GValue     *value,
+                     GParamSpec *pspec)
+{
+  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
+  GdkRGBA color;
+
+  switch (prop_id)
+    {
+    case PROP_RGBA:
+      gtk_color_swatch_get_rgba (swatch, &color);
+      g_value_set_boxed (value, &color);
+      break;
+    case PROP_SELECTED:
+      g_value_set_boolean (value, swatch->priv->selected);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+swatch_set_property (GObject      *object,
+                     guint         prop_id,
+                     const GValue *value,
+                     GParamSpec   *pspec)
+{
+  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
+
+  switch (prop_id)
+    {
+    case PROP_RGBA:
+      gtk_color_swatch_set_rgba (swatch, g_value_get_boxed (value));
+      break;
+    case PROP_SELECTED:
+      gtk_color_swatch_set_selected (swatch, g_value_get_boolean (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+swatch_finalize (GObject *object)
+{
+  GtkColorSwatch *swatch = GTK_COLOR_SWATCH (object);
+
+  g_free (swatch->priv->icon);
+
+  G_OBJECT_CLASS (gtk_color_swatch_parent_class)->finalize (object);
+}
+
 static void
 gtk_color_swatch_class_init (GtkColorSwatchClass *class)
 {
@@ -565,11 +565,11 @@ gtk_color_swatch_class_init (GtkColorSwatchClass *class)
   widget_class->drag_data_get = swatch_drag_data_get;
   widget_class->drag_data_received = swatch_drag_data_received;
   widget_class->key_press_event = swatch_key_press;
-  widget_class->popup_menu = swatch_menu;
+  widget_class->popup_menu = swatch_popup_menu;
   widget_class->button_press_event = swatch_button_press;
   widget_class->button_release_event = swatch_button_release;
-  widget_class->enter_notify_event = swatch_enter;
-  widget_class->leave_notify_event = swatch_leave;
+  widget_class->enter_notify_event = swatch_enter_notify;
+  widget_class->leave_notify_event = swatch_leave_notify;
 
   signals[ACTIVATE] =
     g_signal_new ("activate",
@@ -596,7 +596,6 @@ gtk_color_swatch_class_init (GtkColorSwatchClass *class)
   g_type_class_add_private (object_class, sizeof (GtkColorSwatchPrivate));
 }
 
-
 /* Public API {{{1 */
 
 GtkWidget *
@@ -605,25 +604,22 @@ gtk_color_swatch_new (void)
   return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SWATCH, NULL);
 }
 
+static const GtkTargetEntry dnd_targets[] = {
+  { "application/x-color", 0 }
+};
+
 void
 gtk_color_swatch_set_rgba (GtkColorSwatch *swatch,
                            const GdkRGBA  *color)
 {
-  static const GtkTargetEntry targets[] = {
-    { "application/x-color", 0 }
-  };
-
   if (!swatch->priv->has_color)
     gtk_drag_source_set (GTK_WIDGET (swatch),
                          GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
-                         targets, 1,
+                         dnd_targets, G_N_ELEMENTS (dnd_targets),
                          GDK_ACTION_COPY | GDK_ACTION_MOVE);
 
   swatch->priv->has_color = TRUE;
-  swatch->priv->color.red = color->red;
-  swatch->priv->color.green = color->green;
-  swatch->priv->color.blue = color->blue;
-  swatch->priv->color.alpha = color->alpha;
+  swatch->priv->color = *color;
 
   gtk_widget_queue_draw (GTK_WIDGET (swatch));
   g_object_notify (G_OBJECT (swatch), "rgba");
@@ -690,16 +686,12 @@ void
 gtk_color_swatch_set_can_drop (GtkColorSwatch *swatch,
                                gboolean        can_drop)
 {
-  static const GtkTargetEntry targets[] = {
-    { "application/x-color", 0 }
-  };
-
   if (!swatch->priv->can_drop)
     gtk_drag_dest_set (GTK_WIDGET (swatch),
                        GTK_DEST_DEFAULT_HIGHLIGHT |
                        GTK_DEST_DEFAULT_MOTION |
                        GTK_DEST_DEFAULT_DROP,
-                       targets, 1,
+                       dnd_targets, G_N_ELEMENTS (dnd_targets),
                        GDK_ACTION_COPY);
 
   swatch->priv->can_drop = can_drop;
@@ -707,11 +699,10 @@ gtk_color_swatch_set_can_drop (GtkColorSwatch *swatch,
 
 void
 gtk_color_swatch_set_use_alpha (GtkColorSwatch *swatch,
-                                gboolean       use_alpha)
+                                gboolean        use_alpha)
 {
   swatch->priv->use_alpha = use_alpha;
   gtk_widget_queue_draw (GTK_WIDGET (swatch));
 }
 
-
 /* vim:set foldmethod=marker: */
diff --git a/gtk/gtkcolorswatch.h b/gtk/gtkcolorswatch.h
index afb6d27..77e45ea 100644
--- a/gtk/gtkcolorswatch.h
+++ b/gtk/gtkcolorswatch.h
@@ -81,6 +81,12 @@ G_GNUC_INTERNAL
 gboolean    gtk_color_swatch_get_rgba         (GtkColorSwatch *swatch,
                                                GdkRGBA        *color);
 G_GNUC_INTERNAL
+void        gtk_color_swatch_set_hsva         (GtkColorSwatch *swatch,
+                                               gdouble         h,
+                                               gdouble         s,
+                                               gdouble         v,
+                                               gdouble         a);
+G_GNUC_INTERNAL
 void        gtk_color_swatch_set_selected     (GtkColorSwatch *swatch,
                                                gboolean        selected);
 G_GNUC_INTERNAL



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