[gtk+/wip/colorchooser-v2: 10/40] Make saving custom colors work as intended
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/colorchooser-v2: 10/40] Make saving custom colors work as intended
- Date: Tue, 7 Feb 2012 17:45:19 +0000 (UTC)
commit 14b7428f71080849cd7a504f442bce441312eb6a
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Jan 31 00:31:41 2012 -0500
Make saving custom colors work as intended
The custom colors are now pushed over to the right as new ones
are added, and everything beyond the 9th row gets dropped.
Customized colors are added to the custom colors array.
gtk/gtkcolorchooserdialog.c | 16 ++++++++++++
gtk/gtkcolorchooserwidget.c | 57 ++++++++++++++++++++++++++++++++++---------
2 files changed, 61 insertions(+), 12 deletions(-)
---
diff --git a/gtk/gtkcolorchooserdialog.c b/gtk/gtkcolorchooserdialog.c
index 7635540..dc211ec 100644
--- a/gtk/gtkcolorchooserdialog.c
+++ b/gtk/gtkcolorchooserdialog.c
@@ -122,6 +122,19 @@ gtk_color_chooser_dialog_init (GtkColorChooserDialog *cc)
}
static void
+gtk_color_chooser_dialog_response (GtkDialog *dialog,
+ gint response_id)
+{
+ if (response_id == GTK_RESPONSE_OK)
+ {
+ GdkRGBA color;
+
+ gtk_color_chooser_get_color (GTK_COLOR_CHOOSER (dialog), &color);
+ gtk_color_chooser_set_color (GTK_COLOR_CHOOSER (dialog), &color);
+ }
+}
+
+static void
gtk_color_chooser_dialog_get_property (GObject *object,
guint prop_id,
GValue *value,
@@ -168,10 +181,13 @@ static void
gtk_color_chooser_dialog_class_init (GtkColorChooserDialogClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
+ GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
object_class->get_property = gtk_color_chooser_dialog_get_property;
object_class->set_property = gtk_color_chooser_dialog_set_property;
+ dialog_class->response = gtk_color_chooser_dialog_response;
+
g_object_class_override_property (object_class, PROP_COLOR, "color");
g_type_class_add_private (class, sizeof (GtkColorChooserDialogPrivate));
diff --git a/gtk/gtkcolorchooserwidget.c b/gtk/gtkcolorchooserwidget.c
index a4ad5e7..54c59cb 100644
--- a/gtk/gtkcolorchooserwidget.c
+++ b/gtk/gtkcolorchooserwidget.c
@@ -40,6 +40,7 @@ struct _GtkColorChooserWidgetPrivate
GtkWidget *grays;
GtkWidget *custom;
+ GtkWidget *button;
GtkColorSwatch *current;
GSettings *settings;
@@ -76,7 +77,7 @@ select_swatch (GtkColorChooserWidget *cc,
g_object_notify (G_OBJECT (cc), "color");
}
-static void save_custom (GtkColorChooserWidget *cc);
+static void save_custom_colors (GtkColorChooserWidget *cc);
static void
button_activate (GtkColorSwatch *swatch,
@@ -144,31 +145,32 @@ static void
connect_custom_signals (GtkWidget *p, gpointer data)
{
connect_swatch_signals (p, data);
- g_signal_connect_swapped (p, "notify::color", G_CALLBACK (save_custom), data);
+ g_signal_connect_swapped (p, "notify::color",
+ G_CALLBACK (save_custom_colors), data);
}
static void
-save_custom (GtkColorChooserWidget *cc)
+save_custom_colors (GtkColorChooserWidget *cc)
{
GVariantBuilder builder;
GVariant *variant;
GdkRGBA color;
- GList *children, *l;
+ GtkWidget *child;
+ gint i;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(dddd)"));
- children = gtk_container_get_children (GTK_CONTAINER (cc->priv->custom));
- for (l = children; l; l = l->next)
+ i = 1;
+ while ((child = gtk_grid_get_child_at (GTK_GRID (cc->priv->custom), i, 0)) != NULL)
{
- if (gtk_color_swatch_get_color (GTK_COLOR_SWATCH (l->data), &color))
+ i++;
+ if (gtk_color_swatch_get_color (GTK_COLOR_SWATCH (child), &color))
{
g_variant_builder_add (&builder, "(dddd)",
color.red, color.green, color.blue, color.alpha);
- }
+ }
}
- g_list_free (children);
-
variant = g_variant_builder_end (&builder);
g_settings_set_value (cc->priv->settings, "custom-colors", variant);
}
@@ -273,7 +275,7 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
gtk_grid_set_column_spacing (GTK_GRID (grid), 4);
gtk_container_add (GTK_CONTAINER (cc->priv->palette), grid);
- button = gtk_color_swatch_new ();
+ cc->priv->button = button = gtk_color_swatch_new ();
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (button), 10, 10, 10, 10);
connect_button_signals (button, cc);
gtk_color_swatch_set_icon (GTK_COLOR_SWATCH (button), "list-add-symbolic");
@@ -410,6 +412,37 @@ gtk_color_chooser_widget_get_color (GtkColorChooser *chooser,
}
static void
+add_custom_color (GtkColorChooserWidget *cc,
+ const GdkRGBA *color)
+{
+ GtkWidget *last;
+ GtkWidget *p;
+
+ last = gtk_grid_get_child_at (GTK_GRID (cc->priv->custom), 9, 0);
+ if (last)
+ gtk_container_remove (GTK_CONTAINER (cc->priv->custom), last);
+
+ gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (cc->priv->button), 10, 1, 1, 10);
+
+ p = gtk_color_swatch_new ();
+ gtk_color_swatch_set_color (GTK_COLOR_SWATCH (p), color);
+ gtk_color_swatch_set_can_drop (GTK_COLOR_SWATCH (p), TRUE);
+ connect_custom_signals (p, cc);
+
+ if (gtk_grid_get_child_at (GTK_GRID (cc->priv->custom), 1, 0) != NULL)
+ gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 1, 1, 1);
+ else
+ gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 10, 10, 1);
+
+ gtk_grid_insert_next_to (GTK_GRID (cc->priv->custom), cc->priv->button, GTK_POS_RIGHT);
+ gtk_grid_attach (GTK_GRID (cc->priv->custom), p, 1, 0, 1, 1);
+ gtk_widget_show (p);
+
+ select_swatch (cc, GTK_COLOR_SWATCH (p));
+ save_custom_colors (cc);
+}
+
+static void
gtk_color_chooser_widget_set_color (GtkColorChooser *chooser,
const GdkRGBA *color)
{
@@ -441,7 +474,7 @@ gtk_color_chooser_widget_set_color (GtkColorChooser *chooser,
g_list_free (children);
}
- /* FIXME: add new custom color */
+ add_custom_color (cc, color);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]