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
28 from ibus import keysyms
29 from ibus import modifier
30 from ibus import interface
32 class Engine (interface.IEngine):
33 _dict = enchant.Dict ()
34 def __init__ (self, dbusconn, object_path):
35 interface.IEngine.__init__ (self, dbusconn, object_path)
36 self._dbusconn = dbusconn
37 self._is_invalidate = False
38 self._preedit_string = u""
39 self._lookup_table = ibus.LookupTable ()
40 self._prop_list = ibus.PropList ()
41 self._prop_list.append (ibus.Property ("test", icon = "ibus-locale"))
43 def _process_key_event (self, keyval, is_press, state):
44 # ignore key release events
48 if self._preedit_string:
49 if keyval == keysyms.Return:
50 self._commit_string (self._preedit_string)
52 elif keyval == keysyms.Escape:
53 self._preedit_string = u""
56 elif keyval == keysyms.BackSpace:
57 self._preedit_string = self._preedit_string[:-1]
60 elif keyval == keysyms.space:
61 if self._lookup_table.get_number_of_candidates () > 0:
62 self._commit_string (self._lookup_table.get_current_candidate ()[0])
64 self._commit_string (self._preedit_string)
66 elif keyval >= keysyms._1 and keyval <= keysyms._9:
67 index = keyval - keysyms._1
68 candidates = self._lookup_table.get_canidates_in_current_page ()
69 if index >= len (candidates):
71 candidate = candidates[index][0]
72 self._commit_string (candidate)
74 elif keyval == keysyms.Page_Up or keyval == keysyms.KP_Page_Up:
75 if self._lookup_table.page_up ():
76 self._update_lookup_table ()
78 elif keyval == keysyms.Up:
81 elif keyval == keysyms.Down:
84 elif keyval == keysyms.Left or keyval == keysyms.Right:
86 elif keyval == keysyms.Page_Down or keyval == keysyms.KP_Page_Down:
87 if self._lookup_table.page_down ():
88 self._update_lookup_table ()
90 if keyval in xrange (keysyms.a, keysyms.z + 1) or \
91 keyval in xrange (keysyms.A, keysyms.Z + 1):
92 if state & (modifier.CONTROL_MASK | modifier.ALT_MASK) == 0:
93 self._preedit_string += unichr (keyval)
97 if keyval < 128 and self._preedit_string:
98 self._commit_string (self._preedit_string)
102 def _invalidate (self):
103 if self._is_invalidate:
105 self._is_invalidate = True
106 gobject.idle_add (self._update, priority = gobject.PRIORITY_LOW)
108 def _cursor_up (self):
109 if self._lookup_table.cursor_up ():
110 self._update_lookup_table ()
114 def _cursor_down (self):
115 if self._lookup_table.cursor_down ():
116 self._update_lookup_table ()
120 def _commit_string (self, text):
121 self.CommitString (text)
122 self._preedit_string = u""
126 preedit_len = len (self._preedit_string)
127 attrs = ibus.AttrList ()
128 self._lookup_table.clean ()
130 if not self._dict.check (self._preedit_string):
131 attrs.append (ibus.AttributeForeground (0xff0000, 0, preedit_len))
132 for text in self._dict.suggest (self._preedit_string):
133 self._lookup_table.append_candidate (text)
134 self.UpdateAuxString (self._preedit_string, attrs.to_dbus_value (), preedit_len > 0)
135 attrs.append (ibus.AttributeUnderline (pango.UNDERLINE_SINGLE, 0, preedit_len))
136 self.UpdatePreedit (self._preedit_string, attrs.to_dbus_value (), dbus.Int32 (preedit_len), preedit_len > 0)
137 self._update_lookup_table ()
138 self._is_invalidate = False
140 def _update_lookup_table (self):
141 show = self._lookup_table.get_number_of_candidates () > 0
142 self.UpdateLookupTable (self._lookup_table.to_dbus_value (), show)
145 # methods for dbus rpc
146 def ProcessKeyEvent (self, keyval, is_press, state):
148 return self._process_key_event (keyval, is_press, state)
154 self.RegisterProperties (self._prop_list.to_dbus_value ())
160 def SetCursorLocation (self, x, y, w, h):
175 def CursorDown (self):
178 def SetEnable (self, enable):
179 self._enable = enable
181 self.RegisterProperties (self._prop_list.to_dbus_value ())
183 def PropertyActivate (self, prop_name):
184 print "PropertyActivate (%s)" % prop_name
189 class DemoEngine (Engine):