Set default value of InputMode.
[ibus.git] / panel / panel.py
blob3e5b76fcc22b008e5b892f8b0a87fb2d0ff1d3f9
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
38 # add icon search path
39 icon_theme = gtk.icon_theme_get_default ()
40 dir = path.dirname (__file__)
41 icondir = path.join (dir, "..", "icons")
42 icon_theme.prepend_search_path (icondir)
44 self._language_bar = LanguageBar ()
45 self._language_bar.connect ("property-activate",
46 lambda widget, prop_name, prop_state: self._proxy.PropertyActivate (prop_name, prop_state))
47 self._language_bar.connect ("get-im-menu",
48 self._get_im_menu_cb)
49 self._language_bar.show_all ()
51 self._candidate_panel = CandidatePanel ()
52 self._candidate_panel.connect ("cursor-up",
53 lambda widget: self._proxy.CursorUp ())
54 self._candidate_panel.connect ("cursor-down",
55 lambda widget: self._proxy.CursorDown ())
57 self._status_icon = gtk.StatusIcon ()
58 self._status_icon.connect ("popup-menu", self._status_icon_popup_menu_cb)
59 self._status_icon.connect ("activate", self._status_icon_activate_cb)
60 self._status_icon.set_from_icon_name ("engine-default")
61 self._status_icon.set_tooltip ("iBus - Running")
62 self._status_icon.set_visible (True)
64 def set_cursor_location (self, x, y, w, h):
65 self._candidate_panel.move (x + w, y + h)
67 def update_preedit (self, text, attrs, cursor_pos, show):
68 self._candidate_panel.update_preedit (text, attrs, cursor_pos, show)
70 def show_preedit_string (self):
71 self._candidate_panel.show_preedit_string ()
73 def hide_preedit_string (self):
74 self._candidate_panel.hide_preedit_string ()
76 def update_aux_string (self, text, attrs, show):
77 self._candidate_panel.update_aux_string (text, attrs, show)
79 def show_aux_string (self):
80 self._candidate_panel.show_aux_string ()
82 def hide_aux_string (self):
83 self._candidate_panel.hide_aux_string ()
85 def update_lookup_table (self, lookup_table, show):
86 self._candidate_panel.update_lookup_table (lookup_table, show)
88 def show_candidate_window (self):
89 self._candidate_panel.show ()
91 def hide_candidate_window (self):
92 self._candidate_panel.hide ()
94 def show_language_bar (self):
95 self._language_bar.show ()
97 def hide_language_bar (self):
98 self._language_bar.hide ()
100 def register_properties (self, props):
101 self._language_bar.register_properties (props)
103 def update_property (self, prop):
104 self._language_bar.update_property (self, prop)
106 def reset (self):
107 self._candidate_panel.reset ()
108 self._language_bar.reset ()
110 def do_destroy (self):
111 gtk.main_quit ()
113 def _create_im_menu (self):
114 menu = gtk.Menu ()
115 factories = self._ibus.GetFactories ()
116 if not factories:
117 item = gtk.MenuItem (label = "no engine")
118 item.set_sensitive (False)
119 menu.add (item)
120 else:
121 for factory in factories:
122 name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory)
123 item = gtk.ImageMenuItem ("%s - %s" % (LANGUAGES.get (lang, lang), name))
124 if not icon:
125 icon = "engine-default"
126 item.set_image (gtk.image_new_from_icon_name (icon, gtk.ICON_SIZE_MENU))
127 item.connect ("activate", self._menu_item_activate_cb, factory)
128 menu.add (item)
130 menu.show_all ()
131 menu.set_take_focus (False)
132 return menu
134 def _get_im_menu_cb (self, languagebar):
135 menu = self._create_im_menu ()
136 return menu
138 def _status_icon_activate_cb (self, status_icon):
139 menu = self._create_im_menu ()
140 menu.popup (None, None,
141 gtk.status_icon_position_menu,
143 gtk.get_current_event_time (),
144 self._status_icon)
146 def _status_icon_popup_menu_cb (self, status_icon, button, active_time):
147 menu = self._create_im_menu ()
148 menu.popup (None, None,
149 gtk.status_icon_position_menu,
150 button,
151 active_time,
152 self._status_icon)
154 def _menu_item_activate_cb (self, item, factory):
155 self._ibus.SetFactory (factory)
157 gobject.type_register (Panel, "IBusPanel")
159 class PanelProxy (interface.IPanel):
160 def __init__ (self, dbusconn, object_path, _ibus):
161 interface.IPanel.__init__ (self, dbusconn, object_path)
162 self._dbusconn = dbusconn
163 self._panel = Panel (self, _ibus)
165 def SetCursorLocation (self, x, y, w, h):
166 self._panel.set_cursor_location (x, y, w, h)
168 def UpdatePreedit (self, text, attrs, cursor_pos, show):
169 attrs = ibus.attr_list_from_dbus_value (attrs)
170 self._panel.update_preedit (text, atrrs, cursor_pos, show)
172 def ShowPreeditString (self):
173 self._panel.show_preedit_string ()
175 def HidePreeditString (self):
176 self._panel.hide_preedit_string ()
178 def UpdateAuxString (self, text, attrs, show):
179 attrs = ibus.attr_list_from_dbus_value (attrs)
180 self._panel.update_aux_string (text, attrs, show)
182 def ShowAuxString (self):
183 self._panel.show_aux_string ()
185 def HideAuxString (self):
186 self._panel.hide_aux_string ()
188 def UpdateLookupTable (self, lookup_table, show):
189 lookup_table = ibus.lookup_table_from_dbus_value (lookup_table)
190 self._panel.update_lookup_table (lookup_table, show)
192 def ShowCandidateWindow (self):
193 self._panel.show_candidate_window ()
195 def HideCandidateWindow (self):
196 self._panel.hide_candidate_window ()
198 def ShowLanguageBar (self):
199 self._panel.show_language_bar ()
201 def HideLanguageBar (self):
202 self._panel.hide_language_bar ()
204 def RegisterProperties (self, props):
205 props = ibus.prop_list_from_dbus_value (props)
206 self._panel.register_properties (props)
208 def UpdateProperty (self, prop):
209 prop = ibus.property_from_dbus_value (props)
210 self._panel.update_property (prop)
212 def Reset (self):
213 self._panel.reset ()
215 def Destroy (self):
216 self._panel.destroy ()