Code

- The snap-delay mechanism should now be more robust. From now on, it must be turned...
[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 "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     }
127 static void sp_dropper_context_finish(SPEventContext *ec)
129     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
131     ec->enableGrDrag(false);
133     if (dc->area) {
134         gtk_object_destroy(GTK_OBJECT(dc->area));
135         dc->area = NULL;
136     }
140 /**
141  * Returns the current dropper context color.
142  */
143 guint32 sp_dropper_context_get_color(SPEventContext *ec)
145     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
146     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
148     int pick = prefs->getInt("/tools/dropper/pick",
149                                        SP_DROPPER_PICK_VISIBLE);
150     bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
152     return SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B,
153         (pick == SP_DROPPER_PICK_ACTUAL && setalpha) ? dc->alpha : 1.0);
157 static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event)
159     SPDropperContext *dc = (SPDropperContext *) event_context;
160     int ret = FALSE;
161     SPDesktop *desktop = event_context->desktop;
162     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
164     int pick = prefs->getInt("/tools/dropper/pick", SP_DROPPER_PICK_VISIBLE);
165     bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
167     switch (event->type) {
168         case GDK_BUTTON_PRESS:
169             if (event->button.button == 1 && !event_context->space_panning) {
170                 dc->centre = Geom::Point(event->button.x, event->button.y);
171                 dc->dragging = TRUE;
172                 ret = TRUE;
173             }
174             break;
175         case GDK_MOTION_NOTIFY:
176             if (event->motion.state & GDK_BUTTON2_MASK) {
177                 // pass on middle-drag
178                 ret = FALSE;
179                 break;
180             } else if (!event_context->space_panning) {
181                 // otherwise, constantly calculate color no matter is any button pressed or not
183                 double rw = 0.0;
184                 double W(0), R(0), G(0), B(0), A(0);
186                 if (dc->dragging) {
187                     // calculate average
189                     // radius
190                     rw = std::min(Geom::L2(Geom::Point(event->button.x, event->button.y) - dc->centre), 400.0);
192                     if (rw == 0) { // happens sometimes, little idea why...
193                         break;
194                     }
196                     Geom::Point const cd = desktop->w2d(dc->centre);
197                     Geom::Matrix const w2dt = desktop->w2d();
198                     const double scale = rw * w2dt.descrim();
199                     Geom::Matrix const sm( Geom::Scale(scale, scale) * Geom::Translate(cd) );
200                     sp_canvas_item_affine_absolute(dc->area, sm);
201                     sp_canvas_item_show(dc->area);
203                     /* Get buffer */
204                     const int x0 = (int) floor(dc->centre[Geom::X] - rw);
205                     const int y0 = (int) floor(dc->centre[Geom::Y] - rw);
206                     const int x1 = (int) ceil(dc->centre[Geom::X] + rw);
207                     const int y1 = (int) ceil(dc->centre[Geom::Y] + rw);
209                     if ((x1 > x0) && (y1 > y0)) {
210                         NRPixBlock pb;
211                         nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
212                         /* fixme: (Lauris) */
213                         sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
214                         for (int y = y0; y < y1; y++) {
215                             const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
216                             for (int x = x0; x < x1; x++) {
217                                 const double dx = x - dc->centre[Geom::X];
218                                 const double dy = y - dc->centre[Geom::Y];
219                                 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
220                                 W += w;
221                                 R += w * s[0];
222                                 G += w * s[1];
223                                 B += w * s[2];
224                                 A += w * s[3];
225                                 s += 4;
226                             }
227                         }
228                         nr_pixblock_release(&pb);
230                         R = (R + 0.001) / (255.0 * W);
231                         G = (G + 0.001) / (255.0 * W);
232                         B = (B + 0.001) / (255.0 * W);
233                         A = (A + 0.001) / (255.0 * W);
235                         R = CLAMP(R, 0.0, 1.0);
236                         G = CLAMP(G, 0.0, 1.0);
237                         B = CLAMP(B, 0.0, 1.0);
238                         A = CLAMP(A, 0.0, 1.0);
239                     }
241                 } else {
242                     // pick single pixel
243                     NRPixBlock pb;
244                     int x = (int) floor(event->button.x);
245                     int y = (int) floor(event->button.y);
246                     nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
247                     sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
248                     const unsigned char *s = NR_PIXBLOCK_PX(&pb);
250                     R = s[0] / 255.0;
251                     G = s[1] / 255.0;
252                     B = s[2] / 255.0;
253                     A = s[3] / 255.0;
254                 }
256                 if (pick == SP_DROPPER_PICK_VISIBLE) {
257                     // compose with page color
258                     guint32 bg = sp_desktop_namedview(desktop)->pagecolor;
259                     R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
260                     G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
261                     B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
262                     A = 1.0;
263                 } else {
264                     // un-premultiply color channels
265                     if (A > 0) {
266                         R /= A;
267                         G /= A;
268                         B /= A;
269                     }
270                 }
272                 if (fabs(A) < 1e-4) {
273                     A = 0; // suppress exponentials, CSS does not allow that
274                 }
276                 // remember color
277                 dc->R = R;
278                 dc->G = G;
279                 dc->B = B;
280                 dc->alpha = A;
282                 // status message
283                 double alpha_to_set = setalpha? dc->alpha : 1.0;
284                 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
286                 gchar c[64];
287                 sp_svg_write_color(c, sizeof(c), c32);
289                 // alpha of color under cursor, to show in the statusbar
290                 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
291                 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
292                 // where the color is picked, to show in the statusbar
293                 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
294                 // message, to show in the statusbar
295                 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");
296                 event_context->defaultMessageContext()->setF(
297                     Inkscape::NORMAL_MESSAGE,
298                     "<b>%s%s</b>%s. %s", c,
299                     (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
300                     where, message
301                     );
303                 g_free(where);
304                 g_free(alpha);
306                 ret = TRUE;
307             }
308             break;
309         case GDK_BUTTON_RELEASE:
310             if (event->button.button == 1 && !event_context->space_panning)
311             {
312                 sp_canvas_item_hide(dc->area);
313                 dc->dragging = FALSE;
315                 double alpha_to_set = setalpha? dc->alpha : 1.0;
317                 // do the actual color setting
318                 sp_desktop_set_color(desktop,
319                                      (event->button.state & GDK_MOD1_MASK)?
320                                      ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
321                                      false,  !(event->button.state & GDK_SHIFT_MASK));
323                 // REJON: set aux. toolbar input to hex color!
326                 if (!(sp_desktop_selection(desktop)->isEmpty())) {
327                     sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER,
328                                      _("Set picked color"));
329                 }
331                 ret = TRUE;
332             }
333             break;
334         case GDK_KEY_PRESS:
335             switch (get_group0_keyval(&event->key)) {
336                 case GDK_Up:
337                 case GDK_Down:
338                 case GDK_KP_Up:
339                 case GDK_KP_Down:
340                     // prevent the zoom field from activation
341                     if (!MOD__CTRL_ONLY) {
342                         ret = TRUE;
343                     }
344                     break;
345                 case GDK_Escape:
346                     sp_desktop_selection(desktop)->clear();
347                 default:
348                     break;
349             }
350             break;
351         default:
352             break;
353     }
355     if (!ret) {
356         if (((SPEventContextClass *) parent_class)->root_handler) {
357             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
358         }
359     }
361     return ret;
365 /*
366   Local Variables:
367   mode:c++
368   c-file-style:"stroustrup"
369   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
370   indent-tabs-mode:nil
371   fill-column:99
372   End:
373 */
374 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :