Set icon search path.
[platform/upstream/ibus.git] / panel / panel.py
1 # vim:set noet ts=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
6 #
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.
11 #
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.
16 #
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
21
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
31
32 class Panel (ibus.Object):
33         def __init__ (self, proxy, _ibus):
34                 gobject.GObject.__init__ (self)
35                 self._proxy = proxy
36                 self._ibus = _ibus
37
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)
43
44                 self._language_bar = LanguageBar ()
45                 self._language_bar.connect ("property-activate",
46                                                 lambda widget, prop_name: self._proxy.PropertyActivate (prop_name))
47                 self._language_bar.connect ("im-menu-popup",
48                                                 self._im_menu_popup_cb)
49                 self._language_bar.show_all ()
50
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 ())
56
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)
63
64         def set_cursor_location (self, x, y, w, h):
65                 self._candidate_panel.move (x + w, y + h)
66
67         def update_preedit (self, text, attrs, cursor_pos, show):
68                 self._candidate_panel.update_preedit (text, attrs, cursor_pos, show)
69
70         def show_preedit_string (self):
71                 self._candidate_panel.show_preedit_string ()
72
73         def hide_preedit_string (self):
74                 self._candidate_panel.hide_preedit_string ()
75
76         def update_aux_string (self, text, attrs, show):
77                 self._candidate_panel.update_aux_string (text, attrs, show)
78
79         def show_aux_string (self):
80                 self._candidate_panel.show_aux_string ()
81
82         def hide_aux_string (self):
83                 self._candidate_panel.hide_aux_string ()
84
85         def update_lookup_table (self, lookup_table, show):
86                 self._candidate_panel.update_lookup_table (lookup_table, show)
87
88         def show_candidate_window (self):
89                 self._candidate_panel.show ()
90
91         def hide_candidate_window (self):
92                 self._candidate_panel.hide ()
93
94         def show_language_bar (self):
95                 self._language_bar.show ()
96
97         def hide_language_bar (self):
98                 self._language_bar.hide ()
99
100         def register_properties (self, props):
101                 self._language_bar.register_properties (props)
102
103         def update_property (self, prop):
104                 self._language_bar.update_property (self, prop)
105
106         def reset (self):
107                 self._candidate_panel.reset ()
108                 self._language_bar.reset ()
109
110         def do_destroy (self):
111                 gtk.main_quit ()
112
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 im")
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)
129
130                 menu.show_all ()
131                 menu.set_take_focus (False)
132                 return menu
133
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)
138
139                 x, y = button.window.get_origin ()
140                 x += button.allocation.x
141                 y += button.allocation.y
142
143                 menu_width, menu_height = menu.size_request ()
144
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
154
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
164
165                 return (x, y, False)
166
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,
171                                 0,
172                                 gtk.get_current_event_time (),
173                                 button)
174
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,
179                                 0,
180                                 gtk.get_current_event_time (),
181                                 self._status_icon)
182
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)
190
191         def _menu_item_activate_cb (self, item, factory):
192                 self._ibus.SetFactory (factory)
193
194 gobject.type_register (Panel, "IBusPanel")
195
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)
201
202         def SetCursorLocation (self, x, y, w, h):
203                 self._panel.set_cursor_location (x, y, w, h)
204
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)
208
209         def ShowPreeditString (self):
210                 self._panel.show_preedit_string ()
211
212         def HidePreeditString (self):
213                 self._panel.hide_preedit_string ()
214
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)
218
219         def ShowAuxString (self):
220                 self._panel.show_aux_string ()
221
222         def HideAuxString (self):
223                 self._panel.hide_aux_string ()
224
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)
228
229         def ShowCandidateWindow (self):
230                 self._panel.show_candidate_window ()
231
232         def HideCandidateWindow (self):
233                 self._panel.hide_candidate_window ()
234
235         def ShowLanguageBar (self):
236                 self._panel.show_language_bar ()
237
238         def HideLanguageBar (self):
239                 self._panel.hide_language_bar ()
240
241         def RegisterProperties (self, props):
242                 props = ibus.prop_list_from_dbus_value (props)
243                 self._panel.register_properties (props)
244
245         def UpdateProperty (self, prop):
246                 prop = ibus.property_from_dbus_value (props)
247                 self._panel.update_property (prop)
248
249         def Reset (self):
250                 self._panel.reset ()
251
252         def Destroy (self):
253                 self._panel.destroy ()
254