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.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_static_bpath(spdc_circle);
115 dc->area = sp_canvas_bpath_new(SP_DT_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 gchar c[64];
156 g_snprintf(c, 64, "%06x%02x", c32 >> 8,
157 pick == SP_DROPPER_PICK_ACTUAL? SP_COLOR_F_TO_U(dc->alpha) : 255);
159 Glib::ustring text;
160 text += c;
161 if (!text.empty())
162 {
163 Glib::RefPtr<Gtk::Clipboard> refClipboard =
164 Gtk::Clipboard::get();
165 refClipboard->set_text(text);
166 }
167 }
169 /**
170 * Makes a copy of the current desktop color to the clipboard.
171 */
172 void sp_dropper_c32_color_copy(guint32 c32)
173 {
174 int const pick = prefs_get_int_attribute("tools.dropper", "pick",
175 SP_DROPPER_PICK_VISIBLE);
177 gchar c[64];
178 g_snprintf(c, 64, "%06x%02x", c32 >> 8,
179 pick == SP_DROPPER_PICK_ACTUAL? SP_RGBA32_A_U(c32) : 255);
181 Glib::ustring text;
182 text += c;
183 if (!text.empty()) {
184 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
185 refClipboard->set_text(text);
186 }
187 }
190 /**
191 * Makes a copy of the current color as a hex value. This should always compute
192 * the current color without alpha, but the on-screen representation.
193 */
194 void sp_dropper_c32_color_copy_hex(guint32 c32)
195 {
196 /*
197 int pick = prefs_get_int_attribute ("tools.dropper", "pick",
198 SP_DROPPER_PICK_VISIBLE);
200 if ( pick == SP_DROPPER_PICK_ACTUAL )
201 ; // process c32 so that it computes against page
202 // else just can cut off that last 2 hex digits....
204 */
206 gchar c[48];
207 g_snprintf(c, 48, "%06x", c32 >> 8);
209 Glib::ustring text;
210 text += c;
211 if (!text.empty()) {
212 Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
213 refClipboard->set_text(text);
214 }
215 }
218 static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent *event)
219 {
220 SPDropperContext *dc = (SPDropperContext *) ec;
221 int ret = FALSE;
223 int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE);
225 switch (event->type) {
226 case GDK_BUTTON_PRESS:
227 if (event->button.button == 1) {
228 dc->centre = NR::Point(event->button.x, event->button.y);
229 dc->dragging = TRUE;
230 ret = TRUE;
231 }
232 break;
233 case GDK_MOTION_NOTIFY:
234 if (event->motion.state & GDK_BUTTON2_MASK) {
235 // pass on middle-drag
236 ret = FALSE;
237 break;
238 } else {
239 // otherwise, constantly calculate color no matter is any button pressed or not
241 double rw = 0.0;
242 double W(0), R(0), G(0), B(0), A(0);
244 if (dc->dragging) {
245 // calculate average
247 // radius
248 rw = std::min(NR::L2(NR::Point(event->button.x, event->button.y) - dc->centre), 400.0);
250 if (rw == 0) { // happens sometimes, little idea why...
251 break;
252 }
254 NR::Point const cd = ec->desktop->w2d(dc->centre);
255 NR::Matrix const w2dt = ec->desktop->w2d();
256 const double scale = rw * NR_MATRIX_DF_EXPANSION(&w2dt);
257 NR::Matrix const sm( NR::scale(scale, scale) * NR::translate(cd) );
258 sp_canvas_item_affine_absolute(dc->area, sm);
259 sp_canvas_item_show(dc->area);
261 /* Get buffer */
262 const int x0 = (int) floor(dc->centre[NR::X] - rw);
263 const int y0 = (int) floor(dc->centre[NR::Y] - rw);
264 const int x1 = (int) ceil(dc->centre[NR::X] + rw);
265 const int y1 = (int) ceil(dc->centre[NR::Y] + rw);
267 if ((x1 > x0) && (y1 > y0)) {
268 NRPixBlock pb;
269 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
270 /* fixme: (Lauris) */
271 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(SP_DT_DRAWING(ec->desktop)), &pb);
272 for (int y = y0; y < y1; y++) {
273 const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
274 for (int x = x0; x < x1; x++) {
275 const double dx = x - dc->centre[NR::X];
276 const double dy = y - dc->centre[NR::Y];
277 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
278 W += w;
279 R += w * s[0];
280 G += w * s[1];
281 B += w * s[2];
282 A += w * s[3];
283 s += 4;
284 }
285 }
286 nr_pixblock_release(&pb);
288 R = (R + 0.001) / (255.0 * W);
289 G = (G + 0.001) / (255.0 * W);
290 B = (B + 0.001) / (255.0 * W);
291 A = (A + 0.001) / (255.0 * W);
293 R = CLAMP(R, 0.0, 1.0);
294 G = CLAMP(G, 0.0, 1.0);
295 B = CLAMP(B, 0.0, 1.0);
296 A = CLAMP(A, 0.0, 1.0);
297 }
299 } else {
300 // pick single pixel
301 NRPixBlock pb;
302 int x = (int) floor(event->button.x);
303 int y = (int) floor(event->button.y);
304 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
305 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(SP_DT_DRAWING(ec->desktop)), &pb);
306 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
308 R = s[0] / 255.0;
309 G = s[1] / 255.0;
310 B = s[2] / 255.0;
311 A = s[3] / 255.0;
312 }
314 if (pick == SP_DROPPER_PICK_VISIBLE) {
315 // compose with page color
316 guint32 bg = SP_DT_NAMEDVIEW(ec->desktop)->pagecolor;
317 R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
318 G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
319 B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
320 A = 1.0;
321 } else {
322 // un-premultiply color channels
323 if (A > 0) {
324 R /= A;
325 G /= A;
326 B /= A;
327 }
328 }
330 if (fabs(A) < 1e-4) {
331 A = 0; // suppress exponentials, CSS does not allow that
332 }
334 // remember color
335 dc->R = R;
336 dc->G = G;
337 dc->B = B;
338 dc->alpha = A;
340 // status message
341 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, A);
343 gchar c[64];
344 sp_svg_write_color(c, 64, c32);
346 // alpha of color under cursor, to show in the statusbar
347 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
348 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), A);
349 // where the color is picked, to show in the statusbar
350 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
351 // message, to show in the statusbar
352 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");
353 ec->defaultMessageContext()->setF(
354 Inkscape::NORMAL_MESSAGE,
355 "<b>%s%s</b>%s. %s", c,
356 (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
357 where, message
358 );
360 g_free(where);
361 g_free(alpha);
363 ret = TRUE;
364 }
365 break;
366 case GDK_BUTTON_RELEASE:
367 if (event->button.button == 1)
368 {
369 sp_canvas_item_hide(dc->area);
370 dc->dragging = FALSE;
372 // do the actual color setting
373 sp_desktop_set_color(ec->desktop,
374 (event->button.state & GDK_MOD1_MASK)?
375 ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, dc->alpha) : ColorRGBA(dc->R, dc->G, dc->B, dc->alpha),
376 false, !(event->button.state & GDK_SHIFT_MASK));
378 // REJON: set aux. toolbar input to hex color!
381 if (!(SP_DT_SELECTION(ec->desktop)->isEmpty())) {
382 sp_document_done(SP_DT_DOCUMENT(ec->desktop));
383 }
385 ret = TRUE;
386 }
387 break;
388 case GDK_KEY_PRESS:
389 switch (get_group0_keyval(&event->key)) {
390 case GDK_Up:
391 case GDK_Down:
392 case GDK_KP_Up:
393 case GDK_KP_Down:
394 // prevent the zoom field from activation
395 if (!MOD__CTRL_ONLY) {
396 ret = TRUE;
397 }
398 break;
399 case GDK_Escape:
400 SP_DT_SELECTION(ec->desktop)->clear();
401 default:
402 break;
403 }
404 break;
405 default:
406 break;
407 }
409 if (!ret) {
410 if (((SPEventContextClass *) parent_class)->root_handler) {
411 ret = ((SPEventContextClass *) parent_class)->root_handler(ec, event);
412 }
413 }
415 return ret;
416 }
419 /*
420 Local Variables:
421 mode:c++
422 c-file-style:"stroustrup"
423 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
424 indent-tabs-mode:nil
425 fill-column:99
426 End:
427 */
428 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :