From: Jon A. Cruz Date: Mon, 8 Mar 2010 01:31:49 +0000 (-0800) Subject: Cleanup of gradients and stops. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=9b8c0838b54555751f0647babbaadaa595741069;p=inkscape.git Cleanup of gradients and stops. --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 46c2586d1..a956f6ad8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -61,6 +61,7 @@ sp-shape.cpp sp-skeleton.cpp sp-spiral.cpp sp-star.cpp +sp-stop.cpp sp-string.cpp sp-style-elem.cpp sp-switch.cpp diff --git a/src/Makefile_insert b/src/Makefile_insert index 574dfe084..1e5b1fea0 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -206,8 +206,7 @@ ink_common_sources += \ sp-shape.cpp sp-shape.h \ sp-spiral.cpp sp-spiral.h \ sp-star.cpp sp-star.h \ - sp-stop-fns.h \ - sp-stop.h \ + sp-stop.cpp sp-stop.h \ sp-string.cpp sp-string.h \ sp-style-elem.cpp sp-style-elem.h \ sp-switch.cpp sp-switch.h \ diff --git a/src/extension/internal/odf.cpp b/src/extension/internal/odf.cpp index 3537be450..76b901023 100644 --- a/src/extension/internal/odf.cpp +++ b/src/extension/internal/odf.cpp @@ -1661,7 +1661,7 @@ bool OdfOutput::processGradient(Writer &outs, SPItem *item, SPGradient *grvec = sp_gradient_get_vector(gradient, FALSE); for (SPStop *stop = grvec->getFirstStop() ; - stop ; stop = sp_next_stop(stop)) + stop ; stop = stop->getNextStop()) { unsigned long rgba = sp_stop_get_rgba32(stop); unsigned long rgb = (rgba >> 8) & 0xffffff; diff --git a/src/gradient-chemistry.cpp b/src/gradient-chemistry.cpp index 04eaeb08d..7382aa49b 100644 --- a/src/gradient-chemistry.cpp +++ b/src/gradient-chemistry.cpp @@ -482,39 +482,10 @@ sp_item_gradient (SPItem *item, bool fill_or_stroke) return gradient; } -SPStop* -sp_prev_stop(SPStop *stop, SPGradient *gradient) -{ - if (sp_object_first_child(SP_OBJECT(gradient)) == SP_OBJECT(stop)) { - return NULL; - } - SPObject *found = NULL; - for ( SPObject *ochild = sp_object_first_child(SP_OBJECT(gradient)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) { - if (SP_IS_STOP (ochild)) { - found = ochild; - } - if (SP_OBJECT_NEXT(ochild) == SP_OBJECT(stop) || SP_OBJECT(ochild) == SP_OBJECT(stop)) { - break; - } - } - return SP_STOP(found); -} - -SPStop* -sp_next_stop(SPStop *stop) +SPStop* sp_last_stop(SPGradient *gradient) { - for (SPObject *ochild = SP_OBJECT_NEXT(stop); ochild != NULL; ochild = SP_OBJECT_NEXT(ochild)) { - if (SP_IS_STOP (ochild)) - return SP_STOP(ochild); - } - return NULL; -} - -SPStop* -sp_last_stop(SPGradient *gradient) -{ - for (SPStop *stop = gradient->getFirstStop(); stop != NULL; stop = sp_next_stop (stop)) { - if (sp_next_stop (stop) == NULL) + for (SPStop *stop = gradient->getFirstStop(); stop != NULL; stop = stop->getNextStop()) { + if (stop->getNextStop() == NULL) return stop; } return NULL; @@ -531,9 +502,11 @@ sp_get_stop_i(SPGradient *gradient, guint stop_i) if (stop->offset != 0) stop_i --; - for (guint i=0; i < stop_i; i++) { - if (!stop) return NULL; - stop = sp_next_stop (stop); + for (guint i = 0; i < stop_i; i++) { + if (!stop) { + return NULL; + } + stop = stop->getNextStop(); } return stop; diff --git a/src/gradient-chemistry.h b/src/gradient-chemistry.h index c6089a658..3f72fa394 100644 --- a/src/gradient-chemistry.h +++ b/src/gradient-chemistry.h @@ -57,8 +57,6 @@ SPGradient *sp_gradient_get_forked_vector_if_necessary(SPGradient *gradient, boo SPStop* sp_last_stop(SPGradient *gradient); -SPStop* sp_prev_stop(SPStop *stop, SPGradient *gradient); -SPStop* sp_next_stop(SPStop *stop); SPStop* sp_get_stop_i(SPGradient *gradient, guint i); guint sp_number_of_stops(SPGradient const *gradient); guint sp_number_of_stops_before_stop(SPGradient const *gradient, SPStop *target); diff --git a/src/gradient-context.cpp b/src/gradient-context.cpp index e78fa44a4..ed20f9b61 100644 --- a/src/gradient-context.cpp +++ b/src/gradient-context.cpp @@ -291,7 +291,7 @@ sp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSLi // from draggables to stops SPStop *this_stop = sp_get_stop_i (vector, d->point_i); - SPStop *next_stop = sp_next_stop (this_stop); + SPStop *next_stop = this_stop->getNextStop(); SPStop *last_stop = sp_last_stop (vector); gint fs = d->fill_or_stroke; @@ -362,7 +362,7 @@ sp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc) SPGradient *gradient = sp_item_gradient (d->item, d->fill_or_stroke); SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false); SPStop *this_stop = sp_get_stop_i (vector, d->point_i); - SPStop *next_stop = sp_next_stop (this_stop); + SPStop *next_stop = this_stop->getNextStop(); if (this_stop && next_stop) { these_stops = g_slist_prepend (these_stops, this_stop); next_stops = g_slist_prepend (next_stops, next_stop); diff --git a/src/gradient-drag.cpp b/src/gradient-drag.cpp index f96775360..13b422119 100644 --- a/src/gradient-drag.cpp +++ b/src/gradient-drag.cpp @@ -334,11 +334,11 @@ GrDrag::addStopNearPoint (SPItem *item, Geom::Point mouse_p, double tolerance) if (addknot) { SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false); SPStop* prev_stop = vector->getFirstStop(); - SPStop* next_stop = sp_next_stop(prev_stop); + SPStop* next_stop = prev_stop->getNextStop(); guint i = 1; while ( (next_stop) && (next_stop->offset < offset) ) { prev_stop = next_stop; - next_stop = sp_next_stop(next_stop); + next_stop = next_stop->getNextStop(); i++; } if (!next_stop) { @@ -913,7 +913,7 @@ gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data) case POINT_RG_CENTER: stop = gradient->getFirstStop(); { - SPStop *next = sp_next_stop (stop); + SPStop *next = stop->getNextStop(); if (next) { next->offset = 0; sp_repr_set_css_double (SP_OBJECT_REPR (next), "offset", 0); @@ -925,7 +925,7 @@ gr_knot_clicked_handler(SPKnot */*knot*/, guint state, gpointer data) case POINT_RG_R2: stop = sp_last_stop(gradient); { - SPStop *prev = sp_prev_stop (stop, gradient); + SPStop *prev = stop->getPrevStop(); if (prev) { prev->offset = 1; sp_repr_set_css_double (SP_OBJECT_REPR (prev), "offset", 1); @@ -2031,11 +2031,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; @@ -2060,17 +2060,17 @@ GrDrag::deleteSelected (bool just_one) // iterate through midstops to set new offset values such that they won't move on canvas. SPStop *stop = stopinfo->vector->getFirstStop(); - stop = sp_next_stop(stop); + 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); @@ -2096,11 +2096,11 @@ GrDrag::deleteSelected (bool just_one) // iterate through midstops to set new offset values such that they won't move on canvas. SPStop *stop = stopinfo->vector->getFirstStop(); - stop = sp_next_stop(stop); + 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; } diff --git a/src/sp-gradient.cpp b/src/sp-gradient.cpp index fb16e9e5a..4e8e8e373 100644 --- a/src/sp-gradient.cpp +++ b/src/sp-gradient.cpp @@ -653,7 +653,7 @@ int SPGradient::getStopCount() const { int count = 0; - for (SPStop *stop = const_cast(this)->getFirstStop(); stop && sp_next_stop(stop); stop = sp_next_stop(stop)) { + for (SPStop *stop = const_cast(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) { count++; } diff --git a/src/sp-object-repr.cpp b/src/sp-object-repr.cpp index 4c3d5196e..62143e3ab 100644 --- a/src/sp-object-repr.cpp +++ b/src/sp-object-repr.cpp @@ -26,7 +26,7 @@ #include "persp3d.h" #include "sp-ellipse.h" #include "sp-star.h" -#include "sp-stop-fns.h" +#include "sp-stop.h" #include "sp-spiral.h" #include "sp-offset.h" #include "sp-line.h" diff --git a/src/sp-object.cpp b/src/sp-object.cpp index f77f228cb..5e0f72758 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -946,6 +946,17 @@ void SPObject::releaseReferences() { this->repr = NULL; } + +SPObject *SPObject::getNext() +{ + return next; +} + +SPObject *SPObject::getPrev() +{ + return sp_object_prev(this); +} + /** * Callback for child_added node event. */ diff --git a/src/sp-object.h b/src/sp-object.h index 22a15a0a4..f7ca0f3a3 100644 --- a/src/sp-object.h +++ b/src/sp-object.h @@ -221,6 +221,9 @@ public: /* A non-const version can be similarly constructed if you want one. * (Don't just cast away the constness, which would be ill-formed.) */ + SPObject *getNext(); + SPObject *getPrev(); + bool hasChildren() const { return ( children != NULL ); } SPObject *firstChild() { return children; } diff --git a/src/sp-stop-fns.h b/src/sp-stop-fns.h deleted file mode 100644 index 9903359e9..000000000 --- a/src/sp-stop-fns.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef SEEN_SP_STOP_FNS_H -#define SEEN_SP_STOP_FNS_H - -#include -struct SPStop; -struct SPStopClass; - -#define SP_TYPE_STOP (sp_stop_get_type()) -#define SP_STOP(o) (G_TYPE_CHECK_INSTANCE_CAST((o), SP_TYPE_STOP, SPStop)) -#define SP_STOP_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), SP_TYPE_STOP, SPStopClass)) -#define SP_IS_STOP(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), SP_TYPE_STOP)) -#define SP_IS_STOP_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE((k), SP_TYPE_STOP)) - -GType sp_stop_get_type(); - - -#endif /* !SEEN_SP_STOP_FNS_H */ diff --git a/src/sp-stop.cpp b/src/sp-stop.cpp new file mode 100644 index 000000000..740cfef78 --- /dev/null +++ b/src/sp-stop.cpp @@ -0,0 +1,66 @@ +/** @file + * @gradient stop class. + */ +/* Authors: + * Lauris Kaplinski + * bulia byak + * Johan Engelen + * Jon A. Cruz + * + * Copyright (C) 1999,2005 authors + * Copyright (C) 2010 Jon A. Cruz + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + + +#include "sp-stop.h" + + +// A stop might have some non-stop siblings +SPStop* SPStop::getNextStop() +{ + SPStop *result = 0; + + for (SPObject* obj = getNext(); obj && !result; obj = obj->getNext()) { + if (SP_IS_STOP(obj)) { + result = SP_STOP(obj); + } + } + + return result; +} + +SPStop* SPStop::getPrevStop() +{ + SPStop *result = 0; + + for (SPObject* obj = getPrev(); obj; obj = obj->getPrev()) { + // The closest previous SPObject that is an SPStop *should* be ourself. + if (SP_IS_STOP(obj)) { + SPStop* stop = SP_STOP(obj); + // Sanity check to ensure we have a proper sibling structure. + if (stop->getNextStop() == this) { + result = stop; + } else { + g_warning("SPStop previous/next relationship broken"); + } + break; + } + } + + return result; +} + + + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : diff --git a/src/sp-stop.h b/src/sp-stop.h index 3203d8f74..bf6893db1 100644 --- a/src/sp-stop.h +++ b/src/sp-stop.h @@ -9,14 +9,23 @@ */ #include -//#include #include "sp-object.h" #include "color.h" -#include "sp-stop-fns.h" class SPObjectClass; class SPColor; +struct SPStop; +struct SPStopClass; + +#define SP_TYPE_STOP (sp_stop_get_type()) +#define SP_STOP(o) (G_TYPE_CHECK_INSTANCE_CAST((o), SP_TYPE_STOP, SPStop)) +#define SP_STOP_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), SP_TYPE_STOP, SPStopClass)) +#define SP_IS_STOP(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), SP_TYPE_STOP)) +#define SP_IS_STOP_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE((k), SP_TYPE_STOP)) + +GType sp_stop_get_type(); + /** Gradient stop. */ struct SPStop : public SPObject { /// \todo fixme: Should be SPSVGPercentage @@ -32,6 +41,10 @@ struct SPStop : public SPObject { /// \todo fixme: Implement SPSVGNumber or something similar. gfloat opacity; + + + SPStop* getNextStop(); + SPStop* getPrevStop(); }; /// The SPStop vtable. diff --git a/src/spray-context.cpp b/src/spray-context.cpp index dc5618eef..051518e1f 100644 --- a/src/spray-context.cpp +++ b/src/spray-context.cpp @@ -56,7 +56,6 @@ #include "path-chemistry.h" #include "sp-gradient.h" #include "sp-stop.h" -#include "sp-stop-fns.h" #include "sp-gradient-reference.h" #include "sp-linear-gradient.h" #include "sp-radial-gradient.h" diff --git a/src/tweak-context.cpp b/src/tweak-context.cpp index 3f55d040b..13299b5a4 100644 --- a/src/tweak-context.cpp +++ b/src/tweak-context.cpp @@ -55,7 +55,6 @@ #include "path-chemistry.h" #include "sp-gradient.h" #include "sp-stop.h" -#include "sp-stop-fns.h" #include "sp-gradient-reference.h" #include "sp-linear-gradient.h" #include "sp-radial-gradient.h" diff --git a/src/widgets/gradient-vector.cpp b/src/widgets/gradient-vector.cpp index 0b3c13820..751898f4f 100644 --- a/src/widgets/gradient-vector.cpp +++ b/src/widgets/gradient-vector.cpp @@ -583,8 +583,6 @@ static void update_stop_list( GtkWidget *mnu, SPGradient *gradient, SPStop *new_ // user selected existing stop from list static void sp_grad_edit_select(GtkOptionMenu *mnu, GtkWidget *tbl) { - SPGradient *gradient = (SPGradient *)g_object_get_data(G_OBJECT(tbl), "gradient"); - GObject *item = G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))); SPStop *stop = SP_STOP(g_object_get_data(item, "stop")); if (!stop) { @@ -607,7 +605,7 @@ static void sp_grad_edit_select(GtkOptionMenu *mnu, GtkWidget *tbl) bool isEndStop = false; SPStop *prev = NULL; - prev = sp_prev_stop(stop, gradient); + prev = stop->getPrevStop(); if (prev != NULL ) { adj->lower = prev->offset; } else { @@ -616,7 +614,7 @@ static void sp_grad_edit_select(GtkOptionMenu *mnu, GtkWidget *tbl) } SPStop *next = NULL; - next = sp_next_stop(stop); + next = stop->getNextStop(); if (next != NULL ) { adj->upper = next->offset; } else { @@ -691,10 +689,10 @@ static void sp_grd_ed_add_stop(GtkWidget */*widget*/, GtkWidget *vb) Inkscape::XML::Node *new_stop_repr = NULL; - SPStop *next = sp_next_stop(stop); + SPStop *next = stop->getNextStop(); if (next == NULL) { - SPStop *prev = sp_prev_stop(stop, gradient); + SPStop *prev = stop->getPrevStop(); if (prev != NULL) { next = stop; stop = prev; @@ -706,8 +704,8 @@ static void sp_grd_ed_add_stop(GtkWidget */*widget*/, GtkWidget *vb) SP_OBJECT_REPR(gradient)->addChild(new_stop_repr, SP_OBJECT_REPR(stop)); } else { next = stop; - new_stop_repr = SP_OBJECT_REPR(sp_prev_stop(stop, gradient))->duplicate(SP_OBJECT_REPR(gradient)->document()); - SP_OBJECT_REPR(gradient)->addChild(new_stop_repr, SP_OBJECT_REPR(sp_prev_stop(stop, gradient))); + new_stop_repr = SP_OBJECT_REPR(stop->getPrevStop())->duplicate(SP_OBJECT_REPR(gradient)->document()); + SP_OBJECT_REPR(gradient)->addChild(new_stop_repr, SP_OBJECT_REPR(stop->getPrevStop())); } SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(gradient)->getObjectByRepr(new_stop_repr); @@ -748,13 +746,13 @@ static void sp_grd_ed_del_stop(GtkWidget */*widget*/, GtkWidget *vb) // if we delete first or last stop, move the next/previous to the edge if (stop->offset == 0) { - SPStop *next = sp_next_stop(stop); + SPStop *next = stop->getNextStop(); if (next) { next->offset = 0; sp_repr_set_css_double(SP_OBJECT_REPR(next), "offset", 0); } } else if (stop->offset == 1) { - SPStop *prev = sp_prev_stop(stop, gradient); + SPStop *prev = stop->getPrevStop(); if (prev) { prev->offset = 1; sp_repr_set_css_double(SP_OBJECT_REPR(prev), "offset", 1);