summaryrefslogtreecommitdiffstats
path: root/panel/panel.py
blob: 3e5b76fcc22b008e5b892f8b0a87fb2d0ff1d3f9 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# vim:set noet ts=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

import gtk
import gtk.gdk as gdk
import gobject
import ibus
from os import path
from lang import LANGUAGES
from ibus import interface
from languagebar import LanguageBar
from candidatepanel import CandidatePanel

class Panel (ibus.Object):
	def __init__ (self, proxy, _ibus):
		gobject.GObject.__init__ (self)
		self._proxy = proxy
		self._ibus = _ibus

		# add icon search path
		icon_theme = gtk.icon_theme_get_default ()
		dir = path.dirname (__file__)
		icondir = path.join (dir, "..", "icons")
		icon_theme.prepend_search_path (icondir)

		self._language_bar = LanguageBar ()
		self._language_bar.connect ("property-activate",
						lambda widget, prop_name, prop_state: self._proxy.PropertyActivate (prop_name, prop_state))
		self._language_bar.connect ("get-im-menu",
						self._get_im_menu_cb)
		self._language_bar.show_all ()

		self._candidate_panel = CandidatePanel ()
		self._candidate_panel.connect ("cursor-up",
						lambda widget: self._proxy.CursorUp ())
		self._candidate_panel.connect ("cursor-down",
						lambda widget: self._proxy.CursorDown ())

		self._status_icon = gtk.StatusIcon ()
		self._status_icon.connect ("popup-menu", self._status_icon_popup_menu_cb)
		self._status_icon.connect ("activate", self._status_icon_activate_cb)
		self._status_icon.set_from_icon_name ("engine-default")
		self._status_icon.set_tooltip ("iBus - Running")
		self._status_icon.set_visible (True)

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

	def update_preedit (self, text, attrs, cursor_pos, show):
		self._candidate_panel.update_preedit (text, attrs, cursor_pos, show)

	def show_preedit_string (self):
		self._candidate_panel.show_preedit_string ()

	def hide_preedit_string (self):
		self._candidate_panel.hide_preedit_string ()

	def update_aux_string (self, text, attrs, show):
		self._candidate_panel.update_aux_string (text, attrs, show)

	def show_aux_string (self):
		self._candidate_panel.show_aux_string ()

	def hide_aux_string (self):
		self._candidate_panel.hide_aux_string ()

	def update_lookup_table (self, lookup_table, show):
		self._candidate_panel.update_lookup_table (lookup_table, show)

	def show_candidate_window (self):
		self._candidate_panel.show ()

	def hide_candidate_window (self):
		self._candidate_panel.hide ()

	def show_language_bar (self):
		self._language_bar.show ()

	def hide_language_bar (self):
		self._language_bar.hide ()

	def register_properties (self, props):
		self._language_bar.register_properties (props)

	def update_property (self, prop):
		self._language_bar.update_property (self, prop)

	def reset (self):
		self._candidate_panel.reset ()
		self._language_bar.reset ()

	def do_destroy (self):
		gtk.main_quit ()

	def _create_im_menu (self):
		menu = gtk.Menu ()
		factories = self._ibus.GetFactories ()
		if not factories:
			item = gtk.MenuItem (label = "no engine")
			item.set_sensitive (False)
			menu.add (item)
		else:
			for factory in factories:
				name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory)
				item = gtk.ImageMenuItem ("%s - %s" % (LANGUAGES.get (lang, lang), name))
				if not icon:
					icon = "engine-default"
				item.set_image (gtk.image_new_from_icon_name (icon, gtk.ICON_SIZE_MENU))
				item.connect ("activate", self._menu_item_activate_cb, factory)
				menu.add (item)

		menu.show_all ()
		menu.set_take_focus (False)
		return menu

	def _get_im_menu_cb (self, languagebar):
		menu = self._create_im_menu ()
		return menu

	def _status_icon_activate_cb (self, status_icon):
		menu = self._create_im_menu ()
		menu.popup (None, None,
				gtk.status_icon_position_menu,
				0,
				gtk.get_current_event_time (),
				self._status_icon)

	def _status_icon_popup_menu_cb (self, status_icon, button, active_time):
		menu = self._create_im_menu ()
		menu.popup (None, None,
				gtk.status_icon_position_menu,
				button,
				active_time,
				self._status_icon)

	def _menu_item_activate_cb (self, item, factory):
		self._ibus.SetFactory (factory)

gobject.type_register (Panel, "IBusPanel")

class PanelProxy (interface.IPanel):
	def __init__ (self, dbusconn, object_path, _ibus):
		interface.IPanel.__init__ (self, dbusconn, object_path)
		self._dbusconn = dbusconn
		self._panel = Panel (self, _ibus)

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

	def UpdatePreedit (self, text, attrs, cursor_pos, show):
		attrs = ibus.attr_list_from_dbus_value (attrs)
		self._panel.update_preedit (text, atrrs, cursor_pos, show)

	def ShowPreeditString (self):
		self._panel.show_preedit_string ()

	def HidePreeditString (self):
		self._panel.hide_preedit_string ()

	def UpdateAuxString (self, text, attrs, show):
		attrs = ibus.attr_list_from_dbus_value (attrs)
		self._panel.update_aux_string (text, attrs, show)

	def ShowAuxString (self):
		self._panel.show_aux_string ()

	def HideAuxString (self):
		self._panel.hide_aux_string ()

	def UpdateLookupTable (self, lookup_table, show):
		lookup_table = ibus.lookup_table_from_dbus_value (lookup_table)
		self._panel.update_lookup_table (lookup_table, show)

	def ShowCandidateWindow (self):
		self._panel.show_candidate_window ()

	def HideCandidateWindow (self):
		self._panel.hide_candidate_window ()

	def ShowLanguageBar (self):
		self._panel.show_language_bar ()

	def HideLanguageBar (self):
		self._panel.hide_language_bar ()

	def RegisterProperties (self, props):
		props = ibus.prop_list_from_dbus_value (props)
		self._panel.register_properties (props)

	def UpdateProperty (self, prop):
		prop = ibus.property_from_dbus_value (props)
		self._panel.update_property (prop)

	def Reset (self):
		self._panel.reset ()

	def Destroy (self):
		self._panel.destroy ()