9 from ibus
import keysyms
10 from ibus
import interface
12 class Engine (interface
.IEngine
):
13 def __init__ (self
, dbusconn
, object_path
):
14 interface
.IEngine
.__init
__ (self
, dbusconn
, object_path
)
15 self
._dbusconn
= dbusconn
17 # create anthy context
18 self
._context
= anthy
.anthy_context ()
19 self
._context
._set
_encoding
(2)
21 self
._lookup
_table
= ibus
.LookupTable ()
22 self
._prop
_list
= ibus
.PropList ()
24 # use reset to init values
27 # reset values of engine
29 self
._input
_chars
= u
""
30 self
._convert
_chars
= u
""
32 self
._need
_update
= False
33 self
._convert
_begined
= False
35 self
._lookup
_table
.clean ()
36 self
._lookup
_table
_visible
= False
39 def _begin_convert (self
):
40 if self
._convert
_begined
:
42 self
._convert
_begined
= True
44 self
._context
.set_string (self
._input
_chars
.encode ("utf-8"))
45 conv_stat
= anthy
.anthy_conv_stat ()
46 self
._context
.get_stat (conv_stat
)
48 for i
in xrange (0, conv_stat
.nr_segment
):
50 l
= self
._context
.get_segment (i
, 0, buf
, 100)
51 text
= unicode (buf
[:l
], "utf-8")
52 self
._segments
.append ((0, text
))
55 self
._fill
_lookup
_table
()
56 self
._lookup
_table
_visible
= False
58 def _fill_lookup_table (self
):
60 seg_stat
= anthy
.anthy_segment_stat ()
61 self
._context
.get_segment_stat (self
._cursor
_pos
, seg_stat
)
64 self
._lookup
_table
.clean ()
65 for i
in xrange (0, seg_stat
.nr_candidate
):
67 l
= self
._context
.get_segment (self
._cursor
_pos
, i
, buf
, 100)
68 candidate
= unicode (buf
[:l
], "utf-8")
69 self
._lookup
_table
.append_candidate (candidate
)
72 def _invalidate (self
):
75 self
._need
_update
= True
76 gobject
.idle_add (self
._update
, priority
= gobject
.PRIORITY_LOW
)
79 # only process cursor down in convert mode
80 if not self
._convert
_begined
:
83 if not self
._lookup
_table
.page_up ():
86 candidate
= self
._lookup
_table
.get_current_candidate ()[0]
87 index
= self
._lookup
_table
.get_cursor_pos ()
88 self
._segments
[self
._cursor
_pos
] = index
, candidate
92 def _page_down (self
):
93 # only process cursor down in convert mode
94 if not self
._convert
_begined
:
97 if not self
._lookup
_table
.page_down ():
100 candidate
= self
._lookup
_table
.get_current_candidate ()[0]
101 index
= self
._lookup
_table
.get_cursor_pos ()
102 self
._segments
[self
._cursor
_pos
] = index
, candidate
106 def _cursor_up (self
):
107 # only process cursor down in convert mode
108 if not self
._convert
_begined
:
111 if not self
._lookup
_table
.cursor_up ():
114 candidate
= self
._lookup
_table
.get_current_candidate ()[0]
115 index
= self
._lookup
_table
.get_cursor_pos ()
116 self
._segments
[self
._cursor
_pos
] = index
, candidate
120 def _cursor_down (self
):
121 # only process cursor down in convert mode
122 if not self
._convert
_begined
:
125 if not self
._lookup
_table
.cursor_down ():
128 candidate
= self
._lookup
_table
.get_current_candidate ()[0]
129 index
= self
._lookup
_table
.get_cursor_pos ()
130 self
._segments
[self
._cursor
_pos
] = index
, candidate
134 def _commit_string (self
, text
):
136 self
.CommitString (text
)
139 def _update_input_chars (self
):
140 begin
, end
= max (self
._cursor
_pos
- 4, 0), self
._cursor
_pos
142 for i
in range (begin
, end
):
143 text
= self
._input
_chars
[i
:end
]
144 romja
= romaji_typing_rule
.get (text
, None)
146 self
._input
_chars
= u
"".join ((self
._input
_chars
[:i
], romja
, self
._input
_chars
[end
:]))
147 self
._cursor
_pos
-= len(text
)
148 self
._cursor
_pos
+= len(romja
)
150 attrs
= ibus
.AttrList ()
151 attrs
.append (ibus
.AttributeUnderline (pango
.UNDERLINE_SINGLE
, 0, len (self
._input
_chars
.encode ("utf-8"))))
153 self
.UpdatePreedit (dbus
.String (self
._input
_chars
),
154 attrs
.to_dbus_value (),
155 dbus
.Int32 (self
._cursor
_pos
),
156 len (self
._input
_chars
) > 0)
157 self
.UpdateAuxString (u
"", ibus
.AttrList ().to_dbus_value (), False)
158 self
.UpdateLookupTable (self
._lookup
_table
.to_dbus_value (), self
._lookup
_table
_visible
)
160 def _update_convert_chars (self
):
161 self
._convert
_chars
= u
""
165 for seg_index
, text
in self
._segments
:
166 self
._convert
_chars
+= text
167 if i
<= self
._cursor
_pos
:
171 attrs
= ibus
.AttrList ()
172 attrs
.append (ibus
.AttributeUnderline (pango
.UNDERLINE_SINGLE
, 0, len (self
._convert
_chars
.encode ("utf-8"))))
174 self
.UpdatePreedit (dbus
.String (self
._convert
_chars
),
175 attrs
.to_dbus_value (),
178 aux_string
= u
"( %d / %d )" % (self
._lookup
_table
.get_cursor_pos () + 1, self
._lookup
_table
.get_number_of_candidates())
179 self
.UpdateAuxString (aux_string
, ibus
.AttrList ().to_dbus_value (), self
._lookup
_table
_visible
)
180 self
.UpdateLookupTable (self
._lookup
_table
.to_dbus_value (), self
._lookup
_table
_visible
)
183 self
._need
_update
= False
184 if self
._convert
_begined
== False:
185 self
._update
_input
_chars
()
187 self
._update
_convert
_chars
()
189 def _on_key_return (self
):
190 if not self
._input
_chars
:
192 if self
._convert
_begined
== False:
193 self
._commit
_string
(self
._input
_chars
)
196 for seg_index
, text
in self
._segments
:
197 self
._context
.commit_segment (i
, seg_index
)
198 self
._commit
_string
(self
._convert
_chars
)
201 def _on_key_escape (self
):
202 if not self
._input
_chars
:
208 def _on_key_back_space (self
):
209 if not self
._input
_chars
:
212 if self
._convert
_begined
:
213 self
._convert
_begined
= False
214 self
._cursor
_pos
= len (self
._input
_chars
)
215 self
._lookup
_table
.clean ()
216 self
._lookup
_table
_visible
= False
217 elif self
._cursor
_pos
> 0:
218 self
._input
_chars
= self
._input
_chars
[:self
._cursor
_pos
- 1] + self
._input
_chars
[self
._cursor
_pos
:]
219 self
._cursor
_pos
-= 1
224 def _on_key_delete (self
):
225 if not self
._input
_chars
:
228 if self
._convert
_begined
:
229 self
._convert
_begined
= False
230 self
._cursor
_pos
= len (self
._input
_chars
)
231 self
._lookup
_table
.clean ()
232 self
._lookup
_table
_visible
= False
233 elif self
._cursor
_pos
< len (self
._input
_chars
):
234 self
._input
_chars
= self
._input
_chars
[:self
._cursor
_pos
] + self
._input
_chars
[self
._cursor
_pos
+ 1:]
239 def _on_key_space (self
):
240 if not self
._input
_chars
:
242 if self
._convert
_begined
== False:
243 self
._begin
_convert
()
246 self
._lookup
_table
_visible
= True
250 def _on_key_up (self
):
251 if not self
._input
_chars
:
253 self
._lookup
_table
_visible
= True
257 def _on_key_down (self
):
258 if not self
._input
_chars
:
260 self
._lookup
_table
_visible
= True
264 def _on_key_page_up (self
):
265 if not self
._input
_chars
:
267 if self
._lookup
_table
_visible
== True:
271 def _on_key_page_down (self
):
272 if not self
._input
_chars
:
274 if self
._lookup
_table
_visible
== True:
278 def _on_key_left (self
):
279 if not self
._input
_chars
:
281 if self
._cursor
_pos
== 0:
283 self
._cursor
_pos
-= 1
284 self
._lookup
_table
_visible
= False
285 self
._fill
_lookup
_table
()
289 def _on_key_right (self
):
290 if not self
._input
_chars
:
293 if self
._convert
_begined
:
294 max_pos
= len (self
._segments
) - 1
296 max_pos
= len (self
._input
_chars
)
297 if self
._cursor
_pos
== max_pos
:
299 self
._cursor
_pos
+= 1
300 self
._lookup
_table
_visible
= False
301 self
._fill
_lookup
_table
()
306 def _on_key_number (self
, index
):
307 if not self
._input
_chars
:
310 if self
._convert
_begined
and self
._lookup
_table
_visible
:
311 candidates
= self
._lookup
_table
.get_canidates_in_current_page ()
312 if self
._lookup
_table
.set_cursor_pos_in_current_page (index
):
313 index
= self
._lookup
_table
.get_cursor_pos ()
314 candidate
= self
._lookup
_table
.get_current_candidate ()[0]
315 self
._segments
[self
._cursor
_pos
] = index
, candidate
316 self
._lookup
_table
_visible
= False
317 self
._on
_key
_right
()
322 def _on_key_common (self
, keyval
):
323 self
._input
_chars
+= unichr (keyval
)
324 self
._cursor
_pos
+= 1
328 def _process_key_event (self
, keyval
, is_press
, state
):
329 # ignore key release events
333 if keyval
== keysyms
.Return
:
334 return self
._on
_key
_return
()
335 elif keyval
== keysyms
.Escape
:
336 return self
._on
_key
_escape
()
337 elif keyval
== keysyms
.BackSpace
:
338 return self
._on
_key
_back
_space
()
339 elif keyval
== keysyms
.Delete
or keyval
== keysyms
.KP_Delete
:
340 return self
._on
_key
_delete
()
341 elif keyval
== keysyms
.space
:
342 return self
._on
_key
_space
()
343 elif keyval
>= keysyms
._1 and keyval
<= keysyms
._9:
344 index
= keyval
- keysyms
._1
345 return self
._on
_key
_number
(index
)
346 elif keyval
== keysyms
.Page_Up
or keyval
== keysyms
.KP_Page_Up
:
347 return self
._on
_key
_page
_up
()
348 elif keyval
== keysyms
.Page_Down
or keyval
== keysyms
.KP_Page_Down
:
349 return self
._on
_key
_page
_down
()
350 elif keyval
== keysyms
.Up
:
351 return self
._on
_key
_up
()
352 elif keyval
== keysyms
.Down
:
353 return self
._on
_key
_down
()
354 elif keyval
== keysyms
.Left
:
355 return self
._on
_key
_left
()
356 elif keyval
== keysyms
.Right
:
357 return self
._on
_key
_right
()
358 elif keyval
in xrange (keysyms
.a
, keysyms
.z
+ 1) or \
359 keyval
in xrange (keysyms
.A
, keysyms
.Z
+ 1):
360 return self
._on
_key
_common
(keyval
)
366 # methods for dbus rpc
367 def ProcessKeyEvent (self
, keyval
, is_press
, state
):
369 return self
._process
_key
_event
(keyval
, is_press
, state
)
375 self
.RegisterProperties (self
._prop
_list
.to_dbus_value ())
381 def SetCursorLocation (self
, x
, y
, w
, h
):
396 def CursorDown (self
):
399 def SetEnable (self
, enable
):
400 self
._enable
= enable
402 self
.RegisterProperties (self
._prop
_list
.to_dbus_value ())
404 def PropertyActivate (self
, prop_name
):
405 print "PropertyActivate (%s)" % prop_name