import pango
import dbus
import ibus
+import enchant
from ibus import keysyms
from ibus import interface
class Engine (interface.IEngine):
+ _dict = enchant.Dict ()
def __init__ (self, dbusconn, object_path):
interface.IEngine.__init__ (self, dbusconn, object_path)
self._dbusconn = dbusconn
self._preedit_string = ""
+ self._lookup_table = ibus.LookupTable ()
# methods for dbus rpc
def ProcessKeyEvent (self, keyval, is_press, state):
if keyval == keysyms.Return:
self.CommitString (self._preedit_string)
self._preedit_string = ""
- self._update_preedit ()
+ self._update ()
return True
elif keyval == keysyms.BackSpace:
self._preedit_string = self._preedit_string[:-1]
- self._update_preedit ()
+ self._update ()
+ return True
+ elif keyval == keysyms.space:
+ self.CommitString (self._preedit_string)
+ self._preedit_string = ""
+ self._update ()
+ return False
+ elif keyval >= keysyms._1 and keyval <= keysyms._9:
+ index = keyval - keysyms._1
+ candidates = self._lookup_table.get_canidates_in_current_page ()
+ if index >= len (candidates):
+ return False
+ candidate = candidates[index][0]
+ self.CommitString (candidate + " ")
+ self._preedit_string = ""
+ self._update ()
+ return True
+ elif keyval == keysyms.Page_Up or keyval == keysyms.KP_Page_Up:
+ if self._lookup_table.page_up ():
+ self._update_lookup_table ()
+ return True
+ elif keyval == keysyms.Page_Down or keyval == keysyms.KP_Page_Down:
+ if self._lookup_table.page_down ():
+ self._update_lookup_table ()
+ return True
+ if (keyval >= keysyms.a and keyval <= keysyms.z) or \
+ (keyval >= keysyms.A and keyval <= keysyms.Z):
+ if state & (keysyms.CONTROL_MASK | keysyms.ALT_MASK) == 0:
+ self._preedit_string += chr (keyval)
+ self._update ()
return True
- # ignore hotkeys or key > 128
- if keyval >= 128 or (state & (keysyms.CONTROL_MASK | keysyms.MOD1_MASK)) != 0:
- return False
-
- self._preedit_string += chr (keyval)
- self._update_preedit ()
-
- return True
+ return False
- def _update_preedit (self):
+ def _update (self):
preedit_len = len (self._preedit_string)
attrs = ibus.AttrList ()
+ self._lookup_table.clean ()
+ if preedit_len > 0:
+ if not self._dict.check (self._preedit_string):
+ attrs.append (ibus.AttributeForeground (0xff0000, 0, preedit_len))
+ for text in self._dict.suggest (self._preedit_string):
+ self._lookup_table.append_candidate (text)
+
attrs.append (ibus.AttributeUnderline (pango.UNDERLINE_SINGLE, 0, preedit_len))
self.PreeditChanged (self._preedit_string, attrs.to_dbus_value (), dbus.Int32 (preedit_len))
self.AuxStringChanged (self._preedit_string, attrs.to_dbus_value ())
+ self._update_lookup_table ()
+
+ def _update_lookup_table (self):
+ self.UpdateLookupTable (self._lookup_table.to_dbus_value ())
+
def FocusIn (self):
print "FocusIn"
self._end_index = end_index
def to_dbus_value (self):
- values = [dbus.Int32 (self._type),
- dbus.Int32 (self._value),
- dbus.Int32 (self._start_index),
- dbus.Int32 (self._end_index)]
- return dbus.Array (values)
+ values = [dbus.UInt32 (self._type),
+ dbus.UInt32 (self._value),
+ dbus.UInt32 (self._start_index),
+ dbus.UInt32 (self._end_index)]
+ return dbus.Array (values, signature="u")
def from_dbus_value (self, value):
if not isinstance (value, dbus.Array):
raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
- if len (value) != 4 or not all (map (lambda x: isinstance (x, dbus.Int32), value)):
+ if len (value) != 4 or not all (map (lambda x: isinstance (x, dbus.UInt32), value)):
raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
self._type = value[0]
self._attrs.append (attr)
def to_dbus_value (self):
- array = dbus.Array ()
+ array = dbus.Array (signature = "v")
for attr in self._attrs:
array.append (attr.to_dbus_value ())
return array
def from_dbus_value (self, value):
attrs = []
if not isinstance (value, dbus.Array):
- raise IBusException ("AttrList must from dbus.Array (iiii)")
+ raise IBusException ("AttrList must from dbus.Array (uuuu)")
for v in value:
attr = attribute_from_dbus_value (v)
pango_attr = pango.AttrBackground (r, g, b,
attr._start_index, attr._end_index)
elif attr._type == ibus.ATTR_TYPE_UNDERLINE:
- pango_attr = pango.AttrUnderline (attr._value,
+ pango_attr = pango.AttrUnderline (int (attr._value),
attr._start_index, attr._end_index)
if pango_attr != None:
self.insert (pango_attr)
SHIFT_MASK = 1 << 0
LOCK_MASK = 1 << 1
CONTROL_MASK = 1 << 2
+ALT_MASK = 1 << 3
MOD1_MASK = 1 << 3
MOD2_MASK = 1 << 4
MOD3_MASK = 1 << 5
from exception import *
class StringList (list):
- def clear (self):
+ def clean (self):
del self[:]
def to_dbus_value (self):
- value = dbus.Array ()
+ value = dbus.Array ([], signature="v")
for text, attrs in self:
value.append (dbus.Struct ((dbus.String (text), attrs.to_dbus_value ())))
return value
attrs = attr_list_from_dbus_value (candidate[1])
candidates.append ((text, attrs))
- self.clear ()
+ self.clean ()
self[:] = candidates
class LookupTable:
pos = self._cursor_pos + self._page_size
if pos >= nr_candidates:
- pos = nr_canidates - 1
+ pos = nr_candidates - 1
self._cursor_pos = pos
return True
self._cursor_pos += 1
return True
- def clear (self):
- self._candidates.clear ()
+ def clean (self):
+ self._candidates.clean ()
self._cursor_pos = 0
def append_candidate (self, candidate, attrs = None):
def get_current_candidate (self):
return self._candidates [self._cursor_pos]
+ def get_number_of_candidates (self):
+ return len (self._candidates)
+
def to_dbus_value (self):
- value = (dbus.UInt32 (self._page_size),
- dbus.UInt32 (self._cursor_pos),
+ value = (dbus.Int32 (self._page_size),
+ dbus.Int32 (self._cursor_pos),
dbus.Boolean (self._cursor_visible),
self._candidates.to_dbus_value ())
return dbus.Struct (value)
if not isinstance (value, dbus.Struct):
raise dbus.Exception ("LookupTable must from dbus.Struct (uuba(...))")
- if len (value) != 5 or \
- not isinstance (value[0], dbus.UInt32) or \
- not isinstance (value[1], dbus.UInt32) or \
+ if len (value) != 4 or \
+ not isinstance (value[0], dbus.Int32) or \
+ not isinstance (value[1], dbus.Int32) or \
not isinstance (value[2], dbus.Boolean):
raise dbus.Exception ("LookupTable must from dbus.Struct (uuba(...))")
def unit_test ():
t = LookupTable ()
- attrs = AttrList ()
- attrs.append (AttributeBackground (RGB (233, 0,1), 0, 3))
- attrs.append (AttributeDecoration (ATTR_DECORATION_HIGHLIGHT, 3, 5))
- t.append_candidate ("Hello", attrs)
+ # attrs = AttrList ()
+ # attrs.append (AttributeBackground (RGB (233, 0,1), 0, 3))
+ # attrs.append (AttributeUnderline (1, 3, 5))
+ t.append_candidate ("Hello")
value = t.to_dbus_value ()
print value
t = lookup_table_from_dbus_value (value)
ibusconn = self._lookup_ibus_connection (dbusconn)
client = self._client_manager.register_client (name, ibusconn)
factory = self._factory_manager.get_default_factory ()
- engine = factory.create_engine ()
- client.set_engine (engine)
+ if factory:
+ engine = factory.create_engine ()
+ client.set_engine (engine)
def focus_in (self, dbusconn):
client = self._lookup_client (dbusconn)
client.set_enable (enable)
if client.get_engine () == None and enable:
factory = self._factory_manager.get_default_factory ()
- engine = factory.create_engine ()
- client.set_engine (engine)
+ if factory:
+ engine = factory.create_engine ()
+ client.set_engine (engine)
return True
return False
self._labels.append ((label1, label2))
+ self._labels[0][0].show ()
+ self._labels[0][1].show ()
+
def set_candidates (self, candidates, focus_candidate = 0):
assert len (candidates) <= len (self._labels)
i = 0
i += 1
- for label1, label2 in self._labels[len(candidates):]:
+ for label1, label2 in self._labels[max (1, len(candidates)):]:
label1.hide ()
label2.hide ()
+ if len (candidates) == 0:
+ self._labels[0][0].set_text ("")
+ self._labels[0][1].set_text ("")
+ else:
+ self._labels[0][0].set_text ("1.")
+
+
class CandidatePanel (gtk.VBox):
__gproperties__ = {
'orientation' : (gtk.Orientation, # type
self._panel.hide_aux_string ()
def UpdateLookupTable (self, lookup_table):
- self._panel.update_lookup_table ()
+ lookup_table = ibus.lookup_table_from_dbus_value (lookup_table)
+ self._panel.update_lookup_table (lookup_table)
def ShowCandidateWindow (self):
self._panel.show_candidate_window ()