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>
20 #include "pen-context.h"
21 #include "sp-namedview.h"
22 #include "sp-metrics.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "selection.h"
26 #include "draw-anchor.h"
27 #include "message-stack.h"
28 #include "message-context.h"
29 #include "prefs-utils.h"
30 #include "sp-path.h"
32 #include "pixmaps/cursor-pen.xpm"
33 #include "display/canvas-bpath.h"
34 #include "display/sp-ctrlline.h"
35 #include "display/sodipodi-ctrl.h"
36 #include <glibmm/i18n.h>
37 #include "libnr/n-art-bpath.h"
38 #include "helper/units.h"
39 #include "macros.h"
40 #include "context-fns.h"
43 static void sp_pen_context_class_init(SPPenContextClass *klass);
44 static void sp_pen_context_init(SPPenContext *pc);
45 static void sp_pen_context_dispose(GObject *object);
47 static void sp_pen_context_setup(SPEventContext *ec);
48 static void sp_pen_context_finish(SPEventContext *ec);
49 static void sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
51 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
53 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
54 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
55 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
56 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
58 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
60 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
61 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
62 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
63 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
64 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
65 static void spdc_reset_colors(SPPenContext *pc);
67 static void pen_disable_events(SPPenContext *const pc);
68 static void pen_enable_events(SPPenContext *const pc);
70 static NR::Point pen_drag_origin_w(0, 0);
71 static bool pen_within_tolerance = false;
73 static SPDrawContextClass *pen_parent_class;
75 /**
76 * Register SPPenContext with Gdk and return its type.
77 */
78 GType
79 sp_pen_context_get_type(void)
80 {
81 static GType type = 0;
82 if (!type) {
83 GTypeInfo info = {
84 sizeof(SPPenContextClass),
85 NULL, NULL,
86 (GClassInitFunc) sp_pen_context_class_init,
87 NULL, NULL,
88 sizeof(SPPenContext),
89 4,
90 (GInstanceInitFunc) sp_pen_context_init,
91 NULL, /* value_table */
92 };
93 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
94 }
95 return type;
96 }
98 /**
99 * Initialize the SPPenContext vtable.
100 */
101 static void
102 sp_pen_context_class_init(SPPenContextClass *klass)
103 {
104 GObjectClass *object_class;
105 SPEventContextClass *event_context_class;
107 object_class = (GObjectClass *) klass;
108 event_context_class = (SPEventContextClass *) klass;
110 pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
112 object_class->dispose = sp_pen_context_dispose;
114 event_context_class->setup = sp_pen_context_setup;
115 event_context_class->finish = sp_pen_context_finish;
116 event_context_class->set = sp_pen_context_set;
117 event_context_class->root_handler = sp_pen_context_root_handler;
118 event_context_class->item_handler = sp_pen_context_item_handler;
119 }
121 /**
122 * Callback to initialize SPPenContext object.
123 */
124 static void
125 sp_pen_context_init(SPPenContext *pc)
126 {
128 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
130 event_context->cursor_shape = cursor_pen_xpm;
131 event_context->hot_x = 4;
132 event_context->hot_y = 4;
134 pc->npoints = 0;
135 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
136 pc->state = SP_PEN_CONTEXT_POINT;
138 pc->c0 = NULL;
139 pc->c1 = NULL;
140 pc->cl0 = NULL;
141 pc->cl1 = NULL;
143 pc->events_disabled = 0;
144 }
146 /**
147 * Callback to destroy the SPPenContext object's members and itself.
148 */
149 static void
150 sp_pen_context_dispose(GObject *object)
151 {
152 SPPenContext *pc;
154 pc = SP_PEN_CONTEXT(object);
156 if (pc->c0) {
157 gtk_object_destroy(GTK_OBJECT(pc->c0));
158 pc->c0 = NULL;
159 }
160 if (pc->c1) {
161 gtk_object_destroy(GTK_OBJECT(pc->c1));
162 pc->c1 = NULL;
163 }
164 if (pc->cl0) {
165 gtk_object_destroy(GTK_OBJECT(pc->cl0));
166 pc->cl0 = NULL;
167 }
168 if (pc->cl1) {
169 gtk_object_destroy(GTK_OBJECT(pc->cl1));
170 pc->cl1 = NULL;
171 }
173 G_OBJECT_CLASS(pen_parent_class)->dispose(object);
174 }
176 /**
177 * Callback to initialize SPPenContext object.
178 */
179 static void
180 sp_pen_context_setup(SPEventContext *ec)
181 {
182 SPPenContext *pc;
184 pc = SP_PEN_CONTEXT(ec);
186 if (((SPEventContextClass *) pen_parent_class)->setup) {
187 ((SPEventContextClass *) pen_parent_class)->setup(ec);
188 }
190 /* Pen indicators */
191 pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
192 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
193 pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
194 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
195 pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
196 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
197 pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
198 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
200 sp_canvas_item_hide(pc->c0);
201 sp_canvas_item_hide(pc->c1);
202 sp_canvas_item_hide(pc->cl0);
203 sp_canvas_item_hide(pc->cl1);
205 sp_event_context_read(ec, "mode");
207 pc->anchor_statusbar = false;
209 if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
210 ec->enableSelectionCue();
211 }
212 }
214 static void
215 pen_cancel (SPPenContext *const pc)
216 {
217 pc->state = SP_PEN_CONTEXT_STOP;
218 spdc_reset_colors(pc);
219 sp_canvas_item_hide(pc->c0);
220 sp_canvas_item_hide(pc->c1);
221 sp_canvas_item_hide(pc->cl0);
222 sp_canvas_item_hide(pc->cl1);
223 pc->_message_context->clear();
224 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
226 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
227 }
229 /**
230 * Finalization callback.
231 */
232 static void
233 sp_pen_context_finish(SPEventContext *ec)
234 {
235 SPPenContext *pc = SP_PEN_CONTEXT(ec);
237 if (pc->npoints != 0) {
238 pen_cancel (pc);
239 }
241 if (((SPEventContextClass *) pen_parent_class)->finish) {
242 ((SPEventContextClass *) pen_parent_class)->finish(ec);
243 }
244 }
246 /**
247 * Callback that sets key to value in pen context.
248 */
249 static void
250 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
251 {
252 SPPenContext *pc = SP_PEN_CONTEXT(ec);
254 if (!strcmp(key, "mode")) {
255 if ( val && !strcmp(val, "drag") ) {
256 pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
257 } else {
258 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
259 }
260 }
261 }
263 /**
264 * Snaps new node relative to the previous node.
265 */
266 static void
267 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
268 {
269 if (pc->npoints > 0) {
270 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
271 }
273 spdc_endpoint_snap_free(pc, p, state);
274 }
276 /**
277 * Snaps new node's handle relative to the new node.
278 */
279 static void
280 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
281 {
282 g_return_if_fail(( pc->npoints == 2 ||
283 pc->npoints == 5 ));
285 spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
286 spdc_endpoint_snap_free(pc, p, state);
287 }
289 static gint
290 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
291 {
292 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
294 gint ret = FALSE;
296 switch (event->type) {
297 case GDK_BUTTON_PRESS:
298 ret = pen_handle_button_press(pc, event->button);
299 break;
300 default:
301 break;
302 }
304 if (!ret) {
305 if (((SPEventContextClass *) pen_parent_class)->item_handler)
306 ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
307 }
309 return ret;
310 }
312 /**
313 * Callback to handle all pen events.
314 */
315 static gint
316 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
317 {
318 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
320 gint ret = FALSE;
322 switch (event->type) {
323 case GDK_BUTTON_PRESS:
324 ret = pen_handle_button_press(pc, event->button);
325 break;
327 case GDK_MOTION_NOTIFY:
328 ret = pen_handle_motion_notify(pc, event->motion);
329 break;
331 case GDK_BUTTON_RELEASE:
332 ret = pen_handle_button_release(pc, event->button);
333 break;
335 case GDK_2BUTTON_PRESS:
336 ret = pen_handle_2button_press(pc, event->button);
337 break;
339 case GDK_KEY_PRESS:
340 ret = pen_handle_key_press(pc, event);
341 break;
343 default:
344 break;
345 }
347 if (!ret) {
348 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
349 = ((SPEventContextClass *) pen_parent_class)->root_handler;
350 if (parent_root_handler) {
351 ret = parent_root_handler(ec, event);
352 }
353 }
355 return ret;
356 }
358 /**
359 * Handle mouse button press event.
360 */
361 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
362 {
363 if (pc->events_disabled) {
364 // skip event processing if events are disabled
365 return FALSE;
366 }
368 SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
369 SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
370 NR::Point const event_w(bevent.x, bevent.y);
371 NR::Point const event_dt(desktop->w2d(event_w));
372 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
374 gint ret = FALSE;
375 if (bevent.button == 1 && !event_context->space_panning) {
377 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
378 return TRUE;
379 }
381 pen_drag_origin_w = event_w;
382 pen_within_tolerance = true;
384 /* Test whether we hit any anchor. */
385 SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
387 switch (pc->mode) {
388 case SP_PEN_CONTEXT_MODE_CLICK:
389 /* In click mode we add point on release */
390 switch (pc->state) {
391 case SP_PEN_CONTEXT_POINT:
392 case SP_PEN_CONTEXT_CONTROL:
393 case SP_PEN_CONTEXT_CLOSE:
394 break;
395 case SP_PEN_CONTEXT_STOP:
396 /* This is allowed, if we just cancelled curve */
397 pc->state = SP_PEN_CONTEXT_POINT;
398 break;
399 default:
400 break;
401 }
402 break;
403 case SP_PEN_CONTEXT_MODE_DRAG:
404 switch (pc->state) {
405 case SP_PEN_CONTEXT_STOP:
406 /* This is allowed, if we just cancelled curve */
407 case SP_PEN_CONTEXT_POINT:
408 if (pc->npoints == 0) {
410 /* Set start anchor */
411 pc->sa = anchor;
412 NR::Point p;
413 if (anchor) {
415 /* Adjust point to anchor if needed */
416 p = anchor->dp;
417 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
419 } else {
421 // This is the first click of a new curve; deselect item so that
422 // this curve is not combined with it (unless it is drawn from its
423 // anchor, which is handled by the sibling branch above)
424 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
425 if (!(bevent.state & GDK_SHIFT_MASK)) {
427 selection->clear();
428 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
430 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
432 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
433 }
435 /* Create green anchor */
436 p = event_dt;
437 spdc_endpoint_snap(pc, p, bevent.state);
438 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
439 }
440 spdc_pen_set_initial_point(pc, p);
441 } else {
443 /* Set end anchor */
444 pc->ea = anchor;
445 NR::Point p;
446 if (anchor) {
448 p = anchor->dp;
449 // we hit an anchor, will finish the curve (either with or without closing)
450 // in release handler
451 pc->state = SP_PEN_CONTEXT_CLOSE;
453 if (pc->green_anchor && pc->green_anchor->active) {
454 // we clicked on the current curve start, so close it even if
455 // we drag a handle away from it
456 dc->green_closed = TRUE;
457 }
458 ret = TRUE;
459 break;
461 } else {
463 p = event_dt;
464 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
465 spdc_pen_set_subsequent_point(pc, p, true);
466 }
468 }
469 pc->state = SP_PEN_CONTEXT_CONTROL;
470 ret = TRUE;
471 break;
472 case SP_PEN_CONTEXT_CONTROL:
473 g_warning("Button down in CONTROL state");
474 break;
475 case SP_PEN_CONTEXT_CLOSE:
476 g_warning("Button down in CLOSE state");
477 break;
478 default:
479 break;
480 }
481 break;
482 default:
483 break;
484 }
485 } else if (bevent.button == 3) {
486 if (pc->npoints != 0) {
488 spdc_pen_finish_segment(pc, event_dt, bevent.state);
489 if (pc->green_closed) {
490 // finishing at the start anchor, close curve
491 spdc_pen_finish(pc, TRUE);
492 } else {
493 // finishing at some other anchor, finish curve but not close
494 spdc_pen_finish(pc, FALSE);
495 }
497 ret = TRUE;
498 }
499 }
501 return ret;
502 }
504 /**
505 * Handle motion_notify event.
506 */
507 static gint
508 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
509 {
510 gint ret = FALSE;
512 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
514 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
515 // allow scrolling
516 return FALSE;
517 }
519 if (pc->events_disabled) {
520 // skip motion events if pen events are disabled
521 return FALSE;
522 }
524 NR::Point const event_w(mevent.x,
525 mevent.y);
526 if (pen_within_tolerance) {
527 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
528 "value", 0, 0, 100);
529 if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
530 return FALSE; // Do not drag if we're within tolerance from origin.
531 }
532 }
533 // Once the user has moved farther than tolerance from the original location
534 // (indicating they intend to move the object, not click), then always process the
535 // motion notify coordinates as given (no snapping back to origin)
536 pen_within_tolerance = false;
538 SPDesktop *const dt = pc->desktop;
539 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
540 /* Grab mouse, so release will not pass unnoticed */
541 pc->grab = SP_CANVAS_ITEM(dt->acetate);
542 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
543 GDK_BUTTON_RELEASE_MASK |
544 GDK_POINTER_MOTION_MASK ),
545 NULL, mevent.time);
546 }
548 /* Find desktop coordinates */
549 NR::Point p = dt->w2d(event_w);
551 /* Test, whether we hit any anchor */
552 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
554 switch (pc->mode) {
555 case SP_PEN_CONTEXT_MODE_CLICK:
556 switch (pc->state) {
557 case SP_PEN_CONTEXT_POINT:
558 if ( pc->npoints != 0 ) {
559 /* Only set point, if we are already appending */
560 spdc_endpoint_snap(pc, p, mevent.state);
561 spdc_pen_set_subsequent_point(pc, p, true);
562 ret = TRUE;
563 }
564 break;
565 case SP_PEN_CONTEXT_CONTROL:
566 case SP_PEN_CONTEXT_CLOSE:
567 /* Placing controls is last operation in CLOSE state */
568 spdc_endpoint_snap(pc, p, mevent.state);
569 spdc_pen_set_ctrl(pc, p, mevent.state);
570 ret = TRUE;
571 break;
572 case SP_PEN_CONTEXT_STOP:
573 /* This is perfectly valid */
574 break;
575 default:
576 break;
577 }
578 break;
579 case SP_PEN_CONTEXT_MODE_DRAG:
580 switch (pc->state) {
581 case SP_PEN_CONTEXT_POINT:
582 if ( pc->npoints > 0 ) {
583 /* Only set point, if we are already appending */
585 if (!anchor) { /* Snap node only if not hitting anchor */
586 spdc_endpoint_snap(pc, p, mevent.state);
587 }
589 spdc_pen_set_subsequent_point(pc, p, !anchor);
591 if (anchor && !pc->anchor_statusbar) {
592 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
593 pc->anchor_statusbar = true;
594 } else if (!anchor && pc->anchor_statusbar) {
595 pc->_message_context->clear();
596 pc->anchor_statusbar = false;
597 }
599 ret = TRUE;
600 } else {
601 if (anchor && !pc->anchor_statusbar) {
602 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
603 pc->anchor_statusbar = true;
604 } else if (!anchor && pc->anchor_statusbar) {
605 pc->_message_context->clear();
606 pc->anchor_statusbar = false;
607 }
608 }
609 break;
610 case SP_PEN_CONTEXT_CONTROL:
611 case SP_PEN_CONTEXT_CLOSE:
612 /* Placing controls is last operation in CLOSE state */
614 // snap the handle
615 spdc_endpoint_snap_handle(pc, p, mevent.state);
617 spdc_pen_set_ctrl(pc, p, mevent.state);
618 gobble_motion_events(GDK_BUTTON1_MASK);
619 ret = TRUE;
620 break;
621 case SP_PEN_CONTEXT_STOP:
622 /* This is perfectly valid */
623 break;
624 default:
625 break;
626 }
627 break;
628 default:
629 break;
630 }
631 return ret;
632 }
634 /**
635 * Handle mouse button release event.
636 */
637 static gint
638 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
639 {
640 if (pc->events_disabled) {
641 // skip event processing if events are disabled
642 return FALSE;
643 }
645 gint ret = FALSE;
646 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
647 if ( revent.button == 1 && !event_context->space_panning) {
649 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
651 NR::Point const event_w(revent.x,
652 revent.y);
653 /* Find desktop coordinates */
654 NR::Point p = pc->desktop->w2d(event_w);
656 /* Test whether we hit any anchor. */
657 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
659 switch (pc->mode) {
660 case SP_PEN_CONTEXT_MODE_CLICK:
661 switch (pc->state) {
662 case SP_PEN_CONTEXT_POINT:
663 if ( pc->npoints == 0 ) {
664 /* Start new thread only with button release */
665 if (anchor) {
666 p = anchor->dp;
667 }
668 pc->sa = anchor;
669 spdc_pen_set_initial_point(pc, p);
670 } else {
671 /* Set end anchor here */
672 pc->ea = anchor;
673 if (anchor) {
674 p = anchor->dp;
675 }
676 }
677 pc->state = SP_PEN_CONTEXT_CONTROL;
678 ret = TRUE;
679 break;
680 case SP_PEN_CONTEXT_CONTROL:
681 /* End current segment */
682 spdc_endpoint_snap(pc, p, revent.state);
683 spdc_pen_finish_segment(pc, p, revent.state);
684 pc->state = SP_PEN_CONTEXT_POINT;
685 ret = TRUE;
686 break;
687 case SP_PEN_CONTEXT_CLOSE:
688 /* End current segment */
689 if (!anchor) { /* Snap node only if not hitting anchor */
690 spdc_endpoint_snap(pc, p, revent.state);
691 }
692 spdc_pen_finish_segment(pc, p, revent.state);
693 spdc_pen_finish(pc, TRUE);
694 pc->state = SP_PEN_CONTEXT_POINT;
695 ret = TRUE;
696 break;
697 case SP_PEN_CONTEXT_STOP:
698 /* This is allowed, if we just cancelled curve */
699 pc->state = SP_PEN_CONTEXT_POINT;
700 ret = TRUE;
701 break;
702 default:
703 break;
704 }
705 break;
706 case SP_PEN_CONTEXT_MODE_DRAG:
707 switch (pc->state) {
708 case SP_PEN_CONTEXT_POINT:
709 case SP_PEN_CONTEXT_CONTROL:
710 spdc_endpoint_snap(pc, p, revent.state);
711 spdc_pen_finish_segment(pc, p, revent.state);
712 break;
713 case SP_PEN_CONTEXT_CLOSE:
714 spdc_endpoint_snap(pc, p, revent.state);
715 spdc_pen_finish_segment(pc, p, revent.state);
716 if (pc->green_closed) {
717 // finishing at the start anchor, close curve
718 spdc_pen_finish(pc, TRUE);
719 } else {
720 // finishing at some other anchor, finish curve but not close
721 spdc_pen_finish(pc, FALSE);
722 }
723 break;
724 case SP_PEN_CONTEXT_STOP:
725 /* This is allowed, if we just cancelled curve */
726 break;
727 default:
728 break;
729 }
730 pc->state = SP_PEN_CONTEXT_POINT;
731 ret = TRUE;
732 break;
733 default:
734 break;
735 }
737 if (pc->grab) {
738 /* Release grab now */
739 sp_canvas_item_ungrab(pc->grab, revent.time);
740 pc->grab = NULL;
741 }
743 ret = TRUE;
745 dc->green_closed = FALSE;
746 }
748 return ret;
749 }
751 static gint
752 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
753 {
754 gint ret = FALSE;
755 if (pc->npoints != 0 && bevent.button != 2) {
756 spdc_pen_finish(pc, FALSE);
757 ret = TRUE;
758 }
759 return ret;
760 }
762 void
763 pen_redraw_all (SPPenContext *const pc)
764 {
765 // green
766 if (pc->green_bpaths) {
767 // remove old piecewise green canvasitems
768 while (pc->green_bpaths) {
769 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
770 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
771 }
772 // one canvas bpath for all of green_curve
773 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
774 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
775 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
777 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
778 }
780 if (pc->green_anchor)
781 SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
783 sp_curve_reset(pc->red_curve);
784 sp_curve_moveto(pc->red_curve, pc->p[0]);
785 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
786 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
788 // handles
789 if (pc->p[0] != pc->p[1]) {
790 SP_CTRL(pc->c1)->moveto(pc->p[1]);
791 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
792 sp_canvas_item_show (pc->c1);
793 sp_canvas_item_show (pc->cl1);
794 } else {
795 sp_canvas_item_hide (pc->c1);
796 sp_canvas_item_hide (pc->cl1);
797 }
799 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
800 if (bpath) {
801 if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
802 SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
803 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
804 sp_canvas_item_show (pc->c0);
805 sp_canvas_item_show (pc->cl0);
806 } else {
807 sp_canvas_item_hide (pc->c0);
808 sp_canvas_item_hide (pc->cl0);
809 }
810 }
811 }
813 void
814 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
815 {
816 if (pc->npoints != 5)
817 return;
819 // green
820 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
821 if (bpath) {
822 if (bpath->code == NR_CURVETO) {
823 bpath->x2 += x;
824 bpath->y2 += y;
825 }
826 bpath->x3 += x;
827 bpath->y3 += y;
828 } else {
829 // start anchor too
830 if (pc->green_anchor) {
831 pc->green_anchor->dp += NR::Point(x, y);
832 }
833 }
835 // red
836 pc->p[0] += NR::Point(x, y);
837 pc->p[1] += NR::Point(x, y);
838 pen_redraw_all(pc);
839 }
841 void
842 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
843 {
844 pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
845 }
847 void
848 pen_lastpoint_tocurve (SPPenContext *const pc)
849 {
850 if (pc->npoints != 5)
851 return;
853 // red
854 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
855 if (bpath && bpath->code == NR_CURVETO) {
856 pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
857 } else {
858 pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
859 }
861 pen_redraw_all(pc);
862 }
864 void
865 pen_lastpoint_toline (SPPenContext *const pc)
866 {
867 if (pc->npoints != 5)
868 return;
870 pc->p[1] = pc->p[0];
872 pen_redraw_all(pc);
873 }
876 static gint
877 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
878 {
879 gint ret = FALSE;
880 gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
882 switch (get_group0_keyval (&event->key)) {
884 case GDK_Left: // move last point left
885 case GDK_KP_Left:
886 case GDK_KP_4:
887 if (!MOD__CTRL) { // not ctrl
888 if (MOD__ALT) { // alt
889 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
890 else pen_lastpoint_move_screen(pc, -1, 0); // no shift
891 }
892 else { // no alt
893 if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
894 else pen_lastpoint_move(pc, -nudge, 0); // no shift
895 }
896 ret = TRUE;
897 }
898 break;
899 case GDK_Up: // move last point up
900 case GDK_KP_Up:
901 case GDK_KP_8:
902 if (!MOD__CTRL) { // not ctrl
903 if (MOD__ALT) { // alt
904 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
905 else pen_lastpoint_move_screen(pc, 0, 1); // no shift
906 }
907 else { // no alt
908 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
909 else pen_lastpoint_move(pc, 0, nudge); // no shift
910 }
911 ret = TRUE;
912 }
913 break;
914 case GDK_Right: // move last point right
915 case GDK_KP_Right:
916 case GDK_KP_6:
917 if (!MOD__CTRL) { // not ctrl
918 if (MOD__ALT) { // alt
919 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
920 else pen_lastpoint_move_screen(pc, 1, 0); // no shift
921 }
922 else { // no alt
923 if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
924 else pen_lastpoint_move(pc, nudge, 0); // no shift
925 }
926 ret = TRUE;
927 }
928 break;
929 case GDK_Down: // move last point down
930 case GDK_KP_Down:
931 case GDK_KP_2:
932 if (!MOD__CTRL) { // not ctrl
933 if (MOD__ALT) { // alt
934 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
935 else pen_lastpoint_move_screen(pc, 0, -1); // no shift
936 }
937 else { // no alt
938 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
939 else pen_lastpoint_move(pc, 0, -nudge); // no shift
940 }
941 ret = TRUE;
942 }
943 break;
945 case GDK_U:
946 case GDK_u:
947 if (MOD__SHIFT_ONLY) {
948 pen_lastpoint_tocurve(pc);
949 ret = TRUE;
950 }
951 break;
952 case GDK_L:
953 case GDK_l:
954 if (MOD__SHIFT_ONLY) {
955 pen_lastpoint_toline(pc);
956 ret = TRUE;
957 }
958 break;
960 case GDK_Return:
961 case GDK_KP_Enter:
962 if (pc->npoints != 0) {
963 spdc_pen_finish(pc, FALSE);
964 ret = TRUE;
965 }
966 break;
967 case GDK_Escape:
968 if (pc->npoints != 0) {
969 // if drawing, cancel, otherwise pass it up for deselecting
970 pen_cancel (pc);
971 ret = TRUE;
972 }
973 break;
974 case GDK_z:
975 case GDK_Z:
976 if (MOD__CTRL_ONLY && pc->npoints != 0) {
977 // if drawing, cancel, otherwise pass it up for undo
978 pen_cancel (pc);
979 ret = TRUE;
980 }
981 break;
982 case GDK_BackSpace:
983 case GDK_Delete:
984 case GDK_KP_Delete:
985 if (sp_curve_is_empty(pc->green_curve)) {
986 pen_cancel (pc);
987 ret = TRUE;
988 } else {
989 /* Reset red curve */
990 sp_curve_reset(pc->red_curve);
991 /* Destroy topmost green bpath */
992 if (pc->green_bpaths) {
993 if (pc->green_bpaths->data)
994 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
995 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
996 }
997 /* Get last segment */
998 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
999 gint const e = SP_CURVE_LENGTH(pc->green_curve);
1000 if ( e < 2 ) {
1001 g_warning("Green curve length is %d", e);
1002 break;
1003 }
1004 pc->p[0] = p[e - 2].c(3);
1005 if (p[e - 1].code == NR_CURVETO) {
1006 pc->p[1] = p[e - 1].c(1);
1007 } else {
1008 pc->p[1] = pc->p[0];
1009 }
1010 NR::Point const pt(( pc->npoints < 4
1011 ? p[e - 1].c(3)
1012 : pc->p[3] ));
1013 pc->npoints = 2;
1014 sp_curve_backspace(pc->green_curve);
1015 sp_canvas_item_hide(pc->c0);
1016 sp_canvas_item_hide(pc->c1);
1017 sp_canvas_item_hide(pc->cl0);
1018 sp_canvas_item_hide(pc->cl1);
1019 pc->state = SP_PEN_CONTEXT_POINT;
1020 spdc_pen_set_subsequent_point(pc, pt, true);
1021 ret = TRUE;
1022 }
1023 break;
1024 default:
1025 break;
1026 }
1027 return ret;
1028 }
1030 static void
1031 spdc_reset_colors(SPPenContext *pc)
1032 {
1033 /* Red */
1034 sp_curve_reset(pc->red_curve);
1035 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1036 /* Blue */
1037 sp_curve_reset(pc->blue_curve);
1038 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1039 /* Green */
1040 while (pc->green_bpaths) {
1041 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1042 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1043 }
1044 sp_curve_reset(pc->green_curve);
1045 if (pc->green_anchor) {
1046 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1047 }
1048 pc->sa = NULL;
1049 pc->ea = NULL;
1050 pc->npoints = 0;
1051 pc->red_curve_is_valid = false;
1052 }
1055 static void
1056 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1057 {
1058 g_assert( pc->npoints == 0 );
1060 pc->p[0] = p;
1061 pc->p[1] = p;
1062 pc->npoints = 2;
1063 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1065 sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1066 }
1068 static void
1069 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1070 {
1071 g_assert( pc->npoints != 0 );
1072 /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1074 pc->p[2] = p;
1075 pc->p[3] = p;
1076 pc->p[4] = p;
1077 pc->npoints = 5;
1078 sp_curve_reset(pc->red_curve);
1079 sp_curve_moveto(pc->red_curve, pc->p[0]);
1080 bool is_curve;
1081 if ( (pc->onlycurves)
1082 || ( pc->p[1] != pc->p[0] ) )
1083 {
1084 sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1085 is_curve = true;
1086 } else {
1087 sp_curve_lineto(pc->red_curve, p);
1088 is_curve = false;
1089 }
1091 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1093 if (statusbar) {
1094 // status text
1095 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1096 NR::Point rel = p - pc->p[0];
1097 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1098 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1099 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1100 angle = angle_to_compass (angle);
1101 pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>%s</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path"), is_curve? "Curve segment" : "Line segment", angle, dist->str);
1102 g_string_free(dist, FALSE);
1103 }
1104 }
1106 static void
1107 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1108 {
1109 sp_canvas_item_show(pc->c1);
1110 sp_canvas_item_show(pc->cl1);
1112 if ( pc->npoints == 2 ) {
1113 pc->p[1] = p;
1114 sp_canvas_item_hide(pc->c0);
1115 sp_canvas_item_hide(pc->cl0);
1116 SP_CTRL(pc->c1)->moveto(pc->p[1]);
1117 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1119 // status text
1120 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1121 NR::Point rel = p - pc->p[0];
1122 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1123 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1124 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1125 angle = angle_to_compass (angle);
1126 pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle"), angle, dist->str);
1127 g_string_free(dist, FALSE);
1129 } else if ( pc->npoints == 5 ) {
1130 pc->p[4] = p;
1131 sp_canvas_item_show(pc->c0);
1132 sp_canvas_item_show(pc->cl0);
1133 bool is_symm = false;
1134 if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1135 ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) && !( state & GDK_SHIFT_MASK ) ) ) {
1136 NR::Point delta = p - pc->p[3];
1137 pc->p[2] = pc->p[3] - delta;
1138 is_symm = true;
1139 sp_curve_reset(pc->red_curve);
1140 sp_curve_moveto(pc->red_curve, pc->p[0]);
1141 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1142 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1143 }
1144 SP_CTRL(pc->c0)->moveto(pc->p[2]);
1145 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1146 SP_CTRL(pc->c1)->moveto(pc->p[4]);
1147 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1149 // status text
1150 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1151 NR::Point rel = p - pc->p[3];
1152 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1153 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1154 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1155 angle = angle_to_compass (angle);
1156 pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>%s</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only"), is_symm? "Curve handle, symmetric" : "Curve handle", angle, dist->str);
1157 g_string_free(dist, FALSE);
1159 } else {
1160 g_warning("Something bad happened - npoints is %d", pc->npoints);
1161 }
1162 }
1164 static void
1165 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1166 {
1167 if (!sp_curve_empty(pc->red_curve)) {
1168 sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1169 SPCurve *curve = sp_curve_copy(pc->red_curve);
1170 /// \todo fixme:
1171 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1172 sp_curve_unref(curve);
1173 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1175 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1177 pc->p[0] = pc->p[3];
1178 pc->p[1] = pc->p[4];
1179 pc->npoints = 2;
1181 sp_curve_reset(pc->red_curve);
1182 }
1183 }
1185 static void
1186 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1187 {
1188 pen_disable_events(pc);
1190 SPDesktop *const desktop = pc->desktop;
1191 pc->_message_context->clear();
1192 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1194 sp_curve_reset(pc->red_curve);
1195 spdc_concat_colors_and_flush(pc, closed);
1196 pc->sa = NULL;
1197 pc->ea = NULL;
1199 pc->npoints = 0;
1200 pc->state = SP_PEN_CONTEXT_POINT;
1202 sp_canvas_item_hide(pc->c0);
1203 sp_canvas_item_hide(pc->c1);
1204 sp_canvas_item_hide(pc->cl0);
1205 sp_canvas_item_hide(pc->cl1);
1207 if (pc->green_anchor) {
1208 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1209 }
1212 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1214 pen_enable_events(pc);
1215 }
1217 static void
1218 pen_disable_events(SPPenContext *const pc) {
1219 pc->events_disabled++;
1220 }
1222 static void
1223 pen_enable_events(SPPenContext *const pc) {
1224 g_return_if_fail(pc->events_disabled != 0);
1226 pc->events_disabled--;
1227 }
1229 /*
1230 Local Variables:
1231 mode:c++
1232 c-file-style:"stroustrup"
1233 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1234 indent-tabs-mode:nil
1235 fill-column:99
1236 End:
1237 */
1238 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :