ibus almost works.
authorHuang Peng <shawn.p.huang@gmail.com>
Thu, 29 May 2008 13:09:39 +0000 (21:09 +0800)
committerHuang Peng <shawn.p.huang@gmail.com>
Thu, 29 May 2008 13:09:39 +0000 (21:09 +0800)
engine/demo/engine.py
ibus/attribute.py
ibus/gtk.py
ibus/keysyms.py
ibus/lookuptable.py
ibusdaemon/bus.py
panel/candidatepanel.py
panel/panel.py

index e7205f6..5a70307 100644 (file)
@@ -4,14 +4,17 @@ import gtk
 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):
@@ -23,28 +26,62 @@ class Engine (interface.IEngine):
                        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"
index 89e75b3..a8af1f2 100644 (file)
@@ -12,17 +12,17 @@ class Attribute:
                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]
@@ -64,7 +64,7 @@ class AttrList:
                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
@@ -72,7 +72,7 @@ class AttrList:
        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)
index b9e5fa7..21294bb 100644 (file)
@@ -21,7 +21,7 @@ class PangoAttrList (pango.AttrList):
                                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)
index e4678c2..cf6d49c 100644 (file)
@@ -22,6 +22,7 @@
 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
index 57e9221..00dd1b3 100644 (file)
@@ -3,11 +3,11 @@ from attribute import *
 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
@@ -26,7 +26,7 @@ class StringList (list):
                        attrs = attr_list_from_dbus_value (candidate[1])
                        candidates.append ((text, attrs))
 
-               self.clear ()
+               self.clean ()
                self[:] = candidates
 
 class LookupTable:
@@ -89,7 +89,7 @@ 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
@@ -108,8 +108,8 @@ class LookupTable:
                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):
@@ -129,9 +129,12 @@ class LookupTable:
        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)
@@ -140,9 +143,9 @@ class LookupTable:
                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(...))")
 
@@ -158,10 +161,10 @@ def lookup_table_from_dbus_value (value):
 
 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)
index 470e603..27e6b9d 100644 (file)
@@ -45,8 +45,9 @@ class IBus (ibus.Object):
                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)
@@ -112,8 +113,9 @@ class IBus (ibus.Object):
                        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
 
index c66c455..097f8a1 100644 (file)
@@ -52,6 +52,9 @@ class CandidateArea (gtk.HBox):
 
                        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
@@ -69,10 +72,17 @@ class CandidateArea (gtk.HBox):
 
                        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
index b0b4251..aba2ec4 100644 (file)
@@ -80,7 +80,8 @@ class PanelProxy (interface.IPanel):
                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 ()