Code

resolving compiler warnings
[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  *
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);
105 static void
106 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
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     }
122 static void
123 sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
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     NRRectL  area;
146     area.x0=(double)item->x1;
147     area.x1=(double)item->x2;
148     area.y0=(double)item->y1;
149     area.y1=(double)item->y2;
150     thePath->Convert(&area, 1.0);
151     if ( cl->shp == NULL ) cl->shp=new Shape;
152     thePath->Stroke(cl->shp,false,0.5,join_straight,butt_straight,20.0,false);
153     cl->shp->CalcBBox();
154     if ( cl->shp->leftX < cl->shp->rightX ) {
155         if ( dbox.x0 >= dbox.x1 ) {
156             dbox.x0=cl->shp->leftX;dbox.x1=cl->shp->rightX;
157             dbox.y0=cl->shp->topY;dbox.y1=cl->shp->bottomY;
158         } else {
159             if ( cl->shp->leftX < dbox.x0 ) dbox.x0=cl->shp->leftX;
160             if ( cl->shp->rightX > dbox.x1 ) dbox.x1=cl->shp->rightX;
161             if ( cl->shp->topY < dbox.y0 ) dbox.y0=cl->shp->topY;
162             if ( cl->shp->bottomY > dbox.y1 ) dbox.y1=cl->shp->bottomY;
163         }
164     }
165     delete thePath;
167     item->x1 = (int)dbox.x0;
168     item->y1 = (int)dbox.y0;
169     item->x2 = (int)dbox.x1;
170     item->y2 = (int)dbox.y1;
172     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
175 void
176 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
178     g_return_if_fail (cl != NULL);
179     g_return_if_fail (SP_IS_CTRLLINE (cl));
181     if (rgba != cl->rgba) {
182         SPCanvasItem *item;
183         cl->rgba = rgba;
184         item = SP_CANVAS_ITEM (cl);
185         sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
186     }
189 #define EPSILON 1e-6
190 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
192 void
193 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
195     g_return_if_fail (cl != NULL);
196     g_return_if_fail (SP_IS_CTRLLINE (cl));
198     if (DIFFER (x0, cl->s.x) || DIFFER (y0, cl->s.y) || DIFFER (x1, cl->e.x) || DIFFER (y1, cl->e.y)) {
199         cl->s.x = x0;
200         cl->s.y = y0;
201         cl->e.x = x1;
202         cl->e.y = y1;
203         sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
204     }
207 void
208 sp_ctrlline_set_coords (SPCtrlLine *cl, const NR::Point start, const NR::Point end)
210     sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
213 /*
214   Local Variables:
215   mode:c++
216   c-file-style:"stroustrup"
217   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
218   indent-tabs-mode:nil
219   fill-column:99
220   End:
221 */
222 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :