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
25 class InputContext (ibus
.Object
):
29 gobject
.SIGNAL_RUN_FIRST
,
31 (gobject
.TYPE_PYOBJECT
, gobject
.TYPE_PYOBJECT
, gobject
.TYPE_INT
, gobject
.TYPE_BOOLEAN
)),
32 "update-aux-string" : (
33 gobject
.SIGNAL_RUN_FIRST
,
35 (gobject
.TYPE_PYOBJECT
, gobject
.TYPE_PYOBJECT
, gobject
.TYPE_BOOLEAN
)),
36 "update-lookup-table" : (
37 gobject
.SIGNAL_RUN_FIRST
,
39 (gobject
.TYPE_PYOBJECT
, gobject
.TYPE_BOOLEAN
)),
40 "register-properties" : (
41 gobject
.SIGNAL_RUN_FIRST
,
43 (gobject
.TYPE_PYOBJECT
, )),
45 gobject
.SIGNAL_RUN_FIRST
,
47 (gobject
.TYPE_PYOBJECT
, )),
49 gobject
.SIGNAL_RUN_FIRST
,
54 def __init__ (self
, name
, ibusconn
):
55 ibus
.Object
.__init
__ (self
)
56 self
._id
= "%d" % InputContext
.id
58 self
._ibusconn
= ibusconn
59 self
._ibusconn
.connect ("destroy", self
._ibusconn
_destroy
_cb
)
64 self
._engine
_handlers
= []
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
75 self
._lookup
_table
= None
76 self
._show
_lookup
_table
= False
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
,
92 if self
._engine
!= None and self
._enable
:
93 self
._engine
.process_key_event (keyval
, is_press
, state
,
98 def set_cursor_location (self
, x
, y
, w
, h
):
100 self
._engine
.set_cursor_location (x
, y
, w
, h
)
104 self
._engine
.focus_in ()
106 def focus_out (self
):
108 self
._engine
.focus_out ()
112 self
._engine
.reset ()
116 self
._engine
.page_up ()
118 def page_down (self
):
120 self
._engine
.page_down ()
122 def cursor_up (self
):
124 self
._engine
.cursor_up ()
126 def cursor_down (self
):
128 self
._engine
.cursor_down ()
130 def property_activate (self
, prop_name
, prop_state
):
132 self
._engine
.property_activate (prop_name
, prop_state
)
134 def property_show (self
, prop_name
):
136 self
._engine
.property_show (prop_name
)
138 def property_hide (self
, prop_name
):
140 self
._engine
.property_hide (prop_name
)
142 def is_enabled (self
):
145 def set_enable (self
, enable
):
146 if self
._enable
!= enable
:
147 self
._enable
= enable
149 self
._ibusconn
.emit_dbus_signal ("Enabled", self
._id
)
151 self
._ibusconn
.emit_dbus_signal ("Disabled", self
._id
)
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
)
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
:
169 if self
._engine
!= None:
170 self
._remove
_engine
_handlers
()
171 self
._engine
.destroy ()
174 self
._engine
= engine
175 self
._install
_engine
_handlers
()
177 def get_engine (self
):
180 def get_factory (self
):
182 return self
._engine
.get_factory ()
185 def _engine_destroy_cb (self
, engine
):
186 if self
._engine
== engine
:
187 self
._remove
_engine
_handlers
()
190 if self
._use
_preedit
:
191 self
._ibusconn
.emit_dbus_signal ("UpdatePreedit",
194 ibus
.AttrList ().to_dbus_value (),
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 ()
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
):
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
)