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.
This commit is contained in:
Michael Natterer 2014-06-03 14:00:01 +02:00
parent 1416743b5c
commit 38c86da8e9
10 changed files with 134 additions and 117 deletions

View File

@ -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);
}
}

View File

@ -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,

View File

@ -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,

View File

@ -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.

View File

@ -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.
*/

View File

@ -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,

View File

@ -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);
}

View File

@ -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
{

View File

@ -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);
}
}

View File

@ -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
{