Exemplo n.º 1
0
static void
spdc_set_endpoint (SPPencilContext *pc, NRPointF *p, guint state)
{
	SPDrawContext *dc;

	dc = SP_DRAW_CONTEXT (pc);

	g_assert (dc->npoints > 0);

	spdc_endpoint_snap (dc, p, state);

	dc->p[1] = *p;
	dc->npoints = 2;

	sp_curve_reset (dc->red_curve);
	sp_curve_moveto (dc->red_curve, dc->p[0].x, dc->p[0].y);
	sp_curve_lineto (dc->red_curve, dc->p[1].x, dc->p[1].y);
	sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (dc->red_bpath), dc->red_curve);
}
Exemplo n.º 2
0
static void
spdc_finish_endpoint (SPPencilContext *pc, NRPointF *p, gboolean snap, guint state)
{
	SPDrawContext *dc;

	dc = SP_DRAW_CONTEXT (pc);

	if (SP_CURVE_LENGTH (dc->red_curve) < 2) {
		/* Just a click, reset red curve and continue */
		g_print ("Finishing empty red curve\n");
		sp_curve_reset (dc->red_curve);
		sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (dc->red_bpath), NULL);
	} else if (SP_CURVE_LENGTH (dc->red_curve) > 2) {
		g_warning ("Red curve length is %d", SP_CURVE_LENGTH (dc->red_curve));
		sp_curve_reset (dc->red_curve);
		sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (dc->red_bpath), NULL);
	} else {
		ArtBpath *s, *e;
		/* We have actual line */
		if (snap) {
			/* Do (bogus?) snap */
			spdc_endpoint_snap (dc, p, state);
		}
		/* fixme: We really should test start and end anchors instead */
		s = SP_CURVE_SEGMENT (dc->red_curve, 0);
		e = SP_CURVE_SEGMENT (dc->red_curve, 1);
		if ((e->x3 == s->x3) && (e->y3 == s->y3)) {
			/* Empty line, reset red curve and continue */
			g_print ("Finishing zero length red curve\n");
			sp_curve_reset (dc->red_curve);
			sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (dc->red_bpath), NULL);
		} else {
			/* Write curves to object */
			g_print ("Finishing real red curve\n");
			spdc_concat_colors_and_flush (dc, FALSE);
			dc->sa = NULL;
			dc->ea = NULL;
		}
	}
}
Exemplo n.º 3
0
static gint
pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
{
    gint ret = FALSE;

    SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
    if ( revent.button == 1 && pc->is_drawing && !event_context->space_panning) {
        SPDesktop *const dt = pc->desktop;

        pc->is_drawing = false;

        /* Find desktop coordinates */
        Geom::Point p = dt->w2d(Geom::Point(revent.x, revent.y));

        /* Test whether we hit any anchor. */
        SPDrawAnchor *anchor = spdc_test_inside(pc, Geom::Point(revent.x,
                                                              revent.y));

        switch (pc->state) {
            case SP_PENCIL_CONTEXT_IDLE:
                /* Releasing button in idle mode means single click */
                /* We have already set up start point/anchor in button_press */
                if (!(revent.state & GDK_CONTROL_MASK)) {
                    // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
                    pc->state = SP_PENCIL_CONTEXT_ADDLINE;
                }
                ret = TRUE;
                break;
            case SP_PENCIL_CONTEXT_ADDLINE:
                /* Finish segment now */
                if (anchor) {
                    p = anchor->dp;
                } else {
                    spdc_endpoint_snap(pc, p, revent.state);
                }
                pc->ea = anchor;
                spdc_set_endpoint(pc, p);
                spdc_finish_endpoint(pc);
                pc->state = SP_PENCIL_CONTEXT_IDLE;
                ret = TRUE;
                break;
            case SP_PENCIL_CONTEXT_FREEHAND:
                if (revent.state & GDK_MOD1_MASK) {
                    /* sketch mode: interpolate the sketched path and improve the current output path with the new interpolation. don't finish sketch */

                    sketch_interpolate(pc);

                    if (pc->green_anchor) {
                        pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
                    }

                    pc->state = SP_PENCIL_CONTEXT_SKETCH;
                } else {
                    /* Finish segment now */
                    /// \todo fixme: Clean up what follows (Lauris)
                    if (anchor) {
                        p = anchor->dp;
                    }
                    pc->ea = anchor;
                    /* Write curves to object */

                    dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));

                    interpolate(pc);
                    spdc_concat_colors_and_flush(pc, FALSE);
                    pc->sa = NULL;
                    pc->ea = NULL;
                    if (pc->green_anchor) {
                        pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
                    }
                    pc->state = SP_PENCIL_CONTEXT_IDLE;
                    // reset sketch mode too
                    pc->sketch_n = 0;
                }
                ret = TRUE;
                break;
            case SP_PENCIL_CONTEXT_SKETCH:
            default:
                break;
        }

        if (pc->grab) {
            /* Release grab now */
            sp_canvas_item_ungrab(pc->grab, revent.time);
            pc->grab = NULL;
        }

        ret = TRUE;
    }
    return ret;
}
Exemplo n.º 4
0
static gint
pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
{
    if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) {
        // mouse was accidentally moved during Ctrl+click;
        // ignore the motion and create a single point
        pc->is_drawing = false;
        return TRUE;
    }
    gint ret = FALSE;
    SPDesktop *const dt = pc->desktop;

    SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
    if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
        // allow scrolling
        return FALSE;
    }

    if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
        /* Grab mouse, so release will not pass unnoticed */
        pc->grab = SP_CANVAS_ITEM(dt->acetate);
        sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
                                        GDK_BUTTON_RELEASE_MASK |
                                        GDK_POINTER_MOTION_MASK  ),
                            NULL, mevent.time);
    }

    /* Find desktop coordinates */
    Geom::Point p = dt->w2d(Geom::Point(mevent.x, mevent.y));

    /* Test whether we hit any anchor. */
    SPDrawAnchor *anchor = spdc_test_inside(pc, Geom::Point(mevent.x, mevent.y));

    if (pencil_within_tolerance) {
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
        gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
        if ( Geom::LInfty( Geom::Point(mevent.x,mevent.y) - pencil_drag_origin_w ) < tolerance ) {
            return FALSE;   // Do not drag if we're within tolerance from origin.
        }
    }

    // Once the user has moved farther than tolerance from the original location
    // (indicating they intend to move the object, not click), then always process the
    // motion notify coordinates as given (no snapping back to origin)
    pencil_within_tolerance = false;

    switch (pc->state) {
        case SP_PENCIL_CONTEXT_ADDLINE:
            /* Set red endpoint */
            if (anchor) {
                p = anchor->dp;
            } else {
                Geom::Point ptnr(p);
                spdc_endpoint_snap(pc, ptnr, mevent.state);
                p = ptnr;
            }
            spdc_set_endpoint(pc, p);
            ret = TRUE;
            break;
        default:
            /* We may be idle or already freehand */
            if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
                pc->state = SP_PENCIL_CONTEXT_FREEHAND;
                if ( !pc->sa && !pc->green_anchor ) {
                    /* Create green anchor */
                    pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
                }
                /** \todo
                 * fixme: I am not sure whether we want to snap to anchors
                 * in middle of freehand (Lauris)
                 */
                SnapManager &m = dt->namedview->snap_manager;
                
                if (anchor) {
                    p = anchor->dp;
                } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
                    m.setup(dt);
                    Inkscape::SnappedPoint const s = m.freeSnap(Inkscape::SnapPreferences::SNAPPOINT_NODE, p);
                    if (s.getSnapped()) {
                    	s.getPoint(p);
                    	pc->prev_snap_was_succesful = true;
                    } else {
                    	pc->prev_snap_was_succesful = false;
                    }
                }
                if ( pc->npoints != 0) { // buttonpress may have happened before we entered draw context!
                	if (!(pc->prev_snap_was_succesful && m.snapprefs.getSnapPostponedGlobally())) {
	                    // When snapping is enabled but temporarily on hold because the mouse is moving 
                        // fast, then we don't want to add nodes off-grid
                        spdc_add_freehand_point(pc, p, mevent.state);
	                    ret = TRUE;
                	}
                }

                if (anchor && !pc->anchor_statusbar) {
                    pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
                    pc->anchor_statusbar = true;
                } else if (!anchor && pc->anchor_statusbar) {
                    pc->_message_context->clear();
                    pc->anchor_statusbar = false;
                } else if (!anchor) {
                    pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
                }

            } else {
                if (anchor && !pc->anchor_statusbar) {
                    pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
                    pc->anchor_statusbar = true;
                } else if (!anchor && pc->anchor_statusbar) {
                    pc->_message_context->clear();
                    pc->anchor_statusbar = false;
                }
            }
            break;
    }
    return ret;
}
Exemplo n.º 5
0
gint
sp_pen_context_root_handler (SPEventContext *ec, GdkEvent *event)
{
	SPDrawContext *dc;
	SPPenContext *pc;
	SPDesktop *dt;
	NRPointF p;
	gint ret;
	SPDrawAnchor *anchor;
	NRPointF fp;

	dc = SP_DRAW_CONTEXT (ec);
	pc = SP_PEN_CONTEXT (ec);
	dt = ec->desktop;

	ret = FALSE;

	switch (event->type) {
	case GDK_BUTTON_PRESS:
		if (event->button.button == 1) {
#if 0
			/* Grab mouse, so release will not pass unnoticed */
			dc->grab = SP_CANVAS_ITEM (dt->acetate);
			sp_canvas_item_grab (dc->grab, SPDC_EVENT_MASK, NULL, event->button.time);
#endif
			/* Find desktop coordinates */
			sp_desktop_w2d_xy_point (dt, &fp, event->button.x, event->button.y);
			p.x = fp.x;
			p.y = fp.y;
			/* Test, whether we hit any anchor */
			anchor = test_inside (dc, event->button.x, event->button.y);

			switch (pc->mode) {
			case SP_PEN_CONTEXT_MODE_CLICK:
				/* In click mode we add point on release */
				switch (pc->state) {
				case SP_PEN_CONTEXT_POINT:
				case SP_PEN_CONTEXT_CONTROL:
				case SP_PEN_CONTEXT_CLOSE:
					break;
				case SP_PEN_CONTEXT_STOP:
					/* This is allowed, if we just cancelled curve */
					pc->state = SP_PEN_CONTEXT_POINT;
					break;
				default:
					break;
				}
				break;
			case SP_PEN_CONTEXT_MODE_DRAG:
				switch (pc->state) {
				case SP_PEN_CONTEXT_STOP:
					/* This is allowed, if we just cancelled curve */
				case SP_PEN_CONTEXT_POINT:
					if (dc->npoints == 0) {
						/* Set start anchor */
						dc->sa = anchor;
						if (anchor) {
							/* Adjust point to anchor if needed */
							p = anchor->dp;
						} else {
							/* Create green anchor */
							dc->green_anchor = sp_draw_anchor_new (dc, dc->green_curve, TRUE, p.x, p.y);
						}
						spdc_pen_set_point (pc, &p, event->motion.state);
					} else {
						/* Set end anchor */
						dc->ea = anchor;
						spdc_pen_set_point (pc, &p, event->motion.state);
						if (dc->green_anchor && dc->green_anchor->active) {
							pc->state = SP_PEN_CONTEXT_CLOSE;
							ret = TRUE;
							break;
						}
					}
					pc->state = SP_PEN_CONTEXT_CONTROL;
					ret = TRUE;
					break;
				case SP_PEN_CONTEXT_CONTROL:
					g_warning ("Button down in CONTROL state");
					break;
				case SP_PEN_CONTEXT_CLOSE:
					g_warning ("Button down in CLOSE state");
					break;
				default:
					break;
				}
				break;
			default:
				break;
			}
		}
		break;
	case GDK_MOTION_NOTIFY:
#if 1
		if ((event->motion.state & GDK_BUTTON1_MASK) && !dc->grab) {
			/* Grab mouse, so release will not pass unnoticed */
			dc->grab = SP_CANVAS_ITEM (dt->acetate);
			sp_canvas_item_grab (dc->grab, SPDC_EVENT_MASK, NULL, event->button.time);
		}
#endif
		/* Find desktop coordinates */
		sp_desktop_w2d_xy_point (dt, &fp, event->motion.x, event->motion.y);
		p.x = fp.x;
		p.y = fp.y;
		/* Test, whether we hit any anchor */
		anchor = test_inside (dc, event->button.x, event->button.y);
		if (!anchor) {
			/* Snap only if not hitting anchor */
			spdc_endpoint_snap (dc, &p, event->motion.state);
		}

		switch (pc->mode) {
		case SP_PEN_CONTEXT_MODE_CLICK:
			switch (pc->state) {
			case SP_PEN_CONTEXT_POINT:
				if (dc->npoints != 0) {
					/* Only set point, if we are already appending */
					/* fixme: Snapping */
					spdc_pen_set_point (pc, &p, event->motion.state);
					ret = TRUE;
				}
				break;
			case SP_PEN_CONTEXT_CONTROL:
			case SP_PEN_CONTEXT_CLOSE:
				/* Placing controls is last operation in CLOSE state */
				/* fixme: Snapping */
				spdc_pen_set_ctrl (pc, &p, event->motion.state);
				ret = TRUE;
				break;
			case SP_PEN_CONTEXT_STOP:
				/* This is perfectly valid */
				break;
			default:
				break;
			}
			break;
		case SP_PEN_CONTEXT_MODE_DRAG:
			switch (pc->state) {
			case SP_PEN_CONTEXT_POINT:
				if (dc->npoints > 0) {
					/* Only set point, if we are already appending */
					/* fixme: Snapping */
					spdc_pen_set_point (pc, &p, event->motion.state);
					ret = TRUE;
				}
				break;
			case SP_PEN_CONTEXT_CONTROL:
			case SP_PEN_CONTEXT_CLOSE:
				/* Placing controls is last operation in CLOSE state */
				/* fixme: Snapping */
				spdc_pen_set_ctrl (pc, &p, event->motion.state);
				ret = TRUE;
				break;
			case SP_PEN_CONTEXT_STOP:
				/* This is perfectly valid */
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		break;
	case GDK_BUTTON_RELEASE:
		if (event->button.button == 1) {
			/* Find desktop coordinates */
			sp_desktop_w2d_xy_point (dt, &fp, event->motion.x, event->motion.y);
			p.x = fp.x;
			p.y = fp.y;
			/* Test, whether we hit any anchor */
			anchor = test_inside (dc, event->button.x, event->button.y);

			switch (pc->mode) {
			case SP_PEN_CONTEXT_MODE_CLICK:
				switch (pc->state) {
				case SP_PEN_CONTEXT_POINT:
					if (dc->npoints == 0) {
						/* Start new thread only with button release */
						if (anchor) {
							p = anchor->dp;
						}
						dc->sa = anchor;
						spdc_pen_set_point (pc, &p, event->motion.state);
					} else {
						/* Set end anchor here */
						dc->ea = anchor;
					}
					pc->state = SP_PEN_CONTEXT_CONTROL;
					ret = TRUE;
					break;
				case SP_PEN_CONTEXT_CONTROL:
					/* End current segment */
					spdc_pen_finish_segment (pc, &p, event->button.state);
					pc->state = SP_PEN_CONTEXT_POINT;
					ret = TRUE;
					break;
				case SP_PEN_CONTEXT_CLOSE:
					/* End current segment */
					spdc_pen_finish_segment (pc, &p, event->button.state);
					spdc_pen_finish (pc, TRUE);
					pc->state = SP_PEN_CONTEXT_POINT;
					ret = TRUE;
					break;
				case SP_PEN_CONTEXT_STOP:
					/* This is allowed, if we just cancelled curve */
					pc->state = SP_PEN_CONTEXT_POINT;
					ret = TRUE;
					break;
				default:
					break;
				}
				break;
			case SP_PEN_CONTEXT_MODE_DRAG:
				switch (pc->state) {
				case SP_PEN_CONTEXT_POINT:
				case SP_PEN_CONTEXT_CONTROL:
					spdc_pen_finish_segment (pc, &p, event->button.state);
					break;
				case SP_PEN_CONTEXT_CLOSE:
					spdc_pen_finish_segment (pc, &p, event->button.state);
					spdc_pen_finish (pc, TRUE);
					break;
				case SP_PEN_CONTEXT_STOP:
					/* This is allowed, if we just cancelled curve */
					break;
				default:
					break;
				}
				pc->state = SP_PEN_CONTEXT_POINT;
				ret = TRUE;
				break;
			default:
				break;
			}
#if 1
			if (dc->grab) {
				/* Release grab now */
				sp_canvas_item_ungrab (dc->grab, event->button.time);
				dc->grab = NULL;
			}
#endif
			ret = TRUE;
		}
		break;
	case GDK_2BUTTON_PRESS:
		spdc_pen_finish (pc, FALSE);
		ret = TRUE;
		break;
	case GDK_KEY_PRESS:
		/* fixme: */
		switch (event->key.keyval) {
		case GDK_Return:
			spdc_pen_finish (pc, FALSE);
			ret = TRUE;
			break;
		case GDK_Escape:
			pc->state = SP_PEN_CONTEXT_STOP;
			spdc_reset_colors (dc);
			sp_canvas_item_hide (pc->c0);
			sp_canvas_item_hide (pc->c1);
			sp_canvas_item_hide (pc->cl0);
			sp_canvas_item_hide (pc->cl1);
			ret = TRUE;
			break;
		case GDK_BackSpace:
			if (sp_curve_is_empty (dc->green_curve)) {
				/* Same as cancel */
				pc->state = SP_PEN_CONTEXT_STOP;
				spdc_reset_colors (dc);
				sp_canvas_item_hide (pc->c0);
				sp_canvas_item_hide (pc->c1);
				sp_canvas_item_hide (pc->cl0);
				sp_canvas_item_hide (pc->cl1);
				ret = TRUE;
				break;
			} else {
				NRPointF pt;
				ArtBpath *p;
				gint e;
				/* Reset red curve */
				sp_curve_reset (dc->red_curve);
				/* Destroy topmost green bpath */
				gtk_object_destroy (GTK_OBJECT (dc->green_bpaths->data));
				dc->green_bpaths = g_slist_remove (dc->green_bpaths, dc->green_bpaths->data);
				/* Get last segment */
				p = SP_CURVE_BPATH (dc->green_curve);
				e = SP_CURVE_LENGTH (dc->green_curve);
				if (e < 2) {
					g_warning ("Green curve length is %d", e);
					break;
				}
				dc->p[0].x = p[e - 2].x3;
				dc->p[0].y = p[e - 2].y3;
				dc->p[1].x = p[e - 1].x1;
				dc->p[1].y = p[e - 1].y1;
				if (dc->npoints < 4) {
					pt.x = p[e - 1].x3;
					pt.y = p[e - 1].y3;
				} else {
					pt.x = dc->p[3].x;
					pt.y = dc->p[3].y;
				}
				dc->npoints = 2;
				sp_curve_backspace (dc->green_curve);
				sp_canvas_item_hide (pc->c0);
				sp_canvas_item_hide (pc->c1);
				sp_canvas_item_hide (pc->cl0);
				sp_canvas_item_hide (pc->cl1);
				pc->state = SP_PEN_CONTEXT_POINT;
				spdc_pen_set_point (pc, &pt, event->motion.state);
				ret = TRUE;
			}
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}

	if (!ret) {
		if (((SPEventContextClass *) pen_parent_class)->root_handler)
			return ((SPEventContextClass *) pen_parent_class)->root_handler (ec, event);
	}

	return ret;
}