[gimp] libgimpwidgets: add gimp_unit_store_set,get_has_pixels()



commit e5faf090ca5f1420502efd3c40321d1d91a74548
Author: Michael Natterer <mitch gimp org>
Date:   Fri Oct 15 00:15:48 2010 +0200

    libgimpwidgets: add gimp_unit_store_set,get_has_pixels()
    
    to allow having unit combos without "pixels". Adapt GimpUnitComboBox
    to not assume that the unit is equal to the index in the store and
    enable "menu_has_pixels" in GimpSizeEntry again.

 libgimpwidgets/gimpsizeentry.c    |    3 +
 libgimpwidgets/gimpunitcombobox.c |   35 +++++++++-
 libgimpwidgets/gimpunitstore.c    |  136 +++++++++++++++++++++++++++++++++----
 libgimpwidgets/gimpunitstore.h    |    4 +
 4 files changed, 162 insertions(+), 16 deletions(-)
---
diff --git a/libgimpwidgets/gimpsizeentry.c b/libgimpwidgets/gimpsizeentry.c
index d0dff2f..46b0699 100644
--- a/libgimpwidgets/gimpsizeentry.c
+++ b/libgimpwidgets/gimpsizeentry.c
@@ -378,6 +378,9 @@ gimp_size_entry_new (gint                       number_of_fields,
     }
 
   store = gimp_unit_store_new (gse->number_of_fields);
+  gimp_unit_store_set_has_pixels (store, gse->menu_show_pixels);
+  /* FIXME menu_show_percent */
+
   gse->unitmenu = gimp_unit_combo_box_new_with_model (store);
   g_object_unref (store);
 
diff --git a/libgimpwidgets/gimpunitcombobox.c b/libgimpwidgets/gimpunitcombobox.c
index ae51a13..af9acbf 100644
--- a/libgimpwidgets/gimpunitcombobox.c
+++ b/libgimpwidgets/gimpunitcombobox.c
@@ -131,16 +131,47 @@ gimp_unit_combo_box_new_with_model (GimpUnitStore *model)
 GimpUnit
 gimp_unit_combo_box_get_active (GimpUnitComboBox *combo)
 {
+  GtkTreeIter iter;
+  gint        unit;
+
   g_return_val_if_fail (GIMP_IS_UNIT_COMBO_BOX (combo), -1);
 
-  return gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
+  gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter);
+
+  gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (combo)), &iter,
+                      GIMP_UNIT_STORE_UNIT, &unit,
+                      -1);
+
+  return (GimpUnit) unit;
 }
 
 void
 gimp_unit_combo_box_set_active (GimpUnitComboBox *combo,
                                 GimpUnit          unit)
 {
+  GtkTreeModel *model;
+  GtkTreeIter   iter;
+  gboolean      iter_valid;
+
   g_return_if_fail (GIMP_IS_UNIT_COMBO_BOX (combo));
 
-  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), unit);
+  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
+
+  for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
+       iter_valid;
+       iter_valid = gtk_tree_model_iter_next (model, &iter))
+    {
+      gint iter_unit;
+
+      gtk_tree_model_get (model, &iter,
+                          GIMP_UNIT_STORE_UNIT, &iter_unit,
+                          -1);
+
+      if (unit == (GimpUnit) iter_unit)
+        {
+          gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
+          break;
+        }
+    }
+
 }
diff --git a/libgimpwidgets/gimpunitstore.c b/libgimpwidgets/gimpunitstore.c
index 2238a29..f122d56 100644
--- a/libgimpwidgets/gimpunitstore.c
+++ b/libgimpwidgets/gimpunitstore.c
@@ -33,14 +33,17 @@
 enum
 {
   PROP_0,
-  PROP_NUM_VALUES
+  PROP_NUM_VALUES,
+  PROP_HAS_PIXELS
 };
 
 typedef struct
 {
-  gint     num_values;
-  gdouble *values;
-  gdouble *resolutions;
+  gint      num_values;
+  gboolean  has_pixels;
+
+  gdouble  *values;
+  gdouble  *resolutions;
 } GimpUnitStorePrivate;
 
 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE (obj, \
@@ -129,12 +132,21 @@ gimp_unit_store_class_init (GimpUnitStoreClass *klass)
                                                      GIMP_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY));
 
+  g_object_class_install_property (object_class, PROP_HAS_PIXELS,
+                                   g_param_spec_boolean ("has-pixels",
+                                                         NULL, NULL,
+                                                         TRUE,
+                                                         GIMP_PARAM_READWRITE));
+
   g_type_class_add_private (object_class, sizeof (GimpUnitStorePrivate));
 }
 
 static void
 gimp_unit_store_init (GimpUnitStore *store)
 {
+  GimpUnitStorePrivate *private = GET_PRIVATE (store);
+
+  private->has_pixels = TRUE;
 }
 
 static void
@@ -188,6 +200,10 @@ gimp_unit_store_set_property (GObject      *object,
           private->resolutions = g_new0 (gdouble, private->num_values);
         }
       break;
+    case PROP_HAS_PIXELS:
+      gimp_unit_store_set_has_pixels (GIMP_UNIT_STORE (object),
+                                      g_value_get_boolean (value));
+      break;
 
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -208,6 +224,10 @@ gimp_unit_store_get_property (GObject      *object,
     case PROP_NUM_VALUES:
       g_value_set_int (value, private->num_values);
       break;
+    case PROP_HAS_PIXELS:
+      g_value_set_boolean (value, private->has_pixels);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
@@ -245,12 +265,16 @@ gimp_unit_store_get_iter (GtkTreeModel *tree_model,
                           GtkTreeIter  *iter,
                           GtkTreePath  *path)
 {
-  gint  unit;
+  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
+  gint                  unit;
 
   g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
 
   unit = gtk_tree_path_get_indices (path)[0];
 
+  if (! private->has_pixels)
+    unit++;
+
   if (unit >= 0 && unit < gimp_unit_get_number_of_units ())
     {
       iter->user_data = GINT_TO_POINTER (unit);
@@ -264,9 +288,14 @@ static GtkTreePath *
 gimp_unit_store_get_path (GtkTreeModel *tree_model,
                           GtkTreeIter  *iter)
 {
-  GtkTreePath *path = gtk_tree_path_new ();
+  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
+  GtkTreePath          *path    = gtk_tree_path_new ();
+  gint                  index   = GPOINTER_TO_INT (iter->user_data);
 
-  gtk_tree_path_append_index (path, GPOINTER_TO_INT (iter->user_data));
+  if (! private->has_pixels)
+    index--;
+
+  gtk_tree_path_append_index (path, GPOINTER_TO_INT (index));
 
   return path;
 }
@@ -341,7 +370,7 @@ static gboolean
 gimp_unit_store_iter_next (GtkTreeModel *tree_model,
                            GtkTreeIter  *iter)
 {
-  gint  unit  = GPOINTER_TO_INT (iter->user_data);
+  gint unit = GPOINTER_TO_INT (iter->user_data);
 
   unit++;
   if (unit > 0 && unit < gimp_unit_get_number_of_units ())
@@ -358,11 +387,16 @@ gimp_unit_store_iter_children (GtkTreeModel *tree_model,
                                GtkTreeIter  *iter,
                                GtkTreeIter  *parent)
 {
+  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
+
   /* this is a list, nodes have no children */
   if (parent)
     return FALSE;
 
-  iter->user_data = GINT_TO_POINTER (0);
+  if (private->has_pixels)
+    iter->user_data = GINT_TO_POINTER (GIMP_UNIT_PIXEL);
+  else
+    iter->user_data = GINT_TO_POINTER (GIMP_UNIT_INCH);
 
   return TRUE;
 }
@@ -378,10 +412,18 @@ static gint
 gimp_unit_store_iter_n_children (GtkTreeModel *tree_model,
                                  GtkTreeIter  *iter)
 {
+  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
+  gint                  n_children;
+
   if (iter)
     return 0;
 
-  return gimp_unit_get_number_of_units ();
+  n_children = gimp_unit_get_number_of_units ();
+
+  if (! private->has_pixels)
+    n_children--;
+
+  return n_children;
 }
 
 static gboolean
@@ -390,16 +432,23 @@ gimp_unit_store_iter_nth_child (GtkTreeModel *tree_model,
                                 GtkTreeIter  *parent,
                                 gint          n)
 {
-  GimpUnitStore *store;
+  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
+  gint                  n_children;
 
   if (parent)
     return FALSE;
 
-  store = GIMP_UNIT_STORE (tree_model);
+  n_children = gimp_unit_store_iter_n_children (tree_model, NULL);
 
-  if (n >= 0 && n < gimp_unit_get_number_of_units ())
+  if (n >= 0 && n < n_children)
     {
-      iter->user_data = GINT_TO_POINTER (n);
+      GimpUnit unit = n;
+
+      if (! private->has_pixels)
+        unit++;
+
+      iter->user_data = GINT_TO_POINTER (unit);
+
       return TRUE;
     }
 
@@ -424,6 +473,65 @@ gimp_unit_store_new (gint  num_values)
 }
 
 void
+gimp_unit_store_set_has_pixels (GimpUnitStore *store,
+                                gboolean       has_pixels)
+{
+  GimpUnitStorePrivate *private;
+
+  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
+
+  private = GET_PRIVATE (store);
+
+  has_pixels = has_pixels ? TRUE : FALSE;
+
+  if (has_pixels != private->has_pixels)
+    {
+      GtkTreeModel *model        = GTK_TREE_MODEL (store);
+      GtkTreePath  *deleted_path = NULL;
+
+      if (! has_pixels)
+        {
+          GtkTreeIter iter;
+
+          gtk_tree_model_get_iter_first (model, &iter);
+          deleted_path = gtk_tree_model_get_path (model, &iter);
+        }
+
+      private->has_pixels = has_pixels;
+
+      if (has_pixels)
+        {
+          GtkTreePath *path;
+          GtkTreeIter  iter;
+
+          gtk_tree_model_get_iter_first (model, &iter);
+          path = gtk_tree_model_get_path (model, &iter);
+          gtk_tree_model_row_inserted (model, path, &iter);
+          gtk_tree_path_free (path);
+        }
+      else if (deleted_path)
+        {
+          gtk_tree_model_row_deleted (model, deleted_path);
+          gtk_tree_path_free (deleted_path);
+        }
+
+      g_object_notify (G_OBJECT (store), "has-pixels");
+    }
+}
+
+gboolean
+gimp_unit_store_get_has_pixels (GimpUnitStore *store)
+{
+  GimpUnitStorePrivate *private;
+
+  g_return_val_if_fail (GIMP_IS_UNIT_STORE (store), FALSE);
+
+  private = GET_PRIVATE (store);
+
+  return private->has_pixels;
+}
+
+void
 gimp_unit_store_set_pixel_value (GimpUnitStore *store,
                                  gint           index,
                                  gdouble        value)
diff --git a/libgimpwidgets/gimpunitstore.h b/libgimpwidgets/gimpunitstore.h
index 54a1c86..4dcd67d 100644
--- a/libgimpwidgets/gimpunitstore.h
+++ b/libgimpwidgets/gimpunitstore.h
@@ -69,6 +69,10 @@ GType           gimp_unit_store_get_type (void) G_GNUC_CONST;
 
 GimpUnitStore * gimp_unit_store_new              (gint           num_values);
 
+void            gimp_unit_store_set_has_pixels   (GimpUnitStore *store,
+                                                  gboolean       has_pixels);
+gboolean        gimp_unit_store_get_has_pixels   (GimpUnitStore *store);
+
 void            gimp_unit_store_set_pixel_value  (GimpUnitStore *store,
                                                   gint           index,
                                                   gdouble        value);



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