be3b8dc4537be37402111cad419b6ddeabb5498a
1 #define __INKSCAPE_CTRLRECT_C__
3 /*
4 * Simple non-transformed rectangle, usable for rubberband
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@ximian.com>
8 * bulia byak <buliabyak@users.sf.net>
9 * Carl Hetherington <inkscape@carlh.net>
10 *
11 * Copyright (C) 1999-2001 Lauris Kaplinski
12 * Copyright (C) 2000-2001 Ximian, Inc.
13 *
14 * Released under GNU GPL
15 *
16 */
18 #include "display-forward.h"
19 #include "sp-canvas-util.h"
20 #include "sodipodi-ctrlrect.h"
21 #include "libnr/nr-pixops.h"
23 /*
24 * Currently we do not have point method, as it should always be painted
25 * during some transformation, which takes care of events...
26 *
27 * Corner coords can be in any order - i.e. x1 < x0 is allowed
28 */
30 static void sp_ctrlrect_class_init(SPCtrlRectClass *c);
31 static void sp_ctrlrect_init(CtrlRect *ctrlrect);
32 static void sp_ctrlrect_destroy(GtkObject *object);
34 static void sp_ctrlrect_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
35 static void sp_ctrlrect_render(SPCanvasItem *item, SPCanvasBuf *buf);
37 static SPCanvasItemClass *parent_class;
39 static const guint DASH_LENGTH = 4;
41 GType sp_ctrlrect_get_type()
42 {
43 static GType type = 0;
45 if (!type) {
46 GTypeInfo info = {
47 sizeof(SPCtrlRectClass),
48 0, // base_init
49 0, // base_finalize
50 (GClassInitFunc)sp_ctrlrect_class_init,
51 0, // class_finalize
52 0, // class_data
53 sizeof(CtrlRect),
54 0, // n_preallocs
55 (GInstanceInitFunc)sp_ctrlrect_init,
56 0 // value_table
57 };
58 type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPCtrlRect", &info, static_cast<GTypeFlags>(0));
59 }
60 return type;
61 }
63 static void sp_ctrlrect_class_init(SPCtrlRectClass *c)
64 {
65 GtkObjectClass *object_class = (GtkObjectClass *) c;
66 SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
68 parent_class = (SPCanvasItemClass*) gtk_type_class(sp_canvas_item_get_type());
70 object_class->destroy = sp_ctrlrect_destroy;
72 item_class->update = sp_ctrlrect_update;
73 item_class->render = sp_ctrlrect_render;
74 }
76 static void sp_ctrlrect_init(CtrlRect *cr)
77 {
78 cr->init();
79 }
81 static void sp_ctrlrect_destroy(GtkObject *object)
82 {
83 if (GTK_OBJECT_CLASS(parent_class)->destroy) {
84 (* GTK_OBJECT_CLASS(parent_class)->destroy)(object);
85 }
86 }
88 /* FIXME: use definitions from somewhere else */
89 #define RGBA_R(v) ((v) >> 24)
90 #define RGBA_G(v) (((v) >> 16) & 0xff)
91 #define RGBA_B(v) (((v) >> 8) & 0xff)
92 #define RGBA_A(v) ((v) & 0xff)
94 static void sp_ctrlrect_hline(SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba, guint dashed)
95 {
96 if (y >= buf->rect.y0 && y < buf->rect.y1) {
97 guint const r = RGBA_R(rgba);
98 guint const g = RGBA_G(rgba);
99 guint const b = RGBA_B(rgba);
100 guint const a = RGBA_A(rgba);
101 gint const x0 = MAX(buf->rect.x0, xs);
102 gint const x1 = MIN(buf->rect.x1, xe + 1);
103 guchar *p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
104 for (gint x = x0; x < x1; x++) {
105 if (!dashed || ((x / DASH_LENGTH) % 2)) {
106 p[0] = INK_COMPOSE(r, a, p[0]);
107 p[1] = INK_COMPOSE(g, a, p[1]);
108 p[2] = INK_COMPOSE(b, a, p[2]);
109 }
110 p += 3;
111 }
112 }
113 }
115 static void sp_ctrlrect_vline(SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba, guint dashed)
116 {
117 if (x >= buf->rect.x0 && x < buf->rect.x1) {
118 guint const r = RGBA_R(rgba);
119 guint const g = RGBA_G(rgba);
120 guint const b = RGBA_B(rgba);
121 guint const a = RGBA_A(rgba);
122 gint const y0 = MAX(buf->rect.y0, ys);
123 gint const y1 = MIN(buf->rect.y1, ye + 1);
124 guchar *p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
125 for (gint y = y0; y < y1; y++) {
126 if (!dashed || ((y / DASH_LENGTH) % 2)) {
127 p[0] = INK_COMPOSE(r, a, p[0]);
128 p[1] = INK_COMPOSE(g, a, p[1]);
129 p[2] = INK_COMPOSE(b, a, p[2]);
130 }
131 p += buf->buf_rowstride;
132 }
133 }
134 }
136 /** Fills the pixels in [xs, xe)*[ys,ye) clipped to the tile with rgb * a. */
137 static void sp_ctrlrect_area(SPCanvasBuf *buf, gint xs, gint ys, gint xe, gint ye, guint32 rgba)
138 {
139 guint const r = RGBA_R(rgba);
140 guint const g = RGBA_G(rgba);
141 guint const b = RGBA_B(rgba);
142 guint const a = RGBA_A(rgba);
143 gint const x0 = MAX(buf->rect.x0, xs);
144 gint const x1 = MIN(buf->rect.x1, xe + 1);
145 gint const y0 = MAX(buf->rect.y0, ys);
146 gint const y1 = MIN(buf->rect.y1, ye + 1);
147 for (gint y = y0; y < y1; y++) {
148 guchar *p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
149 for (gint x = x0; x < x1; x++) {
150 p[0] = INK_COMPOSE(r, a, p[0]);
151 p[1] = INK_COMPOSE(g, a, p[1]);
152 p[2] = INK_COMPOSE(b, a, p[2]);
153 p += 3;
154 }
155 }
156 }
158 static void sp_ctrlrect_render(SPCanvasItem *item, SPCanvasBuf *buf)
159 {
160 SP_CTRLRECT(item)->render(buf);
161 }
164 static void sp_ctrlrect_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
165 {
166 SP_CTRLRECT(item)->update(affine, flags);
167 }
171 void CtrlRect::init()
172 {
173 _has_fill = false;
174 _dashed = false;
175 _shadow = 0;
177 _area.x0 = _area.y0 = 0;
178 _area.x1 = _area.y1 = 0;
180 _rect = NR::Rect(NR::Point(0,0),NR::Point(0,0));
182 _shadow_size = 0;
184 _border_color = 0x000000ff;
185 _fill_color = 0xffffffff;
186 _shadow_color = 0x000000ff;
187 }
190 void CtrlRect::render(SPCanvasBuf *buf)
191 {
192 if ((_area.x0 != 0 || _area.x1 != 0 || _area.y0 != 0 || _area.y1 != 0) &&
193 (_area.x0 < buf->rect.x1) &&
194 (_area.y0 < buf->rect.y1) &&
195 ((_area.x1 + _shadow_size) >= buf->rect.x0) &&
196 ((_area.y1 + _shadow_size) >= buf->rect.y0)) {
197 sp_canvas_prepare_buffer(buf);
199 /* Top */
200 sp_ctrlrect_hline(buf, _area.y0, _area.x0, _area.x1, _border_color, _dashed);
201 /* Bottom */
202 sp_ctrlrect_hline(buf, _area.y1, _area.x0, _area.x1, _border_color, _dashed);
203 /* Left */
204 sp_ctrlrect_vline(buf, _area.x0, _area.y0 + 1, _area.y1 - 1, _border_color, _dashed);
205 /* Right */
206 sp_ctrlrect_vline(buf, _area.x1, _area.y0 + 1, _area.y1 - 1, _border_color, _dashed);
207 if (_shadow_size > 0) {
208 /* Right shadow */
209 sp_ctrlrect_area(buf, _area.x1 + 1, _area.y0 + _shadow_size,
210 _area.x1 + _shadow_size, _area.y1 + _shadow_size, _shadow_color);
211 /* Bottom shadow */
212 sp_ctrlrect_area(buf, _area.x0 + _shadow_size, _area.y1 + 1,
213 _area.x1, _area.y1 + _shadow_size, _shadow_color);
214 }
215 if (_has_fill) {
216 /* Fill */
217 sp_ctrlrect_area(buf, _area.x0 + 1, _area.y0 + 1,
218 _area.x1 - 1, _area.y1 - 1, _fill_color);
219 }
220 }
221 }
224 void CtrlRect::update(NR::Matrix const &affine, unsigned int flags)
225 {
226 if (((SPCanvasItemClass *) parent_class)->update) {
227 ((SPCanvasItemClass *) parent_class)->update(this, affine, flags);
228 }
230 sp_canvas_item_reset_bounds(this);
232 if (_area.x0 != 0 || _area.x1 != 0 || _area.y0 != 0 || _area.y1 != 0) {
233 /* Request redraw old */
234 if (!_has_fill) {
235 /* Top */
236 sp_canvas_request_redraw(canvas,
237 _area.x0 - 1, _area.y0 - 1,
238 _area.x1 + 1, _area.y0 + 1);
239 /* Left */
240 sp_canvas_request_redraw(canvas,
241 _area.x0 - 1, _area.y0 - 1,
242 _area.x0 + 1, _area.y1 + 1);
243 /* Right */
244 sp_canvas_request_redraw(canvas,
245 _area.x1 - 1, _area.y0 - 1,
246 _area.x1 + _shadow_size + 1, _area.y1 + _shadow_size + 1);
247 /* Bottom */
248 sp_canvas_request_redraw(canvas,
249 _area.x0 - 1, _area.y1 - 1,
250 _area.x1 + _shadow_size + 1, _area.y1 + _shadow_size + 1);
251 } else {
252 sp_canvas_request_redraw(canvas,
253 _area.x0 - 1, _area.y0 - 1,
254 _area.x1 + _shadow_size + 1, _area.y1 + _shadow_size + 1);
255 }
256 }
258 NR::Rect bbox(_rect.min() * affine, _rect.max() * affine);
260 _area.x0 = (int) floor(bbox.min()[NR::X] + 0.5);
261 _area.y0 = (int) floor(bbox.min()[NR::Y] + 0.5);
262 _area.x1 = (int) floor(bbox.max()[NR::X] + 0.5);
263 _area.y1 = (int) floor(bbox.max()[NR::Y] + 0.5);
265 _shadow_size = _shadow;
267 if (_area.x0 != 0 || _area.x1 != 0 || _area.y0 != 0 || _area.y1 != 0) {
268 /* Request redraw new */
269 if (!_has_fill) {
270 /* Top */
271 sp_canvas_request_redraw(canvas,
272 _area.x0 - 1, _area.y0 - 1,
273 _area.x1 + 1, _area.y0 + 1);
274 /* Left */
275 sp_canvas_request_redraw(canvas,
276 _area.x0 - 1, _area.y0 - 1,
277 _area.x0 + 1, _area.y1 + 1);
278 /* Right */
279 sp_canvas_request_redraw(canvas,
280 _area.x1 - 1, _area.y0 - 1,
281 _area.x1 + _shadow_size + 1, _area.y1 + _shadow_size + 1);
282 /* Bottom */
283 sp_canvas_request_redraw(canvas,
284 _area.x0 - 1, _area.y1 - 1,
285 _area.x1 + _shadow_size + 1, _area.y1 + _shadow_size + 1);
286 } else {
287 sp_canvas_request_redraw(canvas,
288 _area.x0 - 1, _area.y0 - 1,
289 _area.x1 + _shadow_size + 1, _area.y1 + _shadow_size + 1);
290 }
292 x1 = _area.x0 - 1;
293 y1 = _area.y0 - 1;
294 x2 = _area.x1 + _shadow_size + 1;
295 y2 = _area.y1 + _shadow_size + 1;
296 }
297 }
300 void CtrlRect::setColor(guint32 b, bool h, guint f)
301 {
302 _border_color = b;
303 _has_fill = h;
304 _fill_color = f;
305 _requestUpdate();
306 }
308 void CtrlRect::setShadow(int s, guint c)
309 {
310 _shadow = s;
311 _shadow_color = c;
312 _requestUpdate();
313 }
315 void CtrlRect::setRectangle(NR::Rect const &r)
316 {
317 _rect = r;
318 _requestUpdate();
319 }
321 void CtrlRect::setDashed(bool d)
322 {
323 _dashed = d;
324 _requestUpdate();
325 }
327 void CtrlRect::_requestUpdate()
328 {
329 sp_canvas_item_request_update(SP_CANVAS_ITEM(this));
330 }
332 /*
333 Local Variables:
334 mode:c++
335 c-file-style:"stroustrup"
336 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
337 indent-tabs-mode:nil
338 fill-column:99
339 End:
340 */
341 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :