diff options
Diffstat (limited to 'src/gradient-drag.cpp')
| -rw-r--r-- | src/gradient-drag.cpp | 626 |
1 files changed, 506 insertions, 120 deletions
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 <b>start</b>"), //POINT_LG_P1 + N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN N_("Linear gradient <b>end</b>"), + N_("Linear gradient <b>midstop</b>"), N_("Radial gradient <b>center</b>"), N_("Radial gradient <b>radius</b>"), N_("Radial gradient <b>radius</b>"), - N_("Radial gradient <b>focus</b>") // POINT_RG_FOCUS + N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS + N_("Linear gradient <b>midstop</b>"), + N_("Linear gradient <b>midstop</b>") }; 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 <b>Ctrl</b> to snap angle, with <b>Ctrl+Alt</b> to preserve angle, with <b>Ctrl+Shift</b> 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 <b>Ctrl</b> to snap offset; click with <b>Ctrl+Alt</b> 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 <b>Ctrl</b> to snap angle, with <b>Ctrl+Alt</b> to preserve angle, with <b>Ctrl+Shift</b> 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 <b>center</b> and <b>focus</b>; drag with <b>Shift</b> 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,25 +938,51 @@ 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 */ void @@ -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); } } |
