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 "libgimpbase/gimpbase.h"
26 #include "libgimpwidgets/gimpwidgets.h"
27
28 #include "actions-types.h"
29
30 #include "core/gimp.h"
31 #include "core/gimp-edit.h"
32 #include "core/gimpbuffer.h"
33 #include "core/gimpcontainer.h"
34 #include "core/gimpdrawable.h"
35 #include "core/gimpdrawable-edit.h"
36 #include "core/gimpfilloptions.h"
37 #include "core/gimplayer.h"
38 #include "core/gimplayer-new.h"
39 #include "core/gimpimage.h"
40 #include "core/gimpimage-undo.h"
41
42 #include "vectors/gimpvectors-import.h"
43
44 #include "widgets/gimpclipboard.h"
45 #include "widgets/gimphelp-ids.h"
46 #include "widgets/gimpdialogfactory.h"
47 #include "widgets/gimpmessagebox.h"
48 #include "widgets/gimpmessagedialog.h"
49 #include "widgets/gimpwidgets-utils.h"
50 #include "widgets/gimpwindowstrategy.h"
51
52 #include "display/gimpdisplay.h"
53 #include "display/gimpdisplayshell.h"
54 #include "display/gimpdisplayshell-transform.h"
55
56 #include "tools/gimptools-utils.h"
57 #include "tools/tool_manager.h"
58
59 #include "actions.h"
60 #include "edit-commands.h"
61
62 #include "gimp-intl.h"
63
64
65 /* local function prototypes */
66
67 static gboolean check_drawable_alpha (GimpDrawable *drawable,
68 gpointer data);
69 static void edit_paste (GimpDisplay *display,
70 GimpPasteType paste_type,
71 gboolean try_svg);
72 static void cut_named_buffer_callback (GtkWidget *widget,
73 const gchar *name,
74 gpointer data);
75 static void copy_named_buffer_callback (GtkWidget *widget,
76 const gchar *name,
77 gpointer data);
78 static void copy_named_visible_buffer_callback (GtkWidget *widget,
79 const gchar *name,
80 gpointer data);
81
82
83 /* public functions */
84
85 void
edit_undo_cmd_callback(GimpAction * action,GVariant * value,gpointer data)86 edit_undo_cmd_callback (GimpAction *action,
87 GVariant *value,
88 gpointer data)
89 {
90 GimpImage *image;
91 GimpDisplay *display;
92 return_if_no_image (image, data);
93 return_if_no_display (display, data);
94
95 if (tool_manager_undo_active (image->gimp, display) ||
96 gimp_image_undo (image))
97 {
98 gimp_image_flush (image);
99 }
100 }
101
102 void
edit_redo_cmd_callback(GimpAction * action,GVariant * value,gpointer data)103 edit_redo_cmd_callback (GimpAction *action,
104 GVariant *value,
105 gpointer data)
106 {
107 GimpImage *image;
108 GimpDisplay *display;
109 return_if_no_image (image, data);
110 return_if_no_display (display, data);
111
112 if (tool_manager_redo_active (image->gimp, display) ||
113 gimp_image_redo (image))
114 {
115 gimp_image_flush (image);
116 }
117 }
118
119 void
edit_strong_undo_cmd_callback(GimpAction * action,GVariant * value,gpointer data)120 edit_strong_undo_cmd_callback (GimpAction *action,
121 GVariant *value,
122 gpointer data)
123 {
124 GimpImage *image;
125 return_if_no_image (image, data);
126
127 if (gimp_image_strong_undo (image))
128 gimp_image_flush (image);
129 }
130
131 void
edit_strong_redo_cmd_callback(GimpAction * action,GVariant * value,gpointer data)132 edit_strong_redo_cmd_callback (GimpAction *action,
133 GVariant *value,
134 gpointer data)
135 {
136 GimpImage *image;
137 return_if_no_image (image, data);
138
139 if (gimp_image_strong_redo (image))
140 gimp_image_flush (image);
141 }
142
143 void
edit_undo_clear_cmd_callback(GimpAction * action,GVariant * value,gpointer data)144 edit_undo_clear_cmd_callback (GimpAction *action,
145 GVariant *value,
146 gpointer data)
147 {
148 GimpImage *image;
149 GimpUndoStack *undo_stack;
150 GimpUndoStack *redo_stack;
151 GtkWidget *widget;
152 GtkWidget *dialog;
153 gchar *size;
154 gint64 memsize;
155 gint64 guisize;
156 return_if_no_image (image, data);
157 return_if_no_widget (widget, data);
158
159 dialog = gimp_message_dialog_new (_("Clear Undo History"),
160 GIMP_ICON_DIALOG_WARNING,
161 widget,
162 GTK_DIALOG_MODAL |
163 GTK_DIALOG_DESTROY_WITH_PARENT,
164 gimp_standard_help_func,
165 GIMP_HELP_EDIT_UNDO_CLEAR,
166
167 _("_Cancel"), GTK_RESPONSE_CANCEL,
168 _("Cl_ear"), GTK_RESPONSE_OK,
169
170 NULL);
171
172 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
173 GTK_RESPONSE_OK,
174 GTK_RESPONSE_CANCEL,
175 -1);
176
177 g_signal_connect_object (gtk_widget_get_toplevel (widget), "unmap",
178 G_CALLBACK (gtk_widget_destroy),
179 dialog, G_CONNECT_SWAPPED);
180
181 g_signal_connect_object (image, "disconnect",
182 G_CALLBACK (gtk_widget_destroy),
183 dialog, G_CONNECT_SWAPPED);
184
185 gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
186 _("Really clear image's undo history?"));
187
188 undo_stack = gimp_image_get_undo_stack (image);
189 redo_stack = gimp_image_get_redo_stack (image);
190
191 memsize = gimp_object_get_memsize (GIMP_OBJECT (undo_stack), &guisize);
192 memsize += guisize;
193 memsize += gimp_object_get_memsize (GIMP_OBJECT (redo_stack), &guisize);
194 memsize += guisize;
195
196 size = g_format_size (memsize);
197
198 gimp_message_box_set_text (GIMP_MESSAGE_DIALOG (dialog)->box,
199 _("Clearing the undo history of this "
200 "image will gain %s of memory."), size);
201 g_free (size);
202
203 if (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK)
204 {
205 gimp_image_undo_disable (image);
206 gimp_image_undo_enable (image);
207 gimp_image_flush (image);
208 }
209
210 gtk_widget_destroy (dialog);
211 }
212
213 void
edit_cut_cmd_callback(GimpAction * action,GVariant * value,gpointer data)214 edit_cut_cmd_callback (GimpAction *action,
215 GVariant *value,
216 gpointer data)
217 {
218 GimpImage *image;
219 GimpDrawable *drawable;
220 GimpObject *cut;
221 GError *error = NULL;
222 return_if_no_drawable (image, drawable, data);
223
224 if (! check_drawable_alpha (drawable, data))
225 return;
226
227 cut = gimp_edit_cut (image, drawable, action_data_get_context (data),
228 &error);
229
230 if (cut)
231 {
232 GimpDisplay *display = action_data_get_display (data);
233
234 if (display)
235 gimp_message_literal (image->gimp,
236 G_OBJECT (display), GIMP_MESSAGE_INFO,
237 GIMP_IS_IMAGE (cut) ?
238 _("Cut layer to the clipboard.") :
239 _("Cut pixels to the clipboard."));
240
241 gimp_image_flush (image);
242 }
243 else
244 {
245 gimp_message_literal (image->gimp,
246 G_OBJECT (action_data_get_display (data)),
247 GIMP_MESSAGE_WARNING,
248 error->message);
249 g_clear_error (&error);
250 }
251 }
252
253 void
edit_copy_cmd_callback(GimpAction * action,GVariant * value,gpointer data)254 edit_copy_cmd_callback (GimpAction *action,
255 GVariant *value,
256 gpointer data)
257 {
258 GimpImage *image;
259 GimpDrawable *drawable;
260 GimpObject *copy;
261 GError *error = NULL;
262 return_if_no_drawable (image, drawable, data);
263
264 copy = gimp_edit_copy (image, drawable, action_data_get_context (data),
265 &error);
266
267 if (copy)
268 {
269 GimpDisplay *display = action_data_get_display (data);
270
271 if (display)
272 gimp_message_literal (image->gimp,
273 G_OBJECT (display), GIMP_MESSAGE_INFO,
274 GIMP_IS_IMAGE (copy) ?
275 _("Copied layer to the clipboard.") :
276 _("Copied pixels to the clipboard."));
277
278 gimp_image_flush (image);
279 }
280 else
281 {
282 gimp_message_literal (image->gimp,
283 G_OBJECT (action_data_get_display (data)),
284 GIMP_MESSAGE_WARNING,
285 error->message);
286 g_clear_error (&error);
287 }
288 }
289
290 void
edit_copy_visible_cmd_callback(GimpAction * action,GVariant * value,gpointer data)291 edit_copy_visible_cmd_callback (GimpAction *action,
292 GVariant *value,
293 gpointer data)
294 {
295 GimpImage *image;
296 GError *error = NULL;
297 return_if_no_image (image, data);
298
299 if (gimp_edit_copy_visible (image, action_data_get_context (data), &error))
300 {
301 GimpDisplay *display = action_data_get_display (data);
302
303 if (display)
304 gimp_message_literal (image->gimp,
305 G_OBJECT (display), GIMP_MESSAGE_INFO,
306 _("Copied pixels to the clipboard."));
307
308 gimp_image_flush (image);
309 }
310 else
311 {
312 gimp_message_literal (image->gimp,
313 G_OBJECT (action_data_get_display (data)),
314 GIMP_MESSAGE_WARNING,
315 error->message);
316 g_clear_error (&error);
317 }
318 }
319
320 void
edit_paste_cmd_callback(GimpAction * action,GVariant * value,gpointer data)321 edit_paste_cmd_callback (GimpAction *action,
322 GVariant *value,
323 gpointer data)
324 {
325 GimpDisplay *display = action_data_get_display (data);
326 GimpPasteType paste_type = (GimpPasteType) g_variant_get_int32 (value);
327
328 if (paste_type == GIMP_PASTE_TYPE_FLOATING)
329 {
330 if (! display || ! gimp_display_get_image (display))
331 {
332 edit_paste_as_new_image_cmd_callback (action, value, data);
333 return;
334 }
335 }
336
337 if (! display)
338 return;
339
340 switch (paste_type)
341 {
342 case GIMP_PASTE_TYPE_FLOATING:
343 case GIMP_PASTE_TYPE_FLOATING_IN_PLACE:
344 case GIMP_PASTE_TYPE_FLOATING_INTO:
345 case GIMP_PASTE_TYPE_FLOATING_INTO_IN_PLACE:
346 edit_paste (display, paste_type, TRUE);
347 break;
348
349 case GIMP_PASTE_TYPE_NEW_LAYER:
350 case GIMP_PASTE_TYPE_NEW_LAYER_IN_PLACE:
351 edit_paste (display, paste_type, FALSE);
352 break;
353 }
354 }
355
356 void
edit_paste_as_new_image_cmd_callback(GimpAction * action,GVariant * value,gpointer data)357 edit_paste_as_new_image_cmd_callback (GimpAction *action,
358 GVariant *value,
359 gpointer data)
360 {
361 Gimp *gimp;
362 GtkWidget *widget;
363 GimpObject *paste;
364 GimpImage *image = NULL;
365 return_if_no_gimp (gimp, data);
366 return_if_no_widget (widget, data);
367
368 paste = gimp_clipboard_get_object (gimp);
369
370 if (paste)
371 {
372 image = gimp_edit_paste_as_new_image (gimp, paste);
373 g_object_unref (paste);
374 }
375
376 if (image)
377 {
378 gimp_create_display (gimp, image, GIMP_UNIT_PIXEL, 1.0,
379 G_OBJECT (gtk_widget_get_screen (widget)),
380 gimp_widget_get_monitor (widget));
381 g_object_unref (image);
382 }
383 else
384 {
385 gimp_message_literal (gimp, NULL, GIMP_MESSAGE_WARNING,
386 _("There is no image data in the clipboard "
387 "to paste."));
388 }
389 }
390
391 void
edit_named_cut_cmd_callback(GimpAction * action,GVariant * value,gpointer data)392 edit_named_cut_cmd_callback (GimpAction *action,
393 GVariant *value,
394 gpointer data)
395 {
396 GimpImage *image;
397 GtkWidget *widget;
398 GtkWidget *dialog;
399 return_if_no_image (image, data);
400 return_if_no_widget (widget, data);
401
402 dialog = gimp_query_string_box (_("Cut Named"), widget,
403 gimp_standard_help_func,
404 GIMP_HELP_BUFFER_CUT,
405 _("Enter a name for this buffer"),
406 NULL,
407 G_OBJECT (image), "disconnect",
408 cut_named_buffer_callback, image);
409 gtk_widget_show (dialog);
410 }
411
412 void
edit_named_copy_cmd_callback(GimpAction * action,GVariant * value,gpointer data)413 edit_named_copy_cmd_callback (GimpAction *action,
414 GVariant *value,
415 gpointer data)
416 {
417 GimpImage *image;
418 GtkWidget *widget;
419 GtkWidget *dialog;
420 return_if_no_image (image, data);
421 return_if_no_widget (widget, data);
422
423 dialog = gimp_query_string_box (_("Copy Named"), widget,
424 gimp_standard_help_func,
425 GIMP_HELP_BUFFER_COPY,
426 _("Enter a name for this buffer"),
427 NULL,
428 G_OBJECT (image), "disconnect",
429 copy_named_buffer_callback, image);
430 gtk_widget_show (dialog);
431 }
432
433 void
edit_named_copy_visible_cmd_callback(GimpAction * action,GVariant * value,gpointer data)434 edit_named_copy_visible_cmd_callback (GimpAction *action,
435 GVariant *value,
436 gpointer data)
437 {
438 GimpImage *image;
439 GtkWidget *widget;
440 GtkWidget *dialog;
441 return_if_no_image (image, data);
442 return_if_no_widget (widget, data);
443
444 dialog = gimp_query_string_box (_("Copy Visible Named"), widget,
445 gimp_standard_help_func,
446 GIMP_HELP_BUFFER_COPY,
447 _("Enter a name for this buffer"),
448 NULL,
449 G_OBJECT (image), "disconnect",
450 copy_named_visible_buffer_callback, image);
451 gtk_widget_show (dialog);
452 }
453
454 void
edit_named_paste_cmd_callback(GimpAction * action,GVariant * value,gpointer data)455 edit_named_paste_cmd_callback (GimpAction *action,
456 GVariant *value,
457 gpointer data)
458 {
459 Gimp *gimp;
460 GtkWidget *widget;
461 return_if_no_gimp (gimp, data);
462 return_if_no_widget (widget, data);
463
464 gimp_window_strategy_show_dockable_dialog (GIMP_WINDOW_STRATEGY (gimp_get_window_strategy (gimp)),
465 gimp,
466 gimp_dialog_factory_get_singleton (),
467 gtk_widget_get_screen (widget),
468 gimp_widget_get_monitor (widget),
469 "gimp-buffer-list|gimp-buffer-grid");
470 }
471
472 void
edit_clear_cmd_callback(GimpAction * action,GVariant * value,gpointer data)473 edit_clear_cmd_callback (GimpAction *action,
474 GVariant *value,
475 gpointer data)
476 {
477 GimpImage *image;
478 GimpDrawable *drawable;
479 return_if_no_drawable (image, drawable, data);
480
481 if (! check_drawable_alpha (drawable, data))
482 return;
483
484 gimp_drawable_edit_clear (drawable, action_data_get_context (data));
485 gimp_image_flush (image);
486 }
487
488 void
edit_fill_cmd_callback(GimpAction * action,GVariant * value,gpointer data)489 edit_fill_cmd_callback (GimpAction *action,
490 GVariant *value,
491 gpointer data)
492 {
493 GimpImage *image;
494 GimpDrawable *drawable;
495 GimpFillType fill_type;
496 GimpFillOptions *options;
497 GError *error = NULL;
498 return_if_no_drawable (image, drawable, data);
499
500 fill_type = (GimpFillType) g_variant_get_int32 (value);
501
502 options = gimp_fill_options_new (action_data_get_gimp (data), NULL, FALSE);
503
504 if (gimp_fill_options_set_by_fill_type (options,
505 action_data_get_context (data),
506 fill_type, &error))
507 {
508 gimp_drawable_edit_fill (drawable, options, NULL);
509 gimp_image_flush (image);
510 }
511 else
512 {
513 gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_WARNING,
514 error->message);
515 g_clear_error (&error);
516 }
517
518 g_object_unref (options);
519 }
520
521
522 /* private functions */
523
524 static gboolean
check_drawable_alpha(GimpDrawable * drawable,gpointer data)525 check_drawable_alpha (GimpDrawable *drawable,
526 gpointer data)
527 {
528 if (gimp_drawable_has_alpha (drawable) &&
529 GIMP_IS_LAYER (drawable) &&
530 gimp_layer_get_lock_alpha (GIMP_LAYER (drawable)))
531 {
532 Gimp *gimp = action_data_get_gimp (data);
533 GimpDisplay *display = action_data_get_display (data);
534
535 if (gimp && display)
536 {
537 gimp_message_literal (
538 gimp, G_OBJECT (display), GIMP_MESSAGE_WARNING,
539 _("The active layer's alpha channel is locked."));
540
541 gimp_tools_blink_lock_box (gimp, GIMP_ITEM (drawable));
542 }
543
544 return FALSE;
545 }
546
547 return TRUE;
548 }
549
550 static void
edit_paste(GimpDisplay * display,GimpPasteType paste_type,gboolean try_svg)551 edit_paste (GimpDisplay *display,
552 GimpPasteType paste_type,
553 gboolean try_svg)
554 {
555 GimpImage *image = gimp_display_get_image (display);
556 GimpObject *paste;
557
558 if (try_svg)
559 {
560 gchar *svg;
561 gsize svg_size;
562
563 svg = gimp_clipboard_get_svg (display->gimp, &svg_size);
564
565 if (svg)
566 {
567 if (gimp_vectors_import_buffer (image, svg, svg_size,
568 TRUE, FALSE,
569 GIMP_IMAGE_ACTIVE_PARENT, -1,
570 NULL, NULL))
571 {
572 gimp_image_flush (image);
573 }
574
575 g_free (svg);
576
577 return;
578 }
579 }
580
581 paste = gimp_clipboard_get_object (display->gimp);
582
583 if (paste)
584 {
585 GimpDisplayShell *shell = gimp_display_get_shell (display);
586 GimpDrawable *drawable = gimp_image_get_active_drawable (image);
587 gint x, y;
588 gint width, height;
589
590 if (drawable &&
591 paste_type != GIMP_PASTE_TYPE_NEW_LAYER &&
592 paste_type != GIMP_PASTE_TYPE_NEW_LAYER_IN_PLACE)
593 {
594 if (gimp_viewable_get_children (GIMP_VIEWABLE (drawable)))
595 {
596 gimp_message_literal (display->gimp, G_OBJECT (display),
597 GIMP_MESSAGE_INFO,
598 _("Pasted as new layer because the "
599 "target is a layer group."));
600 }
601 else if (gimp_item_is_content_locked (GIMP_ITEM (drawable)))
602 {
603 gimp_message_literal (display->gimp, G_OBJECT (display),
604 GIMP_MESSAGE_INFO,
605 _("Pasted as new layer because the "
606 "target's pixels are locked."));
607 }
608
609 /* the actual paste-type conversion happens in gimp_edit_paste() */
610 }
611
612 gimp_display_shell_untransform_viewport (
613 shell,
614 ! gimp_display_shell_get_infinite_canvas (shell),
615 &x, &y, &width, &height);
616
617 if (gimp_edit_paste (image, drawable, paste,
618 paste_type, x, y, width, height))
619 {
620 gimp_image_flush (image);
621 }
622
623 g_object_unref (paste);
624 }
625 else
626 {
627 gimp_message_literal (display->gimp, G_OBJECT (display),
628 GIMP_MESSAGE_WARNING,
629 _("There is no image data in the clipboard "
630 "to paste."));
631 }
632 }
633
634 static void
cut_named_buffer_callback(GtkWidget * widget,const gchar * name,gpointer data)635 cut_named_buffer_callback (GtkWidget *widget,
636 const gchar *name,
637 gpointer data)
638 {
639 GimpImage *image = GIMP_IMAGE (data);
640 GimpDrawable *drawable = gimp_image_get_active_drawable (image);
641 GError *error = NULL;
642
643 if (! drawable)
644 {
645 gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_WARNING,
646 _("There is no active layer or channel to cut from."));
647 return;
648 }
649
650 if (! (name && strlen (name)))
651 name = _("(Unnamed Buffer)");
652
653 if (gimp_edit_named_cut (image, name, drawable,
654 gimp_get_user_context (image->gimp), &error))
655 {
656 gimp_image_flush (image);
657 }
658 else
659 {
660 gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_WARNING,
661 error->message);
662 g_clear_error (&error);
663 }
664 }
665
666 static void
copy_named_buffer_callback(GtkWidget * widget,const gchar * name,gpointer data)667 copy_named_buffer_callback (GtkWidget *widget,
668 const gchar *name,
669 gpointer data)
670 {
671 GimpImage *image = GIMP_IMAGE (data);
672 GimpDrawable *drawable = gimp_image_get_active_drawable (image);
673 GError *error = NULL;
674
675 if (! drawable)
676 {
677 gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_WARNING,
678 _("There is no active layer or channel to copy from."));
679 return;
680 }
681
682 if (! (name && strlen (name)))
683 name = _("(Unnamed Buffer)");
684
685 if (gimp_edit_named_copy (image, name, drawable,
686 gimp_get_user_context (image->gimp), &error))
687 {
688 gimp_image_flush (image);
689 }
690 else
691 {
692 gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_WARNING,
693 error->message);
694 g_clear_error (&error);
695 }
696 }
697
698 static void
copy_named_visible_buffer_callback(GtkWidget * widget,const gchar * name,gpointer data)699 copy_named_visible_buffer_callback (GtkWidget *widget,
700 const gchar *name,
701 gpointer data)
702 {
703 GimpImage *image = GIMP_IMAGE (data);
704 GError *error = NULL;
705
706 if (! (name && strlen (name)))
707 name = _("(Unnamed Buffer)");
708
709 if (gimp_edit_named_copy_visible (image, name,
710 gimp_get_user_context (image->gimp),
711 &error))
712 {
713 gimp_image_flush (image);
714 }
715 else
716 {
717 gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_WARNING,
718 error->message);
719 g_clear_error (&error);
720 }
721 }
722