4 #include <libgnomecanvas/libgnomecanvas.h>
6 #include "canvas-simpleline.h"
7 #include "rgb_macros.h"
9 #define _(Text) dgettext (PACKAGE,Text)
20 static void gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass
*class);
22 static void gnome_canvas_simpleline_init (GnomeCanvasSimpleLine
*simpleline
);
24 static void gnome_canvas_simpleline_destroy (GtkObject
*object
);
26 static void gnome_canvas_simpleline_set_property (GObject
*object
,
30 static void gnome_canvas_simpleline_get_property (GObject
*object
,
35 static void gnome_canvas_simpleline_update (GnomeCanvasItem
*item
,
40 static void gnome_canvas_simpleline_bounds (GnomeCanvasItem
*item
,
46 static double gnome_canvas_simpleline_point (GnomeCanvasItem
*item
,
51 GnomeCanvasItem
**actual_item
);
53 static void gnome_canvas_simpleline_render (GnomeCanvasItem
*item
,
56 static void gnome_canvas_simpleline_draw (GnomeCanvasItem
*item
,
57 GdkDrawable
*drawable
,
63 static GnomeCanvasItemClass
*parent_class
;
67 gnome_canvas_simpleline_get_type (void)
69 static GType simpleline_type
;
71 if (!simpleline_type
) {
72 static const GTypeInfo object_info
= {
73 sizeof (GnomeCanvasSimpleLineClass
),
75 (GBaseFinalizeFunc
) NULL
,
76 (GClassInitFunc
) gnome_canvas_simpleline_class_init
,
77 (GClassFinalizeFunc
) NULL
,
78 NULL
, /* class_data */
79 sizeof (GnomeCanvasSimpleLine
),
81 (GInstanceInitFunc
) gnome_canvas_simpleline_init
,
82 NULL
/* value_table */
85 simpleline_type
= g_type_register_static (GNOME_TYPE_CANVAS_ITEM
, "GnomeCanvasSimpleLine",
89 return simpleline_type
;
93 gnome_canvas_simpleline_class_init (GnomeCanvasSimpleLineClass
*class)
95 GObjectClass
*gobject_class
;
96 GtkObjectClass
*object_class
;
97 GnomeCanvasItemClass
*item_class
;
99 gobject_class
= (GObjectClass
*) class;
100 object_class
= (GtkObjectClass
*) class;
101 item_class
= (GnomeCanvasItemClass
*) class;
103 parent_class
= g_type_class_peek_parent (class);
105 gobject_class
->set_property
= gnome_canvas_simpleline_set_property
;
106 gobject_class
->get_property
= gnome_canvas_simpleline_get_property
;
108 g_object_class_install_property (gobject_class
,
110 g_param_spec_double ("x1",
112 _("x coordinate of upper left corner of rect"),
118 g_object_class_install_property (gobject_class
,
120 g_param_spec_double ("y1",
122 _("y coordinate of upper left corner of rect "),
129 g_object_class_install_property (gobject_class
,
131 g_param_spec_double ("x2",
133 _("x coordinate of lower right corner of rect"),
139 g_object_class_install_property (gobject_class
,
141 g_param_spec_double ("y2",
143 _("y coordinate of lower right corner of rect "),
148 g_object_class_install_property (gobject_class
,
150 g_param_spec_uint ("color_rgba",
158 object_class
->destroy
= gnome_canvas_simpleline_destroy
;
160 item_class
->update
= gnome_canvas_simpleline_update
;
161 item_class
->bounds
= gnome_canvas_simpleline_bounds
;
162 item_class
->point
= gnome_canvas_simpleline_point
;
163 item_class
->render
= gnome_canvas_simpleline_render
;
164 item_class
->draw
= gnome_canvas_simpleline_draw
;
168 gnome_canvas_simpleline_init (GnomeCanvasSimpleLine
*simpleline
)
170 simpleline
->x1
= 0.0;
171 simpleline
->y1
= 0.0;
172 simpleline
->x2
= 0.0;
173 simpleline
->y2
= 0.0;
174 simpleline
->color
= RGBA_TO_UINT(98,123,174,241);
175 simpleline
->horizontal
= TRUE
; /* reset in the _update() method */
179 gnome_canvas_simpleline_destroy (GtkObject
*object
)
181 GnomeCanvasSimpleLine
*line
;
183 g_return_if_fail (object
!= NULL
);
184 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object
));
186 line
= GNOME_CANVAS_SIMPLELINE (object
);
188 if (GTK_OBJECT_CLASS (parent_class
)->destroy
)
189 (* GTK_OBJECT_CLASS (parent_class
)->destroy
) (object
);
193 gnome_canvas_simpleline_bounds_world (GnomeCanvasItem
*item
, int* ix1
, int* iy1
, int* ix2
, int* iy2
)
195 double x1
, x2
, y1
, y2
;
199 GnomeCanvasSimpleLine
*simpleline
= GNOME_CANVAS_SIMPLELINE(item
);
201 gnome_canvas_simpleline_bounds (item
, &x1
, &y1
, &x2
, &y2
);
208 gnome_canvas_item_i2w_affine (item
, i2w
);
209 art_affine_point (&w1
, &i1
, i2w
);
210 art_affine_point (&w2
, &i2
, i2w
);
212 *ix1
= (int) rint(w1
.x
);
213 *ix2
= (int) rint(w2
.x
);
214 *iy1
= (int) rint(w1
.y
);
215 *iy2
= (int) rint(w2
.y
);
217 /* the update rect has to be of non-zero width and height */
220 simpleline
->horizontal
= FALSE
;
223 simpleline
->horizontal
= TRUE
;
229 gnome_canvas_simpleline_reset_bounds (GnomeCanvasItem
*item
)
231 int Ix1
, Ix2
, Iy1
, Iy2
;
233 gnome_canvas_simpleline_bounds_world (item
, &Ix1
, &Iy1
, &Ix2
, &Iy2
);
234 gnome_canvas_update_bbox (item
, Ix1
, Iy1
, Ix2
, Iy2
);
242 gnome_canvas_simpleline_set_property (GObject
*object
,
248 GnomeCanvasSimpleLine
*simpleline
;
250 int bounds_changed
= FALSE
;
252 g_return_if_fail (object
!= NULL
);
253 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object
));
255 simpleline
= GNOME_CANVAS_SIMPLELINE (object
);
259 if (simpleline
->x1
!= g_value_get_double (value
)) {
260 simpleline
->x1
= g_value_get_double (value
);
261 bounds_changed
= TRUE
;
266 if (simpleline
->y1
!= g_value_get_double (value
)) {
267 simpleline
->y1
= g_value_get_double (value
);
268 bounds_changed
= TRUE
;
273 if (simpleline
->x2
!= g_value_get_double (value
)) {
274 simpleline
->x2
= g_value_get_double (value
);
275 bounds_changed
= TRUE
;
280 if (simpleline
->y2
!= g_value_get_double (value
)) {
281 simpleline
->y2
= g_value_get_double (value
);
282 bounds_changed
= TRUE
;
286 case PROP_COLOR_RGBA
:
287 if (simpleline
->color
!= g_value_get_uint(value
)) {
288 simpleline
->color
= g_value_get_uint(value
);
289 UINT_TO_RGBA (simpleline
->color
, &simpleline
->r
, &simpleline
->g
, &simpleline
->b
, &simpleline
->a
);
297 if (update
|| bounds_changed
) {
298 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object
));
303 gnome_canvas_simpleline_get_property (GObject
*object
,
308 g_return_if_fail (object
!= NULL
);
309 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object
));
311 GnomeCanvasSimpleLine
*line
= GNOME_CANVAS_SIMPLELINE (object
);
315 g_value_set_double (value
, line
->x1
);
318 g_value_set_double (value
, line
->x2
);
321 g_value_set_double (value
, line
->y1
);
324 g_value_set_double (value
, line
->y2
);
326 case PROP_COLOR_RGBA
:
327 g_value_set_uint (value
, line
->color
);
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
336 gnome_canvas_simpleline_update (GnomeCanvasItem
*item
, double *affine
, ArtSVP
*clip_path
, int flags
)
338 GnomeCanvasSimpleLine
*simpleline
;
342 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
344 if (parent_class
->update
)
345 (* parent_class
->update
) (item
, affine
, clip_path
, flags
);
347 gnome_canvas_simpleline_reset_bounds (item
);
352 gnome_canvas_item_i2w (item
, &x
, &y
);
353 gnome_canvas_w2c (GNOME_CANVAS(item
->canvas
), x
, y
, &simpleline
->bbox_ulx
, &simpleline
->bbox_uly
);
358 gnome_canvas_item_i2w (item
, &x
, &y
);
359 gnome_canvas_w2c (GNOME_CANVAS(item
->canvas
), x
, y
, &simpleline
->bbox_lrx
, &simpleline
->bbox_lry
);
363 gnome_canvas_simpleline_render (GnomeCanvasItem
*item
,
366 GnomeCanvasSimpleLine
*simpleline
;
369 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
371 if (parent_class
->render
) {
372 (*parent_class
->render
) (item
, buf
);
376 gnome_canvas_buf_ensure_buf (buf
);
380 //begin = MAX(simpleline->bbox_ulx,buf->rect.x0);
381 //end = MIN(simpleline->bbox_lrx,buf->rect.x1);
383 begin
= simpleline
->bbox_ulx
;
384 end
= simpleline
->bbox_lrx
;
386 if (simpleline
->color
!= 0) {
387 if (simpleline
->horizontal
) {
388 PAINT_HORIZA(buf
, simpleline
->r
, simpleline
->g
, simpleline
->b
, simpleline
->a
,
389 begin
, end
, simpleline
->bbox_uly
);
391 PAINT_VERTA(buf
, simpleline
->r
, simpleline
->g
, simpleline
->b
, simpleline
->a
,
392 begin
, simpleline
->bbox_uly
, simpleline
->bbox_lry
);
398 gnome_canvas_simpleline_draw (GnomeCanvasItem
*item
,
399 GdkDrawable
*drawable
,
401 int width
, int height
)
403 GnomeCanvasSimpleLine
*simpleline
;
410 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
412 cr
= gdk_cairo_create (drawable
);
414 if (x
> simpleline
->bbox_ulx
) {
417 ulx
= simpleline
->bbox_ulx
;
420 if (y
> simpleline
->bbox_uly
) {
423 uly
= simpleline
->bbox_uly
;
426 if (x
+ width
> simpleline
->bbox_lrx
) {
427 lrx
= simpleline
->bbox_lrx
;
432 if (y
+ height
> simpleline
->bbox_lry
) {
433 lry
= simpleline
->bbox_lry
;
443 cairo_set_source_rgba (cr
,
447 simpleline
->a
/255.0);
448 cairo_set_line_width (cr
, 1);
449 cairo_move_to (cr
, ulx
+0.5, uly
+0.5);
450 cairo_line_to (cr
, lrx
+0.5, lry
+0.5);
455 gnome_canvas_simpleline_bounds (GnomeCanvasItem
*item
, double *x1
, double *y1
, double *x2
, double *y2
)
457 GnomeCanvasSimpleLine
*simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
459 *x1
= simpleline
->x1
;
460 *y1
= simpleline
->y1
;
461 *x2
= simpleline
->x2
;
462 *y2
= simpleline
->y2
;
466 gnome_canvas_simpleline_point (GnomeCanvasItem
*item
, double x
, double y
, int cx
, int cy
, GnomeCanvasItem
**actual_item
)
468 GnomeCanvasSimpleLine
*simpleline
;
469 double x1
, y1
, x2
, y2
;
472 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
476 /* Find the bounds for the line plus its outline width */
478 gnome_canvas_simpleline_bounds (item
, &x1
, &y1
, &x2
, &y2
);
480 /* Is point inside line */
482 if ((x
>= x1
) && (y
>= y1
) && (x
<= x2
) && (y
<= y2
)) {
486 /* Point is outside line */
502 return sqrt (dx
* dx
+ dy
* dy
);