index 8fb811e3d205448dc2693c8a5287e07e8d1843ce..755a10519c2e1f02219a3af6065b6e265991df0f 100644 (file)
+
/** @file
* @brief Color swatches dialog
*/
#include <glibmm/i18n.h>
#include <gdkmm/pixbuf.h>
+#include "color-item.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "desktop-style.h"
#include "document.h"
+#include "document-private.h"
#include "extension/db.h"
#include "inkscape.h"
#include "inkscape.h"
#include "path-prefix.h"
#include "preferences.h"
#include "sp-item.h"
-#include "svg/svg-color.h"
#include "sp-gradient-fns.h"
#include "sp-gradient.h"
#include "sp-gradient-vector.h"
#include "swatches.h"
+#include "style.h"
+#include "ui/previewholder.h"
+#include "widgets/desktop-widget.h"
+#include "widgets/gradient-vector.h"
#include "widgets/eek-preview.h"
+#include "display/nr-plain-stuff.h"
+#include "sp-gradient-reference.h"
+
namespace Inkscape {
namespace UI {
namespace Dialogs {
+#define VBLOCK 16
+#define PREVIEW_PIXBUF_WIDTH 128
-ColorItem::ColorItem(eek::ColorDef::ColorType type) :
- def(type),
- _isLive(false),
- _linkIsTone(false),
- _linkPercent(0),
- _linkGray(0),
- _linkSrc(0)
-{
-}
+void _loadPaletteFile( gchar const *filename );
-ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
- def( r, g, b, name ),
- _isLive(false),
- _linkIsTone(false),
- _linkPercent(0),
- _linkGray(0),
- _linkSrc(0)
-{
-}
-ColorItem::~ColorItem()
-{
-}
+class DocTrack;
+
+std::vector<SwatchPage*> possible;
+static std::map<SPDocument*, SwatchPage*> docPalettes;
+static std::vector<DocTrack*> docTrackings;
+static std::map<SwatchesPanel*, SPDocument*> docPerPanel;
-ColorItem::ColorItem(ColorItem const &other) :
- Inkscape::UI::Previewable()
+
+class SwatchesPanelHook : public SwatchesPanel
{
- if ( this != &other ) {
- *this = other;
+public:
+ static void convertGradient( GtkMenuItem *menuitem, gpointer userData );
+ static void addNewGradient( GtkMenuItem *menuitem, gpointer user_data );
+};
+
+static void handleClick( GtkWidget* /*widget*/, gpointer callback_data ) {
+ ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
+ if ( item ) {
+ item->buttonClicked(false);
}
}
-ColorItem &ColorItem::operator=(ColorItem const &other)
-{
- if ( this != &other ) {
- def = other.def;
-
- // TODO - correct linkage
- _linkSrc = other._linkSrc;
- g_message("Erk!");
+static void handleSecondaryClick( GtkWidget* /*widget*/, gint /*arg1*/, gpointer callback_data ) {
+ ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
+ if ( item ) {
+ item->buttonClicked(true);
}
- return *this;
}
+static GtkWidget* popupMenu = 0;
+static GtkWidget *popupSubHolder = 0;
+static GtkWidget *popupSub = 0;
+static std::vector<Glib::ustring> popupItems;
+static std::vector<GtkWidget*> popupExtras;
+static ColorItem* bounceTarget = 0;
+static SwatchesPanel* bouncePanel = 0;
-class JustForNow
+static void redirClick( GtkMenuItem *menuitem, gpointer /*user_data*/ )
{
-public:
- JustForNow() : _prefWidth(0) {}
-
- Glib::ustring _name;
- int _prefWidth;
- std::vector<ColorItem*> _colors;
-};
-
-static std::vector<JustForNow*> possible;
-
-
-static std::vector<std::string> mimeStrings;
-static std::map<std::string, guint> mimeToInt;
+ if ( bounceTarget ) {
+ handleClick( GTK_WIDGET(menuitem), bounceTarget );
+ }
+}
-void ColorItem::_dragGetColorData( GtkWidget */*widget*/,
- GdkDragContext */*drag_context*/,
- GtkSelectionData *data,
- guint info,
- guint /*time*/,
- gpointer user_data)
+static void redirSecondaryClick( GtkMenuItem *menuitem, gpointer /*user_data*/ )
{
- ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
- std::string key;
- if ( info < mimeStrings.size() ) {
- key = mimeStrings[info];
- } else {
- g_warning("ERROR: unknown value (%d)", info);
+ if ( bounceTarget ) {
+ handleSecondaryClick( GTK_WIDGET(menuitem), 0, bounceTarget );
}
+}
- 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;
- }
+static void editGradientImpl( SPGradient* gr )
+{
+ if ( gr ) {
+ GtkWidget *dialog = sp_gradient_vector_editor_new( gr );
+ gtk_widget_show( dialog );
}
}
-static void dragBegin( GtkWidget */*widget*/, GdkDragContext* dc, gpointer data )
+static void editGradient( GtkMenuItem */*menuitem*/, gpointer /*user_data*/ )
{
- ColorItem* item = reinterpret_cast<ColorItem*>(data);
- if ( item )
- {
- using Inkscape::IO::Resource::get_path;
- using Inkscape::IO::Resource::ICONS;
- using Inkscape::IO::Resource::SYSTEM;
-
- if (item->def.getType() != eek::ColorDef::RGB){
- GError *error = NULL;
- gsize bytesRead = 0;
- gsize bytesWritten = 0;
- gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
- -1,
- &bytesRead,
- &bytesWritten,
- &error);
- GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_scale(localFilename, 32, 24, FALSE, &error);
- g_free(localFilename);
- gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
- return;
+ if ( bounceTarget ) {
+ SwatchesPanel* swp = bouncePanel;
+ SPDesktop* desktop = swp ? swp->getDesktop() : 0;
+ SPDocument *doc = desktop ? desktop->doc() : 0;
+ if (doc) {
+ std::string targetName(bounceTarget->def.descr);
+ const GSList *gradients = sp_document_get_resource_list(doc, "gradient");
+ for (const GSList *item = gradients; item; item = item->next) {
+ SPGradient* grad = SP_GRADIENT(item->data);
+ if ( targetName == grad->getId() ) {
+ editGradientImpl( grad );
+ break;
+ }
+ }
}
-
- 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 );
}
-
}
-//"drag-drop"
-// gboolean dragDropColorData( GtkWidget *widget,
-// GdkDragContext *drag_context,
-// gint x,
-// gint y,
-// guint time,
-// gpointer user_data)
-// {
-// // TODO finish
+void SwatchesPanelHook::addNewGradient( GtkMenuItem */*menuitem*/, gpointer /*user_data*/ )
+{
+ if ( bounceTarget ) {
+ SwatchesPanel* swp = bouncePanel;
+ SPDesktop* desktop = swp ? swp->getDesktop() : 0;
+ SPDocument *doc = desktop ? desktop->doc() : 0;
+ if (doc) {
+ Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+ SPGradient * gr = 0;
+ {
+ Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient");
+ Inkscape::XML::Node *stop = xml_doc->createElement("svg:stop");
+ stop->setAttribute("offset", "0");
+ stop->setAttribute("style", "stop-color:#000;stop-opacity:1;");
+ repr->appendChild(stop);
+ Inkscape::GC::release(stop);
-// return TRUE;
-// }
+ SP_OBJECT_REPR( SP_DOCUMENT_DEFS(doc) )->addChild(repr, NULL);
-static void handleClick( GtkWidget* /*widget*/, gpointer callback_data ) {
- ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
- if ( item ) {
- item->buttonClicked(false);
- }
-}
+ gr = static_cast<SPGradient *>(doc->getObjectByRepr(repr));
+ Inkscape::GC::release(repr);
+ }
-static void handleSecondaryClick( GtkWidget* /*widget*/, gint /*arg1*/, gpointer callback_data ) {
- ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
- if ( item ) {
- item->buttonClicked(true);
- }
-}
+ gr->setSwatch();
-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);
+ editGradientImpl( gr );
}
}
- 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();
+void SwatchesPanelHook::convertGradient( GtkMenuItem * /*menuitem*/, gpointer userData )
+{
+ if ( bounceTarget ) {
+ SwatchesPanel* swp = bouncePanel;
+ SPDesktop* desktop = swp ? swp->getDesktop() : 0;
+ SPDocument *doc = desktop ? desktop->doc() : 0;
+ gint index = GPOINTER_TO_INT(userData);
+ if ( doc && (index >= 0) && (static_cast<guint>(index) < popupItems.size()) ) {
+ Glib::ustring targetName = popupItems[index];
+
+ const GSList *gradients = sp_document_get_resource_list(doc, "gradient");
+ for (const GSList *item = gradients; item; item = item->next) {
+ SPGradient* grad = SP_GRADIENT(item->data);
+ if ( targetName == grad->getId() ) {
+ grad->setSwatch();
+ sp_document_done(doc, SP_VERB_CONTEXT_GRADIENT,
+ _("Add gradient stop"));
+ break;
+ }
+ }
}
}
- return FALSE;
}
-static GtkWidget* popupMenu = 0;
-static ColorItem* bounceTarget = 0;
-
-static void redirClick( GtkMenuItem *menuitem, gpointer /*user_data*/ )
+static SwatchesPanel* findContainingPanel( GtkWidget *widget )
{
- if ( bounceTarget ) {
- handleClick( GTK_WIDGET(menuitem), bounceTarget );
+ SwatchesPanel *swp = 0;
+
+ std::map<GtkWidget*, SwatchesPanel*> rawObjects;
+ for (std::map<SwatchesPanel*, SPDocument*>::iterator it = docPerPanel.begin(); it != docPerPanel.end(); ++it) {
+ rawObjects[GTK_WIDGET(it->first->gobj())] = it->first;
+ }
+
+ for (GtkWidget* curr = widget; curr && !swp; curr = gtk_widget_get_parent(curr)) {
+ if (rawObjects.find(curr) != rawObjects.end()) {
+ swp = rawObjects[curr];
+ }
}
+
+ return swp;
}
-static void redirSecondaryClick( GtkMenuItem *menuitem, gpointer /*user_data*/ )
+static void removeit( GtkWidget *widget, gpointer data )
{
- if ( bounceTarget ) {
- handleSecondaryClick( GTK_WIDGET(menuitem), 0, bounceTarget );
- }
+ gtk_container_remove( GTK_CONTAINER(data), widget );
}
-static gboolean handleButtonPress( GtkWidget* /*widget*/, GdkEventButton* event, gpointer user_data)
+gboolean colorItemHandleButtonPress( GtkWidget* widget, GdkEventButton* event, gpointer user_data )
{
gboolean handled = FALSE;
if ( (event->button == 3) && (event->type == GDK_BUTTON_PRESS) ) {
+ SwatchesPanel* swp = findContainingPanel( widget );
+
if ( !popupMenu ) {
popupMenu = gtk_menu_new();
GtkWidget* child = 0;
@@ -265,369 +245,99 @@ static gboolean handleButtonPress( GtkWidget* /*widget*/, GdkEventButton* event,
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 );
-}
-
-#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*/)
-{
-}
-
-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;
- }
+ child = gtk_separator_menu_item_new();
+ gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+ popupExtras.push_back(child);
- Inkscape::XML::Node* first = node->firstChild();
- changed |= bruteForce( document, first, match, r, g, b );
+ child = gtk_menu_item_new_with_label(_("Add"));
+ g_signal_connect( G_OBJECT(child),
+ "activate",
+ G_CALLBACK(SwatchesPanelHook::addNewGradient),
+ user_data );
+ gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+ popupExtras.push_back(child);
- changed |= bruteForce( document, node->next(), match, r, g, b );
- }
+ child = gtk_menu_item_new_with_label(_("Delete"));
+ gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+ //popupExtras.push_back(child);
+ gtk_widget_set_sensitive( child, FALSE );
- return changed;
-}
+ child = gtk_menu_item_new_with_label(_("Edit..."));
+ g_signal_connect( G_OBJECT(child),
+ "activate",
+ G_CALLBACK(editGradient),
+ user_data );
+ gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+ popupExtras.push_back(child);
-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();
- }
- }
+ child = gtk_separator_menu_item_new();
+ gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+ popupExtras.push_back(child);
- 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;
+ child = gtk_menu_item_new_with_label(_("Convert"));
+ gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
+ //popupExtras.push_back(child);
+ //gtk_widget_set_sensitive( child, FALSE );
+ {
+ popupSubHolder = child;
+ popupSub = gtk_menu_new();
+ gtk_menu_item_set_submenu( GTK_MENU_ITEM(child), popupSub );
}
- (*it)->def.setRGB( r, g, b );
+ gtk_widget_show_all(popupMenu);
}
+ ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
+ if ( item ) {
+ bool show = swp && (swp->getSelectedIndex() == 0);
+ for ( std::vector<GtkWidget*>::iterator it = popupExtras.begin(); it != popupExtras.end(); ++ it) {
+ gtk_widget_set_sensitive(*it, show);
+ }
- // 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 {
+ bounceTarget = item;
+ bouncePanel = swp;
+ popupItems.clear();
+ if ( popupMenu ) {
+ gtk_container_foreach(GTK_CONTAINER(popupSub), removeit, popupSub);
+ bool processed = false;
+ GtkWidget *wdgt = gtk_widget_get_ancestor(widget, SP_TYPE_DESKTOP_WIDGET);
+ if ( wdgt ) {
+ SPDesktopWidget *dtw = SP_DESKTOP_WIDGET(wdgt);
+ if ( dtw && dtw->desktop ) {
+ // Pick up all gradients with vectors
+ const GSList *gradients = sp_document_get_resource_list(dtw->desktop->doc(), "gradient");
+ gint index = 0;
+ for (const GSList *curr = gradients; curr; curr = curr->next) {
+ SPGradient* grad = SP_GRADIENT(curr->data);
+ if ( grad->hasStops() && !grad->isSwatch() ) {
+ //gl = g_slist_prepend(gl, curr->data);
+ processed = true;
+ GtkWidget *child = gtk_menu_item_new_with_label(grad->getId());
+ gtk_menu_shell_append(GTK_MENU_SHELL(popupSub), child);
+
+ popupItems.push_back(grad->getId());
+ g_signal_connect( G_OBJECT(child),
+ "activate",
+ G_CALLBACK(SwatchesPanelHook::convertGradient),
+ GINT_TO_POINTER(index) );
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_show_all(popupSub);
}
}
- }
- }
- }
-}
-
+ gtk_widget_set_sensitive( popupSubHolder, processed );
-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(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 = " ";
-// }
-
- 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 ( def.getType() != eek::ColorDef::RGB ) {
- using Inkscape::IO::Resource::get_path;
- using Inkscape::IO::Resource::ICONS;
- using Inkscape::IO::Resource::SYSTEM;
- GError *error = NULL;
- gsize bytesRead = 0;
- gsize bytesWritten = 0;
- gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
- -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);
-
- 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)) );
-
- def.addCallback( _colorDefChanged, this );
-
- GValue val = {0, {{0}, {0}}};
- g_value_init( &val, G_TYPE_BOOLEAN );
- g_value_set_boolean( &val, FALSE );
- g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
-
-/*
- Gtk::Button *btn = new Gtk::Button(blank);
- Gdk::Color color;
- color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
- btn->modify_bg(Gtk::STATE_NORMAL, color);
- btn->modify_bg(Gtk::STATE_ACTIVE, color);
- btn->modify_bg(Gtk::STATE_PRELIGHT, color);
- btn->modify_bg(Gtk::STATE_SELECTED, color);
-
- Gtk::Widget* newBlot = btn;
-*/
-
- 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(handleClick),
- this);
-
- g_signal_connect( G_OBJECT(newBlot->gobj()),
- "alt-clicked",
- G_CALLBACK(handleSecondaryClick),
- this);
-
- g_signal_connect( G_OBJECT(newBlot->gobj()),
- "button-press-event",
- G_CALLBACK(handleButtonPress),
- this);
-
- {
- std::vector<std::string> listing = def.getMIMETypes();
- int entryCount = listing.size();
- GtkTargetEntry* entries = new GtkTargetEntry[entryCount];
- GtkTargetEntry* curr = entries;
- for ( std::vector<std::string>::iterator it = listing.begin(); it != listing.end(); ++it ) {
- curr->target = g_strdup(it->c_str());
- curr->flags = 0;
- if ( mimeToInt.find(*it) == mimeToInt.end() ){
- // these next lines are order-dependent:
- mimeToInt[*it] = mimeStrings.size();
- mimeStrings.push_back(*it);
- }
- 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);
+ gtk_menu_popup(GTK_MENU(popupMenu), NULL, NULL, NULL, NULL, event->button, event->time);
+ handled = TRUE;
}
- delete[] entries;
}
-
- g_signal_connect( G_OBJECT(newBlot->gobj()),
- "drag-data-get",
- 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()),
- "destroy",
- G_CALLBACK(dieDieDie),
- this);
-
-
- widget = newBlot;
}
- _previews.push_back( widget );
-
- return widget;
+ return handled;
}
-void ColorItem::buttonClicked(bool secondary)
-{
- SPDesktop *desktop = SP_ACTIVE_DESKTOP;
- if (desktop) {
- char const * attrName = secondary ? "stroke" : "fill";
-
- SPCSSAttr *css = sp_repr_css_attr_new();
- Glib::ustring descr;
- switch (def.getType()) {
- case eek::ColorDef::CLEAR: {
- // TODO actually make this clear
- sp_repr_css_set_property( css, attrName, "none" );
- descr = secondary? _("Remove stroke color") : _("Remove fill color");
- break;
- }
- case eek::ColorDef::NONE: {
- sp_repr_css_set_property( css, attrName, "none" );
- descr = secondary? _("Set stroke color to none") : _("Set fill color to none");
- break;
- }
- case eek::ColorDef::RGB: {
- gchar c[64];
- guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
- sp_svg_write_color(c, sizeof(c), rgba);
-
- sp_repr_css_set_property( css, attrName, c );
- descr = secondary? _("Set stroke color from swatch") : _("Set fill color from swatch");
- break;
- }
- }
- sp_desktop_set_style(desktop, css);
- sp_repr_css_attr_unref(css);
-
- sp_document_done( sp_desktop_document(desktop), SP_VERB_DIALOG_SWATCHES, descr.c_str() );
- }
-}
static char* trim( char* str ) {
char* ret = str;
}
-static bool getBlock( std::string& dst, guchar ch, std::string const str )
-{
- 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;
-}
-
-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();
- }
-
- 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);
- }
-}
-
-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];
bool inHeader = true;
bool hasErr = false;
- JustForNow *onceMore = new JustForNow();
+ SwatchPage *onceMore = new SwatchPage();
do {
result = fgets( block, sizeof(block), f );
_remove(0),
_currentIndex(0),
_currentDesktop(0),
- _currentDocument(0),
- _ptr(0)
+ _currentDocument(0)
{
Gtk::RadioMenuItem* hotItem = 0;
_holder = new PreviewHolder();
- _clear = new ColorItem( eek::ColorDef::CLEAR );
- _remove = new ColorItem( eek::ColorDef::NONE );
- {
- JustForNow *docPalette = new JustForNow();
+ _clear = new ColorItem( ege::PaintDef::CLEAR );
+ _remove = new ColorItem( ege::PaintDef::NONE );
+ if (docPalettes.empty()) {
+ SwatchPage *docPalette = new SwatchPage();
docPalette->_name = "Auto";
- possible.push_back(docPalette);
-
- _ptr = docPalette;
+ docPalettes[0] = docPalette;
}
+
loadEmUp();
if ( !possible.empty() ) {
- JustForNow* first = 0;
+ SwatchPage* first = 0;
int index = 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 (targetName == "Auto") {
+ first = docPalettes[0];
+ } else {
index++;
+ for ( std::vector<SwatchPage*>::iterator iter = possible.begin(); iter != possible.end(); ++iter ) {
+ if ( (*iter)->_name == targetName ) {
+ first = *iter;
+ break;
+ }
+ index++;
+ }
}
}
}
if ( !first ) {
- first = possible.front();
+ first = docPalettes[0];
_currentIndex = 0;
} else {
_currentIndex = index;
Gtk::RadioMenuItem::Group groupOne;
int i = 0;
- for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
- JustForNow* curr = *it;
+ std::vector<SwatchPage*> swatchSets = _getSwatchSets();
+ for ( std::vector<SwatchPage*>::iterator it = swatchSets.begin(); it != swatchSets.end(); it++ ) {
+ SwatchPage* curr = *it;
Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
if ( curr == first ) {
hotItem = single;
SwatchesPanel::~SwatchesPanel()
{
+ _trackDocument( this, 0 );
+
_documentConnection.disconnect();
- _resourceConnection.disconnect();
+ _selChanged.disconnect();
if ( _clear ) {
delete _clear;
if ( desktop != _currentDesktop ) {
if ( _currentDesktop ) {
_documentConnection.disconnect();
+ _selChanged.disconnect();
}
_currentDesktop = desktop;
if ( desktop ) {
- sigc::bound_mem_functor1<void, Inkscape::UI::Dialogs::SwatchesPanel, SPDocument*> first = sigc::mem_fun(*this, &SwatchesPanel::_setDocument);
+ _currentDesktop->selection->connectChanged(
+ sigc::hide(sigc::mem_fun(*this, &SwatchesPanel::_updateFromSelection)));
- sigc::slot<void, SPDocument*> base2 = first;
+ _currentDesktop->selection->connectModified(
+ sigc::hide(sigc::hide(sigc::mem_fun(*this, &SwatchesPanel::_updateFromSelection))));
+
+ _currentDesktop->connectToolSubselectionChanged(
+ sigc::hide(sigc::mem_fun(*this, &SwatchesPanel::_updateFromSelection)));
+ sigc::bound_mem_functor1<void, Inkscape::UI::Dialogs::SwatchesPanel, SPDocument*> first = sigc::mem_fun(*this, &SwatchesPanel::_setDocument);
+ sigc::slot<void, SPDocument*> base2 = first;
sigc::slot<void,SPDesktop*, SPDocument*> slot2 = sigc::hide<0>( base2 );
_documentConnection = desktop->connectDocumentReplaced( slot2 );
}
}
-void SwatchesPanel::_setDocument( SPDocument *document )
+
+class DocTrack
{
- if ( document != _currentDocument ) {
- if ( _currentDocument ) {
- _resourceConnection.disconnect();
+public:
+ DocTrack(SPDocument *doc, sigc::connection &gradientRsrcChanged, sigc::connection &defsChanged, sigc::connection &defsModified) :
+ doc(doc),
+ gradientRsrcChanged(gradientRsrcChanged),
+ defsChanged(defsChanged),
+ defsModified(defsModified)
+ {
+ }
+
+ ~DocTrack()
+ {
+ if (doc) {
+ gradientRsrcChanged.disconnect();
+ defsChanged.disconnect();
+ defsModified.disconnect();
}
- _currentDocument = document;
- if ( _currentDocument ) {
- _resourceConnection = sp_document_resources_changed_connect(document,
- "gradient",
- sigc::mem_fun(*this, &SwatchesPanel::_handleGradientsChange));
+ }
+
+ SPDocument *doc;
+ sigc::connection gradientRsrcChanged;
+ sigc::connection defsChanged;
+ sigc::connection defsModified;
+
+private:
+ DocTrack(DocTrack const &); // no copy
+ DocTrack &operator=(DocTrack const &); // no assign
+};
+
+void SwatchesPanel::_trackDocument( SwatchesPanel *panel, SPDocument *document )
+{
+ SPDocument *oldDoc = 0;
+ if (docPerPanel.find(panel) != docPerPanel.end()) {
+ oldDoc = docPerPanel[panel];
+ if (!oldDoc) {
+ docPerPanel.erase(panel); // Should not be needed, but clean up just in case.
+ }
+ }
+ if (oldDoc != document) {
+ if (oldDoc) {
+ docPerPanel[panel] = 0;
+ bool found = false;
+ for (std::map<SwatchesPanel*, SPDocument*>::iterator it = docPerPanel.begin(); (it != docPerPanel.end()) && !found; ++it) {
+ found = (it->second == document);
+ }
+ if (!found) {
+ for (std::vector<DocTrack*>::iterator it = docTrackings.begin(); it != docTrackings.end(); ++it){
+ if ((*it)->doc == oldDoc) {
+ delete *it;
+ docTrackings.erase(it);
+ break;
+ }
+ }
+ }
}
- _handleGradientsChange();
+ if (document) {
+ bool found = false;
+ for (std::map<SwatchesPanel*, SPDocument*>::iterator it = docPerPanel.begin(); (it != docPerPanel.end()) && !found; ++it) {
+ found = (it->second == document);
+ }
+ docPerPanel[panel] = document;
+ if (!found) {
+ sigc::connection conn1 = sp_document_resources_changed_connect( document, "gradient", sigc::bind(sigc::ptr_fun(&SwatchesPanel::handleGradientsChange), document) );
+ sigc::connection conn2 = SP_DOCUMENT_DEFS(document)->connectRelease( sigc::hide(sigc::bind(sigc::ptr_fun(&SwatchesPanel::handleDefsModified), document)) );
+ sigc::connection conn3 = SP_DOCUMENT_DEFS(document)->connectModified( sigc::hide(sigc::hide(sigc::bind(sigc::ptr_fun(&SwatchesPanel::handleDefsModified), document))) );
+
+ DocTrack *dt = new DocTrack(document, conn1, conn2, conn3);
+ docTrackings.push_back(dt);
+
+ if (docPalettes.find(document) == docPalettes.end()) {
+ SwatchPage *docPalette = new SwatchPage();
+ docPalette->_name = "Auto";
+ docPalettes[document] = docPalette;
+ }
+ }
+ }
+ }
+
+ std::set<SPDocument*> docs;
+ for (std::map<SwatchesPanel*, SPDocument*>::iterator it = docPerPanel.begin(); it != docPerPanel.end(); ++it) {
+ docs.insert(it->second);
+ }
+}
+
+void SwatchesPanel::_setDocument( SPDocument *document )
+{
+ if ( document != _currentDocument ) {
+ _trackDocument(this, document);
+ _currentDocument = document;
+ handleGradientsChange( document );
}
}
-void SwatchesPanel::_handleGradientsChange()
+static void recalcSwatchContents(SPDocument* doc,
+ std::vector<ColorItem*> &tmpColors,
+ std::map<ColorItem*, guchar*> &previewMappings,
+ std::map<ColorItem*, SPGradient*> &gradMappings)
{
std::vector<SPGradient*> newList;
- const GSList *gradients = sp_document_get_resource_list(_currentDocument, "gradient");
+ const GSList *gradients = sp_document_get_resource_list(doc, "gradient");
for (const GSList *item = gradients; item; item = item->next) {
SPGradient* grad = SP_GRADIENT(item->data);
- if ( grad->has_stops ) {
+ if ( grad->isSwatch() ) {
newList.push_back(SP_GRADIENT(item->data));
}
}
- if ( _ptr ) {
- JustForNow *docPalette = reinterpret_cast<JustForNow *>(_ptr);
- // TODO delete pointed to objects
- docPalette->_colors.clear();
- if ( !newList.empty() ) {
- for ( std::vector<SPGradient*>::iterator it = newList.begin(); it != newList.end(); ++it )
+ if ( !newList.empty() ) {
+ for ( std::vector<SPGradient*>::iterator it = newList.begin(); it != newList.end(); ++it )
+ {
+ SPGradient* grad = *it;
+ grad->ensureVector();
+ SPGradientStop first = grad->vector.stops[0];
+ SPColor color = first.color;
+ guint32 together = color.toRGBA32(first.opacity);
+
+ SPGradientStop second = (*it)->vector.stops[1];
+ SPColor color2 = second.color;
+
+ Glib::ustring name( grad->getId() );
+ unsigned int r = SP_RGBA32_R_U(together);
+ unsigned int g = SP_RGBA32_G_U(together);
+ unsigned int b = SP_RGBA32_B_U(together);
+ ColorItem* item = new ColorItem( r, g, b, name );
+
+ gint width = PREVIEW_PIXBUF_WIDTH;
+ gint height = VBLOCK;
+ guchar* px = g_new( guchar, 3 * height * width );
+ nr_render_checkerboard_rgb( px, width, height, 3 * width, 0, 0 );
+
+ sp_gradient_render_vector_block_rgb( grad,
+ px, width, height, 3 * width,
+ 0, width, TRUE );
+
+ previewMappings[item] = px;
+
+ tmpColors.push_back(item);
+ gradMappings[item] = grad;
+ }
+ }
+}
+
+void SwatchesPanel::handleGradientsChange(SPDocument *document)
+{
+ SwatchPage *docPalette = (docPalettes.find(document) != docPalettes.end()) ? docPalettes[document] : 0;
+ if (docPalette) {
+ std::vector<ColorItem*> tmpColors;
+ std::map<ColorItem*, guchar*> tmpPrevs;
+ std::map<ColorItem*, SPGradient*> tmpGrads;
+ recalcSwatchContents(document, tmpColors, tmpPrevs, tmpGrads);
+
+ for (std::map<ColorItem*, guchar*>::iterator it = tmpPrevs.begin(); it != tmpPrevs.end(); ++it) {
+ it->first->setPixData(it->second, PREVIEW_PIXBUF_WIDTH, VBLOCK);
+ }
+
+ for (std::map<ColorItem*, SPGradient*>::iterator it = tmpGrads.begin(); it != tmpGrads.end(); ++it) {
+ it->first->setGradient(it->second);
+ }
+
+ docPalette->_colors.swap(tmpColors);
+ for (std::vector<ColorItem*>::iterator it = tmpColors.begin(); it != tmpColors.end(); ++it) {
+ delete *it;
+ }
+
+
+ // Figure out which SwatchesPanel instances are affected and update them.
+
+ for (std::map<SwatchesPanel*, SPDocument*>::iterator it = docPerPanel.begin(); it != docPerPanel.end(); ++it) {
+ if (it->second == document) {
+ SwatchesPanel* swp = it->first;
+ std::vector<SwatchPage*> pages = swp->_getSwatchSets();
+ SwatchPage* curr = pages[swp->_currentIndex];
+ if (curr == docPalette) {
+ swp->_rebuild();
+ }
+ }
+ }
+ }
+}
+
+void SwatchesPanel::handleDefsModified(SPDocument *document)
+{
+ SwatchPage *docPalette = (docPalettes.find(document) != docPalettes.end()) ? docPalettes[document] : 0;
+ if (docPalette) {
+ std::vector<ColorItem*> tmpColors;
+ std::map<ColorItem*, guchar*> tmpPrevs;
+ std::map<ColorItem*, SPGradient*> tmpGrads;
+ recalcSwatchContents(document, tmpColors, tmpPrevs, tmpGrads);
+
+ if ( tmpColors.size() != docPalette->_colors.size() ) {
+ handleGradientsChange(document);
+ } else {
+ int cap = std::min(docPalette->_colors.size(), tmpColors.size());
+ for (int i = 0; i < cap; i++) {
+ ColorItem* newColor = tmpColors[i];
+ ColorItem* oldColor = docPalette->_colors[i];
+ if ( (newColor->def.getType() != oldColor->def.getType()) ||
+ (newColor->def.getR() != oldColor->def.getR()) ||
+ (newColor->def.getG() != oldColor->def.getG()) ||
+ (newColor->def.getB() != oldColor->def.getB()) ) {
+ oldColor->def.setRGB(newColor->def.getR(), newColor->def.getG(), newColor->def.getB());
+ }
+ if (tmpGrads.find(newColor) != tmpGrads.end()) {
+ oldColor->setGradient(tmpGrads[newColor]);
+ }
+ if ( tmpPrevs.find(newColor) != tmpPrevs.end() ) {
+ oldColor->setPixData(tmpPrevs[newColor], PREVIEW_PIXBUF_WIDTH, VBLOCK);
+ }
+ }
+ }
+ }
+}
+
+std::vector<SwatchPage*> SwatchesPanel::_getSwatchSets() const
+{
+ std::vector<SwatchPage*> tmp;
+ if (docPalettes.find(_currentDocument) != docPalettes.end()) {
+ tmp.push_back(docPalettes[_currentDocument]);
+ }
+
+ tmp.insert(tmp.end(), possible.begin(), possible.end());
+
+ return tmp;
+}
+
+void SwatchesPanel::_updateFromSelection()
+{
+ SwatchPage *docPalette = (docPalettes.find(_currentDocument) != docPalettes.end()) ? docPalettes[_currentDocument] : 0;
+ if ( docPalette ) {
+ Glib::ustring fillId;
+ Glib::ustring strokeId;
+
+ SPStyle *tmpStyle = sp_style_new( sp_desktop_document(_currentDesktop) );
+ int result = sp_desktop_query_style( _currentDesktop, tmpStyle, QUERY_STYLE_PROPERTY_FILL );
+ switch (result) {
+ case QUERY_STYLE_SINGLE:
+ case QUERY_STYLE_MULTIPLE_AVERAGED:
+ case QUERY_STYLE_MULTIPLE_SAME:
+ {
+ if (tmpStyle->fill.set && tmpStyle->fill.isPaintserver()) {
+ SPPaintServer* server = tmpStyle->getFillPaintServer();
+ if ( SP_IS_GRADIENT(server) ) {
+ SPGradient* target = 0;
+ SPGradient* grad = SP_GRADIENT(server);
+
+ if ( grad->isSwatch() ) {
+ target = grad;
+ } else if ( grad->ref ) {
+ SPGradient *tmp = grad->ref->getObject();
+ if ( tmp && tmp->isSwatch() ) {
+ target = tmp;
+ }
+ }
+ if ( target ) {
+ gchar const* id = target->repr->attribute("id");
+ if ( id ) {
+ fillId = id;
+ }
+ }
+ }
+ }
+ break;
+ }
+ }
+
+ result = sp_desktop_query_style( _currentDesktop, tmpStyle, QUERY_STYLE_PROPERTY_STROKE );
+ switch (result) {
+ case QUERY_STYLE_SINGLE:
+ case QUERY_STYLE_MULTIPLE_AVERAGED:
+ case QUERY_STYLE_MULTIPLE_SAME:
{
- if ( (*it)->vector.stops.size() == 2 ) {
- SPGradientStop first = (*it)->vector.stops[0];
- SPGradientStop second = (*it)->vector.stops[1];
- if ((first.offset <0.0001) && (static_cast<int>(second.offset * 100.0f) == 100)) {
- SPColor color = first.color;
- guint32 together = color.toRGBA32(0);
-
- // Pick only single-color gradients for now
- SPColor color2 = second.color;
- guint32 together2 = color2.toRGBA32(0);
- if ( together == together2 ) {
- Glib::ustring name((*it)->id);
- unsigned int r = SP_RGBA32_R_U(together);
- unsigned int g = SP_RGBA32_G_U(together);
- unsigned int b = SP_RGBA32_B_U(together);
- ColorItem* item = new ColorItem( r, g, b, name );
- docPalette->_colors.push_back(item);
+ if (tmpStyle->stroke.set && tmpStyle->stroke.isPaintserver()) {
+ SPPaintServer* server = tmpStyle->getStrokePaintServer();
+ if ( SP_IS_GRADIENT(server) ) {
+ SPGradient* target = 0;
+ SPGradient* grad = SP_GRADIENT(server);
+ if ( grad->isSwatch() ) {
+ target = grad;
+ } else if ( grad->ref ) {
+ SPGradient *tmp = grad->ref->getObject();
+ if ( tmp && tmp->isSwatch() ) {
+ target = tmp;
+ }
+ }
+ if ( target ) {
+ gchar const* id = target->repr->attribute("id");
+ if ( id ) {
+ strokeId = id;
+ }
}
}
}
+ break;
}
}
- JustForNow* curr = possible[_currentIndex];
- if (curr == docPalette) {
- _rebuild();
+ sp_style_unref(tmpStyle);
+
+ for ( std::vector<ColorItem*>::iterator it = docPalette->_colors.begin(); it != docPalette->_colors.end(); ++it ) {
+ ColorItem* item = *it;
+ bool isFill = (fillId == item->def.descr);
+ bool isStroke = (strokeId == item->def.descr);
+ item->setState( isFill, isStroke );
}
}
}
switch( setId ) {
case 3:
{
- if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
+ std::vector<SwatchPage*> pages = _getSwatchSets();
+ if ( itemId >= 0 && itemId < static_cast<int>(pages.size()) ) {
_currentIndex = itemId;
if ( !_prefs_path.empty() ) {
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
- prefs->setString(_prefs_path + "/palette", possible[_currentIndex]->_name);
+ prefs->setString(_prefs_path + "/palette", pages[_currentIndex]->_name);
}
_rebuild();
void SwatchesPanel::_rebuild()
{
- JustForNow* curr = possible[_currentIndex];
+ std::vector<SwatchPage*> pages = _getSwatchSets();
+ SwatchPage* curr = pages[_currentIndex];
_holder->clear();
if ( curr->_prefWidth > 0 ) {