1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
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.
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.
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/>.
26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpmath/gimpmath.h"
29
#include "display-types.h"
31
#include "gimpcanvasline.h"
32
#include "gimpdisplayshell.h"
33
#include "gimpdisplayshell-transform.h"
46
typedef struct _GimpCanvasLinePrivate GimpCanvasLinePrivate;
48
struct _GimpCanvasLinePrivate
56
#define GET_PRIVATE(line) \
57
G_TYPE_INSTANCE_GET_PRIVATE (line, \
58
GIMP_TYPE_CANVAS_LINE, \
59
GimpCanvasLinePrivate)
62
/* local function prototypes */
64
static void gimp_canvas_line_set_property (GObject *object,
68
static void gimp_canvas_line_get_property (GObject *object,
72
static void gimp_canvas_line_draw (GimpCanvasItem *item,
73
GimpDisplayShell *shell,
75
static cairo_region_t * gimp_canvas_line_get_extents (GimpCanvasItem *item,
76
GimpDisplayShell *shell);
79
G_DEFINE_TYPE (GimpCanvasLine, gimp_canvas_line, GIMP_TYPE_CANVAS_ITEM)
81
#define parent_class gimp_canvas_line_parent_class
85
gimp_canvas_line_class_init (GimpCanvasLineClass *klass)
87
GObjectClass *object_class = G_OBJECT_CLASS (klass);
88
GimpCanvasItemClass *item_class = GIMP_CANVAS_ITEM_CLASS (klass);
90
object_class->set_property = gimp_canvas_line_set_property;
91
object_class->get_property = gimp_canvas_line_get_property;
93
item_class->draw = gimp_canvas_line_draw;
94
item_class->get_extents = gimp_canvas_line_get_extents;
96
g_object_class_install_property (object_class, PROP_X1,
97
g_param_spec_double ("x1", NULL, NULL,
99
GIMP_MAX_IMAGE_SIZE, 0,
100
GIMP_PARAM_READWRITE));
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));
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));
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));
120
g_type_class_add_private (klass, sizeof (GimpCanvasLinePrivate));
124
gimp_canvas_line_init (GimpCanvasLine *line)
129
gimp_canvas_line_set_property (GObject *object,
134
GimpCanvasLinePrivate *private = GET_PRIVATE (object);
139
private->x1 = g_value_get_double (value);
142
private->y1 = g_value_get_double (value);
145
private->x2 = g_value_get_double (value);
148
private->y2 = g_value_get_double (value);
152
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158
gimp_canvas_line_get_property (GObject *object,
163
GimpCanvasLinePrivate *private = GET_PRIVATE (object);
168
g_value_set_double (value, private->x1);
171
g_value_set_double (value, private->y1);
174
g_value_set_double (value, private->x2);
177
g_value_set_double (value, private->y2);
181
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
187
gimp_canvas_line_transform (GimpCanvasItem *item,
188
GimpDisplayShell *shell,
194
GimpCanvasLinePrivate *private = GET_PRIVATE (item);
196
gimp_display_shell_transform_xy_f (shell,
197
private->x1, private->y1,
199
gimp_display_shell_transform_xy_f (shell,
200
private->x2, private->y2,
203
*x1 = floor (*x1) + 0.5;
204
*y1 = floor (*y1) + 0.5;
205
*x2 = floor (*x2) + 0.5;
206
*y2 = floor (*y2) + 0.5;
210
gimp_canvas_line_draw (GimpCanvasItem *item,
211
GimpDisplayShell *shell,
217
gimp_canvas_line_transform (item, shell, &x1, &y1, &x2, &y2);
219
cairo_move_to (cr, x1, y1);
220
cairo_line_to (cr, x2, y2);
222
_gimp_canvas_item_stroke (item, cr);
225
static cairo_region_t *
226
gimp_canvas_line_get_extents (GimpCanvasItem *item,
227
GimpDisplayShell *shell)
229
cairo_rectangle_int_t rectangle;
233
gimp_canvas_line_transform (item, shell, &x1, &y1, &x2, &y2);
235
if (x1 == x2 || y1 == y2)
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;
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);
250
return cairo_region_create_rectangle (&rectangle);
254
gimp_canvas_line_new (GimpDisplayShell *shell,
260
g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);
262
return g_object_new (GIMP_TYPE_CANVAS_LINE,
272
gimp_canvas_line_set (GimpCanvasItem *line,
278
g_return_if_fail (GIMP_IS_CANVAS_LINE (line));
280
gimp_canvas_item_begin_change (line);
289
gimp_canvas_item_end_change (line);