~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/tools/gimprectselecttool.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* The GIMP -- an image manipulation program
2
 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
 
 *
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.
8
 
 *
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.
13
 
 *
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.
17
 
 */
18
 
 
19
 
#include "config.h"
20
 
 
21
 
#include <stdlib.h>
22
 
 
23
 
#include <gtk/gtk.h>
24
 
#include <gdk/gdkkeysyms.h>
25
 
 
26
 
#include "libgimpwidgets/gimpwidgets.h"
27
 
 
28
 
#include "tools-types.h"
29
 
 
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"
39
 
 
40
 
#include "widgets/gimphelp-ids.h"
41
 
 
42
 
#include "display/gimpdisplay.h"
43
 
#include "display/gimpdisplayshell.h"
44
 
 
45
 
#include "gimprectselecttool.h"
46
 
#include "gimpselectionoptions.h"
47
 
#include "gimptoolcontrol.h"
48
 
 
49
 
#include "gimp-intl.h"
50
 
 
51
 
 
52
 
static void   gimp_rect_select_tool_class_init (GimpRectSelectToolClass *klass);
53
 
static void   gimp_rect_select_tool_init       (GimpRectSelectTool      *rect_select);
54
 
 
55
 
static void   gimp_rect_select_tool_button_press     (GimpTool        *tool,
56
 
                                                      GimpCoords      *coords,
57
 
                                                      guint32          time,
58
 
                                                      GdkModifierType  state,
59
 
                                                      GimpDisplay     *gdisp);
60
 
static void   gimp_rect_select_tool_button_release   (GimpTool        *tool,
61
 
                                                      GimpCoords      *coords,
62
 
                                                      guint32          time,
63
 
                                                      GdkModifierType  state,
64
 
                                                      GimpDisplay     *gdisp);
65
 
static void   gimp_rect_select_tool_motion           (GimpTool        *tool,
66
 
                                                      GimpCoords      *coords,
67
 
                                                      guint32          time,
68
 
                                                      GdkModifierType  state,
69
 
                                                      GimpDisplay     *gdisp);
70
 
 
71
 
static void   gimp_rect_select_tool_draw             (GimpDrawTool    *draw_tool);
72
 
 
73
 
static void   gimp_rect_select_tool_real_rect_select (GimpRectSelectTool *rect_tool,
74
 
                                                      gint                x,
75
 
                                                      gint                y,
76
 
                                                      gint                w,
77
 
                                                      gint                h);
78
 
 
79
 
static void   gimp_rect_select_tool_update_options   (GimpRectSelectTool *rect_sel,
80
 
                                                      GimpDisplay        *display);
81
 
 
82
 
static GimpSelectionToolClass *parent_class = NULL;
83
 
 
84
 
 
85
 
/*  public functions  */
86
 
 
87
 
void
88
 
gimp_rect_select_tool_register (GimpToolRegisterCallback  callback,
89
 
                                gpointer                  data)
90
 
{
91
 
  (* callback) (GIMP_TYPE_RECT_SELECT_TOOL,
92
 
                GIMP_TYPE_SELECTION_OPTIONS,
93
 
                gimp_selection_options_gui,
94
 
                0,
95
 
                "gimp-rect-select-tool",
96
 
                _("Rect Select"),
97
 
                _("Select rectangular regions"),
98
 
                N_("_Rect Select"), "R",
99
 
                NULL, GIMP_HELP_TOOL_RECT_SELECT,
100
 
                GIMP_STOCK_TOOL_RECT_SELECT,
101
 
                data);
102
 
}
103
 
 
104
 
GType
105
 
gimp_rect_select_tool_get_type (void)
106
 
{
107
 
  static GtkType tool_type = 0;
108
 
 
109
 
  if (! tool_type)
110
 
    {
111
 
      static const GTypeInfo tool_info =
112
 
      {
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),
120
 
        0,              /* n_preallocs    */
121
 
        (GInstanceInitFunc) gimp_rect_select_tool_init,
122
 
      };
123
 
 
124
 
      tool_type = g_type_register_static (GIMP_TYPE_SELECTION_TOOL,
125
 
                                          "GimpRectSelectTool",
126
 
                                          &tool_info, 0);
127
 
    }
128
 
 
129
 
  return tool_type;
130
 
}
131
 
 
132
 
 
133
 
/*  private functions  */
134
 
 
135
 
static void
136
 
gimp_rect_select_tool_class_init (GimpRectSelectToolClass *klass)
137
 
{
138
 
  GimpToolClass     *tool_class      = GIMP_TOOL_CLASS (klass);
139
 
  GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
140
 
 
141
 
  parent_class = g_type_class_peek_parent (klass);
142
 
 
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;
146
 
 
147
 
  draw_tool_class->draw      = gimp_rect_select_tool_draw;
148
 
 
149
 
  klass->rect_select         = gimp_rect_select_tool_real_rect_select;
150
 
}
151
 
 
152
 
static void
153
 
gimp_rect_select_tool_init (GimpRectSelectTool *rect_select)
154
 
{
155
 
  GimpTool *tool = GIMP_TOOL (rect_select);
156
 
 
157
 
  gimp_tool_control_set_tool_cursor (tool->control,
158
 
                                     GIMP_TOOL_CURSOR_RECT_SELECT);
159
 
 
160
 
  rect_select->sx = rect_select->sy = 0;
161
 
  rect_select->x = rect_select->y = 0;
162
 
  rect_select->w = rect_select->h = 0;
163
 
}
164
 
 
165
 
static void
166
 
gimp_rect_select_tool_button_press (GimpTool        *tool,
167
 
                                    GimpCoords      *coords,
168
 
                                    guint32          time,
169
 
                                    GdkModifierType  state,
170
 
                                    GimpDisplay     *gdisp)
171
 
{
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;
176
 
  gdouble               unit_factor;
177
 
 
178
 
  options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
179
 
 
180
 
  rect_sel->sx     = RINT(coords->x);
181
 
  rect_sel->sy     = RINT(coords->y);
182
 
  rect_sel->x      = 0;
183
 
  rect_sel->y      = 0;
184
 
  rect_sel->w      = 0;
185
 
  rect_sel->h      = 0;
186
 
  rect_sel->lx     = RINT(coords->x);
187
 
  rect_sel->ly     = RINT(coords->y);
188
 
  rect_sel->center = FALSE;
189
 
 
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;
194
 
 
195
 
  switch (unit)
196
 
    {
197
 
    case GIMP_UNIT_PIXEL:
198
 
      break;
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;
204
 
      break;
205
 
    default:
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;
211
 
      break;
212
 
    }
213
 
 
214
 
  rect_sel->fixed_width  = MAX (1, rect_sel->fixed_width);
215
 
  rect_sel->fixed_height = MAX (1, rect_sel->fixed_height);
216
 
 
217
 
  gimp_tool_control_activate (tool->control);
218
 
  tool->gdisp = gdisp;
219
 
 
220
 
  if (gimp_selection_tool_start_edit (sel_tool, coords))
221
 
    return;
222
 
 
223
 
  switch (sel_tool->op)
224
 
    {
225
 
    case SELECTION_ADD:
226
 
      gimp_tool_push_status (tool, _("Selection: ADD"));
227
 
      break;
228
 
    case SELECTION_SUBTRACT:
229
 
      gimp_tool_push_status (tool, _("Selection: SUBTRACT"));
230
 
      break;
231
 
    case SELECTION_INTERSECT:
232
 
      gimp_tool_push_status (tool, _("Selection: INTERSECT"));
233
 
      break;
234
 
    case SELECTION_REPLACE:
235
 
      gimp_tool_push_status (tool, _("Selection: REPLACE"));
236
 
      break;
237
 
    default:
238
 
      break;
239
 
    }
240
 
 
241
 
  gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), gdisp);
242
 
}
243
 
 
244
 
static void
245
 
gimp_rect_select_tool_button_release (GimpTool        *tool,
246
 
                                      GimpCoords      *coords,
247
 
                                      guint32          time,
248
 
                                      GdkModifierType  state,
249
 
                                      GimpDisplay     *gdisp)
250
 
{
251
 
  GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (tool);
252
 
 
253
 
  gimp_tool_pop_status (tool);
254
 
 
255
 
  gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
256
 
 
257
 
  gimp_tool_control_halt (tool->control);
258
 
 
259
 
  /*  First take care of the case where the user "cancels" the action  */
260
 
  if (! (state & GDK_BUTTON3_MASK))
261
 
    {                                          
262
 
      if (rect_sel->w == 0 || rect_sel->h == 0)
263
 
        {
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  */
268
 
          else
269
 
            gimp_channel_clear (gimp_image_get_mask (gdisp->gimage), NULL,
270
 
                                TRUE);
271
 
 
272
 
          gimp_image_flush (gdisp->gimage);
273
 
          return;
274
 
        }
275
 
 
276
 
      gimp_rect_select_tool_rect_select (rect_sel,
277
 
                                         rect_sel->x, rect_sel->y,
278
 
                                         rect_sel->w, rect_sel->h);
279
 
                                         
280
 
      /*  show selection on all views  */
281
 
      gimp_image_flush (gdisp->gimage);
282
 
    }
283
 
}
284
 
 
285
 
static void
286
 
gimp_rect_select_tool_motion (GimpTool        *tool,
287
 
                              GimpCoords      *coords,
288
 
                              guint32          time,
289
 
                              GdkModifierType  state,
290
 
                              GimpDisplay     *gdisp)
291
 
{
292
 
  GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (tool);
293
 
  GimpSelectionTool  *sel_tool = GIMP_SELECTION_TOOL (tool);
294
 
  gint                mx, my;
295
 
  gdouble             ratio;
296
 
 
297
 
  if (sel_tool->op == SELECTION_ANCHOR)
298
 
    {
299
 
      sel_tool->op = SELECTION_REPLACE;
300
 
 
301
 
      gimp_tool_cursor_update (tool, coords, state, gdisp);
302
 
    }
303
 
 
304
 
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
305
 
 
306
 
  
307
 
  if (state & GDK_MOD1_MASK)
308
 
    {
309
 
      /*  Just move the selection rectangle around  */
310
 
      
311
 
      mx = RINT(coords->x) - rect_sel->lx;
312
 
      my = RINT(coords->y) - rect_sel->ly;
313
 
 
314
 
      rect_sel->sx += mx;
315
 
      rect_sel->sy += my;
316
 
      rect_sel->x += mx;
317
 
      rect_sel->y += my;
318
 
    }
319
 
  else
320
 
    {
321
 
      /* Change the selection rectangle's size, first calculate absolute 
322
 
       * width and height, then take care of quadrants.
323
 
       */
324
 
 
325
 
      if (rect_sel->fixed_mode == GIMP_RECT_SELECT_MODE_FIXED_SIZE)
326
 
        {
327
 
          rect_sel->w = RINT(rect_sel->fixed_width);
328
 
          rect_sel->h = RINT(rect_sel->fixed_height);
329
 
        }
330
 
      else
331
 
        {
332
 
          rect_sel->w = abs(RINT(coords->x) - rect_sel->sx);
333
 
          rect_sel->h = abs(RINT(coords->y) - rect_sel->sy);
334
 
        }
335
 
      
336
 
      if (rect_sel->fixed_mode == GIMP_RECT_SELECT_MODE_FIXED_RATIO)
337
 
        {
338
 
          ratio = rect_sel->fixed_height / rect_sel->fixed_width;
339
 
          if ( ( (gdouble) rect_sel->h) / ( (gdouble) rect_sel->w ) > ratio)
340
 
            {
341
 
              rect_sel->w = RINT(rect_sel->h / ratio);
342
 
            }
343
 
          else
344
 
            {
345
 
              rect_sel->h = RINT(rect_sel->w * ratio);
346
 
            }
347
 
        }
348
 
      
349
 
 
350
 
      /* If the shift key is down, then make the rectangle square (or
351
 
       * ellipse circular)
352
 
       */
353
 
      if ((state & GDK_SHIFT_MASK) &&
354
 
          rect_sel->fixed_mode == GIMP_RECT_SELECT_MODE_FREE)
355
 
        {
356
 
          if (rect_sel->w > rect_sel->h)
357
 
            {
358
 
              rect_sel->h = rect_sel->w;
359
 
            }
360
 
          else
361
 
            {
362
 
              rect_sel->w = rect_sel->h;
363
 
            }
364
 
        }
365
 
 
366
 
      if (state & GDK_CONTROL_MASK)
367
 
        {
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)
370
 
            {
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);
374
 
            }
375
 
          else
376
 
            {
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;
382
 
            }
383
 
        }
384
 
      else
385
 
        {
386
 
          /*  Make (rect->x, rect->y) upper left hand point of selection  */
387
 
          if (coords->x > rect_sel->sx)
388
 
            {
389
 
              rect_sel->x = rect_sel->sx;
390
 
            }
391
 
          else
392
 
            {
393
 
              rect_sel->x = rect_sel->sx - rect_sel->w;
394
 
            }
395
 
          if (coords->y > rect_sel->sy)
396
 
            {
397
 
              rect_sel->y = rect_sel->sy;
398
 
            }
399
 
          else
400
 
            {
401
 
              rect_sel->y = rect_sel->sy - rect_sel->h;
402
 
            }
403
 
        }
404
 
    }
405
 
 
406
 
  rect_sel->lx = RINT(coords->x);
407
 
  rect_sel->ly = RINT(coords->y);
408
 
  
409
 
  gimp_rect_select_tool_update_options (rect_sel, gdisp);
410
 
 
411
 
  gimp_tool_pop_status (tool);
412
 
 
413
 
  gimp_tool_push_status_coords (tool,
414
 
                                _("Selection: "),
415
 
                                rect_sel->w,
416
 
                                " x ",
417
 
                                rect_sel->h);
418
 
 
419
 
  gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
420
 
}
421
 
 
422
 
static void
423
 
gimp_rect_select_tool_draw (GimpDrawTool *draw_tool)
424
 
{
425
 
  GimpRectSelectTool *rect_sel = GIMP_RECT_SELECT_TOOL (draw_tool);
426
 
 
427
 
  gimp_draw_tool_draw_rectangle (draw_tool,
428
 
                                 FALSE,
429
 
                                 rect_sel->x, rect_sel->y,
430
 
                                 rect_sel->w, rect_sel->h,
431
 
                                 FALSE);
432
 
}
433
 
 
434
 
static void
435
 
gimp_rect_select_tool_real_rect_select (GimpRectSelectTool *rect_tool,
436
 
                                        gint                x,
437
 
                                        gint                y,
438
 
                                        gint                w,
439
 
                                        gint                h)
440
 
{
441
 
  GimpTool             *tool     = GIMP_TOOL (rect_tool);
442
 
  GimpSelectionTool    *sel_tool = GIMP_SELECTION_TOOL (rect_tool);
443
 
  GimpSelectionOptions *options;
444
 
 
445
 
  options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
446
 
 
447
 
  gimp_channel_select_rectangle (gimp_image_get_mask (tool->gdisp->gimage),
448
 
                                 x, y, w, h,
449
 
                                 sel_tool->op,
450
 
                                 options->feather,
451
 
                                 options->feather_radius,
452
 
                                 options->feather_radius);
453
 
}
454
 
 
455
 
void
456
 
gimp_rect_select_tool_rect_select (GimpRectSelectTool *rect_tool,
457
 
                                   gint                x,
458
 
                                   gint                y,
459
 
                                   gint                w,
460
 
                                   gint                h)
461
 
{
462
 
  GimpTool             *tool;
463
 
  GimpSelectionOptions *options;
464
 
 
465
 
  g_return_if_fail (GIMP_IS_RECT_SELECT_TOOL (rect_tool));
466
 
 
467
 
  tool    = GIMP_TOOL (rect_tool);
468
 
  options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);
469
 
 
470
 
  if (options->auto_shrink)
471
 
    {
472
 
      gint off_x = 0;
473
 
      gint off_y = 0;
474
 
      gint x2, y2;
475
 
 
476
 
      if (! gimp_rectangle_intersect (x, y, w, h,
477
 
                                      0, 0,
478
 
                                      tool->gdisp->gimage->width,
479
 
                                      tool->gdisp->gimage->height,
480
 
                                      &x, &y, &w, &h))
481
 
        {
482
 
          return;
483
 
        }
484
 
 
485
 
      if (! options->shrink_merged)
486
 
        {
487
 
          GimpItem *item;
488
 
          gint      width, height;
489
 
 
490
 
          item = GIMP_ITEM (gimp_image_active_drawable (tool->gdisp->gimage));
491
 
 
492
 
          gimp_item_offsets (item, &off_x, &off_y);
493
 
          width  = gimp_item_width  (item);
494
 
          height = gimp_item_height (item);
495
 
 
496
 
          if (! gimp_rectangle_intersect (x, y, w, h,
497
 
                                          off_x, off_y, width, height,
498
 
                                          &x, &y, &w, &h))
499
 
            {
500
 
              return;
501
 
            }
502
 
 
503
 
          x -= off_x;
504
 
          y -= off_y;
505
 
        }
506
 
 
507
 
      if (gimp_image_crop_auto_shrink (tool->gdisp->gimage,
508
 
                                       x, y,
509
 
                                       x + w, y + h,
510
 
                                       ! options->shrink_merged,
511
 
                                       &x, &y,
512
 
                                       &x2, &y2))
513
 
        {
514
 
          w = x2 - x;
515
 
          h = y2 - y;
516
 
        }
517
 
 
518
 
      x += off_x;
519
 
      y += off_y;
520
 
    }
521
 
 
522
 
  GIMP_RECT_SELECT_TOOL_GET_CLASS (rect_tool)->rect_select (rect_tool,
523
 
                                                            x, y, w, h);
524
 
}
525
 
 
526
 
static void
527
 
gimp_rect_select_tool_update_options (GimpRectSelectTool *rect_sel,
528
 
                                      GimpDisplay        *gdisp)
529
 
{
530
 
  GimpDisplayShell *shell;
531
 
  gdouble           width;
532
 
  gdouble           height;
533
 
 
534
 
  if (rect_sel->fixed_mode != GIMP_RECT_SELECT_MODE_FREE)
535
 
    return;
536
 
 
537
 
  shell = GIMP_DISPLAY_SHELL (gdisp->shell);
538
 
 
539
 
  if (shell->unit == GIMP_UNIT_PIXEL)
540
 
    {
541
 
      width  = rect_sel->w;
542
 
      height = rect_sel->h;
543
 
    }
544
 
  else
545
 
    {
546
 
      GimpImage *image = gdisp->gimage;
547
 
 
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);
554
 
    }
555
 
 
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,
560
 
                NULL);
561
 
}