1 #define __INKSCAPE_CTRLLINE_C__
3 /*
4 * Simple straight line
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 *
10 * Copyright (C) 2007 Johan Engelen
11 * Copyright (C) 1999-2002 Lauris Kaplinski
12 *
13 * Released under GNU GPL
14 */
16 /*
17 * TODO:
18 * Draw it by hand - we really do not need aa stuff for it
19 *
20 */
22 #include "display-forward.h"
23 #include "sp-canvas-util.h"
24 #include "sp-ctrlline.h"
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29 #include <color.h>
30 #include "display/inkscape-cairo.h"
33 static void sp_ctrlline_class_init (SPCtrlLineClass *klass);
34 static void sp_ctrlline_init (SPCtrlLine *ctrlline);
35 static void sp_ctrlline_destroy (GtkObject *object);
37 static void sp_ctrlline_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
38 static void sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf);
40 static SPCanvasItemClass *parent_class;
42 GType
43 sp_ctrlline_get_type (void)
44 {
45 static GType type = 0;
46 if (!type) {
47 GTypeInfo info = {
48 sizeof(SPCtrlLineClass),
49 NULL, NULL,
50 (GClassInitFunc) sp_ctrlline_class_init,
51 NULL, NULL,
52 sizeof(SPCtrlLine),
53 0,
54 (GInstanceInitFunc) sp_ctrlline_init,
55 NULL
56 };
57 type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPCtrlLine", &info, (GTypeFlags)0);
58 }
59 return type;
60 }
62 static void
63 sp_ctrlline_class_init (SPCtrlLineClass *klass)
64 {
65 GtkObjectClass *object_class = (GtkObjectClass *) klass;
66 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
68 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
70 object_class->destroy = sp_ctrlline_destroy;
72 item_class->update = sp_ctrlline_update;
73 item_class->render = sp_ctrlline_render;
74 }
76 static void
77 sp_ctrlline_init (SPCtrlLine *ctrlline)
78 {
79 ctrlline->rgba = 0x0000ff7f;
80 ctrlline->s[Geom::X] = ctrlline->s[Geom::Y] = ctrlline->e[Geom::X] = ctrlline->e[Geom::Y] = 0.0;
81 ctrlline->item=NULL;
82 }
84 static void
85 sp_ctrlline_destroy (GtkObject *object)
86 {
87 g_return_if_fail (object != NULL);
88 g_return_if_fail (SP_IS_CTRLLINE (object));
90 SPCtrlLine *ctrlline = SP_CTRLLINE (object);
92 ctrlline->item=NULL;
94 if (GTK_OBJECT_CLASS (parent_class)->destroy)
95 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
96 }
98 static void
99 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
100 {
101 SPCtrlLine *cl = SP_CTRLLINE (item);
103 if (!buf->ct)
104 return;
106 sp_canvas_prepare_buffer (buf);
108 guint32 rgba = cl->rgba;
109 cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
111 cairo_set_line_width(buf->ct, 1);
112 cairo_new_path(buf->ct);
114 Geom::Point s = cl->s * cl->affine;
115 Geom::Point e = cl->e * cl->affine;
117 cairo_move_to (buf->ct, s[Geom::X] - buf->rect.x0, s[Geom::Y] - buf->rect.y0);
118 cairo_line_to (buf->ct, e[Geom::X] - buf->rect.x0, e[Geom::Y] - buf->rect.y0);
120 cairo_stroke(buf->ct);
121 }
123 static void
124 sp_ctrlline_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
125 {
126 SPCtrlLine *cl = SP_CTRLLINE (item);
128 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
130 if (parent_class->update)
131 (* parent_class->update) (item, affine, flags);
133 sp_canvas_item_reset_bounds (item);
135 cl->affine = affine;
137 Geom::Point s = cl->s * affine;
138 Geom::Point e = cl->e * affine;
140 item->x1 = round(MIN(s[Geom::X], e[Geom::X]) - 1);
141 item->y1 = round(MIN(s[Geom::Y], e[Geom::Y]) - 1);
142 item->x2 = round(MAX(s[Geom::X], e[Geom::X]) + 1);
143 item->y2 = round(MAX(s[Geom::Y], e[Geom::Y]) + 1);
145 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
146 }
148 void
149 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
150 {
151 g_return_if_fail (cl != NULL);
152 g_return_if_fail (SP_IS_CTRLLINE (cl));
154 if (rgba != cl->rgba) {
155 SPCanvasItem *item;
156 cl->rgba = rgba;
157 item = SP_CANVAS_ITEM (cl);
158 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
159 }
160 }
162 #define EPSILON 1e-6
163 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
165 void
166 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
167 {
168 g_return_if_fail (cl != NULL);
169 g_return_if_fail (SP_IS_CTRLLINE (cl));
171 if (DIFFER (x0, cl->s[Geom::X]) || DIFFER (y0, cl->s[Geom::Y]) || DIFFER (x1, cl->e[Geom::X]) || DIFFER (y1, cl->e[Geom::Y])) {
172 cl->s[Geom::X] = x0;
173 cl->s[Geom::Y] = y0;
174 cl->e[Geom::X] = x1;
175 cl->e[Geom::Y] = y1;
176 sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
177 }
178 }
180 void
181 sp_ctrlline_set_coords (SPCtrlLine *cl, const Geom::Point start, const Geom::Point end)
182 {
183 sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
184 }
186 /*
187 Local Variables:
188 mode:c++
189 c-file-style:"stroustrup"
190 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
191 indent-tabs-mode:nil
192 fill-column:99
193 End:
194 */
195 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :