summaryrefslogtreecommitdiffstats
path: root/app/tools/gimpfliptool.c
blob: 8714b6182b122147adcbb59b0650f6052d384460 (plain)
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
/* 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 "libgimpwidgets/gimpwidgets.h"

#include "tools-types.h"

#include "config/gimpdisplayconfig.h"

#include "core/gimpdrawable-transform.h"
#include "core/gimpguide.h"
#include "core/gimpimage.h"
#include "core/gimpimage-flip.h"
#include "core/gimpimage-pick-item.h"
#include "core/gimpitem-linked.h"
#include "core/gimplayer.h"
#include "core/gimplayermask.h"
#include "core/gimppickable.h"
#include "core/gimpprogress.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 "gimpflipoptions.h"
#include "gimpfliptool.h"
#include "gimptoolcontrol.h"

#include "gimp-intl.h"


/*  local function prototypes  */

static void         gimp_flip_tool_button_press  (GimpTool             *tool,
                                                  const GimpCoords     *coords,
                                                  guint32               time,
                                                  GdkModifierType       state,
                                                  GimpButtonPressType   press_type,
                                                  GimpDisplay          *display);
static void         gimp_flip_tool_modifier_key  (GimpTool             *tool,
                                                  GdkModifierType       key,
                                                  gboolean              press,
                                                  GdkModifierType       state,
                                                  GimpDisplay          *display);
static void         gimp_flip_tool_oper_update   (GimpTool             *tool,
                                                  const GimpCoords     *coords,
                                                  GdkModifierType       state,
                                                  gboolean              proximity,
                                                  GimpDisplay          *display);
static void         gimp_flip_tool_cursor_update (GimpTool             *tool,
                                                  const GimpCoords     *coords,
                                                  GdkModifierType       state,
                                                  GimpDisplay          *display);

static void         gimp_flip_tool_draw          (GimpDrawTool         *draw_tool);

static gchar      * gimp_flip_tool_get_undo_desc (GimpTransformTool    *tr_tool);
static GeglBuffer * gimp_flip_tool_transform     (GimpTransformTool    *tr_tool,
                                                  GimpObject           *object,
                                                  GeglBuffer           *orig_buffer,
                                                  gint                  orig_offset_x,
                                                  gint                  orig_offset_y,
                                                  GimpColorProfile    **buffer_profile,
                                                  gint                 *new_offset_x,
                                                  gint                 *new_offset_y);

static GimpOrientationType   gimp_flip_tool_get_flip_type  (GimpFlipTool      *flip);


G_DEFINE_TYPE (GimpFlipTool, gimp_flip_tool, GIMP_TYPE_TRANSFORM_TOOL)

#define parent_class gimp_flip_tool_parent_class


void
gimp_flip_tool_register (GimpToolRegisterCallback  callback,
                         gpointer                  data)
{
  (* callback) (GIMP_TYPE_FLIP_TOOL,
                GIMP_TYPE_FLIP_OPTIONS,
                gimp_flip_options_gui,
                GIMP_CONTEXT_PROP_MASK_BACKGROUND,
                "gimp-flip-tool",
                _("Flip"),
                _("Flip Tool: "
                  "Reverse the layer, selection or path horizontally or vertically"),
                N_("_Flip"), "<shift>F",
                NULL, GIMP_HELP_TOOL_FLIP,
                GIMP_ICON_TOOL_FLIP,
                data);
}

static void
gimp_flip_tool_class_init (GimpFlipToolClass *klass)
{
  GimpToolClass          *tool_class      = GIMP_TOOL_CLASS (klass);
  GimpDrawToolClass      *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
  GimpTransformToolClass *tr_class        = GIMP_TRANSFORM_TOOL_CLASS (klass);

  tool_class->button_press  = gimp_flip_tool_button_press;
  tool_class->modifier_key  = gimp_flip_tool_modifier_key;
  tool_class->oper_update   = gimp_flip_tool_oper_update;
  tool_class->cursor_update = gimp_flip_tool_cursor_update;

  draw_tool_class->draw     = gimp_flip_tool_draw;

  tr_class->get_undo_desc   = gimp_flip_tool_get_undo_desc;
  tr_class->transform       = gimp_flip_tool_transform;

  tr_class->undo_desc       = C_("undo-type", "Flip");
  tr_class->progress_text   = _("Flipping");
}

static void
gimp_flip_tool_init (GimpFlipTool *flip_tool)
{
  GimpTool *tool = GIMP_TOOL (flip_tool);

  gimp_tool_control_set_snap_to            (tool->control, FALSE);
  gimp_tool_control_set_precision          (tool->control,
                                            GIMP_CURSOR_PRECISION_PIXEL_CENTER);
  gimp_tool_control_set_cursor             (tool->control, GIMP_CURSOR_MOUSE);
  gimp_tool_control_set_toggle_cursor      (tool->control, GIMP_CURSOR_MOUSE);
  gimp_tool_control_set_tool_cursor        (tool->control,
                                            GIMP_TOOL_CURSOR_FLIP_HORIZONTAL);
  gimp_tool_control_set_toggle_tool_cursor (tool->control,
                                            GIMP_TOOL_CURSOR_FLIP_VERTICAL);

  flip_tool->guide = NULL;
}

static void
gimp_flip_tool_button_press (GimpTool            *tool,
                             const GimpCoords    *coords,
                             guint32              time,
                             GdkModifierType      state,
                             GimpButtonPressType  press_type,
                             GimpDisplay         *display)
{
  GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (tool);

  tool->display = display;

  gimp_transform_tool_transform (tr_tool, display);

  gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
}

static void
gimp_flip_tool_modifier_key (GimpTool        *tool,
                             GdkModifierType  key,
                             gboolean         press,
                             GdkModifierType  state,
                             GimpDisplay     *display)
{
  GimpFlipOptions *options = GIMP_FLIP_TOOL_GET_OPTIONS (tool);

  if (key == gimp_get_toggle_behavior_mask ())
    {
      switch (options->flip_type)
        {
        case GIMP_ORIENTATION_HORIZONTAL:
          g_object_set (options,
                        "flip-type", GIMP_ORIENTATION_VERTICAL,
                        NULL);
          break;

        case GIMP_ORIENTATION_VERTICAL:
          g_object_set (options,
                        "flip-type", GIMP_ORIENTATION_HORIZONTAL,
                        NULL);
          break;

        default:
          break;
        }
    }
}

static void
gimp_flip_tool_oper_update (GimpTool         *tool,
                            const GimpCoords *coords,
                            GdkModifierType   state,
                            gboolean          proximity,
                            GimpDisplay      *display)
{
  GimpFlipTool     *flip      = GIMP_FLIP_TOOL (tool);
  GimpDrawTool     *draw_tool = GIMP_DRAW_TOOL (tool);
  GimpFlipOptions  *options   = GIMP_FLIP_TOOL_GET_OPTIONS (tool);
  GimpDisplayShell *shell     = gimp_display_get_shell (display);
  GimpImage        *image     = gimp_display_get_image (display);
  GimpGuide        *guide     = NULL;

  if (gimp_display_shell_get_show_guides (shell) &&
      proximity)
    {
      gint snap_distance = display->config->snap_distance;

      guide = gimp_image_pick_guide (image, coords->x, coords->y,
                                     FUNSCALEX (shell, snap_distance),
                                     FUNSCALEY (shell, snap_distance));
    }

  if (flip->guide != guide ||
      (guide && ! gimp_draw_tool_is_active (draw_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);

      flip->guide = guide;

      if (! gimp_draw_tool_is_active (draw_tool))
        gimp_draw_tool_start (draw_tool, display);

      gimp_draw_tool_resume (draw_tool);
    }

  gtk_widget_set_sensitive (options->direction_frame, guide == NULL);

  GIMP_TOOL_CLASS (parent_class)->oper_update (tool,
                                               coords, state, proximity,
                                               display);
}

static void
gimp_flip_tool_cursor_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              GimpDisplay      *display)
{
  GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (tool);
  GimpFlipTool      *flip    = GIMP_FLIP_TOOL (tool);

  if (! gimp_transform_tool_check_active_object (tr_tool, display, NULL))
    {
      gimp_tool_set_cursor (tool, display,
                            gimp_tool_control_get_cursor (tool->control),
                            gimp_tool_control_get_tool_cursor (tool->control),
                            GIMP_CURSOR_MODIFIER_BAD);
      return;
    }

  gimp_tool_control_set_toggled (tool->control,
                                 gimp_flip_tool_get_flip_type (flip) ==
                                 GIMP_ORIENTATION_VERTICAL);

  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
}

static void
gimp_flip_tool_draw (GimpDrawTool *draw_tool)
{
  GimpFlipTool *flip = GIMP_FLIP_TOOL (draw_tool);

  if (flip->guide)
    {
      GimpCanvasItem *item;
      GimpGuideStyle  style;

      style = gimp_guide_get_style (flip->guide);

      item = gimp_draw_tool_add_guide (draw_tool,
                                       gimp_guide_get_orientation (flip->guide),
                                       gimp_guide_get_position (flip->guide),
                                       style);
      gimp_canvas_item_set_highlight (item, TRUE);
    }
}

static gchar *
gimp_flip_tool_get_undo_desc (GimpTransformTool *tr_tool)
{
  GimpFlipTool *flip = GIMP_FLIP_TOOL (tr_tool);

  switch (gimp_flip_tool_get_flip_type (flip))
    {
    case GIMP_ORIENTATION_HORIZONTAL:
      return g_strdup (C_("undo-type", "Flip horizontally"));

    case GIMP_ORIENTATION_VERTICAL:
      return g_strdup (C_("undo-type", "Flip vertically"));

    default:
      /* probably this is not actually reached today, but
       * could be if someone defined FLIP_DIAGONAL, say...
       */
      return GIMP_TRANSFORM_TOOL_CLASS (parent_class)->get_undo_desc (tr_tool);
    }
}

static GeglBuffer *
gimp_flip_tool_transform (GimpTransformTool *tr_tool,
                          GimpObject        *object,
                          GeglBuffer        *orig_buffer,
                          gint               orig_offset_x,
                          gint               orig_offset_y,
                          GimpColorProfile **buffer_profile,
                          gint              *new_offset_x,
                          gint              *new_offset_y)
{
  GimpFlipTool         *flip        = GIMP_FLIP_TOOL (tr_tool);
  GimpFlipOptions      *options     = GIMP_FLIP_TOOL_GET_OPTIONS (tr_tool);
  GimpTransformOptions *tr_options  = GIMP_TRANSFORM_TOOL_GET_OPTIONS (tr_tool);
  GimpContext          *context     = GIMP_CONTEXT (options);
  GimpOrientationType   flip_type   = GIMP_ORIENTATION_UNKNOWN;
  gdouble               axis        = 0.0;
  gboolean              clip_result = FALSE;
  GeglBuffer           *ret         = NULL;

  flip_type = gimp_flip_tool_get_flip_type (flip);

  if (flip->guide)
    {
      axis = gimp_guide_get_position (flip->guide);
    }
  else
    {
      switch (flip_type)
        {
        case GIMP_ORIENTATION_HORIZONTAL:
          axis = ((gdouble) tr_tool->x1 +
                  (gdouble) (tr_tool->x2 - tr_tool->x1) / 2.0);
          break;

        case GIMP_ORIENTATION_VERTICAL:
          axis = ((gdouble) tr_tool->y1 +
                  (gdouble) (tr_tool->y2 - tr_tool->y1) / 2.0);
          break;

        default:
          break;
        }
    }

  switch (tr_options->clip)
    {
    case GIMP_TRANSFORM_RESIZE_ADJUST:
      clip_result = FALSE;
      break;

    case GIMP_TRANSFORM_RESIZE_CLIP:
      clip_result = TRUE;
      break;

    default:
      g_return_val_if_reached (NULL);
    }

  if (orig_buffer)
    {
      /*  this happens when transforming a selection cut out of a
       *  normal drawable
       */

      g_return_val_if_fail (GIMP_IS_DRAWABLE (object), NULL);

      ret = gimp_drawable_transform_buffer_flip (GIMP_DRAWABLE (object),
                                                 context,
                                                 orig_buffer,
                                                 orig_offset_x,
                                                 orig_offset_y,
                                                 flip_type, axis,
                                                 clip_result,
                                                 buffer_profile,
                                                 new_offset_x,
                                                 new_offset_y);
    }
  else if (GIMP_IS_ITEM (object))
    {
      /*  this happens for entire drawables, paths and layer groups  */

      GimpItem *item = GIMP_ITEM (object);

      if (gimp_item_get_linked (item))
        {
          gimp_item_linked_flip (item, context,
                                 flip_type, axis, clip_result);
        }
      else
        {
          clip_result = gimp_item_get_clip (item, clip_result);

          gimp_item_flip (item, context,
                          flip_type, axis, clip_result);
        }
    }
  else
    {
      /*  this happens for images  */
      GimpTransformToolClass *tr_class = GIMP_TRANSFORM_TOOL_GET_CLASS (tr_tool);
      GimpProgress           *progress;

      g_return_val_if_fail (GIMP_IS_IMAGE (object), NULL);

      progress = gimp_progress_start (GIMP_PROGRESS (tr_tool), FALSE,
                                      "%s", tr_class->progress_text);

      gimp_image_flip_full (GIMP_IMAGE (object), context,
                            flip_type, axis, clip_result,
                            progress);

      if (progress)
        gimp_progress_end (progress);
    }

  return ret;
}

static GimpOrientationType
gimp_flip_tool_get_flip_type (GimpFlipTool *flip)
{
  GimpFlipOptions *options = GIMP_FLIP_TOOL_GET_OPTIONS (flip);

  if (flip->guide)
    {
      switch (gimp_guide_get_orientation (flip->guide))
        {
        case GIMP_ORIENTATION_HORIZONTAL:
          return GIMP_ORIENTATION_VERTICAL;

        case GIMP_ORIENTATION_VERTICAL:
          return GIMP_ORIENTATION_HORIZONTAL;

        default:
          return gimp_guide_get_orientation (flip->guide);
        }
    }
  else
    {
      return options->flip_type;
    }
}