527c5051890ffd7860d67f7344fe0428be1b88ba
[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                 icon_theme = gtk.icon_theme_get_default ()
47                 icon_theme.prepend_search_path ("/home/phuang/sources/ibus/icons")
48                 self._create_ui ()
49
50                 self._properties = {}
51                 self._toplevel = gtk.Window (gtk.WINDOW_POPUP)
52                 self._toplevel.add (self)
53
54                 root = gdk.get_default_root_window ()
55                 workarea = root.property_get ("_NET_WORKAREA")[2]
56                 self._toplevel.move (workarea[2] - 200, workarea[3] - 40)
57
58         def _add_items (self):
59                 img = gtk.image_new_from_icon_name ("engine-default", ICON_SIZE)
60                 btn = gtk.ToolButton (img, "engine")
61                 btn.connect ("clicked", lambda x: self._add_items ())
62                 self.insert (btn, -1)
63
64                 img = gtk.image_new_from_icon_name ("ibus-keyboard", ICON_SIZE)
65                 btn = gtk.ToolButton (img, "keyboard")
66                 self.insert (btn, -1)
67
68                 img = gtk.image_new_from_icon_name ("ibus-zh", ICON_SIZE)
69                 btn = gtk.ToolButton (img, "keyboard")
70                 self.insert (btn, -1)
71
72                 self.insert (gtk.SeparatorToolItem (), -1)
73                 self.show_all ()
74                 self.check_resize ()
75
76         def _create_ui (self):
77                 # create move handle
78                 self._handle = gtk.ToolItem ()
79                 self._handle.add (Handle ())
80                 self.insert (self._handle, -1)
81
82                 # create input methods menu
83                 image = gtk.image_new_from_icon_name ("engine-default", gtk.ICON_SIZE_MENU)
84                 self._im_menu = gtk.ToolButton (icon_widget = image)
85                 self._im_menu.connect ("clicked", lambda w: self.emit ("im-menu-popup", self._im_menu))
86                 self.insert (self._im_menu, -1)
87
88         def _remove_properties (self):
89                 # reset all properties
90                 for name, props in self._properties.items ():
91                         for prop, widget in props:
92                                 widget.hide ()
93                                 widget.destroy ()
94                 self._properties = {}
95                 self.check_resize ()
96
97         def do_show (self):
98                 gtk.Toolbar.do_show (self)
99                 self.check_resize ()
100
101         def do_check_resize (self):
102                 width = 0
103                 for item in self:
104                         w, h = item.size_request ()
105                         width += w
106                 self.set_size_request (width + 4, -1)
107
108         def do_size_request (self, requisition):
109                 gtk.Toolbar.do_size_request (self, requisition)
110                 self._toplevel.resize (1, 1)
111
112         def reset (self):
113                 self._remove_properties ()
114
115         def register_properties (self, props):
116                 self._remove_properties ()
117                 # create new properties
118                 for prop in props:
119                         if prop._type == ibus.PROP_TYPE_NORMAL:
120                                 widget = gtk.ToolButton ()
121                                 widget.set_icon_name (prop._icon)
122                                 widget.set_label (prop._label)
123                                 widget.connect ("clicked",
124                                                 lambda widget, prop: self.emit ("property-activate", prop._name),
125                                                 prop)
126                         else:
127                                 widget = gtk.ToolItem ()
128
129                         widget.set_sensitive (prop._sensitive)
130                         if prop._visible:
131                                 widget.set_no_show_all (False)
132                                 widget.show ()
133                         else:
134                                 widget.set_no_show_all (True)
135                                 widget.hide ()
136                         if not self._properties.has_key (prop._name):
137                                 self._properties [prop._name] = []
138                         self._properties [prop._name].append ((prop, widget))
139                         self.insert (widget, -1)
140                 self.check_resize ()
141
142         def update_properties (self, props):
143                 pass
144
145         def show_all (self):
146                 self._toplevel.show_all ()
147                 gtk.Toolbar.show_all (self)
148
149         def hide_all (self):
150                 self._toplevel.hide_all ()
151                 gtk.Toolbar.hide_all (self)
152
153 gobject.type_register (LanguageBar, "IBusLanguageBar")
154