[gimp] app: add new widget GimpIconPicker, ripped out of GimpTemplateEditor



commit 30100cf6135bed8c70e6618a8717a3c4a5c955bc
Author: Michael Natterer <mitch gimp org>
Date:   Tue Mar 1 12:31:17 2011 +0100

    app: add new widget GimpIconPicker, ripped out of GimpTemplateEditor

 app/widgets/Makefile.am          |    2 +
 app/widgets/gimpiconpicker.c     |  301 ++++++++++++++++++++++++++++++++++++++
 app/widgets/gimpiconpicker.h     |   55 +++++++
 app/widgets/gimptemplateeditor.c |   90 +++---------
 app/widgets/widgets-types.h      |    1 +
 5 files changed, 379 insertions(+), 70 deletions(-)
---
diff --git a/app/widgets/Makefile.am b/app/widgets/Makefile.am
index 9960922..256d34a 100644
--- a/app/widgets/Makefile.am
+++ b/app/widgets/Makefile.am
@@ -199,6 +199,8 @@ libappwidgets_a_sources = \
 	gimphistogrameditor.h		\
 	gimphistogramview.c		\
 	gimphistogramview.h		\
+	gimpiconpicker.c		\
+	gimpiconpicker.h		\
 	gimpimagecommenteditor.c	\
 	gimpimagecommenteditor.h	\
 	gimpimageeditor.c		\
diff --git a/app/widgets/gimpiconpicker.c b/app/widgets/gimpiconpicker.c
new file mode 100644
index 0000000..a31431e
--- /dev/null
+++ b/app/widgets/gimpiconpicker.c
@@ -0,0 +1,301 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
+ *
+ * gimpiconpicker.c
+ * Copyright (C) 2011 Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gtk/gtk.h>
+
+#include "widgets-types.h"
+
+#include "core/gimp.h"
+#include "core/gimplist.h"
+#include "core/gimpcontext.h"
+#include "core/gimptemplate.h"
+
+#include "gimpiconpicker.h"
+#include "gimpviewablebutton.h"
+
+#include "gimp-intl.h"
+
+
+enum
+{
+  PROP_0,
+  PROP_GIMP,
+  PROP_STOCK_ID
+};
+
+
+typedef struct _GimpIconPickerPrivate GimpIconPickerPrivate;
+
+struct _GimpIconPickerPrivate
+{
+  Gimp          *gimp;
+
+  gchar         *stock_id;
+
+  GimpContainer *stock_id_container;
+  GimpContext   *stock_id_context;
+};
+
+#define GET_PRIVATE(picker) \
+        G_TYPE_INSTANCE_GET_PRIVATE (picker, \
+                                     GIMP_TYPE_ICON_PICKER, \
+                                     GimpIconPickerPrivate)
+
+
+static void    gimp_icon_picker_constructed  (GObject        *object);
+static void    gimp_icon_picker_finalize     (GObject        *object);
+static void    gimp_icon_picker_set_property (GObject        *object,
+                                              guint           property_id,
+                                              const GValue   *value,
+                                              GParamSpec     *pspec);
+static void    gimp_icon_picker_get_property (GObject        *object,
+                                              guint           property_id,
+                                              GValue         *value,
+                                              GParamSpec     *pspec);
+
+static void gimp_icon_picker_icon_changed    (GimpContext    *context,
+                                              GimpTemplate   *template,
+                                              GimpIconPicker *picker);
+
+
+G_DEFINE_TYPE (GimpIconPicker, gimp_icon_picker, GTK_TYPE_BOX)
+
+#define parent_class gimp_icon_picker_parent_class
+
+
+static void
+gimp_icon_picker_class_init (GimpIconPickerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed  = gimp_icon_picker_constructed;
+  object_class->finalize     = gimp_icon_picker_finalize;
+  object_class->set_property = gimp_icon_picker_set_property;
+  object_class->get_property = gimp_icon_picker_get_property;
+
+  g_object_class_install_property (object_class, PROP_GIMP,
+                                   g_param_spec_object ("gimp", NULL, NULL,
+                                                        GIMP_TYPE_GIMP,
+                                                        GIMP_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT_ONLY));
+
+  g_object_class_install_property (object_class, PROP_STOCK_ID,
+                                   g_param_spec_string ("stock-id", NULL, NULL,
+                                                        "gimp-toilet-paper",
+                                                        GIMP_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT));
+
+  g_type_class_add_private (object_class, sizeof (GimpIconPickerPrivate));
+}
+
+static void
+gimp_icon_picker_init (GimpIconPicker *picker)
+{
+  gtk_orientable_set_orientation (GTK_ORIENTABLE (picker),
+                                  GTK_ORIENTATION_HORIZONTAL);
+}
+
+static void
+gimp_icon_picker_constructed (GObject *object)
+{
+  GimpIconPicker        *picker  = GIMP_ICON_PICKER (object);
+  GimpIconPickerPrivate *private = GET_PRIVATE (object);
+  GtkWidget             *button;
+  GSList                *stock_list;
+  GSList                *list;
+
+  if (G_OBJECT_CLASS (parent_class)->constructed)
+    G_OBJECT_CLASS (parent_class)->constructed (object);
+
+  g_assert (GIMP_IS_GIMP (private->gimp));
+
+  private->stock_id_container = gimp_list_new (GIMP_TYPE_TEMPLATE, FALSE);
+  private->stock_id_context = gimp_context_new (private->gimp, "foo", NULL);
+
+  g_signal_connect (private->stock_id_context, "template-changed",
+                    G_CALLBACK (gimp_icon_picker_icon_changed),
+                    picker);
+
+  stock_list = gtk_stock_list_ids ();
+
+  for (list = stock_list; list; list = g_slist_next (list))
+    {
+      GimpObject *object = g_object_new (GIMP_TYPE_TEMPLATE,
+                                         "name",     list->data,
+                                         "stock-id", list->data,
+                                         NULL);
+
+      gimp_container_add (private->stock_id_container, object);
+      g_object_unref (object);
+
+      if (private->stock_id && strcmp (list->data, private->stock_id) == 0)
+        gimp_context_set_template (private->stock_id_context,
+                                   GIMP_TEMPLATE (object));
+    }
+
+  g_slist_foreach (stock_list, (GFunc) g_free, NULL);
+  g_slist_free (stock_list);
+
+  button = gimp_viewable_button_new (private->stock_id_container,
+                                     private->stock_id_context,
+                                     GIMP_VIEW_TYPE_LIST,
+                                     GIMP_VIEW_SIZE_SMALL,
+                                     GIMP_VIEW_SIZE_SMALL, 0,
+                                     NULL, NULL, NULL, NULL);
+  gimp_viewable_button_set_view_type (GIMP_VIEWABLE_BUTTON (button),
+                                      GIMP_VIEW_TYPE_GRID);
+  gtk_box_pack_start (GTK_BOX (picker), button, FALSE, FALSE, 0);
+  gtk_widget_show (button);
+}
+
+static void
+gimp_icon_picker_finalize (GObject *object)
+{
+  GimpIconPickerPrivate *private = GET_PRIVATE (object);
+
+  if (private->stock_id)
+    {
+      g_free (private->stock_id);
+      private->stock_id = NULL;
+    }
+
+  if (private->stock_id_container)
+    {
+      g_object_unref (private->stock_id_container);
+      private->stock_id_container = NULL;
+    }
+
+  if (private->stock_id_context)
+    {
+      g_object_unref (private->stock_id_context);
+      private->stock_id_context = NULL;
+    }
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gimp_icon_picker_set_property (GObject      *object,
+                               guint         property_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  GimpIconPickerPrivate *private = GET_PRIVATE (object);
+
+  switch (property_id)
+    {
+    case PROP_GIMP:
+      private->gimp = g_value_get_object (value); /* don't ref */
+      break;
+
+    case PROP_STOCK_ID:
+      gimp_icon_picker_set_stock_id (GIMP_ICON_PICKER (object),
+                                     g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_icon_picker_get_property (GObject      *object,
+                               guint         property_id,
+                               GValue       *value,
+                               GParamSpec   *pspec)
+{
+  GimpIconPickerPrivate *private = GET_PRIVATE (object);
+
+  switch (property_id)
+    {
+    case PROP_GIMP:
+      g_value_set_object (value, private->gimp);
+      break;
+
+    case PROP_STOCK_ID:
+      g_value_set_string (value, private->stock_id);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+GtkWidget *
+gimp_icon_picker_new (Gimp *gimp)
+{
+  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
+
+  return g_object_new (GIMP_TYPE_ICON_PICKER,
+                       "gimp", gimp,
+                       NULL);
+}
+
+const gchar *
+gimp_icon_picker_get_stock_id (GimpIconPicker *picker)
+{
+  g_return_val_if_fail (GIMP_IS_ICON_PICKER (picker), NULL);
+
+  return GET_PRIVATE (picker)->stock_id;
+}
+
+void
+gimp_icon_picker_set_stock_id (GimpIconPicker *picker,
+                               const gchar    *stock_id)
+{
+  GimpIconPickerPrivate *private;
+
+  g_return_if_fail (GIMP_IS_ICON_PICKER (picker));
+  g_return_if_fail (stock_id != NULL);
+
+  private = GET_PRIVATE (picker);
+
+  g_free (private->stock_id);
+  private->stock_id = g_strdup (stock_id);
+
+  if (private->stock_id_container)
+    {
+      GimpObject *object;
+
+      object = gimp_container_get_child_by_name (private->stock_id_container,
+                                                 stock_id);
+
+      if (object)
+        gimp_context_set_template (private->stock_id_context,
+                                   GIMP_TEMPLATE (object));
+    }
+
+  g_object_notify (G_OBJECT (picker), "stock-id");
+}
+
+
+/*  private functions  */
+
+static void
+gimp_icon_picker_icon_changed (GimpContext    *context,
+                               GimpTemplate   *template,
+                               GimpIconPicker *picker)
+{
+  gimp_icon_picker_set_stock_id (picker, gimp_object_get_name (template));
+}
diff --git a/app/widgets/gimpiconpicker.h b/app/widgets/gimpiconpicker.h
new file mode 100644
index 0000000..c7b2519
--- /dev/null
+++ b/app/widgets/gimpiconpicker.h
@@ -0,0 +1,55 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpiconpicker.h
+ * Copyright (C) 2011 Michael Natterer <mitch gimp org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_ICON_PICKER_H__
+#define __GIMP_ICON_PICKER_H__
+
+
+#define GIMP_TYPE_ICON_PICKER            (gimp_icon_picker_get_type ())
+#define GIMP_ICON_PICKER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_ICON_PICKER, GimpIconPicker))
+#define GIMP_ICON_PICKER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_ICON_PICKER, GimpIconPickerClass))
+#define GIMP_IS_ICON_PICKER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_ICON_PICKER))
+#define GIMP_IS_ICON_PICKER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_ICON_PICKER))
+#define GIMP_ICON_PICKER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_ICON_PICKER, GimpIconPickerClass))
+
+
+typedef struct _GimpIconPickerClass GimpIconPickerClass;
+
+struct _GimpIconPicker
+{
+  GtkBox  parent_instance;
+};
+
+struct _GimpIconPickerClass
+{
+  GtkBoxClass   parent_class;
+};
+
+
+GType          gimp_icon_picker_get_type    (void) G_GNUC_CONST;
+
+GtkWidget   * gimp_icon_picker_new          (Gimp           *gimp);
+
+const gchar * gimp_icon_picker_get_stock_id (GimpIconPicker *picker);
+void          gimp_icon_picker_set_stock_id (GimpIconPicker *picker,
+                                             const gchar    *stock_id);
+
+
+#endif  /*  __GIMP_ICON_PICKER_H__  */
diff --git a/app/widgets/gimptemplateeditor.c b/app/widgets/gimptemplateeditor.c
index 4486a69..915fde7 100644
--- a/app/widgets/gimptemplateeditor.c
+++ b/app/widgets/gimptemplateeditor.c
@@ -29,12 +29,10 @@
 #include "widgets-types.h"
 
 #include "core/gimp.h"
-#include "core/gimplist.h"
-#include "core/gimpcontext.h"
 #include "core/gimptemplate.h"
 
+#include "gimpiconpicker.h"
 #include "gimptemplateeditor.h"
-#include "gimpviewablebutton.h"
 
 #include "gimp-intl.h"
 
@@ -56,8 +54,7 @@ struct _GimpTemplateEditorPrivate
 {
   GimpTemplate  *template;
 
-  GimpContainer *stock_id_container;
-  GimpContext   *stock_id_context;
+  GtkWidget     *icon_picker;
 
   GtkWidget     *aspect_button;
   gboolean       block_aspect;
@@ -92,8 +89,8 @@ static void gimp_template_editor_aspect_callback (GtkWidget          *widget,
 static void gimp_template_editor_template_notify (GimpTemplate       *template,
                                                   GParamSpec         *param_spec,
                                                   GimpTemplateEditor *editor);
-static void gimp_template_editor_icon_changed    (GimpContext        *context,
-                                                  GimpTemplate       *template,
+static void gimp_template_editor_icon_changed    (GimpIconPicker     *picker,
+                                                  const GParamSpec   *pspec,
                                                   GimpTemplateEditor *editor);
 
 
@@ -433,18 +430,6 @@ gimp_template_editor_finalize (GObject *object)
       private->template = NULL;
     }
 
-  if (private->stock_id_container)
-    {
-      g_object_unref (private->stock_id_container);
-      private->stock_id_container = NULL;
-    }
-
-  if (private->stock_id_context)
-    {
-      g_object_unref (private->stock_id_context);
-      private->stock_id_context = NULL;
-    }
-
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -508,40 +493,8 @@ gimp_template_editor_new (GimpTemplate *template,
     {
       GtkWidget   *table;
       GtkWidget   *entry;
-      GtkWidget   *button;
-      GSList      *stock_list;
-      GSList      *list;
       const gchar *stock_id;
 
-      stock_id = gimp_viewable_get_stock_id (GIMP_VIEWABLE (private->template));
-
-      private->stock_id_container = gimp_list_new (GIMP_TYPE_TEMPLATE, FALSE);
-      private->stock_id_context = gimp_context_new (gimp, "foo", NULL);
-
-      g_signal_connect (private->stock_id_context, "template-changed",
-                        G_CALLBACK (gimp_template_editor_icon_changed),
-                        editor);
-
-      stock_list = gtk_stock_list_ids ();
-
-      for (list = stock_list; list; list = g_slist_next (list))
-        {
-          GimpObject *object = g_object_new (GIMP_TYPE_TEMPLATE,
-                                             "name",     list->data,
-                                             "stock-id", list->data,
-                                             NULL);
-
-          gimp_container_add (private->stock_id_container, object);
-          g_object_unref (object);
-
-          if (strcmp (list->data, stock_id) == 0)
-            gimp_context_set_template (private->stock_id_context,
-                                       GIMP_TEMPLATE (object));
-        }
-
-      g_slist_foreach (stock_list, (GFunc) g_free, NULL);
-      g_slist_free (stock_list);
-
       table = gtk_table_new (2, 2, FALSE);
       gtk_table_set_col_spacings (GTK_TABLE (table), 6);
       gtk_table_set_row_spacings (GTK_TABLE (table), 6);
@@ -555,18 +508,19 @@ gimp_template_editor_new (GimpTemplate *template,
                                  _("_Name:"), 1.0, 0.5,
                                  entry, 1, FALSE);
 
-      button = gimp_viewable_button_new (private->stock_id_container,
-                                         private->stock_id_context,
-                                         GIMP_VIEW_TYPE_LIST,
-                                         GIMP_VIEW_SIZE_SMALL,
-                                         GIMP_VIEW_SIZE_SMALL, 0,
-                                         NULL, NULL, NULL, NULL);
-      gimp_viewable_button_set_view_type (GIMP_VIEWABLE_BUTTON (button),
-                                          GIMP_VIEW_TYPE_GRID);
+      stock_id = gimp_viewable_get_stock_id (GIMP_VIEWABLE (private->template));
+
+      private->icon_picker = gimp_icon_picker_new (gimp);
+      gimp_icon_picker_set_stock_id (GIMP_ICON_PICKER (private->icon_picker),
+                                     stock_id);
+
+      g_signal_connect (private->icon_picker, "notify::stock-id",
+                        G_CALLBACK (gimp_template_editor_icon_changed),
+                        editor);
 
       gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
                                  _("_Icon:"), 1.0, 0.5,
-                                 button, 1, TRUE);
+                                 private->icon_picker, 1, TRUE);
     }
 
   return GTK_WIDGET (editor);
@@ -726,29 +680,25 @@ gimp_template_editor_template_notify (GimpTemplate       *template,
   gtk_label_set_text (GTK_LABEL (private->more_label), text);
   g_free (text);
 
-  if (private->stock_id_container)
+  if (private->icon_picker)
     {
-      GimpObject  *object;
       const gchar *stock_id;
 
       stock_id = gimp_viewable_get_stock_id (GIMP_VIEWABLE (template));
 
-      object = gimp_container_get_child_by_name (private->stock_id_container,
-                                                 stock_id);
-
-      gimp_context_set_template (private->stock_id_context,
-                                 (GimpTemplate *) object);
+      gimp_icon_picker_set_stock_id (GIMP_ICON_PICKER (private->icon_picker),
+                                     stock_id);
     }
 }
 
 static void
-gimp_template_editor_icon_changed (GimpContext        *context,
-                                   GimpTemplate       *template,
+gimp_template_editor_icon_changed (GimpIconPicker     *picker,
+                                   const GParamSpec   *pspec,
                                    GimpTemplateEditor *editor)
 {
   GimpTemplateEditorPrivate *private = GET_PRIVATE (editor);
 
   g_object_set (private->template,
-                "stock-id", gimp_object_get_name (template),
+                "stock-id", gimp_icon_picker_get_stock_id (picker),
                 NULL);
 }
diff --git a/app/widgets/widgets-types.h b/app/widgets/widgets-types.h
index 1336512..aa61b29 100644
--- a/app/widgets/widgets-types.h
+++ b/app/widgets/widgets-types.h
@@ -176,6 +176,7 @@ typedef struct _GimpGridEditor               GimpGridEditor;
 typedef struct _GimpHandleBar                GimpHandleBar;
 typedef struct _GimpHistogramBox             GimpHistogramBox;
 typedef struct _GimpHistogramView            GimpHistogramView;
+typedef struct _GimpIconPicker               GimpIconPicker;
 typedef struct _GimpImageCommentEditor       GimpImageCommentEditor;
 typedef struct _GimpImageParasiteView        GimpImageParasiteView;
 typedef struct _GimpImageProfileView         GimpImageProfileView;



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