From dd6acfa5013b35b61aeb4085eef7da63f581f2f8 Mon Sep 17 00:00:00 2001 From: fujiwarat Date: Thu, 22 Nov 2012 15:07:12 +0900 Subject: [PATCH] Remove gnome-icon-theme-legacy dependency. gtk_image_set_from_icon_name() accesses the icon_name only but gtk_image_set_from_stock() can fallback the stock name to the real name. E.g. "gtk-about.png" is the symlink of "help-about.png" and "gtk-about.png" is included in gnome-icon-theme-legacy package in Fedora. Review URL: https://codereview.appspot.com/6846058 --- setup/engineabout.py | 3 ++- setup/enginecombobox.py | 5 ----- setup/enginetreeview.py | 7 ------- setup/icon.py | 33 +++++++++++++++++++++++++++------ ui/gtk3/iconwidget.vala | 36 +++++++++++++++++++++++++++++------- ui/gtk3/panel.vala | 15 +++++++++------ ui/gtk3/property.vala | 4 +--- ui/gtk3/switcher.vala | 4 ++-- 8 files changed, 70 insertions(+), 37 deletions(-) diff --git a/setup/engineabout.py b/setup/engineabout.py index 7e2813a..a020f8c 100644 --- a/setup/engineabout.py +++ b/setup/engineabout.py @@ -38,7 +38,8 @@ class EngineAbout(Gtk.Dialog): self.__init_ui() def __init_ui(self): - self.set_icon_name(Gtk.STOCK_ABOUT) + # set_icon_name() cannot fallback any stock ids to the real files. + self.set_icon_name('help-about') sw = Gtk.ScrolledWindow() sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN) sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) diff --git a/setup/enginecombobox.py b/setup/enginecombobox.py index f80b164..0f8a6ae 100644 --- a/setup/enginecombobox.py +++ b/setup/enginecombobox.py @@ -116,11 +116,6 @@ class EngineComboBox(Gtk.ComboBox): renderer.set_property("visible", True) renderer.set_property("sensitive", True) pixbuf = load_icon(engine.get_icon(), Gtk.IconSize.LARGE_TOOLBAR) - if pixbuf == None: - pixbuf = load_icon("ibus-engine", Gtk.IconSize.LARGE_TOOLBAR) - if pixbuf == None: - pixbuf = load_icon(Gtk.STOCK_MISSING_IMAGE, - Gtk.IconSize.LARGE_TOOLBAR) renderer.set_property("pixbuf", pixbuf) def __name_cell_data_cb(self, celllayout, renderer, model, iter, data): diff --git a/setup/enginetreeview.py b/setup/enginetreeview.py index 8ade7db..51e4fa2 100644 --- a/setup/enginetreeview.py +++ b/setup/enginetreeview.py @@ -118,13 +118,6 @@ class EngineTreeView(Gtk.TreeView): icon_size = Gtk.icon_size_lookup(Gtk.IconSize.LARGE_TOOLBAR)[0] pixbuf = load_icon(engine.get_icon(), Gtk.IconSize.LARGE_TOOLBAR) - - if pixbuf == None: - pixbuf = load_icon("ibus-engine", Gtk.IconSize.LARGE_TOOLBAR) - if pixbuf == None: - pixbuf = load_icon(Gtk.STOCK_MISSING_IMAGE, - Gtk.IconSize.LARGE_TOOLBAR) - renderer.set_property("pixbuf", pixbuf) def __name_cell_data_cb(self, celllayout, renderer, model, it, data): diff --git a/setup/icon.py b/setup/icon.py index be8a38a..a4a2919 100644 --- a/setup/icon.py +++ b/setup/icon.py @@ -37,9 +37,14 @@ icon_theme.prepend_search_path(icondir) icon_cache = {} -def load_icon(icon, size): - if (icon, size) in icon_cache: - return icon_cache[(icon, size)] +# load_icon: +# @icon_name_or_path: Can be a name or path but not stock id +# because gtk_icon_theme_load_icon() cannot fallback the stock id to +# a real file name against gtk_image_new_from_stock(). +# @size: #GtkIconSize +def load_icon(icon_name_or_path, size): + if (icon_name_or_path, size) in icon_cache: + return icon_cache[(icon_name_or_path, size)] icon_size = Gtk.icon_size_lookup(size) if icon_size[0]: @@ -47,7 +52,7 @@ def load_icon(icon, size): pixbuf = None try: - pixbuf = GdkPixbuf.Pixbuf.new_from_file(icon) + pixbuf = GdkPixbuf.Pixbuf.new_from_file(icon_name_or_path) w, h = pixbuf.get_width(), pixbuf.get_height() rate = max(w, h) / float(icon_size) w = int(w / rate) @@ -60,10 +65,26 @@ def load_icon(icon, size): if pixbuf == None: try: theme = Gtk.IconTheme.get_default() - pixbuf = theme.load_icon(icon, icon_size, 0) + pixbuf = theme.load_icon(icon_name_or_path, icon_size, 0) except: # import traceback # traceback.print_exc() pass - icon_cache[(icon, size)] = pixbuf + if pixbuf == None: + try: + theme = Gtk.IconTheme.get_default() + pixbuf = theme.load_icon('ibus-engine', icon_size, 0) + except: + # import traceback + # traceback.print_exc() + pass + if pixbuf == None: + try: + theme = Gtk.IconTheme.get_default() + pixbuf = theme.load_icon('image-missing', icon_size, 0) + except: + # import traceback + # traceback.print_exc() + pass + icon_cache[(icon_name_or_path, size)] = pixbuf return pixbuf diff --git a/ui/gtk3/iconwidget.vala b/ui/gtk3/iconwidget.vala index 4dfcfea..e4ff9c2 100644 --- a/ui/gtk3/iconwidget.vala +++ b/ui/gtk3/iconwidget.vala @@ -21,27 +21,49 @@ */ class IconWidget: Gtk.Image { - public IconWidget(string icon, int size) { + /** + * IconWidget: + * @icon_name_or_path: Can be a name or path but not stock id + * because gtk_icon_theme_load_icon() cannot fallback the + * stock id to a real file name against + * gtk_image_new_from_stock(). + * @size: #Gtk.IconSize + */ + public IconWidget(string icon_name_or_path, Gtk.IconSize size) { Gdk.Pixbuf pixbuf = null; + int fixed_width, fixed_height; + Gtk.icon_size_lookup(size, out fixed_width, out fixed_height); + try { - if (icon[0] == '/') { - pixbuf = new Gdk.Pixbuf.from_file(icon); + if (icon_name_or_path[0] == '/') { + pixbuf = new Gdk.Pixbuf.from_file(icon_name_or_path); } else { var theme = Gtk.IconTheme.get_default(); - pixbuf = theme.load_icon(icon, size, 0); + pixbuf = theme.load_icon(icon_name_or_path, fixed_width, 0); } } catch (GLib.Error e) { try { var theme = Gtk.IconTheme.get_default(); - pixbuf = theme.load_icon(Gtk.Stock.MISSING_IMAGE, size, 0); - } catch (GLib.Error e) {} + pixbuf = theme.load_icon("ibus-engine", fixed_width, 0); + } catch (GLib.Error e) { + /* "gtk-missing-image.png" is the symlink of + * "image-missing.png" and included in + * gnome-icon-theme-legacy package in fedora. + * gtk_image_set_from_stock() can fallback the stock name + * to the real name instead of gtk_image_set_from_icon_name() + * or gtk_icon_theme_load_icon() and + * could remove gnome-icon-theme-legacy. + */ + set_from_stock(Gtk.Stock.MISSING_IMAGE, size); + return; + } } if (pixbuf == null) return; float width = (float)pixbuf.get_width(); float height = (float)pixbuf.get_height(); - float scale = size / (width > height ? width : height); + float scale = fixed_width / (width > height ? width : height); width *= scale; height *= scale; diff --git a/ui/gtk3/panel.vala b/ui/gtk3/panel.vala index c159693..3df0be7 100644 --- a/ui/gtk3/panel.vala +++ b/ui/gtk3/panel.vala @@ -492,9 +492,6 @@ class Panel : IBus.PanelService { m_ime_menu.append(new Gtk.SeparatorMenuItem()); - int width, height; - Gtk.icon_size_lookup(Gtk.IconSize.MENU, out width, out height); - // Append IMEs foreach (var engine in m_engines) { var language = engine.get_language(); @@ -502,7 +499,7 @@ class Panel : IBus.PanelService { var item = new Gtk.ImageMenuItem.with_label( "%s - %s".printf (IBus.get_language_name(language), longname)); if (engine.get_icon() != "") { - var icon = new IconWidget(engine.get_icon(), width); + var icon = new IconWidget(engine.get_icon(), Gtk.IconSize.MENU); item.set_image(icon); } // Make a copy of engine to workaround a bug in vala. @@ -590,8 +587,14 @@ class Panel : IBus.PanelService { if (icon_name[0] == '/') m_status_icon.set_from_file(icon_name); - else - m_status_icon.set_from_icon_name(icon_name); + else { + var theme = Gtk.IconTheme.get_default(); + if (theme.lookup_icon(icon_name, 48, 0) != null) { + m_status_icon.set_from_icon_name(icon_name); + } else { + m_status_icon.set_from_icon_name("ibus-engine"); + } + } if (engine == null) return; diff --git a/ui/gtk3/property.vala b/ui/gtk3/property.vala index 718ba94..1dabccd 100644 --- a/ui/gtk3/property.vala +++ b/ui/gtk3/property.vala @@ -129,9 +129,7 @@ public class PropImageMenuItem : Gtk.ImageMenuItem, IPropItem { } private void set_icon(string icon) { - int width, height; - Gtk.icon_size_lookup(Gtk.IconSize.MENU, out width, out height); - set_image(new IconWidget(icon, width)); + set_image(new IconWidget(icon, Gtk.IconSize.MENU)); } public override void activate() { diff --git a/ui/gtk3/switcher.vala b/ui/gtk3/switcher.vala index 494fab9..74c3372 100644 --- a/ui/gtk3/switcher.vala +++ b/ui/gtk3/switcher.vala @@ -24,7 +24,6 @@ class Switcher : Gtk.Window { public extern const bool USE_SYMBOL_ICON; private const int DEFAULT_FONT_SIZE = 16; private const int DESC_LABEL_MAX_LEN = 20; - private const int ICON_SIZE = 48; private class IBusEngineButton : Gtk.Button { public IBusEngineButton(IBus.EngineDesc engine) { @@ -36,7 +35,8 @@ class Switcher : Gtk.Window { add(align); if (!USE_SYMBOL_ICON) { - IconWidget icon = new IconWidget(engine.get_icon(), ICON_SIZE); + IconWidget icon = new IconWidget(engine.get_icon(), + Gtk.IconSize.DIALOG); align.add(icon); } else { var language = engine.get_language(); -- 2.7.4