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 *ec, GdkEvent *event)
221 {
222 SPDropperContext *dc = (SPDropperContext *) ec;
223 int ret = FALSE;
225 int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE);
226 int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
228 switch (event->type) {
229 case GDK_BUTTON_PRESS:
230 if (event->button.button == 1) {
231 dc->centre = NR::Point(event->button.x, event->button.y);
232 dc->dragging = TRUE;
233 ret = TRUE;
234 }
235 break;
236 case GDK_MOTION_NOTIFY:
237 if (event->motion.state & GDK_BUTTON2_MASK) {
238 // pass on middle-drag
239 ret = FALSE;
240 break;
241 } else {
242 // otherwise, constantly calculate color no matter is any button pressed or not
244 double rw = 0.0;
245 double W(0), R(0), G(0), B(0), A(0);
247 if (dc->dragging) {
248 // calculate average
250 // radius
251 rw = std::min(NR::L2(NR::Point(event->button.x, event->button.y) - dc->centre), 400.0);
253 if (rw == 0) { // happens sometimes, little idea why...
254 break;
255 }
257 NR::Point const cd = ec->desktop->w2d(dc->centre);
258 NR::Matrix const w2dt = ec->desktop->w2d();
259 const double scale = rw * NR_MATRIX_DF_EXPANSION(&w2dt);
260 NR::Matrix const sm( NR::scale(scale, scale) * NR::translate(cd) );
261 sp_canvas_item_affine_absolute(dc->area, sm);
262 sp_canvas_item_show(dc->area);
264 /* Get buffer */
265 const int x0 = (int) floor(dc->centre[NR::X] - rw);
266 const int y0 = (int) floor(dc->centre[NR::Y] - rw);
267 const int x1 = (int) ceil(dc->centre[NR::X] + rw);
268 const int y1 = (int) ceil(dc->centre[NR::Y] + rw);
270 if ((x1 > x0) && (y1 > y0)) {
271 NRPixBlock pb;
272 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
273 /* fixme: (Lauris) */
274 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(ec->desktop)), &pb);
275 for (int y = y0; y < y1; y++) {
276 const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
277 for (int x = x0; x < x1; x++) {
278 const double dx = x - dc->centre[NR::X];
279 const double dy = y - dc->centre[NR::Y];
280 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
281 W += w;
282 R += w * s[0];
283 G += w * s[1];
284 B += w * s[2];
285 A += w * s[3];
286 s += 4;
287 }
288 }
289 nr_pixblock_release(&pb);
291 R = (R + 0.001) / (255.0 * W);
292 G = (G + 0.001) / (255.0 * W);
293 B = (B + 0.001) / (255.0 * W);
294 A = (A + 0.001) / (255.0 * W);
296 R = CLAMP(R, 0.0, 1.0);
297 G = CLAMP(G, 0.0, 1.0);
298 B = CLAMP(B, 0.0, 1.0);
299 A = CLAMP(A, 0.0, 1.0);
300 }
302 } else {
303 // pick single pixel
304 NRPixBlock pb;
305 int x = (int) floor(event->button.x);
306 int y = (int) floor(event->button.y);
307 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
308 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(ec->desktop)), &pb);
309 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
311 R = s[0] / 255.0;
312 G = s[1] / 255.0;
313 B = s[2] / 255.0;
314 A = s[3] / 255.0;
315 }
317 if (pick == SP_DROPPER_PICK_VISIBLE) {
318 // compose with page color
319 guint32 bg = sp_desktop_namedview(ec->desktop)->pagecolor;
320 R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
321 G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
322 B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
323 A = 1.0;
324 } else {
325 // un-premultiply color channels
326 if (A > 0) {
327 R /= A;
328 G /= A;
329 B /= A;
330 }
331 }
333 if (fabs(A) < 1e-4) {
334 A = 0; // suppress exponentials, CSS does not allow that
335 }
337 // remember color
338 dc->R = R;
339 dc->G = G;
340 dc->B = B;
341 dc->alpha = A;
343 // status message
344 double alpha_to_set = setalpha? dc->alpha : 1.0;
345 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
347 gchar c[64];
348 sp_svg_write_color(c, 64, c32);
350 // alpha of color under cursor, to show in the statusbar
351 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
352 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
353 // where the color is picked, to show in the statusbar
354 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
355 // message, to show in the statusbar
356 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");
357 ec->defaultMessageContext()->setF(
358 Inkscape::NORMAL_MESSAGE,
359 "<b>%s%s</b>%s. %s", c,
360 (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
361 where, message
362 );
364 g_free(where);
365 g_free(alpha);
367 ret = TRUE;
368 }
369 break;
370 case GDK_BUTTON_RELEASE:
371 if (event->button.button == 1)
372 {
373 sp_canvas_item_hide(dc->area);
374 dc->dragging = FALSE;
376 double alpha_to_set = setalpha? dc->alpha : 1.0;
378 // do the actual color setting
379 sp_desktop_set_color(ec->desktop,
380 (event->button.state & GDK_MOD1_MASK)?
381 ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
382 false, !(event->button.state & GDK_SHIFT_MASK));
384 // REJON: set aux. toolbar input to hex color!
387 if (!(sp_desktop_selection(ec->desktop)->isEmpty())) {
388 sp_document_done(sp_desktop_document(ec->desktop), SP_VERB_CONTEXT_DROPPER,
389 _("Set picked color"));
390 }
392 ret = TRUE;
393 }
394 break;
395 case GDK_KEY_PRESS:
396 switch (get_group0_keyval(&event->key)) {
397 case GDK_Up:
398 case GDK_Down:
399 case GDK_KP_Up:
400 case GDK_KP_Down:
401 // prevent the zoom field from activation
402 if (!MOD__CTRL_ONLY) {
403 ret = TRUE;
404 }
405 break;
406 case GDK_Escape:
407 sp_desktop_selection(ec->desktop)->clear();
408 default:
409 break;
410 }
411 break;
412 default:
413 break;
414 }
416 if (!ret) {
417 if (((SPEventContextClass *) parent_class)->root_handler) {
418 ret = ((SPEventContextClass *) parent_class)->root_handler(ec, event);
419 }
420 }
422 return ret;
423 }
426 /*
427 Local Variables:
428 mode:c++
429 c-file-style:"stroustrup"
430 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
431 indent-tabs-mode:nil
432 fill-column:99
433 End:
434 */
435 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :