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