Code

Pot and Dutch translation update
[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>
15 #include <gtk/gtkdnd.h>
17 #include "color-item.h"
19 #include "desktop.h"
20 #include "desktop-handles.h"
21 #include "desktop-style.h"
22 #include "display/nr-plain-stuff.h"
23 #include "document.h"
24 #include "inkscape.h" // for SP_ACTIVE_DESKTOP
25 #include "io/resource.h"
26 #include "io/sys.h"
27 #include "message-context.h"
28 #include "sp-gradient.h"
29 #include "sp-item.h"
30 #include "svg/svg-color.h"
31 #include "xml/node.h"
32 #include "xml/repr.h"
34 #include "color.h" // for SP_RGBA32_U_COMPOSE
37 namespace Inkscape {
38 namespace UI {
39 namespace Dialogs {
41 static std::vector<std::string> mimeStrings;
42 static std::map<std::string, guint> mimeToInt;
45 #if ENABLE_MAGIC_COLORS
46 // TODO remove this soon:
47 extern std::vector<SwatchPage*> possible;
48 #endif // ENABLE_MAGIC_COLORS
51 #if ENABLE_MAGIC_COLORS
52 static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::ustring const& match, int r, int g, int b )
53 {
54     bool changed = false;
56     if ( node ) {
57         gchar const * val = node->attribute("inkscape:x-fill-tag");
58         if ( val  && (match == val) ) {
59             SPObject *obj = document->getObjectByRepr( node );
61             gchar c[64] = {0};
62             sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
63             SPCSSAttr *css = sp_repr_css_attr_new();
64             sp_repr_css_set_property( css, "fill", c );
66             sp_desktop_apply_css_recursive( obj, css, true );
67             static_cast<SPItem*>(obj)->updateRepr();
69             changed = true;
70         }
72         val = node->attribute("inkscape:x-stroke-tag");
73         if ( val  && (match == val) ) {
74             SPObject *obj = document->getObjectByRepr( node );
76             gchar c[64] = {0};
77             sp_svg_write_color( c, sizeof(c), SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
78             SPCSSAttr *css = sp_repr_css_attr_new();
79             sp_repr_css_set_property( css, "stroke", c );
81             sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
82             ((SPItem*)obj)->updateRepr();
84             changed = true;
85         }
87         Inkscape::XML::Node* first = node->firstChild();
88         changed |= bruteForce( document, first, match, r, g, b );
90         changed |= bruteForce( document, node->next(), match, r, g, b );
91     }
93     return changed;
94 }
95 #endif // ENABLE_MAGIC_COLORS
97 static void handleClick( GtkWidget* /*widget*/, gpointer callback_data ) {
98     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
99     if ( item ) {
100         item->buttonClicked(false);
101     }
104 static void handleSecondaryClick( GtkWidget* /*widget*/, gint /*arg1*/, gpointer callback_data ) {
105     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
106     if ( item ) {
107         item->buttonClicked(true);
108     }
111 static gboolean handleEnterNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
112     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
113     if ( item ) {
114         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
115         if ( desktop ) {
116             gchar* msg = g_strdup_printf(_("Color: <b>%s</b>; <b>Click</b> to set fill, <b>Shift+click</b> to set stroke"),
117                                          item->def.descr.c_str());
118             desktop->tipsMessageContext()->set(Inkscape::INFORMATION_MESSAGE, msg);
119             g_free(msg);
120         }
121     }
122     return FALSE;
125 static gboolean handleLeaveNotify( GtkWidget* /*widget*/, GdkEventCrossing* /*event*/, gpointer callback_data ) {
126     ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
127     if ( item ) {
128         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
129         if ( desktop ) {
130             desktop->tipsMessageContext()->clear();
131         }
132     }
133     return FALSE;
136 static void dieDieDie( GtkObject *obj, gpointer user_data )
138     g_message("die die die %p  %p", obj, user_data );
141 static bool getBlock( std::string& dst, guchar ch, std::string const str )
143     bool good = false;
144     std::string::size_type pos = str.find(ch);
145     if ( pos != std::string::npos )
146     {
147         std::string::size_type pos2 = str.find( '(', pos );
148         if ( pos2 != std::string::npos ) {
149             std::string::size_type endPos = str.find( ')', pos2 );
150             if ( endPos != std::string::npos ) {
151                 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
152                 good = true;
153             }
154         }
155     }
156     return good;
159 static bool popVal( guint64& numVal, std::string& str )
161     bool good = false;
162     std::string::size_type endPos = str.find(',');
163     if ( endPos == std::string::npos ) {
164         endPos = str.length();
165     }
167     if ( endPos != std::string::npos && endPos > 0 ) {
168         std::string xxx = str.substr( 0, endPos );
169         const gchar* ptr = xxx.c_str();
170         gchar* endPtr = 0;
171         numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
172         if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
173             // overflow
174         } else if ( (numVal == 0) && (endPtr == ptr) ) {
175             // failed conversion
176         } else {
177             good = true;
178             str.erase( 0, endPos + 1 );
179         }
180     }
182     return good;
185 // TODO resolve this more cleanly:
186 extern gboolean colorItemHandleButtonPress( GtkWidget* /*widget*/, GdkEventButton* event, gpointer user_data);
188 static void colorItemDragBegin( GtkWidget */*widget*/, GdkDragContext* dc, gpointer data )
190     ColorItem* item = reinterpret_cast<ColorItem*>(data);
191     if ( item )
192     {
193         using Inkscape::IO::Resource::get_path;
194         using Inkscape::IO::Resource::ICONS;
195         using Inkscape::IO::Resource::SYSTEM;
196         int width = 32;
197         int height = 24;
199         if (item->def.getType() != ege::PaintDef::RGB){
200             GError *error = NULL;
201             gsize bytesRead = 0;
202             gsize bytesWritten = 0;
203             gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
204                                                  -1,
205                                                  &bytesRead,
206                                                  &bytesWritten,
207                                                  &error);
208             GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_scale(localFilename, width, height, FALSE, &error);
209             g_free(localFilename);
210             gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
211         } else {
212             GdkPixbuf* pixbuf = 0;
213             if ( item->getGradient() ){
214                 guchar* px = g_new( guchar, 3 * height * width );
215                 nr_render_checkerboard_rgb( px, width, height, 3 * width, 0, 0 );
217                 sp_gradient_render_vector_block_rgb( item->getGradient(),
218                                                      px, width, height, 3 * width,
219                                                      0, width, TRUE );
221                 pixbuf = gdk_pixbuf_new_from_data( px, GDK_COLORSPACE_RGB, FALSE, 8,
222                                                    width, height, width * 3,
223                                                    0, // add delete function
224                                                    0 );
225             } else {
226                 Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, width, height );
227                 guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
228                     | (0x00ff0000 & (item->def.getG() << 16))
229                     | (0x0000ff00 & (item->def.getB() <<  8));
230                 thumb->fill( fillWith );
231                 pixbuf = thumb->gobj();
232                 g_object_ref(G_OBJECT(pixbuf));
233             }
234             gtk_drag_set_icon_pixbuf( dc, pixbuf, 0, 0 );
235         }
236     }
240 //"drag-drop"
241 // gboolean dragDropColorData( GtkWidget *widget,
242 //                             GdkDragContext *drag_context,
243 //                             gint x,
244 //                             gint y,
245 //                             guint time,
246 //                             gpointer user_data)
247 // {
248 // // TODO finish
250 //     return TRUE;
251 // }
254 SwatchPage::SwatchPage() :
255     _name(),
256     _prefWidth(0),
257     _colors()
261 SwatchPage::~SwatchPage()
266 ColorItem::ColorItem(ege::PaintDef::ColorType type) :
267     Previewable(),
268     def(type),
269     tips(),
270     _previews(),
271     _isFill(false),
272     _isStroke(false),
273     _isLive(false),
274     _linkIsTone(false),
275     _linkPercent(0),
276     _linkGray(0),
277     _linkSrc(0),
278     _grad(0),
279     _pixData(0),
280     _pixWidth(0),
281     _pixHeight(0),
282     _listeners()
286 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
287     Previewable(),
288     def( r, g, b, name ),
289     tips(),
290     _previews(),
291     _isFill(false),
292     _isStroke(false),
293     _isLive(false),
294     _linkIsTone(false),
295     _linkPercent(0),
296     _linkGray(0),
297     _linkSrc(0),
298     _grad(0),
299     _pixData(0),
300     _pixWidth(0),
301     _pixHeight(0),
302     _listeners()
306 ColorItem::~ColorItem()
310 ColorItem::ColorItem(ColorItem const &other) :
311     Inkscape::UI::Previewable()
313     if ( this != &other ) {
314         *this = other;
315     }
318 ColorItem &ColorItem::operator=(ColorItem const &other)
320     if ( this != &other ) {
321         def = other.def;
323         // TODO - correct linkage
324         _linkSrc = other._linkSrc;
325         g_message("Erk!");
326     }
327     return *this;
330 void ColorItem::setState( bool fill, bool stroke )
332     if ( (_isFill != fill) || (_isStroke != stroke) ) {
333         _isFill = fill;
334         _isStroke = stroke;
336         for ( std::vector<Gtk::Widget*>::iterator it = _previews.begin(); it != _previews.end(); ++it ) {
337             Gtk::Widget* widget = *it;
338             if ( IS_EEK_PREVIEW(widget->gobj()) ) {
339                 EekPreview * preview = EEK_PREVIEW(widget->gobj());
341                 int val = eek_preview_get_linked( preview );
342                 val &= ~(PREVIEW_FILL | PREVIEW_STROKE);
343                 if ( _isFill ) {
344                     val |= PREVIEW_FILL;
345                 }
346                 if ( _isStroke ) {
347                     val |= PREVIEW_STROKE;
348                 }
349                 eek_preview_set_linked( preview, static_cast<LinkType>(val) );
350             }
351         }
352     }
355 void ColorItem::setGradient(SPGradient *grad)
357     if (_grad != grad) {
358         _grad = grad;
359         // TODO regen and push to listeners
360     }
363 void ColorItem::setPixData(guchar* px, int width, int height)
365     if (px != _pixData) {
366         if (_pixData) {
367             g_free(_pixData);
368         }
369         _pixData = px;
370         _pixWidth = width;
371         _pixHeight = height;
373         _updatePreviews();
374     }
377 void ColorItem::_dragGetColorData( GtkWidget */*widget*/,
378                                    GdkDragContext */*drag_context*/,
379                                    GtkSelectionData *data,
380                                    guint info,
381                                    guint /*time*/,
382                                    gpointer user_data)
384     ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
385     std::string key;
386     if ( info < mimeStrings.size() ) {
387         key = mimeStrings[info];
388     } else {
389         g_warning("ERROR: unknown value (%d)", info);
390     }
392     if ( !key.empty() ) {
393         char* tmp = 0;
394         int len = 0;
395         int format = 0;
396         item->def.getMIMEData(key, tmp, len, format);
397         if ( tmp ) {
398             GdkAtom dataAtom = gdk_atom_intern( key.c_str(), FALSE );
399             gtk_selection_data_set( data, dataAtom, format, (guchar*)tmp, len );
400             delete[] tmp;
401         }
402     }
405 void ColorItem::_dropDataIn( GtkWidget */*widget*/,
406                              GdkDragContext */*drag_context*/,
407                              gint /*x*/, gint /*y*/,
408                              GtkSelectionData */*data*/,
409                              guint /*info*/,
410                              guint /*event_time*/,
411                              gpointer /*user_data*/)
415 void ColorItem::_colorDefChanged(void* data)
417     ColorItem* item = reinterpret_cast<ColorItem*>(data);
418     if ( item ) {
419         item->_updatePreviews();
420     }
423 void ColorItem::_updatePreviews()
425     for ( std::vector<Gtk::Widget*>::iterator it =  _previews.begin(); it != _previews.end(); ++it ) {
426         Gtk::Widget* widget = *it;
427         if ( IS_EEK_PREVIEW(widget->gobj()) ) {
428             EekPreview * preview = EEK_PREVIEW(widget->gobj());
430             _regenPreview(preview);
432             widget->queue_draw();
433         }
434     }
436     for ( std::vector<ColorItem*>::iterator it = _listeners.begin(); it != _listeners.end(); ++it ) {
437         guint r = def.getR();
438         guint g = def.getG();
439         guint b = def.getB();
441         if ( (*it)->_linkIsTone ) {
442             r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
443             g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
444             b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
445         } else {
446             r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
447             g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
448             b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
449         }
451         (*it)->def.setRGB( r, g, b );
452     }
455 #if ENABLE_MAGIC_COLORS
456     // Look for objects using this color
457     {
458         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
459         if ( desktop ) {
460             SPDocument* document = sp_desktop_document( desktop );
461             Inkscape::XML::Node *rroot =  sp_document_repr_root( document );
462             if ( rroot ) {
464                 // Find where this thing came from
465                 Glib::ustring paletteName;
466                 bool found = false;
467                 int index = 0;
468                 for ( std::vector<SwatchPage*>::iterator it2 = possible.begin(); it2 != possible.end() && !found; ++it2 ) {
469                     SwatchPage* curr = *it2;
470                     index = 0;
471                     for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
472                         if ( this == *zz ) {
473                             found = true;
474                             paletteName = curr->_name;
475                             break;
476                         } else {
477                             index++;
478                         }
479                     }
480                 }
482                 if ( !paletteName.empty() ) {
483                     gchar* str = g_strdup_printf("%d|", index);
484                     paletteName.insert( 0, str );
485                     g_free(str);
486                     str = 0;
488                     if ( bruteForce( document, rroot, paletteName, def.getR(), def.getG(), def.getB() ) ) {
489                         sp_document_done( document , SP_VERB_DIALOG_SWATCHES,
490                                           _("Change color definition"));
491                     }
492                 }
493             }
494         }
495     }
496 #endif // ENABLE_MAGIC_COLORS
500 void ColorItem::_regenPreview(EekPreview * preview)
502     if ( def.getType() != ege::PaintDef::RGB ) {
503         using Inkscape::IO::Resource::get_path;
504         using Inkscape::IO::Resource::ICONS;
505         using Inkscape::IO::Resource::SYSTEM;
506         GError *error = NULL;
507         gsize bytesRead = 0;
508         gsize bytesWritten = 0;
509         gchar *localFilename = g_filename_from_utf8( get_path(SYSTEM, ICONS, "remove-color.png"),
510                                                      -1,
511                                                      &bytesRead,
512                                                      &bytesWritten,
513                                                      &error);
514         GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(localFilename, &error);
515         if (!pixbuf) {
516             g_warning("Null pixbuf for %p [%s]", localFilename, localFilename );
517         }
518         g_free(localFilename);
520         eek_preview_set_pixbuf( preview, pixbuf );
521     }
522     else if ( !_pixData ){
523         eek_preview_set_color( preview,
524                                (def.getR() << 8) | def.getR(),
525                                (def.getG() << 8) | def.getG(),
526                                (def.getB() << 8) | def.getB() );
527     } else {
528         GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( _pixData, GDK_COLORSPACE_RGB, FALSE, 8,
529                                                       _pixWidth, _pixHeight, _pixWidth * 3,
530                                                       0, // add delete function
531                                                       0 );
532         eek_preview_set_pixbuf( preview, pixbuf );
533     }
535     eek_preview_set_linked( preview, (LinkType)((_linkSrc ? PREVIEW_LINK_IN:0)
536                                                 | (_listeners.empty() ? 0:PREVIEW_LINK_OUT)
537                                                 | (_isLive ? PREVIEW_LINK_OTHER:0)) );
540 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio)
542     Gtk::Widget* widget = 0;
543     if ( style == PREVIEW_STYLE_BLURB) {
544         Gtk::Label *lbl = new Gtk::Label(def.descr);
545         lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
546         widget = lbl;
547     } else {
548 //         Glib::ustring blank("          ");
549 //         if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
550 //             blank = " ";
551 //         }
553         GtkWidget* eekWidget = eek_preview_new();
554         EekPreview * preview = EEK_PREVIEW(eekWidget);
555         Gtk::Widget* newBlot = Glib::wrap(eekWidget);
557         _regenPreview(preview);
559         eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::PreviewSize)size, ratio );
561         def.addCallback( _colorDefChanged, this );
563         GValue val = {0, {{0}, {0}}};
564         g_value_init( &val, G_TYPE_BOOLEAN );
565         g_value_set_boolean( &val, FALSE );
566         g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
568 /*
569         Gtk::Button *btn = new Gtk::Button(blank);
570         Gdk::Color color;
571         color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
572         btn->modify_bg(Gtk::STATE_NORMAL, color);
573         btn->modify_bg(Gtk::STATE_ACTIVE, color);
574         btn->modify_bg(Gtk::STATE_PRELIGHT, color);
575         btn->modify_bg(Gtk::STATE_SELECTED, color);
577         Gtk::Widget* newBlot = btn;
578 */
580         tips.set_tip((*newBlot), def.descr);
582 /*
583         newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
585         sigc::signal<void> type_signal_something;
586 */
588         g_signal_connect( G_OBJECT(newBlot->gobj()),
589                           "clicked",
590                           G_CALLBACK(handleClick),
591                           this);
593         g_signal_connect( G_OBJECT(newBlot->gobj()),
594                           "alt-clicked",
595                           G_CALLBACK(handleSecondaryClick),
596                           this);
598         g_signal_connect( G_OBJECT(newBlot->gobj()),
599                           "button-press-event",
600                           G_CALLBACK(colorItemHandleButtonPress),
601                           this);
603         {
604             std::vector<std::string> listing = def.getMIMETypes();
605             int entryCount = listing.size();
606             GtkTargetEntry* entries = new GtkTargetEntry[entryCount];
607             GtkTargetEntry* curr = entries;
608             for ( std::vector<std::string>::iterator it = listing.begin(); it != listing.end(); ++it ) {
609                 curr->target = g_strdup(it->c_str());
610                 curr->flags = 0;
611                 if ( mimeToInt.find(*it) == mimeToInt.end() ){
612                     // these next lines are order-dependent:
613                     mimeToInt[*it] = mimeStrings.size();
614                     mimeStrings.push_back(*it);
615                 }
616                 curr->info = mimeToInt[curr->target];
617                 curr++;
618             }
619             gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
620                                  GDK_BUTTON1_MASK,
621                                  entries, entryCount,
622                                  GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
623             for ( int i = 0; i < entryCount; i++ ) {
624                 g_free(entries[i].target);
625             }
626             delete[] entries;
627         }
629         g_signal_connect( G_OBJECT(newBlot->gobj()),
630                           "drag-data-get",
631                           G_CALLBACK(ColorItem::_dragGetColorData),
632                           this);
634         g_signal_connect( G_OBJECT(newBlot->gobj()),
635                           "drag-begin",
636                           G_CALLBACK(colorItemDragBegin),
637                           this );
639         g_signal_connect( G_OBJECT(newBlot->gobj()),
640                           "enter-notify-event",
641                           G_CALLBACK(handleEnterNotify),
642                           this);
644         g_signal_connect( G_OBJECT(newBlot->gobj()),
645                           "leave-notify-event",
646                           G_CALLBACK(handleLeaveNotify),
647                           this);
649 //         g_signal_connect( G_OBJECT(newBlot->gobj()),
650 //                           "drag-drop",
651 //                           G_CALLBACK(dragDropColorData),
652 //                           this);
654         if ( def.isEditable() )
655         {
656 //             gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
657 //                                GTK_DEST_DEFAULT_ALL,
658 //                                destColorTargets,
659 //                                G_N_ELEMENTS(destColorTargets),
660 //                                GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
663 //             g_signal_connect( G_OBJECT(newBlot->gobj()),
664 //                               "drag-data-received",
665 //                               G_CALLBACK(_dropDataIn),
666 //                               this );
667         }
669         g_signal_connect( G_OBJECT(newBlot->gobj()),
670                           "destroy",
671                           G_CALLBACK(dieDieDie),
672                           this);
675         widget = newBlot;
676     }
678     _previews.push_back( widget );
680     return widget;
683 void ColorItem::buttonClicked(bool secondary)
685     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
686     if (desktop) {
687         char const * attrName = secondary ? "stroke" : "fill";
689         SPCSSAttr *css = sp_repr_css_attr_new();
690         Glib::ustring descr;
691         switch (def.getType()) {
692             case ege::PaintDef::CLEAR: {
693                 // TODO actually make this clear
694                 sp_repr_css_set_property( css, attrName, "none" );
695                 descr = secondary? _("Remove stroke color") : _("Remove fill color");
696                 break;
697             }
698             case ege::PaintDef::NONE: {
699                 sp_repr_css_set_property( css, attrName, "none" );
700                 descr = secondary? _("Set stroke color to none") : _("Set fill color to none");
701                 break;
702             }
703             case ege::PaintDef::RGB: {
704                 Glib::ustring colorspec;
705                 if ( _grad ){
706                     colorspec = "url(#";
707                     colorspec += _grad->getId();
708                     colorspec += ")";
709                 } else {
710                     gchar c[64];
711                     guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
712                     sp_svg_write_color(c, sizeof(c), rgba);
713                     colorspec = c;
714                 }
715                 sp_repr_css_set_property( css, attrName, colorspec.c_str() );
716                 descr = secondary? _("Set stroke color from swatch") : _("Set fill color from swatch");
717                 break;
718             }
719         }
720         sp_desktop_set_style(desktop, css);
721         sp_repr_css_attr_unref(css);
723         sp_document_done( sp_desktop_document(desktop), SP_VERB_DIALOG_SWATCHES, descr.c_str() );
724     }
727 void ColorItem::_wireMagicColors( SwatchPage *colorSet )
729     if ( colorSet )
730     {
731         for ( std::vector<ColorItem*>::iterator it = colorSet->_colors.begin(); it != colorSet->_colors.end(); ++it )
732         {
733             std::string::size_type pos = (*it)->def.descr.find("*{");
734             if ( pos != std::string::npos )
735             {
736                 std::string subby = (*it)->def.descr.substr( pos + 2 );
737                 std::string::size_type endPos = subby.find("}*");
738                 if ( endPos != std::string::npos )
739                 {
740                     subby.erase( endPos );
741                     //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
742                     //g_message("               '%s'", subby.c_str());
744                     if ( subby.find('E') != std::string::npos )
745                     {
746                         (*it)->def.setEditable( true );
747                     }
749                     if ( subby.find('L') != std::string::npos )
750                     {
751                         (*it)->_isLive = true;
752                     }
754                     std::string part;
755                     // Tint. index + 1 more val.
756                     if ( getBlock( part, 'T', subby ) ) {
757                         guint64 colorIndex = 0;
758                         if ( popVal( colorIndex, part ) ) {
759                             guint64 percent = 0;
760                             if ( popVal( percent, part ) ) {
761                                 (*it)->_linkTint( *(colorSet->_colors[colorIndex]), percent );
762                             }
763                         }
764                     }
766                     // Shade/tone. index + 1 or 2 more val.
767                     if ( getBlock( part, 'S', subby ) ) {
768                         guint64 colorIndex = 0;
769                         if ( popVal( colorIndex, part ) ) {
770                             guint64 percent = 0;
771                             if ( popVal( percent, part ) ) {
772                                 guint64 grayLevel = 0;
773                                 if ( !popVal( grayLevel, part ) ) {
774                                     grayLevel = 0;
775                                 }
776                                 (*it)->_linkTone( *(colorSet->_colors[colorIndex]), percent, grayLevel );
777                             }
778                         }
779                     }
781                 }
782             }
783         }
784     }
788 void ColorItem::_linkTint( ColorItem& other, int percent )
790     if ( !_linkSrc )
791     {
792         other._listeners.push_back(this);
793         _linkIsTone = false;
794         _linkPercent = percent;
795         if ( _linkPercent > 100 )
796             _linkPercent = 100;
797         if ( _linkPercent < 0 )
798             _linkPercent = 0;
799         _linkGray = 0;
800         _linkSrc = &other;
802         ColorItem::_colorDefChanged(&other);
803     }
806 void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
808     if ( !_linkSrc )
809     {
810         other._listeners.push_back(this);
811         _linkIsTone = true;
812         _linkPercent = percent;
813         if ( _linkPercent > 100 )
814             _linkPercent = 100;
815         if ( _linkPercent < 0 )
816             _linkPercent = 0;
817         _linkGray = grayLevel;
818         _linkSrc = &other;
820         ColorItem::_colorDefChanged(&other);
821     }
824 } // namespace Dialogs
825 } // namespace UI
826 } // namespace Inkscape
828 /*
829   Local Variables:
830   mode:c++
831   c-file-style:"stroustrup"
832   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
833   indent-tabs-mode:nil
834   fill-column:99
835   End:
836 */
837 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :