Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
style-swatch.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6/* Authors:
7 * buliabyak@gmail.com
8 * Krzysztof Kosiński <tweenk.pl@gmail.com>
9 *
10 * Copyright (C) 2005-2008 Authors
11 *
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15#include "style-swatch.h"
16
17#include <functional>
18#include <gtkmm/enums.h>
19#include <utility>
20#include <glibmm/i18n.h>
21#include <gtkmm/grid.h>
22#include <sigc++/adaptors/bind.h>
23#include <sigc++/functors/mem_fun.h>
24
25#include "style.h"
26
27#include "actions/actions-tools.h" // Open tool preferences.
30#include "object/sp-pattern.h"
32#include "ui/controller.h"
33#include "ui/pack.h"
34#include "ui/util.h"
36#include "util/units.h"
37#include "xml/sp-css-attr.h"
38
39static constexpr int STYLE_SWATCH_WIDTH = 135;
40
41enum {
44};
45
46namespace Inkscape::UI::Widget {
47
51void style_obs_callback(StyleSwatch &_style_swatch, Preferences::Entry const &val)
52{
54 _style_swatch.setStyle(css);
56}
57
61void tool_obs_callback(StyleSwatch &_style_swatch, Preferences::Entry const &val)
62{
63 auto const prefs = Preferences::get();
64 Glib::ustring path;
65 SPCSSAttr *css = nullptr;
66
67 bool usecurrent = val.getBool();
68 if (usecurrent) {
69 path = "/desktop/style";
70 css = prefs->getStyle(path);
71 const auto &al = css->attributeList();
72 if (al.empty()) {
73 // Fallback to own style if desktop style empty (does this ever happen?).
75 css = nullptr;
76 }
77 }
78
79 if (!css) {
80 path = _style_swatch._tool_path + "/style";
81 css = prefs->getInheritedStyle(path);
82 }
83
84 // Set style at least once.
85 _style_swatch.setStyle(css);
87
88 auto callback = sigc::bind<0>(&style_obs_callback, std::ref(_style_swatch));
89 _style_swatch._style_obs = StyleSwatch::PrefObs::create(std::move(path), std::move(callback));
90}
91
92StyleSwatch::StyleSwatch(SPCSSAttr *css, gchar const *main_tip, Gtk::Orientation orient)
93 : Gtk::Box(Gtk::Orientation::HORIZONTAL),
94 _desktop(nullptr),
95 _css(nullptr),
96 _table(Gtk::make_managed<Gtk::Grid>()),
97 _sw_unit(nullptr),
98 _stroke(Gtk::Orientation::HORIZONTAL)
99{
100 set_name("StyleSwatch");
101 add_css_class(orient == Gtk::Orientation::HORIZONTAL ? "horizontal" : "vertical");
102 _label[SS_FILL].set_markup(_("Fill"));
103 _label[SS_STROKE].set_markup(_("Stroke"));
104
105 for (int i = SS_FILL; i <= SS_STROKE; i++) {
106 _label[i].set_halign(Gtk::Align::START);
107 _label[i].set_valign(Gtk::Align::CENTER);
108 _label[i].set_margin_top(0);
109 _label[i].set_margin_bottom(0);
110 _label[i].set_margin_start(0);
111 _label[i].set_margin_end(0);
112
113 _color_preview[i] = std::make_unique<ColorPreview>(0);
114 }
115
116 _opacity_value.set_halign(Gtk::Align::START);
117 _opacity_value.set_valign(Gtk::Align::CENTER);
118 _opacity_value.set_margin_top(0);
119 _opacity_value.set_margin_bottom(0);
120 _opacity_value.set_margin_start(0);
121 _opacity_value.set_margin_end(0);
122
123 _table->set_column_spacing(2);
124 _table->set_row_spacing(0);
125
126 // We let pack()ed children expand but donʼt propagate expand upwards.
127 set_hexpand(false);
128 _stroke.set_hexpand(false);
129
132
133 if (orient == Gtk::Orientation::VERTICAL) {
134 _table->attach(_label[SS_FILL], 0, 0, 1, 1);
135 _table->attach(_label[SS_STROKE], 0, 1, 1, 1);
136 _table->attach(_place[SS_FILL], 1, 0, 1, 1);
137 _table->attach(_stroke, 1, 1, 1, 1);
138 _table->attach(_empty_space, 2, 0, 1, 2);
139 _table->attach(_opacity_value, 2, 0, 1, 2);
140 UI::pack_start(*this, *_table, true, true);
141
142 set_size_request (STYLE_SWATCH_WIDTH, -1);
143 }
144 else {
145 _table->set_column_spacing(4);
146 _table->attach(_label[SS_FILL], 0, 0, 1, 1);
147 _table->attach(_place[SS_FILL], 1, 0, 1, 1);
148 _label[SS_STROKE].set_margin_start(6);
149 _table->attach(_label[SS_STROKE], 2, 0, 1, 1);
150 _table->attach(_stroke, 3, 0, 1, 1);
151 _opacity_value.set_margin_start(6);
152 _table->attach(_opacity_value, 4, 0, 1, 1);
153 UI::pack_start(*this, *_table, true, true);
154
155 int patch_w = 6 * 6;
156 _place[SS_FILL].set_size_request(patch_w, -1);
157 _place[SS_STROKE].set_size_request(patch_w, -1);
158 }
159
160 setStyle (css);
161
162 if (main_tip) {
163 _table->set_tooltip_text(main_tip);
164 }
165}
166
167void StyleSwatch::setToolName(const Glib::ustring& tool_name) {
168 _tool_name = tool_name;
169}
170
174
180
181void
182StyleSwatch::setWatchedTool(const char *path, bool synthesize)
183{
184 _tool_obs.reset();
185
186 if (path) {
187 _tool_path = path;
188 _tool_obs = PrefObs::create(_tool_path + "/usecurrent",
189 sigc::bind<0>(&tool_obs_callback, std::ref(*this)));
190 } else {
191 _tool_path = "";
192 }
193
194 if (synthesize && _tool_obs) {
195 _tool_obs->call();
196 }
197}
198
199
201{
202 if (_css)
204
205 if (!css)
206 return;
207
210
211 Glib::ustring css_string;
212 sp_repr_css_write_string (_css, css_string);
213
214 SPStyle style(_desktop ? _desktop->getDocument() : nullptr);
215 if (!css_string.empty()) {
216 style.mergeString(css_string.c_str());
217 }
218 setStyle (&style);
219}
220
222{
225
226 bool has_stroke = true;
227
228 for (int i = SS_FILL; i <= SS_STROKE; i++) {
229 auto const place = &_place[i];
230
231 SPIPaint *paint;
232 if (i == SS_FILL) {
233 paint = &(query->fill);
234 } else {
235 paint = &(query->stroke);
236 }
237
238 if (paint->set && paint->isPaintserver()) {
239 SPPaintServer *server = (i == SS_FILL)? SP_STYLE_FILL_SERVER (query) : SP_STYLE_STROKE_SERVER (query);
240
241 if (is<SPLinearGradient>(server)) {
242 _value[i].set_markup(_("L Gradient"));
243 place->append(_value[i]);
244 place->set_tooltip_text((i == SS_FILL)? (_("Linear gradient (fill)")) : (_("Linear gradient (stroke)")));
245 } else if (is<SPRadialGradient>(server)) {
246 _value[i].set_markup(_("R Gradient"));
247 place->append(_value[i]);
248 place->set_tooltip_text((i == SS_FILL)? (_("Radial gradient (fill)")) : (_("Radial gradient (stroke)")));
249 } else if (is<SPPattern>(server)) {
250 _value[i].set_markup(_("Pattern"));
251 place->append(_value[i]);
252 place->set_tooltip_text((i == SS_FILL)? (_("Pattern (fill)")) : (_("Pattern (stroke)")));
253 }
254 } else if (paint->set && paint->isColor()) {
255 auto color = paint->getColor();
256 color.addOpacity(i == SS_FILL ? query->fill_opacity : query->stroke_opacity);
257 _color_preview[i]->setRgba32(color.toRGBA());
258 place->append(*_color_preview[i]);
259 gchar *tip;
260 if (i == SS_FILL) {
261 tip = g_strdup_printf (_("Fill: %s"), color.toString().c_str());
262 } else {
263 tip = g_strdup_printf (_("Stroke: %s"), color.toString().c_str());
264 }
265 place->set_tooltip_text(tip);
266 g_free (tip);
267 } else if (paint->set && paint->isNone()) {
268 _value[i].set_markup(C_("Fill and stroke", "<i>None</i>"));
269 place->append(_value[i]);
270 place->set_tooltip_text((i == SS_FILL)? (C_("Fill and stroke", "No fill")) : (C_("Fill and stroke", "No stroke")));
271 if (i == SS_STROKE) has_stroke = false;
272 } else if (!paint->set) {
273 _value[i].set_markup(_("<b>Unset</b>"));
274 place->append(_value[i]);
275 place->set_tooltip_text((i == SS_FILL)? (_("Unset fill")) : (_("Unset stroke")));
276 if (i == SS_STROKE) has_stroke = false;
277 }
278 }
279
280// Now query stroke_width
281 if (has_stroke) {
282 if (query->stroke_extensions.hairline) {
283 Glib::ustring swidth = "<small>";
284 swidth += _("Hairline");
285 swidth += "</small>";
286 _stroke_width.set_markup(swidth.c_str());
287 auto str = Glib::ustring::compose(_("Stroke width: %1"), _("Hairline"));
288 _stroke_width.set_tooltip_text(str);
289 } else {
290 double w;
291 if (_sw_unit) {
292 w = Util::Quantity::convert(query->stroke_width.computed, "px", _sw_unit);
293 } else {
294 w = query->stroke_width.computed;
295 }
296
297 {
298 gchar *str = g_strdup_printf(" %.3g", w);
299 Glib::ustring swidth = "<small>";
300 swidth += str;
301 swidth += "</small>";
302 _stroke_width.set_markup(swidth.c_str());
303 g_free (str);
304 }
305 {
306 gchar *str = g_strdup_printf(_("Stroke width: %.5g%s"),
307 w,
308 _sw_unit? _sw_unit->abbr.c_str() : "px");
309 _stroke_width.set_tooltip_text(str);
310 g_free (str);
311 }
312 }
313 } else {
314 _stroke_width.set_tooltip_text("");
315 _stroke_width.set_markup("");
316 }
317
318 gdouble op = SP_SCALE24_TO_FLOAT(query->opacity.value);
319 if (op != 1) {
320 {
321 gchar *str;
322 str = g_strdup_printf(_("O: %2.0f"), (op*100.0));
323 Glib::ustring opacity = "<small>";
324 opacity += str;
325 opacity += "</small>";
326 _opacity_value.set_markup (opacity.c_str());
327 g_free (str);
328 }
329 {
330 gchar *str = g_strdup_printf(_("Opacity: %2.1f %%"), (op*100.0));
331 _opacity_value.set_tooltip_text(str);
332 g_free (str);
333 }
334 } else {
335 _opacity_value.set_tooltip_text("");
336 _opacity_value.set_markup("");
337 }
338}
339
340} // namespace Inkscape::UI::Widget
341
342/*
343 Local Variables:
344 mode:c++
345 c-file-style:"stroustrup"
346 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
347 indent-tabs-mode:nil
348 fill-column:99
349 End:
350*/
351// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Data type representing a typeless value of a preference.
bool getBool(bool def=false) const
Interpret the preference as a Boolean value.
SPCSSAttr * getInheritedStyle() const
Interpret the preference as a CSS style with directory-based inheritance.
static std::unique_ptr< PreferencesObserver > create(Glib::ustring path, std::function< void(const Preferences::Entry &new_value)> callback)
static Preferences * get()
Access the singleton Preferences object.
void setWatchedTool(const char *path, bool synthesize)
friend void tool_obs_callback(StyleSwatch &, Preferences::Entry const &)
Watches whether the tool uses the current style.
std::unique_ptr< ColorPreview > _color_preview[2]
void setDesktop(SPDesktop *desktop)
void setToolName(const Glib::ustring &tool_name)
std::unique_ptr< PrefObs > _style_obs
std::unique_ptr< PrefObs > _tool_obs
StyleSwatch(SPCSSAttr *attr, gchar const *main_tip, Gtk::Orientation orient=Gtk::Orientation::VERTICAL)
static double convert(double from_dist, Unit const *from, Unit const *to)
Convert distances.
Definition units.cpp:588
Glib::ustring abbr
Definition units.h:81
To do: update description of desktop.
Definition desktop.h:149
SPDocument * getDocument() const
Definition desktop.h:189
Paint type internal to SPStyle.
bool isPaintserver() const
bool isColor() const
bool isNone() const
Colors::Color const & getColor() const
An SVG style object.
Definition style.h:45
T< SPAttr::FILL, SPIPaint > fill
fill
Definition style.h:240
T< SPAttr::STROKE, SPIPaint > stroke
stroke
Definition style.h:247
T< SPAttr::STROKE_WIDTH, SPILength > stroke_width
stroke-width
Definition style.h:249
T< SPAttr::FILL_OPACITY, SPIScale24 > fill_opacity
fill-opacity
Definition style.h:242
T< SPAttr::STROKE_OPACITY, SPIScale24 > stroke_opacity
stroke-opacity
Definition style.h:261
void mergeString(char const *p)
Parses a style="..." string and merges it with an existing SPStyle.
Definition style.cpp:854
T< SPAttr::OPACITY, SPIScale24 > opacity
opacity
Definition style.h:216
T< SPAttr::STROKE_EXTENSIONS, SPIStrokeExtensions > stroke_extensions
-inkscape-stroke
Definition style.h:263
const double w
Definition conic-4.cpp:19
Utilities to more easily use Gtk::EventController & subclasses like Gesture.
std::shared_ptr< Css const > css
static bool has_stroke(SPObject *source)
Definition desktop.h:50
Custom widgets.
Definition desktop.h:126
void tool_obs_callback(StyleSwatch &_style_swatch, Preferences::Entry const &val)
Watches whether the tool uses the current style.
void style_obs_callback(StyleSwatch &_style_swatch, Preferences::Entry const &val)
Watches for changes in the observed style pref.
void remove_all_children(Widget &widget)
For each child in get_children(widget), call widget.remove(*child). May not cause delete child!
Definition util.h:75
void pack_start(Gtk::Box &box, Gtk::Widget &child, bool const expand, bool const fill, unsigned const padding)
Adds child to box, packed with reference to the start of box.
Definition pack.cpp:141
Helpers for using Gtk::Boxes, encapsulating large changes between GTK3 & GTK4.
SPCSSAttr * sp_repr_css_attr_new()
Creates an empty SPCSSAttr (a class for manipulating CSS style properties).
Definition repr-css.cpp:67
void sp_repr_css_write_string(SPCSSAttr *css, Glib::ustring &str)
Write a style attribute string from a list of properties stored in an SPCSAttr object.
Definition repr-css.cpp:242
void sp_repr_css_merge(SPCSSAttr *dst, SPCSSAttr *src)
Merges two SPCSSAttr's.
Definition repr-css.cpp:298
void sp_repr_css_attr_unref(SPCSSAttr *css)
Unreferences an SPCSSAttr (will be garbage collected if no references remain).
Definition repr-css.cpp:76
SPCSSAttr - interface for CSS Attributes.
TODO: insert short description here.
SVG <pattern> implementation.
TODO: insert short description here.
static constexpr int STYLE_SWATCH_WIDTH
@ SS_STROKE
@ SS_FILL
Static style swatch (fill, stroke, opacity)
SPStyle - a style object for SPItem objects.
SPDesktop * desktop