Cleanup the vala code of gtk3 panel and tool by:
[platform/upstream/ibus.git] / ui / gtk3 / iconwidget.vala
1 /* vim:set et sts=4 sw=4:
2  *
3  * ibus - The Input Bus
4  *
5  * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or(at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA  02111-1307  USA
21  */
22
23 class IconWidget: Gtk.Image {
24     public IconWidget(string icon, int size) {
25         Gdk.Pixbuf pixbuf = null;
26         try {
27             if (icon[0] == '/') {
28                 pixbuf = new Gdk.Pixbuf.from_file(icon);
29             } else {
30                 var theme = Gtk.IconTheme.get_default();
31                 pixbuf = theme.load_icon(icon, size, 0);
32             }
33         } catch (GLib.Error e) {
34             try {
35                 var theme = Gtk.IconTheme.get_default();
36                 pixbuf = theme.load_icon(Gtk.Stock.MISSING_IMAGE, size, 0);
37             } catch (GLib.Error e) {}
38         }
39
40         if (pixbuf == null)
41             return;
42         float width = (float)pixbuf.get_width();
43         float height = (float)pixbuf.get_height();
44         float scale = size / (width > height ? width : height);
45         width *= scale;
46         height *= scale;
47
48         pixbuf = pixbuf.scale_simple((int)width, (int)height, Gdk.InterpType.BILINEAR);
49         set_from_pixbuf(pixbuf);
50         show();
51     }
52 }