Code

Sorry, forgot the copyright text.
[inkscape.git] / src / display / sp-ctrlline.cpp
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>
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)
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     if (ctrlline->shp) {
112         sp_canvas_prepare_buffer (buf);
113         nr_pixblock_render_ctrl_rgba (ctrlline->shp,ctrlline->rgba,area,(char*)buf->buf, buf->buf_rowstride);
114     }
117 static void
118 sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
120     NRRect dbox;
122     SPCtrlLine *cl = SP_CTRLLINE (item);
124     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
126     if (parent_class->update)
127         (* parent_class->update) (item, affine, flags);
129     sp_canvas_item_reset_bounds (item);
131     dbox.x0=dbox.x1=dbox.y0=dbox.y1=0;
132     if (cl->shp) {
133         delete cl->shp;
134         cl->shp = NULL;
135     }
136     Path* thePath = new Path;
137     thePath->MoveTo(NR::Point(cl->s.x, cl->s.y) * affine);
138     thePath->LineTo(NR::Point(cl->e.x, cl->e.y) * affine);
140     NRRectL  area;
141     area.x0=(NR::ICoord)(double)item->x1;
142     area.x1=(NR::ICoord)(double)item->x2;
143     area.y0=(NR::ICoord)(double)item->y1;
144     area.y1=(NR::ICoord)(double)item->y2;
145     thePath->Convert(&area, 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);
170 void
171 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
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     }
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)
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     }
202 void
203 sp_ctrlline_set_coords (SPCtrlLine *cl, const NR::Point start, const NR::Point end)
205     sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
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 :