Code

Refactored preferences handling into a new version of
[inkscape.git] / src / dialogs / swatches.cpp
index dc9b6fdd6db8ec0f24441ad2e736055bd4acaa82..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"
@@ -30,7 +37,7 @@
 #include "path-prefix.h"
 #include "swatches.h"
 #include "sp-item.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 
 #include "eek-preview.h"
 
@@ -38,8 +45,6 @@ namespace Inkscape {
 namespace UI {
 namespace Dialogs {
 
-SwatchesPanel* SwatchesPanel::instance = 0;
-
 
 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
     def( r, g, b, name ),
@@ -98,10 +103,10 @@ typedef enum {
 } colorFlavorType;
 
 static const GtkTargetEntry sourceColorEntries[] = {
-#if ENABLE_LCMS
+#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_LCMS
+#endif // ENABLE_MAGIC_COLORS
     {"application/x-color", 0, APP_X_COLOR},
     {"text/plain", 0, TEXT_DATA},
 };
@@ -113,6 +118,9 @@ void ColorItem::_dragGetColorData( GtkWidget *widget,
                                    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);
 
@@ -188,6 +196,7 @@ void ColorItem::_dragGetColorData( GtkWidget *widget,
 
 static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
 {
+    (void)widget;
     ColorItem* item = reinterpret_cast<ColorItem*>(data);
     if ( item )
     {
@@ -214,30 +223,120 @@ static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
 //     return TRUE;
 // }
 
-static void bouncy( GtkWidget* widget, gpointer callback_data ) {
+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);
     }
 }
 
+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_LCMS
+#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_LCMS
+#endif // ENABLE_MAGIC_COLORS
     {"application/x-color", 0, APP_X_COLOR},
 };
 
@@ -251,6 +350,11 @@ void ColorItem::_dropDataIn( GtkWidget *widget,
                              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:
@@ -310,7 +414,7 @@ static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::u
             SPObject *obj = document->getObjectByRepr( node );
 
             gchar c[64] = {0};
-            sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
+            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 );
 
@@ -325,7 +429,7 @@ static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::u
             SPObject *obj = document->getObjectByRepr( node );
 
             gchar c[64] = {0};
-            sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
+            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 );
 
@@ -417,7 +521,8 @@ void ColorItem::_colorDefChanged(void* data)
                         str = 0;
 
                         if ( bruteForce( document, rroot, paletteName, item->def.getR(), item->def.getG(), item->def.getB() ) ) {
-                            sp_document_done( document );
+                            sp_document_done( document , SP_VERB_DIALOG_SWATCHES, 
+                                              _("Change color definition"));
                         }
                     }
                 }
@@ -427,7 +532,7 @@ void ColorItem::_colorDefChanged(void* data)
 }
 
 
-Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::IconSize size)
+Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio)
 {
     Gtk::Widget* widget = 0;
     if ( style == PREVIEW_STYLE_BLURB ) {
@@ -435,10 +540,10 @@ Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::
         lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
         widget = lbl;
     } else {
-        Glib::ustring blank("          ");
-        if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
-            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);
@@ -446,7 +551,7 @@ Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::
 
         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)) );
@@ -477,14 +582,20 @@ Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::
 
         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()),
@@ -503,6 +614,16 @@ Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::
                           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),
@@ -544,14 +665,15 @@ void ColorItem::buttonClicked(bool secondary)
         char const * attrName = secondary ? "stroke" : "fill";
         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_desktop_document (desktop));
+        sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_SWATCHES, 
+                          secondary? _("Set stroke color from swatch") : _("Set fill color from swatch"));
     }
 }
 
@@ -594,12 +716,12 @@ bool parseNum( char*& str, int& val ) {
 static bool getBlock( std::string& dst, guchar ch, std::string const str )
 {
     bool good = false;
-    size_t pos = str.find(ch);
+    std::string::size_type pos = str.find(ch);
     if ( pos != std::string::npos )
     {
-        size_t pos2 = str.find( '(', pos );
+        std::string::size_type pos2 = str.find( '(', pos );
         if ( pos2 != std::string::npos ) {
-            size_t endPos = str.find( ')', pos2 );
+            std::string::size_type endPos = str.find( ')', pos2 );
             if ( endPos != std::string::npos ) {
                 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
                 good = true;
@@ -612,7 +734,7 @@ static bool getBlock( std::string& dst, guchar ch, std::string const str )
 static bool popVal( guint64& numVal, std::string& str )
 {
     bool good = false;
-    size_t endPos = str.find(',');
+    std::string::size_type endPos = str.find(',');
     if ( endPos == std::string::npos ) {
         endPos = str.length();
     }
@@ -642,11 +764,11 @@ void ColorItem::_wireMagicColors( void* p )
     {
         for ( std::vector<ColorItem*>::iterator it = onceMore->_colors.begin(); it != onceMore->_colors.end(); ++it )
         {
-            size_t pos = (*it)->def.descr.find("*{");
+            std::string::size_type pos = (*it)->def.descr.find("*{");
             if ( pos != std::string::npos )
             {
                 std::string subby = (*it)->def.descr.substr( pos + 2 );
-                size_t endPos = subby.find("}*");
+                std::string::size_type endPos = subby.find("}*");
                 if ( endPos != std::string::npos )
                 {
                     subby.erase( endPos );
@@ -759,7 +881,7 @@ 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
@@ -837,7 +959,9 @@ 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;
                 }
@@ -863,7 +987,8 @@ static void loadEmUp()
         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) {
@@ -876,7 +1001,7 @@ static void loadEmUp()
                         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, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
+                            if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
                                 _loadPaletteFile(full);
                             }
                             g_free(full);
@@ -904,20 +1029,15 @@ static void loadEmUp()
 
 SwatchesPanel& SwatchesPanel::getInstance()
 {
-    if ( !instance ) {
-        instance = new SwatchesPanel();
-    }
-
-    return *instance;
+    return *new SwatchesPanel();
 }
 
 
-
 /**
  * Constructor
  */
 SwatchesPanel::SwatchesPanel(gchar const* prefsPath) :
-    Inkscape::UI::Widget::Panel( Glib::ustring(), prefsPath, true ),
+    Inkscape::UI::Widget::Panel("", prefsPath, SP_VERB_DIALOG_SWATCHES, "", true),
     _holder(0)
 {
     Gtk::RadioMenuItem* hotItem = 0;
@@ -926,10 +1046,11 @@ SwatchesPanel::SwatchesPanel(gchar const* prefsPath) :
 
     if ( !possible.empty() ) {
         JustForNow* first = 0;
-        gchar const* targetName = 0;
+        Glib::ustring targetName;
         if ( _prefs_path ) {
-            targetName = prefs_get_string_attribute( _prefs_path, "palette" );
-            if ( targetName ) {
+            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;
@@ -1003,7 +1124,8 @@ void SwatchesPanel::_handleAction( int setId, int itemId )
                 JustForNow* curr = possible[itemId];
 
                 if ( _prefs_path ) {
-                    prefs_set_string_attribute( _prefs_path, "palette", curr->_name.c_str() );
+                    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+                    prefs->setString(_prefs_path, "palette", curr->_name);
                 }
 
                 if ( curr->_prefWidth > 0 ) {