layers-commands.c 33.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

21
#include <string.h>
22 23 24 25

#include <gtk/gtk.h>

#include "libgimpmath/gimpmath.h"
26
#include "libgimpbase/gimpbase.h"
27 28
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
29
#include "actions-types.h"
30

31 32
#include "config/gimpcoreconfig.h"

33
#include "core/gimp.h"
34
#include "core/gimpchannel-select.h"
35
#include "core/gimpcontext.h"
36
#include "core/gimpimage.h"
37
#include "core/gimpimage-merge.h"
38
#include "core/gimpimage-undo.h"
39
#include "core/gimpimage-undo-push.h"
40
#include "core/gimpitemundo.h"
41
#include "core/gimplayer.h"
42
#include "core/gimplayer-floating-sel.h"
43
#include "core/gimplayermask.h"
44
#include "core/gimptoolinfo.h"
45
#include "core/gimpundostack.h"
46
#include "core/gimpprogress.h"
47

48
#include "text/gimptext.h"
49
#include "text/gimptext-vectors.h"
50 51
#include "text/gimptextlayer.h"

52 53
#include "vectors/gimpvectors-warp.h"

54
#include "widgets/gimpaction.h"
55
#include "widgets/gimpdock.h"
56
#include "widgets/gimphelp-ids.h"
57
#include "widgets/gimpprogressdialog.h"
58

59
#include "display/gimpdisplay.h"
60
#include "display/gimpdisplayshell.h"
61

62 63 64
#include "tools/gimptexttool.h"
#include "tools/tool_manager.h"

65
#include "dialogs/layer-add-mask-dialog.h"
66
#include "dialogs/layer-options-dialog.h"
67
#include "dialogs/resize-dialog.h"
68
#include "dialogs/scale-dialog.h"
Michael Natterer's avatar
Michael Natterer committed
69

70
#include "actions.h"
71 72
#include "layers-commands.h"

73
#include "gimp-intl.h"
74 75


76
static const GimpLayerModeEffects layer_modes[] =
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
{
  GIMP_NORMAL_MODE,
  GIMP_DISSOLVE_MODE,
  GIMP_MULTIPLY_MODE,
  GIMP_DIVIDE_MODE,
  GIMP_SCREEN_MODE,
  GIMP_OVERLAY_MODE,
  GIMP_DODGE_MODE,
  GIMP_BURN_MODE,
  GIMP_HARDLIGHT_MODE,
  GIMP_SOFTLIGHT_MODE,
  GIMP_GRAIN_EXTRACT_MODE,
  GIMP_GRAIN_MERGE_MODE,
  GIMP_DIFFERENCE_MODE,
  GIMP_ADDITION_MODE,
  GIMP_SUBTRACT_MODE,
  GIMP_DARKEN_ONLY_MODE,
  GIMP_LIGHTEN_ONLY_MODE,
  GIMP_HUE_MODE,
  GIMP_SATURATION_MODE,
  GIMP_COLOR_MODE,
  GIMP_VALUE_MODE
};


102 103
/*  local function prototypes  */

104 105
static void   layers_new_layer_response    (GtkWidget             *widget,
                                            gint                   response_id,
106
                                            LayerOptionsDialog    *dialog);
107 108
static void   layers_edit_layer_response   (GtkWidget             *widget,
                                            gint                   response_id,
109
                                            LayerOptionsDialog    *dialog);
110 111
static void   layers_add_mask_response     (GtkWidget             *widget,
                                            gint                   response_id,
112
                                            LayerAddMaskDialog    *dialog);
113

114 115 116 117 118 119
static void   layers_scale_layer_callback  (GtkWidget             *dialog,
                                            GimpViewable          *viewable,
                                            gint                   width,
                                            gint                   height,
                                            GimpUnit               unit,
                                            GimpInterpolationType  interpolation,
120 121 122
                                            gdouble                xresolution,
                                            gdouble                yresolution,
                                            GimpUnit               resolution_unit,
123
                                            gpointer               data);
124 125 126 127
static void   layers_resize_layer_callback (GtkWidget             *dialog,
                                            GimpViewable          *viewable,
                                            gint                   width,
                                            gint                   height,
128
                                            GimpUnit               unit,
129 130
                                            gint                   offset_x,
                                            gint                   offset_y,
131
                                            GimpItemSet            unused,
132
                                            gpointer               data);
133

134
static gint   layers_mode_index            (GimpLayerModeEffects   layer_mode);
135 136


137 138
/*  private variables  */

139 140 141 142 143 144 145
static GimpFillType           layer_fill_type     = GIMP_TRANSPARENT_FILL;
static gchar                 *layer_name          = NULL;
static GimpUnit               layer_resize_unit   = GIMP_UNIT_PIXEL;
static GimpUnit               layer_scale_unit    = GIMP_UNIT_PIXEL;
static GimpInterpolationType  layer_scale_interp  = -1;
static GimpAddMaskType        layer_add_mask_type = GIMP_ADD_WHITE_MASK;
static gboolean               layer_mask_invert   = FALSE;
146 147


148 149
/*  public functions  */

150 151 152 153
void
layers_text_tool_cmd_callback (GtkAction *action,
                               gpointer   data)
{
154
  GimpImage *image;
155 156
  GimpLayer *layer;
  GtkWidget *widget;
157
  GimpTool  *active_tool;
158
  return_if_no_layer (image, layer, data);
159 160
  return_if_no_widget (widget, data);

161 162
  if (! gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
163
      layers_edit_attributes_cmd_callback (action, data);
164 165 166
      return;
    }

167
  active_tool = tool_manager_get_active (image->gimp);
168 169 170

  if (! GIMP_IS_TEXT_TOOL (active_tool))
    {
171 172
      GimpToolInfo *tool_info = gimp_get_tool_info (image->gimp,
                                                    "gimp-text-tool");
173 174 175 176

      if (GIMP_IS_TOOL_INFO (tool_info))
        {
          gimp_context_set_tool (action_data_get_context (data), tool_info);
177
          active_tool = tool_manager_get_active (image->gimp);
178 179 180 181 182
        }
    }

  if (GIMP_IS_TEXT_TOOL (active_tool))
    gimp_text_tool_set_layer (GIMP_TEXT_TOOL (active_tool), layer);
183 184 185 186
}

void
layers_edit_attributes_cmd_callback (GtkAction *action,
187
                                     gpointer   data)
188
{
189
  LayerOptionsDialog *dialog;
190
  GimpImage          *image;
191 192
  GimpLayer          *layer;
  GtkWidget          *widget;
193
  return_if_no_layer (image, layer, data);
194 195
  return_if_no_widget (widget, data);

196
  dialog = layer_options_dialog_new (gimp_item_get_image (GIMP_ITEM (layer)),
197
                                     layer,
198
                                     action_data_get_context (data),
199
                                     widget,
200
                                     gimp_object_get_name (GIMP_OBJECT (layer)),
201
                                     layer_fill_type,
202 203
                                     _("Layer Attributes"),
                                     "gimp-layer-edit",
204
                                     GTK_STOCK_EDIT,
205 206 207 208
                                     _("Edit Layer Attributes"),
                                     GIMP_HELP_LAYER_EDIT);

  g_signal_connect (dialog->dialog, "response",
209
                    G_CALLBACK (layers_edit_layer_response),
210
                    dialog);
211

212
  gtk_widget_show (dialog->dialog);
213 214 215 216
}

void
layers_new_cmd_callback (GtkAction *action,
217
                         gpointer   data)
218
{
219
  LayerOptionsDialog *dialog;
220
  GimpImage          *image;
221 222
  GtkWidget          *widget;
  GimpLayer          *floating_sel;
223
  return_if_no_image (image, data);
224 225
  return_if_no_widget (widget, data);

226 227 228
  /*  If there is a floating selection, the new command transforms
   *  the current fs into a new layer
   */
229
  if ((floating_sel = gimp_image_floating_sel (image)))
230
    {
231 232 233 234 235 236 237 238 239 240
      GError *error = NULL;

      if (! floating_sel_to_layer (floating_sel, &error))
        {
          gimp_message (image->gimp, G_OBJECT (widget), GIMP_MESSAGE_WARNING,
                        error->message);
          g_clear_error (&error);
          return;
        }

241
      gimp_image_flush (image);
242 243 244
      return;
    }

245 246 247
  dialog = layer_options_dialog_new (image, NULL,
                                     action_data_get_context (data),
                                     widget,
248
                                     layer_name ? layer_name : _("New Layer"),
249
                                     layer_fill_type,
250 251 252 253 254 255 256
                                     _("New Layer"),
                                     "gimp-layer-new",
                                     GIMP_STOCK_LAYER,
                                     _("Create a New Layer"),
                                     GIMP_HELP_LAYER_NEW);

  g_signal_connect (dialog->dialog, "response",
257
                    G_CALLBACK (layers_new_layer_response),
258
                    dialog);
259

260
  gtk_widget_show (dialog->dialog);
261 262 263
}

void
264 265
layers_new_last_vals_cmd_callback (GtkAction *action,
                                   gpointer   data)
266
{
267
  GimpImage            *image;
268
  GtkWidget            *widget;
269 270 271 272 273 274
  GimpLayer            *floating_sel;
  GimpLayer            *new_layer;
  gint                  width, height;
  gint                  off_x, off_y;
  gdouble               opacity;
  GimpLayerModeEffects  mode;
275
  return_if_no_image (image, data);
276
  return_if_no_widget (widget, data);
277 278 279 280

  /*  If there is a floating selection, the new command transforms
   *  the current fs into a new layer
   */
281
  if ((floating_sel = gimp_image_floating_sel (image)))
282
    {
283 284 285 286 287 288 289 290 291 292
      GError *error = NULL;

      if (! floating_sel_to_layer (floating_sel, &error))
        {
          gimp_message (image->gimp, G_OBJECT (widget), GIMP_MESSAGE_WARNING,
                        error->message);
          g_clear_error (&error);
          return;
        }

293
      gimp_image_flush (image);
294 295 296 297 298 299 300 301 302 303
      return;
    }

  if (GIMP_IS_LAYER (GIMP_ACTION (action)->viewable))
    {
      GimpLayer *template = GIMP_LAYER (GIMP_ACTION (action)->viewable);

      gimp_item_offsets (GIMP_ITEM (template), &off_x, &off_y);
      width   = gimp_item_width  (GIMP_ITEM (template));
      height  = gimp_item_height (GIMP_ITEM (template));
304 305
      opacity = gimp_layer_get_opacity (template);
      mode    = gimp_layer_get_mode (template);
306 307 308
    }
  else
    {
309 310
      width   = gimp_image_get_width (image);
      height  = gimp_image_get_height (image);
311 312 313 314 315 316
      off_x   = 0;
      off_y   = 0;
      opacity = 1.0;
      mode    = GIMP_NORMAL_MODE;
    }

317
  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_EDIT_PASTE,
318 319
                               _("New Layer"));

320 321
  new_layer = gimp_layer_new (image, width, height,
                              gimp_image_base_type_with_alpha (image),
322 323
                              layer_name ? layer_name : _("New Layer"),
                              opacity, mode);
324 325 326

  gimp_drawable_fill_by_type (GIMP_DRAWABLE (new_layer),
                              action_data_get_context (data),
327
                              layer_fill_type);
328 329
  gimp_item_translate (GIMP_ITEM (new_layer), off_x, off_y, FALSE);

330
  gimp_image_add_layer (image, new_layer, -1);
331

332
  gimp_image_undo_group_end (image);
333

334
  gimp_image_flush (image);
335 336
}

337
void
338 339 340
layers_select_cmd_callback (GtkAction *action,
                            gint       value,
                            gpointer   data)
341
{
342
  GimpImage *image;
343
  GimpLayer *layer;
344
  GimpLayer *new_layer;
345
  return_if_no_image (image, data);
346

347
  layer = gimp_image_get_active_layer (image);
348

349
  new_layer = (GimpLayer *) action_select_object ((GimpActionSelectType) value,
350
                                                  image->layers,
351
                                                  (GimpObject *) layer);
352

353
  if (new_layer && new_layer != layer)
354
    {
355 356
      gimp_image_set_active_layer (image, new_layer);
      gimp_image_flush (image);
357 358 359
    }
}

360
void
361
layers_raise_cmd_callback (GtkAction *action,
362
                           gpointer   data)
363
{
364
  GimpImage *image;
365
  GimpLayer *layer;
366
  return_if_no_layer (image, layer, data);
367

368 369
  gimp_image_raise_layer (image, layer);
  gimp_image_flush (image);
370 371 372
}

void
373
layers_raise_to_top_cmd_callback (GtkAction *action,
374
                                  gpointer   data)
375
{
376
  GimpImage *image;
377
  GimpLayer *layer;
378
  return_if_no_layer (image, layer, data);
379

380 381
  gimp_image_raise_layer_to_top (image, layer);
  gimp_image_flush (image);
382 383 384
}

void
385
layers_lower_cmd_callback (GtkAction *action,
386
                           gpointer   data)
387
{
388
  GimpImage *image;
389
  GimpLayer *layer;
390
  return_if_no_layer (image, layer, data);
391

392 393
  gimp_image_lower_layer (image, layer);
  gimp_image_flush (image);
394 395 396
}

void
397
layers_lower_to_bottom_cmd_callback (GtkAction *action,
398
                                     gpointer   data)
399
{
400
  GimpImage *image;
401
  GimpLayer *layer;
402
  return_if_no_layer (image, layer, data);
403

404 405
  gimp_image_lower_layer_to_bottom (image, layer);
  gimp_image_flush (image);
406 407 408
}

void
409
layers_duplicate_cmd_callback (GtkAction *action,
410
                               gpointer   data)
411
{
412
  GimpImage *image;
413
  GimpLayer *layer;
414
  GimpLayer *new_layer;
415
  return_if_no_layer (image, layer, data);
416

417 418
  new_layer = GIMP_LAYER (gimp_item_duplicate (GIMP_ITEM (layer),
                                               G_TYPE_FROM_INSTANCE (layer)));
419
  gimp_image_add_layer (image, new_layer, -1);
420

421
  gimp_image_flush (image);
422 423 424
}

void
425
layers_anchor_cmd_callback (GtkAction *action,
426
                            gpointer   data)
427
{
428
  GimpImage *image;
429
  GimpLayer *layer;
430
  return_if_no_layer (image, layer, data);
431

432
  if (gimp_layer_is_floating_sel (layer))
433
    {
434
      floating_sel_anchor (layer);
435
      gimp_image_flush (image);
436
    }
437
}
438

439
void
440
layers_merge_down_cmd_callback (GtkAction *action,
441
                                gpointer   data)
442
{
443
  GimpImage *image;
444
  GimpLayer *layer;
445
  return_if_no_layer (image, layer, data);
446

447
  gimp_image_merge_down (image, layer, action_data_get_context (data),
448
                         GIMP_EXPAND_AS_NECESSARY);
449
  gimp_image_flush (image);
450 451
}

452
void
453
layers_delete_cmd_callback (GtkAction *action,
454
                            gpointer   data)
455
{
456
  GimpImage *image;
457
  GimpLayer *layer;
458
  return_if_no_layer (image, layer, data);
459

460 461
  if (gimp_layer_is_floating_sel (layer))
    floating_sel_remove (layer);
462
  else
463
    gimp_image_remove_layer (image, layer);
464

465
  gimp_image_flush (image);
466 467
}

468
void
469
layers_text_discard_cmd_callback (GtkAction *action,
470 471
                                  gpointer   data)
{
472
  GimpImage *image;
473
  GimpLayer *layer;
474
  return_if_no_layer (image, layer, data);
475

476 477
  if (GIMP_IS_TEXT_LAYER (layer))
    gimp_text_layer_discard (GIMP_TEXT_LAYER (layer));
478 479
}

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
void
layers_text_to_vectors_cmd_callback (GtkAction *action,
                                     gpointer   data)
{
  GimpImage *image;
  GimpLayer *layer;
  return_if_no_layer (image, layer, data);

  if (GIMP_IS_TEXT_LAYER (layer))
    {
      GimpVectors *vectors;
      gint         x, y;

      vectors = gimp_text_vectors_new (image, GIMP_TEXT_LAYER (layer)->text);

      gimp_item_offsets (GIMP_ITEM (layer), &x, &y);
      gimp_item_translate (GIMP_ITEM (vectors), x, y, FALSE);

      gimp_image_add_vectors (image, vectors, -1);
      gimp_image_set_active_vectors (image, vectors);

      gimp_image_flush (image);
    }
}

void
layers_text_along_vectors_cmd_callback (GtkAction *action,
                                        gpointer   data)
{
  GimpImage   *image;
  GimpLayer   *layer;
  GimpVectors *vectors;
  return_if_no_layer (image, layer, data);
  return_if_no_vectors (image, vectors, data);

  if (GIMP_IS_TEXT_LAYER (layer))
    {
      GimpVectors *new_vectors;

      new_vectors = gimp_text_vectors_new (image, GIMP_TEXT_LAYER (layer)->text);

      gimp_vectors_warp_vectors (vectors, new_vectors,
                                 0.5 * gimp_item_height (GIMP_ITEM (layer)));

      gimp_item_set_visible (GIMP_ITEM (new_vectors), TRUE, FALSE);

      gimp_image_add_vectors (image, new_vectors, -1);
      gimp_image_set_active_vectors (image, new_vectors);

      gimp_image_flush (image);
    }
}

533
void
534
layers_resize_cmd_callback (GtkAction *action,
535
                            gpointer   data)
536
{
537 538 539 540
  GimpImage *image;
  GimpLayer *layer;
  GtkWidget *widget;
  GtkWidget *dialog;
541
  return_if_no_layer (image, layer, data);
542
  return_if_no_widget (widget, data);
543

544 545
  if (layer_resize_unit != GIMP_UNIT_PERCENT && GIMP_IS_DISPLAY (data))
    layer_resize_unit = GIMP_DISPLAY_SHELL (GIMP_DISPLAY (data)->shell)->unit;
546

547
  dialog = resize_dialog_new (GIMP_VIEWABLE (layer),
548
                              action_data_get_context (data),
549 550 551
                              _("Set Layer Boundary Size"), "gimp-layer-resize",
                              widget,
                              gimp_standard_help_func, GIMP_HELP_LAYER_RESIZE,
552
                              layer_resize_unit,
553 554 555 556
                              layers_resize_layer_callback,
                              action_data_get_context (data));

  gtk_widget_show (dialog);
557 558 559
}

void
560
layers_resize_to_image_cmd_callback (GtkAction *action,
561
                                     gpointer   data)
562
{
563
  GimpImage *image;
564
  GimpLayer *layer;
565
  return_if_no_layer (image, layer, data);
566

567
  gimp_layer_resize_to_image (layer, action_data_get_context (data));
568
  gimp_image_flush (image);
569
}
570

571
void
572
layers_scale_cmd_callback (GtkAction *action,
573
                           gpointer   data)
574
{
575 576 577 578
  GimpImage *image;
  GimpLayer *layer;
  GtkWidget *widget;
  GtkWidget *dialog;
579
  return_if_no_layer (image, layer, data);
580
  return_if_no_widget (widget, data);
581

582 583
  if (layer_scale_unit != GIMP_UNIT_PERCENT && GIMP_IS_DISPLAY (data))
    layer_scale_unit = GIMP_DISPLAY_SHELL (GIMP_DISPLAY (data)->shell)->unit;
584

585 586
  if (layer_scale_interp == -1)
    layer_scale_interp = image->gimp->config->interpolation_type;
587 588

  dialog = scale_dialog_new (GIMP_VIEWABLE (layer),
589
                             action_data_get_context (data),
590 591 592
                             _("Scale Layer"), "gimp-layer-scale",
                             widget,
                             gimp_standard_help_func, GIMP_HELP_LAYER_SCALE,
593 594
                             layer_scale_unit,
                             layer_scale_interp,
595
                             layers_scale_layer_callback,
596
                             GIMP_IS_DISPLAY (data) ? data : NULL);
597 598

  gtk_widget_show (dialog);
599 600 601
}

void
602
layers_crop_cmd_callback (GtkAction *action,
603
                          gpointer   data)
604
{
605
  GimpImage *image;
606
  GimpLayer *layer;
607
  GtkWidget *widget;
608 609
  gint       x1, y1, x2, y2;
  gint       off_x, off_y;
610
  return_if_no_layer (image, layer, data);
611
  return_if_no_widget (widget, data);
612

613
  if (! gimp_channel_bounds (gimp_image_get_mask (image),
614
                             &x1, &y1, &x2, &y2))
615
    {
616 617
      gimp_message (image->gimp, G_OBJECT (widget), GIMP_MESSAGE_WARNING,
                    _("Cannot crop because the current selection is empty."));
618 619 620
      return;
    }

621
  gimp_item_offsets (GIMP_ITEM (layer), &off_x, &off_y);
622 623 624 625

  off_x -= x1;
  off_y -= y1;

626
  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
627
                               _("Crop Layer"));
628

629
  gimp_item_resize (GIMP_ITEM (layer), action_data_get_context (data),
630
                    x2 - x1, y2 - y1, off_x, off_y);
631

632
  gimp_image_undo_group_end (image);
633

634
  gimp_image_flush (image);
635 636 637
}

void
638
layers_mask_add_cmd_callback (GtkAction *action,
639
                              gpointer   data)
640
{
641
  LayerAddMaskDialog *dialog;
642
  GimpImage          *image;
643 644
  GimpLayer          *layer;
  GtkWidget          *widget;
645
  return_if_no_layer (image, layer, data);
646
  return_if_no_widget (widget, data);
647

648 649
  dialog = layer_add_mask_dialog_new (layer, action_data_get_context (data),
                                      widget,
650
                                      layer_add_mask_type, layer_mask_invert);
651

652
  g_signal_connect (dialog->dialog, "response",
653
                    G_CALLBACK (layers_add_mask_response),
654
                    dialog);
655

656
  gtk_widget_show (dialog->dialog);
657 658 659
}

void
660
layers_mask_apply_cmd_callback (GtkAction *action,
661
                                gint       value,
662
                                gpointer   data)
663
{
664
  GimpImage         *image;
665 666
  GimpLayer         *layer;
  GimpMaskApplyMode  mode;
667
  return_if_no_layer (image, layer, data);
668

669
  mode = (GimpMaskApplyMode) value;
670

671
  if (gimp_layer_get_mask (layer))
672
    {
673
      gimp_layer_apply_mask (layer, mode, TRUE);
674
      gimp_image_flush (image);
675 676 677
    }
}

678 679 680 681
void
layers_mask_edit_cmd_callback (GtkAction *action,
                               gpointer   data)
{
682
  GimpImage     *image;
Sven Neumann's avatar
Sven Neumann committed
683 684
  GimpLayer     *layer;
  GimpLayerMask *mask;
685
  return_if_no_layer (image, layer, data);
686 687

  mask = gimp_layer_get_mask (layer);
688

689 690 691 692 693 694
  if (mask)
    {
      gboolean active;

      active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));

695
      gimp_layer_mask_set_edit (mask, active);
696
      gimp_image_flush (image);
697 698 699 700 701 702 703
    }
}

void
layers_mask_show_cmd_callback (GtkAction *action,
                               gpointer   data)
{
704
  GimpImage     *image;
Sven Neumann's avatar
Sven Neumann committed
705 706
  GimpLayer     *layer;
  GimpLayerMask *mask;
707
  return_if_no_layer (image, layer, data);
708 709 710 711 712 713 714 715 716

  mask = gimp_layer_get_mask (layer);

  if (mask)
    {
      gboolean active;

      active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));

717
      gimp_layer_mask_set_show (mask, active, TRUE);
718
      gimp_image_flush (image);
719 720 721 722 723 724 725
    }
}

void
layers_mask_disable_cmd_callback (GtkAction *action,
                                  gpointer   data)
{
726
  GimpImage     *image;
Sven Neumann's avatar
Sven Neumann committed
727 728
  GimpLayer     *layer;
  GimpLayerMask *mask;
729
  return_if_no_layer (image, layer, data);
730 731 732 733 734 735 736 737 738

  mask = gimp_layer_get_mask (layer);

  if (mask)
    {
      gboolean active;

      active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));

739
      gimp_layer_mask_set_apply (mask, ! active, TRUE);
740
      gimp_image_flush (image);
741 742 743
    }
}

744
void
745 746 747
layers_mask_to_selection_cmd_callback (GtkAction *action,
                                       gint       value,
                                       gpointer   data)
748 749
{
  GimpChannelOps  op;
750
  GimpImage      *image;
751
  GimpLayer      *layer;
752
  GimpLayerMask  *mask;
753
  return_if_no_layer (image, layer, data);
754

755
  op = (GimpChannelOps) value;
756

757
  mask = gimp_layer_get_mask (layer);
758 759

  if (mask)
760
    {
761 762 763 764
      gint off_x, off_y;

      gimp_item_offsets (GIMP_ITEM (mask), &off_x, &off_y);

765
      gimp_channel_select_channel (gimp_image_get_mask (image),
766 767 768 769
                                   _("Layer Mask to Selection"),
                                   GIMP_CHANNEL (mask),
                                   off_x, off_y,
                                   op, FALSE, 0.0, 0.0);
770
      gimp_image_flush (image);
771
    }
772 773
}

774
void
775
layers_alpha_add_cmd_callback (GtkAction *action,
776
                               gpointer   data)
777
{
778
  GimpImage *image;
779
  GimpLayer *layer;
780
  return_if_no_layer (image, layer, data);
781

782
  if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
783
    {
784
      gimp_layer_add_alpha (layer);
785
      gimp_image_flush (image);
786
    }
787 788
}

Michael Natterer's avatar
Michael Natterer committed
789 790 791 792
void
layers_alpha_remove_cmd_callback (GtkAction *action,
                                  gpointer   data)
{
793
  GimpImage *image;
Michael Natterer's avatar
Michael Natterer committed
794
  GimpLayer *layer;
795
  return_if_no_layer (image, layer, data);
Michael Natterer's avatar
Michael Natterer committed
796 797 798 799

  if (gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
    {
      gimp_layer_flatten (layer, action_data_get_context (data));
800
      gimp_image_flush (image);
Michael Natterer's avatar
Michael Natterer committed
801 802 803
    }
}

804
void
805 806 807
layers_alpha_to_selection_cmd_callback (GtkAction *action,
                                        gint       value,
                                        gpointer   data)
808
{
809
  GimpChannelOps  op;
810
  GimpImage      *image;
811
  GimpLayer      *layer;
812
  return_if_no_layer (image, layer, data);
813

814
  op = (GimpChannelOps) value;
815

816
  gimp_channel_select_alpha (gimp_image_get_mask (image),
817
                             GIMP_DRAWABLE (layer),
818
                             op, FALSE, 0.0, 0.0);
819
  gimp_image_flush (image);
820 821
}

822 823 824 825 826
void
layers_opacity_cmd_callback (GtkAction *action,
                             gint       value,
                             gpointer   data)
{
827
  GimpImage      *image;
828 829
  GimpLayer      *layer;
  gdouble         opacity;
830
  GimpUndo       *undo;
831
  gboolean        push_undo = TRUE;
832
  return_if_no_layer (image, layer, data);
833

834
  undo = gimp_image_undo_can_compress (image, GIMP_TYPE_ITEM_UNDO,
835 836 837 838
                                       GIMP_UNDO_LAYER_OPACITY);

  if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
    push_undo = FALSE;
839 840

  opacity = action_select_value ((GimpActionSelectType) value,
841
                                 gimp_layer_get_opacity (layer),
842
                                 0.0, 1.0,
843
                                 1.0 / 255.0, 0.01, 0.1, 0.0, FALSE);
844
  gimp_layer_set_opacity (layer, opacity, push_undo);
845
  gimp_image_flush (image);
846 847
}

848
void
849 850 851
layers_mode_cmd_callback (GtkAction *action,
                          gint       value,
                          gpointer   data)
852
{
853
  GimpImage            *image;
854
  GimpLayer            *layer;
855
  GimpLayerModeEffects  layer_mode;
856 857 858
  gint                  index;
  GimpUndo             *undo;
  gboolean              push_undo = TRUE;
859
  return_if_no_layer (image, layer, data);
860

861
  undo = gimp_image_undo_can_compress (image, GIMP_TYPE_ITEM_UNDO,
862
                                       GIMP_UNDO_LAYER_MODE);
863 864 865 866

  if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
    push_undo = FALSE;

867
  layer_mode = gimp_layer_get_mode (layer);
868 869

  index = action_select_value ((GimpActionSelectType) value,
870 871
                               layers_mode_index (layer_mode),
                               0, G_N_ELEMENTS (layer_modes) - 1,
872
                               0.0, 1.0, 1.0, 0.0, FALSE);
873
  gimp_layer_set_mode (layer, layer_modes[index], push_undo);
874
  gimp_image_flush (image);
875 876 877
}

void
878 879
layers_lock_alpha_cmd_callback (GtkAction *action,
                                gpointer   data)
880
{
881
  GimpImage *image;
882
  GimpLayer *layer;
883
  gboolean   lock_alpha;
884
  return_if_no_layer (image, layer, data);
885

886
  lock_alpha = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
887

888
  if (lock_alpha != gimp_layer_get_lock_alpha (layer))
889 890 891 892
    {
      GimpUndo *undo;
      gboolean  push_undo = TRUE;

893
      undo = gimp_image_undo_can_compress (image, GIMP_TYPE_ITEM_UNDO,
894
                                           GIMP_UNDO_LAYER_LOCK_ALPHA);
895 896 897 898

      if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
        push_undo = FALSE;

899
      gimp_layer_set_lock_alpha (layer, lock_alpha, push_undo);
900
      gimp_image_flush (image);
901 902 903
    }
}

904

905
/*  private functions  */
906 907

static void
908 909
layers_new_layer_response (GtkWidget          *widget,
                           gint                response_id,
910
                           LayerOptionsDialog *dialog)
911
{
912
  if (response_id == GTK_RESPONSE_OK)
913
    {
914
      GimpLayer *layer;
915

916 917 918
      if (layer_name)
        g_free (layer_name);
      layer_name =
919 920
        g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->name_entry)));

921
      layer_fill_type = dialog->fill_type;
922

923 924
      dialog->xsize =
        RINT (gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (dialog->size_se),
925
                                          0));
926 927
      dialog->ysize =
        RINT (gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (dialog->size_se),
928 929
                                          1));

930
      layer = gimp_layer_new (dialog->image,
931 932
                              dialog->xsize,
                              dialog->ysize,
933
                              gimp_image_base_type_with_alpha (dialog->image),
934 935 936 937
                              layer_name,
                              GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);

      if (layer)
938
        {
939
          gimp_drawable_fill_by_type (GIMP_DRAWABLE (layer),
940
                                      dialog->context,
941
                                      layer_fill_type);
942
          gimp_image_add_layer (dialog->image, layer, -1);
943

944
          gimp_image_flush (dialog->image);
945
        }
946
      else
947
        {
948
          g_warning ("%s: could not allocate new layer", G_STRFUNC);
949
        }
950 951
    }

952
  gtk_widget_destroy (dialog->dialog);
953 954
}

955
static void
956 957
layers_edit_layer_response (GtkWidget          *widget,
                            gint                response_id,
958
                            LayerOptionsDialog *dialog)
959 960 961
{
  if (response_id == GTK_RESPONSE_OK)
    {
962
      GimpLayer   *layer = dialog->layer;
963
      const gchar *new_name;
964

965
      new_name = gtk_entry_get_text (GTK_ENTRY (dialog->name_entry));
966

967 968
      if (strcmp (new_name, gimp_object_get_name (GIMP_OBJECT (layer))))
        {
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
          GError *error = NULL;

          if (gimp_item_rename (GIMP_ITEM (layer), new_name, &error))
            {
              gimp_image_flush (dialog->image);
            }
          else
            {
              gimp_message (dialog->image->gimp, G_OBJECT (widget),
                            GIMP_MESSAGE_WARNING,
                            "%s", error->message);
              g_clear_error (&error);

              return;
            }
984
        }
985

986
      if (dialog->rename_toggle &&
987 988 989 990
          gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
        {
          g_object_set (layer,
                        "auto-rename",
991
                        GTK_TOGGLE_BUTTON (dialog->rename_toggle)->active,
992 993 994 995
                        NULL);
        }
    }

996
  gtk_widget_destroy (dialog->dialog);
997
}
998 999

static void
1000 1001 1002
layers_add_mask_response (GtkWidget          *widget,
                          gint                response_id,
                          LayerAddMaskDialog *dialog)
1003
{
1004
  if (response_id == GTK_RESPONSE_OK)
1005
    {
1006
      GimpLayer     *layer = dialog->layer;
1007
      GimpImage     *image = gimp_item_get_image (GIMP_ITEM (layer));
1008
      GimpLayerMask *mask;
1009

1010 1011 1012
      if (dialog->add_mask_type == GIMP_ADD_CHANNEL_MASK &&
          ! dialog->channel)
        {
1013 1014
          gimp_message (image->gimp, G_OBJECT (widget), GIMP_MESSAGE_WARNING,
                        _("Please select a channel first"));
1015 1016 1017
          return;
        }

1018 1019 1020
      layer_add_mask_type = dialog->add_mask_type;
      layer_mask_invert   = dialog->invert;

1021
      gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_LAYER_ADD_MASK,
1022
                                   _("Add Layer Mask"));
1023

1024 1025
      mask = gimp_layer_create_mask (layer, layer_add_mask_type,
                                     dialog->channel);
1026

1027
      if (layer_mask_invert)
1028
        gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);
1029

1030
      gimp_layer_add_mask (layer, mask, TRUE);
1031

1032
      gimp_image_undo_group_end (image);
1033

1034
      gimp_image_flush (image);
1035 1036
    }

1037
  gtk_widget_destroy (dialog->dialog);
1038 1039 1040
}

static void
1041 1042 1043 1044 1045 1046
layers_scale_layer_callback (GtkWidget             *dialog,
                             GimpViewable          *viewable,
                             gint                   width,
                             gint                   height,
                             GimpUnit               unit,
                             GimpInterpolationType  interpolation,
1047 1048 1049
                             gdouble                xresolution,    /* unused */
                             gdouble                yresolution,    /* unused */
                             GimpUnit               resolution_unit,/* unused */
1050
                             gpointer               data)
1051
{
1052
  GimpDisplay *display = GIMP_DISPLAY (data);
1053

1054 1055 1056