1 #define __INKSCAPE_CTRLPOINT_C__
3 /*
4 * Simple point
5 *
6 * Author:
7 * Maximilian Albert <maximilian.albert@gmail.com>
8 *
9 * Copyright (C) 2008 Maximilian Albert
10 *
11 * Released under GNU GPL
12 */
14 #include "display-forward.h"
15 #include "sp-canvas-util.h"
16 #include "sp-ctrlpoint.h"
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include <color.h>
22 #include "display/inkscape-cairo.h"
25 static void sp_ctrlpoint_class_init (SPCtrlPointClass *klass);
26 static void sp_ctrlpoint_init (SPCtrlPoint *ctrlpoint);
27 static void sp_ctrlpoint_destroy (GtkObject *object);
29 static void sp_ctrlpoint_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
30 static void sp_ctrlpoint_render (SPCanvasItem *item, SPCanvasBuf *buf);
32 static SPCanvasItemClass *parent_class;
34 GType
35 sp_ctrlpoint_get_type (void)
36 {
37 static GType type = 0;
38 if (!type) {
39 GTypeInfo info = {
40 sizeof(SPCtrlPointClass),
41 NULL, NULL,
42 (GClassInitFunc) sp_ctrlpoint_class_init,
43 NULL, NULL,
44 sizeof(SPCtrlPoint),
45 0,
46 (GInstanceInitFunc) sp_ctrlpoint_init,
47 NULL
48 };
49 type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPCtrlPoint", &info, (GTypeFlags)0);
50 }
51 return type;
52 }
54 static void
55 sp_ctrlpoint_class_init (SPCtrlPointClass *klass)
56 {
57 GtkObjectClass *object_class = (GtkObjectClass *) klass;
58 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
60 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
62 object_class->destroy = sp_ctrlpoint_destroy;
64 item_class->update = sp_ctrlpoint_update;
65 item_class->render = sp_ctrlpoint_render;
66 }
68 static void
69 sp_ctrlpoint_init (SPCtrlPoint *ctrlpoint)
70 {
71 ctrlpoint->rgba = 0x0000ff7f;
72 ctrlpoint->pt[Geom::X] = ctrlpoint->pt[Geom::Y] = 0.0;
73 ctrlpoint->item=NULL;
74 ctrlpoint->radius = 2;
75 }
77 static void
78 sp_ctrlpoint_destroy (GtkObject *object)
79 {
80 g_return_if_fail (object != NULL);
81 g_return_if_fail (SP_IS_CTRLPOINT (object));
83 SPCtrlPoint *ctrlpoint = SP_CTRLPOINT (object);
85 ctrlpoint->item=NULL;
87 if (GTK_OBJECT_CLASS (parent_class)->destroy)
88 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
89 }
91 static void
92 sp_ctrlpoint_render (SPCanvasItem *item, SPCanvasBuf *buf)
93 {
94 SPCtrlPoint *cp = SP_CTRLPOINT (item);
96 if (!buf->ct)
97 return;
99 sp_canvas_prepare_buffer (buf);
101 guint32 rgba = cp->rgba;
102 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));
104 cairo_set_line_width(buf->ct, 1);
105 cairo_new_path(buf->ct);
107 Geom::Point pt = cp->pt * cp->affine;
109 cairo_arc(buf->ct, pt[Geom::X] - buf->rect.x0, pt[Geom::Y] - buf->rect.y0, cp->radius, 0.0, 2 * M_PI);
110 cairo_stroke(buf->ct);
111 }
113 static void
114 sp_ctrlpoint_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
115 {
116 SPCtrlPoint *cp = SP_CTRLPOINT (item);
118 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
120 if (parent_class->update)
121 (* parent_class->update) (item, affine, flags);
123 sp_canvas_item_reset_bounds (item);
125 cp->affine = affine;
127 Geom::Point pt = cp->pt * affine;
129 item->x1 = pt[Geom::X] - cp->radius;
130 item->y1 = pt[Geom::Y] - cp->radius;
131 item->x2 = pt[Geom::X] + cp->radius;
132 item->y2 = pt[Geom::Y] + cp->radius;
134 sp_canvas_request_redraw (item->canvas,
135 (int)item->x1 - 15, (int)item->y1 - 15,
136 (int)item->x1 + 15, (int)item->y1 + 15);
137 }
139 void
140 sp_ctrlpoint_set_color (SPCtrlPoint *cp, guint32 rgba)
141 {
142 g_return_if_fail (cp != NULL);
143 g_return_if_fail (SP_IS_CTRLPOINT (cp));
145 if (rgba != cp->rgba) {
146 SPCanvasItem *item;
147 cp->rgba = rgba;
148 item = SP_CANVAS_ITEM (cp);
149 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
150 }
151 }
153 #define EPSILON 1e-6
154 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
156 void
157 sp_ctrlpoint_set_coords (SPCtrlPoint *cp, const gdouble x, const gdouble y)
158 {
159 g_return_if_fail (cp != NULL);
160 g_return_if_fail (SP_IS_CTRLPOINT (cp));
162 if (DIFFER (x, cp->pt[Geom::X]) || DIFFER (y, cp->pt[Geom::Y])) {
163 cp->pt[Geom::X] = x;
164 cp->pt[Geom::Y] = y;
165 sp_canvas_item_request_update (SP_CANVAS_ITEM (cp));
166 }
167 }
169 void
170 sp_ctrlpoint_set_coords (SPCtrlPoint *cp, const Geom::Point pt)
171 {
172 sp_ctrlpoint_set_coords(cp, pt[Geom::X], pt[Geom::Y]);
173 }
175 void
176 sp_ctrlpoint_set_radius (SPCtrlPoint *cp, const double r)
177 {
178 cp->radius = r;
179 }
181 /*
182 Local Variables:
183 mode:c++
184 c-file-style:"stroustrup"
185 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
186 indent-tabs-mode:nil
187 fill-column:99
188 End:
189 */
190 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :