Code

8eed4e4ac54ce55ed8212a40b277288aba06ee1b
[inkscape.git] / src / ui / dialog / color-item.cpp
1 /** @file
2  * @brief Inkscape color swatch UI item.
3  */
4 /* Authors:
5  *   Jon A. Cruz
6  *
7  * Copyright (C) 2010 Jon A. Cruz
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #include <errno.h>
13 #include <glibmm/i18n.h>
14 #include <gtkmm/label.h>
16 #include "color-item.h"
18 #include "desktop.h"
19 #include "desktop-handles.h"
20 #include "desktop-style.h"
21 #include "display/nr-plain-stuff.h"
22 #include "document.h"
23 #include "inkscape.h" // for SP_ACTIVE_DESKTOP
24 #include "io/resource.h"
25 #include "io/sys.h"
26 #include "message-context.h"
27 #include "sp-gradient.h"
28 #include "sp-item.h"
29 #include "svg/svg-color.h"
30 #include "xml/node.h"
31 #include "xml/repr.h"
33 #include "color.h" // for SP_RGBA32_U_COMPOSE
36 namespace Inkscape {
37 namespace UI {
38 namespace Dialogs {
40 static std::vector<std::string> mimeStrings;
41 static std::map<std::string, guint> mimeToInt;
44 #if ENABLE_MAGIC_COLORS
45 // TODO remove this soon:
46 extern std::vector<SwatchPage*> possible;
47 #endif // ENABLE_MAGIC_COLORS
50 #if ENABLE_MAGIC_COLORS
51 static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::ustring const& match, int r, int g, int b )
52 {
53     bool changed = false;
55     if ( node ) {
56         gchar const * val = node->attribute("inkscape:x-fill-tag");
57         if ( val  && (match == val) ) {
58             SPObject *obj = document->getObjectByRepr( node );
60             gchar c[64] = {0};
61             sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
62             SPCSSAttr *css = sp_repr_css_attr_new();
63             sp_repr_css_set_property( css, "fill", c );
65             sp_desktop_apply_css_recursive( obj, css, true );
66             static_cast<SPItem*>(obj)->updateRepr();
68             changed = true;
69         }
71         val = node->attribute("inkscape:x-stroke-tag");
72         if ( val  && (match == val) ) {
73             SPObject *obj = document->getObjectByRepr( node );
75             gchar c[64] = {0};
76             sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
77             SPCSSAttr *css = sp_repr_css_attr_new();
78             sp_repr_css_set_property( css, "stroke", c );
80             sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
81             ((SPItem*)obj)->updateRepr();
83             changed = true;
84         }
86         Inkscape::XML::Node* first = node->firstChild();
87         changed |= bruteForce( document, first, match, r, g, b );
89         changed |= bruteForce( document, node->next(), match, r, g, b );
90     }
92     return changed;
93 }
94 #endif // ENABLE_MAGIC_COLORS
96 static void handleClick( GtkWidget* /*widget*/, gpointer callback_data ) {
97     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
98     if ( item ) {
99         item->buttonClicked(false);
100     }
103 static void handleSecondaryClick( GtkWidget* /*widget*/, gint /*arg1*/, gpointer callback_data ) {
104     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
105     if ( item ) {
106         item->buttonClicked(true);
107     }
110 static gboolean handleEnterNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
111     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
112     if ( item ) {
113         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
114         if ( desktop ) {
115             gchar* msg = g_strdup_printf(_("Color: <b>%s</b>; <b>Click</b> to set fill, <b>Shift+click</b> to set stroke"),
116                                          item->def.descr.c_str());
117             desktop->tipsMessageContext()->set(Inkscape::INFORMATION_MESSAGE, msg);
118             g_free(msg);
119         }
120     }
121     return FALSE;
124 static gboolean handleLeaveNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
125     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
126     if ( item ) {
127         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
128         if ( desktop ) {
129             desktop->tipsMessageContext()->clear();
130         }
131     }
132     return FALSE;
135 static void dieDieDie( GtkObject *obj, gpointer user_data )
137     g_message("die die die %p  %p", obj, user_data );
140 static bool getBlock( std::string& dst, guchar ch, std::string const str )
142     bool good = false;
143     std::string::size_type pos = str.find(ch);
144     if ( pos != std::string::npos )
145     {
146         std::string::size_type pos2 = str.find( '(', pos );
147         if ( pos2 != std::string::npos ) {
148             std::string::size_type endPos = str.find( ')', pos2 );
149             if ( endPos != std::string::npos ) {
150                 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
151                 good = true;
152             }
153         }
154     }
155     return good;
158 static bool popVal( guint64& numVal, std::string& str )
160     bool good = false;
161     std::string::size_type endPos = str.find(',');
162     if ( endPos == std::string::npos ) {
163         endPos = str.length();
164     }
166     if ( endPos != std::string::npos && endPos > 0 ) {
167         std::string xxx = str.substr( 0, endPos );
168         const gchar* ptr = xxx.c_str();
169         gchar* endPtr = 0;
170         numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
171         if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
172             // overflow
173         } else if ( (numVal == 0) && (endPtr == ptr) ) {
174             // failed conversion
175         } else {
176             good = true;
177             str.erase( 0, endPos + 1 );
178         }
179     }
181     return good;
184 // TODO resolve this more cleanly:
185 extern gboolean colorItemHandleButtonPress( GtkWidget* /*widget*/, GdkEventButton* event, gpointer user_data);
187 static void colorItemDragBegin( GtkWidget */*widget*/, GdkDragContext* dc, gpointer data )
189     ColorItem* item = reinterpret_cast<ColorItem*>(data);
190     if ( item )
191     {
192         using Inkscape::IO::Resource::get_path;
193         using Inkscape::IO::Resource::ICONS;
194         using Inkscape::IO::Resource::SYSTEM;
195         int width = 32;
196         int height = 24;
198         if (item->def.getType() != ege::PaintDef::RGB){
199             GError *error = NULL;
200             gsize bytesRead = 0;
201             gsize bytesWritten = 0;
202             gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
203                                                  -1,
204                                                  &bytesRead,
205                                                  &bytesWritten,
206                                                  &error);
207             GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_scale(localFilename, width, height, FALSE, &error);
208             g_free(localFilename);
209             gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
210         } else {
211             GdkPixbuf* pixbuf = 0;
212             if ( item->getGradient() ){
213                 guchar* px = g_new( guchar, 3 * height * width );
214                 nr_render_checkerboard_rgb( px, width, height, 3 * width, 0, 0 );
216                 sp_gradient_render_vector_block_rgb( item->getGradient(),
217                                                      px, width, height, 3 * width,
218                                                      0, width, TRUE );
220                 pixbuf = gdk_pixbuf_new_from_data( px, GDK_COLORSPACE_RGB, FALSE, 8,
221                                                    width, height, width * 3,
222                                                    0, // add delete function
223                                                    0 );
224             } else {
225                 Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, width, height );
226                 guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
227                     | (0x00ff0000 & (item->def.getG() << 16))
228                     | (0x0000ff00 & (item->def.getB() <<  8));
229                 thumb->fill( fillWith );
230                 pixbuf = thumb->gobj();
231                 g_object_ref(G_OBJECT(pixbuf));
232             }
233             gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
234         }
235     }
239 //"drag-drop"
240 // gboolean dragDropColorData( GtkWidget *widget,
241 //                             GdkDragContext *drag_context,
242 //                             gint x,
243 //                             gint y,
244 //                             guint time,
245 //                             gpointer user_data)
246 // {
247 // // TODO finish
249 //     return TRUE;
250 // }
253 SwatchPage::SwatchPage() :
254     _name(),
255     _prefWidth(0),
256     _colors()
260 SwatchPage::~SwatchPage()
265 ColorItem::ColorItem(ege::PaintDef::ColorType type) :
266     Previewable(),
267     def(type),
268     tips(),
269     _previews(),
270     _isFill(false),
271     _isStroke(false),
272     _isLive(false),
273     _linkIsTone(false),
274     _linkPercent(0),
275     _linkGray(0),
276     _linkSrc(0),
277     _grad(0),
278     _pixData(0),
279     _pixWidth(0),
280     _pixHeight(0),
281     _listeners()
285 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
286     Previewable(),
287     def( r, g, b, name ),
288     tips(),
289     _previews(),
290     _isFill(false),
291     _isStroke(false),
292     _isLive(false),
293     _linkIsTone(false),
294     _linkPercent(0),
295     _linkGray(0),
296     _linkSrc(0),
297     _grad(0),
298     _pixData(0),
299     _pixWidth(0),
300     _pixHeight(0),
301     _listeners()
305 ColorItem::~ColorItem()
309 ColorItem::ColorItem(ColorItem const &other) :
310     Inkscape::UI::Previewable()
312     if ( this != &other ) {
313         *this = other;
314     }
317 ColorItem &ColorItem::operator=(ColorItem const &other)
319     if ( this != &other ) {
320         def = other.def;
322         // TODO - correct linkage
323         _linkSrc = other._linkSrc;
324         g_message("Erk!");
325     }
326     return *this;
329 void ColorItem::setState( bool fill, bool stroke )
331     if ( (_isFill != fill) || (_isStroke != stroke) ) {
332         _isFill = fill;
333         _isStroke = stroke;
335         for ( std::vector<Gtk::Widget*>::iterator it = _previews.begin(); it != _previews.end(); ++it ) {
336             Gtk::Widget* widget = *it;
337             if ( IS_EEK_PREVIEW(widget->gobj()) ) {
338                 EekPreview * preview = EEK_PREVIEW(widget->gobj());
340                 int val = eek_preview_get_linked( preview );
341                 val &= ~(PREVIEW_FILL | PREVIEW_STROKE);
342                 if ( _isFill ) {
343                     val |= PREVIEW_FILL;
344                 }
345                 if ( _isStroke ) {
346                     val |= PREVIEW_STROKE;
347                 }
348                 eek_preview_set_linked( preview, static_cast<LinkType>(val) );
349             }
350         }
351     }
354 void ColorItem::setGradient(SPGradient *grad)
356     if (_grad != grad) {
357         _grad = grad;
358         // TODO regen and push to listeners
359     }
362 void ColorItem::setPixData(guchar* px, int width, int height)
364     if (px != _pixData) {
365         if (_pixData) {
366             g_free(_pixData);
367         }
368         _pixData = px;
369         _pixWidth = width;
370         _pixHeight = height;
372         _updatePreviews();
373     }
376 void ColorItem::_dragGetColorData( GtkWidget */*widget*/,
377                                    GdkDragContext */*drag_context*/,
378                                    GtkSelectionData *data,
379                                    guint info,
380                                    guint /*time*/,
381                                    gpointer user_data)
383     ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
384     std::string key;
385     if ( info < mimeStrings.size() ) {
386         key = mimeStrings[info];
387     } else {
388         g_warning("ERROR: unknown value (%d)", info);
389     }
391     if ( !key.empty() ) {
392         char* tmp = 0;
393         int len = 0;
394         int format = 0;
395         item->def.getMIMEData(key, tmp, len, format);
396         if ( tmp ) {
397             GdkAtom dataAtom = gdk_atom_intern( key.c_str(), FALSE );
398             gtk_selection_data_set( data, dataAtom, format, (guchar*)tmp, len );
399             delete[] tmp;
400         }
401     }
404 void ColorItem::_dropDataIn( GtkWidget */*widget*/,
405                              GdkDragContext */*drag_context*/,
406                              gint /*x*/, gint /*y*/,
407                              GtkSelectionData */*data*/,
408                              guint /*info*/,
409                              guint /*event_time*/,
410                              gpointer /*user_data*/)
414 void ColorItem::_colorDefChanged(void* data)
416     ColorItem* item = reinterpret_cast<ColorItem*>(data);
417     if ( item ) {
418         item->_updatePreviews();
419     }
422 void ColorItem::_updatePreviews()
424     for ( std::vector<Gtk::Widget*>::iterator it =  _previews.begin(); it != _previews.end(); ++it ) {
425         Gtk::Widget* widget = *it;
426         if ( IS_EEK_PREVIEW(widget->gobj()) ) {
427             EekPreview * preview = EEK_PREVIEW(widget->gobj());
429             _regenPreview(preview);
431             widget->queue_draw();
432         }
433     }
435     for ( std::vector<ColorItem*>::iterator it = _listeners.begin(); it != _listeners.end(); ++it ) {
436         guint r = def.getR();
437         guint g = def.getG();
438         guint b = def.getB();
440         if ( (*it)->_linkIsTone ) {
441             r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
442             g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
443             b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
444         } else {
445             r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
446             g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
447             b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
448         }
450         (*it)->def.setRGB( r, g, b );
451     }
454 #if ENABLE_MAGIC_COLORS
455     // Look for objects using this color
456     {
457         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
458         if ( desktop ) {
459             SPDocument* document = sp_desktop_document( desktop );
460             Inkscape::XML::Node *rroot =  sp_document_repr_root( document );
461             if ( rroot ) {
463                 // Find where this thing came from
464                 Glib::ustring paletteName;
465                 bool found = false;
466                 int index = 0;
467                 for ( std::vector<SwatchPage*>::iterator it2 = possible.begin(); it2 != possible.end() && !found; ++it2 ) {
468                     SwatchPage* curr = *it2;
469                     index = 0;
470                     for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
471                         if ( this == *zz ) {
472                             found = true;
473                             paletteName = curr->_name;
474                             break;
475                         } else {
476                             index++;
477                         }
478                     }
479                 }
481                 if ( !paletteName.empty() ) {
482                     gchar* str = g_strdup_printf("%d|", index);
483                     paletteName.insert( 0, str );
484                     g_free(str);
485                     str = 0;
487                     if ( bruteForce( document, rroot, paletteName, def.getR(), def.getG(), def.getB() ) ) {
488                         sp_document_done( document , SP_VERB_DIALOG_SWATCHES,
489                                           _("Change color definition"));
490                     }
491                 }
492             }
493         }
494     }
495 #endif // ENABLE_MAGIC_COLORS
499 void ColorItem::_regenPreview(EekPreview * preview)
501     if ( def.getType() != ege::PaintDef::RGB ) {
502         using Inkscape::IO::Resource::get_path;
503         using Inkscape::IO::Resource::ICONS;
504         using Inkscape::IO::Resource::SYSTEM;
505         GError *error = NULL;
506         gsize bytesRead = 0;
507         gsize bytesWritten = 0;
508         gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
509                                                      -1,
510                                                      &bytesRead,
511                                                      &bytesWritten,
512                                                      &error);
513         GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(localFilename, &error);
514         if (!pixbuf) {
515             g_warning("Null pixbuf for %p [%s]", localFilename, localFilename );
516         }
517         g_free(localFilename);
519         eek_preview_set_pixbuf( preview, pixbuf );
520     }
521     else if ( !_pixData ){
522         eek_preview_set_color( preview,
523                                (def.getR() << 8) | def.getR(),
524                                (def.getG() << 8) | def.getG(),
525                                (def.getB() << 8) | def.getB() );
526     } else {
527         GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( _pixData, GDK_COLORSPACE_RGB, FALSE, 8,
528                                                       _pixWidth, _pixHeight, _pixWidth * 3,
529                                                       0, // add delete function
530                                                       0 );
531         eek_preview_set_pixbuf( preview, pixbuf );
532     }
534     eek_preview_set_linked( preview, (LinkType)((_linkSrc ? PREVIEW_LINK_IN:0)
535                                                 | (_listeners.empty() ? 0:PREVIEW_LINK_OUT)
536                                                 | (_isLive ? PREVIEW_LINK_OTHER:0)) );
539 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio)
541     Gtk::Widget* widget = 0;
542     if ( style == PREVIEW_STYLE_BLURB) {
543         Gtk::Label *lbl = new Gtk::Label(def.descr);
544         lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
545         widget = lbl;
546     } else {
547 //         Glib::ustring blank("          ");
548 //         if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
549 //             blank = " ";
550 //         }
552         GtkWidget* eekWidget = eek_preview_new();
553         EekPreview * preview = EEK_PREVIEW(eekWidget);
554         Gtk::Widget* newBlot = Glib::wrap(eekWidget);
556         _regenPreview(preview);
558         eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::PreviewSize)size, ratio );
560         def.addCallback( _colorDefChanged, this );
562         GValue val = {0, {{0}, {0}}};
563         g_value_init( &val, G_TYPE_BOOLEAN );
564         g_value_set_boolean( &val, FALSE );
565         g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
567 /*
568         Gtk::Button *btn = new Gtk::Button(blank);
569         Gdk::Color color;
570         color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
571         btn->modify_bg(Gtk::STATE_NORMAL, color);
572         btn->modify_bg(Gtk::STATE_ACTIVE, color);
573         btn->modify_bg(Gtk::STATE_PRELIGHT, color);
574         btn->modify_bg(Gtk::STATE_SELECTED, color);
576         Gtk::Widget* newBlot = btn;
577 */
579         tips.set_tip((*newBlot), def.descr);
581 /*
582         newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
584         sigc::signal<void> type_signal_something;
585 */
587         g_signal_connect( G_OBJECT(newBlot->gobj()),
588                           "clicked",
589                           G_CALLBACK(handleClick),
590                           this);
592         g_signal_connect( G_OBJECT(newBlot->gobj()),
593                           "alt-clicked",
594                           G_CALLBACK(handleSecondaryClick),
595                           this);
597         g_signal_connect( G_OBJECT(newBlot->gobj()),
598                           "button-press-event",
599                           G_CALLBACK(colorItemHandleButtonPress),
600                           this);
602         {
603             std::vector<std::string> listing = def.getMIMETypes();
604             int entryCount = listing.size();
605             GtkTargetEntry* entries = new GtkTargetEntry[entryCount];
606             GtkTargetEntry* curr = entries;
607             for ( std::vector<std::string>::iterator it = listing.begin(); it != listing.end(); ++it ) {
608                 curr->target = g_strdup(it->c_str());
609                 curr->flags = 0;
610                 if ( mimeToInt.find(*it) == mimeToInt.end() ){
611                     // these next lines are order-dependent:
612                     mimeToInt[*it] = mimeStrings.size();
613                     mimeStrings.push_back(*it);
614                 }
615                 curr->info = mimeToInt[curr->target];
616                 curr++;
617             }
618             gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
619                                  GDK_BUTTON1_MASK,
620                                  entries, entryCount,
621                                  GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
622             for ( int i = 0; i < entryCount; i++ ) {
623                 g_free(entries[i].target);
624             }
625             delete[] entries;
626         }
628         g_signal_connect( G_OBJECT(newBlot->gobj()),
629                           "drag-data-get",
630                           G_CALLBACK(ColorItem::_dragGetColorData),
631                           this);
633         g_signal_connect( G_OBJECT(newBlot->gobj()),
634                           "drag-begin",
635                           G_CALLBACK(colorItemDragBegin),
636                           this );
638         g_signal_connect( G_OBJECT(newBlot->gobj()),
639                           "enter-notify-event",
640                           G_CALLBACK(handleEnterNotify),
641                           this);
643         g_signal_connect( G_OBJECT(newBlot->gobj()),
644                           "leave-notify-event",
645                           G_CALLBACK(handleLeaveNotify),
646                           this);
648 //         g_signal_connect( G_OBJECT(newBlot->gobj()),
649 //                           "drag-drop",
650 //                           G_CALLBACK(dragDropColorData),
651 //                           this);
653         if ( def.isEditable() )
654         {
655 //             gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
656 //                                GTK_DEST_DEFAULT_ALL,
657 //                                destColorTargets,
658 //                                G_N_ELEMENTS(destColorTargets),
659 //                                GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
662 //             g_signal_connect( G_OBJECT(newBlot->gobj()),
663 //                               "drag-data-received",
664 //                               G_CALLBACK(_dropDataIn),
665 //                               this );
666         }
668         g_signal_connect( G_OBJECT(newBlot->gobj()),
669                           "destroy",
670                           G_CALLBACK(dieDieDie),
671                           this);
674         widget = newBlot;
675     }
677     _previews.push_back( widget );
679     return widget;
682 void ColorItem::buttonClicked(bool secondary)
684     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
685     if (desktop) {
686         char const * attrName = secondary ? "stroke" : "fill";
688         SPCSSAttr *css = sp_repr_css_attr_new();
689         Glib::ustring descr;
690         switch (def.getType()) {
691             case ege::PaintDef::CLEAR: {
692                 // TODO actually make this clear
693                 sp_repr_css_set_property( css, attrName, "none" );
694                 descr = secondary? _("Remove stroke color") : _("Remove fill color");
695                 break;
696             }
697             case ege::PaintDef::NONE: {
698                 sp_repr_css_set_property( css, attrName, "none" );
699                 descr = secondary? _("Set stroke color to none") : _("Set fill color to none");
700                 break;
701             }
702             case ege::PaintDef::RGB: {
703                 Glib::ustring colorspec;
704                 if ( _grad ){
705                     colorspec = "url(#";
706                     colorspec += _grad->getId();
707                     colorspec += ")";
708                 } else {
709                     gchar c[64];
710                     guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
711                     sp_svg_write_color(c, sizeof(c), rgba);
712                     colorspec = c;
713                 }
714                 sp_repr_css_set_property( css, attrName, colorspec.c_str() );
715                 descr = secondary? _("Set stroke color from swatch") : _("Set fill color from swatch");
716                 break;
717             }
718         }
719         sp_desktop_set_style(desktop, css);
720         sp_repr_css_attr_unref(css);
722         sp_document_done( sp_desktop_document(desktop), SP_VERB_DIALOG_SWATCHES, descr.c_str() );
723     }
726 void ColorItem::_wireMagicColors( SwatchPage *colorSet )
728     if ( colorSet )
729     {
730         for ( std::vector<ColorItem*>::iterator it = colorSet->_colors.begin(); it != colorSet->_colors.end(); ++it )
731         {
732             std::string::size_type pos = (*it)->def.descr.find("*{");
733             if ( pos != std::string::npos )
734             {
735                 std::string subby = (*it)->def.descr.substr( pos + 2 );
736                 std::string::size_type endPos = subby.find("}*");
737                 if ( endPos != std::string::npos )
738                 {
739                     subby.erase( endPos );
740                     //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
741                     //g_message("               '%s'", subby.c_str());
743                     if ( subby.find('E') != std::string::npos )
744                     {
745                         (*it)->def.setEditable( true );
746                     }
748                     if ( subby.find('L') != std::string::npos )
749                     {
750                         (*it)->_isLive = true;
751                     }
753                     std::string part;
754                     // Tint. index + 1 more val.
755                     if ( getBlock( part, 'T', subby ) ) {
756                         guint64 colorIndex = 0;
757                         if ( popVal( colorIndex, part ) ) {
758                             guint64 percent = 0;
759                             if ( popVal( percent, part ) ) {
760                                 (*it)->_linkTint( *(colorSet->_colors[colorIndex]), percent );
761                             }
762                         }
763                     }
765                     // Shade/tone. index + 1 or 2 more val.
766                     if ( getBlock( part, 'S', subby ) ) {
767                         guint64 colorIndex = 0;
768                         if ( popVal( colorIndex, part ) ) {
769                             guint64 percent = 0;
770                             if ( popVal( percent, part ) ) {
771                                 guint64 grayLevel = 0;
772                                 if ( !popVal( grayLevel, part ) ) {
773                                     grayLevel = 0;
774                                 }
775                                 (*it)->_linkTone( *(colorSet->_colors[colorIndex]), percent, grayLevel );
776                             }
777                         }
778                     }
780                 }
781             }
782         }
783     }
787 void ColorItem::_linkTint( ColorItem& other, int percent )
789     if ( !_linkSrc )
790     {
791         other._listeners.push_back(this);
792         _linkIsTone = false;
793         _linkPercent = percent;
794         if ( _linkPercent > 100 )
795             _linkPercent = 100;
796         if ( _linkPercent < 0 )
797             _linkPercent = 0;
798         _linkGray = 0;
799         _linkSrc = &other;
801         ColorItem::_colorDefChanged(&other);
802     }
805 void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
807     if ( !_linkSrc )
808     {
809         other._listeners.push_back(this);
810         _linkIsTone = true;
811         _linkPercent = percent;
812         if ( _linkPercent > 100 )
813             _linkPercent = 100;
814         if ( _linkPercent < 0 )
815             _linkPercent = 0;
816         _linkGray = grayLevel;
817         _linkSrc = &other;
819         ColorItem::_colorDefChanged(&other);
820     }
823 } // namespace Dialogs
824 } // namespace UI
825 } // namespace Inkscape
827 /*
828   Local Variables:
829   mode:c++
830   c-file-style:"stroustrup"
831   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
832   indent-tabs-mode:nil
833   fill-column:99
834   End:
835 */
836 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :