Code

No more NRMatrix or NRPoint.
[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>
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[NR::X] = ctrlline->s[NR::Y] = ctrlline->e[NR::X] = ctrlline->e[NR::Y] = 0.0;
80     ctrlline->shp=NULL;
81     ctrlline->item=NULL;
82 }
84 static void
85 sp_ctrlline_destroy (GtkObject *object)
86 {
87     g_return_if_fail (object != NULL);
88     g_return_if_fail (SP_IS_CTRLLINE (object));
90     SPCtrlLine *ctrlline = SP_CTRLLINE (object);
92     if (ctrlline->shp) {
93         delete ctrlline->shp;
94         ctrlline->shp = NULL;
95     }
97     ctrlline->item=NULL;
99     if (GTK_OBJECT_CLASS (parent_class)->destroy)
100         (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
103 static void
104 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
106     SPCtrlLine *ctrlline = SP_CTRLLINE (item);
108     NRRectL  area;
109     area.x0=buf->rect.x0;
110     area.x1=buf->rect.x1;
111     area.y0=buf->rect.y0;
112     area.y1=buf->rect.y1;
114 /*
115 // CAIRO FIXME: after SPCanvasBuf is switched to unpacked 32bit rgb, rendering can be done via cairo:
116     cairo_surface_t* cst = cairo_image_surface_create_for_data (
117         buf->buf,
118         CAIRO_FORMAT_RGB24,
119         buf->rect.x1 - buf->rect.x0,
120         buf->rect.y1 - buf->rect.y0,
121         buf->buf_rowstride
122         );
123     cairo_t *ct = cairo_create (cst);
125     guint32 rgba = ctrlline->rgba;
126     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));
128     cairo_set_line_width(ct, 0.5);
129     cairo_new_path(ct);
131     cairo_move_to (ct, ctrlline->s.x - buf->rect.x0, ctrlline->s.y - buf->rect.y0);
132     cairo_line_to (ct, ctrlline->e.x - buf->rect.x0, ctrlline->e.y - buf->rect.y0);
134     cairo_stroke(ct);
135     cairo_destroy (ct);
136     cairo_surface_finish (cst);
137     cairo_surface_destroy (cst);
138 */
140 // CAIRO FIXME: instead of this:
141     if (ctrlline->shp) {
142         sp_canvas_prepare_buffer (buf);
143         nr_pixblock_render_ctrl_rgba (ctrlline->shp,ctrlline->rgba,area,(char*)buf->buf, buf->buf_rowstride);
144     }
147 static void
148 sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
150     SPCtrlLine *cl = SP_CTRLLINE (item);
152     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
154     if (parent_class->update)
155         (* parent_class->update) (item, affine, flags);
157     sp_canvas_item_reset_bounds (item);
159 /*
160 // CAIRO FIXME: all that is needed for update with cairo:
161     NR::Point s = NR::Point(cl->s.x, cl->s.y) * affine;
162     NR::Point e = NR::Point(cl->e.x, cl->e.y) * affine;
164     cl->s.x = s[NR::X];
165     cl->s.y = s[NR::Y];
166     cl->e.x = e[NR::X];
167     cl->e.y = e[NR::Y];
169     item->x1 = (int)(MIN(s[NR::X], e[NR::X]) - 1);
170     item->y1 = (int)(MIN(s[NR::Y], e[NR::Y]) - 1);
171     item->x2 = (int)(MAX(s[NR::X], e[NR::X]) + 1);
172     item->y2 = (int)(MAX(s[NR::Y], e[NR::Y]) + 1);
173 */
175 // CAIRO FIXME: instead of:
176     NRRect dbox;
177     dbox.x0=dbox.x1=dbox.y0=dbox.y1=0;
178     if (cl->shp) {
179         delete cl->shp;
180         cl->shp = NULL;
181     }
182     Path* thePath = new Path;
183     thePath->MoveTo(NR::Point(cl->s[NR::X], cl->s[NR::Y]) * affine);
184     thePath->LineTo(NR::Point(cl->e[NR::X], cl->e[NR::Y]) * affine);
186     NRRectL  area;
187     area.x0=(NR::ICoord)(double)item->x1;
188     area.x1=(NR::ICoord)(double)item->x2;
189     area.y0=(NR::ICoord)(double)item->y1;
190     area.y1=(NR::ICoord)(double)item->y2;
191     thePath->Convert(&area, 1.0);
192     if ( cl->shp == NULL ) cl->shp=new Shape;
193     thePath->Stroke(cl->shp,false,0.5,join_straight,butt_straight,20.0,false);
194     cl->shp->CalcBBox();
195     if ( cl->shp->leftX < cl->shp->rightX ) {
196         if ( dbox.x0 >= dbox.x1 ) {
197             dbox.x0=cl->shp->leftX;dbox.x1=cl->shp->rightX;
198             dbox.y0=cl->shp->topY;dbox.y1=cl->shp->bottomY;
199         } else {
200             if ( cl->shp->leftX < dbox.x0 ) dbox.x0=cl->shp->leftX;
201             if ( cl->shp->rightX > dbox.x1 ) dbox.x1=cl->shp->rightX;
202             if ( cl->shp->topY < dbox.y0 ) dbox.y0=cl->shp->topY;
203             if ( cl->shp->bottomY > dbox.y1 ) dbox.y1=cl->shp->bottomY;
204         }
205     }
206     delete thePath;
208     item->x1 = (int)dbox.x0;
209     item->y1 = (int)dbox.y0;
210     item->x2 = (int)dbox.x1;
211     item->y2 = (int)dbox.y1;
213     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
216 void
217 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
219     g_return_if_fail (cl != NULL);
220     g_return_if_fail (SP_IS_CTRLLINE (cl));
222     if (rgba != cl->rgba) {
223         SPCanvasItem *item;
224         cl->rgba = rgba;
225         item = SP_CANVAS_ITEM (cl);
226         sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
227     }
230 #define EPSILON 1e-6
231 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
233 void
234 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
236     g_return_if_fail (cl != NULL);
237     g_return_if_fail (SP_IS_CTRLLINE (cl));
239     if (DIFFER (x0, cl->s[NR::X]) || DIFFER (y0, cl->s[NR::Y]) || DIFFER (x1, cl->e[NR::X]) || DIFFER (y1, cl->e[NR::Y])) {
240         cl->s[NR::X] = x0;
241         cl->s[NR::Y] = y0;
242         cl->e[NR::X] = x1;
243         cl->e[NR::Y] = y1;
244         sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
245     }
248 void
249 sp_ctrlline_set_coords (SPCtrlLine *cl, const NR::Point start, const NR::Point end)
251     sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
254 /*
255   Local Variables:
256   mode:c++
257   c-file-style:"stroustrup"
258   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
259   indent-tabs-mode:nil
260   fill-column:99
261   End:
262 */
263 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :