gimprectangleselecttool.c 10.6 KB
Newer Older
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
/* The GIMP -- an 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#include "libgimpmath/gimpmath.h"
#include "libgimpconfig/gimpconfig.h"
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "tools-types.h"

#include "core/gimpchannel.h"
#include "core/gimpchannel-select.h"
33
#include "core/gimplayer-floating-sel.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "core/gimpimage.h"
#include "core/gimptoolinfo.h"
#include "core/gimp-utils.h"

#include "widgets/gimpdialogfactory.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpviewabledialog.h"
#include "widgets/gimpwidgets-utils.h"

#include "display/gimpcanvas.h"
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-transform.h"

#include "gimpselectiontool.h"
#include "gimpselectionoptions.h"
50
51
#include "gimprectangletool.h"
#include "gimprectangleoptions.h"
52
53
54
55
56
57
#include "gimpnewrectselecttool.h"
#include "gimptoolcontrol.h"

#include "gimp-intl.h"


58
59
static void gimp_new_rect_select_tool_class_init     (GimpNewRectSelectToolClass *klass);
static void gimp_new_rect_select_tool_init           (GimpNewRectSelectTool      *rect_tool);
60

61
62
63
64
65
static void gimp_new_rect_select_tool_button_press   (GimpTool                   *tool,
                                                      GimpCoords                 *coords,
                                                      guint32                     time,
                                                      GdkModifierType             state,
                                                      GimpDisplay                *gdisp);
66
67
68
69
70
71
72
73
74
static void gimp_new_rect_select_tool_modifier_key   (GimpTool                   *tool,
                                                      GdkModifierType             key,
                                                      gboolean                    press,
                                                      GdkModifierType             state,
                                                      GimpDisplay                *gdisp);
static void gimp_new_rect_select_tool_cursor_update  (GimpTool                   *tool,
                                                      GimpCoords                 *coords,
                                                      GdkModifierType             state,
                                                      GimpDisplay                *gdisp);
75

76
static gboolean gimp_new_rect_select_tool_execute    (GimpRectangleTool          *rect_tool,
77
78
79
80
                                                      gint                        x,
                                                      gint                        y,
                                                      gint                        w,
                                                      gint                        h);
81
82
83
84
85
86
87
88
89
90
91

static GimpDrawToolClass *parent_class = NULL;


/*  public functions  */

void
gimp_new_rect_select_tool_register (GimpToolRegisterCallback  callback,
                                    gpointer                  data)
{
  (* callback) (GIMP_TYPE_NEW_RECT_SELECT_TOOL,
92
93
                GIMP_TYPE_RECTANGLE_OPTIONS,
                gimp_rectangle_options_gui,
94
95
96
97
                0,
                "gimp-new-rect-select-tool",
                _("New Rect Select"),
                _("Select a Rectangular part of an image"),
98
                N_("_New Rect Select"), NULL,
99
                NULL, GIMP_HELP_TOOL_RECT_SELECT,
100
                GIMP_STOCK_TOOL_RECT_SELECT,
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
                data);
}

GType
gimp_new_rect_select_tool_get_type (void)
{
  static GType tool_type = 0;

  if (! tool_type)
    {
      static const GTypeInfo tool_info =
      {
        sizeof (GimpNewRectSelectToolClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_new_rect_select_tool_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpNewRectSelectTool),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_new_rect_select_tool_init,
      };

124
      tool_type = g_type_register_static (GIMP_TYPE_RECTANGLE_TOOL,
125
126
127
128
129
130
131
132
133
134
                                          "GimpNewRectSelectTool",
                                          &tool_info, 0);
    }

  return tool_type;
}

static void
gimp_new_rect_select_tool_class_init (GimpNewRectSelectToolClass *klass)
{
135
136
  GimpToolClass          *tool_class  = GIMP_TOOL_CLASS (klass);
  GimpRectangleToolClass *rect_class  = GIMP_RECTANGLE_TOOL_CLASS (klass);
137
138
139

  parent_class = g_type_class_peek_parent (klass);

140
  tool_class->button_press   = gimp_new_rect_select_tool_button_press;
141
142
143
  tool_class->modifier_key   = gimp_new_rect_select_tool_modifier_key;
  tool_class->cursor_update  = gimp_new_rect_select_tool_cursor_update;

144
  rect_class->execute  = gimp_new_rect_select_tool_execute;
145
146
147
148
149
}

static void
gimp_new_rect_select_tool_init (GimpNewRectSelectTool *new_rect_select_tool)
{
150
151
  GimpTool          *tool      = GIMP_TOOL (new_rect_select_tool);
  GimpRectangleTool *rectangle = GIMP_RECTANGLE_TOOL (new_rect_select_tool);
152
153

  gimp_tool_control_set_tool_cursor (tool->control, GIMP_TOOL_CURSOR_RECT_SELECT);
154
155

  rectangle->selection_tool = TRUE;
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
static void
gimp_new_rect_select_tool_button_press (GimpTool        *tool,
                                        GimpCoords      *coords,
                                        guint32          time,
                                        GdkModifierType  state,
                                        GimpDisplay     *gdisp)
{
  GimpSelectionTool    *sel_tool = GIMP_SELECTION_TOOL (tool);

  GIMP_TOOL_CLASS (parent_class)->button_press (tool, coords,
                                                time, state, gdisp);

  if (gimp_selection_tool_start_edit (sel_tool, coords))
    {
      gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
      return;
    }

  switch (sel_tool->op)
    {
    case SELECTION_ADD:
      gimp_tool_push_status (tool, _("Selection: ADD"));
      break;
    case SELECTION_SUBTRACT:
      gimp_tool_push_status (tool, _("Selection: SUBTRACT"));
      break;
    case SELECTION_INTERSECT:
      gimp_tool_push_status (tool, _("Selection: INTERSECT"));
      break;
    case SELECTION_REPLACE:
      gimp_tool_push_status (tool, _("Selection: REPLACE"));
      break;
    default:
      break;
    }
}

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
static void
gimp_new_rect_select_tool_modifier_key (GimpTool        *tool,
                                        GdkModifierType  key,
                                        gboolean         press,
                                        GdkModifierType  state,
                                        GimpDisplay     *gdisp)
{
}

static void
gimp_new_rect_select_tool_cursor_update (GimpTool        *tool,
                                         GimpCoords      *coords,
                                         GdkModifierType  state,
                                         GimpDisplay     *gdisp)
{
210
  gimp_tool_control_set_tool_cursor (tool->control, GIMP_TOOL_CURSOR_RECT_SELECT);
211
212
213
214

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

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * This function is called if the user clicks and releases the left
 * button without moving it.  There are five things we might want
 * to do here:
 * 1) If there is a floating selection, we anchor it.
 * 2) If there is an existing rectangle and we are inside it, we
 *    convert it into a selection.
 * 3) If there is an existing rectangle and we are outside it, we
 *    clear it.
 * 4) If there is no rectangle and we are inside the selection, we
 *    create a rectangle from the selection bounds.
 * 5) If there is no rectangle and we are outside the selection,
 *    we clear the selection.
 */
static gboolean
gimp_new_rect_select_tool_execute (GimpRectangleTool  *rectangle,
231
232
233
234
                                   gint                x,
                                   gint                y,
                                   gint                w,
                                   gint                h)
235
{
236
  GimpTool             *tool     = GIMP_TOOL (rectangle);
237
  GimpSelectionOptions *options;
238
239
240
241
242
  GimpImage            *gimage;
  gboolean              rectangle_exists;
  gboolean              selected;
  gint                  val;
  GimpChannel          *selection_mask;
243
244
245

  options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);

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
  gimage = tool->gdisp->gimage;
  selection_mask = gimp_image_get_mask (gimage);

  rectangle_exists = (w > 0 && h > 0);

  /*  If there is a floating selection, anchor it  */
  if (gimp_image_floating_sel (gimage))
    {
      floating_sel_anchor (gimp_image_floating_sel (gimage));
      return FALSE;
    }

  /* if rectangle exists, turn it into a selection */
  if (rectangle_exists)
    {
      GimpChannel  *selection_mask = gimp_image_get_mask (gimage);

      gimp_channel_select_rectangle (selection_mask,
                                     x, y, w, h,
                                     options->operation,
                                     options->feather,
                                     options->feather_radius,
                                     options->feather_radius);
      return TRUE;
    }


  val = gimp_channel_value (selection_mask,
                            rectangle->pressx, rectangle->pressy);

  selected = (val > 127);

  /* if point clicked is inside selection, set rectangle to  */
  /* selection bounds.                                       */
  if (selected)
    {
      if (! gimp_channel_bounds (selection_mask,
                                 &rectangle->x1, &rectangle->y1,
                                 &rectangle->x2, &rectangle->y2))
        {
          rectangle->x1 = 0;
          rectangle->y1 = 0;
          rectangle->x2 = gimage->width;
          rectangle->y2 = gimage->height;
        }

      return FALSE;
    }

  /* otherwise clear the selection */
  gimp_channel_clear (selection_mask, NULL, TRUE);

  return TRUE;
299
300
}