Code

remove color cursors
[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"
38 #include "sp-namedview.h"
40 static void sp_pencil_context_class_init(SPPencilContextClass *klass);
41 static void sp_pencil_context_init(SPPencilContext *pc);
42 static void sp_pencil_context_setup(SPEventContext *ec);
43 static void sp_pencil_context_dispose(GObject *object);
45 static gint sp_pencil_context_root_handler(SPEventContext *event_context, GdkEvent *event);
46 static gint pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent);
47 static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent);
48 static gint pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent);
49 static gint pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state);
51 static void spdc_set_startpoint(SPPencilContext *pc, NR::Point const p);
52 static void spdc_set_endpoint(SPPencilContext *pc, NR::Point const p);
53 static void spdc_finish_endpoint(SPPencilContext *pc);
54 static void spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state);
55 static void fit_and_split(SPPencilContext *pc);
58 static SPDrawContextClass *pencil_parent_class;
60 /**
61  * Register SPPencilContext class with Gdk and return its type number.
62  */
63 GType
64 sp_pencil_context_get_type()
65 {
66     static GType type = 0;
67     if (!type) {
68         GTypeInfo info = {
69             sizeof(SPPencilContextClass),
70             NULL, NULL,
71             (GClassInitFunc) sp_pencil_context_class_init,
72             NULL, NULL,
73             sizeof(SPPencilContext),
74             4,
75             (GInstanceInitFunc) sp_pencil_context_init,
76             NULL,   /* value_table */
77         };
78         type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPencilContext", &info, (GTypeFlags)0);
79     }
80     return type;
81 }
83 /**
84  * Initialize SPPencilContext vtable.
85  */
86 static void
87 sp_pencil_context_class_init(SPPencilContextClass *klass)
88 {
89     GObjectClass *object_class;
90     SPEventContextClass *event_context_class;
92     object_class = (GObjectClass *) klass;
93     event_context_class = (SPEventContextClass *) klass;
95     pencil_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
97     object_class->dispose = sp_pencil_context_dispose;
99     event_context_class->setup = sp_pencil_context_setup;
100     event_context_class->root_handler = sp_pencil_context_root_handler;
103 /**
104  * Callback to initialize SPPencilContext object.
105  */
106 static void
107 sp_pencil_context_init(SPPencilContext *pc)
109     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
111     event_context->cursor_shape = cursor_pencil_xpm;
112     event_context->hot_x = 4;
113     event_context->hot_y = 4;
115     pc->npoints = 0;
116     pc->state = SP_PENCIL_CONTEXT_IDLE;
117     pc->req_tangent = NR::Point(0, 0);
120 /**
121  * Callback to setup SPPencilContext object.
122  */
123 static void
124 sp_pencil_context_setup(SPEventContext *ec)
126     if (prefs_get_int_attribute("tools.freehand.pencil", "selcue", 0) != 0) {
127         ec->enableSelectionCue();
128     }
130     if (((SPEventContextClass *) pencil_parent_class)->setup) {
131         ((SPEventContextClass *) pencil_parent_class)->setup(ec);
132     }
134     SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
135     pc->is_drawing = false;
137     pc->anchor_statusbar = false;
140 static void
141 sp_pencil_context_dispose(GObject *object)
143     G_OBJECT_CLASS(pencil_parent_class)->dispose(object);
146 /** Snaps new node relative to the previous node. */
147 static void
148 spdc_endpoint_snap(SPPencilContext const *pc, NR::Point &p, guint const state)
150     spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
151     spdc_endpoint_snap_free(pc, p, state);
154 /**
155  * Callback for handling all pencil context events.
156  */
157 gint
158 sp_pencil_context_root_handler(SPEventContext *const ec, GdkEvent *event)
160     SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
162     gint ret = FALSE;
164     switch (event->type) {
165         case GDK_BUTTON_PRESS:
166             ret = pencil_handle_button_press(pc, event->button);
167             break;
169         case GDK_MOTION_NOTIFY:
170             ret = pencil_handle_motion_notify(pc, event->motion);
171             break;
173         case GDK_BUTTON_RELEASE:
174             ret = pencil_handle_button_release(pc, event->button);
175             break;
177         case GDK_KEY_PRESS:
178             ret = pencil_handle_key_press(pc, get_group0_keyval (&event->key), event->key.state);
179             break;
181         default:
182             break;
183     }
185     if (!ret) {
186         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
187             = ((SPEventContextClass *) pencil_parent_class)->root_handler;
188         if (parent_root_handler) {
189             ret = parent_root_handler(ec, event);
190         }
191     }
193     return ret;
196 static gint
197 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
199     gint ret = FALSE;
200     if ( bevent.button == 1 ) {
202         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
203         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
204         Inkscape::Selection *selection = sp_desktop_selection(desktop);
206         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
207             return TRUE;
208         }
210         NR::Point const button_w(bevent.x, bevent.y);
212         /* Find desktop coordinates */
213         NR::Point p = pc->desktop->w2d(button_w);
215         /* Test whether we hit any anchor. */
216         SPDrawAnchor *anchor = spdc_test_inside(pc, button_w);
218         switch (pc->state) {
219             case SP_PENCIL_CONTEXT_ADDLINE:
220                 /* Current segment will be finished with release */
221                 ret = TRUE;
222                 break;
223             default:
224                 /* Set first point of sequence */
225                 if (anchor) {
226                     p = anchor->dp;
227                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
228                 } else {
230                     if (!(bevent.state & GDK_SHIFT_MASK)) {
232                         // This is the first click of a new curve; deselect item so that
233                         // this curve is not combined with it (unless it is drawn from its
234                         // anchor, which is handled by the sibling branch above)
235                         selection->clear();
236                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
237                         SnapManager const &m = desktop->namedview->snap_manager;
238                         p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
239                     } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
240                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
241                     }
242                 }
243                 pc->sa = anchor;
244                 spdc_set_startpoint(pc, p);
245                 ret = TRUE;
246                 break;
247         }
249         pc->is_drawing = true;
250     }
251     return ret;
254 static gint
255 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
257     gint ret = FALSE;
258     SPDesktop *const dt = pc->desktop;
260     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
261         // allow middle-button scrolling
262         return FALSE;
263     }
265     if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
266         /* Grab mouse, so release will not pass unnoticed */
267         pc->grab = SP_CANVAS_ITEM(dt->acetate);
268         sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
269                                         GDK_BUTTON_RELEASE_MASK |
270                                         GDK_POINTER_MOTION_MASK  ),
271                             NULL, mevent.time);
272     }
274     /* Find desktop coordinates */
275     NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
277     /* Test whether we hit any anchor. */
278     SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
280     switch (pc->state) {
281         case SP_PENCIL_CONTEXT_ADDLINE:
282             /* Set red endpoint */
283             if (anchor) {
284                 p = anchor->dp;
285             } else {
286                 spdc_endpoint_snap(pc, p, mevent.state);
287             }
288             spdc_set_endpoint(pc, p);
289             ret = TRUE;
290             break;
291         default:
292             /* We may be idle or already freehand */
293             if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
294                 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
295                 if ( !pc->sa && !pc->green_anchor ) {
296                     /* Create green anchor */
297                     pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
298                 }
299                 /** \todo
300                  * fixme: I am not sure whether we want to snap to anchors
301                  * in middle of freehand (Lauris)
302                  */
303                 if (anchor) {
304                     p = anchor->dp;
305                 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
306                     SnapManager const &m = dt->namedview->snap_manager;
307                     p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
308                 }
309                 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
310                     spdc_add_freehand_point(pc, p, mevent.state);
311                     ret = TRUE;
312                 }
314                 if (anchor && !pc->anchor_statusbar) {
315                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
316                     pc->anchor_statusbar = true;
317                 } else if (!anchor && pc->anchor_statusbar) {
318                     pc->_message_context->clear();
319                     pc->anchor_statusbar = false;
320                 } else if (!anchor) {
321                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
322                 }
324             } else {
325                 if (anchor && !pc->anchor_statusbar) {
326                     pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
327                     pc->anchor_statusbar = true;
328                 } else if (!anchor && pc->anchor_statusbar) {
329                     pc->_message_context->clear();
330                     pc->anchor_statusbar = false;
331                 }
332             }
333             break;
334     }
335     return ret;
338 static gint
339 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
341     gint ret = FALSE;
343     if ( revent.button == 1 && pc->is_drawing) {
344         SPDesktop *const dt = pc->desktop;
346         pc->is_drawing = false;
348         /* Find desktop coordinates */
349         NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
351         /* Test whether we hit any anchor. */
352         SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
353                                                               revent.y));
355         switch (pc->state) {
356             case SP_PENCIL_CONTEXT_IDLE:
357                 /* Releasing button in idle mode means single click */
358                 /* We have already set up start point/anchor in button_press */
359                 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
360                 ret = TRUE;
361                 break;
362             case SP_PENCIL_CONTEXT_ADDLINE:
363                 /* Finish segment now */
364                 if (anchor) {
365                     p = anchor->dp;
366                 } else {
367                     spdc_endpoint_snap(pc, p, revent.state);
368                 }
369                 pc->ea = anchor;
370                 spdc_set_endpoint(pc, p);
371                 spdc_finish_endpoint(pc);
372                 pc->state = SP_PENCIL_CONTEXT_IDLE;
373                 ret = TRUE;
374                 break;
375             case SP_PENCIL_CONTEXT_FREEHAND:
376                 /* Finish segment now */
377                 /// \todo fixme: Clean up what follows (Lauris)
378                 if (anchor) {
379                     p = anchor->dp;
380                 }
381                 pc->ea = anchor;
382                 /* Write curves to object */
384                 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
386                 spdc_concat_colors_and_flush(pc, FALSE);
387                 pc->sa = NULL;
388                 pc->ea = NULL;
389                 if (pc->green_anchor) {
390                     pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
391                 }
392                 pc->state = SP_PENCIL_CONTEXT_IDLE;
393                 ret = TRUE;
394                 break;
395             default:
396                 break;
397         }
399         if (pc->grab) {
400             /* Release grab now */
401             sp_canvas_item_ungrab(pc->grab, revent.time);
402             pc->grab = NULL;
403         }
405         pc->grab = NULL;
406         ret = TRUE;
407     }
408     return ret;
411 static gint
412 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
414     gint ret = FALSE;
415     switch (keyval) {
416         case GDK_Up:
417         case GDK_Down:
418         case GDK_KP_Up:
419         case GDK_KP_Down:
420             // Prevent the zoom field from activation.
421             if (!mod_ctrl_only(state)) {
422                 ret = TRUE;
423             }
424             break;
425         default:
426             break;
427     }
428     return ret;
431 /**
432  * Reset points and set new starting point.
433  */
434 static void
435 spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
437     pc->npoints = 0;
438     pc->red_curve_is_valid = false;
439     if (in_svg_plane(p)) {
440         pc->p[pc->npoints++] = p;
441     }
444 /**
445  * Change moving endpoint position.
446  * <ul>
447  * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
448  * <li>Otherwise we snap freely to whatever attractors are available.
449  * </ul>
450  *
451  * Number of points is (re)set to 2 always, 2nd point is modified.
452  * We change RED curve.
453  */
454 static void
455 spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
457     if (pc->npoints == 0) {
458         return;
459         /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
460          * zoom setting).
461          */
462     }
463     g_return_if_fail( pc->npoints > 0 );
465     sp_curve_reset(pc->red_curve);
466     if ( ( p == pc->p[0] )
467          || !in_svg_plane(p) )
468     {
469         pc->npoints = 1;
470     } else {
471         pc->p[1] = p;
472         pc->npoints = 2;
474         sp_curve_moveto(pc->red_curve, pc->p[0]);
475         sp_curve_lineto(pc->red_curve, pc->p[1]);
476         pc->red_curve_is_valid = true;
478         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
479     }
482 /**
483  * Finalize addline.
484  *
485  * \todo
486  * fixme: I'd like remove red reset from concat colors (lauris).
487  * Still not sure, how it will make most sense.
488  */
489 static void
490 spdc_finish_endpoint(SPPencilContext *const pc)
492     if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
493          || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
494               SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3)   ) )
495     {
496         sp_curve_reset(pc->red_curve);
497         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
498     } else {
499         /* Write curves to object. */
500         spdc_concat_colors_and_flush(pc, FALSE);
501         pc->sa = NULL;
502         pc->ea = NULL;
503     }
506 static void
507 spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state)
509     g_assert( pc->npoints > 0 );
510     g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
512     if ( ( p != pc->p[ pc->npoints - 1 ] )
513          && in_svg_plane(p) )
514     {
515         pc->p[pc->npoints++] = p;
516         fit_and_split(pc);
517     }
520 static inline double
521 square(double const x)
523     return x * x;
526 static void
527 fit_and_split(SPPencilContext *pc)
529     g_assert( pc->npoints > 1 );
531     double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
532                                         * prefs_get_double_attribute_limited("tools.freehand.pencil",
533                                                                              "tolerance", 10.0, 1.0, 100.0) );
535     NR::Point b[4];
536     g_assert(is_zero(pc->req_tangent)
537              || is_unit_vector(pc->req_tangent));
538     NR::Point const tHatEnd(0, 0);
539     int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
540                                                 pc->req_tangent, tHatEnd, tolerance_sq, 1);
541     if ( n_segs > 0
542          && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
543     {
544         /* Fit and draw and reset state */
545         sp_curve_reset(pc->red_curve);
546         sp_curve_moveto(pc->red_curve, b[0]);
547         sp_curve_curveto(pc->red_curve, b[1], b[2], b[3]);
548         sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
549         pc->red_curve_is_valid = true;
550     } else {
551         /* Fit and draw and copy last point */
553         g_assert(!sp_curve_empty(pc->red_curve));
555         /* Set up direction of next curve. */
556         {
557             NArtBpath const &last_seg = *sp_curve_last_bpath(pc->red_curve);
558             pc->p[0] = last_seg.c(3);
559             pc->npoints = 1;
560             g_assert( last_seg.code == NR_CURVETO );
561             /* Relevance: validity of last_seg.c(2). */
562             NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
563             pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
564                                 ? NR::Point(0, 0)
565                                 : NR::unit_vector(req_vec) );
566         }
568         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
569         SPCurve *curve = sp_curve_copy(pc->red_curve);
571         /// \todo fixme:
572         SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
573         sp_curve_unref(curve);
574         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
576         pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
578         pc->red_curve_is_valid = false;
579     }
583 /*
584   Local Variables:
585   mode:c++
586   c-file-style:"stroustrup"
587   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
588   indent-tabs-mode:nil
589   fill-column:99
590   End:
591 */
592 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :