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
,
250 GnomeCanvasSimpleLine
*simpleline
;
252 int bounds_changed
= FALSE
;
254 g_return_if_fail (object
!= NULL
);
255 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object
));
257 simpleline
= GNOME_CANVAS_SIMPLELINE (object
);
261 if (simpleline
->x1
!= g_value_get_double (value
)) {
262 simpleline
->x1
= g_value_get_double (value
);
263 bounds_changed
= TRUE
;
268 if (simpleline
->y1
!= g_value_get_double (value
)) {
269 simpleline
->y1
= g_value_get_double (value
);
270 bounds_changed
= TRUE
;
275 if (simpleline
->x2
!= g_value_get_double (value
)) {
276 simpleline
->x2
= g_value_get_double (value
);
277 bounds_changed
= TRUE
;
282 if (simpleline
->y2
!= g_value_get_double (value
)) {
283 simpleline
->y2
= g_value_get_double (value
);
284 bounds_changed
= TRUE
;
288 case PROP_COLOR_RGBA
:
289 if (simpleline
->color
!= g_value_get_uint(value
)) {
290 simpleline
->color
= g_value_get_uint(value
);
291 UINT_TO_RGBA (simpleline
->color
, &simpleline
->r
, &simpleline
->g
, &simpleline
->b
, &simpleline
->a
);
299 if (update
|| bounds_changed
) {
300 gnome_canvas_item_request_update (GNOME_CANVAS_ITEM(object
));
305 gnome_canvas_simpleline_get_property (GObject
*object
,
310 g_return_if_fail (object
!= NULL
);
311 g_return_if_fail (GNOME_IS_CANVAS_SIMPLELINE (object
));
313 GnomeCanvasSimpleLine
*line
= GNOME_CANVAS_SIMPLELINE (object
);
317 g_value_set_double (value
, line
->x1
);
320 g_value_set_double (value
, line
->x2
);
323 g_value_set_double (value
, line
->y1
);
326 g_value_set_double (value
, line
->y2
);
328 case PROP_COLOR_RGBA
:
329 g_value_set_uint (value
, line
->color
);
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
338 gnome_canvas_simpleline_update (GnomeCanvasItem
*item
, double *affine
, ArtSVP
*clip_path
, int flags
)
340 GnomeCanvasSimpleLine
*simpleline
;
344 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
346 if (parent_class
->update
)
347 (* parent_class
->update
) (item
, affine
, clip_path
, flags
);
349 gnome_canvas_simpleline_reset_bounds (item
);
354 gnome_canvas_item_i2w (item
, &x
, &y
);
355 gnome_canvas_w2c (GNOME_CANVAS(item
->canvas
), x
, y
, &simpleline
->bbox_ulx
, &simpleline
->bbox_uly
);
360 gnome_canvas_item_i2w (item
, &x
, &y
);
361 gnome_canvas_w2c (GNOME_CANVAS(item
->canvas
), x
, y
, &simpleline
->bbox_lrx
, &simpleline
->bbox_lry
);
365 gnome_canvas_simpleline_render (GnomeCanvasItem
*item
,
368 GnomeCanvasSimpleLine
*simpleline
;
371 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
373 if (parent_class
->render
) {
374 (*parent_class
->render
) (item
, buf
);
378 gnome_canvas_buf_ensure_buf (buf
);
382 //begin = MAX(simpleline->bbox_ulx,buf->rect.x0);
383 //end = MIN(simpleline->bbox_lrx,buf->rect.x1);
385 begin
= simpleline
->bbox_ulx
;
386 end
= simpleline
->bbox_lrx
;
388 if (simpleline
->color
!= 0) {
389 if (simpleline
->horizontal
) {
390 PAINT_HORIZA(buf
, simpleline
->r
, simpleline
->g
, simpleline
->b
, simpleline
->a
,
391 begin
, end
, simpleline
->bbox_uly
);
393 PAINT_VERTA(buf
, simpleline
->r
, simpleline
->g
, simpleline
->b
, simpleline
->a
,
394 begin
, simpleline
->bbox_uly
, simpleline
->bbox_lry
);
400 gnome_canvas_simpleline_draw (GnomeCanvasItem
*item
,
401 GdkDrawable
*drawable
,
403 int width
, int height
)
405 GnomeCanvasSimpleLine
*simpleline
;
412 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
414 cr
= gdk_cairo_create (drawable
);
416 if (x
> simpleline
->bbox_ulx
) {
419 ulx
= simpleline
->bbox_ulx
;
422 if (y
> simpleline
->bbox_uly
) {
425 uly
= simpleline
->bbox_uly
;
428 if (x
+ width
> simpleline
->bbox_lrx
) {
429 lrx
= simpleline
->bbox_lrx
;
434 if (y
+ height
> simpleline
->bbox_lry
) {
435 lry
= simpleline
->bbox_lry
;
445 cairo_set_source_rgba (cr
,
449 simpleline
->a
/255.0);
450 cairo_set_line_width (cr
, 1);
451 cairo_move_to (cr
, ulx
+0.5, uly
+0.5);
452 cairo_line_to (cr
, lrx
+0.5, lry
+0.5);
457 gnome_canvas_simpleline_bounds (GnomeCanvasItem
*item
, double *x1
, double *y1
, double *x2
, double *y2
)
459 GnomeCanvasSimpleLine
*simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
461 *x1
= simpleline
->x1
;
462 *y1
= simpleline
->y1
;
463 *x2
= simpleline
->x2
;
464 *y2
= simpleline
->y2
;
468 gnome_canvas_simpleline_point (GnomeCanvasItem
*item
, double x
, double y
, int cx
, int cy
, GnomeCanvasItem
**actual_item
)
473 GnomeCanvasSimpleLine
*simpleline
;
474 double x1
, y1
, x2
, y2
;
477 simpleline
= GNOME_CANVAS_SIMPLELINE (item
);
481 /* Find the bounds for the line plus its outline width */
483 gnome_canvas_simpleline_bounds (item
, &x1
, &y1
, &x2
, &y2
);
485 /* Is point inside line */
487 if ((x
>= x1
) && (y
>= y1
) && (x
<= x2
) && (y
<= y2
)) {
491 /* Point is outside line */
507 return sqrt (dx
* dx
+ dy
* dy
);