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 "selection-chemistry.h"
25 #include "draw-anchor.h"
26 #include "message-stack.h"
27 #include "message-context.h"
28 #include "modifier-fns.h"
29 #include "sp-path.h"
30 #include "prefs-utils.h"
31 #include "snap.h"
32 #include "pixmaps/cursor-pencil.xpm"
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"
40 #include "xml/repr.h"
41 #include "document.h"
42 #include "desktop-style.h"
43 #include "macros.h"
44 #include "display/curve.h"
46 static void sp_pencil_context_class_init(SPPencilContextClass *klass);
47 static void sp_pencil_context_init(SPPencilContext *pc);
48 static void sp_pencil_context_setup(SPEventContext *ec);
49 static void sp_pencil_context_dispose(GObject *object);
51 static gint sp_pencil_context_root_handler(SPEventContext *event_context, GdkEvent *event);
52 static gint pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent);
53 static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent);
54 static gint pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent);
55 static gint pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state);
57 static void spdc_set_startpoint(SPPencilContext *pc, NR::Point const p);
58 static void spdc_set_endpoint(SPPencilContext *pc, NR::Point const p);
59 static void spdc_finish_endpoint(SPPencilContext *pc);
60 static void spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state);
61 static void fit_and_split(SPPencilContext *pc);
64 static SPDrawContextClass *pencil_parent_class;
66 /**
67 * Register SPPencilContext class with Gdk and return its type number.
68 */
69 GType
70 sp_pencil_context_get_type()
71 {
72 static GType type = 0;
73 if (!type) {
74 GTypeInfo info = {
75 sizeof(SPPencilContextClass),
76 NULL, NULL,
77 (GClassInitFunc) sp_pencil_context_class_init,
78 NULL, NULL,
79 sizeof(SPPencilContext),
80 4,
81 (GInstanceInitFunc) sp_pencil_context_init,
82 NULL, /* value_table */
83 };
84 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPencilContext", &info, (GTypeFlags)0);
85 }
86 return type;
87 }
89 /**
90 * Initialize SPPencilContext vtable.
91 */
92 static void
93 sp_pencil_context_class_init(SPPencilContextClass *klass)
94 {
95 GObjectClass *object_class;
96 SPEventContextClass *event_context_class;
98 object_class = (GObjectClass *) klass;
99 event_context_class = (SPEventContextClass *) klass;
101 pencil_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
103 object_class->dispose = sp_pencil_context_dispose;
105 event_context_class->setup = sp_pencil_context_setup;
106 event_context_class->root_handler = sp_pencil_context_root_handler;
107 }
109 /**
110 * Callback to initialize SPPencilContext object.
111 */
112 static void
113 sp_pencil_context_init(SPPencilContext *pc)
114 {
115 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
117 event_context->cursor_shape = cursor_pencil_xpm;
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);
124 }
126 /**
127 * Callback to setup SPPencilContext object.
128 */
129 static void
130 sp_pencil_context_setup(SPEventContext *ec)
131 {
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;
144 }
146 static void
147 sp_pencil_context_dispose(GObject *object)
148 {
149 G_OBJECT_CLASS(pencil_parent_class)->dispose(object);
150 }
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)
155 {
156 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
157 spdc_endpoint_snap_free(pc, p, state);
158 }
160 /**
161 * Callback for handling all pencil context events.
162 */
163 gint
164 sp_pencil_context_root_handler(SPEventContext *const ec, GdkEvent *event)
165 {
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;
200 }
202 static gint
203 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
204 {
205 gint ret = FALSE;
206 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
207 if ( bevent.button == 1 && !event_context->space_panning) {
209 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
210 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
211 Inkscape::Selection *selection = sp_desktop_selection(desktop);
213 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
214 return TRUE;
215 }
217 NR::Point const button_w(bevent.x, bevent.y);
219 /* Find desktop coordinates */
220 NR::Point p = pc->desktop->w2d(button_w);
222 /* Test whether we hit any anchor. */
223 SPDrawAnchor *anchor = spdc_test_inside(pc, button_w);
225 switch (pc->state) {
226 case SP_PENCIL_CONTEXT_ADDLINE:
227 /* Current segment will be finished with release */
228 ret = TRUE;
229 break;
230 default:
231 /* Set first point of sequence */
232 if (bevent.state & GDK_CONTROL_MASK) {
233 freehand_create_single_dot(event_context, p, "tools.freehand.pencil", bevent.state);
234 ret = true;
235 break;
236 }
237 if (anchor) {
238 p = anchor->dp;
239 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
240 } else {
242 if (!(bevent.state & GDK_SHIFT_MASK)) {
244 // This is the first click of a new curve; deselect item so that
245 // this curve is not combined with it (unless it is drawn from its
246 // anchor, which is handled by the sibling branch above)
247 selection->clear();
248 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
249 SnapManager &m = desktop->namedview->snap_manager;
250 m.setup(desktop);
251 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p).getPoint();
252 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
253 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
254 }
255 }
256 pc->sa = anchor;
257 spdc_set_startpoint(pc, p);
258 ret = TRUE;
259 break;
260 }
262 pc->is_drawing = true;
263 }
264 return ret;
265 }
267 static gint
268 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
269 {
270 if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) {
271 // mouse was accidentally moved during Ctrl+click;
272 // ignore the motion and create a single point
273 pc->is_drawing = false;
274 return TRUE;
275 }
276 gint ret = FALSE;
277 SPDesktop *const dt = pc->desktop;
279 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
280 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
281 // allow scrolling
282 return FALSE;
283 }
285 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
286 /* Grab mouse, so release will not pass unnoticed */
287 pc->grab = SP_CANVAS_ITEM(dt->acetate);
288 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
289 GDK_BUTTON_RELEASE_MASK |
290 GDK_POINTER_MOTION_MASK ),
291 NULL, mevent.time);
292 }
294 /* Find desktop coordinates */
295 NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
297 /* Test whether we hit any anchor. */
298 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
300 switch (pc->state) {
301 case SP_PENCIL_CONTEXT_ADDLINE:
302 /* Set red endpoint */
303 if (anchor) {
304 p = anchor->dp;
305 } else {
306 spdc_endpoint_snap(pc, p, mevent.state);
307 }
308 spdc_set_endpoint(pc, p);
309 ret = TRUE;
310 break;
311 default:
312 /* We may be idle or already freehand */
313 if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
314 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
315 if ( !pc->sa && !pc->green_anchor ) {
316 /* Create green anchor */
317 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
318 }
319 /** \todo
320 * fixme: I am not sure whether we want to snap to anchors
321 * in middle of freehand (Lauris)
322 */
323 if (anchor) {
324 p = anchor->dp;
325 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
326 SnapManager &m = dt->namedview->snap_manager;
327 m.setup(dt, NULL);
328 Inkscape::SnappedPoint const s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p);
329 p = s.getPoint();
330 }
331 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
332 spdc_add_freehand_point(pc, p, mevent.state);
333 ret = TRUE;
334 }
336 if (anchor && !pc->anchor_statusbar) {
337 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
338 pc->anchor_statusbar = true;
339 } else if (!anchor && pc->anchor_statusbar) {
340 pc->_message_context->clear();
341 pc->anchor_statusbar = false;
342 } else if (!anchor) {
343 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
344 }
346 } else {
347 if (anchor && !pc->anchor_statusbar) {
348 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
349 pc->anchor_statusbar = true;
350 } else if (!anchor && pc->anchor_statusbar) {
351 pc->_message_context->clear();
352 pc->anchor_statusbar = false;
353 }
354 }
355 break;
356 }
357 return ret;
358 }
360 static gint
361 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
362 {
363 gint ret = FALSE;
365 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
366 if ( revent.button == 1 && pc->is_drawing && !event_context->space_panning) {
367 SPDesktop *const dt = pc->desktop;
369 pc->is_drawing = false;
371 /* Find desktop coordinates */
372 NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
374 /* Test whether we hit any anchor. */
375 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
376 revent.y));
378 switch (pc->state) {
379 case SP_PENCIL_CONTEXT_IDLE:
380 /* Releasing button in idle mode means single click */
381 /* We have already set up start point/anchor in button_press */
382 if (!(revent.state & GDK_CONTROL_MASK)) {
383 // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
384 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
385 }
386 ret = TRUE;
387 break;
388 case SP_PENCIL_CONTEXT_ADDLINE:
389 /* Finish segment now */
390 if (anchor) {
391 p = anchor->dp;
392 } else {
393 spdc_endpoint_snap(pc, p, revent.state);
394 }
395 pc->ea = anchor;
396 spdc_set_endpoint(pc, p);
397 spdc_finish_endpoint(pc);
398 pc->state = SP_PENCIL_CONTEXT_IDLE;
399 ret = TRUE;
400 break;
401 case SP_PENCIL_CONTEXT_FREEHAND:
402 /* Finish segment now */
403 /// \todo fixme: Clean up what follows (Lauris)
404 if (anchor) {
405 p = anchor->dp;
406 }
407 pc->ea = anchor;
408 /* Write curves to object */
410 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
412 spdc_concat_colors_and_flush(pc, FALSE);
413 pc->sa = NULL;
414 pc->ea = NULL;
415 if (pc->green_anchor) {
416 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
417 }
418 pc->state = SP_PENCIL_CONTEXT_IDLE;
419 ret = TRUE;
420 break;
421 default:
422 break;
423 }
425 if (pc->grab) {
426 /* Release grab now */
427 sp_canvas_item_ungrab(pc->grab, revent.time);
428 pc->grab = NULL;
429 }
431 ret = TRUE;
432 }
433 return ret;
434 }
436 static void
437 pencil_cancel (SPPencilContext *const pc)
438 {
439 if (pc->grab) {
440 /* Release grab now */
441 sp_canvas_item_ungrab(pc->grab, 0);
442 pc->grab = NULL;
443 }
445 pc->is_drawing = false;
447 pc->state = SP_PENCIL_CONTEXT_IDLE;
449 pc->red_curve->reset();
450 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
451 while (pc->green_bpaths) {
452 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
453 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
454 }
455 pc->green_curve->reset();
456 if (pc->green_anchor) {
457 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
458 }
460 pc->_message_context->clear();
461 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
463 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
464 }
467 static gint
468 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
469 {
470 gint ret = FALSE;
471 switch (keyval) {
472 case GDK_Up:
473 case GDK_Down:
474 case GDK_KP_Up:
475 case GDK_KP_Down:
476 // Prevent the zoom field from activation.
477 if (!mod_ctrl_only(state)) {
478 ret = TRUE;
479 }
480 break;
481 case GDK_Escape:
482 if (pc->npoints != 0) {
483 // if drawing, cancel, otherwise pass it up for deselecting
484 if (pc->is_drawing) {
485 pencil_cancel (pc);
486 ret = TRUE;
487 }
488 }
489 break;
490 case GDK_z:
491 case GDK_Z:
492 if (mod_ctrl_only(state) && pc->npoints != 0) {
493 // if drawing, cancel, otherwise pass it up for undo
494 if (pc->is_drawing) {
495 pencil_cancel (pc);
496 ret = TRUE;
497 }
498 }
499 break;
500 case GDK_g:
501 case GDK_G:
502 if (mod_shift_only(state)) {
503 sp_selection_to_guides();
504 ret = true;
505 }
506 break;
507 default:
508 break;
509 }
510 return ret;
511 }
513 /**
514 * Reset points and set new starting point.
515 */
516 static void
517 spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
518 {
519 pc->npoints = 0;
520 pc->red_curve_is_valid = false;
521 if (in_svg_plane(p)) {
522 pc->p[pc->npoints++] = p;
523 }
524 }
526 /**
527 * Change moving endpoint position.
528 * <ul>
529 * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
530 * <li>Otherwise we snap freely to whatever attractors are available.
531 * </ul>
532 *
533 * Number of points is (re)set to 2 always, 2nd point is modified.
534 * We change RED curve.
535 */
536 static void
537 spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
538 {
539 if (pc->npoints == 0) {
540 return;
541 /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
542 * zoom setting).
543 */
544 }
545 g_return_if_fail( pc->npoints > 0 );
547 pc->red_curve->reset();
548 if ( ( p == pc->p[0] )
549 || !in_svg_plane(p) )
550 {
551 pc->npoints = 1;
552 } else {
553 pc->p[1] = p;
554 pc->npoints = 2;
556 pc->red_curve->moveto(pc->p[0]);
557 pc->red_curve->lineto(pc->p[1]);
558 pc->red_curve_is_valid = true;
560 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
561 }
562 }
564 /**
565 * Finalize addline.
566 *
567 * \todo
568 * fixme: I'd like remove red reset from concat colors (lauris).
569 * Still not sure, how it will make most sense.
570 */
571 static void
572 spdc_finish_endpoint(SPPencilContext *const pc)
573 {
574 if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
575 || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
576 SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3) ) )
577 {
578 pc->red_curve->reset();
579 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
580 } else {
581 /* Write curves to object. */
582 spdc_concat_colors_and_flush(pc, FALSE);
583 pc->sa = NULL;
584 pc->ea = NULL;
585 }
586 }
588 static void
589 spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint /*state*/)
590 {
591 g_assert( pc->npoints > 0 );
592 g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
594 if ( ( p != pc->p[ pc->npoints - 1 ] )
595 && in_svg_plane(p) )
596 {
597 pc->p[pc->npoints++] = p;
598 fit_and_split(pc);
599 }
600 }
602 static inline double
603 square(double const x)
604 {
605 return x * x;
606 }
608 static void
609 fit_and_split(SPPencilContext *pc)
610 {
611 g_assert( pc->npoints > 1 );
613 double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
614 * prefs_get_double_attribute_limited("tools.freehand.pencil",
615 "tolerance", 10.0, 1.0, 100.0) );
617 NR::Point b[4];
618 g_assert(is_zero(pc->req_tangent)
619 || is_unit_vector(pc->req_tangent));
620 NR::Point const tHatEnd(0, 0);
621 int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
622 pc->req_tangent, tHatEnd, tolerance_sq, 1);
623 if ( n_segs > 0
624 && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
625 {
626 /* Fit and draw and reset state */
627 pc->red_curve->reset();
628 pc->red_curve->moveto(b[0]);
629 pc->red_curve->curveto(b[1], b[2], b[3]);
630 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
631 pc->red_curve_is_valid = true;
632 } else {
633 /* Fit and draw and copy last point */
635 g_assert(!pc->red_curve->is_empty());
637 /* Set up direction of next curve. */
638 {
639 NArtBpath const &last_seg = *pc->red_curve->last_bpath();
640 pc->p[0] = last_seg.c(3);
641 pc->npoints = 1;
642 g_assert( last_seg.code == NR_CURVETO );
643 /* Relevance: validity of last_seg.c(2). */
644 NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
645 pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
646 ? NR::Point(0, 0)
647 : NR::unit_vector(req_vec) );
648 }
650 pc->green_curve->append_continuous(pc->red_curve, 0.0625);
651 SPCurve *curve = pc->red_curve->copy();
653 /// \todo fixme:
654 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
655 curve->unref();
656 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
658 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
660 pc->red_curve_is_valid = false;
661 }
662 }
665 /*
666 Local Variables:
667 mode:c++
668 c-file-style:"stroustrup"
669 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
670 indent-tabs-mode:nil
671 fill-column:99
672 End:
673 */
674 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :