From d2bdd8a04cd69dad117d2c7a76e44fe0f78a4265 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Sun, 8 Mar 2009 09:30:53 +0000 Subject: Reworked internals of color and drag-n-drop. (bzr r7439) --- src/widgets/eek-color-def.cpp | 129 ++++++++++++++++++++++++++++++++++++++---- src/widgets/eek-color-def.h | 1 + 2 files changed, 120 insertions(+), 10 deletions(-) (limited to 'src/widgets') 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 +#include +#include +#include +#include +#include #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 ColorDef::getMIMETypes() { std::vector 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(tmp); len = 8; format = 16; + } else if ( type == mimeOSWB_COLOR ) { + std::string tmp(""); + switch ( getType() ) { + case eek::ColorDef::NONE: + { + tmp += ""; + } + break; + case eek::ColorDef::CLEAR: + { + tmp += ""; + } + break; + default: + { + tmp += std::string(""; + tmp += ""; + tmp += ""; + } + } + tmp += ""; + 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("") != 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("", 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(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(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(255 * dbl); + } + changed = true; + worked = true; + } + } + } + if ( changed ) { + // beware of callbacks changing things + for ( std::vector::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 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; } -- cgit v1.2.3