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 <livarot/Path.h>
30 #include <color.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, NR::Matrix const &affine, unsigned int flags);
38 static void sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf);
40 static SPCanvasItemClass *parent_class;
42 GtkType
43 sp_ctrlline_get_type (void)
44 {
45 static GtkType type = 0;
47 if (!type) {
48 GtkTypeInfo info = {
49 "SPCtrlLine",
50 sizeof (SPCtrlLine),
51 sizeof (SPCtrlLineClass),
52 (GtkClassInitFunc) sp_ctrlline_class_init,
53 (GtkObjectInitFunc) sp_ctrlline_init,
54 NULL, NULL, NULL
55 };
56 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
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.x = ctrlline->s.y = ctrlline->e.x = ctrlline->e.y = 0.0;
80 ctrlline->shp=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 if (ctrlline->shp) {
92 delete ctrlline->shp;
93 ctrlline->shp = NULL;
94 }
96 if (GTK_OBJECT_CLASS (parent_class)->destroy)
97 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
98 }
100 static void
101 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
102 {
103 SPCtrlLine *ctrlline = SP_CTRLLINE (item);
105 NRRectL area;
106 area.x0=buf->rect.x0;
107 area.x1=buf->rect.x1;
108 area.y0=buf->rect.y0;
109 area.y1=buf->rect.y1;
111 /*
112 // CAIRO FIXME: after SPCanvasBuf is switched to unpacked 32bit rgb, rendering can be done via cairo:
113 cairo_surface_t* cst = cairo_image_surface_create_for_data (
114 buf->buf,
115 CAIRO_FORMAT_RGB24,
116 buf->rect.x1 - buf->rect.x0,
117 buf->rect.y1 - buf->rect.y0,
118 buf->buf_rowstride
119 );
120 cairo_t *ct = cairo_create (cst);
122 guint32 rgba = ctrlline->rgba;
123 cairo_set_source_rgba(ct, SP_RGBA32_R_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_B_F(rgba), SP_RGBA32_A_F(rgba));
125 cairo_set_line_width(ct, 0.5);
126 cairo_new_path(ct);
128 cairo_move_to (ct, ctrlline->s.x - buf->rect.x0, ctrlline->s.y - buf->rect.y0);
129 cairo_line_to (ct, ctrlline->e.x - buf->rect.x0, ctrlline->e.y - buf->rect.y0);
131 cairo_stroke(ct);
132 cairo_destroy (ct);
133 cairo_surface_finish (cst);
134 cairo_surface_destroy (cst);
135 */
137 // CAIRO FIXME: instead of this:
138 if (ctrlline->shp) {
139 sp_canvas_prepare_buffer (buf);
140 nr_pixblock_render_ctrl_rgba (ctrlline->shp,ctrlline->rgba,area,(char*)buf->buf, buf->buf_rowstride);
141 }
142 }
144 static void
145 sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
146 {
147 SPCtrlLine *cl = SP_CTRLLINE (item);
149 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
151 if (parent_class->update)
152 (* parent_class->update) (item, affine, flags);
154 sp_canvas_item_reset_bounds (item);
156 /*
157 // CAIRO FIXME: all that is needed for update with cairo:
158 NR::Point s = NR::Point(cl->s.x, cl->s.y) * affine;
159 NR::Point e = NR::Point(cl->e.x, cl->e.y) * affine;
161 cl->s.x = s[NR::X];
162 cl->s.y = s[NR::Y];
163 cl->e.x = e[NR::X];
164 cl->e.y = e[NR::Y];
166 item->x1 = (int)(MIN(s[NR::X], e[NR::X]) - 1);
167 item->y1 = (int)(MIN(s[NR::Y], e[NR::Y]) - 1);
168 item->x2 = (int)(MAX(s[NR::X], e[NR::X]) + 1);
169 item->y2 = (int)(MAX(s[NR::Y], e[NR::Y]) + 1);
170 */
172 // CAIRO FIXME: instead of:
173 NRRect dbox;
174 dbox.x0=dbox.x1=dbox.y0=dbox.y1=0;
175 if (cl->shp) {
176 delete cl->shp;
177 cl->shp = NULL;
178 }
179 Path* thePath = new Path;
180 thePath->MoveTo(NR::Point(cl->s.x, cl->s.y) * affine);
181 thePath->LineTo(NR::Point(cl->e.x, cl->e.y) * affine);
183 NRRectL area;
184 area.x0=(NR::ICoord)(double)item->x1;
185 area.x1=(NR::ICoord)(double)item->x2;
186 area.y0=(NR::ICoord)(double)item->y1;
187 area.y1=(NR::ICoord)(double)item->y2;
188 thePath->Convert(&area, 1.0);
189 if ( cl->shp == NULL ) cl->shp=new Shape;
190 thePath->Stroke(cl->shp,false,0.5,join_straight,butt_straight,20.0,false);
191 cl->shp->CalcBBox();
192 if ( cl->shp->leftX < cl->shp->rightX ) {
193 if ( dbox.x0 >= dbox.x1 ) {
194 dbox.x0=cl->shp->leftX;dbox.x1=cl->shp->rightX;
195 dbox.y0=cl->shp->topY;dbox.y1=cl->shp->bottomY;
196 } else {
197 if ( cl->shp->leftX < dbox.x0 ) dbox.x0=cl->shp->leftX;
198 if ( cl->shp->rightX > dbox.x1 ) dbox.x1=cl->shp->rightX;
199 if ( cl->shp->topY < dbox.y0 ) dbox.y0=cl->shp->topY;
200 if ( cl->shp->bottomY > dbox.y1 ) dbox.y1=cl->shp->bottomY;
201 }
202 }
203 delete thePath;
205 item->x1 = (int)dbox.x0;
206 item->y1 = (int)dbox.y0;
207 item->x2 = (int)dbox.x1;
208 item->y2 = (int)dbox.y1;
210 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
211 }
213 void
214 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
215 {
216 g_return_if_fail (cl != NULL);
217 g_return_if_fail (SP_IS_CTRLLINE (cl));
219 if (rgba != cl->rgba) {
220 SPCanvasItem *item;
221 cl->rgba = rgba;
222 item = SP_CANVAS_ITEM (cl);
223 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
224 }
225 }
227 #define EPSILON 1e-6
228 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
230 void
231 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
232 {
233 g_return_if_fail (cl != NULL);
234 g_return_if_fail (SP_IS_CTRLLINE (cl));
236 if (DIFFER (x0, cl->s.x) || DIFFER (y0, cl->s.y) || DIFFER (x1, cl->e.x) || DIFFER (y1, cl->e.y)) {
237 cl->s.x = x0;
238 cl->s.y = y0;
239 cl->e.x = x1;
240 cl->e.y = y1;
241 sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
242 }
243 }
245 void
246 sp_ctrlline_set_coords (SPCtrlLine *cl, const NR::Point start, const NR::Point end)
247 {
248 sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
249 }
251 /*
252 Local Variables:
253 mode:c++
254 c-file-style:"stroustrup"
255 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
256 indent-tabs-mode:nil
257 fill-column:99
258 End:
259 */
260 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :