1 #define __INKSCAPE_CTRLLINE_C__
3 /*
4 * Simple straight line
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2002 Lauris Kaplinski
10 *
11 * Released under GNU GPL
12 */
14 /*
15 * TODO:
16 * Draw it by hand - we really do not need aa stuff for it
17 *
18 */
20 #include "display-forward.h"
21 #include "sp-canvas-util.h"
22 #include "sp-ctrlline.h"
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <livarot/Shape.h>
28 #include <livarot/Path.h>
30 struct SPCtrlLine : public SPCanvasItem{
31 guint32 rgba;
32 NRPoint s, e;
33 Shape* shp;
34 };
36 struct SPCtrlLineClass : public SPCanvasItemClass{};
38 static void sp_ctrlline_class_init (SPCtrlLineClass *klass);
39 static void sp_ctrlline_init (SPCtrlLine *ctrlline);
40 static void sp_ctrlline_destroy (GtkObject *object);
42 static void sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
43 static void sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf);
45 static SPCanvasItemClass *parent_class;
47 GtkType
48 sp_ctrlline_get_type (void)
49 {
50 static GtkType type = 0;
52 if (!type) {
53 GtkTypeInfo info = {
54 "SPCtrlLine",
55 sizeof (SPCtrlLine),
56 sizeof (SPCtrlLineClass),
57 (GtkClassInitFunc) sp_ctrlline_class_init,
58 (GtkObjectInitFunc) sp_ctrlline_init,
59 NULL, NULL, NULL
60 };
61 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
62 }
63 return type;
64 }
66 static void
67 sp_ctrlline_class_init (SPCtrlLineClass *klass)
68 {
69 GtkObjectClass *object_class = (GtkObjectClass *) klass;
70 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
72 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
74 object_class->destroy = sp_ctrlline_destroy;
76 item_class->update = sp_ctrlline_update;
77 item_class->render = sp_ctrlline_render;
78 }
80 static void
81 sp_ctrlline_init (SPCtrlLine *ctrlline)
82 {
83 ctrlline->rgba = 0x0000ff7f;
84 ctrlline->s.x = ctrlline->s.y = ctrlline->e.x = ctrlline->e.y = 0.0;
85 ctrlline->shp=NULL;
86 }
88 static void
89 sp_ctrlline_destroy (GtkObject *object)
90 {
91 g_return_if_fail (object != NULL);
92 g_return_if_fail (SP_IS_CTRLLINE (object));
94 SPCtrlLine *ctrlline = SP_CTRLLINE (object);
96 if (ctrlline->shp) {
97 delete ctrlline->shp;
98 ctrlline->shp = NULL;
99 }
101 if (GTK_OBJECT_CLASS (parent_class)->destroy)
102 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
103 }
105 static void
106 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
107 {
108 SPCtrlLine *ctrlline = SP_CTRLLINE (item);
110 NRRectL area;
111 area.x0=buf->rect.x0;
112 area.x1=buf->rect.x1;
113 area.y0=buf->rect.y0;
114 area.y1=buf->rect.y1;
116 if (ctrlline->shp) {
117 sp_canvas_prepare_buffer (buf);
118 nr_pixblock_render_ctrl_rgba (ctrlline->shp,ctrlline->rgba,area,(char*)buf->buf, buf->buf_rowstride);
119 }
120 }
122 static void
123 sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
124 {
125 NRRect dbox;
127 SPCtrlLine *cl = SP_CTRLLINE (item);
129 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
131 if (parent_class->update)
132 (* parent_class->update) (item, affine, flags);
134 sp_canvas_item_reset_bounds (item);
136 dbox.x0=dbox.x1=dbox.y0=dbox.y1=0;
137 if (cl->shp) {
138 delete cl->shp;
139 cl->shp = NULL;
140 }
141 Path* thePath = new Path;
142 thePath->MoveTo(NR::Point(cl->s.x, cl->s.y) * affine);
143 thePath->LineTo(NR::Point(cl->e.x, cl->e.y) * affine);
145 thePath->Convert(1.0);
146 if ( cl->shp == NULL ) cl->shp=new Shape;
147 thePath->Stroke(cl->shp,false,0.5,join_straight,butt_straight,20.0,false);
148 cl->shp->CalcBBox();
149 if ( cl->shp->leftX < cl->shp->rightX ) {
150 if ( dbox.x0 >= dbox.x1 ) {
151 dbox.x0=cl->shp->leftX;dbox.x1=cl->shp->rightX;
152 dbox.y0=cl->shp->topY;dbox.y1=cl->shp->bottomY;
153 } else {
154 if ( cl->shp->leftX < dbox.x0 ) dbox.x0=cl->shp->leftX;
155 if ( cl->shp->rightX > dbox.x1 ) dbox.x1=cl->shp->rightX;
156 if ( cl->shp->topY < dbox.y0 ) dbox.y0=cl->shp->topY;
157 if ( cl->shp->bottomY > dbox.y1 ) dbox.y1=cl->shp->bottomY;
158 }
159 }
160 delete thePath;
162 item->x1 = (int)dbox.x0;
163 item->y1 = (int)dbox.y0;
164 item->x2 = (int)dbox.x1;
165 item->y2 = (int)dbox.y1;
167 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
168 }
170 void
171 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
172 {
173 g_return_if_fail (cl != NULL);
174 g_return_if_fail (SP_IS_CTRLLINE (cl));
176 if (rgba != cl->rgba) {
177 SPCanvasItem *item;
178 cl->rgba = rgba;
179 item = SP_CANVAS_ITEM (cl);
180 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
181 }
182 }
184 #define EPSILON 1e-6
185 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
187 void
188 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
189 {
190 g_return_if_fail (cl != NULL);
191 g_return_if_fail (SP_IS_CTRLLINE (cl));
193 if (DIFFER (x0, cl->s.x) || DIFFER (y0, cl->s.y) || DIFFER (x1, cl->e.x) || DIFFER (y1, cl->e.y)) {
194 cl->s.x = x0;
195 cl->s.y = y0;
196 cl->e.x = x1;
197 cl->e.y = y1;
198 sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
199 }
200 }
202 void
203 sp_ctrlline_set_coords (SPCtrlLine *cl, const NR::Point start, const NR::Point end)
204 {
205 sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
206 }
208 /*
209 Local Variables:
210 mode:c++
211 c-file-style:"stroustrup"
212 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
213 indent-tabs-mode:nil
214 fill-column:99
215 End:
216 */
217 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :