Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-gradient.cpp
index 56ddd09d956d23dfb5f1b002c635b31fd35be0e3..1b8f9a11137a0e1e311200c2c00ca7ad97a81145 100644 (file)
@@ -6,6 +6,8 @@
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
  *   Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2002 Lauris Kaplinski
  * Copyright (C) 2000-2001 Ximian, Inc.
@@ -45,6 +47,7 @@
 #include "streq.h"
 #include "uri.h"
 #include "xml/repr.h"
+#include "style.h"
 
 #define SP_MACROS_SILENT
 #include "macros.h"
@@ -141,10 +144,10 @@ static void sp_stop_build(SPObject *object, SPDocument *document, Inkscape::XML:
     if (((SPObjectClass *) stop_parent_class)->build)
         (* ((SPObjectClass *) stop_parent_class)->build)(object, document, repr);
 
-    object->readAttr( "offset");
-    object->readAttr( "stop-color");
-    object->readAttr( "stop-opacity");
-    object->readAttr( "style");
+    object->readAttr( "offset" );
+    object->readAttr( "stop-color" );
+    object->readAttr( "stop-opacity" );
+    object->readAttr( "style" );
 }
 
 /**
@@ -172,8 +175,7 @@ sp_stop_set(SPObject *object, unsigned key, gchar const *value)
                 if (streq(p, "currentColor")) {
                     stop->currentColor = true;
                 } else {
-                    guint32 const color = sp_svg_read_color(p, 0);
-                    stop->specified_color.set( color );
+                    stop->specified_color = SPStop::readStopColor( p );
                 }
             }
             {
@@ -191,8 +193,7 @@ sp_stop_set(SPObject *object, unsigned key, gchar const *value)
                     stop->currentColor = true;
                 } else {
                     stop->currentColor = false;
-                    guint32 const color = sp_svg_read_color(p, 0);
-                    stop->specified_color.set( color );
+                    stop->specified_color = SPStop::readStopColor( p );
                 }
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
@@ -232,11 +233,12 @@ sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML:
         repr = xml_doc->createElement("svg:stop");
     }
 
-    guint32 specifiedcolor = stop->specified_color.toRGBA32( 255 );
+    Glib::ustring colorStr = stop->specified_color.toString();
     gfloat opacity = stop->opacity;
 
-    if (((SPObjectClass *) stop_parent_class)->write)
+    if (((SPObjectClass *) stop_parent_class)->write) {
         (* ((SPObjectClass *) stop_parent_class)->write)(object, xml_doc, repr, flags);
+    }
 
     // Since we do a hackish style setting here (because SPStyle does not support stop-color and
     // stop-opacity), we must do it AFTER calling the parent write method; otherwise
@@ -247,9 +249,7 @@ sp_stop_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML:
     if (stop->currentColor) {
         os << "currentColor";
     } else {
-        gchar c[64];
-        sp_svg_write_color(c, sizeof(c), specifiedcolor);
-        os << c;
+        os << colorStr;
     }
     os << ";stop-opacity:" << opacity;
     repr->setAttribute("style", os.str().c_str());
@@ -288,6 +288,17 @@ SPGradientSpread SPGradient::getSpread() const
     return spread;
 }
 
+void SPGradient::setSwatch( bool swatch )
+{
+    if ( swatch != isSwatch() ) {
+        this->swatch = swatch; // to make isSolid() work, this happens first
+        gchar const* paintVal = swatch ? (isSolid() ? "solid" : "gradient") : 0;
+        setAttribute( "osb:paint", paintVal, 0 );
+
+        requestModified( SP_OBJECT_MODIFIED_FLAG );
+    }
+}
+
 /**
  * Return stop's color as 32bit value.
  */
@@ -312,29 +323,6 @@ sp_stop_get_rgba32(SPStop const *const stop)
     }
 }
 
-/**
- * Return stop's color as SPColor.
- */
-static SPColor
-sp_stop_get_color(SPStop const *const stop)
-{
-    if (stop->currentColor) {
-        char const *str = stop->getStyleProperty( "color", NULL);
-        guint32 const dfl = 0;
-        /* Default value: arbitrarily black.  (SVG1.1 and CSS2 both say that the initial
-         * value depends on user agent, and don't give any further restrictions that I can
-         * see.) */
-        guint32 color = dfl;
-        if (str) {
-            color = sp_svg_read_color(str, dfl);
-        }
-        SPColor ret( color );
-        return ret;
-    } else {
-        return stop->specified_color;
-    }
-}
-
 /*
  * Gradient
  */
@@ -422,24 +410,30 @@ void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML
 {
     SPGradient *gradient = SP_GRADIENT(object);
 
-    if (((SPObjectClass *) gradient_parent_class)->build)
+    // Work-around in case a swatch had been marked for immediate collection:
+    if ( repr->attribute("osb:paint") && repr->attribute("inkscape:collect") ) {
+        repr->setAttribute("inkscape:collect", 0);
+    }
+
+    if (((SPObjectClass *) gradient_parent_class)->build) {
         (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr);
+    }
 
-    SPObject *ochild;
-    for ( ochild = object->first_child() ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
+    for ( SPObject *ochild = object->firstChild() ; ochild ; ochild = ochild->getNext() ) {
         if (SP_IS_STOP(ochild)) {
             gradient->has_stops = TRUE;
             break;
         }
     }
 
-    object->readAttr( "gradientUnits");
-    object->readAttr( "gradientTransform");
-    object->readAttr( "spreadMethod");
-    object->readAttr( "xlink:href");
+    object->readAttr( "gradientUnits" );
+    object->readAttr( "gradientTransform" );
+    object->readAttr( "spreadMethod" );
+    object->readAttr( "xlink:href" );
+    object->readAttr( "osb:paint" );
 
-    /* Register ourselves */
-    document->add_resource("gradient", object);
+    // Register ourselves
+    document->addResource("gradient", object);
 }
 
 /**
@@ -450,12 +444,12 @@ void SPGradientImpl::release(SPObject *object)
     SPGradient *gradient = (SPGradient *) object;
 
 #ifdef SP_GRADIENT_VERBOSE
-    g_print("Releasing gradient %s\n", SP_OBJECT_ID(object));
+    g_print("Releasing gradient %s\n", object->getId());
 #endif
 
     if (SP_OBJECT_DOCUMENT(object)) {
         /* Unregister ourselves */
-        SP_OBJECT_DOCUMENT(object)->remove_resource("gradient", SP_OBJECT(object));
+        SP_OBJECT_DOCUMENT(object)->removeResource("gradient", SP_OBJECT(object));
     }
 
     if (gradient->ref) {
@@ -537,9 +531,31 @@ void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const
                 gr->ref->detach();
             }
             break;
+        case SP_ATTR_OSB_SWATCH:
+        {
+            bool newVal = (value != 0);
+            bool modified = false;
+            if (newVal != gr->swatch) {
+                gr->swatch = newVal;
+                modified = true;
+            }
+            if (newVal) {
+                // Might need to flip solid/gradient
+                Glib::ustring paintVal = ( gr->hasStops() && (gr->getStopCount() == 0) ) ? "solid" : "gradient";
+                if ( paintVal != value ) {
+                    gr->setAttribute( "osb:paint", paintVal.c_str(), 0 );
+                    modified = true;
+                }
+            }
+            if (modified) {
+                object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            }
+        }
+            break;
         default:
-            if (((SPObjectClass *) gradient_parent_class)->set)
+            if (((SPObjectClass *) gradient_parent_class)->set) {
                 ((SPObjectClass *) gradient_parent_class)->set(object, key, value);
+            }
             break;
     }
 }
@@ -582,12 +598,19 @@ void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, In
 
     gr->invalidateVector();
 
-    if (((SPObjectClass *) gradient_parent_class)->child_added)
+    if (((SPObjectClass *) gradient_parent_class)->child_added) {
         (* ((SPObjectClass *) gradient_parent_class)->child_added)(object, child, ref);
+    }
 
     SPObject *ochild = object->get_child_by_repr(child);
     if ( ochild && SP_IS_STOP(ochild) ) {
         gr->has_stops = TRUE;
+        if ( gr->getStopCount() > 0 ) {
+            gchar const * attr = gr->getAttribute("osb:paint");
+            if ( attr && strcmp(attr, "gradient") ) {
+                gr->setAttribute( "osb:paint", "gradient", 0 );
+            }
+        }
     }
 
     /// \todo Fixme: should we schedule "modified" here?
@@ -608,14 +631,20 @@ void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child)
     }
 
     gr->has_stops = FALSE;
-    SPObject *ochild;
-    for ( ochild = object->first_child() ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
+    for ( SPObject *ochild = object->firstChild() ; ochild ; ochild = ochild->getNext() ) {
         if (SP_IS_STOP(ochild)) {
             gr->has_stops = TRUE;
             break;
         }
     }
 
+    if ( gr->getStopCount() == 0 ) {
+        gchar const * attr = gr->getAttribute("osb:paint");
+        if ( attr && strcmp(attr, "solid") ) {
+            gr->setAttribute( "osb:paint", "solid", 0 );
+        }
+    }
+
     /* Fixme: should we schedule "modified" here? */
     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
 }
@@ -640,7 +669,7 @@ void SPGradientImpl::modified(SPObject *object, guint flags)
 
     // FIXME: climb up the ladder of hrefs
     GSList *l = NULL;
-    for (SPObject *child = object->first_child() ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
+    for (SPObject *child = object->firstChild() ; child; child = child->getNext() ) {
         g_object_ref(G_OBJECT(child));
         l = g_slist_prepend(l, child);
     }
@@ -658,7 +687,7 @@ void SPGradientImpl::modified(SPObject *object, guint flags)
 SPStop* SPGradient::getFirstStop()
 {
     SPStop* first = 0;
-    for (SPObject *ochild = this->first_child(); ochild && !first; ochild = SP_OBJECT_NEXT(ochild)) {
+    for (SPObject *ochild = firstChild(); ochild && !first; ochild = ochild->getNext()) {
         if (SP_IS_STOP(ochild)) {
             first = SP_STOP(ochild);
         }
@@ -684,15 +713,17 @@ Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Docu
 {
     SPGradient *gr = SP_GRADIENT(object);
 
-    if (((SPObjectClass *) gradient_parent_class)->write)
+    if (((SPObjectClass *) gradient_parent_class)->write) {
         (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags);
+    }
 
     if (flags & SP_OBJECT_WRITE_BUILD) {
         GSList *l = NULL;
-        for (SPObject *child = object->first_child(); child; child = SP_OBJECT_NEXT(child)) {
-            Inkscape::XML::Node *crepr;
-            crepr = child->updateRepr(xml_doc, NULL, flags);
-            if (crepr) l = g_slist_prepend(l, crepr);
+        for (SPObject *child = object->firstChild(); child; child = child->getNext()) {
+            Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
+            if (crepr) {
+                l = g_slist_prepend(l, crepr);
+            }
         }
         while (l) {
             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
@@ -741,6 +772,16 @@ Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Docu
         }
     }
 
+    if ( (flags & SP_OBJECT_WRITE_EXT) && gr->isSwatch() ) {
+        if ( gr->isSolid() ) {
+            repr->setAttribute( "osb:paint", "solid" );
+        } else {
+            repr->setAttribute( "osb:paint", "gradient" );
+        }
+    } else {
+        repr->setAttribute( "osb:paint", 0 );
+    }
+
     return repr;
 }
 
@@ -921,7 +962,7 @@ sp_gradient_repr_write_vector(SPGradient *gr)
     g_return_if_fail(gr != NULL);
     g_return_if_fail(SP_IS_GRADIENT(gr));
 
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(gr));
+    Inkscape::XML::Document *xml_doc = SP_OBJECT_DOCUMENT(gr)->getReprDoc();
     Inkscape::XML::Node *repr = SP_OBJECT_REPR(gr);
 
     /* We have to be careful, as vector may be our own, so construct repr list at first */
@@ -933,9 +974,7 @@ sp_gradient_repr_write_vector(SPGradient *gr)
         sp_repr_set_css_double(child, "offset", gr->vector.stops[i].offset);
         /* strictly speaking, offset an SVG <number> rather than a CSS one, but exponents make no
          * sense for offset proportions. */
-        gchar c[64];
-        sp_svg_write_color(c, sizeof(c), gr->vector.stops[i].color.toRGBA32( 0x00 ));
-        os << "stop-color:" << c << ";stop-opacity:" << gr->vector.stops[i].opacity;
+        os << "stop-color:" << gr->vector.stops[i].color.toString() << ";stop-opacity:" << gr->vector.stops[i].opacity;
         child->setAttribute("style", os.str().c_str());
         /* Order will be reversed here */
         cl = g_slist_prepend(cl, child);
@@ -985,9 +1024,7 @@ bool SPGradient::invalidateVector()
 void SPGradient::rebuildVector()
 {
     gint len = 0;
-    for ( SPObject *child = SP_OBJECT(this)->first_child() ;
-          child != NULL ;
-          child = SP_OBJECT_NEXT(child) ) {
+    for ( SPObject *child = firstChild() ; child ; child = child->getNext() ) {
         if (SP_IS_STOP(child)) {
             len ++;
         }
@@ -1009,9 +1046,7 @@ void SPGradient::rebuildVector()
         }
     }
 
-    for (SPObject *child = SP_OBJECT(this)->first_child() ;
-         child != NULL;
-         child = SP_OBJECT_NEXT(child) ) {
+    for ( SPObject *child = firstChild(); child; child = child->getNext() ) {
         if (SP_IS_STOP(child)) {
             SPStop *stop = SP_STOP(child);
 
@@ -1032,7 +1067,7 @@ void SPGradient::rebuildVector()
             // down to 100%."
             gstop.offset = CLAMP(gstop.offset, 0, 1);
 
-            gstop.color = sp_stop_get_color(stop);
+            gstop.color = stop->getEffectiveColor();
             gstop.opacity = stop->opacity;
 
             vector.stops.push_back(gstop);
@@ -1492,10 +1527,10 @@ static void sp_lineargradient_build(SPObject *object,
     if (((SPObjectClass *) lg_parent_class)->build)
         (* ((SPObjectClass *) lg_parent_class)->build)(object, document, repr);
 
-    object->readAttr( "x1");
-    object->readAttr( "y1");
-    object->readAttr( "x2");
-    object->readAttr( "y2");
+    object->readAttr( "x1" );
+    object->readAttr( "y1" );
+    object->readAttr( "x2" );
+    object->readAttr( "y2" );
 }
 
 /**
@@ -1769,11 +1804,11 @@ sp_radialgradient_build(SPObject *object, SPDocument *document, Inkscape::XML::N
     if (((SPObjectClass *) rg_parent_class)->build)
         (* ((SPObjectClass *) rg_parent_class)->build)(object, document, repr);
 
-    object->readAttr( "cx");
-    object->readAttr( "cy");
-    object->readAttr( "r");
-    object->readAttr( "fx");
-    object->readAttr( "fy");
+    object->readAttr( "cx" );
+    object->readAttr( "cy" );
+    object->readAttr( "r" );
+    object->readAttr( "fx" );
+    object->readAttr( "fy" );
 }
 
 /**
@@ -1961,4 +1996,4 @@ sp_rg_fill(SPPainter *painter, NRPixBlock *pb)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :