1 /**
2 * \brief Static style swatch (fill, stroke, opacity)
3 *
4 * Author:
5 * buliabyak@gmail.com
6 *
7 * Copyright (C) 2005 author
8 *
9 * Released under GNU GPL. Read the file 'COPYING' for more information.
10 */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #include "style-swatch.h"
18 #include "widgets/spw-utilities.h"
19 #include "ui/widget/color-preview.h"
21 #include "style.h"
22 #include "sp-linear-gradient-fns.h"
23 #include "sp-radial-gradient-fns.h"
24 #include "sp-pattern.h"
25 #include "xml/repr.h"
26 #include "widgets/widget-sizes.h"
27 #include "helper/units.h"
29 enum {
30 SS_FILL,
31 SS_STROKE
32 };
35 namespace Inkscape {
36 namespace UI {
37 namespace Widget {
39 StyleSwatch::StyleSwatch(SPCSSAttr *css)
40 : _css (NULL),
42 _table(2, 6),
44 _sw_unit(NULL),
46 _tooltips ()
47 {
48 _label[SS_FILL].set_markup(_("F:"));
49 _label[SS_STROKE].set_markup(_("S:"));
51 for (int i = SS_FILL; i <= SS_STROKE; i++) {
52 _label[i].set_alignment(0.0, 0.5);
53 _label[i].set_padding(0, 0);
55 _color_preview[i] = new Inkscape::UI::Widget::ColorPreview (0);
56 }
58 _opacity_value.set_alignment(0.0, 0.5);
59 _opacity_value.set_padding(0, 0);
61 _table.set_col_spacings (2);
62 _table.set_row_spacings (0);
64 _stroke.pack_start(_place[SS_STROKE]);
65 _stroke_width_place.add(_stroke_width);
66 _stroke.pack_start(_stroke_width_place, Gtk::PACK_SHRINK);
68 _table.attach(_label[SS_FILL], 0,1, 0,1, Gtk::SHRINK, Gtk::SHRINK);
69 _table.attach(_label[SS_STROKE], 0,1, 1,2, Gtk::SHRINK, Gtk::SHRINK);
71 _table.attach(_place[SS_FILL], 1,2, 0,1);
72 _table.attach(_stroke, 1,2, 1,2);
74 _opacity_place.add(_opacity_value);
75 _table.attach(_opacity_place, 2,3, 0,2, Gtk::SHRINK, Gtk::SHRINK);
77 pack_start(_table, true, true, 0);
79 set_size_request (STYLE_SWATCH_WIDTH, -1);
81 sp_set_font_size_smaller (GTK_WIDGET(_opacity_value.gobj()));
82 sp_set_font_size_smaller (GTK_WIDGET(_stroke_width.gobj()));
83 for (int i = SS_FILL; i <= SS_STROKE; i++) {
84 sp_set_font_size_smaller (GTK_WIDGET(_value[i].gobj()));
85 sp_set_font_size_smaller (GTK_WIDGET(_place[i].gobj()));
86 sp_set_font_size_smaller (GTK_WIDGET(_label[i].gobj()));
87 }
89 setStyle (css);
90 }
92 StyleSwatch::~StyleSwatch()
93 {
94 if (_css)
95 sp_repr_css_attr_unref (_css);
97 for (int i = SS_FILL; i <= SS_STROKE; i++) {
98 delete _color_preview[i];
99 }
100 }
102 void
103 StyleSwatch::setStyle(SPCSSAttr *css)
104 {
105 if (_css)
106 sp_repr_css_attr_unref (_css);
107 _css = sp_repr_css_attr_new();
108 sp_repr_css_merge(_css, css);
110 gchar const *css_string = sp_repr_css_write_string (_css);
111 SPStyle *temp_spstyle = sp_style_new();
112 sp_style_merge_from_style_string (temp_spstyle, css_string);
114 setStyle (temp_spstyle);
116 sp_style_unref (temp_spstyle);
117 }
119 void
120 StyleSwatch::setStyle(SPStyle *query)
121 {
122 _place[SS_FILL].remove();
123 _place[SS_STROKE].remove();
125 bool has_stroke = true;
127 for (int i = SS_FILL; i <= SS_STROKE; i++) {
128 Gtk::EventBox *place = &(_place[i]);
130 SPIPaint *paint;
131 if (i == SS_FILL) {
132 paint = &(query->fill);
133 } else {
134 paint = &(query->stroke);
135 }
137 if (paint->set && paint->type == SP_PAINT_TYPE_COLOR) {
138 guint32 color = sp_color_get_rgba32_falpha (&(paint->value.color),
139 SP_SCALE24_TO_FLOAT ((i == SS_FILL)? query->fill_opacity.value : query->stroke_opacity.value));
140 ((Inkscape::UI::Widget::ColorPreview*)_color_preview[i])->setRgba32 (color);
141 _color_preview[i]->show_all();
142 place->add(*_color_preview[i]);
143 gchar *tip;
144 if (i == SS_FILL) {
145 tip = g_strdup_printf ("Fill: %06x/%.3g", color >> 8, SP_RGBA32_A_F(color));
146 } else {
147 tip = g_strdup_printf ("Stroke: %06x/%.3g", color >> 8, SP_RGBA32_A_F(color));
148 }
149 _tooltips.set_tip(*place, tip);
150 g_free (tip);
151 } else if (paint->set && paint->type == SP_PAINT_TYPE_PAINTSERVER) {
152 SPPaintServer *server = (i == SS_FILL)? SP_STYLE_FILL_SERVER (query) : SP_STYLE_STROKE_SERVER (query);
154 if (SP_IS_LINEARGRADIENT (server)) {
155 _value[i].set_markup(_("L Gradient"));
156 place->add(_value[i]);
157 _tooltips.set_tip(*place, (i == SS_FILL)? (_("Linear gradient fill")) : (_("Linear gradient stroke")));
158 } else if (SP_IS_RADIALGRADIENT (server)) {
159 _value[i].set_markup(_("R Gradient"));
160 place->add(_value[i]);
161 _tooltips.set_tip(*place, (i == SS_FILL)? (_("Radial gradient fill")) : (_("Radial gradient stroke")));
162 } else if (SP_IS_PATTERN (server)) {
163 _value[i].set_markup(_("Pattern"));
164 place->add(_value[i]);
165 _tooltips.set_tip(*place, (i == SS_FILL)? (_("Pattern fill")) : (_("Pattern stroke")));
166 }
168 } else if (paint->set && paint->type == SP_PAINT_TYPE_NONE) {
169 _value[i].set_markup(_("None"));
170 place->add(_value[i]);
171 _tooltips.set_tip(*place, (i == SS_FILL)? (_("No fill")) : (_("No stroke")));
172 if (i == SS_STROKE) has_stroke = false;
173 } else if (!paint->set) {
174 _value[i].set_markup(_("Unset"));
175 place->add(_value[i]);
176 _tooltips.set_tip(*place, (i == SS_FILL)? (_("Unset fill")) : (_("Unset stroke")));
177 if (i == SS_STROKE) has_stroke = false;
178 }
179 }
181 // Now query stroke_width
182 if (has_stroke) {
183 double w;
184 if (_sw_unit) {
185 w = sp_pixels_get_units(query->stroke_width.computed, *_sw_unit);
186 } else {
187 w = query->stroke_width.computed;
188 }
190 {
191 gchar *str = g_strdup_printf(" %.3g", w);
192 _stroke_width.set_markup(str);
193 g_free (str);
194 }
195 {
196 gchar *str = g_strdup_printf(_("Stroke width: %.5g%s"),
197 w,
198 _sw_unit? sp_unit_get_abbreviation(_sw_unit) : "px");
199 _tooltips.set_tip(_stroke_width_place, str);
200 g_free (str);
201 }
202 } else {
203 _tooltips.unset_tip(_stroke_width_place);
204 _stroke_width.set_markup ("");
205 }
207 gdouble op = SP_SCALE24_TO_FLOAT(query->opacity.value);
208 if (op != 1) {
209 {
210 gchar *str;
211 if (op == 0)
212 str = g_strdup_printf(_("0:%.3g"), op);
213 else
214 str = g_strdup_printf(_("0:.%d"), (int) (op*10));
215 _opacity_value.set_markup (str);
216 g_free (str);
217 }
218 {
219 gchar *str = g_strdup_printf(_("Opacity: %.3g"), op);
220 _tooltips.set_tip(_opacity_place, str);
221 g_free (str);
222 }
223 } else {
224 _tooltips.unset_tip(_opacity_place);
225 _opacity_value.set_markup ("");
226 }
228 show_all();
229 }
231 } // namespace Widget
232 } // namespace UI
233 } // namespace Inkscape
235 /*
236 Local Variables:
237 mode:c++
238 c-file-style:"stroustrup"
239 c-file-offsets:((innamespace . 0)(inline-open . 0))
240 indent-tabs-mode:nil
241 fill-column:99
242 End:
243 */
244 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :