b029be7a0ee91f02ea162ff3151b314d7c87af26
1 #define __SP_GUIDELINE_C__
3 /*
4 * Infinite horizontal/vertical line
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2000-2002 Lauris Kaplinski
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
15 #include <libnr/nr-pixops.h>
16 #include "display-forward.h"
17 #include "sp-canvas-util.h"
18 #include "guideline.h"
20 static void sp_guideline_class_init(SPGuideLineClass *c);
21 static void sp_guideline_init(SPGuideLine *guideline);
22 static void sp_guideline_destroy(GtkObject *object);
24 static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
25 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf);
27 static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
29 static SPCanvasItemClass *parent_class;
31 GType sp_guideline_get_type()
32 {
33 static GType guideline_type = 0;
35 if (!guideline_type) {
36 static GTypeInfo const guideline_info = {
37 sizeof (SPGuideLineClass),
38 NULL, NULL,
39 (GClassInitFunc) sp_guideline_class_init,
40 NULL, NULL,
41 sizeof (SPGuideLine),
42 16,
43 (GInstanceInitFunc) sp_guideline_init,
44 NULL,
45 };
47 guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
48 }
50 return guideline_type;
51 }
53 static void sp_guideline_class_init(SPGuideLineClass *c)
54 {
55 parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
57 GtkObjectClass *object_class = (GtkObjectClass *) c;
58 object_class->destroy = sp_guideline_destroy;
60 SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
61 item_class->update = sp_guideline_update;
62 item_class->render = sp_guideline_render;
63 item_class->point = sp_guideline_point;
64 }
66 static void sp_guideline_init(SPGuideLine *gl)
67 {
68 gl->rgba = 0x0000ff7f;
70 gl->normal = Geom::Point(0,1);
71 gl->sensitive = 0;
72 }
74 static void sp_guideline_destroy(GtkObject *object)
75 {
76 GTK_OBJECT_CLASS(parent_class)->destroy(object);
77 }
79 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
80 {
81 SPGuideLine const *gl = SP_GUIDELINE (item);
83 sp_canvas_prepare_buffer(buf);
85 unsigned int const r = NR_RGBA32_R (gl->rgba);
86 unsigned int const g = NR_RGBA32_G (gl->rgba);
87 unsigned int const b = NR_RGBA32_B (gl->rgba);
88 unsigned int const a = NR_RGBA32_A (gl->rgba);
90 int p0, p1, step;
91 unsigned char *d;
93 if (gl->normal[Geom::Y] == 0.) {
95 if (gl->position < buf->rect.x0 || gl->position >= buf->rect.x1) {
96 return;
97 }
99 p0 = buf->rect.y0;
100 p1 = buf->rect.y1;
101 step = buf->buf_rowstride;
102 d = buf->buf + 3 * (gl->position - buf->rect.x0);
104 } else {
106 if (gl->position < buf->rect.y0 || gl->position >= buf->rect.y1) {
107 return;
108 }
110 p0 = buf->rect.x0;
111 p1 = buf->rect.x1;
112 step = 3;
113 d = buf->buf + (gl->position - buf->rect.y0) * buf->buf_rowstride;
114 }
116 for (int p = p0; p < p1; p++) {
117 d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
118 d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
119 d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
120 d += step;
121 }
122 }
124 static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
125 {
126 SPGuideLine *gl = SP_GUIDELINE(item);
128 if (((SPCanvasItemClass *) parent_class)->update) {
129 ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
130 }
132 if (gl->normal[Geom::Y] == 0.) {
133 gl->position = (int) (affine[4] + 0.5);
134 sp_canvas_update_bbox (item, gl->position, -1000000, gl->position + 1, 1000000);
135 } else {
136 gl->position = (int) (affine[5] - 0.5);
137 sp_canvas_update_bbox (item, -1000000, gl->position, 1000000, gl->position + 1);
138 }
139 }
141 static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
142 {
143 SPGuideLine *gl = SP_GUIDELINE (item);
145 if (!gl->sensitive) {
146 return NR_HUGE;
147 }
149 *actual_item = item;
151 if (gl->normal[Geom::Y] == 0.) {
152 return MAX(fabs(gl->position - p[NR::X])-1, 0);
153 } else {
154 return MAX(fabs(gl->position - p[NR::Y])-1, 0);
155 }
156 }
158 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, double position, Geom::Point normal)
159 {
160 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
162 SPGuideLine *gl = SP_GUIDELINE(item);
164 normal.normalize();
165 gl->normal = normal;
166 sp_guideline_set_position(gl, position);
168 return item;
169 }
171 void sp_guideline_set_position(SPGuideLine *gl, double position)
172 {
173 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl),
174 NR::Matrix(NR::translate(position, position)));
175 }
177 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
178 {
179 gl->rgba = rgba;
181 sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
182 }
184 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
185 {
186 gl->sensitive = sensitive;
187 }
190 /*
191 Local Variables:
192 mode:c++
193 c-file-style:"stroustrup"
194 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
195 indent-tabs-mode:nil
196 fill-column:99
197 End:
198 */
199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :