Code

struct SPCurve => class SPCurve
[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"
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;
106 static void sp_dropper_context_setup(SPEventContext *ec)
108     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
110     if (((SPEventContextClass *) parent_class)->setup) {
111         ((SPEventContextClass *) parent_class)->setup(ec);
112     }
114     SPCurve *c = SPCurve::new_from_foreign_bpath(spdc_circle);
115     dc->area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
116     c->unref();
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     }
130 static void sp_dropper_context_finish(SPEventContext *ec)
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     }
143 /**
144  * Returns the current dropper context color.
145  */
146 guint32 sp_dropper_context_get_color(SPEventContext *ec)
148     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
149     
150     int pick = prefs_get_int_attribute("tools.dropper", "pick",
151                                        SP_DROPPER_PICK_VISIBLE);
152     int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
153     
154     return SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B,
155         (pick == SP_DROPPER_PICK_ACTUAL && setalpha) ? dc->alpha : 1.0);
159 static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event)
161     SPDropperContext *dc = (SPDropperContext *) event_context;
162     int ret = FALSE;
163     SPDesktop *desktop = event_context->desktop;
165     int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE);
166     int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1);
168     switch (event->type) {
169         case GDK_BUTTON_PRESS:
170             if (event->button.button == 1 && !event_context->space_panning) {
171                 dc->centre = NR::Point(event->button.x, event->button.y);
172                 dc->dragging = TRUE;
173                 ret = TRUE;
174             }
175             break;
176         case GDK_MOTION_NOTIFY:
177             if (event->motion.state & GDK_BUTTON2_MASK) {
178                 // pass on middle-drag
179                 ret = FALSE;
180                 break;
181             } else if (!event_context->space_panning) {
182                 // otherwise, constantly calculate color no matter is any button pressed or not
184                 double rw = 0.0;
185                 double W(0), R(0), G(0), B(0), A(0);
187                 if (dc->dragging) {
188                     // calculate average
190                     // radius
191                     rw = std::min(NR::L2(NR::Point(event->button.x, event->button.y) - dc->centre), 400.0);
193                     if (rw == 0) { // happens sometimes, little idea why...
194                         break;
195                     }
197                     NR::Point const cd = desktop->w2d(dc->centre);
198                     NR::Matrix const w2dt = desktop->w2d();
199                     const double scale = rw * NR::expansion(w2dt);
200                     NR::Matrix const sm( NR::scale(scale, scale) * NR::translate(cd) );
201                     sp_canvas_item_affine_absolute(dc->area, sm);
202                     sp_canvas_item_show(dc->area);
204                     /* Get buffer */
205                     const int x0 = (int) floor(dc->centre[NR::X] - rw);
206                     const int y0 = (int) floor(dc->centre[NR::Y] - rw);
207                     const int x1 = (int) ceil(dc->centre[NR::X] + rw);
208                     const int y1 = (int) ceil(dc->centre[NR::Y] + rw);
210                     if ((x1 > x0) && (y1 > y0)) {
211                         NRPixBlock pb;
212                         nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
213                         /* fixme: (Lauris) */
214                         sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
215                         for (int y = y0; y < y1; y++) {
216                             const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
217                             for (int x = x0; x < x1; x++) {
218                                 const double dx = x - dc->centre[NR::X];
219                                 const double dy = y - dc->centre[NR::Y];
220                                 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
221                                 W += w;
222                                 R += w * s[0];
223                                 G += w * s[1];
224                                 B += w * s[2];
225                                 A += w * s[3];
226                                 s += 4;
227                             }
228                         }
229                         nr_pixblock_release(&pb);
231                         R = (R + 0.001) / (255.0 * W);
232                         G = (G + 0.001) / (255.0 * W);
233                         B = (B + 0.001) / (255.0 * W);
234                         A = (A + 0.001) / (255.0 * W);
236                         R = CLAMP(R, 0.0, 1.0);
237                         G = CLAMP(G, 0.0, 1.0);
238                         B = CLAMP(B, 0.0, 1.0);
239                         A = CLAMP(A, 0.0, 1.0);
240                     }
242                 } else {
243                     // pick single pixel
244                     NRPixBlock pb;
245                     int x = (int) floor(event->button.x);
246                     int y = (int) floor(event->button.y);
247                     nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
248                     sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
249                     const unsigned char *s = NR_PIXBLOCK_PX(&pb);
251                     R = s[0] / 255.0;
252                     G = s[1] / 255.0;
253                     B = s[2] / 255.0;
254                     A = s[3] / 255.0;
255                 }
257                 if (pick == SP_DROPPER_PICK_VISIBLE) {
258                     // compose with page color
259                     guint32 bg = sp_desktop_namedview(desktop)->pagecolor;
260                     R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
261                     G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
262                     B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
263                     A = 1.0;
264                 } else {
265                     // un-premultiply color channels
266                     if (A > 0) {
267                         R /= A;
268                         G /= A;
269                         B /= A;
270                     }
271                 }
273                 if (fabs(A) < 1e-4) {
274                     A = 0; // suppress exponentials, CSS does not allow that
275                 }
277                 // remember color
278                 dc->R = R;
279                 dc->G = G;
280                 dc->B = B;
281                 dc->alpha = A;
283                 // status message
284                 double alpha_to_set = setalpha? dc->alpha : 1.0;
285                 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
287                 gchar c[64];
288                 sp_svg_write_color(c, sizeof(c), c32);
290                 // alpha of color under cursor, to show in the statusbar
291                 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
292                 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
293                 // where the color is picked, to show in the statusbar
294                 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
295                 // message, to show in the statusbar
296                 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");
297                 event_context->defaultMessageContext()->setF(
298                     Inkscape::NORMAL_MESSAGE,
299                     "<b>%s%s</b>%s. %s", c,
300                     (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
301                     where, message
302                     );
304                 g_free(where);
305                 g_free(alpha);
307                 ret = TRUE;
308             }
309             break;
310         case GDK_BUTTON_RELEASE:
311             if (event->button.button == 1 && !event_context->space_panning)
312             {
313                 sp_canvas_item_hide(dc->area);
314                 dc->dragging = FALSE;
316                 double alpha_to_set = setalpha? dc->alpha : 1.0;
318                 // do the actual color setting
319                 sp_desktop_set_color(desktop,
320                                      (event->button.state & GDK_MOD1_MASK)?
321                                      ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
322                                      false,  !(event->button.state & GDK_SHIFT_MASK));
324                 // REJON: set aux. toolbar input to hex color!
327                 if (!(sp_desktop_selection(desktop)->isEmpty())) {
328                     sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER, 
329                                      _("Set picked color"));
330                 }
332                 ret = TRUE;
333             }
334             break;
335         case GDK_KEY_PRESS:
336             switch (get_group0_keyval(&event->key)) {
337                 case GDK_Up:
338                 case GDK_Down:
339                 case GDK_KP_Up:
340                 case GDK_KP_Down:
341                     // prevent the zoom field from activation
342                     if (!MOD__CTRL_ONLY) {
343                         ret = TRUE;
344                     }
345                     break;
346                 case GDK_Escape:
347                     sp_desktop_selection(desktop)->clear();
348                 default:
349                     break;
350             }
351             break;
352         default:
353             break;
354     }
356     if (!ret) {
357         if (((SPEventContextClass *) parent_class)->root_handler) {
358             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
359         }
360     }
362     return ret;
366 /*
367   Local Variables:
368   mode:c++
369   c-file-style:"stroustrup"
370   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
371   indent-tabs-mode:nil
372   fill-column:99
373   End:
374 */
375 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :