X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fgradient-drag.cpp;h=fe8297286c2489562352fa06cd128a7c384a910a;hb=0dc33d4ce43e0bb49c63aa53b826ec4a1ff68e28;hp=9d209846c62035e1fa76aa6b1cdb960e3385922e;hpb=3eeea57cf5e414b7e81cd72f3c4751e6de992acb;p=inkscape.git diff --git a/src/gradient-drag.cpp b/src/gradient-drag.cpp index 9d209846c..fe8297286 100644 --- a/src/gradient-drag.cpp +++ b/src/gradient-drag.cpp @@ -1,14 +1,14 @@ -#define __GRADIENT_DRAG_C__ - /* * On-canvas gradient dragging * * Authors: * bulia byak * Johan Engelen + * Jon A. Cruz + * Abhishek Sharma * * Copyright (C) 2007 Johan Engelen - * Copyright (C) 2005 Authors + * Copyright (C) 2005,2010 Authors * * Released under GNU GPL, read the file 'COPYING' for more information */ @@ -18,20 +18,22 @@ #endif #include +#include +#include #include "desktop-handles.h" #include "selection.h" #include "desktop.h" #include "desktop-style.h" +#include "desktop-handles.h" #include "document.h" #include "display/sp-ctrlline.h" - +#include "display/sp-canvas-util.h" #include "xml/repr.h" #include "svg/css-ostringstream.h" - #include "svg/svg.h" - -#include "prefs-utils.h" +#include "libnr/nr-point-fns.h" +#include "preferences.h" #include "sp-item.h" #include "style.h" #include "knot.h" @@ -40,12 +42,14 @@ #include "gradient-chemistry.h" #include "gradient-drag.h" #include "sp-stop.h" - #include "snap.h" #include "sp-namedview.h" +#include "selection-chemistry.h" +using Inkscape::DocumentUndo; #define GR_KNOT_COLOR_NORMAL 0xffffff00 +#define GR_KNOT_COLOR_MOUSEOVER 0xff000000 #define GR_KNOT_COLOR_SELECTED 0x0000ff00 #define GR_LINE_COLOR_FILL 0x0000ff7f @@ -73,26 +77,26 @@ SPKnotShapeType gr_knot_shapes [] = { const gchar *gr_knot_descr [] = { N_("Linear gradient start"), //POINT_LG_BEGIN N_("Linear gradient end"), - N_("Linear gradient midstop"), + N_("Linear gradient mid stop"), N_("Radial gradient center"), N_("Radial gradient radius"), N_("Radial gradient radius"), N_("Radial gradient focus"), // POINT_RG_FOCUS - N_("Linear gradient midstop"), - N_("Linear gradient midstop") + N_("Radial gradient mid stop"), + N_("Radial gradient mid stop") }; static void -gr_drag_sel_changed(Inkscape::Selection *selection, gpointer data) +gr_drag_sel_changed(Inkscape::Selection */*selection*/, gpointer data) { - GrDrag *drag = (GrDrag *) data; - drag->updateDraggers (); - drag->updateLines (); - drag->updateLevels (); + GrDrag *drag = (GrDrag *) data; + drag->updateDraggers (); + drag->updateLines (); + drag->updateLevels (); } static void -gr_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data) +gr_drag_sel_modified (Inkscape::Selection */*selection*/, guint /*flags*/, gpointer data) { GrDrag *drag = (GrDrag *) data; if (drag->local_change) { @@ -128,22 +132,25 @@ gr_drag_style_query (SPStyle *style, int property, gpointer data) int count = 0; - for (GSList const* i = ((GrDragger*)drag->selected->data)->draggables; i != NULL; i = i->next) { // for all draggables of dragger - GrDraggable *draggable = (GrDraggable *) i->data; + for (GList *i = drag->selected; i != NULL; i = i->next) { // for all selected draggers + GrDragger *d = (GrDragger *) i->data; + for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger + GrDraggable *draggable = (GrDraggable *) j->data; - if (ret == QUERY_STYLE_NOTHING) { - ret = QUERY_STYLE_SINGLE; - } else if (ret == QUERY_STYLE_SINGLE) { - ret = QUERY_STYLE_MULTIPLE_AVERAGED; - } + if (ret == QUERY_STYLE_NOTHING) { + ret = QUERY_STYLE_SINGLE; + } else if (ret == QUERY_STYLE_SINGLE) { + ret = QUERY_STYLE_MULTIPLE_AVERAGED; + } - 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); - cf[3] += SP_RGBA32_A_F (c); + 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); + cf[3] += SP_RGBA32_A_F (c); - count ++; + count ++; + } } if (count) { @@ -173,41 +180,91 @@ gr_drag_style_query (SPStyle *style, int property, gpointer data) } } -bool -gr_drag_style_set (const SPCSSAttr *css, gpointer data) +Glib::ustring GrDrag::makeStopSafeColor( gchar const *str, bool &isNull ) { - GrDrag *drag = (GrDrag *) data; + Glib::ustring colorStr; + if ( str ) { + isNull = false; + colorStr = str; + Glib::ustring::size_type pos = colorStr.find("url(#"); + if ( pos != Glib::ustring::npos ) { + Glib::ustring targetName = colorStr.substr(pos + 5, colorStr.length() - 6); + const GSList *gradients = desktop->doc()->getResourceList("gradient"); + for (const GSList *item = gradients; item; item = item->next) { + SPGradient* grad = SP_GRADIENT(item->data); + if ( targetName == grad->getId() ) { + SPGradient *vect = grad->getVector(); + SPStop *firstStop = (vect) ? vect->getFirstStop() : grad->getFirstStop(); + if (firstStop) { + Glib::ustring stopColorStr; + if (firstStop->currentColor) { + stopColorStr = firstStop->getStyleProperty("color", NULL); + } else { + stopColorStr = firstStop->specified_color.toString(); + } + if ( !stopColorStr.empty() ) { + colorStr = stopColorStr; + } + } + break; + } + } + } + } else { + isNull = true; + } - if (!drag->selected) + return colorStr; +} + +bool GrDrag::styleSet( const SPCSSAttr *css ) +{ + if (!selected) { return false; + } - SPCSSAttr *stop = sp_repr_css_attr_new (); + SPCSSAttr *stop = sp_repr_css_attr_new(); // See if the css contains interesting properties, and if so, translate them into the format // acceptable for gradient stops // any of color properties, in order of increasing priority: - if (css->attribute("flood-color")) + if (css->attribute("flood-color")) { sp_repr_css_set_property (stop, "stop-color", css->attribute("flood-color")); + } - if (css->attribute("lighting-color")) + if (css->attribute("lighting-color")) { sp_repr_css_set_property (stop, "stop-color", css->attribute("lighting-color")); + } - if (css->attribute("color")) + if (css->attribute("color")) { sp_repr_css_set_property (stop, "stop-color", css->attribute("color")); + } - if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none")) + if (css->attribute("stroke") && strcmp(css->attribute("stroke"), "none")) { sp_repr_css_set_property (stop, "stop-color", css->attribute("stroke")); + } - if (css->attribute("fill") && strcmp(css->attribute("fill"), "none")) + if (css->attribute("fill") && strcmp(css->attribute("fill"), "none")) { sp_repr_css_set_property (stop, "stop-color", css->attribute("fill")); + } - if (css->attribute("stop-color")) + if (css->attribute("stop-color")) { sp_repr_css_set_property (stop, "stop-color", css->attribute("stop-color")); + } + + // Make sure the style is allowed for gradient stops. + if ( !sp_repr_css_property_is_unset( stop, "stop-color") ) { + bool stopIsNull = false; + Glib::ustring tmp = makeStopSafeColor( sp_repr_css_property( stop, "stop-color", "" ), stopIsNull ); + if ( !stopIsNull && !tmp.empty() ) { + sp_repr_css_set_property( stop, "stop-color", tmp.c_str() ); + } + } if (css->attribute("stop-opacity")) { // direct setting of stop-opacity has priority - sp_repr_css_set_property (stop, "stop-opacity", css->attribute("stop-opacity")); + sp_repr_css_set_property(stop, "stop-opacity", css->attribute("stop-opacity")); } else { // multiply all opacity properties: gdouble accumulated = 1.0; accumulated *= sp_svg_read_percentage(css->attribute("flood-opacity"), 1.0); @@ -217,11 +274,12 @@ gr_drag_style_set (const SPCSSAttr *css, gpointer data) Inkscape::CSSOStringStream os; os << accumulated; - sp_repr_css_set_property (stop, "stop-opacity", os.str().c_str()); + sp_repr_css_set_property(stop, "stop-opacity", os.str().c_str()); - if ((css->attribute("fill") && !strcmp(css->attribute("fill"), "none")) || - (css->attribute("stroke") && !strcmp(css->attribute("stroke"), "none"))) - sp_repr_css_set_property (stop, "stop-opacity", "0"); // if set to none, don't change color, set opacity to 0 + if ((css->attribute("fill") && !css->attribute("stroke") && !strcmp(css->attribute("fill"), "none")) || + (css->attribute("stroke") && !css->attribute("fill") && !strcmp(css->attribute("stroke"), "none"))) { + sp_repr_css_set_property(stop, "stop-opacity", "0"); // if a single fill/stroke property is set to none, don't change color, set opacity to 0 + } } if (!stop->attributeList()) { // nothing for us here, pass it on @@ -229,13 +287,13 @@ gr_drag_style_set (const SPCSSAttr *css, gpointer data) return false; } - for (GList const* sel = drag->selected; sel != NULL; sel = sel->next) { // for all selected draggers - GrDragger* dragger = (GrDragger*) sel->data; + for (GList const* sel = selected; sel != NULL; sel = sel->next) { // for all selected draggers + GrDragger* dragger = reinterpret_cast(sel->data); for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger - GrDraggable *draggable = (GrDraggable *) i->data; + GrDraggable *draggable = reinterpret_cast(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); + local_change = true; + sp_item_gradient_stop_set_style(draggable->item, draggable->point_type, draggable->point_i, draggable->fill_or_stroke, stop); } } @@ -244,51 +302,206 @@ gr_drag_style_set (const SPCSSAttr *css, gpointer data) return true; } -GrDrag::GrDrag(SPDesktop *desktop) { +guint32 GrDrag::getColor() +{ + if (!selected) return 0; - this->desktop = desktop; + float cf[4]; + cf[0] = cf[1] = cf[2] = cf[3] = 0; - this->selection = sp_desktop_selection(desktop); + int count = 0; - this->draggers = NULL; - this->lines = NULL; - this->selected = NULL; + for (GList *i = selected; i != NULL; i = i->next) { // for all selected draggers + GrDragger *d = (GrDragger *) i->data; + for (GSList const* j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger + GrDraggable *draggable = (GrDraggable *) j->data; + + 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); + cf[3] += SP_RGBA32_A_F (c); + + count ++; + } + } + + if (count) { + cf[0] /= count; + cf[1] /= count; + cf[2] /= count; + cf[3] /= count; + } + + return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]); +} + +SPStop * +GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double 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)) { + Geom::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke); + Geom::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke); + + Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0); + double dist_screen = Geom::L2 (mouse_p - nearest); + if ( dist_screen < 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)) { + Geom::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke); + Geom::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke); + Geom::Point nearest = snap_vector_midpoint (mouse_p, begin, end, 0); + double dist_screen = Geom::L2 (mouse_p - nearest); + if ( dist_screen < 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, 0); + dist_screen = Geom::L2 (mouse_p - nearest); + if ( dist_screen < 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_forked_vector_if_necessary (gradient, false); + SPStop* prev_stop = vector->getFirstStop(); + SPStop* next_stop = prev_stop->getNextStop(); + guint i = 1; + while ( (next_stop) && (next_stop->offset < offset) ) { + prev_stop = next_stop; + next_stop = next_stop->getNextStop(); + i++; + } + if (!next_stop) { + // logical error: the endstop should have offset 1 and should always be more than this offset here + return NULL; + } + + + SPStop *newstop = sp_vector_add_stop (vector, prev_stop, next_stop, offset); + gradient->ensureVector(); + updateDraggers(); + + return newstop; + } + + return NULL; +} - this->hor_levels.clear(); - this->vert_levels.clear(); - this->local_change = false; +bool +GrDrag::dropColor(SPItem */*item*/, gchar const *c, Geom::Point p) +{ + // Note: not sure if a null pointer can come in for the style, but handle that just in case + bool stopIsNull = false; + Glib::ustring toUse = makeStopSafeColor( c, stopIsNull ); + + // first, see if we can drop onto one of the existing draggers + for (GList *i = draggers; i != NULL; i = i->next) { // for all draggables of dragger + GrDragger *d = (GrDragger *) i->data; + + if (Geom::L2(p - d->point)*desktop->current_zoom() < 5) { + SPCSSAttr *stop = sp_repr_css_attr_new (); + sp_repr_css_set_property( stop, "stop-color", stopIsNull ? 0 : toUse.c_str() ); + sp_repr_css_set_property( stop, "stop-opacity", "1" ); + for (GSList *j = d->draggables; j != NULL; j = j->next) { // for all draggables of dragger + GrDraggable *draggable = (GrDraggable *) j->data; + 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_attr_unref(stop); + return true; + } + } + + // now see if we're over line and create a new stop + bool over_line = false; + SPCtrlLine *line = NULL; + if (lines) { + for (GSList *l = lines; (l != NULL) && (!over_line); l = l->next) { + line = (SPCtrlLine*) l->data; + Geom::Point nearest = snap_vector_midpoint (p, line->s, line->e, 0); + double dist_screen = Geom::L2 (p - nearest) * desktop->current_zoom(); + if (line->item && dist_screen < 5) { + SPStop *stop = addStopNearPoint (line->item, p, 5/desktop->current_zoom()); + if (stop) { + SPCSSAttr *css = sp_repr_css_attr_new (); + sp_repr_css_set_property( css, "stop-color", stopIsNull ? 0 : toUse.c_str() ); + sp_repr_css_set_property( css, "stop-opacity", "1" ); + sp_repr_css_change (SP_OBJECT_REPR (stop), css, "style"); + return true; + } + } + } + } + + return false; +} - this->sel_changed_connection = this->selection->connectChanged( - sigc::bind ( + +GrDrag::GrDrag(SPDesktop *desktop) : + selected(0), + keep_selection(false), + local_change(false), + desktop(desktop), + hor_levels(), + vert_levels(), + draggers(0), + lines(0), + selection(sp_desktop_selection(desktop)), + sel_changed_connection(), + sel_modified_connection(), + style_set_connection(), + style_query_connection() +{ + sel_changed_connection = selection->connectChanged( + sigc::bind( sigc::ptr_fun(&gr_drag_sel_changed), (gpointer)this ) ); - this->sel_modified_connection = this->selection->connectModified( + sel_modified_connection = selection->connectModified( sigc::bind( sigc::ptr_fun(&gr_drag_sel_modified), (gpointer)this ) ); - this->style_set_connection = this->desktop->connectSetStyle( - sigc::bind( - sigc::ptr_fun(&gr_drag_style_set), - (gpointer)this ) - ); + style_set_connection = desktop->connectSetStyle( sigc::mem_fun(*this, &GrDrag::styleSet) ); - this->style_query_connection = this->desktop->connectQueryStyle( + style_query_connection = desktop->connectQueryStyle( sigc::bind( sigc::ptr_fun(&gr_drag_style_query), (gpointer)this ) ); - this->updateDraggers (); - this->updateLines (); - this->updateLevels (); + updateDraggers(); + updateLines(); + updateLevels(); if (desktop->gr_item) { - this->setSelected (getDraggerFor (desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke)); + setSelected(getDraggerFor(desktop->gr_item, desktop->gr_point_type, desktop->gr_point_i, desktop->gr_fill_or_stroke)); } } @@ -342,47 +555,34 @@ GrDraggable::~GrDraggable () g_object_unref (G_OBJECT (this->item)); } -// 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); - if (r < 1e-3) - return NULL; - double angle = NR::atan2 (p - o); - // snap angle to snaps increments, starting from initial: - double a_snapped = initial + floor((angle - initial)/snap + 0.5) * snap; - // calculate the new position and subtract p to get the vector: - 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) +SPObject *GrDraggable::getServer() { - 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; + if (!item) { + return NULL; + } - double snapdist = length * snap; - double r_snapped = (snap==0) ? r : floor(r/(snapdist + 0.5)) * snapdist; + SPObject *server = NULL; + if (fill_or_stroke) { + server = item->style->getFillPaintServer(); + }else { + server = item->style->getStrokePaintServer(); + } - return (begin + r_snapped * be); + return server; } static void -gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data) +gr_knot_moved_handler(SPKnot *knot, Geom::Point const &ppointer, guint state, gpointer data) { GrDragger *dragger = (GrDragger *) data; GrDrag *drag = dragger->parent; - NR::Point p = *ppointer; + Geom::Point p = ppointer; - // FIXME: take from prefs - double snap_dist = SNAP_DIST / dragger->parent->desktop->current_zoom(); + SPDesktop *desktop = dragger->parent->desktop; + SnapManager &m = desktop->namedview->snap_manager; + double snap_dist = m.snapprefs.getObjectTolerance() / dragger->parent->desktop->current_zoom(); if (state & GDK_SHIFT_MASK) { // with Shift; unsnap if we carry more than one draggable @@ -405,7 +605,7 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi // without Shift or Ctrl; see if we need to snap to another dragger for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) { GrDragger *d_new = (GrDragger *) di->data; - if (dragger->mayMerge(d_new) && NR::L2 (d_new->point - p) < snap_dist) { + if (dragger->mayMerge(d_new) && Geom::L2 (d_new->point - p) < snap_dist) { // Merge draggers: for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { // for all draggables of dragger @@ -426,48 +626,32 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi d_new->updateKnotShape (); d_new->updateTip (); d_new->updateDependencies(true); - sp_document_done (sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT, - _("Merge gradient handles")); + DocumentUndo::done(sp_desktop_document (d_new->parent->desktop), SP_VERB_CONTEXT_GRADIENT, + _("Merge gradient handles")); return; } } } - if (!((state & GDK_SHIFT_MASK) || ((state & GDK_CONTROL_MASK) && (state & GDK_MOD1_MASK)))) { - // Try snapping to the grid or guides - SnapManager const &m = dragger->parent->desktop->namedview->snap_manager; - Inkscape::SnappedPoint s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL); - if (s.getDistance() < 1e6) { + if (!((state & GDK_SHIFT_MASK) || (state & GDK_CONTROL_MASK))) { + m.setup(desktop); + Inkscape::SnappedPoint s = m.freeSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE)); + m.unSetup(); + if (s.getSnapped()) { p = s.getPoint(); - sp_knot_moveto (knot, &p); - } else { - // No snapping so far, let's see if we need to snap to any of the levels - for (guint i = 0; i < dragger->parent->hor_levels.size(); i++) { - if (fabs(p[NR::Y] - dragger->parent->hor_levels[i]) < snap_dist) { - p[NR::Y] = dragger->parent->hor_levels[i]; - sp_knot_moveto (knot, &p); - } - } - for (guint i = 0; i < dragger->parent->vert_levels.size(); i++) { - if (fabs(p[NR::X] - dragger->parent->vert_levels[i]) < snap_dist) { - p[NR::X] = dragger->parent->vert_levels[i]; - sp_knot_moveto (knot, &p); - } - } + sp_knot_moveto (knot, p); } - } - - if (state & GDK_CONTROL_MASK) { - unsigned snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12)); + } else if (state & GDK_CONTROL_MASK) { + SnappedConstraints sc; + Inkscape::SnapCandidatePoint scp = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + unsigned snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12)); /* 0 means no snapping. */ - // This list will store snap vectors from all draggables of dragger - GSList *snap_vectors = NULL; - for (GSList const* i = dragger->draggables; i != NULL; i = i->next) { GrDraggable *draggable = (GrDraggable *) i->data; - NR::Point *dr_snap = NULL; + Geom::Point dr_snap(Geom::infinity(), Geom::infinity()); if (draggable->point_type == POINT_LG_BEGIN || draggable->point_type == POINT_LG_END) { for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) { @@ -476,16 +660,15 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi continue; if (d_new->isA (draggable->item, 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) { // moving linear around center - NR::Point center = NR::Point (0.5*(d_new->point + dragger->point)); - dr_snap = ¢er; + Geom::Point center = Geom::Point (0.5*(d_new->point + dragger->point)); + dr_snap = center; } else { // moving linear around the other end - dr_snap = &d_new->point; + dr_snap = d_new->point; } } } @@ -496,53 +679,56 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi 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); + dr_snap = d_new->point; } } } else if (draggable->point_type == POINT_RG_CENTER) { // radial center snaps to hor/vert relative to its original position - dr_snap = &(dragger->point_original); + dr_snap = dragger->point_original; } - NR::Point *snap_vector = NULL; - if (dr_snap) { + // dr_snap contains the origin of the gradient, whereas p will be the new endpoint which we will try to snap now + Inkscape::SnappedPoint sp; + if (dr_snap.isFinite()) { + m.setup(desktop); if (state & GDK_MOD1_MASK) { // with Alt, snap to the original angle and its perpendiculars - snap_vector = get_snap_vector (p, *dr_snap, M_PI/2, NR::atan2 (dragger->point_original - *dr_snap)); + sp = m.constrainedAngularSnap(scp, dragger->point_original, dr_snap, 2); } else { // with Ctrl, snap to M_PI/snaps - snap_vector = get_snap_vector (p, *dr_snap, M_PI/snaps, 0); + sp = m.constrainedAngularSnap(scp, boost::optional(), dr_snap, snaps); } - } - if (snap_vector) { - snap_vectors = g_slist_prepend (snap_vectors, snap_vector); + m.unSetup(); + sc.points.push_back(sp); } } - // Move by the smallest of snap vectors: - NR::Point move(9999, 9999); - for (GSList const *i = snap_vectors; i != NULL; i = i->next) { - NR::Point *snap_vector = (NR::Point *) i->data; - if (NR::L2(*snap_vector) < NR::L2(move)) - move = *snap_vector; - } - if (move[NR::X] < 9999) { - p += move; - sp_knot_moveto (knot, &p); + m.setup(desktop, false); // turn of the snap indicator temporarily + Inkscape::SnappedPoint bsp = m.findBestSnap(scp, sc, true); + m.unSetup(); + if (!bsp.getSnapped()) { + // If we didn't truly snap to an object or to a grid, then we will still have to look for the + // closest projection onto one of the constraints. findBestSnap() will not do this for us + for (std::list::const_iterator i = sc.points.begin(); i != sc.points.end(); i++) { + if (i == sc.points.begin() || (Geom::L2((*i).getPoint() - p) < Geom::L2(bsp.getPoint() - p))) { + bsp.setPoint((*i).getPoint()); + bsp.setTarget(Inkscape::SNAPTARGET_CONSTRAINED_ANGLE); + } + } } - - g_slist_free(snap_vectors); + //p = sc.points.front().getPoint(); + p = bsp.getPoint(); + sp_knot_moveto (knot, p); } drag->keep_selection = (bool) g_list_find(drag->selected, dragger); bool scale_radial = (state & GDK_CONTROL_MASK) && (state & GDK_SHIFT_MASK); if (drag->keep_selection) { - NR::Point diff = p - dragger->point; - drag->selected_move_nowrite (diff[NR::X], diff[NR::Y], scale_radial); + Geom::Point diff = p - dragger->point; + drag->selected_move_nowrite (diff[Geom::X], diff[Geom::Y], scale_radial); } else { dragger->point = p; dragger->fireDraggables (false, scale_radial); @@ -552,48 +738,30 @@ gr_knot_moved_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpoi } -/** -Called when a midpoint knot is dragged. -*/ static void -gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data) +gr_midpoint_limits(GrDragger *dragger, SPObject *server, Geom::Point *begin, Geom::Point *end, Geom::Point *low_lim, Geom::Point *high_lim, GSList **moving) { - 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); + *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 = dragger->isSelected(); - if (is_selected) { + if (dragger->isSelected()) { 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); + *moving = g_slist_prepend(*moving, d_add); lowest_dragger = d_add; } else { break; @@ -605,7 +773,7 @@ gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint st 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); + *moving = g_slist_append(*moving, d_add); highest_dragger = d_add; } else { break; @@ -621,13 +789,15 @@ gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint st } else { d_temp = drag->getDraggerFor (draggable->item, POINT_LG_MID, lowest_i - 1, draggable->fill_or_stroke); } - if (d_temp) begin = d_temp->point; + 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, num-1, draggable->fill_or_stroke); } - if (d_temp) end = d_temp->point; + if (d_temp) + *end = d_temp->point; } else if ( SP_IS_RADIALGRADIENT(server) ) { guint num = SP_RADIALGRADIENT(server)->vector.stops.size(); GrDragger *d_temp; @@ -636,55 +806,91 @@ gr_knot_moved_midpoint_handler(SPKnot *knot, NR::Point const *ppointer, guint st } else { d_temp = drag->getDraggerFor (draggable->item, draggable->point_type, lowest_i - 1, draggable->fill_or_stroke); } - if (d_temp) begin = d_temp->point; + 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, num-1, draggable->fill_or_stroke); } - if (d_temp) end = d_temp->point; + 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); + *low_lim = dragger->point - (lowest_dragger->point - *begin); + *high_lim = dragger->point - (highest_dragger->point - *end); +} + + + +/** +Called when a midpoint knot is dragged. +*/ +static void +gr_knot_moved_midpoint_handler(SPKnot */*knot*/, Geom::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; + + Geom::Point p = ppointer; + Geom::Point begin(0,0), end(0,0); + Geom::Point low_lim(0,0), high_lim(0,0); + + SPObject *server = draggable->getServer(); + + GSList *moving = NULL; + gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving); 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); + if (!(state & GDK_SHIFT_MASK)) { + Inkscape::Snapper::SnapConstraint cl(low_lim, high_lim - low_lim); + SPDesktop *desktop = dragger->parent->desktop; + SnapManager &m = desktop->namedview->snap_manager; + m.setup(desktop); + m.constrainedSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE, cl); + m.unSetup(); + } } - NR::Point displacement = p - dragger->point; + Geom::Point displacement = p - dragger->point; for (GSList const* i = moving; i != NULL; i = i->next) { GrDragger *drg = (GrDragger*) i->data; SPKnot *drgknot = drg->knot; - NR::Point this_move = displacement; + Geom::Point this_move = displacement; if (state & GDK_MOD1_MASK) { // FIXME: unify all these profiles (here, in nodepath, in tweak) in one place - double alpha = 1.5; - if (NR::L2(drg->point - dragger->point) + NR::L2(drg->point - begin) - 1e-3 > NR::L2(dragger->point - begin)) { // drg is on the end side from dragger - double x = NR::L2(drg->point - dragger->point)/NR::L2(end - dragger->point); + double alpha = 1.0; + if (Geom::L2(drg->point - dragger->point) + Geom::L2(drg->point - begin) - 1e-3 > Geom::L2(dragger->point - begin)) { // drg is on the end side from dragger + double x = Geom::L2(drg->point - dragger->point)/Geom::L2(end - dragger->point); this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move; } else { // drg is on the begin side from dragger - double x = NR::L2(drg->point - dragger->point)/NR::L2(begin - dragger->point); + double x = Geom::L2(drg->point - dragger->point)/Geom::L2(begin - dragger->point); this_move = (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5) * this_move; } } drg->point += this_move; - sp_knot_moveto (drgknot, & drg->point); + sp_knot_moveto (drgknot, drg->point); drg->fireDraggables (false); drg->updateDependencies(false); } g_slist_free(moving); - drag->keep_selection = is_selected; + drag->keep_selection = dragger->isSelected(); } static void -gr_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data) +gr_knot_grabbed_handler (SPKnot */*knot*/, unsigned int /*state*/, gpointer data) { GrDragger *dragger = (GrDragger *) data; @@ -725,8 +931,8 @@ gr_knot_ungrabbed_handler (SPKnot *knot, unsigned int state, gpointer data) dragger->updateDependencies(true); // we did an undoable action - sp_document_done (sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT, - _("Move gradient handle")); + DocumentUndo::done(sp_desktop_document (dragger->parent->desktop), SP_VERB_CONTEXT_GRADIENT, + _("Move gradient handle")); } /** @@ -734,7 +940,7 @@ Called when a dragger knot is clicked; selects the dragger or deletes it dependi state of the keyboard keys */ static void -gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data) +gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data) { GrDragger *dragger = (GrDragger *) data; GrDraggable *draggable = (GrDraggable *) dragger->draggables->data; @@ -742,45 +948,45 @@ gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data) 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 = NULL; - 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")); - } + SPGradient *gradient = sp_item_gradient (draggable->item, draggable->fill_or_stroke); + gradient = gradient->getVector(); + if (gradient->vector.stops.size() > 2) { // 2 is the minimum + SPStop *stop = NULL; + 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 = gradient->getFirstStop(); + { + SPStop *next = stop->getNextStop(); + 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 = stop->getPrevStop(); + 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)); + DocumentUndo::done(SP_OBJECT_DOCUMENT (gradient), SP_VERB_CONTEXT_GRADIENT, + _("Delete gradient stop")); + } } else { // select the dragger dragger->point_original = dragger->point; @@ -797,7 +1003,7 @@ gr_knot_clicked_handler(SPKnot *knot, guint state, gpointer data) Called when a dragger knot is doubleclicked; opens gradient editor with the stop from the first draggable */ static void -gr_knot_doubleclicked_handler (SPKnot *knot, guint state, gpointer data) +gr_knot_doubleclicked_handler (SPKnot */*knot*/, guint /*state*/, gpointer data) { GrDragger *dragger = (GrDragger *) data; @@ -848,7 +1054,7 @@ GrDragger::isA (gint point_type) } /** -Checks if the dragger has a draggable with this item, point_type, fill_or_stroke +Checks if the dragger has a draggable with this item, point_type + point_i (number), fill_or_stroke */ bool GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke) @@ -862,6 +1068,21 @@ GrDragger::isA (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke return false; } +/** +Checks if the dragger has a draggable with this item, point_type, fill_or_stroke + */ +bool +GrDragger::isA (SPItem *item, gint point_type, bool fill_or_stroke) +{ + for (GSList const* i = this->draggables; i != NULL; i = i->next) { + GrDraggable *draggable = (GrDraggable *) i->data; + if ( (draggable->point_type == point_type) && (draggable->item == item) && (draggable->fill_or_stroke == fill_or_stroke) ) { + return true; + } + } + return false; +} + bool GrDraggable::mayMerge (GrDraggable *da2) { @@ -916,14 +1137,14 @@ Updates the statusbar tip of the dragger knot, based on its draggables void GrDragger::updateTip () { - if (this->knot && this->knot->tip) { - g_free (this->knot->tip); - this->knot->tip = NULL; - } + if (this->knot && this->knot->tip) { + g_free (this->knot->tip); + this->knot->tip = NULL; + } if (g_slist_length (this->draggables) == 1) { GrDraggable *draggable = (GrDraggable *) this->draggables->data; - char *item_desc = sp_item_description(draggable->item); + char *item_desc = draggable->item->description(); switch (draggable->point_type) { case POINT_LG_MID: case POINT_RG_MID1: @@ -984,14 +1205,17 @@ Moves this dragger to the point of the given draggable, acting upon all other dr void GrDragger::moveThisToDraggable (SPItem *item, gint point_type, gint point_i, bool fill_or_stroke, bool write_repr) { - this->point = sp_item_gradient_get_coords (item, point_type, point_i, fill_or_stroke); + GrDraggable *dr_first = (GrDraggable *) this->draggables->data; + if (!dr_first) return; + + this->point = sp_item_gradient_get_coords (dr_first->item, dr_first->point_type, dr_first->point_i, dr_first->fill_or_stroke); this->point_original = this->point; - sp_knot_moveto (this->knot, &(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) && + if ( (da->item == item) && (point_type == -1 || da->point_type == point_type) && (point_i == -1 || da->point_i == point_i) && (da->fill_or_stroke == fill_or_stroke) ) { @@ -1008,11 +1232,9 @@ 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); + SPObject *server = draggable->getServer(); + if (!server) + return; guint num = SP_GRADIENT(server)->vector.stops.size(); if (num <= 2) return; @@ -1090,24 +1312,23 @@ GrDragger::updateDependencies (bool write_repr) -GrDragger::GrDragger (GrDrag *parent, NR::Point p, GrDraggable *draggable) +GrDragger::GrDragger (GrDrag *parent, Geom::Point p, GrDraggable *draggable) + : point(p), + point_original(p) { this->draggables = NULL; this->parent = parent; - this->point = p; - this->point_original = p; - // create the knot this->knot = sp_knot_new (parent->desktop, NULL); this->knot->setMode(SP_KNOT_MODE_XOR); - this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_NORMAL); - this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff); + this->knot->setFill(GR_KNOT_COLOR_NORMAL, GR_KNOT_COLOR_MOUSEOVER, GR_KNOT_COLOR_MOUSEOVER); + this->knot->setStroke(0x0000007f, 0x0000007f, 0x0000007f); sp_knot_update_ctrl(this->knot); // move knot to the given point - sp_knot_set_position (this->knot, &p, SP_KNOT_STATE_NORMAL); + sp_knot_set_position (this->knot, p, SP_KNOT_STATE_NORMAL); sp_knot_show (this->knot); // connect knot's signals @@ -1165,7 +1386,7 @@ GrDrag::getDraggerFor (SPItem *item, gint point_type, gint point_i, bool fill_or 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) && + if ( (da2->item == item) && (point_type == -1 || da2->point_type == point_type) && // -1 means this does not matter (point_i == -1 || da2->point_i == point_i) && // -1 means this does not matter (da2->fill_or_stroke == fill_or_stroke)) { @@ -1250,19 +1471,34 @@ GrDrag::selectAll() /** \brief Select all stops/draggers that match the coords */ -void -GrDrag::selectByCoords(std::vector coords) +void +GrDrag::selectByCoords(std::vector coords) { for (GList *l = this->draggers; l != NULL; l = l->next) { GrDragger *d = ((GrDragger *) l->data); for (guint k = 0; k < coords.size(); k++) { - if (NR::L2 (d->point - coords[k]) < 1e-4) { + if (Geom::L2 (d->point - coords[k]) < 1e-4) { setSelected (d, true, true); } } } } + +/** +\brief Select all stops/draggers that fall within the rect +*/ +void +GrDrag::selectRect(Geom::Rect const &r) +{ + for (GList *l = this->draggers; l != NULL; l = l->next) { + GrDragger *d = ((GrDragger *) l->data); + if (r.contains(d->point)) { + setSelected (d, true, true); + } + } +} + /** \brief Select a dragger \param dragger The dragger to select @@ -1328,10 +1564,12 @@ GrDrag::setDeselected (GrDragger *dragger) Create a line from p1 to p2 and add it to the lines list */ void -GrDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba) +GrDrag::addLine (SPItem *item, Geom::Point p1, Geom::Point p2, guint32 rgba) { SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop), SP_TYPE_CTRLLINE, NULL); + sp_canvas_item_move_to_z(line, 0); + SP_CTRLLINE(line)->item = item; sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2); if (rgba != GR_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba); @@ -1346,11 +1584,11 @@ 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_type, draggable->point_i, draggable->fill_or_stroke); + Geom::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; - if (dragger->mayMerge (draggable) && NR::L2 (dragger->point - p) < MERGE_DIST) { + if (dragger->mayMerge (draggable) && Geom::L2 (dragger->point - p) < MERGE_DIST) { // distance is small, merge this draggable into dragger, no need to create new dragger dragger->addDraggable (draggable); dragger->updateKnotShape(); @@ -1402,6 +1640,17 @@ GrDrag::addDraggersLinear (SPLinearGradient *lg, SPItem *item, bool fill_or_stro addDragger (new GrDraggable (item, POINT_LG_END, num-1, fill_or_stroke)); } +/** +Artificially grab the knot of this dragger; used by the gradient context +*/ +void +GrDrag::grabKnot (GrDragger *dragger, gint x, gint y, guint32 etime) +{ + if (dragger) { + sp_knot_start_dragging (dragger->knot, dragger->point, x, y, etime); + } +} + /** Artificially grab the knot of the dragger with this draggable; used by the gradient context */ @@ -1418,46 +1667,64 @@ GrDrag::grabKnot (SPItem *item, gint point_type, gint point_i, bool fill_or_stro Regenerates the draggers list from the current selection; is called when selection is changed or modified, also when a radial dragger needs to update positions of other draggers in the gradient */ -void -GrDrag::updateDraggers () +void GrDrag::updateDraggers () { 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); + delete static_cast(i->data); } - g_list_free (this->draggers); + g_list_free(this->draggers); this->draggers = NULL; - g_return_if_fail (this->selection != NULL); + g_return_if_fail(this->selection != NULL); for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) { - SPItem *item = SP_ITEM(i->data); - SPStyle *style = SP_OBJECT_STYLE (item); + SPStyle *style = item->style; if (style && (style->fill.isPaintserver())) { - SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item); - if (SP_IS_LINEARGRADIENT (server)) { - addDraggersLinear (SP_LINEARGRADIENT (server), item, true); - } else if (SP_IS_RADIALGRADIENT (server)) { - addDraggersRadial (SP_RADIALGRADIENT (server), item, true); + SPPaintServer *server = style->getFillPaintServer(); + if ( server && server->isSolid() ) { + // Suppress "gradientness" of solid paint + } else if ( SP_IS_LINEARGRADIENT(server) ) { + addDraggersLinear( SP_LINEARGRADIENT(server), item, true ); + } else if ( SP_IS_RADIALGRADIENT(server) ) { + addDraggersRadial( SP_RADIALGRADIENT(server), item, true ); } } if (style && (style->stroke.isPaintserver())) { - SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item); - if (SP_IS_LINEARGRADIENT (server)) { - addDraggersLinear (SP_LINEARGRADIENT (server), item, false); - } else if (SP_IS_RADIALGRADIENT (server)) { - addDraggersRadial (SP_RADIALGRADIENT (server), item, false); + SPPaintServer *server = style->getStrokePaintServer(); + if ( server && server->isSolid() ) { + // Suppress "gradientness" of solid paint + } else if ( SP_IS_LINEARGRADIENT(server) ) { + addDraggersLinear( SP_LINEARGRADIENT(server), item, false ); + } else if ( SP_IS_RADIALGRADIENT(server) ) { + addDraggersRadial( SP_RADIALGRADIENT(server), item, false ); } } } } + +/** + * \brief Returns true if at least one of the draggers' knots has the mouse hovering above it + */ + +bool +GrDrag::mouseOver() +{ + for (GList const* i = this->draggers; i != NULL; i = i->next) { + GrDragger *d = (GrDragger *) i->data; + if (d->knot && (d->knot->flags & SP_KNOT_MOUSEOVER)) { + return true; + } + } + return false; +} /** Regenerates the lines list from the current selection; is called on each move of a dragger, so that lines are always in sync with the actual gradient @@ -1481,24 +1748,28 @@ GrDrag::updateLines () SPStyle *style = SP_OBJECT_STYLE (item); if (style && (style->fill.isPaintserver())) { - SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item); - if (SP_IS_LINEARGRADIENT (server)) { - 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, 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); + SPPaintServer *server = item->style->getFillPaintServer(); + if ( server && server->isSolid() ) { + // Suppress "gradientness" of solid paint + } else if ( SP_IS_LINEARGRADIENT(server) ) { + this->addLine (item, 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) ) { + Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, true); + this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, true), GR_LINE_COLOR_FILL); + this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, true), GR_LINE_COLOR_FILL); } } if (style && (style->stroke.isPaintserver())) { - SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item); - if (SP_IS_LINEARGRADIENT (server)) { - 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, 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); + SPPaintServer *server = item->style->getStrokePaintServer(); + if ( server && server->isSolid() ) { + // Suppress "gradientness" of solid paint + } else if ( SP_IS_LINEARGRADIENT(server) ) { + this->addLine (item, 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) ) { + Geom::Point center = sp_item_gradient_get_coords (item, POINT_RG_CENTER, 0, false); + this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R1, 0, false), GR_LINE_COLOR_STROKE); + this->addLine (item, center, sp_item_gradient_get_coords (item, POINT_RG_R2, 0, false), GR_LINE_COLOR_STROKE); } } } @@ -1517,15 +1788,15 @@ GrDrag::updateLevels () for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) { SPItem *item = SP_ITEM(i->data); - NR::Maybe rect = sp_item_bbox_desktop (item); + Geom::OptRect rect = item->getBboxDesktop (); if (rect) { // Remember the edges of the bbox and the center axis - hor_levels.push_back(rect->min()[NR::Y]); - hor_levels.push_back(rect->max()[NR::Y]); - hor_levels.push_back(0.5 * (rect->min()[NR::Y] + rect->max()[NR::Y])); - vert_levels.push_back(rect->min()[NR::X]); - vert_levels.push_back(rect->max()[NR::X]); - vert_levels.push_back(0.5 * (rect->min()[NR::X] + rect->max()[NR::X])); + hor_levels.push_back(rect->min()[Geom::Y]); + hor_levels.push_back(rect->max()[Geom::Y]); + hor_levels.push_back(0.5 * (rect->min()[Geom::Y] + rect->max()[Geom::Y])); + vert_levels.push_back(rect->min()[Geom::X]); + vert_levels.push_back(rect->max()[Geom::X]); + vert_levels.push_back(0.5 * (rect->min()[Geom::X] + rect->max()[Geom::X])); } } } @@ -1555,18 +1826,18 @@ GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial) if (selected == NULL) return; - bool did = false; + bool did = false; for (GList *i = selected; i != NULL; i = i->next) { GrDragger *d = (GrDragger *) i->data; if (!d->isA(POINT_LG_MID) && !d->isA(POINT_RG_MID1) && !d->isA(POINT_RG_MID2)) { - // if this is a an endpoint, + // if this is an endpoint, // Moving an rg center moves its focus and radii as well. // therefore, if this is a focus or radius and if selection // contains the center as well, do not move this one - if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) || + if (d->isA(POINT_RG_R1) || d->isA(POINT_RG_R2) || (d->isA(POINT_RG_FOCUS) && !d->isA(POINT_RG_CENTER))) { bool skip_radius_with_center = false; for (GList *di = selected; di != NULL; di = di->next) { @@ -1584,9 +1855,9 @@ GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial) } did = true; - d->point += NR::Point (x, y); + d->point += Geom::Point (x, y); d->point_original = d->point; - sp_knot_moveto (d->knot, &(d->point)); + sp_knot_moveto (d->knot, d->point); d->fireDraggables (write_repr, scale_radial); @@ -1594,19 +1865,47 @@ GrDrag::selected_move (double x, double y, bool write_repr, bool scale_radial) } } + if (write_repr && did) { + // we did an undoable action + DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmoveh", SP_VERB_CONTEXT_GRADIENT, + _("Move gradient handle(s)")); + return; + } + if (!did) { // none of the end draggers are selected, so let's try to move the mids - for (GList *i = selected; i != NULL; i = i->next) { - GrDragger *d = (GrDragger *) i->data; - if (d->isA(POINT_LG_MID) || !d->isA(POINT_RG_MID1) || !d->isA(POINT_RG_MID2)) { - } + GrDragger *dragger = (GrDragger *) selected->data; + // a midpoint dragger can (logically) only contain one GrDraggable + GrDraggable *draggable = (GrDraggable *) dragger->draggables->data; + + Geom::Point begin(0,0), end(0,0); + Geom::Point low_lim(0,0), high_lim(0,0); + + SPObject *server = draggable->getServer(); + GSList *moving = NULL; + gr_midpoint_limits(dragger, server, &begin, &end, &low_lim, &high_lim, &moving); + + Geom::Point p(x, y); + p = snap_vector_midpoint (dragger->point + p, low_lim, high_lim, 0); + Geom::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 (true); + drg->updateDependencies(true); + did = true; } - } - if (write_repr && did) { - // we did an undoable action - sp_document_maybe_done (sp_desktop_document (desktop), "grmove", SP_VERB_CONTEXT_GRADIENT, - _("Move gradient handle")); + g_slist_free(moving); + + if (write_repr && did) { + // we did an undoable action + DocumentUndo::maybeDone(sp_desktop_document (desktop), "grmovem", SP_VERB_CONTEXT_GRADIENT, + _("Move gradient mid stop(s)")); + } } } @@ -1623,29 +1922,37 @@ GrDrag::selected_move_screen (double x, double y) /** Select the knot next to the last selected one and deselect all other selected. */ -void +GrDragger * GrDrag::select_next () { + GrDragger *d = NULL; if (selected == NULL || g_list_find(draggers, selected->data)->next == NULL) { if (draggers) - setSelected ((GrDragger *) draggers->data); + d = (GrDragger *) draggers->data; } else { - setSelected ((GrDragger *) g_list_find(draggers, selected->data)->next->data); + d = (GrDragger *) g_list_find(draggers, selected->data)->next->data; } + if (d) + setSelected (d); + return d; } /** Select the knot previous from the last selected one and deselect all other selected. */ -void +GrDragger * GrDrag::select_prev () { + GrDragger *d = NULL; if (selected == NULL || g_list_find(draggers, selected->data)->prev == NULL) { if (draggers) - setSelected ((GrDragger *) g_list_last (draggers)->data); + d = (GrDragger *) g_list_last (draggers)->data; } else { - setSelected ((GrDragger *) g_list_find(draggers, selected->data)->prev->data); + d = (GrDragger *) g_list_find(draggers, selected->data)->prev->data; } + if (d) + setSelected (d); + return d; } @@ -1699,7 +2006,7 @@ GrDrag::deleteSelected (bool just_one) { SPStop *stop = NULL; if ( (draggable->point_type == POINT_LG_BEGIN) || (draggable->point_type == POINT_RG_CENTER) ) { - stop = sp_first_stop(vector); + stop = vector->getFirstStop(); } else { stop = sp_last_stop(vector); } @@ -1744,11 +2051,11 @@ GrDrag::deleteSelected (bool just_one) // cannot use vector->vector.stops.size() because the vector might be invalidated by deletion of a midstop // manually count the children, don't know if there already exists a function for this... int len = 0; - for ( SPObject *child = sp_object_first_child(stopinfo->vector) ; - child != NULL ; - child = SP_OBJECT_NEXT(child) ) + for ( SPObject *child = (stopinfo->vector)->firstChild() ; child ; child = child->getNext() ) { - if ( SP_IS_STOP(child) ) len ++; + if ( SP_IS_STOP(child) ) { + len ++; + } } if (len > 2) { @@ -1758,13 +2065,13 @@ GrDrag::deleteSelected (bool just_one) SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop)); SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient); - NR::Point oldbegin = NR::Point (lg->x1.computed, lg->y1.computed); - NR::Point end = NR::Point (lg->x2.computed, lg->y2.computed); - SPStop *stop = sp_first_stop(stopinfo->vector); + Geom::Point oldbegin = Geom::Point (lg->x1.computed, lg->y1.computed); + Geom::Point end = Geom::Point (lg->x2.computed, lg->y2.computed); + SPStop *stop = stopinfo->vector->getFirstStop(); gdouble offset = stop->offset; - NR::Point newbegin = oldbegin + offset * (end - oldbegin); - lg->x1.computed = newbegin[NR::X]; - lg->y1.computed = newbegin[NR::Y]; + Geom::Point newbegin = oldbegin + offset * (end - oldbegin); + lg->x1.computed = newbegin[Geom::X]; + lg->y1.computed = newbegin[Geom::Y]; Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient); sp_repr_set_svg_double(repr, "x1", lg->x1.computed); @@ -1774,11 +2081,11 @@ GrDrag::deleteSelected (bool just_one) // iterate through midstops to set new offset values such that they won't move on canvas. SPStop *laststop = sp_last_stop(stopinfo->vector); - stop = sp_next_stop(stop); + stop = stop->getNextStop(); while ( stop != laststop ) { stop->offset = (stop->offset - offset)/(1 - offset); sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset); - stop = sp_next_stop(stop); + stop = stop->getNextStop(); } } break; @@ -1787,13 +2094,13 @@ GrDrag::deleteSelected (bool just_one) SP_OBJECT_REPR(stopinfo->vector)->removeChild(SP_OBJECT_REPR(stopinfo->spstop)); SPLinearGradient *lg = SP_LINEARGRADIENT(stopinfo->gradient); - NR::Point begin = NR::Point (lg->x1.computed, lg->y1.computed); - NR::Point oldend = NR::Point (lg->x2.computed, lg->y2.computed); + Geom::Point begin = Geom::Point (lg->x1.computed, lg->y1.computed); + Geom::Point oldend = Geom::Point (lg->x2.computed, lg->y2.computed); SPStop *laststop = sp_last_stop(stopinfo->vector); gdouble offset = laststop->offset; - NR::Point newend = begin + offset * (oldend - begin); - lg->x2.computed = newend[NR::X]; - lg->y2.computed = newend[NR::Y]; + Geom::Point newend = begin + offset * (oldend - begin); + lg->x2.computed = newend[Geom::X]; + lg->y2.computed = newend[Geom::Y]; Inkscape::XML::Node *repr = SP_OBJECT_REPR(stopinfo->gradient); sp_repr_set_svg_double(repr, "x2", lg->x2.computed); @@ -1802,18 +2109,18 @@ GrDrag::deleteSelected (bool just_one) sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1); // iterate through midstops to set new offset values such that they won't move on canvas. - SPStop *stop = sp_first_stop(stopinfo->vector); - stop = sp_next_stop(stop); + SPStop *stop = stopinfo->vector->getFirstStop(); + stop = stop->getNextStop(); while ( stop != laststop ) { stop->offset = stop->offset / offset; sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset); - stop = sp_next_stop(stop); + stop = stop->getNextStop(); } } break; case POINT_RG_CENTER: { - SPStop *newfirst = sp_next_stop (stopinfo->spstop); + SPStop *newfirst = stopinfo->spstop->getNextStop(); if (newfirst) { newfirst->offset = 0; sp_repr_set_css_double (SP_OBJECT_REPR (newfirst), "offset", 0); @@ -1838,12 +2145,12 @@ GrDrag::deleteSelected (bool just_one) sp_repr_set_css_double (SP_OBJECT_REPR (laststop), "offset", 1); // iterate through midstops to set new offset values such that they won't move on canvas. - SPStop *stop = sp_first_stop(stopinfo->vector); - stop = sp_next_stop(stop); + SPStop *stop = stopinfo->vector->getFirstStop(); + stop = stop->getNextStop(); while ( stop != laststop ) { stop->offset = stop->offset / offset; sp_repr_set_css_double (SP_OBJECT_REPR (stop), "offset", stop->offset); - stop = sp_next_stop(stop); + stop = stop->getNextStop(); } break; } @@ -1875,7 +2182,7 @@ GrDrag::deleteSelected (bool just_one) } sp_repr_css_attr_unref (stopcss); } - + sp_repr_css_change (SP_OBJECT_REPR (stopinfo->draggable->item), css, "style"); sp_repr_css_attr_unref (css); } @@ -1885,7 +2192,7 @@ GrDrag::deleteSelected (bool just_one) } if (document) { - sp_document_done ( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") ); + DocumentUndo::done( document, SP_VERB_CONTEXT_GRADIENT, _("Delete gradient stop(s)") ); } }