2008-04-15 13:21:13 <timj@imendio.com>
[hildon] / src / hildon-color-chooser-dialog.c
index ddfc349..456dcff 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * This file is part of hildon-libs
+ * This file is a part of hildon
  *
  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
  *
@@ -9,7 +9,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation; version 2.1 of
- * the License.
+ * 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
  *
  */
 
+/**
+ * SECTION:hildon-color-chooser-dialog
+ * @short_description: A dialog used to select a color from HSV colorspace.
+ * @see_also: #HildonColorButton
+ *
+ * HildonColorChooserDialog enables the user to 
+ * select an arbitrary color from a HSV colorspace.
+ * The color is stored in one of the predefined color 
+ * slots and can be reselected later on. 
+ *
+ * Additionally the user can choose one of the standard "factory"
+ * colors.
+ *
+ */
+
+#ifdef                                          HAVE_CONFIG_H
+#include                                        <config.h>
+#endif
+
 #include                                        <memory.h>
 #include                                        <string.h>
 #include                                        <libintl.h>
@@ -153,7 +172,7 @@ hildon_color_chooser_dialog_get_type            (void)
             NULL
         };
 
-        dialog_type = g_type_register_static (HILDON_TYPE_COLOR_CHOOSER_DIALOG, 
+        dialog_type = g_type_register_static (GTK_TYPE_DIALOG, 
                 "HildonColorChooserDialog", &dialog_info, 0);
     }
 
@@ -172,6 +191,7 @@ hildon_color_chooser_dialog_init                (HildonColorChooserDialog *objec
 
     priv->hbox = gtk_hbox_new (FALSE, 0);
     priv->vbox = gtk_vbox_new (FALSE, 0);
+    priv->chooser = hildon_color_chooser_new ();
 
     gtk_box_pack_start (GTK_BOX (priv->hbox), priv->chooser, TRUE, TRUE, 0);
     gtk_box_pack_end (GTK_BOX (priv->hbox), priv->vbox, FALSE, FALSE, 0);
@@ -275,7 +295,6 @@ hildon_color_chooser_dialog_class_init          (HildonColorChooserDialogClass *
                 GTK_TYPE_BORDER,
                 G_PARAM_READABLE));
 
-
     gtk_widget_class_install_style_property (widget_klass,
             g_param_spec_boxed ("default_color", "Default color",
                 "Default color for nonpainted custom colors",
@@ -292,6 +311,8 @@ hildon_color_chooser_dialog_class_init          (HildonColorChooserDialogClass *
                     GDK_TYPE_COLOR,
                     G_PARAM_READABLE));
     }
+
+    g_type_class_add_private (object_klass, sizeof (HildonColorChooserDialogPrivate));
 }
 
 static void 
@@ -468,6 +489,7 @@ hildon_color_chooser_dialog_unrealize           (GtkWidget *widget)
 
     for (i = 0; i < tmp; i++) {
         g_object_unref (priv->gc_array[i]);
+       priv->gc_array[i] = NULL;
     }
 
     GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
@@ -495,6 +517,7 @@ hildon_color_chooser_dialog_style_set           (GtkWidget *widget,
 
     if (tmpcolor) {
         priv->style_info.default_color = *tmpcolor;
+       gdk_color_free (tmpcolor);
     } else {
         priv->style_info.default_color.red   = 0x0000;
         priv->style_info.default_color.green = 0x0000;
@@ -516,6 +539,7 @@ hildon_color_chooser_dialog_style_set           (GtkWidget *widget,
 
                 for (i = 0; i < tmpn; i++) {
                     g_object_unref (priv->gc_array[i]);
+                   priv->gc_array[i] = NULL;
                 }
             }
 
@@ -534,6 +558,11 @@ hildon_color_chooser_dialog_style_set           (GtkWidget *widget,
             (priv->style_info.num_buttons.top * priv->style_info.num_buttons.bottom);
 
         priv->gc_array = (GdkGC **) g_malloc0 (sizeof (GdkGC *) * tmpn);
+       if (GTK_WIDGET_REALIZED (widget)) {
+                for (i = 0; i < tmpn; i++) {
+                   priv->gc_array[i] = gdk_gc_new (widget->window);
+                }
+       }
 
         if (priv->gconf_client) {
 
@@ -572,6 +601,7 @@ hildon_color_chooser_dialog_style_set           (GtkWidget *widget,
 
         if (tmpcolor) {
             priv->colors_defined[i] = *tmpcolor;
+           gdk_color_free (tmpcolor);
         } else {
             if(i < 16) {
                 priv->colors_defined[i] = hardcoded_colors[i];
@@ -757,6 +787,17 @@ hildon_color_chooser_dialog_destroy             (GtkObject *object)
     GTK_OBJECT_CLASS (parent_class)->destroy (object);
 }
 
+/**
+ * hildon_color_chooser_dialog_set_color:
+ * @dialog: a #HildonColorChooserDialog
+ * @color: a color to set on the #HildonColorChooserDialog
+ *
+ * Sets the dialog to point at the given color. It'll first try to
+ * search the palette of the existing colors to match the passed color. 
+ * If the color is not found in the pallette, the color in the currently 
+ * selected box will be modified.
+ *
+ */
 void 
 hildon_color_chooser_dialog_set_color           (HildonColorChooserDialog *dialog, 
                                                  GdkColor *color)
@@ -931,16 +972,16 @@ hildon_color_chooser_dialog_color_changed       (HildonColorChooser *chooser,
     HildonColorChooserDialogPrivate *priv = HILDON_COLOR_CHOOSER_DIALOG_GET_PRIVATE (data);
     char key[128], color_str[13];
     int tmp;
-    GdkColor *color;
+    GdkColor color;
 
     g_assert (priv);
 
-    g_object_get (G_OBJECT (chooser), "color", &color, NULL);
+    hildon_color_chooser_get_color (chooser, &color);
 
     tmp = (priv->style_info.num_buttons.left * priv->style_info.num_buttons.right);
 
     if (priv->selected >= tmp) {
-        priv->colors_custom[priv->selected - tmp] = *color;
+        priv->colors_custom[priv->selected - tmp] = color;
 
         gdk_gc_set_rgb_fg_color (priv->gc_array[priv->selected], &priv->colors_custom[priv->selected - tmp]);
         gtk_widget_queue_draw (priv->area_custom);
@@ -979,7 +1020,7 @@ hildon_color_chooser_dialog_refresh_style_info  (HildonColorChooserDialog *dialo
 
     if (tmp1) {
         priv->style_info.cont_sizes = *tmp1;
-        g_free (tmp1);
+        gtk_border_free (tmp1);
     } else {
         priv->style_info.cont_sizes.left = 0;
         priv->style_info.cont_sizes.right = 8;
@@ -989,7 +1030,7 @@ hildon_color_chooser_dialog_refresh_style_info  (HildonColorChooserDialog *dialo
 
     if (tmp2) {
         priv->style_info.radio_sizes = *tmp2;
-        g_free (tmp2);
+        gtk_border_free (tmp2);
     } else {
         priv->style_info.radio_sizes.left = 16;
         priv->style_info.radio_sizes.right = 16;
@@ -999,7 +1040,7 @@ hildon_color_chooser_dialog_refresh_style_info  (HildonColorChooserDialog *dialo
 
     if (tmp3) {
         priv->style_info.num_buttons = *tmp3;
-        g_free (tmp3);
+        gtk_border_free (tmp3);
     } else {
         priv->style_info.num_buttons.left = 8;
         priv->style_info.num_buttons.right = 2;
@@ -1087,7 +1128,9 @@ hildon_color_chooser_dialog_new                 (void)
 /**
  * hildon_color_chooser_dialog_get_color:
  * @dialog: a #HildonColorChooserDialog
- * @color: a color to fill with new values
+ * @color: a color structure to fill with the currently selected color
+ *
+ * Retrives the currently selected color in the color chooser dialog.
  *
  */
 void
@@ -1099,7 +1142,8 @@ hildon_color_chooser_dialog_get_color           (HildonColorChooserDialog *dialo
 
     g_return_if_fail (HILDON_IS_COLOR_CHOOSER_DIALOG (dialog));
     priv = HILDON_COLOR_CHOOSER_DIALOG_GET_PRIVATE (dialog);
+    g_assert (priv);
 
-    g_object_get (G_OBJECT (priv->chooser), "color", &color, NULL);
+    hildon_color_chooser_get_color (HILDON_COLOR_CHOOSER (priv->chooser), color);
 }