WIP property.
[ibus.git] / panel / languagebar.py
blob814cdcd3b9f7330a2324a84aee6a6bd59afbf13a
1 # vim:set noet ts=4:
3 # ibus - The Input Bus
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
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.
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.
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
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 from toolitem import ToolButton,\
29 ToggleToolButton, \
30 MenuToolButton
32 ICON_SIZE = gtk.ICON_SIZE_MENU
34 class LanguageBar (gtk.Toolbar):
35 __gsignals__ = {
36 "property-activate" : (
37 gobject.SIGNAL_RUN_FIRST,
38 gobject.TYPE_NONE,
39 (gobject.TYPE_STRING, gobject.TYPE_INT)),
40 "im-menu-popup" : (
41 gobject.SIGNAL_RUN_FIRST,
42 gobject.TYPE_NONE,
43 (gobject.TYPE_PYOBJECT, )),
46 def __init__ (self):
47 gtk.Toolbar.__init__ (self)
48 self.set_property ("icon-size", ICON_SIZE)
49 self._create_ui ()
51 self._properties = {}
52 self._toplevel = gtk.Window (gtk.WINDOW_POPUP)
53 self._toplevel.add (self)
55 root = gdk.get_default_root_window ()
56 workarea = root.property_get ("_NET_WORKAREA")[2]
57 self._toplevel.move (workarea[2] - 200, workarea[3] - 40)
59 def _create_ui (self):
60 # create move handle
61 self._handle = gtk.ToolItem ()
62 self._handle.add (Handle ())
63 self.insert (self._handle, -1)
65 # create input methods menu
66 self._im_menu = ToolButton (icon = "engine-default")
67 self._im_menu.connect ("clicked", lambda w: self.emit ("im-menu-popup", self._im_menu))
68 self.insert (self._im_menu, -1)
70 def _remove_properties (self):
71 # reset all properties
72 for name, props in self._properties.items ():
73 for prop, widget in props:
74 widget.hide ()
75 widget.destroy ()
76 self._properties = {}
77 self.check_resize ()
79 def do_show (self):
80 gtk.Toolbar.do_show (self)
81 self.check_resize ()
83 def do_check_resize (self):
84 width = 0
85 for item in self:
86 w, h = item.size_request ()
87 width += w
88 self.set_size_request (width + 4, -1)
90 def do_size_request (self, requisition):
91 gtk.Toolbar.do_size_request (self, requisition)
92 self._toplevel.resize (1, 1)
94 def reset (self):
95 self._remove_properties ()
97 def register_properties (self, props):
98 self._remove_properties ()
99 # create new properties
100 for prop in props:
101 if prop._type == ibus.PROP_TYPE_NORMAL:
102 widget = ToolButton (prop = prop)
103 widget.connect ("property-activate",
104 lambda w, n, s: self.emit ("property-activate", n, s))
105 elif prop._type == ibus.PROP_TYPE_TOGGLE:
106 widget = ToggleToolButton (prop = prop)
107 widget.connect ("property-activate",
108 lambda w, n, s: self.emit ("property-activate", n, s))
109 elif prop._type == ibus.PROP_TYPE_MENU:
110 widget = MenuToolButton (prop = prop)
111 widget.connect ("property-activate",
112 lambda w, n, s: self.emit ("property-activate", n, s))
113 elif prop._type == PROP_TYPE_SEPARATOR:
114 widget = gtk.SeparatorToolItem ()
115 else:
116 widget = gtk.ToolItem ()
118 widget.set_sensitive (prop._sensitive)
120 widget.set_no_show_all (True)
121 if prop._visible:
122 widget.show ()
123 else:
124 widget.hide ()
126 if not self._properties.has_key (prop._name):
127 self._properties [prop._name] = []
129 self._properties [prop._name].append ((prop, widget))
130 self.insert (widget, -1)
132 self.check_resize ()
134 def update_properties (self, props):
135 pass
137 def show_all (self):
138 self._toplevel.show_all ()
139 gtk.Toolbar.show_all (self)
141 def hide_all (self):
142 self._toplevel.hide_all ()
143 gtk.Toolbar.hide_all (self)
145 gobject.type_register (LanguageBar, "IBusLanguageBar")