1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
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 2 of the License, or
7
* (at your option) any later version.
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.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include "libgimpwidgets/gimpwidgets.h"
25
#include "tools-types.h"
27
#include "base/boundary.h"
29
#include "core/gimpchannel.h"
30
#include "core/gimpchannel-select.h"
31
#include "core/gimplayer-floating-sel.h"
32
#include "core/gimpimage.h"
33
#include "core/gimpimage-undo.h"
34
#include "core/gimppickable.h"
35
#include "core/gimp-utils.h"
36
#include "core/gimpundostack.h"
38
#include "widgets/gimpdialogfactory.h"
39
#include "widgets/gimphelp-ids.h"
40
#include "widgets/gimpviewabledialog.h"
41
#include "widgets/gimpwidgets-utils.h"
43
#include "display/gimpdisplay.h"
44
#include "display/gimpdisplayshell.h"
45
#include "display/gimpdisplayshell-transform.h"
46
#include "display/gimpdisplayshell-appearance.h"
48
#include "gimpselectiontool.h"
49
#include "gimpselectionoptions.h"
50
#include "gimprectangletool.h"
51
#include "gimprectangleoptions.h"
52
#include "gimprectangleselecttool.h"
53
#include "gimprectangleselectoptions.h"
54
#include "gimptoolcontrol.h"
56
#include "gimp-intl.h"
59
static void gimp_rect_select_tool_rectangle_tool_iface_init (GimpRectangleToolInterface *iface);
61
static GObject *gimp_rect_select_tool_constructor (GType type,
63
GObjectConstructParam *params);
64
static void gimp_rect_select_tool_control (GimpTool *tool,
65
GimpToolAction action,
66
GimpDisplay *display);
67
static void gimp_rect_select_tool_button_press (GimpTool *tool,
70
GdkModifierType state,
71
GimpDisplay *display);
72
static void gimp_rect_select_tool_button_release (GimpTool *tool,
75
GdkModifierType state,
76
GimpButtonReleaseType release_type,
77
GimpDisplay *display);
78
static void gimp_rect_select_tool_active_modifier_key (GimpTool *tool,
81
GdkModifierType state,
82
GimpDisplay *display);
83
static gboolean gimp_rect_select_tool_key_press (GimpTool *tool,
85
GimpDisplay *display);
86
static void gimp_rect_select_tool_oper_update (GimpTool *tool,
88
GdkModifierType state,
90
GimpDisplay *display);
91
static void gimp_rect_select_tool_cursor_update (GimpTool *tool,
93
GdkModifierType state,
94
GimpDisplay *display);
95
static void gimp_rect_select_tool_draw (GimpDrawTool *draw_tool);
96
static gboolean gimp_rect_select_tool_select (GimpRectangleTool *rect_tool,
101
static gboolean gimp_rect_select_tool_execute (GimpRectangleTool *rect_tool,
106
static void gimp_rect_select_tool_cancel (GimpRectangleTool *rect_tool);
107
static gboolean gimp_rect_select_tool_rectangle_changed (GimpRectangleTool *rect_tool);
108
static void gimp_rect_select_tool_real_select (GimpRectSelectTool *rect_select,
109
GimpChannelOps operation,
115
static void gimp_rect_select_tool_round_corners_notify (GimpRectSelectOptions *options,
117
GimpRectSelectTool *rect_sel);
120
G_DEFINE_TYPE_WITH_CODE (GimpRectSelectTool, gimp_rect_select_tool,
121
GIMP_TYPE_SELECTION_TOOL,
122
G_IMPLEMENT_INTERFACE (GIMP_TYPE_RECTANGLE_TOOL,
123
gimp_rect_select_tool_rectangle_tool_iface_init))
125
#define parent_class gimp_rect_select_tool_parent_class
129
gimp_rect_select_tool_register (GimpToolRegisterCallback callback,
132
(* callback) (GIMP_TYPE_RECT_SELECT_TOOL,
133
GIMP_TYPE_RECT_SELECT_OPTIONS,
134
gimp_rect_select_options_gui,
136
"gimp-rect-select-tool",
137
_("Rectangle Select"),
138
_("Rectangle Select Tool: Select a rectangular region"),
139
N_("_Rectangle Select"), "R",
140
NULL, GIMP_HELP_TOOL_RECT_SELECT,
141
GIMP_STOCK_TOOL_RECT_SELECT,
146
gimp_rect_select_tool_class_init (GimpRectSelectToolClass *klass)
148
GObjectClass *object_class = G_OBJECT_CLASS (klass);
149
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
150
GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
152
object_class->constructor = gimp_rect_select_tool_constructor;
153
object_class->set_property = gimp_rectangle_tool_set_property;
154
object_class->get_property = gimp_rectangle_tool_get_property;
156
gimp_rectangle_tool_install_properties (object_class);
158
tool_class->control = gimp_rect_select_tool_control;
159
tool_class->button_press = gimp_rect_select_tool_button_press;
160
tool_class->button_release = gimp_rect_select_tool_button_release;
161
tool_class->motion = gimp_rectangle_tool_motion;
162
tool_class->key_press = gimp_rect_select_tool_key_press;
163
tool_class->active_modifier_key = gimp_rect_select_tool_active_modifier_key;
164
tool_class->oper_update = gimp_rect_select_tool_oper_update;
165
tool_class->cursor_update = gimp_rect_select_tool_cursor_update;
167
draw_tool_class->draw = gimp_rect_select_tool_draw;
169
klass->select = gimp_rect_select_tool_real_select;
173
gimp_rect_select_tool_rectangle_tool_iface_init (GimpRectangleToolInterface *iface)
175
iface->execute = gimp_rect_select_tool_execute;
176
iface->cancel = gimp_rect_select_tool_cancel;
177
iface->rectangle_changed = gimp_rect_select_tool_rectangle_changed;
181
gimp_rect_select_tool_init (GimpRectSelectTool *rect_select)
183
GimpTool *tool = GIMP_TOOL (rect_select);
185
gimp_tool_control_set_wants_click (tool->control, TRUE);
186
gimp_tool_control_set_tool_cursor (tool->control,
187
GIMP_TOOL_CURSOR_RECT_SELECT);
188
gimp_tool_control_set_dirty_mask (tool->control,
189
GIMP_DIRTY_IMAGE_SIZE |
190
GIMP_DIRTY_SELECTION);
192
rect_select->undo = NULL;
193
rect_select->redo = NULL;
197
gimp_rect_select_tool_constructor (GType type,
199
GObjectConstructParam *params)
202
GimpRectSelectTool *rect_sel;
203
GimpRectSelectOptions *options;
205
object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
207
gimp_rectangle_tool_constructor (object);
209
rect_sel = GIMP_RECT_SELECT_TOOL (object);
210
options = GIMP_RECT_SELECT_TOOL_GET_OPTIONS (rect_sel);
212
rect_sel->round_corners = options->round_corners;
213
rect_sel->corner_radius = options->corner_radius;
215
g_signal_connect_object (options, "notify::round-corners",
216
G_CALLBACK (gimp_rect_select_tool_round_corners_notify),
218
g_signal_connect_object (options, "notify::corner-radius",
219
G_CALLBACK (gimp_rect_select_tool_round_corners_notify),
226
gimp_rect_select_tool_control (GimpTool *tool,
227
GimpToolAction action,
228
GimpDisplay *display)
230
gimp_rectangle_tool_control (tool, action, display);
232
GIMP_TOOL_CLASS (parent_class)->control (tool, action, display);
236
gimp_rect_select_tool_draw (GimpDrawTool *draw_tool)
238
GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (draw_tool);
240
gimp_rectangle_tool_draw (draw_tool);
242
if (rect_sel->round_corners)
248
g_object_get (rect_sel,
255
radius = MIN (rect_sel->corner_radius,
256
MIN ((x2 - x1) / 2.0, (y2 - y1) / 2.0));
258
square_size = (int) (radius * 2);
260
gimp_draw_tool_draw_arc (draw_tool, FALSE,
262
square_size, square_size,
266
gimp_draw_tool_draw_arc (draw_tool, FALSE,
267
x2 - square_size, y1,
268
square_size, square_size,
272
gimp_draw_tool_draw_arc (draw_tool, FALSE,
273
x2 - square_size, y2 - square_size,
274
square_size, square_size,
278
gimp_draw_tool_draw_arc (draw_tool, FALSE,
279
x1, y2 - square_size,
280
square_size, square_size,
287
gimp_rect_select_tool_button_press (GimpTool *tool,
290
GdkModifierType state,
291
GimpDisplay *display)
293
GimpRectangleTool *rectangle = GIMP_RECTANGLE_TOOL (tool);
294
GimpRectSelectTool *rect_select = GIMP_RECT_SELECT_TOOL (tool);
295
GimpRectangleFunction function;
297
if (tool->display && display != tool->display)
298
gimp_rectangle_tool_cancel (GIMP_RECTANGLE_TOOL (tool));
300
function = gimp_rectangle_tool_get_function (rectangle);
302
rect_select->saved_show_selection
303
= gimp_display_shell_get_show_selection (GIMP_DISPLAY_SHELL (display->shell));
305
if (function == RECT_INACTIVE)
307
GimpDisplay *old_display;
308
gboolean edit_started;
310
old_display = tool->display;
311
tool->display = display;
312
gimp_tool_control_activate (tool->control);
314
edit_started = gimp_selection_tool_start_edit (GIMP_SELECTION_TOOL (tool),
317
if (gimp_tool_control_is_active (tool->control))
318
gimp_tool_control_halt (tool->control);
319
tool->display = old_display;
325
/* if the shift or ctrl keys are down, we don't want to adjust, we
326
* want to create a new rectangle, regardless of pointer loc */
327
if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
328
gimp_rectangle_tool_set_function (rectangle, RECT_CREATING);
330
gimp_rectangle_tool_button_press (tool, coords, time, state, display);
332
/* if we have an existing rectangle in the current display, then
333
* we have already "executed", and need to undo at this point,
334
* unless the user has done something in the meantime
336
function = gimp_rectangle_tool_get_function (rectangle);
338
if (function == RECT_CREATING)
340
rect_select->use_saved_op = FALSE;
344
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
345
GimpImage *image = tool->display->image;
347
GimpChannelOps operation;
349
if (rect_select->use_saved_op)
350
operation = rect_select->operation;
352
operation = options->operation;
354
undo = gimp_undo_stack_peek (image->undo_stack);
356
if (undo && rect_select->undo == undo)
358
/* prevent this change from halting the tool */
359
gimp_tool_control_set_preserve (tool->control, TRUE);
361
gimp_image_undo (image);
363
gimp_tool_control_set_preserve (tool->control, FALSE);
365
/* we will need to redo if the user cancels or executes */
366
rect_select->redo = gimp_undo_stack_peek (image->redo_stack);
369
/* if the operation is "Replace", turn off the marching ants,
370
because they are confusing */
371
if (operation == GIMP_CHANNEL_OP_REPLACE)
372
gimp_display_shell_set_show_selection (GIMP_DISPLAY_SHELL (display->shell),
376
rect_select->undo = NULL;
380
gimp_rect_select_tool_button_release (GimpTool *tool,
383
GdkModifierType state,
384
GimpButtonReleaseType release_type,
385
GimpDisplay *display)
387
GimpRectSelectTool *rect_select = GIMP_RECT_SELECT_TOOL (tool);
389
gimp_tool_pop_status (tool, display);
390
gimp_display_shell_set_show_selection (GIMP_DISPLAY_SHELL (display->shell),
391
rect_select->saved_show_selection);
394
* if the user has not moved the mouse, we need to redo the operation
395
* that was undone on button press.
397
if (release_type == GIMP_BUTTON_RELEASE_CLICK)
399
GimpImage *image = tool->display->image;
402
redo = gimp_undo_stack_peek (image->redo_stack);
404
if (redo && rect_select->redo == redo)
406
/* prevent this from halting the tool */
407
gimp_tool_control_set_preserve (tool->control, TRUE);
409
gimp_image_redo (image);
410
rect_select->redo = NULL;
412
gimp_tool_control_set_preserve (tool->control, FALSE);
416
gimp_rectangle_tool_button_release (tool, coords, time, state, release_type,
419
if (release_type == GIMP_BUTTON_RELEASE_CANCEL)
421
if (rect_select->redo)
423
/* prevent this from halting the tool */
424
gimp_tool_control_set_preserve (tool->control, TRUE);
426
gimp_image_redo (tool->display->image);
428
gimp_tool_control_set_preserve (tool->control, FALSE);
431
rect_select->use_saved_op = TRUE; /* is this correct? */
434
rect_select->redo = NULL;
438
gimp_rect_select_tool_active_modifier_key (GimpTool *tool,
441
GdkModifierType state,
442
GimpDisplay *display)
444
GIMP_TOOL_CLASS (parent_class)->active_modifier_key (tool, key, press, state,
447
gimp_rectangle_tool_active_modifier_key (tool, key, press, state, display);
451
gimp_rect_select_tool_key_press (GimpTool *tool,
453
GimpDisplay *display)
455
return (gimp_rectangle_tool_key_press (tool, kevent, display) ||
456
gimp_edit_selection_tool_key_press (tool, kevent, display));
460
gimp_rect_select_tool_oper_update (GimpTool *tool,
462
GdkModifierType state,
464
GimpDisplay *display)
466
GimpRectangleFunction function;
468
gimp_rectangle_tool_oper_update (tool, coords, state, proximity, display);
470
function = gimp_rectangle_tool_get_function (GIMP_RECTANGLE_TOOL (tool));
472
if (function == RECT_INACTIVE)
473
GIMP_SELECTION_TOOL (tool)->allow_move = TRUE;
475
GIMP_SELECTION_TOOL (tool)->allow_move = FALSE;
477
GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
482
gimp_rect_select_tool_cursor_update (GimpTool *tool,
484
GdkModifierType state,
485
GimpDisplay *display)
487
gimp_rectangle_tool_cursor_update (tool, coords, state, display);
489
/* override the previous if shift or ctrl are down */
490
if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
491
gimp_tool_control_set_cursor (tool->control, GIMP_CURSOR_CROSSHAIR_SMALL);
493
GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
498
gimp_rect_select_tool_select (GimpRectangleTool *rectangle,
504
GimpTool *tool = GIMP_TOOL (rectangle);
505
GimpRectSelectTool *rect_select = GIMP_RECT_SELECT_TOOL (rectangle);
506
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
508
gboolean rectangle_exists;
509
GimpChannelOps operation;
511
gimp_tool_pop_status (tool, tool->display);
513
image = tool->display->image;
515
rectangle_exists = (x <= image->width && y <= image->height &&
516
x + w >= 0 && y + h >= 0 &&
519
if (rect_select->use_saved_op)
520
operation = rect_select->operation;
522
operation = options->operation;
524
/* if rectangle exists, turn it into a selection */
525
if (rectangle_exists)
526
GIMP_RECT_SELECT_TOOL_GET_CLASS (rect_select)->select (rect_select,
530
return rectangle_exists;
534
gimp_rect_select_tool_real_select (GimpRectSelectTool *rect_select,
535
GimpChannelOps operation,
541
GimpTool *tool = GIMP_TOOL (rect_select);
542
GimpSelectionOptions *options;
543
GimpRectSelectOptions *rect_select_options;
545
options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
546
rect_select_options = GIMP_RECT_SELECT_TOOL_GET_OPTIONS (tool);
548
if (rect_select_options->round_corners)
550
/* To prevent elliptification of the rect, we must cap the corner radius */
551
gdouble radius = MIN (rect_select_options->corner_radius,
552
MIN (w / 2.0, h / 2.0));
554
gimp_channel_select_round_rect (gimp_image_get_mask (tool->display->image),
560
options->feather_radius,
561
options->feather_radius,
566
gimp_channel_select_rectangle (gimp_image_get_mask (tool->display->image),
570
options->feather_radius,
571
options->feather_radius,
577
* This function is called if the user clicks and releases the left
578
* button without moving it. There are the things we might want
580
* 1) If there is an existing rectangle and we are inside it, we
581
* convert it into a selection.
582
* 2) If there is an existing rectangle and we are outside it, we
584
* 3) If there is no rectangle and there is a floating selection,
586
* 4) If there is no rectangle and we are inside the selection, we
587
* create a rectangle from the selection bounds.
588
* 5) If there is no rectangle and we are outside the selection,
589
* we clear the selection.
592
gimp_rect_select_tool_execute (GimpRectangleTool *rectangle,
598
if (w == 0 && h == 0)
600
GimpImage *image = GIMP_TOOL (rectangle)->display->image;
601
GimpChannel *selection = gimp_image_get_mask (image);
605
if (gimp_image_floating_sel (image))
607
floating_sel_anchor (gimp_image_floating_sel (image));
608
gimp_image_flush (image);
612
gimp_rectangle_tool_get_press_coords (rectangle, &pressx, &pressy);
614
/* if the click was inside the marching ants */
615
if (gimp_pickable_get_opacity_at (GIMP_PICKABLE (selection),
616
pressx, pressy) > BOUNDARY_HALF_WAY)
620
if (gimp_channel_bounds (selection, &x1, &y1, &x2, &y2))
622
g_object_set (rectangle,
630
gimp_rectangle_tool_set_function (rectangle, RECT_MOVING);
636
GimpTool *tool = GIMP_TOOL (rectangle);
638
/* prevent this change from halting the tool */
639
gimp_tool_control_set_preserve (tool->control, TRUE);
641
/* otherwise clear the selection */
642
gimp_channel_clear (selection, NULL, TRUE);
643
gimp_image_flush (image);
645
gimp_tool_control_set_preserve (tool->control, FALSE);
653
gimp_rect_select_tool_cancel (GimpRectangleTool *rectangle)
655
GimpTool *tool = GIMP_TOOL (rectangle);
656
GimpRectSelectTool *rect_select = GIMP_RECT_SELECT_TOOL (rectangle);
660
GimpImage *image = tool->display->image;
663
/* if we have an existing rectangle in the current display, then
664
* we have already "executed", and need to undo at this point,
665
* unless the user has done something in the meantime
667
undo = gimp_undo_stack_peek (image->undo_stack);
669
if (undo && rect_select->undo == undo)
671
/* prevent this change from halting the tool */
672
gimp_tool_control_set_preserve (tool->control, TRUE);
674
gimp_image_undo (image);
675
gimp_image_flush (image);
677
gimp_tool_control_set_preserve (tool->control, FALSE);
681
rect_select->undo = NULL;
682
rect_select->redo = NULL;
686
gimp_rect_select_tool_rectangle_changed (GimpRectangleTool *rectangle)
688
GimpTool *tool = GIMP_TOOL (rectangle);
690
/* prevent change in selection from halting the tool */
691
gimp_tool_control_set_preserve (tool->control, TRUE);
693
if (tool->display && ! gimp_tool_control_is_active (tool->control))
695
GimpRectSelectTool *rect_select = GIMP_RECT_SELECT_TOOL (tool);
696
GimpImage *image = tool->display->image;
700
/* if we got here via button release, we have already undone the
701
* previous operation. But if we got here by some other means,
702
* we need to undo it now.
704
undo = gimp_undo_stack_peek (image->undo_stack);
706
if (undo && rect_select->undo == undo)
708
gimp_image_undo (image);
709
rect_select->undo = NULL;
712
g_object_get (rectangle,
719
if (gimp_rect_select_tool_select (rectangle, x1, y1, x2 - x1, y2 - y1))
721
/* save the undo that we got when executing, but only if
722
* we actually selected something
724
rect_select->undo = gimp_undo_stack_peek (image->undo_stack);
725
rect_select->redo = NULL;
728
if (! rect_select->use_saved_op)
730
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
732
/* remember the operation now in case we modify the rectangle */
733
rect_select->operation = options->operation;
734
rect_select->use_saved_op = TRUE;
737
gimp_image_flush (image);
740
gimp_tool_control_set_preserve (tool->control, FALSE);
746
gimp_rect_select_tool_round_corners_notify (GimpRectSelectOptions *options,
748
GimpRectSelectTool *rect_sel)
750
gimp_draw_tool_pause (GIMP_DRAW_TOOL (rect_sel));
752
rect_sel->round_corners = options->round_corners;
753
rect_sel->corner_radius = options->corner_radius;
755
gimp_draw_tool_resume (GIMP_DRAW_TOOL (rect_sel));