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 "sp-canvas-util.h"
23 #include "sp-ctrlline.h"
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 #include <color.h>
29 #include "display/inkscape-cairo.h"
32 static void sp_ctrlline_class_init (SPCtrlLineClass *klass);
33 static void sp_ctrlline_init (SPCtrlLine *ctrlline);
34 static void sp_ctrlline_destroy (GtkObject *object);
36 static void sp_ctrlline_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
37 static void sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf);
39 static SPCanvasItemClass *parent_class;
41 GType
42 sp_ctrlline_get_type (void)
43 {
44 static GType type = 0;
45 if (!type) {
46 GTypeInfo info = {
47 sizeof(SPCtrlLineClass),
48 NULL, NULL,
49 (GClassInitFunc) sp_ctrlline_class_init,
50 NULL, NULL,
51 sizeof(SPCtrlLine),
52 0,
53 (GInstanceInitFunc) sp_ctrlline_init,
54 NULL
55 };
56 type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPCtrlLine", &info, (GTypeFlags)0);
57 }
58 return type;
59 }
61 static void
62 sp_ctrlline_class_init (SPCtrlLineClass *klass)
63 {
64 GtkObjectClass *object_class = (GtkObjectClass *) klass;
65 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
67 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
69 object_class->destroy = sp_ctrlline_destroy;
71 item_class->update = sp_ctrlline_update;
72 item_class->render = sp_ctrlline_render;
73 }
75 static void
76 sp_ctrlline_init (SPCtrlLine *ctrlline)
77 {
78 ctrlline->rgba = 0x0000ff7f;
79 ctrlline->s[Geom::X] = ctrlline->s[Geom::Y] = ctrlline->e[Geom::X] = ctrlline->e[Geom::Y] = 0.0;
80 ctrlline->item=NULL;
81 }
83 static void
84 sp_ctrlline_destroy (GtkObject *object)
85 {
86 g_return_if_fail (object != NULL);
87 g_return_if_fail (SP_IS_CTRLLINE (object));
89 SPCtrlLine *ctrlline = SP_CTRLLINE (object);
91 ctrlline->item=NULL;
93 if (GTK_OBJECT_CLASS (parent_class)->destroy)
94 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
95 }
97 static void
98 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
99 {
100 SPCtrlLine *cl = SP_CTRLLINE (item);
102 if (!buf->ct)
103 return;
105 if (cl->s == cl->e)
106 return;
108 sp_canvas_prepare_buffer (buf);
110 guint32 rgba = cl->rgba;
111 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));
113 cairo_set_line_width(buf->ct, 1);
114 cairo_new_path(buf->ct);
116 Geom::Point s = cl->s * cl->affine;
117 Geom::Point e = cl->e * cl->affine;
119 cairo_move_to (buf->ct, s[Geom::X] - buf->rect.x0, s[Geom::Y] - buf->rect.y0);
120 cairo_line_to (buf->ct, e[Geom::X] - buf->rect.x0, e[Geom::Y] - buf->rect.y0);
122 cairo_stroke(buf->ct);
123 }
125 static void
126 sp_ctrlline_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
127 {
128 SPCtrlLine *cl = SP_CTRLLINE (item);
130 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
132 if (parent_class->update)
133 (* parent_class->update) (item, affine, flags);
135 sp_canvas_item_reset_bounds (item);
137 cl->affine = affine;
139 if (cl->s == cl->e) {
140 item->x1 = item->x2 = item->y1 = item->y2 = 0;
141 } else {
143 Geom::Point s = cl->s * affine;
144 Geom::Point e = cl->e * affine;
146 item->x1 = round(MIN(s[Geom::X], e[Geom::X]) - 1);
147 item->y1 = round(MIN(s[Geom::Y], e[Geom::Y]) - 1);
148 item->x2 = round(MAX(s[Geom::X], e[Geom::X]) + 1);
149 item->y2 = round(MAX(s[Geom::Y], e[Geom::Y]) + 1);
151 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
153 }
154 }
156 void
157 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
158 {
159 g_return_if_fail (cl != NULL);
160 g_return_if_fail (SP_IS_CTRLLINE (cl));
162 if (rgba != cl->rgba) {
163 SPCanvasItem *item;
164 cl->rgba = rgba;
165 item = SP_CANVAS_ITEM (cl);
166 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
167 }
168 }
170 #define EPSILON 1e-6
171 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
173 void
174 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
175 {
176 g_return_if_fail (cl != NULL);
177 g_return_if_fail (SP_IS_CTRLLINE (cl));
179 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])) {
180 cl->s[Geom::X] = x0;
181 cl->s[Geom::Y] = y0;
182 cl->e[Geom::X] = x1;
183 cl->e[Geom::Y] = y1;
184 sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
185 }
186 }
188 void
189 sp_ctrlline_set_coords (SPCtrlLine *cl, const Geom::Point start, const Geom::Point end)
190 {
191 sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
192 }
194 /*
195 Local Variables:
196 mode:c++
197 c-file-style:"stroustrup"
198 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
199 indent-tabs-mode:nil
200 fill-column:99
201 End:
202 */
203 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :