691dcb0986c7e3917a5901cb8b6a2a89ed9b59ac
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"
45 static void sp_pencil_context_class_init(SPPencilContextClass *klass);
46 static void sp_pencil_context_init(SPPencilContext *pc);
47 static void sp_pencil_context_setup(SPEventContext *ec);
48 static void sp_pencil_context_dispose(GObject *object);
50 static gint sp_pencil_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static gint pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent);
52 static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent);
53 static gint pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent);
54 static gint pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state);
56 static void spdc_set_startpoint(SPPencilContext *pc, NR::Point const p);
57 static void spdc_set_endpoint(SPPencilContext *pc, NR::Point const p);
58 static void spdc_finish_endpoint(SPPencilContext *pc);
59 static void spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state);
60 static void fit_and_split(SPPencilContext *pc);
63 static SPDrawContextClass *pencil_parent_class;
65 /**
66 * Register SPPencilContext class with Gdk and return its type number.
67 */
68 GType
69 sp_pencil_context_get_type()
70 {
71 static GType type = 0;
72 if (!type) {
73 GTypeInfo info = {
74 sizeof(SPPencilContextClass),
75 NULL, NULL,
76 (GClassInitFunc) sp_pencil_context_class_init,
77 NULL, NULL,
78 sizeof(SPPencilContext),
79 4,
80 (GInstanceInitFunc) sp_pencil_context_init,
81 NULL, /* value_table */
82 };
83 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPencilContext", &info, (GTypeFlags)0);
84 }
85 return type;
86 }
88 /**
89 * Initialize SPPencilContext vtable.
90 */
91 static void
92 sp_pencil_context_class_init(SPPencilContextClass *klass)
93 {
94 GObjectClass *object_class;
95 SPEventContextClass *event_context_class;
97 object_class = (GObjectClass *) klass;
98 event_context_class = (SPEventContextClass *) klass;
100 pencil_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
102 object_class->dispose = sp_pencil_context_dispose;
104 event_context_class->setup = sp_pencil_context_setup;
105 event_context_class->root_handler = sp_pencil_context_root_handler;
106 }
108 /**
109 * Callback to initialize SPPencilContext object.
110 */
111 static void
112 sp_pencil_context_init(SPPencilContext *pc)
113 {
114 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
116 event_context->cursor_shape = cursor_pencil_xpm;
117 event_context->hot_x = 4;
118 event_context->hot_y = 4;
120 pc->npoints = 0;
121 pc->state = SP_PENCIL_CONTEXT_IDLE;
122 pc->req_tangent = NR::Point(0, 0);
123 }
125 /**
126 * Callback to setup SPPencilContext object.
127 */
128 static void
129 sp_pencil_context_setup(SPEventContext *ec)
130 {
131 if (prefs_get_int_attribute("tools.freehand.pencil", "selcue", 0) != 0) {
132 ec->enableSelectionCue();
133 }
135 if (((SPEventContextClass *) pencil_parent_class)->setup) {
136 ((SPEventContextClass *) pencil_parent_class)->setup(ec);
137 }
139 SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
140 pc->is_drawing = false;
142 pc->anchor_statusbar = false;
143 }
145 static void
146 sp_pencil_context_dispose(GObject *object)
147 {
148 G_OBJECT_CLASS(pencil_parent_class)->dispose(object);
149 }
151 /** Snaps new node relative to the previous node. */
152 static void
153 spdc_endpoint_snap(SPPencilContext const *pc, NR::Point &p, guint const state)
154 {
155 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
156 spdc_endpoint_snap_free(pc, p, state);
157 }
159 /**
160 * Callback for handling all pencil context events.
161 */
162 gint
163 sp_pencil_context_root_handler(SPEventContext *const ec, GdkEvent *event)
164 {
165 SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
167 gint ret = FALSE;
169 switch (event->type) {
170 case GDK_BUTTON_PRESS:
171 ret = pencil_handle_button_press(pc, event->button);
172 break;
174 case GDK_MOTION_NOTIFY:
175 ret = pencil_handle_motion_notify(pc, event->motion);
176 break;
178 case GDK_BUTTON_RELEASE:
179 ret = pencil_handle_button_release(pc, event->button);
180 break;
182 case GDK_KEY_PRESS:
183 ret = pencil_handle_key_press(pc, get_group0_keyval (&event->key), event->key.state);
184 break;
186 default:
187 break;
188 }
190 if (!ret) {
191 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
192 = ((SPEventContextClass *) pencil_parent_class)->root_handler;
193 if (parent_root_handler) {
194 ret = parent_root_handler(ec, event);
195 }
196 }
198 return ret;
199 }
201 static gint
202 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
203 {
204 gint ret = FALSE;
205 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
206 if ( bevent.button == 1 && !event_context->space_panning) {
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 (bevent.state & GDK_CONTROL_MASK) {
232 freehand_create_single_dot(event_context, p, "tools.freehand.pencil", bevent.state & GDK_SHIFT_MASK);
233 ret = true;
234 break;
235 }
236 if (anchor) {
237 p = anchor->dp;
238 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
239 } else {
241 if (!(bevent.state & GDK_SHIFT_MASK)) {
243 // This is the first click of a new curve; deselect item so that
244 // this curve is not combined with it (unless it is drawn from its
245 // anchor, which is handled by the sibling branch above)
246 selection->clear();
247 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
248 SnapManager const &m = desktop->namedview->snap_manager;
249 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
250 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
251 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
252 }
253 }
254 pc->sa = anchor;
255 spdc_set_startpoint(pc, p);
256 ret = TRUE;
257 break;
258 }
260 pc->is_drawing = true;
261 }
262 return ret;
263 }
265 static gint
266 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
267 {
268 if (mevent.state & GDK_CONTROL_MASK) {
269 // mouse was accidentally moved during Ctrl+click;
270 // ignore the motion and create a single point
271 return TRUE;
272 }
273 gint ret = FALSE;
274 SPDesktop *const dt = pc->desktop;
276 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
277 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
278 // allow scrolling
279 return FALSE;
280 }
282 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
283 /* Grab mouse, so release will not pass unnoticed */
284 pc->grab = SP_CANVAS_ITEM(dt->acetate);
285 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
286 GDK_BUTTON_RELEASE_MASK |
287 GDK_POINTER_MOTION_MASK ),
288 NULL, mevent.time);
289 }
291 /* Find desktop coordinates */
292 NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
294 /* Test whether we hit any anchor. */
295 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
297 switch (pc->state) {
298 case SP_PENCIL_CONTEXT_ADDLINE:
299 /* Set red endpoint */
300 if (anchor) {
301 p = anchor->dp;
302 } else {
303 spdc_endpoint_snap(pc, p, mevent.state);
304 }
305 spdc_set_endpoint(pc, p);
306 ret = TRUE;
307 break;
308 default:
309 /* We may be idle or already freehand */
310 if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
311 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
312 if ( !pc->sa && !pc->green_anchor ) {
313 /* Create green anchor */
314 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
315 }
316 /** \todo
317 * fixme: I am not sure whether we want to snap to anchors
318 * in middle of freehand (Lauris)
319 */
320 if (anchor) {
321 p = anchor->dp;
322 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
323 SnapManager const &m = dt->namedview->snap_manager;
324 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
325 }
326 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
327 spdc_add_freehand_point(pc, p, mevent.state);
328 ret = TRUE;
329 }
331 if (anchor && !pc->anchor_statusbar) {
332 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
333 pc->anchor_statusbar = true;
334 } else if (!anchor && pc->anchor_statusbar) {
335 pc->_message_context->clear();
336 pc->anchor_statusbar = false;
337 } else if (!anchor) {
338 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
339 }
341 } else {
342 if (anchor && !pc->anchor_statusbar) {
343 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
344 pc->anchor_statusbar = true;
345 } else if (!anchor && pc->anchor_statusbar) {
346 pc->_message_context->clear();
347 pc->anchor_statusbar = false;
348 }
349 }
350 break;
351 }
352 return ret;
353 }
355 static gint
356 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
357 {
358 gint ret = FALSE;
360 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
361 if ( revent.button == 1 && pc->is_drawing && !event_context->space_panning) {
362 SPDesktop *const dt = pc->desktop;
364 pc->is_drawing = false;
366 /* Find desktop coordinates */
367 NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
369 /* Test whether we hit any anchor. */
370 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
371 revent.y));
373 switch (pc->state) {
374 case SP_PENCIL_CONTEXT_IDLE:
375 /* Releasing button in idle mode means single click */
376 /* We have already set up start point/anchor in button_press */
377 if (!(revent.state & GDK_CONTROL_MASK)) {
378 // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
379 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
380 }
381 ret = TRUE;
382 break;
383 case SP_PENCIL_CONTEXT_ADDLINE:
384 /* Finish segment now */
385 if (anchor) {
386 p = anchor->dp;
387 } else {
388 spdc_endpoint_snap(pc, p, revent.state);
389 }
390 pc->ea = anchor;
391 spdc_set_endpoint(pc, p);
392 spdc_finish_endpoint(pc);
393 pc->state = SP_PENCIL_CONTEXT_IDLE;
394 ret = TRUE;
395 break;
396 case SP_PENCIL_CONTEXT_FREEHAND:
397 /* Finish segment now */
398 /// \todo fixme: Clean up what follows (Lauris)
399 if (anchor) {
400 p = anchor->dp;
401 }
402 pc->ea = anchor;
403 /* Write curves to object */
405 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
407 spdc_concat_colors_and_flush(pc, FALSE);
408 pc->sa = NULL;
409 pc->ea = NULL;
410 if (pc->green_anchor) {
411 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
412 }
413 pc->state = SP_PENCIL_CONTEXT_IDLE;
414 ret = TRUE;
415 break;
416 default:
417 break;
418 }
420 if (pc->grab) {
421 /* Release grab now */
422 sp_canvas_item_ungrab(pc->grab, revent.time);
423 pc->grab = NULL;
424 }
426 ret = TRUE;
427 }
428 return ret;
429 }
431 static void
432 pencil_cancel (SPPencilContext *const pc)
433 {
434 if (pc->grab) {
435 /* Release grab now */
436 sp_canvas_item_ungrab(pc->grab, 0);
437 pc->grab = NULL;
438 }
440 pc->is_drawing = false;
442 pc->state = SP_PENCIL_CONTEXT_IDLE;
444 sp_curve_reset(pc->red_curve);
445 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
446 while (pc->green_bpaths) {
447 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
448 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
449 }
450 sp_curve_reset(pc->green_curve);
451 if (pc->green_anchor) {
452 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
453 }
455 pc->_message_context->clear();
456 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
458 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
459 }
462 static gint
463 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
464 {
465 gint ret = FALSE;
466 switch (keyval) {
467 case GDK_Up:
468 case GDK_Down:
469 case GDK_KP_Up:
470 case GDK_KP_Down:
471 // Prevent the zoom field from activation.
472 if (!mod_ctrl_only(state)) {
473 ret = TRUE;
474 }
475 break;
476 case GDK_Escape:
477 if (pc->npoints != 0) {
478 // if drawing, cancel, otherwise pass it up for deselecting
479 if (pc->is_drawing) {
480 pencil_cancel (pc);
481 ret = TRUE;
482 }
483 }
484 break;
485 case GDK_z:
486 case GDK_Z:
487 if (mod_ctrl_only(state) && pc->npoints != 0) {
488 // if drawing, cancel, otherwise pass it up for undo
489 if (pc->is_drawing) {
490 pencil_cancel (pc);
491 ret = TRUE;
492 }
493 }
494 break;
495 case GDK_g:
496 case GDK_G:
497 if (mod_shift_only(state)) {
498 sp_selection_to_guides();
499 ret = true;
500 }
501 break;
502 default:
503 break;
504 }
505 return ret;
506 }
508 /**
509 * Reset points and set new starting point.
510 */
511 static void
512 spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
513 {
514 pc->npoints = 0;
515 pc->red_curve_is_valid = false;
516 if (in_svg_plane(p)) {
517 pc->p[pc->npoints++] = p;
518 }
519 }
521 /**
522 * Change moving endpoint position.
523 * <ul>
524 * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
525 * <li>Otherwise we snap freely to whatever attractors are available.
526 * </ul>
527 *
528 * Number of points is (re)set to 2 always, 2nd point is modified.
529 * We change RED curve.
530 */
531 static void
532 spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
533 {
534 if (pc->npoints == 0) {
535 return;
536 /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
537 * zoom setting).
538 */
539 }
540 g_return_if_fail( pc->npoints > 0 );
542 sp_curve_reset(pc->red_curve);
543 if ( ( p == pc->p[0] )
544 || !in_svg_plane(p) )
545 {
546 pc->npoints = 1;
547 } else {
548 pc->p[1] = p;
549 pc->npoints = 2;
551 sp_curve_moveto(pc->red_curve, pc->p[0]);
552 sp_curve_lineto(pc->red_curve, pc->p[1]);
553 pc->red_curve_is_valid = true;
555 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
556 }
557 }
559 /**
560 * Finalize addline.
561 *
562 * \todo
563 * fixme: I'd like remove red reset from concat colors (lauris).
564 * Still not sure, how it will make most sense.
565 */
566 static void
567 spdc_finish_endpoint(SPPencilContext *const pc)
568 {
569 if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
570 || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
571 SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3) ) )
572 {
573 sp_curve_reset(pc->red_curve);
574 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
575 } else {
576 /* Write curves to object. */
577 spdc_concat_colors_and_flush(pc, FALSE);
578 pc->sa = NULL;
579 pc->ea = NULL;
580 }
581 }
583 static void
584 spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint /*state*/)
585 {
586 g_assert( pc->npoints > 0 );
587 g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
589 if ( ( p != pc->p[ pc->npoints - 1 ] )
590 && in_svg_plane(p) )
591 {
592 pc->p[pc->npoints++] = p;
593 fit_and_split(pc);
594 }
595 }
597 static inline double
598 square(double const x)
599 {
600 return x * x;
601 }
603 static void
604 fit_and_split(SPPencilContext *pc)
605 {
606 g_assert( pc->npoints > 1 );
608 double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
609 * prefs_get_double_attribute_limited("tools.freehand.pencil",
610 "tolerance", 10.0, 1.0, 100.0) );
612 NR::Point b[4];
613 g_assert(is_zero(pc->req_tangent)
614 || is_unit_vector(pc->req_tangent));
615 NR::Point const tHatEnd(0, 0);
616 int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
617 pc->req_tangent, tHatEnd, tolerance_sq, 1);
618 if ( n_segs > 0
619 && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
620 {
621 /* Fit and draw and reset state */
622 sp_curve_reset(pc->red_curve);
623 sp_curve_moveto(pc->red_curve, b[0]);
624 sp_curve_curveto(pc->red_curve, b[1], b[2], b[3]);
625 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
626 pc->red_curve_is_valid = true;
627 } else {
628 /* Fit and draw and copy last point */
630 g_assert(!sp_curve_empty(pc->red_curve));
632 /* Set up direction of next curve. */
633 {
634 NArtBpath const &last_seg = *sp_curve_last_bpath(pc->red_curve);
635 pc->p[0] = last_seg.c(3);
636 pc->npoints = 1;
637 g_assert( last_seg.code == NR_CURVETO );
638 /* Relevance: validity of last_seg.c(2). */
639 NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
640 pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
641 ? NR::Point(0, 0)
642 : NR::unit_vector(req_vec) );
643 }
645 sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
646 SPCurve *curve = sp_curve_copy(pc->red_curve);
648 /// \todo fixme:
649 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
650 sp_curve_unref(curve);
651 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
653 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
655 pc->red_curve_is_valid = false;
656 }
657 }
660 /*
661 Local Variables:
662 mode:c++
663 c-file-style:"stroustrup"
664 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
665 indent-tabs-mode:nil
666 fill-column:99
667 End:
668 */
669 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :