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
)
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 ("get-im-menu",
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
)
107 self
._candidate
_panel
.reset ()
108 self
._language
_bar
.reset ()
110 def do_destroy (self
):
113 def _create_im_menu (self
):
115 factories
= self
._ibus
.GetFactories ()
117 item
= gtk
.MenuItem (label
= "no engine")
118 item
.set_sensitive (False)
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
))
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
)
131 menu
.set_take_focus (False)
134 def _get_im_menu_cb (self
, languagebar
):
135 menu
= self
._create
_im
_menu
()
138 def _status_icon_activate_cb (self
, status_icon
):
139 menu
= self
._create
_im
_menu
()
140 menu
.popup (None, None,
141 gtk
.status_icon_position_menu
,
143 gtk
.get_current_event_time (),
146 def _status_icon_popup_menu_cb (self
, status_icon
, button
, active_time
):
147 menu
= self
._create
_im
_menu
()
148 menu
.popup (None, None,
149 gtk
.status_icon_position_menu
,
154 def _menu_item_activate_cb (self
, item
, factory
):
155 self
._ibus
.SetFactory (factory
)
157 gobject
.type_register (Panel
, "IBusPanel")
159 class PanelProxy (interface
.IPanel
):
160 def __init__ (self
, dbusconn
, object_path
, _ibus
):
161 interface
.IPanel
.__init
__ (self
, dbusconn
, object_path
)
162 self
._dbusconn
= dbusconn
163 self
._panel
= Panel (self
, _ibus
)
165 def SetCursorLocation (self
, x
, y
, w
, h
):
166 self
._panel
.set_cursor_location (x
, y
, w
, h
)
168 def UpdatePreedit (self
, text
, attrs
, cursor_pos
, show
):
169 attrs
= ibus
.attr_list_from_dbus_value (attrs
)
170 self
._panel
.update_preedit (text
, atrrs
, cursor_pos
, show
)
172 def ShowPreeditString (self
):
173 self
._panel
.show_preedit_string ()
175 def HidePreeditString (self
):
176 self
._panel
.hide_preedit_string ()
178 def UpdateAuxString (self
, text
, attrs
, show
):
179 attrs
= ibus
.attr_list_from_dbus_value (attrs
)
180 self
._panel
.update_aux_string (text
, attrs
, show
)
182 def ShowAuxString (self
):
183 self
._panel
.show_aux_string ()
185 def HideAuxString (self
):
186 self
._panel
.hide_aux_string ()
188 def UpdateLookupTable (self
, lookup_table
, show
):
189 lookup_table
= ibus
.lookup_table_from_dbus_value (lookup_table
)
190 self
._panel
.update_lookup_table (lookup_table
, show
)
192 def ShowCandidateWindow (self
):
193 self
._panel
.show_candidate_window ()
195 def HideCandidateWindow (self
):
196 self
._panel
.hide_candidate_window ()
198 def ShowLanguageBar (self
):
199 self
._panel
.show_language_bar ()
201 def HideLanguageBar (self
):
202 self
._panel
.hide_language_bar ()
204 def RegisterProperties (self
, props
):
205 props
= ibus
.prop_list_from_dbus_value (props
)
206 self
._panel
.register_properties (props
)
208 def UpdateProperty (self
, prop
):
209 prop
= ibus
.property_from_dbus_value (props
)
210 self
._panel
.update_property (prop
)
216 self
._panel
.destroy ()