757a15d25244e92fbadc180ad13f5c1468f415c6
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"
33 #include "eek-preview.h"
35 namespace Inkscape {
36 namespace UI {
37 namespace Dialogs {
39 SwatchesPanel* SwatchesPanel::instance = 0;
42 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
43 def( r, g, b, name ),
44 _linkIsTone(false),
45 _linkPercent(0),
46 _linkGray(0),
47 _linkSrc(0)
48 {
49 }
51 ColorItem::~ColorItem()
52 {
53 }
55 ColorItem::ColorItem(ColorItem const &other) :
56 Inkscape::UI::Previewable()
57 {
58 if ( this != &other ) {
59 *this = other;
60 }
61 }
63 ColorItem &ColorItem::operator=(ColorItem const &other)
64 {
65 if ( this != &other ) {
66 def = other.def;
68 // TODO - correct linkage
69 _linkSrc = other._linkSrc;
70 g_message("Erk!");
71 }
72 return *this;
73 }
75 typedef enum {
76 APP_X_INKY_COLOR_ID = 0,
77 APP_X_INKY_COLOR = 0,
78 APP_X_COLOR,
79 TEXT_DATA
80 } colorFlavorType;
82 static const GtkTargetEntry sourceColorEntries[] = {
83 // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID},
84 {"application/x-inkscape-color", 0, APP_X_INKY_COLOR},
85 {"application/x-color", 0, APP_X_COLOR},
86 {"text/plain", 0, TEXT_DATA},
87 };
89 static void dragGetColorData( GtkWidget *widget,
90 GdkDragContext *drag_context,
91 GtkSelectionData *data,
92 guint info,
93 guint time,
94 gpointer user_data)
95 {
96 static GdkAtom typeXColor = gdk_atom_intern("application/x-color", FALSE);
97 static GdkAtom typeText = gdk_atom_intern("text/plain", FALSE);
99 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
100 if ( info == TEXT_DATA ) {
101 gchar* tmp = g_strdup_printf("#%02x%02x%02x", item->def.getR(), item->def.getG(), item->def.getB() );
103 gtk_selection_data_set( data,
104 typeText,
105 8, // format
106 (guchar*)tmp,
107 strlen((const char*)tmp) + 1);
108 g_free(tmp);
109 tmp = 0;
110 } else {
111 guint16 tmp[4];
112 tmp[0] = (item->def.getR() << 8) | item->def.getR();
113 tmp[1] = (item->def.getG() << 8) | item->def.getG();
114 tmp[2] = (item->def.getB() << 8) | item->def.getB();
115 tmp[3] = 0xffff;
116 gtk_selection_data_set( data,
117 typeXColor,
118 16, // format
119 reinterpret_cast<const guchar*>(tmp),
120 (3+1) * 2);
121 }
122 }
124 static void dragBegin( GtkWidget *widget, GdkDragContext* dc, gpointer data )
125 {
126 ColorItem* item = reinterpret_cast<ColorItem*>(data);
127 if ( item )
128 {
129 Glib::RefPtr<Gdk::Pixbuf> thumb = Gdk::Pixbuf::create( Gdk::COLORSPACE_RGB, false, 8, 32, 24 );
130 guint32 fillWith = (0xff000000 & (item->def.getR() << 24))
131 | (0x00ff0000 & (item->def.getG() << 16))
132 | (0x0000ff00 & (item->def.getB() << 8));
133 thumb->fill( fillWith );
134 gtk_drag_set_icon_pixbuf( dc, thumb->gobj(), 0, 0 );
135 }
137 }
139 //"drag-drop"
140 // gboolean dragDropColorData( GtkWidget *widget,
141 // GdkDragContext *drag_context,
142 // gint x,
143 // gint y,
144 // guint time,
145 // gpointer user_data)
146 // {
147 // // TODO finish
149 // return TRUE;
150 // }
152 static void bouncy( GtkWidget* widget, gpointer callback_data ) {
153 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
154 if ( item ) {
155 item->buttonClicked(false);
156 }
157 }
159 static void bouncy2( GtkWidget* widget, gint arg1, gpointer callback_data ) {
160 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
161 if ( item ) {
162 item->buttonClicked(true);
163 }
164 }
166 static void dieDieDie( GtkObject *obj, gpointer user_data )
167 {
168 g_message("die die die %p %p", obj, user_data );
169 }
171 static const GtkTargetEntry destColorTargets[] = {
172 // {"application/x-inkscape-color-id", GTK_TARGET_SAME_APP, APP_X_INKY_COLOR_ID},
173 {"application/x-inkscape-color", 0, APP_X_INKY_COLOR},
174 {"application/x-color", 0, APP_X_COLOR},
175 };
177 #include "color.h" // for SP_RGBA32_U_COMPOSE
179 void ColorItem::_dropDataIn( GtkWidget *widget,
180 GdkDragContext *drag_context,
181 gint x, gint y,
182 GtkSelectionData *data,
183 guint info,
184 guint event_time,
185 gpointer user_data)
186 {
187 // g_message(" droppy droppy %d", info);
188 switch (info) {
189 case APP_X_INKY_COLOR:
190 {
191 // g_message("inky color");
192 // Fallthrough
193 }
194 case APP_X_COLOR:
195 {
196 if ( data->length == 8 ) {
197 // Careful about endian issues.
198 guint16* dataVals = (guint16*)data->data;
199 // {
200 // gchar c[64] = {0};
201 // sp_svg_write_color( c, 64,
202 // SP_RGBA32_U_COMPOSE(
203 // 0x0ff & (dataVals[0] >> 8),
204 // 0x0ff & (dataVals[1] >> 8),
205 // 0x0ff & (dataVals[2] >> 8),
206 // 0xff // can't have transparency in the color itself
207 // //0x0ff & (data->data[3] >> 8),
208 // ));
209 // }
210 if ( user_data ) {
211 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
212 if ( item->def.isEditable() ) {
213 // Shove on in the new value
214 item->def.setRGB( 0x0ff & (dataVals[0] >> 8), 0x0ff & (dataVals[1] >> 8), 0x0ff & (dataVals[2] >> 8) );
215 }
216 }
217 }
218 break;
219 }
220 default:
221 g_message("unknown drop type");
222 }
224 }
226 void ColorItem::_colorDefChanged(void* data)
227 {
228 ColorItem* item = reinterpret_cast<ColorItem*>(data);
229 if ( item ) {
230 for ( std::vector<Gtk::Widget*>::iterator it = item->_previews.begin(); it != item->_previews.end(); ++it ) {
231 Gtk::Widget* widget = *it;
232 if ( IS_EEK_PREVIEW(widget->gobj()) ) {
233 EekPreview * preview = EEK_PREVIEW(widget->gobj());
234 eek_preview_set_color( preview,
235 (item->def.getR() << 8) | item->def.getR(),
236 (item->def.getG() << 8) | item->def.getG(),
237 (item->def.getB() << 8) | item->def.getB() );
239 eek_preview_set_linked( preview, (item->_linkSrc ? PREVIEW_LINK_IN:0) | (item->_listeners.empty() ? 0:PREVIEW_LINK_OUT) );
241 widget->queue_draw();
242 }
243 }
245 for ( std::vector<ColorItem*>::iterator it = item->_listeners.begin(); it != item->_listeners.end(); ++it ) {
246 guint r = item->def.getR();
247 guint g = item->def.getG();
248 guint b = item->def.getB();
250 if ( (*it)->_linkIsTone ) {
251 r = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * r) ) / 100;
252 g = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * g) ) / 100;
253 b = ( ((*it)->_linkPercent * (*it)->_linkGray) + ((100 - (*it)->_linkPercent) * b) ) / 100;
254 } else {
255 r = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * r) ) / 100;
256 g = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * g) ) / 100;
257 b = ( ((*it)->_linkPercent * 255) + ((100 - (*it)->_linkPercent) * b) ) / 100;
258 }
260 (*it)->def.setRGB( r, g, b );
261 }
262 }
263 }
266 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Gtk::BuiltinIconSize size)
267 {
268 Gtk::Widget* widget = 0;
269 if ( style == PREVIEW_STYLE_BLURB ) {
270 Gtk::Label *lbl = new Gtk::Label(def.descr);
271 lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
272 widget = lbl;
273 } else {
274 Glib::ustring blank(" ");
275 if ( size == Gtk::ICON_SIZE_MENU ) {
276 blank = " ";
277 }
279 GtkWidget* eekWidget = eek_preview_new();
280 EekPreview * preview = EEK_PREVIEW(eekWidget);
281 Gtk::Widget* newBlot = Glib::wrap(eekWidget);
283 eek_preview_set_color( preview, (def.getR() << 8) | def.getR(), (def.getG() << 8) | def.getG(), (def.getB() << 8) | def.getB());
285 eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::GtkIconSize)size );
286 eek_preview_set_linked( preview, (_linkSrc ? PREVIEW_LINK_IN:0) | (_listeners.empty() ? 0:PREVIEW_LINK_OUT) );
288 def.addCallback( _colorDefChanged, this );
290 GValue val = {0, {{0}, {0}}};
291 g_value_init( &val, G_TYPE_BOOLEAN );
292 g_value_set_boolean( &val, FALSE );
293 g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
295 /*
296 Gtk::Button *btn = new Gtk::Button(blank);
297 Gdk::Color color;
298 color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
299 btn->modify_bg(Gtk::STATE_NORMAL, color);
300 btn->modify_bg(Gtk::STATE_ACTIVE, color);
301 btn->modify_bg(Gtk::STATE_PRELIGHT, color);
302 btn->modify_bg(Gtk::STATE_SELECTED, color);
304 Gtk::Widget* newBlot = btn;
305 */
307 tips.set_tip((*newBlot), def.descr);
309 /*
310 newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
312 sigc::signal<void> type_signal_something;
313 */
314 g_signal_connect( G_OBJECT(newBlot->gobj()),
315 "clicked",
316 G_CALLBACK(bouncy),
317 this);
319 g_signal_connect( G_OBJECT(newBlot->gobj()),
320 "alt-clicked",
321 G_CALLBACK(bouncy2),
322 this);
324 gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
325 GDK_BUTTON1_MASK,
326 sourceColorEntries,
327 G_N_ELEMENTS(sourceColorEntries),
328 GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
330 g_signal_connect( G_OBJECT(newBlot->gobj()),
331 "drag-data-get",
332 G_CALLBACK(dragGetColorData),
333 this);
335 g_signal_connect( G_OBJECT(newBlot->gobj()),
336 "drag-begin",
337 G_CALLBACK(dragBegin),
338 this );
340 // g_signal_connect( G_OBJECT(newBlot->gobj()),
341 // "drag-drop",
342 // G_CALLBACK(dragDropColorData),
343 // this);
345 if ( def.isEditable() )
346 {
347 gtk_drag_dest_set( GTK_WIDGET(newBlot->gobj()),
348 GTK_DEST_DEFAULT_ALL,
349 destColorTargets,
350 G_N_ELEMENTS(destColorTargets),
351 GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
354 g_signal_connect( G_OBJECT(newBlot->gobj()),
355 "drag-data-received",
356 G_CALLBACK(_dropDataIn),
357 this );
358 }
360 g_signal_connect( G_OBJECT(newBlot->gobj()),
361 "destroy",
362 G_CALLBACK(dieDieDie),
363 this);
366 widget = newBlot;
367 }
369 _previews.push_back( widget );
371 return widget;
372 }
374 void ColorItem::buttonClicked(bool secondary)
375 {
376 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
377 if (desktop) {
378 char const * attrName = secondary ? "stroke" : "fill";
379 guint32 rgba = (def.getR() << 24) | (def.getG() << 16) | (def.getB() << 8) | 0xff;
380 gchar c[64];
381 sp_svg_write_color(c, 64, rgba);
383 SPCSSAttr *css = sp_repr_css_attr_new();
384 sp_repr_css_set_property( css, attrName, c );
385 sp_desktop_set_style(desktop, css);
387 sp_repr_css_attr_unref(css);
388 sp_document_done (SP_DT_DOCUMENT (desktop));
389 }
390 }
395 static char* trim( char* str ) {
396 char* ret = str;
397 while ( *str && (*str == ' ' || *str == '\t') ) {
398 str++;
399 }
400 ret = str;
401 while ( *str ) {
402 str++;
403 }
404 str--;
405 while ( str > ret && ( *str == ' ' || *str == '\t' ) || *str == '\r' || *str == '\n' ) {
406 *str-- = 0;
407 }
408 return ret;
409 }
411 void skipWhitespace( char*& str ) {
412 while ( *str == ' ' || *str == '\t' ) {
413 str++;
414 }
415 }
417 bool parseNum( char*& str, int& val ) {
418 val = 0;
419 while ( '0' <= *str && *str <= '9' ) {
420 val = val * 10 + (*str - '0');
421 str++;
422 }
423 bool retval = !(*str == 0 || *str == ' ' || *str == '\t' || *str == '\r' || *str == '\n');
424 return retval;
425 }
428 class JustForNow
429 {
430 public:
431 JustForNow() : _prefWidth(0) {}
433 Glib::ustring _name;
434 int _prefWidth;
435 std::vector<ColorItem*> _colors;
436 };
438 static std::vector<JustForNow*> possible;
440 static bool getBlock( std::string& dst, guchar ch, std::string const str )
441 {
442 bool good = false;
443 size_t pos = str.find(ch);
444 if ( pos != std::string::npos )
445 {
446 size_t pos2 = str.find( '(', pos );
447 if ( pos2 != std::string::npos ) {
448 size_t endPos = str.find( ')', pos2 );
449 if ( endPos != std::string::npos ) {
450 dst = str.substr( pos2 + 1, (endPos - pos2 - 1) );
451 good = true;
452 }
453 }
454 }
455 return good;
456 }
458 static bool popVal( guint64& numVal, std::string& str )
459 {
460 bool good = false;
461 size_t endPos = str.find(',');
462 if ( endPos == std::string::npos ) {
463 endPos = str.length();
464 }
466 if ( endPos != std::string::npos && endPos > 0 ) {
467 std::string xxx = str.substr( 0, endPos );
468 const gchar* ptr = xxx.c_str();
469 gchar* endPtr = 0;
470 numVal = g_ascii_strtoull( ptr, &endPtr, 10 );
471 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
472 // overflow
473 } else if ( (numVal == 0) && (endPtr == ptr) ) {
474 // failed conversion
475 } else {
476 good = true;
477 str.erase( 0, endPos + 1 );
478 }
479 }
481 return good;
482 }
484 void ColorItem::_wireMagicColors( void* p )
485 {
486 JustForNow* onceMore = reinterpret_cast<JustForNow*>(p);
487 if ( onceMore )
488 {
489 for ( std::vector<ColorItem*>::iterator it = onceMore->_colors.begin(); it != onceMore->_colors.end(); ++it )
490 {
491 size_t pos = (*it)->def.descr.find("*{");
492 if ( pos != std::string::npos )
493 {
494 std::string subby = (*it)->def.descr.substr( pos + 2 );
495 size_t endPos = subby.find("}*");
496 if ( endPos != std::string::npos )
497 {
498 subby.erase( endPos );
499 //g_message("FOUND MAGIC at '%s'", (*it)->def.descr.c_str());
500 //g_message(" '%s'", subby.c_str());
502 if ( subby.find('E') != std::string::npos )
503 {
504 //g_message(" HOT!");
505 (*it)->def.setEditable( true );
506 }
508 std::string part;
509 // Tint. index + 1 more val.
510 if ( getBlock( part, 'T', subby ) ) {
511 guint64 colorIndex = 0;
512 if ( popVal( colorIndex, part ) ) {
513 guint64 percent = 0;
514 if ( popVal( percent, part ) ) {
515 (*it)->_linkTint( *(onceMore->_colors[colorIndex]), percent );
516 }
517 }
518 }
520 // Shade/tone. index + 1 or 2 more val.
521 if ( getBlock( part, 'S', subby ) ) {
522 guint64 colorIndex = 0;
523 if ( popVal( colorIndex, part ) ) {
524 guint64 percent = 0;
525 if ( popVal( percent, part ) ) {
526 guint64 grayLevel = 0;
527 if ( !popVal( grayLevel, part ) ) {
528 grayLevel = 0;
529 }
530 (*it)->_linkTone( *(onceMore->_colors[colorIndex]), percent, grayLevel );
531 }
532 }
533 }
535 }
536 }
537 }
538 }
539 }
542 void ColorItem::_linkTint( ColorItem& other, int percent )
543 {
544 if ( !_linkSrc )
545 {
546 other._listeners.push_back(this);
547 _linkIsTone = false;
548 _linkPercent = percent;
549 if ( _linkPercent > 100 )
550 _linkPercent = 100;
551 if ( _linkPercent < 0 )
552 _linkPercent = 0;
553 _linkGray = 0;
554 _linkSrc = &other;
556 ColorItem::_colorDefChanged(&other);
557 }
558 }
560 void ColorItem::_linkTone( ColorItem& other, int percent, int grayLevel )
561 {
562 if ( !_linkSrc )
563 {
564 other._listeners.push_back(this);
565 _linkIsTone = true;
566 _linkPercent = percent;
567 if ( _linkPercent > 100 )
568 _linkPercent = 100;
569 if ( _linkPercent < 0 )
570 _linkPercent = 0;
571 _linkGray = grayLevel;
572 _linkSrc = &other;
574 ColorItem::_colorDefChanged(&other);
575 }
576 }
579 void _loadPaletteFile( gchar const *filename )
580 {
581 char block[1024];
582 FILE *f = Inkscape::IO::fopen_utf8name( filename, "r" );
583 if ( f ) {
584 char* result = fgets( block, sizeof(block), f );
585 if ( result ) {
586 if ( strncmp( "GIMP Palette", block, 12 ) == 0 ) {
587 bool inHeader = true;
588 bool hasErr = false;
590 JustForNow *onceMore = new JustForNow();
592 do {
593 result = fgets( block, sizeof(block), f );
594 block[sizeof(block) - 1] = 0;
595 if ( result ) {
596 if ( block[0] == '#' ) {
597 // ignore comment
598 } else {
599 char *ptr = block;
600 // very simple check for header versus entry
601 while ( *ptr == ' ' || *ptr == '\t' ) {
602 ptr++;
603 }
604 if ( *ptr == 0 ) {
605 // blank line. skip it.
606 } else if ( '0' <= *ptr && *ptr <= '9' ) {
607 // should be an entry link
608 inHeader = false;
609 ptr = block;
610 Glib::ustring name("");
611 int r = 0;
612 int g = 0;
613 int b = 0;
614 skipWhitespace(ptr);
615 if ( *ptr ) {
616 hasErr = parseNum(ptr, r);
617 if ( !hasErr ) {
618 skipWhitespace(ptr);
619 hasErr = parseNum(ptr, g);
620 }
621 if ( !hasErr ) {
622 skipWhitespace(ptr);
623 hasErr = parseNum(ptr, b);
624 }
625 if ( !hasErr && *ptr ) {
626 char* n = trim(ptr);
627 if (n != NULL) {
628 name = n;
629 }
630 }
631 if ( !hasErr ) {
632 // Add the entry now
633 Glib::ustring nameStr(name);
634 ColorItem* item = new ColorItem( r, g, b, nameStr );
635 onceMore->_colors.push_back(item);
636 }
637 } else {
638 hasErr = true;
639 }
640 } else {
641 if ( !inHeader ) {
642 // Hmmm... probably bad. Not quite the format we want?
643 hasErr = true;
644 } else {
645 char* sep = strchr(result, ':');
646 if ( sep ) {
647 *sep = 0;
648 char* val = trim(sep + 1);
649 char* name = trim(result);
650 if ( *name ) {
651 if ( strcmp( "Name", name ) == 0 )
652 {
653 onceMore->_name = val;
654 }
655 else if ( strcmp( "Columns", name ) == 0 )
656 {
657 gchar* endPtr = 0;
658 guint64 numVal = g_ascii_strtoull( val, &endPtr, 10 );
659 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
660 // overflow
661 } else if ( (numVal == 0) && (endPtr == val) ) {
662 // failed conversion
663 } else {
664 onceMore->_prefWidth = numVal;
665 }
666 }
667 } else {
668 // error
669 hasErr = true;
670 }
671 } else {
672 // error
673 hasErr = true;
674 }
675 }
676 }
677 }
678 }
679 } while ( result && !hasErr );
680 if ( !hasErr ) {
681 possible.push_back(onceMore);
682 ColorItem::_wireMagicColors( onceMore );
683 } else {
684 delete onceMore;
685 }
686 }
687 }
689 fclose(f);
690 }
691 }
693 static void loadEmUp()
694 {
695 static bool beenHere = false;
696 if ( !beenHere ) {
697 beenHere = true;
699 std::list<gchar *> sources;
700 sources.push_back( profile_path("palettes") );
701 sources.push_back( g_strdup(INKSCAPE_PALETTESDIR) );
703 // Use this loop to iterate through a list of possible document locations.
704 while (!sources.empty()) {
705 gchar *dirname = sources.front();
707 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
708 GError *err = 0;
709 GDir *directory = g_dir_open(dirname, 0, &err);
710 if (!directory) {
711 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
712 g_warning(_("Palettes directory (%s) is unavailable."), safeDir);
713 g_free(safeDir);
714 } else {
715 gchar *filename = 0;
716 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
717 gchar* lower = g_ascii_strdown( filename, -1 );
718 if ( g_str_has_suffix(lower, ".gpl") ) {
719 gchar* full = g_build_filename(dirname, filename, NULL);
720 if ( !Inkscape::IO::file_test( full, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
721 _loadPaletteFile(full);
722 }
723 g_free(full);
724 }
725 g_free(lower);
726 }
727 g_dir_close(directory);
728 }
729 }
731 // toss the dirname
732 g_free(dirname);
733 sources.pop_front();
734 }
735 }
736 }
746 SwatchesPanel& SwatchesPanel::getInstance()
747 {
748 if ( !instance ) {
749 instance = new SwatchesPanel();
750 }
752 return *instance;
753 }
757 /**
758 * Constructor
759 */
760 SwatchesPanel::SwatchesPanel() :
761 Inkscape::UI::Widget::Panel ("dialogs.swatches"),
762 _holder(0)
763 {
764 _holder = new PreviewHolder();
765 loadEmUp();
767 if ( !possible.empty() ) {
768 JustForNow* first = possible.front();
769 if ( first->_prefWidth > 0 ) {
770 _holder->setColumnPref( first->_prefWidth );
771 }
772 _holder->freezeUpdates();
773 for ( std::vector<ColorItem*>::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) {
774 _holder->addPreview(*it);
775 }
776 _holder->thawUpdates();
778 Gtk::RadioMenuItem::Group groupOne;
779 int i = 0;
780 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
781 JustForNow* curr = *it;
782 Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
783 _regItem( single, 3, i );
784 i++;
785 }
787 }
790 _getContents()->pack_start(*_holder, Gtk::PACK_EXPAND_WIDGET);
791 _setTargetFillable(_holder);
793 show_all_children();
795 restorePanelPrefs();
796 }
798 SwatchesPanel::~SwatchesPanel()
799 {
800 }
802 void SwatchesPanel::setOrientation( Gtk::AnchorType how )
803 {
804 // Must call the parent class or bad things might happen
805 Inkscape::UI::Widget::Panel::setOrientation( how );
807 if ( _holder )
808 {
809 _holder->setOrientation( Gtk::ANCHOR_SOUTH );
810 }
811 }
813 void SwatchesPanel::_handleAction( int setId, int itemId )
814 {
815 switch( setId ) {
816 case 3:
817 {
818 if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
819 _holder->clear();
820 JustForNow* curr = possible[itemId];
821 if ( curr->_prefWidth > 0 ) {
822 _holder->setColumnPref( curr->_prefWidth );
823 }
824 _holder->freezeUpdates();
825 for ( std::vector<ColorItem*>::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) {
826 _holder->addPreview(*it);
827 }
828 _holder->thawUpdates();
829 }
830 }
831 break;
832 }
833 }
835 } //namespace Dialogs
836 } //namespace UI
837 } //namespace Inkscape
840 /*
841 Local Variables:
842 mode:c++
843 c-file-style:"stroustrup"
844 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
845 indent-tabs-mode:nil
846 fill-column:99
847 End:
848 */
849 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :