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