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 "libnr/n-art-bpath.h"
26 #include "macros.h"
27 #include "display/canvas-bpath.h"
28 #include "display/canvas-arena.h"
29 #include "display/curve.h"
30 #include "svg/svg-color.h"
31 #include "color.h"
32 #include "color-rgba.h"
33 #include "desktop-style.h"
34 #include "prefs-utils.h"
35 #include "sp-namedview.h"
36 #include "desktop.h"
37 #include "desktop-handles.h"
38 #include "selection.h"
39 #include "document.h"
41 #include "pixmaps/cursor-dropper.xpm"
43 #include "dropper-context.h"
44 #include "message-context.h"
45 #include "libnr/nr-scale-translate-ops.h"
47 #define C1 0.552
48 static NArtBpath const spdc_circle[] = {
49 { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
50 { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
51 { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
52 { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
53 { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
54 { NR_END, 0, 0, 0, 0, 0, 0 }
55 };
56 #undef C1
58 static void sp_dropper_context_class_init(SPDropperContextClass *klass);
59 static void sp_dropper_context_init(SPDropperContext *dc);
61 static void sp_dropper_context_setup(SPEventContext *ec);
62 static void sp_dropper_context_finish(SPEventContext *ec);
64 static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent * event);
66 static SPEventContextClass *parent_class;
68 GType sp_dropper_context_get_type()
69 {
70 static GType type = 0;
71 if (!type) {
72 GTypeInfo info = {
73 sizeof(SPDropperContextClass),
74 NULL, NULL,
75 (GClassInitFunc) sp_dropper_context_class_init,
76 NULL, NULL,
77 sizeof(SPDropperContext),
78 4,
79 (GInstanceInitFunc) sp_dropper_context_init,
80 NULL, /* value_table */
81 };
82 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDropperContext", &info, (GTypeFlags) 0);
83 }
84 return type;
85 }
87 static void sp_dropper_context_class_init(SPDropperContextClass *klass)
88 {
89 SPEventContextClass *ec_class = (SPEventContextClass *) klass;
91 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
93 ec_class->setup = sp_dropper_context_setup;
94 ec_class->finish = sp_dropper_context_finish;
95 ec_class->root_handler = sp_dropper_context_root_handler;
96 }
98 static void sp_dropper_context_init(SPDropperContext *dc)
99 {
100 SPEventContext *event_context = SP_EVENT_CONTEXT(dc);
101 event_context->cursor_shape = cursor_dropper_xpm;
102 event_context->hot_x = 7;
103 event_context->hot_y = 7;
104 }
106 static void sp_dropper_context_setup(SPEventContext *ec)
107 {
108 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
110 if (((SPEventContextClass *) parent_class)->setup) {
111 ((SPEventContextClass *) parent_class)->setup(ec);
112 }
114 SPCurve *c = sp_curve_new_from_foreign_bpath(spdc_circle);
115 dc->area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
116 sp_curve_unref(c);
117 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(dc->area), 0x00000000,(SPWindRule)0);
118 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
119 sp_canvas_item_hide(dc->area);
121 if (prefs_get_int_attribute("tools.dropper", "selcue", 0) != 0) {
122 ec->enableSelectionCue();
123 }
125 if (prefs_get_int_attribute("tools.dropper", "gradientdrag", 0) != 0) {
126 ec->enableGrDrag();
127 }
128 }
130 static void sp_dropper_context_finish(SPEventContext *ec)
131 {
132 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
134 ec->enableGrDrag(false);
136 if (dc->area) {
137 gtk_object_destroy(GTK_OBJECT(dc->area));
138 dc->area = NULL;
139 }
140 }
143 /**
144 * Copies the current context color to the clipboard.
145 */
146 void sp_dropper_context_copy(SPEventContext *ec)
147 {
148 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
150 guint32 const c32 = SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B, dc->alpha);
152 int pick = prefs_get_int_attribute("tools.dropper", "pick",
153 SP_DROPPER_PICK_VISIBLE);
155 int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
157 gchar c[64];
158 g_snprintf(c, 64, "%06x%02x", c32 >> 8,
159 (pick == SP_DROPPER_PICK_ACTUAL && setalpha)? SP_COLOR_F_TO_U(dc->alpha) : 255);
161 Glib::ustring text;
162 text += c;
163 if (!text.empty())
164 {
165 Glib::RefPtr<Gtk::Clipboard> refClipboard =
166 Gtk::Clipboard::get();
167 refClipboard->set_text(text);
168 }
169 }
171 /**
172 * Makes a copy of the current desktop color to the clipboard.
173 */
174 void sp_dropper_c32_color_copy(guint32 c32)
175 {
176 int const pick = prefs_get_int_attribute("tools.dropper", "pick",
177 SP_DROPPER_PICK_VISIBLE);
179 gchar c[64];
180 g_snprintf(c, 64, "%06x%02x", c32 >> 8,
181 pick == SP_DROPPER_PICK_ACTUAL? SP_RGBA32_A_U(c32) : 255);
183 Glib::ustring text;
184 text += c;
185 if (!text.empty()) {
186 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
187 refClipboard->set_text(text);
188 }
189 }
192 /**
193 * Makes a copy of the current color as a hex value. This should always compute
194 * the current color without alpha, but the on-screen representation.
195 */
196 void sp_dropper_c32_color_copy_hex(guint32 c32)
197 {
198 /*
199 int pick = prefs_get_int_attribute ("tools.dropper", "pick",
200 SP_DROPPER_PICK_VISIBLE);
202 if ( pick == SP_DROPPER_PICK_ACTUAL )
203 ; // process c32 so that it computes against page
204 // else just can cut off that last 2 hex digits....
206 */
208 gchar c[48];
209 g_snprintf(c, 48, "%06x", c32 >> 8);
211 Glib::ustring text;
212 text += c;
213 if (!text.empty()) {
214 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
215 refClipboard->set_text(text);
216 }
217 }
220 static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event)
221 {
222 SPDropperContext *dc = (SPDropperContext *) event_context;
223 int ret = FALSE;
224 SPDesktop *desktop = event_context->desktop;
226 int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE);
227 int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
229 switch (event->type) {
230 case GDK_BUTTON_PRESS:
231 if (event->button.button == 1 && !event_context->space_panning) {
232 dc->centre = NR::Point(event->button.x, event->button.y);
233 dc->dragging = TRUE;
234 ret = TRUE;
235 }
236 break;
237 case GDK_MOTION_NOTIFY:
238 if (event->motion.state & GDK_BUTTON2_MASK) {
239 // pass on middle-drag
240 ret = FALSE;
241 break;
242 } else if (!event_context->space_panning) {
243 // otherwise, constantly calculate color no matter is any button pressed or not
245 double rw = 0.0;
246 double W(0), R(0), G(0), B(0), A(0);
248 if (dc->dragging) {
249 // calculate average
251 // radius
252 rw = std::min(NR::L2(NR::Point(event->button.x, event->button.y) - dc->centre), 400.0);
254 if (rw == 0) { // happens sometimes, little idea why...
255 break;
256 }
258 NR::Point const cd = desktop->w2d(dc->centre);
259 NR::Matrix const w2dt = desktop->w2d();
260 const double scale = rw * NR_MATRIX_DF_EXPANSION(&w2dt);
261 NR::Matrix const sm( NR::scale(scale, scale) * NR::translate(cd) );
262 sp_canvas_item_affine_absolute(dc->area, sm);
263 sp_canvas_item_show(dc->area);
265 /* Get buffer */
266 const int x0 = (int) floor(dc->centre[NR::X] - rw);
267 const int y0 = (int) floor(dc->centre[NR::Y] - rw);
268 const int x1 = (int) ceil(dc->centre[NR::X] + rw);
269 const int y1 = (int) ceil(dc->centre[NR::Y] + rw);
271 if ((x1 > x0) && (y1 > y0)) {
272 NRPixBlock pb;
273 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
274 /* fixme: (Lauris) */
275 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
276 for (int y = y0; y < y1; y++) {
277 const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
278 for (int x = x0; x < x1; x++) {
279 const double dx = x - dc->centre[NR::X];
280 const double dy = y - dc->centre[NR::Y];
281 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
282 W += w;
283 R += w * s[0];
284 G += w * s[1];
285 B += w * s[2];
286 A += w * s[3];
287 s += 4;
288 }
289 }
290 nr_pixblock_release(&pb);
292 R = (R + 0.001) / (255.0 * W);
293 G = (G + 0.001) / (255.0 * W);
294 B = (B + 0.001) / (255.0 * W);
295 A = (A + 0.001) / (255.0 * W);
297 R = CLAMP(R, 0.0, 1.0);
298 G = CLAMP(G, 0.0, 1.0);
299 B = CLAMP(B, 0.0, 1.0);
300 A = CLAMP(A, 0.0, 1.0);
301 }
303 } else {
304 // pick single pixel
305 NRPixBlock pb;
306 int x = (int) floor(event->button.x);
307 int y = (int) floor(event->button.y);
308 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
309 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
310 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
312 R = s[0] / 255.0;
313 G = s[1] / 255.0;
314 B = s[2] / 255.0;
315 A = s[3] / 255.0;
316 }
318 if (pick == SP_DROPPER_PICK_VISIBLE) {
319 // compose with page color
320 guint32 bg = sp_desktop_namedview(desktop)->pagecolor;
321 R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
322 G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
323 B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
324 A = 1.0;
325 } else {
326 // un-premultiply color channels
327 if (A > 0) {
328 R /= A;
329 G /= A;
330 B /= A;
331 }
332 }
334 if (fabs(A) < 1e-4) {
335 A = 0; // suppress exponentials, CSS does not allow that
336 }
338 // remember color
339 dc->R = R;
340 dc->G = G;
341 dc->B = B;
342 dc->alpha = A;
344 // status message
345 double alpha_to_set = setalpha? dc->alpha : 1.0;
346 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
348 gchar c[64];
349 sp_svg_write_color(c, sizeof(c), c32);
351 // alpha of color under cursor, to show in the statusbar
352 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
353 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
354 // where the color is picked, to show in the statusbar
355 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
356 // message, to show in the statusbar
357 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");
358 event_context->defaultMessageContext()->setF(
359 Inkscape::NORMAL_MESSAGE,
360 "<b>%s%s</b>%s. %s", c,
361 (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
362 where, message
363 );
365 g_free(where);
366 g_free(alpha);
368 ret = TRUE;
369 }
370 break;
371 case GDK_BUTTON_RELEASE:
372 if (event->button.button == 1 && !event_context->space_panning)
373 {
374 sp_canvas_item_hide(dc->area);
375 dc->dragging = FALSE;
377 double alpha_to_set = setalpha? dc->alpha : 1.0;
379 // do the actual color setting
380 sp_desktop_set_color(desktop,
381 (event->button.state & GDK_MOD1_MASK)?
382 ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
383 false, !(event->button.state & GDK_SHIFT_MASK));
385 // REJON: set aux. toolbar input to hex color!
388 if (!(sp_desktop_selection(desktop)->isEmpty())) {
389 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER,
390 _("Set picked color"));
391 }
393 ret = TRUE;
394 }
395 break;
396 case GDK_KEY_PRESS:
397 switch (get_group0_keyval(&event->key)) {
398 case GDK_Up:
399 case GDK_Down:
400 case GDK_KP_Up:
401 case GDK_KP_Down:
402 // prevent the zoom field from activation
403 if (!MOD__CTRL_ONLY) {
404 ret = TRUE;
405 }
406 break;
407 case GDK_Escape:
408 sp_desktop_selection(desktop)->clear();
409 default:
410 break;
411 }
412 break;
413 default:
414 break;
415 }
417 if (!ret) {
418 if (((SPEventContextClass *) parent_class)->root_handler) {
419 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
420 }
421 }
423 return ret;
424 }
427 /*
428 Local Variables:
429 mode:c++
430 c-file-style:"stroustrup"
431 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
432 indent-tabs-mode:nil
433 fill-column:99
434 End:
435 */
436 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :