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 Glib::ustring _name;
280 std::vector<ColorItem*> _colors;
281 };
283 static std::vector<JustForNow*> possible;
285 static void loadPaletteFile( gchar const *filename )
286 {
287 char block[1024];
288 FILE *f = Inkscape::IO::fopen_utf8name( filename, "r" );
289 if ( f ) {
290 char* result = fgets( block, sizeof(block), f );
291 if ( result ) {
292 if ( strncmp( "GIMP Palette", block, 12 ) == 0 ) {
293 bool inHeader = true;
294 bool hasErr = false;
296 JustForNow *onceMore = new JustForNow();
298 do {
299 result = fgets( block, sizeof(block), f );
300 block[sizeof(block) - 1] = 0;
301 if ( result ) {
302 if ( block[0] == '#' ) {
303 // ignore comment
304 } else {
305 char *ptr = block;
306 // very simple check for header versus entry
307 while ( *ptr == ' ' || *ptr == '\t' ) {
308 ptr++;
309 }
310 if ( *ptr == 0 ) {
311 // blank line. skip it.
312 } else if ( '0' <= *ptr && *ptr <= '9' ) {
313 // should be an entry link
314 inHeader = false;
315 ptr = block;
316 Glib::ustring name("");
317 int r = 0;
318 int g = 0;
319 int b = 0;
320 skipWhitespace(ptr);
321 if ( *ptr ) {
322 hasErr = parseNum(ptr, r);
323 if ( !hasErr ) {
324 skipWhitespace(ptr);
325 hasErr = parseNum(ptr, g);
326 }
327 if ( !hasErr ) {
328 skipWhitespace(ptr);
329 hasErr = parseNum(ptr, b);
330 }
331 if ( !hasErr && *ptr ) {
332 char* n = trim(ptr);
333 if (n != NULL) {
334 name = n;
335 }
336 }
337 if ( !hasErr ) {
338 // Add the entry now
339 Glib::ustring nameStr(name);
340 ColorItem* item = new ColorItem( r, g, b, nameStr );
341 onceMore->_colors.push_back(item);
342 }
343 } else {
344 hasErr = true;
345 }
346 } else {
347 if ( !inHeader ) {
348 // Hmmm... probably bad. Not quite the format we want?
349 hasErr = true;
350 } else {
351 char* sep = strchr(result, ':');
352 if ( sep ) {
353 *sep = 0;
354 char* val = trim(sep + 1);
355 char* name = trim(result);
356 if ( *name ) {
357 if ( strcmp( "Name", name ) == 0 ) {
358 onceMore->_name = val;
359 }
360 } else {
361 // error
362 hasErr = true;
363 }
364 } else {
365 // error
366 hasErr = true;
367 }
368 }
369 }
370 }
371 }
372 } while ( result && !hasErr );
373 if ( !hasErr ) {
374 possible.push_back(onceMore);
375 } else {
376 delete onceMore;
377 }
378 }
379 }
381 fclose(f);
382 }
383 }
385 static void loadEmUp()
386 {
387 static bool beenHere = false;
388 if ( !beenHere ) {
389 beenHere = true;
391 std::list<gchar *> sources;
392 sources.push_back( profile_path("palettes") );
393 sources.push_back( g_strdup(INKSCAPE_PALETTESDIR) );
395 // Use this loop to iterate through a list of possible document locations.
396 while (!sources.empty()) {
397 gchar *dirname = sources.front();
399 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
400 GError *err = 0;
401 GDir *directory = g_dir_open(dirname, 0, &err);
402 if (!directory) {
403 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
404 g_warning(_("Palettes directory (%s) is unavailable."), safeDir);
405 g_free(safeDir);
406 } else {
407 gchar *filename = 0;
408 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
409 gchar* full = g_build_filename(dirname, filename, NULL);
410 if ( !Inkscape::IO::file_test( full, (GFileTest)(G_FILE_TEST_IS_DIR ) ) ) {
411 loadPaletteFile(full);
412 }
413 g_free(full);
414 }
415 g_dir_close(directory);
416 }
417 }
419 // toss the dirname
420 g_free(dirname);
421 sources.pop_front();
422 }
423 }
424 }
434 SwatchesPanel& SwatchesPanel::getInstance()
435 {
436 if ( !instance ) {
437 instance = new SwatchesPanel();
438 }
440 return *instance;
441 }
445 /**
446 * Constructor
447 */
448 SwatchesPanel::SwatchesPanel() :
449 Inkscape::UI::Widget::Panel ("dialogs.swatches"),
450 _holder(0)
451 {
452 _holder = new PreviewHolder();
453 loadEmUp();
455 if ( !possible.empty() ) {
456 JustForNow* first = possible.front();
457 for ( std::vector<ColorItem*>::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) {
458 _holder->addPreview(*it);
459 }
461 Gtk::RadioMenuItem::Group groupOne;
462 int i = 0;
463 for ( std::vector<JustForNow*>::iterator it = possible.begin(); it != possible.end(); it++ ) {
464 JustForNow* curr = *it;
465 Gtk::RadioMenuItem* single = manage(new Gtk::RadioMenuItem(groupOne, curr->_name));
466 _regItem( single, 3, i );
467 i++;
468 }
470 }
473 pack_start(*_holder, Gtk::PACK_EXPAND_WIDGET);
474 _setTargetFillable(_holder);
476 show_all_children();
478 restorePanelPrefs();
479 }
481 SwatchesPanel::~SwatchesPanel()
482 {
483 }
485 void SwatchesPanel::_handleAction( int setId, int itemId )
486 {
487 switch( setId ) {
488 case 3:
489 {
490 if ( itemId >= 0 && itemId < static_cast<int>(possible.size()) ) {
491 _holder->clear();
492 JustForNow* curr = possible[itemId];
493 for ( std::vector<ColorItem*>::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) {
494 _holder->addPreview(*it);
495 }
496 }
497 }
498 break;
499 }
500 }
502 } //namespace Dialogs
503 } //namespace UI
504 } //namespace Inkscape
507 /*
508 Local Variables:
509 mode:c++
510 c-file-style:"stroustrup"
511 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
512 indent-tabs-mode:nil
513 fill-column:99
514 End:
515 */
516 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :