Code

delivarotify, render with cairo
[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 <color.h>
30 #include "display/inkscape-cairo.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->item=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     ctrlline->item=NULL;
93     if (GTK_OBJECT_CLASS (parent_class)->destroy)
94         (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
95 }
97 static void
98 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
99 {
100     SPCtrlLine *cl = SP_CTRLLINE (item);
102     if (!buf->ct)
103         return;
105     guint32 rgba = cl->rgba;
106     cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
108     cairo_set_line_width(buf->ct, 1);
109     cairo_new_path(buf->ct);
111     NR::Point s = cl->s * cl->affine;
112     NR::Point e = cl->e * cl->affine;
114     cairo_move_to (buf->ct, s[NR::X] - buf->rect.x0, s[NR::Y] - buf->rect.y0);
115     cairo_line_to (buf->ct, e[NR::X] - buf->rect.x0, e[NR::Y] - buf->rect.y0);
117     cairo_stroke(buf->ct);
120 static void
121 sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
123     SPCtrlLine *cl = SP_CTRLLINE (item);
125     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
127     if (parent_class->update)
128         (* parent_class->update) (item, affine, flags);
130     sp_canvas_item_reset_bounds (item);
132     cl->affine = affine;
134     NR::Point s = cl->s * affine;
135     NR::Point e = cl->e * affine;
137     item->x1 = (int)(MIN(s[NR::X], e[NR::X]) - 1);
138     item->y1 = (int)(MIN(s[NR::Y], e[NR::Y]) - 1);
139     item->x2 = (int)(MAX(s[NR::X], e[NR::X]) + 1);
140     item->y2 = (int)(MAX(s[NR::Y], e[NR::Y]) + 1);
142     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
145 void
146 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
148     g_return_if_fail (cl != NULL);
149     g_return_if_fail (SP_IS_CTRLLINE (cl));
151     if (rgba != cl->rgba) {
152         SPCanvasItem *item;
153         cl->rgba = rgba;
154         item = SP_CANVAS_ITEM (cl);
155         sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
156     }
159 #define EPSILON 1e-6
160 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
162 void
163 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
165     g_return_if_fail (cl != NULL);
166     g_return_if_fail (SP_IS_CTRLLINE (cl));
168     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])) {
169         cl->s[NR::X] = x0;
170         cl->s[NR::Y] = y0;
171         cl->e[NR::X] = x1;
172         cl->e[NR::Y] = y1;
173         sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
174     }
177 void
178 sp_ctrlline_set_coords (SPCtrlLine *cl, const NR::Point start, const NR::Point end)
180     sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
183 /*
184   Local Variables:
185   mode:c++
186   c-file-style:"stroustrup"
187   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
188   indent-tabs-mode:nil
189   fill-column:99
190   End:
191 */
192 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :