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