Code

Pot and Dutch translation update
[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);
132         
133     if (dc->grabbed) {
134         sp_canvas_item_ungrab(dc->grabbed, GDK_CURRENT_TIME);
135         dc->grabbed = NULL;
136     }
138     if (dc->area) {
139         gtk_object_destroy(GTK_OBJECT(dc->area));
140         dc->area = NULL;
141     }
145 /**
146  * Returns the current dropper context icc-color.
147  */
148 SPColor* sp_dropper_context_get_icc_color(SPEventContext */*ec*/)
150     //TODO: implement-me!
152     return 0; // At least we will cause a clean crash, instead of random corruption.
155 /**
156  * Returns the current dropper context color.
157  */
158 guint32 sp_dropper_context_get_color(SPEventContext *ec)
160     SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
161     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
163     int pick = prefs->getInt("/tools/dropper/pick",
164                                        SP_DROPPER_PICK_VISIBLE);
165     bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
167     return SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B,
168         (pick == SP_DROPPER_PICK_ACTUAL && setalpha) ? dc->alpha : 1.0);
172 static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event)
174     SPDropperContext *dc = (SPDropperContext *) event_context;
175     int ret = FALSE;
176     SPDesktop *desktop = event_context->desktop;
177     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
179     int pick = prefs->getInt("/tools/dropper/pick", SP_DROPPER_PICK_VISIBLE);
180     bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
182     switch (event->type) {
183         case GDK_BUTTON_PRESS:
184             if (event->button.button == 1 && !event_context->space_panning) {
185                 dc->centre = Geom::Point(event->button.x, event->button.y);
186                 dc->dragging = TRUE;
187                 ret = TRUE;
188             }
189                         
190                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
191                                                                 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
192                                                                 NULL, event->button.time);
193                         dc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
194                         
195             break;
196         case GDK_MOTION_NOTIFY:
197             if (event->motion.state & GDK_BUTTON2_MASK) {
198                 // pass on middle-drag
199                 ret = FALSE;
200                 break;
201             } else if (!event_context->space_panning) {
202                 // otherwise, constantly calculate color no matter is any button pressed or not
204                 double rw = 0.0;
205                 double W(0), R(0), G(0), B(0), A(0);
207                 if (dc->dragging) {
208                     // calculate average
210                     // radius
211                     rw = std::min(Geom::L2(Geom::Point(event->button.x, event->button.y) - dc->centre), 400.0);
213                     if (rw == 0) { // happens sometimes, little idea why...
214                         break;
215                     }
217                     Geom::Point const cd = desktop->w2d(dc->centre);
218                     Geom::Matrix const w2dt = desktop->w2d();
219                     const double scale = rw * w2dt.descrim();
220                     Geom::Matrix const sm( Geom::Scale(scale, scale) * Geom::Translate(cd) );
221                     sp_canvas_item_affine_absolute(dc->area, sm);
222                     sp_canvas_item_show(dc->area);
224                     /* Get buffer */
225                     const int x0 = (int) floor(dc->centre[Geom::X] - rw);
226                     const int y0 = (int) floor(dc->centre[Geom::Y] - rw);
227                     const int x1 = (int) ceil(dc->centre[Geom::X] + rw);
228                     const int y1 = (int) ceil(dc->centre[Geom::Y] + rw);
230                     if ((x1 > x0) && (y1 > y0)) {
231                         NRPixBlock pb;
232                         nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
233                         /* fixme: (Lauris) */
234                         sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
235                         for (int y = y0; y < y1; y++) {
236                             const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
237                             for (int x = x0; x < x1; x++) {
238                                 const double dx = x - dc->centre[Geom::X];
239                                 const double dy = y - dc->centre[Geom::Y];
240                                 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
241                                 W += w;
242                                 R += w * s[0];
243                                 G += w * s[1];
244                                 B += w * s[2];
245                                 A += w * s[3];
246                                 s += 4;
247                             }
248                         }
249                         nr_pixblock_release(&pb);
251                         R = (R + 0.001) / (255.0 * W);
252                         G = (G + 0.001) / (255.0 * W);
253                         B = (B + 0.001) / (255.0 * W);
254                         A = (A + 0.001) / (255.0 * W);
256                         R = CLAMP(R, 0.0, 1.0);
257                         G = CLAMP(G, 0.0, 1.0);
258                         B = CLAMP(B, 0.0, 1.0);
259                         A = CLAMP(A, 0.0, 1.0);
260                     }
262                 } else {
263                     // pick single pixel
264                     NRPixBlock pb;
265                     int x = (int) floor(event->button.x);
266                     int y = (int) floor(event->button.y);
267                     nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
268                     sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
269                     const unsigned char *s = NR_PIXBLOCK_PX(&pb);
271                     R = s[0] / 255.0;
272                     G = s[1] / 255.0;
273                     B = s[2] / 255.0;
274                     A = s[3] / 255.0;
275                 }
277                 if (pick == SP_DROPPER_PICK_VISIBLE) {
278                     // compose with page color
279                     guint32 bg = sp_desktop_namedview(desktop)->pagecolor;
280                     R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
281                     G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
282                     B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
283                     A = 1.0;
284                 } else {
285                     // un-premultiply color channels
286                     if (A > 0) {
287                         R /= A;
288                         G /= A;
289                         B /= A;
290                     }
291                 }
293                 if (fabs(A) < 1e-4) {
294                     A = 0; // suppress exponentials, CSS does not allow that
295                 }
297                 // remember color
298                 dc->R = R;
299                 dc->G = G;
300                 dc->B = B;
301                 dc->alpha = A;
303                 // status message
304                 double alpha_to_set = setalpha? dc->alpha : 1.0;
305                 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
307                 gchar c[64];
308                 sp_svg_write_color(c, sizeof(c), c32);
310                 // alpha of color under cursor, to show in the statusbar
311                 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
312                 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
313                 // where the color is picked, to show in the statusbar
314                 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
315                 // message, to show in the statusbar
316                 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");
317                 event_context->defaultMessageContext()->setF(
318                     Inkscape::NORMAL_MESSAGE,
319                     "<b>%s%s</b>%s. %s", c,
320                     (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
321                     where, message
322                     );
324                 g_free(where);
325                 g_free(alpha);
327                 ret = TRUE;
328             }
329             break;
330         case GDK_BUTTON_RELEASE:
331             if (event->button.button == 1 && !event_context->space_panning)
332             {
333                 sp_canvas_item_hide(dc->area);
334                 dc->dragging = FALSE;
335                                 
336                                 if (dc->grabbed) {
337                                         sp_canvas_item_ungrab(dc->grabbed, event->button.time);
338                                         dc->grabbed = NULL;
339                                 }
341                 double alpha_to_set = setalpha? dc->alpha : 1.0;
343                 // do the actual color setting
344                 sp_desktop_set_color(desktop,
345                                      (event->button.state & GDK_MOD1_MASK)?
346                                      ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
347                                      false,  !(event->button.state & GDK_SHIFT_MASK));
349                 // REJON: set aux. toolbar input to hex color!
352                 if (!(sp_desktop_selection(desktop)->isEmpty())) {
353                     sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER,
354                                      _("Set picked color"));
355                 }
357                 ret = TRUE;
358             }
359             break;
360         case GDK_KEY_PRESS:
361             switch (get_group0_keyval(&event->key)) {
362                 case GDK_Up:
363                 case GDK_Down:
364                 case GDK_KP_Up:
365                 case GDK_KP_Down:
366                     // prevent the zoom field from activation
367                     if (!MOD__CTRL_ONLY) {
368                         ret = TRUE;
369                     }
370                     break;
371                 case GDK_Escape:
372                     sp_desktop_selection(desktop)->clear();
373                 default:
374                     break;
375             }
376             break;
377         default:
378             break;
379     }
381     if (!ret) {
382         if (((SPEventContextClass *) parent_class)->root_handler) {
383             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
384         }
385     }
387     return ret;
391 /*
392   Local Variables:
393   mode:c++
394   c-file-style:"stroustrup"
395   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
396   indent-tabs-mode:nil
397   fill-column:99
398   End:
399 */
400 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :