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);
67 static NR::Point pen_drag_origin_w(0, 0);
68 static bool pen_within_tolerance = false;
70 static SPDrawContextClass *pen_parent_class;
72 /**
73 * Register SPPenContext with Gdk and return its type.
74 */
75 GType
76 sp_pen_context_get_type(void)
77 {
78 static GType type = 0;
79 if (!type) {
80 GTypeInfo info = {
81 sizeof(SPPenContextClass),
82 NULL, NULL,
83 (GClassInitFunc) sp_pen_context_class_init,
84 NULL, NULL,
85 sizeof(SPPenContext),
86 4,
87 (GInstanceInitFunc) sp_pen_context_init,
88 NULL, /* value_table */
89 };
90 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
91 }
92 return type;
93 }
95 /**
96 * Initialize the SPPenContext vtable.
97 */
98 static void
99 sp_pen_context_class_init(SPPenContextClass *klass)
100 {
101 GObjectClass *object_class;
102 SPEventContextClass *event_context_class;
104 object_class = (GObjectClass *) klass;
105 event_context_class = (SPEventContextClass *) klass;
107 pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
109 object_class->dispose = sp_pen_context_dispose;
111 event_context_class->setup = sp_pen_context_setup;
112 event_context_class->finish = sp_pen_context_finish;
113 event_context_class->set = sp_pen_context_set;
114 event_context_class->root_handler = sp_pen_context_root_handler;
115 }
117 /**
118 * Callback to initialize SPPenContext object.
119 */
120 static void
121 sp_pen_context_init(SPPenContext *pc)
122 {
124 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
126 event_context->cursor_shape = cursor_pen_xpm;
127 event_context->hot_x = 4;
128 event_context->hot_y = 4;
130 pc->npoints = 0;
131 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
132 pc->state = SP_PEN_CONTEXT_POINT;
134 pc->c0 = NULL;
135 pc->c1 = NULL;
136 pc->cl0 = NULL;
137 pc->cl1 = NULL;
138 }
140 /**
141 * Callback to destroy the SPPenContext object's members and itself.
142 */
143 static void
144 sp_pen_context_dispose(GObject *object)
145 {
146 SPPenContext *pc;
148 pc = SP_PEN_CONTEXT(object);
150 if (pc->c0) {
151 gtk_object_destroy(GTK_OBJECT(pc->c0));
152 pc->c0 = NULL;
153 }
154 if (pc->c1) {
155 gtk_object_destroy(GTK_OBJECT(pc->c1));
156 pc->c1 = NULL;
157 }
158 if (pc->cl0) {
159 gtk_object_destroy(GTK_OBJECT(pc->cl0));
160 pc->cl0 = NULL;
161 }
162 if (pc->cl1) {
163 gtk_object_destroy(GTK_OBJECT(pc->cl1));
164 pc->cl1 = NULL;
165 }
167 G_OBJECT_CLASS(pen_parent_class)->dispose(object);
168 }
170 /**
171 * Callback to initialize SPPenContext object.
172 */
173 static void
174 sp_pen_context_setup(SPEventContext *ec)
175 {
176 SPPenContext *pc;
178 pc = SP_PEN_CONTEXT(ec);
180 if (((SPEventContextClass *) pen_parent_class)->setup) {
181 ((SPEventContextClass *) pen_parent_class)->setup(ec);
182 }
184 /* Pen indicators */
185 pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
186 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
187 pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
188 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
189 pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
190 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
191 pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
192 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
194 sp_canvas_item_hide(pc->c0);
195 sp_canvas_item_hide(pc->c1);
196 sp_canvas_item_hide(pc->cl0);
197 sp_canvas_item_hide(pc->cl1);
199 sp_event_context_read(ec, "mode");
201 pc->anchor_statusbar = false;
203 if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
204 ec->enableSelectionCue();
205 }
206 }
208 /**
209 * Finalization callback.
210 */
211 static void
212 sp_pen_context_finish(SPEventContext *ec)
213 {
214 spdc_pen_finish(SP_PEN_CONTEXT(ec), FALSE);
216 if (((SPEventContextClass *) pen_parent_class)->finish) {
217 ((SPEventContextClass *) pen_parent_class)->finish(ec);
218 }
219 }
221 /**
222 * Callback that sets key to value in pen context.
223 */
224 static void
225 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
226 {
227 SPPenContext *pc = SP_PEN_CONTEXT(ec);
229 if (!strcmp(key, "mode")) {
230 if ( val && !strcmp(val, "drag") ) {
231 pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
232 } else {
233 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
234 }
235 }
236 }
238 /**
239 * Snaps new node relative to the previous node.
240 */
241 static void
242 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
243 {
244 if (pc->npoints > 0) {
245 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
246 }
248 spdc_endpoint_snap_free(pc, p, state);
249 }
251 /**
252 * Snaps new node's handle relative to the new node.
253 */
254 static void
255 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
256 {
257 g_return_if_fail(( pc->npoints == 2 ||
258 pc->npoints == 5 ));
260 spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
261 spdc_endpoint_snap_free(pc, p, state);
262 }
264 /**
265 * Callback to handle all pen events.
266 */
267 static gint
268 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
269 {
270 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
272 gint ret = FALSE;
274 switch (event->type) {
275 case GDK_BUTTON_PRESS:
276 ret = pen_handle_button_press(pc, event->button);
277 break;
279 case GDK_MOTION_NOTIFY:
280 ret = pen_handle_motion_notify(pc, event->motion);
281 break;
283 case GDK_BUTTON_RELEASE:
284 ret = pen_handle_button_release(pc, event->button);
285 break;
287 case GDK_2BUTTON_PRESS:
288 ret = pen_handle_2button_press(pc);
289 break;
291 case GDK_KEY_PRESS:
292 ret = pen_handle_key_press(pc, event);
293 break;
295 default:
296 break;
297 }
299 if (!ret) {
300 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
301 = ((SPEventContextClass *) pen_parent_class)->root_handler;
302 if (parent_root_handler) {
303 ret = parent_root_handler(ec, event);
304 }
305 }
307 return ret;
308 }
310 /**
311 * Handle mouse button press event.
312 */
313 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
314 {
315 gint ret = FALSE;
316 if (bevent.button == 1) {
318 SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
319 SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
321 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
322 return TRUE;
323 }
325 NR::Point const event_w(bevent.x, bevent.y);
326 pen_drag_origin_w = event_w;
327 pen_within_tolerance = true;
329 /* Test whether we hit any anchor. */
330 SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
332 NR::Point const event_dt(desktop->w2d(event_w));
333 switch (pc->mode) {
334 case SP_PEN_CONTEXT_MODE_CLICK:
335 /* In click mode we add point on release */
336 switch (pc->state) {
337 case SP_PEN_CONTEXT_POINT:
338 case SP_PEN_CONTEXT_CONTROL:
339 case SP_PEN_CONTEXT_CLOSE:
340 break;
341 case SP_PEN_CONTEXT_STOP:
342 /* This is allowed, if we just cancelled curve */
343 pc->state = SP_PEN_CONTEXT_POINT;
344 break;
345 default:
346 break;
347 }
348 break;
349 case SP_PEN_CONTEXT_MODE_DRAG:
350 switch (pc->state) {
351 case SP_PEN_CONTEXT_STOP:
352 /* This is allowed, if we just cancelled curve */
353 case SP_PEN_CONTEXT_POINT:
354 if (pc->npoints == 0) {
356 /* Set start anchor */
357 pc->sa = anchor;
358 NR::Point p;
359 if (anchor) {
361 /* Adjust point to anchor if needed */
362 p = anchor->dp;
363 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
365 } else {
367 // This is the first click of a new curve; deselect item so that
368 // this curve is not combined with it (unless it is drawn from its
369 // anchor, which is handled by the sibling branch above)
370 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
371 if (!(bevent.state & GDK_SHIFT_MASK)) {
373 selection->clear();
374 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
376 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
378 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
379 }
381 /* Create green anchor */
382 p = event_dt;
383 spdc_endpoint_snap(pc, p, bevent.state);
384 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
385 }
386 spdc_pen_set_initial_point(pc, p);
387 } else {
389 /* Set end anchor */
390 pc->ea = anchor;
391 NR::Point p;
392 if (anchor) {
394 p = anchor->dp;
395 // we hit an anchor, will finish the curve (either with or without closing)
396 // in release handler
397 pc->state = SP_PEN_CONTEXT_CLOSE;
399 if (pc->green_anchor && pc->green_anchor->active) {
400 // we clicked on the current curve start, so close it even if
401 // we drag a handle away from it
402 dc->green_closed = TRUE;
403 }
404 ret = TRUE;
405 break;
407 } else {
409 p = event_dt;
410 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
411 spdc_pen_set_subsequent_point(pc, p, true);
412 }
414 }
415 pc->state = SP_PEN_CONTEXT_CONTROL;
416 ret = TRUE;
417 break;
418 case SP_PEN_CONTEXT_CONTROL:
419 g_warning("Button down in CONTROL state");
420 break;
421 case SP_PEN_CONTEXT_CLOSE:
422 g_warning("Button down in CLOSE state");
423 break;
424 default:
425 break;
426 }
427 break;
428 default:
429 break;
430 }
431 } else if (bevent.button == 3) {
432 if (pc->npoints != 0) {
433 spdc_pen_finish(pc, FALSE);
434 ret = TRUE;
435 }
436 }
438 return ret;
439 }
441 /**
442 * Handle motion_notify event.
443 */
444 static gint
445 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
446 {
447 gint ret = FALSE;
449 if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
450 // allow middle-button scrolling
451 return FALSE;
452 }
454 NR::Point const event_w(mevent.x,
455 mevent.y);
456 if (pen_within_tolerance) {
457 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
458 "value", 0, 0, 100);
459 if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
460 return FALSE; // Do not drag if we're within tolerance from origin.
461 }
462 }
463 // Once the user has moved farther than tolerance from the original location
464 // (indicating they intend to move the object, not click), then always process the
465 // motion notify coordinates as given (no snapping back to origin)
466 pen_within_tolerance = false;
468 SPDesktop *const dt = pc->desktop;
469 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
470 /* Grab mouse, so release will not pass unnoticed */
471 pc->grab = SP_CANVAS_ITEM(dt->acetate);
472 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
473 GDK_BUTTON_RELEASE_MASK |
474 GDK_POINTER_MOTION_MASK ),
475 NULL, mevent.time);
476 }
478 /* Find desktop coordinates */
479 NR::Point p = dt->w2d(event_w);
481 /* Test, whether we hit any anchor */
482 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
484 switch (pc->mode) {
485 case SP_PEN_CONTEXT_MODE_CLICK:
486 switch (pc->state) {
487 case SP_PEN_CONTEXT_POINT:
488 if ( pc->npoints != 0 ) {
489 /* Only set point, if we are already appending */
490 spdc_endpoint_snap(pc, p, mevent.state);
491 spdc_pen_set_subsequent_point(pc, p, true);
492 ret = TRUE;
493 }
494 break;
495 case SP_PEN_CONTEXT_CONTROL:
496 case SP_PEN_CONTEXT_CLOSE:
497 /* Placing controls is last operation in CLOSE state */
498 spdc_endpoint_snap(pc, p, mevent.state);
499 spdc_pen_set_ctrl(pc, p, mevent.state);
500 ret = TRUE;
501 break;
502 case SP_PEN_CONTEXT_STOP:
503 /* This is perfectly valid */
504 break;
505 default:
506 break;
507 }
508 break;
509 case SP_PEN_CONTEXT_MODE_DRAG:
510 switch (pc->state) {
511 case SP_PEN_CONTEXT_POINT:
512 if ( pc->npoints > 0 ) {
513 /* Only set point, if we are already appending */
515 if (!anchor) { /* Snap node only if not hitting anchor */
516 spdc_endpoint_snap(pc, p, mevent.state);
517 }
519 spdc_pen_set_subsequent_point(pc, p, !anchor);
521 if (anchor && !pc->anchor_statusbar) {
522 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
523 pc->anchor_statusbar = true;
524 } else if (!anchor && pc->anchor_statusbar) {
525 pc->_message_context->clear();
526 pc->anchor_statusbar = false;
527 }
529 ret = TRUE;
530 } else {
531 if (anchor && !pc->anchor_statusbar) {
532 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
533 pc->anchor_statusbar = true;
534 } else if (!anchor && pc->anchor_statusbar) {
535 pc->_message_context->clear();
536 pc->anchor_statusbar = false;
537 }
538 }
539 break;
540 case SP_PEN_CONTEXT_CONTROL:
541 case SP_PEN_CONTEXT_CLOSE:
542 /* Placing controls is last operation in CLOSE state */
544 // snap the handle
545 spdc_endpoint_snap_handle(pc, p, mevent.state);
547 spdc_pen_set_ctrl(pc, p, mevent.state);
548 ret = TRUE;
549 break;
550 case SP_PEN_CONTEXT_STOP:
551 /* This is perfectly valid */
552 break;
553 default:
554 break;
555 }
556 break;
557 default:
558 break;
559 }
560 return ret;
561 }
563 /**
564 * Handle mouse button release event.
565 */
566 static gint
567 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
568 {
569 gint ret = FALSE;
570 if ( revent.button == 1 ) {
572 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
574 NR::Point const event_w(revent.x,
575 revent.y);
576 /* Find desktop coordinates */
577 NR::Point p = pc->desktop->w2d(event_w);
579 /* Test whether we hit any anchor. */
580 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
582 switch (pc->mode) {
583 case SP_PEN_CONTEXT_MODE_CLICK:
584 switch (pc->state) {
585 case SP_PEN_CONTEXT_POINT:
586 if ( pc->npoints == 0 ) {
587 /* Start new thread only with button release */
588 if (anchor) {
589 p = anchor->dp;
590 }
591 pc->sa = anchor;
592 spdc_pen_set_initial_point(pc, p);
593 } else {
594 /* Set end anchor here */
595 pc->ea = anchor;
596 if (anchor) {
597 p = anchor->dp;
598 }
599 }
600 pc->state = SP_PEN_CONTEXT_CONTROL;
601 ret = TRUE;
602 break;
603 case SP_PEN_CONTEXT_CONTROL:
604 /* End current segment */
605 spdc_endpoint_snap(pc, p, revent.state);
606 spdc_pen_finish_segment(pc, p, revent.state);
607 pc->state = SP_PEN_CONTEXT_POINT;
608 ret = TRUE;
609 break;
610 case SP_PEN_CONTEXT_CLOSE:
611 /* End current segment */
612 if (!anchor) { /* Snap node only if not hitting anchor */
613 spdc_endpoint_snap(pc, p, revent.state);
614 }
615 spdc_pen_finish_segment(pc, p, revent.state);
616 spdc_pen_finish(pc, TRUE);
617 pc->state = SP_PEN_CONTEXT_POINT;
618 ret = TRUE;
619 break;
620 case SP_PEN_CONTEXT_STOP:
621 /* This is allowed, if we just cancelled curve */
622 pc->state = SP_PEN_CONTEXT_POINT;
623 ret = TRUE;
624 break;
625 default:
626 break;
627 }
628 break;
629 case SP_PEN_CONTEXT_MODE_DRAG:
630 switch (pc->state) {
631 case SP_PEN_CONTEXT_POINT:
632 case SP_PEN_CONTEXT_CONTROL:
633 spdc_endpoint_snap(pc, p, revent.state);
634 spdc_pen_finish_segment(pc, p, revent.state);
635 break;
636 case SP_PEN_CONTEXT_CLOSE:
637 spdc_endpoint_snap(pc, p, revent.state);
638 spdc_pen_finish_segment(pc, p, revent.state);
639 if (pc->green_closed) {
640 // finishing at the start anchor, close curve
641 spdc_pen_finish(pc, TRUE);
642 } else {
643 // finishing at some other anchor, finish curve but not close
644 spdc_pen_finish(pc, FALSE);
645 }
646 break;
647 case SP_PEN_CONTEXT_STOP:
648 /* This is allowed, if we just cancelled curve */
649 break;
650 default:
651 break;
652 }
653 pc->state = SP_PEN_CONTEXT_POINT;
654 ret = TRUE;
655 break;
656 default:
657 break;
658 }
660 if (pc->grab) {
661 /* Release grab now */
662 sp_canvas_item_ungrab(pc->grab, revent.time);
663 pc->grab = NULL;
664 }
666 ret = TRUE;
668 dc->green_closed = FALSE;
669 }
671 return ret;
672 }
674 static gint
675 pen_handle_2button_press(SPPenContext *const pc)
676 {
677 gint ret = FALSE;
678 if (pc->npoints != 0) {
679 spdc_pen_finish(pc, FALSE);
680 ret = TRUE;
681 }
682 return ret;
683 }
685 void
686 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
687 {
688 if (pc->npoints != 5)
689 return;
691 // green
692 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
693 if (bpath) {
694 if (bpath->code == NR_CURVETO) {
695 bpath->x2 += x;
696 bpath->y2 += y;
697 }
698 bpath->x3 += x;
699 bpath->y3 += y;
700 if (pc->green_bpaths && pc->green_bpaths->data) {
701 // remove old piecewise green canvasitems
702 while (pc->green_bpaths) {
703 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
704 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
705 }
706 // one canvas bpath for all of green_curve
707 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
708 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
709 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
711 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
712 }
713 } else {
714 // start anchor too
715 if (pc->green_anchor) {
716 pc->green_anchor->dp += NR::Point(x, y);
717 SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
718 }
719 }
721 // red
722 pc->p[0] += NR::Point(x, y);
723 pc->p[1] += NR::Point(x, y);
724 sp_curve_reset(pc->red_curve);
725 sp_curve_moveto(pc->red_curve, pc->p[0]);
726 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
727 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
729 // handles
730 if (pc->p[0] != pc->p[1]) {
731 SP_CTRL(pc->c1)->moveto(pc->p[1]);
732 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
733 }
734 if (bpath && bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
735 SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
736 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
737 }
738 }
740 void
741 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
742 {
743 pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
744 }
746 static void
747 pen_cancel (SPPenContext *const pc)
748 {
749 pc->state = SP_PEN_CONTEXT_STOP;
750 spdc_reset_colors(pc);
751 sp_canvas_item_hide(pc->c0);
752 sp_canvas_item_hide(pc->c1);
753 sp_canvas_item_hide(pc->cl0);
754 sp_canvas_item_hide(pc->cl1);
755 pc->_message_context->clear();
756 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
757 }
759 static gint
760 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
761 {
762 gint ret = FALSE;
763 gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
765 switch (get_group0_keyval (&event->key)) {
767 case GDK_Left: // move last point left
768 case GDK_KP_Left:
769 case GDK_KP_4:
770 if (!MOD__CTRL) { // not ctrl
771 if (MOD__ALT) { // alt
772 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
773 else pen_lastpoint_move_screen(pc, -1, 0); // no shift
774 }
775 else { // no alt
776 if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
777 else pen_lastpoint_move(pc, -nudge, 0); // no shift
778 }
779 ret = TRUE;
780 }
781 break;
782 case GDK_Up: // move last point up
783 case GDK_KP_Up:
784 case GDK_KP_8:
785 if (!MOD__CTRL) { // not ctrl
786 if (MOD__ALT) { // alt
787 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
788 else pen_lastpoint_move_screen(pc, 0, 1); // no shift
789 }
790 else { // no alt
791 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
792 else pen_lastpoint_move(pc, 0, nudge); // no shift
793 }
794 ret = TRUE;
795 }
796 break;
797 case GDK_Right: // move last point right
798 case GDK_KP_Right:
799 case GDK_KP_6:
800 if (!MOD__CTRL) { // not ctrl
801 if (MOD__ALT) { // alt
802 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
803 else pen_lastpoint_move_screen(pc, 1, 0); // no shift
804 }
805 else { // no alt
806 if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
807 else pen_lastpoint_move(pc, nudge, 0); // no shift
808 }
809 ret = TRUE;
810 }
811 break;
812 case GDK_Down: // move last point down
813 case GDK_KP_Down:
814 case GDK_KP_2:
815 if (!MOD__CTRL) { // not ctrl
816 if (MOD__ALT) { // alt
817 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
818 else pen_lastpoint_move_screen(pc, 0, -1); // no shift
819 }
820 else { // no alt
821 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
822 else pen_lastpoint_move(pc, 0, -nudge); // no shift
823 }
824 ret = TRUE;
825 }
826 break;
828 case GDK_Return:
829 case GDK_KP_Enter:
830 if (pc->npoints != 0) {
831 spdc_pen_finish(pc, FALSE);
832 ret = TRUE;
833 }
834 break;
835 case GDK_Escape:
836 if (pc->npoints != 0) {
837 // if drawing, cancel, otherwise pass it up for deselecting
838 pen_cancel (pc);
839 ret = TRUE;
840 }
841 break;
842 case GDK_z:
843 case GDK_Z:
844 if (MOD__CTRL_ONLY && pc->npoints != 0) {
845 // if drawing, cancel, otherwise pass it up for undo
846 pen_cancel (pc);
847 ret = TRUE;
848 }
849 break;
850 case GDK_BackSpace:
851 case GDK_Delete:
852 case GDK_KP_Delete:
853 if (sp_curve_is_empty(pc->green_curve)) {
854 /* Same as cancel */
855 pc->state = SP_PEN_CONTEXT_STOP;
856 spdc_reset_colors(pc);
857 sp_canvas_item_hide(pc->c0);
858 sp_canvas_item_hide(pc->c1);
859 sp_canvas_item_hide(pc->cl0);
860 sp_canvas_item_hide(pc->cl1);
861 ret = TRUE;
862 } else {
863 /* Reset red curve */
864 sp_curve_reset(pc->red_curve);
865 /* Destroy topmost green bpath */
866 if (pc->green_bpaths) {
867 if (pc->green_bpaths->data)
868 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
869 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
870 }
871 /* Get last segment */
872 NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
873 gint const e = SP_CURVE_LENGTH(pc->green_curve);
874 if ( e < 2 ) {
875 g_warning("Green curve length is %d", e);
876 break;
877 }
878 pc->p[0] = p[e - 2].c(3);
879 pc->p[1] = p[e - 1].c(1);
880 NR::Point const pt(( pc->npoints < 4
881 ? p[e - 1].c(3)
882 : pc->p[3] ));
883 pc->npoints = 2;
884 sp_curve_backspace(pc->green_curve);
885 sp_canvas_item_hide(pc->c0);
886 sp_canvas_item_hide(pc->c1);
887 sp_canvas_item_hide(pc->cl0);
888 sp_canvas_item_hide(pc->cl1);
889 pc->state = SP_PEN_CONTEXT_POINT;
890 spdc_pen_set_subsequent_point(pc, pt, true);
891 ret = TRUE;
892 }
893 break;
894 default:
895 break;
896 }
897 return ret;
898 }
900 static void
901 spdc_reset_colors(SPPenContext *pc)
902 {
903 /* Red */
904 sp_curve_reset(pc->red_curve);
905 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
906 /* Blue */
907 sp_curve_reset(pc->blue_curve);
908 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
909 /* Green */
910 while (pc->green_bpaths) {
911 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
912 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
913 }
914 sp_curve_reset(pc->green_curve);
915 if (pc->green_anchor) {
916 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
917 }
918 pc->sa = NULL;
919 pc->ea = NULL;
920 pc->npoints = 0;
921 pc->red_curve_is_valid = false;
922 }
925 static void
926 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
927 {
928 g_assert( pc->npoints == 0 );
930 pc->p[0] = p;
931 pc->p[1] = p;
932 pc->npoints = 2;
933 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
934 }
936 static void
937 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
938 {
939 g_assert( pc->npoints != 0 );
940 /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
942 pc->p[2] = p;
943 pc->p[3] = p;
944 pc->p[4] = p;
945 pc->npoints = 5;
946 sp_curve_reset(pc->red_curve);
947 sp_curve_moveto(pc->red_curve, pc->p[0]);
948 bool is_curve;
949 if ( (pc->onlycurves)
950 || ( pc->p[1] != pc->p[0] ) )
951 {
952 sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
953 is_curve = true;
954 } else {
955 sp_curve_lineto(pc->red_curve, p);
956 is_curve = false;
957 }
958 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
960 if (statusbar) {
961 // status text
962 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
963 NR::Point rel = p - pc->p[0];
964 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
965 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
966 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
967 angle = angle_to_compass (angle);
968 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);
969 g_string_free(dist, FALSE);
970 }
971 }
973 static void
974 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
975 {
976 sp_canvas_item_show(pc->c1);
977 sp_canvas_item_show(pc->cl1);
979 if ( pc->npoints == 2 ) {
980 pc->p[1] = p;
981 sp_canvas_item_hide(pc->c0);
982 sp_canvas_item_hide(pc->cl0);
983 SP_CTRL(pc->c1)->moveto(pc->p[1]);
984 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
986 // status text
987 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
988 NR::Point rel = p - pc->p[0];
989 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
990 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
991 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
992 angle = angle_to_compass (angle);
993 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);
994 g_string_free(dist, FALSE);
996 } else if ( pc->npoints == 5 ) {
997 pc->p[4] = p;
998 sp_canvas_item_show(pc->c0);
999 sp_canvas_item_show(pc->cl0);
1000 bool is_symm = false;
1001 if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1002 ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) && !( state & GDK_SHIFT_MASK ) ) ) {
1003 NR::Point delta = p - pc->p[3];
1004 pc->p[2] = pc->p[3] - delta;
1005 is_symm = true;
1006 sp_curve_reset(pc->red_curve);
1007 sp_curve_moveto(pc->red_curve, pc->p[0]);
1008 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1009 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1010 }
1011 SP_CTRL(pc->c0)->moveto(pc->p[2]);
1012 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1013 SP_CTRL(pc->c1)->moveto(pc->p[4]);
1014 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1016 // status text
1017 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1018 NR::Point rel = p - pc->p[3];
1019 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1020 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1021 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1022 angle = angle_to_compass (angle);
1023 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);
1024 g_string_free(dist, FALSE);
1026 } else {
1027 g_warning("Something bad happened - npoints is %d", pc->npoints);
1028 }
1029 }
1031 static void
1032 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1033 {
1034 if (!sp_curve_empty(pc->red_curve)) {
1035 sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1036 SPCurve *curve = sp_curve_copy(pc->red_curve);
1037 /// \todo fixme:
1038 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1039 sp_curve_unref(curve);
1040 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1042 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1044 pc->p[0] = pc->p[3];
1045 pc->p[1] = pc->p[4];
1046 pc->npoints = 2;
1048 sp_curve_reset(pc->red_curve);
1049 }
1050 }
1052 static void
1053 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1054 {
1055 SPDesktop *const desktop = pc->desktop;
1056 pc->_message_context->clear();
1057 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1059 sp_curve_reset(pc->red_curve);
1060 spdc_concat_colors_and_flush(pc, closed);
1061 pc->sa = NULL;
1062 pc->ea = NULL;
1064 pc->npoints = 0;
1065 pc->state = SP_PEN_CONTEXT_POINT;
1067 sp_canvas_item_hide(pc->c0);
1068 sp_canvas_item_hide(pc->c1);
1069 sp_canvas_item_hide(pc->cl0);
1070 sp_canvas_item_hide(pc->cl1);
1072 if (pc->green_anchor) {
1073 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1074 }
1075 }
1078 /*
1079 Local Variables:
1080 mode:c++
1081 c-file-style:"stroustrup"
1082 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1083 indent-tabs-mode:nil
1084 fill-column:99
1085 End:
1086 */
1087 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :