Remove gnome-icon-theme-legacy dependency.
authorfujiwarat <takao.fujiwara1@gmail.com>
Thu, 22 Nov 2012 06:07:12 +0000 (15:07 +0900)
committerfujiwarat <takao.fujiwara1@gmail.com>
Thu, 22 Nov 2012 06:07:12 +0000 (15:07 +0900)
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
setup/enginecombobox.py
setup/enginetreeview.py
setup/icon.py
ui/gtk3/iconwidget.vala
ui/gtk3/panel.vala
ui/gtk3/property.vala
ui/gtk3/switcher.vala

index 7e2813a..a020f8c 100644 (file)
@@ -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)
index f80b164..0f8a6ae 100644 (file)
@@ -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):
index 8ade7db..51e4fa2 100644 (file)
@@ -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):
index be8a38a..a4a2919 100644 (file)
@@ -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
index 4dfcfea..e4ff9c2 100644 (file)
  */
 
 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;
 
index c159693..3df0be7 100644 (file)
@@ -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;
index 718ba94..1dabccd 100644 (file)
@@ -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() {
index 494fab9..74c3372 100644 (file)
@@ -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();