Code

Merge and cleanup of GSoC C++-ification project.
[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  *   Abhishek Sharma
7  *
8  * Copyright (C) 2010 Jon A. Cruz
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include <errno.h>
14 #include <glibmm/i18n.h>
15 #include <gtkmm/label.h>
16 #include <gtk/gtkdnd.h>
18 #include "color-item.h"
20 #include "desktop.h"
21 #include "desktop-handles.h"
22 #include "desktop-style.h"
23 #include "display/nr-plain-stuff.h"
24 #include "document.h"
25 #include "inkscape.h" // for SP_ACTIVE_DESKTOP
26 #include "io/resource.h"
27 #include "io/sys.h"
28 #include "message-context.h"
29 #include "sp-gradient.h"
30 #include "sp-item.h"
31 #include "svg/svg-color.h"
32 #include "xml/node.h"
33 #include "xml/repr.h"
35 #include "color.h" // for SP_RGBA32_U_COMPOSE
38 namespace Inkscape {
39 namespace UI {
40 namespace Dialogs {
42 static std::vector<std::string> mimeStrings;
43 static std::map<std::string, guint> mimeToInt;
46 #if ENABLE_MAGIC_COLORS
47 // TODO remove this soon:
48 extern std::vector<SwatchPage*> possible;
49 #endif // ENABLE_MAGIC_COLORS
52 #if ENABLE_MAGIC_COLORS
53 static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::ustring const& match, int r, int g, int b )
54 {
55     bool changed = false;
57     if ( node ) {
58         gchar const * val = node->attribute("inkscape:x-fill-tag");
59         if ( val  && (match == val) ) {
60             SPObject *obj = document->getObjectByRepr( node );
62             gchar c[64] = {0};
63             sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
64             SPCSSAttr *css = sp_repr_css_attr_new();
65             sp_repr_css_set_property( css, "fill", c );
67             sp_desktop_apply_css_recursive( obj, css, true );
68             static_cast<SPItem*>(obj)->updateRepr();
70             changed = true;
71         }
73         val = node->attribute("inkscape:x-stroke-tag");
74         if ( val  && (match == val) ) {
75             SPObject *obj = document->getObjectByRepr( node );
77             gchar c[64] = {0};
78             sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
79             SPCSSAttr *css = sp_repr_css_attr_new();
80             sp_repr_css_set_property( css, "stroke", c );
82             sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
83             ((SPItem*)obj)->updateRepr();
85             changed = true;
86         }
88         Inkscape::XML::Node* first = node->firstChild();
89         changed |= bruteForce( document, first, match, r, g, b );
91         changed |= bruteForce( document, node->next(), match, r, g, b );
92     }
94     return changed;
95 }
96 #endif // ENABLE_MAGIC_COLORS
98 static void handleClick( GtkWidget* /*widget*/, gpointer callback_data ) {
99     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
100     if ( item ) {
101         item->buttonClicked(false);
102     }
105 static void handleSecondaryClick( GtkWidget* /*widget*/, gint /*arg1*/, gpointer callback_data ) {
106     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
107     if ( item ) {
108         item->buttonClicked(true);
109     }
112 static gboolean handleEnterNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
113     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
114     if ( item ) {
115         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
116         if ( desktop ) {
117             gchar* msg = g_strdup_printf(_("Color: <b>%s</b>; <b>Click</b> to set fill, <b>Shift+click</b> to set stroke"),
118                                          item->def.descr.c_str());
119             desktop->tipsMessageContext()->set(Inkscape::INFORMATION_MESSAGE, msg);
120             g_free(msg);
121         }
122     }
123     return FALSE;
126 static gboolean handleLeaveNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
127     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
128     if ( item ) {
129         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
130         if ( desktop ) {
131             desktop->tipsMessageContext()->clear();
132         }
133     }
134     return FALSE;
137 static void dieDieDie( GtkObject *obj, gpointer user_data )
139     g_message("die die die %p  %p", obj, user_data );
142 static bool getBlock( std::string& dst, guchar ch, std::string const str )
144     bool good = false;
145     std::string::size_type pos = str.find(ch);
146     if ( pos != std::string::npos )
147     {
148         std::string::size_type pos2 = str.find( '(', pos );
149         if ( pos2 != std::string::npos ) {
150             std::string::size_type endPos = str.find( ')', pos2 );
151             if ( endPos != std::string::npos ) {
152                 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
153                 good = true;
154             }
155         }
156     }
157     return good;
160 static bool popVal( guint64& numVal, std::string& str )
162     bool good = false;
163     std::string::size_type endPos = str.find(',');
164     if ( endPos == std::string::npos ) {
165         endPos = str.length();
166     }
168     if ( endPos != std::string::npos && endPos > 0 ) {
169         std::string xxx = str.substr( 0, endPos );
170         const gchar* ptr = xxx.c_str();
171         gchar* endPtr = 0;
172         numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
173         if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
174             // overflow
175         } else if ( (numVal == 0) && (endPtr == ptr) ) {
176             // failed conversion
177         } else {
178             good = true;
179             str.erase( 0, endPos + 1 );
180         }
181     }
183     return good;
186 // TODO resolve this more cleanly:
187 extern gboolean colorItemHandleButtonPress( GtkWidget* /*widget*/, GdkEventButton* event, gpointer user_data);
189 static void colorItemDragBegin( GtkWidget */*widget*/, GdkDragContext* dc, gpointer data )
191     ColorItem* item = reinterpret_cast<ColorItem*>(data);
192     if ( item )
193     {
194         using Inkscape::IO::Resource::get_path;
195         using Inkscape::IO::Resource::ICONS;
196         using Inkscape::IO::Resource::SYSTEM;
197         int width = 32;
198         int height = 24;
200         if (item->def.getType() != ege::PaintDef::RGB){
201             GError *error = NULL;
202             gsize bytesRead = 0;
203             gsize bytesWritten = 0;
204             gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
205                                                  -1,
206                                                  &bytesRead,
207                                                  &bytesWritten,
208                                                  &error);
209             GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_scale(localFilename, width, height, FALSE, &error);
210             g_free(localFilename);
211             gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
212         } else {
213             GdkPixbuf* pixbuf = 0;
214             if ( item->getGradient() ){
215                 guchar* px = g_new( guchar, 3 * height * width );
216                 nr_render_checkerboard_rgb( px, width, height, 3 * width, 0, 0 );
218                 sp_gradient_render_vector_block_rgb( item->getGradient(),
219                                                      px, width, height, 3 * width,
220                                                      0, width, TRUE );
222                 pixbuf = gdk_pixbuf_new_from_data( px, GDK_COLORSPACE_RGB, FALSE, 8,
223                                                    width, height, width * 3,
224                                                    0, // add delete function
225                                                    0 );
226             } else {
227                 Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, width, height );
228                 guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
229                     | (0x00ff0000 & (item->def.getG() << 16))
230                     | (0x0000ff00 & (item->def.getB() <<  8));
231                 thumb->fill( fillWith );
232                 pixbuf = thumb->gobj();
233                 g_object_ref(G_OBJECT(pixbuf));
234             }
235             gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
236         }
237     }
241 //"drag-drop"
242 // gboolean dragDropColorData( GtkWidget *widget,
243 //                             GdkDragContext *drag_context,
244 //                             gint x,
245 //                             gint y,
246 //                             guint time,
247 //                             gpointer user_data)
248 // {
249 // // TODO finish
251 //     return TRUE;
252 // }
255 SwatchPage::SwatchPage() :
256     _name(),
257     _prefWidth(0),
258     _colors()
262 SwatchPage::~SwatchPage()
267 ColorItem::ColorItem(ege::PaintDef::ColorType type) :
268     Previewable(),
269     def(type),
270     tips(),
271     _previews(),
272     _isFill(false),
273     _isStroke(false),
274     _isLive(false),
275     _linkIsTone(false),
276     _linkPercent(0),
277     _linkGray(0),
278     _linkSrc(0),
279     _grad(0),
280     _pixData(0),
281     _pixWidth(0),
282     _pixHeight(0),
283     _listeners()
287 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
288     Previewable(),
289     def( r, g, b, name ),
290     tips(),
291     _previews(),
292     _isFill(false),
293     _isStroke(false),
294     _isLive(false),
295     _linkIsTone(false),
296     _linkPercent(0),
297     _linkGray(0),
298     _linkSrc(0),
299     _grad(0),
300     _pixData(0),
301     _pixWidth(0),
302     _pixHeight(0),
303     _listeners()
307 ColorItem::~ColorItem()
311 ColorItem::ColorItem(ColorItem const &other) :
312     Inkscape::UI::Previewable()
314     if ( this != &other ) {
315         *this = other;
316     }
319 ColorItem &ColorItem::operator=(ColorItem const &other)
321     if ( this != &other ) {
322         def = other.def;
324         // TODO - correct linkage
325         _linkSrc = other._linkSrc;
326         g_message("Erk!");
327     }
328     return *this;
331 void ColorItem::setState( bool fill, bool stroke )
333     if ( (_isFill != fill) || (_isStroke != stroke) ) {
334         _isFill = fill;
335         _isStroke = stroke;
337         for ( std::vector<Gtk::Widget*>::iterator it = _previews.begin(); it != _previews.end(); ++it ) {
338             Gtk::Widget* widget = *it;
339             if ( IS_EEK_PREVIEW(widget->gobj()) ) {
340                 EekPreview * preview = EEK_PREVIEW(widget->gobj());
342                 int val = eek_preview_get_linked( preview );
343                 val &= ~(PREVIEW_FILL | PREVIEW_STROKE);
344                 if ( _isFill ) {
345                     val |= PREVIEW_FILL;
346                 }
347                 if ( _isStroke ) {
348                     val |= PREVIEW_STROKE;
349                 }
350                 eek_preview_set_linked( preview, static_cast<LinkType>(val) );
351             }
352         }
353     }
356 void ColorItem::setGradient(SPGradient *grad)
358     if (_grad != grad) {
359         _grad = grad;
360         // TODO regen and push to listeners
361     }
364 void ColorItem::setPixData(guchar* px, int width, int height)
366     if (px != _pixData) {
367         if (_pixData) {
368             g_free(_pixData);
369         }
370         _pixData = px;
371         _pixWidth = width;
372         _pixHeight = height;
374         _updatePreviews();
375     }
378 void ColorItem::_dragGetColorData( GtkWidget */*widget*/,
379                                    GdkDragContext */*drag_context*/,
380                                    GtkSelectionData *data,
381                                    guint info,
382                                    guint /*time*/,
383                                    gpointer user_data)
385     ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
386     std::string key;
387     if ( info < mimeStrings.size() ) {
388         key = mimeStrings[info];
389     } else {
390         g_warning("ERROR: unknown value (%d)", info);
391     }
393     if ( !key.empty() ) {
394         char* tmp = 0;
395         int len = 0;
396         int format = 0;
397         item->def.getMIMEData(key, tmp, len, format);
398         if ( tmp ) {
399             GdkAtom dataAtom = gdk_atom_intern( key.c_str(), FALSE );
400             gtk_selection_data_set( data, dataAtom, format, (guchar*)tmp, len );
401             delete[] tmp;
402         }
403     }
406 void ColorItem::_dropDataIn( GtkWidget */*widget*/,
407                              GdkDragContext */*drag_context*/,
408                              gint /*x*/, gint /*y*/,
409                              GtkSelectionData */*data*/,
410                              guint /*info*/,
411                              guint /*event_time*/,
412                              gpointer /*user_data*/)
416 void ColorItem::_colorDefChanged(void* data)
418     ColorItem* item = reinterpret_cast<ColorItem*>(data);
419     if ( item ) {
420         item->_updatePreviews();
421     }
424 void ColorItem::_updatePreviews()
426     for ( std::vector<Gtk::Widget*>::iterator it =  _previews.begin(); it != _previews.end(); ++it ) {
427         Gtk::Widget* widget = *it;
428         if ( IS_EEK_PREVIEW(widget->gobj()) ) {
429             EekPreview * preview = EEK_PREVIEW(widget->gobj());
431             _regenPreview(preview);
433             widget->queue_draw();
434         }
435     }
437     for ( std::vector<ColorItem*>::iterator it = _listeners.begin(); it != _listeners.end(); ++it ) {
438         guint r = def.getR();
439         guint g = def.getG();
440         guint b = def.getB();
442         if ( (*it)->_linkIsTone ) {
443             r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
444             g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
445             b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
446         } else {
447             r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
448             g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
449             b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
450         }
452         (*it)->def.setRGB( r, g, b );
453     }
456 #if ENABLE_MAGIC_COLORS
457     // Look for objects using this color
458     {
459         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
460         if ( desktop ) {
461             SPDocument* document = sp_desktop_document( desktop );
462             Inkscape::XML::Node *rroot =  document->getReprRoot();
463             if ( rroot ) {
465                 // Find where this thing came from
466                 Glib::ustring paletteName;
467                 bool found = false;
468                 int index = 0;
469                 for ( std::vector<SwatchPage*>::iterator it2 = possible.begin(); it2 != possible.end() && !found; ++it2 ) {
470                     SwatchPage* curr = *it2;
471                     index = 0;
472                     for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
473                         if ( this == *zz ) {
474                             found = true;
475                             paletteName = curr->_name;
476                             break;
477                         } else {
478                             index++;
479                         }
480                     }
481                 }
483                 if ( !paletteName.empty() ) {
484                     gchar* str = g_strdup_printf("%d|", index);
485                     paletteName.insert( 0, str );
486                     g_free(str);
487                     str = 0;
489                     if ( bruteForce( document, rroot, paletteName, def.getR(), def.getG(), def.getB() ) ) {
490                         SPDocumentUndo::done( document , SP_VERB_DIALOG_SWATCHES,
491                                           _("Change color definition"));
492                     }
493                 }
494             }
495         }
496     }
497 #endif // ENABLE_MAGIC_COLORS
501 void ColorItem::_regenPreview(EekPreview * preview)
503     if ( def.getType() != ege::PaintDef::RGB ) {
504         using Inkscape::IO::Resource::get_path;
505         using Inkscape::IO::Resource::ICONS;
506         using Inkscape::IO::Resource::SYSTEM;
507         GError *error = NULL;
508         gsize bytesRead = 0;
509         gsize bytesWritten = 0;
510         gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
511                                                      -1,
512                                                      &bytesRead,
513                                                      &bytesWritten,
514                                                      &error);
515         GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(localFilename, &error);
516         if (!pixbuf) {
517             g_warning("Null pixbuf for %p [%s]", localFilename, localFilename );
518         }
519         g_free(localFilename);
521         eek_preview_set_pixbuf( preview, pixbuf );
522     }
523     else if ( !_pixData ){
524         eek_preview_set_color( preview,
525                                (def.getR() << 8) | def.getR(),
526                                (def.getG() << 8) | def.getG(),
527                                (def.getB() << 8) | def.getB() );
528     } else {
529         GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( _pixData, GDK_COLORSPACE_RGB, FALSE, 8,
530                                                       _pixWidth, _pixHeight, _pixWidth * 3,
531                                                       0, // add delete function
532                                                       0 );
533         eek_preview_set_pixbuf( preview, pixbuf );
534     }
536     eek_preview_set_linked( preview, (LinkType)((_linkSrc ? PREVIEW_LINK_IN:0)
537                                                 | (_listeners.empty() ? 0:PREVIEW_LINK_OUT)
538                                                 | (_isLive ? PREVIEW_LINK_OTHER:0)) );
541 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio)
543     Gtk::Widget* widget = 0;
544     if ( style == PREVIEW_STYLE_BLURB) {
545         Gtk::Label *lbl = new Gtk::Label(def.descr);
546         lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
547         widget = lbl;
548     } else {
549 //         Glib::ustring blank("          ");
550 //         if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
551 //             blank = " ";
552 //         }
554         GtkWidget* eekWidget = eek_preview_new();
555         EekPreview * preview = EEK_PREVIEW(eekWidget);
556         Gtk::Widget* newBlot = Glib::wrap(eekWidget);
558         _regenPreview(preview);
560         eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::PreviewSize)size, ratio );
562         def.addCallback( _colorDefChanged, this );
564         GValue val = {0, {{0}, {0}}};
565         g_value_init( &val, G_TYPE_BOOLEAN );
566         g_value_set_boolean( &val, FALSE );
567         g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
569 /*
570         Gtk::Button *btn = new Gtk::Button(blank);
571         Gdk::Color color;
572         color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
573         btn->modify_bg(Gtk::STATE_NORMAL, color);
574         btn->modify_bg(Gtk::STATE_ACTIVE, color);
575         btn->modify_bg(Gtk::STATE_PRELIGHT, color);
576         btn->modify_bg(Gtk::STATE_SELECTED, color);
578         Gtk::Widget* newBlot = btn;
579 */
581         tips.set_tip((*newBlot), def.descr);
583 /*
584         newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
586         sigc::signal<void> type_signal_something;
587 */
589         g_signal_connect( G_OBJECT(newBlot->gobj()),
590                           "clicked",
591                           G_CALLBACK(handleClick),
592                           this);
594         g_signal_connect( G_OBJECT(newBlot->gobj()),
595                           "alt-clicked",
596                           G_CALLBACK(handleSecondaryClick),
597                           this);
599         g_signal_connect( G_OBJECT(newBlot->gobj()),
600                           "button-press-event",
601                           G_CALLBACK(colorItemHandleButtonPress),
602                           this);
604         {
605             std::vector<std::string> listing = def.getMIMETypes();
606             int entryCount = listing.size();
607             GtkTargetEntry* entries = new GtkTargetEntry[entryCount];
608             GtkTargetEntry* curr = entries;
609             for ( std::vector<std::string>::iterator it = listing.begin(); it != listing.end(); ++it ) {
610                 curr->target = g_strdup(it->c_str());
611                 curr->flags = 0;
612                 if ( mimeToInt.find(*it) == mimeToInt.end() ){
613                     // these next lines are order-dependent:
614                     mimeToInt[*it] = mimeStrings.size();
615                     mimeStrings.push_back(*it);
616                 }
617                 curr->info = mimeToInt[curr->target];
618                 curr++;
619             }
620             gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
621                                  GDK_BUTTON1_MASK,
622                                  entries, entryCount,
623                                  GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
624             for ( int i = 0; i < entryCount; i++ ) {
625                 g_free(entries[i].target);
626             }
627             delete[] entries;
628         }
630         g_signal_connect( G_OBJECT(newBlot->gobj()),
631                           "drag-data-get",
632                           G_CALLBACK(ColorItem::_dragGetColorData),
633                           this);
635         g_signal_connect( G_OBJECT(newBlot->gobj()),
636                           "drag-begin",
637                           G_CALLBACK(colorItemDragBegin),
638                           this );
640         g_signal_connect( G_OBJECT(newBlot->gobj()),
641                           "enter-notify-event",
642                           G_CALLBACK(handleEnterNotify),
643                           this);
645         g_signal_connect( G_OBJECT(newBlot->gobj()),
646                           "leave-notify-event",
647                           G_CALLBACK(handleLeaveNotify),
648                           this);
650 //         g_signal_connect( G_OBJECT(newBlot->gobj()),
651 //                           "drag-drop",
652 //                           G_CALLBACK(dragDropColorData),
653 //                           this);
655         if ( def.isEditable() )
656         {
657 //             gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
658 //                                GTK_DEST_DEFAULT_ALL,
659 //                                destColorTargets,
660 //                                G_N_ELEMENTS(destColorTargets),
661 //                                GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
664 //             g_signal_connect( G_OBJECT(newBlot->gobj()),
665 //                               "drag-data-received",
666 //                               G_CALLBACK(_dropDataIn),
667 //                               this );
668         }
670         g_signal_connect( G_OBJECT(newBlot->gobj()),
671                           "destroy",
672                           G_CALLBACK(dieDieDie),
673                           this);
676         widget = newBlot;
677     }
679     _previews.push_back( widget );
681     return widget;
684 void ColorItem::buttonClicked(bool secondary)
686     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
687     if (desktop) {
688         char const * attrName = secondary ? "stroke" : "fill";
690         SPCSSAttr *css = sp_repr_css_attr_new();
691         Glib::ustring descr;
692         switch (def.getType()) {
693             case ege::PaintDef::CLEAR: {
694                 // TODO actually make this clear
695                 sp_repr_css_set_property( css, attrName, "none" );
696                 descr = secondary? _("Remove stroke color") : _("Remove fill color");
697                 break;
698             }
699             case ege::PaintDef::NONE: {
700                 sp_repr_css_set_property( css, attrName, "none" );
701                 descr = secondary? _("Set stroke color to none") : _("Set fill color to none");
702                 break;
703             }
704             case ege::PaintDef::RGB: {
705                 Glib::ustring colorspec;
706                 if ( _grad ){
707                     colorspec = "url(#";
708                     colorspec += _grad->getId();
709                     colorspec += ")";
710                 } else {
711                     gchar c[64];
712                     guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
713                     sp_svg_write_color(c, sizeof(c), rgba);
714                     colorspec = c;
715                 }
716                 sp_repr_css_set_property( css, attrName, colorspec.c_str() );
717                 descr = secondary? _("Set stroke color from swatch") : _("Set fill color from swatch");
718                 break;
719             }
720         }
721         sp_desktop_set_style(desktop, css);
722         sp_repr_css_attr_unref(css);
724         DocumentUndo::done( sp_desktop_document(desktop), SP_VERB_DIALOG_SWATCHES, descr.c_str() );
725     }
728 void ColorItem::_wireMagicColors( SwatchPage *colorSet )
730     if ( colorSet )
731     {
732         for ( std::vector<ColorItem*>::iterator it = colorSet->_colors.begin(); it != colorSet->_colors.end(); ++it )
733         {
734             std::string::size_type pos = (*it)->def.descr.find("*{");
735             if ( pos != std::string::npos )
736             {
737                 std::string subby = (*it)->def.descr.substr( pos + 2 );
738                 std::string::size_type endPos = subby.find("}*");
739                 if ( endPos != std::string::npos )
740                 {
741                     subby.erase( endPos );
742                     //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
743                     //g_message("               '%s'", subby.c_str());
745                     if ( subby.find('E') != std::string::npos )
746                     {
747                         (*it)->def.setEditable( true );
748                     }
750                     if ( subby.find('L') != std::string::npos )
751                     {
752                         (*it)->_isLive = true;
753                     }
755                     std::string part;
756                     // Tint. index + 1 more val.
757                     if ( getBlock( part, 'T', subby ) ) {
758                         guint64 colorIndex = 0;
759                         if ( popVal( colorIndex, part ) ) {
760                             guint64 percent = 0;
761                             if ( popVal( percent, part ) ) {
762                                 (*it)->_linkTint( *(colorSet->_colors[colorIndex]), percent );
763                             }
764                         }
765                     }
767                     // Shade/tone. index + 1 or 2 more val.
768                     if ( getBlock( part, 'S', subby ) ) {
769                         guint64 colorIndex = 0;
770                         if ( popVal( colorIndex, part ) ) {
771                             guint64 percent = 0;
772                             if ( popVal( percent, part ) ) {
773                                 guint64 grayLevel = 0;
774                                 if ( !popVal( grayLevel, part ) ) {
775                                     grayLevel = 0;
776                                 }
777                                 (*it)->_linkTone( *(colorSet->_colors[colorIndex]), percent, grayLevel );
778                             }
779                         }
780                     }
782                 }
783             }
784         }
785     }
789 void ColorItem::_linkTint( ColorItem& other, int percent )
791     if ( !_linkSrc )
792     {
793         other._listeners.push_back(this);
794         _linkIsTone = false;
795         _linkPercent = percent;
796         if ( _linkPercent > 100 )
797             _linkPercent = 100;
798         if ( _linkPercent < 0 )
799             _linkPercent = 0;
800         _linkGray = 0;
801         _linkSrc = &other;
803         ColorItem::_colorDefChanged(&other);
804     }
807 void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
809     if ( !_linkSrc )
810     {
811         other._listeners.push_back(this);
812         _linkIsTone = true;
813         _linkPercent = percent;
814         if ( _linkPercent > 100 )
815             _linkPercent = 100;
816         if ( _linkPercent < 0 )
817             _linkPercent = 0;
818         _linkGray = grayLevel;
819         _linkSrc = &other;
821         ColorItem::_colorDefChanged(&other);
822     }
825 } // namespace Dialogs
826 } // namespace UI
827 } // namespace Inkscape
829 /*
830   Local Variables:
831   mode:c++
832   c-file-style:"stroustrup"
833   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
834   indent-tabs-mode:nil
835   fill-column:99
836   End:
837 */
838 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :