Code

initial color cursor implementation (reads from pixbufs, will work on reading from...
[inkscape.git] / src / pencil-context.cpp
1 /** \file
2  * Pencil event context implementation.
3  */
5 /*
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2000 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2002 Lauris Kaplinski
13  * Copyright (C) 2004 Monash University
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <gdk/gdkkeysyms.h>
20 #include "pencil-context.h"
21 #include "desktop.h"
22 #include "desktop-handles.h"
23 #include "selection.h"
24 #include "draw-anchor.h"
25 #include "message-stack.h"
26 #include "message-context.h"
27 #include "modifier-fns.h"
28 #include "sp-path.h"
29 #include "prefs-utils.h"
30 #include "snap.h"
31 #include "pixmaps/cursor-pencil.xpm"
32 #include "pixmaps/cursor-pencil.pixbuf"
33 #include "display/bezier-utils.h"
34 #include "display/canvas-bpath.h"
35 #include <glibmm/i18n.h>
36 #include "libnr/in-svg-plane.h"
37 #include "libnr/n-art-bpath.h"
38 #include "context-fns.h"
39 #include "sp-namedview.h"
41 static void sp_pencil_context_class_init(SPPencilContextClass *klass);
42 static void sp_pencil_context_init(SPPencilContext *pc);
43 static void sp_pencil_context_setup(SPEventContext *ec);
44 static void sp_pencil_context_dispose(GObject *object);
46 static gint sp_pencil_context_root_handler(SPEventContext *event_context, GdkEvent *event);
47 static gint pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent);
48 static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent);
49 static gint pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent);
50 static gint pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state);
52 static void spdc_set_startpoint(SPPencilContext *pc, NR::Point const p);
53 static void spdc_set_endpoint(SPPencilContext *pc, NR::Point const p);
54 static void spdc_finish_endpoint(SPPencilContext *pc);
55 static void spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state);
56 static void fit_and_split(SPPencilContext *pc);
59 static SPDrawContextClass *pencil_parent_class;
61 /**
62  * Register SPPencilContext class with Gdk and return its type number.
63  */
64 GType
65 sp_pencil_context_get_type()
66 {
67     static GType type = 0;
68     if (!type) {
69         GTypeInfo info = {
70             sizeof(SPPencilContextClass),
71             NULL, NULL,
72             (GClassInitFunc) sp_pencil_context_class_init,
73             NULL, NULL,
74             sizeof(SPPencilContext),
75             4,
76             (GInstanceInitFunc) sp_pencil_context_init,
77             NULL,   /* value_table */
78         };
79         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPencilContext", &info, (GTypeFlags)0);
80     }
81     return type;
82 }
84 /**
85  * Initialize SPPencilContext vtable.
86  */
87 static void
88 sp_pencil_context_class_init(SPPencilContextClass *klass)
89 {
90     GObjectClass *object_class;
91     SPEventContextClass *event_context_class;
93     object_class = (GObjectClass *) klass;
94     event_context_class = (SPEventContextClass *) klass;
96     pencil_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
98     object_class->dispose = sp_pencil_context_dispose;
100     event_context_class->setup = sp_pencil_context_setup;
101     event_context_class->root_handler = sp_pencil_context_root_handler;
104 /**
105  * Callback to initialize SPPencilContext object.
106  */
107 static void
108 sp_pencil_context_init(SPPencilContext *pc)
110     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
112     event_context->cursor_shape = cursor_pencil_xpm;
113     event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
114             -1,
115             cursor_pencil_pixbuf,
116             FALSE,
117             NULL);  
118     event_context->hot_x = 4;
119     event_context->hot_y = 4;
121     pc->npoints = 0;
122     pc->state = SP_PENCIL_CONTEXT_IDLE;
123     pc->req_tangent = NR::Point(0, 0);
126 /**
127  * Callback to setup SPPencilContext object.
128  */
129 static void
130 sp_pencil_context_setup(SPEventContext *ec)
132     if (prefs_get_int_attribute("tools.freehand.pencil", "selcue", 0) != 0) {
133         ec->enableSelectionCue();
134     }
136     if (((SPEventContextClass *) pencil_parent_class)->setup) {
137         ((SPEventContextClass *) pencil_parent_class)->setup(ec);
138     }
140     SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
141     pc->is_drawing = false;
143     pc->anchor_statusbar = false;
146 static void
147 sp_pencil_context_dispose(GObject *object)
149     G_OBJECT_CLASS(pencil_parent_class)->dispose(object);
152 /** Snaps new node relative to the previous node. */
153 static void
154 spdc_endpoint_snap(SPPencilContext const *pc, NR::Point &p, guint const state)
156     spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
157     spdc_endpoint_snap_free(pc, p, state);
160 /**
161  * Callback for handling all pencil context events.
162  */
163 gint
164 sp_pencil_context_root_handler(SPEventContext *const ec, GdkEvent *event)
166     SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
168     gint ret = FALSE;
170     switch (event->type) {
171         case GDK_BUTTON_PRESS:
172             ret = pencil_handle_button_press(pc, event->button);
173             break;
175         case GDK_MOTION_NOTIFY:
176             ret = pencil_handle_motion_notify(pc, event->motion);
177             break;
179         case GDK_BUTTON_RELEASE:
180             ret = pencil_handle_button_release(pc, event->button);
181             break;
183         case GDK_KEY_PRESS:
184             ret = pencil_handle_key_press(pc, get_group0_keyval (&event->key), event->key.state);
185             break;
187         default:
188             break;
189     }
191     if (!ret) {
192         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
193             = ((SPEventContextClass *) pencil_parent_class)->root_handler;
194         if (parent_root_handler) {
195             ret = parent_root_handler(ec, event);
196         }
197     }
199     return ret;
202 static gint
203 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
205     gint ret = FALSE;
206     if ( bevent.button == 1 ) {
208         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
209         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
210         Inkscape::Selection *selection = sp_desktop_selection(desktop);
212         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
213             return TRUE;
214         }
216         NR::Point const button_w(bevent.x, bevent.y);
218         /* Find desktop coordinates */
219         NR::Point p = pc->desktop->w2d(button_w);
221         /* Test whether we hit any anchor. */
222         SPDrawAnchor *anchor = spdc_test_inside(pc, button_w);
224         switch (pc->state) {
225             case SP_PENCIL_CONTEXT_ADDLINE:
226                 /* Current segment will be finished with release */
227                 ret = TRUE;
228                 break;
229             default:
230                 /* Set first point of sequence */
231                 if (anchor) {
232                     p = anchor->dp;
233                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
234                 } else {
236                     if (!(bevent.state & GDK_SHIFT_MASK)) {
238                         // This is the first click of a new curve; deselect item so that
239                         // this curve is not combined with it (unless it is drawn from its
240                         // anchor, which is handled by the sibling branch above)
241                         selection->clear();
242                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
243                         SnapManager const &m = desktop->namedview->snap_manager;
244                         p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
245                     } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
246                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
247                     }
248                 }
249                 pc->sa = anchor;
250                 spdc_set_startpoint(pc, p);
251                 ret = TRUE;
252                 break;
253         }
255         pc->is_drawing = true;
256     }
257     return ret;
260 static gint
261 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
263     gint ret = FALSE;
264     SPDesktop *const dt = pc->desktop;
266     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
267         // allow middle-button scrolling
268         return FALSE;
269     }
271     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
272         /* Grab mouse, so release will not pass unnoticed */
273         pc->grab = SP_CANVAS_ITEM(dt->acetate);
274         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
275                                         GDK_BUTTON_RELEASE_MASK |
276                                         GDK_POINTER_MOTION_MASK  ),
277                             NULL, mevent.time);
278     }
280     /* Find desktop coordinates */
281     NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
283     /* Test whether we hit any anchor. */
284     SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
286     switch (pc->state) {
287         case SP_PENCIL_CONTEXT_ADDLINE:
288             /* Set red endpoint */
289             if (anchor) {
290                 p = anchor->dp;
291             } else {
292                 spdc_endpoint_snap(pc, p, mevent.state);
293             }
294             spdc_set_endpoint(pc, p);
295             ret = TRUE;
296             break;
297         default:
298             /* We may be idle or already freehand */
299             if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
300                 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
301                 if ( !pc->sa && !pc->green_anchor ) {
302                     /* Create green anchor */
303                     pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
304                 }
305                 /** \todo
306                  * fixme: I am not sure whether we want to snap to anchors
307                  * in middle of freehand (Lauris)
308                  */
309                 if (anchor) {
310                     p = anchor->dp;
311                 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
312                     SnapManager const &m = dt->namedview->snap_manager;
313                     p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
314                 }
315                 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
316                     spdc_add_freehand_point(pc, p, mevent.state);
317                     ret = TRUE;
318                 }
320                 if (anchor && !pc->anchor_statusbar) {
321                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
322                     pc->anchor_statusbar = true;
323                 } else if (!anchor && pc->anchor_statusbar) {
324                     pc->_message_context->clear();
325                     pc->anchor_statusbar = false;
326                 } else if (!anchor) {
327                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
328                 }
330             } else {
331                 if (anchor && !pc->anchor_statusbar) {
332                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
333                     pc->anchor_statusbar = true;
334                 } else if (!anchor && pc->anchor_statusbar) {
335                     pc->_message_context->clear();
336                     pc->anchor_statusbar = false;
337                 }
338             }
339             break;
340     }
341     return ret;
344 static gint
345 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
347     gint ret = FALSE;
349     if ( revent.button == 1 && pc->is_drawing) {
350         SPDesktop *const dt = pc->desktop;
352         pc->is_drawing = false;
354         /* Find desktop coordinates */
355         NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
357         /* Test whether we hit any anchor. */
358         SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
359                                                               revent.y));
361         switch (pc->state) {
362             case SP_PENCIL_CONTEXT_IDLE:
363                 /* Releasing button in idle mode means single click */
364                 /* We have already set up start point/anchor in button_press */
365                 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
366                 ret = TRUE;
367                 break;
368             case SP_PENCIL_CONTEXT_ADDLINE:
369                 /* Finish segment now */
370                 if (anchor) {
371                     p = anchor->dp;
372                 } else {
373                     spdc_endpoint_snap(pc, p, revent.state);
374                 }
375                 pc->ea = anchor;
376                 spdc_set_endpoint(pc, p);
377                 spdc_finish_endpoint(pc);
378                 pc->state = SP_PENCIL_CONTEXT_IDLE;
379                 ret = TRUE;
380                 break;
381             case SP_PENCIL_CONTEXT_FREEHAND:
382                 /* Finish segment now */
383                 /// \todo fixme: Clean up what follows (Lauris)
384                 if (anchor) {
385                     p = anchor->dp;
386                 }
387                 pc->ea = anchor;
388                 /* Write curves to object */
390                 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
392                 spdc_concat_colors_and_flush(pc, FALSE);
393                 pc->sa = NULL;
394                 pc->ea = NULL;
395                 if (pc->green_anchor) {
396                     pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
397                 }
398                 pc->state = SP_PENCIL_CONTEXT_IDLE;
399                 ret = TRUE;
400                 break;
401             default:
402                 break;
403         }
405         if (pc->grab) {
406             /* Release grab now */
407             sp_canvas_item_ungrab(pc->grab, revent.time);
408             pc->grab = NULL;
409         }
411         pc->grab = NULL;
412         ret = TRUE;
413     }
414     return ret;
417 static gint
418 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
420     gint ret = FALSE;
421     switch (keyval) {
422         case GDK_Up:
423         case GDK_Down:
424         case GDK_KP_Up:
425         case GDK_KP_Down:
426             // Prevent the zoom field from activation.
427             if (!mod_ctrl_only(state)) {
428                 ret = TRUE;
429             }
430             break;
431         default:
432             break;
433     }
434     return ret;
437 /**
438  * Reset points and set new starting point.
439  */
440 static void
441 spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
443     pc->npoints = 0;
444     pc->red_curve_is_valid = false;
445     if (in_svg_plane(p)) {
446         pc->p[pc->npoints++] = p;
447     }
450 /**
451  * Change moving endpoint position.
452  * <ul>
453  * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
454  * <li>Otherwise we snap freely to whatever attractors are available.
455  * </ul>
456  *
457  * Number of points is (re)set to 2 always, 2nd point is modified.
458  * We change RED curve.
459  */
460 static void
461 spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
463     if (pc->npoints == 0) {
464         return;
465         /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
466          * zoom setting).
467          */
468     }
469     g_return_if_fail( pc->npoints > 0 );
471     sp_curve_reset(pc->red_curve);
472     if ( ( p == pc->p[0] )
473          || !in_svg_plane(p) )
474     {
475         pc->npoints = 1;
476     } else {
477         pc->p[1] = p;
478         pc->npoints = 2;
480         sp_curve_moveto(pc->red_curve, pc->p[0]);
481         sp_curve_lineto(pc->red_curve, pc->p[1]);
482         pc->red_curve_is_valid = true;
484         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
485     }
488 /**
489  * Finalize addline.
490  *
491  * \todo
492  * fixme: I'd like remove red reset from concat colors (lauris).
493  * Still not sure, how it will make most sense.
494  */
495 static void
496 spdc_finish_endpoint(SPPencilContext *const pc)
498     if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
499          || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
500               SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3)   ) )
501     {
502         sp_curve_reset(pc->red_curve);
503         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
504     } else {
505         /* Write curves to object. */
506         spdc_concat_colors_and_flush(pc, FALSE);
507         pc->sa = NULL;
508         pc->ea = NULL;
509     }
512 static void
513 spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state)
515     g_assert( pc->npoints > 0 );
516     g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
518     if ( ( p != pc->p[ pc->npoints - 1 ] )
519          && in_svg_plane(p) )
520     {
521         pc->p[pc->npoints++] = p;
522         fit_and_split(pc);
523     }
526 static inline double
527 square(double const x)
529     return x * x;
532 static void
533 fit_and_split(SPPencilContext *pc)
535     g_assert( pc->npoints > 1 );
537     double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
538                                         * prefs_get_double_attribute_limited("tools.freehand.pencil",
539                                                                              "tolerance", 10.0, 1.0, 100.0) );
541     NR::Point b[4];
542     g_assert(is_zero(pc->req_tangent)
543              || is_unit_vector(pc->req_tangent));
544     NR::Point const tHatEnd(0, 0);
545     int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
546                                                 pc->req_tangent, tHatEnd, tolerance_sq, 1);
547     if ( n_segs > 0
548          && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
549     {
550         /* Fit and draw and reset state */
551         sp_curve_reset(pc->red_curve);
552         sp_curve_moveto(pc->red_curve, b[0]);
553         sp_curve_curveto(pc->red_curve, b[1], b[2], b[3]);
554         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
555         pc->red_curve_is_valid = true;
556     } else {
557         /* Fit and draw and copy last point */
559         g_assert(!sp_curve_empty(pc->red_curve));
561         /* Set up direction of next curve. */
562         {
563             NArtBpath const &last_seg = *sp_curve_last_bpath(pc->red_curve);
564             pc->p[0] = last_seg.c(3);
565             pc->npoints = 1;
566             g_assert( last_seg.code == NR_CURVETO );
567             /* Relevance: validity of last_seg.c(2). */
568             NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
569             pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
570                                 ? NR::Point(0, 0)
571                                 : NR::unit_vector(req_vec) );
572         }
574         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
575         SPCurve *curve = sp_curve_copy(pc->red_curve);
577         /// \todo fixme:
578         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
579         sp_curve_unref(curve);
580         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
582         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
584         pc->red_curve_is_valid = false;
585     }
589 /*
590   Local Variables:
591   mode:c++
592   c-file-style:"stroustrup"
593   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
594   indent-tabs-mode:nil
595   fill-column:99
596   End:
597 */
598 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :