1 /** \file
2 * Pen 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>
19 #include <cstring>
20 #include <string>
22 #include "pen-context.h"
23 #include "sp-namedview.h"
24 #include "sp-metrics.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "selection.h"
28 #include "selection-chemistry.h"
29 #include "draw-anchor.h"
30 #include "message-stack.h"
31 #include "message-context.h"
32 #include "prefs-utils.h"
33 #include "sp-path.h"
34 #include "display/curve.h"
35 #include "pixmaps/cursor-pen.xpm"
36 #include "display/canvas-bpath.h"
37 #include "display/sp-ctrlline.h"
38 #include "display/sodipodi-ctrl.h"
39 #include <glibmm/i18n.h>
40 #include "libnr/n-art-bpath.h"
41 #include "libnr/nr-point-ops.h"
42 #include "helper/units.h"
43 #include "macros.h"
44 #include "context-fns.h"
45 #include "live_effects/effect.h"
47 static void sp_pen_context_class_init(SPPenContextClass *klass);
48 static void sp_pen_context_init(SPPenContext *pc);
49 static void sp_pen_context_dispose(GObject *object);
51 static void sp_pen_context_setup(SPEventContext *ec);
52 static void sp_pen_context_finish(SPEventContext *ec);
53 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
54 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
55 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
57 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
58 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
59 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
60 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
62 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
64 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
65 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
66 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
67 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
68 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
69 static void spdc_reset_colors(SPPenContext *pc);
71 static void pen_disable_events(SPPenContext *const pc);
72 static void pen_enable_events(SPPenContext *const pc);
74 static NR::Point pen_drag_origin_w(0, 0);
75 static bool pen_within_tolerance = false;
77 static SPDrawContextClass *pen_parent_class;
79 /**
80 * Register SPPenContext with Gdk and return its type.
81 */
82 GType
83 sp_pen_context_get_type(void)
84 {
85 static GType type = 0;
86 if (!type) {
87 GTypeInfo info = {
88 sizeof(SPPenContextClass),
89 NULL, NULL,
90 (GClassInitFunc) sp_pen_context_class_init,
91 NULL, NULL,
92 sizeof(SPPenContext),
93 4,
94 (GInstanceInitFunc) sp_pen_context_init,
95 NULL, /* value_table */
96 };
97 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
98 }
99 return type;
100 }
102 /**
103 * Initialize the SPPenContext vtable.
104 */
105 static void
106 sp_pen_context_class_init(SPPenContextClass *klass)
107 {
108 GObjectClass *object_class;
109 SPEventContextClass *event_context_class;
111 object_class = (GObjectClass *) klass;
112 event_context_class = (SPEventContextClass *) klass;
114 pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
116 object_class->dispose = sp_pen_context_dispose;
118 event_context_class->setup = sp_pen_context_setup;
119 event_context_class->finish = sp_pen_context_finish;
120 event_context_class->set = sp_pen_context_set;
121 event_context_class->root_handler = sp_pen_context_root_handler;
122 event_context_class->item_handler = sp_pen_context_item_handler;
123 }
125 /**
126 * Callback to initialize SPPenContext object.
127 */
128 static void
129 sp_pen_context_init(SPPenContext *pc)
130 {
132 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
134 event_context->cursor_shape = cursor_pen_xpm;
135 event_context->hot_x = 4;
136 event_context->hot_y = 4;
138 pc->npoints = 0;
139 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
140 pc->state = SP_PEN_CONTEXT_POINT;
142 pc->c0 = NULL;
143 pc->c1 = NULL;
144 pc->cl0 = NULL;
145 pc->cl1 = NULL;
147 pc->events_disabled = 0;
149 pc->polylines_only = false;
150 }
152 /**
153 * Callback to destroy the SPPenContext object's members and itself.
154 */
155 static void
156 sp_pen_context_dispose(GObject *object)
157 {
158 SPPenContext *pc;
160 pc = SP_PEN_CONTEXT(object);
162 if (pc->c0) {
163 gtk_object_destroy(GTK_OBJECT(pc->c0));
164 pc->c0 = NULL;
165 }
166 if (pc->c1) {
167 gtk_object_destroy(GTK_OBJECT(pc->c1));
168 pc->c1 = NULL;
169 }
170 if (pc->cl0) {
171 gtk_object_destroy(GTK_OBJECT(pc->cl0));
172 pc->cl0 = NULL;
173 }
174 if (pc->cl1) {
175 gtk_object_destroy(GTK_OBJECT(pc->cl1));
176 pc->cl1 = NULL;
177 }
179 G_OBJECT_CLASS(pen_parent_class)->dispose(object);
181 pc->polylines_only = false;
182 if (pc->expecting_clicks_for_LPE > 0) {
183 // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
184 sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
185 }
186 }
188 /**
189 * Callback to initialize SPPenContext object.
190 */
191 static void
192 sp_pen_context_setup(SPEventContext *ec)
193 {
194 SPPenContext *pc;
196 pc = SP_PEN_CONTEXT(ec);
198 if (((SPEventContextClass *) pen_parent_class)->setup) {
199 ((SPEventContextClass *) pen_parent_class)->setup(ec);
200 }
202 /* Pen indicators */
203 pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
204 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
205 pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
206 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
207 pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
208 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
209 pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
210 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
212 sp_canvas_item_hide(pc->c0);
213 sp_canvas_item_hide(pc->c1);
214 sp_canvas_item_hide(pc->cl0);
215 sp_canvas_item_hide(pc->cl1);
217 sp_event_context_read(ec, "mode");
219 pc->anchor_statusbar = false;
221 if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
222 ec->enableSelectionCue();
223 }
224 }
226 static void
227 pen_cancel (SPPenContext *const pc)
228 {
229 pc->state = SP_PEN_CONTEXT_STOP;
230 spdc_reset_colors(pc);
231 sp_canvas_item_hide(pc->c0);
232 sp_canvas_item_hide(pc->c1);
233 sp_canvas_item_hide(pc->cl0);
234 sp_canvas_item_hide(pc->cl1);
235 pc->_message_context->clear();
236 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
238 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
239 }
241 /**
242 * Finalization callback.
243 */
244 static void
245 sp_pen_context_finish(SPEventContext *ec)
246 {
247 SPPenContext *pc = SP_PEN_CONTEXT(ec);
249 if (pc->npoints != 0) {
250 pen_cancel (pc);
251 }
253 if (((SPEventContextClass *) pen_parent_class)->finish) {
254 ((SPEventContextClass *) pen_parent_class)->finish(ec);
255 }
256 }
258 /**
259 * Callback that sets key to value in pen context.
260 */
261 static void
262 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
263 {
264 SPPenContext *pc = SP_PEN_CONTEXT(ec);
266 if (!strcmp(key, "mode")) {
267 if ( val && !strcmp(val, "drag") ) {
268 pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
269 } else {
270 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
271 }
272 }
273 }
275 /**
276 * Snaps new node relative to the previous node.
277 */
278 static void
279 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
280 {
281 if (pc->npoints > 0) {
282 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
283 }
285 spdc_endpoint_snap_free(pc, p, state);
286 }
288 /**
289 * Snaps new node's handle relative to the new node.
290 */
291 static void
292 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
293 {
294 g_return_if_fail(( pc->npoints == 2 ||
295 pc->npoints == 5 ));
297 spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
298 spdc_endpoint_snap_free(pc, p, state);
299 }
301 static gint
302 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
303 {
304 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
306 gint ret = FALSE;
308 switch (event->type) {
309 case GDK_BUTTON_PRESS:
310 ret = pen_handle_button_press(pc, event->button);
311 break;
312 default:
313 break;
314 }
316 if (!ret) {
317 if (((SPEventContextClass *) pen_parent_class)->item_handler)
318 ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
319 }
321 return ret;
322 }
324 /**
325 * Callback to handle all pen events.
326 */
327 static gint
328 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
329 {
330 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
332 gint ret = FALSE;
334 switch (event->type) {
335 case GDK_BUTTON_PRESS:
336 ret = pen_handle_button_press(pc, event->button);
337 break;
339 case GDK_MOTION_NOTIFY:
340 ret = pen_handle_motion_notify(pc, event->motion);
341 break;
343 case GDK_BUTTON_RELEASE:
344 ret = pen_handle_button_release(pc, event->button);
345 break;
347 case GDK_2BUTTON_PRESS:
348 ret = pen_handle_2button_press(pc, event->button);
349 break;
351 case GDK_KEY_PRESS:
352 ret = pen_handle_key_press(pc, event);
353 break;
355 default:
356 break;
357 }
359 if (!ret) {
360 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
361 = ((SPEventContextClass *) pen_parent_class)->root_handler;
362 if (parent_root_handler) {
363 ret = parent_root_handler(ec, event);
364 }
365 }
367 return ret;
368 }
370 /**
371 * Handle mouse button press event.
372 */
373 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
374 {
375 if (pc->events_disabled) {
376 // skip event processing if events are disabled
377 return FALSE;
378 }
380 SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
381 SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
382 NR::Point const event_w(bevent.x, bevent.y);
383 NR::Point const event_dt(desktop->w2d(event_w));
384 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
386 gint ret = FALSE;
387 if (bevent.button == 1 && !event_context->space_panning
388 && pc->expecting_clicks_for_LPE != 1) { // when the last click for a waiting LPE occurs we want to finish the path
391 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
392 return TRUE;
393 }
395 pen_drag_origin_w = event_w;
396 pen_within_tolerance = true;
398 /* Test whether we hit any anchor. */
399 SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
401 switch (pc->mode) {
402 case SP_PEN_CONTEXT_MODE_CLICK:
403 /* In click mode we add point on release */
404 switch (pc->state) {
405 case SP_PEN_CONTEXT_POINT:
406 case SP_PEN_CONTEXT_CONTROL:
407 case SP_PEN_CONTEXT_CLOSE:
408 break;
409 case SP_PEN_CONTEXT_STOP:
410 /* This is allowed, if we just cancelled curve */
411 pc->state = SP_PEN_CONTEXT_POINT;
412 break;
413 default:
414 break;
415 }
416 break;
417 case SP_PEN_CONTEXT_MODE_DRAG:
418 switch (pc->state) {
419 case SP_PEN_CONTEXT_STOP:
420 /* This is allowed, if we just cancelled curve */
421 case SP_PEN_CONTEXT_POINT:
422 if (pc->npoints == 0) {
424 if (bevent.state & GDK_CONTROL_MASK) {
425 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
426 ret = TRUE;
427 break;
428 }
430 /* Set start anchor */
431 pc->sa = anchor;
432 NR::Point p;
433 if (anchor) {
435 /* Adjust point to anchor if needed */
436 p = anchor->dp;
437 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
439 } else {
441 // This is the first click of a new curve; deselect item so that
442 // this curve is not combined with it (unless it is drawn from its
443 // anchor, which is handled by the sibling branch above)
444 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
445 if (!(bevent.state & GDK_SHIFT_MASK)) {
447 selection->clear();
448 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
450 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
452 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
453 }
455 /* Create green anchor */
456 p = event_dt;
457 spdc_endpoint_snap(pc, p, bevent.state);
458 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
459 }
460 spdc_pen_set_initial_point(pc, p);
461 } else {
463 /* Set end anchor */
464 pc->ea = anchor;
465 NR::Point p;
466 if (anchor) {
468 p = anchor->dp;
469 // we hit an anchor, will finish the curve (either with or without closing)
470 // in release handler
471 pc->state = SP_PEN_CONTEXT_CLOSE;
473 if (pc->green_anchor && pc->green_anchor->active) {
474 // we clicked on the current curve start, so close it even if
475 // we drag a handle away from it
476 dc->green_closed = TRUE;
477 }
478 ret = TRUE;
479 break;
481 } else {
483 p = event_dt;
484 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
485 spdc_pen_set_subsequent_point(pc, p, true);
486 if (pc->polylines_only) {
487 spdc_pen_finish_segment(pc, p, bevent.state);
488 }
489 }
491 }
492 pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
493 ret = TRUE;
494 break;
495 case SP_PEN_CONTEXT_CONTROL:
496 g_warning("Button down in CONTROL state");
497 break;
498 case SP_PEN_CONTEXT_CLOSE:
499 g_warning("Button down in CLOSE state");
500 break;
501 default:
502 break;
503 }
504 break;
505 default:
506 break;
507 }
508 } else if (bevent.button == 3 || pc->expecting_clicks_for_LPE == 1) { // when the last click for a waiting LPE occurs we want to finish the path
509 if (pc->npoints != 0) {
511 spdc_pen_finish_segment(pc, event_dt, bevent.state);
512 if (pc->green_closed) {
513 // finishing at the start anchor, close curve
514 spdc_pen_finish(pc, TRUE);
515 } else {
516 // finishing at some other anchor, finish curve but not close
517 spdc_pen_finish(pc, FALSE);
518 }
520 ret = TRUE;
521 }
522 }
524 return ret;
525 }
527 /**
528 * Handle motion_notify event.
529 */
530 static gint
531 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
532 {
533 gint ret = FALSE;
535 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
537 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
538 // allow scrolling
539 return FALSE;
540 }
542 if (pc->events_disabled) {
543 // skip motion events if pen events are disabled
544 return FALSE;
545 }
547 NR::Point const event_w(mevent.x,
548 mevent.y);
549 if (pen_within_tolerance) {
550 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
551 "value", 0, 0, 100);
552 if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
553 return FALSE; // Do not drag if we're within tolerance from origin.
554 }
555 }
556 // Once the user has moved farther than tolerance from the original location
557 // (indicating they intend to move the object, not click), then always process the
558 // motion notify coordinates as given (no snapping back to origin)
559 pen_within_tolerance = false;
561 SPDesktop *const dt = pc->desktop;
562 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
563 /* Grab mouse, so release will not pass unnoticed */
564 pc->grab = SP_CANVAS_ITEM(dt->acetate);
565 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
566 GDK_BUTTON_RELEASE_MASK |
567 GDK_POINTER_MOTION_MASK ),
568 NULL, mevent.time);
569 }
571 /* Find desktop coordinates */
572 NR::Point p = dt->w2d(event_w);
574 /* Test, whether we hit any anchor */
575 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
577 switch (pc->mode) {
578 case SP_PEN_CONTEXT_MODE_CLICK:
579 switch (pc->state) {
580 case SP_PEN_CONTEXT_POINT:
581 if ( pc->npoints != 0 ) {
582 /* Only set point, if we are already appending */
583 spdc_endpoint_snap(pc, p, mevent.state);
584 spdc_pen_set_subsequent_point(pc, p, true);
585 ret = TRUE;
586 }
587 break;
588 case SP_PEN_CONTEXT_CONTROL:
589 case SP_PEN_CONTEXT_CLOSE:
590 /* Placing controls is last operation in CLOSE state */
591 spdc_endpoint_snap(pc, p, mevent.state);
592 spdc_pen_set_ctrl(pc, p, mevent.state);
593 ret = TRUE;
594 break;
595 case SP_PEN_CONTEXT_STOP:
596 /* This is perfectly valid */
597 break;
598 default:
599 break;
600 }
601 break;
602 case SP_PEN_CONTEXT_MODE_DRAG:
603 switch (pc->state) {
604 case SP_PEN_CONTEXT_POINT:
605 if ( pc->npoints > 0 ) {
606 /* Only set point, if we are already appending */
608 if (!anchor) { /* Snap node only if not hitting anchor */
609 spdc_endpoint_snap(pc, p, mevent.state);
610 }
612 spdc_pen_set_subsequent_point(pc, p, !anchor);
614 if (anchor && !pc->anchor_statusbar) {
615 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
616 pc->anchor_statusbar = true;
617 } else if (!anchor && pc->anchor_statusbar) {
618 pc->_message_context->clear();
619 pc->anchor_statusbar = false;
620 }
622 ret = TRUE;
623 } else {
624 if (anchor && !pc->anchor_statusbar) {
625 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
626 pc->anchor_statusbar = true;
627 } else if (!anchor && pc->anchor_statusbar) {
628 pc->_message_context->clear();
629 pc->anchor_statusbar = false;
630 }
631 }
632 break;
633 case SP_PEN_CONTEXT_CONTROL:
634 case SP_PEN_CONTEXT_CLOSE:
635 /* Placing controls is last operation in CLOSE state */
637 // snap the handle
638 spdc_endpoint_snap_handle(pc, p, mevent.state);
640 if (!pc->polylines_only) {
641 spdc_pen_set_ctrl(pc, p, mevent.state);
642 } else {
643 spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
644 }
645 gobble_motion_events(GDK_BUTTON1_MASK);
646 ret = TRUE;
647 break;
648 case SP_PEN_CONTEXT_STOP:
649 /* This is perfectly valid */
650 break;
651 default:
652 break;
653 }
654 break;
655 default:
656 break;
657 }
658 return ret;
659 }
661 /**
662 * Handle mouse button release event.
663 */
664 static gint
665 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
666 {
667 if (pc->events_disabled) {
668 // skip event processing if events are disabled
669 return FALSE;
670 }
672 gint ret = FALSE;
673 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
674 if ( revent.button == 1 && !event_context->space_panning) {
676 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
678 NR::Point const event_w(revent.x,
679 revent.y);
680 /* Find desktop coordinates */
681 NR::Point p = pc->desktop->w2d(event_w);
683 /* Test whether we hit any anchor. */
684 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
686 switch (pc->mode) {
687 case SP_PEN_CONTEXT_MODE_CLICK:
688 switch (pc->state) {
689 case SP_PEN_CONTEXT_POINT:
690 if ( pc->npoints == 0 ) {
691 /* Start new thread only with button release */
692 if (anchor) {
693 p = anchor->dp;
694 }
695 pc->sa = anchor;
696 spdc_pen_set_initial_point(pc, p);
697 } else {
698 /* Set end anchor here */
699 pc->ea = anchor;
700 if (anchor) {
701 p = anchor->dp;
702 }
703 }
704 pc->state = SP_PEN_CONTEXT_CONTROL;
705 ret = TRUE;
706 break;
707 case SP_PEN_CONTEXT_CONTROL:
708 /* End current segment */
709 spdc_endpoint_snap(pc, p, revent.state);
710 spdc_pen_finish_segment(pc, p, revent.state);
711 pc->state = SP_PEN_CONTEXT_POINT;
712 ret = TRUE;
713 break;
714 case SP_PEN_CONTEXT_CLOSE:
715 /* End current segment */
716 if (!anchor) { /* Snap node only if not hitting anchor */
717 spdc_endpoint_snap(pc, p, revent.state);
718 }
719 spdc_pen_finish_segment(pc, p, revent.state);
720 spdc_pen_finish(pc, TRUE);
721 pc->state = SP_PEN_CONTEXT_POINT;
722 ret = TRUE;
723 break;
724 case SP_PEN_CONTEXT_STOP:
725 /* This is allowed, if we just cancelled curve */
726 pc->state = SP_PEN_CONTEXT_POINT;
727 ret = TRUE;
728 break;
729 default:
730 break;
731 }
732 break;
733 case SP_PEN_CONTEXT_MODE_DRAG:
734 switch (pc->state) {
735 case SP_PEN_CONTEXT_POINT:
736 case SP_PEN_CONTEXT_CONTROL:
737 if (!pc->polylines_only) {
738 spdc_endpoint_snap(pc, p, revent.state);
739 spdc_pen_finish_segment(pc, p, revent.state);
740 }
741 break;
742 case SP_PEN_CONTEXT_CLOSE:
743 spdc_endpoint_snap(pc, p, revent.state);
744 spdc_pen_finish_segment(pc, p, revent.state);
745 if (pc->green_closed) {
746 // finishing at the start anchor, close curve
747 spdc_pen_finish(pc, TRUE);
748 } else {
749 // finishing at some other anchor, finish curve but not close
750 spdc_pen_finish(pc, FALSE);
751 }
752 break;
753 case SP_PEN_CONTEXT_STOP:
754 /* This is allowed, if we just cancelled curve */
755 break;
756 default:
757 break;
758 }
759 pc->state = SP_PEN_CONTEXT_POINT;
760 ret = TRUE;
761 break;
762 default:
763 break;
764 }
766 if (pc->grab) {
767 /* Release grab now */
768 sp_canvas_item_ungrab(pc->grab, revent.time);
769 pc->grab = NULL;
770 }
772 ret = TRUE;
774 dc->green_closed = FALSE;
775 }
777 // TODO: can we be sure that the path was created correctly?
778 // TODO: should we offer an option to collect the clicks in a list?
779 if (pc->expecting_clicks_for_LPE > 0) {
780 --pc->expecting_clicks_for_LPE;
782 if (pc->expecting_clicks_for_LPE == 0) {
783 pc->polylines_only = false;
785 SPEventContext *ec = SP_EVENT_CONTEXT(pc);
786 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
788 if (pc->waiting_LPE) {
789 // we have an already created LPE waiting for a path
790 pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
791 selection->add(SP_OBJECT(pc->waiting_item));
792 } else {
793 // the case that we need to create a new LPE and apply it to the just-drawn path is
794 // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
795 }
796 }
797 }
799 return ret;
800 }
802 static gint
803 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
804 {
805 gint ret = FALSE;
806 if (pc->npoints != 0 && bevent.button != 2) {
807 spdc_pen_finish(pc, FALSE);
808 ret = TRUE;
809 }
810 return ret;
811 }
813 void
814 pen_redraw_all (SPPenContext *const pc)
815 {
816 // green
817 if (pc->green_bpaths) {
818 // remove old piecewise green canvasitems
819 while (pc->green_bpaths) {
820 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
821 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
822 }
823 // one canvas bpath for all of green_curve
824 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
825 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
826 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
828 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
829 }
831 if (pc->green_anchor)
832 SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
834 pc->red_curve->reset();
835 pc->red_curve->moveto(pc->p[0]);
836 pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
837 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
839 // handles
840 if (pc->p[0] != pc->p[1]) {
841 SP_CTRL(pc->c1)->moveto(pc->p[1]);
842 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
843 sp_canvas_item_show (pc->c1);
844 sp_canvas_item_show (pc->cl1);
845 } else {
846 sp_canvas_item_hide (pc->c1);
847 sp_canvas_item_hide (pc->cl1);
848 }
850 NArtBpath const * bpath = pc->green_curve->last_bpath();
851 if (bpath) {
852 if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
853 SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
854 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
855 sp_canvas_item_show (pc->c0);
856 sp_canvas_item_show (pc->cl0);
857 } else {
858 sp_canvas_item_hide (pc->c0);
859 sp_canvas_item_hide (pc->cl0);
860 }
861 }
862 }
864 void
865 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
866 {
867 if (pc->npoints != 5)
868 return;
870 // green
871 NArtBpath const * bpath = pc->green_curve->last_bpath();
872 if (bpath) {
873 pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
874 } else {
875 // start anchor too
876 if (pc->green_anchor) {
877 pc->green_anchor->dp += NR::Point(x, y);
878 }
879 }
881 // red
882 pc->p[0] += NR::Point(x, y);
883 pc->p[1] += NR::Point(x, y);
884 pen_redraw_all(pc);
885 }
887 void
888 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
889 {
890 pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
891 }
893 void
894 pen_lastpoint_tocurve (SPPenContext *const pc)
895 {
896 if (pc->npoints != 5)
897 return;
899 // red
900 NArtBpath const * bpath = pc->green_curve->last_bpath();
901 if (bpath && bpath->code == NR_CURVETO) {
902 pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
903 } else {
904 pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
905 }
907 pen_redraw_all(pc);
908 }
910 void
911 pen_lastpoint_toline (SPPenContext *const pc)
912 {
913 if (pc->npoints != 5)
914 return;
916 pc->p[1] = pc->p[0];
918 pen_redraw_all(pc);
919 }
922 static gint
923 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
924 {
925 gint ret = FALSE;
926 gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
928 switch (get_group0_keyval (&event->key)) {
930 case GDK_Left: // move last point left
931 case GDK_KP_Left:
932 case GDK_KP_4:
933 if (!MOD__CTRL) { // not ctrl
934 if (MOD__ALT) { // alt
935 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
936 else pen_lastpoint_move_screen(pc, -1, 0); // no shift
937 }
938 else { // no alt
939 if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
940 else pen_lastpoint_move(pc, -nudge, 0); // no shift
941 }
942 ret = TRUE;
943 }
944 break;
945 case GDK_Up: // move last point up
946 case GDK_KP_Up:
947 case GDK_KP_8:
948 if (!MOD__CTRL) { // not ctrl
949 if (MOD__ALT) { // alt
950 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
951 else pen_lastpoint_move_screen(pc, 0, 1); // no shift
952 }
953 else { // no alt
954 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
955 else pen_lastpoint_move(pc, 0, nudge); // no shift
956 }
957 ret = TRUE;
958 }
959 break;
960 case GDK_Right: // move last point right
961 case GDK_KP_Right:
962 case GDK_KP_6:
963 if (!MOD__CTRL) { // not ctrl
964 if (MOD__ALT) { // alt
965 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
966 else pen_lastpoint_move_screen(pc, 1, 0); // no shift
967 }
968 else { // no alt
969 if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
970 else pen_lastpoint_move(pc, nudge, 0); // no shift
971 }
972 ret = TRUE;
973 }
974 break;
975 case GDK_Down: // move last point down
976 case GDK_KP_Down:
977 case GDK_KP_2:
978 if (!MOD__CTRL) { // not ctrl
979 if (MOD__ALT) { // alt
980 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
981 else pen_lastpoint_move_screen(pc, 0, -1); // no shift
982 }
983 else { // no alt
984 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
985 else pen_lastpoint_move(pc, 0, -nudge); // no shift
986 }
987 ret = TRUE;
988 }
989 break;
991 case GDK_U:
992 case GDK_u:
993 if (MOD__SHIFT_ONLY) {
994 pen_lastpoint_tocurve(pc);
995 ret = TRUE;
996 }
997 break;
998 case GDK_L:
999 case GDK_l:
1000 if (MOD__SHIFT_ONLY) {
1001 pen_lastpoint_toline(pc);
1002 ret = TRUE;
1003 }
1004 break;
1006 case GDK_Return:
1007 case GDK_KP_Enter:
1008 if (pc->npoints != 0) {
1009 spdc_pen_finish(pc, FALSE);
1010 ret = TRUE;
1011 }
1012 break;
1013 case GDK_Escape:
1014 if (pc->npoints != 0) {
1015 // if drawing, cancel, otherwise pass it up for deselecting
1016 pen_cancel (pc);
1017 ret = TRUE;
1018 }
1019 break;
1020 case GDK_z:
1021 case GDK_Z:
1022 if (MOD__CTRL_ONLY && pc->npoints != 0) {
1023 // if drawing, cancel, otherwise pass it up for undo
1024 pen_cancel (pc);
1025 ret = TRUE;
1026 }
1027 break;
1028 case GDK_g:
1029 case GDK_G:
1030 if (MOD__SHIFT_ONLY) {
1031 sp_selection_to_guides();
1032 ret = true;
1033 }
1034 break;
1035 case GDK_BackSpace:
1036 case GDK_Delete:
1037 case GDK_KP_Delete:
1038 if (pc->green_curve->is_empty()) {
1039 if (!pc->red_curve->is_empty()) {
1040 pen_cancel (pc);
1041 ret = TRUE;
1042 } else {
1043 // do nothing; this event should be handled upstream
1044 }
1045 } else {
1046 /* Reset red curve */
1047 pc->red_curve->reset();
1048 /* Destroy topmost green bpath */
1049 if (pc->green_bpaths) {
1050 if (pc->green_bpaths->data)
1051 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1052 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1053 }
1054 /* Get last segment */
1055 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
1056 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1057 if ( e < 2 ) {
1058 g_warning("Green curve length is %d", e);
1059 break;
1060 }
1061 pc->p[0] = p[e - 2].c(3);
1062 if (p[e - 1].code == NR_CURVETO) {
1063 pc->p[1] = p[e - 1].c(1);
1064 } else {
1065 pc->p[1] = pc->p[0];
1066 }
1067 NR::Point const pt(( pc->npoints < 4
1068 ? p[e - 1].c(3)
1069 : pc->p[3] ));
1070 pc->npoints = 2;
1071 pc->green_curve->backspace();
1072 sp_canvas_item_hide(pc->c0);
1073 sp_canvas_item_hide(pc->c1);
1074 sp_canvas_item_hide(pc->cl0);
1075 sp_canvas_item_hide(pc->cl1);
1076 pc->state = SP_PEN_CONTEXT_POINT;
1077 spdc_pen_set_subsequent_point(pc, pt, true);
1078 ret = TRUE;
1079 }
1080 break;
1081 default:
1082 break;
1083 }
1084 return ret;
1085 }
1087 static void
1088 spdc_reset_colors(SPPenContext *pc)
1089 {
1090 /* Red */
1091 pc->red_curve->reset();
1092 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1093 /* Blue */
1094 pc->blue_curve->reset();
1095 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1096 /* Green */
1097 while (pc->green_bpaths) {
1098 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1099 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1100 }
1101 pc->green_curve->reset();
1102 if (pc->green_anchor) {
1103 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1104 }
1105 pc->sa = NULL;
1106 pc->ea = NULL;
1107 pc->npoints = 0;
1108 pc->red_curve_is_valid = false;
1109 }
1112 static void
1113 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1114 {
1115 g_assert( pc->npoints == 0 );
1117 pc->p[0] = p;
1118 pc->p[1] = p;
1119 pc->npoints = 2;
1120 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1122 sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1123 }
1125 /**
1126 * Show the status message for the current line/curve segment.
1127 * This type of message always shows angle/distance as the last
1128 * two parameters ("angle %3.2f°, distance %s").
1129 */
1130 static void
1131 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1132 {
1133 g_assert(pc != NULL);
1134 g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1135 g_assert(message != NULL);
1137 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1138 NR::Point rel = p - pc->p[pc_point_to_compare];
1139 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1140 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1141 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1142 angle = angle_to_compass (angle);
1144 pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1145 g_string_free(dist, FALSE);
1146 }
1148 static void
1149 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1150 {
1151 g_assert( pc->npoints != 0 );
1152 /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1154 pc->p[2] = p;
1155 pc->p[3] = p;
1156 pc->p[4] = p;
1157 pc->npoints = 5;
1158 pc->red_curve->reset();
1159 pc->red_curve->moveto(pc->p[0]);
1160 bool is_curve;
1161 if (pc->p[1] != pc->p[0])
1162 {
1163 pc->red_curve->curveto(pc->p[1], p, p);
1164 is_curve = true;
1165 } else {
1166 pc->red_curve->lineto(p);
1167 is_curve = false;
1168 }
1170 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1172 if (statusbar) {
1173 gchar *message = is_curve ?
1174 _("<b>Curve segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1175 _("<b>Line segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1176 spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1177 }
1178 }
1180 static void
1181 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1182 {
1183 sp_canvas_item_show(pc->c1);
1184 sp_canvas_item_show(pc->cl1);
1186 if ( pc->npoints == 2 ) {
1187 pc->p[1] = p;
1188 sp_canvas_item_hide(pc->c0);
1189 sp_canvas_item_hide(pc->cl0);
1190 SP_CTRL(pc->c1)->moveto(pc->p[1]);
1191 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1193 spdc_pen_set_angle_distance_status_message(pc, p, 0, _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle"));
1194 } else if ( pc->npoints == 5 ) {
1195 pc->p[4] = p;
1196 sp_canvas_item_show(pc->c0);
1197 sp_canvas_item_show(pc->cl0);
1198 bool is_symm = false;
1199 if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1200 ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) && !( state & GDK_SHIFT_MASK ) ) ) {
1201 NR::Point delta = p - pc->p[3];
1202 pc->p[2] = pc->p[3] - delta;
1203 is_symm = true;
1204 pc->red_curve->reset();
1205 pc->red_curve->moveto(pc->p[0]);
1206 pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1207 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1208 }
1209 SP_CTRL(pc->c0)->moveto(pc->p[2]);
1210 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1211 SP_CTRL(pc->c1)->moveto(pc->p[4]);
1212 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1214 gchar *message = is_symm ?
1215 _("<b>Curve handle, symmetric</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only") :
1216 _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only");
1217 spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1218 } else {
1219 g_warning("Something bad happened - npoints is %d", pc->npoints);
1220 }
1221 }
1223 static void
1224 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
1225 {
1226 if (!pc->red_curve->is_empty()) {
1227 pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1228 SPCurve *curve = pc->red_curve->copy();
1229 /// \todo fixme:
1230 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1231 curve->unref();
1232 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1234 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1236 pc->p[0] = pc->p[3];
1237 pc->p[1] = pc->p[4];
1238 pc->npoints = 2;
1240 pc->red_curve->reset();
1241 }
1242 }
1244 static void
1245 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1246 {
1247 if (pc->expecting_clicks_for_LPE > 1) {
1248 // don't let the path be finished before we have collected the required number of mouse clicks
1249 return;
1250 }
1252 pen_disable_events(pc);
1254 SPDesktop *const desktop = pc->desktop;
1255 pc->_message_context->clear();
1256 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1258 pc->red_curve->reset();
1259 spdc_concat_colors_and_flush(pc, closed);
1260 pc->sa = NULL;
1261 pc->ea = NULL;
1263 pc->npoints = 0;
1264 pc->state = SP_PEN_CONTEXT_POINT;
1266 sp_canvas_item_hide(pc->c0);
1267 sp_canvas_item_hide(pc->c1);
1268 sp_canvas_item_hide(pc->cl0);
1269 sp_canvas_item_hide(pc->cl1);
1271 if (pc->green_anchor) {
1272 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1273 }
1276 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1278 pen_enable_events(pc);
1279 }
1281 static void
1282 pen_disable_events(SPPenContext *const pc) {
1283 pc->events_disabled++;
1284 }
1286 static void
1287 pen_enable_events(SPPenContext *const pc) {
1288 g_return_if_fail(pc->events_disabled != 0);
1290 pc->events_disabled--;
1291 }
1293 /*
1294 Local Variables:
1295 mode:c++
1296 c-file-style:"stroustrup"
1297 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1298 indent-tabs-mode:nil
1299 fill-column:99
1300 End:
1301 */
1302 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :