Code

undo annotations
[inkscape.git] / src / dropper-context.cpp
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"
42 #include "pixmaps/cursor-dropper.pixbuf"
44 #include "dropper-context.h"
45 #include "message-context.h"
46 #include "libnr/nr-scale-translate-ops.h"
48 #define C1 0.552
49 static NArtBpath const spdc_circle[] = {
50     { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
51     { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
52     { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
53     { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
54     { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
55     { NR_END, 0, 0, 0, 0, 0, 0 }
56 };
57 #undef C1
59 static void sp_dropper_context_class_init(SPDropperContextClass *klass);
60 static void sp_dropper_context_init(SPDropperContext *dc);
62 static void sp_dropper_context_setup(SPEventContext *ec);
63 static void sp_dropper_context_finish(SPEventContext *ec);
65 static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent * event);
67 static SPEventContextClass *parent_class;
69 GType sp_dropper_context_get_type()
70 {
71     static GType type = 0;
72     if (!type) {
73         GTypeInfo info = {
74             sizeof(SPDropperContextClass),
75             NULL, NULL,
76             (GClassInitFunc) sp_dropper_context_class_init,
77             NULL, NULL,
78             sizeof(SPDropperContext),
79             4,
80             (GInstanceInitFunc) sp_dropper_context_init,
81             NULL,       /* value_table */
82         };
83         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDropperContext", &info, (GTypeFlags) 0);
84     }
85     return type;
86 }
88 static void sp_dropper_context_class_init(SPDropperContextClass *klass)
89 {
90     SPEventContextClass *ec_class = (SPEventContextClass *) klass;
92     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
94     ec_class->setup = sp_dropper_context_setup;
95     ec_class->finish = sp_dropper_context_finish;
96     ec_class->root_handler = sp_dropper_context_root_handler;
97 }
99 static void sp_dropper_context_init(SPDropperContext *dc)
101     SPEventContext *event_context = SP_EVENT_CONTEXT(dc);
102     event_context->cursor_shape = cursor_dropper_xpm;
103     event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
104             -1,
105             cursor_dropper_pixbuf,
106             FALSE,
107             NULL);  
108     event_context->hot_x = 7;
109     event_context->hot_y = 7;
112 static void sp_dropper_context_setup(SPEventContext *ec)
114     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
116     if (((SPEventContextClass *) parent_class)->setup) {
117         ((SPEventContextClass *) parent_class)->setup(ec);
118     }
120     SPCurve *c = sp_curve_new_from_foreign_bpath(spdc_circle);
121     dc->area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
122     sp_curve_unref(c);
123     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(dc->area), 0x00000000,(SPWindRule)0);
124     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
125     sp_canvas_item_hide(dc->area);
127     if (prefs_get_int_attribute("tools.dropper", "selcue", 0) != 0) {
128         ec->enableSelectionCue();
129     }
131     if (prefs_get_int_attribute("tools.dropper", "gradientdrag", 0) != 0) {
132         ec->enableGrDrag();
133     }
136 static void sp_dropper_context_finish(SPEventContext *ec)
138     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
140     ec->enableGrDrag(false);
142     if (dc->area) {
143         gtk_object_destroy(GTK_OBJECT(dc->area));
144         dc->area = NULL;
145     }
149 /**
150  * Copies the current context color to the clipboard.
151  */
152 void sp_dropper_context_copy(SPEventContext *ec)
154     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
156     guint32 const c32 = SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B, dc->alpha);
158     int pick = prefs_get_int_attribute("tools.dropper", "pick",
159                                        SP_DROPPER_PICK_VISIBLE);
161     int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
163     gchar c[64];
164     g_snprintf(c, 64, "%06x%02x", c32 >> 8,
165                (pick == SP_DROPPER_PICK_ACTUAL && setalpha)? SP_COLOR_F_TO_U(dc->alpha) : 255);
167     Glib::ustring text;
168     text += c;
169     if (!text.empty())
170     {
171         Glib::RefPtr<Gtk::Clipboard> refClipboard =
172             Gtk::Clipboard::get();
173         refClipboard->set_text(text);
174     }
177 /**
178  * Makes a copy of the current desktop color to the clipboard.
179  */
180 void sp_dropper_c32_color_copy(guint32 c32)
182     int const pick = prefs_get_int_attribute("tools.dropper", "pick",
183                                              SP_DROPPER_PICK_VISIBLE);
185     gchar c[64];
186     g_snprintf(c, 64, "%06x%02x", c32 >> 8,
187         pick == SP_DROPPER_PICK_ACTUAL? SP_RGBA32_A_U(c32) : 255);
189     Glib::ustring text;
190     text += c;
191     if (!text.empty()) {
192             Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
193             refClipboard->set_text(text);
194     }
198 /**
199  * Makes a copy of the current color as a hex value. This should always compute
200  * the current color without alpha, but the on-screen representation.
201  */
202 void sp_dropper_c32_color_copy_hex(guint32 c32)
204     /*
205     int pick = prefs_get_int_attribute ("tools.dropper", "pick",
206                                     SP_DROPPER_PICK_VISIBLE);
208     if ( pick == SP_DROPPER_PICK_ACTUAL )
209         ; // process c32 so that it computes against page
210     // else just can cut off that last 2 hex digits....
212     */
214     gchar c[48];
215     g_snprintf(c, 48, "%06x", c32 >> 8);
217     Glib::ustring text;
218     text += c;
219     if (!text.empty()) {
220             Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
221             refClipboard->set_text(text);
222     }
226 static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent *event)
228     SPDropperContext *dc = (SPDropperContext *) ec;
229     int ret = FALSE;
231     int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE);
232     int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
234     switch (event->type) {
235         case GDK_BUTTON_PRESS:
236             if (event->button.button == 1) {
237                 dc->centre = NR::Point(event->button.x, event->button.y);
238                 dc->dragging = TRUE;
239                 ret = TRUE;
240             }
241             break;
242         case GDK_MOTION_NOTIFY:
243             if (event->motion.state & GDK_BUTTON2_MASK) {
244                 // pass on middle-drag
245                 ret = FALSE;
246                 break;
247             } else {
248                 // otherwise, constantly calculate color no matter is any button pressed or not
250                 double rw = 0.0;
251                 double W(0), R(0), G(0), B(0), A(0);
253                 if (dc->dragging) {
254                     // calculate average
256                     // radius
257                     rw = std::min(NR::L2(NR::Point(event->button.x, event->button.y) - dc->centre), 400.0);
259                     if (rw == 0) { // happens sometimes, little idea why...
260                         break;
261                     }
263                     NR::Point const cd = ec->desktop->w2d(dc->centre);
264                     NR::Matrix const w2dt = ec->desktop->w2d();
265                     const double scale = rw * NR_MATRIX_DF_EXPANSION(&w2dt);
266                     NR::Matrix const sm( NR::scale(scale, scale) * NR::translate(cd) );
267                     sp_canvas_item_affine_absolute(dc->area, sm);
268                     sp_canvas_item_show(dc->area);
270                     /* Get buffer */
271                     const int x0 = (int) floor(dc->centre[NR::X] - rw);
272                     const int y0 = (int) floor(dc->centre[NR::Y] - rw);
273                     const int x1 = (int) ceil(dc->centre[NR::X] + rw);
274                     const int y1 = (int) ceil(dc->centre[NR::Y] + rw);
276                     if ((x1 > x0) && (y1 > y0)) {
277                         NRPixBlock pb;
278                         nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
279                         /* fixme: (Lauris) */
280                         sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(ec->desktop)), &pb);
281                         for (int y = y0; y < y1; y++) {
282                             const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
283                             for (int x = x0; x < x1; x++) {
284                                 const double dx = x - dc->centre[NR::X];
285                                 const double dy = y - dc->centre[NR::Y];
286                                 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
287                                 W += w;
288                                 R += w * s[0];
289                                 G += w * s[1];
290                                 B += w * s[2];
291                                 A += w * s[3];
292                                 s += 4;
293                             }
294                         }
295                         nr_pixblock_release(&pb);
297                         R = (R + 0.001) / (255.0 * W);
298                         G = (G + 0.001) / (255.0 * W);
299                         B = (B + 0.001) / (255.0 * W);
300                         A = (A + 0.001) / (255.0 * W);
302                         R = CLAMP(R, 0.0, 1.0);
303                         G = CLAMP(G, 0.0, 1.0);
304                         B = CLAMP(B, 0.0, 1.0);
305                         A = CLAMP(A, 0.0, 1.0);
306                     }
308                 } else {
309                     // pick single pixel
310                     NRPixBlock pb;
311                     int x = (int) floor(event->button.x);
312                     int y = (int) floor(event->button.y);
313                     nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
314                     sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(ec->desktop)), &pb);
315                     const unsigned char *s = NR_PIXBLOCK_PX(&pb);
317                     R = s[0] / 255.0;
318                     G = s[1] / 255.0;
319                     B = s[2] / 255.0;
320                     A = s[3] / 255.0;
321                 }
323                 if (pick == SP_DROPPER_PICK_VISIBLE) {
324                     // compose with page color
325                     guint32 bg = sp_desktop_namedview(ec->desktop)->pagecolor;
326                     R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
327                     G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
328                     B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
329                     A = 1.0;
330                 } else {
331                     // un-premultiply color channels
332                     if (A > 0) {
333                         R /= A;
334                         G /= A;
335                         B /= A;
336                     }
337                 }
339                 if (fabs(A) < 1e-4) {
340                     A = 0; // suppress exponentials, CSS does not allow that
341                 }
343                 // remember color
344                 dc->R = R;
345                 dc->G = G;
346                 dc->B = B;
347                 dc->alpha = A;
349                 // status message
350                 double alpha_to_set = setalpha? dc->alpha : 1.0;
351                 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
353                 gchar c[64];
354                 sp_svg_write_color(c, 64, c32);
356                 // alpha of color under cursor, to show in the statusbar
357                 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
358                 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
359                 // where the color is picked, to show in the statusbar
360                 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
361                 // message, to show in the statusbar
362                 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");
363                 ec->defaultMessageContext()->setF(
364                     Inkscape::NORMAL_MESSAGE,
365                     "<b>%s%s</b>%s. %s", c,
366                     (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
367                     where, message
368                     );
370                 g_free(where);
371                 g_free(alpha);
373                 ret = TRUE;
374             }
375             break;
376         case GDK_BUTTON_RELEASE:
377             if (event->button.button == 1)
378             {
379                 sp_canvas_item_hide(dc->area);
380                 dc->dragging = FALSE;
382                 double alpha_to_set = setalpha? dc->alpha : 1.0;
384                 // do the actual color setting
385                 sp_desktop_set_color(ec->desktop,
386                                      (event->button.state & GDK_MOD1_MASK)?
387                                      ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
388                                      false,  !(event->button.state & GDK_SHIFT_MASK));
390                 // REJON: set aux. toolbar input to hex color!
393                 if (!(sp_desktop_selection(ec->desktop)->isEmpty())) {
394                     sp_document_done(sp_desktop_document(ec->desktop), SP_VERB_CONTEXT_DROPPER, 
395                                      _("Set picked color"));
396                 }
398                 ret = TRUE;
399             }
400             break;
401         case GDK_KEY_PRESS:
402             switch (get_group0_keyval(&event->key)) {
403                 case GDK_Up:
404                 case GDK_Down:
405                 case GDK_KP_Up:
406                 case GDK_KP_Down:
407                     // prevent the zoom field from activation
408                     if (!MOD__CTRL_ONLY) {
409                         ret = TRUE;
410                     }
411                     break;
412                 case GDK_Escape:
413                     sp_desktop_selection(ec->desktop)->clear();
414                 default:
415                     break;
416             }
417             break;
418         default:
419             break;
420     }
422     if (!ret) {
423         if (((SPEventContextClass *) parent_class)->root_handler) {
424             ret = ((SPEventContextClass *) parent_class)->root_handler(ec, event);
425         }
426     }
428     return ret;
432 /*
433   Local Variables:
434   mode:c++
435   c-file-style:"stroustrup"
436   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
437   indent-tabs-mode:nil
438   fill-column:99
439   End:
440 */
441 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :