Return empty if factory is "".
[platform/upstream/ibus.git] / engine / enchant / engine.py
1 # vim:set noet ts=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
6 #
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.
11 #
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.
16 #
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
21
22 import gobject
23 import gtk
24 import pango
25 import dbus
26 import ibus
27 import enchant
28 from ibus import keysyms
29 from ibus import modifier
30 from ibus import interface
31
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"))
42
43         def _process_key_event (self, keyval, is_press, state):
44                 # ignore key release events
45                 if not is_press:
46                         return False
47
48                 if self._preedit_string:
49                         if keyval == keysyms.Return:
50                                 self._commit_string (self._preedit_string)
51                                 return True
52                         elif keyval == keysyms.Escape:
53                                 self._preedit_string = u""
54                                 self._update ()
55                                 return True
56                         elif keyval == keysyms.BackSpace:
57                                 self._preedit_string = self._preedit_string[:-1]
58                                 self._invalidate ()
59                                 return True
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])
63                                 else:
64                                         self._commit_string (self._preedit_string)
65                                 return False
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):
70                                         return False
71                                 candidate = candidates[index][0]
72                                 self._commit_string (candidate)
73                                 return True
74                         elif keyval == keysyms.Page_Up or keyval == keysyms.KP_Page_Up:
75                                 if self._lookup_table.page_up ():
76                                         self._update_lookup_table ()
77                                 return True
78                         elif keyval == keysyms.Up:
79                                 self._cursor_up ()
80                                 return True
81                         elif keyval == keysyms.Down:
82                                 self._cursor_down ()
83                                 return True
84                         elif keyval == keysyms.Left or keyval == keysyms.Right:
85                                 return True
86                         elif keyval == keysyms.Page_Down or keyval == keysyms.KP_Page_Down:
87                                 if self._lookup_table.page_down ():
88                                         self._update_lookup_table ()
89                                 return True
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)
94                                 self._invalidate ()
95                                 return True
96                 else:
97                         if keyval < 128 and self._preedit_string:
98                                 self._commit_string (self._preedit_string)
99
100                 return False
101
102         def _invalidate (self):
103                 if self._is_invalidate:
104                         return
105                 self._is_invalidate = True
106                 gobject.idle_add (self._update, priority = gobject.PRIORITY_LOW)
107
108         def _cursor_up (self):
109                 if self._lookup_table.cursor_up ():
110                         self._update_lookup_table ()
111                         return True
112                 return False
113
114         def _cursor_down (self):
115                 if self._lookup_table.cursor_down ():
116                         self._update_lookup_table ()
117                         return True
118                 return False
119
120         def _commit_string (self, text):
121                 self.CommitString (text)
122                 self._preedit_string = u""
123                 self._update ()
124
125         def _update (self):
126                 preedit_len = len (self._preedit_string)
127                 attrs = ibus.AttrList ()
128                 self._lookup_table.clean ()
129                 if preedit_len > 0:
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
139
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)
143
144
145         # methods for dbus rpc
146         def ProcessKeyEvent (self, keyval, is_press, state):
147                 try:
148                         return self._process_key_event (keyval, is_press, state)
149                 except Exception, e:
150                         print e
151                 return False
152
153         def FocusIn (self):
154                 self.RegisterProperties (self._prop_list.to_dbus_value ())
155                 print "FocusIn"
156
157         def FocusOut (self):
158                 print "FocusOut"
159
160         def SetCursorLocation (self, x, y, w, h):
161                 pass
162
163         def Reset (self):
164                 print "Reset"
165
166         def PageUp (self):
167                 print "PageUp"
168
169         def PageDown (self):
170                 print "PageDown"
171
172         def CursorUp (self):
173                 self._cursor_up ()
174
175         def CursorDown (self):
176                 self._cursor_down ()
177
178         def SetEnable (self, enable):
179                 self._enable = enable
180                 if self._enable:
181                         self.RegisterProperties (self._prop_list.to_dbus_value ())
182
183         def PropertyActivate (self, prop_name):
184                 print "PropertyActivate (%s)" % prop_name
185
186         def Destroy (self):
187                 print "Destroy"
188
189 class DemoEngine (Engine):
190         pass
191