From: johanengelen Date: Wed, 17 Jan 2007 08:28:11 +0000 (+0000) Subject: Upgraded gradient tool =) X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=aaa77a55f097db8d9c526d7f7b17b58bf065830f;p=inkscape.git Upgraded gradient tool =) --- diff --git a/src/desktop.cpp b/src/desktop.cpp index bf4b0ba65..903a6bd7c 100644 --- a/src/desktop.cpp +++ b/src/desktop.cpp @@ -128,7 +128,8 @@ SPDesktop::SPDesktop() is_fullscreen = false; gr_item = NULL; - gr_point_num = 0; + gr_point_type = 0; + gr_point_i = 0; gr_fill_or_stroke = true; _layer_hierarchy = NULL; diff --git a/src/desktop.h b/src/desktop.h index e9e2f638e..8f2fac1e8 100644 --- a/src/desktop.h +++ b/src/desktop.h @@ -104,8 +104,10 @@ struct SPDesktop : public Inkscape::UI::View::View // storage for selected dragger used by GrDrag as it's // created and deleted by tools SPItem *gr_item; - guint gr_point_num; + guint gr_point_type; + guint gr_point_i; bool gr_fill_or_stroke; + Inkscape::ObjectHierarchy *_layer_hierarchy; gchar * _reconstruction_old_layer_id; diff --git a/src/display/sp-ctrlline.cpp b/src/display/sp-ctrlline.cpp index 96bda9880..f862b64af 100644 --- a/src/display/sp-ctrlline.cpp +++ b/src/display/sp-ctrlline.cpp @@ -24,16 +24,9 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif -#include #include -struct SPCtrlLine : public SPCanvasItem{ - guint32 rgba; - NRPoint s, e; - Shape* shp; -}; -struct SPCtrlLineClass : public SPCanvasItemClass{}; static void sp_ctrlline_class_init (SPCtrlLineClass *klass); static void sp_ctrlline_init (SPCtrlLine *ctrlline); diff --git a/src/display/sp-ctrlline.h b/src/display/sp-ctrlline.h index 500fbf08f..c4368686f 100644 --- a/src/display/sp-ctrlline.h +++ b/src/display/sp-ctrlline.h @@ -13,6 +13,7 @@ */ #include "sp-canvas.h" +#include @@ -20,8 +21,12 @@ #define SP_CTRLLINE(obj) (GTK_CHECK_CAST ((obj), SP_TYPE_CTRLLINE, SPCtrlLine)) #define SP_IS_CTRLLINE(obj) (GTK_CHECK_TYPE ((obj), SP_TYPE_CTRLLINE)) -struct SPCtrlLine; -struct SPCtrlLineClass; +struct SPCtrlLine : public SPCanvasItem{ + guint32 rgba; + NRPoint s, e; + Shape* shp; +}; +struct SPCtrlLineClass : public SPCanvasItemClass{}; GtkType sp_ctrlline_get_type (void); diff --git a/src/gradient-chemistry.cpp b/src/gradient-chemistry.cpp index 6a485e573..0da9896e1 100644 --- a/src/gradient-chemistry.cpp +++ b/src/gradient-chemistry.cpp @@ -19,6 +19,7 @@ #include "desktop-style.h" #include "sp-gradient-reference.h" +#include "sp-gradient-vector.h" #include "sp-linear-gradient.h" #include "sp-radial-gradient.h" #include "sp-stop.h" @@ -498,11 +499,24 @@ sp_last_stop(SPGradient *gradient) return stop; } return NULL; +} + +SPStop* +sp_get_stop_i(SPGradient *gradient, guint stop_i) +{ + SPStop *stop = sp_first_stop (gradient); + + for (guint i=0; i < stop_i; i++) { + if (!stop) return NULL; + stop = sp_next_stop (stop); + } + + return stop; } void -sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke) +sp_item_gradient_edit_stop (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -510,8 +524,8 @@ sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke) return; SPGradient *vector = sp_gradient_get_vector (gradient, false); - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: case POINT_RG_CENTER: case POINT_RG_FOCUS: { @@ -520,7 +534,7 @@ sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke) } break; - case POINT_LG_P2: + case POINT_LG_END: case POINT_RG_R1: case POINT_RG_R2: { @@ -528,13 +542,22 @@ sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke) gtk_widget_show (dialog); } break; + + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + { + GtkWidget *dialog = sp_gradient_vector_editor_new (vector, sp_get_stop_i (vector, point_i)); + gtk_widget_show (dialog); + } + break; default: break; } } guint32 -sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_stroke) +sp_item_gradient_stop_query_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -546,8 +569,8 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s if (!vector) // orphan! return 0; // what else to do? - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: case POINT_RG_CENTER: case POINT_RG_FOCUS: { @@ -558,7 +581,7 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s } break; - case POINT_LG_P2: + case POINT_LG_END: case POINT_RG_R1: case POINT_RG_R2: { @@ -568,6 +591,18 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s } } break; + + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + { + SPStop *stopi = sp_get_stop_i (vector, point_i); + if (stopi) { + return sp_stop_get_rgba32(stopi); + } + } + break; + default: break; } @@ -575,7 +610,7 @@ sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_s } void -sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_stroke, SPCSSAttr *stop) +sp_item_gradient_stop_set_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, SPCSSAttr *stop) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -586,14 +621,16 @@ sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_str if (!vector) // orphan! return; - - vector = sp_gradient_fork_vector_if_necessary (vector); + + /* + vector = sp_gradient_fork_vector_if_necessary (vector); // TESTJOHAN if ( gradient != vector && gradient->ref->getObject() != vector ) { sp_gradient_repr_set_link(SP_OBJECT_REPR(gradient), vector); } + */ - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: case POINT_RG_CENTER: case POINT_RG_FOCUS: { @@ -604,7 +641,7 @@ sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_str } break; - case POINT_LG_P2: + case POINT_LG_END: case POINT_RG_R1: case POINT_RG_R2: { @@ -614,6 +651,18 @@ sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_str } } break; + + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + { + SPStop *stopi = sp_get_stop_i (vector, point_i); + if (stopi) { + sp_repr_css_change (SP_OBJECT_REPR (stopi), stop, "style"); + } + } + break; + default: break; } @@ -672,13 +721,27 @@ sp_item_gradient_reverse_vector (SPItem *item, bool fill_or_stroke) } +// FIXME: make general global function +static double +get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end) +{ + double length = NR::L2(end - begin); + NR::Point be = (end - begin) / length; + double r = NR::dot(p - begin, be); + + if (r < 0.0) return 0.0; + if (r > length) return 1.0; + + return (r / length); +} + /** -Set the position of point point_num of the gradient applied to item (either fill_or_stroke) to +Set the position of point point_type of the gradient applied to item (either fill_or_stroke) to p_w (in desktop coordinates). Write_repr if you want the change to become permanent. */ void -sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool fill_or_stroke, bool write_repr, bool scale) +sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::Point p_w, bool fill_or_stroke, bool write_repr, bool scale) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -696,8 +759,8 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool if (SP_IS_LINEARGRADIENT(gradient)) { SPLinearGradient *lg = SP_LINEARGRADIENT(gradient); - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: if (scale) { lg->x2.computed += (lg->x1.computed - p[NR::X]); lg->y2.computed += (lg->y1.computed - p[NR::Y]); @@ -715,7 +778,7 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG); } break; - case POINT_LG_P2: + case POINT_LG_END: if (scale) { lg->x1.computed += (lg->x2.computed - p[NR::X]); lg->y1.computed += (lg->y2.computed - p[NR::Y]); @@ -733,6 +796,21 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG); } break; + case POINT_LG_MID: + { + // using X-coordinates only to determine the offset, assuming p has been snapped to the vector from begin to end. + double offset = get_offset_between_points (p, NR::Point(lg->x1.computed, lg->y1.computed), NR::Point(lg->x2.computed, lg->y2.computed)); + lg->vector.stops.at(point_i).offset = offset; + SPGradient *vector = sp_gradient_get_vector (lg, false); + SPStop* stopi = sp_get_stop_i(vector, point_i); + stopi->offset = offset; + if (write_repr) { + sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset); + } else { + SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + } + } + break; default: break; } @@ -741,14 +819,14 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool SPRadialGradient *rg = SP_RADIALGRADIENT(gradient); NR::Point c (rg->cx.computed, rg->cy.computed); NR::Point c_w = c * gradient->gradientTransform * i2d; // now in desktop coords - if ((point_num == POINT_RG_R1 || point_num == POINT_RG_R2) && NR::L2 (p_w - c_w) < 1e-3) { + if ((point_type == POINT_RG_R1 || point_type == POINT_RG_R2) && NR::L2 (p_w - c_w) < 1e-3) { // prevent setting a radius too close to the center return; } NR::Matrix new_transform; bool transform_set = false; - switch (point_num) { + switch (point_type) { case POINT_RG_CENTER: rg->fx.computed = p[NR::X] + (rg->fx.computed - rg->cx.computed); rg->fy.computed = p[NR::Y] + (rg->fy.computed - rg->cy.computed); @@ -811,8 +889,37 @@ sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_w, bool break; } - } - + case POINT_RG_MID1: + { + NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed); + NR::Point end = NR::Point (rg->cx.computed + rg->r.computed, rg->cy.computed); + double offset = get_offset_between_points (p, start, end); + rg->vector.stops.at(point_i).offset = offset; + SPGradient *vector = sp_gradient_get_vector (rg, false); + SPStop* stopi = sp_get_stop_i(vector, point_i); + stopi->offset = offset; + if (write_repr) { + sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset); + } else { + SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + } + break; + } + case POINT_RG_MID2: + NR::Point start = NR::Point (rg->cx.computed, rg->cy.computed); + NR::Point end = NR::Point (rg->cx.computed, rg->cy.computed - rg->r.computed); + double offset = get_offset_between_points (p, start, end); + rg->vector.stops.at(point_i).offset = offset; + SPGradient *vector = sp_gradient_get_vector (rg, false); + SPStop* stopi = sp_get_stop_i(vector, point_i); + stopi->offset = offset; + if (write_repr) { + sp_repr_set_css_double(SP_OBJECT_REPR(stopi), "offset", stopi->offset); + } else { + SP_OBJECT (gradient)->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG); + } + break; + } if (transform_set) { gradient->gradientTransform = new_transform; gradient->gradientTransform_set = TRUE; @@ -852,12 +959,12 @@ sp_item_gradient_get_spread (SPItem *item, bool fill_or_stroke) /** -Returns the position of point point_num of the gradient applied to item (either fill_or_stroke), +Returns the position of point point_type of the gradient applied to item (either fill_or_stroke), in desktop coordinates. */ NR::Point -sp_item_gradient_get_coords (SPItem *item, guint point_num, bool fill_or_stroke) +sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { SPGradient *gradient = sp_item_gradient (item, fill_or_stroke); @@ -868,17 +975,23 @@ sp_item_gradient_get_coords (SPItem *item, guint point_num, bool fill_or_stroke) if (SP_IS_LINEARGRADIENT(gradient)) { SPLinearGradient *lg = SP_LINEARGRADIENT(gradient); - switch (point_num) { - case POINT_LG_P1: + switch (point_type) { + case POINT_LG_BEGIN: p = NR::Point (lg->x1.computed, lg->y1.computed); break; - case POINT_LG_P2: + case POINT_LG_END: p = NR::Point (lg->x2.computed, lg->y2.computed); break; + case POINT_LG_MID: + { + gdouble offset = lg->vector.stops.at(point_i).offset; + p = (1-offset) * NR::Point(lg->x1.computed, lg->y1.computed) + offset * NR::Point(lg->x2.computed, lg->y2.computed); + } + break; } } else if (SP_IS_RADIALGRADIENT(gradient)) { SPRadialGradient *rg = SP_RADIALGRADIENT(gradient); - switch (point_num) { + switch (point_type) { case POINT_RG_CENTER: p = NR::Point (rg->cx.computed, rg->cy.computed); break; @@ -891,6 +1004,18 @@ sp_item_gradient_get_coords (SPItem *item, guint point_num, bool fill_or_stroke) case POINT_RG_R2: p = NR::Point (rg->cx.computed, rg->cy.computed - rg->r.computed); break; + case POINT_RG_MID1: + { + gdouble offset = rg->vector.stops.at(point_i).offset; + p = (1-offset) * NR::Point (rg->cx.computed, rg->cy.computed) + offset * NR::Point(rg->cx.computed + rg->r.computed, rg->cy.computed); + } + break; + case POINT_RG_MID2: + { + gdouble offset = rg->vector.stops.at(point_i).offset; + p = (1-offset) * NR::Point (rg->cx.computed, rg->cy.computed) + offset * NR::Point(rg->cx.computed, rg->cy.computed - rg->r.computed); + } + break; } } diff --git a/src/gradient-chemistry.h b/src/gradient-chemistry.h index b649a22ec..b949c68d1 100644 --- a/src/gradient-chemistry.h +++ b/src/gradient-chemistry.h @@ -54,18 +54,20 @@ SPStop* sp_first_stop(SPGradient *gradient); SPStop* sp_last_stop(SPGradient *gradient); SPStop* sp_prev_stop(SPStop *stop, SPGradient *gradient); SPStop* sp_next_stop(SPStop *stop); +SPStop* sp_get_stop_i(SPGradient *gradient, guint i); void sp_gradient_transform_multiply (SPGradient *gradient, NR::Matrix postmul, bool set); -void sp_item_gradient_set_coords (SPItem *item, guint point_num, NR::Point p_desk, bool fill_or_stroke, bool write_repr, bool scale); -NR::Point sp_item_gradient_get_coords (SPItem *item, guint point_num, bool fill_or_stroke); +SPGradient * sp_item_gradient (SPItem *item, bool fill_or_stroke); +void sp_item_gradient_set_coords (SPItem *item, guint point_type, guint point_i, NR::Point p_desk, bool fill_or_stroke, bool write_repr, bool scale); +NR::Point sp_item_gradient_get_coords (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke); SPGradient *sp_item_gradient_get_vector (SPItem *item, bool fill_or_stroke); SPGradientSpread sp_item_gradient_get_spread (SPItem *item, bool fill_or_stroke); struct SPCSSAttr; -void sp_item_gradient_stop_set_style (SPItem *item, guint point_num, bool fill_or_stroke, SPCSSAttr *stop); -guint32 sp_item_gradient_stop_query_style (SPItem *item, guint point_num, bool fill_or_stroke); -void sp_item_gradient_edit_stop (SPItem *item, guint point_num, bool fill_or_stroke); +void sp_item_gradient_stop_set_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, SPCSSAttr *stop); +guint32 sp_item_gradient_stop_query_style (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke); +void sp_item_gradient_edit_stop (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke); void sp_item_gradient_reverse_vector (SPItem *item, bool fill_or_stroke); #endif diff --git a/src/gradient-context.cpp b/src/gradient-context.cpp index ad1dd8730..a84bb4f80 100644 --- a/src/gradient-context.cpp +++ b/src/gradient-context.cpp @@ -26,13 +26,25 @@ #include "message-context.h" #include "message-stack.h" #include "pixmaps/cursor-gradient.xpm" +#include "pixmaps/cursor-gradient-add.xpm" +#include "pixmaps/cursor-gradient-delete.xpm" #include "gradient-context.h" +#include "gradient-chemistry.h" #include #include "prefs-utils.h" #include "gradient-drag.h" #include "gradient-chemistry.h" #include "xml/repr.h" #include "sp-item.h" +#include "display/sp-ctrlline.h" +#include "sp-linear-gradient.h" +#include "sp-radial-gradient.h" +#include "sp-stop.h" +#include "svg/css-ostringstream.h" +#include "svg/svg-color.h" + + + static void sp_gradient_context_class_init(SPGradientContextClass *klass); static void sp_gradient_context_init(SPGradientContext *gr_context); @@ -83,12 +95,13 @@ static void sp_gradient_context_init(SPGradientContext *gr_context) { SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context); + gr_context->cursor_addnode = false; event_context->cursor_shape = cursor_gradient_xpm; event_context->hot_x = 4; event_context->hot_y = 4; event_context->xp = 0; event_context->yp = 0; - event_context->tolerance = 0; + event_context->tolerance = 4; event_context->within_tolerance = false; event_context->item_to_select = NULL; } @@ -142,7 +155,267 @@ sp_gradient_context_select_prev (SPEventContext *event_context) drag->select_prev(); } -static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event) +// FIXME: make global function in libnr or somewhere. +static NR::Point +snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end) +{ + double length = NR::L2(end - begin); + NR::Point be = (end - begin) / length; + double r = NR::dot(p - begin, be); + + if (r < 0.0) return begin; + if (r > length) return end; + + return (begin + r * be); +} + +static bool +sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p) +{ + SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop; + + //Translate mouse point into proper coord system + rc->mousepoint_doc = desktop->w2d(event_p); + + SPCtrlLine* line = SP_CTRLLINE(item); + + NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e); + NR::Point delta = rc->mousepoint_doc - nearest; + + double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance; + + bool close = (NR::L2 (delta) < tolerance); + + return close; +} + +// Fixme : must be able to put this in a general file. +static guint32 +average_color (guint32 c1, guint32 c2, gdouble p = 0.5) +{ + guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p); + guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p); + guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p); + guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p); + + return SP_RGBA32_U_COMPOSE (r, g, b, a); +} + +static double +get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end) +{ + double length = NR::L2(end - begin); + NR::Point be = (end - begin) / length; + double r = NR::dot(p - begin, be); + + if (r < 0.0) return 0.0; + if (r > length) return 1.0; + + return (r / length); +} + +static void +sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 etime) +{ + // item is the selected item. mouse_p the location in doc coordinates of where to add the stop + + SPEventContext *ec = SP_EVENT_CONTEXT(rc); + double tolerance = (double) ec->tolerance; + + gfloat offset; // type of SPStop.offset = gfloat + SPGradient *gradient; + bool fill_or_stroke = true; + bool r1_knot = false; + + bool addknot = false; + do { + gradient = sp_item_gradient (item, fill_or_stroke); + if (SP_IS_LINEARGRADIENT(gradient)) { + NR::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke); + NR::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke); + + NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end); + NR::Point delta = mouse_p - nearest; + if ( NR::L2 (delta) < tolerance ) { + // add the knot + offset = get_offset_between_points(nearest, begin, end); + addknot = true; + break; // break out of the while loop: add only one knot + } + } else if (SP_IS_RADIALGRADIENT(gradient)) { + NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke); + NR::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke); + NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end); + NR::Point delta = mouse_p - nearest; + if ( NR::L2 (delta) < tolerance ) { + offset = get_offset_between_points(nearest, begin, end); + addknot = true; + r1_knot = true; + break; // break out of the while loop: add only one knot + } + + end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke); + nearest = snap_vector_midpoint (mouse_p, begin, end); + delta = mouse_p - nearest; + if ( NR::L2 (delta) < tolerance ) { + offset = get_offset_between_points(nearest, begin, end); + addknot = true; + r1_knot = false; + break; // break out of the while loop: add only one knot + } + } + fill_or_stroke = !fill_or_stroke; + } while (!fill_or_stroke && !addknot) ; + + if (addknot) { + SPGradient *vector = sp_gradient_get_vector (gradient, false); + SPStop* prev_stop = sp_first_stop(vector); + SPStop* next_stop = sp_next_stop(prev_stop); + while ( (next_stop) && (next_stop->offset < offset) ) { + prev_stop = next_stop; + next_stop = sp_next_stop(next_stop); + } + if (!next_stop) { + // logical error: the endstop should have offset 1 and should always be more than this offset here + return; + } + + Inkscape::XML::Node *new_stop_repr = NULL; + new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(); + SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop)); + + SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr); + newstop->offset = offset; + sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset); + guint32 const c1 = sp_stop_get_rgba32(prev_stop); + guint32 const c2 = sp_stop_get_rgba32(next_stop); + guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset)); + Inkscape::CSSOStringStream os; + gchar c[64]; + sp_svg_write_color (c, 64, cnew); + gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew); + os << "stop-color:" << c << ";stop-opacity:" << opacity <<";"; + SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str()); + + + Inkscape::GC::release(new_stop_repr); + sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT, + _("Add gradient stop")); + + ec->_grdrag->updateDraggers(); + sp_gradient_ensure_vector (gradient); + + if (vector->has_stops) { + int i = 0; + for ( SPObject *ochild = sp_object_first_child (SP_OBJECT(vector)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) { + if (SP_IS_STOP (ochild)) { + if ( SP_STOP(ochild) == newstop ) { + break; + } else { + i++; + } + } + } + GrDragger *dragger = NULL; + gradient = sp_item_gradient (item, fill_or_stroke); + GrPointType pointtype; + if (SP_IS_LINEARGRADIENT(gradient)) { + dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, POINT_LG_MID, i, fill_or_stroke); + pointtype = POINT_LG_MID; + } else if (SP_IS_RADIALGRADIENT(gradient)) { + dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, r1_knot ? POINT_RG_MID1 : POINT_RG_MID2, i, fill_or_stroke); + pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2; + } + if (dragger && (etime == 0) ) { + ec->_grdrag->setSelected (dragger); + } else { + ec->_grdrag->grabKnot (item, + pointtype, + i, + fill_or_stroke, 99999, 99999, etime); + } + ec->_grdrag->local_change = true; + + } + } +} + +static void +sp_gradient_context_delete_stops (SPGradientContext *rc, SPItem *item, GrDrag *drag, bool just_one) { + GrDragger *draggertemp = (GrDragger*) drag->selected->data; + GrDraggable *draggabletemp = (GrDraggable*) draggertemp->draggables->data; + SPGradient *gradient = sp_item_gradient (item, draggabletemp->fill_or_stroke); + SPGradient *vector = sp_gradient_get_vector (gradient, false); + + // 2 is the minimum, cannot delete more than that without deleting the whole vector + guint num_delete = just_one ? 1 : g_list_length(drag->selected); + if (vector->vector.stops.size() >= (2+num_delete) ) { + GSList *stoplist = NULL; + while (drag->selected) { + GrDragger *dragger = (GrDragger*) drag->selected->data; + GrDraggable *draggable = (GrDraggable*) dragger->draggables->data; + SPStop *selstop = NULL; + switch (draggable->point_type) { + case POINT_LG_END: + case POINT_RG_R1: + case POINT_RG_R2: + selstop = sp_last_stop(vector); + break; + default: + selstop = sp_get_stop_i(vector, draggable->point_i); + break; + } + if ( !g_slist_find(stoplist, selstop) ) { + stoplist = g_slist_append(stoplist, selstop); + } + drag->selected = g_list_remove(drag->selected, dragger); + if ( just_one ) break; // iterate once if just_one is set. + } + while (stoplist) { + SPStop *stop = (SPStop*) stoplist->data; + SP_OBJECT_REPR(vector)->removeChild(SP_OBJECT_REPR(stop)); + stoplist = g_slist_remove(stoplist, stop); + } + // if we delete first or last stop, move the next/previous to the edge + SPStop *first = sp_first_stop (vector); + if (first) { + if (first->offset != 0) { + first->offset = 0; + sp_repr_set_css_double (SP_OBJECT_REPR (first), "offset", 0); + } + } + SPStop *last = sp_last_stop (vector); + if (last) { + if (last->offset != 1) { + last->offset = 1; + sp_repr_set_css_double (SP_OBJECT_REPR (last), "offset", 1); + } + } + if ( just_one || (num_delete == 1) ) { + sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT, + _("Delete gradient stop")); + } else { + sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT, + _("Delete gradient stops")); + } + } else { // delete the gradient from the object. set fill to unset + SPCSSAttr *css = sp_repr_css_attr_new (); + if (draggabletemp->fill_or_stroke) { + sp_repr_css_unset_property (css, "fill"); + } else { + sp_repr_css_unset_property (css, "stroke"); + } + sp_repr_css_change (SP_OBJECT_REPR (item), css, "style"); + sp_repr_css_attr_unref (css); + sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT, + _("Remove gradient")); + } +} + + + +static gint +sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event) { static bool dragging; @@ -161,20 +434,31 @@ static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkE switch (event->type) { case GDK_2BUTTON_PRESS: if ( event->button.button == 1 ) { - for (GSList const* i = selection->itemList(); i != NULL; i = i->next) { - SPItem *item = SP_ITEM(i->data); - SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR); - guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1); - - SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill); - - SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill); - sp_gradient_reset_to_userspace(priv, item); + bool over_line = false; + SPCtrlLine *line = NULL; + if (drag->lines) { + for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) { + line = (SPCtrlLine*) l->data; + over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y)); + } + } + if (over_line) { + sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time); + } else { + for (GSList const* i = selection->itemList(); i != NULL; i = i->next) { + SPItem *item = SP_ITEM(i->data); + SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR); + guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1); + + SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill); + + SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill); + sp_gradient_reset_to_userspace(priv, item); + } + + sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT, + _("Create default gradient")); } - - sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT, - _("Create default gradient")); - ret = TRUE; } break; @@ -223,39 +507,71 @@ static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkE sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time); ret = TRUE; + } else { + bool over_line = false; + if (drag->lines) { + for (GSList *l = drag->lines; l != NULL; l = l->next) { + over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y)); + } + } + + if (rc->cursor_addnode && !over_line) { + event_context->cursor_shape = cursor_gradient_xpm; + sp_event_context_update_cursor(event_context); + rc->cursor_addnode = false; + } else if (!rc->cursor_addnode && over_line) { + event_context->cursor_shape = cursor_gradient_add_xpm; + sp_event_context_update_cursor(event_context); + rc->cursor_addnode = true; + } } break; case GDK_BUTTON_RELEASE: event_context->xp = event_context->yp = 0; if ( event->button.button == 1 ) { - dragging = false; - - // unless clicked with Ctrl (to enable Ctrl+doubleclick) - if (event->button.state & GDK_CONTROL_MASK) { - ret = TRUE; - break; - } - - if (!event_context->within_tolerance) { - // we've been dragging, do nothing (grdrag handles that) - } else if (event_context->item_to_select) { - // no dragging, select clicked item if any - if (event->button.state & GDK_SHIFT_MASK) { - selection->toggle(event_context->item_to_select); - } else { - selection->set(event_context->item_to_select); + if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) { + bool over_line = false; + SPCtrlLine *line = NULL; + if (drag->lines) { + for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) { + line = (SPCtrlLine*) l->data; + over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y)); + } } - } else { - // click in an empty space; do the same as Esc - if (drag->selected) { - drag->setSelected (NULL); + if (over_line) { + sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0); + ret = TRUE; + } + } else { + dragging = false; + + // unless clicked with Ctrl (to enable Ctrl+doubleclick). (don't what this is for (johan)) + if (event->button.state & GDK_CONTROL_MASK) { + ret = TRUE; + break; + } + + if (!event_context->within_tolerance) { + // we've been dragging, do nothing (grdrag handles that) + } else if (event_context->item_to_select) { + // no dragging, select clicked item if any + if (event->button.state & GDK_SHIFT_MASK) { + selection->toggle(event_context->item_to_select); + } else { + selection->set(event_context->item_to_select); + } } else { - selection->clear(); + // click in an empty space; do the same as Esc + if (drag->selected) { + drag->deselect_all(); + } else { + selection->clear(); + } } + + event_context->item_to_select = NULL; + ret = TRUE; } - - event_context->item_to_select = NULL; - ret = TRUE; } break; case GDK_KEY_PRESS: @@ -284,7 +600,7 @@ static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkE case GDK_Escape: if (drag->selected) { - drag->setSelected (NULL); + drag->deselect_all(); } else { selection->clear(); } @@ -370,6 +686,22 @@ static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkE ret = TRUE; } break; +/* + case GDK_Insert: + case GDK_KP_Insert: + // with any modifiers + sp_node_selected_add_node(); + ret = TRUE; + break; +*/ + case GDK_Delete: + case GDK_KP_Delete: + case GDK_BackSpace: + if ( drag->selected ) { + sp_gradient_context_delete_stops ( rc, SP_ITEM(selection->itemList()->data), drag, MOD__CTRL_ONLY ) ; + ret = TRUE; + } + break; default: break; } @@ -399,7 +731,7 @@ static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkE ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event); } } - + return ret; } @@ -433,11 +765,11 @@ static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint st sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke); if (type == SP_GRADIENT_TYPE_LINEAR) { - sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_P1, rc.origin, fill_or_stroke, true, false); - sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_P2, pt, fill_or_stroke, true, false); + sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false); + sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false); } else if (type == SP_GRADIENT_TYPE_RADIAL) { - sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, rc.origin, fill_or_stroke, true, false); - sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, pt, fill_or_stroke, true, false); + sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false); + sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false); } SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG); } @@ -449,7 +781,8 @@ static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint st // give the grab out-of-bounds values of xp/yp because we're already dragging // and therefore are already out of tolerance ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data), - type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_P2 : POINT_RG_R1, + type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1, + 0, //point_i fill_or_stroke, 99999, 99999, etime); } // We did an undoable action, but sp_document_done will be called by the knot when released diff --git a/src/gradient-context.h b/src/gradient-context.h index 80dfecd3a..e185157a1 100644 --- a/src/gradient-context.h +++ b/src/gradient-context.h @@ -29,7 +29,13 @@ class SPGradientContextClass; struct SPGradientContext : public SPEventContext { NR::Point origin; + + bool cursor_addnode; + + bool node_added; + NR::Point mousepoint_doc; // stores mousepoint when over_line in doc coords + Inkscape::MessageContext *_message_context; }; diff --git a/src/gradient-drag.cpp b/src/gradient-drag.cpp index aaafce67e..2320a98de 100644 --- a/src/gradient-drag.cpp +++ b/src/gradient-drag.cpp @@ -34,6 +34,7 @@ #include "sp-radial-gradient.h" #include "gradient-chemistry.h" #include "gradient-drag.h" +#include "sp-stop.h" #define GR_KNOT_COLOR_NORMAL 0xffffff00 #define GR_KNOT_COLOR_SELECTED 0x0000ff00 @@ -47,23 +48,29 @@ // absolute distance between gradient points for them to become a single dragger when the drag is created: #define MERGE_DIST 0.1 -// knot shapes corresponding to GrPoint enum +// knot shapes corresponding to GrPointType enum SPKnotShapeType gr_knot_shapes [] = { - SP_KNOT_SHAPE_SQUARE, //POINT_LG_P1 - SP_KNOT_SHAPE_SQUARE, + SP_KNOT_SHAPE_SQUARE, //POINT_LG_BEGIN + SP_KNOT_SHAPE_CIRCLE, //POINT_LG_END + SP_KNOT_SHAPE_DIAMOND, //POINT_LG_MID SP_KNOT_SHAPE_DIAMOND, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_SHAPE_CIRCLE, - SP_KNOT_SHAPE_CROSS // POINT_RG_FOCUS + SP_KNOT_SHAPE_CROSS, // POINT_RG_FOCUS + SP_KNOT_SHAPE_DIAMOND, //POINT_RG_MID1 + SP_KNOT_SHAPE_DIAMOND //POINT_RG_MID2 }; const gchar *gr_knot_descr [] = { - N_("Linear gradient start"), //POINT_LG_P1 + N_("Linear gradient start"), //POINT_LG_BEGIN N_("Linear gradient end"), + N_("Linear gradient midstop"), N_("Radial gradient center"), N_("Radial gradient radius"), N_("Radial gradient radius"), - N_("Radial gradient focus") // POINT_RG_FOCUS + N_("Radial gradient focus"), // POINT_RG_FOCUS + N_("Linear gradient midstop"), + N_("Linear gradient midstop") }; static void @@ -112,7 +119,7 @@ gr_drag_style_query (SPStyle *style, int property, gpointer data) int count = 0; - for (GSList const* i = drag->selected->draggables; i != NULL; i = i->next) { // for all draggables of dragger + for (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger GrDraggable *draggable = (GrDraggable *) i->data; if (ret == QUERY_STYLE_NOTHING) { @@ -121,7 +128,7 @@ gr_drag_style_query (SPStyle *style, int property, gpointer data) ret = QUERY_STYLE_MULTIPLE_AVERAGED; } - guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_num, draggable->fill_or_stroke); + guint32 c = sp_item_gradient_stop_query_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke); cf[0] += SP_RGBA32_R_F (c); cf[1] += SP_RGBA32_G_F (c); cf[2] += SP_RGBA32_B_F (c); @@ -211,12 +218,15 @@ gr_drag_style_set (const SPCSSAttr *css, gpointer data) return false; } - for (GSList const* i = drag->selected->draggables; i != NULL; i = i->next) { // for all draggables of dragger - GrDraggable *draggable = (GrDraggable *) i->data; - - drag->local_change = true; - sp_item_gradient_stop_set_style (draggable->item, draggable->point_num, draggable->fill_or_stroke, stop); - } + for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers + GrDragger* dragger = (GrDragger*) sel->data; + for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger + GrDraggable *draggable = (GrDraggable *) i->data; + + drag->local_change = true; + sp_item_gradient_stop_set_style (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop); + } + } //sp_repr_css_print(stop); sp_repr_css_attr_unref(stop); @@ -267,7 +277,7 @@ GrDrag::GrDrag(SPDesktop *desktop) { this->updateLevels (); if (desktop->gr_item) { - this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_num, desktop->gr_fill_or_stroke)); + this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke)); } } @@ -279,16 +289,19 @@ GrDrag::~GrDrag() this->style_query_connection.disconnect(); if (this->selected) { - GrDraggable *draggable = (GrDraggable *) this->selected->draggables->data; + GrDraggable *draggable = (GrDraggable *) ((GrDragger*)this->selected->data)->draggables->data; desktop->gr_item = draggable->item; - desktop->gr_point_num = draggable->point_num; + desktop->gr_point_type = draggable->point_type; + desktop->gr_point_i = draggable->point_i; desktop->gr_fill_or_stroke = draggable->fill_or_stroke; } else { desktop->gr_item = NULL; - desktop->gr_point_num = 0; + desktop->gr_point_type = 0; + desktop->gr_point_i = 0; desktop->gr_fill_or_stroke = true; } - + + deselect_all(); for (GList *l = this->draggers; l != NULL; l = l->next) { delete ((GrDragger *) l->data); } @@ -303,10 +316,11 @@ GrDrag::~GrDrag() this->lines = NULL; } -GrDraggable::GrDraggable (SPItem *item, guint point_num, bool fill_or_stroke) +GrDraggable::GrDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { this->item = item; - this->point_num = point_num; + this->point_type = point_type; + this->point_i = point_i; this->fill_or_stroke = fill_or_stroke; g_object_ref (G_OBJECT (this->item)); @@ -317,7 +331,8 @@ GrDraggable::~GrDraggable () g_object_unref (G_OBJECT (this->item)); } -NR::Point * +// FIXME: make global function in libnr or somewhere. +static NR::Point * get_snap_vector (NR::Point p, NR::Point o, double snap, double initial) { double r = NR::L2 (p - o); @@ -330,10 +345,28 @@ get_snap_vector (NR::Point p, NR::Point o, double snap, double initial) return new NR::Point (o + r * NR::Point(cos(a_snapped), sin(a_snapped)) - p); } +// FIXME: make global function in libnr or somewhere. +static NR::Point +snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end, double snap) +{ + double length = NR::L2(end - begin); + NR::Point be = (end - begin) / length; + double r = NR::dot(p - begin, be); + + if (r < 0.0) return begin; + if (r > length) return end; + + double snapdist = length * snap; + double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist; + + return (begin + r_snapped * be); +} + static void gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data) { GrDragger *dragger = (GrDragger *) data; + GrDrag *drag = dragger->parent; NR::Point p = *ppointer; @@ -367,7 +400,7 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger GrDraggable *draggable = (GrDraggable *) i->data; // copy draggable to d_new: - GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_num, draggable->fill_or_stroke); + GrDraggable *da_new = new GrDraggable (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke); d_new->addDraggable (da_new); } @@ -389,6 +422,7 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi } } + if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) { // See if we need to snap to any of the levels for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) { @@ -417,13 +451,14 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi NR::Point *dr_snap = NULL; - if (draggable->point_num == POINT_LG_P1 || draggable->point_num == POINT_LG_P2) { + if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) { for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) { GrDragger *d_new = (GrDragger *) di->data; if (d_new == dragger) continue; if (d_new->isA (draggable->item, - draggable->point_num == POINT_LG_P1? POINT_LG_P2 : POINT_LG_P1, + draggable->point_type == POINT_LG_BEGIN? POINT_LG_END : POINT_LG_BEGIN, + draggable->point_i, draggable->fill_or_stroke)) { // found the other end of the linear gradient; if (state & GDK_SHIFT_MASK) { @@ -436,19 +471,20 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi } } } - } else if (draggable->point_num == POINT_RG_R1 || draggable->point_num == POINT_RG_R2 || draggable->point_num == POINT_RG_FOCUS) { + } else if (draggable->point_type == POINT_RG_R1 || draggable->point_type == POINT_RG_R2 || draggable->point_type == POINT_RG_FOCUS) { for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) { GrDragger *d_new = (GrDragger *) di->data; if (d_new == dragger) continue; if (d_new->isA (draggable->item, POINT_RG_CENTER, + draggable->point_i, draggable->fill_or_stroke)) { // found the center of the radial gradient; dr_snap = &(d_new->point); } } - } else if (draggable->point_num == POINT_RG_CENTER) { + } else if (draggable->point_type == POINT_RG_CENTER) { // radial center snaps to hor/vert relative to its original position dr_snap = &(dragger->point_original); } @@ -479,7 +515,7 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi p += move; sp_knot_moveto (knot, &p); } - + g_slist_free(snap_vectors); } @@ -492,7 +528,125 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi } dragger->updateDependencies(false); + + drag->keep_selection = (bool) g_list_find(drag->selected, dragger); } + + +/** +Called when a midpoint knot is dragged. +*/ +static void +gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data) +{ + GrDragger *dragger = (GrDragger *) data; + GrDrag *drag = dragger->parent; + // a midpoint dragger can (logically) only contain one GrDraggable + GrDraggable *draggable = (GrDraggable *) dragger->draggables->data; + + // FIXME: take from prefs + double snap_fraction = 0.1; + + NR::Point p = *ppointer; + NR::Point begin(0,0), end(0,0); + + SPObject *server; + if (draggable->fill_or_stroke) + server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item); + else + server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item); + + + // get begin and end points between which dragging is allowed: + // the draglimits are between knot(lowest_i - 1) and knot(highest_i + 1) + GSList *moving = NULL; + moving = g_slist_append(moving, dragger); + + guint lowest_i = draggable->point_i; + guint highest_i = draggable->point_i; + GrDragger *lowest_dragger = dragger; + GrDragger *highest_dragger = dragger; + bool is_selected = g_list_find(drag->selected, dragger); + if (is_selected) { + GrDragger* d_add; + while ( true ) + { + d_add = drag->getDraggerFor(draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke); + if ( d_add && g_list_find(drag->selected, d_add) ) { + lowest_i = lowest_i - 1; + moving = g_slist_prepend(moving, d_add); + lowest_dragger = d_add; + } else { + break; + } + } + + while ( true ) + { + d_add = drag->getDraggerFor(draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke); + if ( d_add && g_list_find(drag->selected, d_add) ) { + highest_i = highest_i + 1; + moving = g_slist_append(moving, d_add); + highest_dragger = d_add; + } else { + break; + } + } + } + + if ( SP_IS_LINEARGRADIENT(server) ) { + GrDragger *d_temp; + if (lowest_i == 1) { + d_temp = drag->getDraggerFor (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke); + } else { + d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke); + } + if (d_temp) begin = d_temp->point; + + d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, highest_i + 1, draggable->fill_or_stroke); + if (d_temp == NULL) { + d_temp = drag->getDraggerFor (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke); + } + if (d_temp) end = d_temp->point; + } else if ( SP_IS_RADIALGRADIENT(server) ) { + GrDragger *d_temp; + if (lowest_i == 1) { + d_temp = drag->getDraggerFor (draggable->item, POINT_RG_CENTER, 0, draggable->fill_or_stroke); + } else { + d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke); + } + if (d_temp) begin = d_temp->point; + + d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, highest_i + 1, draggable->fill_or_stroke); + if (d_temp == NULL) { + d_temp = drag->getDraggerFor (draggable->item, (draggable->point_type==POINT_RG_MID1) ? POINT_RG_R1 : POINT_RG_R2, 0, draggable->fill_or_stroke); + } + if (d_temp) end = d_temp->point; + } + + NR::Point low_lim = dragger->point - (lowest_dragger->point - begin); + NR::Point high_lim = dragger->point - (highest_dragger->point - end); + + if (state & GDK_CONTROL_MASK) { + p = snap_vector_midpoint (p, low_lim, high_lim, snap_fraction); + } else { + p = snap_vector_midpoint (p, low_lim, high_lim, 0); + } + NR::Point displacement = p - dragger->point; + + for (GSList const* i = moving; i != NULL; i = i->next) { + GrDragger *drg = (GrDragger*) i->data; + SPKnot *drgknot = drg->knot; + drg->point += displacement; + sp_knot_moveto (drgknot, & drg->point); + drg->fireDraggables (false); + drg->updateDependencies(false); + } + + drag->keep_selection = is_selected; +} + + static void gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data) @@ -521,7 +675,10 @@ gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data) } // make this dragger selected - dragger->parent->setSelected (dragger); + if (!dragger->parent->keep_selection) { + dragger->parent->setSelected (dragger); + } + dragger->parent->keep_selection = false; dragger->updateDependencies(true); @@ -531,16 +688,67 @@ gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data) } /** -Called when a dragger knot is clicked; selects the dragger +Called when a dragger knot is clicked; selects the dragger or deletes it depending on the +state of the keyboard keys */ static void gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data) { - GrDragger *dragger = (GrDragger *) data; - - dragger->point_original = dragger->point; - - dragger->parent->setSelected (dragger); + GrDragger *dragger = (GrDragger *) data; + GrDraggable *draggable = (GrDraggable *) dragger->draggables->data; + if (!draggable) return; + + if ( (state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK ) ) { + // delete this knot from vector + SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke); + gradient = sp_gradient_get_vector (gradient, false); + if (gradient->vector.stops.size() > 2) { // 2 is the minimum + SPStop *stop; + switch (draggable->point_type) { // if we delete first or last stop, move the next/previous to the edge + case POINT_LG_BEGIN: + case POINT_RG_CENTER: + stop = sp_first_stop(gradient); + { + SPStop *next = sp_next_stop (stop); + if (next) { + next->offset = 0; + sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0); + } + } + break; + case POINT_LG_END: + case POINT_RG_R1: + case POINT_RG_R2: + stop = sp_last_stop(gradient); + { + SPStop *prev = sp_prev_stop (stop, gradient); + if (prev) { + prev->offset = 1; + sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1); + } + } + break; + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + stop = sp_get_stop_i(gradient, draggable->point_i); + break; + } + + SP_OBJECT_REPR(gradient)->removeChild(SP_OBJECT_REPR(stop)); + sp_document_done (SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT, + _("Delete gradient stop")); + } + } else { + // select the dragger + dragger->point_original = dragger->point; + + if ( state & GDK_SHIFT_MASK ) { + dragger->parent->setSelected (dragger, true, false); + } else { + dragger->parent->setSelected (dragger); + } + } } /** @@ -549,15 +757,15 @@ Called when a dragger knot is doubleclicked; opens gradient editor with the stop static void gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data) { - GrDragger *dragger = (GrDragger *) data; + GrDragger *dragger = (GrDragger *) data; - dragger->point_original = dragger->point; + dragger->point_original = dragger->point; - if (dragger->draggables == NULL) - return; + if (dragger->draggables == NULL) + return; - GrDraggable *draggable = (GrDraggable *) dragger->draggables->data; - sp_item_gradient_edit_stop (draggable->item, draggable->point_num, draggable->fill_or_stroke); + GrDraggable *draggable = (GrDraggable *) dragger->draggables->data; + sp_item_gradient_edit_stop (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke); } /** @@ -575,20 +783,20 @@ GrDragger::fireDraggables (bool write_repr, bool scale_radial, bool merging_focu // change gradient, optionally writing to repr; prevent focus from moving if it's snapped // to the center, unless it's the first update upon merge when we must snap it to the point if (merging_focus || - !(draggable->point_num == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->fill_or_stroke))) - sp_item_gradient_set_coords (draggable->item, draggable->point_num, this->point, draggable->fill_or_stroke, write_repr, scale_radial); + !(draggable->point_type == POINT_RG_FOCUS && this->isA(draggable->item, POINT_RG_CENTER, draggable->point_i, draggable->fill_or_stroke))) + sp_item_gradient_set_coords (draggable->item, draggable->point_type, draggable->point_i, this->point, draggable->fill_or_stroke, write_repr, scale_radial); } } /** -Checks if the dragger has a draggable with this point_num +Checks if the dragger has a draggable with this point_type */ bool -GrDragger::isA (guint point_num) +GrDragger::isA (guint point_type) { for (GSList const* i = this->draggables; i != NULL; i = i->next) { GrDraggable *draggable = (GrDraggable *) i->data; - if (draggable->point_num == point_num) { + if (draggable->point_type == point_type) { return true; } } @@ -596,14 +804,14 @@ GrDragger::isA (guint point_num) } /** -Checks if the dragger has a draggable with this item, point_num, fill_or_stroke +Checks if the dragger has a draggable with this item, point_type, fill_or_stroke */ bool -GrDragger::isA (SPItem *item, guint point_num, bool fill_or_stroke) +GrDragger::isA (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { for (GSList const* i = this->draggables; i != NULL; i = i->next) { GrDraggable *draggable = (GrDraggable *) i->data; - if (draggable->point_num == point_num && draggable->item == item && draggable->fill_or_stroke == fill_or_stroke) { + if ( (draggable->point_type == point_type) && (draggable->point_i == point_i) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) { return true; } } @@ -615,12 +823,18 @@ GrDraggable::mayMerge (GrDraggable *da2) { if ((this->item == da2->item) && (this->fill_or_stroke == da2->fill_or_stroke)) { // we must not merge the points of the same gradient! - if (!((this->point_num == POINT_RG_FOCUS && da2->point_num == POINT_RG_CENTER) || - (this->point_num == POINT_RG_CENTER && da2->point_num == POINT_RG_FOCUS))) { + if (!((this->point_type == POINT_RG_FOCUS && da2->point_type == POINT_RG_CENTER) || + (this->point_type == POINT_RG_CENTER && da2->point_type == POINT_RG_FOCUS))) { // except that we can snap center and focus together return false; } } + // disable merging of midpoints. + if ( (this->point_type == POINT_LG_MID) || (da2->point_type == POINT_LG_MID) + || (this->point_type == POINT_RG_MID1) || (da2->point_type == POINT_RG_MID1) + || (this->point_type == POINT_RG_MID2) || (da2->point_type == POINT_RG_MID2) ) + return false; + return true; } @@ -666,10 +880,24 @@ GrDragger::updateTip () if (g_slist_length (this->draggables) == 1) { GrDraggable *draggable = (GrDraggable *) this->draggables->data; char *item_desc = sp_item_description(draggable->item); - this->knot->tip = g_strdup_printf (_("%s for: %s%s; drag with Ctrl to snap angle, with Ctrl+Alt to preserve angle, with Ctrl+Shift to scale around center"), - _(gr_knot_descr[draggable->point_num]), - item_desc, - draggable->fill_or_stroke == false ? _(" (stroke)") : ""); + switch (draggable->point_type) { + case POINT_LG_MID: + case POINT_RG_MID1: + case POINT_RG_MID2: + this->knot->tip = g_strdup_printf (_("%s %d for: %s%s; drag with Ctrl to snap offset; click with Ctrl+Alt to delete stop"), + _(gr_knot_descr[draggable->point_type]), + draggable->point_i, + item_desc, + draggable->fill_or_stroke == false ? _(" (stroke)") : ""); + break; + + default: + this->knot->tip = g_strdup_printf (_("%s for: %s%s; drag with Ctrl to snap angle, with Ctrl+Alt to preserve angle, with Ctrl+Shift to scale around center"), + _(gr_knot_descr[draggable->point_type]), + item_desc, + draggable->fill_or_stroke == false ? _(" (stroke)") : ""); + break; + } g_free(item_desc); } else if (g_slist_length (draggables) == 2 && isA (POINT_RG_CENTER) && isA (POINT_RG_FOCUS)) { this->knot->tip = g_strdup_printf (_("Radial gradient center and focus; drag with Shift to separate focus")); @@ -691,7 +919,7 @@ GrDragger::updateKnotShape () if (!draggables) return; GrDraggable *last = (GrDraggable *) g_slist_last(draggables)->data; - g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_num], NULL); + g_object_set (G_OBJECT (this->knot->item), "shape", gr_knot_shapes[last->point_type], NULL); } /** @@ -710,24 +938,50 @@ GrDragger::addDraggable (GrDraggable *draggable) Moves this dragger to the point of the given draggable, acting upon all other draggables */ void -GrDragger::moveThisToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr) +GrDragger::moveThisToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr) { - this->point = sp_item_gradient_get_coords (item, point_num, fill_or_stroke); + this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke); this->point_original = this->point; sp_knot_moveto (this->knot, &(this->point)); for (GSList const* i = this->draggables; i != NULL; i = i->next) { GrDraggable *da = (GrDraggable *) i->data; - if (da->item == item && da->point_num == point_num && da->fill_or_stroke == fill_or_stroke) { + if ( (da->item == item) && (da->point_type == point_type) && (da->point_i == point_i) && (da->fill_or_stroke == fill_or_stroke) ) { continue; } - sp_item_gradient_set_coords (da->item, da->point_num, this->point, da->fill_or_stroke, write_repr, false); + sp_item_gradient_set_coords (da->item, da->point_type, da->point_i, this->point, da->fill_or_stroke, write_repr, false); } // FIXME: here we should also call this->updateDependencies(write_repr); to propagate updating, but how to prevent loops? } +/** +Moves all midstop draggables that depend on this one + */ +void +GrDragger::updateMidstopDependencies (GrDraggable *draggable, bool write_repr) { + SPObject *server; + if (draggable->fill_or_stroke) + server = SP_OBJECT_STYLE_FILL_SERVER (draggable->item); + else + server = SP_OBJECT_STYLE_STROKE_SERVER (draggable->item); + guint num = SP_GRADIENT(server)->vector.stops.size(); + if (num <= 2) return; + + if ( SP_IS_LINEARGRADIENT(server) ) { + for ( guint i = 1; i < num - 1; i++ ) { + this->moveOtherToDraggable (draggable->item, POINT_LG_MID, i, draggable->fill_or_stroke, write_repr); + } + } else if ( SP_IS_RADIALGRADIENT(server) ) { + for ( guint i = 1; i < num - 1; i++ ) { + this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, i, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, i, draggable->fill_or_stroke, write_repr); + } + } +} + + /** Moves all draggables that depend on this one */ @@ -736,30 +990,51 @@ GrDragger::updateDependencies (bool write_repr) { for (GSList const* i = this->draggables; i != NULL; i = i->next) { GrDraggable *draggable = (GrDraggable *) i->data; - switch (draggable->point_num) { - case POINT_LG_P1: - // the other point is dependent only when dragging with ctrl+shift - this->moveOtherToDraggable (draggable->item, POINT_LG_P2, draggable->fill_or_stroke, write_repr); + switch (draggable->point_type) { + case POINT_LG_BEGIN: + { + // the end point is dependent only when dragging with ctrl+shift + this->moveOtherToDraggable (draggable->item, POINT_LG_END, 0, draggable->fill_or_stroke, write_repr); + + this->updateMidstopDependencies (draggable, write_repr); + } break; - case POINT_LG_P2: - this->moveOtherToDraggable (draggable->item, POINT_LG_P1, draggable->fill_or_stroke, write_repr); + case POINT_LG_END: + { + // the begin point is dependent only when dragging with ctrl+shift + this->moveOtherToDraggable (draggable->item, POINT_LG_BEGIN, 0, draggable->fill_or_stroke, write_repr); + + this->updateMidstopDependencies (draggable, write_repr); + } + break; + case POINT_LG_MID: + // no other nodes depend on mid points. break; case POINT_RG_R2: - this->moveOtherToDraggable (draggable->item, POINT_RG_R1, draggable->fill_or_stroke, write_repr); - this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr); + this->updateMidstopDependencies (draggable, write_repr); break; case POINT_RG_R1: - this->moveOtherToDraggable (draggable->item, POINT_RG_R2, draggable->fill_or_stroke, write_repr); - this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr); + this->updateMidstopDependencies (draggable, write_repr); break; case POINT_RG_CENTER: - this->moveOtherToDraggable (draggable->item, POINT_RG_R1, draggable->fill_or_stroke, write_repr); - this->moveOtherToDraggable (draggable->item, POINT_RG_R2, draggable->fill_or_stroke, write_repr); - this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_R1, 0, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_R2, 0, draggable->fill_or_stroke, write_repr); + this->moveOtherToDraggable (draggable->item, POINT_RG_FOCUS, 0, draggable->fill_or_stroke, write_repr); + this->updateMidstopDependencies (draggable, write_repr); break; case POINT_RG_FOCUS: // nothing can depend on that break; + case POINT_RG_MID1: + this->moveOtherToDraggable (draggable->item, POINT_RG_MID2, draggable->point_i, draggable->fill_or_stroke, write_repr); + break; + case POINT_RG_MID2: + this->moveOtherToDraggable (draggable->item, POINT_RG_MID1, draggable->point_i, draggable->fill_or_stroke, write_repr); + break; default: break; } @@ -789,7 +1064,16 @@ GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable) sp_knot_show (this->knot); // connect knot's signals - this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this); + if ( (draggable) // it can be NULL if a node in unsnapped (eg. focus point unsnapped from center) + // luckily, midstops never snap to other nodes so are never unsnapped... + && ( (draggable->point_type == POINT_LG_MID) + || (draggable->point_type == POINT_RG_MID1) + || (draggable->point_type == POINT_RG_MID2) ) ) + { + this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_midpoint_handler), this); + } else { + this->handler_id = g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (gr_knot_moved_handler), this); + } g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (gr_knot_clicked_handler), this); g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (gr_knot_doubleclicked_handler), this); g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (gr_knot_grabbed_handler), this); @@ -804,8 +1088,7 @@ GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable) GrDragger::~GrDragger () { // unselect if it was selected - if (this->parent->selected == this) - this->parent->setSelected (NULL); + this->parent->setDeselected(this); // disconnect signals g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (gr_knot_moved_handler), this); @@ -829,13 +1112,13 @@ GrDragger::~GrDragger () Select the dragger which has the given draggable. */ GrDragger * -GrDrag::getDraggerFor (SPItem *item, guint point_num, bool fill_or_stroke) +GrDrag::getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke) { for (GList const* i = this->draggers; i != NULL; i = i->next) { GrDragger *dragger = (GrDragger *) i->data; for (GSList const* j = dragger->draggables; j != NULL; j = j->next) { GrDraggable *da2 = (GrDraggable *) j->data; - if (da2->item == item && da2->point_num == point_num && da2->fill_or_stroke == fill_or_stroke) { + if ( (da2->item == item) && (da2->point_type == point_type) && (da2->point_i == point_i) && (da2->fill_or_stroke == fill_or_stroke)) { return (dragger); } } @@ -845,34 +1128,110 @@ GrDrag::getDraggerFor (SPItem *item, guint point_num, bool fill_or_stroke) void -GrDragger::moveOtherToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr) +GrDragger::moveOtherToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr) { - GrDragger *d = this->parent->getDraggerFor (item, point_num, fill_or_stroke); + GrDragger *d = this->parent->getDraggerFor (item, point_type, point_i, fill_or_stroke); if (d && d != this) { - d->moveThisToDraggable (item, point_num, fill_or_stroke, write_repr); + d->moveThisToDraggable (item, point_type, point_i, fill_or_stroke, write_repr); } } /** -Set selected dragger + Draw this dragger as selected */ void -GrDrag::setSelected (GrDragger *dragger) +GrDragger::select() { - if (this->selected) { - this->selected->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL; - g_object_set (G_OBJECT (this->selected->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL); + this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED; + g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL); +} + +/** + Draw this dragger as normal (deselected) +*/ +void +GrDragger::deselect() +{ + this->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_NORMAL; + g_object_set (G_OBJECT (this->knot->item), "fill_color", GR_KNOT_COLOR_NORMAL, NULL); +} + + + +/** +\brief Deselect all stops/draggers +*/ +void +GrDrag::deselect_all() +{ + while (selected) { + ( (GrDragger*) selected->data)->deselect(); + selected = g_list_remove(selected, selected->data); } - if (dragger) { - dragger->knot->fill [SP_KNOT_STATE_NORMAL] = GR_KNOT_COLOR_SELECTED; - g_object_set (G_OBJECT (dragger->knot->item), "fill_color", GR_KNOT_COLOR_SELECTED, NULL); +} + +/** +\brief Select a dragger +\param dragger The dragger to select +\param add_to_selection If true, add to selection, otherwise deselect others +\param override If true, always select this node, otherwise toggle selected status +*/ +void +GrDrag::setSelected (GrDragger *dragger, bool add_to_selection, bool override) +{ + GrDragger *seldragger = NULL; + + if (add_to_selection) { + if (!dragger) return; + if (override) { + if (!g_list_find(selected, dragger)) { + selected = g_list_prepend(selected, dragger); + } + dragger->select(); + seldragger = dragger; + } else { // toggle + if (g_list_find(selected, dragger)) { + selected = g_list_remove(selected, dragger); + dragger->deselect(); + if (selected) { + seldragger = (GrDragger*) selected->data; // select the dragger that is first in the list + } + } else { + selected = g_list_prepend(selected, dragger); + dragger->select(); + seldragger = dragger; + } + } + } else { + deselect_all(); + if (dragger) { + selected = g_list_prepend(selected, dragger); + dragger->select(); + seldragger = dragger; + } } - this->selected = dragger; + if (seldragger) { + this->desktop->emitToolSubselectionChanged((gpointer) seldragger); + } +} - this->desktop->emitToolSubselectionChanged((gpointer) dragger); +/** +\brief Deselect a dragger +\param dragger The dragger to deselect +*/ +void +GrDrag::setDeselected (GrDragger *dragger) +{ + if (g_list_find(selected, dragger)) { + selected = g_list_remove(selected, dragger); + dragger->deselect(); + } + this->desktop->emitToolSubselectionChanged((gpointer) (selected ? selected->data : NULL )); } + + /** Create a line from p1 to p2 and add it to the lines list */ @@ -895,7 +1254,7 @@ new dragger and add it to draggers list void GrDrag::addDragger (GrDraggable *draggable) { - NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_num, draggable->fill_or_stroke); + NR::Point p = sp_item_gradient_get_coords (draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke); for (GList *i = this->draggers; i != NULL; i = i->next) { GrDragger *dragger = (GrDragger *) i->data; @@ -907,8 +1266,9 @@ GrDrag::addDragger (GrDraggable *draggable) } } - GrDragger *new_dragger = new GrDragger(this, p, draggable); - this->draggers = g_list_prepend (this->draggers, new_dragger); + GrDragger *new_dragger = new GrDragger(this, p, draggable); + // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end. + this->draggers = g_list_append (this->draggers, new_dragger); } /** @@ -917,10 +1277,21 @@ Add draggers for the radial gradient rg on item void GrDrag::addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke) { - addDragger (new GrDraggable (item, POINT_RG_CENTER, fill_or_stroke)); - addDragger (new GrDraggable (item, POINT_RG_FOCUS, fill_or_stroke)); - addDragger (new GrDraggable (item, POINT_RG_R1, fill_or_stroke)); - addDragger (new GrDraggable (item, POINT_RG_R2, fill_or_stroke)); + addDragger (new GrDraggable (item, POINT_RG_CENTER, 0, fill_or_stroke)); + guint num = rg->vector.stops.size(); + if (num > 2) { + for ( guint i = 1; i < num - 1; i++ ) { + addDragger (new GrDraggable (item, POINT_RG_MID1, i, fill_or_stroke)); + } + } + addDragger (new GrDraggable (item, POINT_RG_R1, 0, fill_or_stroke)); + if (num > 2) { + for ( guint i = 1; i < num - 1; i++ ) { + addDragger (new GrDraggable (item, POINT_RG_MID2, i, fill_or_stroke)); + } + } + addDragger (new GrDraggable (item, POINT_RG_R2, 0, fill_or_stroke)); + addDragger (new GrDraggable (item, POINT_RG_FOCUS, 0, fill_or_stroke)); } /** @@ -929,17 +1300,23 @@ Add draggers for the linear gradient lg on item void GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke) { - addDragger (new GrDraggable (item, POINT_LG_P1, fill_or_stroke)); - addDragger (new GrDraggable (item, POINT_LG_P2, fill_or_stroke)); + addDragger (new GrDraggable (item, POINT_LG_BEGIN, 0, fill_or_stroke)); + guint num = lg->vector.stops.size(); + if (num > 2) { + for ( guint i = 1; i < num - 1; i++ ) { + addDragger (new GrDraggable (item, POINT_LG_MID, i, fill_or_stroke)); + } + } + addDragger (new GrDraggable (item, POINT_LG_END, 0, fill_or_stroke)); } /** Artificially grab the knot of the dragger with this draggable; used by the gradient context */ void -GrDrag::grabKnot (SPItem *item, guint point_num, bool fill_or_stroke, gint x, gint y, guint32 etime) +GrDrag::grabKnot (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime) { - GrDragger *dragger = getDraggerFor (item, point_num, fill_or_stroke); + GrDragger *dragger = getDraggerFor (item, point_type, point_i, fill_or_stroke); if (dragger) { sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime); } @@ -952,13 +1329,15 @@ modified, also when a radial dragger needs to update positions of other draggers void GrDrag::updateDraggers () { - // delete old draggers and deselect + while (selected) { + selected = g_list_remove(selected, selected->data); + } + // delete old draggers for (GList const* i = this->draggers; i != NULL; i = i->next) { delete ((GrDragger *) i->data); } g_list_free (this->draggers); this->draggers = NULL; - this->selected = NULL; g_return_if_fail (this->selection != NULL); @@ -984,8 +1363,6 @@ GrDrag::updateDraggers () addDraggersRadial (SP_RADIALGRADIENT (server), item, false); } } - - } } @@ -1014,22 +1391,22 @@ GrDrag::updateLines () if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) { SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item); if (SP_IS_LINEARGRADIENT (server)) { - this->addLine (sp_item_gradient_get_coords (item, POINT_LG_P1, true), sp_item_gradient_get_coords (item, POINT_LG_P2, true), GR_LINE_COLOR_FILL); + this->addLine (sp_item_gradient_get_coords (item, POINT_LG_BEGIN, 0, true), sp_item_gradient_get_coords (item, POINT_LG_END, 0, true), GR_LINE_COLOR_FILL); } else if (SP_IS_RADIALGRADIENT (server)) { - NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, true); - this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, true), GR_LINE_COLOR_FILL); - this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, true), GR_LINE_COLOR_FILL); + NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true); + this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL); + this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL); } } if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) { SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item); if (SP_IS_LINEARGRADIENT (server)) { - this->addLine (sp_item_gradient_get_coords (item, POINT_LG_P1, false), sp_item_gradient_get_coords (item, POINT_LG_P2, false), GR_LINE_COLOR_STROKE); + this->addLine (sp_item_gradient_get_coords (item, POINT_LG_BEGIN, 0, false), sp_item_gradient_get_coords (item, POINT_LG_END, 0, false), GR_LINE_COLOR_STROKE); } else if (SP_IS_RADIALGRADIENT (server)) { - NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, false); - this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, false), GR_LINE_COLOR_STROKE); - this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, false), GR_LINE_COLOR_STROKE); + NR::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false); + this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE); + this->addLine (center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE); } } } @@ -1065,7 +1442,7 @@ GrDrag::selected_reverse_vector () if (selected == NULL) return; - for (GSList const* i = selected->draggables; i != NULL; i = i->next) { + for (GSList const* i = ( (GrDragger*) selected->data )->draggables; i != NULL; i = i->next) { GrDraggable *draggable = (GrDraggable *) i->data; sp_item_gradient_reverse_vector (draggable->item, draggable->fill_or_stroke); @@ -1075,9 +1452,11 @@ GrDrag::selected_reverse_vector () void GrDrag::selected_move (double x, double y) { +/* if (selected == NULL) return; - + + if ( (g_list_length(selected) == 1) ) selected->point += NR::Point (x, y); selected->point_original = selected->point; sp_knot_moveto (selected->knot, &(selected->point)); @@ -1089,6 +1468,7 @@ GrDrag::selected_move (double x, double y) // we did an undoable action sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT, _("Move gradient handle")); +*/ } void @@ -1101,25 +1481,31 @@ GrDrag::selected_move_screen (double x, double y) selected_move (zx, zy); } +/** +Select the knot next to the last selected one and deselect all other selected. +*/ void GrDrag::select_next () { - if (selected == NULL || g_list_find(draggers, selected)->next == NULL) { + if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) { if (draggers) setSelected ((GrDragger *) draggers->data); } else { - setSelected ((GrDragger *) g_list_find(draggers, selected)->next->data); + setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data); } } +/** +Select the knot previous from the last selected one and deselect all other selected. +*/ void GrDrag::select_prev () { - if (selected == NULL || g_list_find(draggers, selected)->prev == NULL) { + if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) { if (draggers) setSelected ((GrDragger *) g_list_last (draggers)->data); } else { - setSelected ((GrDragger *) g_list_find(draggers, selected)->prev->data); + setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data); } } diff --git a/src/gradient-drag.h b/src/gradient-drag.h index 365da9b92..c66aba169 100644 --- a/src/gradient-drag.h +++ b/src/gradient-drag.h @@ -27,21 +27,22 @@ class Point; /** This class represents a single draggable point of a gradient. It remembers the item -which has the gradient, whether it's fill or stroke, and the point number (from the -GrPoint enum). +which has the gradient, whether it's fill or stroke, the point type (from the +GrPointType enum), and the point number (needed if more than 2 stops are present). */ struct GrDraggable { - GrDraggable(SPItem *item, guint point_num, bool fill_or_stroke); + GrDraggable(SPItem *item, guint point_type, guint point_i, bool fill_or_stroke); ~GrDraggable(); SPItem *item; - guint point_num; + guint point_type; + guint point_i; // the stop number of this point ( = 0 for all types except POINT_LG_MID) bool fill_or_stroke; bool mayMerge (GrDraggable *da2); inline int equals (GrDraggable *other) { - return ((item == other->item) && (point_num == other->point_num) && (fill_or_stroke == other->fill_or_stroke)); + return ((item == other->item) && (point_type == other->point_type) && (point_i == other->point_i) && (fill_or_stroke == other->fill_or_stroke)); } }; @@ -74,16 +75,20 @@ struct GrDragger { void updateKnotShape(); void updateTip(); + + void select(); + void deselect(); - void moveThisToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr); - void moveOtherToDraggable (SPItem *item, guint point_num, bool fill_or_stroke, bool write_repr); - void updateDependencies (bool write_repr); + void moveThisToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr); + void moveOtherToDraggable (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, bool write_repr); + void updateMidstopDependencies (GrDraggable *draggable, bool write_repr); + void updateDependencies (bool write_repr); bool mayMerge (GrDragger *other); bool mayMerge (GrDraggable *da2); - bool isA (guint point_num); - bool isA (SPItem *item, guint point_num, bool fill_or_stroke); + bool isA (guint point_type); + bool isA (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke); void fireDraggables (bool write_repr, bool scale_radial = false, bool merging_focus = false); }; @@ -103,12 +108,16 @@ struct GrDrag { void addDraggersRadial (SPRadialGradient *rg, SPItem *item, bool fill_or_stroke); void addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stroke); - GrDragger *selected; - void setSelected (GrDragger *dragger); + GList *selected; // list of GrDragger* + void setSelected (GrDragger *dragger, bool add_to_selection = false, bool override = true); + void setDeselected (GrDragger *dragger); + void deselect_all(); + + bool keep_selection; + + GrDragger *getDraggerFor (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke); - GrDragger *getDraggerFor (SPItem *item, guint point_num, bool fill_or_stroke); - - void grabKnot (SPItem *item, guint point_num, bool fill_or_stroke, gint x, gint y, guint32 etime); + void grabKnot (SPItem *item, guint point_type, guint point_i, bool fill_or_stroke, gint x, gint y, guint32 etime); bool local_change; diff --git a/src/pixmaps/cursor-gradient-add.xpm b/src/pixmaps/cursor-gradient-add.xpm new file mode 100644 index 000000000..1f33bc659 --- /dev/null +++ b/src/pixmaps/cursor-gradient-add.xpm @@ -0,0 +1,38 @@ +/* XPM */ +static char * cursor_gradient_add_xpm[] = { +"32 32 3 1", +" c None", +". c #FFFFFF", +"+ c #000000", +" ... ", +" .+. ", +" .+. ", +"....+.... ", +".+++ +++. ", +"....+.... ", +" .+. ", +" .+. ..... ", +" ... .+++. ", +" .+.+. ", +" .+++. ", +" .+. ", +" .+. ", +" .+. ", +" .+. ", +" .+++. ", +" .+.+. ", +" .+++. ", +" ..... ", +" ... ", +" .+. ", +" .+. ", +" .+. ", +" .+. ", +" ......+...... ", +" .+++++++++++. ", +" ......+...... ", +" .+. ", +" .+. ", +" .+. ", +" .+. ", +" ... "}; diff --git a/src/pixmaps/cursor-gradient-delete.xpm b/src/pixmaps/cursor-gradient-delete.xpm new file mode 100644 index 000000000..9832aa525 --- /dev/null +++ b/src/pixmaps/cursor-gradient-delete.xpm @@ -0,0 +1,38 @@ +/* XPM */ +static char * cursor_gradient_delete_xpm[] = { +"32 32 3 1", +" c None", +". c #FFFFFF", +"+ c #000000", +" ... ", +" .+. ", +" .+. ", +"....+.... ", +".+++ +++. ", +"....+.... ", +" .+. ", +" .+. ..... ", +" ... .+++. ", +" .+.+. ", +" .+++. ", +" .+. ", +" .+. ", +" .+. ", +" .+. ", +" .+++. ", +" .+.+. ", +" .+++. ", +" ..... ", +" ", +" ", +" ", +" ", +" ", +" ", +" ............. ", +" .+++++++++++. ", +" ............. ", +" ", +" ", +" ", +" "}; diff --git a/src/sp-gradient.h b/src/sp-gradient.h index b0ed49c00..999f4f0a8 100644 --- a/src/sp-gradient.h +++ b/src/sp-gradient.h @@ -37,13 +37,16 @@ typedef enum { } SPGradientState; typedef enum { - POINT_LG_P1, - POINT_LG_P2, + POINT_LG_BEGIN, + POINT_LG_END, + POINT_LG_MID, POINT_RG_CENTER, POINT_RG_R1, POINT_RG_R2, - POINT_RG_FOCUS -} GrPoint; + POINT_RG_FOCUS, + POINT_RG_MID1, + POINT_RG_MID2 +} GrPointType; /** * Gradient diff --git a/src/widgets/gradient-toolbar.cpp b/src/widgets/gradient-toolbar.cpp index d0034611f..9dd43fb31 100644 --- a/src/widgets/gradient-toolbar.cpp +++ b/src/widgets/gradient-toolbar.cpp @@ -114,9 +114,12 @@ gr_apply_gradient (Inkscape::Selection *selection, GrDrag *drag, SPGradient *gr) SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR); guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1); + + // GRADIENTFIXME: make this work for multiple selected draggers. + // First try selected dragger if (drag && drag->selected) { - GrDragger *dragger = drag->selected; + GrDragger *dragger = (GrDragger*) drag->selected->data; for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger GrDraggable *draggable = (GrDraggable *) i->data; gr_apply_gradient_to_item (draggable->item, gr, new_type, new_fill, draggable->fill_or_stroke, !draggable->fill_or_stroke); @@ -126,7 +129,7 @@ gr_apply_gradient (Inkscape::Selection *selection, GrDrag *drag, SPGradient *gr) // If no drag or no dragger selected, act on selection for (GSList const* i = selection->itemList(); i != NULL; i = i->next) { - gr_apply_gradient_to_item (SP_ITEM(i->data), gr, new_type, new_fill, true, true); + gr_apply_gradient_to_item (SP_ITEM(i->data), gr, new_type, new_fill, new_fill, !new_fill); } } @@ -263,7 +266,8 @@ void gr_read_selection (Inkscape::Selection *selection, GrDrag *drag, SPGradient **gr_selected, bool *gr_multi, SPGradientSpread *spr_selected, bool *spr_multi) { if (drag && drag->selected) { - GrDragger *dragger = drag->selected; + // GRADIENTFIXME: make this work for more than one selected dragger? + GrDragger *dragger = (GrDragger*) drag->selected->data; for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger GrDraggable *draggable = (GrDraggable *) i->data; SPGradient *gradient = sp_item_gradient_get_vector (draggable->item, draggable->fill_or_stroke);