Fix a typo.
[ibus.git] / daemon / inputcontext.py
blob3a4c339d996c39fb856e703a30fbbed937bf8045
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 gobject
23 import ibus
25 class InputContext (ibus.Object):
26 id = 1
27 __gsignals__ = {
28 "update-preedit" : (
29 gobject.SIGNAL_RUN_FIRST,
30 gobject.TYPE_NONE,
31 (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT, gobject.TYPE_INT, gobject.TYPE_BOOLEAN)),
32 "update-aux-string" : (
33 gobject.SIGNAL_RUN_FIRST,
34 gobject.TYPE_NONE,
35 (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT, gobject.TYPE_BOOLEAN)),
36 "update-lookup-table" : (
37 gobject.SIGNAL_RUN_FIRST,
38 gobject.TYPE_NONE,
39 (gobject.TYPE_PYOBJECT, gobject.TYPE_BOOLEAN)),
40 "register-properties" : (
41 gobject.SIGNAL_RUN_FIRST,
42 gobject.TYPE_NONE,
43 (gobject.TYPE_PYOBJECT, )),
44 "update-property" : (
45 gobject.SIGNAL_RUN_FIRST,
46 gobject.TYPE_NONE,
47 (gobject.TYPE_PYOBJECT, )),
48 "engine-lost" : (
49 gobject.SIGNAL_RUN_FIRST,
50 gobject.TYPE_NONE,
51 ()),
54 def __init__ (self, name, ibusconn):
55 ibus.Object.__init__ (self)
56 self._id = "%d" % InputContext.id
57 InputContext.id += 1
58 self._ibusconn = ibusconn
59 self._ibusconn.connect ("destroy", self._ibusconn_destroy_cb)
61 # init default values
62 self._enable = False
63 self._engine = None
64 self._engine_handlers = []
66 # client state
67 self._aux_string = None
68 self._aux_attrs = None
70 self._use_preedit = True
71 self._preedit_string = None
72 self._preedit_attrs = None
73 self._cursor_pos = 0
75 self._lookup_table = None
76 self._show_lookup_table = False
78 def get_id (self):
79 return self._id;
81 def get_preedit_string (self):
82 return self._preedit_string, self._preedit_attrs, self._cursor_pos
84 def get_use_preedit (self):
85 return self._use_preedit
87 def get_aux_string (self):
88 return self._aux_string, self._aux_attrs
90 def process_key_event (self, keyval, is_press, state,
91 reply_cb, error_cb):
92 if self._engine != None and self._enable:
93 self._engine.process_key_event (keyval, is_press, state,
94 reply_cb, error_cb)
95 else:
96 reply_cb (False)
98 def set_cursor_location (self, x, y, w, h):
99 if self._engine:
100 self._engine.set_cursor_location (x, y, w, h)
102 def focus_in (self):
103 if self._engine:
104 self._engine.focus_in ()
106 def focus_out (self):
107 if self._engine:
108 self._engine.focus_out ()
110 def reset (self):
111 if self._engine:
112 self._engine.reset ()
114 def page_up (self):
115 if self._engine:
116 self._engine.page_up ()
118 def page_down (self):
119 if self._engine:
120 self._engine.page_down ()
122 def cursor_up (self):
123 if self._engine:
124 self._engine.cursor_up ()
126 def cursor_down (self):
127 if self._engine:
128 self._engine.cursor_down ()
130 def property_activate (self, prop_name, prop_state):
131 if self._engine:
132 self._engine.property_activate (prop_name, prop_state)
134 def property_show (self, prop_name):
135 if self._engine:
136 self._engine.property_show (prop_name)
138 def property_hide (self, prop_name):
139 if self._engine:
140 self._engine.property_hide (prop_name)
142 def is_enabled (self):
143 return self._enable
145 def set_enable (self, enable):
146 if self._enable != enable:
147 self._enable = enable
148 if self._enable:
149 self._ibusconn.emit_dbus_signal ("Enabled", self._id)
150 else:
151 self._ibusconn.emit_dbus_signal ("Disabled", self._id)
152 if self._engine:
153 self._engine.set_enable (self._enable)
155 def commit_string (self, text):
156 self._ibusconn.emit_dbus_signal ("CommitString", self._id, text)
158 def update_preedit (self, text, attrs, cursor_pos, visible):
159 if self._use_preedit:
160 self._ibusconn.emit_dbus_signal ("UpdatePreedit", self._id, text, attrs, cursor_pos, visible)
161 else:
162 # show preedit on panel
163 self.emit ("update-preedit", text, attrs, cursor_pos, visible)
165 def set_engine (self, engine):
166 if self._engine == engine:
167 return
169 if self._engine != None:
170 self._remove_engine_handlers ()
171 self._engine.destroy ()
172 self._engine = None
174 self._engine = engine
175 self._install_engine_handlers ()
177 def get_engine (self):
178 return self._engine
180 def get_factory (self):
181 if self._engine:
182 return self._engine.get_factory ()
183 return None
185 def _engine_destroy_cb (self, engine):
186 if self._engine == engine:
187 self._remove_engine_handlers ()
188 self._engine = None
189 self._enable = False
190 if self._use_preedit:
191 self._ibusconn.emit_dbus_signal ("UpdatePreedit",
192 self._id,
193 u"",
194 ibus.AttrList ().to_dbus_value (),
196 False)
197 self._ibusconn.emit_dbus_signal ("Disabled", self._id)
198 self.emit ("engine-lost")
200 def _ibusconn_destroy_cb (self, ibusconn):
201 if self._engine != None:
202 self._remove_engine_handlers ()
203 self._engine.destroy ()
204 self._engine = None
205 self.destroy ()
207 def _commit_string_cb (self, engine, text):
208 self.commit_string (text)
210 def _update_preedit_cb (self, engine, text, attrs, cursor_pos, visible):
211 self.update_preedit (text, attrs, cursor_pos, visible)
213 def _update_aux_string_cb (self, engine, text, attrs, visible):
214 self._aux_string = text
215 self._aux_attrs = attrs
216 self.emit ("update-aux-string", text, attrs, visible)
218 def _update_lookup_table_cb (self, engine, lookup_table, visible):
219 self._lookup_table = lookup_table
220 self.emit ("update-lookup-table", lookup_table, visible)
222 def _register_properties_cb (self, engine, props):
223 self.emit ("register-properties", props)
225 def _update_property_cb (self, engine, prop):
226 self.emit ("update-property", prop)
228 def _remove_engine_handlers (self):
229 assert self._engine != None
231 map (self._engine.disconnect, self._engine_handlers)
232 del self._engine_handlers[:]
234 def _install_engine_handlers (self):
235 signals = (
236 ("destroy", self._engine_destroy_cb),
237 ("commit-string", self._commit_string_cb),
238 ("update-preedit", self._update_preedit_cb),
239 ("update-aux-string", self._update_aux_string_cb),
240 ("update-lookup-table", self._update_lookup_table_cb),
241 ("register-properties", self._register_properties_cb),
242 ("update-property", self._update_property_cb)
245 for signal, handler in signals:
246 id = self._engine.connect (signal, handler)
247 self._engine_handlers.append (id)
249 gobject.type_register (InputContext)