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"
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, gchar const *key, gchar const *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, NR::Point const p);
57 static void spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status = 0);
58 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
59 static void spdc_pen_finish_segment(SPPenContext *pc, NR::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 NR::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, NR::Point const &pt, NR::Point const &origin, guint state);
79 static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state);
80 static NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::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 guint mode = prefs_get_int_attribute("tools.freehand.pen", "freehand-mode", 0);
197 pc->polylines_only = (mode == 2 || mode == 3);
198 pc->polylines_paraxial = (mode == 3);
199 }
201 /**
202 * Callback to initialize SPPenContext object.
203 */
204 static void
205 sp_pen_context_setup(SPEventContext *ec)
206 {
207 SPPenContext *pc;
209 pc = SP_PEN_CONTEXT(ec);
211 if (((SPEventContextClass *) pen_parent_class)->setup) {
212 ((SPEventContextClass *) pen_parent_class)->setup(ec);
213 }
215 /* Pen indicators */
216 pc->c0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
217 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
218 pc->c1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRL, "shape", SP_CTRL_SHAPE_CIRCLE,
219 "size", 4.0, "filled", 0, "fill_color", 0xff00007f, "stroked", 1, "stroke_color", 0x0000ff7f, NULL);
220 pc->cl0 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
221 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl0), 0x0000007f);
222 pc->cl1 = sp_canvas_item_new(sp_desktop_controls(SP_EVENT_CONTEXT_DESKTOP(ec)), SP_TYPE_CTRLLINE, NULL);
223 sp_ctrlline_set_rgba32(SP_CTRLLINE(pc->cl1), 0x0000007f);
225 sp_canvas_item_hide(pc->c0);
226 sp_canvas_item_hide(pc->c1);
227 sp_canvas_item_hide(pc->cl0);
228 sp_canvas_item_hide(pc->cl1);
230 sp_event_context_read(ec, "mode");
232 pc->anchor_statusbar = false;
234 sp_pen_context_set_polyline_mode(pc);
236 if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
237 ec->enableSelectionCue();
238 }
239 }
241 static void
242 pen_cancel (SPPenContext *const pc)
243 {
244 pc->num_clicks = 0;
245 pc->state = SP_PEN_CONTEXT_STOP;
246 spdc_reset_colors(pc);
247 sp_canvas_item_hide(pc->c0);
248 sp_canvas_item_hide(pc->c1);
249 sp_canvas_item_hide(pc->cl0);
250 sp_canvas_item_hide(pc->cl1);
251 pc->_message_context->clear();
252 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
254 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
255 }
257 /**
258 * Finalization callback.
259 */
260 static void
261 sp_pen_context_finish(SPEventContext *ec)
262 {
263 SPPenContext *pc = SP_PEN_CONTEXT(ec);
265 if (pc->npoints != 0) {
266 pen_cancel (pc);
267 }
269 if (((SPEventContextClass *) pen_parent_class)->finish) {
270 ((SPEventContextClass *) pen_parent_class)->finish(ec);
271 }
272 }
274 /**
275 * Callback that sets key to value in pen context.
276 */
277 static void
278 sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
279 {
280 SPPenContext *pc = SP_PEN_CONTEXT(ec);
282 if (!strcmp(key, "mode")) {
283 if ( val && !strcmp(val, "drag") ) {
284 pc->mode = SP_PEN_CONTEXT_MODE_DRAG;
285 } else {
286 pc->mode = SP_PEN_CONTEXT_MODE_CLICK;
287 }
288 }
289 }
291 /**
292 * Snaps new node relative to the previous node.
293 */
294 static void
295 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
296 {
297 if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
298 if (pc->npoints > 0) {
299 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
300 }
301 } else {
302 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
303 //After all, the user explicitely asked for angular snapping by
304 //pressing CTRL
305 spdc_endpoint_snap_free(pc, p, state);
306 }
307 }
308 if (pc->polylines_paraxial) {
309 // TODO: must we avoid one of the snaps in the previous case distinction in some situations?
310 pen_set_to_nearest_horiz_vert(pc, p, state);
311 }
312 }
314 /**
315 * Snaps new node's handle relative to the new node.
316 */
317 static void
318 spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint const state)
319 {
320 g_return_if_fail(( pc->npoints == 2 ||
321 pc->npoints == 5 ));
323 if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
324 spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
325 } else {
326 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
327 spdc_endpoint_snap_free(pc, p, state);
328 }
329 }
330 }
332 static gint
333 sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
334 {
335 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
337 gint ret = FALSE;
339 switch (event->type) {
340 case GDK_BUTTON_PRESS:
341 ret = pen_handle_button_press(pc, event->button);
342 break;
343 case GDK_BUTTON_RELEASE:
344 ret = pen_handle_button_release(pc, event->button);
345 break;
346 default:
347 break;
348 }
350 if (!ret) {
351 if (((SPEventContextClass *) pen_parent_class)->item_handler)
352 ret = ((SPEventContextClass *) pen_parent_class)->item_handler(ec, item, event);
353 }
355 return ret;
356 }
358 /**
359 * Callback to handle all pen events.
360 */
361 static gint
362 sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event)
363 {
364 g_print ("sp_pen_context_root_handler()\n");
365 SPPenContext *const pc = SP_PEN_CONTEXT(ec);
367 gint ret = FALSE;
369 switch (event->type) {
370 case GDK_BUTTON_PRESS:
371 ret = pen_handle_button_press(pc, event->button);
372 break;
374 case GDK_MOTION_NOTIFY:
375 ret = pen_handle_motion_notify(pc, event->motion);
376 break;
378 case GDK_BUTTON_RELEASE:
379 ret = pen_handle_button_release(pc, event->button);
380 break;
382 case GDK_2BUTTON_PRESS:
383 ret = pen_handle_2button_press(pc, event->button);
384 break;
386 case GDK_KEY_PRESS:
387 ret = pen_handle_key_press(pc, event);
388 break;
390 default:
391 break;
392 }
394 if (!ret) {
395 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
396 = ((SPEventContextClass *) pen_parent_class)->root_handler;
397 if (parent_root_handler) {
398 ret = parent_root_handler(ec, event);
399 }
400 }
402 return ret;
403 }
405 /**
406 * Handle mouse button press event.
407 */
408 static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const &bevent)
409 {
410 if (pc->events_disabled) {
411 // skip event processing if events are disabled
412 return FALSE;
413 }
415 SPDrawContext * const dc = SP_DRAW_CONTEXT(pc);
416 SPDesktop * const desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
417 NR::Point const event_w(bevent.x, bevent.y);
418 NR::Point const event_dt(desktop->w2d(event_w));
419 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
421 gint ret = FALSE;
422 if (bevent.button == 1 && !event_context->space_panning
423 // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
424 && pc->expecting_clicks_for_LPE != 1) {
426 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
427 return TRUE;
428 }
430 if (!pc->grab ) {
431 /* Grab mouse, so release will not pass unnoticed */
432 pc->grab = SP_CANVAS_ITEM(desktop->acetate);
433 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
434 GDK_BUTTON_RELEASE_MASK |
435 GDK_POINTER_MOTION_MASK ),
436 NULL, bevent.time);
437 }
439 pen_drag_origin_w = event_w;
440 pen_within_tolerance = true;
442 /* Test whether we hit any anchor. */
443 SPDrawAnchor * const anchor = spdc_test_inside(pc, event_w);
445 switch (pc->mode) {
446 case SP_PEN_CONTEXT_MODE_CLICK:
447 /* In click mode we add point on release */
448 switch (pc->state) {
449 case SP_PEN_CONTEXT_POINT:
450 case SP_PEN_CONTEXT_CONTROL:
451 case SP_PEN_CONTEXT_CLOSE:
452 break;
453 case SP_PEN_CONTEXT_STOP:
454 /* This is allowed, if we just cancelled curve */
455 pc->state = SP_PEN_CONTEXT_POINT;
456 break;
457 default:
458 break;
459 }
460 break;
461 case SP_PEN_CONTEXT_MODE_DRAG:
462 switch (pc->state) {
463 case SP_PEN_CONTEXT_STOP:
464 /* This is allowed, if we just cancelled curve */
465 case SP_PEN_CONTEXT_POINT:
466 if (pc->npoints == 0) {
468 if (bevent.state & GDK_CONTROL_MASK) {
469 freehand_create_single_dot(event_context, event_dt, "tools.freehand.pen", bevent.state);
470 ret = TRUE;
471 break;
472 }
474 // TODO: Perhaps it would be nicer to rearrange the following case
475 // distinction so that the case of a waiting LPE is treated separately
477 /* Set start anchor */
478 pc->sa = anchor;
479 NR::Point p;
480 if (anchor && !sp_pen_context_has_waiting_LPE(pc)) {
481 /* Adjust point to anchor if needed; if we have a waiting LPE, we need
482 a fresh path to be created so don't continue an existing one */
483 p = anchor->dp;
484 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
485 } else {
486 // This is the first click of a new curve; deselect item so that
487 // this curve is not combined with it (unless it is drawn from its
488 // anchor, which is handled by the sibling branch above)
489 Inkscape::Selection * const selection = sp_desktop_selection(desktop);
490 if (!(bevent.state & GDK_SHIFT_MASK) || sp_pen_context_has_waiting_LPE(pc)) {
491 /* if we have a waiting LPE, we need a fresh path to be created
492 so don't append to an existing one */
493 selection->clear();
494 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
495 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
496 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
497 }
499 /* Create green anchor */
500 p = event_dt;
501 if (!pc->polylines_paraxial) {
502 // only snap the starting point if we're not in horizontal/vertical mode
503 // because otherwise it gets shifted; TODO: why do we snap here at all??
504 spdc_endpoint_snap(pc, p, bevent.state);
505 }
506 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
507 }
508 spdc_pen_set_initial_point(pc, p);
509 } else {
511 /* Set end anchor */
512 pc->ea = anchor;
513 NR::Point p;
514 if (anchor) {
515 p = anchor->dp;
516 // we hit an anchor, will finish the curve (either with or without closing)
517 // in release handler
518 pc->state = SP_PEN_CONTEXT_CLOSE;
520 if (pc->green_anchor && pc->green_anchor->active) {
521 // we clicked on the current curve start, so close it even if
522 // we drag a handle away from it
523 dc->green_closed = TRUE;
524 }
525 ret = TRUE;
526 break;
528 } else {
529 p = event_dt;
530 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
531 spdc_pen_set_subsequent_point(pc, p, true);
532 if (pc->polylines_only) {
533 spdc_pen_finish_segment(pc, p, bevent.state);
534 }
535 }
536 }
538 pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
539 ret = TRUE;
540 break;
541 case SP_PEN_CONTEXT_CONTROL:
542 g_warning("Button down in CONTROL state");
543 break;
544 case SP_PEN_CONTEXT_CLOSE:
545 g_warning("Button down in CLOSE state");
546 break;
547 default:
548 break;
549 }
550 break;
551 default:
552 break;
553 }
554 } 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
555 if (pc->npoints != 0) {
557 spdc_pen_finish_segment(pc, event_dt, bevent.state);
558 if (pc->green_closed) {
559 // finishing at the start anchor, close curve
560 spdc_pen_finish(pc, TRUE);
561 } else {
562 // finishing at some other anchor, finish curve but not close
563 spdc_pen_finish(pc, FALSE);
564 }
566 ret = TRUE;
567 }
568 }
570 if (pc->expecting_clicks_for_LPE > 0) {
571 --pc->expecting_clicks_for_LPE;
572 }
574 return ret;
575 }
577 /**
578 * Handle motion_notify event.
579 */
580 static gint
581 pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
582 {
583 gint ret = FALSE;
585 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
586 SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
588 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
589 // allow scrolling
590 return FALSE;
591 }
593 if (pc->events_disabled) {
594 // skip motion events if pen events are disabled
595 return FALSE;
596 }
598 NR::Point const event_w(mevent.x,
599 mevent.y);
600 if (pen_within_tolerance) {
601 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
602 "value", 0, 0, 100);
603 if ( NR::LInfty( event_w - pen_drag_origin_w ) < tolerance ) {
604 return FALSE; // Do not drag if we're within tolerance from origin.
605 }
606 }
607 // Once the user has moved farther than tolerance from the original location
608 // (indicating they intend to move the object, not click), then always process the
609 // motion notify coordinates as given (no snapping back to origin)
610 pen_within_tolerance = false;
612 /* Find desktop coordinates */
613 NR::Point p = dt->w2d(event_w);
615 /* Test, whether we hit any anchor */
616 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
618 switch (pc->mode) {
619 case SP_PEN_CONTEXT_MODE_CLICK:
620 switch (pc->state) {
621 case SP_PEN_CONTEXT_POINT:
622 if ( pc->npoints != 0 ) {
623 /* Only set point, if we are already appending */
624 spdc_endpoint_snap(pc, p, mevent.state);
625 spdc_pen_set_subsequent_point(pc, p, true);
626 ret = TRUE;
627 }
628 break;
629 case SP_PEN_CONTEXT_CONTROL:
630 case SP_PEN_CONTEXT_CLOSE:
631 /* Placing controls is last operation in CLOSE state */
632 spdc_endpoint_snap(pc, p, mevent.state);
633 spdc_pen_set_ctrl(pc, p, mevent.state);
634 ret = TRUE;
635 break;
636 case SP_PEN_CONTEXT_STOP:
637 /* This is perfectly valid */
638 break;
639 default:
640 break;
641 }
642 break;
643 case SP_PEN_CONTEXT_MODE_DRAG:
644 switch (pc->state) {
645 case SP_PEN_CONTEXT_POINT:
646 if ( pc->npoints > 0 ) {
647 /* Only set point, if we are already appending */
649 if (!anchor) { /* Snap node only if not hitting anchor */
650 spdc_endpoint_snap(pc, p, mevent.state);
651 }
653 spdc_pen_set_subsequent_point(pc, p, !anchor, mevent.state);
655 if (anchor && !pc->anchor_statusbar) {
656 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
657 pc->anchor_statusbar = true;
658 } else if (!anchor && pc->anchor_statusbar) {
659 pc->_message_context->clear();
660 pc->anchor_statusbar = false;
661 }
663 ret = TRUE;
664 } else {
665 if (anchor && !pc->anchor_statusbar) {
666 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to continue the path from this point."));
667 pc->anchor_statusbar = true;
668 } else if (!anchor && pc->anchor_statusbar) {
669 pc->_message_context->clear();
670 pc->anchor_statusbar = false;
671 }
672 }
673 break;
674 case SP_PEN_CONTEXT_CONTROL:
675 case SP_PEN_CONTEXT_CLOSE:
676 /* Placing controls is last operation in CLOSE state */
678 // snap the handle
679 spdc_endpoint_snap_handle(pc, p, mevent.state);
681 if (!pc->polylines_only) {
682 spdc_pen_set_ctrl(pc, p, mevent.state);
683 } else {
684 spdc_pen_set_ctrl(pc, pc->p[1], mevent.state);
685 }
686 gobble_motion_events(GDK_BUTTON1_MASK);
687 ret = TRUE;
688 break;
689 case SP_PEN_CONTEXT_STOP:
690 /* This is perfectly valid */
691 break;
692 default:
693 break;
694 }
695 break;
696 default:
697 break;
698 }
699 return ret;
700 }
702 /**
703 * Handle mouse button release event.
704 */
705 static gint
706 pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
707 {
708 if (pc->events_disabled) {
709 // skip event processing if events are disabled
710 return FALSE;
711 }
713 gint ret = FALSE;
714 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
715 if ( revent.button == 1 && !event_context->space_panning) {
717 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
719 NR::Point const event_w(revent.x,
720 revent.y);
721 /* Find desktop coordinates */
722 NR::Point p = pc->desktop->w2d(event_w);
724 /* Test whether we hit any anchor. */
725 SPDrawAnchor *anchor = spdc_test_inside(pc, event_w);
727 switch (pc->mode) {
728 case SP_PEN_CONTEXT_MODE_CLICK:
729 switch (pc->state) {
730 case SP_PEN_CONTEXT_POINT:
731 if ( pc->npoints == 0 ) {
732 /* Start new thread only with button release */
733 if (anchor) {
734 p = anchor->dp;
735 }
736 pc->sa = anchor;
737 spdc_pen_set_initial_point(pc, p);
738 } else {
739 /* Set end anchor here */
740 pc->ea = anchor;
741 if (anchor) {
742 p = anchor->dp;
743 }
744 }
745 pc->state = SP_PEN_CONTEXT_CONTROL;
746 ret = TRUE;
747 break;
748 case SP_PEN_CONTEXT_CONTROL:
749 /* End current segment */
750 spdc_endpoint_snap(pc, p, revent.state);
751 spdc_pen_finish_segment(pc, p, revent.state);
752 pc->state = SP_PEN_CONTEXT_POINT;
753 ret = TRUE;
754 break;
755 case SP_PEN_CONTEXT_CLOSE:
756 /* End current segment */
757 if (!anchor) { /* Snap node only if not hitting anchor */
758 spdc_endpoint_snap(pc, p, revent.state);
759 }
760 spdc_pen_finish_segment(pc, p, revent.state);
761 spdc_pen_finish(pc, TRUE);
762 pc->state = SP_PEN_CONTEXT_POINT;
763 ret = TRUE;
764 break;
765 case SP_PEN_CONTEXT_STOP:
766 /* This is allowed, if we just cancelled curve */
767 pc->state = SP_PEN_CONTEXT_POINT;
768 ret = TRUE;
769 break;
770 default:
771 break;
772 }
773 break;
774 case SP_PEN_CONTEXT_MODE_DRAG:
775 switch (pc->state) {
776 case SP_PEN_CONTEXT_POINT:
777 case SP_PEN_CONTEXT_CONTROL:
778 if (!pc->polylines_only) {
779 spdc_endpoint_snap(pc, p, revent.state);
780 spdc_pen_finish_segment(pc, p, revent.state);
781 }
782 break;
783 case SP_PEN_CONTEXT_CLOSE:
784 spdc_endpoint_snap(pc, p, revent.state);
785 spdc_pen_finish_segment(pc, p, revent.state);
786 if (pc->green_closed) {
787 // finishing at the start anchor, close curve
788 spdc_pen_finish(pc, TRUE);
789 } else {
790 // finishing at some other anchor, finish curve but not close
791 spdc_pen_finish(pc, FALSE);
792 }
793 break;
794 case SP_PEN_CONTEXT_STOP:
795 /* This is allowed, if we just cancelled curve */
796 break;
797 default:
798 break;
799 }
800 pc->state = SP_PEN_CONTEXT_POINT;
801 ret = TRUE;
802 break;
803 default:
804 break;
805 }
807 if (pc->grab) {
808 /* Release grab now */
809 sp_canvas_item_ungrab(pc->grab, revent.time);
810 pc->grab = NULL;
811 }
813 ret = TRUE;
815 dc->green_closed = FALSE;
816 }
818 // TODO: can we be sure that the path was created correctly?
819 // TODO: should we offer an option to collect the clicks in a list?
820 if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
821 sp_pen_context_set_polyline_mode(pc);
823 SPEventContext *ec = SP_EVENT_CONTEXT(pc);
824 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
826 if (pc->waiting_LPE) {
827 // we have an already created LPE waiting for a path
828 pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
829 selection->add(SP_OBJECT(pc->waiting_item));
830 pc->waiting_LPE = NULL;
831 } else {
832 // the case that we need to create a new LPE and apply it to the just-drawn path is
833 // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
834 }
835 }
837 return ret;
838 }
840 static gint
841 pen_handle_2button_press(SPPenContext *const pc, GdkEventButton const &bevent)
842 {
843 gint ret = FALSE;
844 if (pc->npoints != 0 && bevent.button != 2) {
845 spdc_pen_finish(pc, FALSE);
846 ret = TRUE;
847 }
848 return ret;
849 }
851 void
852 pen_redraw_all (SPPenContext *const pc)
853 {
854 // green
855 if (pc->green_bpaths) {
856 // remove old piecewise green canvasitems
857 while (pc->green_bpaths) {
858 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
859 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
860 }
861 // one canvas bpath for all of green_curve
862 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), pc->green_curve);
863 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
864 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cshape), 0, SP_WIND_RULE_NONZERO);
866 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
867 }
869 if (pc->green_anchor)
870 SP_CTRL(pc->green_anchor->ctrl)->moveto(pc->green_anchor->dp);
872 pc->red_curve->reset();
873 pc->red_curve->moveto(pc->p[0]);
874 pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
875 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
877 // handles
878 if (pc->p[0] != pc->p[1]) {
879 SP_CTRL(pc->c1)->moveto(pc->p[1]);
880 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
881 sp_canvas_item_show (pc->c1);
882 sp_canvas_item_show (pc->cl1);
883 } else {
884 sp_canvas_item_hide (pc->c1);
885 sp_canvas_item_hide (pc->cl1);
886 }
888 Geom::Curve const * last_seg = pc->green_curve->last_segment();
889 if (last_seg) {
890 Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
891 if ( cubic &&
892 (*cubic)[2] != to_2geom(pc->p[0]) )
893 {
894 NR::Point p2 = (*cubic)[2];
895 SP_CTRL(pc->c0)->moveto(p2);
896 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
897 sp_canvas_item_show (pc->c0);
898 sp_canvas_item_show (pc->cl0);
899 } else {
900 sp_canvas_item_hide (pc->c0);
901 sp_canvas_item_hide (pc->cl0);
902 }
903 }
904 }
906 void
907 pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
908 {
909 if (pc->npoints != 5)
910 return;
912 // green
913 if (!pc->green_curve->is_empty()) {
914 pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
915 } else {
916 // start anchor too
917 if (pc->green_anchor) {
918 pc->green_anchor->dp += NR::Point(x, y);
919 }
920 }
922 // red
923 pc->p[0] += NR::Point(x, y);
924 pc->p[1] += NR::Point(x, y);
925 pen_redraw_all(pc);
926 }
928 void
929 pen_lastpoint_move_screen (SPPenContext *const pc, gdouble x, gdouble y)
930 {
931 pen_lastpoint_move (pc, x / pc->desktop->current_zoom(), y / pc->desktop->current_zoom());
932 }
934 void
935 pen_lastpoint_tocurve (SPPenContext *const pc)
936 {
937 if (pc->npoints != 5)
938 return;
940 Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
941 if ( cubic ) {
942 pc->p[1] = pc->p[0] + (NR::Point)( (*cubic)[3] - (*cubic)[2] );
943 } else {
944 pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
945 }
947 pen_redraw_all(pc);
948 }
950 void
951 pen_lastpoint_toline (SPPenContext *const pc)
952 {
953 if (pc->npoints != 5)
954 return;
956 pc->p[1] = pc->p[0];
958 pen_redraw_all(pc);
959 }
962 static gint
963 pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
964 {
965 gint ret = FALSE;
966 gdouble const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
968 switch (get_group0_keyval (&event->key)) {
970 case GDK_Left: // move last point left
971 case GDK_KP_Left:
972 case GDK_KP_4:
973 if (!MOD__CTRL) { // not ctrl
974 if (MOD__ALT) { // alt
975 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, -10, 0); // shift
976 else pen_lastpoint_move_screen(pc, -1, 0); // no shift
977 }
978 else { // no alt
979 if (MOD__SHIFT) pen_lastpoint_move(pc, -10*nudge, 0); // shift
980 else pen_lastpoint_move(pc, -nudge, 0); // no shift
981 }
982 ret = TRUE;
983 }
984 break;
985 case GDK_Up: // move last point up
986 case GDK_KP_Up:
987 case GDK_KP_8:
988 if (!MOD__CTRL) { // not ctrl
989 if (MOD__ALT) { // alt
990 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, 10); // shift
991 else pen_lastpoint_move_screen(pc, 0, 1); // no shift
992 }
993 else { // no alt
994 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, 10*nudge); // shift
995 else pen_lastpoint_move(pc, 0, nudge); // no shift
996 }
997 ret = TRUE;
998 }
999 break;
1000 case GDK_Right: // move last point right
1001 case GDK_KP_Right:
1002 case GDK_KP_6:
1003 if (!MOD__CTRL) { // not ctrl
1004 if (MOD__ALT) { // alt
1005 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 10, 0); // shift
1006 else pen_lastpoint_move_screen(pc, 1, 0); // no shift
1007 }
1008 else { // no alt
1009 if (MOD__SHIFT) pen_lastpoint_move(pc, 10*nudge, 0); // shift
1010 else pen_lastpoint_move(pc, nudge, 0); // no shift
1011 }
1012 ret = TRUE;
1013 }
1014 break;
1015 case GDK_Down: // move last point down
1016 case GDK_KP_Down:
1017 case GDK_KP_2:
1018 if (!MOD__CTRL) { // not ctrl
1019 if (MOD__ALT) { // alt
1020 if (MOD__SHIFT) pen_lastpoint_move_screen(pc, 0, -10); // shift
1021 else pen_lastpoint_move_screen(pc, 0, -1); // no shift
1022 }
1023 else { // no alt
1024 if (MOD__SHIFT) pen_lastpoint_move(pc, 0, -10*nudge); // shift
1025 else pen_lastpoint_move(pc, 0, -nudge); // no shift
1026 }
1027 ret = TRUE;
1028 }
1029 break;
1031 case GDK_P:
1032 case GDK_p:
1033 if (MOD__SHIFT_ONLY) {
1034 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
1035 ret = TRUE;
1036 }
1037 break;
1039 case GDK_C:
1040 case GDK_c:
1041 if (MOD__SHIFT_ONLY) {
1042 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::CIRCLE_3PTS, 3);
1043 ret = TRUE;
1044 }
1045 break;
1047 case GDK_B:
1048 case GDK_b:
1049 if (MOD__SHIFT_ONLY) {
1050 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PERP_BISECTOR, 2);
1051 ret = TRUE;
1052 }
1053 break;
1055 case GDK_A:
1056 case GDK_a:
1057 if (MOD__SHIFT_ONLY) {
1058 sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::ANGLE_BISECTOR, 3);
1059 ret = TRUE;
1060 }
1061 break;
1063 case GDK_U:
1064 case GDK_u:
1065 if (MOD__SHIFT_ONLY) {
1066 pen_lastpoint_tocurve(pc);
1067 ret = TRUE;
1068 }
1069 break;
1070 case GDK_L:
1071 case GDK_l:
1072 if (MOD__SHIFT_ONLY) {
1073 pen_lastpoint_toline(pc);
1074 ret = TRUE;
1075 }
1076 break;
1078 case GDK_Return:
1079 case GDK_KP_Enter:
1080 if (pc->npoints != 0) {
1081 spdc_pen_finish(pc, FALSE);
1082 ret = TRUE;
1083 }
1084 break;
1085 case GDK_Escape:
1086 if (pc->npoints != 0) {
1087 // if drawing, cancel, otherwise pass it up for deselecting
1088 pen_cancel (pc);
1089 ret = TRUE;
1090 }
1091 break;
1092 case GDK_z:
1093 case GDK_Z:
1094 if (MOD__CTRL_ONLY && pc->npoints != 0) {
1095 // if drawing, cancel, otherwise pass it up for undo
1096 pen_cancel (pc);
1097 ret = TRUE;
1098 }
1099 break;
1100 case GDK_g:
1101 case GDK_G:
1102 if (MOD__SHIFT_ONLY) {
1103 sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
1104 ret = true;
1105 }
1106 break;
1107 case GDK_BackSpace:
1108 case GDK_Delete:
1109 case GDK_KP_Delete:
1110 if (pc->green_curve->is_empty()) {
1111 if (!pc->red_curve->is_empty()) {
1112 pen_cancel (pc);
1113 ret = TRUE;
1114 } else {
1115 // do nothing; this event should be handled upstream
1116 }
1117 } else {
1118 /* Reset red curve */
1119 pc->red_curve->reset();
1120 /* Destroy topmost green bpath */
1121 if (pc->green_bpaths) {
1122 if (pc->green_bpaths->data)
1123 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1124 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1125 }
1126 /* Get last segment */
1127 if ( pc->green_curve->is_empty() ) {
1128 g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
1129 break;
1130 }
1131 // The code below assumes that pc->green_curve has only ONE path !
1132 Geom::Path const & path = pc->green_curve->get_pathvector().back();
1133 Geom::Curve const * crv = &path.back_default();
1134 pc->p[0] = crv->initialPoint();
1135 if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
1136 pc->p[1] = (*cubic)[1];
1137 } else {
1138 pc->p[1] = pc->p[0];
1139 }
1140 NR::Point const pt(( pc->npoints < 4
1141 ? (NR::Point)(crv->finalPoint())
1142 : pc->p[3] ));
1143 pc->npoints = 2;
1144 pc->green_curve->backspace();
1145 sp_canvas_item_hide(pc->c0);
1146 sp_canvas_item_hide(pc->c1);
1147 sp_canvas_item_hide(pc->cl0);
1148 sp_canvas_item_hide(pc->cl1);
1149 pc->state = SP_PEN_CONTEXT_POINT;
1150 spdc_pen_set_subsequent_point(pc, pt, true);
1151 ret = TRUE;
1152 }
1153 break;
1154 default:
1155 break;
1156 }
1157 return ret;
1158 }
1160 static void
1161 spdc_reset_colors(SPPenContext *pc)
1162 {
1163 /* Red */
1164 pc->red_curve->reset();
1165 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1166 /* Blue */
1167 pc->blue_curve->reset();
1168 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->blue_bpath), NULL);
1169 /* Green */
1170 while (pc->green_bpaths) {
1171 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
1172 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
1173 }
1174 pc->green_curve->reset();
1175 if (pc->green_anchor) {
1176 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1177 }
1178 pc->sa = NULL;
1179 pc->ea = NULL;
1180 pc->npoints = 0;
1181 pc->red_curve_is_valid = false;
1182 }
1185 static void
1186 spdc_pen_set_initial_point(SPPenContext *const pc, NR::Point const p)
1187 {
1188 g_assert( pc->npoints == 0 );
1190 pc->p[0] = p;
1191 pc->p[1] = p;
1192 pc->npoints = 2;
1193 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
1195 sp_canvas_force_full_redraw_after_interruptions(pc->desktop->canvas, 5);
1196 }
1198 /**
1199 * Show the status message for the current line/curve segment.
1200 * This type of message always shows angle/distance as the last
1201 * two parameters ("angle %3.2f°, distance %s").
1202 */
1203 static void
1204 spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point const p, int pc_point_to_compare, gchar const *message)
1205 {
1206 g_assert(pc != NULL);
1207 g_assert((pc_point_to_compare == 0) || (pc_point_to_compare == 3)); // exclude control handles
1208 g_assert(message != NULL);
1210 SPDesktop *desktop = SP_EVENT_CONTEXT(pc)->desktop;
1211 NR::Point rel = p - pc->p[pc_point_to_compare];
1212 GString *dist = SP_PX_TO_METRIC_STRING(NR::L2(rel), desktop->namedview->getDefaultMetric());
1213 double angle = atan2(rel[NR::Y], rel[NR::X]) * 180 / M_PI;
1214 if (prefs_get_int_attribute("options.compassangledisplay", "value", 0) != 0)
1215 angle = angle_to_compass (angle);
1217 pc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, message, angle, dist->str);
1218 g_string_free(dist, FALSE);
1219 }
1221 static void
1222 spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status)
1223 {
1224 g_assert( pc->npoints != 0 );
1225 /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
1227 pc->p[2] = p;
1228 pc->p[3] = p;
1229 pc->p[4] = p;
1230 pc->npoints = 5;
1231 pc->red_curve->reset();
1232 bool is_curve;
1233 pc->red_curve->moveto(pc->p[0]);
1234 if (pc->polylines_paraxial && !statusbar) {
1235 // we are drawing horizontal/vertical lines and hit an anchor; draw an L-shaped path
1236 NR::Point intermed = p;
1237 pen_set_to_nearest_horiz_vert(pc, intermed, status);
1238 pc->red_curve->lineto(intermed);
1239 pc->red_curve->lineto(p);
1240 is_curve = false;
1241 } else {
1242 // one of the 'regular' modes
1243 if (pc->p[1] != pc->p[0])
1244 {
1245 pc->red_curve->curveto(pc->p[1], p, p);
1246 is_curve = true;
1247 } else {
1248 pc->red_curve->lineto(p);
1249 is_curve = false;
1250 }
1251 }
1253 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1255 if (statusbar) {
1256 gchar *message = is_curve ?
1257 _("<b>Curve segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path" ):
1258 _("<b>Line segment</b>: angle %3.2f°, distance %s; with <b>Ctrl</b> to snap angle, <b>Enter</b> to finish the path");
1259 spdc_pen_set_angle_distance_status_message(pc, p, 0, message);
1260 }
1261 }
1263 static void
1264 spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
1265 {
1266 sp_canvas_item_show(pc->c1);
1267 sp_canvas_item_show(pc->cl1);
1269 if ( pc->npoints == 2 ) {
1270 pc->p[1] = p;
1271 sp_canvas_item_hide(pc->c0);
1272 sp_canvas_item_hide(pc->cl0);
1273 SP_CTRL(pc->c1)->moveto(pc->p[1]);
1274 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[0], pc->p[1]);
1276 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"));
1277 } else if ( pc->npoints == 5 ) {
1278 pc->p[4] = p;
1279 sp_canvas_item_show(pc->c0);
1280 sp_canvas_item_show(pc->cl0);
1281 bool is_symm = false;
1282 if ( ( ( pc->mode == SP_PEN_CONTEXT_MODE_CLICK ) && ( state & GDK_CONTROL_MASK ) ) ||
1283 ( ( pc->mode == SP_PEN_CONTEXT_MODE_DRAG ) && !( state & GDK_SHIFT_MASK ) ) ) {
1284 NR::Point delta = p - pc->p[3];
1285 pc->p[2] = pc->p[3] - delta;
1286 is_symm = true;
1287 pc->red_curve->reset();
1288 pc->red_curve->moveto(pc->p[0]);
1289 pc->red_curve->curveto(pc->p[1], pc->p[2], pc->p[3]);
1290 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
1291 }
1292 SP_CTRL(pc->c0)->moveto(pc->p[2]);
1293 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), pc->p[3], pc->p[2]);
1294 SP_CTRL(pc->c1)->moveto(pc->p[4]);
1295 sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl1), pc->p[3], pc->p[4]);
1297 gchar *message = is_symm ?
1298 _("<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") :
1299 _("<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");
1300 spdc_pen_set_angle_distance_status_message(pc, p, 3, message);
1301 } else {
1302 g_warning("Something bad happened - npoints is %d", pc->npoints);
1303 }
1304 }
1306 static void
1307 spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
1308 {
1309 if (pc->polylines_paraxial) {
1310 pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
1311 }
1312 ++pc->num_clicks;
1314 if (!pc->red_curve->is_empty()) {
1315 pc->green_curve->append_continuous(pc->red_curve, 0.0625);
1316 SPCurve *curve = pc->red_curve->copy();
1317 /// \todo fixme:
1318 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
1319 curve->unref();
1320 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1322 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
1324 pc->p[0] = pc->p[3];
1325 pc->p[1] = pc->p[4];
1326 pc->npoints = 2;
1328 pc->red_curve->reset();
1329 }
1330 }
1332 static void
1333 spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
1334 {
1335 if (pc->expecting_clicks_for_LPE > 1) {
1336 // don't let the path be finished before we have collected the required number of mouse clicks
1337 return;
1338 }
1340 pc->num_clicks = 0;
1342 pen_disable_events(pc);
1344 SPDesktop *const desktop = pc->desktop;
1345 pc->_message_context->clear();
1346 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Drawing finished"));
1348 pc->red_curve->reset();
1349 spdc_concat_colors_and_flush(pc, closed);
1350 pc->sa = NULL;
1351 pc->ea = NULL;
1353 pc->npoints = 0;
1354 pc->state = SP_PEN_CONTEXT_POINT;
1356 sp_canvas_item_hide(pc->c0);
1357 sp_canvas_item_hide(pc->c1);
1358 sp_canvas_item_hide(pc->cl0);
1359 sp_canvas_item_hide(pc->cl1);
1361 if (pc->green_anchor) {
1362 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
1363 }
1366 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
1368 pen_enable_events(pc);
1369 }
1371 static void
1372 pen_disable_events(SPPenContext *const pc) {
1373 pc->events_disabled++;
1374 }
1376 static void
1377 pen_enable_events(SPPenContext *const pc) {
1378 g_return_if_fail(pc->events_disabled != 0);
1380 pc->events_disabled--;
1381 }
1383 void
1384 sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEffect::EffectType effect_type,
1385 unsigned int num_clicks, bool use_polylines)
1386 {
1387 if (effect_type == Inkscape::LivePathEffect::INVALID_LPE)
1388 return;
1390 g_print ("Now waiting for %s to be applied\n",
1391 Inkscape::LivePathEffect::LPETypeConverter.get_label(effect_type).c_str());
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 g_print ("Cancelled waiting for mouse clicks for %s\n",
1403 Inkscape::LivePathEffect::LPETypeConverter.get_label(pc->waiting_LPE_type).c_str());
1405 pc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
1406 pc->expecting_clicks_for_LPE = 0;
1407 sp_pen_context_set_polyline_mode(pc);
1408 }
1410 static int pen_next_paraxial_direction(const SPPenContext *const pc,
1411 NR::Point const &pt, NR::Point const &origin, guint state) {
1412 /*
1413 * after the first mouse click we determine whether the mouse pointer is closest to a
1414 * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
1415 * orthogonal to the last one; pressing Shift toggles the direction
1416 */
1417 if (pc->num_clicks == 0) {
1418 // first mouse click
1419 double dist_h = fabs(pt[NR::X] - origin[NR::X]);
1420 double dist_v = fabs(pt[NR::Y] - origin[NR::Y]);
1421 int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
1422 pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
1423 return pen_last_paraxial_dir;
1424 } else {
1425 // subsequent mouse click
1426 return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
1427 }
1428 }
1430 void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state)
1431 {
1432 NR::Point const &origin = pc->p[0];
1434 int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
1436 if (next_dir == 0) {
1437 // line is forced to be horizontal
1438 pt[NR::Y] = origin[NR::Y];
1439 } else {
1440 // line is forced to be vertical
1441 pt[NR::X] = origin[NR::X];
1442 }
1443 }
1445 NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::Point const &pt)
1446 {
1447 NR::Point const &origin = pc->p[0];
1449 if (pen_last_paraxial_dir == 0) {
1450 return NR::Point (origin[NR::X], pt[NR::Y]);
1451 } else {
1452 return NR::Point (pt[NR::X], origin[NR::Y]);
1453 }
1454 }
1456 /*
1457 Local Variables:
1458 mode:c++
1459 c-file-style:"stroustrup"
1460 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1461 indent-tabs-mode:nil
1462 fill-column:99
1463 End:
1464 */
1465 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :