Code

Refactored preferences handling into a new version of
[inkscape.git] / src / dialogs / swatches.cpp
index 7da1b5694fa0bd1927674e3e68a1f3faf4ecc2d7..5d76cc8f6dcc70d61da43214606dd2acb061b0b0 100644 (file)
@@ -3,8 +3,10 @@
  *
  * Authors:
  *   Jon A. Cruz
+ *   John Bintz
  *
  * Copyright (C) 2005 Jon A. Cruz
+ * Copyright (C) 2008 John Bintz
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
 #include <gtk/gtkdialog.h> //for GTK_RESPONSE* types
 #include <gtk/gtkdnd.h>
+#include <gtk/gtkmenu.h>
+#include <gtk/gtkmenuitem.h>
+#include <gtk/gtkseparatormenuitem.h>
 
 #include <glibmm/i18n.h>
+#include <gdkmm/pixbuf.h>
 #include "inkscape.h"
+#include "desktop.h"
+#include "message-context.h"
 #include "document.h"
 #include "desktop-handles.h"
 #include "extension/db.h"
 #include "inkscape.h"
-#include "svg/svg.h"
+#include "svg/svg-color.h"
 #include "desktop-style.h"
 #include "io/sys.h"
 #include "path-prefix.h"
 #include "swatches.h"
+#include "sp-item.h"
+#include "preferences.h"
 
 #include "eek-preview.h"
 
@@ -35,14 +45,14 @@ namespace Inkscape {
 namespace UI {
 namespace Dialogs {
 
-SwatchesPanel* SwatchesPanel::instance = 0;
-
 
 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
-    _r(r),
-    _g(g),
-    _b(b),
-    _name(name)
+    def( r, g, b, name ),
+    _isLive(false),
+    _linkIsTone(false),
+    _linkPercent(0),
+    _linkGray(0),
+    _linkSrc(0)
 {
 }
 
@@ -61,37 +71,62 @@ ColorItem::ColorItem(ColorItem const &other) :
 ColorItem &ColorItem::operator=(ColorItem const &other)
 {
     if ( this != &other ) {
-        _r = other._r;
-        _g = other._g;
-        _b = other._b;
-        _name = other._name;
+        def = other.def;
+
+        // TODO - correct linkage
+        _linkSrc = other._linkSrc;
+        g_message("Erk!");
     }
     return *this;
 }
 
+
+class JustForNow
+{
+public:
+    JustForNow() : _prefWidth(0) {}
+
+    Glib::ustring _name;
+    int _prefWidth;
+    std::vector<ColorItem*> _colors;
+};
+
+static std::vector<JustForNow*> possible;
+
+
+
 typedef enum {
-    XCOLOR_DATA = 0,
+    APP_X_INKY_COLOR_ID = 0,
+    APP_X_INKY_COLOR = 0,
+    APP_X_COLOR,
     TEXT_DATA
 } colorFlavorType;
 
-static const GtkTargetEntry color_entries[] = {
-    {"application/x-color", 0, XCOLOR_DATA},
+static const GtkTargetEntry sourceColorEntries[] = {
+#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},
+#endif // ENABLE_MAGIC_COLORS
+    {"application/x-color", 0, APP_X_COLOR},
     {"text/plain", 0, TEXT_DATA},
 };
 
-static void dragGetColorData( GtkWidget *widget,
-                              GdkDragContext *drag_context,
-                              GtkSelectionData *data,
-                              guint info,
-                              guint time,
-                              gpointer user_data)
+void ColorItem::_dragGetColorData( GtkWidget *widget,
+                                   GdkDragContext *drag_context,
+                                   GtkSelectionData *data,
+                                   guint info,
+                                   guint time,
+                                   gpointer user_data)
 {
+    (void)widget;
+    (void)drag_context;
+    (void)time;
     static GdkAtom typeXColor = gdk_atom_intern("application/x-color", FALSE);
     static GdkAtom typeText = gdk_atom_intern("text/plain", FALSE);
 
     ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
-    if ( info == 1 ) {
-        gchar* tmp = g_strdup_printf("#%02x%02x%02x", item->_r, item->_g, item->_b);
+    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,
@@ -100,70 +135,428 @@ static void dragGetColorData( GtkWidget *widget,
                                 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<JustForNow*>::iterator it = possible.begin(); it != possible.end() && !found; ++it ) {
+            JustForNow* curr = *it;
+            index = 0;
+            for ( std::vector<ColorItem*>::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<const guchar*>(tmp),
+                                itemCount * 2);
+        delete[] tmp;
     } else {
-        guchar tmp[8];
-        tmp[0] = item->_r;
-        tmp[1] = item->_r;
-        tmp[2] = item->_g;
-        tmp[3] = item->_g;
-        tmp[4] = item->_b;
-        tmp[5] = item->_b;
-        tmp[6] = 0x0ff;
-        tmp[7] = 0x0ff;
+        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,
-                                8, // format
-                                tmp,
+                                16, // format
+                                reinterpret_cast<const guchar*>(tmp),
                                 (3+1) * 2);
     }
 }
 
-//"drag-drop"
-gboolean dragDropColorData( GtkWidget *widget,
-                            GdkDragContext *drag_context,
-                            gint x,
-                            gint y,
-                            guint time,
-                            gpointer user_data)
+static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
 {
-// TODO finish
-    return TRUE;
+    (void)widget;
+    ColorItem* item = reinterpret_cast<ColorItem*>(data);
+    if ( item )
+    {
+        Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, 32, 24 );
+        guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
+                         | (0x00ff0000 & (item->def.getG() << 16))
+                         | (0x0000ff00 & (item->def.getB() <<  8));
+        thumb->fill( fillWith );
+        gtk_drag_set_icon_pixbuf( dc, thumb->gobj(), 0, 0 );
+    }
+
 }
 
-static void bouncy( GtkWidget* widget, gpointer callback_data ) {
+//"drag-drop"
+// gboolean dragDropColorData( GtkWidget *widget,
+//                             GdkDragContext *drag_context,
+//                             gint x,
+//                             gint y,
+//                             guint time,
+//                             gpointer user_data)
+// {
+// // TODO finish
+
+//     return TRUE;
+// }
+
+static void handleClick( GtkWidget* widget, gpointer callback_data ) {
+    (void)widget;
     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
     if ( item ) {
         item->buttonClicked(false);
     }
 }
 
-static void bouncy2( GtkWidget* widget, gint arg1, gpointer callback_data ) {
+static void handleSecondaryClick( GtkWidget* widget, gint arg1, gpointer callback_data ) {
+    (void)widget;
+    (void)arg1;
     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
     if ( item ) {
         item->buttonClicked(true);
     }
 }
 
-Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Gtk::BuiltinIconSize size)
+static gboolean handleEnterNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
+    ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
+    if ( item ) {
+        SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+        if ( desktop ) {
+            gchar* msg = g_strdup_printf(_("Color: <b>%s</b>; <b>Click</b> to set fill, <b>Shift+click</b> to set stroke"),
+                                         item->def.descr.c_str());
+            desktop->tipsMessageContext()->set(Inkscape::INFORMATION_MESSAGE, msg);
+            g_free(msg);
+        }
+    }
+    return FALSE;
+}
+
+static gboolean handleLeaveNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
+    ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
+    if ( item ) {
+        SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+        if ( desktop ) {
+            desktop->tipsMessageContext()->clear();
+        }
+    }
+    return FALSE;
+}
+
+static GtkWidget* popupMenu = 0;
+static ColorItem* bounceTarget = 0;
+
+static void redirClick( GtkMenuItem *menuitem, gpointer user_data )
+{
+    (void)user_data;
+    if ( bounceTarget ) {
+        handleClick( GTK_WIDGET(menuitem), bounceTarget );
+    }
+}
+
+static void redirSecondaryClick( GtkMenuItem *menuitem, gpointer user_data )
+{
+    (void)user_data;
+    if ( bounceTarget ) {
+        handleSecondaryClick( GTK_WIDGET(menuitem), 0, bounceTarget );
+    }
+}
+
+static gboolean handleButtonPress( GtkWidget* widget, GdkEventButton* event, gpointer user_data)
+{
+    (void)widget;
+    gboolean handled = FALSE;
+
+    if ( (event->button == 3) && (event->type == GDK_BUTTON_PRESS) ) {
+        if ( !popupMenu ) {
+            popupMenu = gtk_menu_new();
+            GtkWidget* child = 0;
+
+            //TRANSLATORS: An item in context menu on a colour in the swatches
+            child = gtk_menu_item_new_with_label(_("Set fill"));
+            g_signal_connect( G_OBJECT(child),
+                              "activate",
+                              G_CALLBACK(redirClick),
+                              user_data);
+            gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+
+            //TRANSLATORS: An item in context menu on a colour in the swatches
+            child = gtk_menu_item_new_with_label(_("Set stroke"));
+
+            g_signal_connect( G_OBJECT(child),
+                              "activate",
+                              G_CALLBACK(redirSecondaryClick),
+                              user_data);
+            gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+
+            gtk_widget_show_all(popupMenu);
+        }
+
+        ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
+        if ( item ) {
+            bounceTarget = item;
+            if ( popupMenu ) {
+                gtk_menu_popup(GTK_MENU(popupMenu), NULL, NULL, NULL, NULL, event->button, event->time);
+                handled = TRUE;
+            }
+        }
+    }
+
+    return handled;
+}
+
+static void dieDieDie( GtkObject *obj, gpointer user_data )
+{
+    g_message("die die die %p  %p", obj, user_data );
+}
+
+static const GtkTargetEntry destColorTargets[] = {
+#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},
+#endif // ENABLE_MAGIC_COLORS
+    {"application/x-color", 0, APP_X_COLOR},
+};
+
+#include "color.h" // for SP_RGBA32_U_COMPOSE
+
+void ColorItem::_dropDataIn( GtkWidget *widget,
+                             GdkDragContext *drag_context,
+                             gint x, gint y,
+                             GtkSelectionData *data,
+                             guint info,
+                             guint event_time,
+                             gpointer user_data)
+{
+    (void)widget;
+    (void)drag_context;
+    (void)x;
+    (void)y;
+    (void)event_time;
+//     g_message("    droppy droppy   %d", info);
+     switch (info) {
+         case APP_X_INKY_COLOR:
+         {
+             if ( data->length >= 8 ) {
+                 // Careful about endian issues.
+                 guint16* dataVals = (guint16*)data->data;
+                 if ( user_data ) {
+                     ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
+                     if ( item->def.isEditable() ) {
+                         // Shove on in the new value
+                         item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
+                     }
+                 }
+             }
+             break;
+         }
+         case APP_X_COLOR:
+         {
+             if ( data->length == 8 ) {
+                 // Careful about endian issues.
+                 guint16* dataVals = (guint16*)data->data;
+//                  {
+//                      gchar c[64] = {0};
+//                      sp_svg_write_color( c, 64,
+//                                          SP_RGBA32_U_COMPOSE(
+//                                              0x0ff & (dataVals[0] >> 8),
+//                                              0x0ff & (dataVals[1] >> 8),
+//                                              0x0ff & (dataVals[2] >> 8),
+//                                              0xff // can't have transparency in the color itself
+//                                              //0x0ff & (data->data[3] >> 8),
+//                                              ));
+//                  }
+                 if ( user_data ) {
+                     ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
+                     if ( item->def.isEditable() ) {
+                         // Shove on in the new value
+                         item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
+                     }
+                 }
+             }
+             break;
+         }
+         default:
+             g_message("unknown drop type");
+     }
+
+}
+
+static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::ustring const& match, int r, int g, int b )
+{
+    bool changed = false;
+
+    if ( node ) {
+        gchar const * val = node->attribute("inkscape:x-fill-tag");
+        if ( val  && (match == val) ) {
+            SPObject *obj = document->getObjectByRepr( node );
+
+            gchar c[64] = {0};
+            sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
+            SPCSSAttr *css = sp_repr_css_attr_new();
+            sp_repr_css_set_property( css, "fill", c );
+
+            sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
+            ((SPItem*)obj)->updateRepr();
+
+            changed = true;
+        }
+
+        val = node->attribute("inkscape:x-stroke-tag");
+        if ( val  && (match == val) ) {
+            SPObject *obj = document->getObjectByRepr( node );
+
+            gchar c[64] = {0};
+            sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
+            SPCSSAttr *css = sp_repr_css_attr_new();
+            sp_repr_css_set_property( css, "stroke", c );
+
+            sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
+            ((SPItem*)obj)->updateRepr();
+
+            changed = true;
+        }
+
+        Inkscape::XML::Node* first = node->firstChild();
+        changed |= bruteForce( document, first, match, r, g, b );
+
+        changed |= bruteForce( document, node->next(), match, r, g, b );
+    }
+
+    return changed;
+}
+
+void ColorItem::_colorDefChanged(void* data)
+{
+    ColorItem* item = reinterpret_cast<ColorItem*>(data);
+    if ( item ) {
+        for ( std::vector<Gtk::Widget*>::iterator it =  item->_previews.begin(); it != item->_previews.end(); ++it ) {
+            Gtk::Widget* widget = *it;
+            if ( IS_EEK_PREVIEW(widget->gobj()) ) {
+                EekPreview * preview = EEK_PREVIEW(widget->gobj());
+                eek_preview_set_color( preview,
+                                       (item->def.getR() << 8) | item->def.getR(),
+                                       (item->def.getG() << 8) | item->def.getG(),
+                                       (item->def.getB() << 8) | item->def.getB() );
+
+                eek_preview_set_linked( preview, (LinkType)((item->_linkSrc ? PREVIEW_LINK_IN:0)
+                                                            | (item->_listeners.empty() ? 0:PREVIEW_LINK_OUT)
+                                                            | (item->_isLive ? PREVIEW_LINK_OTHER:0)) );
+
+                widget->queue_draw();
+            }
+        }
+
+        for ( std::vector<ColorItem*>::iterator it = item->_listeners.begin(); it != item->_listeners.end(); ++it ) {
+            guint r = item->def.getR();
+            guint g = item->def.getG();
+            guint b = item->def.getB();
+
+            if ( (*it)->_linkIsTone ) {
+                r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
+                g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
+                b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
+            } else {
+                r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
+                g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
+                b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
+            }
+
+            (*it)->def.setRGB( r, g, b );
+        }
+
+
+        // Look for objects using this color
+        {
+            SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+            if ( desktop ) {
+                SPDocument* document = sp_desktop_document( desktop );
+                Inkscape::XML::Node *rroot =  sp_document_repr_root( document );
+                if ( rroot ) {
+
+                    // Find where this thing came from
+                    Glib::ustring paletteName;
+                    bool found = false;
+                    int index = 0;
+                    for ( std::vector<JustForNow*>::iterator it2 = possible.begin(); it2 != possible.end() && !found; ++it2 ) {
+                        JustForNow* curr = *it2;
+                        index = 0;
+                        for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
+                            if ( item == *zz ) {
+                                found = true;
+                                paletteName = curr->_name;
+                                break;
+                            } else {
+                                index++;
+                            }
+                        }
+                    }
+
+                    if ( !paletteName.empty() ) {
+                        gchar* str = g_strdup_printf("%d|", index);
+                        paletteName.insert( 0, str );
+                        g_free(str);
+                        str = 0;
+
+                        if ( bruteForce( document, rroot, paletteName, item->def.getR(), item->def.getG(), item->def.getB() ) ) {
+                            sp_document_done( document , SP_VERB_DIALOG_SWATCHES, 
+                                              _("Change color definition"));
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+
+
+Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio)
 {
     Gtk::Widget* widget = 0;
     if ( style == PREVIEW_STYLE_BLURB ) {
-        Gtk::Label *lbl = new Gtk::Label(_name);
+        Gtk::Label *lbl = new Gtk::Label(def.descr);
         lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
         widget = lbl;
     } else {
-        Glib::ustring blank("          ");
-        if ( size == Gtk::ICON_SIZE_MENU ) {
-            blank = " ";
-        }
+//         Glib::ustring blank("          ");
+//         if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
+//             blank = " ";
+//         }
 
         GtkWidget* eekWidget = eek_preview_new();
         EekPreview * preview = EEK_PREVIEW(eekWidget);
         Gtk::Widget* newBlot = Glib::wrap(eekWidget);
 
-        eek_preview_set_color( preview, (_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
+        eek_preview_set_color( preview, (def.getR() << 8) | def.getR(), (def.getG() << 8) | def.getG(), (def.getB() << 8) | def.getB());
 
-        eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::GtkIconSize)size );
+        eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::PreviewSize)size, ratio );
+        eek_preview_set_linked( preview, (LinkType)((_linkSrc ? PREVIEW_LINK_IN:0)
+                                                    | (_listeners.empty() ? 0:PREVIEW_LINK_OUT)
+                                                    | (_isLive ? PREVIEW_LINK_OTHER:0)) );
+
+        def.addCallback( _colorDefChanged, this );
 
         GValue val = {0, {{0}, {0}}};
         g_value_init( &val, G_TYPE_BOOLEAN );
@@ -182,42 +575,86 @@ Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Gtk::Built
         Gtk::Widget* newBlot = btn;
 */
 
-        tips.set_tip((*newBlot), _name);
+        tips.set_tip((*newBlot), def.descr);
 
 /*
         newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
 
         sigc::signal<void> type_signal_something;
 */
+
         g_signal_connect( G_OBJECT(newBlot->gobj()),
                           "clicked",
-                          G_CALLBACK(bouncy),
+                          G_CALLBACK(handleClick),
                           this);
 
         g_signal_connect( G_OBJECT(newBlot->gobj()),
                           "alt-clicked",
-                          G_CALLBACK(bouncy2),
+                          G_CALLBACK(handleSecondaryClick),
+                          this);
+
+        g_signal_connect( G_OBJECT(newBlot->gobj()),
+                          "button-press-event",
+                          G_CALLBACK(handleButtonPress),
                           this);
 
         gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
                              GDK_BUTTON1_MASK,
-                             color_entries,
-                             G_N_ELEMENTS(color_entries),
+                             sourceColorEntries,
+                             G_N_ELEMENTS(sourceColorEntries),
                              GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
 
         g_signal_connect( G_OBJECT(newBlot->gobj()),
                           "drag-data-get",
-                          G_CALLBACK(dragGetColorData),
+                          G_CALLBACK(ColorItem::_dragGetColorData),
+                          this);
+
+        g_signal_connect( G_OBJECT(newBlot->gobj()),
+                          "drag-begin",
+                          G_CALLBACK(dragBegin),
+                          this );
+
+        g_signal_connect( G_OBJECT(newBlot->gobj()),
+                          "enter-notify-event",
+                          G_CALLBACK(handleEnterNotify),
+                          this);
+
+        g_signal_connect( G_OBJECT(newBlot->gobj()),
+                          "leave-notify-event",
+                          G_CALLBACK(handleLeaveNotify),
                           this);
 
+//         g_signal_connect( G_OBJECT(newBlot->gobj()),
+//                           "drag-drop",
+//                           G_CALLBACK(dragDropColorData),
+//                           this);
+
+        if ( def.isEditable() )
+        {
+            gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
+                               GTK_DEST_DEFAULT_ALL,
+                               destColorTargets,
+                               G_N_ELEMENTS(destColorTargets),
+                               GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
+
+
+            g_signal_connect( G_OBJECT(newBlot->gobj()),
+                              "drag-data-received",
+                              G_CALLBACK(_dropDataIn),
+                              this );
+        }
+
         g_signal_connect( G_OBJECT(newBlot->gobj()),
-                          "drag-drop",
-                          G_CALLBACK(dragDropColorData),
+                          "destroy",
+                          G_CALLBACK(dieDieDie),
                           this);
 
+
         widget = newBlot;
     }
 
+    _previews.push_back( widget );
+
     return widget;
 }
 
@@ -226,16 +663,17 @@ void ColorItem::buttonClicked(bool secondary)
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
     if (desktop) {
         char const * attrName = secondary ? "stroke" : "fill";
-        guint32 rgba = (_r << 24) | (_g << 16) | (_b << 8) | 0xff;
+        guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
         gchar c[64];
-        sp_svg_write_color(c, 64, rgba);
+        sp_svg_write_color(c, sizeof(c), rgba);
 
         SPCSSAttr *css = sp_repr_css_attr_new();
         sp_repr_css_set_property( css, attrName, c );
         sp_desktop_set_style(desktop, css);
 
         sp_repr_css_attr_unref(css);
-        sp_document_done (SP_DT_DOCUMENT (desktop));
+        sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_SWATCHES, 
+                          secondary? _("Set stroke color from swatch") : _("Set fill color from swatch"));
     }
 }
 
@@ -275,19 +713,150 @@ bool parseNum( char*& str, int& val ) {
 }
 
 
-class JustForNow
+static bool getBlock( std::string& dst, guchar ch, std::string const str )
 {
-public:
-    JustForNow() : _prefWidth(0) {}
+    bool good = false;
+    std::string::size_type pos = str.find(ch);
+    if ( pos != std::string::npos )
+    {
+        std::string::size_type pos2 = str.find( '(', pos );
+        if ( pos2 != std::string::npos ) {
+            std::string::size_type endPos = str.find( ')', pos2 );
+            if ( endPos != std::string::npos ) {
+                dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
+                good = true;
+            }
+        }
+    }
+    return good;
+}
 
-    Glib::ustring _name;
-    int _prefWidth;
-    std::vector<ColorItem*> _colors;
-};
+static bool popVal( guint64& numVal, std::string& str )
+{
+    bool good = false;
+    std::string::size_type endPos = str.find(',');
+    if ( endPos == std::string::npos ) {
+        endPos = str.length();
+    }
 
-static std::vector<JustForNow*> possible;
+    if ( endPos != std::string::npos && endPos > 0 ) {
+        std::string xxx = str.substr( 0, endPos );
+        const gchar* ptr = xxx.c_str();
+        gchar* endPtr = 0;
+        numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
+        if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
+            // overflow
+        } else if ( (numVal == 0) && (endPtr == ptr) ) {
+            // failed conversion
+        } else {
+            good = true;
+            str.erase( 0, endPos + 1 );
+        }
+    }
+
+    return good;
+}
+
+void ColorItem::_wireMagicColors( void* p )
+{
+    JustForNow* onceMore = reinterpret_cast<JustForNow*>(p);
+    if ( onceMore )
+    {
+        for ( std::vector<ColorItem*>::iterator it = onceMore->_colors.begin(); it != onceMore->_colors.end(); ++it )
+        {
+            std::string::size_type pos = (*it)->def.descr.find("*{");
+            if ( pos != std::string::npos )
+            {
+                std::string subby = (*it)->def.descr.substr( pos + 2 );
+                std::string::size_type endPos = subby.find("}*");
+                if ( endPos != std::string::npos )
+                {
+                    subby.erase( endPos );
+                    //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
+                    //g_message("               '%s'", subby.c_str());
+
+                    if ( subby.find('E') != std::string::npos )
+                    {
+                        (*it)->def.setEditable( true );
+                    }
+
+                    if ( subby.find('L') != std::string::npos )
+                    {
+                        (*it)->_isLive = true;
+                    }
+
+                    std::string part;
+                    // Tint. index + 1 more val.
+                    if ( getBlock( part, 'T', subby ) ) {
+                        guint64 colorIndex = 0;
+                        if ( popVal( colorIndex, part ) ) {
+                            guint64 percent = 0;
+                            if ( popVal( percent, part ) ) {
+                                (*it)->_linkTint( *(onceMore->_colors[colorIndex]), percent );
+                            }
+                        }
+                    }
+
+                    // Shade/tone. index + 1 or 2 more val.
+                    if ( getBlock( part, 'S', subby ) ) {
+                        guint64 colorIndex = 0;
+                        if ( popVal( colorIndex, part ) ) {
+                            guint64 percent = 0;
+                            if ( popVal( percent, part ) ) {
+                                guint64 grayLevel = 0;
+                                if ( !popVal( grayLevel, part ) ) {
+                                    grayLevel = 0;
+                                }
+                                (*it)->_linkTone( *(onceMore->_colors[colorIndex]), percent, grayLevel );
+                            }
+                        }
+                    }
+
+                }
+            }
+        }
+    }
+}
+
+
+void ColorItem::_linkTint( ColorItem& other, int percent )
+{
+    if ( !_linkSrc )
+    {
+        other._listeners.push_back(this);
+        _linkIsTone = false;
+        _linkPercent = percent;
+        if ( _linkPercent > 100 )
+            _linkPercent = 100;
+        if ( _linkPercent < 0 )
+            _linkPercent = 0;
+        _linkGray = 0;
+        _linkSrc = &other;
+
+        ColorItem::_colorDefChanged(&other);
+    }
+}
 
-static void loadPaletteFile( gchar const *filename )
+void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
+{
+    if ( !_linkSrc )
+    {
+        other._listeners.push_back(this);
+        _linkIsTone = true;
+        _linkPercent = percent;
+        if ( _linkPercent > 100 )
+            _linkPercent = 100;
+        if ( _linkPercent < 0 )
+            _linkPercent = 0;
+        _linkGray = grayLevel;
+        _linkSrc = &other;
+
+        ColorItem::_colorDefChanged(&other);
+    }
+}
+
+
+void _loadPaletteFile( gchar const *filename )
 {
     char block[1024];
     FILE *f = Inkscape::IO::fopen_utf8name( filename, "r" );
@@ -312,7 +881,7 @@ static void loadPaletteFile( gchar const *filename )
                             while ( *ptr == ' ' || *ptr == '\t' ) {
                                 ptr++;
                             }
-                            if ( *ptr == 0 ) {
+                            if ( (*ptr == 0) || (*ptr == '\r') || (*ptr == '\n') ) {
                                 // blank line. skip it.
                             } else if ( '0' <= *ptr && *ptr <= '9' ) {
                                 // should be an entry link
@@ -390,6 +959,9 @@ static void loadPaletteFile( gchar const *filename )
                 } while ( result && !hasErr );
                 if ( !hasErr ) {
                     possible.push_back(onceMore);
+#if ENABLE_MAGIC_COLORS
+                    ColorItem::_wireMagicColors( onceMore );
+#endif // ENABLE_MAGIC_COLORS
                 } else {
                     delete onceMore;
                 }
@@ -409,12 +981,14 @@ static void loadEmUp()
         std::list<gchar *> sources;
         sources.push_back( profile_path("palettes") );
         sources.push_back( g_strdup(INKSCAPE_PALETTESDIR) );
+        sources.push_back( g_strdup(CREATE_PALETTESDIR) );
 
         // Use this loop to iterate through a list of possible document locations.
         while (!sources.empty()) {
             gchar *dirname = sources.front();
 
-            if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
+            if ( Inkscape::IO::file_test( dirname, G_FILE_TEST_EXISTS )
+                && Inkscape::IO::file_test( dirname, G_FILE_TEST_IS_DIR )) {
                 GError *err = 0;
                 GDir *directory = g_dir_open(dirname, 0, &err);
                 if (!directory) {
@@ -424,11 +998,15 @@ static void loadEmUp()
                 } else {
                     gchar *filename = 0;
                     while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
-                        gchar* full = g_build_filename(dirname, filename, NULL);
-                        if ( !Inkscape::IO::file_test( full, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
-                            loadPaletteFile(full);
-                        }
-                        g_free(full);
+                        gchar* lower = g_ascii_strdown( filename, -1 );
+//                        if ( g_str_has_suffix(lower, ".gpl") ) {
+                            gchar* full = g_build_filename(dirname, filename, NULL);
+                            if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
+                                _loadPaletteFile(full);
+                            }
+                            g_free(full);
+//                      }
+                        g_free(lower);
                     }
                     g_dir_close(directory);
                 }
@@ -451,27 +1029,41 @@ static void loadEmUp()
 
 SwatchesPanel& SwatchesPanel::getInstance()
 {
-    if ( !instance ) {
-        instance = new SwatchesPanel();
-    }
-
-    return *instance;
+    return *new SwatchesPanel();
 }
 
 
-
 /**
  * Constructor
  */
-SwatchesPanel::SwatchesPanel() :
-    Inkscape::UI::Widget::Panel ("dialogs.swatches"),
+SwatchesPanel::SwatchesPanel(gchar const* prefsPath) :
+    Inkscape::UI::Widget::Panel("", prefsPath, SP_VERB_DIALOG_SWATCHES, "", true),
     _holder(0)
 {
+    Gtk::RadioMenuItem* hotItem = 0;
     _holder = new PreviewHolder();
     loadEmUp();
 
     if ( !possible.empty() ) {
-        JustForNow* first = possible.front();
+        JustForNow* first = 0;
+        Glib::ustring targetName;
+        if ( _prefs_path ) {
+            Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+            targetName = prefs->getString(_prefs_path, "palette");
+            if (!targetName.empty()) {
+                for ( std::vector<JustForNow*>::iterator iter = possible.begin(); iter != possible.end(); ++iter ) {
+                    if ( (*iter)->_name == targetName ) {
+                        first = *iter;
+                        break;
+                    }
+                }
+            }
+        }
+
+        if ( !first ) {
+            first = possible.front();
+        }
+
         if ( first->_prefWidth > 0 ) {
             _holder->setColumnPref( first->_prefWidth );
         }
@@ -486,6 +1078,9 @@ SwatchesPanel::SwatchesPanel() :
         for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
             JustForNow* curr = *it;
             Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
+            if ( curr == first ) {
+                hotItem = single;
+            }
             _regItem( single, 3, i );
             i++;
         }
@@ -499,6 +1094,9 @@ SwatchesPanel::SwatchesPanel() :
     show_all_children();
 
     restorePanelPrefs();
+    if ( hotItem ) {
+        hotItem->set_active();
+    }
 }
 
 SwatchesPanel::~SwatchesPanel()
@@ -524,6 +1122,12 @@ void SwatchesPanel::_handleAction( int setId, int itemId )
             if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
                 _holder->clear();
                 JustForNow* curr = possible[itemId];
+
+                if ( _prefs_path ) {
+                    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+                    prefs->setString(_prefs_path, "palette", curr->_name);
+                }
+
                 if ( curr->_prefWidth > 0 ) {
                     _holder->setColumnPref( curr->_prefWidth );
                 }