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

« back to all changes in this revision

Viewing changes to app/display/gimpcanvasline.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
 * gimpcanvasline.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 "gimpcanvasline.h"
 
32
#include "gimpdisplayshell.h"
 
33
#include "gimpdisplayshell-transform.h"
 
34
 
 
35
 
 
36
enum
 
37
{
 
38
  PROP_0,
 
39
  PROP_X1,
 
40
  PROP_Y1,
 
41
  PROP_X2,
 
42
  PROP_Y2
 
43
};
 
44
 
 
45
 
 
46
typedef struct _GimpCanvasLinePrivate GimpCanvasLinePrivate;
 
47
 
 
48
struct _GimpCanvasLinePrivate
 
49
{
 
50
  gdouble  x1;
 
51
  gdouble  y1;
 
52
  gdouble  x2;
 
53
  gdouble  y2;
 
54
};
 
55
 
 
56
#define GET_PRIVATE(line) \
 
57
        G_TYPE_INSTANCE_GET_PRIVATE (line, \
 
58
                                     GIMP_TYPE_CANVAS_LINE, \
 
59
                                     GimpCanvasLinePrivate)
 
60
 
 
61
 
 
62
/*  local function prototypes  */
 
63
 
 
64
static void             gimp_canvas_line_set_property (GObject          *object,
 
65
                                                       guint             property_id,
 
66
                                                       const GValue     *value,
 
67
                                                       GParamSpec       *pspec);
 
68
static void             gimp_canvas_line_get_property (GObject          *object,
 
69
                                                       guint             property_id,
 
70
                                                       GValue           *value,
 
71
                                                       GParamSpec       *pspec);
 
72
static void             gimp_canvas_line_draw         (GimpCanvasItem   *item,
 
73
                                                       GimpDisplayShell *shell,
 
74
                                                       cairo_t          *cr);
 
75
static cairo_region_t * gimp_canvas_line_get_extents  (GimpCanvasItem   *item,
 
76
                                                       GimpDisplayShell *shell);
 
77
 
 
78
 
 
79
G_DEFINE_TYPE (GimpCanvasLine, gimp_canvas_line, GIMP_TYPE_CANVAS_ITEM)
 
80
 
 
81
#define parent_class gimp_canvas_line_parent_class
 
82
 
 
83
 
 
84
static void
 
85
gimp_canvas_line_class_init (GimpCanvasLineClass *klass)
 
86
{
 
87
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
 
88
  GimpCanvasItemClass *item_class   = GIMP_CANVAS_ITEM_CLASS (klass);
 
89
 
 
90
  object_class->set_property = gimp_canvas_line_set_property;
 
91
  object_class->get_property = gimp_canvas_line_get_property;
 
92
 
 
93
  item_class->draw           = gimp_canvas_line_draw;
 
94
  item_class->get_extents    = gimp_canvas_line_get_extents;
 
95
 
 
96
  g_object_class_install_property (object_class, PROP_X1,
 
97
                                   g_param_spec_double ("x1", NULL, NULL,
 
98
                                                        -GIMP_MAX_IMAGE_SIZE,
 
99
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
100
                                                        GIMP_PARAM_READWRITE));
 
101
 
 
102
  g_object_class_install_property (object_class, PROP_Y1,
 
103
                                   g_param_spec_double ("y1", NULL, NULL,
 
104
                                                        -GIMP_MAX_IMAGE_SIZE,
 
105
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
106
                                                        GIMP_PARAM_READWRITE));
 
107
 
 
108
  g_object_class_install_property (object_class, PROP_X2,
 
109
                                   g_param_spec_double ("x2", NULL, NULL,
 
110
                                                        -GIMP_MAX_IMAGE_SIZE,
 
111
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
112
                                                        GIMP_PARAM_READWRITE));
 
113
 
 
114
  g_object_class_install_property (object_class, PROP_Y2,
 
115
                                   g_param_spec_double ("y2", NULL, NULL,
 
116
                                                        -GIMP_MAX_IMAGE_SIZE,
 
117
                                                        GIMP_MAX_IMAGE_SIZE, 0,
 
118
                                                        GIMP_PARAM_READWRITE));
 
119
 
 
120
  g_type_class_add_private (klass, sizeof (GimpCanvasLinePrivate));
 
121
}
 
122
 
 
123
static void
 
124
gimp_canvas_line_init (GimpCanvasLine *line)
 
125
{
 
126
}
 
127
 
 
128
static void
 
129
gimp_canvas_line_set_property (GObject      *object,
 
130
                               guint         property_id,
 
131
                               const GValue *value,
 
132
                               GParamSpec   *pspec)
 
133
{
 
134
  GimpCanvasLinePrivate *private = GET_PRIVATE (object);
 
135
 
 
136
  switch (property_id)
 
137
    {
 
138
    case PROP_X1:
 
139
      private->x1 = g_value_get_double (value);
 
140
      break;
 
141
    case PROP_Y1:
 
142
      private->y1 = g_value_get_double (value);
 
143
      break;
 
144
    case PROP_X2:
 
145
      private->x2 = g_value_get_double (value);
 
146
      break;
 
147
    case PROP_Y2:
 
148
      private->y2 = g_value_get_double (value);
 
149
      break;
 
150
 
 
151
    default:
 
152
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
153
      break;
 
154
    }
 
155
}
 
156
 
 
157
static void
 
158
gimp_canvas_line_get_property (GObject    *object,
 
159
                               guint       property_id,
 
160
                               GValue     *value,
 
161
                               GParamSpec *pspec)
 
162
{
 
163
  GimpCanvasLinePrivate *private = GET_PRIVATE (object);
 
164
 
 
165
  switch (property_id)
 
166
    {
 
167
    case PROP_X1:
 
168
      g_value_set_double (value, private->x1);
 
169
      break;
 
170
    case PROP_Y1:
 
171
      g_value_set_double (value, private->y1);
 
172
      break;
 
173
    case PROP_X2:
 
174
      g_value_set_double (value, private->x2);
 
175
      break;
 
176
    case PROP_Y2:
 
177
      g_value_set_double (value, private->y2);
 
178
      break;
 
179
 
 
180
    default:
 
181
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
182
      break;
 
183
    }
 
184
}
 
185
 
 
186
static void
 
187
gimp_canvas_line_transform (GimpCanvasItem   *item,
 
188
                            GimpDisplayShell *shell,
 
189
                            gdouble          *x1,
 
190
                            gdouble          *y1,
 
191
                            gdouble          *x2,
 
192
                            gdouble          *y2)
 
193
{
 
194
  GimpCanvasLinePrivate *private = GET_PRIVATE (item);
 
195
 
 
196
  gimp_display_shell_transform_xy_f (shell,
 
197
                                     private->x1, private->y1,
 
198
                                     x1, y1);
 
199
  gimp_display_shell_transform_xy_f (shell,
 
200
                                     private->x2, private->y2,
 
201
                                     x2, y2);
 
202
 
 
203
  *x1 = floor (*x1) + 0.5;
 
204
  *y1 = floor (*y1) + 0.5;
 
205
  *x2 = floor (*x2) + 0.5;
 
206
  *y2 = floor (*y2) + 0.5;
 
207
}
 
208
 
 
209
static void
 
210
gimp_canvas_line_draw (GimpCanvasItem   *item,
 
211
                       GimpDisplayShell *shell,
 
212
                       cairo_t          *cr)
 
213
{
 
214
  gdouble x1, y1;
 
215
  gdouble x2, y2;
 
216
 
 
217
  gimp_canvas_line_transform (item, shell, &x1, &y1, &x2, &y2);
 
218
 
 
219
  cairo_move_to (cr, x1, y1);
 
220
  cairo_line_to (cr, x2, y2);
 
221
 
 
222
  _gimp_canvas_item_stroke (item, cr);
 
223
}
 
224
 
 
225
static cairo_region_t *
 
226
gimp_canvas_line_get_extents (GimpCanvasItem   *item,
 
227
                              GimpDisplayShell *shell)
 
228
{
 
229
  cairo_rectangle_int_t rectangle;
 
230
  gdouble               x1, y1;
 
231
  gdouble               x2, y2;
 
232
 
 
233
  gimp_canvas_line_transform (item, shell, &x1, &y1, &x2, &y2);
 
234
 
 
235
  if (x1 == x2 || y1 == y2)
 
236
    {
 
237
      rectangle.x      = MIN (x1, x2) - 1.5;
 
238
      rectangle.y      = MIN (y1, y2) - 1.5;
 
239
      rectangle.width  = ABS (x2 - x1) + 3.0;
 
240
      rectangle.height = ABS (y2 - y1) + 3.0;
 
241
    }
 
242
  else
 
243
    {
 
244
      rectangle.x      = floor (MIN (x1, x2) - 2.5);
 
245
      rectangle.y      = floor (MIN (y1, y2) - 2.5);
 
246
      rectangle.width  = ceil (ABS (x2 - x1) + 5.0);
 
247
      rectangle.height = ceil (ABS (y2 - y1) + 5.0);
 
248
    }
 
249
 
 
250
  return cairo_region_create_rectangle (&rectangle);
 
251
}
 
252
 
 
253
GimpCanvasItem *
 
254
gimp_canvas_line_new (GimpDisplayShell *shell,
 
255
                      gdouble           x1,
 
256
                      gdouble           y1,
 
257
                      gdouble           x2,
 
258
                      gdouble           y2)
 
259
{
 
260
  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);
 
261
 
 
262
  return g_object_new (GIMP_TYPE_CANVAS_LINE,
 
263
                       "shell", shell,
 
264
                       "x1",    x1,
 
265
                       "y1",    y1,
 
266
                       "x2",    x2,
 
267
                       "y2",    y2,
 
268
                       NULL);
 
269
}
 
270
 
 
271
void
 
272
gimp_canvas_line_set (GimpCanvasItem *line,
 
273
                      gdouble         x1,
 
274
                      gdouble         y1,
 
275
                      gdouble         x2,
 
276
                      gdouble         y2)
 
277
{
 
278
  g_return_if_fail (GIMP_IS_CANVAS_LINE (line));
 
279
 
 
280
  gimp_canvas_item_begin_change (line);
 
281
 
 
282
  g_object_set (line,
 
283
                "x1", x1,
 
284
                "y1", y1,
 
285
                "x2", x2,
 
286
                "y2", y2,
 
287
                NULL);
 
288
 
 
289
  gimp_canvas_item_end_change (line);
 
290
}