WIP property.
[ibus.git] / panel / panel.py
blob1f51514f4a752f5d1e822b98da82277ad0b3cf87
1 # vim:set noet ts=4:
3 # ibus - The Input Bus
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307 USA
22 import gtk
23 import gtk.gdk as gdk
24 import gobject
25 import ibus
26 from os import path
27 from lang import LANGUAGES
28 from ibus import interface
29 from languagebar import LanguageBar
30 from candidatepanel import CandidatePanel
32 class Panel (ibus.Object):
33 def __init__ (self, proxy, _ibus):
34 gobject.GObject.__init__ (self)
35 self._proxy = proxy
36 self._ibus = _ibus
38 # add icon search path
39 icon_theme = gtk.icon_theme_get_default ()
40 dir = path.dirname (__file__)
41 icondir = path.join (dir, "..", "icons")
42 icon_theme.prepend_search_path (icondir)
44 self._language_bar = LanguageBar ()
45 self._language_bar.connect ("property-activate",
46 lambda widget, prop_name, prop_state: self._proxy.PropertyActivate (prop_name, prop_state))
47 self._language_bar.connect ("im-menu-popup",
48 self._im_menu_popup_cb)
49 self._language_bar.show_all ()
51 self._candidate_panel = CandidatePanel ()
52 self._candidate_panel.connect ("cursor-up",
53 lambda widget: self._proxy.CursorUp ())
54 self._candidate_panel.connect ("cursor-down",
55 lambda widget: self._proxy.CursorDown ())
57 self._status_icon = gtk.StatusIcon ()
58 self._status_icon.connect ("popup-menu", self._status_icon_popup_menu_cb)
59 self._status_icon.connect ("activate", self._status_icon_activate_cb)
60 self._status_icon.set_from_icon_name ("engine-default")
61 self._status_icon.set_tooltip ("iBus - Running")
62 self._status_icon.set_visible (True)
64 def set_cursor_location (self, x, y, w, h):
65 self._candidate_panel.move (x + w, y + h)
67 def update_preedit (self, text, attrs, cursor_pos, show):
68 self._candidate_panel.update_preedit (text, attrs, cursor_pos, show)
70 def show_preedit_string (self):
71 self._candidate_panel.show_preedit_string ()
73 def hide_preedit_string (self):
74 self._candidate_panel.hide_preedit_string ()
76 def update_aux_string (self, text, attrs, show):
77 self._candidate_panel.update_aux_string (text, attrs, show)
79 def show_aux_string (self):
80 self._candidate_panel.show_aux_string ()
82 def hide_aux_string (self):
83 self._candidate_panel.hide_aux_string ()
85 def update_lookup_table (self, lookup_table, show):
86 self._candidate_panel.update_lookup_table (lookup_table, show)
88 def show_candidate_window (self):
89 self._candidate_panel.show ()
91 def hide_candidate_window (self):
92 self._candidate_panel.hide ()
94 def show_language_bar (self):
95 self._language_bar.show ()
97 def hide_language_bar (self):
98 self._language_bar.hide ()
100 def register_properties (self, props):
101 self._language_bar.register_properties (props)
103 def update_property (self, prop):
104 self._language_bar.update_property (self, prop)
106 def reset (self):
107 self._candidate_panel.reset ()
108 self._language_bar.reset ()
110 def do_destroy (self):
111 gtk.main_quit ()
113 def _create_im_menu (self):
114 menu = gtk.Menu ()
115 factories = self._ibus.GetFactories ()
116 if not factories:
117 item = gtk.MenuItem (label = "no engine")
118 item.set_sensitive (False)
119 menu.add (item)
120 else:
121 for factory in factories:
122 name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory)
123 item = gtk.ImageMenuItem ("%s - %s" % (LANGUAGES.get (lang, lang), name))
124 if not icon:
125 icon = "engine-default"
126 item.set_image (gtk.image_new_from_icon_name (icon, gtk.ICON_SIZE_MENU))
127 item.connect ("activate", self._menu_item_activate_cb, factory)
128 menu.add (item)
130 menu.show_all ()
131 menu.set_take_focus (False)
132 return menu
134 def _menu_position_cb (self, menu, button):
135 screen = button.get_screen ()
136 monitor = screen.get_monitor_at_window (button.window)
137 monitor_allocation = screen.get_monitor_geometry (monitor)
139 x, y = button.window.get_origin ()
140 x += button.allocation.x
141 y += button.allocation.y
143 menu_width, menu_height = menu.size_request ()
145 if x + menu_width >= monitor_allocation.width:
146 x -= menu_width - button.allocation.width
147 elif x - menu_width <= 0:
148 pass
149 else:
150 if x <= monitor_allocation.width * 3 / 4:
151 pass
152 else:
153 x -= menu_width - button.allocation.width
155 if y + button.allocation.height + menu_height >= monitor_allocation.height:
156 y -= menu_height
157 elif y - menu_height <= 0:
158 y += button.allocation.height
159 else:
160 if y <= monitor_allocation.height * 3 / 4:
161 y += button.allocation.height
162 else:
163 y -= menu_height
165 return (x, y, False)
167 def _im_menu_popup_cb (self, languagebar, button):
168 menu = self._create_im_menu ()
169 menu.popup (None, None,
170 self._menu_position_cb,
172 gtk.get_current_event_time (),
173 button)
175 def _status_icon_activate_cb (self, status_icon):
176 menu = self._create_im_menu ()
177 menu.popup (None, None,
178 gtk.status_icon_position_menu,
180 gtk.get_current_event_time (),
181 self._status_icon)
183 def _status_icon_popup_menu_cb (self, status_icon, button, active_time):
184 menu = self._create_im_menu ()
185 menu.popup (None, None,
186 gtk.status_icon_position_menu,
187 button,
188 active_time,
189 self._status_icon)
191 def _menu_item_activate_cb (self, item, factory):
192 self._ibus.SetFactory (factory)
194 gobject.type_register (Panel, "IBusPanel")
196 class PanelProxy (interface.IPanel):
197 def __init__ (self, dbusconn, object_path, _ibus):
198 interface.IPanel.__init__ (self, dbusconn, object_path)
199 self._dbusconn = dbusconn
200 self._panel = Panel (self, _ibus)
202 def SetCursorLocation (self, x, y, w, h):
203 self._panel.set_cursor_location (x, y, w, h)
205 def UpdatePreedit (self, text, attrs, cursor_pos, show):
206 attrs = ibus.attr_list_from_dbus_value (attrs)
207 self._panel.update_preedit (text, atrrs, cursor_pos, show)
209 def ShowPreeditString (self):
210 self._panel.show_preedit_string ()
212 def HidePreeditString (self):
213 self._panel.hide_preedit_string ()
215 def UpdateAuxString (self, text, attrs, show):
216 attrs = ibus.attr_list_from_dbus_value (attrs)
217 self._panel.update_aux_string (text, attrs, show)
219 def ShowAuxString (self):
220 self._panel.show_aux_string ()
222 def HideAuxString (self):
223 self._panel.hide_aux_string ()
225 def UpdateLookupTable (self, lookup_table, show):
226 lookup_table = ibus.lookup_table_from_dbus_value (lookup_table)
227 self._panel.update_lookup_table (lookup_table, show)
229 def ShowCandidateWindow (self):
230 self._panel.show_candidate_window ()
232 def HideCandidateWindow (self):
233 self._panel.hide_candidate_window ()
235 def ShowLanguageBar (self):
236 self._panel.show_language_bar ()
238 def HideLanguageBar (self):
239 self._panel.hide_language_bar ()
241 def RegisterProperties (self, props):
242 props = ibus.prop_list_from_dbus_value (props)
243 self._panel.register_properties (props)
245 def UpdateProperty (self, prop):
246 prop = ibus.property_from_dbus_value (props)
247 self._panel.update_property (prop)
249 def Reset (self):
250 self._panel.reset ()
252 def Destroy (self):
253 self._panel.destroy ()