]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcolorsel.c
fix bug in tooltip
[~andy/gtk] / gtk / gtkcolorsel.c
index eb660b9d4746f9936d9d796c9a6f4b468aa84192..a894e9fc5cf6b2ff3626b0b60ccee7933abba1f3 100644 (file)
 /* GTK - The GIMP Toolkit
+ * Copyright (C) 2000 Red Hat, Inc.
  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
-#include <stdlib.h>
-#include <stdio.h>
-#include <math.h>
-#include <gdk/gdk.h>
-#include "gtkcolorsel.h"
-#include "gtkwindow.h"
-#include "gtkhbbox.h"
-#include "gtkdnd.h"
-#include "gtkselection.h"
 
 /*
- * If you change the way the color values are stored,
- * please make sure to update the drag & drop support so it sends
- * across all the color info (currently RGBA). - Elliot
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#ifndef M_PI
-#define M_PI    3.14159265358979323846
-#endif /* M_PI */
-
-#define DEGTORAD(a) (2.0*M_PI*a/360.0)
-#define SQR(a) (a*a)
-
-#define TIMER_DELAY 300
-
-#define CIRCLE_RADIUS 65
-
-#define WHEEL_WIDTH   2*CIRCLE_RADIUS+2
-#define WHEEL_HEIGHT  2*CIRCLE_RADIUS+2
-
-#define VALUE_WIDTH   32
-#define VALUE_HEIGHT  WHEEL_HEIGHT
-
-#define SAMPLE_WIDTH  WHEEL_WIDTH+VALUE_WIDTH+5
-#define SAMPLE_HEIGHT 28
+#include "gdkconfig.h"
+#include <math.h>
 
-static void gtk_color_selection_class_init (GtkColorSelectionClass *klass);
-static void gtk_color_selection_init (GtkColorSelection *colorsel);
-static void gtk_color_selection_dialog_class_init(GtkColorSelectionDialogClass *klass);
-static void gtk_color_selection_dialog_init(GtkColorSelectionDialog *colorseldiag);
+#if defined (GDK_WINDOWING_X11)
+#include "x11/gdkx.h"
+#elif defined (GDK_WINDOWING_WIN32)
+#include "win32/gdkwin32.h"
+#elif defined (GDK_WINDOWING_FB)
+#include "linux-fb/gdkfb.h"
+#endif
 
-enum
-{
+#include "gdk/gdkkeysyms.h"
+#include "gtkcolorsel.h"
+#include "gtkhsv.h"
+#include "gtkwindow.h"
+#include "gtkselection.h"
+#include "gtkdnd.h"
+#include "gtkdrawingarea.h"
+#include "gtksignal.h"
+#include "gtkhbox.h"
+#include "gtkhbbox.h"
+#include "gtkrc.h"
+#include "gtkframe.h"
+#include "gtktable.h"
+#include "gtklabel.h"
+#include "gtkpixmap.h"
+#include "gtkspinbutton.h"
+#include "gtkrange.h"
+#include "gtkhscale.h"
+#include "gtkentry.h"
+#include "gtkbutton.h"
+#include "gtkhseparator.h"
+#include "gtktooltips.h"
+#include "gtkinvisible.h"
+#include "gtkmenuitem.h"
+#include "gtkmain.h"
+#include "gtksettings.h"
+#include "gtkintl.h"
+
+#include <string.h>
+
+/* Number of elements in the custom palatte */
+#define GTK_CUSTOM_PALETTE_WIDTH 10
+#define GTK_CUSTOM_PALETTE_HEIGHT 2
+
+enum {
   COLOR_CHANGED,
   LAST_SIGNAL
 };
 
-enum
-{
-  RGB_INPUTS     = 1 << 0,
-  HSV_INPUTS     = 1 << 1,
-  OPACITY_INPUTS = 1 << 2
+enum {
+  COLORSEL_RED = 0,
+  COLORSEL_GREEN = 1,
+  COLORSEL_BLUE = 2,
+  COLORSEL_OPACITY = 3,
+  COLORSEL_HUE,
+  COLORSEL_SATURATION,
+  COLORSEL_VALUE,
+  COLORSEL_NUM_CHANNELS
 };
 
-enum
-{
-  SCALE,
-  ENTRY,
-  BOTH
-};
+typedef struct _ColorSelectionPrivate ColorSelectionPrivate;
 
-enum
+struct _ColorSelectionPrivate
 {
-  HUE,
-  SATURATION,
-  VALUE,
-  RED,
-  GREEN,
-  BLUE,
-  OPACITY,
-  NUM_CHANNELS
+  guint has_opacity : 1;
+  guint has_palette : 1;
+  guint changing : 1;
+  guint default_set : 1;
+  guint default_alpha_set : 1;
+  
+  gdouble color[COLORSEL_NUM_CHANNELS];
+  gdouble old_color[COLORSEL_NUM_CHANNELS];
+  
+  GtkWidget *triangle_colorsel;
+  GtkWidget *hue_spinbutton;
+  GtkWidget *sat_spinbutton;
+  GtkWidget *val_spinbutton;
+  GtkWidget *red_spinbutton;
+  GtkWidget *green_spinbutton;
+  GtkWidget *blue_spinbutton;
+  GtkWidget *opacity_slider;
+  GtkWidget *opacity_label;
+  GtkWidget *opacity_entry;
+  GtkWidget *palette_frame;
+  GtkWidget *hex_entry;
+  
+  /* The Palette code */
+  GtkWidget *custom_palette [GTK_CUSTOM_PALETTE_WIDTH][GTK_CUSTOM_PALETTE_HEIGHT];
+  
+  /* The color_sample stuff */
+  GtkWidget *sample_area;
+  GtkWidget *old_sample;
+  GtkWidget *cur_sample;
+  GtkWidget *colorsel;
+
+  /* Tooltips group */
+  GtkTooltips *tooltips;
+
+  /* Window for grabbing on */
+  GtkWidget *dropper_grab_widget;
 };
 
-typedef struct
-{
-  gchar *label;
-  gfloat lower, upper, step_inc, page_inc;
-  GtkSignalFunc updater;
-} scale_val_type;
-
-
-#define HSV_TO_RGB()  gtk_color_selection_hsv_to_rgb( \
-                        colorsel->values[HUE], \
-                        colorsel->values[SATURATION], \
-                        colorsel->values[VALUE], \
-                        &colorsel->values[RED], \
-                        &colorsel->values[GREEN], \
-                        &colorsel->values[BLUE])
-
-#define RGB_TO_HSV()  gtk_color_selection_rgb_to_hsv( \
-                        colorsel->values[RED], \
-                        colorsel->values[GREEN], \
-                        colorsel->values[BLUE], \
-                        &colorsel->values[HUE], \
-                        &colorsel->values[SATURATION], \
-                        &colorsel->values[VALUE])
-
-
-static void gtk_color_selection_hsv_updater       (GtkWidget         *widget,
-                                                   gpointer           data);
-static void gtk_color_selection_rgb_updater       (GtkWidget         *widget,
-                                                   gpointer           data);
-static void gtk_color_selection_opacity_updater   (GtkWidget         *widget,
-                                                   gpointer           data);
-static void gtk_color_selection_realize           (GtkWidget         *widget);
-static void gtk_color_selection_unrealize         (GtkWidget         *widget);
-static void gtk_color_selection_finalize          (GtkObject         *object);
-static void gtk_color_selection_color_changed     (GtkColorSelection *colorsel);
-static void gtk_color_selection_update_input      (GtkWidget         *scale,
-                                                   GtkWidget         *entry,
-                                                   gdouble            value);
-static void gtk_color_selection_update_inputs     (GtkColorSelection *colorsel,
-                                                   gint               inputs,
-                                                   gint               which);
-static void gtk_color_selection_update_value      (GtkColorSelection *colorsel,
-                                                   gint               y);
-static void gtk_color_selection_update_wheel      (GtkColorSelection *colorsel,
-                                                   gint               x,
-                                                   gint               y);
-static void gtk_color_selection_value_resize      (GtkWidget          *widget,
-                                                   gpointer            data);
-static gint gtk_color_selection_value_events      (GtkWidget          *area,
-                                                   GdkEvent           *event);
-static gint gtk_color_selection_value_timeout     (GtkColorSelection  *colorsel);
-static void gtk_color_selection_wheel_resize      (GtkWidget          *widget,
-                                                   gpointer            data);
-static gint gtk_color_selection_wheel_events      (GtkWidget          *area,
-                                                   GdkEvent           *event);
-static gint gtk_color_selection_wheel_timeout     (GtkColorSelection  *colorsel);
-static void gtk_color_selection_sample_resize     (GtkWidget          *widget,
-                                                   gpointer            data);
-static void gtk_color_selection_drag_begin        (GtkWidget          *widget,
-                                                  GdkDragContext     *context,
-                                                  gpointer            data);
-static void gtk_color_selection_drop_handle       (GtkWidget          *widget, 
-                                                  GdkDragContext     *context,
-                                                  gint                x,
-                                                  gint                y,
-                                                  GtkSelectionData   *selection_data,
-                                                  guint               info,
-                                                  guint               time,
-                                                  gpointer            data);
-static void gtk_color_selection_drag_handle       (GtkWidget        *widget, 
-                                                  GdkDragContext   *context,
-                                                  GtkSelectionData *selection_data,
-                                                  guint             info,
-                                                  guint             time,
-                                                  gpointer          data);
-static void gtk_color_selection_draw_wheel_marker (GtkColorSelection  *colorsel);
-static void gtk_color_selection_draw_wheel_frame  (GtkColorSelection  *colorsel);
-static void gtk_color_selection_draw_value_marker (GtkColorSelection  *colorsel);
-static void gtk_color_selection_draw_value_bar    (GtkColorSelection  *colorsel,
-                                                   gint                resize);
-static void gtk_color_selection_draw_wheel        (GtkColorSelection  *colorsel,
-                                                   gint                resize);
-static void gtk_color_selection_draw_sample       (GtkColorSelection  *colorsel,
-                                                   gint                resize);
-
-static gint gtk_color_selection_eval_wheel        (gint     x, gint     y,
-                                                  gdouble cx, gdouble cy,
-                                                  gdouble *h, gdouble *s);
-
-static void gtk_color_selection_hsv_to_rgb        (gdouble  h, gdouble  s, gdouble  v,
-                                                  gdouble *r, gdouble *g, gdouble *b);
-static void gtk_color_selection_rgb_to_hsv        (gdouble  r, gdouble  g, gdouble  b,
-                                                  gdouble *h, gdouble *s, gdouble *v);
-
-
-static GtkVBoxClass *color_selection_parent_class = NULL;
-static GtkWindowClass *color_selection_dialog_parent_class = NULL;
-
-
-static guint color_selection_signals[LAST_SIGNAL] = {0};
-
-static const gchar     *value_index_key = "gtk-value-index";
-
-
-#define SF GtkSignalFunc
-
-
-static const scale_val_type scale_vals[NUM_CHANNELS] =
-{
-  {"Hue:",        0.0, 360.0, 1.00, 10.00, (SF) gtk_color_selection_hsv_updater},
-  {"Saturation:", 0.0,   1.0, 0.01,  0.01, (SF) gtk_color_selection_hsv_updater},
-  {"Value:",      0.0,   1.0, 0.01,  0.01, (SF) gtk_color_selection_hsv_updater},
-  {"Red:",        0.0,   1.0, 0.01,  0.01, (SF) gtk_color_selection_rgb_updater},
-  {"Green:",      0.0,   1.0, 0.01,  0.01, (SF) gtk_color_selection_rgb_updater},
-  {"Blue:",       0.0,   1.0, 0.01,  0.01, (SF) gtk_color_selection_rgb_updater},
-  {"Opacity:",    0.0,   1.0, 0.01,  0.01, (SF) gtk_color_selection_opacity_updater}
+
+static void gtk_color_selection_init           (GtkColorSelection       *colorsel);
+static void gtk_color_selection_class_init     (GtkColorSelectionClass  *klass);
+static void gtk_color_selection_destroy                (GtkObject               *object);
+static void gtk_color_selection_finalize        (GObject                *object);
+static void update_color                       (GtkColorSelection       *colorsel);
+
+static gint     gtk_color_selection_get_palette_size    (GtkColorSelection *colorsel);
+static gboolean gtk_color_selection_get_palette_color   (GtkColorSelection *colorsel,
+                                                         gint               index,
+                                                         GdkColor          *color);
+static void     gtk_color_selection_set_palette_color   (GtkColorSelection *colorsel,
+                                                         gint               index,
+                                                         GdkColor          *color);
+static void     gtk_color_selection_unset_palette_color (GtkColorSelection *colorsel,
+                                                         gint               index);
+
+static gpointer parent_class = NULL;
+static guint color_selection_signals[LAST_SIGNAL] = { 0 };
+
+static gchar* default_colors = "black:white:gray50:red:purple:blue:light blue:green:yellow:orange:lavender:brown:goldenrod4:dodger blue:pink:light green:gray10:gray30:gray75:gray90";
+
+static GtkColorSelectionChangePaletteFunc change_palette_hook = NULL;
+
+static GdkColor current_colors[GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT];
+
+/* The cursor for the dropper */
+#define DROPPER_WIDTH 17
+#define DROPPER_HEIGHT 17
+#define DROPPER_X_HOT 2
+#define DROPPER_Y_HOT 16
+
+
+static char dropper_bits[] = {
+  0xff, 0x8f, 0x01, 0xff, 0x77, 0x01, 0xff, 0xfb, 0x00, 0xff, 0xf8, 0x00,
+  0x7f, 0xff, 0x00, 0xff, 0x7e, 0x01, 0xff, 0x9d, 0x01, 0xff, 0xd8, 0x01,
+  0x7f, 0xd4, 0x01, 0x3f, 0xee, 0x01, 0x1f, 0xff, 0x01, 0x8f, 0xff, 0x01,
+  0xc7, 0xff, 0x01, 0xe3, 0xff, 0x01, 0xf3, 0xff, 0x01, 0xfd, 0xff, 0x01,
+  0xff, 0xff, 0x01, };
+
+static char dropper_mask[] = {
+  0x00, 0x70, 0x00, 0x00, 0xf8, 0x00, 0x00, 0xfc, 0x01, 0x00, 0xff, 0x01,
+  0x80, 0xff, 0x01, 0x00, 0xff, 0x00, 0x00, 0x7f, 0x00, 0x80, 0x3f, 0x00,
+  0xc0, 0x3f, 0x00, 0xe0, 0x13, 0x00, 0xf0, 0x01, 0x00, 0xf8, 0x00, 0x00,
+  0x7c, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x0d, 0x00, 0x00,
+  0x02, 0x00, 0x00, };
+
+static GdkCursor *picker_cursor = NULL;
+
+
+/* XPM */
+static char *picker[] = {
+  /* columns rows colors chars-per-pixel */
+  "25 25 8 1",
+  "  c Gray0",
+  ". c #020202",
+  "X c Gray12",
+  "o c Gray13",
+  "O c Gray52",
+  "+ c #929292",
+  "@ c Gray100",
+  "# c None",
+  /* pixels */
+  "#########################",
+  "#########################",
+  "#########################",
+  "#########################",
+  "#########################",
+  "#################   #####",
+  "################     ####",
+  "################     +###",
+  "#############        +###",
+  "##############      ++###",
+  "#############+@   +++####",
+  "############+@@@  +######",
+  "###########+@@@ + +######",
+  "##########+@@@ ++#+######",
+  "#########+@@@ ++#########",
+  "########+@@@ ++##########",
+  "#######+@@@ ++###########",
+  "######+@@@ ++############",
+  "######+@@ ++#############",
+  "#####+@  ++##############",
+  "###### +++###############",
+  "#########################",
+  "#########################",
+  "#########################",
+  "#########################"
 };
 
-guint
-gtk_color_selection_get_type (void)
-{
-  static guint color_selection_type = 0;
 
-  if (!color_selection_type)
-    {
-      static const GtkTypeInfo colorsel_info =
-      {
-       "GtkColorSelection",
-       sizeof (GtkColorSelection),
-       sizeof (GtkColorSelectionClass),
-       (GtkClassInitFunc) gtk_color_selection_class_init,
-       (GtkObjectInitFunc) gtk_color_selection_init,
-       /* reserved_1 */ NULL,
-       /* reserved_2 */ NULL,
-        (GtkClassInitFunc) NULL,
-      };
+/*
+ *
+ * The Sample Color
+ *
+ */
+#define SAMPLE_WIDTH  64
+#define SAMPLE_HEIGHT 28
 
-      color_selection_type = gtk_type_unique (gtk_vbox_get_type (), &colorsel_info);
-    }
+static void color_sample_draw_sample (GtkColorSelection *colorsel, int which);
+static void color_sample_draw_samples (GtkColorSelection *colorsel);
 
-  return color_selection_type;
+static void
+color_sample_drag_begin (GtkWidget      *widget,
+                        GdkDragContext *context,
+                        gpointer        data)
+{
+  GtkColorSelection *colorsel = data;
+  ColorSelectionPrivate *priv;
+  GtkWidget *window;
+  gdouble colors[4];
+  gdouble *colsrc;
+  GdkColor bg;
+  gint n, i;
+  
+  priv = colorsel->private_data;
+  window = gtk_window_new (GTK_WINDOW_POPUP);
+  gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
+  gtk_widget_set_usize (window, 48, 32);
+  gtk_widget_realize (window);
+  gtk_object_set_data_full (GTK_OBJECT (widget),
+                           "gtk-color-selection-drag-window",
+                           window,
+                           (GtkDestroyNotify) gtk_widget_destroy);
+  
+  if (widget == priv->old_sample)
+    colsrc = priv->old_color;
+  else
+    colsrc = priv->color;
+  
+  for (i=0, n = COLORSEL_RED; n <= COLORSEL_BLUE; n++)
+    {
+      colors[i++] = colsrc[n];
+    }
+  
+  if (priv->has_opacity)
+    {
+      colors[i] = colsrc[COLORSEL_OPACITY];
+    }
+  
+  bg.red = 0xffff * colors[0];
+  bg.green = 0xffff * colors[1];
+  bg.blue = 0xffff * colors[2];
+  
+  gdk_color_alloc (gtk_widget_get_colormap (window), &bg);
+  gdk_window_set_background (window->window, &bg);
+  
+  gtk_drag_set_icon_widget (context, window, -2, -2);
 }
 
 static void
-gtk_color_selection_class_init (GtkColorSelectionClass *klass)
+color_sample_drag_end (GtkWidget      *widget,
+                      GdkDragContext *context,
+                      gpointer        data)
 {
-  GtkObjectClass *object_class;
-  GtkWidgetClass *widget_class;
-  GtkContainerClass *container_class;
-
-  object_class = (GtkObjectClass*) klass;
-  widget_class = (GtkWidgetClass*) klass;
-  container_class = (GtkContainerClass*) klass;
-
-  color_selection_parent_class = gtk_type_class (gtk_vbox_get_type ());
-
-  color_selection_signals[COLOR_CHANGED] =
-     gtk_signal_new ("color_changed",
-                    GTK_RUN_FIRST,
-                     object_class->type,
-                    GTK_SIGNAL_OFFSET (GtkColorSelectionClass, color_changed),
-                     gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0);
-
-  gtk_object_class_add_signals (object_class, color_selection_signals, LAST_SIGNAL);
-
-  object_class->finalize = gtk_color_selection_finalize;
-
-  widget_class->realize = gtk_color_selection_realize;
-  widget_class->unrealize = gtk_color_selection_unrealize;
+  gtk_object_set_data (GTK_OBJECT (widget), "gtk-color-selection-drag-window", NULL);
 }
 
 static void
-gtk_color_selection_init (GtkColorSelection *colorsel)
+color_sample_drop_handle (GtkWidget        *widget,
+                         GdkDragContext   *context,
+                         gint              x,
+                         gint              y,
+                         GtkSelectionData *selection_data,
+                         guint             info,
+                         guint             time,
+                         gpointer          data)
 {
-  GtkWidget *frame, *hbox, *vbox, *hbox2, *label, *table;
-  GtkObject *adj;
-  gint old_mask, n;
-  gchar txt[32];
-
-  for (n = RED; n <= OPACITY; n++)
-    colorsel->values[n] = 1.0;
-
-  RGB_TO_HSV ();
-
-  for (n = HUE; n <= OPACITY; n++)
-    colorsel->old_values[n] = colorsel->values[n];
-
-  colorsel->wheel_gc = NULL;
-  colorsel->value_gc = NULL;
-  colorsel->sample_gc = NULL;
-  colorsel->wheel_buf = NULL;
-  colorsel->value_buf = NULL;
-  colorsel->sample_buf = NULL;
-
-  colorsel->use_opacity = FALSE;
-  colorsel->timer_active = FALSE;
-  colorsel->policy = GTK_UPDATE_CONTINUOUS;
-
-  hbox = gtk_hbox_new (FALSE, 5);
-  gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
-  gtk_container_add (GTK_CONTAINER (colorsel), hbox);
-
-  vbox = gtk_vbox_new (FALSE, 5);
-  gtk_container_add (GTK_CONTAINER (hbox), vbox);
-  gtk_widget_show (vbox);
-
-  hbox2 = gtk_hbox_new (FALSE, 5);
-  gtk_container_add (GTK_CONTAINER (vbox), hbox2);
-  gtk_widget_show (hbox2);
-
-  colorsel->wheel_area = gtk_preview_new (GTK_PREVIEW_COLOR);
-  old_mask = gtk_widget_get_events(colorsel->wheel_area);
-  gtk_widget_set_events (colorsel->wheel_area,
-                        old_mask |
-                        GDK_BUTTON_PRESS_MASK |
-                        GDK_BUTTON_RELEASE_MASK |
-                        GDK_BUTTON_MOTION_MASK |
-                        GDK_POINTER_MOTION_HINT_MASK);
-  gtk_preview_size (GTK_PREVIEW (colorsel->wheel_area), WHEEL_WIDTH, WHEEL_HEIGHT);
-  gtk_preview_set_expand (GTK_PREVIEW (colorsel->wheel_area), TRUE);
-  gtk_container_add (GTK_CONTAINER (hbox2), colorsel->wheel_area);
-  gtk_widget_show (colorsel->wheel_area);
-
-  old_mask = gtk_widget_get_events (colorsel->wheel_area);
-
-  gtk_signal_connect (GTK_OBJECT (colorsel->wheel_area), "event",
-    (SF) gtk_color_selection_wheel_events, (gpointer) colorsel->wheel_area);
-  gtk_signal_connect_after (GTK_OBJECT (colorsel->wheel_area), "expose_event",
-    (SF) gtk_color_selection_wheel_events, (gpointer) colorsel->wheel_area);
-  gtk_signal_connect_after (GTK_OBJECT (colorsel->wheel_area), "size_allocate",
-    (SF) gtk_color_selection_wheel_resize, (gpointer) colorsel->wheel_area);
-  gtk_object_set_data (GTK_OBJECT (colorsel->wheel_area), "_GtkColorSelection", (gpointer) colorsel);
-
-  frame = gtk_frame_new (NULL);
-  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
-  gtk_container_set_border_width (GTK_CONTAINER (frame), 0);
-  gtk_box_pack_start (GTK_BOX (hbox2), frame, FALSE, TRUE, 0);
-  gtk_widget_show (frame);
-
-  colorsel->value_area = gtk_preview_new (GTK_PREVIEW_COLOR);
-  gtk_preview_size (GTK_PREVIEW (colorsel->value_area), VALUE_WIDTH, VALUE_HEIGHT);
-  gtk_preview_set_expand (GTK_PREVIEW (colorsel->value_area), TRUE);
-  gtk_container_add (GTK_CONTAINER (frame), colorsel->value_area);
-  gtk_widget_show (colorsel->value_area);
-
-  old_mask = gtk_widget_get_events (colorsel->value_area);
-  gtk_widget_set_events (colorsel->value_area,
-                        old_mask |
-                        GDK_BUTTON_PRESS_MASK |
-                        GDK_BUTTON_RELEASE_MASK |
-                        GDK_BUTTON_MOTION_MASK |
-                        GDK_POINTER_MOTION_HINT_MASK);
-
-  gtk_signal_connect_after (GTK_OBJECT (colorsel->value_area), "expose_event",
-    (SF) gtk_color_selection_value_events, (gpointer) colorsel->value_area);
-  gtk_signal_connect_after (GTK_OBJECT (colorsel->value_area), "size_allocate",
-    (SF) gtk_color_selection_value_resize, (gpointer) colorsel->value_area);
-  gtk_signal_connect (GTK_OBJECT (colorsel->value_area), "event",
-    (SF) gtk_color_selection_value_events, (gpointer) colorsel->value_area);
-  gtk_object_set_data (GTK_OBJECT (colorsel->value_area), "_GtkColorSelection", (gpointer) colorsel);
-
-  /* New/old color samples */
-  /* ===================== */
-
-  frame = gtk_frame_new (NULL);
-  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
-  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
-  gtk_widget_show (frame);
-
-/*  colorsel->sample_area_eb = gtk_button_new ();
-  gtk_container_add (GTK_CONTAINER (frame), colorsel->sample_area_eb);
-  gtk_widget_show (colorsel->sample_area_eb); */
-
-  colorsel->sample_area = gtk_preview_new (GTK_PREVIEW_COLOR);
-  gtk_preview_size (GTK_PREVIEW (colorsel->sample_area), SAMPLE_WIDTH, SAMPLE_HEIGHT);
-  gtk_preview_set_expand (GTK_PREVIEW (colorsel->sample_area), TRUE);
-  gtk_container_add (GTK_CONTAINER (frame),
-                    colorsel->sample_area);
-  gtk_widget_set_events(colorsel->sample_area,
-               gtk_widget_get_events(colorsel->sample_area)
-               | GDK_BUTTON_MOTION_MASK
-               | GDK_BUTTON_PRESS_MASK
-               | GDK_BUTTON_RELEASE_MASK
-               | GDK_ENTER_NOTIFY_MASK
-               | GDK_LEAVE_NOTIFY_MASK);
-  gtk_widget_show (colorsel->sample_area);
-
-  gtk_signal_connect_after (GTK_OBJECT (colorsel->sample_area),
-                           "size_allocate",
-                           GTK_SIGNAL_FUNC (gtk_color_selection_sample_resize),
-                           colorsel->sample_area);
-  gtk_object_set_data (GTK_OBJECT (colorsel->sample_area), "_GtkColorSelection", (gpointer) colorsel);
-
-  table = gtk_table_new (NUM_CHANNELS, 3, FALSE);
-  gtk_table_set_col_spacings (GTK_TABLE (table), 3);
-  gtk_box_pack_start (GTK_BOX (hbox), table, FALSE, TRUE, 0);
-
-  for (n = HUE; n <= OPACITY; n++)
+  GtkColorSelection *colorsel = data;
+  ColorSelectionPrivate *priv;
+  guint16 *vals;
+  gdouble color[4];
+  priv = colorsel->private_data;
+  
+  /* This is currently a guint16 array of the format:
+   * R
+   * G
+   * B
+   * opacity
+   */
+  
+  if (selection_data->length < 0)
+    return;
+  
+  if ((selection_data->format != 16) ||
+      (selection_data->length != 8))
     {
-      label = gtk_label_new (scale_vals[n].label);
-      gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
-      gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, n, n + 1);
-
-      adj = gtk_adjustment_new (colorsel->values[n], scale_vals[n].lower,
-                               scale_vals[n].upper, scale_vals[n].step_inc,
-                                scale_vals[n].page_inc, 0.0);
-      colorsel->scales[n] = gtk_hscale_new (GTK_ADJUSTMENT (adj));
-      gtk_widget_set_usize (colorsel->scales[n], 128, 0);
-      gtk_scale_set_value_pos (GTK_SCALE (colorsel->scales[n]), GTK_POS_TOP);
-
-      gtk_range_set_update_policy (GTK_RANGE (colorsel->scales[n]), colorsel->policy);
-      gtk_scale_set_draw_value (GTK_SCALE (colorsel->scales[n]), FALSE);
-      gtk_scale_set_digits (GTK_SCALE (colorsel->scales[n]), 2);
-      gtk_table_attach_defaults (GTK_TABLE (table), colorsel->scales[n], 1, 2, n, n + 1);
-
-      colorsel->entries[n] = gtk_entry_new ();
-      gtk_widget_set_usize (colorsel->entries[n], 40, 0);
-      sprintf (txt, "%.2f", colorsel->values[n]);
-      gtk_entry_set_text (GTK_ENTRY (colorsel->entries[n]), txt);
-      gtk_table_attach_defaults (GTK_TABLE (table), colorsel->entries[n], 2, 3, n, n + 1);
-
-      if (n != OPACITY)
-       {
-         gtk_widget_show (label);
-         gtk_widget_show (colorsel->scales[n]);
-         gtk_widget_show (colorsel->entries[n]);
-       }
-
-      gtk_signal_connect_object (GTK_OBJECT (adj), "value_changed",
-                                 scale_vals[n].updater, (gpointer) colorsel->scales[n]);
-      gtk_object_set_data (GTK_OBJECT (colorsel->scales[n]), "_GtkColorSelection", (gpointer) colorsel);
-      gtk_object_set_data (GTK_OBJECT (colorsel->scales[n]), value_index_key, GINT_TO_POINTER (n));
-      gtk_signal_connect_object (GTK_OBJECT (colorsel->entries[n]), "changed",
-                                 scale_vals[n].updater, (gpointer) colorsel->entries[n]);
-      gtk_object_set_data (GTK_OBJECT (colorsel->entries[n]), "_GtkColorSelection", (gpointer) colorsel);
-      gtk_object_set_data (GTK_OBJECT (colorsel->entries[n]), value_index_key, GINT_TO_POINTER (n));
+      g_warning ("Received invalid color data\n");
+      return;
+    }
+  
+  vals = (guint16 *)selection_data->data;
+  
+  if (widget == priv->cur_sample)
+    {
+      color[0] = (gdouble)vals[0] / 0xffff;
+      color[1] = (gdouble)vals[1] / 0xffff;
+      color[2] = (gdouble)vals[2] / 0xffff;
+      color[3] = (gdouble)vals[3] / 0xffff;
+      
+      gtk_color_selection_set_color (colorsel, color);
     }
-
-  colorsel->opacity_label = label;
-
-  gtk_widget_show (table);
-  gtk_widget_show (hbox);
 }
 
-GtkWidget *
-gtk_color_selection_new (void)
+static void
+color_sample_drag_handle (GtkWidget        *widget,
+                         GdkDragContext   *context,
+                         GtkSelectionData *selection_data,
+                         guint             info,
+                         guint             time,
+                         gpointer          data)
 {
-  GtkColorSelection *colorsel;
-
-  colorsel = gtk_type_new (gtk_color_selection_get_type ());
-
-  return GTK_WIDGET (colorsel);
+  GtkColorSelection *colorsel = data;
+  ColorSelectionPrivate *priv;
+  guint16 vals[4];
+  gdouble *colsrc;
+  
+  priv = colorsel->private_data;
+  
+  if (widget == priv->old_sample)
+    colsrc = priv->old_color;
+  else
+    colsrc = priv->color;
+  
+  vals[0] = colsrc[COLORSEL_RED] * 0xffff;
+  vals[1] = colsrc[COLORSEL_GREEN] * 0xffff;
+  vals[2] = colsrc[COLORSEL_BLUE] * 0xffff;
+  vals[3] = priv->has_opacity ? colsrc[COLORSEL_OPACITY] * 0xffff : 0xffff;
+  
+  gtk_selection_data_set (selection_data,
+                         gdk_atom_intern ("application/x-color", FALSE),
+                         16, (guchar *)vals, 8);
 }
 
-void
-gtk_color_selection_set_update_policy (GtkColorSelection *colorsel,
-                                      GtkUpdateType      policy)
+/* which = 0 means draw old sample, which = 1 means draw new */
+static void
+color_sample_draw_sample (GtkColorSelection *colorsel, int which)
 {
-  gint n;
-
+  GtkWidget *da;
+  gint x, y, i, wid, heig, f, n, goff;
+  guchar c[3 * 2], cc[3 * 4], *cp = c;
+  gdouble o;
+  guchar *buf;
+  ColorSelectionPrivate *priv;
+  
   g_return_if_fail (colorsel != NULL);
-
-  if (policy != colorsel->policy)
+  priv = colorsel->private_data;
+  
+  g_return_if_fail (priv->sample_area != NULL);
+  if (!GTK_WIDGET_DRAWABLE (priv->sample_area))
+    return;
+  
+  if (which == 0)
     {
-      colorsel->policy = policy;
-
-      for (n = 0; n < NUM_CHANNELS; n++)
-       gtk_range_set_update_policy (GTK_RANGE (colorsel->scales[n]), policy);
+      da = priv->old_sample;
+      for (n = 0, i = COLORSEL_RED; n < 3; n++, i++)
+       c[n] = (guchar) (255.0 * priv->old_color[i]);
+      goff = 0;
     }
-}
-
-
-void
-gtk_color_selection_set_color (GtkColorSelection *colorsel,
-                              gdouble           *color)
-{
-  gint n, i = 0;
-
-  g_return_if_fail (colorsel != NULL);
-  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
-
-  if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (colorsel)))
-    gtk_color_selection_draw_wheel_marker (colorsel);
-
-  for (n = RED; n <= BLUE; n++)
+  else
     {
-      colorsel->old_values[n] = colorsel->values[n];
-      colorsel->values[n] = color[i++];
+      da = priv->cur_sample;
+      for (n = 0, i = COLORSEL_RED; n < 3; n++, i++)
+       c[n] = (guchar) (255.0 * priv->color[i]);
+      goff =  priv->old_sample->allocation.width % 32;
     }
-
-  if (colorsel->use_opacity)
+  
+  wid = da->allocation.width;
+  heig = da->allocation.height;
+  
+  buf = g_new(guchar, 3 * wid * heig);
+  
+#if 0
+  i = COLORSEL_RED;
+  for (n = 0; n < 3; n++)
     {
-      colorsel->old_values[OPACITY] = colorsel->values[OPACITY];
-      colorsel->values[OPACITY] = color[i];
+      c[n] = (guchar) (255.0 * priv->old_color[i]);
+      c[n + 3] = (guchar) (255.0 * priv->color[i++]);
     }
-
-  RGB_TO_HSV ();
-
-  gtk_color_selection_update_inputs (colorsel, RGB_INPUTS | HSV_INPUTS | OPACITY_INPUTS, BOTH);
-
-  if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (colorsel)))
+#endif
+  
+  if (priv->has_opacity)
+    {
+      o = (which) ? priv->color[COLORSEL_OPACITY] : priv->old_color[COLORSEL_OPACITY];
+      
+      for (n = 0; n < 3; n++)
+       {
+         cc[n] = (guchar) ((1.0 - o) * 192 + (o * (gdouble) c[n]));
+         cc[n + 3] = (guchar) ((1.0 - o) * 128 + (o * (gdouble) c[n]));
+       }
+      cp = cc;
+    }
+  
+  i = 0;
+  for (y = 0; y < heig; y++)
     {
-      gtk_color_selection_draw_value_bar (colorsel, FALSE);
-      gtk_color_selection_draw_sample (colorsel, FALSE);
-      gtk_color_selection_draw_wheel_marker (colorsel);
+      for (x = 0; x < wid; x++)
+       {
+         if (priv->has_opacity)
+           f = 3 * ((((goff + x) % 32) < 16) ^ ((y % 32) < 16));
+         else
+           f = 0;
+         
+         for (n = 0; n < 3; n++)
+           buf[i++] = cp[n + f];
+       }
     }
+  
+  gdk_draw_rgb_image(da->window,
+                    da->style->black_gc,
+                    0, 0,
+                    wid, heig,
+                    GDK_RGB_DITHER_NORMAL,
+                    buf,
+                    3*wid);
+  
+  
+  g_free (buf);
 }
 
-void
-gtk_color_selection_get_color (GtkColorSelection *colorsel,
-                              gdouble           *color)
-{
-  gint n, i = 0;
 
-  g_return_if_fail (colorsel != NULL);
-  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+static void
+color_sample_draw_samples (GtkColorSelection *colorsel)
+{
+  color_sample_draw_sample (colorsel, 0);
+  color_sample_draw_sample (colorsel, 1);
+}
 
-  for (n = RED; n <= BLUE; n++)
-    color[i++] = colorsel->values[n];
-  if (colorsel->use_opacity)
-    color[i] = colorsel->values[OPACITY];
+static void
+color_old_sample_expose(GtkWidget* da, GdkEventExpose* event, GtkColorSelection *colorsel)
+{
+  color_sample_draw_sample (colorsel, 0);
 }
 
+
 static void
-gtk_color_selection_realize (GtkWidget         *widget)
+color_cur_sample_expose(GtkWidget* da, GdkEventExpose* event, GtkColorSelection *colorsel)
 {
-  GtkColorSelection *colorsel;
+  color_sample_draw_sample (colorsel, 1);
+}
 
+static void
+color_sample_setup_dnd (GtkColorSelection *colorsel, GtkWidget *sample)
+{
   static const GtkTargetEntry targets[] = {
     { "application/x-color", 0 }
   };
-
-  g_return_if_fail (widget != NULL);
-  g_return_if_fail (GTK_IS_COLOR_SELECTION (widget));
-
-  colorsel = GTK_COLOR_SELECTION (widget);
-
-  if (GTK_WIDGET_CLASS (color_selection_parent_class)->realize)
-    (*GTK_WIDGET_CLASS (color_selection_parent_class)->realize) (widget);
-
-  gtk_drag_dest_set (colorsel->sample_area,
-                      GTK_DEST_DEFAULT_HIGHLIGHT |
-                      GTK_DEST_DEFAULT_MOTION |
-                      GTK_DEST_DEFAULT_DROP,
-                      targets, 1,
-                      GDK_ACTION_COPY);
-
-  gtk_drag_source_set (colorsel->sample_area, 
+  ColorSelectionPrivate *priv;
+  priv = colorsel->private_data;
+  
+  gtk_drag_source_set (sample,
                       GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
                       targets, 1,
                       GDK_ACTION_COPY | GDK_ACTION_MOVE);
-
-  gtk_signal_connect (GTK_OBJECT (colorsel->sample_area),
+  
+  gtk_signal_connect (GTK_OBJECT (sample),
                      "drag_begin",
-                     GTK_SIGNAL_FUNC (gtk_color_selection_drag_begin),
+                     GTK_SIGNAL_FUNC (color_sample_drag_begin),
                      colorsel);
-  gtk_signal_connect (GTK_OBJECT (colorsel->sample_area),
+  if (sample == priv->cur_sample)
+    {
+      
+      gtk_drag_dest_set (sample,
+                        GTK_DEST_DEFAULT_HIGHLIGHT |
+                        GTK_DEST_DEFAULT_MOTION |
+                        GTK_DEST_DEFAULT_DROP,
+                        targets, 1,
+                        GDK_ACTION_COPY);
+      
+      gtk_signal_connect (GTK_OBJECT (sample),
+                         "drag_end",
+                         GTK_SIGNAL_FUNC (color_sample_drag_end),
+                         colorsel);
+    }
+  
+  gtk_signal_connect (GTK_OBJECT (sample),
                      "drag_data_get",
-                     GTK_SIGNAL_FUNC (gtk_color_selection_drag_handle),
+                     GTK_SIGNAL_FUNC (color_sample_drag_handle),
                      colorsel);
-  gtk_signal_connect (GTK_OBJECT (colorsel->sample_area),
+  gtk_signal_connect (GTK_OBJECT (sample),
                      "drag_data_received",
-                     GTK_SIGNAL_FUNC (gtk_color_selection_drop_handle),
+                     GTK_SIGNAL_FUNC (color_sample_drop_handle),
                      colorsel);
+  
 }
 
+
 static void
-gtk_color_selection_unrealize (GtkWidget      *widget)
+color_sample_new (GtkColorSelection *colorsel)
 {
-  GtkColorSelection *colorsel;
-
-  g_return_if_fail (widget != NULL);
-  g_return_if_fail (GTK_IS_COLOR_SELECTION (widget));
+  ColorSelectionPrivate *priv;
+  
+  priv = colorsel->private_data;
+  
+  priv->sample_area = gtk_hbox_new (FALSE, 0);
+  priv->old_sample = gtk_drawing_area_new ();
+  priv->cur_sample = gtk_drawing_area_new ();
+
+  /* We need enter/leave to do tooltips */
+  gtk_widget_add_events (priv->old_sample,
+                         GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
+  gtk_widget_add_events (priv->cur_sample,
+                         GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
+  
+  gtk_box_pack_start (GTK_BOX (priv->sample_area), priv->old_sample,
+                     TRUE, TRUE, 0);
+  gtk_box_pack_start (GTK_BOX (priv->sample_area), priv->cur_sample,
+                     TRUE, TRUE, 0);
+  
+  gtk_signal_connect(GTK_OBJECT (priv->old_sample), "expose_event",
+                    GTK_SIGNAL_FUNC (color_old_sample_expose),
+                    colorsel);
+  gtk_signal_connect(GTK_OBJECT (priv->cur_sample), "expose_event",
+                    GTK_SIGNAL_FUNC (color_cur_sample_expose),
+                    colorsel);
+  
+  color_sample_setup_dnd (colorsel, priv->old_sample);
+  color_sample_setup_dnd (colorsel, priv->cur_sample);
 
-  colorsel = GTK_COLOR_SELECTION (widget);
+  gtk_tooltips_set_tip (priv->tooltips,
+                        priv->old_sample,
+                        _("The previously-selected color, for comparison to the color you're selecting now. You can drag this color to a palette entry, or select this color as current by dragging it to the other color swatch alongside."), NULL);
 
-  if (colorsel->value_gc != NULL)
-    {
-      gdk_gc_unref (colorsel->value_gc);
-      colorsel->value_gc = NULL;
-    }
-  if (colorsel->wheel_gc != NULL)
-    {
-      gdk_gc_unref (colorsel->wheel_gc);
-      colorsel->wheel_gc = NULL;
-    }
-  if (colorsel->sample_gc != NULL)
-    {
-      gdk_gc_unref (colorsel->sample_gc);
-      colorsel->sample_gc = NULL;
-    }
 
-  (* GTK_WIDGET_CLASS (color_selection_parent_class)->unrealize) (widget);
+  gtk_tooltips_set_tip (priv->tooltips,
+                        priv->cur_sample,
+                        _("The color you've chosen. You can drag this color to a palette entry to save it for use in the future."), NULL);
+  
+  gtk_widget_show_all (priv->sample_area);
 }
 
-static void
-gtk_color_selection_finalize (GtkObject *object)
-{
-  GtkColorSelection *colorsel;
-
-  g_return_if_fail (object != NULL);
-  g_return_if_fail (GTK_IS_COLOR_SELECTION (object));
 
-  colorsel = GTK_COLOR_SELECTION (object);
-
-  if (colorsel->wheel_buf != NULL)
-    g_free (colorsel->wheel_buf);
-  if (colorsel->value_buf != NULL)
-    g_free (colorsel->value_buf);
-  if (colorsel->sample_buf != NULL)
-    g_free (colorsel->sample_buf);
-
-  (*GTK_OBJECT_CLASS (color_selection_parent_class)->finalize) (object);
-}
+/*
+ *
+ * The palette area code
+ *
+ */
+#define CUSTOM_PALETTE_ENTRY_WIDTH   20
+#define CUSTOM_PALETTE_ENTRY_HEIGHT  20
 
 static void
-gtk_color_selection_color_changed (GtkColorSelection *colorsel)
+palette_get_color (GtkWidget *drawing_area, gdouble *color)
 {
-  gtk_signal_emit (GTK_OBJECT (colorsel), color_selection_signals[COLOR_CHANGED]);
+  gdouble *color_val;
+  
+  g_return_if_fail (color != NULL);
+  
+  color_val = gtk_object_get_data (GTK_OBJECT (drawing_area), "color_val");
+  if (color_val == NULL)
+    {
+      /* Default to white for no good reason */
+      color[0] = 1.0;
+      color[1] = 1.0;
+      color[2] = 1.0;
+      color[3] = 1.0;
+      return;
+    }
+  
+  color[0] = color_val[0];
+  color[1] = color_val[1];
+  color[2] = color_val[2];
+  color[3] = 1.0;
 }
 
+#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
 static void
-gtk_color_selection_update_input (GtkWidget *scale,
-                                  GtkWidget *entry,
-                                  gdouble    value)
+palette_paint (GtkWidget    *drawing_area,
+              GdkRectangle *area,
+              gpointer      data)
 {
-  GtkAdjustment *adj;
-  gchar txt[32];
-
-  if (scale != NULL)
+  if (drawing_area->window == NULL)
+    return;
+  
+  gdk_window_clear_area (drawing_area->window,
+                        area->x,
+                        area->y,
+                        area->width, 
+                        area->height);
+  
+  if (GTK_WIDGET_HAS_FOCUS (drawing_area))
     {
-      adj = gtk_range_get_adjustment (GTK_RANGE (scale));
-      adj->value = (gfloat) value;
-      gtk_signal_handler_block_by_data (GTK_OBJECT (adj), (gpointer) scale);
-      gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
-      gtk_range_slider_update (GTK_RANGE (scale));
-      gtk_signal_handler_unblock_by_data (GTK_OBJECT (adj), (gpointer) scale);
-    }
+      GdkGC *gc;
+      gdouble color[4];
+      
+      palette_get_color (drawing_area, color);
+      
+      if (INTENSITY (color[0], color[1], color[2]) > 0.5)
+       gc = drawing_area->style->black_gc;
+      else
+       gc = drawing_area->style->white_gc;
 
-  if (entry != NULL)
-    {
-      gtk_signal_handler_block_by_data (GTK_OBJECT (entry), (gpointer) entry);
-      sprintf (txt, "%.2f", value);
-      gtk_entry_set_text (GTK_ENTRY (entry), txt);
-      gtk_signal_handler_unblock_by_data (GTK_OBJECT (entry), (gpointer) entry);
+      gdk_draw_rectangle (drawing_area->window,
+                         gc, FALSE, 0, 0,
+                         drawing_area->allocation.width - 1,
+                         drawing_area->allocation.height - 1);
     }
 }
 
+
 static void
-gtk_color_selection_update_inputs (GtkColorSelection *colorsel,
-                                   gint               inputs,
-                                   gint               which)
+palette_drag_begin (GtkWidget      *widget,
+                   GdkDragContext *context,
+                   gpointer        data)
 {
-  gint n;
-
-  switch (which)
-    {
-    case SCALE:
-      if ((inputs & RGB_INPUTS) != 0)
-       for (n = RED; n <= BLUE; n++)
-         gtk_color_selection_update_input (colorsel->scales[n], NULL,
-                                           colorsel->values[n]);
-      if ((inputs & HSV_INPUTS) != 0)
-       for (n = HUE; n <= VALUE; n++)
-         gtk_color_selection_update_input (colorsel->scales[n], NULL,
-                                           colorsel->values[n]);
-      if ((inputs & OPACITY_INPUTS) != 0)
-       gtk_color_selection_update_input(colorsel->scales[OPACITY], NULL,
-                                        colorsel->values[OPACITY]);
-      break;
-    case ENTRY:
-      if ((inputs & RGB_INPUTS) != 0)
-       for (n = RED; n <= BLUE; n++)
-         gtk_color_selection_update_input (NULL, colorsel->entries[n], colorsel->values[n]);
-      if ((inputs & HSV_INPUTS) != 0)
-       for (n = HUE; n <= VALUE; n++)
-         gtk_color_selection_update_input (NULL, colorsel->entries[n], colorsel->values[n]);
-      if ((inputs & OPACITY_INPUTS) != 0)
-       gtk_color_selection_update_input(NULL, colorsel->entries[OPACITY], colorsel->values[OPACITY]);
-      break;
-    default:
-      if ((inputs & RGB_INPUTS) != 0)
-       for (n = RED; n <= BLUE; n++)
-         gtk_color_selection_update_input (colorsel->scales[n], colorsel->entries[n],
-                                            colorsel->values[n]);
-      if ((inputs & HSV_INPUTS) != 0)
-       for (n = HUE; n <= VALUE; n++)
-         gtk_color_selection_update_input (colorsel->scales[n], colorsel->entries[n],
-                                            colorsel->values[n]);
-      if ((inputs & OPACITY_INPUTS) != 0)
-       gtk_color_selection_update_input(colorsel->scales[OPACITY], colorsel->entries[OPACITY],
-                                        colorsel->values[OPACITY]);
-      break;
-    }
+  GtkColorSelection *colorsel = data;
+  ColorSelectionPrivate *priv;
+  GtkWidget *window;
+  gdouble colors[4];
+  GdkColor bg;
+  
+  priv = colorsel->private_data;
+  window = gtk_window_new (GTK_WINDOW_POPUP);
+  gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
+  gtk_widget_set_usize (window, 48, 32);
+  gtk_widget_realize (window);
+  gtk_object_set_data_full (GTK_OBJECT (widget),
+                           "gtk-color-selection-drag-window",
+                           window,
+                           (GtkDestroyNotify) gtk_widget_destroy);
+  
+  palette_get_color (widget, colors);
+  bg.red = 0xffff * colors[0];
+  bg.green = 0xffff * colors[1];
+  bg.blue = 0xffff * colors[2];
+  
+  gdk_color_alloc (gtk_widget_get_colormap (window), &bg);
+  gdk_window_set_background (window->window, &bg);
+  
+  gtk_drag_set_icon_widget (context, window, -2, -2);
 }
 
 static void
-gtk_color_selection_hsv_updater (GtkWidget *widget,
-                                 gpointer   data)
+palette_drag_handle (GtkWidget        *widget,
+                    GdkDragContext   *context,
+                    GtkSelectionData *selection_data,
+                    guint             info,
+                    guint             time,
+                    gpointer          data)
 {
-  GtkColorSelection *colorsel;
-  GtkAdjustment *adj;
-  gdouble newvalue;
-  gint i, which = SCALE;
-
-  if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (widget)))
-    {
-      colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (widget), "_GtkColorSelection");
-      i = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (widget), value_index_key));
-
-      if (GTK_IS_SCALE (widget))
-       {
-         adj = gtk_range_get_adjustment (GTK_RANGE (GTK_SCALE (widget)));
-         newvalue = (gdouble) adj->value;
-         which = ENTRY;
-       }
-      else
-       newvalue = (gdouble) atof (gtk_entry_get_text (GTK_ENTRY (widget)));
-
-      if (i == VALUE)
-       {
-         gtk_color_selection_draw_value_marker (colorsel);
-         colorsel->values[i] = newvalue;
-
-         HSV_TO_RGB ();
-
-         gtk_color_selection_draw_value_marker (colorsel);
-       }
-      else
-       {
-         gtk_color_selection_draw_wheel_marker (colorsel);
-         colorsel->values[i] = newvalue;
-
-         HSV_TO_RGB ();
-
-         gtk_color_selection_draw_wheel_marker (colorsel);
-         gtk_color_selection_draw_value_bar (colorsel, FALSE);
-       }
+  guint16 vals[4];
+  gdouble colsrc[4];
+  
+  palette_get_color (widget, colsrc);
+  
+  vals[0] = colsrc[COLORSEL_RED] * 0xffff;
+  vals[1] = colsrc[COLORSEL_GREEN] * 0xffff;
+  vals[2] = colsrc[COLORSEL_BLUE] * 0xffff;
+  vals[3] = 0xffff;
+  
+  gtk_selection_data_set (selection_data,
+                         gdk_atom_intern ("application/x-color", FALSE),
+                         16, (guchar *)vals, 8);
+}
 
-      gtk_color_selection_draw_sample (colorsel, FALSE);
-      gtk_color_selection_color_changed (colorsel);
-      gtk_color_selection_update_inputs (colorsel, HSV_INPUTS, which);
-      gtk_color_selection_update_inputs (colorsel, RGB_INPUTS, BOTH);
-    }
+static void
+palette_drag_end (GtkWidget      *widget,
+                 GdkDragContext *context,
+                 gpointer        data)
+{
+  gtk_object_set_data (GTK_OBJECT (widget), "gtk-color-selection-drag-window", NULL);
 }
 
+/* Changes the model color */
 static void
-gtk_color_selection_rgb_updater (GtkWidget *widget,
-                                 gpointer   data)
+palette_change_color (GtkWidget         *drawing_area,
+                      GtkColorSelection *colorsel,
+                      gdouble           *color)
 {
-  GtkColorSelection *colorsel;
-  GtkAdjustment *adj;
-  gdouble newvalue;
-  gint i, which = SCALE;
+  gint x, y;
+  ColorSelectionPrivate *priv;
+  GdkColor gdk_color;
 
-  if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (widget)))
-    {
-      colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (widget), "_GtkColorSelection");
-      i = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (widget), value_index_key));
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  g_return_if_fail (GTK_IS_DRAWING_AREA (drawing_area));
+  
+  priv = colorsel->private_data;
+  
+  gdk_color.red = color[0]*65535;
+  gdk_color.green = color[1]*65535;
+  gdk_color.blue = color[2]*65535;
 
-      if (GTK_IS_SCALE (widget))
-       {
-         adj = gtk_range_get_adjustment (GTK_RANGE (GTK_SCALE (widget)));
-         newvalue = (gdouble) adj->value;
-         which = ENTRY;
-       }
-      else
-       newvalue = (gdouble) atof (gtk_entry_get_text (GTK_ENTRY (widget)));
+  x = 0;
+  while (x < GTK_CUSTOM_PALETTE_WIDTH)
+    {
+      y = 0;
+      while (y < GTK_CUSTOM_PALETTE_HEIGHT)
+        {
+          if (priv->custom_palette[x][y] == drawing_area)
+            goto out;
+          
+          ++y;
+        }
+
+      ++x;
+    }
 
-      gtk_color_selection_draw_wheel_marker (colorsel);
+ out:
+  
+  g_assert (x < GTK_CUSTOM_PALETTE_WIDTH || y < GTK_CUSTOM_PALETTE_HEIGHT);
 
-      colorsel->values[i] = newvalue;
-      RGB_TO_HSV ();
+  current_colors[y * GTK_CUSTOM_PALETTE_WIDTH + x] = gdk_color;
 
-      gtk_color_selection_draw_wheel_marker (colorsel);
-      gtk_color_selection_draw_value_bar (colorsel, FALSE);
-      gtk_color_selection_draw_sample (colorsel, FALSE);
-      gtk_color_selection_color_changed (colorsel);
-      gtk_color_selection_update_inputs (colorsel, RGB_INPUTS, which);
-      gtk_color_selection_update_inputs (colorsel, HSV_INPUTS, BOTH);
-    }
+  if (change_palette_hook)
+    (* change_palette_hook) (current_colors, GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT);
 }
 
+/* Changes the view color */
 static void
-gtk_color_selection_opacity_updater (GtkWidget *widget,
-                                     gpointer   data)
+palette_set_color (GtkWidget         *drawing_area,
+                  GtkColorSelection *colorsel,
+                  gdouble           *color)
 {
-  GtkColorSelection *colorsel;
-  GtkAdjustment *adj;
-
-  colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (widget), "_GtkColorSelection");
+  gdouble *new_color = g_new (double, 4);
+  gdouble *old_color;
+  GdkColor gdk_color;
+  
+  gdk_color.red = color[0]*65535;
+  gdk_color.green = color[1]*65535;
+  gdk_color.blue = color[2]*65535;
 
-  if (GTK_IS_SCALE (widget))
+  gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, &gdk_color);
+  
+  if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (drawing_area), "color_set")) == 0)
     {
-      adj = gtk_range_get_adjustment (GTK_RANGE (widget));
-      colorsel->values[OPACITY] = (gdouble) adj->value;
-      gtk_color_selection_update_input (NULL, colorsel->entries[OPACITY], colorsel->values[OPACITY]);
+      static const GtkTargetEntry targets[] = {
+       { "application/x-color", 0 }
+      };
+      gtk_drag_source_set (drawing_area,
+                          GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
+                          targets, 1,
+                          GDK_ACTION_COPY | GDK_ACTION_MOVE);
+      
+      gtk_signal_connect (GTK_OBJECT (drawing_area),
+                         "drag_begin",
+                         GTK_SIGNAL_FUNC (palette_drag_begin),
+                         colorsel);
+      gtk_signal_connect (GTK_OBJECT (drawing_area),
+                         "drag_data_get",
+                         GTK_SIGNAL_FUNC (palette_drag_handle),
+                         colorsel);
+      
+      gtk_object_set_data (GTK_OBJECT (drawing_area), "color_set", GINT_TO_POINTER (1));
     }
   else
     {
-      colorsel->values[OPACITY] = (gdouble) atof (gtk_entry_get_text (GTK_ENTRY (widget)));
-      gtk_color_selection_update_input (colorsel->scales[OPACITY], NULL, colorsel->values[OPACITY]);
+      old_color = (gdouble *) gtk_object_get_data (GTK_OBJECT (drawing_area), "color_val");
+      if (old_color)
+       {
+         g_free (old_color);
+       }
     }
+  new_color[0] = color[0];
+  new_color[1] = color[1];
+  new_color[2] = color[2];
+  new_color[3] = 1.0;
+  
+  gtk_object_set_data (GTK_OBJECT (drawing_area), "color_val", new_color);
+}
 
-  gtk_color_selection_draw_sample (colorsel, FALSE);
-  gtk_color_selection_color_changed (colorsel);
+static void
+palette_expose (GtkWidget      *drawing_area,
+               GdkEventExpose *event,
+               gpointer        data)
+{
+  if (drawing_area->window == NULL)
+    return;
+  
+  palette_paint (drawing_area, &(event->area), data);
 }
 
-void
-gtk_color_selection_set_opacity (GtkColorSelection *colorsel,
-                                 gint               use_opacity)
+static void
+popup_position_func (GtkMenu   *menu,
+                     gint      *x,
+                     gint      *y,
+                     gboolean  *push_in,
+                     gpointer  user_data)
 {
-  g_return_if_fail (colorsel != NULL);
+  GtkWidget *widget;
+  GtkRequisition req;      
+  gint root_x, root_y;
+  
+  widget = GTK_WIDGET (user_data);
+  
+  g_return_if_fail (GTK_WIDGET_REALIZED (widget));
 
-  colorsel->use_opacity = use_opacity;
+  gdk_window_get_origin (widget->window, &root_x, &root_y);
+  
+  gtk_widget_size_request (GTK_WIDGET (menu), &req);
 
-  if (!use_opacity && GTK_WIDGET_VISIBLE (colorsel->scales[OPACITY]))
-    {
-      gtk_widget_hide (colorsel->opacity_label);
-      gtk_widget_hide (colorsel->scales[OPACITY]);
-      gtk_widget_hide (colorsel->entries[OPACITY]);
-    }
-  else if (use_opacity && !GTK_WIDGET_VISIBLE (colorsel->scales[OPACITY]))
-    {
-      gtk_widget_show (colorsel->opacity_label);
-      gtk_widget_show (colorsel->scales[OPACITY]);
-      gtk_widget_show (colorsel->entries[OPACITY]);
-    }
+  /* Put corner of menu centered on color cell */
+  *x = root_x + widget->allocation.width / 2;
+  *y = root_y + widget->allocation.height / 2;
 
-  if (GTK_WIDGET_DRAWABLE (colorsel->sample_area))
-    gtk_color_selection_draw_sample (colorsel, FALSE);
+  /* Ensure sanity */
+  *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
+  *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
 }
 
 static void
-gtk_color_selection_value_resize (GtkWidget *widget,
-                                  gpointer   data)
+save_color_selected (GtkWidget *menuitem,
+                     gpointer   data)
 {
   GtkColorSelection *colorsel;
+  GtkWidget *drawing_area;
+  ColorSelectionPrivate *priv;
+
+  drawing_area = GTK_WIDGET (data);
+  
+  colorsel = GTK_COLOR_SELECTION (g_object_get_data (G_OBJECT (drawing_area),
+                                                     "gtk-color-sel"));
 
-  colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (widget), "_GtkColorSelection");
-  gtk_color_selection_draw_value_bar (colorsel, TRUE);
+  priv = colorsel->private_data;
+  
+  palette_change_color (drawing_area, colorsel, priv->color);  
 }
 
 static void
-gtk_color_selection_wheel_resize (GtkWidget *widget,
-                                  gpointer   data)
+do_popup (GtkColorSelection *colorsel,
+          GtkWidget         *drawing_area,
+          guint32            timestamp)
 {
-  GtkColorSelection *colorsel;
+  GtkWidget *menu;
+  GtkWidget *mi;
+  GtkWidget *label;
+  
+  g_object_set_data (G_OBJECT (drawing_area),
+                     "gtk-color-sel",
+                     colorsel);
+  
+  menu = gtk_menu_new ();
+
+  label = gtk_label_new_with_mnemonic (_("_Save color here"));
+  mi = gtk_menu_item_new ();
+  gtk_container_add (GTK_CONTAINER (mi), label);
 
-  colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (widget), "_GtkColorSelection");
-  gtk_color_selection_draw_wheel (colorsel, TRUE);
+  gtk_signal_connect (GTK_OBJECT (mi), "activate",
+                      GTK_SIGNAL_FUNC (save_color_selected),
+                      drawing_area);
+  
+  gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
+
+  gtk_widget_show_all (mi);
+
+  gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
+                  popup_position_func, drawing_area,
+                  3, timestamp);
 }
 
-static void
-gtk_color_selection_sample_resize (GtkWidget *widget,
-                                   gpointer   data)
+
+static gint
+palette_press (GtkWidget      *drawing_area,
+              GdkEventButton *event,
+              gpointer        data)
 {
-  GtkColorSelection *colorsel;
+  GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
 
-  colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (widget), "_GtkColorSelection");
-  gtk_color_selection_draw_sample (colorsel, TRUE);
+  gtk_widget_grab_focus (drawing_area);
+  
+  if (event->button == 1 &&
+      event->type == GDK_BUTTON_PRESS)
+    {      
+      if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (drawing_area), "color_set")) != 0)
+        {
+          gdouble color[4];
+          palette_get_color (drawing_area, color);
+          gtk_color_selection_set_color (GTK_COLOR_SELECTION (data), color);
+        }
+    }
+  
+  if (event->button == 3 &&
+      event->type == GDK_BUTTON_PRESS)
+    {
+      do_popup (colorsel, drawing_area, event->time);
+    }
+  
+  return TRUE;
 }
 
 static void
-gtk_color_selection_drag_begin (GtkWidget      *widget,
-                               GdkDragContext *context,
-                               gpointer        data)
+palette_unset_color (GtkWidget *drawing_area)
 {
-  GtkColorSelection *colorsel = data;
-  GtkWidget *window;
-  gdouble colors[4];
-  GdkColor bg;
-
-  window = gtk_window_new(GTK_WINDOW_POPUP);
-  gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
-  gtk_widget_set_usize (window, 48, 32);
-  gtk_widget_realize (window);
-
-  gtk_color_selection_get_color (colorsel, colors);
-  bg.red = 0xffff * colors[0];
-  bg.green = 0xffff * colors[1];
-  bg.blue = 0xffff * colors[2];
-
-  gdk_color_alloc (gtk_widget_get_colormap (window), &bg);
-  gdk_window_set_background (window->window, &bg);
-
-  gtk_drag_set_icon_widget (context, window, -2, -2);
+  if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (drawing_area), "color_set")) == 0)
+    return;
+  
+  gtk_widget_reset_rc_styles (drawing_area);
+  gtk_object_set_data (GTK_OBJECT (drawing_area), "color_set", GINT_TO_POINTER (0));
 }
 
 static void
-gtk_color_selection_drop_handle (GtkWidget        *widget, 
-                                GdkDragContext   *context,
-                                gint              x,
-                                gint              y,
-                                GtkSelectionData *selection_data,
-                                guint             info,
-                                guint             time,
-                                gpointer          data)
+palette_drop_handle (GtkWidget        *widget,
+                    GdkDragContext   *context,
+                    gint              x,
+                    gint              y,
+                    GtkSelectionData *selection_data,
+                    guint             info,
+                    guint             time,
+                    gpointer          data)
 {
-  GtkColorSelection *colorsel = data;
   guint16 *vals;
-  gdouble colors[4];
-
-  /* This is currently a guint16 array of the format:
-     R
-     G
-     B
-     opacity
-  */
-
+  gdouble color[4];
+  
   if (selection_data->length < 0)
     return;
-
-  if ((selection_data->format != 16) || 
+  
+  if ((selection_data->format != 16) ||
       (selection_data->length != 8))
     {
       g_warning ("Received invalid color data\n");
@@ -910,753 +940,1666 @@ gtk_color_selection_drop_handle (GtkWidget        *widget,
     }
   
   vals = (guint16 *)selection_data->data;
+  
+  color[0] = (gdouble)vals[0] / 0xffff;
+  color[1] = (gdouble)vals[1] / 0xffff;
+  color[2] = (gdouble)vals[2] / 0xffff;
+  color[3] = (gdouble)vals[3] / 0xffff;
+  palette_change_color (widget, GTK_COLOR_SELECTION (data), color);
+  gtk_color_selection_set_color (GTK_COLOR_SELECTION (data), color);
+}
 
-  colors[0] = (gdouble)vals[0] / 0xffff;
-  colors[1] = (gdouble)vals[1] / 0xffff;
-  colors[2] = (gdouble)vals[2] / 0xffff;
-  colors[3] = (gdouble)vals[3] / 0xffff;
+static gint
+palette_activate (GtkWidget   *widget,
+                 GdkEventKey *event,
+                 gpointer     data)
+{
+  /* should have a drawing area subclass with an activate signal */
+  if ((event->keyval == ' ') || (event->keyval == GDK_Return))
+    {
+      if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (widget), "color_set")) != 0)
+        {
+          gdouble color[4];
+          palette_get_color (widget, color);
+          gtk_color_selection_set_color (GTK_COLOR_SELECTION (data), color);
+        }
+      return TRUE;
+    }
   
-  gtk_color_selection_set_color(colorsel, colors);
+  return FALSE;
 }
 
 static void
-gtk_color_selection_drag_handle (GtkWidget        *widget, 
-                                GdkDragContext   *context,
-                                GtkSelectionData *selection_data,
-                                guint             info,
-                                guint             time,
-                                gpointer          data)
+palette_popup (GtkWidget *widget,
+               gpointer   data)
 {
-  GtkColorSelection *colorsel = data;
-  guint16 vals[4];
-  gdouble colors[4];
-
-  gtk_color_selection_get_color(colorsel, colors);
+  GtkColorSelection *colorsel = GTK_COLOR_SELECTION (data);
 
-  vals[0] = colors[0] * 0xffff;
-  vals[1] = colors[1] * 0xffff;
-  vals[2] = colors[2] * 0xffff;
-  vals[3] = colorsel->use_opacity ? colors[3] * 0xffff : 0xffff;
-
-  g_print ("%x %x\n", vals[0], vals[4]);
-  g_print ("%g\n", colorsel->values[OPACITY]);
-  g_print ("%d\n", colorsel->use_opacity);
-
-  gtk_selection_data_set (selection_data,
-                         gdk_atom_intern ("application/x-color", FALSE),
-                         16, (guchar *)vals, 8);
+  do_popup (colorsel, widget, GDK_CURRENT_TIME);
 }
+               
 
-static void
-gtk_color_selection_draw_wheel_marker (GtkColorSelection *colorsel)
+static GtkWidget*
+palette_new (GtkColorSelection *colorsel)
 {
-  gint xpos, ypos;
+  GtkWidget *retval;
+  ColorSelectionPrivate *priv;
+  
+  static const GtkTargetEntry targets[] = {
+    { "application/x-color", 0 }
+  };
 
-  gdk_gc_set_function (colorsel->wheel_gc, GDK_INVERT);
+  priv = colorsel->private_data;
+  
+  retval = gtk_drawing_area_new ();
 
-  xpos = (gint) ((-(gdouble) (colorsel->wheel_area->allocation.width) / 2.0) *
-                colorsel->values[SATURATION] * cos (DEGTORAD ((colorsel->values[HUE] - 90)))) +
-                 (colorsel->wheel_area->allocation.width >> 1) - 4;
-  ypos = (gint) (((gdouble) (colorsel->wheel_area->allocation.height) / 2.0) *
-                colorsel->values[SATURATION] * sin (DEGTORAD ((colorsel->values[HUE] - 90)))) +
-                 (colorsel->wheel_area->allocation.height >> 1) - 4;
+  GTK_WIDGET_SET_FLAGS (retval, GTK_CAN_FOCUS);
+  
+  gtk_object_set_data (GTK_OBJECT (retval), "color_set", GINT_TO_POINTER (0)); 
+  gtk_widget_set_events (retval, GDK_BUTTON_PRESS_MASK
+                         | GDK_BUTTON_RELEASE_MASK
+                         | GDK_EXPOSURE_MASK
+                         | GDK_ENTER_NOTIFY_MASK
+                         | GDK_LEAVE_NOTIFY_MASK);
+  
 
-  gdk_draw_arc (colorsel->wheel_area->window, colorsel->wheel_gc, FALSE, xpos, ypos, 8, 8, 0, 360 * 64);
+  gtk_signal_connect (GTK_OBJECT (retval), "expose_event",
+                      GTK_SIGNAL_FUNC (palette_expose), colorsel);
+  gtk_signal_connect (GTK_OBJECT (retval), "button_press_event",
+                      GTK_SIGNAL_FUNC (palette_press), colorsel);
+  gtk_signal_connect (GTK_OBJECT (retval), "key_press_event",
+                      GTK_SIGNAL_FUNC (palette_activate), colorsel);
+  gtk_signal_connect (GTK_OBJECT (retval), "popup_menu",
+                      GTK_SIGNAL_FUNC (palette_popup), colorsel);
+  
+  gtk_drag_dest_set (retval,
+                    GTK_DEST_DEFAULT_HIGHLIGHT |
+                    GTK_DEST_DEFAULT_MOTION |
+                    GTK_DEST_DEFAULT_DROP,
+                    targets, 1,
+                    GDK_ACTION_COPY);
+  
+  gtk_signal_connect (GTK_OBJECT (retval), "drag_end",
+                      GTK_SIGNAL_FUNC (palette_drag_end), NULL);
+  gtk_signal_connect (GTK_OBJECT (retval), "drag_data_received",
+                      GTK_SIGNAL_FUNC (palette_drop_handle), colorsel);
+
+  gtk_tooltips_set_tip (priv->tooltips,
+                        retval,
+                        _("Click this palette entry to make it the current color. To change this entry, drag a color swatch here or right-click it and select \"Save color here.\""),
+                        NULL);
+  return retval;
 }
 
-static void
-gtk_color_selection_draw_value_marker (GtkColorSelection *colorsel)
-{
-  gint y;
 
-  gdk_gc_set_function (colorsel->value_gc, GDK_INVERT);
+/*
+ *
+ * The actual GtkColorSelection widget
+ *
+ */
 
-  y = (gint) ((gdouble) (colorsel->value_area->allocation.height) * (1.0 - colorsel->values[VALUE]));
-  gdk_draw_line (colorsel->value_area->window, colorsel->value_gc,
-                 0, y, colorsel->value_area->allocation.width, y);
+static void
+initialize_cursor (void)
+{
+  GdkColor fg, bg;
+  
+  GdkPixmap *pixmap =
+    gdk_bitmap_create_from_data (NULL,
+                                dropper_bits,
+                                DROPPER_WIDTH, DROPPER_HEIGHT);
+  GdkPixmap *mask =
+    gdk_bitmap_create_from_data (NULL,
+                                dropper_mask,
+                                DROPPER_WIDTH, DROPPER_HEIGHT);
+  
+  gdk_color_white (gdk_colormap_get_system (), &bg);
+  gdk_color_black (gdk_colormap_get_system (), &fg);
+  
+  picker_cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, DROPPER_X_HOT ,DROPPER_Y_HOT);
+  
+  gdk_pixmap_unref (pixmap);
+  gdk_pixmap_unref (mask);
+  
 }
 
 static void
-gtk_color_selection_update_value (GtkColorSelection *colorsel,
-                                  gint               y)
+grab_color_at_mouse (GtkWidget *invisible,
+                    gint       x_root,
+                    gint       y_root,
+                    gpointer   data)
 {
-  gtk_color_selection_draw_value_marker (colorsel);
+  GdkImage *image;
+  guint32 pixel;
+  GtkColorSelection *colorsel = data;
+  ColorSelectionPrivate *priv;
+  GdkColormap *colormap = gdk_colormap_get_system ();
+  GdkColor color;
+  
+  priv = colorsel->private_data;
+  
+  image = gdk_image_get (GDK_ROOT_PARENT (), x_root, y_root, 1, 1);
+  pixel = gdk_image_get_pixel (image, 0, 0);
+  gdk_image_unref (image);
 
-  if (y < 0)
-    y = 0;
-  else if (y > colorsel->value_area->allocation.height - 1)
-    y = colorsel->value_area->allocation.height - 1;
+  gdk_colormap_query_color (colormap, pixel, &color);
+  
+  priv->color[COLORSEL_RED] = (double)color.red / 65535.0;
+  priv->color[COLORSEL_GREEN] = (double)color.green / 65535.0;
+  priv->color[COLORSEL_BLUE] = (double)color.blue / 65535.0;
+  
+  gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
+                 priv->color[COLORSEL_GREEN],
+                 priv->color[COLORSEL_BLUE],
+                 &priv->color[COLORSEL_HUE],
+                 &priv->color[COLORSEL_SATURATION],
+                 &priv->color[COLORSEL_VALUE]);
+
+  update_color (colorsel);
+}
 
-  colorsel->values[VALUE] = 1.0 - (gdouble) y / (gdouble) (colorsel->value_area->allocation.height);
+static void
+shutdown_eyedropper (GtkWidget *widget)
+{
+  GtkColorSelection *colorsel;
+  ColorSelectionPrivate *priv;
 
-  HSV_TO_RGB ();
+  colorsel = GTK_COLOR_SELECTION (widget);
+  priv = colorsel->private_data;    
 
-  gtk_color_selection_draw_value_marker (colorsel);
-  gtk_color_selection_draw_sample (colorsel, FALSE);
-  gtk_color_selection_update_input (colorsel->scales[VALUE], colorsel->entries[VALUE],
-                                   colorsel->values[VALUE]);
-  gtk_color_selection_update_inputs (colorsel, RGB_INPUTS, BOTH);
+  gdk_keyboard_ungrab (gtk_get_current_event_time ());
+  gdk_pointer_ungrab (gtk_get_current_event_time ());
+  gtk_grab_remove (priv->dropper_grab_widget);
 }
 
 static void
-gtk_color_selection_update_wheel (GtkColorSelection *colorsel,
-                                  gint               x,
-                                  gint               y)
+mouse_motion (GtkWidget      *invisible,
+             GdkEventMotion *event,
+             gpointer        data)
 {
-  gdouble wid, heig;
-  gint res;
-
-  gtk_color_selection_draw_wheel_marker (colorsel);
-
-  wid = (gdouble) (colorsel->wheel_area->allocation.width) / 2.0;
-  heig = (gdouble) (colorsel->wheel_area->allocation.height) / 2.0;
+  grab_color_at_mouse (invisible, event->x_root, event->y_root, data); 
+}
 
-  res = gtk_color_selection_eval_wheel (x, y, wid, heig, &colorsel->values[HUE],
-                                        &colorsel->values[SATURATION]);
+static gboolean
+mouse_release (GtkWidget      *invisible,
+              GdkEventButton *event,
+              gpointer        data)
+{
+  GtkColorSelection *colorsel = data;
+  ColorSelectionPrivate *priv;
+  priv = colorsel->private_data;  
 
-  HSV_TO_RGB ();
+  if (event->button != 1)
+    return FALSE;
 
-  gtk_color_selection_draw_wheel_marker (colorsel);
-  gtk_color_selection_draw_value_bar (colorsel, FALSE);
-  gtk_color_selection_draw_sample (colorsel, FALSE);
-  gtk_color_selection_update_inputs (colorsel, RGB_INPUTS | HSV_INPUTS, BOTH);
-}
+  grab_color_at_mouse (invisible, event->x_root, event->y_root, data);
 
-static gint
-gtk_color_selection_value_timeout (GtkColorSelection *colorsel)
-{
-  gint x, y;
-  
-  GDK_THREADS_ENTER ();
+  shutdown_eyedropper (GTK_WIDGET (data));
   
-  gdk_window_get_pointer (colorsel->value_area->window, &x, &y, NULL);
-  gtk_color_selection_update_value (colorsel, y);
-  gtk_color_selection_color_changed (colorsel);
+  gtk_signal_disconnect_by_func (GTK_OBJECT (invisible),
+                                 GTK_SIGNAL_FUNC (mouse_motion), data);
+  gtk_signal_disconnect_by_func (GTK_OBJECT (invisible),
+                                 GTK_SIGNAL_FUNC (mouse_release), data);
 
-  GDK_THREADS_LEAVE ();
-
-  return (TRUE);
+  return TRUE;
 }
 
-static gint
-gtk_color_selection_value_events (GtkWidget *area,
-                                  GdkEvent  *event)
-{
-  GtkColorSelection *colorsel;
-  gint y;
-
-  colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (area), "_GtkColorSelection");
+/* Helper Functions */
 
-  if (colorsel->value_gc == NULL)
-    colorsel->value_gc = gdk_gc_new (colorsel->value_area->window);
+static gboolean mouse_press (GtkWidget      *invisible,
+                             GdkEventButton *event,
+                             gpointer        data);
 
-  switch (event->type)
+static gboolean
+key_press (GtkWidget   *invisible,
+           GdkEventKey *event,
+           gpointer     data)
+{  
+  if (event->keyval == GDK_Escape)
     {
-    case GDK_MAP:
-      gtk_color_selection_draw_value_marker (colorsel);
-      break;
-    case GDK_EXPOSE:
-      gtk_color_selection_draw_value_marker (colorsel);
-      break;
-    case GDK_BUTTON_PRESS:
-      gtk_grab_add (area);
-      gtk_color_selection_update_value (colorsel, event->button.y);
-      gtk_color_selection_color_changed (colorsel);
-      break;
-    case GDK_BUTTON_RELEASE:
-      gtk_grab_remove (area);
-      if (colorsel->timer_active)
-       gtk_timeout_remove (colorsel->timer_tag);
-      colorsel->timer_active = FALSE;
-
-      y = event->button.y;
-      if (event->button.window != area->window)
-       gdk_window_get_pointer (area->window, NULL, &y, NULL);
-
-      gtk_color_selection_update_value (colorsel, y);
-      gtk_color_selection_color_changed (colorsel);
-      break;
-    case GDK_MOTION_NOTIFY:
-      /* Even though we select BUTTON_MOTION_MASK, we seem to need
-       * to occasionally get events without buttons pressed.
-       */
-      if (!(event->motion.state &
-           (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)))
-       return FALSE;
+      shutdown_eyedropper (data);
+
+      gtk_signal_disconnect_by_func (GTK_OBJECT (invisible),
+                                     GTK_SIGNAL_FUNC (mouse_press),
+                                     data);
+      gtk_signal_disconnect_by_func (GTK_OBJECT (invisible),
+                                     GTK_SIGNAL_FUNC (key_press),
+                                     data);
       
-      y = event->motion.y;
-
-      if (event->motion.is_hint || (event->motion.window != area->window))
-       gdk_window_get_pointer (area->window, NULL, &y, NULL);
-
-      switch (colorsel->policy)
-       {
-       case GTK_UPDATE_CONTINUOUS:
-         gtk_color_selection_update_value (colorsel, y);
-         gtk_color_selection_color_changed (colorsel);
-         break;
-       case GTK_UPDATE_DELAYED:
-         if (colorsel->timer_active)
-           gtk_timeout_remove (colorsel->timer_tag);
-
-         colorsel->timer_tag = gtk_timeout_add (TIMER_DELAY,
-                                                (GtkFunction) gtk_color_selection_value_timeout,
-                                                (gpointer) colorsel);
-         colorsel->timer_active = TRUE;
-         break;
-       default:
-         break;
-       }
-      break;
-    default:
-      break;
+      return TRUE;
     }
 
-  return (FALSE);
+  return FALSE;
 }
 
-static gint
-gtk_color_selection_wheel_timeout (GtkColorSelection *colorsel)
+static gboolean
+mouse_press (GtkWidget      *invisible,
+            GdkEventButton *event,
+            gpointer        data)
 {
-  gint x, y;
-
-  gdk_window_get_pointer (colorsel->wheel_area->window, &x, &y, NULL);
-  gtk_color_selection_update_wheel (colorsel, x, y);
-  gtk_color_selection_color_changed (colorsel);
+  GtkColorSelection *colorsel = data;
+  ColorSelectionPrivate *priv;
+  priv = colorsel->private_data;
+  
+  if (event->type == GDK_BUTTON_PRESS &&
+      event->button == 1)
+    {
+      gtk_signal_connect (GTK_OBJECT (invisible), "motion_notify_event",
+                          GTK_SIGNAL_FUNC (mouse_motion),
+                          data);
+      gtk_signal_connect (GTK_OBJECT (invisible), "button_release_event",
+                          GTK_SIGNAL_FUNC (mouse_release),
+                          data);
+      gtk_signal_disconnect_by_func (GTK_OBJECT (invisible),
+                                     GTK_SIGNAL_FUNC (mouse_press),
+                                     data);
+      gtk_signal_disconnect_by_func (GTK_OBJECT (invisible),
+                                     GTK_SIGNAL_FUNC (key_press),
+                                     data);
+      return TRUE;
+    }
 
-  return (TRUE);
+  return FALSE;
 }
 
-static gint
-gtk_color_selection_wheel_events (GtkWidget *area,
-                                  GdkEvent  *event)
+/* when the button is clicked */
+static void
+get_screen_color (GtkWidget *button)
 {
-  GtkColorSelection *colorsel;
-  gint x, y;
-
-  colorsel = (GtkColorSelection*) gtk_object_get_data (GTK_OBJECT (area), "_GtkColorSelection");
+  GtkColorSelection *colorsel = gtk_object_get_data (GTK_OBJECT (button), "COLORSEL");
+  ColorSelectionPrivate *priv = colorsel->private_data; 
   
-  if (colorsel->wheel_gc == NULL)
-    colorsel->wheel_gc = gdk_gc_new (colorsel->wheel_area->window);
-  if (colorsel->sample_gc == NULL)
-    colorsel->sample_gc = gdk_gc_new (colorsel->sample_area->window);
-  if (colorsel->value_gc == NULL)
-    colorsel->value_gc = gdk_gc_new (colorsel->value_area->window);
-  
-  switch (event->type)
+  if (picker_cursor == NULL)
     {
-    case GDK_MAP:
-      gtk_color_selection_draw_wheel (colorsel, TRUE);
-      gtk_color_selection_draw_wheel_marker (colorsel);
-      gtk_color_selection_draw_sample (colorsel, TRUE);
-      break;
-    case GDK_EXPOSE:
-      gtk_color_selection_draw_wheel_marker (colorsel);
-      gtk_color_selection_draw_wheel_frame (colorsel);
-      break;
-    case GDK_BUTTON_PRESS:
-      gtk_grab_add (area);
-      gtk_color_selection_update_wheel (colorsel, event->button.x, event->button.y);
-      gtk_color_selection_color_changed (colorsel);
-      break;
-    case GDK_BUTTON_RELEASE:
-      gtk_grab_remove (area);
-      if (colorsel->timer_active)
-       gtk_timeout_remove (colorsel->timer_tag);
-      colorsel->timer_active = FALSE;
-
-      x = event->button.x;
-      y = event->button.y;
+      initialize_cursor ();
+    }
 
-      if (event->button.window != area->window)
-       gdk_window_get_pointer (area->window, &x, &y, NULL);
+  if (priv->dropper_grab_widget == NULL)
+    {
+      priv->dropper_grab_widget = gtk_invisible_new ();
 
-      gtk_color_selection_update_wheel (colorsel, x, y);
-      gtk_color_selection_color_changed (colorsel);
-      break;
-    case GDK_MOTION_NOTIFY:
-      /* Even though we select BUTTON_MOTION_MASK, we seem to need
-       * to occasionally get events without buttons pressed.
-       */
-      if (!(event->motion.state &
-           (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)))
-       return FALSE;
+      gtk_widget_add_events (priv->dropper_grab_widget,
+                             GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
       
-      x = event->motion.x;
-      y = event->motion.y;
-
-      if (event->motion.is_hint || (event->motion.window != area->window))
-       gdk_window_get_pointer (area->window, &x, &y, NULL);
+      gtk_widget_show (priv->dropper_grab_widget);
+    }
 
-      switch (colorsel->policy)
-       {
-       case GTK_UPDATE_CONTINUOUS:
-         gtk_color_selection_update_wheel (colorsel, x, y);
-         gtk_color_selection_color_changed (colorsel);
-         break;
-       case GTK_UPDATE_DELAYED:
-         if (colorsel->timer_active)
-           gtk_timeout_remove (colorsel->timer_tag);
-         colorsel->timer_tag = gtk_timeout_add (TIMER_DELAY,
-                                                (GtkFunction) gtk_color_selection_wheel_timeout,
-                                                (gpointer) colorsel);
-         colorsel->timer_active = TRUE;
-         break;
-       default:
-         break;
-       }
-      break;
-    default:
-      break;
+  if (gdk_keyboard_grab (priv->dropper_grab_widget->window,
+                         FALSE,
+                         gtk_get_current_event_time ()) != GDK_GRAB_SUCCESS)
+    {
+      g_warning ("Failed to grab keyboard to do eyedropper");
+      return;
+    }
+  
+  if (gdk_pointer_grab (priv->dropper_grab_widget->window,
+                        FALSE,
+                        GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK,
+                        NULL,
+                        picker_cursor,
+                        gtk_get_current_event_time ()) != GDK_GRAB_SUCCESS)
+    {
+      gdk_keyboard_ungrab (GDK_CURRENT_TIME);
+      g_warning ("Failed to grab pointer to do eyedropper");
+      return;
     }
 
-  return (FALSE);
+  gtk_grab_add (priv->dropper_grab_widget);
+  
+  gtk_signal_connect (GTK_OBJECT (priv->dropper_grab_widget), "button_press_event",
+                      GTK_SIGNAL_FUNC (mouse_press), colorsel);
+  gtk_signal_connect (GTK_OBJECT (priv->dropper_grab_widget), "key_press_event",
+                      GTK_SIGNAL_FUNC (key_press), colorsel);
 }
 
 static void
-gtk_color_selection_draw_value_bar (GtkColorSelection *colorsel,
-                                    gint               resize)
+hex_changed (GtkWidget *hex_entry,
+            gpointer   data)
 {
-  gint x, y, i, wid, heig, n;
-  gdouble sv, v, c[3];
-  guchar rc[3];
-
-  wid = colorsel->value_area->allocation.width;
-  heig = colorsel->value_area->allocation.height;
-
-  if (resize)
-    {
-      if (colorsel->value_buf != NULL)
-       g_free (colorsel->value_buf);
-
-      colorsel->value_buf = g_new(guchar, 3 * wid);
-    }
-
-  v = 1.0;
-  sv = 1.0 / (gdouble) (heig - 1);
-
-  for (y = 0; y < heig; y++)
+  GtkColorSelection *colorsel;
+  ColorSelectionPrivate *priv;
+  GdkColor color;
+  gchar *text;
+  
+  colorsel = GTK_COLOR_SELECTION (data);
+  priv = colorsel->private_data;
+  
+  if (priv->changing)
+    return;
+  
+  text = gtk_editable_get_chars (GTK_EDITABLE (priv->hex_entry), 0, -1);
+  if (gdk_color_parse (text, &color))
     {
-      i = 0;
+      priv->color[COLORSEL_RED] = CLAMP (color.red/65280.0, 0.0, 1.0);
+      priv->color[COLORSEL_GREEN] = CLAMP (color.green/65280.0, 0.0, 1.0);
+      priv->color[COLORSEL_BLUE] = CLAMP (color.blue/65280.0, 0.0, 1.0);
+      gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
+                     priv->color[COLORSEL_GREEN],
+                     priv->color[COLORSEL_BLUE],
+                     &priv->color[COLORSEL_HUE],
+                     &priv->color[COLORSEL_SATURATION],
+                     &priv->color[COLORSEL_VALUE]);
+      update_color (colorsel);
+    }
+  g_free (text);
+}
 
-      gtk_color_selection_hsv_to_rgb (colorsel->values[HUE],colorsel->values[SATURATION],v,
-                                      &c[0], &c[1], &c[2]);
+static void
+hsv_changed (GtkWidget *hsv,
+            gpointer   data)
+{
+  GtkColorSelection *colorsel;
+  ColorSelectionPrivate *priv;
+  
+  colorsel = GTK_COLOR_SELECTION (data);
+  priv = colorsel->private_data;
+  
+  if (priv->changing)
+    return;
+  
+  gtk_hsv_get_color (GTK_HSV (hsv),
+                    &priv->color[COLORSEL_HUE],
+                    &priv->color[COLORSEL_SATURATION],
+                    &priv->color[COLORSEL_VALUE]);
+  gtk_hsv_to_rgb (priv->color[COLORSEL_HUE],
+                 priv->color[COLORSEL_SATURATION],
+                 priv->color[COLORSEL_VALUE],
+                 &priv->color[COLORSEL_RED],
+                 &priv->color[COLORSEL_GREEN],
+                 &priv->color[COLORSEL_BLUE]);
+  update_color (colorsel);
+}
 
-      for (n = 0; n < 3; n++)
-       rc[n] = (guchar) (255.0 * c[n]);
+static void
+adjustment_changed (GtkAdjustment *adjustment,
+                   gpointer       data)
+{
+  GtkColorSelection *colorsel;
+  ColorSelectionPrivate *priv;
+  
+  colorsel = GTK_COLOR_SELECTION (gtk_object_get_data (GTK_OBJECT (adjustment), "COLORSEL"));
+  priv = colorsel->private_data;
+  
+  if (priv->changing)
+    return;
+  
+  switch (GPOINTER_TO_INT (data))
+    {
+    case COLORSEL_SATURATION:
+    case COLORSEL_VALUE:
+      priv->color[GPOINTER_TO_INT (data)] = adjustment->value / 255;
+      gtk_hsv_to_rgb (priv->color[COLORSEL_HUE],
+                     priv->color[COLORSEL_SATURATION],
+                     priv->color[COLORSEL_VALUE],
+                     &priv->color[COLORSEL_RED],
+                     &priv->color[COLORSEL_GREEN],
+                     &priv->color[COLORSEL_BLUE]);
+      break;
+    case COLORSEL_HUE:
+      priv->color[GPOINTER_TO_INT (data)] = adjustment->value / 360;
+      gtk_hsv_to_rgb (priv->color[COLORSEL_HUE],
+                     priv->color[COLORSEL_SATURATION],
+                     priv->color[COLORSEL_VALUE],
+                     &priv->color[COLORSEL_RED],
+                     &priv->color[COLORSEL_GREEN],
+                     &priv->color[COLORSEL_BLUE]);
+      break;
+    case COLORSEL_RED:
+    case COLORSEL_GREEN:
+    case COLORSEL_BLUE:
+      priv->color[GPOINTER_TO_INT (data)] = adjustment->value / 255;
+      
+      gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
+                     priv->color[COLORSEL_GREEN],
+                     priv->color[COLORSEL_BLUE],
+                     &priv->color[COLORSEL_HUE],
+                     &priv->color[COLORSEL_SATURATION],
+                     &priv->color[COLORSEL_VALUE]);
+      break;
+    default:
+      priv->color[GPOINTER_TO_INT (data)] = adjustment->value / 255;
+      break;
+    }
+  update_color (colorsel);
+}
 
-      for (x = 0; x < wid; x++)
-       {
-         for (n = 0; n < 3; n++)
-           colorsel->value_buf[i++] = rc[n];
-       }
+static void 
+opacity_entry_changed (GtkWidget *opacity_entry,
+                      gpointer   data)
+{
+  GtkColorSelection *colorsel;
+  ColorSelectionPrivate *priv;
+  GtkAdjustment *adj;
+  gchar *text;
+  
+  colorsel = GTK_COLOR_SELECTION (data);
+  priv = colorsel->private_data;
+  
+  if (priv->changing)
+    return;
+  
+  text = gtk_editable_get_chars (GTK_EDITABLE (priv->opacity_entry), 0, -1);
+  adj = gtk_range_get_adjustment (GTK_RANGE (priv->opacity_slider));
+  gtk_adjustment_set_value (adj, g_strtod (text, NULL)); 
+  
+  update_color (colorsel);
+  
+  g_free (text);
+}
 
-      gtk_preview_draw_row (GTK_PREVIEW (colorsel->value_area), colorsel->value_buf, 0, y, wid);
-      v -= sv;
+static void
+make_label_spinbutton (GtkColorSelection *colorsel,
+                      GtkWidget        **spinbutton,
+                      gchar             *text,
+                      GtkWidget         *table,
+                      gint               i,
+                      gint               j,
+                      gint               channel_type,
+                       const gchar       *tooltip)
+{
+  GtkWidget *label;
+  GtkAdjustment *adjust;
+  ColorSelectionPrivate *priv = colorsel->private_data;
+  
+  if (channel_type == COLORSEL_HUE)
+    {
+      adjust = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 360.0, 1.0, 1.0, 1.0));
+    }
+  else
+    {
+      adjust = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 1.0, 1.0));
     }
+  gtk_object_set_data (GTK_OBJECT (adjust), "COLORSEL", colorsel);
+  *spinbutton = gtk_spin_button_new (adjust, 10.0, 0);
 
-  gtk_widget_queue_draw (colorsel->value_area);
+  gtk_tooltips_set_tip (priv->tooltips, *spinbutton, tooltip, NULL);  
+
+  gtk_signal_connect (GTK_OBJECT (adjust), "value_changed",
+                      GTK_SIGNAL_FUNC (adjustment_changed),
+                      GINT_TO_POINTER (channel_type));
+  label = gtk_label_new_with_mnemonic (text);
+  gtk_label_set_mnemonic_widget (GTK_LABEL (label), *spinbutton);
+
+  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
+  gtk_table_attach_defaults (GTK_TABLE (table), label, i, i+1, j, j+1);
+  gtk_table_attach_defaults (GTK_TABLE (table), *spinbutton, i+1, i+2, j, j+1);
+  
 }
 
 static void
-gtk_color_selection_draw_wheel_frame (GtkColorSelection *colorsel)
+make_palette_frame (GtkColorSelection *colorsel,
+                   GtkWidget         *table,
+                   gint               i,
+                   gint               j)
 {
-  GtkStyle *style;
-  gint w, h;
+  GtkWidget *frame;
+  ColorSelectionPrivate *priv;
+  
+  priv = colorsel->private_data;
+  frame = gtk_frame_new (NULL);
+  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
+  priv->custom_palette[i][j] = palette_new (colorsel);
+  gtk_widget_set_usize (priv->custom_palette[i][j], CUSTOM_PALETTE_ENTRY_WIDTH, CUSTOM_PALETTE_ENTRY_HEIGHT);
+  gtk_container_add (GTK_CONTAINER (frame), priv->custom_palette[i][j]);
+  gtk_table_attach_defaults (GTK_TABLE (table), frame, i, i+1, j, j+1);
+}
 
-  style = gtk_widget_get_style (colorsel->wheel_area);
+/* Set the palette entry [x][y] to be the currently selected one. */
+static void 
+set_selected_palette (GtkColorSelection *colorsel, int x, int y)
+{
+  ColorSelectionPrivate *priv = colorsel->private_data; 
 
-  w = colorsel->wheel_area->allocation.width;
-  h = colorsel->wheel_area->allocation.height;
+  gtk_widget_grab_focus (priv->custom_palette[x][y]);
+}
 
-  gdk_draw_arc (colorsel->wheel_area->window, style->black_gc,
-               FALSE, 1, 1, w - 1, h - 1, 30 * 64, 180 * 64);
-  gdk_draw_arc (colorsel->wheel_area->window, style->mid_gc[GTK_STATE_NORMAL],
-               FALSE, 0, 0, w, h, 30 * 64, 180 * 64);
+static double
+scale_round (double val, double factor)
+{
+  val = floor (val * factor + 0.5);
+  val = MAX (val, 0);
+  val = MIN (val, factor);
+  return val;
+}
 
-  gdk_draw_arc (colorsel->wheel_area->window, style->bg_gc[GTK_STATE_NORMAL],
-               FALSE, 1, 1, w - 1, h - 1, 210 * 64, 180 * 64);
-  gdk_draw_arc (colorsel->wheel_area->window, style->light_gc[GTK_STATE_NORMAL],
-               FALSE, 0, 0, w, h, 210 * 64, 180 * 64);
+static void
+update_color (GtkColorSelection *colorsel)
+{
+  ColorSelectionPrivate *priv = colorsel->private_data;
+  gchar entryval[12];
+  gchar opacity_text[32];
+  gchar *ptr;
+  
+  priv->changing = TRUE;
+  color_sample_draw_samples (colorsel);
+  
+  gtk_hsv_set_color (GTK_HSV (priv->triangle_colorsel),
+                    priv->color[COLORSEL_HUE],
+                    priv->color[COLORSEL_SATURATION],
+                    priv->color[COLORSEL_VALUE]);
+  gtk_adjustment_set_value (gtk_spin_button_get_adjustment
+                           (GTK_SPIN_BUTTON (priv->hue_spinbutton)),
+                           scale_round (priv->color[COLORSEL_HUE], 360));
+  gtk_adjustment_set_value (gtk_spin_button_get_adjustment
+                           (GTK_SPIN_BUTTON (priv->sat_spinbutton)),
+                           scale_round (priv->color[COLORSEL_SATURATION], 255));
+  gtk_adjustment_set_value (gtk_spin_button_get_adjustment
+                           (GTK_SPIN_BUTTON (priv->val_spinbutton)),
+                           scale_round (priv->color[COLORSEL_VALUE], 255));
+  gtk_adjustment_set_value (gtk_spin_button_get_adjustment
+                           (GTK_SPIN_BUTTON (priv->red_spinbutton)),
+                           scale_round (priv->color[COLORSEL_RED], 255));
+  gtk_adjustment_set_value (gtk_spin_button_get_adjustment
+                           (GTK_SPIN_BUTTON (priv->green_spinbutton)),
+                           scale_round (priv->color[COLORSEL_GREEN], 255));
+  gtk_adjustment_set_value (gtk_spin_button_get_adjustment
+                           (GTK_SPIN_BUTTON (priv->blue_spinbutton)),
+                           scale_round (priv->color[COLORSEL_BLUE], 255));
+  gtk_adjustment_set_value (gtk_range_get_adjustment
+                           (GTK_RANGE (priv->opacity_slider)),
+                           scale_round (priv->color[COLORSEL_OPACITY], 255));
+  
+  g_snprintf (opacity_text, 32, "%.0f", priv->color[COLORSEL_OPACITY] * 255);
+  gtk_entry_set_text (GTK_ENTRY (priv->opacity_entry), opacity_text);
+  
+  g_snprintf (entryval, 11, "#%2X%2X%2X",
+             (guint) (scale_round (priv->color[COLORSEL_RED], 255)),
+             (guint) (scale_round (priv->color[COLORSEL_GREEN], 255)),
+             (guint) (scale_round (priv->color[COLORSEL_BLUE], 255)));
+  
+  for (ptr = entryval; *ptr; ptr++)
+    if (*ptr == ' ')
+      *ptr = '0';
+  gtk_entry_set_text (GTK_ENTRY (priv->hex_entry), entryval);
+  priv->changing = FALSE;
 }
 
+
 static void
-gtk_color_selection_draw_wheel (GtkColorSelection *colorsel,
-                                gint               resize)
+fill_palette_from_string (const gchar *str)
 {
-  gint x, y, i, wid, heig, n;
-  gdouble cx, cy, h, s, c[3];
-  guchar bg[3];
-  GtkStyle *style = gtk_widget_get_style (colorsel->wheel_area);
+  GdkColor *colors = NULL;
+  gint n_colors = 0;
+
+  if (str == NULL)
+    return;
+  
+  if (!gtk_color_selection_palette_from_string (str, &colors, &n_colors))
+    return;
 
-  wid = colorsel->wheel_area->allocation.width;
-  heig = colorsel->wheel_area->allocation.height;
+  if (n_colors > (GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT))    
+    n_colors = GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT;
 
-  if (resize)
-    {
-      if (colorsel->wheel_buf != NULL)
-       g_free (colorsel->wheel_buf);
+  memcpy (current_colors, colors, sizeof (GdkColor) * n_colors);
 
-      colorsel->wheel_buf = g_new(guchar, 3 * wid);
-    }
+  g_free (colors);
+}
 
-  cx = (gdouble) (wid) / 2.0;
-  cy = (gdouble) (heig) / 2.0;
+static void
+palette_change_notify_class (GObject    *object,
+                             GParamSpec *pspec)
+{
+  gchar *str = NULL;
+  
+  g_object_get (object, pspec->name, &str, NULL);
 
-  bg[0] = style->bg[GTK_STATE_NORMAL].red >> 8;
-  bg[1] = style->bg[GTK_STATE_NORMAL].green >> 8;
-  bg[2] = style->bg[GTK_STATE_NORMAL].blue >> 8;
+  fill_palette_from_string (str);
 
-  for (y = 0; y < heig; y++)
+  g_free (str);
+}
+
+static void
+update_palette (GtkColorSelection *colorsel)
+{
+  gint i, j;
+  
+  for (i = 0; i < GTK_CUSTOM_PALETTE_HEIGHT; i++)
     {
-      i = 0;
-      for (x = 0; x < wid; x++)
+      for (j = 0; j < GTK_CUSTOM_PALETTE_WIDTH; j++)
        {
-         if (gtk_color_selection_eval_wheel (x, y, cx, cy, &h, &s))
-           {
-             for (n = 0; n < 3; n++)
-               colorsel->wheel_buf[i++] = bg[n];
-           }
-         else
-           {
-             gtk_color_selection_hsv_to_rgb (h, s, 1.0, &c[0], &c[1], &c[2]);
-             for (n = 0; n < 3; n++)
-               colorsel->wheel_buf[i++] = (guchar) (255.0 * c[n]);
-           }
-       }
+          gint index;
 
-      gtk_preview_draw_row (GTK_PREVIEW (colorsel->wheel_area), colorsel->wheel_buf, 0, y, wid);
+          index = i * GTK_CUSTOM_PALETTE_WIDTH + j;
+          
+          gtk_color_selection_set_palette_color (colorsel,
+                                                 index,
+                                                 &current_colors[index]);
+       }
     }
-
-  if (colorsel->wheel_area->window)
-     {
-       GdkPixmap *pm = NULL;
-       GdkGC     *pmgc = NULL;
-       GdkColor   col;
-       gint w, h;
-       
-       pm = gdk_pixmap_new (colorsel->wheel_area->window, wid, heig, 1);
-       pmgc = gdk_gc_new (pm);
-       
-       col.pixel = 0;
-       gdk_gc_set_foreground(pmgc, &col);
-       gdk_draw_rectangle(pm, pmgc, TRUE, 0, 0, wid, heig);
-       col.pixel = 1;
-       
-       gdk_gc_set_foreground(pmgc, &col);
-       gdk_draw_arc (pm, pmgc, TRUE, 0, 0, wid, heig, 0, 360*64);
-
-       w = colorsel->wheel_area->allocation.width;
-       h = colorsel->wheel_area->allocation.height;
-       
-       gdk_draw_arc (pm, pmgc,
-                     FALSE, 1, 1, w - 1, h - 1, 30 * 64, 180 * 64);
-       gdk_draw_arc (pm, pmgc,
-                     FALSE, 0, 0, w, h, 30 * 64, 180 * 64);
-       gdk_draw_arc (pm, pmgc,
-                     FALSE, 1, 1, w - 1, h - 1, 210 * 64, 180 * 64);
-       gdk_draw_arc (pm, pmgc,
-                     FALSE, 0, 0, w, h, 210 * 64, 180 * 64);
-       gdk_window_shape_combine_mask(colorsel->wheel_area->window, pm, 0, 0);
-       gdk_pixmap_unref(pm);
-       gdk_gc_destroy(pmgc);
-     }
 }
 
 static void
-gtk_color_selection_draw_sample (GtkColorSelection *colorsel,
-                                 gint               resize)
+palette_change_notify_instance (GObject    *object,
+                                GParamSpec *pspec,
+                                gpointer    data)
 {
-  gint x, y, i, wid, heig, f, half, n;
-  guchar c[3 * 2], cc[3 * 4], *cp = c;
-  gdouble o, oldo;
+  update_palette (GTK_COLOR_SELECTION (data));
+}
 
-  wid = colorsel->sample_area->allocation.width;
-  heig = colorsel->sample_area->allocation.height;
-  half = wid >> 1;
+static void
+default_change_palette_func (const GdkColor *colors,
+                             gint            n_colors)
+{
+  gchar *str;
+  
+  str = gtk_color_selection_palette_to_string (colors, n_colors);
 
-  if (resize)
-    {
-      if (colorsel->sample_buf != NULL)
-       g_free (colorsel->sample_buf);
+  gtk_settings_set_string_property (gtk_settings_get_global (),
+                                    "gtk-color-palette",
+                                    str,
+                                    "gtk_color_selection_palette_to_string");
 
-      colorsel->sample_buf = g_new(guchar, 3 * wid);
-    }
+  g_free (str);
+}
 
-  i = RED;
-  for (n = 0; n < 3; n++)
+GtkType
+gtk_color_selection_get_type (void)
+{
+  static GtkType color_selection_type = 0;
+  
+  if (!color_selection_type)
     {
-      c[n] = (guchar) (255.0 * colorsel->old_values[i]);
-      c[n + 3] = (guchar) (255.0 * colorsel->values[i++]);
+      static const GtkTypeInfo color_selection_info =
+      {
+        "GtkColorSelection",
+        sizeof (GtkColorSelection),
+        sizeof (GtkColorSelectionClass),
+        (GtkClassInitFunc) gtk_color_selection_class_init,
+        (GtkObjectInitFunc) gtk_color_selection_init,
+        /* reserved_1 */ NULL,
+        /* reserved_2 */ NULL,
+        (GtkClassInitFunc) NULL,
+      };
+      
+      color_selection_type = gtk_type_unique (GTK_TYPE_VBOX, &color_selection_info);
     }
+  
+  return color_selection_type;
+}
 
-  if (colorsel->use_opacity)
-    {
-      o = colorsel->values[OPACITY];
-      oldo = colorsel->old_values[OPACITY];
+static void
+gtk_color_selection_class_init (GtkColorSelectionClass *klass)
+{
+  GtkObjectClass *object_class;
+  GObjectClass *gobject_class;
+  
+  object_class = GTK_OBJECT_CLASS (klass);
+  gobject_class = G_OBJECT_CLASS (klass);
+  
+  parent_class = gtk_type_class (GTK_TYPE_VBOX);
+  
+  object_class->destroy = gtk_color_selection_destroy;
+  gobject_class->finalize = gtk_color_selection_finalize;
+  
+  color_selection_signals[COLOR_CHANGED] =
+    gtk_signal_new ("color_changed",
+                    GTK_RUN_FIRST,
+                    GTK_CLASS_TYPE (object_class),
+                    GTK_SIGNAL_OFFSET (GtkColorSelectionClass, color_changed),
+                    gtk_marshal_VOID__VOID,
+                    GTK_TYPE_NONE, 0);
+
+  gtk_settings_install_property (gtk_settings_get_global (),
+                                 g_param_spec_string ("gtk-color-palette",
+                                                      _("Custom palette"),
+                                                      _("Palette to use in the color selector"),
+                                                      default_colors,
+                                                      G_PARAM_READWRITE));
+  
+  fill_palette_from_string (default_colors);
 
-      for (n = 0; n < 3; n++)
+  change_palette_hook = default_change_palette_func;
+  
+  g_signal_connect_data (G_OBJECT (gtk_settings_get_global ()),
+                         "notify::gtk-color-palette",
+                         G_CALLBACK (palette_change_notify_class),
+                         NULL, NULL, FALSE, FALSE);
+}
+
+/* widget functions */
+
+static void
+gtk_color_selection_init (GtkColorSelection *colorsel)
+{
+  GtkWidget *top_hbox;
+  GtkWidget *top_right_vbox;
+  GtkWidget *table, *label, *hbox, *frame, *vbox;
+  GtkAdjustment *adjust;
+  GdkPixmap *dropper_pixmap;
+  GtkWidget *dropper_image;
+  GtkWidget *button;
+  GdkBitmap *mask = NULL;
+  gint i, j;
+  ColorSelectionPrivate *priv;
+  
+  priv = colorsel->private_data = g_new0 (ColorSelectionPrivate, 1);
+  priv->changing = FALSE;
+  priv->default_set = FALSE;
+  priv->default_alpha_set = FALSE;
+  
+  priv->tooltips = gtk_tooltips_new ();
+  
+  gtk_box_set_spacing (GTK_BOX (colorsel), 4);
+  top_hbox = gtk_hbox_new (FALSE, 8);
+  gtk_box_pack_start (GTK_BOX (colorsel), top_hbox, FALSE, FALSE, 0);
+  
+  vbox = gtk_vbox_new (FALSE, 4);
+  priv->triangle_colorsel = gtk_hsv_new ();
+  gtk_signal_connect (GTK_OBJECT (priv->triangle_colorsel), "changed",
+                      GTK_SIGNAL_FUNC (hsv_changed), colorsel);
+  gtk_hsv_set_metrics (GTK_HSV (priv->triangle_colorsel), 174, 15);
+  gtk_box_pack_start (GTK_BOX (top_hbox), vbox, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (vbox), priv->triangle_colorsel, FALSE, FALSE, 0);
+  gtk_tooltips_set_tip (priv->tooltips, priv->triangle_colorsel,
+                        _("Select the color you want from the outer ring. Select the darkness or lightness of that color using the inner triangle."), NULL);
+  
+  hbox = gtk_hbox_new (FALSE, 4);
+  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
+  
+  frame = gtk_frame_new (NULL);
+  gtk_widget_set_usize (frame, -1, 30);
+  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
+  color_sample_new (colorsel);
+  gtk_container_add (GTK_CONTAINER (frame), priv->sample_area);
+  gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
+  
+  button = gtk_button_new ();
+
+  gtk_widget_set_events (button, GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
+  gtk_object_set_data (GTK_OBJECT (button), "COLORSEL", colorsel); 
+  gtk_signal_connect (GTK_OBJECT (button), "clicked",
+                      GTK_SIGNAL_FUNC (get_screen_color), NULL);
+  dropper_pixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, gtk_widget_get_colormap (button), &mask, NULL, picker);
+  dropper_image = gtk_pixmap_new (dropper_pixmap, mask);
+  gdk_pixmap_unref (dropper_pixmap);
+  if (mask)
+    gdk_pixmap_unref (mask);
+  gtk_container_add (GTK_CONTAINER (button), dropper_image);
+  gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+
+  gtk_tooltips_set_tip (priv->tooltips,
+                        button,
+                        _("Click the eyedropper, then click a color anywhere on your screen to select that color."), NULL);
+  
+  top_right_vbox = gtk_vbox_new (FALSE, 4);
+  gtk_box_pack_start (GTK_BOX (top_hbox), top_right_vbox, FALSE, FALSE, 0);
+  table = gtk_table_new (8, 6, FALSE);
+  gtk_box_pack_start (GTK_BOX (top_right_vbox), table, FALSE, FALSE, 0);
+  gtk_table_set_row_spacings (GTK_TABLE (table), 4);
+  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
+  
+  make_label_spinbutton (colorsel, &priv->hue_spinbutton, _("_Hue:"), table, 0, 0, COLORSEL_HUE,
+                         _("Position on the color wheel."));
+  make_label_spinbutton (colorsel, &priv->sat_spinbutton, _("_Saturation:"), table, 0, 1, COLORSEL_SATURATION,
+                         _("\"Deepness\" of the color."));
+  make_label_spinbutton (colorsel, &priv->val_spinbutton, _("_Value:"), table, 0, 2, COLORSEL_VALUE,
+                         _("Brightness of the color."));
+  make_label_spinbutton (colorsel, &priv->red_spinbutton, _("_Red:"), table, 6, 0, COLORSEL_RED,
+                         _("Amount of red light in the color."));
+  make_label_spinbutton (colorsel, &priv->green_spinbutton, _("_Green:"), table, 6, 1, COLORSEL_GREEN,
+                         _("Amount of green light in the color."));
+  make_label_spinbutton (colorsel, &priv->blue_spinbutton, _("_Blue:"), table, 6, 2, COLORSEL_BLUE,
+                         _("Amount of blue light in the color."));
+  gtk_table_attach_defaults (GTK_TABLE (table), gtk_hseparator_new (), 0, 8, 3, 4); 
+  
+  priv->opacity_label = gtk_label_new_with_mnemonic (_("_Opacity:")); 
+  gtk_misc_set_alignment (GTK_MISC (priv->opacity_label), 1.0, 0.5); 
+  gtk_table_attach_defaults (GTK_TABLE (table), priv->opacity_label, 0, 1, 4, 5); 
+  adjust = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 255.0, 1.0, 1.0, 0.0)); 
+  gtk_object_set_data (GTK_OBJECT (adjust), "COLORSEL", colorsel); 
+  priv->opacity_slider = gtk_hscale_new (adjust);
+  gtk_tooltips_set_tip (priv->tooltips,
+                        priv->opacity_slider,
+                        _("Transparency of the currently-selected color."), NULL);
+  gtk_label_set_mnemonic_widget (GTK_LABEL (priv->opacity_label),
+                                 priv->opacity_slider);
+  gtk_scale_set_draw_value (GTK_SCALE (priv->opacity_slider), FALSE);
+  gtk_signal_connect (GTK_OBJECT(adjust), "value_changed",
+                      GTK_SIGNAL_FUNC (adjustment_changed),
+                      GINT_TO_POINTER (COLORSEL_OPACITY));
+  gtk_table_attach_defaults (GTK_TABLE (table), priv->opacity_slider, 1, 7, 4, 5); 
+  priv->opacity_entry = gtk_entry_new (); 
+  gtk_widget_set_usize (priv->opacity_entry, 40, 0); 
+
+  gtk_signal_connect (GTK_OBJECT (priv->opacity_entry), "activate",
+                      GTK_SIGNAL_FUNC (opacity_entry_changed), colorsel);
+  gtk_table_attach_defaults (GTK_TABLE (table), priv->opacity_entry, 7, 8, 4, 5);
+  
+  label = gtk_label_new_with_mnemonic (_("Color _Name:"));
+  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 5, 6);
+  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
+  priv->hex_entry = gtk_entry_new ();
+
+  gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->hex_entry);
+
+  gtk_signal_connect (GTK_OBJECT (priv->hex_entry), "activate",
+                      GTK_SIGNAL_FUNC (hex_changed), colorsel);
+
+  gtk_tooltips_set_tip (priv->tooltips,
+                        priv->hex_entry,
+                        _("You can enter an HTML-style hexadecimal color value, or simply a color name such as 'orange' in this entry."), NULL);
+  
+  gtk_widget_set_usize (priv->hex_entry, 75, -1);  
+  gtk_table_set_col_spacing (GTK_TABLE (table), 3, 15);
+  gtk_table_attach_defaults (GTK_TABLE (table), priv->hex_entry, 1, 5, 5, 6);
+  
+  /* Set up the palette */
+  table = gtk_table_new (GTK_CUSTOM_PALETTE_HEIGHT, GTK_CUSTOM_PALETTE_WIDTH, TRUE);
+  gtk_table_set_row_spacings (GTK_TABLE (table), 1);
+  gtk_table_set_col_spacings (GTK_TABLE (table), 1);
+  for (i = 0; i < GTK_CUSTOM_PALETTE_WIDTH; i++)
+    {
+      for (j = 0; j < GTK_CUSTOM_PALETTE_HEIGHT; j++)
        {
-         cc[n] = (guchar) ((1.0 - oldo) * 192 + (oldo * (gdouble) c[n]));
-         cc[n + 3] = (guchar) ((1.0 - oldo) * 128 + (oldo * (gdouble) c[n]));
-         cc[n + 6] = (guchar) ((1.0 - o) * 192 + (o * (gdouble) c[n + 3]));
-         cc[n + 9] = (guchar) ((1.0 - o) * 128 + (o * (gdouble) c[n + 3]));
+         make_palette_frame (colorsel, table, i, j);
        }
-      cp = cc;
     }
+  set_selected_palette (colorsel, 0, 0);
+  priv->palette_frame = gtk_frame_new (NULL);
+  label = gtk_label_new_with_mnemonic (_("_Palette"));
+  gtk_frame_set_label_widget (GTK_FRAME (priv->palette_frame), label);
 
-  for (y = 0; y < heig; y++)
+  gtk_label_set_mnemonic_widget (GTK_LABEL (label),
+                                 priv->custom_palette[0][0]);
+  
+  gtk_box_pack_end (GTK_BOX (top_right_vbox), priv->palette_frame, FALSE, FALSE, 0);
+  vbox = gtk_vbox_new (FALSE, 4);
+  gtk_container_add (GTK_CONTAINER (priv->palette_frame), vbox);
+  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
+
+  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
+  
+  gtk_widget_show_all (top_hbox);
+
+  /* Set default colors */
+
+  update_palette (colorsel);
+  
+  g_signal_connect_data (G_OBJECT (gtk_settings_get_global ()),
+                         "notify::gtk-color-palette",
+                         G_CALLBACK (palette_change_notify_instance),
+                         colorsel, NULL, FALSE, FALSE);
+  
+  /* hide unused stuff */
+  
+  if (priv->has_opacity == FALSE)
     {
-      i = 0;
-      for (x = 0; x < wid; x++)
-       {
-         if (colorsel->use_opacity)
-           {
-             f = 3 * (((x % 32) < 16) ^ ((y % 32) < 16));
-             f += (x > half) * 6;
-           }
-         else
-           f = (x > half) * 3;
+      gtk_widget_hide (priv->opacity_label);
+      gtk_widget_hide (priv->opacity_slider);
+      gtk_widget_hide (priv->opacity_entry);
+    }
+  
+  if (priv->has_palette == FALSE)
+    {
+      gtk_widget_hide (priv->palette_frame);
+    }
+}
 
-         for (n = 0; n < 3; n++)
-           colorsel->sample_buf[i++] = cp[n + f];
-       }
+static void
+gtk_color_selection_destroy (GtkObject *object)
+{
+  GtkColorSelection *cselection = GTK_COLOR_SELECTION (object);
+  ColorSelectionPrivate *priv = cselection->private_data;
 
-      gtk_preview_draw_row (GTK_PREVIEW (colorsel->sample_area), colorsel->sample_buf, 0, y, wid);
+  if (priv->dropper_grab_widget)
+    {
+      gtk_widget_destroy (priv->dropper_grab_widget);
+      priv->dropper_grab_widget = NULL;
     }
 
-  gtk_widget_queue_draw (colorsel->sample_area);
+  if (priv->tooltips)
+    {
+      gtk_object_destroy (priv->tooltips);
+      priv->tooltips = NULL;
+    }
+  
+  GTK_OBJECT_CLASS (parent_class)->destroy (object);
 }
 
-static gint
-gtk_color_selection_eval_wheel (gint     x,  gint     y,
-                               gdouble  cx, gdouble  cy,
-                               gdouble *h,  gdouble *s)
+static void
+gtk_color_selection_finalize (GObject *object)
 {
-  gdouble r, rx, ry;
+  GtkColorSelection *cselection = GTK_COLOR_SELECTION (object);
+  
+  if (cselection->private_data)
+    {
+      g_free (cselection->private_data);
+      cselection->private_data = NULL;
+    }
+  
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
 
-  rx = ((gdouble) x - cx);
-  ry = ((gdouble) y - cy);
 
-  rx = rx/cx;
-  ry = ry/cy;
+/**
+ * gtk_color_selection_new:
+ * 
+ * Creates a new GtkColorSelection.
+ * 
+ * Return value: The new GtkColorSelection.
+ **/
+GtkWidget *
+gtk_color_selection_new (void)
+{
+  GtkColorSelection *colorsel;
+  ColorSelectionPrivate *priv;
+  gdouble color[4];
+  color[0] = 1.0;
+  color[1] = 1.0;
+  color[2] = 1.0;
+  color[3] = 1.0;
+  
+  colorsel = gtk_type_new (GTK_TYPE_COLOR_SELECTION);
+  priv = colorsel->private_data;
+  gtk_color_selection_set_color (colorsel, color);
+  gtk_color_selection_set_has_opacity_control (colorsel, TRUE);
+  
+  /* We want to make sure that default_set is FALSE */
+  /* This way the user can still set it */
+  priv->default_set = FALSE;
+  priv->default_alpha_set = FALSE;
+  
+  return GTK_WIDGET (colorsel);
+}
 
-  r = sqrt (SQR (rx) + SQR (ry));
 
-  if (r != 0.0)
-    *h = atan2 (rx / r, ry / r);
-  else
-    *h = 0.0;
+void
+gtk_color_selection_set_update_policy (GtkColorSelection *colorsel,
+                                      GtkUpdateType      policy)
+{
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  /* */
+  g_warning (G_STRLOC ": This function is deprecated.");
+}
 
-  *s = r;
-  *h = 360.0 * (*h) / (2.0 * M_PI) + 180;
+/**
+ * gtk_color_selection_get_has_opacity_control:
+ * @colorsel: A GtkColorSelection.
+ * 
+ * Determines whether the colorsel has an opacity control.
+ * 
+ * Return value: TRUE if the @colorsel has an opacity control.  FALSE if it does't.
+ **/
+gboolean
+gtk_color_selection_get_has_opacity_control (GtkColorSelection *colorsel)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_val_if_fail (colorsel != NULL, FALSE);
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
+  
+  priv = colorsel->private_data;
+  
+  return priv->has_opacity;
+}
 
-  if (*s == 0.0)
-    *s = 0.00001;
-  else if (*s > 1.0)
+/**
+ * gtk_color_selection_set_has_opacity_control:
+ * @colorsel: A GtkColorSelection.
+ * @has_opacity: TRUE if @colorsel can set the opacity, FALSE otherwise.
+ *
+ * Sets the @colorsel to use or not use opacity.
+ * 
+ **/
+void
+gtk_color_selection_set_has_opacity_control (GtkColorSelection *colorsel,
+                                            gboolean           has_opacity)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  has_opacity = has_opacity != FALSE;
+  
+  if (priv->has_opacity != has_opacity)
     {
-      *s = 1.0;
-      return TRUE;
+      priv->has_opacity = has_opacity;
+      if (has_opacity)
+       {
+         gtk_widget_show (priv->opacity_slider);
+         gtk_widget_show (priv->opacity_label);
+         gtk_widget_show (priv->opacity_entry);
+       }
+      else
+       {
+         gtk_widget_hide (priv->opacity_slider);
+         gtk_widget_hide (priv->opacity_label);
+         gtk_widget_hide (priv->opacity_entry);
+       }
+      color_sample_draw_samples (colorsel);
     }
-  return FALSE;
 }
 
-static void
-gtk_color_selection_hsv_to_rgb (gdouble  h, gdouble  s, gdouble  v,
-                               gdouble *r, gdouble *g, gdouble *b)
+/**
+ * gtk_color_selection_get_has_palette:
+ * @colorsel: A GtkColorSelection.
+ * 
+ * Determines whether the color selector has a color palette.
+ * 
+ * Return value: TRUE if the selector has a palette.  FALSE if it hasn't.
+ **/
+gboolean
+gtk_color_selection_get_has_palette (GtkColorSelection *colorsel)
 {
-  gint i;
-  gdouble f, w, q, t;
+  ColorSelectionPrivate *priv;
+  
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
+  
+  priv = colorsel->private_data;
+  
+  return priv->has_palette;
+}
 
-  if (s == 0.0)
-    s = 0.000001;
+/**
+ * gtk_color_selection_set_has_palette:
+ * @colorsel: A GtkColorSelection.
+ * @has_palette: TRUE if palette is to be visible, FALSE otherwise.
+ *
+ * Shows and hides the palette based upon the value of @has_palette.
+ * 
+ **/
+void
+gtk_color_selection_set_has_palette (GtkColorSelection *colorsel,
+                                    gboolean           has_palette)
+{
+  ColorSelectionPrivate *priv;
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  has_palette = has_palette != FALSE;
+  
+  if (priv->has_palette != has_palette)
+    {
+      priv->has_palette = has_palette;
+      if (has_palette)
+       gtk_widget_show (priv->palette_frame);
+      else
+       gtk_widget_hide (priv->palette_frame);
+    }
+}
 
-  if (h == -1.0)
+/**
+ * gtk_color_selection_set_current_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A GdkColor to set the current color with.
+ *
+ * Sets the current color to be @color.  The first time this is called, it will
+ * also set the original color to be @color too.
+ * 
+ **/
+void
+gtk_color_selection_set_current_color (GtkColorSelection *colorsel,
+                                      GdkColor          *color)
+{
+  ColorSelectionPrivate *priv;
+  gint i;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->color[COLORSEL_RED] = color->red / 65535.0;
+  priv->color[COLORSEL_GREEN] = color->green / 65535.0;
+  priv->color[COLORSEL_BLUE] = color->blue / 65535.0;
+  gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
+                 priv->color[COLORSEL_GREEN],
+                 priv->color[COLORSEL_BLUE],
+                 &priv->color[COLORSEL_HUE],
+                 &priv->color[COLORSEL_SATURATION],
+                 &priv->color[COLORSEL_VALUE]);
+  if (priv->default_set == FALSE)
     {
-      *r = v;
-      *g = v;
-      *b = v;
+      for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
+       priv->old_color[i] = priv->color[i];
     }
-  else
+  update_color (colorsel);
+  priv->default_set = TRUE;
+}
+
+/**
+ * gtk_color_selection_set_current_alpha:
+ * @colorsel: A GtkColorSelection.
+ * @alpha: an integer between 0 and 65535
+ *
+ * Sets the current opacity to be @alpha.  The first time this is called, it will
+ * also set the original opacity to be @alpha too.
+ * 
+ **/
+void
+gtk_color_selection_set_current_alpha (GtkColorSelection *colorsel,
+                                      guint16            alpha)
+{
+  ColorSelectionPrivate *priv;
+  gint i;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->color[COLORSEL_OPACITY] = alpha / 65535.0;
+  if (priv->default_alpha_set == FALSE)
     {
-      if (h == 360.0)
-       h = 0.0;
-      h = h / 60.0;
-      i = (gint) h;
-      f = h - i;
-      w = v * (1.0 - s);
-      q = v * (1.0 - (s * f));
-      t = v * (1.0 - (s * (1.0 - f)));
-
-      switch (i)
-       {
-       case 0:
-         *r = v;
-         *g = t;
-         *b = w;
-         break;
-       case 1:
-         *r = q;
-         *g = v;
-         *b = w;
-         break;
-       case 2:
-         *r = w;
-         *g = v;
-         *b = t;
-         break;
-       case 3:
-         *r = w;
-         *g = q;
-         *b = v;
-         break;
-       case 4:
-         *r = t;
-         *g = w;
-         *b = v;
-         break;
-       case 5:
-         *r = v;
-         *g = w;
-         *b = q;
-         break;
-       }
+      for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
+       priv->old_color[i] = priv->color[i];
     }
+  update_color (colorsel);
+  priv->default_alpha_set = TRUE;
 }
 
-static void
-gtk_color_selection_rgb_to_hsv (gdouble  r, gdouble  g, gdouble  b,
-                               gdouble *h, gdouble *s, gdouble *v)
+/**
+ * gtk_color_selection_set_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A array of doubles that specifies the color to set the current color with.
+ *
+ * Sets the current color to be @color.  The first time this is called, it will
+ * also set the original color to be @color too.
+ *
+ * This function is deprecated, use gtk_color_selection_set_current_color() instead.
+ *
+ **/
+void
+gtk_color_selection_set_color (GtkColorSelection    *colorsel,
+                              gdouble              *color)
 {
-  double max, min, delta;
+  ColorSelectionPrivate *priv;
+  gint i;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->color[COLORSEL_RED] = color[0];
+  priv->color[COLORSEL_GREEN] = color[1];
+  priv->color[COLORSEL_BLUE] = color[2];
+  priv->color[COLORSEL_OPACITY] = color[3];
+  gtk_rgb_to_hsv (priv->color[COLORSEL_RED],
+                 priv->color[COLORSEL_GREEN],
+                 priv->color[COLORSEL_BLUE],
+                 &priv->color[COLORSEL_HUE],
+                 &priv->color[COLORSEL_SATURATION],
+                 &priv->color[COLORSEL_VALUE]);
+  if (priv->default_set == FALSE)
+    {
+      for (i = 0; i < COLORSEL_NUM_CHANNELS; i++)
+       priv->old_color[i] = priv->color[i];
+    }
+  update_color (colorsel);
+  priv->default_set = TRUE;
+  priv->default_alpha_set = TRUE;
+}
 
-  max = r;
-  if (g > max)
-    max = g;
-  if (b > max)
-    max = b;
+/**
+ * gtk_color_selection_get_current_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A GdkColor to fill in with the current color.
+ *
+ * Sets @color to be the current color in the GtkColorSelection widget.
+ *
+ * This function is deprecated, use gtk_color_selection_get_current_color() instead.
+ **/
+void
+gtk_color_selection_get_current_color (GtkColorSelection *colorsel,
+                                      GdkColor          *color)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  color->red = priv->color[COLORSEL_RED] * 65535;
+  color->green = priv->color[COLORSEL_GREEN] * 65535;
+  color->blue = priv->color[COLORSEL_BLUE] * 65535;
+}
 
-  min = r;
-  if (g < min)
-    min = g;
-  if (b < min)
-    min = b;
+/**
+ * gtk_color_selection_get_current_alpha:
+ * @colorsel: A GtkColorSelection.
+ *
+ * Returns the current alpha value
+ *
+ * Return value: an integer between 0 and 65535
+ **/
+guint16
+gtk_color_selection_get_current_alpha (GtkColorSelection *colorsel)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_val_if_fail (colorsel != NULL, 0);
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), 0);
+  
+  priv = colorsel->private_data;
+  return priv->has_opacity ? priv->color[COLORSEL_OPACITY] * 65535 : 1.0;
+}
 
-  *v = max;
+/**
+ * gtk_color_selection_get_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A color to fill in with the current color.
+ *
+ * Sets @color to be the current color in the GtkColorSelection widget.
+ * 
+ **/
+void
+gtk_color_selection_get_color (GtkColorSelection *colorsel,
+                              gdouble           *color)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  color[0] = priv->color[COLORSEL_RED];
+  color[1] = priv->color[COLORSEL_GREEN];
+  color[2] = priv->color[COLORSEL_BLUE];
+  color[3] = priv->has_opacity ? priv->color[COLORSEL_OPACITY] : 1.0;
+}
 
-  if (max != 0.0)
-    *s = (max - min) / max;
-  else
-    *s = 0.0;
+/**
+ * gtk_color_selection_set_previous_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A color to set the previous color with.
+ *
+ * Sets the 'previous' color to be @color.  This function should be called with
+ * some hesitations, as it might seem confusing to have that color change.
+ * Calling gtk_color_selection_set_current_color will also set this color the first
+ * time it is called.
+ * 
+ **/
+void
+gtk_color_selection_set_previous_color (GtkColorSelection *colorsel,
+                                       GdkColor          *color)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->old_color[COLORSEL_RED] = color->red / 65535.0;
+  priv->old_color[COLORSEL_GREEN] = color->green / 65535.0;
+  priv->old_color[COLORSEL_BLUE] = color->blue / 65535.0;
+  gtk_rgb_to_hsv (priv->old_color[COLORSEL_RED],
+                 priv->old_color[COLORSEL_GREEN],
+                 priv->old_color[COLORSEL_BLUE],
+                 &priv->old_color[COLORSEL_HUE],
+                 &priv->old_color[COLORSEL_SATURATION],
+                 &priv->old_color[COLORSEL_VALUE]);
+  color_sample_draw_samples (colorsel);
+  priv->default_set = TRUE;
+}
 
-  if (*s == 0.0)
-    *h = -1.0;
-  else
-    {
-      delta = max - min;
+/**
+ * gtk_color_selection_set_previous_alpha:
+ * @colorsel: A GtkColorSelection.
+ * @alpha: an integer between 0 and 65535
+ *
+ * Sets the 'previous' alpha to be @alpha.  This function should be called with
+ * some hesitations, as it might seem confusing to have that color change.
+ * 
+ **/
+void
+gtk_color_selection_set_previous_alpha (GtkColorSelection *colorsel,
+                                       guint16            alpha)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  priv->changing = TRUE;
+  priv->old_color[COLORSEL_OPACITY] = alpha / 65535.0;
+  color_sample_draw_samples (colorsel);
+  priv->default_alpha_set = TRUE;
+}
 
-      if (r == max)
-       *h = (g - b) / delta;
-      else if (g == max)
-       *h = 2.0 + (b - r) / delta;
-      else if (b == max)
-       *h = 4.0 + (r - g) / delta;
 
-      *h = *h * 60.0;
+/**
+ * gtk_color_selection_get_previous_color:
+ * @colorsel: A GtkColorSelection.
+ * @color: A GdkColor to fill in with the original color value.
+ *
+ * Fills @color in with the original color value.
+ * 
+ **/
+void
+gtk_color_selection_get_previous_color (GtkColorSelection *colorsel,
+                                       GdkColor           *color)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  
+  priv = colorsel->private_data;
+  color->red = priv->old_color[COLORSEL_RED] * 65535;
+  color->green = priv->old_color[COLORSEL_GREEN] * 65535;
+  color->blue = priv->old_color[COLORSEL_BLUE] * 65535;
+}
 
-      if (*h < 0.0)
-       *h = *h + 360;
-    }
+/**
+ * gtk_color_selection_get_previous_alpha:
+ * @colorsel: A GtkColorSelection.
+ *
+ * Returns the previous alpha value
+ *
+ * Return value: an integer between 0 and 65535
+ **/
+guint16
+gtk_color_selection_get_previous_alpha (GtkColorSelection *colorsel)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_val_if_fail (colorsel != NULL, 0);
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), 0);
+  
+  priv = colorsel->private_data;
+  return priv->has_opacity ? priv->old_color[COLORSEL_OPACITY] * 65535 : 1.0;
 }
 
-/***************************/
-/* GtkColorSelectionDialog */
-/***************************/
+/**
+ * gtk_color_selection_set_palette_color:
+ * @colorsel: A GtkColorSelection.
+ * @index: The color index of the palette.
+ * @color: A GdkColor to set the palette with.
+ *
+ * Set the palette located at at @index to have @color set as its color.
+ * 
+ **/
+static void
+gtk_color_selection_set_palette_color (GtkColorSelection   *colorsel,
+                                      gint                 index,
+                                      GdkColor            *color)
+{
+  ColorSelectionPrivate *priv;
+  gint x, y;
+  gdouble col[3];
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  g_return_if_fail (index >= 0  && index < GTK_CUSTOM_PALETTE_WIDTH*GTK_CUSTOM_PALETTE_HEIGHT);
 
-guint
-gtk_color_selection_dialog_get_type (void)
+  x = index % GTK_CUSTOM_PALETTE_WIDTH;
+  y = index / GTK_CUSTOM_PALETTE_WIDTH;
+  
+  priv = colorsel->private_data;
+  col[0] = color->red / 65535.0;
+  col[1] = color->green / 65535.0;
+  col[2] = color->blue / 65535.0;
+  
+  palette_set_color (priv->custom_palette[x][y], colorsel, col);
+}
+
+/**
+ * gtk_color_selection_get_palette_color:
+ * @colorsel: A GtkColorSelection.
+ * @index: The color index of the palette.
+ * @color: A color to fill in with the color value.
+ * 
+ * Set @color to have the color found in the palette at @index.  If
+ * the palette is unset, it will leave the color unset.
+ * 
+ * Return value: TRUE if the palette located at @index has a color set.  FALSE
+ * if it doesn't.
+ **/
+static gboolean
+gtk_color_selection_get_palette_color (GtkColorSelection   *colorsel,
+                                      gint                 index,
+                                      GdkColor            *color)
 {
-  static guint color_selection_dialog_type = 0;
+  ColorSelectionPrivate *priv;
+  gint x, y;
+  gdouble col[4];
+  
+  g_return_val_if_fail (colorsel != NULL, FALSE);
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
+  g_return_val_if_fail (index >= 0  && index < GTK_CUSTOM_PALETTE_WIDTH*GTK_CUSTOM_PALETTE_HEIGHT, FALSE);
+  
+  priv = colorsel->private_data;
 
-  if (!color_selection_dialog_type)
-    {
-      GtkTypeInfo colorsel_diag_info =
-      {
-       "GtkColorSelectionDialog",
-       sizeof (GtkColorSelectionDialog),
-       sizeof (GtkColorSelectionDialogClass),
-       (GtkClassInitFunc) gtk_color_selection_dialog_class_init,
-       (GtkObjectInitFunc) gtk_color_selection_dialog_init,
-       /* reserved_1 */ NULL,
-       /* reserved_2 */ NULL,
-        (GtkClassInitFunc) NULL,
-      };
+  x = index % GTK_CUSTOM_PALETTE_WIDTH;
+  y = index / GTK_CUSTOM_PALETTE_WIDTH;
 
-      color_selection_dialog_type = gtk_type_unique (gtk_window_get_type (), &colorsel_diag_info);
-    }
+  if (GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (priv->custom_palette[x][y]), "color_set")) == 0)
+    return FALSE;
+  
+  palette_get_color (priv->custom_palette[x][y], col);
 
-  return color_selection_dialog_type;
+  color->red = col[0] * 65535;
+  color->green = col[1] * 65535;
+  color->blue = col[2] * 65535;
+  
+  return TRUE;
 }
 
+/**
+ * gtk_color_selection_unset_palette_color:
+ * @colorsel: A GtkColorSelection.
+ * @index: The color index in the palette.
+ *
+ * Change the palette located @index to have no color set.
+ * 
+ **/
 static void
-gtk_color_selection_dialog_class_init (GtkColorSelectionDialogClass *klass)
+gtk_color_selection_unset_palette_color (GtkColorSelection   *colorsel,
+                                        gint                 index)
 {
-  GtkObjectClass *object_class;
+  ColorSelectionPrivate *priv;
+  gint x, y;
+  
+  g_return_if_fail (colorsel != NULL);
+  g_return_if_fail (GTK_IS_COLOR_SELECTION (colorsel));
+  g_return_if_fail (index >= 0  && index < GTK_CUSTOM_PALETTE_WIDTH*GTK_CUSTOM_PALETTE_HEIGHT);
+  
+  x = index % GTK_CUSTOM_PALETTE_WIDTH;
+  y = index / GTK_CUSTOM_PALETTE_WIDTH;
+  
+  priv = colorsel->private_data;
+  palette_unset_color (priv->custom_palette[x][y]);
+}
+
+/**
+ * gtk_color_selection_get_current_alpha:
+ * @colorsel: A GtkColorSelection.
+ *
+ * Returns the maximum number of palette colors.
+ *
+ * Return value: the maximum number of palette indexes
+ **/
+static gint
+gtk_color_selection_get_palette_size (GtkColorSelection *colorsel)
+{
+  return GTK_CUSTOM_PALETTE_WIDTH * GTK_CUSTOM_PALETTE_HEIGHT;
+}
 
-  object_class = (GtkObjectClass*) klass;
 
-  color_selection_dialog_parent_class = gtk_type_class (gtk_window_get_type ());
+/**
+ * gtk_color_selection_is_adjusting:
+ * @colorsel: A GtkColorSelection.
+ *
+ * Gets the current state of the @colorsel.
+ *
+ * Return value: TRUE if the user is currently dragging a color around, and FALSE
+ * if the selection has stopped.
+ **/
+gboolean
+gtk_color_selection_is_adjusting (GtkColorSelection *colorsel)
+{
+  ColorSelectionPrivate *priv;
+  
+  g_return_val_if_fail (colorsel != NULL, FALSE);
+  g_return_val_if_fail (GTK_IS_COLOR_SELECTION (colorsel), FALSE);
+  
+  priv = colorsel->private_data;
+  
+  return (gtk_hsv_is_adjusting (GTK_HSV (priv->triangle_colorsel)));
 }
 
-static void
-gtk_color_selection_dialog_init (GtkColorSelectionDialog *colorseldiag)
+
+/**
+ * gtk_color_selection_palette_from_string:
+ * @str: a string encoding a color palette
+ * @colors: return location for allocated array of GdkColor
+ * @n_colors: return location for length of array
+ * 
+ * Parses a color palette string; the string is a colon-separated
+ * list of color names readable by gdk_color_parse().
+ * 
+ * Return value: %TRUE if a palette was successfully parsed
+ **/
+gboolean
+gtk_color_selection_palette_from_string (const gchar *str,
+                                         GdkColor   **colors,
+                                         gint        *n_colors)
 {
-  GtkWidget *action_area, *frame;
+  GdkColor *retval;
+  gint count;
+  gchar *p;
+  gchar *start;
+  gchar *copy;
+  
+  count = 0;
+  retval = NULL;
+  copy = g_strdup (str);
 
-  colorseldiag->main_vbox = gtk_vbox_new (FALSE, 10);
-  gtk_container_set_border_width (GTK_CONTAINER (colorseldiag), 10);
-  gtk_container_add (GTK_CONTAINER (colorseldiag), colorseldiag->main_vbox);
-  gtk_widget_show (colorseldiag->main_vbox);
+  start = copy;
+  p = copy;
+  while (TRUE)
+    {
+      if (*p == ':' || *p == '\0')
+        {
+          gboolean done = TRUE;
+
+          if (start == p)
+            {
+              goto failed; /* empty entry */
+            }
+              
+          if (*p)
+            {
+              *p = '\0';
+              done = FALSE;
+            }
+
+          retval = g_renew (GdkColor, retval, count + 1);
+          if (!gdk_color_parse (start, retval + count))
+            {
+              goto failed;
+            }
+
+          ++count;
+
+          if (done)
+            break;
+          else
+            start = p + 1;
+        }
+
+      ++p;
+    }
 
-  frame = gtk_frame_new (NULL);
-  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
-  gtk_container_add (GTK_CONTAINER (colorseldiag->main_vbox), frame);
-  gtk_widget_show (frame);
+  g_free (copy);
+  
+  if (colors)
+    *colors = retval;
+  else
+    g_free (retval);
+
+  if (n_colors)
+    *n_colors = count;
 
-  colorseldiag->colorsel = gtk_color_selection_new ();
-  gtk_container_add (GTK_CONTAINER (frame), colorseldiag->colorsel);
-  gtk_widget_show (colorseldiag->colorsel);
+  return TRUE;
+  
+ failed:
+  g_free (copy);
+  g_free (retval);
 
-  action_area = gtk_hbutton_box_new ();
-  gtk_button_box_set_layout(GTK_BUTTON_BOX(action_area), GTK_BUTTONBOX_END);
-  gtk_button_box_set_spacing(GTK_BUTTON_BOX(action_area), 5);
-  gtk_box_pack_end (GTK_BOX (colorseldiag->main_vbox), action_area, FALSE, FALSE, 0);
-  gtk_widget_show (action_area);
+  if (colors)
+    *colors = NULL;
+  if (n_colors)
+    *n_colors = 0;
 
-  colorseldiag->ok_button = gtk_button_new_with_label ("OK");
-  GTK_WIDGET_SET_FLAGS (colorseldiag->ok_button, GTK_CAN_DEFAULT);
-  gtk_box_pack_start (GTK_BOX (action_area), colorseldiag->ok_button, TRUE, TRUE, 0);
-  gtk_widget_grab_default (colorseldiag->ok_button);
-  gtk_widget_show (colorseldiag->ok_button);
+  return FALSE;
+}
+
+/**
+ * gtk_color_selection_palette_to_string:
+ * @colors: an array of colors
+ * @n_colors: length of the array
+ * 
+ * Encodes a palette as a string, useful for persistent storage.
+ * 
+ * Return value: allocated string encoding the palette
+ **/
+gchar*
+gtk_color_selection_palette_to_string (const GdkColor *colors,
+                                       gint            n_colors)
+{
+  gint i;
+  gchar **strs = NULL;
+  gchar *retval;
+  
+  if (n_colors == 0)
+    return g_strdup ("");
 
-  colorseldiag->cancel_button = gtk_button_new_with_label ("Cancel");
-  GTK_WIDGET_SET_FLAGS (colorseldiag->cancel_button, GTK_CAN_DEFAULT);
-  gtk_box_pack_start (GTK_BOX (action_area), colorseldiag->cancel_button, TRUE, TRUE, 0);
-  gtk_widget_show (colorseldiag->cancel_button);
+  strs = g_new0 (gchar*, n_colors + 1);
 
-  colorseldiag->help_button = gtk_button_new_with_label ("Help");
-  GTK_WIDGET_SET_FLAGS (colorseldiag->help_button, GTK_CAN_DEFAULT);
-  gtk_box_pack_start (GTK_BOX (action_area), colorseldiag->help_button, TRUE, TRUE, 0);
-  gtk_widget_show (colorseldiag->help_button);
+  i = 0;
+  while (i < n_colors)
+    {
+      gchar *ptr;
+      
+      strs[i] =
+        g_strdup_printf ("#%2X%2X%2X",
+                         colors[i].red / 256,
+                         colors[i].green / 256,
+                         colors[i].blue / 256);
+
+      for (ptr = strs[i]; *ptr; ptr++)
+        if (*ptr == ' ')
+          *ptr = '0';
+      
+      ++i;
+    }
+
+  retval = g_strjoinv (":", strs);
+
+  g_strfreev (strs);
+
+  return retval;
 }
 
-GtkWidget *
-gtk_color_selection_dialog_new (const gchar *title)
+/**
+ * gtk_color_selection_set_change_palette_hook:
+ * @func: a function to call when the custom palette needs saving
+ * 
+ * Installs a global function to be called whenever the user tries to
+ * modify the palette in a color selection. This function should save
+ * the new palette contents, and update the GtkSettings property
+ * "gtk-color-palette" so all GtkColorSelection widgets will be modified.
+ * 
+ * Return value: the previous change palette hook (that was replaced)
+ **/
+GtkColorSelectionChangePaletteFunc
+gtk_color_selection_set_change_palette_hook (GtkColorSelectionChangePaletteFunc func)
 {
-  GtkColorSelectionDialog *colorseldiag;
+  GtkColorSelectionChangePaletteFunc old;
+
+  old = change_palette_hook;
 
-  colorseldiag = gtk_type_new (gtk_color_selection_dialog_get_type ());
-  gtk_window_set_title (GTK_WINDOW (colorseldiag), title);
+  change_palette_hook = func;
 
-  return GTK_WIDGET (colorseldiag);
+  return old;
 }
+
+
+