Code

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