[gimp] app: factor out utility function gimp_get_fill_params()



commit 38c86da8e96b59456e95b374222b5d628c57dac7
Author: Michael Natterer <mitch gimp org>
Date:   Tue Jun 3 14:00:01 2014 +0200

    app: factor out utility function gimp_get_fill_params()
    
    which returns an error if there is no pattern for GIMP_FILL_PATTERN.
    Use it instead of having the same code in 3 variants, and make error
    handling consistent with bucket fill.

 app/actions/edit-commands.c         |   16 ++++++++--
 app/core/gimp-edit.c                |   33 ++++++++-----------
 app/core/gimp-edit.h                |    3 +-
 app/core/gimp-utils.c               |   57 ++++++++++++++++++++++++++++++++++-
 app/core/gimp-utils.h               |    6 ++++
 app/core/gimpdrawable-bucket-fill.c |   40 ++----------------------
 app/core/gimpdrawable.c             |   30 ++----------------
 app/pdb/edit-cmds.c                 |    9 ++++--
 app/tools/gimpbucketfilltool.c      |   48 ++++++++++++++--------------
 tools/pdbgen/pdb/edit.pdb           |    9 ++++--
 10 files changed, 134 insertions(+), 117 deletions(-)
---
diff --git a/app/actions/edit-commands.c b/app/actions/edit-commands.c
index a24ef60..017270d 100644
--- a/app/actions/edit-commands.c
+++ b/app/actions/edit-commands.c
@@ -497,13 +497,23 @@ edit_fill_cmd_callback (GtkAction *action,
   GimpImage    *image;
   GimpDrawable *drawable;
   GimpFillType  fill_type;
+  GError       *error = NULL;
   return_if_no_drawable (image, drawable, data);
 
   fill_type = (GimpFillType) value;
 
-  gimp_edit_fill (image, drawable, action_data_get_context (data),
-                  fill_type, GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
-  gimp_image_flush (image);
+  if (gimp_edit_fill (image, drawable, action_data_get_context (data),
+                      fill_type, GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
+                      &error))
+    {
+      gimp_image_flush (image);
+    }
+  else
+    {
+      gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_WARNING,
+                            error->message);
+      g_clear_error (&error);
+    }
 }
 
 
diff --git a/app/core/gimp-edit.c b/app/core/gimp-edit.c
index 2e13627..7caf6a1 100644
--- a/app/core/gimp-edit.c
+++ b/app/core/gimp-edit.c
@@ -32,6 +32,7 @@
 
 #include "gimp.h"
 #include "gimp-edit.h"
+#include "gimp-utils.h"
 #include "gimpbuffer.h"
 #include "gimpchannel.h"
 #include "gimpcontext.h"
@@ -420,54 +421,48 @@ gimp_edit_clear (GimpImage    *image,
 }
 
 gboolean
-gimp_edit_fill (GimpImage            *image,
-                GimpDrawable         *drawable,
-                GimpContext          *context,
-                GimpFillType          fill_type,
-                gdouble               opacity,
-                GimpLayerModeEffects  paint_mode)
+gimp_edit_fill (GimpImage             *image,
+                GimpDrawable          *drawable,
+                GimpContext           *context,
+                GimpFillType           fill_type,
+                gdouble                opacity,
+                GimpLayerModeEffects   paint_mode,
+                GError               **error)
 {
   GimpRGB      color;
-  GimpPattern *pattern = NULL;
-  const gchar *undo_desc;
+  GimpPattern *pattern;
+  const gchar *undo_desc = NULL;
 
   g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
   g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
   g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
   g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  if (! gimp_get_fill_params (context, fill_type, &color, &pattern, error))
+    return FALSE;
 
   switch (fill_type)
     {
     case GIMP_FILL_FOREGROUND:
-      gimp_context_get_foreground (context, &color);
       undo_desc = C_("undo-type", "Fill with Foreground Color");
       break;
 
     case GIMP_FILL_BACKGROUND:
-      gimp_context_get_background (context, &color);
       undo_desc = C_("undo-type", "Fill with Background Color");
       break;
 
     case GIMP_FILL_WHITE:
-      gimp_rgba_set (&color, 1.0, 1.0, 1.0, GIMP_OPACITY_OPAQUE);
       undo_desc = C_("undo-type", "Fill with White");
       break;
 
     case GIMP_FILL_TRANSPARENT:
-      gimp_context_get_background (context, &color);
       undo_desc = C_("undo-type", "Fill with Transparency");
       break;
 
     case GIMP_FILL_PATTERN:
-      pattern = gimp_context_get_pattern (context);
       undo_desc = C_("undo-type", "Fill with Pattern");
       break;
-
-    default:
-      g_warning ("%s: unknown fill type", G_STRFUNC);
-      return gimp_edit_fill (image, drawable,
-                             context, GIMP_FILL_BACKGROUND,
-                             GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
     }
 
   return gimp_edit_fill_full (image, drawable,
diff --git a/app/core/gimp-edit.h b/app/core/gimp-edit.h
index 0d44086..38b1a94 100644
--- a/app/core/gimp-edit.h
+++ b/app/core/gimp-edit.h
@@ -62,7 +62,8 @@ gboolean           gimp_edit_fill               (GimpImage     *image,
                                                  GimpContext   *context,
                                                  GimpFillType   fill_type,
                                                  gdouble        opacity,
-                                                 GimpLayerModeEffects  paint_mode);
+                                                 GimpLayerModeEffects  paint_mode,
+                                                 GError       **error);
 
 gboolean           gimp_edit_fill_full          (GimpImage     *image,
                                                  GimpDrawable  *drawable,
diff --git a/app/core/gimp-utils.c b/app/core/gimp-utils.c
index 43f7334..7980bb4 100644
--- a/app/core/gimp-utils.c
+++ b/app/core/gimp-utils.c
@@ -61,10 +61,13 @@
 #include "gimp-utils.h"
 #include "gimpcontainer.h"
 #include "gimpcontext.h"
+#include "gimperror.h"
 #include "gimpparamspecs.h"
 
+#include "gimp-intl.h"
 
-#define MAX_FUNC               100
+
+#define MAX_FUNC 100
 
 
 gint64
@@ -767,6 +770,58 @@ gimp_enum_get_value_name (GType enum_type,
   return value_name;
 }
 
+gboolean
+gimp_get_fill_params (GimpContext   *context,
+                      GimpFillType   fill_type,
+                      GimpRGB       *color,
+                      GimpPattern  **pattern,
+                      GError       **error)
+
+{
+  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
+  g_return_val_if_fail (color != NULL, NULL);
+  g_return_val_if_fail (pattern != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  *pattern = NULL;
+
+  switch (fill_type)
+    {
+    case GIMP_FILL_FOREGROUND:
+      gimp_context_get_foreground (context, color);
+      break;
+
+    case GIMP_FILL_BACKGROUND:
+      gimp_context_get_background (context, color);
+      break;
+
+    case GIMP_FILL_WHITE:
+      gimp_rgba_set (color, 1.0, 1.0, 1.0, GIMP_OPACITY_OPAQUE);
+      break;
+
+    case GIMP_FILL_TRANSPARENT:
+      gimp_rgba_set (color, 0.0, 0.0, 0.0, GIMP_OPACITY_TRANSPARENT);
+      break;
+
+    case GIMP_FILL_PATTERN:
+      *pattern = gimp_context_get_pattern (context);
+
+      if (! *pattern)
+        {
+          g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
+                              _("No patterns available for this operation."));
+          return FALSE;
+        }
+      break;
+
+    default:
+      g_warning ("%s: invalid fill_type %d", G_STRFUNC, fill_type);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
 /**
  * gimp_utils_point_to_line_distance:
  * @point:              The point to calculate the distance for.
diff --git a/app/core/gimp-utils.h b/app/core/gimp-utils.h
index 3165155..f1126e0 100644
--- a/app/core/gimp-utils.h
+++ b/app/core/gimp-utils.h
@@ -80,6 +80,12 @@ gchar      * gimp_markup_extract_text              (const gchar     *markup);
 const gchar* gimp_enum_get_value_name              (GType            enum_type,
                                                     gint             value);
 
+gboolean     gimp_get_fill_params                  (GimpContext      *context,
+                                                    GimpFillType      fill_type,
+                                                    GimpRGB          *color,
+                                                    GimpPattern     **pattern,
+                                                    GError          **error);
+
 /* Common values for the n_snap_lines parameter of
  * gimp_constrain_line.
  */
diff --git a/app/core/gimpdrawable-bucket-fill.c b/app/core/gimpdrawable-bucket-fill.c
index fb2178a..ae67fb4 100644
--- a/app/core/gimpdrawable-bucket-fill.c
+++ b/app/core/gimpdrawable-bucket-fill.c
@@ -37,7 +37,6 @@
 #include "gimpcontext.h"
 #include "gimpdrawable.h"
 #include "gimpdrawable-bucket-fill.h"
-#include "gimperror.h"
 #include "gimpimage.h"
 #include "gimppattern.h"
 #include "gimppickable.h"
@@ -76,47 +75,16 @@ gimp_drawable_bucket_fill (GimpDrawable         *drawable,
                            gdouble               y,
                            GError              **error)
 {
-  GimpRGB      color   = { 0, };
-  GimpPattern *pattern = NULL;
+  GimpRGB      color;
+  GimpPattern *pattern;
 
   g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
   g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
   g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-  switch (fill_type)
-    {
-    case GIMP_FILL_FOREGROUND:
-      gimp_context_get_foreground (context, &color);
-      break;
-
-    case GIMP_FILL_BACKGROUND:
-      gimp_context_get_background (context, &color);
-      break;
-
-    case GIMP_FILL_WHITE:
-      gimp_rgba_set (&color, 1.0, 1.0, 1.0, 1.0);
-      break;
-
-    case GIMP_FILL_TRANSPARENT:
-      gimp_rgba_set (&color, 0.0, 0.0, 0.0, 0.0);
-      break;
-
-    case GIMP_FILL_PATTERN:
-      pattern = gimp_context_get_pattern (context);
-
-      if (! pattern)
-        {
-          g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
-                              _("No patterns available for this operation."));
-          return FALSE;
-        }
-      break;
-
-    default:
-      g_warning ("%s: invalid fill_type passed", G_STRFUNC);
-      return FALSE;
-    }
+  if (! gimp_get_fill_params (context, &color, &pattern, error))
+    return FALSE;
 
   gimp_drawable_bucket_fill_internal (drawable,
                                       fill_type,
diff --git a/app/core/gimpdrawable.c b/app/core/gimpdrawable.c
index 1950470..9744797 100644
--- a/app/core/gimpdrawable.c
+++ b/app/core/gimpdrawable.c
@@ -1532,36 +1532,12 @@ gimp_drawable_fill_by_type (GimpDrawable *drawable,
                             GimpFillType  fill_type)
 {
   GimpRGB      color;
-  GimpPattern *pattern = NULL;
+  GimpPattern *pattern;
 
   g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
 
-  switch (fill_type)
-    {
-    case GIMP_FILL_FOREGROUND:
-      gimp_context_get_foreground (context, &color);
-      break;
-
-    case GIMP_FILL_BACKGROUND:
-      gimp_context_get_background (context, &color);
-      break;
-
-    case GIMP_FILL_WHITE:
-      gimp_rgba_set (&color, 1.0, 1.0, 1.0, GIMP_OPACITY_OPAQUE);
-      break;
-
-    case GIMP_FILL_TRANSPARENT:
-      gimp_rgba_set (&color, 0.0, 0.0, 0.0, GIMP_OPACITY_TRANSPARENT);
-      break;
-
-    case GIMP_FILL_PATTERN:
-      pattern = gimp_context_get_pattern (context);
-      break;
-
-    default:
-      g_warning ("%s: unknown fill type %d", G_STRFUNC, fill_type);
-      return;
-    }
+  if (! gimp_get_fill_params (context, fill_type, &color, &pattern, NULL))
+    return;
 
   gimp_drawable_fill (drawable, pattern ? NULL : &color, pattern);
 }
diff --git a/app/pdb/edit-cmds.c b/app/pdb/edit-cmds.c
index 6479f34..d1eecb1 100644
--- a/app/pdb/edit-cmds.c
+++ b/app/pdb/edit-cmds.c
@@ -557,7 +557,8 @@ edit_fill_invoker (GimpProcedure         *procedure,
 
           success = gimp_edit_fill (image, drawable, context,
                                     (GimpFillType) fill_type,
-                                    GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
+                                    GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
+                                    error);
         }
       else
         success = FALSE;
@@ -622,7 +623,8 @@ edit_bucket_fill_invoker (GimpProcedure         *procedure,
           if (! gimp_channel_is_empty (gimp_image_get_mask (image)))
             {
               success = gimp_edit_fill (image, drawable, context, fill_type,
-                                        GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
+                                        GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
+                                        error);
             }
           else
             {
@@ -702,7 +704,8 @@ edit_bucket_fill_full_invoker (GimpProcedure         *procedure,
           if (! gimp_channel_is_empty (gimp_image_get_mask (image)))
             {
               success = gimp_edit_fill (image, drawable, context, fill_type,
-                                        GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
+                                        GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
+                                        error);
             }
           else
             {
diff --git a/app/tools/gimpbucketfilltool.c b/app/tools/gimpbucketfilltool.c
index be3ac8c..0ba7e46 100644
--- a/app/tools/gimpbucketfilltool.c
+++ b/app/tools/gimpbucketfilltool.c
@@ -175,6 +175,8 @@ gimp_bucket_fill_tool_button_release (GimpTool              *tool,
       GimpContext  *context  = GIMP_CONTEXT (options);
       GimpFillType  fill_type;
       gint          x, y;
+      gboolean      success;
+      GError       *error = NULL;
 
       switch (options->fill_mode)
         {
@@ -205,34 +207,32 @@ gimp_bucket_fill_tool_button_release (GimpTool              *tool,
 
       if (options->fill_selection)
         {
-          gimp_edit_fill (image, drawable, context, fill_type,
-                          gimp_context_get_opacity (context),
-                          gimp_context_get_paint_mode (context));
+          success = gimp_edit_fill (image, drawable, context, fill_type,
+                                    gimp_context_get_opacity (context),
+                                    gimp_context_get_paint_mode (context),
+                                    &error);
+        }
+      else
+        {
+          success = gimp_drawable_bucket_fill (drawable, context, fill_type,
+                                               gimp_context_get_paint_mode (context),
+                                               gimp_context_get_opacity (context),
+                                               options->fill_transparent,
+                                               options->fill_criterion,
+                                               options->threshold / 255.0,
+                                               options->sample_merged,
+                                               x, y, &error);
+        }
+
+      if (success)
+        {
           gimp_image_flush (image);
         }
       else
         {
-          GError *error = NULL;
-
-          if (! gimp_drawable_bucket_fill (drawable,
-                                           context,
-                                           fill_type,
-                                           gimp_context_get_paint_mode (context),
-                                           gimp_context_get_opacity (context),
-                                           options->fill_transparent,
-                                           options->fill_criterion,
-                                           options->threshold / 255.0,
-                                           options->sample_merged,
-                                           x, y, &error))
-            {
-              gimp_message_literal (display->gimp, G_OBJECT (display),
-                                    GIMP_MESSAGE_WARNING, error->message);
-              g_clear_error (&error);
-            }
-          else
-            {
-              gimp_image_flush (image);
-            }
+          gimp_message_literal (display->gimp, G_OBJECT (display),
+                                GIMP_MESSAGE_WARNING, error->message);
+          g_clear_error (&error);
         }
     }
 
diff --git a/tools/pdbgen/pdb/edit.pdb b/tools/pdbgen/pdb/edit.pdb
index f5dc292..e2a54ab 100644
--- a/tools/pdbgen/pdb/edit.pdb
+++ b/tools/pdbgen/pdb/edit.pdb
@@ -569,7 +569,8 @@ HELP
 
       success = gimp_edit_fill (image, drawable, context,
                                 (GimpFillType) fill_type,
-                                GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
+                                GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
+                                error);
     }
   else
     success = FALSE;
@@ -658,7 +659,8 @@ HELP
       if (! gimp_channel_is_empty (gimp_image_get_mask (image)))
         {
           success = gimp_edit_fill (image, drawable, context, fill_type,
-                                    GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
+                                    GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
+                                    error);
         }
       else
         {
@@ -766,7 +768,8 @@ HELP
       if (! gimp_channel_is_empty (gimp_image_get_mask (image)))
         {
           success = gimp_edit_fill (image, drawable, context, fill_type,
-                                    GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
+                                    GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
+                                    error);
         }
       else
         {


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