Code

Get rid of the SP_DT_* macros which do nothing more than provide additional, confusin...
[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 "display/bezier-utils.h"
33 #include "display/canvas-bpath.h"
34 #include <glibmm/i18n.h>
35 #include "libnr/in-svg-plane.h"
36 #include "libnr/n-art-bpath.h"
37 #include "context-fns.h"
39 static void sp_pencil_context_class_init(SPPencilContextClass *klass);
40 static void sp_pencil_context_init(SPPencilContext *pc);
41 static void sp_pencil_context_setup(SPEventContext *ec);
42 static void sp_pencil_context_dispose(GObject *object);
44 static gint sp_pencil_context_root_handler(SPEventContext *event_context, GdkEvent *event);
45 static gint pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent);
46 static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent);
47 static gint pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent);
48 static gint pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state);
50 static void spdc_set_startpoint(SPPencilContext *pc, NR::Point const p);
51 static void spdc_set_endpoint(SPPencilContext *pc, NR::Point const p);
52 static void spdc_finish_endpoint(SPPencilContext *pc);
53 static void spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state);
54 static void fit_and_split(SPPencilContext *pc);
57 static SPDrawContextClass *pencil_parent_class;
59 /**
60  * Register SPPencilContext class with Gdk and return its type number.
61  */
62 GType
63 sp_pencil_context_get_type()
64 {
65     static GType type = 0;
66     if (!type) {
67         GTypeInfo info = {
68             sizeof(SPPencilContextClass),
69             NULL, NULL,
70             (GClassInitFunc) sp_pencil_context_class_init,
71             NULL, NULL,
72             sizeof(SPPencilContext),
73             4,
74             (GInstanceInitFunc) sp_pencil_context_init,
75             NULL,   /* value_table */
76         };
77         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPencilContext", &info, (GTypeFlags)0);
78     }
79     return type;
80 }
82 /**
83  * Initialize SPPencilContext vtable.
84  */
85 static void
86 sp_pencil_context_class_init(SPPencilContextClass *klass)
87 {
88     GObjectClass *object_class;
89     SPEventContextClass *event_context_class;
91     object_class = (GObjectClass *) klass;
92     event_context_class = (SPEventContextClass *) klass;
94     pencil_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
96     object_class->dispose = sp_pencil_context_dispose;
98     event_context_class->setup = sp_pencil_context_setup;
99     event_context_class->root_handler = sp_pencil_context_root_handler;
102 /**
103  * Callback to initialize SPPencilContext object.
104  */
105 static void
106 sp_pencil_context_init(SPPencilContext *pc)
108     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
110     event_context->cursor_shape = cursor_pencil_xpm;
111     event_context->hot_x = 4;
112     event_context->hot_y = 4;
114     pc->npoints = 0;
115     pc->state = SP_PENCIL_CONTEXT_IDLE;
116     pc->req_tangent = NR::Point(0, 0);
119 /**
120  * Callback to setup SPPencilContext object.
121  */
122 static void
123 sp_pencil_context_setup(SPEventContext *ec)
125     if (prefs_get_int_attribute("tools.freehand.pencil", "selcue", 0) != 0) {
126         ec->enableSelectionCue();
127     }
129     if (((SPEventContextClass *) pencil_parent_class)->setup) {
130         ((SPEventContextClass *) pencil_parent_class)->setup(ec);
131     }
133     SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
134     pc->is_drawing = false;
136     pc->anchor_statusbar = false;
139 static void
140 sp_pencil_context_dispose(GObject *object)
142     G_OBJECT_CLASS(pencil_parent_class)->dispose(object);
145 /** Snaps new node relative to the previous node. */
146 static void
147 spdc_endpoint_snap(SPPencilContext const *pc, NR::Point &p, guint const state)
149     spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
150     spdc_endpoint_snap_free(pc, p, state);
153 /**
154  * Callback for handling all pencil context events.
155  */
156 gint
157 sp_pencil_context_root_handler(SPEventContext *const ec, GdkEvent *event)
159     SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
161     gint ret = FALSE;
163     switch (event->type) {
164         case GDK_BUTTON_PRESS:
165             ret = pencil_handle_button_press(pc, event->button);
166             break;
168         case GDK_MOTION_NOTIFY:
169             ret = pencil_handle_motion_notify(pc, event->motion);
170             break;
172         case GDK_BUTTON_RELEASE:
173             ret = pencil_handle_button_release(pc, event->button);
174             break;
176         case GDK_KEY_PRESS:
177             ret = pencil_handle_key_press(pc, get_group0_keyval (&event->key), event->key.state);
178             break;
180         default:
181             break;
182     }
184     if (!ret) {
185         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
186             = ((SPEventContextClass *) pencil_parent_class)->root_handler;
187         if (parent_root_handler) {
188             ret = parent_root_handler(ec, event);
189         }
190     }
192     return ret;
195 static gint
196 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
198     gint ret = FALSE;
199     if ( bevent.button == 1 ) {
201         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
202         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
203         Inkscape::Selection *selection = sp_desktop_selection(desktop);
205         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
206             return TRUE;
207         }
209         NR::Point const button_w(bevent.x, bevent.y);
211         /* Find desktop coordinates */
212         NR::Point p = pc->desktop->w2d(button_w);
214         /* Test whether we hit any anchor. */
215         SPDrawAnchor *anchor = spdc_test_inside(pc, button_w);
217         switch (pc->state) {
218             case SP_PENCIL_CONTEXT_ADDLINE:
219                 /* Current segment will be finished with release */
220                 ret = TRUE;
221                 break;
222             default:
223                 /* Set first point of sequence */
224                 if (anchor) {
225                     p = anchor->dp;
226                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
227                 } else {
229                     if (!(bevent.state & GDK_SHIFT_MASK)) {
231                         // This is the first click of a new curve; deselect item so that
232                         // this curve is not combined with it (unless it is drawn from its
233                         // anchor, which is handled by the sibling branch above)
234                         selection->clear();
235                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
236                         SnapManager const m(desktop->namedview);
237                         p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
238                     } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
239                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
240                     }
241                 }
242                 pc->sa = anchor;
243                 spdc_set_startpoint(pc, p);
244                 ret = TRUE;
245                 break;
246         }
248         pc->is_drawing = true;
249     }
250     return ret;
253 static gint
254 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
256     gint ret = FALSE;
257     SPDesktop *const dt = pc->desktop;
259     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
260         // allow middle-button scrolling
261         return FALSE;
262     }
264     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
265         /* Grab mouse, so release will not pass unnoticed */
266         pc->grab = SP_CANVAS_ITEM(dt->acetate);
267         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
268                                         GDK_BUTTON_RELEASE_MASK |
269                                         GDK_POINTER_MOTION_MASK  ),
270                             NULL, mevent.time);
271     }
273     /* Find desktop coordinates */
274     NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
276     /* Test whether we hit any anchor. */
277     SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
279     switch (pc->state) {
280         case SP_PENCIL_CONTEXT_ADDLINE:
281             /* Set red endpoint */
282             if (anchor) {
283                 p = anchor->dp;
284             } else {
285                 spdc_endpoint_snap(pc, p, mevent.state);
286             }
287             spdc_set_endpoint(pc, p);
288             ret = TRUE;
289             break;
290         default:
291             /* We may be idle or already freehand */
292             if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
293                 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
294                 if ( !pc->sa && !pc->green_anchor ) {
295                     /* Create green anchor */
296                     pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
297                 }
298                 /** \todo
299                  * fixme: I am not sure whether we want to snap to anchors
300                  * in middle of freehand (Lauris)
301                  */
302                 if (anchor) {
303                     p = anchor->dp;
304                 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
305                     SnapManager const m(dt->namedview);
306                     p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
307                 }
308                 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
309                     spdc_add_freehand_point(pc, p, mevent.state);
310                     ret = TRUE;
311                 }
313                 if (anchor && !pc->anchor_statusbar) {
314                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
315                     pc->anchor_statusbar = true;
316                 } else if (!anchor && pc->anchor_statusbar) {
317                     pc->_message_context->clear();
318                     pc->anchor_statusbar = false;
319                 } else if (!anchor) {
320                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
321                 }
323             } else {
324                 if (anchor && !pc->anchor_statusbar) {
325                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
326                     pc->anchor_statusbar = true;
327                 } else if (!anchor && pc->anchor_statusbar) {
328                     pc->_message_context->clear();
329                     pc->anchor_statusbar = false;
330                 }
331             }
332             break;
333     }
334     return ret;
337 static gint
338 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
340     gint ret = FALSE;
342     if ( revent.button == 1 && pc->is_drawing) {
343         SPDesktop *const dt = pc->desktop;
345         pc->is_drawing = false;
347         /* Find desktop coordinates */
348         NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
350         /* Test whether we hit any anchor. */
351         SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
352                                                               revent.y));
354         switch (pc->state) {
355             case SP_PENCIL_CONTEXT_IDLE:
356                 /* Releasing button in idle mode means single click */
357                 /* We have already set up start point/anchor in button_press */
358                 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
359                 ret = TRUE;
360                 break;
361             case SP_PENCIL_CONTEXT_ADDLINE:
362                 /* Finish segment now */
363                 if (anchor) {
364                     p = anchor->dp;
365                 } else {
366                     spdc_endpoint_snap(pc, p, revent.state);
367                 }
368                 pc->ea = anchor;
369                 spdc_set_endpoint(pc, p);
370                 spdc_finish_endpoint(pc);
371                 pc->state = SP_PENCIL_CONTEXT_IDLE;
372                 ret = TRUE;
373                 break;
374             case SP_PENCIL_CONTEXT_FREEHAND:
375                 /* Finish segment now */
376                 /// \todo fixme: Clean up what follows (Lauris)
377                 if (anchor) {
378                     p = anchor->dp;
379                 }
380                 pc->ea = anchor;
381                 /* Write curves to object */
383                 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
385                 spdc_concat_colors_and_flush(pc, FALSE);
386                 pc->sa = NULL;
387                 pc->ea = NULL;
388                 if (pc->green_anchor) {
389                     pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
390                 }
391                 pc->state = SP_PENCIL_CONTEXT_IDLE;
392                 ret = TRUE;
393                 break;
394             default:
395                 break;
396         }
398         if (pc->grab) {
399             /* Release grab now */
400             sp_canvas_item_ungrab(pc->grab, revent.time);
401             pc->grab = NULL;
402         }
404         pc->grab = NULL;
405         ret = TRUE;
406     }
407     return ret;
410 static gint
411 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
413     gint ret = FALSE;
414     switch (keyval) {
415         case GDK_Up:
416         case GDK_Down:
417         case GDK_KP_Up:
418         case GDK_KP_Down:
419             // Prevent the zoom field from activation.
420             if (!mod_ctrl_only(state)) {
421                 ret = TRUE;
422             }
423             break;
424         default:
425             break;
426     }
427     return ret;
430 /**
431  * Reset points and set new starting point.
432  */
433 static void
434 spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
436     pc->npoints = 0;
437     pc->red_curve_is_valid = false;
438     if (in_svg_plane(p)) {
439         pc->p[pc->npoints++] = p;
440     }
443 /**
444  * Change moving endpoint position.
445  * <ul>
446  * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
447  * <li>Otherwise we snap freely to whatever attractors are available.
448  * </ul>
449  *
450  * Number of points is (re)set to 2 always, 2nd point is modified.
451  * We change RED curve.
452  */
453 static void
454 spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
456     if (pc->npoints == 0) {
457         return;
458         /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
459          * zoom setting).
460          */
461     }
462     g_return_if_fail( pc->npoints > 0 );
464     sp_curve_reset(pc->red_curve);
465     if ( ( p == pc->p[0] )
466          || !in_svg_plane(p) )
467     {
468         pc->npoints = 1;
469     } else {
470         pc->p[1] = p;
471         pc->npoints = 2;
473         sp_curve_moveto(pc->red_curve, pc->p[0]);
474         sp_curve_lineto(pc->red_curve, pc->p[1]);
475         pc->red_curve_is_valid = true;
477         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
478     }
481 /**
482  * Finalize addline.
483  *
484  * \todo
485  * fixme: I'd like remove red reset from concat colors (lauris).
486  * Still not sure, how it will make most sense.
487  */
488 static void
489 spdc_finish_endpoint(SPPencilContext *const pc)
491     if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
492          || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
493               SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3)   ) )
494     {
495         sp_curve_reset(pc->red_curve);
496         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
497     } else {
498         /* Write curves to object. */
499         spdc_concat_colors_and_flush(pc, FALSE);
500         pc->sa = NULL;
501         pc->ea = NULL;
502     }
505 static void
506 spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state)
508     g_assert( pc->npoints > 0 );
509     g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
511     if ( ( p != pc->p[ pc->npoints - 1 ] )
512          && in_svg_plane(p) )
513     {
514         pc->p[pc->npoints++] = p;
515         fit_and_split(pc);
516     }
519 static inline double
520 square(double const x)
522     return x * x;
525 static void
526 fit_and_split(SPPencilContext *pc)
528     g_assert( pc->npoints > 1 );
530     double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
531                                         * prefs_get_double_attribute_limited("tools.freehand.pencil",
532                                                                              "tolerance", 10.0, 1.0, 100.0) );
534     NR::Point b[4];
535     g_assert(is_zero(pc->req_tangent)
536              || is_unit_vector(pc->req_tangent));
537     NR::Point const tHatEnd(0, 0);
538     int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
539                                                 pc->req_tangent, tHatEnd, tolerance_sq, 1);
540     if ( n_segs > 0
541          && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
542     {
543         /* Fit and draw and reset state */
544         sp_curve_reset(pc->red_curve);
545         sp_curve_moveto(pc->red_curve, b[0]);
546         sp_curve_curveto(pc->red_curve, b[1], b[2], b[3]);
547         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
548         pc->red_curve_is_valid = true;
549     } else {
550         /* Fit and draw and copy last point */
552         g_assert(!sp_curve_empty(pc->red_curve));
554         /* Set up direction of next curve. */
555         {
556             NArtBpath const &last_seg = *sp_curve_last_bpath(pc->red_curve);
557             pc->p[0] = last_seg.c(3);
558             pc->npoints = 1;
559             g_assert( last_seg.code == NR_CURVETO );
560             /* Relevance: validity of last_seg.c(2). */
561             NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
562             pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
563                                 ? NR::Point(0, 0)
564                                 : NR::unit_vector(req_vec) );
565         }
567         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
568         SPCurve *curve = sp_curve_copy(pc->red_curve);
570         /// \todo fixme:
571         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
572         sp_curve_unref(curve);
573         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
575         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
577         pc->red_curve_is_valid = false;
578     }
582 /*
583   Local Variables:
584   mode:c++
585   c-file-style:"stroustrup"
586   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
587   indent-tabs-mode:nil
588   fill-column:99
589   End:
590 */
591 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :