1 /*
2 * Copyright (C) 2005-2007 Authors:
3 * Ted Gould <ted@gould.cx>
4 * Johan Engelen <johan@shouraizou.nl>
5 * Christopher Brown <audiere@gmail.com>
6 * Released under GNU GPL, read the file 'COPYING' for more information
7 */
9 #ifdef HAVE_CONFIG_H
10 # include "config.h"
11 #endif
13 #include <iostream>
14 #include <sstream>
16 #include <gtkmm/adjustment.h>
17 #include <gtkmm/box.h>
18 #include <gtkmm/spinbutton.h>
20 #include <xml/node.h>
22 #include "extension.h"
23 #include "paramcolor.h"
25 #include "color.h"
26 #include "widgets/sp-color-selector.h"
27 #include "widgets/sp-color-notebook.h"
30 namespace Inkscape {
31 namespace Extension {
33 void sp_color_param_changed(SPColorSelector *csel, GObject *cp);
36 /** \brief Free the allocated data. */
37 ParamColor::~ParamColor(void)
38 {
40 }
42 guint32
43 ParamColor::set( guint32 in, SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/ )
44 {
45 _value = in;
47 gchar * prefname = this->pref_name();
48 prefs_set_string_attribute(PREF_DIR, prefname, this->string()->c_str());
49 g_free(prefname);
51 return _value;
52 }
54 /** \brief Initialize the object, to do that, copy the data. */
55 ParamColor::ParamColor (const gchar * name, const gchar * guitext, const gchar * desc, const Parameter::_scope_t scope, Inkscape::Extension::Extension * ext, Inkscape::XML::Node * xml) :
56 Parameter(name, guitext, desc, scope, ext)
57 {
58 const char * defaulthex = NULL;
59 if (sp_repr_children(xml) != NULL)
60 defaulthex = sp_repr_children(xml)->content();
62 gchar * pref_name = this->pref_name();
63 const gchar * paramval = prefs_get_string_attribute(PREF_DIR, pref_name);
64 g_free(pref_name);
66 if (paramval != NULL)
67 defaulthex = paramval;
69 _value = atoi(defaulthex);
71 return;
72 }
74 /** \brief Return the value as a string */
75 Glib::ustring *
76 ParamColor::string (void)
77 {
78 char str[16];
79 sprintf(str, "%i", _value);
81 return new Glib::ustring(str);
82 }
84 Gtk::Widget *
85 ParamColor::get_widget( SPDocument * /*doc*/, Inkscape::XML::Node * /*node*/, sigc::signal<void> * changeSignal )
86 {
87 _changeSignal = new sigc::signal<void>(*changeSignal);
88 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox(false, 4));
89 SPColorSelector* spColorSelector = (SPColorSelector*)sp_color_selector_new(SP_TYPE_COLOR_NOTEBOOK);
91 ColorSelector* colorSelector = spColorSelector->base;
92 if (_value < 1) {
93 _value = 0xFF000000;
94 }
95 SPColor *color = new SPColor( _value );
96 float alpha = (_value & 0xff) / 255.0F;
97 colorSelector->setColorAlpha(*color, alpha);
99 hbox->pack_start (*Glib::wrap(&spColorSelector->vbox), true, true, 0);
100 g_signal_connect(G_OBJECT(spColorSelector), "changed", G_CALLBACK(sp_color_param_changed), (void*)this);
102 gtk_widget_show(GTK_WIDGET(spColorSelector));
103 hbox->show();
105 return dynamic_cast<Gtk::Widget *>(hbox);
106 }
108 void
109 sp_color_param_changed(SPColorSelector *csel, GObject *obj)
110 {
111 const SPColor color = csel->base->getColor();
112 float alpha = csel->base->getAlpha();
114 ParamColor* ptr = (ParamColor*)obj;
115 ptr->set(color.toRGBA32( alpha ), NULL, NULL);
117 ptr->_changeSignal->emit();
118 }
120 }; /* namespace Extension */
121 }; /* namespace Inkscape */