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 if (cl->s == cl->e)
107 return;
109 sp_canvas_prepare_buffer (buf);
111 guint32 rgba = cl->rgba;
112 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));
114 cairo_set_line_width(buf->ct, 1);
115 cairo_new_path(buf->ct);
117 Geom::Point s = cl->s * cl->affine;
118 Geom::Point e = cl->e * cl->affine;
120 cairo_move_to (buf->ct, s[Geom::X] - buf->rect.x0, s[Geom::Y] - buf->rect.y0);
121 cairo_line_to (buf->ct, e[Geom::X] - buf->rect.x0, e[Geom::Y] - buf->rect.y0);
123 cairo_stroke(buf->ct);
124 }
126 static void
127 sp_ctrlline_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
128 {
129 SPCtrlLine *cl = SP_CTRLLINE (item);
131 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
133 if (parent_class->update)
134 (* parent_class->update) (item, affine, flags);
136 sp_canvas_item_reset_bounds (item);
138 cl->affine = affine;
140 if (cl->s == cl->e) {
141 item->x1 = item->x2 = item->y1 = item->y2 = 0;
142 } else {
144 Geom::Point s = cl->s * affine;
145 Geom::Point e = cl->e * affine;
147 item->x1 = round(MIN(s[Geom::X], e[Geom::X]) - 1);
148 item->y1 = round(MIN(s[Geom::Y], e[Geom::Y]) - 1);
149 item->x2 = round(MAX(s[Geom::X], e[Geom::X]) + 1);
150 item->y2 = round(MAX(s[Geom::Y], e[Geom::Y]) + 1);
152 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
154 }
155 }
157 void
158 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
159 {
160 g_return_if_fail (cl != NULL);
161 g_return_if_fail (SP_IS_CTRLLINE (cl));
163 if (rgba != cl->rgba) {
164 SPCanvasItem *item;
165 cl->rgba = rgba;
166 item = SP_CANVAS_ITEM (cl);
167 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
168 }
169 }
171 #define EPSILON 1e-6
172 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
174 void
175 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
176 {
177 g_return_if_fail (cl != NULL);
178 g_return_if_fail (SP_IS_CTRLLINE (cl));
180 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])) {
181 cl->s[Geom::X] = x0;
182 cl->s[Geom::Y] = y0;
183 cl->e[Geom::X] = x1;
184 cl->e[Geom::Y] = y1;
185 sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
186 }
187 }
189 void
190 sp_ctrlline_set_coords (SPCtrlLine *cl, const Geom::Point start, const Geom::Point end)
191 {
192 sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
193 }
195 /*
196 Local Variables:
197 mode:c++
198 c-file-style:"stroustrup"
199 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
200 indent-tabs-mode:nil
201 fill-column:99
202 End:
203 */
204 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :