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 "preferences.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 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
118 if (prefs->getBool("/tools/dropper/selcue")) {
119 ec->enableSelectionCue();
120 }
122 if (prefs->getBool("/tools/dropper/gradientdrag")) {
123 ec->enableGrDrag();
124 }
125 }
127 static void sp_dropper_context_finish(SPEventContext *ec)
128 {
129 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
131 ec->enableGrDrag(false);
133 if (dc->grabbed) {
134 sp_canvas_item_ungrab(dc->grabbed, GDK_CURRENT_TIME);
135 dc->grabbed = NULL;
136 }
138 if (dc->area) {
139 gtk_object_destroy(GTK_OBJECT(dc->area));
140 dc->area = NULL;
141 }
142 }
145 /**
146 * Returns the current dropper context color.
147 */
148 guint32 sp_dropper_context_get_color(SPEventContext *ec)
149 {
150 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
151 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
153 int pick = prefs->getInt("/tools/dropper/pick",
154 SP_DROPPER_PICK_VISIBLE);
155 bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
157 return SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B,
158 (pick == SP_DROPPER_PICK_ACTUAL && setalpha) ? dc->alpha : 1.0);
159 }
162 static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event)
163 {
164 SPDropperContext *dc = (SPDropperContext *) event_context;
165 int ret = FALSE;
166 SPDesktop *desktop = event_context->desktop;
167 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
169 int pick = prefs->getInt("/tools/dropper/pick", SP_DROPPER_PICK_VISIBLE);
170 bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
172 switch (event->type) {
173 case GDK_BUTTON_PRESS:
174 if (event->button.button == 1 && !event_context->space_panning) {
175 dc->centre = Geom::Point(event->button.x, event->button.y);
176 dc->dragging = TRUE;
177 ret = TRUE;
178 }
180 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
181 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
182 NULL, event->button.time);
183 dc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
185 break;
186 case GDK_MOTION_NOTIFY:
187 if (event->motion.state & GDK_BUTTON2_MASK) {
188 // pass on middle-drag
189 ret = FALSE;
190 break;
191 } else if (!event_context->space_panning) {
192 // otherwise, constantly calculate color no matter is any button pressed or not
194 double rw = 0.0;
195 double W(0), R(0), G(0), B(0), A(0);
197 if (dc->dragging) {
198 // calculate average
200 // radius
201 rw = std::min(Geom::L2(Geom::Point(event->button.x, event->button.y) - dc->centre), 400.0);
203 if (rw == 0) { // happens sometimes, little idea why...
204 break;
205 }
207 Geom::Point const cd = desktop->w2d(dc->centre);
208 Geom::Matrix const w2dt = desktop->w2d();
209 const double scale = rw * w2dt.descrim();
210 Geom::Matrix const sm( Geom::Scale(scale, scale) * Geom::Translate(cd) );
211 sp_canvas_item_affine_absolute(dc->area, sm);
212 sp_canvas_item_show(dc->area);
214 /* Get buffer */
215 const int x0 = (int) floor(dc->centre[Geom::X] - rw);
216 const int y0 = (int) floor(dc->centre[Geom::Y] - rw);
217 const int x1 = (int) ceil(dc->centre[Geom::X] + rw);
218 const int y1 = (int) ceil(dc->centre[Geom::Y] + rw);
220 if ((x1 > x0) && (y1 > y0)) {
221 NRPixBlock pb;
222 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
223 /* fixme: (Lauris) */
224 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
225 for (int y = y0; y < y1; y++) {
226 const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
227 for (int x = x0; x < x1; x++) {
228 const double dx = x - dc->centre[Geom::X];
229 const double dy = y - dc->centre[Geom::Y];
230 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
231 W += w;
232 R += w * s[0];
233 G += w * s[1];
234 B += w * s[2];
235 A += w * s[3];
236 s += 4;
237 }
238 }
239 nr_pixblock_release(&pb);
241 R = (R + 0.001) / (255.0 * W);
242 G = (G + 0.001) / (255.0 * W);
243 B = (B + 0.001) / (255.0 * W);
244 A = (A + 0.001) / (255.0 * W);
246 R = CLAMP(R, 0.0, 1.0);
247 G = CLAMP(G, 0.0, 1.0);
248 B = CLAMP(B, 0.0, 1.0);
249 A = CLAMP(A, 0.0, 1.0);
250 }
252 } else {
253 // pick single pixel
254 NRPixBlock pb;
255 int x = (int) floor(event->button.x);
256 int y = (int) floor(event->button.y);
257 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
258 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
259 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
261 R = s[0] / 255.0;
262 G = s[1] / 255.0;
263 B = s[2] / 255.0;
264 A = s[3] / 255.0;
265 }
267 if (pick == SP_DROPPER_PICK_VISIBLE) {
268 // compose with page color
269 guint32 bg = sp_desktop_namedview(desktop)->pagecolor;
270 R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
271 G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
272 B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
273 A = 1.0;
274 } else {
275 // un-premultiply color channels
276 if (A > 0) {
277 R /= A;
278 G /= A;
279 B /= A;
280 }
281 }
283 if (fabs(A) < 1e-4) {
284 A = 0; // suppress exponentials, CSS does not allow that
285 }
287 // remember color
288 dc->R = R;
289 dc->G = G;
290 dc->B = B;
291 dc->alpha = A;
293 // status message
294 double alpha_to_set = setalpha? dc->alpha : 1.0;
295 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
297 gchar c[64];
298 sp_svg_write_color(c, sizeof(c), c32);
300 // alpha of color under cursor, to show in the statusbar
301 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
302 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
303 // where the color is picked, to show in the statusbar
304 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
305 // message, to show in the statusbar
306 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");
307 event_context->defaultMessageContext()->setF(
308 Inkscape::NORMAL_MESSAGE,
309 "<b>%s%s</b>%s. %s", c,
310 (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
311 where, message
312 );
314 g_free(where);
315 g_free(alpha);
317 ret = TRUE;
318 }
319 break;
320 case GDK_BUTTON_RELEASE:
321 if (event->button.button == 1 && !event_context->space_panning)
322 {
323 sp_canvas_item_hide(dc->area);
324 dc->dragging = FALSE;
326 if (dc->grabbed) {
327 sp_canvas_item_ungrab(dc->grabbed, event->button.time);
328 dc->grabbed = NULL;
329 }
331 double alpha_to_set = setalpha? dc->alpha : 1.0;
333 // do the actual color setting
334 sp_desktop_set_color(desktop,
335 (event->button.state & GDK_MOD1_MASK)?
336 ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
337 false, !(event->button.state & GDK_SHIFT_MASK));
339 // REJON: set aux. toolbar input to hex color!
342 if (!(sp_desktop_selection(desktop)->isEmpty())) {
343 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER,
344 _("Set picked color"));
345 }
347 ret = TRUE;
348 }
349 break;
350 case GDK_KEY_PRESS:
351 switch (get_group0_keyval(&event->key)) {
352 case GDK_Up:
353 case GDK_Down:
354 case GDK_KP_Up:
355 case GDK_KP_Down:
356 // prevent the zoom field from activation
357 if (!MOD__CTRL_ONLY) {
358 ret = TRUE;
359 }
360 break;
361 case GDK_Escape:
362 sp_desktop_selection(desktop)->clear();
363 default:
364 break;
365 }
366 break;
367 default:
368 break;
369 }
371 if (!ret) {
372 if (((SPEventContextClass *) parent_class)->root_handler) {
373 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
374 }
375 }
377 return ret;
378 }
381 /*
382 Local Variables:
383 mode:c++
384 c-file-style:"stroustrup"
385 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
386 indent-tabs-mode:nil
387 fill-column:99
388 End:
389 */
390 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :