1
/* The GIMP -- an 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.
24
#include <gdk/gdkkeysyms.h>
26
#include "libgimpwidgets/gimpwidgets.h"
28
#include "tools-types.h"
30
#include "core/gimp-utils.h"
31
#include "core/gimpchannel.h"
32
#include "core/gimpchannel-select.h"
33
#include "core/gimpimage.h"
34
#include "core/gimpimage-crop.h"
35
#include "core/gimplayer-floating-sel.h"
36
#include "core/gimpmarshal.h"
37
#include "core/gimptoolinfo.h"
38
#include "core/gimpunit.h"
40
#include "widgets/gimphelp-ids.h"
42
#include "display/gimpdisplay.h"
43
#include "display/gimpdisplayshell.h"
45
#include "gimprectselecttool.h"
46
#include "gimpselectionoptions.h"
47
#include "gimptoolcontrol.h"
49
#include "gimp-intl.h"
52
static void gimp_rect_select_tool_class_init (GimpRectSelectToolClass *klass);
53
static void gimp_rect_select_tool_init (GimpRectSelectTool *rect_select);
55
static void gimp_rect_select_tool_button_press (GimpTool *tool,
58
GdkModifierType state,
60
static void gimp_rect_select_tool_button_release (GimpTool *tool,
63
GdkModifierType state,
65
static void gimp_rect_select_tool_motion (GimpTool *tool,
68
GdkModifierType state,
71
static void gimp_rect_select_tool_draw (GimpDrawTool *draw_tool);
73
static void gimp_rect_select_tool_real_rect_select (GimpRectSelectTool *rect_tool,
79
static void gimp_rect_select_tool_update_options (GimpRectSelectTool *rect_sel,
80
GimpDisplay *display);
82
static GimpSelectionToolClass *parent_class = NULL;
85
/* public functions */
88
gimp_rect_select_tool_register (GimpToolRegisterCallback callback,
91
(* callback) (GIMP_TYPE_RECT_SELECT_TOOL,
92
GIMP_TYPE_SELECTION_OPTIONS,
93
gimp_selection_options_gui,
95
"gimp-rect-select-tool",
97
_("Select rectangular regions"),
98
N_("_Rect Select"), "R",
99
NULL, GIMP_HELP_TOOL_RECT_SELECT,
100
GIMP_STOCK_TOOL_RECT_SELECT,
105
gimp_rect_select_tool_get_type (void)
107
static GtkType tool_type = 0;
111
static const GTypeInfo tool_info =
113
sizeof (GimpRectSelectToolClass),
114
(GBaseInitFunc) NULL,
115
(GBaseFinalizeFunc) NULL,
116
(GClassInitFunc) gimp_rect_select_tool_class_init,
117
NULL, /* class_finalize */
118
NULL, /* class_data */
119
sizeof (GimpRectSelectTool),
121
(GInstanceInitFunc) gimp_rect_select_tool_init,
124
tool_type = g_type_register_static (GIMP_TYPE_SELECTION_TOOL,
125
"GimpRectSelectTool",
133
/* private functions */
136
gimp_rect_select_tool_class_init (GimpRectSelectToolClass *klass)
138
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
139
GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
141
parent_class = g_type_class_peek_parent (klass);
143
tool_class->button_press = gimp_rect_select_tool_button_press;
144
tool_class->button_release = gimp_rect_select_tool_button_release;
145
tool_class->motion = gimp_rect_select_tool_motion;
147
draw_tool_class->draw = gimp_rect_select_tool_draw;
149
klass->rect_select = gimp_rect_select_tool_real_rect_select;
153
gimp_rect_select_tool_init (GimpRectSelectTool *rect_select)
155
GimpTool *tool = GIMP_TOOL (rect_select);
157
gimp_tool_control_set_tool_cursor (tool->control,
158
GIMP_TOOL_CURSOR_RECT_SELECT);
160
rect_select->sx = rect_select->sy = 0;
161
rect_select->x = rect_select->y = 0;
162
rect_select->w = rect_select->h = 0;
166
gimp_rect_select_tool_button_press (GimpTool *tool,
169
GdkModifierType state,
172
GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (tool);
173
GimpSelectionTool *sel_tool = GIMP_SELECTION_TOOL (tool);
174
GimpSelectionOptions *options;
175
GimpUnit unit = GIMP_UNIT_PIXEL;
178
options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
180
rect_sel->sx = RINT(coords->x);
181
rect_sel->sy = RINT(coords->y);
186
rect_sel->lx = RINT(coords->x);
187
rect_sel->ly = RINT(coords->y);
188
rect_sel->center = FALSE;
190
rect_sel->fixed_mode = options->fixed_mode;
191
rect_sel->fixed_width = options->fixed_width;
192
rect_sel->fixed_height = options->fixed_height;
193
unit = options->fixed_unit;
197
case GIMP_UNIT_PIXEL:
199
case GIMP_UNIT_PERCENT:
200
rect_sel->fixed_width =
201
gdisp->gimage->width * rect_sel->fixed_width / 100;
202
rect_sel->fixed_height =
203
gdisp->gimage->height * rect_sel->fixed_height / 100;
206
unit_factor = _gimp_unit_get_factor (tool->tool_info->gimp, unit);
207
rect_sel->fixed_width =
208
rect_sel->fixed_width * gdisp->gimage->xresolution / unit_factor;
209
rect_sel->fixed_height =
210
rect_sel->fixed_height * gdisp->gimage->yresolution / unit_factor;
214
rect_sel->fixed_width = MAX (1, rect_sel->fixed_width);
215
rect_sel->fixed_height = MAX (1, rect_sel->fixed_height);
217
gimp_tool_control_activate (tool->control);
220
if (gimp_selection_tool_start_edit (sel_tool, coords))
223
switch (sel_tool->op)
226
gimp_tool_push_status (tool, _("Selection: ADD"));
228
case SELECTION_SUBTRACT:
229
gimp_tool_push_status (tool, _("Selection: SUBTRACT"));
231
case SELECTION_INTERSECT:
232
gimp_tool_push_status (tool, _("Selection: INTERSECT"));
234
case SELECTION_REPLACE:
235
gimp_tool_push_status (tool, _("Selection: REPLACE"));
241
gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), gdisp);
245
gimp_rect_select_tool_button_release (GimpTool *tool,
248
GdkModifierType state,
251
GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (tool);
253
gimp_tool_pop_status (tool);
255
gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
257
gimp_tool_control_halt (tool->control);
259
/* First take care of the case where the user "cancels" the action */
260
if (! (state & GDK_BUTTON3_MASK))
262
if (rect_sel->w == 0 || rect_sel->h == 0)
264
/* If there is a floating selection, anchor it */
265
if (gimp_image_floating_sel (gdisp->gimage))
266
floating_sel_anchor (gimp_image_floating_sel (gdisp->gimage));
267
/* Otherwise, clear the selection mask */
269
gimp_channel_clear (gimp_image_get_mask (gdisp->gimage), NULL,
272
gimp_image_flush (gdisp->gimage);
276
gimp_rect_select_tool_rect_select (rect_sel,
277
rect_sel->x, rect_sel->y,
278
rect_sel->w, rect_sel->h);
280
/* show selection on all views */
281
gimp_image_flush (gdisp->gimage);
286
gimp_rect_select_tool_motion (GimpTool *tool,
289
GdkModifierType state,
292
GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (tool);
293
GimpSelectionTool *sel_tool = GIMP_SELECTION_TOOL (tool);
297
if (sel_tool->op == SELECTION_ANCHOR)
299
sel_tool->op = SELECTION_REPLACE;
301
gimp_tool_cursor_update (tool, coords, state, gdisp);
304
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
307
if (state & GDK_MOD1_MASK)
309
/* Just move the selection rectangle around */
311
mx = RINT(coords->x) - rect_sel->lx;
312
my = RINT(coords->y) - rect_sel->ly;
321
/* Change the selection rectangle's size, first calculate absolute
322
* width and height, then take care of quadrants.
325
if (rect_sel->fixed_mode == GIMP_RECT_SELECT_MODE_FIXED_SIZE)
327
rect_sel->w = RINT(rect_sel->fixed_width);
328
rect_sel->h = RINT(rect_sel->fixed_height);
332
rect_sel->w = abs(RINT(coords->x) - rect_sel->sx);
333
rect_sel->h = abs(RINT(coords->y) - rect_sel->sy);
336
if (rect_sel->fixed_mode == GIMP_RECT_SELECT_MODE_FIXED_RATIO)
338
ratio = rect_sel->fixed_height / rect_sel->fixed_width;
339
if ( ( (gdouble) rect_sel->h) / ( (gdouble) rect_sel->w ) > ratio)
341
rect_sel->w = RINT(rect_sel->h / ratio);
345
rect_sel->h = RINT(rect_sel->w * ratio);
350
/* If the shift key is down, then make the rectangle square (or
353
if ((state & GDK_SHIFT_MASK) &&
354
rect_sel->fixed_mode == GIMP_RECT_SELECT_MODE_FREE)
356
if (rect_sel->w > rect_sel->h)
358
rect_sel->h = rect_sel->w;
362
rect_sel->w = rect_sel->h;
366
if (state & GDK_CONTROL_MASK)
368
/* If the control key is down, create the selection from the center out */
369
if (rect_sel->fixed_mode == GIMP_RECT_SELECT_MODE_FIXED_SIZE)
371
/* Fixed size selection is simply centered over start point */
372
rect_sel->x = RINT(rect_sel->sx - rect_sel->w / 2.0);
373
rect_sel->y = RINT(rect_sel->sy - rect_sel->h / 2.0);
377
/* Non-fixed size is mirrored over starting point */
378
rect_sel->x = rect_sel->sx - rect_sel->w;
379
rect_sel->y = rect_sel->sy - rect_sel->h;
380
rect_sel->w = rect_sel->w * 2;
381
rect_sel->h = rect_sel->h * 2;
386
/* Make (rect->x, rect->y) upper left hand point of selection */
387
if (coords->x > rect_sel->sx)
389
rect_sel->x = rect_sel->sx;
393
rect_sel->x = rect_sel->sx - rect_sel->w;
395
if (coords->y > rect_sel->sy)
397
rect_sel->y = rect_sel->sy;
401
rect_sel->y = rect_sel->sy - rect_sel->h;
406
rect_sel->lx = RINT(coords->x);
407
rect_sel->ly = RINT(coords->y);
409
gimp_rect_select_tool_update_options (rect_sel, gdisp);
411
gimp_tool_pop_status (tool);
413
gimp_tool_push_status_coords (tool,
419
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
423
gimp_rect_select_tool_draw (GimpDrawTool *draw_tool)
425
GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (draw_tool);
427
gimp_draw_tool_draw_rectangle (draw_tool,
429
rect_sel->x, rect_sel->y,
430
rect_sel->w, rect_sel->h,
435
gimp_rect_select_tool_real_rect_select (GimpRectSelectTool *rect_tool,
441
GimpTool *tool = GIMP_TOOL (rect_tool);
442
GimpSelectionTool *sel_tool = GIMP_SELECTION_TOOL (rect_tool);
443
GimpSelectionOptions *options;
445
options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
447
gimp_channel_select_rectangle (gimp_image_get_mask (tool->gdisp->gimage),
451
options->feather_radius,
452
options->feather_radius);
456
gimp_rect_select_tool_rect_select (GimpRectSelectTool *rect_tool,
463
GimpSelectionOptions *options;
465
g_return_if_fail (GIMP_IS_RECT_SELECT_TOOL (rect_tool));
467
tool = GIMP_TOOL (rect_tool);
468
options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
470
if (options->auto_shrink)
476
if (! gimp_rectangle_intersect (x, y, w, h,
478
tool->gdisp->gimage->width,
479
tool->gdisp->gimage->height,
485
if (! options->shrink_merged)
490
item = GIMP_ITEM (gimp_image_active_drawable (tool->gdisp->gimage));
492
gimp_item_offsets (item, &off_x, &off_y);
493
width = gimp_item_width (item);
494
height = gimp_item_height (item);
496
if (! gimp_rectangle_intersect (x, y, w, h,
497
off_x, off_y, width, height,
507
if (gimp_image_crop_auto_shrink (tool->gdisp->gimage,
510
! options->shrink_merged,
522
GIMP_RECT_SELECT_TOOL_GET_CLASS (rect_tool)->rect_select (rect_tool,
527
gimp_rect_select_tool_update_options (GimpRectSelectTool *rect_sel,
530
GimpDisplayShell *shell;
534
if (rect_sel->fixed_mode != GIMP_RECT_SELECT_MODE_FREE)
537
shell = GIMP_DISPLAY_SHELL (gdisp->shell);
539
if (shell->unit == GIMP_UNIT_PIXEL)
542
height = rect_sel->h;
546
GimpImage *image = gdisp->gimage;
548
width = (rect_sel->w *
549
_gimp_unit_get_factor (image->gimp,
550
shell->unit) / image->xresolution);
551
height = (rect_sel->h *
552
_gimp_unit_get_factor (image->gimp,
553
shell->unit) / image->yresolution);
556
g_object_set (GIMP_TOOL (rect_sel)->tool_info->tool_options,
557
"fixed-width", width,
558
"fixed-height", height,
559
"fixed-unit", shell->unit,