summaryrefslogtreecommitdiffstats
path: root/ibus/eng_demo.py
blob: f5993948a0816b917e4d9ca2f5eacd196cf9aae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
import interface
import gobject
import gtk
import dbus.connection
import dbus.mainloop.glib
import ibus.keysyms as keysyms
from ibus.common import \
	IBUS_NAME, \
	IBUS_IFACE, \
	IBUS_PATH, \
	IBUS_ADDR

IBUS_DEMO_ENGINE_FACTORY_PATH = "/com/redhat/IBus/engines/Demo/Factory"
IBUS_DEMO_ENGINE_PATH = "/com/redhat/IBus/engines/Demo/Engine/%d"

class LookupTable (gtk.Window):
	def __init__ (self):
		gtk.Window.__init__ (self, gtk.WINDOW_POPUP)
		self._enable = False
		# create ui
		vbox = gtk.VBox ()
		self._preedit_label = gtk.Label ("preedit string")
		self._candidates_label = gtk.Label ("candidates")
		vbox.pack_start (self._preedit_label)
		vbox.pack_start (self._candidates_label)
		self.add (vbox)

	def focus_in (self):
		if self._enable:
			self.show_all ()

	def focus_out (self):
		if self._enable:
			self.hide_all ()

	def set_enable (self, enable):
		self._enable = enable
		if enable:
			self.show_all ()
		else:
			self.hide_all ()

	def set_cursor_location (self, x, y, w, h):
		self.move (x + w, y + h)

class DemoEngineFactory (interface.IEngineFactory):
	NAME = "DemoEngine"
	LANG = "en"
	ICON = ""
	AUTHORS = "Huang Peng <shawn.p.huang@gmail.com>"
	CREDITS = "GPLv2"

	def __init__ (self, dbusconn):
		interface.IEngineFactory.__init__ (self, dbusconn, object_path = IBUS_DEMO_ENGINE_FACTORY_PATH)
		self._dbusconn = dbusconn
		self._max_engine_id = 1
	
	def GetInfo (self):
		return [
			self.NAME,
			self.LANG,
			self.ICON,
			self.AUTHORS,
			self.CREDITS
			]

	def CreateEngine (self):
		engine_path = IBUS_DEMO_ENGINE_PATH % self._max_engine_id
		self._max_engine_id += 1
		return DemoEngine (self._dbusconn, engine_path)


class DemoEngine (interface.IEngine):

	def __init__ (self, dbusconn, object_path):
		interface.IEngine.__init__ (self, dbusconn, object_path = object_path)
		self._dbusconn = dbusconn
		self._lookup_table = LookupTable ()

	def ProcessKeyEvent (self, keyval, is_press, state):
		if not is_press:
			return False
		if keyval < 128 and (state & keysyms.CONTROL_MASK | keysyms.MOD1_MASK | keysyms.SHIFT_MASK) == 0:
			self.CommitString (chr(keyval))
			print "commit %s" % chr(keyval)
			return True
		return False

	def FocusIn (self):
		print "FocusIn"
		self._lookup_table.focus_in ()

	def FocusOut (self):
		print "FocusOut"
		self._lookup_table.focus_out ()

	def Reset (self):
		print "Reset"

	def SetEnable (self, enable):
		self._enable = enable
		self._lookup_table.set_enable (enable)

	def Destroy (self):
		print "Destroy"
		self._lookup_table.destroy ()

	def SetCursorLocation (self, x, y, w, h):
		self._lookup_table.set_cursor_location (x, y, w, h)
		

class IMApp:
	def __init__ (self):
		self._dbusconn = dbus.connection.Connection (IBUS_ADDR)
		self._dbusconn.add_signal_receiver (self._disconnected_cb, 
							"Disconnected", 
							dbus_interface=dbus.LOCAL_IFACE)
		self._engine = DemoEngineFactory (self._dbusconn)
		self._ibus = self._dbusconn.get_object (IBUS_NAME, IBUS_PATH)
		self._ibus.RegisterFactories ([IBUS_DEMO_ENGINE_FACTORY_PATH])

	def run (self):
		gtk.main ()

	def _disconnected_cb (self):
		print "disconnected"
		gtk.main_quit ()
	

def main ():
	IMApp ().run ()

if __name__ == "__main__":
	dbus.mainloop.glib.DBusGMainLoop (set_as_default=True)
	main ()