1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "tools-types.h"
#include "config/gimpdisplayconfig.h"
#include "config/gimpguiconfig.h"
#include "core/gimp.h"
#include "core/gimp-cairo.h"
#include "core/gimp-utils.h"
#include "core/gimpguide.h"
#include "core/gimpimage.h"
#include "core/gimpimage-pick-item.h"
#include "core/gimplayer.h"
#include "core/gimpimage-undo.h"
#include "core/gimplayermask.h"
#include "core/gimplayer-floating-selection.h"
#include "core/gimpundostack.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpwidgets-utils.h"
#include "display/gimpcanvasitem.h"
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-appearance.h"
#include "display/gimpdisplayshell-selection.h"
#include "display/gimpdisplayshell-transform.h"
#include "gimpeditselectiontool.h"
#include "gimpguidetool.h"
#include "gimpmoveoptions.h"
#include "gimpmovetool.h"
#include "gimptoolcontrol.h"
#include "gimptools-utils.h"
#include "gimp-intl.h"
/* local function prototypes */
static void gimp_move_tool_finalize (GObject *object);
static void gimp_move_tool_button_press (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonPressType press_type,
GimpDisplay *display);
static void gimp_move_tool_button_release (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonReleaseType release_type,
GimpDisplay *display);
static gboolean gimp_move_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display);
static void gimp_move_tool_modifier_key (GimpTool *tool,
GdkModifierType key,
gboolean press,
GdkModifierType state,
GimpDisplay *display);
static void gimp_move_tool_oper_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity,
GimpDisplay *display);
static void gimp_move_tool_cursor_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display);
static void gimp_move_tool_draw (GimpDrawTool *draw_tool);
G_DEFINE_TYPE (GimpMoveTool, gimp_move_tool, GIMP_TYPE_DRAW_TOOL)
#define parent_class gimp_move_tool_parent_class
void
gimp_move_tool_register (GimpToolRegisterCallback callback,
gpointer data)
{
(* callback) (GIMP_TYPE_MOVE_TOOL,
GIMP_TYPE_MOVE_OPTIONS,
gimp_move_options_gui,
0,
"gimp-move-tool",
C_("tool", "Move"),
_("Move Tool: Move layers, selections, and other objects"),
N_("_Move"), "M",
NULL, GIMP_HELP_TOOL_MOVE,
GIMP_ICON_TOOL_MOVE,
data);
}
static void
gimp_move_tool_class_init (GimpMoveToolClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
object_class->finalize = gimp_move_tool_finalize;
tool_class->button_press = gimp_move_tool_button_press;
tool_class->button_release = gimp_move_tool_button_release;
tool_class->key_press = gimp_move_tool_key_press;
tool_class->modifier_key = gimp_move_tool_modifier_key;
tool_class->oper_update = gimp_move_tool_oper_update;
tool_class->cursor_update = gimp_move_tool_cursor_update;
draw_tool_class->draw = gimp_move_tool_draw;
}
static void
gimp_move_tool_init (GimpMoveTool *move_tool)
{
GimpTool *tool = GIMP_TOOL (move_tool);
gimp_tool_control_set_snap_to (tool->control, FALSE);
gimp_tool_control_set_handle_empty_image (tool->control, TRUE);
gimp_tool_control_set_tool_cursor (tool->control,
GIMP_TOOL_CURSOR_MOVE);
move_tool->floating_layer = NULL;
move_tool->guides = NULL;
move_tool->saved_type = GIMP_TRANSFORM_TYPE_LAYER;
move_tool->old_active_layer = NULL;
move_tool->old_active_vectors = NULL;
}
static void
gimp_move_tool_finalize (GObject *object)
{
GimpMoveTool *move = GIMP_MOVE_TOOL (object);
g_clear_pointer (&move->guides, g_list_free);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_move_tool_button_press (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonPressType press_type,
GimpDisplay *display)
{
GimpMoveTool *move = GIMP_MOVE_TOOL (tool);
GimpMoveOptions *options = GIMP_MOVE_TOOL_GET_OPTIONS (tool);
GimpDisplayShell *shell = gimp_display_get_shell (display);
GimpImage *image = gimp_display_get_image (display);
GimpItem *active_item = NULL;
GimpTranslateMode translate_mode = GIMP_TRANSLATE_MODE_MASK;
const gchar *null_message = NULL;
const gchar *locked_message = NULL;
tool->display = display;
move->floating_layer = NULL;
g_clear_pointer (&move->guides, g_list_free);
if (! options->move_current)
{
const gint snap_distance = display->config->snap_distance;
if (options->move_type == GIMP_TRANSFORM_TYPE_PATH)
{
GimpVectors *vectors;
vectors = gimp_image_pick_vectors (image,
coords->x, coords->y,
FUNSCALEX (shell, snap_distance),
FUNSCALEY (shell, snap_distance));
if (vectors)
{
move->old_active_vectors =
gimp_image_get_active_vectors (image);
gimp_image_set_active_vectors (image, vectors);
}
else
{
/* no path picked */
return;
}
}
else if (options->move_type == GIMP_TRANSFORM_TYPE_LAYER)
{
GList *guides;
GimpLayer *layer;
if (gimp_display_shell_get_show_guides (shell) &&
(guides = gimp_image_pick_guides (image,
coords->x, coords->y,
FUNSCALEX (shell, snap_distance),
FUNSCALEY (shell, snap_distance))))
{
move->guides = guides;
gimp_guide_tool_start_edit_many (tool, display, guides);
return;
}
else if ((layer = gimp_image_pick_layer (image,
coords->x,
coords->y,
NULL)))
{
if (gimp_image_get_floating_selection (image) &&
! gimp_layer_is_floating_sel (layer))
{
/* If there is a floating selection, and this aint it,
* use the move tool to anchor it.
*/
move->floating_layer =
gimp_image_get_floating_selection (image);
gimp_tool_control_activate (tool->control);
return;
}
else
{
move->old_active_layer = gimp_image_get_active_layer (image);
gimp_image_set_active_layer (image, layer);
}
}
else
{
/* no guide and no layer picked */
return;
}
}
}
switch (options->move_type)
{
case GIMP_TRANSFORM_TYPE_PATH:
{
active_item = GIMP_ITEM (gimp_image_get_active_vectors (image));
translate_mode = GIMP_TRANSLATE_MODE_VECTORS;
if (! active_item)
{
null_message = _("There is no path to move.");
}
else if (gimp_item_is_position_locked (active_item))
{
locked_message = _("The active path's position is locked.");
}
}
break;
case GIMP_TRANSFORM_TYPE_SELECTION:
{
active_item = GIMP_ITEM (gimp_image_get_mask (image));
if (gimp_channel_is_empty (GIMP_CHANNEL (active_item)))
active_item = NULL;
translate_mode = GIMP_TRANSLATE_MODE_MASK;
if (! active_item)
{
/* cannot happen, don't translate this message */
null_message = "There is no selection to move.";
}
else if (gimp_item_is_position_locked (active_item))
{
locked_message = "The selection's position is locked.";
}
}
break;
case GIMP_TRANSFORM_TYPE_LAYER:
{
active_item = GIMP_ITEM (gimp_image_get_active_drawable (image));
if (! active_item)
{
null_message = _("There is no layer to move.");
}
else if (GIMP_IS_LAYER_MASK (active_item))
{
translate_mode = GIMP_TRANSLATE_MODE_LAYER_MASK;
if (gimp_item_is_position_locked (active_item))
locked_message = _("The active layer's position is locked.");
else if (gimp_item_is_content_locked (active_item))
locked_message = _("The active layer's pixels are locked.");
}
else if (GIMP_IS_CHANNEL (active_item))
{
translate_mode = GIMP_TRANSLATE_MODE_CHANNEL;
if (gimp_item_is_position_locked (active_item))
locked_message = _("The active channel's position is locked.");
else if (gimp_item_is_content_locked (active_item))
locked_message = _("The active channel's pixels are locked.");
}
else
{
translate_mode = GIMP_TRANSLATE_MODE_LAYER;
if (gimp_item_is_position_locked (active_item))
locked_message = _("The active layer's position is locked.");
}
}
break;
case GIMP_TRANSFORM_TYPE_IMAGE:
g_return_if_reached ();
}
if (! active_item)
{
gimp_tool_message_literal (tool, display, null_message);
gimp_widget_blink (options->type_box);
gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
return;
}
else if (locked_message)
{
gimp_tool_message_literal (tool, display, locked_message);
gimp_tools_blink_lock_box (display->gimp, active_item);
gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
return;
}
gimp_tool_control_activate (tool->control);
gimp_edit_selection_tool_start (tool, display, coords,
translate_mode,
TRUE);
}
static void
gimp_move_tool_button_release (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonReleaseType release_type,
GimpDisplay *display)
{
GimpMoveTool *move = GIMP_MOVE_TOOL (tool);
GimpGuiConfig *config = GIMP_GUI_CONFIG (display->gimp->config);
GimpImage *image = gimp_display_get_image (display);
gboolean flush = FALSE;
gimp_tool_control_halt (tool->control);
if (! config->move_tool_changes_active ||
(release_type == GIMP_BUTTON_RELEASE_CANCEL))
{
if (move->old_active_layer)
{
gimp_image_set_active_layer (image, move->old_active_layer);
move->old_active_layer = NULL;
flush = TRUE;
}
if (move->old_active_vectors)
{
gimp_image_set_active_vectors (image, move->old_active_vectors);
move->old_active_vectors = NULL;
flush = TRUE;
}
}
if (release_type != GIMP_BUTTON_RELEASE_CANCEL)
{
if (move->floating_layer)
{
floating_sel_anchor (move->floating_layer);
flush = TRUE;
}
}
if (flush)
gimp_image_flush (image);
}
static gboolean
gimp_move_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display)
{
GimpMoveOptions *options = GIMP_MOVE_TOOL_GET_OPTIONS (tool);
return gimp_edit_selection_tool_translate (tool, kevent,
options->move_type,
display,
options->type_box);
}
static void
gimp_move_tool_modifier_key (GimpTool *tool,
GdkModifierType key,
gboolean press,
GdkModifierType state,
GimpDisplay *display)
{
GimpMoveTool *move = GIMP_MOVE_TOOL (tool);
GimpMoveOptions *options = GIMP_MOVE_TOOL_GET_OPTIONS (tool);
if (key == gimp_get_extend_selection_mask ())
{
g_object_set (options, "move-current", ! options->move_current, NULL);
}
else if (key == GDK_MOD1_MASK ||
key == gimp_get_toggle_behavior_mask ())
{
GimpTransformType button_type;
button_type = options->move_type;
if (press)
{
if (key == (state & (GDK_MOD1_MASK |
gimp_get_toggle_behavior_mask ())))
{
/* first modifier pressed */
move->saved_type = options->move_type;
}
}
else
{
if (! (state & (GDK_MOD1_MASK |
gimp_get_toggle_behavior_mask ())))
{
/* last modifier released */
button_type = move->saved_type;
}
}
if (state & GDK_MOD1_MASK)
{
button_type = GIMP_TRANSFORM_TYPE_SELECTION;
}
else if (state & gimp_get_toggle_behavior_mask ())
{
button_type = GIMP_TRANSFORM_TYPE_PATH;
}
if (button_type != options->move_type)
{
g_object_set (options, "move-type", button_type, NULL);
}
}
}
static void
gimp_move_tool_oper_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity,
GimpDisplay *display)
{
GimpMoveTool *move = GIMP_MOVE_TOOL (tool);
GimpMoveOptions *options = GIMP_MOVE_TOOL_GET_OPTIONS (tool);
GimpDisplayShell *shell = gimp_display_get_shell (display);
GimpImage *image = gimp_display_get_image (display);
GList *guides = NULL;
if (options->move_type == GIMP_TRANSFORM_TYPE_LAYER &&
! options->move_current &&
gimp_display_shell_get_show_guides (shell) &&
proximity)
{
gint snap_distance = display->config->snap_distance;
guides = gimp_image_pick_guides (image, coords->x, coords->y,
FUNSCALEX (shell, snap_distance),
FUNSCALEY (shell, snap_distance));
}
if (gimp_g_list_compare (guides, move->guides))
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
gimp_draw_tool_pause (draw_tool);
if (gimp_draw_tool_is_active (draw_tool) &&
draw_tool->display != display)
gimp_draw_tool_stop (draw_tool);
g_clear_pointer (&move->guides, g_list_free);
move->guides = guides;
if (! gimp_draw_tool_is_active (draw_tool))
gimp_draw_tool_start (draw_tool, display);
gimp_draw_tool_resume (draw_tool);
}
else
{
g_list_free (guides);
}
}
static void
gimp_move_tool_cursor_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display)
{
GimpMoveOptions *options = GIMP_MOVE_TOOL_GET_OPTIONS (tool);
GimpDisplayShell *shell = gimp_display_get_shell (display);
GimpImage *image = gimp_display_get_image (display);
GimpCursorType cursor = GIMP_CURSOR_MOUSE;
GimpToolCursorType tool_cursor = GIMP_TOOL_CURSOR_MOVE;
GimpCursorModifier modifier = GIMP_CURSOR_MODIFIER_NONE;
gint snap_distance = display->config->snap_distance;
if (options->move_type == GIMP_TRANSFORM_TYPE_PATH)
{
tool_cursor = GIMP_TOOL_CURSOR_PATHS;
modifier = GIMP_CURSOR_MODIFIER_MOVE;
if (options->move_current)
{
GimpItem *item = GIMP_ITEM (gimp_image_get_active_vectors (image));
if (! item || gimp_item_is_position_locked (item))
modifier = GIMP_CURSOR_MODIFIER_BAD;
}
else
{
if (gimp_image_pick_vectors (image,
coords->x, coords->y,
FUNSCALEX (shell, snap_distance),
FUNSCALEY (shell, snap_distance)))
{
tool_cursor = GIMP_TOOL_CURSOR_HAND;
}
else
{
modifier = GIMP_CURSOR_MODIFIER_BAD;
}
}
}
else if (options->move_type == GIMP_TRANSFORM_TYPE_SELECTION)
{
tool_cursor = GIMP_TOOL_CURSOR_RECT_SELECT;
modifier = GIMP_CURSOR_MODIFIER_MOVE;
if (gimp_channel_is_empty (gimp_image_get_mask (image)))
modifier = GIMP_CURSOR_MODIFIER_BAD;
}
else if (options->move_current)
{
GimpItem *item = GIMP_ITEM (gimp_image_get_active_drawable (image));
if (! item || gimp_item_is_position_locked (item))
modifier = GIMP_CURSOR_MODIFIER_BAD;
}
else
{
GimpLayer *layer;
if (gimp_display_shell_get_show_guides (shell) &&
gimp_image_pick_guide (image, coords->x, coords->y,
FUNSCALEX (shell, snap_distance),
FUNSCALEY (shell, snap_distance)))
{
tool_cursor = GIMP_TOOL_CURSOR_HAND;
modifier = GIMP_CURSOR_MODIFIER_MOVE;
}
else if ((layer = gimp_image_pick_layer (image,
coords->x, coords->y,
NULL)))
{
/* if there is a floating selection, and this aint it... */
if (gimp_image_get_floating_selection (image) &&
! gimp_layer_is_floating_sel (layer))
{
tool_cursor = GIMP_TOOL_CURSOR_MOVE;
modifier = GIMP_CURSOR_MODIFIER_ANCHOR;
}
else if (gimp_item_is_position_locked (GIMP_ITEM (layer)))
{
modifier = GIMP_CURSOR_MODIFIER_BAD;
}
else if (layer != gimp_image_get_active_layer (image))
{
tool_cursor = GIMP_TOOL_CURSOR_HAND;
modifier = GIMP_CURSOR_MODIFIER_MOVE;
}
}
else
{
modifier = GIMP_CURSOR_MODIFIER_BAD;
}
}
gimp_tool_control_set_cursor (tool->control, cursor);
gimp_tool_control_set_tool_cursor (tool->control, tool_cursor);
gimp_tool_control_set_cursor_modifier (tool->control, modifier);
GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
}
static void
gimp_move_tool_draw (GimpDrawTool *draw_tool)
{
GimpMoveTool *move = GIMP_MOVE_TOOL (draw_tool);
GList *iter;
for (iter = move->guides; iter; iter = g_list_next (iter))
{
GimpGuide *guide = iter->data;
GimpCanvasItem *item;
GimpGuideStyle style;
style = gimp_guide_get_style (guide);
item = gimp_draw_tool_add_guide (draw_tool,
gimp_guide_get_orientation (guide),
gimp_guide_get_position (guide),
style);
gimp_canvas_item_set_highlight (item, TRUE);
}
}
|