Blame bindings/vala/iconwidget.vala

Packit 3ff832
/* vim:set et sts=4 sw=4:
Packit 3ff832
 *
Packit 3ff832
 * ibus - The Input Bus
Packit 3ff832
 *
Packit 3ff832
 * Copyright(c) 2011-2014 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
 * Copyright(c) 2018 Takao Fujiwara <takao.fujiwara1@gmail.com>
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
Packit 3ff832
class ThemedRGBA {
Packit 3ff832
    public Gdk.RGBA *normal_fg { get; set; }
Packit 3ff832
    public Gdk.RGBA *normal_bg { get; set; }
Packit 3ff832
    public Gdk.RGBA *selected_fg { get; set; }
Packit 3ff832
    public Gdk.RGBA *selected_bg { get; set; }
Packit 3ff832
Packit 3ff832
    private Gtk.StyleContext m_style_context;
Packit 3ff832
Packit 3ff832
    public ThemedRGBA(Gtk.Widget widget) {
Packit 3ff832
        this.normal_fg = null;
Packit 3ff832
        this.normal_bg = null;
Packit 3ff832
        this.selected_fg = null;
Packit 3ff832
        this.selected_bg = null;
Packit 3ff832
Packit 3ff832
        /* Use the color of Gtk.TextView instead of Gtk.Label
Packit 3ff832
         * because the selected label "color" is not configured
Packit 3ff832
         * in "Adwaita" theme and the selected label "background-color"
Packit 3ff832
         * is not configured in "Maia" theme.
Packit 3ff832
         * https://github.com/ibus/ibus/issues/1871
Packit 3ff832
         */
Packit 3ff832
        Gtk.WidgetPath widget_path = new Gtk.WidgetPath();
Packit 3ff832
        widget_path.append_type(typeof(Gtk.TextView));
Packit 3ff832
        m_style_context = new Gtk.StyleContext();
Packit 3ff832
        m_style_context.set_path(widget_path);
Packit 3ff832
        m_style_context.add_class(Gtk.STYLE_CLASS_VIEW);
Packit 3ff832
Packit 3ff832
        /* "-gtk-secondary-caret-color" value is different
Packit 3ff832
         * if the parent widget is set in "Menta" theme.
Packit 3ff832
         */
Packit 3ff832
        m_style_context.set_parent(widget.get_style_context());
Packit 3ff832
Packit 3ff832
        get_rgba();
Packit 3ff832
Packit 3ff832
        m_style_context.changed.connect(() => { get_rgba(); });
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    ~ThemedRGBA() {
Packit 3ff832
        reset_rgba();
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    private void reset_rgba() {
Packit 3ff832
        if (this.normal_fg != null) {
Packit 3ff832
            this.normal_fg.free();
Packit 3ff832
            this.normal_fg = null;
Packit 3ff832
        }
Packit 3ff832
        if (this.normal_bg != null) {
Packit 3ff832
            this.normal_bg.free();
Packit 3ff832
            this.normal_bg = null;
Packit 3ff832
        }
Packit 3ff832
        if (this.selected_fg != null) {
Packit 3ff832
            this.selected_fg.free();
Packit 3ff832
            this.selected_fg = null;
Packit 3ff832
        }
Packit 3ff832
        if (this.selected_bg != null) {
Packit 3ff832
            this.selected_bg.free();
Packit 3ff832
            this.selected_bg = null;
Packit 3ff832
        }
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    private void get_rgba() {
Packit 3ff832
        reset_rgba();
Packit 3ff832
        Gdk.RGBA *normal_fg = null;
Packit 3ff832
        Gdk.RGBA *normal_bg = null;
Packit 3ff832
        Gdk.RGBA *selected_fg = null;
Packit 3ff832
        Gdk.RGBA *selected_bg = null;
Packit 3ff832
        m_style_context.get(Gtk.StateFlags.NORMAL,
Packit 3ff832
                            "color",
Packit 3ff832
                            out normal_fg);
Packit 3ff832
        m_style_context.get(Gtk.StateFlags.SELECTED,
Packit 3ff832
                            "color",
Packit 3ff832
                            out selected_fg);
Packit 3ff832
Packit 3ff832
        string bg_prop = "background-color";
Packit 3ff832
        m_style_context.get(Gtk.StateFlags.NORMAL,
Packit 3ff832
                            bg_prop,
Packit 3ff832
                            out normal_bg);
Packit 3ff832
        m_style_context.get(Gtk.StateFlags.SELECTED,
Packit 3ff832
                            bg_prop,
Packit 3ff832
                            out selected_bg);
Packit 3ff832
        if (normal_bg.red   == selected_bg.red &&
Packit 3ff832
            normal_bg.green == selected_bg.green &&
Packit 3ff832
            normal_bg.blue  == selected_bg.blue &&
Packit 3ff832
            normal_bg.alpha == selected_bg.alpha) {
Packit 3ff832
            normal_bg.free();
Packit 3ff832
            normal_bg = null;
Packit 3ff832
            normal_bg.free();
Packit 3ff832
            normal_bg = null;
Packit 3ff832
            bg_prop = "-gtk-secondary-caret-color";
Packit 3ff832
            m_style_context.get(Gtk.StateFlags.NORMAL,
Packit 3ff832
                                bg_prop,
Packit 3ff832
                                out normal_bg);
Packit 3ff832
            m_style_context.get(Gtk.StateFlags.SELECTED,
Packit 3ff832
                                bg_prop,
Packit 3ff832
                                out selected_bg);
Packit 3ff832
        }
Packit 3ff832
        this.normal_fg   = normal_fg;
Packit 3ff832
        this.normal_bg   = normal_bg;
Packit 3ff832
        this.selected_fg = selected_fg;
Packit 3ff832
        this.selected_bg = selected_bg;
Packit 3ff832
    }
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
class IconWidget: Gtk.Image {
Packit 3ff832
    /**
Packit 3ff832
     * IconWidget:
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
Packit 3ff832
     *     stock id to a real file name against
Packit 3ff832
     *     gtk_image_new_from_stock().
Packit 3ff832
     * @size: #Gtk.IconSize
Packit 3ff832
     */
Packit 3ff832
    public IconWidget(string icon_name_or_path, Gtk.IconSize size) {
Packit 3ff832
        Gdk.Pixbuf pixbuf = null;
Packit 3ff832
        int fixed_width, fixed_height;
Packit 3ff832
        Gtk.icon_size_lookup(size, out fixed_width, out fixed_height);
Packit 3ff832
Packit 3ff832
        try {
Packit 3ff832
            if (icon_name_or_path[0] == '/') {
Packit 3ff832
                pixbuf = new Gdk.Pixbuf.from_file(icon_name_or_path);
Packit 3ff832
            } else {
Packit 3ff832
                var theme = Gtk.IconTheme.get_default();
Packit 3ff832
                pixbuf = theme.load_icon(icon_name_or_path, fixed_width, 0);
Packit 3ff832
            }
Packit 3ff832
        } catch (GLib.Error e) {
Packit 3ff832
            try {
Packit 3ff832
                var theme = Gtk.IconTheme.get_default();
Packit 3ff832
                pixbuf = theme.load_icon("ibus-engine", fixed_width, 0);
Packit 3ff832
            } catch (GLib.Error e) {
Packit 3ff832
                set_from_icon_name("image-missing", size);
Packit 3ff832
                return;
Packit 3ff832
            }
Packit 3ff832
        }
Packit 3ff832
Packit 3ff832
        if (pixbuf == null)
Packit 3ff832
            return;
Packit 3ff832
        float width = (float)pixbuf.get_width();
Packit 3ff832
        float height = (float)pixbuf.get_height();
Packit 3ff832
        float scale = fixed_width / (width > height ? width : height);
Packit 3ff832
        width *= scale;
Packit 3ff832
        height *= scale;
Packit 3ff832
Packit 3ff832
        pixbuf = pixbuf.scale_simple((int)width, (int)height, Gdk.InterpType.BILINEAR);
Packit 3ff832
        set_from_pixbuf(pixbuf);
Packit 3ff832
        show();
Packit 3ff832
    }
Packit 3ff832
}