Set icon search path.
[platform/upstream/ibus.git] / panel / languagebar.py
1 # vim:set noet ts=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright (c) 2007-2008 Huang Peng <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 import gtk
23 import gtk.gdk as gdk
24 import gobject
25 import ibus
26 from image import Image
27 from handle import Handle
28
29 ICON_SIZE = gtk.ICON_SIZE_MENU
30
31 class LanguageBar (gtk.Toolbar):
32         __gsignals__ = {
33                 "property-activate" : (
34                         gobject.SIGNAL_RUN_FIRST,
35                         gobject.TYPE_NONE,
36                         (gobject.TYPE_STRING, )),
37                 "im-menu-popup" : (
38                         gobject.SIGNAL_RUN_FIRST,
39                         gobject.TYPE_NONE,
40                         (gobject.TYPE_PYOBJECT, )),
41                 }
42
43         def __init__ (self):
44                 gtk.Toolbar.__init__ (self)
45                 self.set_property ("icon-size", ICON_SIZE)
46                 self._create_ui ()
47
48                 self._properties = {}
49                 self._toplevel = gtk.Window (gtk.WINDOW_POPUP)
50                 self._toplevel.add (self)
51
52                 root = gdk.get_default_root_window ()
53                 workarea = root.property_get ("_NET_WORKAREA")[2]
54                 self._toplevel.move (workarea[2] - 200, workarea[3] - 40)
55
56         def _add_items (self):
57                 img = gtk.image_new_from_icon_name ("engine-default", ICON_SIZE)
58                 btn = gtk.ToolButton (img, "engine")
59                 btn.connect ("clicked", lambda x: self._add_items ())
60                 self.insert (btn, -1)
61
62                 img = gtk.image_new_from_icon_name ("ibus-keyboard", ICON_SIZE)
63                 btn = gtk.ToolButton (img, "keyboard")
64                 self.insert (btn, -1)
65
66                 img = gtk.image_new_from_icon_name ("ibus-zh", ICON_SIZE)
67                 btn = gtk.ToolButton (img, "keyboard")
68                 self.insert (btn, -1)
69
70                 self.insert (gtk.SeparatorToolItem (), -1)
71                 self.show_all ()
72                 self.check_resize ()
73
74         def _create_ui (self):
75                 # create move handle
76                 self._handle = gtk.ToolItem ()
77                 self._handle.add (Handle ())
78                 self.insert (self._handle, -1)
79
80                 # create input methods menu
81                 image = gtk.image_new_from_icon_name ("engine-default", gtk.ICON_SIZE_MENU)
82                 self._im_menu = gtk.ToolButton (icon_widget = image)
83                 self._im_menu.connect ("clicked", lambda w: self.emit ("im-menu-popup", self._im_menu))
84                 self.insert (self._im_menu, -1)
85
86         def _remove_properties (self):
87                 # reset all properties
88                 for name, props in self._properties.items ():
89                         for prop, widget in props:
90                                 widget.hide ()
91                                 widget.destroy ()
92                 self._properties = {}
93                 self.check_resize ()
94
95         def do_show (self):
96                 gtk.Toolbar.do_show (self)
97                 self.check_resize ()
98
99         def do_check_resize (self):
100                 width = 0
101                 for item in self:
102                         w, h = item.size_request ()
103                         width += w
104                 self.set_size_request (width + 4, -1)
105
106         def do_size_request (self, requisition):
107                 gtk.Toolbar.do_size_request (self, requisition)
108                 self._toplevel.resize (1, 1)
109
110         def reset (self):
111                 self._remove_properties ()
112
113         def register_properties (self, props):
114                 self._remove_properties ()
115                 # create new properties
116                 for prop in props:
117                         if prop._type == ibus.PROP_TYPE_NORMAL:
118                                 widget = gtk.ToolButton ()
119                                 widget.set_icon_name (prop._icon)
120                                 widget.set_label (prop._label)
121                                 widget.connect ("clicked",
122                                                 lambda widget, prop: self.emit ("property-activate", prop._name),
123                                                 prop)
124                         else:
125                                 widget = gtk.ToolItem ()
126
127                         widget.set_sensitive (prop._sensitive)
128                         if prop._visible:
129                                 widget.set_no_show_all (False)
130                                 widget.show ()
131                         else:
132                                 widget.set_no_show_all (True)
133                                 widget.hide ()
134                         if not self._properties.has_key (prop._name):
135                                 self._properties [prop._name] = []
136                         self._properties [prop._name].append ((prop, widget))
137                         self.insert (widget, -1)
138                 self.check_resize ()
139
140         def update_properties (self, props):
141                 pass
142
143         def show_all (self):
144                 self._toplevel.show_all ()
145                 gtk.Toolbar.show_all (self)
146
147         def hide_all (self):
148                 self._toplevel.hide_all ()
149                 gtk.Toolbar.hide_all (self)
150
151 gobject.type_register (LanguageBar, "IBusLanguageBar")
152