[gimp] libgimp, libgimpwidgets: support of GimpRGB properties in…



commit ca72c41fcd6d303aa30a6a90a5fba1f280afb532
Author: Jehan <jehan girinstud io>
Date:   Tue Apr 20 16:40:18 2021 +0200

    libgimp, libgimpwidgets: support of GimpRGB properties in…
    
    … GimpProcedureDialog.
    
    Technically I added:
    - New gimp_prop_color_select_new() property widget to create a
      GimpColorButton for a given GimpRGB property.
    - gimp_procedure_dialog_get_widget() now supports a GimpRGB property and
      will create a GimpColorArea by default.
    - When the default doesn't suit you, a new function
      gimp_procedure_dialog_get_color_widget() allows to create either a
      GimpColorArea or a GimpColorButton (editable = TRUE), as well as
      choose the area type (small or large checks, as well as flat area,
      i.e. no alpha support).

 libgimp/gimpproceduredialog.c    | 86 ++++++++++++++++++++++++++++++++++++++++
 libgimp/gimpproceduredialog.h    |  4 ++
 libgimpwidgets/gimppropwidgets.c | 47 ++++++++++++++++++++++
 libgimpwidgets/gimppropwidgets.h |  6 +++
 4 files changed, 143 insertions(+)
---
diff --git a/libgimp/gimpproceduredialog.c b/libgimp/gimpproceduredialog.c
index 45f2926d24..cf5b12225a 100644
--- a/libgimp/gimpproceduredialog.c
+++ b/libgimp/gimpproceduredialog.c
@@ -580,6 +580,12 @@ gimp_procedure_dialog_get_widget (GimpProcedureDialog *dialog,
                                         property, -1);
         }
     }
+  else if (G_PARAM_SPEC_TYPE (pspec) == GIMP_TYPE_PARAM_RGB)
+    {
+      widget = gimp_prop_color_area_new (G_OBJECT (dialog->priv->config),
+                                         property, 20, 20,
+                                         GIMP_COLOR_AREA_SMALL_CHECKS);
+    }
   else
     {
       g_warning ("%s: parameter %s has non supported type %s",
@@ -625,6 +631,86 @@ gimp_procedure_dialog_get_widget (GimpProcedureDialog *dialog,
   return widget;
 }
 
+/**
+ * gimp_procedure_dialog_get_color_widget:
+ * @dialog:   the associated #GimpProcedureDialog.
+ * @property: name of the #GimpRGB property to build a widget for. It
+ *            must be a property of the #GimpProcedure @dialog has been
+ *            created for.
+ * @editable: whether the color can be edited or is only for display.
+ * @type:     the #GimpColorAreaType.
+ *
+ * Creates a new widget for @property which must necessarily be a
+ * #GimpRGB property.
+ * This must be used instead of gimp_procedure_dialog_get_widget() when
+ * you want more customizability for an RGB property.
+ *
+ * If a widget has already been created for this procedure, it will be
+ * returned instead (whatever its actual widget type).
+ *
+ * Returns: (transfer none): a #GimpColorButton representing @property
+ *                           if @editable is %TRUE, a #GimpColorArea otherwise.
+ *                           The object belongs to @dialog and must not
+ *                           be freed.
+ */
+GtkWidget *
+gimp_procedure_dialog_get_color_widget (GimpProcedureDialog *dialog,
+                                        const gchar         *property,
+                                        gboolean             editable,
+                                        GimpColorAreaType    type)
+{
+  GtkWidget  *widget = NULL;
+  GParamSpec *pspec;
+
+  g_return_val_if_fail (property != NULL, NULL);
+
+  /* First check if it already exists. */
+  widget = g_hash_table_lookup (dialog->priv->widgets, property);
+
+  if (widget)
+    return widget;
+
+  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (dialog->priv->config),
+                                        property);
+  if (! pspec)
+    {
+      g_warning ("%s: parameter %s does not exist.",
+                 G_STRFUNC, property);
+      return NULL;
+    }
+
+  if (G_PARAM_SPEC_TYPE (pspec) == GIMP_TYPE_PARAM_RGB)
+    {
+      if (editable)
+        widget = gimp_prop_color_select_new (G_OBJECT (dialog->priv->config),
+                                             property, 20, 20, type);
+      else
+        widget = gimp_prop_color_area_new (G_OBJECT (dialog->priv->config),
+                                           property, 20, 20, type);
+
+      gtk_widget_set_vexpand (widget, FALSE);
+      gtk_widget_set_hexpand (widget, TRUE);
+    }
+
+  if (! widget)
+    {
+      g_warning ("%s: parameter '%s' of type %s not suitable as color widget",
+                 G_STRFUNC, property, G_PARAM_SPEC_TYPE_NAME (pspec));
+      return NULL;
+    }
+  else if (GIMP_IS_LABELED (widget))
+    {
+      GtkWidget *label = gimp_labeled_get_label (GIMP_LABELED (widget));
+
+      gtk_size_group_add_widget (dialog->priv->label_group, label);
+    }
+
+  gimp_procedure_dialog_check_mnemonic (dialog, widget, property, NULL);
+  g_hash_table_insert (dialog->priv->widgets, g_strdup (property), widget);
+
+  return widget;
+}
+
 /**
  * gimp_procedure_dialog_get_int_combo:
  * @dialog:   the associated #GimpProcedureDialog.
diff --git a/libgimp/gimpproceduredialog.h b/libgimp/gimpproceduredialog.h
index 45253fab39..289b68116f 100644
--- a/libgimp/gimpproceduredialog.h
+++ b/libgimp/gimpproceduredialog.h
@@ -78,6 +78,10 @@ GtkWidget * gimp_procedure_dialog_new               (GimpProcedure       *proced
 GtkWidget * gimp_procedure_dialog_get_widget        (GimpProcedureDialog *dialog,
                                                      const gchar         *property,
                                                      GType                widget_type);
+GtkWidget * gimp_procedure_dialog_get_color_widget  (GimpProcedureDialog *dialog,
+                                                     const gchar         *property,
+                                                     gboolean             editable,
+                                                     GimpColorAreaType    type);
 GtkWidget * gimp_procedure_dialog_get_int_combo     (GimpProcedureDialog *dialog,
                                                      const gchar         *property,
                                                      GimpIntStore        *store);
diff --git a/libgimpwidgets/gimppropwidgets.c b/libgimpwidgets/gimppropwidgets.c
index 6faaa7117d..34db39d2a6 100644
--- a/libgimpwidgets/gimppropwidgets.c
+++ b/libgimpwidgets/gimppropwidgets.c
@@ -3750,6 +3750,53 @@ gimp_prop_color_area_notify (GObject    *config,
                                      config);
 }
 
+/**
+ * gimp_prop_color_select_new:
+ * @config:        Object to which property is attached.
+ * @property_name: Name of RGB property.
+ * @width:         Width of the colorpreview in pixels.
+ * @height:        Height of the colorpreview in pixels.
+ * @type:          How transparency is represented.
+ *
+ * Creates a #GimpColorButton to set and display the value of an RGB
+ * property.
+ *
+ * Returns: (transfer full): A new #GimpColorButton widget.
+ *
+ * Since: 3.0
+ */
+GtkWidget *
+gimp_prop_color_select_new (GObject           *config,
+                            const gchar       *property_name,
+                            gint               width,
+                            gint               height,
+                            GimpColorAreaType  type)
+{
+  GParamSpec *param_spec;
+  GtkWidget  *button;
+  GimpRGB    *value;
+
+  param_spec = check_param_spec_w (config, property_name,
+                                   GIMP_TYPE_PARAM_RGB, G_STRFUNC);
+  if (! param_spec)
+    return NULL;
+
+  g_object_get (config,
+                property_name, &value,
+                NULL);
+
+  button = gimp_color_button_new (g_param_spec_get_nick (param_spec),
+                                  width, height, value, type);
+
+  g_free (value);
+
+  g_object_bind_property (config, property_name,
+                          button, "color",
+                          G_BINDING_BIDIRECTIONAL);
+  gtk_widget_show (button);
+
+  return button;
+}
 
 /********************/
 /*  unit combo box  */
diff --git a/libgimpwidgets/gimppropwidgets.h b/libgimpwidgets/gimppropwidgets.h
index 18fc441dd7..120df1ab2c 100644
--- a/libgimpwidgets/gimppropwidgets.h
+++ b/libgimpwidgets/gimppropwidgets.h
@@ -213,6 +213,12 @@ GtkWidget     * gimp_prop_color_area_new          (GObject       *config,
                                                    gint           height,
                                                    GimpColorAreaType  type);
 
+GtkWidget     * gimp_prop_color_select_new        (GObject       *config,
+                                                   const gchar   *property_name,
+                                                   gint           width,
+                                                   gint           height,
+                                                   GimpColorAreaType  type);
+
 /*  GimpParamUnit  */
 
 GtkWidget     * gimp_prop_unit_combo_box_new      (GObject       *config,


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