1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <string.h>
21 
22 #include <gegl.h>
23 #include <gtk/gtk.h>
24 
25 #include "libgimpmath/gimpmath.h"
26 #include "libgimpbase/gimpbase.h"
27 #include "libgimpcolor/gimpcolor.h"
28 #include "libgimpwidgets/gimpwidgets.h"
29 
30 #include "actions-types.h"
31 
32 #include "config/gimpdialogconfig.h"
33 
34 #include "operations/layer-modes/gimp-layer-modes.h"
35 
36 #include "core/gimp.h"
37 #include "core/gimpchannel.h"
38 #include "core/gimpcontainer.h"
39 #include "core/gimpcontext.h"
40 #include "core/gimpdrawable-fill.h"
41 #include "core/gimpgrouplayer.h"
42 #include "core/gimpimage.h"
43 #include "core/gimpimage-merge.h"
44 #include "core/gimpimage-undo.h"
45 #include "core/gimpimage-undo-push.h"
46 #include "core/gimpitemundo.h"
47 #include "core/gimplayerpropundo.h"
48 #include "core/gimplayer-floating-selection.h"
49 #include "core/gimplayer-new.h"
50 #include "core/gimppickable.h"
51 #include "core/gimppickable-auto-shrink.h"
52 #include "core/gimptoolinfo.h"
53 #include "core/gimpundostack.h"
54 #include "core/gimpprogress.h"
55 
56 #include "text/gimptext.h"
57 #include "text/gimptext-vectors.h"
58 #include "text/gimptextlayer.h"
59 
60 #include "vectors/gimpstroke.h"
61 #include "vectors/gimpvectors.h"
62 #include "vectors/gimpvectors-warp.h"
63 
64 #include "widgets/gimpaction.h"
65 #include "widgets/gimpdock.h"
66 #include "widgets/gimphelp-ids.h"
67 #include "widgets/gimpprogressdialog.h"
68 
69 #include "display/gimpdisplay.h"
70 #include "display/gimpdisplayshell.h"
71 #include "display/gimpimagewindow.h"
72 
73 #include "tools/gimptexttool.h"
74 #include "tools/tool_manager.h"
75 
76 #include "dialogs/dialogs.h"
77 #include "dialogs/layer-add-mask-dialog.h"
78 #include "dialogs/layer-options-dialog.h"
79 #include "dialogs/resize-dialog.h"
80 #include "dialogs/scale-dialog.h"
81 
82 #include "actions.h"
83 #include "items-commands.h"
84 #include "layers-commands.h"
85 
86 #include "gimp-intl.h"
87 
88 
89 /*  local function prototypes  */
90 
91 static void   layers_new_callback             (GtkWidget             *dialog,
92                                                GimpImage             *image,
93                                                GimpLayer             *layer,
94                                                GimpContext           *context,
95                                                const gchar           *layer_name,
96                                                GimpLayerMode          layer_mode,
97                                                GimpLayerColorSpace    layer_blend_space,
98                                                GimpLayerColorSpace    layer_composite_space,
99                                                GimpLayerCompositeMode layer_composite_mode,
100                                                gdouble                layer_opacity,
101                                                GimpFillType           layer_fill_type,
102                                                gint                   layer_width,
103                                                gint                   layer_height,
104                                                gint                   layer_offset_x,
105                                                gint                   layer_offset_y,
106                                                gboolean               layer_visible,
107                                                gboolean               layer_linked,
108                                                GimpColorTag           layer_color_tag,
109                                                gboolean               layer_lock_pixels,
110                                                gboolean               layer_lock_position,
111                                                gboolean               layer_lock_alpha,
112                                                gboolean               rename_text_layer,
113                                                gpointer               user_data);
114 static void   layers_edit_attributes_callback (GtkWidget             *dialog,
115                                                GimpImage             *image,
116                                                GimpLayer             *layer,
117                                                GimpContext           *context,
118                                                const gchar           *layer_name,
119                                                GimpLayerMode          layer_mode,
120                                                GimpLayerColorSpace    layer_blend_space,
121                                                GimpLayerColorSpace    layer_composite_space,
122                                                GimpLayerCompositeMode layer_composite_mode,
123                                                gdouble                layer_opacity,
124                                                GimpFillType           layer_fill_type,
125                                                gint                   layer_width,
126                                                gint                   layer_height,
127                                                gint                   layer_offset_x,
128                                                gint                   layer_offset_y,
129                                                gboolean               layer_visible,
130                                                gboolean               layer_linked,
131                                                GimpColorTag           layer_color_tag,
132                                                gboolean               layer_lock_pixels,
133                                                gboolean               layer_lock_position,
134                                                gboolean               layer_lock_alpha,
135                                                gboolean               rename_text_layer,
136                                                gpointer               user_data);
137 static void   layers_add_mask_callback        (GtkWidget             *dialog,
138                                                GimpLayer             *layer,
139                                                GimpAddMaskType        add_mask_type,
140                                                GimpChannel           *channel,
141                                                gboolean               invert,
142                                                gpointer               user_data);
143 static void   layers_scale_callback           (GtkWidget             *dialog,
144                                                GimpViewable          *viewable,
145                                                gint                   width,
146                                                gint                   height,
147                                                GimpUnit               unit,
148                                                GimpInterpolationType  interpolation,
149                                                gdouble                xresolution,
150                                                gdouble                yresolution,
151                                                GimpUnit               resolution_unit,
152                                                gpointer               user_data);
153 static void   layers_resize_callback          (GtkWidget             *dialog,
154                                                GimpViewable          *viewable,
155                                                GimpContext           *context,
156                                                gint                   width,
157                                                gint                   height,
158                                                GimpUnit               unit,
159                                                gint                   offset_x,
160                                                gint                   offset_y,
161                                                GimpFillType           fill_type,
162                                                GimpItemSet            unused,
163                                                gboolean               unused2,
164                                                gpointer               data);
165 
166 static gint   layers_mode_index               (GimpLayerMode          layer_mode,
167                                                const GimpLayerMode   *modes,
168                                                gint                   n_modes);
169 
170 
171 /*  private variables  */
172 
173 static GimpUnit               layer_resize_unit   = GIMP_UNIT_PIXEL;
174 static GimpUnit               layer_scale_unit    = GIMP_UNIT_PIXEL;
175 static GimpInterpolationType  layer_scale_interp  = -1;
176 
177 
178 /*  public functions  */
179 
180 void
layers_edit_cmd_callback(GimpAction * action,GVariant * value,gpointer data)181 layers_edit_cmd_callback (GimpAction *action,
182                           GVariant   *value,
183                           gpointer    data)
184 {
185   GimpImage *image;
186   GimpLayer *layer;
187   GtkWidget *widget;
188   return_if_no_layer (image, layer, data);
189   return_if_no_widget (widget, data);
190 
191   if (gimp_item_is_text_layer (GIMP_ITEM (layer)))
192     {
193       layers_edit_text_cmd_callback (action, value, data);
194     }
195   else
196     {
197       layers_edit_attributes_cmd_callback (action, value, data);
198     }
199 }
200 
201 void
layers_edit_text_cmd_callback(GimpAction * action,GVariant * value,gpointer data)202 layers_edit_text_cmd_callback (GimpAction *action,
203                                GVariant   *value,
204                                gpointer    data)
205 {
206   GimpImage *image;
207   GimpLayer *layer;
208   GtkWidget *widget;
209   GimpTool  *active_tool;
210   return_if_no_layer (image, layer, data);
211   return_if_no_widget (widget, data);
212 
213   g_return_if_fail (gimp_item_is_text_layer (GIMP_ITEM (layer)));
214 
215   active_tool = tool_manager_get_active (image->gimp);
216 
217   if (! GIMP_IS_TEXT_TOOL (active_tool))
218     {
219       GimpToolInfo *tool_info = gimp_get_tool_info (image->gimp,
220                                                     "gimp-text-tool");
221 
222       if (GIMP_IS_TOOL_INFO (tool_info))
223         {
224           gimp_context_set_tool (action_data_get_context (data), tool_info);
225           active_tool = tool_manager_get_active (image->gimp);
226         }
227     }
228 
229   if (GIMP_IS_TEXT_TOOL (active_tool))
230     {
231       if (gimp_text_tool_set_layer (GIMP_TEXT_TOOL (active_tool), layer))
232         {
233           GimpDisplayShell *shell;
234 
235           shell = gimp_display_get_shell (active_tool->display);
236           gtk_widget_grab_focus (shell->canvas);
237         }
238     }
239 }
240 
241 void
layers_edit_attributes_cmd_callback(GimpAction * action,GVariant * value,gpointer data)242 layers_edit_attributes_cmd_callback (GimpAction *action,
243                                      GVariant   *value,
244                                      gpointer    data)
245 {
246   GimpImage *image;
247   GimpLayer *layer;
248   GtkWidget *widget;
249   GtkWidget *dialog;
250   return_if_no_layer (image, layer, data);
251   return_if_no_widget (widget, data);
252 
253 #define EDIT_DIALOG_KEY "gimp-layer-edit-attributes-dialog"
254 
255   dialog = dialogs_get_dialog (G_OBJECT (layer), EDIT_DIALOG_KEY);
256 
257   if (! dialog)
258     {
259       GimpItem *item = GIMP_ITEM (layer);
260 
261       dialog = layer_options_dialog_new (gimp_item_get_image (GIMP_ITEM (layer)),
262                                          layer,
263                                          action_data_get_context (data),
264                                          widget,
265                                          _("Layer Attributes"),
266                                          "gimp-layer-edit",
267                                          GIMP_ICON_EDIT,
268                                          _("Edit Layer Attributes"),
269                                          GIMP_HELP_LAYER_EDIT,
270                                          gimp_object_get_name (layer),
271                                          gimp_layer_get_mode (layer),
272                                          gimp_layer_get_blend_space (layer),
273                                          gimp_layer_get_composite_space (layer),
274                                          gimp_layer_get_composite_mode (layer),
275                                          gimp_layer_get_opacity (layer),
276                                          0 /* unused */,
277                                          gimp_item_get_visible (item),
278                                          gimp_item_get_linked (item),
279                                          gimp_item_get_color_tag (item),
280                                          gimp_item_get_lock_content (item),
281                                          gimp_item_get_lock_position (item),
282                                          gimp_layer_get_lock_alpha (layer),
283                                          layers_edit_attributes_callback,
284                                          NULL);
285 
286       dialogs_attach_dialog (G_OBJECT (layer), EDIT_DIALOG_KEY, dialog);
287     }
288 
289   gtk_window_present (GTK_WINDOW (dialog));
290 }
291 
292 void
layers_new_cmd_callback(GimpAction * action,GVariant * value,gpointer data)293 layers_new_cmd_callback (GimpAction *action,
294                          GVariant   *value,
295                          gpointer    data)
296 {
297   GimpImage *image;
298   GtkWidget *widget;
299   GimpLayer *floating_sel;
300   GtkWidget *dialog;
301   return_if_no_image (image, data);
302   return_if_no_widget (widget, data);
303 
304   /*  If there is a floating selection, the new command transforms
305    *  the current fs into a new layer
306    */
307   if ((floating_sel = gimp_image_get_floating_selection (image)))
308     {
309       GError *error = NULL;
310 
311       if (! floating_sel_to_layer (floating_sel, &error))
312         {
313           gimp_message_literal (image->gimp,
314                                 G_OBJECT (widget), GIMP_MESSAGE_WARNING,
315                                 error->message);
316           g_clear_error (&error);
317           return;
318         }
319 
320       gimp_image_flush (image);
321       return;
322     }
323 
324 #define NEW_DIALOG_KEY "gimp-layer-new-dialog"
325 
326   dialog = dialogs_get_dialog (G_OBJECT (image), NEW_DIALOG_KEY);
327 
328   if (! dialog)
329     {
330       GimpDialogConfig *config     = GIMP_DIALOG_CONFIG (image->gimp->config);
331       GimpLayerMode     layer_mode = config->layer_new_mode;
332 
333       if (layer_mode == GIMP_LAYER_MODE_NORMAL ||
334           layer_mode == GIMP_LAYER_MODE_NORMAL_LEGACY)
335         {
336           layer_mode = gimp_image_get_default_new_layer_mode (image);
337         }
338 
339       dialog = layer_options_dialog_new (image, NULL,
340                                          action_data_get_context (data),
341                                          widget,
342                                          _("New Layer"),
343                                          "gimp-layer-new",
344                                          GIMP_ICON_LAYER,
345                                          _("Create a New Layer"),
346                                          GIMP_HELP_LAYER_NEW,
347                                          config->layer_new_name,
348                                          layer_mode,
349                                          config->layer_new_blend_space,
350                                          config->layer_new_composite_space,
351                                          config->layer_new_composite_mode,
352                                          config->layer_new_opacity,
353                                          config->layer_new_fill_type,
354                                          TRUE,
355                                          FALSE,
356                                          GIMP_COLOR_TAG_NONE,
357                                          FALSE,
358                                          FALSE,
359                                          FALSE,
360                                          layers_new_callback,
361                                          NULL);
362 
363       dialogs_attach_dialog (G_OBJECT (image), NEW_DIALOG_KEY, dialog);
364     }
365 
366   gtk_window_present (GTK_WINDOW (dialog));
367 }
368 
369 void
layers_new_last_vals_cmd_callback(GimpAction * action,GVariant * value,gpointer data)370 layers_new_last_vals_cmd_callback (GimpAction *action,
371                                    GVariant   *value,
372                                    gpointer    data)
373 {
374   GimpImage        *image;
375   GtkWidget        *widget;
376   GimpLayer        *layer;
377   GimpDialogConfig *config;
378   GimpLayerMode     layer_mode;
379 
380   return_if_no_image (image, data);
381   return_if_no_widget (widget, data);
382 
383   config = GIMP_DIALOG_CONFIG (image->gimp->config);
384 
385   /*  If there is a floating selection, the new command transforms
386    *  the current fs into a new layer
387    */
388   if (gimp_image_get_floating_selection (image))
389     {
390       layers_new_cmd_callback (action, value, data);
391       return;
392     }
393 
394   layer_mode = config->layer_new_mode;
395 
396   if (layer_mode == GIMP_LAYER_MODE_NORMAL ||
397       layer_mode == GIMP_LAYER_MODE_NORMAL_LEGACY)
398     {
399       layer_mode = gimp_image_get_default_new_layer_mode (image);
400     }
401 
402   layer = gimp_layer_new (image,
403                           gimp_image_get_width  (image),
404                           gimp_image_get_height (image),
405                           gimp_image_get_layer_format (image, TRUE),
406                           config->layer_new_name,
407                           config->layer_new_opacity,
408                           layer_mode);
409 
410   gimp_drawable_fill (GIMP_DRAWABLE (layer),
411                       action_data_get_context (data),
412                       config->layer_new_fill_type);
413   gimp_layer_set_blend_space (layer,
414                               config->layer_new_blend_space, FALSE);
415   gimp_layer_set_composite_space (layer,
416                                   config->layer_new_composite_space, FALSE);
417   gimp_layer_set_composite_mode (layer,
418                                  config->layer_new_composite_mode, FALSE);
419 
420   gimp_image_add_layer (image, layer, GIMP_IMAGE_ACTIVE_PARENT, -1, TRUE);
421   gimp_image_flush (image);
422 }
423 
424 void
layers_new_from_visible_cmd_callback(GimpAction * action,GVariant * value,gpointer data)425 layers_new_from_visible_cmd_callback (GimpAction *action,
426                                       GVariant   *value,
427                                       gpointer    data)
428 {
429   GimpImage        *image;
430   GimpDisplayShell *shell;
431   GimpLayer        *layer;
432   GimpPickable     *pickable;
433   GimpColorProfile *profile;
434   return_if_no_image (image, data);
435   return_if_no_shell (shell, data);
436 
437   pickable = gimp_display_shell_get_canvas_pickable (shell);
438 
439   gimp_pickable_flush (pickable);
440 
441   profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
442 
443   layer = gimp_layer_new_from_gegl_buffer (gimp_pickable_get_buffer (pickable),
444                                            image,
445                                            gimp_image_get_layer_format (image,
446                                                                         TRUE),
447                                            _("Visible"),
448                                            GIMP_OPACITY_OPAQUE,
449                                            gimp_image_get_default_new_layer_mode (image),
450                                            profile);
451 
452   gimp_image_add_layer (image, layer, GIMP_IMAGE_ACTIVE_PARENT, -1, TRUE);
453   gimp_image_flush (image);
454 }
455 
456 void
layers_new_group_cmd_callback(GimpAction * action,GVariant * value,gpointer data)457 layers_new_group_cmd_callback (GimpAction *action,
458                                GVariant   *value,
459                                gpointer    data)
460 {
461   GimpImage *image;
462   GimpLayer *layer;
463   return_if_no_image (image, data);
464 
465   layer = gimp_group_layer_new (image);
466 
467   gimp_image_add_layer (image, layer, GIMP_IMAGE_ACTIVE_PARENT, -1, TRUE);
468   gimp_image_flush (image);
469 }
470 
471 void
layers_select_cmd_callback(GimpAction * action,GVariant * value,gpointer data)472 layers_select_cmd_callback (GimpAction *action,
473                             GVariant   *value,
474                             gpointer    data)
475 {
476   GimpImage            *image;
477   GimpLayer            *layer;
478   GimpContainer        *container;
479   GimpLayer            *new_layer;
480   GimpActionSelectType  select_type;
481   return_if_no_image (image, data);
482 
483   select_type = (GimpActionSelectType) g_variant_get_int32 (value);
484 
485   layer = gimp_image_get_active_layer (image);
486 
487   if (layer)
488     container = gimp_item_get_container (GIMP_ITEM (layer));
489   else
490     container = gimp_image_get_layers (image);
491 
492   new_layer = (GimpLayer *) action_select_object (select_type,
493                                                   container,
494                                                   (GimpObject *) layer);
495 
496   if (new_layer && new_layer != layer)
497     {
498       gimp_image_set_active_layer (image, new_layer);
499       gimp_image_flush (image);
500     }
501 }
502 
503 void
layers_raise_cmd_callback(GimpAction * action,GVariant * value,gpointer data)504 layers_raise_cmd_callback (GimpAction *action,
505                            GVariant   *value,
506                            gpointer    data)
507 {
508   GimpImage *image;
509   GimpLayer *layer;
510   return_if_no_layer (image, layer, data);
511 
512   gimp_image_raise_item (image, GIMP_ITEM (layer), NULL);
513   gimp_image_flush (image);
514 }
515 
516 void
layers_raise_to_top_cmd_callback(GimpAction * action,GVariant * value,gpointer data)517 layers_raise_to_top_cmd_callback (GimpAction *action,
518                                   GVariant   *value,
519                                   gpointer    data)
520 {
521   GimpImage *image;
522   GimpLayer *layer;
523   return_if_no_layer (image, layer, data);
524 
525   gimp_image_raise_item_to_top (image, GIMP_ITEM (layer));
526   gimp_image_flush (image);
527 }
528 
529 void
layers_lower_cmd_callback(GimpAction * action,GVariant * value,gpointer data)530 layers_lower_cmd_callback (GimpAction *action,
531                            GVariant   *value,
532                            gpointer    data)
533 {
534   GimpImage *image;
535   GimpLayer *layer;
536   return_if_no_layer (image, layer, data);
537 
538   gimp_image_lower_item (image, GIMP_ITEM (layer), NULL);
539   gimp_image_flush (image);
540 }
541 
542 void
layers_lower_to_bottom_cmd_callback(GimpAction * action,GVariant * value,gpointer data)543 layers_lower_to_bottom_cmd_callback (GimpAction *action,
544                                      GVariant   *value,
545                                      gpointer    data)
546 {
547   GimpImage *image;
548   GimpLayer *layer;
549   return_if_no_layer (image, layer, data);
550 
551   gimp_image_lower_item_to_bottom (image, GIMP_ITEM (layer));
552   gimp_image_flush (image);
553 }
554 
555 void
layers_duplicate_cmd_callback(GimpAction * action,GVariant * value,gpointer data)556 layers_duplicate_cmd_callback (GimpAction *action,
557                                GVariant   *value,
558                                gpointer    data)
559 {
560   GimpImage *image;
561   GimpLayer *layer;
562   GimpLayer *new_layer;
563   return_if_no_layer (image, layer, data);
564 
565   new_layer = GIMP_LAYER (gimp_item_duplicate (GIMP_ITEM (layer),
566                                                G_TYPE_FROM_INSTANCE (layer)));
567 
568   /*  use the actual parent here, not GIMP_IMAGE_ACTIVE_PARENT because
569    *  the latter would add a duplicated group inside itself instead of
570    *  above it
571    */
572   gimp_image_add_layer (image, new_layer,
573                         gimp_layer_get_parent (layer), -1,
574                         TRUE);
575   gimp_image_flush (image);
576 }
577 
578 void
layers_anchor_cmd_callback(GimpAction * action,GVariant * value,gpointer data)579 layers_anchor_cmd_callback (GimpAction *action,
580                             GVariant   *value,
581                             gpointer    data)
582 {
583   GimpImage *image;
584   GimpLayer *layer;
585   return_if_no_layer (image, layer, data);
586 
587   if (gimp_layer_is_floating_sel (layer))
588     {
589       floating_sel_anchor (layer);
590       gimp_image_flush (image);
591     }
592 }
593 
594 void
layers_merge_down_cmd_callback(GimpAction * action,GVariant * value,gpointer data)595 layers_merge_down_cmd_callback (GimpAction *action,
596                                 GVariant   *value,
597                                 gpointer    data)
598 {
599   GimpImage   *image;
600   GimpLayer   *layer;
601   GimpDisplay *display;
602   return_if_no_layer (image, layer, data);
603   return_if_no_display (display, data);
604 
605   gimp_image_merge_down (image, layer, action_data_get_context (data),
606                          GIMP_EXPAND_AS_NECESSARY,
607                          GIMP_PROGRESS (display), NULL);
608   gimp_image_flush (image);
609 }
610 
611 void
layers_merge_group_cmd_callback(GimpAction * action,GVariant * value,gpointer data)612 layers_merge_group_cmd_callback (GimpAction *action,
613                                  GVariant   *value,
614                                  gpointer    data)
615 {
616   GimpImage *image;
617   GimpLayer *layer;
618   return_if_no_layer (image, layer, data);
619 
620   gimp_image_merge_group_layer (image, GIMP_GROUP_LAYER (layer));
621   gimp_image_flush (image);
622 }
623 
624 void
layers_delete_cmd_callback(GimpAction * action,GVariant * value,gpointer data)625 layers_delete_cmd_callback (GimpAction *action,
626                             GVariant   *value,
627                             gpointer    data)
628 {
629   GimpImage *image;
630   GimpLayer *layer;
631   return_if_no_layer (image, layer, data);
632 
633   gimp_image_remove_layer (image, layer, TRUE, NULL);
634   gimp_image_flush (image);
635 }
636 
637 void
layers_text_discard_cmd_callback(GimpAction * action,GVariant * value,gpointer data)638 layers_text_discard_cmd_callback (GimpAction *action,
639                                   GVariant   *value,
640                                   gpointer    data)
641 {
642   GimpImage *image;
643   GimpLayer *layer;
644   return_if_no_layer (image, layer, data);
645 
646   if (GIMP_IS_TEXT_LAYER (layer))
647     gimp_text_layer_discard (GIMP_TEXT_LAYER (layer));
648 }
649 
650 void
layers_text_to_vectors_cmd_callback(GimpAction * action,GVariant * value,gpointer data)651 layers_text_to_vectors_cmd_callback (GimpAction *action,
652                                      GVariant   *value,
653                                      gpointer    data)
654 {
655   GimpImage *image;
656   GimpLayer *layer;
657   return_if_no_layer (image, layer, data);
658 
659   if (GIMP_IS_TEXT_LAYER (layer))
660     {
661       GimpVectors *vectors;
662       gint         x, y;
663 
664       vectors = gimp_text_vectors_new (image, GIMP_TEXT_LAYER (layer)->text);
665 
666       gimp_item_get_offset (GIMP_ITEM (layer), &x, &y);
667       gimp_item_translate (GIMP_ITEM (vectors), x, y, FALSE);
668 
669       gimp_image_add_vectors (image, vectors,
670                               GIMP_IMAGE_ACTIVE_PARENT, -1, TRUE);
671       gimp_image_flush (image);
672     }
673 }
674 
675 void
layers_text_along_vectors_cmd_callback(GimpAction * action,GVariant * value,gpointer data)676 layers_text_along_vectors_cmd_callback (GimpAction *action,
677                                         GVariant   *value,
678                                         gpointer    data)
679 {
680   GimpImage   *image;
681   GimpLayer   *layer;
682   GimpVectors *vectors;
683   return_if_no_layer (image, layer, data);
684   return_if_no_vectors (image, vectors, data);
685 
686   if (GIMP_IS_TEXT_LAYER (layer))
687     {
688       gdouble      box_width;
689       gdouble      box_height;
690       GimpVectors *new_vectors;
691       gdouble      offset;
692 
693       box_width  = gimp_item_get_width  (GIMP_ITEM (layer));
694       box_height = gimp_item_get_height (GIMP_ITEM (layer));
695 
696       new_vectors = gimp_text_vectors_new (image, GIMP_TEXT_LAYER (layer)->text);
697 
698       offset = 0;
699       switch (GIMP_TEXT_LAYER (layer)->text->base_dir)
700         {
701         case GIMP_TEXT_DIRECTION_LTR:
702         case GIMP_TEXT_DIRECTION_RTL:
703           offset = 0.5 * box_height;
704           break;
705         case GIMP_TEXT_DIRECTION_TTB_RTL:
706         case GIMP_TEXT_DIRECTION_TTB_RTL_UPRIGHT:
707         case GIMP_TEXT_DIRECTION_TTB_LTR:
708         case GIMP_TEXT_DIRECTION_TTB_LTR_UPRIGHT:
709           {
710             GimpStroke *stroke = NULL;
711 
712             while ((stroke = gimp_vectors_stroke_get_next (new_vectors, stroke)))
713               {
714                 gimp_stroke_rotate (stroke, 0, 0, 270);
715                 gimp_stroke_translate (stroke, 0, box_width);
716               }
717           }
718           offset = 0.5 * box_width;
719           break;
720         }
721 
722 
723       gimp_vectors_warp_vectors (vectors, new_vectors, offset);
724 
725       gimp_item_set_visible (GIMP_ITEM (new_vectors), TRUE, FALSE);
726 
727       gimp_image_add_vectors (image, new_vectors,
728                               GIMP_IMAGE_ACTIVE_PARENT, -1, TRUE);
729       gimp_image_flush (image);
730     }
731 }
732 
733 void
layers_resize_cmd_callback(GimpAction * action,GVariant * value,gpointer data)734 layers_resize_cmd_callback (GimpAction *action,
735                             GVariant   *value,
736                             gpointer    data)
737 {
738   GimpImage *image;
739   GimpLayer *layer;
740   GtkWidget *widget;
741   GtkWidget *dialog;
742   return_if_no_layer (image, layer, data);
743   return_if_no_widget (widget, data);
744 
745 #define RESIZE_DIALOG_KEY "gimp-resize-dialog"
746 
747   dialog = dialogs_get_dialog (G_OBJECT (layer), RESIZE_DIALOG_KEY);
748 
749   if (! dialog)
750     {
751       GimpDialogConfig *config  = GIMP_DIALOG_CONFIG (image->gimp->config);
752       GimpDisplay      *display = NULL;
753 
754       if (GIMP_IS_IMAGE_WINDOW (data))
755         display = action_data_get_display (data);
756 
757       if (layer_resize_unit != GIMP_UNIT_PERCENT && display)
758         layer_resize_unit = gimp_display_get_shell (display)->unit;
759 
760       dialog = resize_dialog_new (GIMP_VIEWABLE (layer),
761                                   action_data_get_context (data),
762                                   _("Set Layer Boundary Size"),
763                                   "gimp-layer-resize",
764                                   widget,
765                                   gimp_standard_help_func,
766                                   GIMP_HELP_LAYER_RESIZE,
767                                   layer_resize_unit,
768                                   config->layer_resize_fill_type,
769                                   GIMP_ITEM_SET_NONE,
770                                   FALSE,
771                                   layers_resize_callback,
772                                   NULL);
773 
774       dialogs_attach_dialog (G_OBJECT (layer), RESIZE_DIALOG_KEY, dialog);
775     }
776 
777   gtk_window_present (GTK_WINDOW (dialog));
778 }
779 
780 void
layers_resize_to_image_cmd_callback(GimpAction * action,GVariant * value,gpointer data)781 layers_resize_to_image_cmd_callback (GimpAction *action,
782                                      GVariant   *value,
783                                      gpointer    data)
784 {
785   GimpImage *image;
786   GimpLayer *layer;
787   return_if_no_layer (image, layer, data);
788 
789   gimp_layer_resize_to_image (layer,
790                               action_data_get_context (data),
791                               GIMP_FILL_TRANSPARENT);
792   gimp_image_flush (image);
793 }
794 
795 void
layers_scale_cmd_callback(GimpAction * action,GVariant * value,gpointer data)796 layers_scale_cmd_callback (GimpAction *action,
797                            GVariant   *value,
798                            gpointer    data)
799 {
800   GimpImage *image;
801   GimpLayer *layer;
802   GtkWidget *widget;
803   GtkWidget *dialog;
804   return_if_no_layer (image, layer, data);
805   return_if_no_widget (widget, data);
806 
807 #define SCALE_DIALOG_KEY "gimp-scale-dialog"
808 
809   dialog = dialogs_get_dialog (G_OBJECT (layer), SCALE_DIALOG_KEY);
810 
811   if (! dialog)
812     {
813       GimpDisplay *display = NULL;
814 
815       if (GIMP_IS_IMAGE_WINDOW (data))
816         display = action_data_get_display (data);
817 
818       if (layer_scale_unit != GIMP_UNIT_PERCENT && display)
819         layer_scale_unit = gimp_display_get_shell (display)->unit;
820 
821       if (layer_scale_interp == -1)
822         layer_scale_interp = image->gimp->config->interpolation_type;
823 
824       dialog = scale_dialog_new (GIMP_VIEWABLE (layer),
825                                  action_data_get_context (data),
826                                  _("Scale Layer"), "gimp-layer-scale",
827                                  widget,
828                                  gimp_standard_help_func, GIMP_HELP_LAYER_SCALE,
829                                  layer_scale_unit,
830                                  layer_scale_interp,
831                                  layers_scale_callback,
832                                  display);
833 
834       dialogs_attach_dialog (G_OBJECT (layer), SCALE_DIALOG_KEY, dialog);
835     }
836 
837   gtk_window_present (GTK_WINDOW (dialog));
838 }
839 
840 void
layers_crop_to_selection_cmd_callback(GimpAction * action,GVariant * value,gpointer data)841 layers_crop_to_selection_cmd_callback (GimpAction *action,
842                                        GVariant   *value,
843                                        gpointer    data)
844 {
845   GimpImage *image;
846   GimpLayer *layer;
847   GtkWidget *widget;
848   gint       x, y;
849   gint       width, height;
850   gint       off_x, off_y;
851   return_if_no_layer (image, layer, data);
852   return_if_no_widget (widget, data);
853 
854   if (! gimp_item_bounds (GIMP_ITEM (gimp_image_get_mask (image)),
855                           &x, &y, &width, &height))
856     {
857       gimp_message_literal (image->gimp,
858                             G_OBJECT (widget), GIMP_MESSAGE_WARNING,
859                             _("Cannot crop because the current selection "
860                               "is empty."));
861       return;
862     }
863 
864   gimp_item_get_offset (GIMP_ITEM (layer), &off_x, &off_y);
865   off_x -= x;
866   off_y -= y;
867 
868   gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
869                                _("Crop Layer to Selection"));
870 
871   gimp_item_resize (GIMP_ITEM (layer),
872                     action_data_get_context (data), GIMP_FILL_TRANSPARENT,
873                     width, height, off_x, off_y);
874 
875   gimp_image_undo_group_end (image);
876   gimp_image_flush (image);
877 }
878 
879 void
layers_crop_to_content_cmd_callback(GimpAction * action,GVariant * value,gpointer data)880 layers_crop_to_content_cmd_callback (GimpAction *action,
881                                      GVariant   *value,
882                                      gpointer    data)
883 {
884   GimpImage *image;
885   GimpLayer *layer;
886   GtkWidget *widget;
887   gint       x, y;
888   gint       width, height;
889   return_if_no_layer (image, layer, data);
890   return_if_no_widget (widget, data);
891 
892   switch (gimp_pickable_auto_shrink (GIMP_PICKABLE (layer),
893                                      0, 0,
894                                      gimp_item_get_width  (GIMP_ITEM (layer)),
895                                      gimp_item_get_height (GIMP_ITEM (layer)),
896                                      &x, &y, &width, &height))
897     {
898     case GIMP_AUTO_SHRINK_SHRINK:
899       gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
900                                    _("Crop Layer to Content"));
901 
902       gimp_item_resize (GIMP_ITEM (layer),
903                         action_data_get_context (data), GIMP_FILL_TRANSPARENT,
904                         width, height, -x, -y);
905 
906       gimp_image_undo_group_end (image);
907       gimp_image_flush (image);
908       break;
909 
910     case GIMP_AUTO_SHRINK_EMPTY:
911       gimp_message_literal (image->gimp,
912                             G_OBJECT (widget), GIMP_MESSAGE_INFO,
913                             _("Cannot crop because the active layer "
914                               "has no content."));
915       break;
916 
917     case GIMP_AUTO_SHRINK_UNSHRINKABLE:
918       gimp_message_literal (image->gimp,
919                             G_OBJECT (widget), GIMP_MESSAGE_INFO,
920                             _("Cannot crop because the active layer "
921                               "is already cropped to its content."));
922       break;
923     }
924 }
925 
926 void
layers_mask_add_cmd_callback(GimpAction * action,GVariant * value,gpointer data)927 layers_mask_add_cmd_callback (GimpAction *action,
928                               GVariant   *value,
929                               gpointer    data)
930 {
931   GimpImage *image;
932   GimpLayer *layer;
933   GtkWidget *widget;
934   GtkWidget *dialog;
935   return_if_no_layer (image, layer, data);
936   return_if_no_widget (widget, data);
937 
938   if (gimp_layer_get_mask (layer))
939     return;
940 
941 #define ADD_MASK_DIALOG_KEY "gimp-add-mask-dialog"
942 
943   dialog = dialogs_get_dialog (G_OBJECT (layer), ADD_MASK_DIALOG_KEY);
944 
945   if (! dialog)
946     {
947       GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
948 
949       dialog = layer_add_mask_dialog_new (layer, action_data_get_context (data),
950                                           widget,
951                                           config->layer_add_mask_type,
952                                           config->layer_add_mask_invert,
953                                           layers_add_mask_callback,
954                                           NULL);
955 
956       dialogs_attach_dialog (G_OBJECT (layer), ADD_MASK_DIALOG_KEY, dialog);
957     }
958 
959   gtk_window_present (GTK_WINDOW (dialog));
960 }
961 
962 void
layers_mask_add_last_vals_cmd_callback(GimpAction * action,GVariant * value,gpointer data)963 layers_mask_add_last_vals_cmd_callback (GimpAction *action,
964                                         GVariant   *value,
965                                         gpointer    data)
966 {
967   GimpImage        *image;
968   GimpLayer        *layer;
969   GtkWidget        *widget;
970   GimpDialogConfig *config;
971   GimpChannel      *channel = NULL;
972   GimpLayerMask    *mask;
973   return_if_no_layer (image, layer, data);
974   return_if_no_widget (widget, data);
975 
976   if (gimp_layer_get_mask (layer))
977     return;
978 
979   config = GIMP_DIALOG_CONFIG (image->gimp->config);
980 
981   if (config->layer_add_mask_type == GIMP_ADD_MASK_CHANNEL)
982     {
983       channel = gimp_image_get_active_channel (image);
984 
985       if (! channel)
986         {
987           GimpContainer *channels = gimp_image_get_channels (image);
988 
989           channel = GIMP_CHANNEL (gimp_container_get_first_child (channels));
990         }
991 
992       if (! channel)
993         {
994           layers_mask_add_cmd_callback (action, value, data);
995           return;
996         }
997     }
998 
999   mask = gimp_layer_create_mask (layer,
1000                                  config->layer_add_mask_type,
1001                                  channel);
1002 
1003   if (config->layer_add_mask_invert)
1004     gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);
1005 
1006   gimp_layer_add_mask (layer, mask, TRUE, NULL);
1007   gimp_image_flush (image);
1008 }
1009 
1010 void
layers_mask_apply_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1011 layers_mask_apply_cmd_callback (GimpAction *action,
1012                                 GVariant   *value,
1013                                 gpointer    data)
1014 {
1015   GimpImage *image;
1016   GimpLayer *layer;
1017   return_if_no_layer (image, layer, data);
1018 
1019   if (gimp_layer_get_mask (layer))
1020     {
1021       GimpMaskApplyMode mode = (GimpMaskApplyMode) g_variant_get_int32 (value);
1022 
1023       gimp_layer_apply_mask (layer, mode, TRUE);
1024       gimp_image_flush (image);
1025     }
1026 }
1027 
1028 void
layers_mask_edit_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1029 layers_mask_edit_cmd_callback (GimpAction *action,
1030                                GVariant   *value,
1031                                gpointer    data)
1032 {
1033   GimpImage *image;
1034   GimpLayer *layer;
1035   return_if_no_layer (image, layer, data);
1036 
1037   if (gimp_layer_get_mask (layer))
1038     {
1039       gboolean active = g_variant_get_boolean (value);
1040 
1041       gimp_layer_set_edit_mask (layer, active);
1042       gimp_image_flush (image);
1043     }
1044 }
1045 
1046 void
layers_mask_show_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1047 layers_mask_show_cmd_callback (GimpAction *action,
1048                                GVariant   *value,
1049                                gpointer    data)
1050 {
1051   GimpImage *image;
1052   GimpLayer *layer;
1053   return_if_no_layer (image, layer, data);
1054 
1055   if (gimp_layer_get_mask (layer))
1056     {
1057       gboolean active = g_variant_get_boolean (value);
1058 
1059       gimp_layer_set_show_mask (layer, active, TRUE);
1060       gimp_image_flush (image);
1061     }
1062 }
1063 
1064 void
layers_mask_disable_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1065 layers_mask_disable_cmd_callback (GimpAction *action,
1066                                   GVariant   *value,
1067                                   gpointer    data)
1068 {
1069   GimpImage *image;
1070   GimpLayer *layer;
1071   return_if_no_layer (image, layer, data);
1072 
1073   if (gimp_layer_get_mask (layer))
1074     {
1075       gboolean active = g_variant_get_boolean (value);
1076 
1077       gimp_layer_set_apply_mask (layer, ! active, TRUE);
1078       gimp_image_flush (image);
1079     }
1080 }
1081 
1082 void
layers_mask_to_selection_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1083 layers_mask_to_selection_cmd_callback (GimpAction *action,
1084                                        GVariant   *value,
1085                                        gpointer    data)
1086 {
1087   GimpImage     *image;
1088   GimpLayer     *layer;
1089   GimpLayerMask *mask;
1090   return_if_no_layer (image, layer, data);
1091 
1092   mask = gimp_layer_get_mask (layer);
1093 
1094   if (mask)
1095     {
1096       GimpChannelOps operation = (GimpChannelOps) g_variant_get_int32 (value);
1097 
1098       gimp_item_to_selection (GIMP_ITEM (mask), operation,
1099                               TRUE, FALSE, 0.0, 0.0);
1100       gimp_image_flush (image);
1101     }
1102 }
1103 
1104 void
layers_alpha_add_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1105 layers_alpha_add_cmd_callback (GimpAction *action,
1106                                GVariant   *value,
1107                                gpointer    data)
1108 {
1109   GimpImage *image;
1110   GimpLayer *layer;
1111   return_if_no_layer (image, layer, data);
1112 
1113   if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
1114     {
1115       gimp_layer_add_alpha (layer);
1116       gimp_image_flush (image);
1117     }
1118 }
1119 
1120 void
layers_alpha_remove_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1121 layers_alpha_remove_cmd_callback (GimpAction *action,
1122                                   GVariant   *value,
1123                                   gpointer    data)
1124 {
1125   GimpImage *image;
1126   GimpLayer *layer;
1127   return_if_no_layer (image, layer, data);
1128 
1129   if (gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
1130     {
1131       gimp_layer_remove_alpha (layer, action_data_get_context (data));
1132       gimp_image_flush (image);
1133     }
1134 }
1135 
1136 void
layers_alpha_to_selection_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1137 layers_alpha_to_selection_cmd_callback (GimpAction *action,
1138                                         GVariant   *value,
1139                                         gpointer    data)
1140 {
1141   GimpImage      *image;
1142   GimpLayer      *layer;
1143   GimpChannelOps  operation;
1144   return_if_no_layer (image, layer, data);
1145 
1146   operation = (GimpChannelOps) g_variant_get_int32 (value);
1147 
1148   gimp_item_to_selection (GIMP_ITEM (layer), operation,
1149                           TRUE, FALSE, 0.0, 0.0);
1150   gimp_image_flush (image);
1151 }
1152 
1153 void
layers_opacity_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1154 layers_opacity_cmd_callback (GimpAction *action,
1155                              GVariant   *value,
1156                              gpointer    data)
1157 {
1158   GimpImage            *image;
1159   GimpLayer            *layer;
1160   gdouble               opacity;
1161   GimpUndo             *undo;
1162   GimpActionSelectType  select_type;
1163   gboolean              push_undo = TRUE;
1164   return_if_no_layer (image, layer, data);
1165 
1166   select_type = (GimpActionSelectType) g_variant_get_int32 (value);
1167 
1168   undo = gimp_image_undo_can_compress (image, GIMP_TYPE_ITEM_UNDO,
1169                                        GIMP_UNDO_LAYER_OPACITY);
1170 
1171   if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
1172     push_undo = FALSE;
1173 
1174   opacity = action_select_value (select_type,
1175                                  gimp_layer_get_opacity (layer),
1176                                  0.0, 1.0, 1.0,
1177                                  1.0 / 255.0, 0.01, 0.1, 0.0, FALSE);
1178   gimp_layer_set_opacity (layer, opacity, push_undo);
1179   gimp_image_flush (image);
1180 }
1181 
1182 void
layers_mode_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1183 layers_mode_cmd_callback (GimpAction *action,
1184                           GVariant   *value,
1185                           gpointer    data)
1186 {
1187   GimpImage            *image;
1188   GimpLayer            *layer;
1189   GimpLayerMode        *modes;
1190   gint                  n_modes;
1191   GimpLayerMode         layer_mode;
1192   gint                  index;
1193   GimpUndo             *undo;
1194   GimpActionSelectType  select_type;
1195   gboolean              push_undo = TRUE;
1196   return_if_no_layer (image, layer, data);
1197 
1198   select_type = (GimpActionSelectType) g_variant_get_int32 (value);
1199 
1200   undo = gimp_image_undo_can_compress (image, GIMP_TYPE_ITEM_UNDO,
1201                                        GIMP_UNDO_LAYER_MODE);
1202 
1203   if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
1204     push_undo = FALSE;
1205 
1206   layer_mode = gimp_layer_get_mode (layer);
1207 
1208   modes = gimp_layer_mode_get_context_array (layer_mode,
1209                                              GIMP_LAYER_MODE_CONTEXT_LAYER,
1210                                              &n_modes);
1211   index = layers_mode_index (layer_mode, modes, n_modes);
1212   index = action_select_value (select_type,
1213                                index, 0, n_modes - 1, 0,
1214                                0.0, 1.0, 1.0, 0.0, FALSE);
1215   layer_mode = modes[index];
1216   g_free (modes);
1217 
1218   gimp_layer_set_mode (layer, layer_mode, push_undo);
1219   gimp_image_flush (image);
1220 }
1221 
1222 void
layers_blend_space_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1223 layers_blend_space_cmd_callback (GimpAction *action,
1224                                  GVariant   *value,
1225                                  gpointer    data)
1226 {
1227   GimpImage           *image;
1228   GimpLayer           *layer;
1229   GimpLayerColorSpace  blend_space;
1230   return_if_no_layer (image, layer, data);
1231 
1232   blend_space = (GimpLayerColorSpace) g_variant_get_int32 (value);
1233 
1234   if (blend_space != gimp_layer_get_blend_space (layer))
1235     {
1236       GimpUndo *undo;
1237       gboolean  push_undo = TRUE;
1238 
1239       undo = gimp_image_undo_can_compress (image, GIMP_TYPE_LAYER_PROP_UNDO,
1240                                            GIMP_UNDO_LAYER_MODE);
1241 
1242       if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
1243         push_undo = FALSE;
1244 
1245       gimp_layer_set_blend_space (layer, blend_space, push_undo);
1246       gimp_image_flush (image);
1247     }
1248 }
1249 
1250 void
layers_composite_space_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1251 layers_composite_space_cmd_callback (GimpAction *action,
1252                                      GVariant   *value,
1253                                      gpointer    data)
1254 {
1255   GimpImage           *image;
1256   GimpLayer           *layer;
1257   GimpLayerColorSpace  composite_space;
1258   return_if_no_layer (image, layer, data);
1259 
1260   composite_space = (GimpLayerColorSpace) g_variant_get_int32 (value);
1261 
1262   if (composite_space != gimp_layer_get_composite_space (layer))
1263     {
1264       GimpUndo *undo;
1265       gboolean  push_undo = TRUE;
1266 
1267       undo = gimp_image_undo_can_compress (image, GIMP_TYPE_LAYER_PROP_UNDO,
1268                                            GIMP_UNDO_LAYER_MODE);
1269 
1270       if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
1271         push_undo = FALSE;
1272 
1273       gimp_layer_set_composite_space (layer, composite_space, push_undo);
1274       gimp_image_flush (image);
1275     }
1276 }
1277 
1278 void
layers_composite_mode_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1279 layers_composite_mode_cmd_callback (GimpAction *action,
1280                                     GVariant   *value,
1281                                     gpointer    data)
1282 {
1283   GimpImage              *image;
1284   GimpLayer              *layer;
1285   GimpLayerCompositeMode  composite_mode;
1286   return_if_no_layer (image, layer, data);
1287 
1288   composite_mode = (GimpLayerCompositeMode) g_variant_get_int32 (value);
1289 
1290   if (composite_mode != gimp_layer_get_composite_mode (layer))
1291     {
1292       GimpUndo *undo;
1293       gboolean  push_undo = TRUE;
1294 
1295       undo = gimp_image_undo_can_compress (image, GIMP_TYPE_LAYER_PROP_UNDO,
1296                                            GIMP_UNDO_LAYER_MODE);
1297 
1298       if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
1299         push_undo = FALSE;
1300 
1301       gimp_layer_set_composite_mode (layer, composite_mode, push_undo);
1302       gimp_image_flush (image);
1303     }
1304 }
1305 
1306 void
layers_visible_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1307 layers_visible_cmd_callback (GimpAction *action,
1308                              GVariant   *value,
1309                              gpointer   data)
1310 {
1311   GimpImage *image;
1312   GimpLayer *layer;
1313   return_if_no_layer (image, layer, data);
1314 
1315   items_visible_cmd_callback (action, value, image, GIMP_ITEM (layer));
1316 }
1317 
1318 void
layers_linked_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1319 layers_linked_cmd_callback (GimpAction *action,
1320                             GVariant   *value,
1321                             gpointer    data)
1322 {
1323   GimpImage *image;
1324   GimpLayer *layer;
1325   return_if_no_layer (image, layer, data);
1326 
1327   items_linked_cmd_callback (action, value, image, GIMP_ITEM (layer));
1328 }
1329 
1330 void
layers_lock_content_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1331 layers_lock_content_cmd_callback (GimpAction *action,
1332                                   GVariant   *value,
1333                                   gpointer    data)
1334 {
1335   GimpImage *image;
1336   GimpLayer *layer;
1337   return_if_no_layer (image, layer, data);
1338 
1339   items_lock_content_cmd_callback (action, value, image, GIMP_ITEM (layer));
1340 }
1341 
1342 void
layers_lock_position_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1343 layers_lock_position_cmd_callback (GimpAction *action,
1344                                    GVariant   *value,
1345                                    gpointer    data)
1346 {
1347   GimpImage *image;
1348   GimpLayer *layer;
1349   return_if_no_layer (image, layer, data);
1350 
1351   items_lock_position_cmd_callback (action, value, image, GIMP_ITEM (layer));
1352 }
1353 
1354 void
layers_lock_alpha_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1355 layers_lock_alpha_cmd_callback (GimpAction *action,
1356                                 GVariant   *value,
1357                                 gpointer    data)
1358 {
1359   GimpImage *image;
1360   GimpLayer *layer;
1361   gboolean   lock_alpha;
1362   return_if_no_layer (image, layer, data);
1363 
1364   lock_alpha = g_variant_get_boolean (value);
1365 
1366   if (lock_alpha != gimp_layer_get_lock_alpha (layer))
1367     {
1368       GimpUndo *undo;
1369       gboolean  push_undo = TRUE;
1370 
1371       undo = gimp_image_undo_can_compress (image, GIMP_TYPE_ITEM_UNDO,
1372                                            GIMP_UNDO_LAYER_LOCK_ALPHA);
1373 
1374       if (undo && GIMP_ITEM_UNDO (undo)->item == GIMP_ITEM (layer))
1375         push_undo = FALSE;
1376 
1377       gimp_layer_set_lock_alpha (layer, lock_alpha, push_undo);
1378       gimp_image_flush (image);
1379     }
1380 }
1381 
1382 void
layers_color_tag_cmd_callback(GimpAction * action,GVariant * value,gpointer data)1383 layers_color_tag_cmd_callback (GimpAction *action,
1384                                GVariant   *value,
1385                                gpointer    data)
1386 {
1387   GimpImage    *image;
1388   GimpLayer    *layer;
1389   GimpColorTag  color_tag;
1390   return_if_no_layer (image, layer, data);
1391 
1392   color_tag = (GimpColorTag) g_variant_get_int32 (value);
1393 
1394   items_color_tag_cmd_callback (action, image, GIMP_ITEM (layer),
1395                                 color_tag);
1396 }
1397 
1398 
1399 /*  private functions  */
1400 
1401 static void
layers_new_callback(GtkWidget * dialog,GimpImage * image,GimpLayer * layer,GimpContext * context,const gchar * layer_name,GimpLayerMode layer_mode,GimpLayerColorSpace layer_blend_space,GimpLayerColorSpace layer_composite_space,GimpLayerCompositeMode layer_composite_mode,gdouble layer_opacity,GimpFillType layer_fill_type,gint layer_width,gint layer_height,gint layer_offset_x,gint layer_offset_y,gboolean layer_visible,gboolean layer_linked,GimpColorTag layer_color_tag,gboolean layer_lock_pixels,gboolean layer_lock_position,gboolean layer_lock_alpha,gboolean rename_text_layer,gpointer user_data)1402 layers_new_callback (GtkWidget              *dialog,
1403                      GimpImage              *image,
1404                      GimpLayer              *layer,
1405                      GimpContext            *context,
1406                      const gchar            *layer_name,
1407                      GimpLayerMode           layer_mode,
1408                      GimpLayerColorSpace     layer_blend_space,
1409                      GimpLayerColorSpace     layer_composite_space,
1410                      GimpLayerCompositeMode  layer_composite_mode,
1411                      gdouble                 layer_opacity,
1412                      GimpFillType            layer_fill_type,
1413                      gint                    layer_width,
1414                      gint                    layer_height,
1415                      gint                    layer_offset_x,
1416                      gint                    layer_offset_y,
1417                      gboolean                layer_visible,
1418                      gboolean                layer_linked,
1419                      GimpColorTag            layer_color_tag,
1420                      gboolean                layer_lock_pixels,
1421                      gboolean                layer_lock_position,
1422                      gboolean                layer_lock_alpha,
1423                      gboolean                rename_text_layer, /* unused */
1424                      gpointer                user_data)
1425 {
1426   GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
1427 
1428   g_object_set (config,
1429                 "layer-new-name",            layer_name,
1430                 "layer-new-mode",            layer_mode,
1431                 "layer-new-blend-space",     layer_blend_space,
1432                 "layer-new-composite-space", layer_composite_space,
1433                 "layer-new-composite-mode",  layer_composite_mode,
1434                 "layer-new-opacity",         layer_opacity,
1435                 "layer-new-fill-type",       layer_fill_type,
1436                 NULL);
1437 
1438   layer = gimp_layer_new (image, layer_width, layer_height,
1439                           gimp_image_get_layer_format (image, TRUE),
1440                           config->layer_new_name,
1441                           config->layer_new_opacity,
1442                           config->layer_new_mode);
1443 
1444   if (layer)
1445     {
1446       gimp_item_set_offset (GIMP_ITEM (layer), layer_offset_x, layer_offset_y);
1447       gimp_drawable_fill (GIMP_DRAWABLE (layer), context,
1448                           config->layer_new_fill_type);
1449       gimp_item_set_visible (GIMP_ITEM (layer), layer_visible, FALSE);
1450       gimp_item_set_linked (GIMP_ITEM (layer), layer_linked, FALSE);
1451       gimp_item_set_color_tag (GIMP_ITEM (layer), layer_color_tag, FALSE);
1452       gimp_item_set_lock_content (GIMP_ITEM (layer), layer_lock_pixels,
1453                                   FALSE);
1454       gimp_item_set_lock_position (GIMP_ITEM (layer), layer_lock_position,
1455                                    FALSE);
1456       gimp_layer_set_lock_alpha (layer, layer_lock_alpha, FALSE);
1457       gimp_layer_set_blend_space (layer, layer_blend_space, FALSE);
1458       gimp_layer_set_composite_space (layer, layer_composite_space, FALSE);
1459       gimp_layer_set_composite_mode (layer, layer_composite_mode, FALSE);
1460 
1461       gimp_image_add_layer (image, layer,
1462                             GIMP_IMAGE_ACTIVE_PARENT, -1, TRUE);
1463       gimp_image_flush (image);
1464     }
1465   else
1466     {
1467       g_warning ("%s: could not allocate new layer", G_STRFUNC);
1468     }
1469 
1470   gtk_widget_destroy (dialog);
1471 }
1472 
1473 static void
layers_edit_attributes_callback(GtkWidget * dialog,GimpImage * image,GimpLayer * layer,GimpContext * context,const gchar * layer_name,GimpLayerMode layer_mode,GimpLayerColorSpace layer_blend_space,GimpLayerColorSpace layer_composite_space,GimpLayerCompositeMode layer_composite_mode,gdouble layer_opacity,GimpFillType unused1,gint unused2,gint unused3,gint layer_offset_x,gint layer_offset_y,gboolean layer_visible,gboolean layer_linked,GimpColorTag layer_color_tag,gboolean layer_lock_pixels,gboolean layer_lock_position,gboolean layer_lock_alpha,gboolean rename_text_layer,gpointer user_data)1474 layers_edit_attributes_callback (GtkWidget              *dialog,
1475                                  GimpImage              *image,
1476                                  GimpLayer              *layer,
1477                                  GimpContext            *context,
1478                                  const gchar            *layer_name,
1479                                  GimpLayerMode           layer_mode,
1480                                  GimpLayerColorSpace     layer_blend_space,
1481                                  GimpLayerColorSpace     layer_composite_space,
1482                                  GimpLayerCompositeMode  layer_composite_mode,
1483                                  gdouble                 layer_opacity,
1484                                  GimpFillType            unused1,
1485                                  gint                    unused2,
1486                                  gint                    unused3,
1487                                  gint                    layer_offset_x,
1488                                  gint                    layer_offset_y,
1489                                  gboolean                layer_visible,
1490                                  gboolean                layer_linked,
1491                                  GimpColorTag            layer_color_tag,
1492                                  gboolean                layer_lock_pixels,
1493                                  gboolean                layer_lock_position,
1494                                  gboolean                layer_lock_alpha,
1495                                  gboolean                rename_text_layer,
1496                                  gpointer                user_data)
1497 {
1498   GimpItem *item = GIMP_ITEM (layer);
1499 
1500   if (strcmp (layer_name, gimp_object_get_name (layer))               ||
1501       layer_mode            != gimp_layer_get_mode (layer)            ||
1502       layer_blend_space     != gimp_layer_get_blend_space (layer)     ||
1503       layer_composite_space != gimp_layer_get_composite_space (layer) ||
1504       layer_composite_mode  != gimp_layer_get_composite_mode (layer)  ||
1505       layer_opacity         != gimp_layer_get_opacity (layer)         ||
1506       layer_offset_x        != gimp_item_get_offset_x (item)          ||
1507       layer_offset_y        != gimp_item_get_offset_y (item)          ||
1508       layer_visible         != gimp_item_get_visible (item)           ||
1509       layer_linked          != gimp_item_get_linked (item)            ||
1510       layer_color_tag       != gimp_item_get_color_tag (item)         ||
1511       layer_lock_pixels     != gimp_item_get_lock_content (item)      ||
1512       layer_lock_position   != gimp_item_get_lock_position (item)     ||
1513       layer_lock_alpha      != gimp_layer_get_lock_alpha (layer))
1514     {
1515       gimp_image_undo_group_start (image,
1516                                    GIMP_UNDO_GROUP_ITEM_PROPERTIES,
1517                                    _("Layer Attributes"));
1518 
1519       if (strcmp (layer_name, gimp_object_get_name (layer)))
1520         {
1521           GError *error = NULL;
1522 
1523           if (! gimp_item_rename (GIMP_ITEM (layer), layer_name, &error))
1524             {
1525               gimp_message_literal (image->gimp,
1526                                     G_OBJECT (dialog), GIMP_MESSAGE_WARNING,
1527                                     error->message);
1528               g_clear_error (&error);
1529             }
1530         }
1531 
1532       if (layer_mode != gimp_layer_get_mode (layer))
1533         gimp_layer_set_mode (layer, layer_mode, TRUE);
1534 
1535       if (layer_blend_space != gimp_layer_get_blend_space (layer))
1536         gimp_layer_set_blend_space (layer, layer_blend_space, TRUE);
1537 
1538       if (layer_composite_space != gimp_layer_get_composite_space (layer))
1539         gimp_layer_set_composite_space (layer, layer_composite_space, TRUE);
1540 
1541       if (layer_composite_mode != gimp_layer_get_composite_mode (layer))
1542         gimp_layer_set_composite_mode (layer, layer_composite_mode, TRUE);
1543 
1544       if (layer_opacity != gimp_layer_get_opacity (layer))
1545         gimp_layer_set_opacity (layer, layer_opacity, TRUE);
1546 
1547       if (layer_offset_x != gimp_item_get_offset_x (item) ||
1548           layer_offset_y != gimp_item_get_offset_y (item))
1549         {
1550           gimp_item_translate (item,
1551                                layer_offset_x - gimp_item_get_offset_x (item),
1552                                layer_offset_y - gimp_item_get_offset_y (item),
1553                                TRUE);
1554         }
1555 
1556       if (layer_visible != gimp_item_get_visible (item))
1557         gimp_item_set_visible (item, layer_visible, TRUE);
1558 
1559       if (layer_linked != gimp_item_get_linked (item))
1560         gimp_item_set_linked (item, layer_linked, TRUE);
1561 
1562       if (layer_color_tag != gimp_item_get_color_tag (item))
1563         gimp_item_set_color_tag (item, layer_color_tag, TRUE);
1564 
1565       if (layer_lock_pixels != gimp_item_get_lock_content (item))
1566         gimp_item_set_lock_content (item, layer_lock_pixels, TRUE);
1567 
1568       if (layer_lock_position != gimp_item_get_lock_position (item))
1569         gimp_item_set_lock_position (item, layer_lock_position, TRUE);
1570 
1571       if (layer_lock_alpha != gimp_layer_get_lock_alpha (layer))
1572         gimp_layer_set_lock_alpha (layer, layer_lock_alpha, TRUE);
1573 
1574       gimp_image_undo_group_end (image);
1575 
1576       gimp_image_flush (image);
1577     }
1578 
1579   if (gimp_item_is_text_layer (GIMP_ITEM (layer)))
1580     {
1581       g_object_set (layer,
1582                     "auto-rename", rename_text_layer,
1583                     NULL);
1584     }
1585 
1586   gtk_widget_destroy (dialog);
1587 }
1588 
1589 static void
layers_add_mask_callback(GtkWidget * dialog,GimpLayer * layer,GimpAddMaskType add_mask_type,GimpChannel * channel,gboolean invert,gpointer user_data)1590 layers_add_mask_callback (GtkWidget       *dialog,
1591                           GimpLayer       *layer,
1592                           GimpAddMaskType  add_mask_type,
1593                           GimpChannel     *channel,
1594                           gboolean         invert,
1595                           gpointer         user_data)
1596 {
1597   GimpImage        *image  = gimp_item_get_image (GIMP_ITEM (layer));
1598   GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
1599   GimpLayerMask    *mask;
1600   GError           *error = NULL;
1601 
1602   g_object_set (config,
1603                 "layer-add-mask-type",   add_mask_type,
1604                 "layer-add-mask-invert", invert,
1605                 NULL);
1606 
1607   mask = gimp_layer_create_mask (layer,
1608                                  config->layer_add_mask_type,
1609                                  channel);
1610 
1611   if (config->layer_add_mask_invert)
1612     gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);
1613 
1614   if (! gimp_layer_add_mask (layer, mask, TRUE, &error))
1615     {
1616       gimp_message_literal (image->gimp,
1617                             G_OBJECT (dialog), GIMP_MESSAGE_WARNING,
1618                             error->message);
1619       g_object_unref (mask);
1620       g_clear_error (&error);
1621       return;
1622     }
1623 
1624   gimp_image_flush (image);
1625 
1626   gtk_widget_destroy (dialog);
1627 }
1628 
1629 static void
layers_scale_callback(GtkWidget * dialog,GimpViewable * viewable,gint width,gint height,GimpUnit unit,GimpInterpolationType interpolation,gdouble xresolution,gdouble yresolution,GimpUnit resolution_unit,gpointer user_data)1630 layers_scale_callback (GtkWidget             *dialog,
1631                        GimpViewable          *viewable,
1632                        gint                   width,
1633                        gint                   height,
1634                        GimpUnit               unit,
1635                        GimpInterpolationType  interpolation,
1636                        gdouble                xresolution,    /* unused */
1637                        gdouble                yresolution,    /* unused */
1638                        GimpUnit               resolution_unit,/* unused */
1639                        gpointer               user_data)
1640 {
1641   GimpDisplay *display = GIMP_DISPLAY (user_data);
1642 
1643   layer_scale_unit   = unit;
1644   layer_scale_interp = interpolation;
1645 
1646   if (width > 0 && height > 0)
1647     {
1648       GimpItem     *item = GIMP_ITEM (viewable);
1649       GimpProgress *progress;
1650       GtkWidget    *progress_dialog = NULL;
1651 
1652       gtk_widget_destroy (dialog);
1653 
1654       if (width  == gimp_item_get_width  (item) &&
1655           height == gimp_item_get_height (item))
1656         return;
1657 
1658       if (display)
1659         {
1660           progress = GIMP_PROGRESS (display);
1661         }
1662       else
1663         {
1664           progress_dialog = gimp_progress_dialog_new ();
1665           progress = GIMP_PROGRESS (progress_dialog);
1666         }
1667 
1668       progress = gimp_progress_start (progress, FALSE, _("Scaling"));
1669 
1670       gimp_item_scale_by_origin (item,
1671                                  width, height, interpolation,
1672                                  progress, TRUE);
1673 
1674       if (progress)
1675         gimp_progress_end (progress);
1676 
1677       if (progress_dialog)
1678         gtk_widget_destroy (progress_dialog);
1679 
1680       gimp_image_flush (gimp_item_get_image (item));
1681     }
1682   else
1683     {
1684       g_warning ("Scale Error: "
1685                  "Both width and height must be greater than zero.");
1686     }
1687 }
1688 
1689 static void
layers_resize_callback(GtkWidget * dialog,GimpViewable * viewable,GimpContext * context,gint width,gint height,GimpUnit unit,gint offset_x,gint offset_y,GimpFillType fill_type,GimpItemSet unused,gboolean unused2,gpointer user_data)1690 layers_resize_callback (GtkWidget    *dialog,
1691                         GimpViewable *viewable,
1692                         GimpContext  *context,
1693                         gint          width,
1694                         gint          height,
1695                         GimpUnit      unit,
1696                         gint          offset_x,
1697                         gint          offset_y,
1698                         GimpFillType  fill_type,
1699                         GimpItemSet   unused,
1700                         gboolean      unused2,
1701                         gpointer      user_data)
1702 {
1703   layer_resize_unit = unit;
1704 
1705   if (width > 0 && height > 0)
1706     {
1707       GimpItem         *item   = GIMP_ITEM (viewable);
1708       GimpImage        *image  = gimp_item_get_image (item);
1709       GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
1710 
1711       g_object_set (config,
1712                     "layer-resize-fill-type", fill_type,
1713                     NULL);
1714 
1715       gtk_widget_destroy (dialog);
1716 
1717       if (width  == gimp_item_get_width  (item) &&
1718           height == gimp_item_get_height (item))
1719         return;
1720 
1721       gimp_item_resize (item, context, fill_type,
1722                         width, height, offset_x, offset_y);
1723       gimp_image_flush (gimp_item_get_image (item));
1724     }
1725   else
1726     {
1727       g_warning ("Resize Error: "
1728                  "Both width and height must be greater than zero.");
1729     }
1730 }
1731 
1732 static gint
layers_mode_index(GimpLayerMode layer_mode,const GimpLayerMode * modes,gint n_modes)1733 layers_mode_index (GimpLayerMode         layer_mode,
1734                    const GimpLayerMode  *modes,
1735                    gint                  n_modes)
1736 {
1737   gint i = 0;
1738 
1739   while (i < (n_modes - 1) && modes[i] != layer_mode)
1740     i++;
1741 
1742   return i;
1743 }
1744