This commit is contained in:
Huang Peng 2008-05-29 11:08:46 +08:00
Родитель 221a099a79
Коммит d92da3d2a8
6 изменённых файлов: 190 добавлений и 56 удалений

Просмотреть файл

@ -15,9 +15,10 @@ class IBus (ibus.Object):
self._client_manager = ClientManager ()
self._factory_manager = FactoryManager ()
self._panel = DummyPanel ()
self._focused_client = None
self._last_focused_client = None
self._client_handlers = []
self._last_key = None
@ -44,14 +45,30 @@ 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 ()
client.set_engine_factory (factory)
engine = factory.create_engine ()
client.set_engine (engine)
def focus_in (self, dbusconn):
client = self._lookup_client (dbusconn)
if self._focused_client != client and self._focused_client != None:
for id in self._client_handlers:
client.disconnect (id)
del self._client_handlers[:]
self._focused_client.focus_out ()
# Install all callback functions
id = client.connect ("preedit-changed", self._preedit_changed_cb)
self._client_handlers.append (id)
id = client.connect ("aux-string-changed", self._aux_string_changed_cb)
self._client_handlers.append (id)
id = client.connect ("update-lookup-table", self._update_lookup_table_cb)
self._client_handlers.append (id)
id = client.connect ("show-lookup-table", self._show_lookup_table_cb)
self._client_handlers.append (id)
id = client.connect ("hide-lookup-table", self._hide_lookup_table_cb)
self._client_handlers.append (id)
self._focused_client = client
self._last_focused_client = client
client.focus_in ()
@ -59,9 +76,12 @@ class IBus (ibus.Object):
def focus_out (self, dbusconn):
client = self._lookup_client (dbusconn)
if client == self._focused_client:
for id in self._client_handlers:
client.disconnect (id)
del self._client_handlers[:]
self._focused_client = None
client.focus_out ()
def reset (self, dbusconn):
client = self._lookup_client (dbusconn)
client.reset ()
@ -79,19 +99,20 @@ class IBus (ibus.Object):
return
else:
client.process_key_event (keyval, is_press, state, reply_cb, error_cb)
def set_cursor_location (self, x, y, w, h, dbusconn):
client = self._lookup_client (dbusconn)
client.set_cursor_location (x, y, w, h)
def _filter_hotkeys (self, client, keyval, is_press, state):
if is_press and keyval == keysyms.space \
and state == keysyms.CONTROL_MASK:
enable = not client.is_enabled ()
client.set_enable (enable)
if client.get_engine_factory () == None and enable:
factory = self._factory_manager.get_default_factory()
client.set_engine_factory (factory)
if client.get_engine () == None and enable:
factory = self._factory_manager.get_default_factory ()
engine = factory.create_engine ()
client.set_engine (engine)
return True
return False
@ -102,6 +123,31 @@ class IBus (ibus.Object):
raise ibus.IBusException ("not register the client")
return self._clients[dbusconn]
def _preedit_changed_cb (self, client, text, attrs, cursor_pos):
assert self._focused_client == client
self._panel.set_preedit_string (text, attrs, cursor_pos)
def _aux_string_changed_cb (self, client, text, attrs):
assert self._focused_client == client
self._pabel.set_aux_string (text, attrs)
def _update_lookup_table_cb (self, client, lookup_table):
assert self._focused_client == client
self._pabel.update_lookup_table (lookup_table)
def _show_lookup_table_cb (self, client, lookup_table):
assert self._focused_client == client
self._pabel.show_candidate_window ()
def _hide_lookup_table_cb (self, client, lookup_table):
assert self._focused_client == client
self._pabel.hide_candidate_window ()
##########################################################
# methods for im engines
##########################################################
@ -116,7 +162,7 @@ class IBus (ibus.Object):
def _lookup_engine (self, dbusconn, path):
ibusconn = self._lookup_ibus_connection (dbusconn)
return self._factory_manager.lookup_engine (ibusconn, path)
##########################################################
# methods for panel
@ -141,7 +187,7 @@ class IBusProxy (ibus.IIBus):
def __init__ (self):
ibus.IIBus.__init__ (self)
self._ibus = IBus ()
def new_connection (self, dbusconn):
self._ibus.new_connection (dbusconn)
@ -156,16 +202,16 @@ class IBusProxy (ibus.IIBus):
def RegisterClient (self, client_name, dbusconn):
self._ibus.register_client (client_name, dbusconn)
def RegisterFactories (self, object_paths, dbusconn):
self._ibus.register_factories (object_paths, dbusconn)
def UnregisterEngines (self, object_paths, dbusconn):
self._ibus.unregister_engines (object_paths, dbusconn)
def RegisterPanel (self, object_path, replace, dbusconn):
self._ibus.register_panel (object_path, replace, dbusconn)
def ProcessKeyEvent (self, keyval, is_press, state, \
dbusconn, reply_cb, error_cb):
try:
@ -176,7 +222,7 @@ class IBusProxy (ibus.IIBus):
def SetCursorLocation (self, x, y, w, h, dbusconn):
self._ibus.set_cursor_location (x, y, w, h, dbusconn)
def FocusIn (self, dbusconn):
self._ibus.focus_in (dbusconn)
@ -188,4 +234,4 @@ class IBusProxy (ibus.IIBus):
def IsEnabled (self, dbusconn):
return self._ibus.is_enabled (dbusconn)

Просмотреть файл

@ -1,18 +1,62 @@
import gobject
import ibus
class Client (ibus.Object):
__gsignals__ = {
"preedit-changed" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT, gobject.TYPE_UINT)),
"aux-string-changed" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
"update-lookup-table" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
(gobject.TYPE_PYOBJECT, )),
"show-lookup-table" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
()),
"hide-lookup-table" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
())
}
def __init__ (self, name, ibusconn):
ibus.Object.__init__ (self)
self._ibusconn = ibusconn
self._ibusconn.connect ("destroy", self._ibusconn_destroy_cb)
# init default values
self._enable = False
self._factory = None
self._engine = None
self._engine_handler_ids = []
# client state
self._aux_string = None
self._aux_attrs = None
self._use_preedit = True
self._preedit_string = None
self._preedit_attrs = None
self._cursor_pos = 0
self._lookup_table = None
self._show_lookup_table = False
def get_preedit_string (self):
return self._preedit_string, self._preedit_attrs, self._cursor_pos
def get_use_preedit (self):
return self._use_preedit
def get_aux_string (self):
return self._aux_string, self._aux_attrs
def process_key_event (self, keyval, is_press, state,
reply_cb, error_cb):
if self._engine != None and self._enable:
@ -53,35 +97,34 @@ class Client (ibus.Object):
def commit_string (self, text):
self._ibusconn.emit_dbus_signal ("CommitString", text)
def preedit_changed (self, text, attrs, cursor):
self._ibusconn.emit_dbus_signal ("PreeditChanged", text, attrs.get_array (), cursor)
def preedit_changed (self, text, attrs, cursor_pos):
if self._use_preedit:
self._ibusconn.emit_dbus_signal ("PreeditChanged", text, attrs, cursor_pos)
else:
# show preedit on panel
self.emit ("preedit-changed", text, attrs, cursor_pos)
def set_engine_factory (self, factory):
if self._factory == factory:
def set_engine (self, engine):
if self._engine == engine:
return
if self._engine != None:
self._remove_engine_handlers ()
self._engine.destroy ()
self._engine = None
self._factory = factory
self._engine = engine
self._install_engine_handlers ()
if self._factory:
self._engine = self._factory.create_engine ()
self._install_engine_handlers ()
def get_engine_factory (self):
return self._factory
def get_engine (self):
return self._engine
def _engine_destroy_cb (self, engine):
if self._engine == engine:
self._remove_engine_handlers ()
self._engine = None
self._factory = None
def _ibusconn_destroy_cb (self, ibusconn):
self._factory = None
if self._engine != None:
self._remove_engine_handlers ()
self._engine.destroy ()
@ -90,20 +133,25 @@ class Client (ibus.Object):
def _commit_string_cb (self, engine, text):
self.commit_string (text)
def _preedit_changed_cb (self, engine, text, attrs, cursor):
self.preedit_changed (self, text, attrs, cursor)
def _preedit_changed_cb (self, engine, text, attrs, cursor_pos):
self.preedit_changed (self, text, attrs, cursor_pos)
def _aux_string_changed_cb (self, engine, text, attrs):
pass
self._aux_string = text
self._aux_attrs = attrs
self.emit ("aux-string-changed", text, attrs)
def _update_lookup_table (self, engine, lookup_table):
pass
def _show_lookup_table (self, engine):
pass
def _hide_lookup_table (self, engine):
pass
def _update_lookup_table_cb (self, engine, lookup_table):
self._lookup_table = lookup_table
self.emit ("update-lookup-table", lookup_table)
def _show_lookup_table_cb (self, engine):
self._show_lookup_table = True
self.emit ("show-lookup-table")
def _hide_lookup_table_cb (self, engine):
self._show_lookup_table = False
self.emit ("hide-lookup-table")
def _remove_engine_handlers (self):
assert self._engine != None
@ -126,4 +174,4 @@ class Client (ibus.Object):
self._engine_handler_ids.append (id)
id = self._engine.connect ("hide-lookup-table", self._hide_lookup_table_cb)
self._engine_handler_ids.append (id)
gobject.type_register (Client)

Просмотреть файл

@ -11,7 +11,7 @@ class Engine (ibus.Object):
"forward-key-event" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
(gobject.TYPE_UINT, gobject.TYPE_UINT, gobject.TYPE_UINT )),
(gobject.TYPE_UINT, gobject.TYPE_BOOLEAN, gobject.TYPE_UINT )),
"preedit-changed" : (
gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
@ -34,14 +34,18 @@ class Engine (ibus.Object):
())
}
def __init__ (self, ibusconn, object_path):
def __init__ (self, factory, ibusconn, object_path):
ibus.Object.__init__ (self)
self._factory = factory
self._ibusconn = ibusconn
self._object_path = object_path
self._engine = ibusconn.get_object (self._object_path)
self._lookup_table = ibus.LookupTable ()
self._ibusconn.connect ("destroy", self._ibusconn_destroy_cb)
def get_factory (self):
return self._factory
def handle_dbus_signal (self, message):
if message.is_signal (ibus.IBUS_ENGINE_IFACE, "CommitString"):
args = message.get_args_list ()
@ -49,14 +53,12 @@ class Engine (ibus.Object):
return True
elif message.is_signal (ibus.IBUS_ENGINE_IFACE, "ForwardKeyEvent"):
args = message.get_args_list ()
self.emit ("forward-key-event", args[0], arg[1], arg[2])
self.emit ("forward-key-event", args[0], bool (arg[1]), arg[2])
return True
elif message.is_signal (ibus.IBUS_ENGINE_IFACE, "PreeditChanged"):
attrs = ibus.attr_list_from_dbus_value (args[1])
self.emit ("preedit-changed", args[0], args[1], args[2])
return True
elif message.is_signal (ibus.IBUS_ENGINE_IFACE, "AuxStringChanged"):
attrs = ibus.attr_list_from_dbus_value (args[1])
self.emit ("aux-string-changed", args[0], args[1])
return True
elif message.is_signal (ibus.IBUS_ENGINE_IFACE, "UpdateLookupTable"):

Просмотреть файл

@ -27,7 +27,7 @@ class EngineFactory (ibus.Object):
def create_engine (self):
object_path = self._factory.CreateEngine ()
engine = Engine (self._ibusconn, object_path)
engine = Engine (self, self._ibusconn, object_path)
self._engines[object_path] = engine
return engine

Просмотреть файл

@ -1,6 +1,7 @@
import weakref
import gobject
import ibus
from ibus import interface
class Panel (ibus.Object):
def __init__ (self, ibusconn, object_path):
@ -8,13 +9,42 @@ class Panel (ibus.Object):
self._ibusconn = ibusconn
self._object_path = object_path
self._panel = self._ibusconn.get_object (self._object_path)
self._lookup_table = ibus.LookupTable ()
self._ibusconn.connect ("destroy", self._ibusconn_destroy_cb)
self._ibusconn.connect ("dbus-signal", self._dbus_signal_cb)
def set_preedit_string (self, text, attrs, cursor_pos):
self._panel.SetPreeditString (text, attrs, cursor_pos)
def show_preedit_string (self):
self._panel.ShowPreeditString ()
def hide_preedit_string (self):
slef._panel.HidePreeditString ()
def set_aux_string (self, text, attrs):
self._panel.SetAuxString (text, attrs)
def show_aux_string (self):
self._panel.ShowAuxString ()
def hide_aux_string (self):
slef._panel.HideAuxString ()
def update_lookup_table (self, lookup_table):
self._lookup_table = lookup_table
self._panel.UpdateLookupTable (lookup_table)
def show_candidate_window (self):
self._panel.ShowCandidateWindow ()
def hide_candidate_window (self):
self._panel.HideCandidateWindow ()
def show_language_bar (self):
self._panel.ShowLanguageBar ()
def hide_language_bar (self):
self._panel.HideLanguageBar ()
def destroy (self):
if self._ibusconn != None:
@ -30,7 +60,14 @@ class Panel (ibus.Object):
self.destroy ()
def _dbus_signal_cb (self, ibusconn, message):
pass
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
# methods for cmp
# def __lt__ (self, other):

Просмотреть файл

@ -75,7 +75,8 @@ class CandidateWindow (gtk.Window):
gobject.type_register (CandidateWindow, "IBusCandidateWindow")
if __name__ == "__main__":
# gtk.rc_parse ("./themes/default/gtkrc")
# style_string = """"""
# gtk.rc_parse_string (style_string)
window = CandidateWindow ()
window.show_all ()
gtk.main ()