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;
101 }
103 /**
104 * Callback to initialize SPPencilContext object.
105 */
106 static void
107 sp_pencil_context_init(SPPencilContext *pc)
108 {
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);
118 }
120 /**
121 * Callback to setup SPPencilContext object.
122 */
123 static void
124 sp_pencil_context_setup(SPEventContext *ec)
125 {
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;
138 }
140 static void
141 sp_pencil_context_dispose(GObject *object)
142 {
143 G_OBJECT_CLASS(pencil_parent_class)->dispose(object);
144 }
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)
149 {
150 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
151 spdc_endpoint_snap_free(pc, p, state);
152 }
154 /**
155 * Callback for handling all pencil context events.
156 */
157 gint
158 sp_pencil_context_root_handler(SPEventContext *const ec, GdkEvent *event)
159 {
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;
194 }
196 static gint
197 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
198 {
199 gint ret = FALSE;
200 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
201 if ( bevent.button == 1 && !event_context->space_panning) {
203 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
204 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
205 Inkscape::Selection *selection = sp_desktop_selection(desktop);
207 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
208 return TRUE;
209 }
211 NR::Point const button_w(bevent.x, bevent.y);
213 /* Find desktop coordinates */
214 NR::Point p = pc->desktop->w2d(button_w);
216 /* Test whether we hit any anchor. */
217 SPDrawAnchor *anchor = spdc_test_inside(pc, button_w);
219 switch (pc->state) {
220 case SP_PENCIL_CONTEXT_ADDLINE:
221 /* Current segment will be finished with release */
222 ret = TRUE;
223 break;
224 default:
225 /* Set first point of sequence */
226 if (anchor) {
227 p = anchor->dp;
228 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
229 } else {
231 if (!(bevent.state & GDK_SHIFT_MASK)) {
233 // This is the first click of a new curve; deselect item so that
234 // this curve is not combined with it (unless it is drawn from its
235 // anchor, which is handled by the sibling branch above)
236 selection->clear();
237 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
238 SnapManager const &m = desktop->namedview->snap_manager;
239 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
240 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
241 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
242 }
243 }
244 pc->sa = anchor;
245 spdc_set_startpoint(pc, p);
246 ret = TRUE;
247 break;
248 }
250 pc->is_drawing = true;
251 }
252 return ret;
253 }
255 static gint
256 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
257 {
258 gint ret = FALSE;
259 SPDesktop *const dt = pc->desktop;
261 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
262 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
263 // allow scrolling
264 return FALSE;
265 }
267 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
268 /* Grab mouse, so release will not pass unnoticed */
269 pc->grab = SP_CANVAS_ITEM(dt->acetate);
270 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
271 GDK_BUTTON_RELEASE_MASK |
272 GDK_POINTER_MOTION_MASK ),
273 NULL, mevent.time);
274 }
276 /* Find desktop coordinates */
277 NR::Point p = dt->w2d(NR::Point(mevent.x, mevent.y));
279 /* Test whether we hit any anchor. */
280 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(mevent.x, mevent.y));
282 switch (pc->state) {
283 case SP_PENCIL_CONTEXT_ADDLINE:
284 /* Set red endpoint */
285 if (anchor) {
286 p = anchor->dp;
287 } else {
288 spdc_endpoint_snap(pc, p, mevent.state);
289 }
290 spdc_set_endpoint(pc, p);
291 ret = TRUE;
292 break;
293 default:
294 /* We may be idle or already freehand */
295 if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
296 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
297 if ( !pc->sa && !pc->green_anchor ) {
298 /* Create green anchor */
299 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
300 }
301 /** \todo
302 * fixme: I am not sure whether we want to snap to anchors
303 * in middle of freehand (Lauris)
304 */
305 if (anchor) {
306 p = anchor->dp;
307 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
308 SnapManager const &m = dt->namedview->snap_manager;
309 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
310 }
311 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
312 spdc_add_freehand_point(pc, p, mevent.state);
313 ret = TRUE;
314 }
316 if (anchor && !pc->anchor_statusbar) {
317 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
318 pc->anchor_statusbar = true;
319 } else if (!anchor && pc->anchor_statusbar) {
320 pc->_message_context->clear();
321 pc->anchor_statusbar = false;
322 } else if (!anchor) {
323 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
324 }
326 } else {
327 if (anchor && !pc->anchor_statusbar) {
328 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
329 pc->anchor_statusbar = true;
330 } else if (!anchor && pc->anchor_statusbar) {
331 pc->_message_context->clear();
332 pc->anchor_statusbar = false;
333 }
334 }
335 break;
336 }
337 return ret;
338 }
340 static gint
341 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
342 {
343 gint ret = FALSE;
345 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
346 if ( revent.button == 1 && pc->is_drawing && !event_context->space_panning) {
347 SPDesktop *const dt = pc->desktop;
349 pc->is_drawing = false;
351 /* Find desktop coordinates */
352 NR::Point p = dt->w2d(NR::Point(revent.x, revent.y));
354 /* Test whether we hit any anchor. */
355 SPDrawAnchor *anchor = spdc_test_inside(pc, NR::Point(revent.x,
356 revent.y));
358 switch (pc->state) {
359 case SP_PENCIL_CONTEXT_IDLE:
360 /* Releasing button in idle mode means single click */
361 /* We have already set up start point/anchor in button_press */
362 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
363 ret = TRUE;
364 break;
365 case SP_PENCIL_CONTEXT_ADDLINE:
366 /* Finish segment now */
367 if (anchor) {
368 p = anchor->dp;
369 } else {
370 spdc_endpoint_snap(pc, p, revent.state);
371 }
372 pc->ea = anchor;
373 spdc_set_endpoint(pc, p);
374 spdc_finish_endpoint(pc);
375 pc->state = SP_PENCIL_CONTEXT_IDLE;
376 ret = TRUE;
377 break;
378 case SP_PENCIL_CONTEXT_FREEHAND:
379 /* Finish segment now */
380 /// \todo fixme: Clean up what follows (Lauris)
381 if (anchor) {
382 p = anchor->dp;
383 }
384 pc->ea = anchor;
385 /* Write curves to object */
387 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
389 spdc_concat_colors_and_flush(pc, FALSE);
390 pc->sa = NULL;
391 pc->ea = NULL;
392 if (pc->green_anchor) {
393 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
394 }
395 pc->state = SP_PENCIL_CONTEXT_IDLE;
396 ret = TRUE;
397 break;
398 default:
399 break;
400 }
402 if (pc->grab) {
403 /* Release grab now */
404 sp_canvas_item_ungrab(pc->grab, revent.time);
405 pc->grab = NULL;
406 }
408 ret = TRUE;
409 }
410 return ret;
411 }
413 static void
414 pencil_cancel (SPPencilContext *const pc)
415 {
416 if (pc->grab) {
417 /* Release grab now */
418 sp_canvas_item_ungrab(pc->grab, 0);
419 pc->grab = NULL;
420 }
422 pc->is_drawing = false;
424 pc->state = SP_PENCIL_CONTEXT_IDLE;
426 sp_curve_reset(pc->red_curve);
427 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
428 while (pc->green_bpaths) {
429 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
430 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
431 }
432 sp_curve_reset(pc->green_curve);
433 if (pc->green_anchor) {
434 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
435 }
437 pc->_message_context->clear();
438 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
440 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
441 }
444 static gint
445 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
446 {
447 gint ret = FALSE;
448 switch (keyval) {
449 case GDK_Up:
450 case GDK_Down:
451 case GDK_KP_Up:
452 case GDK_KP_Down:
453 // Prevent the zoom field from activation.
454 if (!mod_ctrl_only(state)) {
455 ret = TRUE;
456 }
457 break;
458 case GDK_Escape:
459 if (pc->npoints != 0) {
460 // if drawing, cancel, otherwise pass it up for deselecting
461 if (pc->is_drawing) {
462 pencil_cancel (pc);
463 ret = TRUE;
464 }
465 }
466 break;
467 case GDK_z:
468 case GDK_Z:
469 if (mod_ctrl_only(state) && pc->npoints != 0) {
470 // if drawing, cancel, otherwise pass it up for undo
471 if (pc->is_drawing) {
472 pencil_cancel (pc);
473 ret = TRUE;
474 }
475 }
476 break;
477 default:
478 break;
479 }
480 return ret;
481 }
483 /**
484 * Reset points and set new starting point.
485 */
486 static void
487 spdc_set_startpoint(SPPencilContext *const pc, NR::Point const p)
488 {
489 pc->npoints = 0;
490 pc->red_curve_is_valid = false;
491 if (in_svg_plane(p)) {
492 pc->p[pc->npoints++] = p;
493 }
494 }
496 /**
497 * Change moving endpoint position.
498 * <ul>
499 * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
500 * <li>Otherwise we snap freely to whatever attractors are available.
501 * </ul>
502 *
503 * Number of points is (re)set to 2 always, 2nd point is modified.
504 * We change RED curve.
505 */
506 static void
507 spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
508 {
509 if (pc->npoints == 0) {
510 return;
511 /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
512 * zoom setting).
513 */
514 }
515 g_return_if_fail( pc->npoints > 0 );
517 sp_curve_reset(pc->red_curve);
518 if ( ( p == pc->p[0] )
519 || !in_svg_plane(p) )
520 {
521 pc->npoints = 1;
522 } else {
523 pc->p[1] = p;
524 pc->npoints = 2;
526 sp_curve_moveto(pc->red_curve, pc->p[0]);
527 sp_curve_lineto(pc->red_curve, pc->p[1]);
528 pc->red_curve_is_valid = true;
530 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
531 }
532 }
534 /**
535 * Finalize addline.
536 *
537 * \todo
538 * fixme: I'd like remove red reset from concat colors (lauris).
539 * Still not sure, how it will make most sense.
540 */
541 static void
542 spdc_finish_endpoint(SPPencilContext *const pc)
543 {
544 if ( ( SP_CURVE_LENGTH(pc->red_curve) != 2 )
545 || ( SP_CURVE_SEGMENT(pc->red_curve, 0)->c(3) ==
546 SP_CURVE_SEGMENT(pc->red_curve, 1)->c(3) ) )
547 {
548 sp_curve_reset(pc->red_curve);
549 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
550 } else {
551 /* Write curves to object. */
552 spdc_concat_colors_and_flush(pc, FALSE);
553 pc->sa = NULL;
554 pc->ea = NULL;
555 }
556 }
558 static void
559 spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state)
560 {
561 g_assert( pc->npoints > 0 );
562 g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
564 if ( ( p != pc->p[ pc->npoints - 1 ] )
565 && in_svg_plane(p) )
566 {
567 pc->p[pc->npoints++] = p;
568 fit_and_split(pc);
569 }
570 }
572 static inline double
573 square(double const x)
574 {
575 return x * x;
576 }
578 static void
579 fit_and_split(SPPencilContext *pc)
580 {
581 g_assert( pc->npoints > 1 );
583 double const tolerance_sq = square( NR::expansion(pc->desktop->w2d())
584 * prefs_get_double_attribute_limited("tools.freehand.pencil",
585 "tolerance", 10.0, 1.0, 100.0) );
587 NR::Point b[4];
588 g_assert(is_zero(pc->req_tangent)
589 || is_unit_vector(pc->req_tangent));
590 NR::Point const tHatEnd(0, 0);
591 int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
592 pc->req_tangent, tHatEnd, tolerance_sq, 1);
593 if ( n_segs > 0
594 && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
595 {
596 /* Fit and draw and reset state */
597 sp_curve_reset(pc->red_curve);
598 sp_curve_moveto(pc->red_curve, b[0]);
599 sp_curve_curveto(pc->red_curve, b[1], b[2], b[3]);
600 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
601 pc->red_curve_is_valid = true;
602 } else {
603 /* Fit and draw and copy last point */
605 g_assert(!sp_curve_empty(pc->red_curve));
607 /* Set up direction of next curve. */
608 {
609 NArtBpath const &last_seg = *sp_curve_last_bpath(pc->red_curve);
610 pc->p[0] = last_seg.c(3);
611 pc->npoints = 1;
612 g_assert( last_seg.code == NR_CURVETO );
613 /* Relevance: validity of last_seg.c(2). */
614 NR::Point const req_vec( pc->p[0] - last_seg.c(2) );
615 pc->req_tangent = ( ( NR::is_zero(req_vec) || !in_svg_plane(req_vec) )
616 ? NR::Point(0, 0)
617 : NR::unit_vector(req_vec) );
618 }
620 sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
621 SPCurve *curve = sp_curve_copy(pc->red_curve);
623 /// \todo fixme:
624 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
625 sp_curve_unref(curve);
626 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
628 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
630 pc->red_curve_is_valid = false;
631 }
632 }
635 /*
636 Local Variables:
637 mode:c++
638 c-file-style:"stroustrup"
639 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
640 indent-tabs-mode:nil
641 fill-column:99
642 End:
643 */
644 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :