1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* gimpregionselecttool.c
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
#include "libgimpwidgets/gimpwidgets.h"
27
#include "tools-types.h"
29
#include "base/boundary.h"
30
#include "base/pixel-region.h"
32
#include "core/gimpchannel.h"
33
#include "core/gimpchannel-select.h"
34
#include "core/gimpimage.h"
35
#include "core/gimplayer-floating-sel.h"
37
#include "display/gimpcanvas.h"
38
#include "display/gimpdisplay.h"
39
#include "display/gimpdisplayshell.h"
40
#include "display/gimpdisplayshell-cursor.h"
41
#include "display/gimpdisplayshell-transform.h"
43
#include "gimpregionselecttool.h"
44
#include "gimpselectionoptions.h"
45
#include "gimptoolcontrol.h"
47
#include "gimp-intl.h"
50
static void gimp_region_select_tool_finalize (GObject *object);
52
static void gimp_region_select_tool_button_press (GimpTool *tool,
55
GdkModifierType state,
56
GimpDisplay *display);
57
static void gimp_region_select_tool_button_release (GimpTool *tool,
60
GdkModifierType state,
61
GimpButtonReleaseType release_type,
62
GimpDisplay *display);
63
static void gimp_region_select_tool_motion (GimpTool *tool,
66
GdkModifierType state,
67
GimpDisplay *display);
68
static void gimp_region_select_tool_cursor_update (GimpTool *tool,
70
GdkModifierType state,
71
GimpDisplay *display);
73
static void gimp_region_select_tool_draw (GimpDrawTool *draw_tool);
75
static GdkSegment *gimp_region_select_tool_calculate (GimpRegionSelectTool *region_sel,
80
G_DEFINE_TYPE (GimpRegionSelectTool, gimp_region_select_tool,
81
GIMP_TYPE_SELECTION_TOOL)
83
#define parent_class gimp_region_select_tool_parent_class
87
gimp_region_select_tool_class_init (GimpRegionSelectToolClass *klass)
89
GObjectClass *object_class = G_OBJECT_CLASS (klass);
90
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
91
GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
93
object_class->finalize = gimp_region_select_tool_finalize;
95
tool_class->button_press = gimp_region_select_tool_button_press;
96
tool_class->button_release = gimp_region_select_tool_button_release;
97
tool_class->motion = gimp_region_select_tool_motion;
98
tool_class->cursor_update = gimp_region_select_tool_cursor_update;
100
draw_tool_class->draw = gimp_region_select_tool_draw;
104
gimp_region_select_tool_init (GimpRegionSelectTool *region_select)
106
GimpTool *tool = GIMP_TOOL (region_select);
108
gimp_tool_control_set_scroll_lock (tool->control, TRUE);
109
gimp_tool_control_set_motion_mode (tool->control, GIMP_MOTION_MODE_COMPRESS);
111
region_select->x = 0;
112
region_select->y = 0;
113
region_select->saved_threshold = 0.0;
115
region_select->region_mask = NULL;
116
region_select->segs = NULL;
117
region_select->num_segs = 0;
121
gimp_region_select_tool_finalize (GObject *object)
123
GimpRegionSelectTool *region_sel = GIMP_REGION_SELECT_TOOL (object);
125
if (region_sel->region_mask)
127
g_object_unref (region_sel->region_mask);
128
region_sel->region_mask = NULL;
131
if (region_sel->segs)
133
g_free (region_sel->segs);
134
region_sel->segs = NULL;
135
region_sel->num_segs = 0;
138
G_OBJECT_CLASS (parent_class)->finalize (object);
142
gimp_region_select_tool_button_press (GimpTool *tool,
145
GdkModifierType state,
146
GimpDisplay *display)
148
GimpRegionSelectTool *region_sel = GIMP_REGION_SELECT_TOOL (tool);
149
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
151
region_sel->x = coords->x;
152
region_sel->y = coords->y;
153
region_sel->saved_threshold = options->threshold;
155
gimp_tool_control_activate (tool->control);
156
tool->display = display;
158
if (gimp_selection_tool_start_edit (GIMP_SELECTION_TOOL (region_sel), coords))
161
gimp_tool_push_status (tool, display,
162
_("Move the mouse to change threshold"));
164
/* calculate the region boundary */
165
region_sel->segs = gimp_region_select_tool_calculate (region_sel, display,
166
®ion_sel->num_segs);
168
gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display);
172
gimp_region_select_tool_button_release (GimpTool *tool,
175
GdkModifierType state,
176
GimpButtonReleaseType release_type,
177
GimpDisplay *display)
179
GimpRegionSelectTool *region_sel = GIMP_REGION_SELECT_TOOL (tool);
180
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
182
gimp_tool_pop_status (tool, display);
184
gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
186
gimp_tool_control_halt (tool->control);
188
if (release_type != GIMP_BUTTON_RELEASE_CANCEL)
192
if (GIMP_SELECTION_TOOL (tool)->function == SELECTION_ANCHOR)
194
if (gimp_image_floating_sel (display->image))
196
/* If there is a floating selection, anchor it */
197
floating_sel_anchor (gimp_image_floating_sel (display->image));
201
/* Otherwise, clear the selection mask */
202
gimp_channel_clear (gimp_image_get_mask (display->image), NULL,
206
gimp_image_flush (display->image);
208
else if (region_sel->region_mask)
210
if (options->sample_merged)
217
GimpDrawable *drawable =
218
gimp_image_active_drawable (display->image);
220
gimp_item_offsets (GIMP_ITEM (drawable), &off_x, &off_y);
223
gimp_channel_select_channel (gimp_image_get_mask (display->image),
224
GIMP_REGION_SELECT_TOOL_GET_CLASS (tool)->undo_desc,
225
region_sel->region_mask,
230
options->feather_radius,
231
options->feather_radius);
234
gimp_image_flush (display->image);
238
if (region_sel->region_mask)
240
g_object_unref (region_sel->region_mask);
241
region_sel->region_mask = NULL;
244
if (region_sel->segs)
246
g_free (region_sel->segs);
247
region_sel->segs = NULL;
248
region_sel->num_segs = 0;
251
/* Restore the original threshold */
252
g_object_set (options,
253
"threshold", region_sel->saved_threshold,
258
gimp_region_select_tool_motion (GimpTool *tool,
261
GdkModifierType state,
262
GimpDisplay *display)
264
GimpRegionSelectTool *region_sel = GIMP_REGION_SELECT_TOOL (tool);
265
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
266
GdkSegment *new_segs;
271
static guint32 last_time = 0;
273
/* don't let the events come in too fast, ignore below a delay of 100 ms */
274
if (time - last_time < 100)
279
diff_x = coords->x - region_sel->x;
280
diff_y = coords->y - region_sel->y;
282
diff = ((ABS (diff_x) > ABS (diff_y)) ? diff_x : diff_y) / 2.0;
284
g_object_set (options,
285
"threshold", CLAMP (region_sel->saved_threshold + diff, 0, 255),
288
/* calculate the new region boundary */
289
new_segs = gimp_region_select_tool_calculate (region_sel, display,
292
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
294
/* make sure the XSegment array is freed before we assign the new one */
295
if (region_sel->segs)
296
g_free (region_sel->segs);
298
region_sel->segs = new_segs;
299
region_sel->num_segs = num_new_segs;
301
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
305
gimp_region_select_tool_cursor_update (GimpTool *tool,
307
GdkModifierType state,
308
GimpDisplay *display)
310
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
311
GimpCursorModifier modifier = GIMP_CURSOR_MODIFIER_NONE;
313
if (! gimp_image_coords_in_active_pickable (display->image, coords,
314
options->sample_merged, FALSE))
315
modifier = GIMP_CURSOR_MODIFIER_BAD;
317
gimp_tool_control_set_cursor_modifier (tool->control, modifier);
319
GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
323
gimp_region_select_tool_draw (GimpDrawTool *draw_tool)
325
GimpRegionSelectTool *region_sel = GIMP_REGION_SELECT_TOOL (draw_tool);
327
if (region_sel->segs)
329
GimpDisplayShell *shell;
331
shell = GIMP_DISPLAY_SHELL (GIMP_TOOL (draw_tool)->display->shell);
333
gimp_canvas_draw_segments (GIMP_CANVAS (shell->canvas),
334
GIMP_CANVAS_STYLE_XOR,
335
region_sel->segs, region_sel->num_segs);
340
gimp_region_select_tool_calculate (GimpRegionSelectTool *region_sel,
341
GimpDisplay *display,
344
GimpTool *tool = GIMP_TOOL (region_sel);
345
GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
346
GimpDisplayShell *shell = GIMP_DISPLAY_SHELL (display->shell);
347
GimpDrawable *drawable;
352
drawable = gimp_image_active_drawable (display->image);
354
gimp_display_shell_set_override_cursor (shell, GDK_WATCH);
356
if (region_sel->region_mask)
357
g_object_unref (region_sel->region_mask);
359
region_sel->region_mask =
360
GIMP_REGION_SELECT_TOOL_GET_CLASS (region_sel)->get_mask (region_sel,
363
if (! region_sel->region_mask)
365
gimp_display_shell_unset_override_cursor (shell);
371
/* calculate and allocate a new segment array which represents the
372
* boundary of the contiguous region
374
pixel_region_init (&maskPR,
375
gimp_drawable_get_tiles (GIMP_DRAWABLE (region_sel->region_mask)),
377
gimp_item_width (GIMP_ITEM (region_sel->region_mask)),
378
gimp_item_height (GIMP_ITEM (region_sel->region_mask)),
381
bsegs = boundary_find (&maskPR, BOUNDARY_WITHIN_BOUNDS,
383
gimp_item_width (GIMP_ITEM (region_sel->region_mask)),
384
gimp_item_height (GIMP_ITEM (region_sel->region_mask)),
388
segs = g_new (GdkSegment, *num_segs);
390
gimp_display_shell_transform_segments (shell, bsegs, segs, *num_segs,
391
! options->sample_merged);
394
gimp_display_shell_unset_override_cursor (shell);