WIP.
[ibus.git] / panel / panel.py
blob1c466c12c83d0f7a7d8412e79fff8270d880e0f6
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 os import path
27 from lang import LANGUAGES
28 from ibus import interface
29 from languagebar import LanguageBar
30 from candidatepanel import CandidatePanel
32 class Panel (ibus.Object):
33 def __init__ (self, proxy, _ibus):
34 gobject.GObject.__init__ (self)
35 self._proxy = proxy
36 self._ibus = _ibus
37 self._focus_ic = None
39 # add icon search path
40 icon_theme = gtk.icon_theme_get_default ()
41 dir = path.dirname (__file__)
42 icondir = path.join (dir, "..", "icons")
43 icon_theme.prepend_search_path (icondir)
45 self._language_bar = LanguageBar ()
46 self._language_bar.connect ("property-activate",
47 lambda widget, prop_name, prop_state: self._proxy.PropertyActivate (prop_name, prop_state))
48 self._language_bar.connect ("get-im-menu",
49 self._get_im_menu_cb)
50 self._language_bar.focus_out ()
51 self._language_bar.show_all ()
53 self._candidate_panel = CandidatePanel ()
54 self._candidate_panel.connect ("cursor-up",
55 lambda widget: self._proxy.CursorUp ())
56 self._candidate_panel.connect ("cursor-down",
57 lambda widget: self._proxy.CursorDown ())
59 self._status_icon = gtk.StatusIcon ()
60 self._status_icon.connect ("popup-menu", self._status_icon_popup_menu_cb)
61 self._status_icon.connect ("activate", self._status_icon_activate_cb)
62 self._status_icon.set_from_icon_name ("engine-default")
63 self._status_icon.set_tooltip ("iBus - Running")
64 self._status_icon.set_visible (True)
66 def set_cursor_location (self, x, y, w, h):
67 self._candidate_panel.move (x + w, y + h)
69 def update_preedit (self, text, attrs, cursor_pos, show):
70 self._candidate_panel.update_preedit (text, attrs, cursor_pos, show)
72 def show_preedit_string (self):
73 self._candidate_panel.show_preedit_string ()
75 def hide_preedit_string (self):
76 self._candidate_panel.hide_preedit_string ()
78 def update_aux_string (self, text, attrs, show):
79 self._candidate_panel.update_aux_string (text, attrs, show)
81 def show_aux_string (self):
82 self._candidate_panel.show_aux_string ()
84 def hide_aux_string (self):
85 self._candidate_panel.hide_aux_string ()
87 def update_lookup_table (self, lookup_table, show):
88 self._candidate_panel.update_lookup_table (lookup_table, show)
90 def show_candidate_window (self):
91 self._candidate_panel.show ()
93 def hide_candidate_window (self):
94 self._candidate_panel.hide ()
96 def show_language_bar (self):
97 self._language_bar.show ()
99 def hide_language_bar (self):
100 self._language_bar.hide ()
102 def register_properties (self, props):
103 self._language_bar.register_properties (props)
105 def update_property (self, prop):
106 self._language_bar.update_property (self, prop)
108 def _set_im_icon (self, icon_name):
109 self._language_bar.set_im_icon (icon_name)
110 self._status_icon.set_from_icon_name (icon_name)
112 def focus_in (self, ic):
113 self.reset ()
114 self._focus_ic = ic
116 factory, enabled = self._ibus.GetInputContextStates (ic)
118 if factory == "" or not enabled:
119 self._set_im_icon ("engine-default")
120 else:
121 name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory)
122 self._set_im_icon (icon)
123 self._language_bar.focus_in ()
125 def focus_out (self, ic):
126 self.reset ()
127 if self._focus_ic == ic:
128 self._focus_ic = None
129 self._language_bar.focus_out ()
130 self._set_im_icon ("engine-default")
132 def states_changed (self):
133 if not self._focus_ic:
134 return
135 factory, enabled = self._ibus.GetInputContextStates (self._focus_ic)
136 if not enabled:
137 self._set_im_icon ("engine-default")
138 else:
139 name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory)
140 self._set_im_icon (icon)
142 def reset (self):
143 self._candidate_panel.reset ()
144 self._language_bar.reset ()
146 def do_destroy (self):
147 gtk.main_quit ()
149 def _create_im_menu (self):
150 menu = gtk.Menu ()
151 factories = self._ibus.GetFactories ()
152 if not factories:
153 item = gtk.MenuItem (label = "no engine")
154 item.set_sensitive (False)
155 menu.add (item)
156 else:
157 for factory in factories:
158 name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory)
159 item = gtk.ImageMenuItem ("%s - %s" % (LANGUAGES.get (lang, lang), name))
160 if not icon:
161 icon = "engine-default"
162 item.set_image (gtk.image_new_from_icon_name (icon, gtk.ICON_SIZE_MENU))
163 item.connect ("activate", self._menu_item_activate_cb, factory)
164 menu.add (item)
166 menu.show_all ()
167 menu.set_take_focus (False)
168 return menu
170 def _get_im_menu_cb (self, languagebar):
171 menu = self._create_im_menu ()
172 return menu
174 def _status_icon_activate_cb (self, status_icon):
175 if not self._focus_ic:
176 return
177 menu = self._create_im_menu ()
178 menu.popup (None, None,
179 gtk.status_icon_position_menu,
181 gtk.get_current_event_time (),
182 self._status_icon)
184 def _status_icon_popup_menu_cb (self, status_icon, button, active_time):
185 if not self._focus_ic:
186 return
187 menu = self._create_im_menu ()
188 menu.popup (None, None,
189 gtk.status_icon_position_menu,
190 button,
191 active_time,
192 self._status_icon)
194 def _menu_item_activate_cb (self, item, factory):
195 self._ibus.SetFactory (factory)
197 gobject.type_register (Panel, "IBusPanel")
199 class PanelProxy (interface.IPanel):
200 def __init__ (self, dbusconn, object_path, _ibus):
201 interface.IPanel.__init__ (self, dbusconn, object_path)
202 self._dbusconn = dbusconn
203 self._panel = Panel (self, _ibus)
205 def SetCursorLocation (self, x, y, w, h):
206 self._panel.set_cursor_location (x, y, w, h)
208 def UpdatePreedit (self, text, attrs, cursor_pos, show):
209 attrs = ibus.attr_list_from_dbus_value (attrs)
210 self._panel.update_preedit (text, atrrs, cursor_pos, show)
212 def ShowPreeditString (self):
213 self._panel.show_preedit_string ()
215 def HidePreeditString (self):
216 self._panel.hide_preedit_string ()
218 def UpdateAuxString (self, text, attrs, show):
219 attrs = ibus.attr_list_from_dbus_value (attrs)
220 self._panel.update_aux_string (text, attrs, show)
222 def ShowAuxString (self):
223 self._panel.show_aux_string ()
225 def HideAuxString (self):
226 self._panel.hide_aux_string ()
228 def UpdateLookupTable (self, lookup_table, show):
229 lookup_table = ibus.lookup_table_from_dbus_value (lookup_table)
230 self._panel.update_lookup_table (lookup_table, show)
232 def ShowCandidateWindow (self):
233 self._panel.show_candidate_window ()
235 def HideCandidateWindow (self):
236 self._panel.hide_candidate_window ()
238 def ShowLanguageBar (self):
239 self._panel.show_language_bar ()
241 def HideLanguageBar (self):
242 self._panel.hide_language_bar ()
244 def RegisterProperties (self, props):
245 props = ibus.prop_list_from_dbus_value (props)
246 self._panel.register_properties (props)
248 def UpdateProperty (self, prop):
249 prop = ibus.property_from_dbus_value (props)
250 self._panel.update_property (prop)
252 def FocusIn (self, ic):
253 self._panel.focus_in (ic)
255 def FocusOut (self, ic):
256 self._panel.focus_out (ic)
258 def StatesChanged (self):
259 self._panel.states_changed ()
261 def Reset (self):
262 self._panel.reset ()
264 def Destroy (self):
265 self._panel.destroy ()