3a41c1ec4a4629b096f828952bd921657a997711
1 /*
2 * A simple panel for color swatches
3 *
4 * Authors:
5 * Jon A. Cruz
6 *
7 * Copyright (C) 2005 Jon A. Cruz
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
15 #include <errno.h>
17 #include <gtk/gtkdialog.h> //for GTK_RESPONSE* types
18 #include <gtk/gtkdnd.h>
20 #include <glibmm/i18n.h>
21 #include <gdkmm/pixbuf.h>
22 #include "inkscape.h"
23 #include "document.h"
24 #include "desktop-handles.h"
25 #include "extension/db.h"
26 #include "inkscape.h"
27 #include "svg/svg-color.h"
28 #include "desktop-style.h"
29 #include "io/sys.h"
30 #include "path-prefix.h"
31 #include "swatches.h"
32 #include "sp-item.h"
33 #include "prefs-utils.h"
35 #include "eek-preview.h"
37 namespace Inkscape {
38 namespace UI {
39 namespace Dialogs {
41 SwatchesPanel* SwatchesPanel::instance = 0;
44 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
45 def( r, g, b, name ),
46 _isLive(false),
47 _linkIsTone(false),
48 _linkPercent(0),
49 _linkGray(0),
50 _linkSrc(0)
51 {
52 }
54 ColorItem::~ColorItem()
55 {
56 }
58 ColorItem::ColorItem(ColorItem const &other) :
59 Inkscape::UI::Previewable()
60 {
61 if ( this != &other ) {
62 *this = other;
63 }
64 }
66 ColorItem &ColorItem::operator=(ColorItem const &other)
67 {
68 if ( this != &other ) {
69 def = other.def;
71 // TODO - correct linkage
72 _linkSrc = other._linkSrc;
73 g_message("Erk!");
74 }
75 return *this;
76 }
79 class JustForNow
80 {
81 public:
82 JustForNow() : _prefWidth(0) {}
84 Glib::ustring _name;
85 int _prefWidth;
86 std::vector<ColorItem*> _colors;
87 };
89 static std::vector<JustForNow*> possible;
93 typedef enum {
94 APP_X_INKY_COLOR_ID = 0,
95 APP_X_INKY_COLOR = 0,
96 APP_X_COLOR,
97 TEXT_DATA
98 } colorFlavorType;
100 static const GtkTargetEntry sourceColorEntries[] = {
101 #if ENABLE_MAGIC_COLORS
102 // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID},
103 {"application/x-inkscape-color", 0, APP_X_INKY_COLOR},
104 #endif // ENABLE_MAGIC_COLORS
105 {"application/x-color", 0, APP_X_COLOR},
106 {"text/plain", 0, TEXT_DATA},
107 };
109 void ColorItem::_dragGetColorData( GtkWidget *widget,
110 GdkDragContext *drag_context,
111 GtkSelectionData *data,
112 guint info,
113 guint time,
114 gpointer user_data)
115 {
116 (void)widget;
117 (void)drag_context;
118 (void)time;
119 static GdkAtom typeXColor = gdk_atom_intern("application/x-color", FALSE);
120 static GdkAtom typeText = gdk_atom_intern("text/plain", FALSE);
122 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
123 if ( info == TEXT_DATA ) {
124 gchar* tmp = g_strdup_printf("#%02x%02x%02x", item->def.getR(), item->def.getG(), item->def.getB() );
126 gtk_selection_data_set( data,
127 typeText,
128 8, // format
129 (guchar*)tmp,
130 strlen((const char*)tmp) + 1);
131 g_free(tmp);
132 tmp = 0;
133 } else if ( info == APP_X_INKY_COLOR ) {
134 Glib::ustring paletteName;
136 // Find where this thing came from
137 bool found = false;
138 int index = 0;
139 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end() && !found; ++it ) {
140 JustForNow* curr = *it;
141 index = 0;
142 for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
143 if ( item == *zz ) {
144 found = true;
145 paletteName = curr->_name;
146 break;
147 } else {
148 index++;
149 }
150 }
151 }
153 // if ( found ) {
154 // g_message("Found the color at entry %d in palette '%s'", index, paletteName.c_str() );
155 // } else {
156 // g_message("Unable to find the color");
157 // }
158 int itemCount = 4 + 2 + 1 + paletteName.length();
160 guint16* tmp = new guint16[itemCount];
161 tmp[0] = (item->def.getR() << 8) | item->def.getR();
162 tmp[1] = (item->def.getG() << 8) | item->def.getG();
163 tmp[2] = (item->def.getB() << 8) | item->def.getB();
164 tmp[3] = 0xffff;
165 tmp[4] = (item->_isLive || !item->_listeners.empty() || (item->_linkSrc != 0) ) ? 1 : 0;
167 tmp[5] = index;
168 tmp[6] = paletteName.length();
169 for ( unsigned int i = 0; i < paletteName.length(); i++ ) {
170 tmp[7 + i] = paletteName[i];
171 }
172 gtk_selection_data_set( data,
173 typeXColor,
174 16, // format
175 reinterpret_cast<const guchar*>(tmp),
176 itemCount * 2);
177 delete[] tmp;
178 } else {
179 guint16 tmp[4];
180 tmp[0] = (item->def.getR() << 8) | item->def.getR();
181 tmp[1] = (item->def.getG() << 8) | item->def.getG();
182 tmp[2] = (item->def.getB() << 8) | item->def.getB();
183 tmp[3] = 0xffff;
184 gtk_selection_data_set( data,
185 typeXColor,
186 16, // format
187 reinterpret_cast<const guchar*>(tmp),
188 (3+1) * 2);
189 }
190 }
192 static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
193 {
194 (void)widget;
195 ColorItem* item = reinterpret_cast<ColorItem*>(data);
196 if ( item )
197 {
198 Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, 32, 24 );
199 guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
200 | (0x00ff0000 & (item->def.getG() << 16))
201 | (0x0000ff00 & (item->def.getB() << 8));
202 thumb->fill( fillWith );
203 gtk_drag_set_icon_pixbuf( dc, thumb->gobj(), 0, 0 );
204 }
206 }
208 //"drag-drop"
209 // gboolean dragDropColorData( GtkWidget *widget,
210 // GdkDragContext *drag_context,
211 // gint x,
212 // gint y,
213 // guint time,
214 // gpointer user_data)
215 // {
216 // // TODO finish
218 // return TRUE;
219 // }
221 static gboolean onButtonPressed (GtkWidget *widget, GdkEventButton *event, gpointer userdata)
222 {
223 (void)widget;
224 /* single click with the right mouse button? */
225 if(event->type == GDK_BUTTON_RELEASE)
226 {
227 ColorItem* item = reinterpret_cast<ColorItem*>(userdata);
228 if(item)
229 {
230 if (event->button == 1)
231 {
232 if(event->state & GDK_SHIFT_MASK)
233 item->buttonClicked(true); /* the button was pressed with shift held down. set the stroke */
234 else item->buttonClicked(false);
235 return TRUE; /* we handled this */
236 }
237 else if (event->button == 3)
238 {
239 item->buttonClicked(true);
240 return TRUE; /* we handled this */
241 }
242 }
243 }
245 return FALSE; /* we did not handle this */
246 }
248 static void dieDieDie( GtkObject *obj, gpointer user_data )
249 {
250 g_message("die die die %p %p", obj, user_data );
251 }
253 static const GtkTargetEntry destColorTargets[] = {
254 #if ENABLE_MAGIC_COLORS
255 // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID},
256 {"application/x-inkscape-color", 0, APP_X_INKY_COLOR},
257 #endif // ENABLE_MAGIC_COLORS
258 {"application/x-color", 0, APP_X_COLOR},
259 };
261 #include "color.h" // for SP_RGBA32_U_COMPOSE
263 void ColorItem::_dropDataIn( GtkWidget *widget,
264 GdkDragContext *drag_context,
265 gint x, gint y,
266 GtkSelectionData *data,
267 guint info,
268 guint event_time,
269 gpointer user_data)
270 {
271 (void)widget;
272 (void)drag_context;
273 (void)x;
274 (void)y;
275 (void)event_time;
276 // g_message(" droppy droppy %d", info);
277 switch (info) {
278 case APP_X_INKY_COLOR:
279 {
280 if ( data->length >= 8 ) {
281 // Careful about endian issues.
282 guint16* dataVals = (guint16*)data->data;
283 if ( user_data ) {
284 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
285 if ( item->def.isEditable() ) {
286 // Shove on in the new value
287 item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
288 }
289 }
290 }
291 break;
292 }
293 case APP_X_COLOR:
294 {
295 if ( data->length == 8 ) {
296 // Careful about endian issues.
297 guint16* dataVals = (guint16*)data->data;
298 // {
299 // gchar c[64] = {0};
300 // sp_svg_write_color( c, 64,
301 // SP_RGBA32_U_COMPOSE(
302 // 0x0ff & (dataVals[0] >> 8),
303 // 0x0ff & (dataVals[1] >> 8),
304 // 0x0ff & (dataVals[2] >> 8),
305 // 0xff // can't have transparency in the color itself
306 // //0x0ff & (data->data[3] >> 8),
307 // ));
308 // }
309 if ( user_data ) {
310 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
311 if ( item->def.isEditable() ) {
312 // Shove on in the new value
313 item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
314 }
315 }
316 }
317 break;
318 }
319 default:
320 g_message("unknown drop type");
321 }
323 }
325 static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::ustring const& match, int r, int g, int b )
326 {
327 bool changed = false;
329 if ( node ) {
330 gchar const * val = node->attribute("inkscape:x-fill-tag");
331 if ( val && (match == val) ) {
332 SPObject *obj = document->getObjectByRepr( node );
334 gchar c[64] = {0};
335 sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
336 SPCSSAttr *css = sp_repr_css_attr_new();
337 sp_repr_css_set_property( css, "fill", c );
339 sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
340 ((SPItem*)obj)->updateRepr();
342 changed = true;
343 }
345 val = node->attribute("inkscape:x-stroke-tag");
346 if ( val && (match == val) ) {
347 SPObject *obj = document->getObjectByRepr( node );
349 gchar c[64] = {0};
350 sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
351 SPCSSAttr *css = sp_repr_css_attr_new();
352 sp_repr_css_set_property( css, "stroke", c );
354 sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
355 ((SPItem*)obj)->updateRepr();
357 changed = true;
358 }
360 Inkscape::XML::Node* first = node->firstChild();
361 changed |= bruteForce( document, first, match, r, g, b );
363 changed |= bruteForce( document, node->next(), match, r, g, b );
364 }
366 return changed;
367 }
369 void ColorItem::_colorDefChanged(void* data)
370 {
371 ColorItem* item = reinterpret_cast<ColorItem*>(data);
372 if ( item ) {
373 for ( std::vector<Gtk::Widget*>::iterator it = item->_previews.begin(); it != item->_previews.end(); ++it ) {
374 Gtk::Widget* widget = *it;
375 if ( IS_EEK_PREVIEW(widget->gobj()) ) {
376 EekPreview * preview = EEK_PREVIEW(widget->gobj());
377 eek_preview_set_color( preview,
378 (item->def.getR() << 8) | item->def.getR(),
379 (item->def.getG() << 8) | item->def.getG(),
380 (item->def.getB() << 8) | item->def.getB() );
382 eek_preview_set_linked( preview, (LinkType)((item->_linkSrc ? PREVIEW_LINK_IN:0)
383 | (item->_listeners.empty() ? 0:PREVIEW_LINK_OUT)
384 | (item->_isLive ? PREVIEW_LINK_OTHER:0)) );
386 widget->queue_draw();
387 }
388 }
390 for ( std::vector<ColorItem*>::iterator it = item->_listeners.begin(); it != item->_listeners.end(); ++it ) {
391 guint r = item->def.getR();
392 guint g = item->def.getG();
393 guint b = item->def.getB();
395 if ( (*it)->_linkIsTone ) {
396 r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
397 g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
398 b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
399 } else {
400 r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
401 g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
402 b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
403 }
405 (*it)->def.setRGB( r, g, b );
406 }
409 // Look for objects using this color
410 {
411 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
412 if ( desktop ) {
413 SPDocument* document = sp_desktop_document( desktop );
414 Inkscape::XML::Node *rroot = sp_document_repr_root( document );
415 if ( rroot ) {
417 // Find where this thing came from
418 Glib::ustring paletteName;
419 bool found = false;
420 int index = 0;
421 for ( std::vector<JustForNow*>::iterator it2 = possible.begin(); it2 != possible.end() && !found; ++it2 ) {
422 JustForNow* curr = *it2;
423 index = 0;
424 for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
425 if ( item == *zz ) {
426 found = true;
427 paletteName = curr->_name;
428 break;
429 } else {
430 index++;
431 }
432 }
433 }
435 if ( !paletteName.empty() ) {
436 gchar* str = g_strdup_printf("%d|", index);
437 paletteName.insert( 0, str );
438 g_free(str);
439 str = 0;
441 if ( bruteForce( document, rroot, paletteName, item->def.getR(), item->def.getG(), item->def.getB() ) ) {
442 sp_document_done( document , SP_VERB_DIALOG_SWATCHES,
443 _("Change color definition"));
444 }
445 }
446 }
447 }
448 }
449 }
450 }
453 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::IconSize size)
454 {
455 Gtk::Widget* widget = 0;
456 if ( style == PREVIEW_STYLE_BLURB ) {
457 Gtk::Label *lbl = new Gtk::Label(def.descr);
458 lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
459 widget = lbl;
460 } else {
461 Glib::ustring blank(" ");
462 if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
463 blank = " ";
464 }
466 GtkWidget* eekWidget = eek_preview_new();
467 EekPreview * preview = EEK_PREVIEW(eekWidget);
468 Gtk::Widget* newBlot = Glib::wrap(eekWidget);
470 eek_preview_set_color( preview, (def.getR() << 8) | def.getR(), (def.getG() << 8) | def.getG(), (def.getB() << 8) | def.getB());
472 eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::GtkIconSize)size );
473 eek_preview_set_linked( preview, (LinkType)((_linkSrc ? PREVIEW_LINK_IN:0)
474 | (_listeners.empty() ? 0:PREVIEW_LINK_OUT)
475 | (_isLive ? PREVIEW_LINK_OTHER:0)) );
477 def.addCallback( _colorDefChanged, this );
479 GValue val = {0, {{0}, {0}}};
480 g_value_init( &val, G_TYPE_BOOLEAN );
481 g_value_set_boolean( &val, FALSE );
482 g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
484 /*
485 Gtk::Button *btn = new Gtk::Button(blank);
486 Gdk::Color color;
487 color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
488 btn->modify_bg(Gtk::STATE_NORMAL, color);
489 btn->modify_bg(Gtk::STATE_ACTIVE, color);
490 btn->modify_bg(Gtk::STATE_PRELIGHT, color);
491 btn->modify_bg(Gtk::STATE_SELECTED, color);
493 Gtk::Widget* newBlot = btn;
494 */
496 tips.set_tip((*newBlot), def.descr);
498 /*
499 newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
501 sigc::signal<void> type_signal_something;
502 */
503 g_signal_connect( G_OBJECT(newBlot->gobj()),
504 "button-release-event",
505 G_CALLBACK(onButtonPressed),
506 this);
508 gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
509 GDK_BUTTON1_MASK,
510 sourceColorEntries,
511 G_N_ELEMENTS(sourceColorEntries),
512 GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
514 g_signal_connect( G_OBJECT(newBlot->gobj()),
515 "drag-data-get",
516 G_CALLBACK(ColorItem::_dragGetColorData),
517 this);
519 g_signal_connect( G_OBJECT(newBlot->gobj()),
520 "drag-begin",
521 G_CALLBACK(dragBegin),
522 this );
524 // g_signal_connect( G_OBJECT(newBlot->gobj()),
525 // "drag-drop",
526 // G_CALLBACK(dragDropColorData),
527 // this);
529 if ( def.isEditable() )
530 {
531 gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
532 GTK_DEST_DEFAULT_ALL,
533 destColorTargets,
534 G_N_ELEMENTS(destColorTargets),
535 GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
538 g_signal_connect( G_OBJECT(newBlot->gobj()),
539 "drag-data-received",
540 G_CALLBACK(_dropDataIn),
541 this );
542 }
544 g_signal_connect( G_OBJECT(newBlot->gobj()),
545 "destroy",
546 G_CALLBACK(dieDieDie),
547 this);
550 widget = newBlot;
551 }
553 _previews.push_back( widget );
555 return widget;
556 }
558 void ColorItem::buttonClicked(bool secondary)
559 {
560 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
561 if (desktop) {
562 char const * attrName = secondary ? "stroke" : "fill";
563 guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
564 gchar c[64];
565 sp_svg_write_color(c, 64, rgba);
567 SPCSSAttr *css = sp_repr_css_attr_new();
568 sp_repr_css_set_property( css, attrName, c );
569 sp_desktop_set_style(desktop, css);
571 sp_repr_css_attr_unref(css);
572 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_SWATCHES,
573 secondary? _("Set stroke color from swatch") : _("Set fill color from swatch"));
574 }
575 }
580 static char* trim( char* str ) {
581 char* ret = str;
582 while ( *str && (*str == ' ' || *str == '\t') ) {
583 str++;
584 }
585 ret = str;
586 while ( *str ) {
587 str++;
588 }
589 str--;
590 while ( str > ret && ( *str == ' ' || *str == '\t' ) || *str == '\r' || *str == '\n' ) {
591 *str-- = 0;
592 }
593 return ret;
594 }
596 void skipWhitespace( char*& str ) {
597 while ( *str == ' ' || *str == '\t' ) {
598 str++;
599 }
600 }
602 bool parseNum( char*& str, int& val ) {
603 val = 0;
604 while ( '0' <= *str && *str <= '9' ) {
605 val = val * 10 + (*str - '0');
606 str++;
607 }
608 bool retval = !(*str == 0 || *str == ' ' || *str == '\t' || *str == '\r' || *str == '\n');
609 return retval;
610 }
613 static bool getBlock( std::string& dst, guchar ch, std::string const str )
614 {
615 bool good = false;
616 std::string::size_type pos = str.find(ch);
617 if ( pos != std::string::npos )
618 {
619 std::string::size_type pos2 = str.find( '(', pos );
620 if ( pos2 != std::string::npos ) {
621 std::string::size_type endPos = str.find( ')', pos2 );
622 if ( endPos != std::string::npos ) {
623 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
624 good = true;
625 }
626 }
627 }
628 return good;
629 }
631 static bool popVal( guint64& numVal, std::string& str )
632 {
633 bool good = false;
634 std::string::size_type endPos = str.find(',');
635 if ( endPos == std::string::npos ) {
636 endPos = str.length();
637 }
639 if ( endPos != std::string::npos && endPos > 0 ) {
640 std::string xxx = str.substr( 0, endPos );
641 const gchar* ptr = xxx.c_str();
642 gchar* endPtr = 0;
643 numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
644 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
645 // overflow
646 } else if ( (numVal == 0) && (endPtr == ptr) ) {
647 // failed conversion
648 } else {
649 good = true;
650 str.erase( 0, endPos + 1 );
651 }
652 }
654 return good;
655 }
657 void ColorItem::_wireMagicColors( void* p )
658 {
659 JustForNow* onceMore = reinterpret_cast<JustForNow*>(p);
660 if ( onceMore )
661 {
662 for ( std::vector<ColorItem*>::iterator it = onceMore->_colors.begin(); it != onceMore->_colors.end(); ++it )
663 {
664 std::string::size_type pos = (*it)->def.descr.find("*{");
665 if ( pos != std::string::npos )
666 {
667 std::string subby = (*it)->def.descr.substr( pos + 2 );
668 std::string::size_type endPos = subby.find("}*");
669 if ( endPos != std::string::npos )
670 {
671 subby.erase( endPos );
672 //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
673 //g_message(" '%s'", subby.c_str());
675 if ( subby.find('E') != std::string::npos )
676 {
677 (*it)->def.setEditable( true );
678 }
680 if ( subby.find('L') != std::string::npos )
681 {
682 (*it)->_isLive = true;
683 }
685 std::string part;
686 // Tint. index + 1 more val.
687 if ( getBlock( part, 'T', subby ) ) {
688 guint64 colorIndex = 0;
689 if ( popVal( colorIndex, part ) ) {
690 guint64 percent = 0;
691 if ( popVal( percent, part ) ) {
692 (*it)->_linkTint( *(onceMore->_colors[colorIndex]), percent );
693 }
694 }
695 }
697 // Shade/tone. index + 1 or 2 more val.
698 if ( getBlock( part, 'S', subby ) ) {
699 guint64 colorIndex = 0;
700 if ( popVal( colorIndex, part ) ) {
701 guint64 percent = 0;
702 if ( popVal( percent, part ) ) {
703 guint64 grayLevel = 0;
704 if ( !popVal( grayLevel, part ) ) {
705 grayLevel = 0;
706 }
707 (*it)->_linkTone( *(onceMore->_colors[colorIndex]), percent, grayLevel );
708 }
709 }
710 }
712 }
713 }
714 }
715 }
716 }
719 void ColorItem::_linkTint( ColorItem& other, int percent )
720 {
721 if ( !_linkSrc )
722 {
723 other._listeners.push_back(this);
724 _linkIsTone = false;
725 _linkPercent = percent;
726 if ( _linkPercent > 100 )
727 _linkPercent = 100;
728 if ( _linkPercent < 0 )
729 _linkPercent = 0;
730 _linkGray = 0;
731 _linkSrc = &other;
733 ColorItem::_colorDefChanged(&other);
734 }
735 }
737 void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
738 {
739 if ( !_linkSrc )
740 {
741 other._listeners.push_back(this);
742 _linkIsTone = true;
743 _linkPercent = percent;
744 if ( _linkPercent > 100 )
745 _linkPercent = 100;
746 if ( _linkPercent < 0 )
747 _linkPercent = 0;
748 _linkGray = grayLevel;
749 _linkSrc = &other;
751 ColorItem::_colorDefChanged(&other);
752 }
753 }
756 void _loadPaletteFile( gchar const *filename )
757 {
758 char block[1024];
759 FILE *f = Inkscape::IO::fopen_utf8name( filename, "r" );
760 if ( f ) {
761 char* result = fgets( block, sizeof(block), f );
762 if ( result ) {
763 if ( strncmp( "GIMP Palette", block, 12 ) == 0 ) {
764 bool inHeader = true;
765 bool hasErr = false;
767 JustForNow *onceMore = new JustForNow();
769 do {
770 result = fgets( block, sizeof(block), f );
771 block[sizeof(block) - 1] = 0;
772 if ( result ) {
773 if ( block[0] == '#' ) {
774 // ignore comment
775 } else {
776 char *ptr = block;
777 // very simple check for header versus entry
778 while ( *ptr == ' ' || *ptr == '\t' ) {
779 ptr++;
780 }
781 if ( *ptr == 0 ) {
782 // blank line. skip it.
783 } else if ( '0' <= *ptr && *ptr <= '9' ) {
784 // should be an entry link
785 inHeader = false;
786 ptr = block;
787 Glib::ustring name("");
788 int r = 0;
789 int g = 0;
790 int b = 0;
791 skipWhitespace(ptr);
792 if ( *ptr ) {
793 hasErr = parseNum(ptr, r);
794 if ( !hasErr ) {
795 skipWhitespace(ptr);
796 hasErr = parseNum(ptr, g);
797 }
798 if ( !hasErr ) {
799 skipWhitespace(ptr);
800 hasErr = parseNum(ptr, b);
801 }
802 if ( !hasErr && *ptr ) {
803 char* n = trim(ptr);
804 if (n != NULL) {
805 name = n;
806 }
807 }
808 if ( !hasErr ) {
809 // Add the entry now
810 Glib::ustring nameStr(name);
811 ColorItem* item = new ColorItem( r, g, b, nameStr );
812 onceMore->_colors.push_back(item);
813 }
814 } else {
815 hasErr = true;
816 }
817 } else {
818 if ( !inHeader ) {
819 // Hmmm... probably bad. Not quite the format we want?
820 hasErr = true;
821 } else {
822 char* sep = strchr(result, ':');
823 if ( sep ) {
824 *sep = 0;
825 char* val = trim(sep + 1);
826 char* name = trim(result);
827 if ( *name ) {
828 if ( strcmp( "Name", name ) == 0 )
829 {
830 onceMore->_name = val;
831 }
832 else if ( strcmp( "Columns", name ) == 0 )
833 {
834 gchar* endPtr = 0;
835 guint64 numVal = g_ascii_strtoull( val, &endPtr, 10 );
836 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
837 // overflow
838 } else if ( (numVal == 0) && (endPtr == val) ) {
839 // failed conversion
840 } else {
841 onceMore->_prefWidth = numVal;
842 }
843 }
844 } else {
845 // error
846 hasErr = true;
847 }
848 } else {
849 // error
850 hasErr = true;
851 }
852 }
853 }
854 }
855 }
856 } while ( result && !hasErr );
857 if ( !hasErr ) {
858 possible.push_back(onceMore);
859 #if ENABLE_MAGIC_COLORS
860 ColorItem::_wireMagicColors( onceMore );
861 #endif // ENABLE_MAGIC_COLORS
862 } else {
863 delete onceMore;
864 }
865 }
866 }
868 fclose(f);
869 }
870 }
872 static void loadEmUp()
873 {
874 static bool beenHere = false;
875 if ( !beenHere ) {
876 beenHere = true;
878 std::list<gchar *> sources;
879 sources.push_back( profile_path("palettes") );
880 sources.push_back( g_strdup(INKSCAPE_PALETTESDIR) );
881 sources.push_back( g_strdup(CREATE_PALETTESDIR) );
883 // Use this loop to iterate through a list of possible document locations.
884 while (!sources.empty()) {
885 gchar *dirname = sources.front();
887 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
888 GError *err = 0;
889 GDir *directory = g_dir_open(dirname, 0, &err);
890 if (!directory) {
891 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
892 g_warning(_("Palettes directory (%s) is unavailable."), safeDir);
893 g_free(safeDir);
894 } else {
895 gchar *filename = 0;
896 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
897 gchar* lower = g_ascii_strdown( filename, -1 );
898 // if ( g_str_has_suffix(lower, ".gpl") ) {
899 gchar* full = g_build_filename(dirname, filename, NULL);
900 if ( !Inkscape::IO::file_test( full, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
901 _loadPaletteFile(full);
902 }
903 g_free(full);
904 // }
905 g_free(lower);
906 }
907 g_dir_close(directory);
908 }
909 }
911 // toss the dirname
912 g_free(dirname);
913 sources.pop_front();
914 }
915 }
916 }
926 SwatchesPanel& SwatchesPanel::getInstance()
927 {
928 if ( !instance ) {
929 instance = new SwatchesPanel();
930 }
932 return *instance;
933 }
937 /**
938 * Constructor
939 */
940 SwatchesPanel::SwatchesPanel(gchar const* prefsPath) :
941 Inkscape::UI::Widget::Panel( Glib::ustring(), prefsPath, true ),
942 _holder(0)
943 {
944 Gtk::RadioMenuItem* hotItem = 0;
945 _holder = new PreviewHolder();
946 loadEmUp();
948 if ( !possible.empty() ) {
949 JustForNow* first = 0;
950 gchar const* targetName = 0;
951 if ( _prefs_path ) {
952 targetName = prefs_get_string_attribute( _prefs_path, "palette" );
953 if ( targetName ) {
954 for ( std::vector<JustForNow*>::iterator iter = possible.begin(); iter != possible.end(); ++iter ) {
955 if ( (*iter)->_name == targetName ) {
956 first = *iter;
957 break;
958 }
959 }
960 }
961 }
963 if ( !first ) {
964 first = possible.front();
965 }
967 if ( first->_prefWidth > 0 ) {
968 _holder->setColumnPref( first->_prefWidth );
969 }
970 _holder->freezeUpdates();
971 for ( std::vector<ColorItem*>::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) {
972 _holder->addPreview(*it);
973 }
974 _holder->thawUpdates();
976 Gtk::RadioMenuItem::Group groupOne;
977 int i = 0;
978 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
979 JustForNow* curr = *it;
980 Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
981 if ( curr == first ) {
982 hotItem = single;
983 }
984 _regItem( single, 3, i );
985 i++;
986 }
988 }
991 _getContents()->pack_start(*_holder, Gtk::PACK_EXPAND_WIDGET);
992 _setTargetFillable(_holder);
994 show_all_children();
996 restorePanelPrefs();
997 if ( hotItem ) {
998 hotItem->set_active();
999 }
1000 }
1002 SwatchesPanel::~SwatchesPanel()
1003 {
1004 }
1006 void SwatchesPanel::setOrientation( Gtk::AnchorType how )
1007 {
1008 // Must call the parent class or bad things might happen
1009 Inkscape::UI::Widget::Panel::setOrientation( how );
1011 if ( _holder )
1012 {
1013 _holder->setOrientation( Gtk::ANCHOR_SOUTH );
1014 }
1015 }
1017 void SwatchesPanel::_handleAction( int setId, int itemId )
1018 {
1019 switch( setId ) {
1020 case 3:
1021 {
1022 if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
1023 _holder->clear();
1024 JustForNow* curr = possible[itemId];
1026 if ( _prefs_path ) {
1027 prefs_set_string_attribute( _prefs_path, "palette", curr->_name.c_str() );
1028 }
1030 if ( curr->_prefWidth > 0 ) {
1031 _holder->setColumnPref( curr->_prefWidth );
1032 }
1033 _holder->freezeUpdates();
1034 for ( std::vector<ColorItem*>::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) {
1035 _holder->addPreview(*it);
1036 }
1037 _holder->thawUpdates();
1038 }
1039 }
1040 break;
1041 }
1042 }
1044 } //namespace Dialogs
1045 } //namespace UI
1046 } //namespace Inkscape
1049 /*
1050 Local Variables:
1051 mode:c++
1052 c-file-style:"stroustrup"
1053 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1054 indent-tabs-mode:nil
1055 fill-column:99
1056 End:
1057 */
1058 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :