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
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
)
39 # add icon search path
40 icon_theme
= gtk
.icon_theme_get_default ()
41 dir = path
.dirname (__file__
)
42 icondir
= path
.join (dir, "..", "icons")
43 icon_theme
.prepend_search_path (icondir
)
45 self
._language
_bar
= LanguageBar ()
46 self
._language
_bar
.connect ("property-activate",
47 lambda widget
, prop_name
, prop_state
: self
._proxy
.PropertyActivate (prop_name
, prop_state
))
48 self
._language
_bar
.connect ("get-im-menu",
50 self
._language
_bar
.focus_out ()
51 self
._language
_bar
.show_all ()
53 self
._candidate
_panel
= CandidatePanel ()
54 self
._candidate
_panel
.connect ("cursor-up",
55 lambda widget
: self
._proxy
.CursorUp ())
56 self
._candidate
_panel
.connect ("cursor-down",
57 lambda widget
: self
._proxy
.CursorDown ())
59 self
._status
_icon
= gtk
.StatusIcon ()
60 self
._status
_icon
.connect ("popup-menu", self
._status
_icon
_popup
_menu
_cb
)
61 self
._status
_icon
.connect ("activate", self
._status
_icon
_activate
_cb
)
62 self
._status
_icon
.set_from_icon_name ("engine-default")
63 self
._status
_icon
.set_tooltip ("iBus - Running")
64 self
._status
_icon
.set_visible (True)
66 def set_cursor_location (self
, x
, y
, w
, h
):
67 self
._candidate
_panel
.move (x
+ w
, y
+ h
)
69 def update_preedit (self
, text
, attrs
, cursor_pos
, show
):
70 self
._candidate
_panel
.update_preedit (text
, attrs
, cursor_pos
, show
)
72 def show_preedit_string (self
):
73 self
._candidate
_panel
.show_preedit_string ()
75 def hide_preedit_string (self
):
76 self
._candidate
_panel
.hide_preedit_string ()
78 def update_aux_string (self
, text
, attrs
, show
):
79 self
._candidate
_panel
.update_aux_string (text
, attrs
, show
)
81 def show_aux_string (self
):
82 self
._candidate
_panel
.show_aux_string ()
84 def hide_aux_string (self
):
85 self
._candidate
_panel
.hide_aux_string ()
87 def update_lookup_table (self
, lookup_table
, show
):
88 self
._candidate
_panel
.update_lookup_table (lookup_table
, show
)
90 def show_candidate_window (self
):
91 self
._candidate
_panel
.show ()
93 def hide_candidate_window (self
):
94 self
._candidate
_panel
.hide ()
96 def show_language_bar (self
):
97 self
._language
_bar
.show ()
99 def hide_language_bar (self
):
100 self
._language
_bar
.hide ()
102 def register_properties (self
, props
):
103 self
._language
_bar
.register_properties (props
)
105 def update_property (self
, prop
):
106 self
._language
_bar
.update_property (self
, prop
)
108 def _set_im_icon (self
, icon_name
):
109 self
._language
_bar
.set_im_icon (icon_name
)
110 self
._status
_icon
.set_from_icon_name (icon_name
)
112 def focus_in (self
, ic
):
116 factory
, enabled
= self
._ibus
.GetInputContextStates (ic
)
118 if factory
== "" or not enabled
:
119 self
._set
_im
_icon
("engine-default")
121 name
, lang
, icon
, authors
, credits
= self
._ibus
.GetFactoryInfo (factory
)
122 self
._set
_im
_icon
(icon
)
123 self
._language
_bar
.focus_in ()
125 def focus_out (self
, ic
):
127 if self
._focus
_ic
== ic
:
128 self
._focus
_ic
= None
129 self
._language
_bar
.focus_out ()
130 self
._set
_im
_icon
("engine-default")
132 def states_changed (self
):
133 if not self
._focus
_ic
:
135 factory
, enabled
= self
._ibus
.GetInputContextStates (self
._focus
_ic
)
137 self
._set
_im
_icon
("engine-default")
139 name
, lang
, icon
, authors
, credits
= self
._ibus
.GetFactoryInfo (factory
)
140 self
._set
_im
_icon
(icon
)
143 self
._candidate
_panel
.reset ()
144 self
._language
_bar
.reset ()
146 def do_destroy (self
):
149 def _create_im_menu (self
):
151 factories
= self
._ibus
.GetFactories ()
153 item
= gtk
.MenuItem (label
= "no engine")
154 item
.set_sensitive (False)
157 for factory
in factories
:
158 name
, lang
, icon
, authors
, credits
= self
._ibus
.GetFactoryInfo (factory
)
159 item
= gtk
.ImageMenuItem ("%s - %s" % (LANGUAGES
.get (lang
, lang
), name
))
161 icon
= "engine-default"
162 item
.set_image (gtk
.image_new_from_icon_name (icon
, gtk
.ICON_SIZE_MENU
))
163 item
.connect ("activate", self
._menu
_item
_activate
_cb
, factory
)
167 menu
.set_take_focus (False)
170 def _get_im_menu_cb (self
, languagebar
):
171 menu
= self
._create
_im
_menu
()
174 def _status_icon_activate_cb (self
, status_icon
):
175 if not self
._focus
_ic
:
177 menu
= self
._create
_im
_menu
()
178 menu
.popup (None, None,
179 gtk
.status_icon_position_menu
,
181 gtk
.get_current_event_time (),
184 def _status_icon_popup_menu_cb (self
, status_icon
, button
, active_time
):
185 if not self
._focus
_ic
:
187 menu
= self
._create
_im
_menu
()
188 menu
.popup (None, None,
189 gtk
.status_icon_position_menu
,
194 def _menu_item_activate_cb (self
, item
, factory
):
195 self
._ibus
.SetFactory (factory
)
197 gobject
.type_register (Panel
, "IBusPanel")
199 class PanelProxy (interface
.IPanel
):
200 def __init__ (self
, dbusconn
, object_path
, _ibus
):
201 interface
.IPanel
.__init
__ (self
, dbusconn
, object_path
)
202 self
._dbusconn
= dbusconn
203 self
._panel
= Panel (self
, _ibus
)
205 def SetCursorLocation (self
, x
, y
, w
, h
):
206 self
._panel
.set_cursor_location (x
, y
, w
, h
)
208 def UpdatePreedit (self
, text
, attrs
, cursor_pos
, show
):
209 attrs
= ibus
.attr_list_from_dbus_value (attrs
)
210 self
._panel
.update_preedit (text
, atrrs
, cursor_pos
, show
)
212 def ShowPreeditString (self
):
213 self
._panel
.show_preedit_string ()
215 def HidePreeditString (self
):
216 self
._panel
.hide_preedit_string ()
218 def UpdateAuxString (self
, text
, attrs
, show
):
219 attrs
= ibus
.attr_list_from_dbus_value (attrs
)
220 self
._panel
.update_aux_string (text
, attrs
, show
)
222 def ShowAuxString (self
):
223 self
._panel
.show_aux_string ()
225 def HideAuxString (self
):
226 self
._panel
.hide_aux_string ()
228 def UpdateLookupTable (self
, lookup_table
, show
):
229 lookup_table
= ibus
.lookup_table_from_dbus_value (lookup_table
)
230 self
._panel
.update_lookup_table (lookup_table
, show
)
232 def ShowCandidateWindow (self
):
233 self
._panel
.show_candidate_window ()
235 def HideCandidateWindow (self
):
236 self
._panel
.hide_candidate_window ()
238 def ShowLanguageBar (self
):
239 self
._panel
.show_language_bar ()
241 def HideLanguageBar (self
):
242 self
._panel
.hide_language_bar ()
244 def RegisterProperties (self
, props
):
245 props
= ibus
.prop_list_from_dbus_value (props
)
246 self
._panel
.register_properties (props
)
248 def UpdateProperty (self
, prop
):
249 prop
= ibus
.property_from_dbus_value (props
)
250 self
._panel
.update_property (prop
)
252 def FocusIn (self
, ic
):
253 self
._panel
.focus_in (ic
)
255 def FocusOut (self
, ic
):
256 self
._panel
.focus_out (ic
)
258 def StatesChanged (self
):
259 self
._panel
.states_changed ()
265 self
._panel
.destroy ()