3 #include <libgnomecanvas/libgnomecanvas.h>
5 #include "canvas-ruler.h"
6 #include "rgb_macros.h"
20 static void gnome_canvas_ruler_class_init (GnomeCanvasRulerClass
*class);
21 static void gnome_canvas_ruler_init (GnomeCanvasRuler
*ruler
);
22 static void gnome_canvas_ruler_set_arg (GObject
*object
,
26 static void gnome_canvas_ruler_get_arg (GObject
*object
,
30 static void gnome_canvas_ruler_update (GnomeCanvasItem
*item
, double *affine
, ArtSVP
*clip_path
, int flags
);
31 static void gnome_canvas_ruler_bounds (GnomeCanvasItem
*item
, double *x1
, double *y1
, double *x2
, double *y2
);
32 static double gnome_canvas_ruler_point (GnomeCanvasItem
*item
, double x
, double y
, int cx
, int cy
, GnomeCanvasItem
**actual_item
);
33 static void gnome_canvas_ruler_render (GnomeCanvasItem
*item
, GnomeCanvasBuf
*buf
);
34 static void gnome_canvas_ruler_draw (GnomeCanvasItem
*item
, GdkDrawable
*drawable
, int x
, int y
, int w
, int h
);
36 static GnomeCanvasItemClass
*parent_class
;
40 gnome_canvas_ruler_get_type (void)
42 static GtkType ruler_type
= 0;
45 GtkTypeInfo ruler_info
= {
47 sizeof (GnomeCanvasRuler
),
48 sizeof (GnomeCanvasRulerClass
),
49 (GtkClassInitFunc
) gnome_canvas_ruler_class_init
,
50 (GtkObjectInitFunc
) gnome_canvas_ruler_init
,
51 NULL
, /* reserved_1 */
52 NULL
, /* reserved_2 */
53 (GtkClassInitFunc
) NULL
56 ruler_type
= gtk_type_unique (gnome_canvas_item_get_type (), &ruler_info
);
63 gnome_canvas_ruler_class_init (GnomeCanvasRulerClass
*class)
65 GtkObjectClass
*object_class
;
66 GnomeCanvasItemClass
*item_class
;
68 object_class
= G_OBJECT_CLASS (class);
69 item_class
= (GnomeCanvasItemClass
*) class;
71 parent_class
= gtk_type_class (gnome_canvas_item_get_type ());
73 object_class
->set_property
= gnome_canvas_ruler_set_property
;
74 object_class
->get_property
= gnome_canvas_ruler_get_property
;
76 g_object_class_install_property (object_class
,
78 g_param_spec_double ("x1",
80 _("x coordinate of upper left corner of rect"),
86 g_object_class_install_property (object_class
,
88 g_param_spec_double ("y1",
90 _("y coordinate of upper left corner of rect "),
97 g_object_class_install_property (object_class
,
99 g_param_spec_double ("x2",
101 _("x coordinate of lower right corner of rect"),
107 g_object_class_install_property (object_class
,
109 g_param_spec_double ("y2",
111 _("y coordinate of lower right corner of rect "),
118 g_object_class_install_property (object_class
,
119 PROP_FRAMES_PER_UNIT
,
120 g_param_spec_long ("frames_per_unit",
121 _("frames_per_unit"),
122 _("frames_per_unit of ruler"),
129 g_object_class_install_property (object_class
,
131 g_param_spec_uint ("fill_color",
140 g_object_class_install_property (object_class
,
142 g_param_spec_uint ("tick_color",
149 item_class
->update
= gnome_canvas_ruler_update
;
150 item_class
->bounds
= gnome_canvas_ruler_bounds
;
151 item_class
->point
= gnome_canvas_ruler_point
;
152 item_class
->render
= gnome_canvas_ruler_render
;
153 item_class
->draw
= gnome_canvas_ruler_draw
;
157 gnome_canvas_ruler_init (GnomeCanvasRuler
*ruler
)
163 ruler
->frames_per_unit
= 1;
164 ruler
->fill_color
= 0;
165 ruler
->tick_color
= 0;
169 gnome_canvas_ruler_reset_bounds (GnomeCanvasItem
*item
)
172 double x1
, x2
, y1
, y2
;
175 int Ix1
, Ix2
, Iy1
, Iy2
;
178 gnome_canvas_ruler_bounds (item
, &x1
, &y1
, &x2
, &y2
);
185 gnome_canvas_item_i2w_affine (item
, i2w
);
186 art_affine_point (&w1
, &i1
, i2w
);
187 art_affine_point (&w2
, &i2
, i2w
);
189 Ix1
= (int) rint(w1
.x
);
190 Ix2
= (int) rint(w2
.x
);
191 Iy1
= (int) rint(w1
.y
);
192 Iy2
= (int) rint(w2
.y
);
194 gnome_canvas_update_bbox (item
, Ix1
, Iy1
, Ix2
, Iy2
);
202 gnome_canvas_ruler_set_property (GObject
*object
,
207 GnomeCanvasItem
*item
;
208 GnomeCanvasRuler
*ruler
;
212 item
= GNOME_CANVAS_ITEM (object
);
213 ruler
= GNOME_CANVAS_RULER (object
);
220 if (ruler
->x1
!= g_value_get_double (value
)) {
221 ruler
->x1
= g_value_get_double (value
);
227 if (ruler
->y1
!= g_value_get_double (value
)) {
228 ruler
->y1
= g_value_get_double (value
);
234 if (ruler
->x2
!= g_value_get_double (value
)) {
235 ruler
->x2
= g_value_get_double (value
);
241 if (ruler
->y2
!= g_value_get_double (value
)) {
242 ruler
->y2
= g_value_get_double (value
);
247 case PROP_FRAMES_PER_UNIT
:
248 if (ruler
->frames_per_unit
!= g_value_get_long(value
)) {
249 ruler
->frames_per_unit
= g_value_get_long(value
);
254 case PROP_FILL_COLOR
:
255 if (ruler
->fill_color
!= g_value_get_uint(value
)) {
256 ruler
->fill_color
= g_value_get_uint(value
);
261 case PROP_TICK_COLOR
:
262 if (ruler
->tick_color
!= g_value_get_uint(value
)) {
263 ruler
->tick_color
= g_value_get_uint(value
);
273 gnome_canvas_ruler_reset_bounds (item
);
277 gnome_canvas_item_request_update (item
);
283 gnome_canvas_ruler_get_property (GObject
*object
,
289 GnomeCanvasRuler
*ruler
;
291 ruler
= GNOME_CANVAS_RULER (object
);
295 g_value_set_double (value
, ruler
->x1
);
298 g_value_set_double (value
, ruler
->y1
);
301 g_value_set_double (value
, ruler
->x2
);
304 g_value_set_double (value
, ruler
->y2
);
306 case PROP_FRAMES_PER_UNIT
:
307 g_value_set_long (value
, ruler
->frames_per_unit
);
309 case PROP_FILL_COLOR
:
310 g_value_set_uint (value
, ruler
->fill_color
);
312 case PROP_TICK_COLOR
:
313 g_value_set_uint (value
, ruler
->tick_color
);
316 arg
->type
= GTK_TYPE_INVALID
;
322 gnome_canvas_ruler_update (GnomeCanvasItem
*item
, double *affine
, ArtSVP
*clip_path
, int flags
)
324 GnomeCanvasRuler
*ruler
;
328 ruler
= GNOME_CANVAS_RULER (item
);
330 if (parent_class
->update
)
331 (* parent_class
->update
) (item
, affine
, clip_path
, flags
);
333 gnome_canvas_ruler_reset_bounds (item
);
338 gnome_canvas_item_i2w (item
, &x
, &y
);
339 gnome_canvas_w2c (GNOME_CANVAS(item
->canvas
), x
, y
, &ruler
->bbox_ulx
, &ruler
->bbox_uly
);
344 gnome_canvas_item_i2w (item
, &x
, &y
);
345 gnome_canvas_w2c (GNOME_CANVAS(item
->canvas
), x
, y
, &ruler
->bbox_lrx
, &ruler
->bbox_lry
);
347 UINT_TO_RGB (ruler
->tick_color
, &ruler
->tick_r
, &ruler
->tick_g
, &ruler
->tick_b
);
348 UINT_TO_RGB (ruler
->fill_color
, &ruler
->fill_r
, &ruler
->fill_g
, &ruler
->fill_b
);
352 gnome_canvas_ruler_render (GnomeCanvasItem
*item
,
355 GnomeCanvasRuler
*ruler
;
358 ruler
= GNOME_CANVAS_RULER (item
);
360 if (parent_class
->render
) {
361 (*parent_class
->render
) (item
, buf
);
365 gnome_canvas_buf_ensure_buf (buf
);
369 begin
= MAX(ruler
->bbox_ulx
,buf
->rect
.x0
);
371 if (ruler
->bbox_lrx
>= 0) {
372 end
= MIN(ruler
->bbox_lrx
,buf
->rect
.x1
);
381 PAINT_BOX (buf
, ruler
->fill_r
, ruler
->fill_g
, ruler
->fill_b
, 255, begin
, ruler
->bbox_uly
, end
, ruler
->bbox_lry
- 1);
382 PAINT_HORIZ (buf
, ruler
->tick_r
, ruler
->tick_g
, ruler
->tick_b
, begin
, end
, ruler
->bbox_lry
- 1);
386 gnome_canvas_ruler_draw (GnomeCanvasItem
*item
,
387 GdkDrawable
*drawable
,
389 int width
, int height
)
391 GnomeCanvasRuler
*ruler
;
393 ruler
= GNOME_CANVAS_RULER (item
);
395 if (parent_class
->draw
) {
396 (* parent_class
->draw
) (item
, drawable
, x
, y
, width
, height
);
399 fprintf (stderr
, "please don't use the CanvasRuler item in a non-aa Canvas\n");
404 gnome_canvas_ruler_bounds (GnomeCanvasItem
*item
, double *x1
, double *y1
, double *x2
, double *y2
)
406 GnomeCanvasRuler
*ruler
= GNOME_CANVAS_RULER (item
);
415 gnome_canvas_ruler_point (GnomeCanvasItem
*item
, double x
, double y
, int cx
, int cy
, GnomeCanvasItem
**actual_item
)
417 GnomeCanvasRuler
*ruler
;
418 double x1
, y1
, x2
, y2
;
421 ruler
= GNOME_CANVAS_RULER (item
);
425 /* Find the bounds for the rectangle plus its outline width */
427 gnome_canvas_ruler_bounds (item
, &x1
, &y1
, &x2
, &y2
);
429 /* Is point inside rectangle */
431 if ((x
>= x1
) && (y
>= y1
) && (x
<= x2
) && (y
<= y2
)) {
435 /* Point is outside rectangle */
451 return sqrt (dx
* dx
+ dy
* dy
);