Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
icon-loader.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors:
6 * see git history
7 * Jabiertxo Arraiza <jabier.arraiza@marker.es>
8 *
9 * Copyright (C) 2018 Authors
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#include "icon-loader.h"
14
15#include <unordered_map>
16#include <giomm/themedicon.h>
17#include <gdkmm/display.h>
18#include <gtkmm/cssprovider.h>
19#include <gtkmm/icontheme.h>
20#include <gtkmm/image.h>
21#include <gtkmm/styleprovider.h>
22#include <gtkmm/window.h>
23
24#include "colors/color.h"
25#include "desktop.h"
26#include "inkscape.h"
27
28Gtk::Image *sp_get_icon_image(Glib::ustring const &icon_name, int size)
29{
30 Gtk::Image *icon = new Gtk::Image();
31 icon->set_from_icon_name(icon_name);
32 icon->set_pixel_size(size);
33 return icon;
34}
35
36Gtk::Image *sp_get_icon_image(Glib::ustring const &icon_name, Gtk::IconSize icon_size)
37{
38 Gtk::Image *icon = new Gtk::Image();
39 icon->set_from_icon_name(icon_name);
40 icon->set_icon_size(icon_size);
41 return icon;
42}
43
44GtkWidget *sp_get_icon_image(Glib::ustring const &icon_name, GtkIconSize icon_size)
45{
46 auto const widget = gtk_image_new_from_icon_name(icon_name.c_str());
47 gtk_image_set_icon_size(GTK_IMAGE(widget), icon_size);
48 return widget;
49}
50
51namespace Inkscape::UI {
52
53// Maintain a map of every color requested to a CSS class that will apply it
54[[nodiscard]] static Glib::ustring const &get_color_class(std::uint32_t const rgba_color,
55 Glib::RefPtr<Gdk::Display> const &display)
56{
57 static std::unordered_map<std::uint32_t, Glib::ustring> color_classes;
58 auto &color_class = color_classes[rgba_color];
59 if (!color_class.empty()) return color_class;
60
61 auto color = Colors::Color(rgba_color);
62
63 // The CSS class is .icon-color-RRGGBBAA
64 auto rgba_str = color.toString();
65 rgba_str.erase(0, 1);
66 color_class = Glib::ustring::compose("icon-color-%1", rgba_str);
67
68 // Add a persistent CSS provider for that class+color
69 auto const css_provider = Gtk::CssProvider::create();
70 auto const data = Glib::ustring::compose(
71 ".symbolic .%1, .regular .%1 { -gtk-icon-style: symbolic; color: %2; opacity: %3; }",
72 color_class, color.toString(false), color.getOpacity());
73
74 css_provider->load_from_data(data);
75 // Add it with the needed priority = higher than themes.cpp _colorizeprovider
76 static constexpr auto priority = GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1;
77 Gtk::StyleProvider::add_provider_for_display(display, css_provider, priority);
78 return color_class;
79}
80
92GetShapeIconResult get_shape_icon(Glib::ustring const &shape_type, std::uint32_t const rgba_color)
93{
94 Glib::RefPtr<Gdk::Display> display = Gdk::Display::get_default();
95 auto const icon_theme = Gtk::IconTheme::get_for_display(display);
96
97 auto icon_name = Glib::ustring::compose("shape-%1-symbolic", shape_type);
98 if (!icon_theme->has_icon(icon_name)) {
99 icon_name = Glib::ustring::compose("%1-symbolic", shape_type);
100 if (!icon_theme->has_icon(icon_name)) {
101 icon_name = "shape-unknown-symbolic";
102 }
103 }
104
105 return {std::move(icon_name), get_color_class(rgba_color, display)};
106}
107
109Gtk::Image *get_shape_image(Glib::ustring const &shape_type, std::uint32_t const rgba_color,
110 Gtk::IconSize const icon_size)
111{
112 auto const [icon_name, color_class] = get_shape_icon(shape_type, rgba_color);
113 auto const icon = Gio::ThemedIcon::create(icon_name);
114 auto const image = Gtk::make_managed<Gtk::Image>(icon);
115 image->set_icon_size(icon_size);
116 image->add_css_class(color_class);
117 return image;
118}
119
120} // namespace Inkscape::UI
121
122/*
123 Local Variables:
124 mode:c++
125 c-file-style:"stroustrup"
126 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
127 indent-tabs-mode:nil
128 fill-column:99
129 End:
130*/
131// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
Geom::IntPoint size
Glib::RefPtr< Gtk::IconTheme > icon_theme
Editable view implementation.
Gtk::Image * sp_get_icon_image(Glib::ustring const &icon_name, int size)
Icon Loader.
std::unique_ptr< Magick::Image > image
User interface code.
Definition desktop.h:113
Gtk::Image * get_shape_image(Glib::ustring const &shape_type, std::uint32_t const rgba_color, Gtk::IconSize const icon_size)
As get_shape_icon(), but returns a ready-made, managed Image having that icon name & CSS class.
GetShapeIconResult get_shape_icon(Glib::ustring const &shape_type, std::uint32_t const rgba_color)
Get the shape icon for this named shape type.
static Glib::ustring const & get_color_class(std::uint32_t const rgba_color, Glib::RefPtr< Gdk::Display > const &display)
static const Point data[]
A pair containing an icon name & a CSS class to set an RGBA color.
Definition icon-loader.h:34