Code

Cleanup of gradients and stops.
authorJon A. Cruz <jon@joncruz.org>
Mon, 8 Mar 2010 01:31:49 +0000 (17:31 -0800)
committerJon A. Cruz <jon@joncruz.org>
Mon, 8 Mar 2010 01:31:49 +0000 (17:31 -0800)
17 files changed:
src/CMakeLists.txt
src/Makefile_insert
src/extension/internal/odf.cpp
src/gradient-chemistry.cpp
src/gradient-chemistry.h
src/gradient-context.cpp
src/gradient-drag.cpp
src/sp-gradient.cpp
src/sp-object-repr.cpp
src/sp-object.cpp
src/sp-object.h
src/sp-stop-fns.h [deleted file]
src/sp-stop.cpp [new file with mode: 0644]
src/sp-stop.h
src/spray-context.cpp
src/tweak-context.cpp
src/widgets/gradient-vector.cpp

index 46c2586d120753936a988cd310213e1e8cb48746..a956f6ad82d847629bbea539f911d48983b18676 100644 (file)
@@ -61,6 +61,7 @@ sp-shape.cpp
 sp-skeleton.cpp\r
 sp-spiral.cpp\r
 sp-star.cpp\r
+sp-stop.cpp\r
 sp-string.cpp\r
 sp-style-elem.cpp\r
 sp-switch.cpp\r
index 574dfe084956f31329e86e0b534a9db7f51f3468..1e5b1fea0e3e62092c5aa402146e9ae182d8e259 100644 (file)
@@ -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               \
index 3537be4504646f29236e8d0f7578112a798a0273..76b901023640bb504ca3dd551611db2aff07726c 100644 (file)
@@ -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;
index 04eaeb08de582814f09e125eb61830e6de867117..7382aa49bcdeb56641a37e2b9f7bcdfa92396b78 100644 (file)
@@ -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;
index c6089a658b0e9bf9c2f04e99379c4a11d3218752..3f72fa394d0ab0ec8c34777c7baeeb9a095f426d 100644 (file)
@@ -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);
index e78fa44a4d0cb11bc0b351d8657d3596d4d41b3b..ed20f9b619e79b5cc090b074d41f61cff0015839 100644 (file)
@@ -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);
index f96775360f0ea80ae9edfa31594c63a2e3e30798..13b422119f0efc6b61c57a20c3ac00e94ee44d5e 100644 (file)
@@ -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;
             }
index fb16e9e5a495f44e3c5625b6ffa9cb34a58d4612..4e8e8e3738c59dfebcc0db49c60a86889169df74 100644 (file)
@@ -653,7 +653,7 @@ int SPGradient::getStopCount() const
 {
     int count = 0;
 
-    for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && sp_next_stop(stop); stop = sp_next_stop(stop)) {
+    for (SPStop *stop = const_cast<SPGradient*>(this)->getFirstStop(); stop && stop->getNextStop(); stop = stop->getNextStop()) {
         count++;
     }
 
index 4c3d5196ee2c4de5bb3753ca8ce46d17f94e4e79..62143e3ab047d458d07f611047ff00cf4935a75b 100644 (file)
@@ -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"
index f77f228cbfcaff724872ebc7435a1366d7243389..5e0f7275837f0b6a4d1a2e62ff18739d8ba35360 100644 (file)
@@ -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.
  */
index 22a15a0a4b272a60ac4ce1b6ad2bd31ebd832156..f7ca0f3a343ddfaa518f8718992c08bff67503ee 100644 (file)
@@ -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 (file)
index 9903359..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef SEEN_SP_STOP_FNS_H
-#define SEEN_SP_STOP_FNS_H
-
-#include <glib-object.h>
-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 (file)
index 0000000..740cfef
--- /dev/null
@@ -0,0 +1,66 @@
+/** @file
+ * @gradient stop class.
+ */
+/* Authors:
+ *   Lauris Kaplinski <lauris@kaplinski.com>
+ *   bulia byak
+ *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *
+ * 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 :
index 3203d8f7432edeb15eb9d72e9979eca719478dc7..bf6893db1262920a1dc84d8f373d30e68ef21108 100644 (file)
@@ -9,14 +9,23 @@
  */
 
 #include <glib/gtypes.h>
-//#include <glib-object.h>
 #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.
index dc5618eef5f674f6b849a3ed09f83bcde5c55b19..051518e1fdd8aa2aa2e2bcb0ecb9b4846af9c621 100644 (file)
@@ -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"
index 3f55d040bc426a0f806c2d0f37617da34c167395..13299b5a4004fef123cb90f05cc659879cd7ca24 100644 (file)
@@ -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"
index 0b3c13820788c92cd9fe86cb8710a86031914e21..751898f4f0dd2091e364d4d184c5b613d99b1ce2 100644 (file)
@@ -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);