index 5af23f95eecd302a420ab460241665a6a5df6807..4546efe132e9ef43298bb362646bc178e8611477 100644 (file)
--- a/src/dialogs/swatches.cpp
+++ b/src/dialogs/swatches.cpp
-/*
- * A simple panel for color swatches
- *
- * Authors:
+/** @file
+ * @brief Color swatches dialog
+ */
+/* 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
*/
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
#include <errno.h>
#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 "path-prefix.h"
#include "swatches.h"
#include "sp-item.h"
+#include "preferences.h"
#include "eek-preview.h"
namespace UI {
namespace Dialogs {
-SwatchesPanel* SwatchesPanel::instance = 0;
-
-
+ColorItem::ColorItem() : _isRemove(true){};
ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
def( r, g, b, name ),
+ _isRemove(false),
_isLive(false),
_linkIsTone(false),
_linkPercent(0),
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[] = {
-#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},
};
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);
static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
{
+ (void)widget;
ColorItem* item = reinterpret_cast<ColorItem*>(data);
if ( item )
{
+ if (item->isRemove()){
+ GError *error = NULL;
+ gchar *filepath = (gchar *) g_strdup_printf("%s/remove-color.png", INKSCAPE_PIXMAPDIR);
+ gsize bytesRead = 0;
+ gsize bytesWritten = 0;
+ gchar *localFilename = g_filename_from_utf8( filepath,
+ -1,
+ &bytesRead,
+ &bytesWritten,
+ &error);
+ GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_scale(localFilename, 32, 24, FALSE, &error);
+ g_free(localFilename);
+ g_free(filepath);
+ gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
+ return;
+ }
+
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))
@@ -213,30 +244,126 @@ 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 );
}
+//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 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},
};
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:
@@ -309,7 +441,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 );
@@ -324,7 +456,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 );
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"));
}
}
}
}
-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 ) {
+ if ( style == PREVIEW_STYLE_BLURB) {
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 == 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);
Gtk::Widget* newBlot = Glib::wrap(eekWidget);
eek_preview_set_color( preview, (def.getR() << 8) | def.getR(), (def.getG() << 8) | def.getG(), (def.getB() << 8) | def.getB());
+ if ( _isRemove ) {
+ GError *error = NULL;
+ gchar *filepath = (gchar *) g_strdup_printf("%s/remove-color.png", INKSCAPE_PIXMAPDIR);
+ gsize bytesRead = 0;
+ gsize bytesWritten = 0;
+ gchar *localFilename = g_filename_from_utf8( filepath,
+ -1,
+ &bytesRead,
+ &bytesWritten,
+ &error);
+ GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(localFilename, &error);
+ if (!pixbuf) {
+ g_warning("Null pixbuf for %p [%s]", localFilename, localFilename );
+ }
+ g_free(localFilename);
+ g_free(filepath);
- eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::GtkIconSize)size );
+ eek_preview_set_pixbuf( preview, pixbuf );
+ }
+
+ 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)) );
@@ -476,14 +628,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()),
@@ -502,6 +660,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),
@@ -539,24 +707,29 @@ Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::
void ColorItem::buttonClicked(bool secondary)
{
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
- if (desktop) {
- char const * attrName = secondary ? "stroke" : "fill";
+ if (!desktop) return;
+ char const * attrName = secondary ? "stroke" : "fill";
+
+ gchar c[64];
+ if (!_isRemove){
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);
+ SPCSSAttr *css = sp_repr_css_attr_new();
+ sp_repr_css_set_property( css, attrName, _isRemove ? "none" : c );
+ sp_desktop_set_style(desktop, css);
+ sp_repr_css_attr_unref(css);
- sp_repr_css_attr_unref(css);
- sp_document_done (sp_desktop_document (desktop));
+ if (_isRemove){
+ sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_SWATCHES,
+ secondary? _("Remove stroke color") : _("Remove fill color"));
+ } else {
+ sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_SWATCHES,
+ secondary? _("Set stroke color from swatch") : _("Set fill color from swatch"));
}
}
-
-
-
static char* trim( char* str ) {
char* ret = str;
while ( *str && (*str == ' ' || *str == '\t') ) {
str++;
}
str--;
- while ( str > ret && ( *str == ' ' || *str == '\t' ) || *str == '\r' || *str == '\n' ) {
+ while ( str > ret && (( *str == ' ' || *str == '\t' ) || *str == '\r' || *str == '\n') ) {
*str-- = 0;
}
return ret;
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;
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();
}
{
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 );
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
} while ( result && !hasErr );
if ( !hasErr ) {
possible.push_back(onceMore);
+#if ENABLE_MAGIC_COLORS
ColorItem::_wireMagicColors( onceMore );
+#endif // ENABLE_MAGIC_COLORS
} else {
delete onceMore;
}
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) {
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);
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();
+ _remove = new ColorItem();
loadEmUp();
-
if ( !possible.empty() ) {
- JustForNow* first = possible.front();
+ JustForNow* first = 0;
+ Glib::ustring targetName;
+ if ( !_prefs_path.empty() ) {
+ 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 );
}
_holder->freezeUpdates();
+ _holder->addPreview(_remove);
for ( std::vector<ColorItem*>::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) {
_holder->addPreview(*it);
}
_holder->thawUpdates();
Gtk::RadioMenuItem::Group groupOne;
+
int i = 0;
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++;
}
-
}
show_all_children();
restorePanelPrefs();
+ if ( hotItem ) {
+ hotItem->set_active();
+ }
}
SwatchesPanel::~SwatchesPanel()
{
+ if (_remove) delete _remove;
+ if (_holder) delete _holder;
}
void SwatchesPanel::setOrientation( Gtk::AnchorType how )
if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
_holder->clear();
JustForNow* curr = possible[itemId];
+
+ if ( !_prefs_path.empty() ) {
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ prefs->setString(_prefs_path + "/palette", curr->_name);
+ }
+
if ( curr->_prefWidth > 0 ) {
_holder->setColumnPref( curr->_prefWidth );
}
_holder->freezeUpdates();
+ _holder->addPreview(_remove);
for ( std::vector<ColorItem*>::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) {
_holder->addPreview(*it);
}