diff options
| author | Jon A. Cruz <jon@joncruz.org> | 2009-03-08 09:30:53 +0000 |
|---|---|---|
| committer | joncruz <joncruz@users.sourceforge.net> | 2009-03-08 09:30:53 +0000 |
| commit | d2bdd8a04cd69dad117d2c7a76e44fe0f78a4265 (patch) | |
| tree | 553d8fa818f154f328427468b82c3f319b1b7f71 /src/widgets | |
| parent | - When finalizing a curve by double clicking then don't snap to the curve itself (diff) | |
| download | inkscape-d2bdd8a04cd69dad117d2c7a76e44fe0f78a4265.tar.gz inkscape-d2bdd8a04cd69dad117d2c7a76e44fe0f78a4265.zip | |
Reworked internals of color and drag-n-drop.
(bzr r7439)
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/eek-color-def.cpp | 129 | ||||
| -rw-r--r-- | src/widgets/eek-color-def.h | 1 |
2 files changed, 120 insertions, 10 deletions
diff --git a/src/widgets/eek-color-def.cpp b/src/widgets/eek-color-def.cpp index 8c8d240c6..fb0eeeb5e 100644 --- a/src/widgets/eek-color-def.cpp +++ b/src/widgets/eek-color-def.cpp @@ -44,6 +44,11 @@ #endif #include <stdint.h> +#include <string> +#include <iostream> +#include <sstream> +#include <stdlib.h> +#include <string.h> #if !defined(_) #define _(s) gettext(s) @@ -54,6 +59,12 @@ namespace eek { +static std::string mimeTEXT("text/plain"); +static std::string mimeX_COLOR("application/x-color"); +static std::string mimeOSWB_COLOR("application/x-oswb-color"); + +static std::string doubleToStr(double d); + ColorDef::ColorDef() : descr(_("none")), type(NONE), @@ -131,23 +142,21 @@ public: std::vector<std::string> ColorDef::getMIMETypes() { std::vector<std::string> listing; - if ( getType() != eek::ColorDef::RGB ) { - listing.push_back("application/x-oswb-nocolor"); - } - listing.push_back("application/x-color"); - listing.push_back("text/plain"); + listing.push_back(mimeOSWB_COLOR); + listing.push_back(mimeX_COLOR); + listing.push_back(mimeTEXT); return listing; } void ColorDef::getMIMEData(std::string const & type, char*& dest, int& len, int& format) { - if ( type == "text/plain" ) { + if ( type == mimeTEXT ) { dest = new char[8]; snprintf( dest, 8, "#%02x%02x%02x", getR(), getG(), getB() ); dest[7] = 0; len = 8; format = 8; - } else if ( (type == "application/x-color") || (type == "application/x-oswb-nocolor") ) { + } else if ( type == mimeX_COLOR ) { uint16_t* tmp = new uint16_t[4]; tmp[0] = (getR() << 8) | getR(); tmp[1] = (getG() << 8) | getG(); @@ -156,6 +165,38 @@ void ColorDef::getMIMEData(std::string const & type, char*& dest, int& len, int& dest = reinterpret_cast<char*>(tmp); len = 8; format = 16; + } else if ( type == mimeOSWB_COLOR ) { + std::string tmp("<paint>"); + switch ( getType() ) { + case eek::ColorDef::NONE: + { + tmp += "<nocolor/>"; + } + break; + case eek::ColorDef::CLEAR: + { + tmp += "<clear/>"; + } + break; + default: + { + tmp += std::string("<color name=\"") + descr + "\">"; + tmp += "<sRGB r=\""; + tmp += doubleToStr(getR()/255.0); + tmp += "\" g=\""; + tmp += doubleToStr(getG()/255.0); + tmp += "\" b=\""; + tmp += doubleToStr(getB()/255.0); + tmp += "\"/>"; + tmp += "</color>"; + } + } + tmp += "</paint>"; + len = tmp.size(); + dest = new char[len]; + // Note that this is not null-terminated: + memcpy(dest, tmp.c_str(), len); + format = 8; } else { // nothing dest = 0; @@ -163,6 +204,68 @@ void ColorDef::getMIMEData(std::string const & type, char*& dest, int& len, int& } } +bool ColorDef::fromMIMEData(std::string const & type, char const * data, int len, int /*format*/) +{ + bool worked = false; + bool changed = false; + if ( type == mimeTEXT ) { + } else if ( type == mimeX_COLOR ) { + } else if ( type == mimeOSWB_COLOR ) { + std::string xml(data, len); + if ( xml.find("<nocolor/>") != std::string::npos ) { + if ( (this->type != eek::ColorDef::NONE) + || (this->r != 0) + || (this->g != 0) + || (this->b != 0) ) { + this->type = eek::ColorDef::NONE; + this->r = 0; + this->g = 0; + this->b = 0; + changed = true; + } + worked = true; + } else { + size_t pos = xml.find("<sRGB"); + if ( pos != std::string::npos ) { + size_t endPos = xml.find(">", pos); + std::string srgb = xml.substr(pos, endPos); + this->type = eek::ColorDef::RGB; + size_t numPos = srgb.find("r="); + if (numPos != std::string::npos) { + char* endPtr = 0; + double dbl = strtod(srgb.c_str() + numPos + 3, &endPtr); + this->r = static_cast<int>(255 * dbl); + } + numPos = srgb.find("g="); + if (numPos != std::string::npos) { + char* endPtr = 0; + double dbl = strtod(srgb.c_str() + numPos + 3, &endPtr); + this->g = static_cast<int>(255 * dbl); + } + numPos = srgb.find("b="); + if (numPos != std::string::npos) { + char* endPtr = 0; + double dbl = strtod(srgb.c_str() + numPos + 3, &endPtr); + this->b = static_cast<int>(255 * dbl); + } + changed = true; + worked = true; + } + } + } + if ( changed ) { + // beware of callbacks changing things + for ( std::vector<HookData*>::iterator it = _listeners.begin(); it != _listeners.end(); ++it ) + { + if ( (*it)->_cb ) + { + (*it)->_cb( (*it)->_data ); + } + } + } + return worked; +} + void ColorDef::setRGB( unsigned int r, unsigned int g, unsigned int b ) { if ( r != this->r || g != this->g || b != this->b ) { @@ -186,10 +289,16 @@ void ColorDef::addCallback( ColorCallback cb, void* data ) _listeners.push_back( new HookData(cb, data) ); } -void ColorDef::removeCallback( ColorCallback cb, void* data ) +void ColorDef::removeCallback( ColorCallback /*cb*/, void* /*data*/ ) +{ +} + +static std::string doubleToStr(double d) { - (void)cb; - (void)data; + // TODO ensure "." is used for decimal separator. + std::stringstream out; + out << d; + return out.str(); } } // namespace eek diff --git a/src/widgets/eek-color-def.h b/src/widgets/eek-color-def.h index 39d69d5d1..2f6117d54 100644 --- a/src/widgets/eek-color-def.h +++ b/src/widgets/eek-color-def.h @@ -66,6 +66,7 @@ public: std::vector<std::string> getMIMETypes(); void getMIMEData(std::string const & type, char*& dest, int& len, int& format); + bool fromMIMEData(std::string const & type, char const * data, int len, int format); void setRGB( unsigned int r, unsigned int g, unsigned int b ); unsigned int getR() const { return r; } |
