Add {cursor, page}-{up, down} support for panel.
authorHuang Peng <shawn.p.huang@gmail.com>
Fri, 30 May 2008 05:39:49 +0000 (13:39 +0800)
committerHuang Peng <shawn.p.huang@gmail.com>
Fri, 30 May 2008 05:39:49 +0000 (13:39 +0800)
engine/demo/engine.py
ibus/interface/iengine.py
ibus/interface/ipanel.py
ibusdaemon/bus.py
ibusdaemon/client.py
ibusdaemon/engine.py
ibusdaemon/panel.py
panel/candidatepanel.py
panel/candidatewindow.py
panel/panel.py

index a1f11ac..ff7ea08 100644 (file)
@@ -18,22 +18,26 @@ class Engine (interface.IEngine):
 
        # methods for dbus rpc
        def ProcessKeyEvent (self, keyval, is_press, state):
+               try:
+                       return self._process_key_event (keyval, is_press, state)
+               except Exception, e:
+                       print e
+               return False
+       def _process_key_event (self, keyval, is_press, state):
                # ignore key release events
                if not is_press:
                        return False
 
                if self._preedit_string:
                        if keyval == keysyms.Return:
-                               self.CommitString (self._preedit_string)
-                               self._preedit_string = ""
-                               self._update ()
+                               self._commit_string (self._preedit_string)
                                return True
                        elif keyval == keysyms.BackSpace:
                                self._preedit_string = self._preedit_string[:-1]
                                self._update ()
                                return True
                        elif keyval == keysyms.space:
-                               self.CommitString (self._preedit_string)
+                               self._commit_string (self._preedit_string)
                                self._preedit_string = ""
                                self._update ()
                                return False
@@ -43,14 +47,20 @@ class Engine (interface.IEngine):
                                if index >= len (candidates):
                                        return False
                                candidate = candidates[index][0]
-                               self.CommitString (candidate + " ")
-                               self._preedit_string = ""
-                               self._update ()
+                               self._commit_string (candidate + " ")
                                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.Up:
+                               self._cursor_up ()
+                               return True
+                       elif keyval == keysyms.Down:
+                               self._cursor_down ()
+                               return True
+                       elif keyval == keysyms.Left or keyval == keysyms.Right:
+                               return True
                        elif keyval == keysyms.Page_Down or keyval == keysyms.KP_Page_Down:
                                if self._lookup_table.page_down ():
                                        self._update_lookup_table ()
@@ -63,11 +73,26 @@ class Engine (interface.IEngine):
                                return True
                else:
                        if keyval < 128 and self._preedit_string:
-                               self.CommitString (self._preedit_string)
-                               self._preedit_string = ""
-                               self._update ()
+                               self._commit_string (self._preedit_string)
+
+               return False
 
+       def _cursor_up (self):
+               if self._lookup_table.cursor_up ():
+                       self._update_lookup_table ()
+                       return True
                return False
+       
+       def _cursor_down (self):
+               if self._lookup_table.cursor_down ():
+                       self._update_lookup_table ()
+                       return True
+               return False
+
+       def _commit_string (self, text):
+               self._preedit_string = ""
+               self._update ()
+               self.CommitString (text)
 
        def _update (self):
                preedit_len = len (self._preedit_string)
@@ -97,6 +122,20 @@ class Engine (interface.IEngine):
        def Reset (self):
                print "Reset"
 
+       def PageUp (self):
+               print "PageUp"
+
+       def PageDown (self):
+               print "PageDown"
+       
+       def CursorUp (self):
+               print "CursorUp"
+               self._cursor_up ()
+       
+       def CursorDown (self):
+               self._cursor_down ()
+               print "CursorDown"
+       
        def SetEnable (self, enable):
                self._enable = enable
 
index f443f3e..04539be 100644 (file)
@@ -34,7 +34,20 @@ class IEngine (dbus.service.Object):
 
        @method ()
        def Reset (self): pass
-
+       
+       # signals for lookup table
+       @method ()
+       def PageUp (self): pass
+       
+       @method ()
+       def PageDown (self): pass
+       
+       @method ()
+       def CursorUp (self): pass
+       
+       @method ()
+       def CursorDown (self): pass
+       
        @method (in_signature = "b")
        def SetEnable (self, enable): pass
 
index 91b9da4..618c5e3 100644 (file)
@@ -65,8 +65,8 @@ class IPanel (dbus.service.Object):
        def PageDown (self): pass
 
        @signal ()
-       def CursorBack (self): pass
+       def CursorUp (self): pass
 
        @signal ()
-       def CursorForward (self): pass
+       def CursorDown (self): pass
 
index 27e6b9d..3a099fb 100644 (file)
@@ -44,6 +44,7 @@ class IBus (ibus.Object):
        def register_client (self, name, dbusconn):
                ibusconn = self._lookup_ibus_connection (dbusconn)
                client = self._client_manager.register_client (name, ibusconn)
+               client.connect ("destroy", self._client_destroy_cb)
                factory = self._factory_manager.get_default_factory ()
                if factory:
                        engine = factory.create_engine ()
@@ -151,6 +152,11 @@ class IBus (ibus.Object):
 
                self._panel.hide_candidate_window ()
 
+       def _client_destroy_cb (self, client):
+               if client == self._focused_client:
+                       del self._client_handlers[:]
+                       self._focused_client = None
+
        ##########################################################
        # methods for im engines
        ##########################################################
@@ -177,12 +183,35 @@ class IBus (ibus.Object):
                        self._panel.destroy ()
                ibusconn = self._lookup_ibus_connection (dbusconn)
                self._panel = Panel (ibusconn, object_path)
+               self._panel.connect ("page-up", self._panel_page_up_cb)
+               self._panel.connect ("page-down", self._panel_page_down_cb)
+               self._panel.connect ("cursor-up", self._panel_cursor_up_cb)
+               self._panel.connect ("cursor-down", self._panel_cursor_down_cb)
                self._panel.connect ("destroy", self._panel_destroy_cb)
 
+       def _panel_page_up_cb (self, panel):
+               assert panel == self._panel
+               if self._focused_client:
+                       self._focused_client.page_up ()
+       
+       def _panel_page_down_cb (self, panel):
+               assert panel == self._panel
+               if self._focused_client:
+                       self._focused_client.page_down ()
+
+       def _panel_cursor_up_cb (self, panel):
+               assert panel == self._panel
+               if self._focused_client:
+                       self._focused_client.cursor_up ()
+
+       def _panel_cursor_down_cb (self, panel):
+               assert panel == self._panel
+               if self._focused_client:
+                       self._focused_client.cursor_down ()
 
        def _panel_destroy_cb (self, panel):
-               if panel == self._panel:
-                       self._panel = DummyPanel ()
+               assert panel == self._panel
+               self._panel = DummyPanel ()
 
 class IBusProxy (ibus.IIBus):
        SUPPORTS_MULTIPLE_CONNECTIONS = True
index 908be9c..37371fa 100644 (file)
@@ -81,6 +81,22 @@ class Client (ibus.Object):
                if self._engine:
                        self._engine.reset ()
 
+       def page_up (self):
+               if self._engine:
+                       self._engine.page_up ()
+       
+       def page_down (self):
+               if self._engine:
+                       self._engine.page_down ()
+       
+       def cursor_up (self):
+               if self._engine:
+                       self._engine.cursor_up ()
+       
+       def cursor_down (self):
+               if self._engine:
+                       self._engine.cursor_down ()
+
        def is_enabled (self):
                return self._enable
 
index b12b28c..ad0d461 100644 (file)
@@ -96,6 +96,20 @@ class Engine (ibus.Object):
        def set_enable (self, enable):
                self._engine.SetEnable (enable)
 
+       # cursor for lookup table
+
+       def page_up (self):
+               self._engine.PageUp ()
+
+       def page_down (self):
+               self._engine.PageDown ()
+       
+       def cursor_up (self):
+               self._engine.CursorUp ()
+       
+       def cursor_down (self):
+               self._engine.CursorDown ()
+
        def destroy (self):
                ibus.Object.destroy (self)
                if self._engine:
index 8c41f7f..18a3fe1 100644 (file)
@@ -1,9 +1,27 @@
 import weakref
 import gobject
 import ibus
-from ibus import interface
 
 class Panel (ibus.Object):
+       __gsignals__ = {
+               "page-up" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+               "page-down" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+               "cursor-up" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+               "cursor-down" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+       }
+
        def __init__ (self, ibusconn, object_path):
                ibus.Object.__init__ (self)
                self._ibusconn = ibusconn
@@ -63,14 +81,14 @@ class Panel (ibus.Object):
                self.destroy ()
 
        def _dbus_signal_cb (self, ibusconn, message):
-               if message.is_signal (interface.IBUS_PANEL_IFACE, "PageUp"):
-                       pass
-               elif message.is_signal (interface.IBUS_PANEL_IFACE, "PageDown"):
-                       pass
-               elif message.is_signal (interface.IBUS_PANEL_IFACE, "CursorBack"):
-                       pass
-               elif message.is_signal (interface.IBUS_PANEL_IFACE, "CursorForward"):
-                       pass
+               if message.is_signal (ibus.IBUS_PANEL_IFACE, "PageUp"):
+                       self.emit ("page-up")
+               elif message.is_signal (ibus.IBUS_PANEL_IFACE, "PageDown"):
+                       self.emit ("page-down")
+               elif message.is_signal (ibus.IBUS_PANEL_IFACE, "CursorUp"):
+                       self.emit ("cursor-up")
+               elif message.is_signal (ibus.IBUS_PANEL_IFACE, "CursorDown"):
+                       self.emit ("cursor-down")
 
        # methods for cmp
        # def __lt__ (self, other):
@@ -85,5 +103,7 @@ class Panel (ibus.Object):
        #               if x[1] > y[1]: return True
        #               if x[1] == y[1]: return x[0] > y[0]
 
+gobject.type_register (Panel)
+
 class DummyPanel:
        pass
index 097f8a1..87ef552 100644 (file)
@@ -92,6 +92,17 @@ class CandidatePanel (gtk.VBox):
                gobject.PARAM_READWRITE)                                # flags
                }
 
+       __gsignals__ = {
+               "cursor-up" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+               "cursor-down" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+       }
+
        def __init__ (self):
                gtk.VBox.__init__ (self)
                self._tooltips = gtk.Tooltips ()
@@ -130,20 +141,18 @@ class CandidatePanel (gtk.VBox):
 
                # create candidates area
                self._candidate_area = CandidateArea (self._orientation)
-               candidates = []
-               for i in xrange (0, 7):
-                       candidates.append (("你好", pango.AttrList ()))
-               self._candidate_area.set_candidates (candidates)
 
                # create state label
                self._state_label = gtk.Label ()
 
                # create buttons
                self._prev_button = gtk.Button ()
+               self._prev_button.connect ("clicked", lambda x: self.emit ("cursor-up"))
                self._prev_button.set_relief (gtk.RELIEF_NONE)
                self._tooltips.set_tip (self._prev_button, "Previous candidate")
 
                self._next_button = gtk.Button ()
+               self._next_button.connect ("clicked", lambda x: self.emit ("cursor-down"))
                self._next_button.set_relief (gtk.RELIEF_NONE)
                self._tooltips.set_tip (self._next_button, "Next candidate")
 
@@ -228,7 +237,7 @@ class CandidatePanel (gtk.VBox):
                self._lookup_table = lookup_table
                candidates = self._lookup_table.get_canidates_in_current_page ()
                candidates = map (lambda x: (x[0], PangoAttrList (x[1])), candidates)
-               self._candidate_area.set_candidates (candidates)
+               self._candidate_area.set_candidates (candidates, self._lookup_table.get_cursor_pos_in_current_page ())
 
        def set_orientation (self, orientation):
                if self._orientation == orientation:
index f54f81c..d1fc086 100644 (file)
@@ -5,6 +5,17 @@ import ibus
 from candidatepanel import CandidatePanel
 
 class CandidateWindow (gtk.Window):
+       __gsignals__ = {
+               "cursor-up" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+               "cursor-down" : (
+                       gobject.SIGNAL_RUN_FIRST,
+                       gobject.TYPE_NONE,
+                       ()),
+       }
+
        def __init__ (self):
                gtk.Window.__init__ (self, gtk.WINDOW_POPUP)
                self.add_events (
@@ -15,6 +26,8 @@ class CandidateWindow (gtk.Window):
                self._candidate_panel = CandidatePanel ()
                self._begin_move = False
                self._candidate_panel.connect ("size-request", self._size_request_cb)
+               self._candidate_panel.connect ("cursor-up", lambda x: self.emit ("cursor-up"))
+               self._candidate_panel.connect ("cursor-down", lambda x: self.emit ("cursor-down"))
                self.add (self._candidate_panel)
                self.move (100, 100)
                self.show_all ()
index aba2ec4..5b3aecf 100644 (file)
@@ -13,6 +13,8 @@ class Panel (gobject.GObject):
                self._proxy = proxy
                self._language_bar = LanguageBarWindow ()
                self._candidate_panel = CandidateWindow ()
+               self._candidate_panel.connect ("cursor-up", lambda x: self._proxy.CursorUp ())
+               self._candidate_panel.connect ("cursor-down", lambda x: self._proxy.CursorDown ())
 
        def set_cursor_location (self, x, y, w, h):
                self._candidate_panel.move (x + w, y + h)