64250c41c00cc02cbd94093f0aeb85c040299ebe
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 <gtk/gtkdialog.h> //for GTK_RESPONSE* types
16 #include <gtk/gtkdnd.h>
18 #include <glibmm/i18n.h>
19 #include "inkscape.h"
20 #include "document.h"
21 #include "desktop-handles.h"
22 #include "extension/db.h"
23 #include "inkscape.h"
24 #include "svg/svg.h"
25 #include "desktop-style.h"
26 #include "io/sys.h"
27 #include "path-prefix.h"
28 #include "swatches.h"
30 #include "eek-preview.h"
32 namespace Inkscape {
33 namespace UI {
34 namespace Dialogs {
36 SwatchesPanel* SwatchesPanel::instance = 0;
39 ColorItem::ColorItem( unsigned int r, unsigned int g, unsigned int b, Glib::ustring& name ) :
40 _r(r),
41 _g(g),
42 _b(b),
43 _name(name)
44 {
45 }
47 ColorItem::~ColorItem()
48 {
49 }
51 ColorItem::ColorItem(ColorItem const &other) :
52 Inkscape::UI::Previewable()
53 {
54 if ( this != &other ) {
55 *this = other;
56 }
57 }
59 ColorItem &ColorItem::operator=(ColorItem const &other)
60 {
61 if ( this != &other ) {
62 _r = other._r;
63 _g = other._g;
64 _b = other._b;
65 _name = other._name;
66 }
67 return *this;
68 }
70 typedef enum {
71 XCOLOR_DATA = 0,
72 TEXT_DATA
73 } colorFlavorType;
75 static const GtkTargetEntry color_entries[] = {
76 {"application/x-color", 0, XCOLOR_DATA},
77 {"text/plain", 0, TEXT_DATA},
78 };
80 static void dragGetColorData( GtkWidget *widget,
81 GdkDragContext *drag_context,
82 GtkSelectionData *data,
83 guint info,
84 guint time,
85 gpointer user_data)
86 {
87 static GdkAtom typeXColor = gdk_atom_intern("application/x-color", FALSE);
88 static GdkAtom typeText = gdk_atom_intern("text/plain", FALSE);
90 ColorItem* item = reinterpret_cast<ColorItem*>(user_data);
91 if ( info == 1 ) {
92 gchar* tmp = g_strdup_printf("#%02x%02x%02x", item->_r, item->_g, item->_b);
94 gtk_selection_data_set( data,
95 typeText,
96 8, // format
97 (guchar*)tmp,
98 strlen((const char*)tmp) + 1);
99 g_free(tmp);
100 tmp = 0;
101 } else {
102 guchar tmp[8];
103 tmp[0] = item->_r;
104 tmp[1] = item->_r;
105 tmp[2] = item->_g;
106 tmp[3] = item->_g;
107 tmp[4] = item->_b;
108 tmp[5] = item->_b;
109 tmp[6] = 0x0ff;
110 tmp[7] = 0x0ff;
111 gtk_selection_data_set( data,
112 typeXColor,
113 8, // format
114 tmp,
115 (3+1) * 2);
116 }
117 }
119 //"drag-drop"
120 gboolean dragDropColorData( GtkWidget *widget,
121 GdkDragContext *drag_context,
122 gint x,
123 gint y,
124 guint time,
125 gpointer user_data)
126 {
127 // TODO finish
128 return TRUE;
129 }
131 static void bouncy( GtkWidget* widget, gpointer callback_data ) {
132 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
133 if ( item ) {
134 item->buttonClicked(false);
135 }
136 }
138 static void bouncy2( GtkWidget* widget, gint arg1, gpointer callback_data ) {
139 ColorItem* item = reinterpret_cast<ColorItem*>(callback_data);
140 if ( item ) {
141 item->buttonClicked(true);
142 }
143 }
145 Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, Gtk::BuiltinIconSize size)
146 {
147 Gtk::Widget* widget = 0;
148 if ( style == PREVIEW_STYLE_BLURB ) {
149 Gtk::Label *lbl = new Gtk::Label(_name);
150 lbl->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
151 widget = lbl;
152 } else {
153 Glib::ustring blank(" ");
154 if ( size == Gtk::ICON_SIZE_MENU ) {
155 blank = " ";
156 }
158 GtkWidget* eekWidget = eek_preview_new();
159 EekPreview * preview = EEK_PREVIEW(eekWidget);
160 Gtk::Widget* newBlot = Glib::wrap(eekWidget);
162 eek_preview_set_color( preview, (_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
164 eek_preview_set_details( preview, (::PreviewStyle)style, (::ViewType)view, (::GtkIconSize)size );
166 GValue val = {0, {{0}, {0}}};
167 g_value_init( &val, G_TYPE_BOOLEAN );
168 g_value_set_boolean( &val, FALSE );
169 g_object_set_property( G_OBJECT(preview), "focus-on-click", &val );
171 /*
172 Gtk::Button *btn = new Gtk::Button(blank);
173 Gdk::Color color;
174 color.set_rgb((_r << 8)|_r, (_g << 8)|_g, (_b << 8)|_b);
175 btn->modify_bg(Gtk::STATE_NORMAL, color);
176 btn->modify_bg(Gtk::STATE_ACTIVE, color);
177 btn->modify_bg(Gtk::STATE_PRELIGHT, color);
178 btn->modify_bg(Gtk::STATE_SELECTED, color);
180 Gtk::Widget* newBlot = btn;
181 */
183 tips.set_tip((*newBlot), _name);
185 /*
186 newBlot->signal_clicked().connect( sigc::mem_fun(*this, &ColorItem::buttonClicked) );
188 sigc::signal<void> type_signal_something;
189 */
190 g_signal_connect( G_OBJECT(newBlot->gobj()),
191 "clicked",
192 G_CALLBACK(bouncy),
193 this);
195 g_signal_connect( G_OBJECT(newBlot->gobj()),
196 "alt-clicked",
197 G_CALLBACK(bouncy2),
198 this);
200 gtk_drag_source_set( GTK_WIDGET(newBlot->gobj()),
201 GDK_BUTTON1_MASK,
202 color_entries,
203 G_N_ELEMENTS(color_entries),
204 GdkDragAction(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
206 g_signal_connect( G_OBJECT(newBlot->gobj()),
207 "drag-data-get",
208 G_CALLBACK(dragGetColorData),
209 this);
211 g_signal_connect( G_OBJECT(newBlot->gobj()),
212 "drag-drop",
213 G_CALLBACK(dragDropColorData),
214 this);
216 widget = newBlot;
217 }
219 return widget;
220 }
222 void ColorItem::buttonClicked(bool secondary)
223 {
224 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
225 if (desktop) {
226 char const * attrName = secondary ? "stroke" : "fill";
227 guint32 rgba = (_r << 24) | (_g << 16) | (_b << 8) | 0xff;
228 gchar c[64];
229 sp_svg_write_color(c, 64, rgba);
231 SPCSSAttr *css = sp_repr_css_attr_new();
232 sp_repr_css_set_property( css, attrName, c );
233 sp_desktop_set_style(desktop, css);
235 sp_repr_css_attr_unref(css);
236 sp_document_done (SP_DT_DOCUMENT (desktop));
237 }
238 }
243 static char* trim( char* str ) {
244 char* ret = str;
245 while ( *str && (*str == ' ' || *str == '\t') ) {
246 str++;
247 }
248 ret = str;
249 while ( *str ) {
250 str++;
251 }
252 str--;
253 while ( str > ret && ( *str == ' ' || *str == '\t' ) || *str == '\r' || *str == '\n' ) {
254 *str-- = 0;
255 }
256 return ret;
257 }
259 void skipWhitespace( char*& str ) {
260 while ( *str == ' ' || *str == '\t' ) {
261 str++;
262 }
263 }
265 bool parseNum( char*& str, int& val ) {
266 val = 0;
267 while ( '0' <= *str && *str <= '9' ) {
268 val = val * 10 + (*str - '0');
269 str++;
270 }
271 bool retval = !(*str == 0 || *str == ' ' || *str == '\t' || *str == '\r' || *str == '\n');
272 return retval;
273 }
276 class JustForNow
277 {
278 public:
279 JustForNow() : _prefWidth(0) {}
281 Glib::ustring _name;
282 int _prefWidth;
283 std::vector<ColorItem*> _colors;
284 };
286 static std::vector<JustForNow*> possible;
288 static void loadPaletteFile( gchar const *filename )
289 {
290 char block[1024];
291 FILE *f = Inkscape::IO::fopen_utf8name( filename, "r" );
292 if ( f ) {
293 char* result = fgets( block, sizeof(block), f );
294 if ( result ) {
295 if ( strncmp( "GIMP Palette", block, 12 ) == 0 ) {
296 bool inHeader = true;
297 bool hasErr = false;
299 JustForNow *onceMore = new JustForNow();
301 do {
302 result = fgets( block, sizeof(block), f );
303 block[sizeof(block) - 1] = 0;
304 if ( result ) {
305 if ( block[0] == '#' ) {
306 // ignore comment
307 } else {
308 char *ptr = block;
309 // very simple check for header versus entry
310 while ( *ptr == ' ' || *ptr == '\t' ) {
311 ptr++;
312 }
313 if ( *ptr == 0 ) {
314 // blank line. skip it.
315 } else if ( '0' <= *ptr && *ptr <= '9' ) {
316 // should be an entry link
317 inHeader = false;
318 ptr = block;
319 Glib::ustring name("");
320 int r = 0;
321 int g = 0;
322 int b = 0;
323 skipWhitespace(ptr);
324 if ( *ptr ) {
325 hasErr = parseNum(ptr, r);
326 if ( !hasErr ) {
327 skipWhitespace(ptr);
328 hasErr = parseNum(ptr, g);
329 }
330 if ( !hasErr ) {
331 skipWhitespace(ptr);
332 hasErr = parseNum(ptr, b);
333 }
334 if ( !hasErr && *ptr ) {
335 char* n = trim(ptr);
336 if (n != NULL) {
337 name = n;
338 }
339 }
340 if ( !hasErr ) {
341 // Add the entry now
342 Glib::ustring nameStr(name);
343 ColorItem* item = new ColorItem( r, g, b, nameStr );
344 onceMore->_colors.push_back(item);
345 }
346 } else {
347 hasErr = true;
348 }
349 } else {
350 if ( !inHeader ) {
351 // Hmmm... probably bad. Not quite the format we want?
352 hasErr = true;
353 } else {
354 char* sep = strchr(result, ':');
355 if ( sep ) {
356 *sep = 0;
357 char* val = trim(sep + 1);
358 char* name = trim(result);
359 if ( *name ) {
360 if ( strcmp( "Name", name ) == 0 )
361 {
362 onceMore->_name = val;
363 }
364 else if ( strcmp( "Columns", name ) == 0 )
365 {
366 gchar* endPtr = 0;
367 guint64 numVal = g_ascii_strtoull( val, &endPtr, 10 );
368 if ( (numVal == G_MAXUINT64) && (ERANGE == errno) ) {
369 // overflow
370 } else if ( (numVal == 0) && (endPtr == val) ) {
371 // failed conversion
372 } else {
373 onceMore->_prefWidth = numVal;
374 }
375 }
376 } else {
377 // error
378 hasErr = true;
379 }
380 } else {
381 // error
382 hasErr = true;
383 }
384 }
385 }
386 }
387 }
388 } while ( result && !hasErr );
389 if ( !hasErr ) {
390 possible.push_back(onceMore);
391 } else {
392 delete onceMore;
393 }
394 }
395 }
397 fclose(f);
398 }
399 }
401 static void loadEmUp()
402 {
403 static bool beenHere = false;
404 if ( !beenHere ) {
405 beenHere = true;
407 std::list<gchar *> sources;
408 sources.push_back( profile_path("palettes") );
409 sources.push_back( g_strdup(INKSCAPE_PALETTESDIR) );
411 // Use this loop to iterate through a list of possible document locations.
412 while (!sources.empty()) {
413 gchar *dirname = sources.front();
415 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
416 GError *err = 0;
417 GDir *directory = g_dir_open(dirname, 0, &err);
418 if (!directory) {
419 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
420 g_warning(_("Palettes directory (%s) is unavailable."), safeDir);
421 g_free(safeDir);
422 } else {
423 gchar *filename = 0;
424 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
425 gchar* full = g_build_filename(dirname, filename, NULL);
426 if ( !Inkscape::IO::file_test( full, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
427 loadPaletteFile(full);
428 }
429 g_free(full);
430 }
431 g_dir_close(directory);
432 }
433 }
435 // toss the dirname
436 g_free(dirname);
437 sources.pop_front();
438 }
439 }
440 }
450 SwatchesPanel& SwatchesPanel::getInstance()
451 {
452 if ( !instance ) {
453 instance = new SwatchesPanel();
454 }
456 return *instance;
457 }
461 /**
462 * Constructor
463 */
464 SwatchesPanel::SwatchesPanel() :
465 Inkscape::UI::Widget::Panel ("dialogs.swatches"),
466 _holder(0)
467 {
468 _holder = new PreviewHolder();
469 loadEmUp();
471 if ( !possible.empty() ) {
472 JustForNow* first = possible.front();
473 if ( first->_prefWidth > 0 ) {
474 _holder->setColumnPref( first->_prefWidth );
475 }
476 for ( std::vector<ColorItem*>::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) {
477 _holder->addPreview(*it);
478 }
480 Gtk::RadioMenuItem::Group groupOne;
481 int i = 0;
482 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
483 JustForNow* curr = *it;
484 Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
485 _regItem( single, 3, i );
486 i++;
487 }
489 }
492 _getContents()->pack_start(*_holder, Gtk::PACK_EXPAND_WIDGET);
493 _setTargetFillable(_holder);
495 show_all_children();
497 restorePanelPrefs();
498 }
500 SwatchesPanel::~SwatchesPanel()
501 {
502 }
504 void SwatchesPanel::setOrientation( Gtk::AnchorType how )
505 {
506 // Must call the parent class or bad things might happen
507 Inkscape::UI::Widget::Panel::setOrientation( how );
509 if ( _holder )
510 {
511 _holder->setOrientation( Gtk::ANCHOR_SOUTH );
512 }
513 }
515 void SwatchesPanel::_handleAction( int setId, int itemId )
516 {
517 switch( setId ) {
518 case 3:
519 {
520 if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
521 _holder->clear();
522 JustForNow* curr = possible[itemId];
523 if ( curr->_prefWidth > 0 ) {
524 _holder->setColumnPref( curr->_prefWidth );
525 }
526 for ( std::vector<ColorItem*>::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) {
527 _holder->addPreview(*it);
528 }
529 }
530 }
531 break;
532 }
533 }
535 } //namespace Dialogs
536 } //namespace UI
537 } //namespace Inkscape
540 /*
541 Local Variables:
542 mode:c++
543 c-file-style:"stroustrup"
544 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
545 indent-tabs-mode:nil
546 fill-column:99
547 End:
548 */
549 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :