1 #define __SP_DROPPER_CONTEXT_C__
3 /*
4 * Tool for picking colors from drawing
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 1999-2005 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include <glibmm/i18n.h>
20 #include <glibmm/ustring.h>
21 #include <glibmm/refptr.h>
22 #include <gtkmm/clipboard.h>
23 #include <gdk/gdkkeysyms.h>
25 #include "macros.h"
26 #include "display/canvas-bpath.h"
27 #include "display/canvas-arena.h"
28 #include "display/curve.h"
29 #include "svg/svg-color.h"
30 #include "color.h"
31 #include "color-rgba.h"
32 #include "desktop-style.h"
33 #include "prefs-utils.h"
34 #include "sp-namedview.h"
35 #include "desktop.h"
36 #include "desktop-handles.h"
37 #include "selection.h"
38 #include "document.h"
40 #include "pixmaps/cursor-dropper.xpm"
42 #include "dropper-context.h"
43 #include "message-context.h"
44 #include "libnr/nr-scale-translate-ops.h"
46 static void sp_dropper_context_class_init(SPDropperContextClass *klass);
47 static void sp_dropper_context_init(SPDropperContext *dc);
49 static void sp_dropper_context_setup(SPEventContext *ec);
50 static void sp_dropper_context_finish(SPEventContext *ec);
52 static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent * event);
54 static SPEventContextClass *parent_class;
56 GType sp_dropper_context_get_type()
57 {
58 static GType type = 0;
59 if (!type) {
60 GTypeInfo info = {
61 sizeof(SPDropperContextClass),
62 NULL, NULL,
63 (GClassInitFunc) sp_dropper_context_class_init,
64 NULL, NULL,
65 sizeof(SPDropperContext),
66 4,
67 (GInstanceInitFunc) sp_dropper_context_init,
68 NULL, /* value_table */
69 };
70 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDropperContext", &info, (GTypeFlags) 0);
71 }
72 return type;
73 }
75 static void sp_dropper_context_class_init(SPDropperContextClass *klass)
76 {
77 SPEventContextClass *ec_class = (SPEventContextClass *) klass;
79 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
81 ec_class->setup = sp_dropper_context_setup;
82 ec_class->finish = sp_dropper_context_finish;
83 ec_class->root_handler = sp_dropper_context_root_handler;
84 }
86 static void sp_dropper_context_init(SPDropperContext *dc)
87 {
88 SPEventContext *event_context = SP_EVENT_CONTEXT(dc);
89 event_context->cursor_shape = cursor_dropper_xpm;
90 event_context->hot_x = 7;
91 event_context->hot_y = 7;
92 }
94 static void sp_dropper_context_setup(SPEventContext *ec)
95 {
96 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
98 if (((SPEventContextClass *) parent_class)->setup) {
99 ((SPEventContextClass *) parent_class)->setup(ec);
100 }
102 /* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
103 SPCurve *c = new SPCurve();
104 const double C1 = 0.552;
105 c->moveto(-1,0);
106 c->curveto(-1, C1, -C1, 1, 0, 1 );
107 c->curveto(C1, 1, 1, C1, 1, 0 );
108 c->curveto(1, -C1, C1, -1, 0, -1 );
109 c->curveto(-C1, -1, -1, -C1, -1, 0 );
110 c->closepath();
111 dc->area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
112 c->unref();
113 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(dc->area), 0x00000000,(SPWindRule)0);
114 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
115 sp_canvas_item_hide(dc->area);
117 if (prefs_get_int_attribute("tools.dropper", "selcue", 0) != 0) {
118 ec->enableSelectionCue();
119 }
121 if (prefs_get_int_attribute("tools.dropper", "gradientdrag", 0) != 0) {
122 ec->enableGrDrag();
123 }
124 }
126 static void sp_dropper_context_finish(SPEventContext *ec)
127 {
128 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
130 ec->enableGrDrag(false);
132 if (dc->area) {
133 gtk_object_destroy(GTK_OBJECT(dc->area));
134 dc->area = NULL;
135 }
136 }
139 /**
140 * Returns the current dropper context color.
141 */
142 guint32 sp_dropper_context_get_color(SPEventContext *ec)
143 {
144 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
146 int pick = prefs_get_int_attribute("tools.dropper", "pick",
147 SP_DROPPER_PICK_VISIBLE);
148 int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
150 return SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B,
151 (pick == SP_DROPPER_PICK_ACTUAL && setalpha) ? dc->alpha : 1.0);
152 }
155 static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event)
156 {
157 SPDropperContext *dc = (SPDropperContext *) event_context;
158 int ret = FALSE;
159 SPDesktop *desktop = event_context->desktop;
161 int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE);
162 int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
164 switch (event->type) {
165 case GDK_BUTTON_PRESS:
166 if (event->button.button == 1 && !event_context->space_panning) {
167 dc->centre = NR::Point(event->button.x, event->button.y);
168 dc->dragging = TRUE;
169 ret = TRUE;
170 }
171 break;
172 case GDK_MOTION_NOTIFY:
173 if (event->motion.state & GDK_BUTTON2_MASK) {
174 // pass on middle-drag
175 ret = FALSE;
176 break;
177 } else if (!event_context->space_panning) {
178 // otherwise, constantly calculate color no matter is any button pressed or not
180 double rw = 0.0;
181 double W(0), R(0), G(0), B(0), A(0);
183 if (dc->dragging) {
184 // calculate average
186 // radius
187 rw = std::min(NR::L2(NR::Point(event->button.x, event->button.y) - dc->centre), 400.0);
189 if (rw == 0) { // happens sometimes, little idea why...
190 break;
191 }
193 NR::Point const cd = desktop->w2d(dc->centre);
194 NR::Matrix const w2dt = desktop->w2d();
195 const double scale = rw * NR::expansion(w2dt);
196 NR::Matrix const sm( NR::scale(scale, scale) * NR::translate(cd) );
197 sp_canvas_item_affine_absolute(dc->area, sm);
198 sp_canvas_item_show(dc->area);
200 /* Get buffer */
201 const int x0 = (int) floor(dc->centre[NR::X] - rw);
202 const int y0 = (int) floor(dc->centre[NR::Y] - rw);
203 const int x1 = (int) ceil(dc->centre[NR::X] + rw);
204 const int y1 = (int) ceil(dc->centre[NR::Y] + rw);
206 if ((x1 > x0) && (y1 > y0)) {
207 NRPixBlock pb;
208 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
209 /* fixme: (Lauris) */
210 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
211 for (int y = y0; y < y1; y++) {
212 const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
213 for (int x = x0; x < x1; x++) {
214 const double dx = x - dc->centre[NR::X];
215 const double dy = y - dc->centre[NR::Y];
216 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
217 W += w;
218 R += w * s[0];
219 G += w * s[1];
220 B += w * s[2];
221 A += w * s[3];
222 s += 4;
223 }
224 }
225 nr_pixblock_release(&pb);
227 R = (R + 0.001) / (255.0 * W);
228 G = (G + 0.001) / (255.0 * W);
229 B = (B + 0.001) / (255.0 * W);
230 A = (A + 0.001) / (255.0 * W);
232 R = CLAMP(R, 0.0, 1.0);
233 G = CLAMP(G, 0.0, 1.0);
234 B = CLAMP(B, 0.0, 1.0);
235 A = CLAMP(A, 0.0, 1.0);
236 }
238 } else {
239 // pick single pixel
240 NRPixBlock pb;
241 int x = (int) floor(event->button.x);
242 int y = (int) floor(event->button.y);
243 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
244 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
245 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
247 R = s[0] / 255.0;
248 G = s[1] / 255.0;
249 B = s[2] / 255.0;
250 A = s[3] / 255.0;
251 }
253 if (pick == SP_DROPPER_PICK_VISIBLE) {
254 // compose with page color
255 guint32 bg = sp_desktop_namedview(desktop)->pagecolor;
256 R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
257 G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
258 B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
259 A = 1.0;
260 } else {
261 // un-premultiply color channels
262 if (A > 0) {
263 R /= A;
264 G /= A;
265 B /= A;
266 }
267 }
269 if (fabs(A) < 1e-4) {
270 A = 0; // suppress exponentials, CSS does not allow that
271 }
273 // remember color
274 dc->R = R;
275 dc->G = G;
276 dc->B = B;
277 dc->alpha = A;
279 // status message
280 double alpha_to_set = setalpha? dc->alpha : 1.0;
281 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
283 gchar c[64];
284 sp_svg_write_color(c, sizeof(c), c32);
286 // alpha of color under cursor, to show in the statusbar
287 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
288 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
289 // where the color is picked, to show in the statusbar
290 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
291 // message, to show in the statusbar
292 const gchar *message = dc->dragging ? _("<b>Release mouse</b> to set color.") : _("<b>Click</b> to set fill, <b>Shift+click</b> to set stroke; <b>drag</b> to average color in area; with <b>Alt</b> to pick inverse color; <b>Ctrl+C</b> to copy the color under mouse to clipboard");
293 event_context->defaultMessageContext()->setF(
294 Inkscape::NORMAL_MESSAGE,
295 "<b>%s%s</b>%s. %s", c,
296 (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
297 where, message
298 );
300 g_free(where);
301 g_free(alpha);
303 ret = TRUE;
304 }
305 break;
306 case GDK_BUTTON_RELEASE:
307 if (event->button.button == 1 && !event_context->space_panning)
308 {
309 sp_canvas_item_hide(dc->area);
310 dc->dragging = FALSE;
312 double alpha_to_set = setalpha? dc->alpha : 1.0;
314 // do the actual color setting
315 sp_desktop_set_color(desktop,
316 (event->button.state & GDK_MOD1_MASK)?
317 ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
318 false, !(event->button.state & GDK_SHIFT_MASK));
320 // REJON: set aux. toolbar input to hex color!
323 if (!(sp_desktop_selection(desktop)->isEmpty())) {
324 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER,
325 _("Set picked color"));
326 }
328 ret = TRUE;
329 }
330 break;
331 case GDK_KEY_PRESS:
332 switch (get_group0_keyval(&event->key)) {
333 case GDK_Up:
334 case GDK_Down:
335 case GDK_KP_Up:
336 case GDK_KP_Down:
337 // prevent the zoom field from activation
338 if (!MOD__CTRL_ONLY) {
339 ret = TRUE;
340 }
341 break;
342 case GDK_Escape:
343 sp_desktop_selection(desktop)->clear();
344 default:
345 break;
346 }
347 break;
348 default:
349 break;
350 }
352 if (!ret) {
353 if (((SPEventContextClass *) parent_class)->root_handler) {
354 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
355 }
356 }
358 return ret;
359 }
362 /*
363 Local Variables:
364 mode:c++
365 c-file-style:"stroustrup"
366 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
367 indent-tabs-mode:nil
368 fill-column:99
369 End:
370 */
371 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :