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