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);
52 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
53 static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
54 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
55 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
57 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
59 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
60 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
61 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
62 static gint pen_handle_2button_press(SPPenContext *const pc);
63 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
64 static void spdc_reset_colors(SPPenContext *pc);
66 static void pen_disable_events(SPPenContext *const pc);
67 static void pen_enable_events(SPPenContext *const pc);
69 static NR::Point pen_drag_origin_w(0, 0);
70 static bool pen_within_tolerance = false;
72 static SPDrawContextClass *pen_parent_class;
74 /**
75 * Register SPPenContext with Gdk and return its type.
76 */
77 GType
78 sp_pen_context_get_type(void)
79 {
80 static GType type = 0;
81 if (!type) {
82 GTypeInfo info = {
83 sizeof(SPPenContextClass),
84 NULL, NULL,
85 (GClassInitFunc) sp_pen_context_class_init,
86 NULL, NULL,
87 sizeof(SPPenContext),
88 4,
89 (GInstanceInitFunc) sp_pen_context_init,
90 NULL, /* value_table */
91 };
92 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
93 }
94 return type;
95 }
97 /**
98 * Initialize the SPPenContext vtable.
99 */
100 static void
101 sp_pen_context_class_init(SPPenContextClass *klass)
102 {
103 GObjectClass *object_class;
104 SPEventContextClass *event_context_class;
106 object_class = (GObjectClass *) klass;
107 event_context_class = (SPEventContextClass *) klass;
109 pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
111 object_class->dispose = sp_pen_context_dispose;
113 event_context_class->setup = sp_pen_context_setup;
114 event_context_class->finish = sp_pen_context_finish;
115 event_context_class->set = sp_pen_context_set;
116 event_context_class->root_handler = sp_pen_context_root_handler;
117 }
119 /**
120 * Callback to initialize SPPenContext object.
121 */
122 static void
123 sp_pen_context_init(SPPenContext *pc)
124 {
126 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
128 event_context->cursor_shape = cursor_pen_xpm;
129 event_context->hot_x = 4;
130 event_context->hot_y = 4;
132 pc->npoints = 0;
133 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
134 pc->state = SP_PEN_CONTEXT_POINT;
136 pc->c0 = NULL;
137 pc->c1 = NULL;
138 pc->cl0 = NULL;
139 pc->cl1 = NULL;
141 pc->events_disabled = 0;
142 }
144 /**
145 * Callback to destroy the SPPenContext object's members and itself.
146 */
147 static void
148 sp_pen_context_dispose(GObject *object)
149 {
150 SPPenContext *pc;
152 pc = SP_PEN_CONTEXT(object);
154 if (pc->c0) {
155 gtk_object_destroy(GTK_OBJECT(pc->c0));
156 pc->c0 = NULL;
157 }
158 if (pc->c1) {
159 gtk_object_destroy(GTK_OBJECT(pc->c1));
160 pc->c1 = NULL;
161 }
162 if (pc->cl0) {
163 gtk_object_destroy(GTK_OBJECT(pc->cl0));
164 pc->cl0 = NULL;
165 }
166 if (pc->cl1) {
167 gtk_object_destroy(GTK_OBJECT(pc->cl1));
168 pc->cl1 = NULL;
169 }
171 G_OBJECT_CLASS(pen_parent_class)->dispose(object);
172 }
174 /**
175 * Callback to initialize SPPenContext object.
176 */
177 static void
178 sp_pen_context_setup(SPEventContext *ec)
179 {
180 SPPenContext *pc;
182 pc = SP_PEN_CONTEXT(ec);
184 if (((SPEventContextClass *) pen_parent_class)->setup) {
185 ((SPEventContextClass *) pen_parent_class)->setup(ec);
186 }
188 /* Pen indicators */
189 pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
190 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
191 pc->c1 = 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->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
194 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
195 pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
196 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
198 sp_canvas_item_hide(pc->c0);
199 sp_canvas_item_hide(pc->c1);
200 sp_canvas_item_hide(pc->cl0);
201 sp_canvas_item_hide(pc->cl1);
203 sp_event_context_read(ec, "mode");
205 pc->anchor_statusbar = false;
207 if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
208 ec->enableSelectionCue();
209 }
210 }
212 static void
213 pen_cancel (SPPenContext *const pc)
214 {
215 pc->state = SP_PEN_CONTEXT_STOP;
216 spdc_reset_colors(pc);
217 sp_canvas_item_hide(pc->c0);
218 sp_canvas_item_hide(pc->c1);
219 sp_canvas_item_hide(pc->cl0);
220 sp_canvas_item_hide(pc->cl1);
221 pc->_message_context->clear();
222 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
224 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
225 }
227 /**
228 * Finalization callback.
229 */
230 static void
231 sp_pen_context_finish(SPEventContext *ec)
232 {
233 SPPenContext *pc = SP_PEN_CONTEXT(ec);
235 if (pc->npoints != 0) {
236 pen_cancel (pc);
237 }
239 if (((SPEventContextClass *) pen_parent_class)->finish) {
240 ((SPEventContextClass *) pen_parent_class)->finish(ec);
241 }
242 }
244 /**
245 * Callback that sets key to value in pen context.
246 */
247 static void
248 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
249 {
250 SPPenContext *pc = SP_PEN_CONTEXT(ec);
252 if (!strcmp(key, "mode")) {
253 if ( val && !strcmp(val, "drag") ) {
254 pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
255 } else {
256 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
257 }
258 }
259 }
261 /**
262 * Snaps new node relative to the previous node.
263 */
264 static void
265 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
266 {
267 if (pc->npoints > 0) {
268 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
269 }
271 spdc_endpoint_snap_free(pc, p, state);
272 }
274 /**
275 * Snaps new node's handle relative to the new node.
276 */
277 static void
278 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
279 {
280 g_return_if_fail(( pc->npoints == 2 ||
281 pc->npoints == 5 ));
283 spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
284 spdc_endpoint_snap_free(pc, p, state);
285 }
287 /**
288 * Callback to handle all pen events.
289 */
290 static gint
291 sp_pen_context_root_handler(SPEventContext *ec, 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;
302 case GDK_MOTION_NOTIFY:
303 ret = pen_handle_motion_notify(pc, event->motion);
304 break;
306 case GDK_BUTTON_RELEASE:
307 ret = pen_handle_button_release(pc, event->button);
308 break;
310 case GDK_2BUTTON_PRESS:
311 ret = pen_handle_2button_press(pc);
312 break;
314 case GDK_KEY_PRESS:
315 ret = pen_handle_key_press(pc, event);
316 break;
318 default:
319 break;
320 }
322 if (!ret) {
323 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
324 = ((SPEventContextClass *) pen_parent_class)->root_handler;
325 if (parent_root_handler) {
326 ret = parent_root_handler(ec, event);
327 }
328 }
330 return ret;
331 }
333 /**
334 * Handle mouse button press event.
335 */
336 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
337 {
338 if (pc->events_disabled) {
339 // skip event processing if events are disabled
340 return FALSE;
341 }
343 gint ret = FALSE;
344 if (bevent.button == 1) {
346 SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
347 SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
349 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
350 return TRUE;
351 }
353 NR::Point const event_w(bevent.x, bevent.y);
354 pen_drag_origin_w = event_w;
355 pen_within_tolerance = true;
357 /* Test whether we hit any anchor. */
358 SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
360 NR::Point const event_dt(desktop->w2d(event_w));
361 switch (pc->mode) {
362 case SP_PEN_CONTEXT_MODE_CLICK:
363 /* In click mode we add point on release */
364 switch (pc->state) {
365 case SP_PEN_CONTEXT_POINT:
366 case SP_PEN_CONTEXT_CONTROL:
367 case SP_PEN_CONTEXT_CLOSE:
368 break;
369 case SP_PEN_CONTEXT_STOP:
370 /* This is allowed, if we just cancelled curve */
371 pc->state = SP_PEN_CONTEXT_POINT;
372 break;
373 default:
374 break;
375 }
376 break;
377 case SP_PEN_CONTEXT_MODE_DRAG:
378 switch (pc->state) {
379 case SP_PEN_CONTEXT_STOP:
380 /* This is allowed, if we just cancelled curve */
381 case SP_PEN_CONTEXT_POINT:
382 if (pc->npoints == 0) {
384 /* Set start anchor */
385 pc->sa = anchor;
386 NR::Point p;
387 if (anchor) {
389 /* Adjust point to anchor if needed */
390 p = anchor->dp;
391 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
393 } else {
395 // This is the first click of a new curve; deselect item so that
396 // this curve is not combined with it (unless it is drawn from its
397 // anchor, which is handled by the sibling branch above)
398 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
399 if (!(bevent.state & GDK_SHIFT_MASK)) {
401 selection->clear();
402 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
404 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
406 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
407 }
409 /* Create green anchor */
410 p = event_dt;
411 spdc_endpoint_snap(pc, p, bevent.state);
412 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
413 }
414 spdc_pen_set_initial_point(pc, p);
415 } else {
417 /* Set end anchor */
418 pc->ea = anchor;
419 NR::Point p;
420 if (anchor) {
422 p = anchor->dp;
423 // we hit an anchor, will finish the curve (either with or without closing)
424 // in release handler
425 pc->state = SP_PEN_CONTEXT_CLOSE;
427 if (pc->green_anchor && pc->green_anchor->active) {
428 // we clicked on the current curve start, so close it even if
429 // we drag a handle away from it
430 dc->green_closed = TRUE;
431 }
432 ret = TRUE;
433 break;
435 } else {
437 p = event_dt;
438 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
439 spdc_pen_set_subsequent_point(pc, p, true);
440 }
442 }
443 pc->state = SP_PEN_CONTEXT_CONTROL;
444 ret = TRUE;
445 break;
446 case SP_PEN_CONTEXT_CONTROL:
447 g_warning("Button down in CONTROL state");
448 break;
449 case SP_PEN_CONTEXT_CLOSE:
450 g_warning("Button down in CLOSE state");
451 break;
452 default:
453 break;
454 }
455 break;
456 default:
457 break;
458 }
459 } else if (bevent.button == 3) {
460 if (pc->npoints != 0) {
461 spdc_pen_finish(pc, FALSE);
462 ret = TRUE;
463 }
464 }
466 return ret;
467 }
469 /**
470 * Handle motion_notify event.
471 */
472 static gint
473 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
474 {
475 gint ret = FALSE;
477 if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
478 // allow middle-button scrolling
479 return FALSE;
480 }
482 if (pc->events_disabled) {
483 // skip motion events if pen events are disabled
484 return FALSE;
485 }
487 NR::Point const event_w(mevent.x,
488 mevent.y);
489 if (pen_within_tolerance) {
490 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
491 "value", 0, 0, 100);
492 if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
493 return FALSE; // Do not drag if we're within tolerance from origin.
494 }
495 }
496 // Once the user has moved farther than tolerance from the original location
497 // (indicating they intend to move the object, not click), then always process the
498 // motion notify coordinates as given (no snapping back to origin)
499 pen_within_tolerance = false;
501 SPDesktop *const dt = pc->desktop;
502 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
503 /* Grab mouse, so release will not pass unnoticed */
504 pc->grab = SP_CANVAS_ITEM(dt->acetate);
505 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
506 GDK_BUTTON_RELEASE_MASK |
507 GDK_POINTER_MOTION_MASK ),
508 NULL, mevent.time);
509 }
511 /* Find desktop coordinates */
512 NR::Point p = dt->w2d(event_w);
514 /* Test, whether we hit any anchor */
515 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
517 switch (pc->mode) {
518 case SP_PEN_CONTEXT_MODE_CLICK:
519 switch (pc->state) {
520 case SP_PEN_CONTEXT_POINT:
521 if ( pc->npoints != 0 ) {
522 /* Only set point, if we are already appending */
523 spdc_endpoint_snap(pc, p, mevent.state);
524 spdc_pen_set_subsequent_point(pc, p, true);
525 ret = TRUE;
526 }
527 break;
528 case SP_PEN_CONTEXT_CONTROL:
529 case SP_PEN_CONTEXT_CLOSE:
530 /* Placing controls is last operation in CLOSE state */
531 spdc_endpoint_snap(pc, p, mevent.state);
532 spdc_pen_set_ctrl(pc, p, mevent.state);
533 ret = TRUE;
534 break;
535 case SP_PEN_CONTEXT_STOP:
536 /* This is perfectly valid */
537 break;
538 default:
539 break;
540 }
541 break;
542 case SP_PEN_CONTEXT_MODE_DRAG:
543 switch (pc->state) {
544 case SP_PEN_CONTEXT_POINT:
545 if ( pc->npoints > 0 ) {
546 /* Only set point, if we are already appending */
548 if (!anchor) { /* Snap node only if not hitting anchor */
549 spdc_endpoint_snap(pc, p, mevent.state);
550 }
552 spdc_pen_set_subsequent_point(pc, p, !anchor);
554 if (anchor && !pc->anchor_statusbar) {
555 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
556 pc->anchor_statusbar = true;
557 } else if (!anchor && pc->anchor_statusbar) {
558 pc->_message_context->clear();
559 pc->anchor_statusbar = false;
560 }
562 ret = TRUE;
563 } else {
564 if (anchor && !pc->anchor_statusbar) {
565 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
566 pc->anchor_statusbar = true;
567 } else if (!anchor && pc->anchor_statusbar) {
568 pc->_message_context->clear();
569 pc->anchor_statusbar = false;
570 }
571 }
572 break;
573 case SP_PEN_CONTEXT_CONTROL:
574 case SP_PEN_CONTEXT_CLOSE:
575 /* Placing controls is last operation in CLOSE state */
577 // snap the handle
578 spdc_endpoint_snap_handle(pc, p, mevent.state);
580 spdc_pen_set_ctrl(pc, p, mevent.state);
581 ret = TRUE;
582 break;
583 case SP_PEN_CONTEXT_STOP:
584 /* This is perfectly valid */
585 break;
586 default:
587 break;
588 }
589 break;
590 default:
591 break;
592 }
593 return ret;
594 }
596 /**
597 * Handle mouse button release event.
598 */
599 static gint
600 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
601 {
602 if (pc->events_disabled) {
603 // skip event processing if events are disabled
604 return FALSE;
605 }
607 gint ret = FALSE;
608 if ( revent.button == 1 ) {
610 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
612 NR::Point const event_w(revent.x,
613 revent.y);
614 /* Find desktop coordinates */
615 NR::Point p = pc->desktop->w2d(event_w);
617 /* Test whether we hit any anchor. */
618 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
620 switch (pc->mode) {
621 case SP_PEN_CONTEXT_MODE_CLICK:
622 switch (pc->state) {
623 case SP_PEN_CONTEXT_POINT:
624 if ( pc->npoints == 0 ) {
625 /* Start new thread only with button release */
626 if (anchor) {
627 p = anchor->dp;
628 }
629 pc->sa = anchor;
630 spdc_pen_set_initial_point(pc, p);
631 } else {
632 /* Set end anchor here */
633 pc->ea = anchor;
634 if (anchor) {
635 p = anchor->dp;
636 }
637 }
638 pc->state = SP_PEN_CONTEXT_CONTROL;
639 ret = TRUE;
640 break;
641 case SP_PEN_CONTEXT_CONTROL:
642 /* End current segment */
643 spdc_endpoint_snap(pc, p, revent.state);
644 spdc_pen_finish_segment(pc, p, revent.state);
645 pc->state = SP_PEN_CONTEXT_POINT;
646 ret = TRUE;
647 break;
648 case SP_PEN_CONTEXT_CLOSE:
649 /* End current segment */
650 if (!anchor) { /* Snap node only if not hitting anchor */
651 spdc_endpoint_snap(pc, p, revent.state);
652 }
653 spdc_pen_finish_segment(pc, p, revent.state);
654 spdc_pen_finish(pc, TRUE);
655 pc->state = SP_PEN_CONTEXT_POINT;
656 ret = TRUE;
657 break;
658 case SP_PEN_CONTEXT_STOP:
659 /* This is allowed, if we just cancelled curve */
660 pc->state = SP_PEN_CONTEXT_POINT;
661 ret = TRUE;
662 break;
663 default:
664 break;
665 }
666 break;
667 case SP_PEN_CONTEXT_MODE_DRAG:
668 switch (pc->state) {
669 case SP_PEN_CONTEXT_POINT:
670 case SP_PEN_CONTEXT_CONTROL:
671 spdc_endpoint_snap(pc, p, revent.state);
672 spdc_pen_finish_segment(pc, p, revent.state);
673 break;
674 case SP_PEN_CONTEXT_CLOSE:
675 spdc_endpoint_snap(pc, p, revent.state);
676 spdc_pen_finish_segment(pc, p, revent.state);
677 if (pc->green_closed) {
678 // finishing at the start anchor, close curve
679 spdc_pen_finish(pc, TRUE);
680 } else {
681 // finishing at some other anchor, finish curve but not close
682 spdc_pen_finish(pc, FALSE);
683 }
684 break;
685 case SP_PEN_CONTEXT_STOP:
686 /* This is allowed, if we just cancelled curve */
687 break;
688 default:
689 break;
690 }
691 pc->state = SP_PEN_CONTEXT_POINT;
692 ret = TRUE;
693 break;
694 default:
695 break;
696 }
698 if (pc->grab) {
699 /* Release grab now */
700 sp_canvas_item_ungrab(pc->grab, revent.time);
701 pc->grab = NULL;
702 }
704 ret = TRUE;
706 dc->green_closed = FALSE;
707 }
709 return ret;
710 }
712 static gint
713 pen_handle_2button_press(SPPenContext *const pc)
714 {
715 gint ret = FALSE;
716 if (pc->npoints != 0) {
717 spdc_pen_finish(pc, FALSE);
718 ret = TRUE;
719 }
720 return ret;
721 }
723 void
724 pen_redraw_all (SPPenContext *const pc)
725 {
726 // green
727 if (pc->green_bpaths) {
728 // remove old piecewise green canvasitems
729 while (pc->green_bpaths) {
730 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
731 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
732 }
733 // one canvas bpath for all of green_curve
734 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
735 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
736 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
738 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
739 }
741 if (pc->green_anchor)
742 SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
744 sp_curve_reset(pc->red_curve);
745 sp_curve_moveto(pc->red_curve, pc->p[0]);
746 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
747 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
749 // handles
750 if (pc->p[0] != pc->p[1]) {
751 SP_CTRL(pc->c1)->moveto(pc->p[1]);
752 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
753 sp_canvas_item_show (pc->c1);
754 sp_canvas_item_show (pc->cl1);
755 } else {
756 sp_canvas_item_hide (pc->c1);
757 sp_canvas_item_hide (pc->cl1);
758 }
760 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
761 if (bpath) {
762 if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
763 SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
764 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
765 sp_canvas_item_show (pc->c0);
766 sp_canvas_item_show (pc->cl0);
767 } else {
768 sp_canvas_item_hide (pc->c0);
769 sp_canvas_item_hide (pc->cl0);
770 }
771 }
772 }
774 void
775 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
776 {
777 if (pc->npoints != 5)
778 return;
780 // green
781 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
782 if (bpath) {
783 if (bpath->code == NR_CURVETO) {
784 bpath->x2 += x;
785 bpath->y2 += y;
786 }
787 bpath->x3 += x;
788 bpath->y3 += y;
789 } else {
790 // start anchor too
791 if (pc->green_anchor) {
792 pc->green_anchor->dp += NR::Point(x, y);
793 }
794 }
796 // red
797 pc->p[0] += NR::Point(x, y);
798 pc->p[1] += NR::Point(x, y);
799 pen_redraw_all(pc);
800 }
802 void
803 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
804 {
805 pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
806 }
808 void
809 pen_lastpoint_tocurve (SPPenContext *const pc)
810 {
811 if (pc->npoints != 5)
812 return;
814 // red
815 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
816 if (bpath && bpath->code == NR_CURVETO) {
817 pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
818 } else {
819 pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
820 }
822 pen_redraw_all(pc);
823 }
825 void
826 pen_lastpoint_toline (SPPenContext *const pc)
827 {
828 if (pc->npoints != 5)
829 return;
831 pc->p[1] = pc->p[0];
833 pen_redraw_all(pc);
834 }
837 static gint
838 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
839 {
840 gint ret = FALSE;
841 gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
843 switch (get_group0_keyval (&event->key)) {
845 case GDK_Left: // move last point left
846 case GDK_KP_Left:
847 case GDK_KP_4:
848 if (!MOD__CTRL) { // not ctrl
849 if (MOD__ALT) { // alt
850 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
851 else pen_lastpoint_move_screen(pc, -1, 0); // no shift
852 }
853 else { // no alt
854 if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
855 else pen_lastpoint_move(pc, -nudge, 0); // no shift
856 }
857 ret = TRUE;
858 }
859 break;
860 case GDK_Up: // move last point up
861 case GDK_KP_Up:
862 case GDK_KP_8:
863 if (!MOD__CTRL) { // not ctrl
864 if (MOD__ALT) { // alt
865 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
866 else pen_lastpoint_move_screen(pc, 0, 1); // no shift
867 }
868 else { // no alt
869 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
870 else pen_lastpoint_move(pc, 0, nudge); // no shift
871 }
872 ret = TRUE;
873 }
874 break;
875 case GDK_Right: // move last point right
876 case GDK_KP_Right:
877 case GDK_KP_6:
878 if (!MOD__CTRL) { // not ctrl
879 if (MOD__ALT) { // alt
880 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
881 else pen_lastpoint_move_screen(pc, 1, 0); // no shift
882 }
883 else { // no alt
884 if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
885 else pen_lastpoint_move(pc, nudge, 0); // no shift
886 }
887 ret = TRUE;
888 }
889 break;
890 case GDK_Down: // move last point down
891 case GDK_KP_Down:
892 case GDK_KP_2:
893 if (!MOD__CTRL) { // not ctrl
894 if (MOD__ALT) { // alt
895 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
896 else pen_lastpoint_move_screen(pc, 0, -1); // no shift
897 }
898 else { // no alt
899 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
900 else pen_lastpoint_move(pc, 0, -nudge); // no shift
901 }
902 ret = TRUE;
903 }
904 break;
906 case GDK_U:
907 case GDK_u:
908 if (MOD__SHIFT_ONLY) {
909 pen_lastpoint_tocurve(pc);
910 ret = TRUE;
911 }
912 break;
913 case GDK_L:
914 case GDK_l:
915 if (MOD__SHIFT_ONLY) {
916 pen_lastpoint_toline(pc);
917 ret = TRUE;
918 }
919 break;
921 case GDK_Return:
922 case GDK_KP_Enter:
923 if (pc->npoints != 0) {
924 spdc_pen_finish(pc, FALSE);
925 ret = TRUE;
926 }
927 break;
928 case GDK_Escape:
929 if (pc->npoints != 0) {
930 // if drawing, cancel, otherwise pass it up for deselecting
931 pen_cancel (pc);
932 ret = TRUE;
933 }
934 break;
935 case GDK_z:
936 case GDK_Z:
937 if (MOD__CTRL_ONLY && pc->npoints != 0) {
938 // if drawing, cancel, otherwise pass it up for undo
939 pen_cancel (pc);
940 ret = TRUE;
941 }
942 break;
943 case GDK_BackSpace:
944 case GDK_Delete:
945 case GDK_KP_Delete:
946 if (sp_curve_is_empty(pc->green_curve)) {
947 pen_cancel (pc);
948 ret = TRUE;
949 } else {
950 /* Reset red curve */
951 sp_curve_reset(pc->red_curve);
952 /* Destroy topmost green bpath */
953 if (pc->green_bpaths) {
954 if (pc->green_bpaths->data)
955 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
956 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
957 }
958 /* Get last segment */
959 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
960 gint const e = SP_CURVE_LENGTH(pc->green_curve);
961 if ( e < 2 ) {
962 g_warning("Green curve length is %d", e);
963 break;
964 }
965 pc->p[0] = p[e - 2].c(3);
966 if (p[e - 1].code == NR_CURVETO) {
967 pc->p[1] = p[e - 1].c(1);
968 } else {
969 pc->p[1] = pc->p[0];
970 }
971 NR::Point const pt(( pc->npoints < 4
972 ? p[e - 1].c(3)
973 : pc->p[3] ));
974 pc->npoints = 2;
975 sp_curve_backspace(pc->green_curve);
976 sp_canvas_item_hide(pc->c0);
977 sp_canvas_item_hide(pc->c1);
978 sp_canvas_item_hide(pc->cl0);
979 sp_canvas_item_hide(pc->cl1);
980 pc->state = SP_PEN_CONTEXT_POINT;
981 spdc_pen_set_subsequent_point(pc, pt, true);
982 ret = TRUE;
983 }
984 break;
985 default:
986 break;
987 }
988 return ret;
989 }
991 static void
992 spdc_reset_colors(SPPenContext *pc)
993 {
994 /* Red */
995 sp_curve_reset(pc->red_curve);
996 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
997 /* Blue */
998 sp_curve_reset(pc->blue_curve);
999 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1000 /* Green */
1001 while (pc->green_bpaths) {
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 sp_curve_reset(pc->green_curve);
1006 if (pc->green_anchor) {
1007 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1008 }
1009 pc->sa = NULL;
1010 pc->ea = NULL;
1011 pc->npoints = 0;
1012 pc->red_curve_is_valid = false;
1013 }
1016 static void
1017 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1018 {
1019 g_assert( pc->npoints == 0 );
1021 pc->p[0] = p;
1022 pc->p[1] = p;
1023 pc->npoints = 2;
1024 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1026 sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1027 }
1029 static void
1030 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
1031 {
1032 g_assert( pc->npoints != 0 );
1033 /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1035 pc->p[2] = p;
1036 pc->p[3] = p;
1037 pc->p[4] = p;
1038 pc->npoints = 5;
1039 sp_curve_reset(pc->red_curve);
1040 sp_curve_moveto(pc->red_curve, pc->p[0]);
1041 bool is_curve;
1042 if ( (pc->onlycurves)
1043 || ( pc->p[1] != pc->p[0] ) )
1044 {
1045 sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1046 is_curve = true;
1047 } else {
1048 sp_curve_lineto(pc->red_curve, p);
1049 is_curve = false;
1050 }
1052 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1054 if (statusbar) {
1055 // status text
1056 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1057 NR::Point rel = p - pc->p[0];
1058 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1059 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1060 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1061 angle = angle_to_compass (angle);
1062 pc->_message_context->setF(Inkscape::NORMAL_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);
1063 g_string_free(dist, FALSE);
1064 }
1065 }
1067 static void
1068 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1069 {
1070 sp_canvas_item_show(pc->c1);
1071 sp_canvas_item_show(pc->cl1);
1073 if ( pc->npoints == 2 ) {
1074 pc->p[1] = p;
1075 sp_canvas_item_hide(pc->c0);
1076 sp_canvas_item_hide(pc->cl0);
1077 SP_CTRL(pc->c1)->moveto(pc->p[1]);
1078 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1080 // status text
1081 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1082 NR::Point rel = p - pc->p[0];
1083 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1084 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1085 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1086 angle = angle_to_compass (angle);
1087 pc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle"), angle, dist->str);
1088 g_string_free(dist, FALSE);
1090 } else if ( pc->npoints == 5 ) {
1091 pc->p[4] = p;
1092 sp_canvas_item_show(pc->c0);
1093 sp_canvas_item_show(pc->cl0);
1094 bool is_symm = false;
1095 if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1096 ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) && !( state & GDK_SHIFT_MASK ) ) ) {
1097 NR::Point delta = p - pc->p[3];
1098 pc->p[2] = pc->p[3] - delta;
1099 is_symm = true;
1100 sp_curve_reset(pc->red_curve);
1101 sp_curve_moveto(pc->red_curve, pc->p[0]);
1102 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1103 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1104 }
1105 SP_CTRL(pc->c0)->moveto(pc->p[2]);
1106 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1107 SP_CTRL(pc->c1)->moveto(pc->p[4]);
1108 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1110 // status text
1111 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1112 NR::Point rel = p - pc->p[3];
1113 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1114 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1115 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1116 angle = angle_to_compass (angle);
1117 pc->_message_context->setF(Inkscape::NORMAL_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);
1118 g_string_free(dist, FALSE);
1120 } else {
1121 g_warning("Something bad happened - npoints is %d", pc->npoints);
1122 }
1123 }
1125 static void
1126 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1127 {
1128 if (!sp_curve_empty(pc->red_curve)) {
1129 sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1130 SPCurve *curve = sp_curve_copy(pc->red_curve);
1131 /// \todo fixme:
1132 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1133 sp_curve_unref(curve);
1134 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1136 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1138 pc->p[0] = pc->p[3];
1139 pc->p[1] = pc->p[4];
1140 pc->npoints = 2;
1142 sp_curve_reset(pc->red_curve);
1143 }
1144 }
1146 static void
1147 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1148 {
1149 pen_disable_events(pc);
1151 SPDesktop *const desktop = pc->desktop;
1152 pc->_message_context->clear();
1153 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1155 sp_curve_reset(pc->red_curve);
1156 spdc_concat_colors_and_flush(pc, closed);
1157 pc->sa = NULL;
1158 pc->ea = NULL;
1160 pc->npoints = 0;
1161 pc->state = SP_PEN_CONTEXT_POINT;
1163 sp_canvas_item_hide(pc->c0);
1164 sp_canvas_item_hide(pc->c1);
1165 sp_canvas_item_hide(pc->cl0);
1166 sp_canvas_item_hide(pc->cl1);
1168 if (pc->green_anchor) {
1169 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1170 }
1173 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1175 pen_enable_events(pc);
1176 }
1178 static void
1179 pen_disable_events(SPPenContext *const pc) {
1180 pc->events_disabled++;
1181 }
1183 static void
1184 pen_enable_events(SPPenContext *const pc) {
1185 g_return_if_fail(pc->events_disabled != 0);
1187 pc->events_disabled--;
1188 }
1190 /*
1191 Local Variables:
1192 mode:c++
1193 c-file-style:"stroustrup"
1194 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1195 indent-tabs-mode:nil
1196 fill-column:99
1197 End:
1198 */
1199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :