-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathgtk-utils.cpp
More file actions
214 lines (188 loc) · 5.11 KB
/
Copy pathgtk-utils.cpp
File metadata and controls
214 lines (188 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "glibmm/markup.h"
#include "glibmm/ustring.h"
#include <gtk-utils.hpp>
#include <glibmm.h>
#include <gtkmm/icontheme.h>
#include <gdk/gdkcairo.h>
#include <iostream>
#include <giomm/desktopappinfo.h>
#include "wf-shell-app.hpp"
Glib::RefPtr<Gdk::Pixbuf> load_icon_pixbuf_safe(std::string icon_path, int size)
{
try {
auto pb = Gdk::Pixbuf::create_from_file(icon_path, size, size);
return pb;
} catch (Glib::FileError&)
{
std::cerr << "Error loading file: " << icon_path << std::endl;
return {};
} catch (Gdk::PixbufError&)
{
std::cerr << "Pixbuf error: " << icon_path << std::endl;
return {};
} catch (...)
{
std::cerr << "Failed to load: " << icon_path << std::endl;
return {};
}
}
Glib::RefPtr<Gtk::CssProvider> load_css_from_path(std::string path)
{
try {
auto css = Gtk::CssProvider::create();
css->load_from_path(path);
return css;
} catch (Glib::Error& err)
{
std::cerr << "Failed to load CSS: " << err.what() << std::endl;
return {};
} catch (...)
{
std::cerr << "Failed to load CSS at: " << path << std::endl;
return {};
}
}
void invert_pixbuf(Glib::RefPtr<Gdk::Pixbuf>& pbuff)
{
int channels = pbuff->get_n_channels();
int stride = pbuff->get_rowstride();
auto data = pbuff->get_pixels();
int w = pbuff->get_width();
int h = pbuff->get_height();
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
auto p = data + j * stride + i * channels;
p[0] = 255 - p[0];
p[1] = 255 - p[1];
p[2] = 255 - p[2];
}
}
}
namespace IconProvider
{
std::map<std::string, std::string> custom_icons;
std::string tolower(std::string str)
{
for (auto& c : str)
{
c = std::tolower(c);
}
return str;
}
/* Gio::DesktopAppInfo
*
* Usually knowing the app_id, we can get a desktop app info from Gio
* The filename is either the app_id + ".desktop" or lower_app_id + ".desktop" */
Glib::RefPtr<Gio::Icon> get_from_desktop_app_info(std::string app_id)
{
std::vector<std::string> prefixes = {
"",
"org.kde.",
};
std::vector<std::string> app_id_variations = {
app_id,
tolower(app_id),
};
std::vector<std::string> suffixes = {
"",
".desktop"
};
for (auto& prefix : prefixes)
{
for (auto& id : app_id_variations)
{
for (auto& suffix : suffixes)
{
auto app_info = Gio::DesktopAppInfo::create(prefix + id + suffix);
if (app_info)
{
return app_info->get_icon();
}
}
}
}
return {};
}
bool image_set_icon(Gtk::Image & image, Glib::ustring path)
{
if (path.empty())
{
return false;
}
if ((path.rfind("/", 0) == 0) || (path.rfind("~", 0) == 0))
{
image.set(path);
return true;
} else
{
/* It might be a space delimited list */
std::istringstream stream(path);
std::string app_id;
auto theme = Gtk::IconTheme::get_for_display(Gdk::Display::get_default());
while (stream >> app_id)
{
if (custom_icons.count(app_id) > 0)
{
image.set_from_icon_name(custom_icons[app_id]);
return true;
}
/* If the icon theme has this, use it */
if (theme && theme->has_icon(app_id))
{
image.set_from_icon_name(app_id);
return true;
}
/* Might be appid, get the .desktop file */
auto icon = get_from_desktop_app_info(app_id);
if (icon)
{
image.set_from_icon_name(icon->to_string());
return true;
}
}
}
return false;
}
void load_custom_icons(std::string section_name)
{
static const std::string prefix = "icon_mapping_";
auto section = WayfireShellApp::get().config.get_section(section_name);
for (auto option : section->get_registered_options())
{
if (option->get_name().compare(0, prefix.length(), prefix) != 0)
{
continue;
}
auto app_id = option->get_name().substr(prefix.length());
custom_icons[app_id] = option->get_value_str();
}
}
}
/*
* Check if this string appears to be markup
*
* Does not check it is *valid* markup
*/
bool is_markup(std::string input)
{
int count_left = std::count(input.begin(), input.end(), '<');
int count_right = std::count(input.begin(), input.end(), '>');
int count_amp = std::count(input.begin(), input.end(), '&');
int count_semi = std::count(input.begin(), input.end(), ';');
if ((count_left == count_right) && (count_amp == count_semi))
{
return true;
}
return false;
}
/* Escape string if it doesn't appear to be markup */
std::string markup_escape(std::string input)
{
if (is_markup(input))
{
return input;
}
return Glib::Markup::escape_text(input);
}