From: joncruz Date: Sat, 7 Mar 2009 10:46:36 +0000 (+0000) Subject: Moving dnd data generation into the generic ColorDef class. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=9fd31c5b67631b1cc906f3031619ad4ac37b94bc;p=inkscape.git Moving dnd data generation into the generic ColorDef class. --- diff --git a/src/interface.cpp b/src/interface.cpp index f03df2917..e542a1944 100644 --- a/src/interface.cpp +++ b/src/interface.cpp @@ -90,8 +90,7 @@ static GtkTargetEntry ui_drop_target_entries [] = { #if ENABLE_MAGIC_COLORS {(gchar *)"application/x-inkscape-color", 0, APP_X_INKY_COLOR}, #endif // ENABLE_MAGIC_COLORS - {(gchar *)"application/x-inkscape-nocolor", 0, APP_X_NOCOLOR }, - {(gchar *)"application/x-inkscape-xcolor", 0, APP_X_XCOLOR }, + {(gchar *)"application/x-oswb-nocolor", 0, APP_X_NOCOLOR }, {(gchar *)"application/x-color", 0, APP_X_COLOR } }; diff --git a/src/ui/dialog/swatches.cpp b/src/ui/dialog/swatches.cpp index b29f9525e..ecd54be39 100644 --- a/src/ui/dialog/swatches.cpp +++ b/src/ui/dialog/swatches.cpp @@ -12,6 +12,7 @@ */ #include +#include #include //for GTK_RESPONSE* types #include @@ -109,32 +110,14 @@ typedef enum { TEXT_DATA } colorFlavorType; -//TODO: warning: deprecated conversion from string constant to ‘gchar*’ -// -//Turn out to be warnings that we should probably leave in place. The -// pointers/types used need to be read-only. So until we correct the using -// code, those warnings are actually desired. They say "Hey! Fix this". We -// definitely don't want to hide/ignore them. --JonCruz -static const GtkTargetEntry sourceColorEntries[] = { +std::map mimeToInt; +std::map intToMime; + #if ENABLE_MAGIC_COLORS // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID}, - {"application/x-inkscape-color", 0, APP_X_INKY_COLOR}, +// {"application/x-inkscape-color", 0, APP_X_INKY_COLOR}, #endif // ENABLE_MAGIC_COLORS - {"application/x-color", 0, APP_X_COLOR}, - {"text/plain", 0, TEXT_DATA}, -}; -static const GtkTargetEntry sourceNoColorEntries[] = { - {"application/x-inkscape-nocolor", 0, APP_X_NOCOLOR}, - {"application/x-color", 0, APP_X_COLOR}, - {"text/plain", 0, TEXT_DATA}, -}; - -static const GtkTargetEntry sourceClearColorEntries[] = { - {"application/x-inkscape-xcolor", 0, APP_X_XCOLOR}, - {"application/x-color", 0, APP_X_COLOR}, - {"text/plain", 0, TEXT_DATA}, -}; void ColorItem::_dragGetColorData( GtkWidget */*widget*/, GdkDragContext */*drag_context*/, @@ -143,121 +126,26 @@ void ColorItem::_dragGetColorData( GtkWidget */*widget*/, guint /*time*/, gpointer user_data) { - static GdkAtom typeXColor = gdk_atom_intern("application/x-color", FALSE); - static GdkAtom typeText = gdk_atom_intern("text/plain", FALSE); - ColorItem* item = reinterpret_cast(user_data); + std::string key; if ( info == TEXT_DATA ) { - gchar* tmp = g_strdup_printf("#%02x%02x%02x", item->def.getR(), item->def.getG(), item->def.getB() ); - - gtk_selection_data_set( data, - typeText, - 8, // format - (guchar*)tmp, - strlen((const char*)tmp) + 1); - g_free(tmp); - tmp = 0; - } else if ( info == APP_X_INKY_COLOR ) { - Glib::ustring paletteName; - - // Find where this thing came from - bool found = false; - int index = 0; - for ( std::vector::iterator it = possible.begin(); it != possible.end() && !found; ++it ) { - JustForNow* curr = *it; - index = 0; - for ( std::vector::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) { - if ( item == *zz ) { - found = true; - paletteName = curr->_name; - break; - } else { - index++; - } - } - } - -// if ( found ) { -// g_message("Found the color at entry %d in palette '%s'", index, paletteName.c_str() ); -// } else { -// g_message("Unable to find the color"); -// } - int itemCount = 4 + 2 + 1 + paletteName.length(); - - guint16* tmp = new guint16[itemCount]; - tmp[0] = (item->def.getR() << 8) | item->def.getR(); - tmp[1] = (item->def.getG() << 8) | item->def.getG(); - tmp[2] = (item->def.getB() << 8) | item->def.getB(); - tmp[3] = 0xffff; - tmp[4] = (item->_isLive || !item->_listeners.empty() || (item->_linkSrc != 0) ) ? 1 : 0; - - tmp[5] = index; - tmp[6] = paletteName.length(); - for ( unsigned int i = 0; i < paletteName.length(); i++ ) { - tmp[7 + i] = paletteName[i]; - } - gtk_selection_data_set( data, - typeXColor, - 16, // format - reinterpret_cast(tmp), - itemCount * 2); - delete[] tmp; + key = "text/plain"; } else if ( (info == APP_X_NOCOLOR) || (info == APP_X_XCOLOR) ) { - Glib::ustring paletteName; + key = "application/x-oswb-nocolor"; + } else { + key = "application/x-color"; + } - // Find where this thing came from - bool found = false; - int index = 0; - for ( std::vector::iterator it = possible.begin(); it != possible.end() && !found; ++it ) { - JustForNow* curr = *it; - index = 0; - for ( std::vector::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) { - if ( item == *zz ) { - found = true; - paletteName = curr->_name; - break; - } else { - index++; - } - } + if ( !key.empty() ) { + char* tmp = 0; + int len = 0; + int format = 0; + item->def.getMIMEData(key, tmp, len, format); + if ( tmp ) { + GdkAtom dataAtom = gdk_atom_intern( key.c_str(), FALSE ); + gtk_selection_data_set( data, dataAtom, format, (guchar*)tmp, len ); + delete[] tmp; } - -// if ( found ) { -// g_message("Found the color at entry %d in palette '%s'", index, paletteName.c_str() ); -// } else { -// g_message("Unable to find the color"); -// } - int itemCount = 4 + 2 + 1 + paletteName.length(); - - guint16* tmp = new guint16[itemCount]; - tmp[0] = (item->def.getR() << 8) | item->def.getR(); - tmp[1] = (item->def.getG() << 8) | item->def.getG(); - tmp[2] = (item->def.getB() << 8) | item->def.getB(); - tmp[3] = 0xffff; - tmp[4] = (item->_isLive || !item->_listeners.empty() || (item->_linkSrc != 0) ) ? 1 : 0; - - tmp[5] = index; - tmp[6] = paletteName.length(); - for ( unsigned int i = 0; i < paletteName.length(); i++ ) { - tmp[7 + i] = paletteName[i]; - } - gtk_selection_data_set( data, - typeXColor, - 16, // format - reinterpret_cast(tmp), - itemCount * 2); - delete[] tmp; - } else { - guint16 tmp[4]; - tmp[0] = (item->def.getR() << 8) | item->def.getR(); - tmp[1] = (item->def.getG() << 8) | item->def.getG(); - tmp[2] = (item->def.getB() << 8) | item->def.getB(); - tmp[3] = 0xffff; - gtk_selection_data_set( data, - typeXColor, - 16, // format - reinterpret_cast(tmp), - (3+1) * 2); } } @@ -617,6 +505,17 @@ void ColorItem::_colorDefChanged(void* data) Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio) { + if (mimeToInt.empty()) { + mimeToInt["application/x-inkscape-nocolor"] = APP_X_NOCOLOR; + intToMime[APP_X_NOCOLOR] = "application/x-inkscape-nocolor"; + + mimeToInt["application/x-color"] = APP_X_COLOR; + intToMime[APP_X_COLOR] = "application/x-color"; + + mimeToInt["text/plain"] = TEXT_DATA; + intToMime[TEXT_DATA] = "text/plain"; + } + Gtk::Widget* widget = 0; if ( style == PREVIEW_STYLE_BLURB) { Gtk::Label *lbl = new Gtk::Label(def.descr); @@ -700,14 +599,26 @@ Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewS G_CALLBACK(handleButtonPress), this); - gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()), - GDK_BUTTON1_MASK, - (def.getType() == eek::ColorDef::CLEAR) ? sourceClearColorEntries : - (def.getType() == eek::ColorDef::NONE) ? sourceNoColorEntries : sourceColorEntries, - (def.getType() == eek::ColorDef::CLEAR) ? G_N_ELEMENTS(sourceClearColorEntries) : - (def.getType() == eek::ColorDef::NONE) ? G_N_ELEMENTS(sourceNoColorEntries) : - G_N_ELEMENTS(sourceColorEntries), - GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) ); + { + std::vector listing = def.getMIMETypes(); + int entryCount = listing.size(); + GtkTargetEntry* entries = new GtkTargetEntry[entryCount]; + GtkTargetEntry* curr = entries; + for ( std::vector::iterator it = listing.begin(); it != listing.end(); ++it ) { + curr->target = g_strdup(it->c_str()); + curr->flags = 0; + curr->info = mimeToInt[curr->target]; + curr++; + } + gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()), + GDK_BUTTON1_MASK, + entries, entryCount, + GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) ); + for ( int i = 0; i < entryCount; i++ ) { + g_free(entries[i].target); + } + delete[] entries; + } g_signal_connect( G_OBJECT(newBlot->gobj()), "drag-data-get", diff --git a/src/widgets/eek-color-def.cpp b/src/widgets/eek-color-def.cpp index 6334061c2..8c8d240c6 100644 --- a/src/widgets/eek-color-def.cpp +++ b/src/widgets/eek-color-def.cpp @@ -43,6 +43,8 @@ #include #endif +#include + #if !defined(_) #define _(s) gettext(s) #endif // !defined(_) @@ -125,6 +127,42 @@ public: void* _data; }; + +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"); + return listing; +} + +void ColorDef::getMIMEData(std::string const & type, char*& dest, int& len, int& format) +{ + if ( type == "text/plain" ) { + 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") ) { + uint16_t* tmp = new uint16_t[4]; + tmp[0] = (getR() << 8) | getR(); + tmp[1] = (getG() << 8) | getG(); + tmp[2] = (getB() << 8) | getB(); + tmp[3] = 0xffff; + dest = reinterpret_cast(tmp); + len = 8; + format = 16; + } else { + // nothing + dest = 0; + len = 0; + } +} + void ColorDef::setRGB( unsigned int r, unsigned int g, unsigned int b ) { if ( r != this->r || g != this->g || b != this->b ) { diff --git a/src/widgets/eek-color-def.h b/src/widgets/eek-color-def.h index 7e54182d6..39d69d5d1 100644 --- a/src/widgets/eek-color-def.h +++ b/src/widgets/eek-color-def.h @@ -64,6 +64,9 @@ public: ColorType getType() const { return type; } + std::vector getMIMETypes(); + void getMIMEData(std::string const & type, char*& dest, int& len, int& format); + void setRGB( unsigned int r, unsigned int g, unsigned int b ); unsigned int getR() const { return r; } unsigned int getG() const { return g; }