~ubuntu-branches/ubuntu/vivid/gimp/vivid

« back to all changes in this revision

Viewing changes to app/display/gimpcanvasrectangle.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-08 18:50:03 UTC
  • mto: (1.1.26) (0.5.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: package-import@ubuntu.com-20120508185003-tltkvbaysf8d2426
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimpcanvasrectangle.c
 
5
 * Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <gegl.h>
 
24
#include <gtk/gtk.h>
 
25
 
 
26
#include "libgimpbase/gimpbase.h"
 
27
#include "libgimpmath/gimpmath.h"
 
28
 
 
29
#include "display-types.h"
 
30
 
 
31
#include "gimpcanvasrectangle.h"
 
32
#include "gimpdisplayshell.h"
 
33
#include "gimpdisplayshell-transform.h"
 
34
 
 
35
 
 
36
enum
 
37
{
 
38
  PROP_0,
 
39
  PROP_X,
 
40
  PROP_Y,
 
41
  PROP_WIDTH,
 
42
  PROP_HEIGHT,
 
43
  PROP_FILLED
 
44
};
 
45
 
 
46
 
 
47
typedef struct _GimpCanvasRectanglePrivate GimpCanvasRectanglePrivate;
 
48
 
 
49
struct _GimpCanvasRectanglePrivate
 
50
{
 
51
  gdouble  x;
 
52
  gdouble  y;
 
53
  gdouble  width;
 
54
  gdouble  height;
 
55
  gboolean filled;
 
56
};
 
57
 
 
58
#define GET_PRIVATE(rectangle) \
 
59
        G_TYPE_INSTANCE_GET_PRIVATE (rectangle, \
 
60
                                     GIMP_TYPE_CANVAS_RECTANGLE, \
 
61
                                     GimpCanvasRectanglePrivate)
 
62
 
 
63
 
 
64
/*  local function prototypes  */
 
65
 
 
66
static void             gimp_canvas_rectangle_set_property (GObject          *object,
 
67
                                                            guint             property_id,
 
68
                                                            const GValue     *value,
 
69
                                                            GParamSpec       *pspec);
 
70
static void             gimp_canvas_rectangle_get_property (GObject          *object,
 
71
                                                            guint             property_id,
 
72
                                                            GValue           *value,
 
73
                                                            GParamSpec       *pspec);
 
74
static void             gimp_canvas_rectangle_draw         (GimpCanvasItem   *item,
 
75
                                                            GimpDisplayShell *shell,
 
76
                                                            cairo_t          *cr);
 
77
static cairo_region_t * gimp_canvas_rectangle_get_extents  (GimpCanvasItem   *item,
 
78
                                                            GimpDisplayShell *shell);
 
79
 
 
80
 
 
81
G_DEFINE_TYPE (GimpCanvasRectangle, gimp_canvas_rectangle,
 
82
               GIMP_TYPE_CANVAS_ITEM)
 
83
 
 
84
#define parent_class gimp_canvas_rectangle_parent_class
 
85
 
 
86
 
 
87
static void
 
88
gimp_canvas_rectangle_class_init (GimpCanvasRectangleClass *klass)
 
89
{
 
90
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
 
91
  GimpCanvasItemClass *item_class   = GIMP_CANVAS_ITEM_CLASS (klass);
 
92
 
 
93
  object_class->set_property = gimp_canvas_rectangle_set_property;
 
94
  object_class->get_property = gimp_canvas_rectangle_get_property;
 
95
 
 
96
  item_class->draw           = gimp_canvas_rectangle_draw;
 
97
  item_class->get_extents    = gimp_canvas_rectangle_get_extents;
 
98
 
 
99
  g_object_class_install_property (object_class, PROP_X,
 
100
                                   g_param_spec_double ("x", NULL, NULL,
 
101
                                                        -GIMP_MAX_IMAGE_SIZE,
 
102
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
103
                                                        GIMP_PARAM_READWRITE));
 
104
 
 
105
  g_object_class_install_property (object_class, PROP_Y,
 
106
                                   g_param_spec_double ("y", NULL, NULL,
 
107
                                                        -GIMP_MAX_IMAGE_SIZE,
 
108
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
109
                                                        GIMP_PARAM_READWRITE));
 
110
 
 
111
  g_object_class_install_property (object_class, PROP_WIDTH,
 
112
                                   g_param_spec_double ("width", NULL, NULL,
 
113
                                                        -GIMP_MAX_IMAGE_SIZE,
 
114
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
115
                                                        GIMP_PARAM_READWRITE));
 
116
 
 
117
  g_object_class_install_property (object_class, PROP_HEIGHT,
 
118
                                   g_param_spec_double ("height", NULL, NULL,
 
119
                                                        -GIMP_MAX_IMAGE_SIZE,
 
120
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
121
                                                        GIMP_PARAM_READWRITE));
 
122
 
 
123
  g_object_class_install_property (object_class, PROP_FILLED,
 
124
                                   g_param_spec_boolean ("filled", NULL, NULL,
 
125
                                                         FALSE,
 
126
                                                         GIMP_PARAM_READWRITE));
 
127
 
 
128
  g_type_class_add_private (klass, sizeof (GimpCanvasRectanglePrivate));
 
129
}
 
130
 
 
131
static void
 
132
gimp_canvas_rectangle_init (GimpCanvasRectangle *rectangle)
 
133
{
 
134
}
 
135
 
 
136
static void
 
137
gimp_canvas_rectangle_set_property (GObject      *object,
 
138
                                    guint         property_id,
 
139
                                    const GValue *value,
 
140
                                    GParamSpec   *pspec)
 
141
{
 
142
  GimpCanvasRectanglePrivate *private = GET_PRIVATE (object);
 
143
 
 
144
  switch (property_id)
 
145
    {
 
146
    case PROP_X:
 
147
      private->x = g_value_get_double (value);
 
148
      break;
 
149
    case PROP_Y:
 
150
      private->y = g_value_get_double (value);
 
151
      break;
 
152
    case PROP_WIDTH:
 
153
      private->width = g_value_get_double (value);
 
154
      break;
 
155
    case PROP_HEIGHT:
 
156
      private->height = g_value_get_double (value);
 
157
      break;
 
158
    case PROP_FILLED:
 
159
      private->filled = g_value_get_boolean (value);
 
160
      break;
 
161
 
 
162
    default:
 
163
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
164
      break;
 
165
    }
 
166
}
 
167
 
 
168
static void
 
169
gimp_canvas_rectangle_get_property (GObject    *object,
 
170
                                    guint       property_id,
 
171
                                    GValue     *value,
 
172
                                    GParamSpec *pspec)
 
173
{
 
174
  GimpCanvasRectanglePrivate *private = GET_PRIVATE (object);
 
175
 
 
176
  switch (property_id)
 
177
    {
 
178
    case PROP_X:
 
179
      g_value_set_double (value, private->x);
 
180
      break;
 
181
    case PROP_Y:
 
182
      g_value_set_double (value, private->y);
 
183
      break;
 
184
    case PROP_WIDTH:
 
185
      g_value_set_double (value, private->width);
 
186
      break;
 
187
    case PROP_HEIGHT:
 
188
      g_value_set_double (value, private->height);
 
189
      break;
 
190
    case PROP_FILLED:
 
191
      g_value_set_boolean (value, private->filled);
 
192
      break;
 
193
 
 
194
    default:
 
195
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
196
      break;
 
197
    }
 
198
}
 
199
 
 
200
static void
 
201
gimp_canvas_rectangle_transform (GimpCanvasItem   *item,
 
202
                                 GimpDisplayShell *shell,
 
203
                                 gdouble          *x,
 
204
                                 gdouble          *y,
 
205
                                 gdouble          *w,
 
206
                                 gdouble          *h)
 
207
{
 
208
  GimpCanvasRectanglePrivate *private = GET_PRIVATE (item);
 
209
  gdouble                     x1, y1;
 
210
  gdouble                     x2, y2;
 
211
 
 
212
  gimp_display_shell_transform_xy_f (shell,
 
213
                                     MIN (private->x,
 
214
                                          private->x + private->width),
 
215
                                     MIN (private->y,
 
216
                                          private->y + private->height),
 
217
                                     &x1, &y1);
 
218
  gimp_display_shell_transform_xy_f (shell,
 
219
                                     MAX (private->x,
 
220
                                          private->x + private->width),
 
221
                                     MAX (private->y,
 
222
                                          private->y + private->height),
 
223
                                     &x2, &y2);
 
224
 
 
225
  x1 = floor (x1);
 
226
  y1 = floor (y1);
 
227
  x2 = ceil (x2);
 
228
  y2 = ceil (y2);
 
229
 
 
230
  if (private->filled)
 
231
    {
 
232
      *x = x1;
 
233
      *y = y1;
 
234
      *w = x2 - x1;
 
235
      *h = y2 - y1;
 
236
    }
 
237
  else
 
238
    {
 
239
      *x = x1 + 0.5;
 
240
      *y = y1 + 0.5;
 
241
      *w = x2 - 0.5 - *x;
 
242
      *h = y2 - 0.5 - *y;
 
243
 
 
244
      *w = MAX (0.0, *w);
 
245
      *h = MAX (0.0, *h);
 
246
    }
 
247
}
 
248
 
 
249
static void
 
250
gimp_canvas_rectangle_draw (GimpCanvasItem   *item,
 
251
                            GimpDisplayShell *shell,
 
252
                            cairo_t          *cr)
 
253
{
 
254
  GimpCanvasRectanglePrivate *private = GET_PRIVATE (item);
 
255
  gdouble                     x, y;
 
256
  gdouble                     w, h;
 
257
 
 
258
  gimp_canvas_rectangle_transform (item, shell, &x, &y, &w, &h);
 
259
 
 
260
  cairo_rectangle (cr, x, y, w, h);
 
261
 
 
262
  if (private->filled)
 
263
    _gimp_canvas_item_fill (item, cr);
 
264
  else
 
265
    _gimp_canvas_item_stroke (item, cr);
 
266
}
 
267
 
 
268
static cairo_region_t *
 
269
gimp_canvas_rectangle_get_extents (GimpCanvasItem   *item,
 
270
                                   GimpDisplayShell *shell)
 
271
{
 
272
  GimpCanvasRectanglePrivate *private = GET_PRIVATE (item);
 
273
  cairo_rectangle_int_t       rectangle;
 
274
  gdouble                     x, y;
 
275
  gdouble                     w, h;
 
276
 
 
277
  gimp_canvas_rectangle_transform (item, shell, &x, &y, &w, &h);
 
278
 
 
279
  if (private->filled)
 
280
    {
 
281
      rectangle.x      = floor (x - 1.0);
 
282
      rectangle.y      = floor (y - 1.0);
 
283
      rectangle.width  = ceil (w + 2.0);
 
284
      rectangle.height = ceil (h + 2.0);
 
285
 
 
286
      return cairo_region_create_rectangle (&rectangle);
 
287
    }
 
288
  else if (w > 64 && h > 64)
 
289
    {
 
290
      cairo_region_t *region;
 
291
 
 
292
      /* left */
 
293
      rectangle.x      = floor (x - 1.5);
 
294
      rectangle.y      = floor (y - 1.5);
 
295
      rectangle.width  = 3.0;
 
296
      rectangle.height = ceil (h + 3.0);
 
297
 
 
298
      region = cairo_region_create_rectangle (&rectangle);
 
299
 
 
300
      /* right */
 
301
      rectangle.x      = floor (x + w - 1.5);
 
302
 
 
303
      cairo_region_union_rectangle (region, &rectangle);
 
304
 
 
305
      /* top */
 
306
      rectangle.x      = floor (x - 1.5);
 
307
      rectangle.y      = floor (y - 1.5);
 
308
      rectangle.width  = ceil (w + 3.0);
 
309
      rectangle.height = 3.0;
 
310
 
 
311
      cairo_region_union_rectangle (region, &rectangle);
 
312
 
 
313
      /* bottom */
 
314
      rectangle.y      = floor (y + h - 1.5);
 
315
 
 
316
      cairo_region_union_rectangle (region, &rectangle);
 
317
 
 
318
      return region;
 
319
    }
 
320
  else
 
321
    {
 
322
      rectangle.x      = floor (x - 1.5);
 
323
      rectangle.y      = floor (y - 1.5);
 
324
      rectangle.width  = ceil (w + 3.0);
 
325
      rectangle.height = ceil (h + 3.0);
 
326
 
 
327
      return cairo_region_create_rectangle (&rectangle);
 
328
    }
 
329
}
 
330
 
 
331
GimpCanvasItem *
 
332
gimp_canvas_rectangle_new (GimpDisplayShell *shell,
 
333
                           gdouble           x,
 
334
                           gdouble           y,
 
335
                           gdouble           width,
 
336
                           gdouble           height,
 
337
                           gboolean          filled)
 
338
{
 
339
  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);
 
340
 
 
341
  return g_object_new (GIMP_TYPE_CANVAS_RECTANGLE,
 
342
                       "shell",  shell,
 
343
                       "x",      x,
 
344
                       "y",      y,
 
345
                       "width",  width,
 
346
                       "height", height,
 
347
                       "filled", filled,
 
348
                       NULL);
 
349
}
 
350
 
 
351
void
 
352
gimp_canvas_rectangle_set (GimpCanvasItem *rectangle,
 
353
                           gdouble         x,
 
354
                           gdouble         y,
 
355
                           gdouble         width,
 
356
                           gdouble         height)
 
357
{
 
358
  g_return_if_fail (GIMP_IS_CANVAS_RECTANGLE (rectangle));
 
359
 
 
360
  gimp_canvas_item_begin_change (rectangle);
 
361
 
 
362
  g_object_set (rectangle,
 
363
                "x",      x,
 
364
                "y",      y,
 
365
                "width",  width,
 
366
                "height", height,
 
367
                NULL);
 
368
 
 
369
  gimp_canvas_item_end_change (rectangle);
 
370
}