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>
19 #include <cstring>
20 #include <string>
22 #include "pen-context.h"
23 #include "sp-namedview.h"
24 #include "sp-metrics.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "selection.h"
28 #include "selection-chemistry.h"
29 #include "draw-anchor.h"
30 #include "message-stack.h"
31 #include "message-context.h"
32 #include "preferences.h"
33 #include "sp-path.h"
34 #include "display/curve.h"
35 #include "pixmaps/cursor-pen.xpm"
36 #include "display/canvas-bpath.h"
37 #include "display/sp-ctrlline.h"
38 #include "display/sodipodi-ctrl.h"
39 #include <glibmm/i18n.h>
40 #include "libnr/nr-point-ops.h"
41 #include "helper/units.h"
42 #include "macros.h"
43 #include "context-fns.h"
44 #include "tools-switch.h"
46 static void sp_pen_context_class_init(SPPenContextClass *klass);
47 static void sp_pen_context_init(SPPenContext *pc);
48 static void sp_pen_context_dispose(GObject *object);
50 static void sp_pen_context_setup(SPEventContext *ec);
51 static void sp_pen_context_finish(SPEventContext *ec);
52 static void sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
53 static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
54 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static void spdc_pen_set_initial_point(SPPenContext *pc, Geom::Point const p);
57 static void spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status = 0);
58 static void spdc_pen_set_ctrl(SPPenContext *pc, Geom::Point const p, guint state);
59 static void spdc_pen_finish_segment(SPPenContext *pc, Geom::Point p, guint state);
61 static void spdc_pen_finish(SPPenContext *pc, gboolean closed);
63 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent);
64 static gint pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent);
65 static gint pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent);
66 static gint pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent);
67 static gint pen_handle_key_press(SPPenContext *const pc, GdkEvent *event);
68 static void spdc_reset_colors(SPPenContext *pc);
70 static void pen_disable_events(SPPenContext *const pc);
71 static void pen_enable_events(SPPenContext *const pc);
73 static Geom::Point pen_drag_origin_w(0, 0);
74 static bool pen_within_tolerance = false;
76 static SPDrawContextClass *pen_parent_class;
78 static int pen_next_paraxial_direction(const SPPenContext *const pc, Geom::Point const &pt, Geom::Point const &origin, guint state);
79 static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state);
80 static Geom::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, Geom::Point const &pt, guint const state);
82 static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
84 /**
85 * Register SPPenContext with Gdk and return its type.
86 */
87 GType
88 sp_pen_context_get_type(void)
89 {
90 static GType type = 0;
91 if (!type) {
92 GTypeInfo info = {
93 sizeof(SPPenContextClass),
94 NULL, NULL,
95 (GClassInitFunc) sp_pen_context_class_init,
96 NULL, NULL,
97 sizeof(SPPenContext),
98 4,
99 (GInstanceInitFunc) sp_pen_context_init,
100 NULL, /* value_table */
101 };
102 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPenContext", &info, (GTypeFlags)0);
103 }
104 return type;
105 }
107 /**
108 * Initialize the SPPenContext vtable.
109 */
110 static void
111 sp_pen_context_class_init(SPPenContextClass *klass)
112 {
113 GObjectClass *object_class;
114 SPEventContextClass *event_context_class;
116 object_class = (GObjectClass *) klass;
117 event_context_class = (SPEventContextClass *) klass;
119 pen_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
121 object_class->dispose = sp_pen_context_dispose;
123 event_context_class->setup = sp_pen_context_setup;
124 event_context_class->finish = sp_pen_context_finish;
125 event_context_class->set = sp_pen_context_set;
126 event_context_class->root_handler = sp_pen_context_root_handler;
127 event_context_class->item_handler = sp_pen_context_item_handler;
128 }
130 /**
131 * Callback to initialize SPPenContext object.
132 */
133 static void
134 sp_pen_context_init(SPPenContext *pc)
135 {
137 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
139 event_context->cursor_shape = cursor_pen_xpm;
140 event_context->hot_x = 4;
141 event_context->hot_y = 4;
143 pc->npoints = 0;
144 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
145 pc->state = SP_PEN_CONTEXT_POINT;
147 pc->c0 = NULL;
148 pc->c1 = NULL;
149 pc->cl0 = NULL;
150 pc->cl1 = NULL;
152 pc->events_disabled = 0;
154 pc->num_clicks = 0;
155 pc->waiting_LPE = NULL;
156 pc->waiting_item = NULL;
157 }
159 /**
160 * Callback to destroy the SPPenContext object's members and itself.
161 */
162 static void
163 sp_pen_context_dispose(GObject *object)
164 {
165 SPPenContext *pc;
167 pc = SP_PEN_CONTEXT(object);
169 if (pc->c0) {
170 gtk_object_destroy(GTK_OBJECT(pc->c0));
171 pc->c0 = NULL;
172 }
173 if (pc->c1) {
174 gtk_object_destroy(GTK_OBJECT(pc->c1));
175 pc->c1 = NULL;
176 }
177 if (pc->cl0) {
178 gtk_object_destroy(GTK_OBJECT(pc->cl0));
179 pc->cl0 = NULL;
180 }
181 if (pc->cl1) {
182 gtk_object_destroy(GTK_OBJECT(pc->cl1));
183 pc->cl1 = NULL;
184 }
186 G_OBJECT_CLASS(pen_parent_class)->dispose(object);
188 if (pc->expecting_clicks_for_LPE > 0) {
189 // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
190 sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
191 }
192 }
194 void
195 sp_pen_context_set_polyline_mode(SPPenContext *const pc) {
196 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
197 guint mode = prefs->getInt("/tools/freehand/pen/freehand-mode", 0);
198 pc->polylines_only = (mode == 2 || mode == 3);
199 pc->polylines_paraxial = (mode == 3);
200 }
202 /**
203 * Callback to initialize SPPenContext object.
204 */
205 static void
206 sp_pen_context_setup(SPEventContext *ec)
207 {
208 SPPenContext *pc;
210 pc = SP_PEN_CONTEXT(ec);
212 if (((SPEventContextClass *) pen_parent_class)->setup) {
213 ((SPEventContextClass *) pen_parent_class)->setup(ec);
214 }
216 /* Pen indicators */
217 pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
218 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
219 pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
220 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
221 pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
222 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
223 pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
224 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
226 sp_canvas_item_hide(pc->c0);
227 sp_canvas_item_hide(pc->c1);
228 sp_canvas_item_hide(pc->cl0);
229 sp_canvas_item_hide(pc->cl1);
231 sp_event_context_read(ec, "mode");
233 pc->anchor_statusbar = false;
235 sp_pen_context_set_polyline_mode(pc);
237 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
238 if (prefs->getBool("/tools/freehand/pen/selcue")) {
239 ec->enableSelectionCue();
240 }
241 }
243 static void
244 pen_cancel (SPPenContext *const pc)
245 {
246 pc->num_clicks = 0;
247 pc->state = SP_PEN_CONTEXT_STOP;
248 spdc_reset_colors(pc);
249 sp_canvas_item_hide(pc->c0);
250 sp_canvas_item_hide(pc->c1);
251 sp_canvas_item_hide(pc->cl0);
252 sp_canvas_item_hide(pc->cl1);
253 pc->_message_context->clear();
254 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
256 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
257 }
259 /**
260 * Finalization callback.
261 */
262 static void
263 sp_pen_context_finish(SPEventContext *ec)
264 {
265 SPPenContext *pc = SP_PEN_CONTEXT(ec);
267 if (pc->npoints != 0) {
268 pen_cancel (pc);
269 }
271 if (((SPEventContextClass *) pen_parent_class)->finish) {
272 ((SPEventContextClass *) pen_parent_class)->finish(ec);
273 }
274 }
276 /**
277 * Callback that sets key to value in pen context.
278 */
279 static void
280 sp_pen_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
281 {
282 SPPenContext *pc = SP_PEN_CONTEXT(ec);
283 Glib::ustring name = val->getEntryName();
285 if (name == "mode") {
286 if ( val->getString() == "drag" ) {
287 pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
288 } else {
289 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
290 }
291 }
292 }
294 /**
295 * Snaps new node relative to the previous node.
296 */
297 static void
298 spdc_endpoint_snap(SPPenContext const *const pc, Geom::Point &p, guint const state)
299 {
300 if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
301 if (pc->npoints > 0) {
302 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
303 }
304 } else {
305 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
306 //After all, the user explicitely asked for angular snapping by
307 //pressing CTRL
308 spdc_endpoint_snap_free(pc, p, state);
309 }
310 }
311 if (pc->polylines_paraxial) {
312 // TODO: must we avoid one of the snaps in the previous case distinction in some situations?
313 pen_set_to_nearest_horiz_vert(pc, p, state);
314 }
315 }
317 /**
318 * Snaps new node's handle relative to the new node.
319 */
320 static void
321 spdc_endpoint_snap_handle(SPPenContext const *const pc, Geom::Point &p, guint const state)
322 {
323 g_return_if_fail(( pc->npoints == 2 ||
324 pc->npoints == 5 ));
326 if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
327 spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
328 } else {
329 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
330 spdc_endpoint_snap_free(pc, p, state);
331 }
332 }
333 }
335 static gint
336 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
337 {
338 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
340 gint ret = FALSE;
342 switch (event->type) {
343 case GDK_BUTTON_PRESS:
344 ret = pen_handle_button_press(pc, event->button);
345 break;
346 case GDK_BUTTON_RELEASE:
347 ret = pen_handle_button_release(pc, event->button);
348 break;
349 default:
350 break;
351 }
353 if (!ret) {
354 if (((SPEventContextClass *) pen_parent_class)->item_handler)
355 ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
356 }
358 return ret;
359 }
361 /**
362 * Callback to handle all pen events.
363 */
364 static gint
365 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
366 {
367 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
369 gint ret = FALSE;
371 switch (event->type) {
372 case GDK_BUTTON_PRESS:
373 ret = pen_handle_button_press(pc, event->button);
374 break;
376 case GDK_MOTION_NOTIFY:
377 ret = pen_handle_motion_notify(pc, event->motion);
378 break;
380 case GDK_BUTTON_RELEASE:
381 ret = pen_handle_button_release(pc, event->button);
382 break;
384 case GDK_2BUTTON_PRESS:
385 ret = pen_handle_2button_press(pc, event->button);
386 break;
388 case GDK_KEY_PRESS:
389 ret = pen_handle_key_press(pc, event);
390 break;
392 default:
393 break;
394 }
396 if (!ret) {
397 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
398 = ((SPEventContextClass *) pen_parent_class)->root_handler;
399 if (parent_root_handler) {
400 ret = parent_root_handler(ec, event);
401 }
402 }
404 return ret;
405 }
407 /**
408 * Handle mouse button press event.
409 */
410 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
411 {
412 if (pc->events_disabled) {
413 // skip event processing if events are disabled
414 return FALSE;
415 }
417 SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
418 SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
419 Geom::Point const event_w(bevent.x, bevent.y);
420 Geom::Point const event_dt(desktop->w2d(event_w));
421 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
423 gint ret = FALSE;
424 if (bevent.button == 1 && !event_context->space_panning
425 // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
426 && pc->expecting_clicks_for_LPE != 1) {
428 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
429 return TRUE;
430 }
432 if (!pc->grab ) {
433 /* Grab mouse, so release will not pass unnoticed */
434 pc->grab = SP_CANVAS_ITEM(desktop->acetate);
435 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
436 GDK_BUTTON_RELEASE_MASK |
437 GDK_POINTER_MOTION_MASK ),
438 NULL, bevent.time);
439 }
441 pen_drag_origin_w = event_w;
442 pen_within_tolerance = true;
444 /* Test whether we hit any anchor. */
445 SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
447 switch (pc->mode) {
448 case SP_PEN_CONTEXT_MODE_CLICK:
449 /* In click mode we add point on release */
450 switch (pc->state) {
451 case SP_PEN_CONTEXT_POINT:
452 case SP_PEN_CONTEXT_CONTROL:
453 case SP_PEN_CONTEXT_CLOSE:
454 break;
455 case SP_PEN_CONTEXT_STOP:
456 /* This is allowed, if we just cancelled curve */
457 pc->state = SP_PEN_CONTEXT_POINT;
458 break;
459 default:
460 break;
461 }
462 break;
463 case SP_PEN_CONTEXT_MODE_DRAG:
464 switch (pc->state) {
465 case SP_PEN_CONTEXT_STOP:
466 /* This is allowed, if we just cancelled curve */
467 case SP_PEN_CONTEXT_POINT:
468 if (pc->npoints == 0) {
470 if (bevent.state & GDK_CONTROL_MASK) {
471 spdc_create_single_dot(event_context, event_dt, "/tools/freehand/pen", bevent.state);
472 ret = TRUE;
473 break;
474 }
476 // TODO: Perhaps it would be nicer to rearrange the following case
477 // distinction so that the case of a waiting LPE is treated separately
479 /* Set start anchor */
480 pc->sa = anchor;
481 Geom::Point p;
482 if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
483 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
484 a fresh path to be created so don't continue an existing one */
485 p = anchor->dp;
486 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
487 } else {
488 // This is the first click of a new curve; deselect item so that
489 // this curve is not combined with it (unless it is drawn from its
490 // anchor, which is handled by the sibling branch above)
491 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
492 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
493 /* if we have a waiting LPE, we need a fresh path to be created
494 so don't append to an existing one */
495 selection->clear();
496 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
497 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
498 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
499 }
501 /* Create green anchor */
502 p = event_dt;
503 if (!pc->polylines_paraxial) {
504 // only snap the starting point if we're not in horizontal/vertical mode
505 // because otherwise it gets shifted; TODO: why do we snap here at all??
506 spdc_endpoint_snap(pc, p, bevent.state);
507 }
508 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
509 }
510 spdc_pen_set_initial_point(pc, p);
511 } else {
513 /* Set end anchor */
514 pc->ea = anchor;
515 Geom::Point p;
516 if (anchor) {
517 p = anchor->dp;
518 // we hit an anchor, will finish the curve (either with or without closing)
519 // in release handler
520 pc->state = SP_PEN_CONTEXT_CLOSE;
522 if (pc->green_anchor && pc->green_anchor->active) {
523 // we clicked on the current curve start, so close it even if
524 // we drag a handle away from it
525 dc->green_closed = TRUE;
526 }
527 ret = TRUE;
528 break;
530 } else {
531 p = event_dt;
532 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
533 spdc_pen_set_subsequent_point(pc, p, true);
534 if (pc->polylines_only) {
535 spdc_pen_finish_segment(pc, p, bevent.state);
536 }
537 }
538 }
540 pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
541 ret = TRUE;
542 break;
543 case SP_PEN_CONTEXT_CONTROL:
544 g_warning("Button down in CONTROL state");
545 break;
546 case SP_PEN_CONTEXT_CLOSE:
547 g_warning("Button down in CLOSE state");
548 break;
549 default:
550 break;
551 }
552 break;
553 default:
554 break;
555 }
556 } else if (bevent.button == 3 || pc->expecting_clicks_for_LPE == 1) { // when the last click for a waiting LPE occurs we want to finish the path
557 if (pc->npoints != 0) {
559 spdc_pen_finish_segment(pc, event_dt, bevent.state);
560 if (pc->green_closed) {
561 // finishing at the start anchor, close curve
562 spdc_pen_finish(pc, TRUE);
563 } else {
564 // finishing at some other anchor, finish curve but not close
565 spdc_pen_finish(pc, FALSE);
566 }
568 ret = TRUE;
569 }
570 }
572 if (pc->expecting_clicks_for_LPE > 0) {
573 --pc->expecting_clicks_for_LPE;
574 }
576 return ret;
577 }
579 /**
580 * Handle motion_notify event.
581 */
582 static gint
583 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
584 {
585 gint ret = FALSE;
587 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
588 SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
590 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
591 // allow scrolling
592 return FALSE;
593 }
595 if (pc->events_disabled) {
596 // skip motion events if pen events are disabled
597 return FALSE;
598 }
600 Geom::Point const event_w(mevent.x,
601 mevent.y);
602 if (pen_within_tolerance) {
603 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
604 gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
605 if ( Geom::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
606 return FALSE; // Do not drag if we're within tolerance from origin.
607 }
608 }
609 // Once the user has moved farther than tolerance from the original location
610 // (indicating they intend to move the object, not click), then always process the
611 // motion notify coordinates as given (no snapping back to origin)
612 pen_within_tolerance = false;
614 /* Find desktop coordinates */
615 Geom::Point p = dt->w2d(event_w);
617 /* Test, whether we hit any anchor */
618 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
620 switch (pc->mode) {
621 case SP_PEN_CONTEXT_MODE_CLICK:
622 switch (pc->state) {
623 case SP_PEN_CONTEXT_POINT:
624 if ( pc->npoints != 0 ) {
625 /* Only set point, if we are already appending */
626 spdc_endpoint_snap(pc, p, mevent.state);
627 spdc_pen_set_subsequent_point(pc, p, true);
628 ret = TRUE;
629 }
630 break;
631 case SP_PEN_CONTEXT_CONTROL:
632 case SP_PEN_CONTEXT_CLOSE:
633 /* Placing controls is last operation in CLOSE state */
634 spdc_endpoint_snap(pc, p, mevent.state);
635 spdc_pen_set_ctrl(pc, p, mevent.state);
636 ret = TRUE;
637 break;
638 case SP_PEN_CONTEXT_STOP:
639 /* This is perfectly valid */
640 break;
641 default:
642 break;
643 }
644 break;
645 case SP_PEN_CONTEXT_MODE_DRAG:
646 switch (pc->state) {
647 case SP_PEN_CONTEXT_POINT:
648 if ( pc->npoints > 0 ) {
649 /* Only set point, if we are already appending */
651 if (!anchor) { /* Snap node only if not hitting anchor */
652 spdc_endpoint_snap(pc, p, mevent.state);
653 }
655 spdc_pen_set_subsequent_point(pc, p, !anchor, mevent.state);
657 if (anchor && !pc->anchor_statusbar) {
658 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
659 pc->anchor_statusbar = true;
660 } else if (!anchor && pc->anchor_statusbar) {
661 pc->_message_context->clear();
662 pc->anchor_statusbar = false;
663 }
665 ret = TRUE;
666 } else {
667 if (anchor && !pc->anchor_statusbar) {
668 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
669 pc->anchor_statusbar = true;
670 } else if (!anchor && pc->anchor_statusbar) {
671 pc->_message_context->clear();
672 pc->anchor_statusbar = false;
673 }
674 }
675 break;
676 case SP_PEN_CONTEXT_CONTROL:
677 case SP_PEN_CONTEXT_CLOSE:
678 /* Placing controls is last operation in CLOSE state */
680 // snap the handle
681 spdc_endpoint_snap_handle(pc, p, mevent.state);
683 if (!pc->polylines_only) {
684 spdc_pen_set_ctrl(pc, p, mevent.state);
685 } else {
686 spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
687 }
688 gobble_motion_events(GDK_BUTTON1_MASK);
689 ret = TRUE;
690 break;
691 case SP_PEN_CONTEXT_STOP:
692 /* This is perfectly valid */
693 break;
694 default:
695 break;
696 }
697 break;
698 default:
699 break;
700 }
701 return ret;
702 }
704 /**
705 * Handle mouse button release event.
706 */
707 static gint
708 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
709 {
710 if (pc->events_disabled) {
711 // skip event processing if events are disabled
712 return FALSE;
713 }
715 gint ret = FALSE;
716 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
717 if ( revent.button == 1 && !event_context->space_panning) {
719 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
721 Geom::Point const event_w(revent.x,
722 revent.y);
723 /* Find desktop coordinates */
724 Geom::Point p = pc->desktop->w2d(event_w);
726 /* Test whether we hit any anchor. */
727 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
729 switch (pc->mode) {
730 case SP_PEN_CONTEXT_MODE_CLICK:
731 switch (pc->state) {
732 case SP_PEN_CONTEXT_POINT:
733 if ( pc->npoints == 0 ) {
734 /* Start new thread only with button release */
735 if (anchor) {
736 p = anchor->dp;
737 }
738 pc->sa = anchor;
739 spdc_pen_set_initial_point(pc, p);
740 } else {
741 /* Set end anchor here */
742 pc->ea = anchor;
743 if (anchor) {
744 p = anchor->dp;
745 }
746 }
747 pc->state = SP_PEN_CONTEXT_CONTROL;
748 ret = TRUE;
749 break;
750 case SP_PEN_CONTEXT_CONTROL:
751 /* End current segment */
752 spdc_endpoint_snap(pc, p, revent.state);
753 spdc_pen_finish_segment(pc, p, revent.state);
754 pc->state = SP_PEN_CONTEXT_POINT;
755 ret = TRUE;
756 break;
757 case SP_PEN_CONTEXT_CLOSE:
758 /* End current segment */
759 if (!anchor) { /* Snap node only if not hitting anchor */
760 spdc_endpoint_snap(pc, p, revent.state);
761 }
762 spdc_pen_finish_segment(pc, p, revent.state);
763 spdc_pen_finish(pc, TRUE);
764 pc->state = SP_PEN_CONTEXT_POINT;
765 ret = TRUE;
766 break;
767 case SP_PEN_CONTEXT_STOP:
768 /* This is allowed, if we just cancelled curve */
769 pc->state = SP_PEN_CONTEXT_POINT;
770 ret = TRUE;
771 break;
772 default:
773 break;
774 }
775 break;
776 case SP_PEN_CONTEXT_MODE_DRAG:
777 switch (pc->state) {
778 case SP_PEN_CONTEXT_POINT:
779 case SP_PEN_CONTEXT_CONTROL:
780 if (!pc->polylines_only) {
781 spdc_endpoint_snap(pc, p, revent.state);
782 spdc_pen_finish_segment(pc, p, revent.state);
783 }
784 break;
785 case SP_PEN_CONTEXT_CLOSE:
786 spdc_endpoint_snap(pc, p, revent.state);
787 spdc_pen_finish_segment(pc, p, revent.state);
788 if (pc->green_closed) {
789 // finishing at the start anchor, close curve
790 spdc_pen_finish(pc, TRUE);
791 } else {
792 // finishing at some other anchor, finish curve but not close
793 spdc_pen_finish(pc, FALSE);
794 }
795 break;
796 case SP_PEN_CONTEXT_STOP:
797 /* This is allowed, if we just cancelled curve */
798 break;
799 default:
800 break;
801 }
802 pc->state = SP_PEN_CONTEXT_POINT;
803 ret = TRUE;
804 break;
805 default:
806 break;
807 }
809 if (pc->grab) {
810 /* Release grab now */
811 sp_canvas_item_ungrab(pc->grab, revent.time);
812 pc->grab = NULL;
813 }
815 ret = TRUE;
817 dc->green_closed = FALSE;
818 }
820 // TODO: can we be sure that the path was created correctly?
821 // TODO: should we offer an option to collect the clicks in a list?
822 if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
823 sp_pen_context_set_polyline_mode(pc);
825 SPEventContext *ec = SP_EVENT_CONTEXT(pc);
826 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
828 if (pc->waiting_LPE) {
829 // we have an already created LPE waiting for a path
830 pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
831 selection->add(SP_OBJECT(pc->waiting_item));
832 pc->waiting_LPE = NULL;
833 } else {
834 // the case that we need to create a new LPE and apply it to the just-drawn path is
835 // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
836 }
837 }
839 return ret;
840 }
842 static gint
843 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
844 {
845 gint ret = FALSE;
846 if (pc->npoints != 0 && bevent.button != 2) {
847 spdc_pen_finish(pc, FALSE);
848 ret = TRUE;
849 }
850 return ret;
851 }
853 void
854 pen_redraw_all (SPPenContext *const pc)
855 {
856 // green
857 if (pc->green_bpaths) {
858 // remove old piecewise green canvasitems
859 while (pc->green_bpaths) {
860 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
861 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
862 }
863 // one canvas bpath for all of green_curve
864 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
865 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
866 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
868 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
869 }
871 if (pc->green_anchor)
872 SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
874 pc->red_curve->reset();
875 pc->red_curve->moveto(pc->p[0]);
876 pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
877 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
879 // handles
880 if (pc->p[0] != pc->p[1]) {
881 SP_CTRL(pc->c1)->moveto(pc->p[1]);
882 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
883 sp_canvas_item_show (pc->c1);
884 sp_canvas_item_show (pc->cl1);
885 } else {
886 sp_canvas_item_hide (pc->c1);
887 sp_canvas_item_hide (pc->cl1);
888 }
890 Geom::Curve const * last_seg = pc->green_curve->last_segment();
891 if (last_seg) {
892 Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
893 if ( cubic &&
894 (*cubic)[2] != to_2geom(pc->p[0]) )
895 {
896 Geom::Point p2 = (*cubic)[2];
897 SP_CTRL(pc->c0)->moveto(p2);
898 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
899 sp_canvas_item_show (pc->c0);
900 sp_canvas_item_show (pc->cl0);
901 } else {
902 sp_canvas_item_hide (pc->c0);
903 sp_canvas_item_hide (pc->cl0);
904 }
905 }
906 }
908 void
909 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
910 {
911 if (pc->npoints != 5)
912 return;
914 // green
915 if (!pc->green_curve->is_empty()) {
916 pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
917 } else {
918 // start anchor too
919 if (pc->green_anchor) {
920 pc->green_anchor->dp += Geom::Point(x, y);
921 }
922 }
924 // red
925 pc->p[0] += Geom::Point(x, y);
926 pc->p[1] += Geom::Point(x, y);
927 pen_redraw_all(pc);
928 }
930 void
931 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
932 {
933 pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
934 }
936 void
937 pen_lastpoint_tocurve (SPPenContext *const pc)
938 {
939 if (pc->npoints != 5)
940 return;
942 Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
943 if ( cubic ) {
944 pc->p[1] = pc->p[0] + (Geom::Point)( (*cubic)[3] - (*cubic)[2] );
945 } else {
946 pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
947 }
949 pen_redraw_all(pc);
950 }
952 void
953 pen_lastpoint_toline (SPPenContext *const pc)
954 {
955 if (pc->npoints != 5)
956 return;
958 pc->p[1] = pc->p[0];
960 pen_redraw_all(pc);
961 }
964 static gint
965 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
966 {
967 gint ret = FALSE;
968 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
969 gdouble const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000); // in px
971 switch (get_group0_keyval (&event->key)) {
973 case GDK_Left: // move last point left
974 case GDK_KP_Left:
975 case GDK_KP_4:
976 if (!MOD__CTRL) { // not ctrl
977 if (MOD__ALT) { // alt
978 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
979 else pen_lastpoint_move_screen(pc, -1, 0); // no shift
980 }
981 else { // no alt
982 if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
983 else pen_lastpoint_move(pc, -nudge, 0); // no shift
984 }
985 ret = TRUE;
986 }
987 break;
988 case GDK_Up: // move last point up
989 case GDK_KP_Up:
990 case GDK_KP_8:
991 if (!MOD__CTRL) { // not ctrl
992 if (MOD__ALT) { // alt
993 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
994 else pen_lastpoint_move_screen(pc, 0, 1); // no shift
995 }
996 else { // no alt
997 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
998 else pen_lastpoint_move(pc, 0, nudge); // no shift
999 }
1000 ret = TRUE;
1001 }
1002 break;
1003 case GDK_Right: // move last point right
1004 case GDK_KP_Right:
1005 case GDK_KP_6:
1006 if (!MOD__CTRL) { // not ctrl
1007 if (MOD__ALT) { // alt
1008 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
1009 else pen_lastpoint_move_screen(pc, 1, 0); // no shift
1010 }
1011 else { // no alt
1012 if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
1013 else pen_lastpoint_move(pc, nudge, 0); // no shift
1014 }
1015 ret = TRUE;
1016 }
1017 break;
1018 case GDK_Down: // move last point down
1019 case GDK_KP_Down:
1020 case GDK_KP_2:
1021 if (!MOD__CTRL) { // not ctrl
1022 if (MOD__ALT) { // alt
1023 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
1024 else pen_lastpoint_move_screen(pc, 0, -1); // no shift
1025 }
1026 else { // no alt
1027 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
1028 else pen_lastpoint_move(pc, 0, -nudge); // no shift
1029 }
1030 ret = TRUE;
1031 }
1032 break;
1034 case GDK_P:
1035 case GDK_p:
1036 if (MOD__SHIFT_ONLY) {
1037 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1038 ret = TRUE;
1039 }
1040 break;
1042 case GDK_C:
1043 case GDK_c:
1044 if (MOD__SHIFT_ONLY) {
1045 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1046 ret = TRUE;
1047 }
1048 break;
1050 case GDK_B:
1051 case GDK_b:
1052 if (MOD__SHIFT_ONLY) {
1053 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1054 ret = TRUE;
1055 }
1056 break;
1058 case GDK_A:
1059 case GDK_a:
1060 if (MOD__SHIFT_ONLY) {
1061 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1062 ret = TRUE;
1063 }
1064 break;
1066 case GDK_U:
1067 case GDK_u:
1068 if (MOD__SHIFT_ONLY) {
1069 pen_lastpoint_tocurve(pc);
1070 ret = TRUE;
1071 }
1072 break;
1073 case GDK_L:
1074 case GDK_l:
1075 if (MOD__SHIFT_ONLY) {
1076 pen_lastpoint_toline(pc);
1077 ret = TRUE;
1078 }
1079 break;
1081 case GDK_Return:
1082 case GDK_KP_Enter:
1083 if (pc->npoints != 0) {
1084 spdc_pen_finish(pc, FALSE);
1085 ret = TRUE;
1086 }
1087 break;
1088 case GDK_Escape:
1089 if (pc->npoints != 0) {
1090 // if drawing, cancel, otherwise pass it up for deselecting
1091 pen_cancel (pc);
1092 ret = TRUE;
1093 }
1094 break;
1095 case GDK_z:
1096 case GDK_Z:
1097 if (MOD__CTRL_ONLY && pc->npoints != 0) {
1098 // if drawing, cancel, otherwise pass it up for undo
1099 pen_cancel (pc);
1100 ret = TRUE;
1101 }
1102 break;
1103 case GDK_g:
1104 case GDK_G:
1105 if (MOD__SHIFT_ONLY) {
1106 sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
1107 ret = true;
1108 }
1109 break;
1110 case GDK_BackSpace:
1111 case GDK_Delete:
1112 case GDK_KP_Delete:
1113 if ( pc->green_curve->is_empty() || (pc->green_curve->last_segment() == NULL) ) {
1114 if (!pc->red_curve->is_empty()) {
1115 pen_cancel (pc);
1116 ret = TRUE;
1117 } else {
1118 // do nothing; this event should be handled upstream
1119 }
1120 } else {
1121 /* Reset red curve */
1122 pc->red_curve->reset();
1123 /* Destroy topmost green bpath */
1124 if (pc->green_bpaths) {
1125 if (pc->green_bpaths->data)
1126 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1127 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1128 }
1129 /* Get last segment */
1130 if ( pc->green_curve->is_empty() ) {
1131 g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1132 break;
1133 }
1134 // The code below assumes that pc->green_curve has only ONE path !
1135 Geom::Curve const * crv = pc->green_curve->last_segment();
1136 pc->p[0] = crv->initialPoint();
1137 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1138 pc->p[1] = (*cubic)[1];
1139 } else {
1140 pc->p[1] = pc->p[0];
1141 }
1142 Geom::Point const pt(( pc->npoints < 4
1143 ? (Geom::Point)(crv->finalPoint())
1144 : pc->p[3] ));
1145 pc->npoints = 2;
1146 pc->green_curve->backspace();
1147 sp_canvas_item_hide(pc->c0);
1148 sp_canvas_item_hide(pc->c1);
1149 sp_canvas_item_hide(pc->cl0);
1150 sp_canvas_item_hide(pc->cl1);
1151 pc->state = SP_PEN_CONTEXT_POINT;
1152 spdc_pen_set_subsequent_point(pc, pt, true);
1153 ret = TRUE;
1154 }
1155 break;
1156 default:
1157 break;
1158 }
1159 return ret;
1160 }
1162 static void
1163 spdc_reset_colors(SPPenContext *pc)
1164 {
1165 /* Red */
1166 pc->red_curve->reset();
1167 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1168 /* Blue */
1169 pc->blue_curve->reset();
1170 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1171 /* Green */
1172 while (pc->green_bpaths) {
1173 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1174 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1175 }
1176 pc->green_curve->reset();
1177 if (pc->green_anchor) {
1178 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1179 }
1180 pc->sa = NULL;
1181 pc->ea = NULL;
1182 pc->npoints = 0;
1183 pc->red_curve_is_valid = false;
1184 }
1187 static void
1188 spdc_pen_set_initial_point(SPPenContext *const pc, Geom::Point const p)
1189 {
1190 g_assert( pc->npoints == 0 );
1192 pc->p[0] = p;
1193 pc->p[1] = p;
1194 pc->npoints = 2;
1195 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1197 sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1198 }
1200 /**
1201 * Show the status message for the current line/curve segment.
1202 * This type of message always shows angle/distance as the last
1203 * two parameters ("angle %3.2f°, distance %s").
1204 */
1205 static void
1206 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, Geom::Point const p, int pc_point_to_compare, gchar const *message)
1207 {
1208 g_assert(pc != NULL);
1209 g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1210 g_assert(message != NULL);
1212 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1213 Geom::Point rel = p - pc->p[pc_point_to_compare];
1214 GString *dist = SP_PX_TO_METRIC_STRING(Geom::L2(rel), desktop->namedview->getDefaultMetric());
1215 double angle = atan2(rel[Geom::Y], rel[Geom::X]) * 180 / M_PI;
1216 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1217 if (prefs->getBool("/options/compassangledisplay/value", 0) != 0)
1218 angle = angle_to_compass (angle);
1220 pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1221 g_string_free(dist, FALSE);
1222 }
1224 static void
1225 spdc_pen_set_subsequent_point(SPPenContext *const pc, Geom::Point const p, bool statusbar, guint status)
1226 {
1227 g_assert( pc->npoints != 0 );
1228 /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1230 pc->p[2] = p;
1231 pc->p[3] = p;
1232 pc->p[4] = p;
1233 pc->npoints = 5;
1234 pc->red_curve->reset();
1235 bool is_curve;
1236 pc->red_curve->moveto(pc->p[0]);
1237 if (pc->polylines_paraxial && !statusbar) {
1238 // we are drawing horizontal/vertical lines and hit an anchor; draw an L-shaped path
1239 Geom::Point intermed = p;
1240 pen_set_to_nearest_horiz_vert(pc, intermed, status);
1241 pc->red_curve->lineto(intermed);
1242 pc->red_curve->lineto(p);
1243 is_curve = false;
1244 } else {
1245 // one of the 'regular' modes
1246 if (pc->p[1] != pc->p[0])
1247 {
1248 pc->red_curve->curveto(pc->p[1], p, p);
1249 is_curve = true;
1250 } else {
1251 pc->red_curve->lineto(p);
1252 is_curve = false;
1253 }
1254 }
1256 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1258 if (statusbar) {
1259 gchar *message = is_curve ?
1260 _("<b>Curve segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1261 _("<b>Line segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1262 spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1263 }
1264 }
1266 static void
1267 spdc_pen_set_ctrl(SPPenContext *const pc, Geom::Point const p, guint const state)
1268 {
1269 sp_canvas_item_show(pc->c1);
1270 sp_canvas_item_show(pc->cl1);
1272 if ( pc->npoints == 2 ) {
1273 pc->p[1] = p;
1274 sp_canvas_item_hide(pc->c0);
1275 sp_canvas_item_hide(pc->cl0);
1276 SP_CTRL(pc->c1)->moveto(pc->p[1]);
1277 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1279 spdc_pen_set_angle_distance_status_message(pc, p, 0, _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle"));
1280 } else if ( pc->npoints == 5 ) {
1281 pc->p[4] = p;
1282 sp_canvas_item_show(pc->c0);
1283 sp_canvas_item_show(pc->cl0);
1284 bool is_symm = false;
1285 if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1286 ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) && !( state & GDK_SHIFT_MASK ) ) ) {
1287 Geom::Point delta = p - pc->p[3];
1288 pc->p[2] = pc->p[3] - delta;
1289 is_symm = true;
1290 pc->red_curve->reset();
1291 pc->red_curve->moveto(pc->p[0]);
1292 pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1293 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1294 }
1295 SP_CTRL(pc->c0)->moveto(pc->p[2]);
1296 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1297 SP_CTRL(pc->c1)->moveto(pc->p[4]);
1298 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1300 gchar *message = is_symm ?
1301 _("<b>Curve handle, symmetric</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only") :
1302 _("<b>Curve handle</b>: angle %3.2f°, length %s; with <b>Ctrl</b> to snap angle, with <b>Shift</b> to move this handle only");
1303 spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1304 } else {
1305 g_warning("Something bad happened - npoints is %d", pc->npoints);
1306 }
1307 }
1309 static void
1310 spdc_pen_finish_segment(SPPenContext *const pc, Geom::Point const p, guint const state)
1311 {
1312 if (pc->polylines_paraxial) {
1313 pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
1314 }
1315 ++pc->num_clicks;
1317 if (!pc->red_curve->is_empty()) {
1318 pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1319 SPCurve *curve = pc->red_curve->copy();
1320 /// \todo fixme:
1321 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1322 curve->unref();
1323 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1325 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1327 pc->p[0] = pc->p[3];
1328 pc->p[1] = pc->p[4];
1329 pc->npoints = 2;
1331 pc->red_curve->reset();
1332 }
1333 }
1335 static void
1336 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1337 {
1338 if (pc->expecting_clicks_for_LPE > 1) {
1339 // don't let the path be finished before we have collected the required number of mouse clicks
1340 return;
1341 }
1343 pc->num_clicks = 0;
1345 pen_disable_events(pc);
1347 SPDesktop *const desktop = pc->desktop;
1348 pc->_message_context->clear();
1349 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1351 pc->red_curve->reset();
1352 spdc_concat_colors_and_flush(pc, closed);
1353 pc->sa = NULL;
1354 pc->ea = NULL;
1356 pc->npoints = 0;
1357 pc->state = SP_PEN_CONTEXT_POINT;
1359 sp_canvas_item_hide(pc->c0);
1360 sp_canvas_item_hide(pc->c1);
1361 sp_canvas_item_hide(pc->cl0);
1362 sp_canvas_item_hide(pc->cl1);
1364 if (pc->green_anchor) {
1365 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1366 }
1369 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1371 pen_enable_events(pc);
1372 }
1374 static void
1375 pen_disable_events(SPPenContext *const pc) {
1376 pc->events_disabled++;
1377 }
1379 static void
1380 pen_enable_events(SPPenContext *const pc) {
1381 g_return_if_fail(pc->events_disabled != 0);
1383 pc->events_disabled--;
1384 }
1386 void
1387 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1388 unsigned int num_clicks, bool use_polylines)
1389 {
1390 if (effect_type == Inkscape::LivePathEffect::INVALID_LPE)
1391 return;
1393 pc->waiting_LPE_type = effect_type;
1394 pc->expecting_clicks_for_LPE = num_clicks;
1395 pc->polylines_only = use_polylines;
1396 pc->polylines_paraxial = false; // TODO: think if this is correct for all cases
1397 }
1399 void
1400 sp_pen_context_cancel_waiting_for_LPE(SPPenContext *pc)
1401 {
1402 pc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
1403 pc->expecting_clicks_for_LPE = 0;
1404 sp_pen_context_set_polyline_mode(pc);
1405 }
1407 static int pen_next_paraxial_direction(const SPPenContext *const pc,
1408 Geom::Point const &pt, Geom::Point const &origin, guint state) {
1409 /*
1410 * after the first mouse click we determine whether the mouse pointer is closest to a
1411 * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
1412 * orthogonal to the last one; pressing Shift toggles the direction
1413 */
1414 if (pc->num_clicks == 0) {
1415 // first mouse click
1416 double dist_h = fabs(pt[Geom::X] - origin[Geom::X]);
1417 double dist_v = fabs(pt[Geom::Y] - origin[Geom::Y]);
1418 int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
1419 pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
1420 return pen_last_paraxial_dir;
1421 } else {
1422 // subsequent mouse click
1423 return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
1424 }
1425 }
1427 void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, Geom::Point &pt, guint const state)
1428 {
1429 Geom::Point const &origin = pc->p[0];
1431 int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
1433 if (next_dir == 0) {
1434 // line is forced to be horizontal
1435 pt[Geom::Y] = origin[Geom::Y];
1436 } else {
1437 // line is forced to be vertical
1438 pt[Geom::X] = origin[Geom::X];
1439 }
1440 }
1442 Geom::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, Geom::Point const &pt)
1443 {
1444 Geom::Point const &origin = pc->p[0];
1446 if (pen_last_paraxial_dir == 0) {
1447 return Geom::Point (origin[Geom::X], pt[Geom::Y]);
1448 } else {
1449 return Geom::Point (pt[Geom::X], origin[Geom::Y]);
1450 }
1451 }
1453 /*
1454 Local Variables:
1455 mode:c++
1456 c-file-style:"stroustrup"
1457 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1458 indent-tabs-mode:nil
1459 fill-column:99
1460 End:
1461 */
1462 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :