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"
34 #include "eek-preview.h"
36 namespace Inkscape {
37 namespace UI {
38 namespace Dialogs {
40 SwatchesPanel* SwatchesPanel::instance = 0;
43 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
44 def( r, g, b, name ),
45 _isLive(false),
46 _linkIsTone(false),
47 _linkPercent(0),
48 _linkGray(0),
49 _linkSrc(0)
50 {
51 }
53 ColorItem::~ColorItem()
54 {
55 }
57 ColorItem::ColorItem(ColorItem const &other) :
58 Inkscape::UI::Previewable()
59 {
60 if ( this != &other ) {
61 *this = other;
62 }
63 }
65 ColorItem &ColorItem::operator=(ColorItem const &other)
66 {
67 if ( this != &other ) {
68 def = other.def;
70 // TODO - correct linkage
71 _linkSrc = other._linkSrc;
72 g_message("Erk!");
73 }
74 return *this;
75 }
78 class JustForNow
79 {
80 public:
81 JustForNow() : _prefWidth(0) {}
83 Glib::ustring _name;
84 int _prefWidth;
85 std::vector<ColorItem*> _colors;
86 };
88 static std::vector<JustForNow*> possible;
92 typedef enum {
93 APP_X_INKY_COLOR_ID = 0,
94 APP_X_INKY_COLOR = 0,
95 APP_X_COLOR,
96 TEXT_DATA
97 } colorFlavorType;
99 static const GtkTargetEntry sourceColorEntries[] = {
100 #if ENABLE_LCMS
101 // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID},
102 {"application/x-inkscape-color", 0, APP_X_INKY_COLOR},
103 #endif // ENABLE_LCMS
104 {"application/x-color", 0, APP_X_COLOR},
105 {"text/plain", 0, TEXT_DATA},
106 };
108 void ColorItem::_dragGetColorData( GtkWidget *widget,
109 GdkDragContext *drag_context,
110 GtkSelectionData *data,
111 guint info,
112 guint time,
113 gpointer user_data)
114 {
115 static GdkAtom typeXColor = gdk_atom_intern("application/x-color", FALSE);
116 static GdkAtom typeText = gdk_atom_intern("text/plain", FALSE);
118 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
119 if ( info == TEXT_DATA ) {
120 gchar* tmp = g_strdup_printf("#%02x%02x%02x", item->def.getR(), item->def.getG(), item->def.getB() );
122 gtk_selection_data_set( data,
123 typeText,
124 8, // format
125 (guchar*)tmp,
126 strlen((const char*)tmp) + 1);
127 g_free(tmp);
128 tmp = 0;
129 } else if ( info == APP_X_INKY_COLOR ) {
130 Glib::ustring paletteName;
132 // Find where this thing came from
133 bool found = false;
134 int index = 0;
135 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end() && !found; ++it ) {
136 JustForNow* curr = *it;
137 index = 0;
138 for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
139 if ( item == *zz ) {
140 found = true;
141 paletteName = curr->_name;
142 break;
143 } else {
144 index++;
145 }
146 }
147 }
149 // if ( found ) {
150 // g_message("Found the color at entry %d in palette '%s'", index, paletteName.c_str() );
151 // } else {
152 // g_message("Unable to find the color");
153 // }
154 int itemCount = 4 + 2 + 1 + paletteName.length();
156 guint16* tmp = new guint16[itemCount];
157 tmp[0] = (item->def.getR() << 8) | item->def.getR();
158 tmp[1] = (item->def.getG() << 8) | item->def.getG();
159 tmp[2] = (item->def.getB() << 8) | item->def.getB();
160 tmp[3] = 0xffff;
161 tmp[4] = (item->_isLive || !item->_listeners.empty() || (item->_linkSrc != 0) ) ? 1 : 0;
163 tmp[5] = index;
164 tmp[6] = paletteName.length();
165 for ( unsigned int i = 0; i < paletteName.length(); i++ ) {
166 tmp[7 + i] = paletteName[i];
167 }
168 gtk_selection_data_set( data,
169 typeXColor,
170 16, // format
171 reinterpret_cast<const guchar*>(tmp),
172 itemCount * 2);
173 delete[] tmp;
174 } else {
175 guint16 tmp[4];
176 tmp[0] = (item->def.getR() << 8) | item->def.getR();
177 tmp[1] = (item->def.getG() << 8) | item->def.getG();
178 tmp[2] = (item->def.getB() << 8) | item->def.getB();
179 tmp[3] = 0xffff;
180 gtk_selection_data_set( data,
181 typeXColor,
182 16, // format
183 reinterpret_cast<const guchar*>(tmp),
184 (3+1) * 2);
185 }
186 }
188 static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
189 {
190 ColorItem* item = reinterpret_cast<ColorItem*>(data);
191 if ( item )
192 {
193 Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, 32, 24 );
194 guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
195 | (0x00ff0000 & (item->def.getG() << 16))
196 | (0x0000ff00 & (item->def.getB() << 8));
197 thumb->fill( fillWith );
198 gtk_drag_set_icon_pixbuf( dc, thumb->gobj(), 0, 0 );
199 }
201 }
203 //"drag-drop"
204 // gboolean dragDropColorData( GtkWidget *widget,
205 // GdkDragContext *drag_context,
206 // gint x,
207 // gint y,
208 // guint time,
209 // gpointer user_data)
210 // {
211 // // TODO finish
213 // return TRUE;
214 // }
216 static void bouncy( GtkWidget* widget, gpointer callback_data ) {
217 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
218 if ( item ) {
219 item->buttonClicked(false);
220 }
221 }
223 static void bouncy2( GtkWidget* widget, gint arg1, gpointer callback_data ) {
224 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
225 if ( item ) {
226 item->buttonClicked(true);
227 }
228 }
230 static void dieDieDie( GtkObject *obj, gpointer user_data )
231 {
232 g_message("die die die %p %p", obj, user_data );
233 }
235 static const GtkTargetEntry destColorTargets[] = {
236 #if ENABLE_LCMS
237 // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID},
238 {"application/x-inkscape-color", 0, APP_X_INKY_COLOR},
239 #endif // ENABLE_LCMS
240 {"application/x-color", 0, APP_X_COLOR},
241 };
243 #include "color.h" // for SP_RGBA32_U_COMPOSE
245 void ColorItem::_dropDataIn( GtkWidget *widget,
246 GdkDragContext *drag_context,
247 gint x, gint y,
248 GtkSelectionData *data,
249 guint info,
250 guint event_time,
251 gpointer user_data)
252 {
253 // g_message(" droppy droppy %d", info);
254 switch (info) {
255 case APP_X_INKY_COLOR:
256 {
257 if ( data->length >= 8 ) {
258 // Careful about endian issues.
259 guint16* dataVals = (guint16*)data->data;
260 if ( user_data ) {
261 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
262 if ( item->def.isEditable() ) {
263 // Shove on in the new value
264 item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
265 }
266 }
267 }
268 break;
269 }
270 case APP_X_COLOR:
271 {
272 if ( data->length == 8 ) {
273 // Careful about endian issues.
274 guint16* dataVals = (guint16*)data->data;
275 // {
276 // gchar c[64] = {0};
277 // sp_svg_write_color( c, 64,
278 // SP_RGBA32_U_COMPOSE(
279 // 0x0ff & (dataVals[0] >> 8),
280 // 0x0ff & (dataVals[1] >> 8),
281 // 0x0ff & (dataVals[2] >> 8),
282 // 0xff // can't have transparency in the color itself
283 // //0x0ff & (data->data[3] >> 8),
284 // ));
285 // }
286 if ( user_data ) {
287 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
288 if ( item->def.isEditable() ) {
289 // Shove on in the new value
290 item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
291 }
292 }
293 }
294 break;
295 }
296 default:
297 g_message("unknown drop type");
298 }
300 }
302 static bool bruteForce( SPDocument* document, Inkscape::XML::Node* node, Glib::ustring const& match, int r, int g, int b )
303 {
304 bool changed = false;
306 if ( node ) {
307 gchar const * val = node->attribute("inkscape:x-fill-tag");
308 if ( val && (match == val) ) {
309 SPObject *obj = document->getObjectByRepr( node );
311 gchar c[64] = {0};
312 sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
313 SPCSSAttr *css = sp_repr_css_attr_new();
314 sp_repr_css_set_property( css, "fill", c );
316 sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
317 ((SPItem*)obj)->updateRepr();
319 changed = true;
320 }
322 val = node->attribute("inkscape:x-stroke-tag");
323 if ( val && (match == val) ) {
324 SPObject *obj = document->getObjectByRepr( node );
326 gchar c[64] = {0};
327 sp_svg_write_color( c, 64, SP_RGBA32_U_COMPOSE( r, g, b, 0xff ) );
328 SPCSSAttr *css = sp_repr_css_attr_new();
329 sp_repr_css_set_property( css, "stroke", c );
331 sp_desktop_apply_css_recursive( (SPItem*)obj, css, true );
332 ((SPItem*)obj)->updateRepr();
334 changed = true;
335 }
337 Inkscape::XML::Node* first = node->firstChild();
338 changed |= bruteForce( document, first, match, r, g, b );
340 changed |= bruteForce( document, node->next(), match, r, g, b );
341 }
343 return changed;
344 }
346 void ColorItem::_colorDefChanged(void* data)
347 {
348 ColorItem* item = reinterpret_cast<ColorItem*>(data);
349 if ( item ) {
350 for ( std::vector<Gtk::Widget*>::iterator it = item->_previews.begin(); it != item->_previews.end(); ++it ) {
351 Gtk::Widget* widget = *it;
352 if ( IS_EEK_PREVIEW(widget->gobj()) ) {
353 EekPreview * preview = EEK_PREVIEW(widget->gobj());
354 eek_preview_set_color( preview,
355 (item->def.getR() << 8) | item->def.getR(),
356 (item->def.getG() << 8) | item->def.getG(),
357 (item->def.getB() << 8) | item->def.getB() );
359 eek_preview_set_linked( preview, (LinkType)((item->_linkSrc ? PREVIEW_LINK_IN:0)
360 | (item->_listeners.empty() ? 0:PREVIEW_LINK_OUT)
361 | (item->_isLive ? PREVIEW_LINK_OTHER:0)) );
363 widget->queue_draw();
364 }
365 }
367 for ( std::vector<ColorItem*>::iterator it = item->_listeners.begin(); it != item->_listeners.end(); ++it ) {
368 guint r = item->def.getR();
369 guint g = item->def.getG();
370 guint b = item->def.getB();
372 if ( (*it)->_linkIsTone ) {
373 r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
374 g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
375 b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
376 } else {
377 r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
378 g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
379 b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
380 }
382 (*it)->def.setRGB( r, g, b );
383 }
386 // Look for objects using this color
387 {
388 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
389 if ( desktop ) {
390 SPDocument* document = SP_DT_DOCUMENT( desktop );
391 Inkscape::XML::Node *rroot = sp_document_repr_root( document );
392 if ( rroot ) {
394 // Find where this thing came from
395 Glib::ustring paletteName;
396 bool found = false;
397 int index = 0;
398 for ( std::vector<JustForNow*>::iterator it2 = possible.begin(); it2 != possible.end() && !found; ++it2 ) {
399 JustForNow* curr = *it2;
400 index = 0;
401 for ( std::vector<ColorItem*>::iterator zz = curr->_colors.begin(); zz != curr->_colors.end(); ++zz ) {
402 if ( item == *zz ) {
403 found = true;
404 paletteName = curr->_name;
405 break;
406 } else {
407 index++;
408 }
409 }
410 }
412 if ( !paletteName.empty() ) {
413 gchar* str = g_strdup_printf("%d|", index);
414 paletteName.insert( 0, str );
415 g_free(str);
416 str = 0;
418 if ( bruteForce( document, rroot, paletteName, item->def.getR(), item->def.getG(), item->def.getB() ) ) {
419 sp_document_done( document );
420 }
421 }
422 }
423 }
424 }
425 }
426 }
429 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Inkscape::IconSize size)
430 {
431 Gtk::Widget* widget = 0;
432 if ( style == PREVIEW_STYLE_BLURB ) {
433 Gtk::Label *lbl = new Gtk::Label(def.descr);
434 lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
435 widget = lbl;
436 } else {
437 Glib::ustring blank(" ");
438 if ( size == Inkscape::ICON_SIZE_MENU || size == Inkscape::ICON_SIZE_DECORATION ) {
439 blank = " ";
440 }
442 GtkWidget* eekWidget = eek_preview_new();
443 EekPreview * preview = EEK_PREVIEW(eekWidget);
444 Gtk::Widget* newBlot = Glib::wrap(eekWidget);
446 eek_preview_set_color( preview, (def.getR() << 8) | def.getR(), (def.getG() << 8) | def.getG(), (def.getB() << 8) | def.getB());
448 eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::GtkIconSize)size );
449 eek_preview_set_linked( preview, (LinkType)((_linkSrc ? PREVIEW_LINK_IN:0)
450 | (_listeners.empty() ? 0:PREVIEW_LINK_OUT)
451 | (_isLive ? PREVIEW_LINK_OTHER:0)) );
453 def.addCallback( _colorDefChanged, this );
455 GValue val = {0, {{0}, {0}}};
456 g_value_init( &val, G_TYPE_BOOLEAN );
457 g_value_set_boolean( &val, FALSE );
458 g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
460 /*
461 Gtk::Button *btn = new Gtk::Button(blank);
462 Gdk::Color color;
463 color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
464 btn->modify_bg(Gtk::STATE_NORMAL, color);
465 btn->modify_bg(Gtk::STATE_ACTIVE, color);
466 btn->modify_bg(Gtk::STATE_PRELIGHT, color);
467 btn->modify_bg(Gtk::STATE_SELECTED, color);
469 Gtk::Widget* newBlot = btn;
470 */
472 tips.set_tip((*newBlot), def.descr);
474 /*
475 newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
477 sigc::signal<void> type_signal_something;
478 */
479 g_signal_connect( G_OBJECT(newBlot->gobj()),
480 "clicked",
481 G_CALLBACK(bouncy),
482 this);
484 g_signal_connect( G_OBJECT(newBlot->gobj()),
485 "alt-clicked",
486 G_CALLBACK(bouncy2),
487 this);
489 gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
490 GDK_BUTTON1_MASK,
491 sourceColorEntries,
492 G_N_ELEMENTS(sourceColorEntries),
493 GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
495 g_signal_connect( G_OBJECT(newBlot->gobj()),
496 "drag-data-get",
497 G_CALLBACK(ColorItem::_dragGetColorData),
498 this);
500 g_signal_connect( G_OBJECT(newBlot->gobj()),
501 "drag-begin",
502 G_CALLBACK(dragBegin),
503 this );
505 // g_signal_connect( G_OBJECT(newBlot->gobj()),
506 // "drag-drop",
507 // G_CALLBACK(dragDropColorData),
508 // this);
510 if ( def.isEditable() )
511 {
512 gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
513 GTK_DEST_DEFAULT_ALL,
514 destColorTargets,
515 G_N_ELEMENTS(destColorTargets),
516 GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
519 g_signal_connect( G_OBJECT(newBlot->gobj()),
520 "drag-data-received",
521 G_CALLBACK(_dropDataIn),
522 this );
523 }
525 g_signal_connect( G_OBJECT(newBlot->gobj()),
526 "destroy",
527 G_CALLBACK(dieDieDie),
528 this);
531 widget = newBlot;
532 }
534 _previews.push_back( widget );
536 return widget;
537 }
539 void ColorItem::buttonClicked(bool secondary)
540 {
541 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
542 if (desktop) {
543 char const * attrName = secondary ? "stroke" : "fill";
544 guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
545 gchar c[64];
546 sp_svg_write_color(c, 64, rgba);
548 SPCSSAttr *css = sp_repr_css_attr_new();
549 sp_repr_css_set_property( css, attrName, c );
550 sp_desktop_set_style(desktop, css);
552 sp_repr_css_attr_unref(css);
553 sp_document_done (SP_DT_DOCUMENT (desktop));
554 }
555 }
560 static char* trim( char* str ) {
561 char* ret = str;
562 while ( *str && (*str == ' ' || *str == '\t') ) {
563 str++;
564 }
565 ret = str;
566 while ( *str ) {
567 str++;
568 }
569 str--;
570 while ( str > ret && ( *str == ' ' || *str == '\t' ) || *str == '\r' || *str == '\n' ) {
571 *str-- = 0;
572 }
573 return ret;
574 }
576 void skipWhitespace( char*& str ) {
577 while ( *str == ' ' || *str == '\t' ) {
578 str++;
579 }
580 }
582 bool parseNum( char*& str, int& val ) {
583 val = 0;
584 while ( '0' <= *str && *str <= '9' ) {
585 val = val * 10 + (*str - '0');
586 str++;
587 }
588 bool retval = !(*str == 0 || *str == ' ' || *str == '\t' || *str == '\r' || *str == '\n');
589 return retval;
590 }
593 static bool getBlock( std::string& dst, guchar ch, std::string const str )
594 {
595 bool good = false;
596 size_t pos = str.find(ch);
597 if ( pos != std::string::npos )
598 {
599 size_t pos2 = str.find( '(', pos );
600 if ( pos2 != std::string::npos ) {
601 size_t endPos = str.find( ')', pos2 );
602 if ( endPos != std::string::npos ) {
603 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
604 good = true;
605 }
606 }
607 }
608 return good;
609 }
611 static bool popVal( guint64& numVal, std::string& str )
612 {
613 bool good = false;
614 size_t endPos = str.find(',');
615 if ( endPos == std::string::npos ) {
616 endPos = str.length();
617 }
619 if ( endPos != std::string::npos && endPos > 0 ) {
620 std::string xxx = str.substr( 0, endPos );
621 const gchar* ptr = xxx.c_str();
622 gchar* endPtr = 0;
623 numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
624 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
625 // overflow
626 } else if ( (numVal == 0) && (endPtr == ptr) ) {
627 // failed conversion
628 } else {
629 good = true;
630 str.erase( 0, endPos + 1 );
631 }
632 }
634 return good;
635 }
637 void ColorItem::_wireMagicColors( void* p )
638 {
639 JustForNow* onceMore = reinterpret_cast<JustForNow*>(p);
640 if ( onceMore )
641 {
642 for ( std::vector<ColorItem*>::iterator it = onceMore->_colors.begin(); it != onceMore->_colors.end(); ++it )
643 {
644 size_t pos = (*it)->def.descr.find("*{");
645 if ( pos != std::string::npos )
646 {
647 std::string subby = (*it)->def.descr.substr( pos + 2 );
648 size_t endPos = subby.find("}*");
649 if ( endPos != std::string::npos )
650 {
651 subby.erase( endPos );
652 //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
653 //g_message(" '%s'", subby.c_str());
655 if ( subby.find('E') != std::string::npos )
656 {
657 (*it)->def.setEditable( true );
658 }
660 if ( subby.find('L') != std::string::npos )
661 {
662 (*it)->_isLive = true;
663 }
665 std::string part;
666 // Tint. index + 1 more val.
667 if ( getBlock( part, 'T', subby ) ) {
668 guint64 colorIndex = 0;
669 if ( popVal( colorIndex, part ) ) {
670 guint64 percent = 0;
671 if ( popVal( percent, part ) ) {
672 (*it)->_linkTint( *(onceMore->_colors[colorIndex]), percent );
673 }
674 }
675 }
677 // Shade/tone. index + 1 or 2 more val.
678 if ( getBlock( part, 'S', subby ) ) {
679 guint64 colorIndex = 0;
680 if ( popVal( colorIndex, part ) ) {
681 guint64 percent = 0;
682 if ( popVal( percent, part ) ) {
683 guint64 grayLevel = 0;
684 if ( !popVal( grayLevel, part ) ) {
685 grayLevel = 0;
686 }
687 (*it)->_linkTone( *(onceMore->_colors[colorIndex]), percent, grayLevel );
688 }
689 }
690 }
692 }
693 }
694 }
695 }
696 }
699 void ColorItem::_linkTint( ColorItem& other, int percent )
700 {
701 if ( !_linkSrc )
702 {
703 other._listeners.push_back(this);
704 _linkIsTone = false;
705 _linkPercent = percent;
706 if ( _linkPercent > 100 )
707 _linkPercent = 100;
708 if ( _linkPercent < 0 )
709 _linkPercent = 0;
710 _linkGray = 0;
711 _linkSrc = &other;
713 ColorItem::_colorDefChanged(&other);
714 }
715 }
717 void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
718 {
719 if ( !_linkSrc )
720 {
721 other._listeners.push_back(this);
722 _linkIsTone = true;
723 _linkPercent = percent;
724 if ( _linkPercent > 100 )
725 _linkPercent = 100;
726 if ( _linkPercent < 0 )
727 _linkPercent = 0;
728 _linkGray = grayLevel;
729 _linkSrc = &other;
731 ColorItem::_colorDefChanged(&other);
732 }
733 }
736 void _loadPaletteFile( gchar const *filename )
737 {
738 char block[1024];
739 FILE *f = Inkscape::IO::fopen_utf8name( filename, "r" );
740 if ( f ) {
741 char* result = fgets( block, sizeof(block), f );
742 if ( result ) {
743 if ( strncmp( "GIMP Palette", block, 12 ) == 0 ) {
744 bool inHeader = true;
745 bool hasErr = false;
747 JustForNow *onceMore = new JustForNow();
749 do {
750 result = fgets( block, sizeof(block), f );
751 block[sizeof(block) - 1] = 0;
752 if ( result ) {
753 if ( block[0] == '#' ) {
754 // ignore comment
755 } else {
756 char *ptr = block;
757 // very simple check for header versus entry
758 while ( *ptr == ' ' || *ptr == '\t' ) {
759 ptr++;
760 }
761 if ( *ptr == 0 ) {
762 // blank line. skip it.
763 } else if ( '0' <= *ptr && *ptr <= '9' ) {
764 // should be an entry link
765 inHeader = false;
766 ptr = block;
767 Glib::ustring name("");
768 int r = 0;
769 int g = 0;
770 int b = 0;
771 skipWhitespace(ptr);
772 if ( *ptr ) {
773 hasErr = parseNum(ptr, r);
774 if ( !hasErr ) {
775 skipWhitespace(ptr);
776 hasErr = parseNum(ptr, g);
777 }
778 if ( !hasErr ) {
779 skipWhitespace(ptr);
780 hasErr = parseNum(ptr, b);
781 }
782 if ( !hasErr && *ptr ) {
783 char* n = trim(ptr);
784 if (n != NULL) {
785 name = n;
786 }
787 }
788 if ( !hasErr ) {
789 // Add the entry now
790 Glib::ustring nameStr(name);
791 ColorItem* item = new ColorItem( r, g, b, nameStr );
792 onceMore->_colors.push_back(item);
793 }
794 } else {
795 hasErr = true;
796 }
797 } else {
798 if ( !inHeader ) {
799 // Hmmm... probably bad. Not quite the format we want?
800 hasErr = true;
801 } else {
802 char* sep = strchr(result, ':');
803 if ( sep ) {
804 *sep = 0;
805 char* val = trim(sep + 1);
806 char* name = trim(result);
807 if ( *name ) {
808 if ( strcmp( "Name", name ) == 0 )
809 {
810 onceMore->_name = val;
811 }
812 else if ( strcmp( "Columns", name ) == 0 )
813 {
814 gchar* endPtr = 0;
815 guint64 numVal = g_ascii_strtoull( val, &endPtr, 10 );
816 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
817 // overflow
818 } else if ( (numVal == 0) && (endPtr == val) ) {
819 // failed conversion
820 } else {
821 onceMore->_prefWidth = numVal;
822 }
823 }
824 } else {
825 // error
826 hasErr = true;
827 }
828 } else {
829 // error
830 hasErr = true;
831 }
832 }
833 }
834 }
835 }
836 } while ( result && !hasErr );
837 if ( !hasErr ) {
838 possible.push_back(onceMore);
839 ColorItem::_wireMagicColors( onceMore );
840 } else {
841 delete onceMore;
842 }
843 }
844 }
846 fclose(f);
847 }
848 }
850 static void loadEmUp()
851 {
852 static bool beenHere = false;
853 if ( !beenHere ) {
854 beenHere = true;
856 std::list<gchar *> sources;
857 sources.push_back( profile_path("palettes") );
858 sources.push_back( g_strdup(INKSCAPE_PALETTESDIR) );
860 // Use this loop to iterate through a list of possible document locations.
861 while (!sources.empty()) {
862 gchar *dirname = sources.front();
864 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
865 GError *err = 0;
866 GDir *directory = g_dir_open(dirname, 0, &err);
867 if (!directory) {
868 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
869 g_warning(_("Palettes directory (%s) is unavailable."), safeDir);
870 g_free(safeDir);
871 } else {
872 gchar *filename = 0;
873 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
874 gchar* lower = g_ascii_strdown( filename, -1 );
875 if ( g_str_has_suffix(lower, ".gpl") ) {
876 gchar* full = g_build_filename(dirname, filename, NULL);
877 if ( !Inkscape::IO::file_test( full, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
878 _loadPaletteFile(full);
879 }
880 g_free(full);
881 }
882 g_free(lower);
883 }
884 g_dir_close(directory);
885 }
886 }
888 // toss the dirname
889 g_free(dirname);
890 sources.pop_front();
891 }
892 }
893 }
903 SwatchesPanel& SwatchesPanel::getInstance()
904 {
905 if ( !instance ) {
906 instance = new SwatchesPanel();
907 }
909 return *instance;
910 }
914 /**
915 * Constructor
916 */
917 SwatchesPanel::SwatchesPanel() :
918 Inkscape::UI::Widget::Panel ("dialogs.swatches"),
919 _holder(0)
920 {
921 _holder = new PreviewHolder();
922 loadEmUp();
924 if ( !possible.empty() ) {
925 JustForNow* first = possible.front();
926 if ( first->_prefWidth > 0 ) {
927 _holder->setColumnPref( first->_prefWidth );
928 }
929 _holder->freezeUpdates();
930 for ( std::vector<ColorItem*>::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) {
931 _holder->addPreview(*it);
932 }
933 _holder->thawUpdates();
935 Gtk::RadioMenuItem::Group groupOne;
936 int i = 0;
937 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
938 JustForNow* curr = *it;
939 Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
940 _regItem( single, 3, i );
941 i++;
942 }
944 }
947 _getContents()->pack_start(*_holder, Gtk::PACK_EXPAND_WIDGET);
948 _setTargetFillable(_holder);
950 show_all_children();
952 restorePanelPrefs();
953 }
955 SwatchesPanel::~SwatchesPanel()
956 {
957 }
959 void SwatchesPanel::setOrientation( Gtk::AnchorType how )
960 {
961 // Must call the parent class or bad things might happen
962 Inkscape::UI::Widget::Panel::setOrientation( how );
964 if ( _holder )
965 {
966 _holder->setOrientation( Gtk::ANCHOR_SOUTH );
967 }
968 }
970 void SwatchesPanel::_handleAction( int setId, int itemId )
971 {
972 switch( setId ) {
973 case 3:
974 {
975 if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
976 _holder->clear();
977 JustForNow* curr = possible[itemId];
978 if ( curr->_prefWidth > 0 ) {
979 _holder->setColumnPref( curr->_prefWidth );
980 }
981 _holder->freezeUpdates();
982 for ( std::vector<ColorItem*>::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) {
983 _holder->addPreview(*it);
984 }
985 _holder->thawUpdates();
986 }
987 }
988 break;
989 }
990 }
992 } //namespace Dialogs
993 } //namespace UI
994 } //namespace Inkscape
997 /*
998 Local Variables:
999 mode:c++
1000 c-file-style:"stroustrup"
1001 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1002 indent-tabs-mode:nil
1003 fill-column:99
1004 End:
1005 */
1006 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :