gimpfilloptions.c 13.8 KB
Newer Older
1 2 3 4 5 6
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
 *
 * gimpfilloptions.c
 * Copyright (C) 2003 Simon Budig
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23
#include <cairo.h>
24
#include <gdk-pixbuf/gdk-pixbuf.h>
25
#include <gegl.h>
26

27
#include "libgimpcolor/gimpcolor.h"
28 29 30 31
#include "libgimpconfig/gimpconfig.h"

#include "core-types.h"

32
#include "gimp.h"
33 34
#include "gimp-palettes.h"
#include "gimpdrawable.h"
35
#include "gimpdrawable-fill.h"
36
#include "gimperror.h"
37
#include "gimpfilloptions.h"
38
#include "gimppattern.h"
39

40 41
#include "gimp-intl.h"

42 43 44 45 46

enum
{
  PROP_0,
  PROP_STYLE,
47 48 49
  PROP_ANTIALIAS,
  PROP_PATTERN_VIEW_TYPE,
  PROP_PATTERN_VIEW_SIZE
50 51 52
};


53 54 55 56 57 58 59 60 61
typedef struct _GimpFillOptionsPrivate GimpFillOptionsPrivate;

struct _GimpFillOptionsPrivate
{
  GimpFillStyle style;
  gboolean      antialias;

  GimpViewType  pattern_view_type;
  GimpViewSize  pattern_view_size;
62 63

  const gchar  *undo_desc;
64 65 66
};

#define GET_PRIVATE(options) \
67
        ((GimpFillOptionsPrivate *) gimp_fill_options_get_instance_private ((GimpFillOptions *) (options)))
68 69


70
static void     gimp_fill_options_config_init  (GimpConfigInterface *iface);
71

72 73 74 75 76 77 78 79
static void     gimp_fill_options_set_property (GObject             *object,
                                                guint                property_id,
                                                const GValue        *value,
                                                GParamSpec          *pspec);
static void     gimp_fill_options_get_property (GObject             *object,
                                                guint                property_id,
                                                GValue              *value,
                                                GParamSpec          *pspec);
80

81 82 83 84 85 86
static gboolean gimp_fill_options_serialize    (GimpConfig          *config,
                                                GimpConfigWriter    *writer,
                                                gpointer             data);


G_DEFINE_TYPE_WITH_CODE (GimpFillOptions, gimp_fill_options, GIMP_TYPE_CONTEXT,
87
                         G_ADD_PRIVATE (GimpFillOptions)
88 89
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG,
                                                gimp_fill_options_config_init))
90 91 92 93 94 95 96 97 98 99


static void
gimp_fill_options_class_init (GimpFillOptionsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = gimp_fill_options_set_property;
  object_class->get_property = gimp_fill_options_get_property;

100 101 102 103 104 105 106 107 108 109 110 111 112 113
  GIMP_CONFIG_PROP_ENUM (object_class, PROP_STYLE,
                         "style",
                         _("Style"),
                         NULL,
                         GIMP_TYPE_FILL_STYLE,
                         GIMP_FILL_STYLE_SOLID,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_ANTIALIAS,
                            "antialias",
                            _("Antialiasing"),
                            NULL,
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  g_object_class_install_property (object_class, PROP_PATTERN_VIEW_TYPE,
                                   g_param_spec_enum ("pattern-view-type",
                                                      NULL, NULL,
                                                      GIMP_TYPE_VIEW_TYPE,
                                                      GIMP_VIEW_TYPE_GRID,
                                                      G_PARAM_CONSTRUCT |
                                                      GIMP_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_PATTERN_VIEW_SIZE,
                                   g_param_spec_int ("pattern-view-size",
                                                     NULL, NULL,
                                                     GIMP_VIEW_SIZE_TINY,
                                                     GIMP_VIEWABLE_MAX_BUTTON_SIZE,
                                                     GIMP_VIEW_SIZE_SMALL,
                                                     G_PARAM_CONSTRUCT |
                                                     GIMP_PARAM_READWRITE));
131 132
}

133 134 135 136 137 138
static void
gimp_fill_options_config_init (GimpConfigInterface *iface)
{
  iface->serialize = gimp_fill_options_serialize;
}

139 140 141 142 143 144 145 146 147 148 149
static void
gimp_fill_options_init (GimpFillOptions *options)
{
}

static void
gimp_fill_options_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
150
  GimpFillOptionsPrivate *private = GET_PRIVATE (object);
151 152 153 154

  switch (property_id)
    {
    case PROP_STYLE:
155
      private->style = g_value_get_enum (value);
156
      private->undo_desc = NULL;
157 158
      break;
    case PROP_ANTIALIAS:
159
      private->antialias = g_value_get_boolean (value);
160 161
      break;

162
    case PROP_PATTERN_VIEW_TYPE:
163
      private->pattern_view_type = g_value_get_enum (value);
164 165
      break;
    case PROP_PATTERN_VIEW_SIZE:
166
      private->pattern_view_size = g_value_get_int (value);
167 168
      break;

169 170 171 172 173 174 175 176 177 178 179 180
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_fill_options_get_property (GObject    *object,
                                guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
181
  GimpFillOptionsPrivate *private = GET_PRIVATE (object);
182 183 184 185

  switch (property_id)
    {
    case PROP_STYLE:
186
      g_value_set_enum (value, private->style);
187 188
      break;
    case PROP_ANTIALIAS:
189
      g_value_set_boolean (value, private->antialias);
190 191
      break;

192
    case PROP_PATTERN_VIEW_TYPE:
193
      g_value_set_enum (value, private->pattern_view_type);
194 195
      break;
    case PROP_PATTERN_VIEW_SIZE:
196
      g_value_set_int (value, private->pattern_view_size);
197 198
      break;

199 200 201 202 203
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}
204

205 206 207 208 209 210 211 212
static gboolean
gimp_fill_options_serialize (GimpConfig       *config,
                             GimpConfigWriter *writer,
                             gpointer          data)
{
  return gimp_config_serialize_properties (config, writer);
}

213 214 215 216

/*  public functions  */

GimpFillOptions *
217 218 219
gimp_fill_options_new (Gimp        *gimp,
                       GimpContext *context,
                       gboolean     use_context_color)
220
{
221 222
  GimpFillOptions *options;

223
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  g_return_val_if_fail (context == NULL || GIMP_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (use_context_color == FALSE || context != NULL, NULL);

  options = g_object_new (GIMP_TYPE_FILL_OPTIONS,
                          "gimp", gimp,
                          NULL);

  if (use_context_color)
    {
      gimp_context_define_properties (GIMP_CONTEXT (options),
                                      GIMP_CONTEXT_PROP_MASK_FOREGROUND |
                                      GIMP_CONTEXT_PROP_MASK_PATTERN,
                                      FALSE);

      gimp_context_set_parent (GIMP_CONTEXT (options), context);
    }
240

241
  return options;
242
}
243 244 245 246 247 248 249 250 251

GimpFillStyle
gimp_fill_options_get_style (GimpFillOptions *options)
{
  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), GIMP_FILL_STYLE_SOLID);

  return GET_PRIVATE (options)->style;
}

252 253 254 255 256 257 258 259 260
void
gimp_fill_options_set_style (GimpFillOptions *options,
                             GimpFillStyle    style)
{
  g_return_if_fail (GIMP_IS_FILL_OPTIONS (options));

  g_object_set (options, "style", style, NULL);
}

261 262 263 264 265 266 267
gboolean
gimp_fill_options_get_antialias (GimpFillOptions *options)
{
  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), FALSE);

  return GET_PRIVATE (options)->antialias;
}
268

269 270 271 272 273 274 275 276 277
void
gimp_fill_options_set_antialias (GimpFillOptions *options,
                                 gboolean         antialias)
{
  g_return_if_fail (GIMP_IS_FILL_OPTIONS (options));

  g_object_set (options, "antialias", antialias, NULL);
}

278 279 280 281 282 283
gboolean
gimp_fill_options_set_by_fill_type (GimpFillOptions  *options,
                                    GimpContext      *context,
                                    GimpFillType      fill_type,
                                    GError          **error)
{
284 285 286
  GimpFillOptionsPrivate *private;
  GimpRGB                 color;
  const gchar            *undo_desc;
287 288 289 290 291

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), FALSE);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

292 293 294 295
  private = GET_PRIVATE (options);

  private->undo_desc = NULL;

296 297 298 299
  switch (fill_type)
    {
    case GIMP_FILL_FOREGROUND:
      gimp_context_get_foreground (context, &color);
300
      undo_desc = C_("undo-type", "Fill with Foreground Color");
301 302 303 304
      break;

    case GIMP_FILL_BACKGROUND:
      gimp_context_get_background (context, &color);
305
      undo_desc = C_("undo-type", "Fill with Background Color");
306 307 308 309
      break;

    case GIMP_FILL_WHITE:
      gimp_rgba_set (&color, 1.0, 1.0, 1.0, GIMP_OPACITY_OPAQUE);
310
      undo_desc = C_("undo-type", "Fill with White");
311 312 313 314
      break;

    case GIMP_FILL_TRANSPARENT:
      gimp_context_get_background (context, &color);
315 316
      gimp_context_set_paint_mode (GIMP_CONTEXT (options),
                                   GIMP_LAYER_MODE_ERASE);
317
      undo_desc = C_("undo-type", "Fill with Transparency");
318 319 320 321 322 323 324 325 326 327 328 329 330
      break;

    case GIMP_FILL_PATTERN:
      {
        GimpPattern *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;
          }

331
        gimp_fill_options_set_style (options, GIMP_FILL_STYLE_PATTERN);
332
        gimp_context_set_pattern (GIMP_CONTEXT (options), pattern);
333
        private->undo_desc = C_("undo-type", "Fill with Pattern");
334 335 336 337 338 339 340 341 342 343

        return TRUE;
      }
      break;

    default:
      g_warning ("%s: invalid fill_type %d", G_STRFUNC, fill_type);
      return FALSE;
    }

344
  gimp_fill_options_set_style (options, GIMP_FILL_STYLE_SOLID);
345
  gimp_context_set_foreground (GIMP_CONTEXT (options), &color);
346
  private->undo_desc = undo_desc;
347 348 349

  return TRUE;
}
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
gboolean
gimp_fill_options_set_by_fill_mode (GimpFillOptions     *options,
                                    GimpContext         *context,
                                    GimpBucketFillMode   fill_mode,
                                    GError             **error)
{
  GimpFillType fill_type;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), FALSE);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  switch (fill_mode)
    {
    default:
    case GIMP_BUCKET_FILL_FG:
      fill_type = GIMP_FILL_FOREGROUND;
      break;

    case GIMP_BUCKET_FILL_BG:
      fill_type = GIMP_FILL_BACKGROUND;
      break;

    case GIMP_BUCKET_FILL_PATTERN:
      fill_type = GIMP_FILL_PATTERN;
      break;
    }

  return gimp_fill_options_set_by_fill_type (options, context,
                                             fill_type, error);
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
const gchar *
gimp_fill_options_get_undo_desc (GimpFillOptions *options)
{
  GimpFillOptionsPrivate *private;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), NULL);

  private = GET_PRIVATE (options);

  if (private->undo_desc)
    return private->undo_desc;

  switch (private->style)
    {
    case GIMP_FILL_STYLE_SOLID:
      return C_("undo-type", "Fill with Solid Color");

    case GIMP_FILL_STYLE_PATTERN:
      return C_("undo-type", "Fill with Pattern");
    }

  g_return_val_if_reached (NULL);
}
406 407 408 409

GeglBuffer *
gimp_fill_options_create_buffer (GimpFillOptions     *options,
                                 GimpDrawable        *drawable,
410 411 412
                                 const GeglRectangle *rect,
                                 gint                 pattern_offset_x,
                                 gint                 pattern_offset_y)
413 414 415 416 417 418 419 420 421 422 423
{
  GeglBuffer  *buffer;

  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), NULL);
  g_return_val_if_fail (gimp_fill_options_get_style (options) !=
                        GIMP_FILL_STYLE_PATTERN ||
                        gimp_context_get_pattern (GIMP_CONTEXT (options)) != NULL,
                        NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (rect != NULL, NULL);

424 425 426
  buffer = gegl_buffer_new (rect,
                            gimp_drawable_get_format_with_alpha (drawable));

427 428 429
  switch (gimp_fill_options_get_style (options))
    {
    case GIMP_FILL_STYLE_SOLID:
430
      {
431
        GimpRGB color;
432 433 434 435

        gimp_context_get_foreground (GIMP_CONTEXT (options), &color);
        gimp_palettes_add_color_history (GIMP_CONTEXT (options)->gimp, &color);

436 437
        gimp_drawable_fill_buffer (drawable, buffer,
                                   &color, NULL, 0, 0);
438
      }
439 440 441
      break;

    case GIMP_FILL_STYLE_PATTERN:
442
      {
443
        GimpPattern *pattern;
444

445
        pattern = gimp_context_get_pattern (GIMP_CONTEXT (options));
446

447
        gimp_drawable_fill_buffer (drawable, buffer,
448 449 450
                                   NULL, pattern,
                                   pattern_offset_x,
                                   pattern_offset_y);
451 452
      }
      break;
453 454 455 456
    }

  return buffer;
}