6bd4cd0b3fb5f2f398b5310d30d1f326b08b7659
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);
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) &&
242 !(bevent.state & GDK_LOCK_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 const &m = desktop->namedview->snap_manager;
250 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
251 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
252 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
253 }
254 }
255 pc->sa = anchor;
256 spdc_set_startpoint(pc, p);
257 ret = TRUE;
258 break;
259 }
261 pc->is_drawing = true;
262 }
263 return ret;
264 }
266 static gint
267 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
268 {
269 if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) {
270 // mouse was accidentally moved during Ctrl+click;
271 // ignore the motion and create a single point
272 pc->is_drawing = false;
273 return TRUE;
274 }
275 gint ret = FALSE;
276 SPDesktop *const dt = pc->desktop;
278 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
279 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
280 // allow scrolling
281 return FALSE;
282 }
284 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
285 /* Grab mouse, so release will not pass unnoticed */
286 pc->grab = SP_CANVAS_ITEM(dt->acetate);
287 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
288 GDK_BUTTON_RELEASE_MASK |
289 GDK_POINTER_MOTION_MASK ),
290 NULL, mevent.time);
291 }
293 /* Find desktop coordinates */
294 NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
296 /* Test whether we hit any anchor. */
297 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
299 switch (pc->state) {
300 case SP_PENCIL_CONTEXT_ADDLINE:
301 /* Set red endpoint */
302 if (anchor) {
303 p = anchor->dp;
304 } else {
305 spdc_endpoint_snap(pc, p, mevent.state);
306 }
307 spdc_set_endpoint(pc, p);
308 ret = TRUE;
309 break;
310 default:
311 /* We may be idle or already freehand */
312 if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
313 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
314 if ( !pc->sa && !pc->green_anchor ) {
315 /* Create green anchor */
316 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
317 }
318 /** \todo
319 * fixme: I am not sure whether we want to snap to anchors
320 * in middle of freehand (Lauris)
321 */
322 if (anchor) {
323 p = anchor->dp;
324 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
325 SnapManager const &m = dt->namedview->snap_manager;
326 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
327 }
328 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
329 spdc_add_freehand_point(pc, p, mevent.state);
330 ret = TRUE;
331 }
333 if (anchor && !pc->anchor_statusbar) {
334 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
335 pc->anchor_statusbar = true;
336 } else if (!anchor && pc->anchor_statusbar) {
337 pc->_message_context->clear();
338 pc->anchor_statusbar = false;
339 } else if (!anchor) {
340 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
341 }
343 } else {
344 if (anchor && !pc->anchor_statusbar) {
345 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
346 pc->anchor_statusbar = true;
347 } else if (!anchor && pc->anchor_statusbar) {
348 pc->_message_context->clear();
349 pc->anchor_statusbar = false;
350 }
351 }
352 break;
353 }
354 return ret;
355 }
357 static gint
358 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
359 {
360 gint ret = FALSE;
362 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
363 if ( revent.button == 1 && pc->is_drawing && !event_context->space_panning) {
364 SPDesktop *const dt = pc->desktop;
366 pc->is_drawing = false;
368 /* Find desktop coordinates */
369 NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
371 /* Test whether we hit any anchor. */
372 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
373 revent.y));
375 switch (pc->state) {
376 case SP_PENCIL_CONTEXT_IDLE:
377 /* Releasing button in idle mode means single click */
378 /* We have already set up start point/anchor in button_press */
379 if (!(revent.state & GDK_CONTROL_MASK)) {
380 // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
381 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
382 }
383 ret = TRUE;
384 break;
385 case SP_PENCIL_CONTEXT_ADDLINE:
386 /* Finish segment now */
387 if (anchor) {
388 p = anchor->dp;
389 } else {
390 spdc_endpoint_snap(pc, p, revent.state);
391 }
392 pc->ea = anchor;
393 spdc_set_endpoint(pc, p);
394 spdc_finish_endpoint(pc);
395 pc->state = SP_PENCIL_CONTEXT_IDLE;
396 ret = TRUE;
397 break;
398 case SP_PENCIL_CONTEXT_FREEHAND:
399 /* Finish segment now */
400 /// \todo fixme: Clean up what follows (Lauris)
401 if (anchor) {
402 p = anchor->dp;
403 }
404 pc->ea = anchor;
405 /* Write curves to object */
407 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
409 spdc_concat_colors_and_flush(pc, FALSE);
410 pc->sa = NULL;
411 pc->ea = NULL;
412 if (pc->green_anchor) {
413 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
414 }
415 pc->state = SP_PENCIL_CONTEXT_IDLE;
416 ret = TRUE;
417 break;
418 default:
419 break;
420 }
422 if (pc->grab) {
423 /* Release grab now */
424 sp_canvas_item_ungrab(pc->grab, revent.time);
425 pc->grab = NULL;
426 }
428 ret = TRUE;
429 }
430 return ret;
431 }
433 static void
434 pencil_cancel (SPPencilContext *const pc)
435 {
436 if (pc->grab) {
437 /* Release grab now */
438 sp_canvas_item_ungrab(pc->grab, 0);
439 pc->grab = NULL;
440 }
442 pc->is_drawing = false;
444 pc->state = SP_PENCIL_CONTEXT_IDLE;
446 sp_curve_reset(pc->red_curve);
447 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
448 while (pc->green_bpaths) {
449 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
450 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
451 }
452 sp_curve_reset(pc->green_curve);
453 if (pc->green_anchor) {
454 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
455 }
457 pc->_message_context->clear();
458 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
460 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
461 }
464 static gint
465 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
466 {
467 gint ret = FALSE;
468 switch (keyval) {
469 case GDK_Up:
470 case GDK_Down:
471 case GDK_KP_Up:
472 case GDK_KP_Down:
473 // Prevent the zoom field from activation.
474 if (!mod_ctrl_only(state)) {
475 ret = TRUE;
476 }
477 break;
478 case GDK_Escape:
479 if (pc->npoints != 0) {
480 // if drawing, cancel, otherwise pass it up for deselecting
481 if (pc->is_drawing) {
482 pencil_cancel (pc);
483 ret = TRUE;
484 }
485 }
486 break;
487 case GDK_z:
488 case GDK_Z:
489 if (mod_ctrl_only(state) && pc->npoints != 0) {
490 // if drawing, cancel, otherwise pass it up for undo
491 if (pc->is_drawing) {
492 pencil_cancel (pc);
493 ret = TRUE;
494 }
495 }
496 break;
497 case GDK_g:
498 case GDK_G:
499 if (mod_shift_only(state)) {
500 sp_selection_to_guides();
501 ret = true;
502 }
503 break;
504 default:
505 break;
506 }
507 return ret;
508 }
510 /**
511 * Reset points and set new starting point.
512 */
513 static void
514 spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
515 {
516 pc->npoints = 0;
517 pc->red_curve_is_valid = false;
518 if (in_svg_plane(p)) {
519 pc->p[pc->npoints++] = p;
520 }
521 }
523 /**
524 * Change moving endpoint position.
525 * <ul>
526 * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
527 * <li>Otherwise we snap freely to whatever attractors are available.
528 * </ul>
529 *
530 * Number of points is (re)set to 2 always, 2nd point is modified.
531 * We change RED curve.
532 */
533 static void
534 spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
535 {
536 if (pc->npoints == 0) {
537 return;
538 /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
539 * zoom setting).
540 */
541 }
542 g_return_if_fail( pc->npoints > 0 );
544 sp_curve_reset(pc->red_curve);
545 if ( ( p == pc->p[0] )
546 || !in_svg_plane(p) )
547 {
548 pc->npoints = 1;
549 } else {
550 pc->p[1] = p;
551 pc->npoints = 2;
553 sp_curve_moveto(pc->red_curve, pc->p[0]);
554 sp_curve_lineto(pc->red_curve, pc->p[1]);
555 pc->red_curve_is_valid = true;
557 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
558 }
559 }
561 /**
562 * Finalize addline.
563 *
564 * \todo
565 * fixme: I'd like remove red reset from concat colors (lauris).
566 * Still not sure, how it will make most sense.
567 */
568 static void
569 spdc_finish_endpoint(SPPencilContext *const pc)
570 {
571 if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
572 || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
573 SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3) ) )
574 {
575 sp_curve_reset(pc->red_curve);
576 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
577 } else {
578 /* Write curves to object. */
579 spdc_concat_colors_and_flush(pc, FALSE);
580 pc->sa = NULL;
581 pc->ea = NULL;
582 }
583 }
585 static void
586 spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint /*state*/)
587 {
588 g_assert( pc->npoints > 0 );
589 g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
591 if ( ( p != pc->p[ pc->npoints - 1 ] )
592 && in_svg_plane(p) )
593 {
594 pc->p[pc->npoints++] = p;
595 fit_and_split(pc);
596 }
597 }
599 static inline double
600 square(double const x)
601 {
602 return x * x;
603 }
605 static void
606 fit_and_split(SPPencilContext *pc)
607 {
608 g_assert( pc->npoints > 1 );
610 double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
611 * prefs_get_double_attribute_limited("tools.freehand.pencil",
612 "tolerance", 10.0, 1.0, 100.0) );
614 NR::Point b[4];
615 g_assert(is_zero(pc->req_tangent)
616 || is_unit_vector(pc->req_tangent));
617 NR::Point const tHatEnd(0, 0);
618 int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
619 pc->req_tangent, tHatEnd, tolerance_sq, 1);
620 if ( n_segs > 0
621 && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
622 {
623 /* Fit and draw and reset state */
624 sp_curve_reset(pc->red_curve);
625 sp_curve_moveto(pc->red_curve, b[0]);
626 sp_curve_curveto(pc->red_curve, b[1], b[2], b[3]);
627 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
628 pc->red_curve_is_valid = true;
629 } else {
630 /* Fit and draw and copy last point */
632 g_assert(!sp_curve_empty(pc->red_curve));
634 /* Set up direction of next curve. */
635 {
636 NArtBpath const &last_seg = *sp_curve_last_bpath(pc->red_curve);
637 pc->p[0] = last_seg.c(3);
638 pc->npoints = 1;
639 g_assert( last_seg.code == NR_CURVETO );
640 /* Relevance: validity of last_seg.c(2). */
641 NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
642 pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
643 ? NR::Point(0, 0)
644 : NR::unit_vector(req_vec) );
645 }
647 sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
648 SPCurve *curve = sp_curve_copy(pc->red_curve);
650 /// \todo fixme:
651 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
652 sp_curve_unref(curve);
653 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
655 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
657 pc->red_curve_is_valid = false;
658 }
659 }
662 /*
663 Local Variables:
664 mode:c++
665 c-file-style:"stroustrup"
666 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
667 indent-tabs-mode:nil
668 fill-column:99
669 End:
670 */
671 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :