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