Blame setup/icon.py

Packit 3ff832
# vim:set et sts=4 sw=4:
Packit 3ff832
#
Packit 3ff832
# ibus - The Input Bus
Packit 3ff832
#
Packit 3ff832
# Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
# Copyright (c) 2007-2010 Red Hat, Inc.
Packit 3ff832
#
Packit 3ff832
# This library is free software; you can redistribute it and/or
Packit 3ff832
# modify it under the terms of the GNU Lesser General Public
Packit 3ff832
# License as published by the Free Software Foundation; either
Packit 3ff832
# version 2.1 of the License, or (at your option) any later version.
Packit 3ff832
#
Packit 3ff832
# This library is distributed in the hope that it will be useful,
Packit 3ff832
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff832
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3ff832
# Lesser General Public License for more details.
Packit 3ff832
#
Packit 3ff832
# You should have received a copy of the GNU Lesser General Public
Packit 3ff832
# License along with this library; if not, write to the Free Software
Packit 3ff832
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit 3ff832
# USA
Packit 3ff832
Packit 3ff832
__all__ = (
Packit 3ff832
    "load_icon"
Packit 3ff832
)
Packit 3ff832
Packit 3ff832
from gi.repository import Gdk
Packit 3ff832
from gi.repository import GdkPixbuf
Packit 3ff832
from gi.repository import Gtk
Packit 3ff832
from os import path
Packit 3ff832
Packit 3ff832
Packit 3ff832
icon_theme = Gtk.IconTheme.get_default()
Packit 3ff832
dir = path.dirname(__file__)
Packit 3ff832
icondir = path.join(dir, "..", "icons")
Packit 3ff832
icon_theme.prepend_search_path(icondir)
Packit 3ff832
Packit 3ff832
icon_cache = {}
Packit 3ff832
Packit 3ff832
# load_icon:
Packit 3ff832
# @icon_name_or_path: Can be a name or path but not stock id
Packit 3ff832
#     because gtk_icon_theme_load_icon() cannot fallback the stock id to
Packit 3ff832
#     a real file name against gtk_image_new_from_stock().
Packit 3ff832
# @size: #GtkIconSize
Packit 3ff832
def load_icon(icon_name_or_path, size):
Packit 3ff832
    if (icon_name_or_path, size) in icon_cache:
Packit 3ff832
        return icon_cache[(icon_name_or_path, size)]
Packit 3ff832
Packit 3ff832
    icon_size = Gtk.icon_size_lookup(size)
Packit 3ff832
    if icon_size[0]:
Packit 3ff832
        icon_size = icon_size[1]
Packit 3ff832
Packit 3ff832
    pixbuf = None
Packit 3ff832
    try:
Packit 3ff832
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(icon_name_or_path)
Packit 3ff832
        w, h = pixbuf.get_width(), pixbuf.get_height()
Packit 3ff832
        rate = max(w, h) / float(icon_size)
Packit 3ff832
        w = int(w / rate)
Packit 3ff832
        h = int(h / rate)
Packit 3ff832
        pixbuf = pixbuf.scale_simple(w, h, GdkPixbuf.InterpType.BILINEAR)
Packit 3ff832
    except:
Packit 3ff832
        # import traceback
Packit 3ff832
        # traceback.print_exc()
Packit 3ff832
        pass
Packit 3ff832
    if pixbuf == None:
Packit 3ff832
        try:
Packit 3ff832
            theme = Gtk.IconTheme.get_default()
Packit 3ff832
            pixbuf = theme.load_icon(icon_name_or_path, icon_size, 0)
Packit 3ff832
        except:
Packit 3ff832
            # import traceback
Packit 3ff832
            # traceback.print_exc()
Packit 3ff832
            pass
Packit 3ff832
    if pixbuf == None:
Packit 3ff832
        try:
Packit 3ff832
            theme = Gtk.IconTheme.get_default()
Packit 3ff832
            pixbuf = theme.load_icon('ibus-engine', icon_size, 0)
Packit 3ff832
        except:
Packit 3ff832
            # import traceback
Packit 3ff832
            # traceback.print_exc()
Packit 3ff832
            pass
Packit 3ff832
    if pixbuf == None:
Packit 3ff832
        try:
Packit 3ff832
            theme = Gtk.IconTheme.get_default()
Packit 3ff832
            pixbuf = theme.load_icon('image-missing', icon_size, 0)
Packit 3ff832
        except:
Packit 3ff832
            # import traceback
Packit 3ff832
            # traceback.print_exc()
Packit 3ff832
            pass
Packit 3ff832
    icon_cache[(icon_name_or_path, size)] = pixbuf
Packit 3ff832
    return pixbuf