2009-03-02 23:31:01 +00:00
|
|
|
/*
|
|
|
|
* icons.[ch] written by Paolo Bacchilega, who writes:
|
2011-01-19 13:48:47 +00:00
|
|
|
* "There is no problem for me, you can license my code
|
|
|
|
* under whatever licence you wish :)"
|
2010-10-01 20:22:51 +00:00
|
|
|
*
|
2009-03-02 23:31:01 +00:00
|
|
|
*/
|
|
|
|
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <algorithm>
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <array>
|
2022-04-26 23:24:07 +00:00
|
|
|
#include <map>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
2022-04-26 23:24:07 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
#include <glibmm.h>
|
|
|
|
#include <giomm.h>
|
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "IconCache.h"
|
|
|
|
#include "Utils.h"
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
std::string_view const DirectoryMimeType = "folder"sv;
|
|
|
|
std::string_view const UnknownMimeType = "unknown"sv;
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
auto const VoidPixbufKey = "void-pixbuf"s;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
struct IconCache
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::RefPtr<Gtk::IconTheme> icon_theme;
|
2017-04-19 12:04:45 +00:00
|
|
|
int icon_size;
|
2022-04-26 23:24:07 +00:00
|
|
|
std::map<std::string, Glib::RefPtr<Gdk::Pixbuf>, std::less<>> cache;
|
2021-10-18 20:22:31 +00:00
|
|
|
};
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::array<std::unique_ptr<IconCache>, 7> icon_cache;
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> create_void_pixbuf(int width, int height)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const p = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, width, height);
|
|
|
|
p->fill(0xFFFFFF00);
|
2009-03-02 23:31:01 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
int get_size_in_pixels(Gtk::IconSize icon_size)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::IconSize::lookup(icon_size, width, height);
|
|
|
|
return std::max(width, height);
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::unique_ptr<IconCache> icon_cache_new(Gtk::Widget& for_widget, Gtk::IconSize icon_size)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto icons = std::make_unique<IconCache>();
|
|
|
|
icons->icon_theme = Gtk::IconTheme::get_for_screen(for_widget.get_screen());
|
2020-11-09 03:31:02 +00:00
|
|
|
icons->icon_size = get_size_in_pixels(icon_size);
|
2022-01-17 18:39:50 +00:00
|
|
|
icons->cache.try_emplace(VoidPixbufKey, create_void_pixbuf(icons->icon_size, icons->icon_size));
|
2020-08-11 18:11:55 +00:00
|
|
|
return icons;
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:30:51 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> get_themed_icon_pixbuf(Gio::ThemedIcon& icon, int size, Gtk::IconTheme& icon_theme)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const icon_names = icon.get_names();
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
auto icon_info = icon_theme.choose_icon(icon_names, size);
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
if (!bool{ icon_info })
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
icon_info = icon_theme.lookup_icon("text-x-generic", size, Gtk::ICON_LOOKUP_USE_BUILTIN);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
try
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return icon_info.load_icon();
|
|
|
|
}
|
|
|
|
catch (Glib::Error const& e)
|
|
|
|
{
|
|
|
|
g_warning("could not load icon pixbuf: %s\n", e.what().c_str());
|
|
|
|
return {};
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> get_file_icon_pixbuf(Gio::FileIcon& icon, int size)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return Gdk::Pixbuf::create_from_file(icon.get_file()->get_path(), size, -1, false);
|
|
|
|
}
|
2022-04-01 19:16:33 +00:00
|
|
|
catch (Glib::Error const&)
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> _get_icon_pixbuf(Glib::RefPtr<Gio::Icon> const& icon, int size, Gtk::IconTheme& theme)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (icon == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (auto* const ticon = dynamic_cast<Gio::ThemedIcon*>(gtr_get_ptr(icon)); ticon != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return get_themed_icon_pixbuf(*ticon, size, theme);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (auto* const ficon = dynamic_cast<Gio::FileIcon*>(gtr_get_ptr(icon)); ficon != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
return get_file_icon_pixbuf(*ficon, size);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
return {};
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> icon_cache_get_mime_type_icon(IconCache& icons, std::string_view mime_type)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
2022-04-26 23:24:07 +00:00
|
|
|
auto& cache = icons.cache;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
if (auto mime_it = cache.find(mime_type); mime_it != std::end(cache))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-26 23:24:07 +00:00
|
|
|
return mime_it->second;
|
2009-03-02 23:31:01 +00:00
|
|
|
}
|
2009-07-27 18:48:21 +00:00
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
auto mime_type_str = std::string{ mime_type };
|
|
|
|
auto icon = Gio::content_type_get_icon(mime_type_str);
|
|
|
|
auto pixbuf = _get_icon_pixbuf(icon, icons.icon_size, *gtr_get_ptr(icons.icon_theme));
|
2021-10-06 16:32:17 +00:00
|
|
|
if (pixbuf != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-26 23:24:07 +00:00
|
|
|
cache.try_emplace(std::move(mime_type_str), pixbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-25 04:42:42 +00:00
|
|
|
|
2009-03-02 23:31:01 +00:00
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-04-26 23:24:07 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> gtr_get_mime_type_icon(std::string_view mime_type, Gtk::IconSize icon_size, Gtk::Widget& for_widget)
|
2009-03-02 23:31:01 +00:00
|
|
|
{
|
|
|
|
int n;
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (icon_size)
|
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
case Gtk::ICON_SIZE_MENU:
|
2017-04-19 12:04:45 +00:00
|
|
|
n = 1;
|
|
|
|
break;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
case Gtk::ICON_SIZE_SMALL_TOOLBAR:
|
2017-04-19 12:04:45 +00:00
|
|
|
n = 2;
|
|
|
|
break;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
case Gtk::ICON_SIZE_LARGE_TOOLBAR:
|
2017-04-19 12:04:45 +00:00
|
|
|
n = 3;
|
|
|
|
break;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
case Gtk::ICON_SIZE_BUTTON:
|
2017-04-19 12:04:45 +00:00
|
|
|
n = 4;
|
|
|
|
break;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
case Gtk::ICON_SIZE_DND:
|
2017-04-19 12:04:45 +00:00
|
|
|
n = 5;
|
|
|
|
break;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
case Gtk::ICON_SIZE_DIALOG:
|
2017-04-19 12:04:45 +00:00
|
|
|
n = 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /*GTK_ICON_SIZE_INVALID*/
|
|
|
|
n = 0;
|
|
|
|
break;
|
2010-05-18 20:55:14 +00:00
|
|
|
}
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (icon_cache[n] == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
icon_cache[n] = icon_cache_new(for_widget, icon_size);
|
|
|
|
}
|
2009-03-02 23:31:01 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
return icon_cache_get_mime_type_icon(*icon_cache[n], mime_type);
|
2010-01-05 23:47:50 +00:00
|
|
|
}
|