Code

prepare for adding angled guideline rendering
[inkscape.git] / src / display / guideline.cpp
1 #define __SP_GUIDELINE_C__
3 /*
4  * Horizontal/vertical but can also be angled line
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Johan Engelen
9  *
10  * Copyright (C) 2000-2002 Lauris Kaplinski
11  * Copyright (C) 2007 Johan Engelen
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
17 #include <libnr/nr-pixops.h>
18 #include "display-forward.h"
19 #include "sp-canvas-util.h"
20 #include "guideline.h"
22 static void sp_guideline_class_init(SPGuideLineClass *c);
23 static void sp_guideline_init(SPGuideLine *guideline);
24 static void sp_guideline_destroy(GtkObject *object);
26 static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
27 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf);
29 static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
31 static SPCanvasItemClass *parent_class;
33 GType sp_guideline_get_type()
34 {
35     static GType guideline_type = 0;
37     if (!guideline_type) {
38         static GTypeInfo const guideline_info = {
39             sizeof (SPGuideLineClass),
40             NULL, NULL,
41             (GClassInitFunc) sp_guideline_class_init,
42             NULL, NULL,
43             sizeof (SPGuideLine),
44             16,
45             (GInstanceInitFunc) sp_guideline_init,
46             NULL,
47         };
49         guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
50     }
52     return guideline_type;
53 }
55 static void sp_guideline_class_init(SPGuideLineClass *c)
56 {
57     parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
59     GtkObjectClass *object_class = (GtkObjectClass *) c;
60     object_class->destroy = sp_guideline_destroy;
62     SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
63     item_class->update = sp_guideline_update;
64     item_class->render = sp_guideline_render;
65     item_class->point = sp_guideline_point;
66 }
68 static void sp_guideline_init(SPGuideLine *gl)
69 {
70     gl->rgba = 0x0000ff7f;
72     gl->normal = Geom::Point(0,1);
73     gl->sensitive = 0;
74 }
76 static void sp_guideline_destroy(GtkObject *object)
77 {
78     GTK_OBJECT_CLASS(parent_class)->destroy(object);
79 }
81 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
82 {
83     SPGuideLine const *gl = SP_GUIDELINE (item);
85     sp_canvas_prepare_buffer(buf);
87     unsigned int const r = NR_RGBA32_R (gl->rgba);
88     unsigned int const g = NR_RGBA32_G (gl->rgba);
89     unsigned int const b = NR_RGBA32_B (gl->rgba);
90     unsigned int const a = NR_RGBA32_A (gl->rgba);
92     if (gl->normal[Geom::Y] == 0.) {
93         if (gl->position < buf->rect.x0 || gl->position >= buf->rect.x1) {
94             return;
95         }
97         int p0 = buf->rect.y0;
98         int p1 = buf->rect.y1;
99         int step = buf->buf_rowstride;
100         unsigned char *d = buf->buf + 3 * (gl->position - buf->rect.x0);
102         for (int p = p0; p < p1; p++) {
103             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
104             d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
105             d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
106             d += step;
107         }
108     } else if (gl->normal[Geom::X] == 0.) {
109         if (gl->position < buf->rect.y0 || gl->position >= buf->rect.y1) {
110             return;
111         }
113         int p0 = buf->rect.x0;
114         int p1 = buf->rect.x1;
115         int step = 3;
116         unsigned char *d = buf->buf + (gl->position - buf->rect.y0) * buf->buf_rowstride;
118         for (int p = p0; p < p1; p++) {
119             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
120             d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
121             d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
122             d += step;
123         }
124     } else {
125         // render angled line
126     }
129 static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
131     SPGuideLine *gl = SP_GUIDELINE(item);
133     if (((SPCanvasItemClass *) parent_class)->update) {
134         ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
135     }
137     if (gl->normal[Geom::Y] == 0.) {
138         gl->position = (int) (affine[4] + 0.5);
139         sp_canvas_update_bbox (item, gl->position, -1000000, gl->position + 1, 1000000);
140     } else {
141         gl->position = (int) (affine[5] - 0.5);
142         sp_canvas_update_bbox (item, -1000000, gl->position, 1000000, gl->position + 1);
143     }
146 static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
148     SPGuideLine *gl = SP_GUIDELINE (item);
150     if (!gl->sensitive) {
151         return NR_HUGE;
152     }
154     *actual_item = item;
156     if (gl->normal[Geom::Y] == 0.) {
157         return MAX(fabs(gl->position - p[NR::X])-1, 0);
158     } else {
159         return MAX(fabs(gl->position - p[NR::Y])-1, 0);
160     }
163 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, double position, Geom::Point normal)
165     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
167     SPGuideLine *gl = SP_GUIDELINE(item);
169     normal.normalize();
170     gl->normal = normal;
171     sp_guideline_set_position(gl, position);
173     return item;
176 void sp_guideline_set_position(SPGuideLine *gl, double position)
178     sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl),
179                                    NR::Matrix(NR::translate(position, position)));
182 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
184     gl->rgba = rgba;
186     sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
189 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
191     gl->sensitive = sensitive;
195 /*
196   Local Variables:
197   mode:c++
198   c-file-style:"stroustrup"
199   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
200   indent-tabs-mode:nil
201   fill-column:99
202   End:
203 */
204 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :