50da6153995ef1e41ba2b379e96117e4137389e5
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 static GdkAtom typeXColor = gdk_atom_intern("application/x-color", FALSE);
117 static GdkAtom typeText = gdk_atom_intern("text/plain", FALSE);
119 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
120 if ( info == TEXT_DATA ) {
121 gchar* tmp = g_strdup_printf("#%02x%02x%02x", item->def.getR(), item->def.getG(), item->def.getB() );
123 gtk_selection_data_set( data,
124 typeText,
125 8, // format
126 (guchar*)tmp,
127 strlen((const char*)tmp) + 1);
128 g_free(tmp);
129 tmp = 0;
130 } else if ( info == APP_X_INKY_COLOR ) {
131 Glib::ustring paletteName;
133 // Find where this thing came from
134 bool found = false;
135 int index = 0;
136 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end() && !found; ++it ) {
137 JustForNow* curr = *it;
138 index = 0;
139 for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
140 if ( item == *zz ) {
141 found = true;
142 paletteName = curr->_name;
143 break;
144 } else {
145 index++;
146 }
147 }
148 }
150 // if ( found ) {
151 // g_message("Found the color at entry %d in palette '%s'", index, paletteName.c_str() );
152 // } else {
153 // g_message("Unable to find the color");
154 // }
155 int itemCount = 4 + 2 + 1 + paletteName.length();
157 guint16* tmp = new guint16[itemCount];
158 tmp[0] = (item->def.getR() << 8) | item->def.getR();
159 tmp[1] = (item->def.getG() << 8) | item->def.getG();
160 tmp[2] = (item->def.getB() << 8) | item->def.getB();
161 tmp[3] = 0xffff;
162 tmp[4] = (item->_isLive || !item->_listeners.empty() || (item->_linkSrc != 0) ) ? 1 : 0;
164 tmp[5] = index;
165 tmp[6] = paletteName.length();
166 for ( unsigned int i = 0; i < paletteName.length(); i++ ) {
167 tmp[7 + i] = paletteName[i];
168 }
169 gtk_selection_data_set( data,
170 typeXColor,
171 16, // format
172 reinterpret_cast<const guchar*>(tmp),
173 itemCount * 2);
174 delete[] tmp;
175 } else {
176 guint16 tmp[4];
177 tmp[0] = (item->def.getR() << 8) | item->def.getR();
178 tmp[1] = (item->def.getG() << 8) | item->def.getG();
179 tmp[2] = (item->def.getB() << 8) | item->def.getB();
180 tmp[3] = 0xffff;
181 gtk_selection_data_set( data,
182 typeXColor,
183 16, // format
184 reinterpret_cast<const guchar*>(tmp),
185 (3+1) * 2);
186 }
187 }
189 static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
190 {
191 ColorItem* item = reinterpret_cast<ColorItem*>(data);
192 if ( item )
193 {
194 Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, 32, 24 );
195 guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
196 | (0x00ff0000 & (item->def.getG() << 16))
197 | (0x0000ff00 & (item->def.getB() << 8));
198 thumb->fill( fillWith );
199 gtk_drag_set_icon_pixbuf( dc, thumb->gobj(), 0, 0 );
200 }
202 }
204 //"drag-drop"
205 // gboolean dragDropColorData( GtkWidget *widget,
206 // GdkDragContext *drag_context,
207 // gint x,
208 // gint y,
209 // guint time,
210 // gpointer user_data)
211 // {
212 // // TODO finish
214 // return TRUE;
215 // }
217 static void bouncy( GtkWidget* widget, gpointer callback_data ) {
218 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
219 if ( item ) {
220 item->buttonClicked(false);
221 }
222 }
224 static void bouncy2( GtkWidget* widget, gint arg1, gpointer callback_data ) {
225 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
226 if ( item ) {
227 item->buttonClicked(true);
228 }
229 }
231 static void dieDieDie( GtkObject *obj, gpointer user_data )
232 {
233 g_message("die die die %p %p", obj, user_data );
234 }
236 static const GtkTargetEntry destColorTargets[] = {
237 #if ENABLE_MAGIC_COLORS
238 // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID},
239 {"application/x-inkscape-color", 0, APP_X_INKY_COLOR},
240 #endif // ENABLE_MAGIC_COLORS
241 {"application/x-color", 0, APP_X_COLOR},
242 };
244 #include "color.h" // for SP_RGBA32_U_COMPOSE
246 void ColorItem::_dropDataIn( GtkWidget *widget,
247 GdkDragContext *drag_context,
248 gint x, gint y,
249 GtkSelectionData *data,
250 guint info,
251 guint event_time,
252 gpointer user_data)
253 {
254 // g_message(" droppy droppy %d", info);
255 switch (info) {
256 case APP_X_INKY_COLOR:
257 {
258 if ( data->length >= 8 ) {
259 // Careful about endian issues.
260 guint16* dataVals = (guint16*)data->data;
261 if ( user_data ) {
262 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
263 if ( item->def.isEditable() ) {
264 // Shove on in the new value
265 item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
266 }
267 }
268 }
269 break;
270 }
271 case APP_X_COLOR:
272 {
273 if ( data->length == 8 ) {
274 // Careful about endian issues.
275 guint16* dataVals = (guint16*)data->data;
276 // {
277 // gchar c[64] = {0};
278 // sp_svg_write_color( c, 64,
279 // SP_RGBA32_U_COMPOSE(
280 // 0x0ff & (dataVals[0] >> 8),
281 // 0x0ff & (dataVals[1] >> 8),
282 // 0x0ff & (dataVals[2] >> 8),
283 // 0xff // can't have transparency in the color itself
284 // //0x0ff & (data->data[3] >> 8),
285 // ));
286 // }
287 if ( user_data ) {
288 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
289 if ( item->def.isEditable() ) {
290 // Shove on in the new value
291 item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
292 }
293 }
294 }
295 break;
296 }
297 default:
298 g_message("unknown drop type");
299 }
301 }
303 static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::ustring const& match, int r, int g, int b )
304 {
305 bool changed = false;
307 if ( node ) {
308 gchar const * val = node->attribute("inkscape:x-fill-tag");
309 if ( val && (match == val) ) {
310 SPObject *obj = document->getObjectByRepr( node );
312 gchar c[64] = {0};
313 sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
314 SPCSSAttr *css = sp_repr_css_attr_new();
315 sp_repr_css_set_property( css, "fill", c );
317 sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
318 ((SPItem*)obj)->updateRepr();
320 changed = true;
321 }
323 val = node->attribute("inkscape:x-stroke-tag");
324 if ( val && (match == val) ) {
325 SPObject *obj = document->getObjectByRepr( node );
327 gchar c[64] = {0};
328 sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
329 SPCSSAttr *css = sp_repr_css_attr_new();
330 sp_repr_css_set_property( css, "stroke", c );
332 sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
333 ((SPItem*)obj)->updateRepr();
335 changed = true;
336 }
338 Inkscape::XML::Node* first = node->firstChild();
339 changed |= bruteForce( document, first, match, r, g, b );
341 changed |= bruteForce( document, node->next(), match, r, g, b );
342 }
344 return changed;
345 }
347 void ColorItem::_colorDefChanged(void* data)
348 {
349 ColorItem* item = reinterpret_cast<ColorItem*>(data);
350 if ( item ) {
351 for ( std::vector<Gtk::Widget*>::iterator it = item->_previews.begin(); it != item->_previews.end(); ++it ) {
352 Gtk::Widget* widget = *it;
353 if ( IS_EEK_PREVIEW(widget->gobj()) ) {
354 EekPreview * preview = EEK_PREVIEW(widget->gobj());
355 eek_preview_set_color( preview,
356 (item->def.getR() << 8) | item->def.getR(),
357 (item->def.getG() << 8) | item->def.getG(),
358 (item->def.getB() << 8) | item->def.getB() );
360 eek_preview_set_linked( preview, (LinkType)((item->_linkSrc ? PREVIEW_LINK_IN:0)
361 | (item->_listeners.empty() ? 0:PREVIEW_LINK_OUT)
362 | (item->_isLive ? PREVIEW_LINK_OTHER:0)) );
364 widget->queue_draw();
365 }
366 }
368 for ( std::vector<ColorItem*>::iterator it = item->_listeners.begin(); it != item->_listeners.end(); ++it ) {
369 guint r = item->def.getR();
370 guint g = item->def.getG();
371 guint b = item->def.getB();
373 if ( (*it)->_linkIsTone ) {
374 r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
375 g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
376 b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
377 } else {
378 r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
379 g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
380 b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
381 }
383 (*it)->def.setRGB( r, g, b );
384 }
387 // Look for objects using this color
388 {
389 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
390 if ( desktop ) {
391 SPDocument* document = sp_desktop_document( desktop );
392 Inkscape::XML::Node *rroot = sp_document_repr_root( document );
393 if ( rroot ) {
395 // Find where this thing came from
396 Glib::ustring paletteName;
397 bool found = false;
398 int index = 0;
399 for ( std::vector<JustForNow*>::iterator it2 = possible.begin(); it2 != possible.end() && !found; ++it2 ) {
400 JustForNow* curr = *it2;
401 index = 0;
402 for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
403 if ( item == *zz ) {
404 found = true;
405 paletteName = curr->_name;
406 break;
407 } else {
408 index++;
409 }
410 }
411 }
413 if ( !paletteName.empty() ) {
414 gchar* str = g_strdup_printf("%d|", index);
415 paletteName.insert( 0, str );
416 g_free(str);
417 str = 0;
419 if ( bruteForce( document, rroot, paletteName, item->def.getR(), item->def.getG(), item->def.getB() ) ) {
420 sp_document_done( document );
421 }
422 }
423 }
424 }
425 }
426 }
427 }
430 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::IconSize size)
431 {
432 Gtk::Widget* widget = 0;
433 if ( style == PREVIEW_STYLE_BLURB ) {
434 Gtk::Label *lbl = new Gtk::Label(def.descr);
435 lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
436 widget = lbl;
437 } else {
438 Glib::ustring blank(" ");
439 if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
440 blank = " ";
441 }
443 GtkWidget* eekWidget = eek_preview_new();
444 EekPreview * preview = EEK_PREVIEW(eekWidget);
445 Gtk::Widget* newBlot = Glib::wrap(eekWidget);
447 eek_preview_set_color( preview, (def.getR() << 8) | def.getR(), (def.getG() << 8) | def.getG(), (def.getB() << 8) | def.getB());
449 eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::GtkIconSize)size );
450 eek_preview_set_linked( preview, (LinkType)((_linkSrc ? PREVIEW_LINK_IN:0)
451 | (_listeners.empty() ? 0:PREVIEW_LINK_OUT)
452 | (_isLive ? PREVIEW_LINK_OTHER:0)) );
454 def.addCallback( _colorDefChanged, this );
456 GValue val = {0, {{0}, {0}}};
457 g_value_init( &val, G_TYPE_BOOLEAN );
458 g_value_set_boolean( &val, FALSE );
459 g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
461 /*
462 Gtk::Button *btn = new Gtk::Button(blank);
463 Gdk::Color color;
464 color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
465 btn->modify_bg(Gtk::STATE_NORMAL, color);
466 btn->modify_bg(Gtk::STATE_ACTIVE, color);
467 btn->modify_bg(Gtk::STATE_PRELIGHT, color);
468 btn->modify_bg(Gtk::STATE_SELECTED, color);
470 Gtk::Widget* newBlot = btn;
471 */
473 tips.set_tip((*newBlot), def.descr);
475 /*
476 newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
478 sigc::signal<void> type_signal_something;
479 */
480 g_signal_connect( G_OBJECT(newBlot->gobj()),
481 "clicked",
482 G_CALLBACK(bouncy),
483 this);
485 g_signal_connect( G_OBJECT(newBlot->gobj()),
486 "alt-clicked",
487 G_CALLBACK(bouncy2),
488 this);
490 gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
491 GDK_BUTTON1_MASK,
492 sourceColorEntries,
493 G_N_ELEMENTS(sourceColorEntries),
494 GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
496 g_signal_connect( G_OBJECT(newBlot->gobj()),
497 "drag-data-get",
498 G_CALLBACK(ColorItem::_dragGetColorData),
499 this);
501 g_signal_connect( G_OBJECT(newBlot->gobj()),
502 "drag-begin",
503 G_CALLBACK(dragBegin),
504 this );
506 // g_signal_connect( G_OBJECT(newBlot->gobj()),
507 // "drag-drop",
508 // G_CALLBACK(dragDropColorData),
509 // this);
511 if ( def.isEditable() )
512 {
513 gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
514 GTK_DEST_DEFAULT_ALL,
515 destColorTargets,
516 G_N_ELEMENTS(destColorTargets),
517 GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
520 g_signal_connect( G_OBJECT(newBlot->gobj()),
521 "drag-data-received",
522 G_CALLBACK(_dropDataIn),
523 this );
524 }
526 g_signal_connect( G_OBJECT(newBlot->gobj()),
527 "destroy",
528 G_CALLBACK(dieDieDie),
529 this);
532 widget = newBlot;
533 }
535 _previews.push_back( widget );
537 return widget;
538 }
540 void ColorItem::buttonClicked(bool secondary)
541 {
542 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
543 if (desktop) {
544 char const * attrName = secondary ? "stroke" : "fill";
545 guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
546 gchar c[64];
547 sp_svg_write_color(c, 64, rgba);
549 SPCSSAttr *css = sp_repr_css_attr_new();
550 sp_repr_css_set_property( css, attrName, c );
551 sp_desktop_set_style(desktop, css);
553 sp_repr_css_attr_unref(css);
554 sp_document_done (sp_desktop_document (desktop));
555 }
556 }
561 static char* trim( char* str ) {
562 char* ret = str;
563 while ( *str && (*str == ' ' || *str == '\t') ) {
564 str++;
565 }
566 ret = str;
567 while ( *str ) {
568 str++;
569 }
570 str--;
571 while ( str > ret && ( *str == ' ' || *str == '\t' ) || *str == '\r' || *str == '\n' ) {
572 *str-- = 0;
573 }
574 return ret;
575 }
577 void skipWhitespace( char*& str ) {
578 while ( *str == ' ' || *str == '\t' ) {
579 str++;
580 }
581 }
583 bool parseNum( char*& str, int& val ) {
584 val = 0;
585 while ( '0' <= *str && *str <= '9' ) {
586 val = val * 10 + (*str - '0');
587 str++;
588 }
589 bool retval = !(*str == 0 || *str == ' ' || *str == '\t' || *str == '\r' || *str == '\n');
590 return retval;
591 }
594 static bool getBlock( std::string& dst, guchar ch, std::string const str )
595 {
596 bool good = false;
597 size_t pos = str.find(ch);
598 if ( pos != std::string::npos )
599 {
600 size_t pos2 = str.find( '(', pos );
601 if ( pos2 != std::string::npos ) {
602 size_t endPos = str.find( ')', pos2 );
603 if ( endPos != std::string::npos ) {
604 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
605 good = true;
606 }
607 }
608 }
609 return good;
610 }
612 static bool popVal( guint64& numVal, std::string& str )
613 {
614 bool good = false;
615 size_t endPos = str.find(',');
616 if ( endPos == std::string::npos ) {
617 endPos = str.length();
618 }
620 if ( endPos != std::string::npos && endPos > 0 ) {
621 std::string xxx = str.substr( 0, endPos );
622 const gchar* ptr = xxx.c_str();
623 gchar* endPtr = 0;
624 numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
625 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
626 // overflow
627 } else if ( (numVal == 0) && (endPtr == ptr) ) {
628 // failed conversion
629 } else {
630 good = true;
631 str.erase( 0, endPos + 1 );
632 }
633 }
635 return good;
636 }
638 void ColorItem::_wireMagicColors( void* p )
639 {
640 JustForNow* onceMore = reinterpret_cast<JustForNow*>(p);
641 if ( onceMore )
642 {
643 for ( std::vector<ColorItem*>::iterator it = onceMore->_colors.begin(); it != onceMore->_colors.end(); ++it )
644 {
645 size_t pos = (*it)->def.descr.find("*{");
646 if ( pos != std::string::npos )
647 {
648 std::string subby = (*it)->def.descr.substr( pos + 2 );
649 size_t endPos = subby.find("}*");
650 if ( endPos != std::string::npos )
651 {
652 subby.erase( endPos );
653 //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
654 //g_message(" '%s'", subby.c_str());
656 if ( subby.find('E') != std::string::npos )
657 {
658 (*it)->def.setEditable( true );
659 }
661 if ( subby.find('L') != std::string::npos )
662 {
663 (*it)->_isLive = true;
664 }
666 std::string part;
667 // Tint. index + 1 more val.
668 if ( getBlock( part, 'T', subby ) ) {
669 guint64 colorIndex = 0;
670 if ( popVal( colorIndex, part ) ) {
671 guint64 percent = 0;
672 if ( popVal( percent, part ) ) {
673 (*it)->_linkTint( *(onceMore->_colors[colorIndex]), percent );
674 }
675 }
676 }
678 // Shade/tone. index + 1 or 2 more val.
679 if ( getBlock( part, 'S', subby ) ) {
680 guint64 colorIndex = 0;
681 if ( popVal( colorIndex, part ) ) {
682 guint64 percent = 0;
683 if ( popVal( percent, part ) ) {
684 guint64 grayLevel = 0;
685 if ( !popVal( grayLevel, part ) ) {
686 grayLevel = 0;
687 }
688 (*it)->_linkTone( *(onceMore->_colors[colorIndex]), percent, grayLevel );
689 }
690 }
691 }
693 }
694 }
695 }
696 }
697 }
700 void ColorItem::_linkTint( ColorItem& other, int percent )
701 {
702 if ( !_linkSrc )
703 {
704 other._listeners.push_back(this);
705 _linkIsTone = false;
706 _linkPercent = percent;
707 if ( _linkPercent > 100 )
708 _linkPercent = 100;
709 if ( _linkPercent < 0 )
710 _linkPercent = 0;
711 _linkGray = 0;
712 _linkSrc = &other;
714 ColorItem::_colorDefChanged(&other);
715 }
716 }
718 void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
719 {
720 if ( !_linkSrc )
721 {
722 other._listeners.push_back(this);
723 _linkIsTone = true;
724 _linkPercent = percent;
725 if ( _linkPercent > 100 )
726 _linkPercent = 100;
727 if ( _linkPercent < 0 )
728 _linkPercent = 0;
729 _linkGray = grayLevel;
730 _linkSrc = &other;
732 ColorItem::_colorDefChanged(&other);
733 }
734 }
737 void _loadPaletteFile( gchar const *filename )
738 {
739 char block[1024];
740 FILE *f = Inkscape::IO::fopen_utf8name( filename, "r" );
741 if ( f ) {
742 char* result = fgets( block, sizeof(block), f );
743 if ( result ) {
744 if ( strncmp( "GIMP Palette", block, 12 ) == 0 ) {
745 bool inHeader = true;
746 bool hasErr = false;
748 JustForNow *onceMore = new JustForNow();
750 do {
751 result = fgets( block, sizeof(block), f );
752 block[sizeof(block) - 1] = 0;
753 if ( result ) {
754 if ( block[0] == '#' ) {
755 // ignore comment
756 } else {
757 char *ptr = block;
758 // very simple check for header versus entry
759 while ( *ptr == ' ' || *ptr == '\t' ) {
760 ptr++;
761 }
762 if ( *ptr == 0 ) {
763 // blank line. skip it.
764 } else if ( '0' <= *ptr && *ptr <= '9' ) {
765 // should be an entry link
766 inHeader = false;
767 ptr = block;
768 Glib::ustring name("");
769 int r = 0;
770 int g = 0;
771 int b = 0;
772 skipWhitespace(ptr);
773 if ( *ptr ) {
774 hasErr = parseNum(ptr, r);
775 if ( !hasErr ) {
776 skipWhitespace(ptr);
777 hasErr = parseNum(ptr, g);
778 }
779 if ( !hasErr ) {
780 skipWhitespace(ptr);
781 hasErr = parseNum(ptr, b);
782 }
783 if ( !hasErr && *ptr ) {
784 char* n = trim(ptr);
785 if (n != NULL) {
786 name = n;
787 }
788 }
789 if ( !hasErr ) {
790 // Add the entry now
791 Glib::ustring nameStr(name);
792 ColorItem* item = new ColorItem( r, g, b, nameStr );
793 onceMore->_colors.push_back(item);
794 }
795 } else {
796 hasErr = true;
797 }
798 } else {
799 if ( !inHeader ) {
800 // Hmmm... probably bad. Not quite the format we want?
801 hasErr = true;
802 } else {
803 char* sep = strchr(result, ':');
804 if ( sep ) {
805 *sep = 0;
806 char* val = trim(sep + 1);
807 char* name = trim(result);
808 if ( *name ) {
809 if ( strcmp( "Name", name ) == 0 )
810 {
811 onceMore->_name = val;
812 }
813 else if ( strcmp( "Columns", name ) == 0 )
814 {
815 gchar* endPtr = 0;
816 guint64 numVal = g_ascii_strtoull( val, &endPtr, 10 );
817 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
818 // overflow
819 } else if ( (numVal == 0) && (endPtr == val) ) {
820 // failed conversion
821 } else {
822 onceMore->_prefWidth = numVal;
823 }
824 }
825 } else {
826 // error
827 hasErr = true;
828 }
829 } else {
830 // error
831 hasErr = true;
832 }
833 }
834 }
835 }
836 }
837 } while ( result && !hasErr );
838 if ( !hasErr ) {
839 possible.push_back(onceMore);
840 #if ENABLE_MAGIC_COLORS
841 ColorItem::_wireMagicColors( onceMore );
842 #endif // ENABLE_MAGIC_COLORS
843 } else {
844 delete onceMore;
845 }
846 }
847 }
849 fclose(f);
850 }
851 }
853 static void loadEmUp()
854 {
855 static bool beenHere = false;
856 if ( !beenHere ) {
857 beenHere = true;
859 std::list<gchar *> sources;
860 sources.push_back( profile_path("palettes") );
861 sources.push_back( g_strdup(INKSCAPE_PALETTESDIR) );
862 sources.push_back( g_strdup(CREATE_PALETTESDIR) );
864 // Use this loop to iterate through a list of possible document locations.
865 while (!sources.empty()) {
866 gchar *dirname = sources.front();
868 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
869 GError *err = 0;
870 GDir *directory = g_dir_open(dirname, 0, &err);
871 if (!directory) {
872 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
873 g_warning(_("Palettes directory (%s) is unavailable."), safeDir);
874 g_free(safeDir);
875 } else {
876 gchar *filename = 0;
877 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
878 gchar* lower = g_ascii_strdown( filename, -1 );
879 // if ( g_str_has_suffix(lower, ".gpl") ) {
880 gchar* full = g_build_filename(dirname, filename, NULL);
881 if ( !Inkscape::IO::file_test( full, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
882 _loadPaletteFile(full);
883 }
884 g_free(full);
885 // }
886 g_free(lower);
887 }
888 g_dir_close(directory);
889 }
890 }
892 // toss the dirname
893 g_free(dirname);
894 sources.pop_front();
895 }
896 }
897 }
907 SwatchesPanel& SwatchesPanel::getInstance()
908 {
909 if ( !instance ) {
910 instance = new SwatchesPanel();
911 }
913 return *instance;
914 }
918 /**
919 * Constructor
920 */
921 SwatchesPanel::SwatchesPanel(gchar const* prefsPath) :
922 Inkscape::UI::Widget::Panel( Glib::ustring(), prefsPath, true ),
923 _holder(0)
924 {
925 Gtk::RadioMenuItem* hotItem = 0;
926 _holder = new PreviewHolder();
927 loadEmUp();
929 if ( !possible.empty() ) {
930 JustForNow* first = 0;
931 gchar const* targetName = 0;
932 if ( _prefs_path ) {
933 targetName = prefs_get_string_attribute( _prefs_path, "palette" );
934 if ( targetName ) {
935 for ( std::vector<JustForNow*>::iterator iter = possible.begin(); iter != possible.end(); ++iter ) {
936 if ( (*iter)->_name == targetName ) {
937 first = *iter;
938 break;
939 }
940 }
941 }
942 }
944 if ( !first ) {
945 first = possible.front();
946 }
948 if ( first->_prefWidth > 0 ) {
949 _holder->setColumnPref( first->_prefWidth );
950 }
951 _holder->freezeUpdates();
952 for ( std::vector<ColorItem*>::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) {
953 _holder->addPreview(*it);
954 }
955 _holder->thawUpdates();
957 Gtk::RadioMenuItem::Group groupOne;
958 int i = 0;
959 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
960 JustForNow* curr = *it;
961 Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
962 if ( curr == first ) {
963 hotItem = single;
964 }
965 _regItem( single, 3, i );
966 i++;
967 }
969 }
972 _getContents()->pack_start(*_holder, Gtk::PACK_EXPAND_WIDGET);
973 _setTargetFillable(_holder);
975 show_all_children();
977 restorePanelPrefs();
978 if ( hotItem ) {
979 hotItem->set_active();
980 }
981 }
983 SwatchesPanel::~SwatchesPanel()
984 {
985 }
987 void SwatchesPanel::setOrientation( Gtk::AnchorType how )
988 {
989 // Must call the parent class or bad things might happen
990 Inkscape::UI::Widget::Panel::setOrientation( how );
992 if ( _holder )
993 {
994 _holder->setOrientation( Gtk::ANCHOR_SOUTH );
995 }
996 }
998 void SwatchesPanel::_handleAction( int setId, int itemId )
999 {
1000 switch( setId ) {
1001 case 3:
1002 {
1003 if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
1004 _holder->clear();
1005 JustForNow* curr = possible[itemId];
1007 if ( _prefs_path ) {
1008 prefs_set_string_attribute( _prefs_path, "palette", curr->_name.c_str() );
1009 }
1011 if ( curr->_prefWidth > 0 ) {
1012 _holder->setColumnPref( curr->_prefWidth );
1013 }
1014 _holder->freezeUpdates();
1015 for ( std::vector<ColorItem*>::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) {
1016 _holder->addPreview(*it);
1017 }
1018 _holder->thawUpdates();
1019 }
1020 }
1021 break;
1022 }
1023 }
1025 } //namespace Dialogs
1026 } //namespace UI
1027 } //namespace Inkscape
1030 /*
1031 Local Variables:
1032 mode:c++
1033 c-file-style:"stroustrup"
1034 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1035 indent-tabs-mode:nil
1036 fill-column:99
1037 End:
1038 */
1039 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :