96140a06cd2b7c2cd0f3d4ed181d742c826fcafb
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"));
230 //sp_canvas_clear_forced_full_redraws(pc->desktop->canvas);
231 }
233 /**
234 * Finalization callback.
235 */
236 static void
237 sp_pen_context_finish(SPEventContext *ec)
238 {
239 SPPenContext *pc = SP_PEN_CONTEXT(ec);
241 if (pc->npoints != 0) {
242 pen_cancel (pc);
243 }
245 if (((SPEventContextClass *) pen_parent_class)->finish) {
246 ((SPEventContextClass *) pen_parent_class)->finish(ec);
247 }
248 }
250 /**
251 * Callback that sets key to value in pen context.
252 */
253 static void
254 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
255 {
256 SPPenContext *pc = SP_PEN_CONTEXT(ec);
258 if (!strcmp(key, "mode")) {
259 if ( val && !strcmp(val, "drag") ) {
260 pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
261 } else {
262 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
263 }
264 }
265 }
267 /**
268 * Snaps new node relative to the previous node.
269 */
270 static void
271 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
272 {
273 if (pc->npoints > 0) {
274 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
275 }
277 spdc_endpoint_snap_free(pc, p, state);
278 }
280 /**
281 * Snaps new node's handle relative to the new node.
282 */
283 static void
284 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
285 {
286 g_return_if_fail(( pc->npoints == 2 ||
287 pc->npoints == 5 ));
289 spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
290 spdc_endpoint_snap_free(pc, p, state);
291 }
293 /**
294 * Callback to handle all pen events.
295 */
296 static gint
297 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
298 {
299 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
301 gint ret = FALSE;
303 switch (event->type) {
304 case GDK_BUTTON_PRESS:
305 ret = pen_handle_button_press(pc, event->button);
306 break;
308 case GDK_MOTION_NOTIFY:
309 ret = pen_handle_motion_notify(pc, event->motion);
310 break;
312 case GDK_BUTTON_RELEASE:
313 ret = pen_handle_button_release(pc, event->button);
314 break;
316 case GDK_2BUTTON_PRESS:
317 ret = pen_handle_2button_press(pc);
318 break;
320 case GDK_KEY_PRESS:
321 ret = pen_handle_key_press(pc, event);
322 break;
324 default:
325 break;
326 }
328 if (!ret) {
329 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
330 = ((SPEventContextClass *) pen_parent_class)->root_handler;
331 if (parent_root_handler) {
332 ret = parent_root_handler(ec, event);
333 }
334 }
336 return ret;
337 }
339 /**
340 * Handle mouse button press event.
341 */
342 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
343 {
344 if (pc->events_disabled) {
345 // skip event processing if events are disabled
346 return FALSE;
347 }
349 gint ret = FALSE;
350 if (bevent.button == 1) {
352 SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
353 SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
355 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
356 return TRUE;
357 }
359 NR::Point const event_w(bevent.x, bevent.y);
360 pen_drag_origin_w = event_w;
361 pen_within_tolerance = true;
363 /* Test whether we hit any anchor. */
364 SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
366 NR::Point const event_dt(desktop->w2d(event_w));
367 switch (pc->mode) {
368 case SP_PEN_CONTEXT_MODE_CLICK:
369 /* In click mode we add point on release */
370 switch (pc->state) {
371 case SP_PEN_CONTEXT_POINT:
372 case SP_PEN_CONTEXT_CONTROL:
373 case SP_PEN_CONTEXT_CLOSE:
374 break;
375 case SP_PEN_CONTEXT_STOP:
376 /* This is allowed, if we just cancelled curve */
377 pc->state = SP_PEN_CONTEXT_POINT;
378 break;
379 default:
380 break;
381 }
382 break;
383 case SP_PEN_CONTEXT_MODE_DRAG:
384 switch (pc->state) {
385 case SP_PEN_CONTEXT_STOP:
386 /* This is allowed, if we just cancelled curve */
387 case SP_PEN_CONTEXT_POINT:
388 if (pc->npoints == 0) {
390 /* Set start anchor */
391 pc->sa = anchor;
392 NR::Point p;
393 if (anchor) {
395 /* Adjust point to anchor if needed */
396 p = anchor->dp;
397 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
399 } else {
401 // This is the first click of a new curve; deselect item so that
402 // this curve is not combined with it (unless it is drawn from its
403 // anchor, which is handled by the sibling branch above)
404 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
405 if (!(bevent.state & GDK_SHIFT_MASK)) {
407 selection->clear();
408 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
410 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
412 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
413 }
415 /* Create green anchor */
416 p = event_dt;
417 spdc_endpoint_snap(pc, p, bevent.state);
418 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
419 }
420 spdc_pen_set_initial_point(pc, p);
421 } else {
423 /* Set end anchor */
424 pc->ea = anchor;
425 NR::Point p;
426 if (anchor) {
428 p = anchor->dp;
429 // we hit an anchor, will finish the curve (either with or without closing)
430 // in release handler
431 pc->state = SP_PEN_CONTEXT_CLOSE;
433 if (pc->green_anchor && pc->green_anchor->active) {
434 // we clicked on the current curve start, so close it even if
435 // we drag a handle away from it
436 dc->green_closed = TRUE;
437 }
438 ret = TRUE;
439 break;
441 } else {
443 p = event_dt;
444 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
445 spdc_pen_set_subsequent_point(pc, p, true);
446 }
448 }
449 pc->state = SP_PEN_CONTEXT_CONTROL;
450 ret = TRUE;
451 break;
452 case SP_PEN_CONTEXT_CONTROL:
453 g_warning("Button down in CONTROL state");
454 break;
455 case SP_PEN_CONTEXT_CLOSE:
456 g_warning("Button down in CLOSE state");
457 break;
458 default:
459 break;
460 }
461 break;
462 default:
463 break;
464 }
465 } else if (bevent.button == 3) {
466 if (pc->npoints != 0) {
467 spdc_pen_finish(pc, FALSE);
468 ret = TRUE;
469 }
470 }
472 return ret;
473 }
475 /**
476 * Handle motion_notify event.
477 */
478 static gint
479 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
480 {
481 gint ret = FALSE;
483 if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
484 // allow middle-button scrolling
485 return FALSE;
486 }
488 if (pc->events_disabled) {
489 // skip motion events if pen events are disabled
490 return FALSE;
491 }
493 NR::Point const event_w(mevent.x,
494 mevent.y);
495 if (pen_within_tolerance) {
496 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
497 "value", 0, 0, 100);
498 if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
499 return FALSE; // Do not drag if we're within tolerance from origin.
500 }
501 }
502 // Once the user has moved farther than tolerance from the original location
503 // (indicating they intend to move the object, not click), then always process the
504 // motion notify coordinates as given (no snapping back to origin)
505 pen_within_tolerance = false;
507 SPDesktop *const dt = pc->desktop;
508 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
509 /* Grab mouse, so release will not pass unnoticed */
510 pc->grab = SP_CANVAS_ITEM(dt->acetate);
511 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
512 GDK_BUTTON_RELEASE_MASK |
513 GDK_POINTER_MOTION_MASK ),
514 NULL, mevent.time);
515 }
517 /* Find desktop coordinates */
518 NR::Point p = dt->w2d(event_w);
520 /* Test, whether we hit any anchor */
521 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
523 switch (pc->mode) {
524 case SP_PEN_CONTEXT_MODE_CLICK:
525 switch (pc->state) {
526 case SP_PEN_CONTEXT_POINT:
527 if ( pc->npoints != 0 ) {
528 /* Only set point, if we are already appending */
529 spdc_endpoint_snap(pc, p, mevent.state);
530 spdc_pen_set_subsequent_point(pc, p, true);
531 ret = TRUE;
532 }
533 break;
534 case SP_PEN_CONTEXT_CONTROL:
535 case SP_PEN_CONTEXT_CLOSE:
536 /* Placing controls is last operation in CLOSE state */
537 spdc_endpoint_snap(pc, p, mevent.state);
538 spdc_pen_set_ctrl(pc, p, mevent.state);
539 ret = TRUE;
540 break;
541 case SP_PEN_CONTEXT_STOP:
542 /* This is perfectly valid */
543 break;
544 default:
545 break;
546 }
547 break;
548 case SP_PEN_CONTEXT_MODE_DRAG:
549 switch (pc->state) {
550 case SP_PEN_CONTEXT_POINT:
551 if ( pc->npoints > 0 ) {
552 /* Only set point, if we are already appending */
554 if (!anchor) { /* Snap node only if not hitting anchor */
555 spdc_endpoint_snap(pc, p, mevent.state);
556 }
558 spdc_pen_set_subsequent_point(pc, p, !anchor);
560 if (anchor && !pc->anchor_statusbar) {
561 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
562 pc->anchor_statusbar = true;
563 } else if (!anchor && pc->anchor_statusbar) {
564 pc->_message_context->clear();
565 pc->anchor_statusbar = false;
566 }
568 ret = TRUE;
569 } else {
570 if (anchor && !pc->anchor_statusbar) {
571 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
572 pc->anchor_statusbar = true;
573 } else if (!anchor && pc->anchor_statusbar) {
574 pc->_message_context->clear();
575 pc->anchor_statusbar = false;
576 }
577 }
578 break;
579 case SP_PEN_CONTEXT_CONTROL:
580 case SP_PEN_CONTEXT_CLOSE:
581 /* Placing controls is last operation in CLOSE state */
583 // snap the handle
584 spdc_endpoint_snap_handle(pc, p, mevent.state);
586 spdc_pen_set_ctrl(pc, p, mevent.state);
587 ret = TRUE;
588 break;
589 case SP_PEN_CONTEXT_STOP:
590 /* This is perfectly valid */
591 break;
592 default:
593 break;
594 }
595 break;
596 default:
597 break;
598 }
599 return ret;
600 }
602 /**
603 * Handle mouse button release event.
604 */
605 static gint
606 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
607 {
608 if (pc->events_disabled) {
609 // skip event processing if events are disabled
610 return FALSE;
611 }
613 gint ret = FALSE;
614 if ( revent.button == 1 ) {
616 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
618 NR::Point const event_w(revent.x,
619 revent.y);
620 /* Find desktop coordinates */
621 NR::Point p = pc->desktop->w2d(event_w);
623 /* Test whether we hit any anchor. */
624 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
626 switch (pc->mode) {
627 case SP_PEN_CONTEXT_MODE_CLICK:
628 switch (pc->state) {
629 case SP_PEN_CONTEXT_POINT:
630 if ( pc->npoints == 0 ) {
631 /* Start new thread only with button release */
632 if (anchor) {
633 p = anchor->dp;
634 }
635 pc->sa = anchor;
636 spdc_pen_set_initial_point(pc, p);
637 } else {
638 /* Set end anchor here */
639 pc->ea = anchor;
640 if (anchor) {
641 p = anchor->dp;
642 }
643 }
644 pc->state = SP_PEN_CONTEXT_CONTROL;
645 ret = TRUE;
646 break;
647 case SP_PEN_CONTEXT_CONTROL:
648 /* End current segment */
649 spdc_endpoint_snap(pc, p, revent.state);
650 spdc_pen_finish_segment(pc, p, revent.state);
651 pc->state = SP_PEN_CONTEXT_POINT;
652 ret = TRUE;
653 break;
654 case SP_PEN_CONTEXT_CLOSE:
655 /* End current segment */
656 if (!anchor) { /* Snap node only if not hitting anchor */
657 spdc_endpoint_snap(pc, p, revent.state);
658 }
659 spdc_pen_finish_segment(pc, p, revent.state);
660 spdc_pen_finish(pc, TRUE);
661 pc->state = SP_PEN_CONTEXT_POINT;
662 ret = TRUE;
663 break;
664 case SP_PEN_CONTEXT_STOP:
665 /* This is allowed, if we just cancelled curve */
666 pc->state = SP_PEN_CONTEXT_POINT;
667 ret = TRUE;
668 break;
669 default:
670 break;
671 }
672 break;
673 case SP_PEN_CONTEXT_MODE_DRAG:
674 switch (pc->state) {
675 case SP_PEN_CONTEXT_POINT:
676 case SP_PEN_CONTEXT_CONTROL:
677 spdc_endpoint_snap(pc, p, revent.state);
678 spdc_pen_finish_segment(pc, p, revent.state);
679 break;
680 case SP_PEN_CONTEXT_CLOSE:
681 spdc_endpoint_snap(pc, p, revent.state);
682 spdc_pen_finish_segment(pc, p, revent.state);
683 if (pc->green_closed) {
684 // finishing at the start anchor, close curve
685 spdc_pen_finish(pc, TRUE);
686 } else {
687 // finishing at some other anchor, finish curve but not close
688 spdc_pen_finish(pc, FALSE);
689 }
690 break;
691 case SP_PEN_CONTEXT_STOP:
692 /* This is allowed, if we just cancelled curve */
693 break;
694 default:
695 break;
696 }
697 pc->state = SP_PEN_CONTEXT_POINT;
698 ret = TRUE;
699 break;
700 default:
701 break;
702 }
704 if (pc->grab) {
705 /* Release grab now */
706 sp_canvas_item_ungrab(pc->grab, revent.time);
707 pc->grab = NULL;
708 }
710 ret = TRUE;
712 dc->green_closed = FALSE;
713 }
715 return ret;
716 }
718 static gint
719 pen_handle_2button_press(SPPenContext *const pc)
720 {
721 gint ret = FALSE;
722 if (pc->npoints != 0) {
723 spdc_pen_finish(pc, FALSE);
724 ret = TRUE;
725 }
726 return ret;
727 }
729 void
730 pen_redraw_all (SPPenContext *const pc)
731 {
732 // green
733 if (pc->green_bpaths) {
734 // remove old piecewise green canvasitems
735 while (pc->green_bpaths) {
736 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
737 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
738 }
739 // one canvas bpath for all of green_curve
740 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
741 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
742 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
744 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
745 }
747 if (pc->green_anchor)
748 SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
750 sp_curve_reset(pc->red_curve);
751 sp_curve_moveto(pc->red_curve, pc->p[0]);
752 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
753 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
755 // handles
756 if (pc->p[0] != pc->p[1]) {
757 SP_CTRL(pc->c1)->moveto(pc->p[1]);
758 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
759 sp_canvas_item_show (pc->c1);
760 sp_canvas_item_show (pc->cl1);
761 } else {
762 sp_canvas_item_hide (pc->c1);
763 sp_canvas_item_hide (pc->cl1);
764 }
766 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
767 if (bpath) {
768 if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
769 SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
770 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
771 sp_canvas_item_show (pc->c0);
772 sp_canvas_item_show (pc->cl0);
773 } else {
774 sp_canvas_item_hide (pc->c0);
775 sp_canvas_item_hide (pc->cl0);
776 }
777 }
778 }
780 void
781 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
782 {
783 if (pc->npoints != 5)
784 return;
786 // green
787 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
788 if (bpath) {
789 if (bpath->code == NR_CURVETO) {
790 bpath->x2 += x;
791 bpath->y2 += y;
792 }
793 bpath->x3 += x;
794 bpath->y3 += y;
795 } else {
796 // start anchor too
797 if (pc->green_anchor) {
798 pc->green_anchor->dp += NR::Point(x, y);
799 }
800 }
802 // red
803 pc->p[0] += NR::Point(x, y);
804 pc->p[1] += NR::Point(x, y);
805 pen_redraw_all(pc);
806 }
808 void
809 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
810 {
811 pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
812 }
814 void
815 pen_lastpoint_tocurve (SPPenContext *const pc)
816 {
817 if (pc->npoints != 5)
818 return;
820 // red
821 NArtBpath *const bpath = sp_curve_last_bpath(pc->green_curve);
822 if (bpath && bpath->code == NR_CURVETO) {
823 pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
824 } else {
825 pc->p[1] = pc->p[0] + (pc->p[3] - pc->p[0])*(1/3);
826 }
828 pen_redraw_all(pc);
829 }
831 void
832 pen_lastpoint_toline (SPPenContext *const pc)
833 {
834 if (pc->npoints != 5)
835 return;
837 pc->p[1] = pc->p[0];
839 pen_redraw_all(pc);
840 }
843 static gint
844 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
845 {
846 gint ret = FALSE;
847 gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
849 switch (get_group0_keyval (&event->key)) {
851 case GDK_Left: // move last point left
852 case GDK_KP_Left:
853 case GDK_KP_4:
854 if (!MOD__CTRL) { // not ctrl
855 if (MOD__ALT) { // alt
856 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
857 else pen_lastpoint_move_screen(pc, -1, 0); // no shift
858 }
859 else { // no alt
860 if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
861 else pen_lastpoint_move(pc, -nudge, 0); // no shift
862 }
863 ret = TRUE;
864 }
865 break;
866 case GDK_Up: // move last point up
867 case GDK_KP_Up:
868 case GDK_KP_8:
869 if (!MOD__CTRL) { // not ctrl
870 if (MOD__ALT) { // alt
871 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
872 else pen_lastpoint_move_screen(pc, 0, 1); // no shift
873 }
874 else { // no alt
875 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
876 else pen_lastpoint_move(pc, 0, nudge); // no shift
877 }
878 ret = TRUE;
879 }
880 break;
881 case GDK_Right: // move last point right
882 case GDK_KP_Right:
883 case GDK_KP_6:
884 if (!MOD__CTRL) { // not ctrl
885 if (MOD__ALT) { // alt
886 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
887 else pen_lastpoint_move_screen(pc, 1, 0); // no shift
888 }
889 else { // no alt
890 if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
891 else pen_lastpoint_move(pc, nudge, 0); // no shift
892 }
893 ret = TRUE;
894 }
895 break;
896 case GDK_Down: // move last point down
897 case GDK_KP_Down:
898 case GDK_KP_2:
899 if (!MOD__CTRL) { // not ctrl
900 if (MOD__ALT) { // alt
901 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
902 else pen_lastpoint_move_screen(pc, 0, -1); // no shift
903 }
904 else { // no alt
905 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
906 else pen_lastpoint_move(pc, 0, -nudge); // no shift
907 }
908 ret = TRUE;
909 }
910 break;
912 case GDK_U:
913 case GDK_u:
914 if (MOD__SHIFT_ONLY) {
915 pen_lastpoint_tocurve(pc);
916 ret = TRUE;
917 }
918 break;
919 case GDK_L:
920 case GDK_l:
921 if (MOD__SHIFT_ONLY) {
922 pen_lastpoint_toline(pc);
923 ret = TRUE;
924 }
925 break;
927 case GDK_Return:
928 case GDK_KP_Enter:
929 if (pc->npoints != 0) {
930 spdc_pen_finish(pc, FALSE);
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 //sp_canvas_force_full_redraws(pc->desktop->canvas, 4);
1041 pc->p[2] = p;
1042 pc->p[3] = p;
1043 pc->p[4] = p;
1044 pc->npoints = 5;
1045 sp_curve_reset(pc->red_curve);
1046 sp_curve_moveto(pc->red_curve, pc->p[0]);
1047 bool is_curve;
1048 if ( (pc->onlycurves)
1049 || ( pc->p[1] != pc->p[0] ) )
1050 {
1051 sp_curve_curveto(pc->red_curve, pc->p[1], p, p);
1052 is_curve = true;
1053 } else {
1054 sp_curve_lineto(pc->red_curve, p);
1055 is_curve = false;
1056 }
1058 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1060 if (statusbar) {
1061 // status text
1062 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1063 NR::Point rel = p - pc->p[0];
1064 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1065 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1066 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1067 angle = angle_to_compass (angle);
1068 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);
1069 g_string_free(dist, FALSE);
1070 }
1071 }
1073 static void
1074 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1075 {
1076 sp_canvas_item_show(pc->c1);
1077 sp_canvas_item_show(pc->cl1);
1079 if ( pc->npoints == 2 ) {
1080 pc->p[1] = p;
1081 sp_canvas_item_hide(pc->c0);
1082 sp_canvas_item_hide(pc->cl0);
1083 SP_CTRL(pc->c1)->moveto(pc->p[1]);
1084 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1086 // status text
1087 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1088 NR::Point rel = p - pc->p[0];
1089 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1090 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1091 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1092 angle = angle_to_compass (angle);
1093 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);
1094 g_string_free(dist, FALSE);
1096 } else if ( pc->npoints == 5 ) {
1097 pc->p[4] = p;
1098 sp_canvas_item_show(pc->c0);
1099 sp_canvas_item_show(pc->cl0);
1100 bool is_symm = false;
1101 if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1102 ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) && !( state & GDK_SHIFT_MASK ) ) ) {
1103 NR::Point delta = p - pc->p[3];
1104 pc->p[2] = pc->p[3] - delta;
1105 is_symm = true;
1106 sp_curve_reset(pc->red_curve);
1107 sp_curve_moveto(pc->red_curve, pc->p[0]);
1108 sp_curve_curveto(pc->red_curve, pc->p[1], pc->p[2], pc->p[3]);
1109 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1110 }
1111 SP_CTRL(pc->c0)->moveto(pc->p[2]);
1112 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1113 SP_CTRL(pc->c1)->moveto(pc->p[4]);
1114 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1116 // status text
1117 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1118 NR::Point rel = p - pc->p[3];
1119 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1120 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1121 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1122 angle = angle_to_compass (angle);
1123 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);
1124 g_string_free(dist, FALSE);
1126 } else {
1127 g_warning("Something bad happened - npoints is %d", pc->npoints);
1128 }
1129 }
1131 static void
1132 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1133 {
1134 if (!sp_curve_empty(pc->red_curve)) {
1135 sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
1136 SPCurve *curve = sp_curve_copy(pc->red_curve);
1137 /// \todo fixme:
1138 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1139 sp_curve_unref(curve);
1140 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1142 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1144 pc->p[0] = pc->p[3];
1145 pc->p[1] = pc->p[4];
1146 pc->npoints = 2;
1148 sp_curve_reset(pc->red_curve);
1149 }
1150 }
1152 static void
1153 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1154 {
1155 pen_disable_events(pc);
1157 SPDesktop *const desktop = pc->desktop;
1158 pc->_message_context->clear();
1159 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1161 sp_curve_reset(pc->red_curve);
1162 spdc_concat_colors_and_flush(pc, closed);
1163 pc->sa = NULL;
1164 pc->ea = NULL;
1166 pc->npoints = 0;
1167 pc->state = SP_PEN_CONTEXT_POINT;
1169 sp_canvas_item_hide(pc->c0);
1170 sp_canvas_item_hide(pc->c1);
1171 sp_canvas_item_hide(pc->cl0);
1172 sp_canvas_item_hide(pc->cl1);
1174 if (pc->green_anchor) {
1175 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1176 }
1178 //sp_canvas_clear_forced_full_redraws(desktop->canvas);
1180 pen_enable_events(pc);
1181 }
1183 static void
1184 pen_disable_events(SPPenContext *const pc) {
1185 pc->events_disabled++;
1186 }
1188 static void
1189 pen_enable_events(SPPenContext *const pc) {
1190 g_return_if_fail(pc->events_disabled != 0);
1192 pc->events_disabled--;
1193 }
1195 /*
1196 Local Variables:
1197 mode:c++
1198 c-file-style:"stroustrup"
1199 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1200 indent-tabs-mode:nil
1201 fill-column:99
1202 End:
1203 */
1204 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :