1 /* vim:set et sts=4 sw=4:
5 * Copyright(c) 2011 Peng Huang <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
28 public extern const string IBUS_VERSION;
29 public extern const string BINDIR;
31 class Panel : IBus.PanelService {
32 private IBus.Bus m_bus;
33 private IBus.Config m_config;
34 private Gtk.StatusIcon m_status_icon;
35 private Gtk.Menu m_ime_menu;
36 private Gtk.Menu m_sys_menu;
37 private IBus.EngineDesc[] m_engines = {};
38 private CandidatePanel m_candidate_panel;
39 private Switcher m_switcher;
40 private PropertyManager m_property_manager;
41 private GLib.Pid m_setup_pid = 0;
42 private Gtk.AboutDialog m_about_dialog;
43 private Gtk.CssProvider m_css_provider;
44 private const string ACCELERATOR_SWITCH_IME_FOREWARD = "<Control>space";
46 private uint m_switch_keysym = 0;
47 private Gdk.ModifierType m_switch_modifiers = 0;
49 public Panel(IBus.Bus bus) {
50 GLib.assert(bus.is_connected());
51 // Chain up base class constructor
52 GLib.Object(connection : bus.get_connection(),
53 object_path : "/org/freedesktop/IBus/Panel");
58 m_status_icon = new Gtk.StatusIcon();
59 m_status_icon.set_name("ibus-ui-gtk");
60 m_status_icon.set_title("IBus Panel");
61 m_status_icon.popup_menu.connect(status_icon_popup_menu_cb);
62 m_status_icon.activate.connect(status_icon_activate_cb);
63 m_status_icon.set_from_icon_name("ibus-keyboard");
65 m_candidate_panel = new CandidatePanel();
66 m_candidate_panel.page_up.connect((w) => this.page_up());
67 m_candidate_panel.page_down.connect((w) => this.page_down());
69 m_switcher = new Switcher();
70 bind_switch_shortcut();
72 m_property_manager = new PropertyManager();
73 m_property_manager.property_activate.connect((k, s) => {
74 property_activate(k, s);
81 unbind_switch_shortcut();
84 private void bind_switch_shortcut() {
85 var keybinding_manager = KeybindingManager.get_instance();
87 Gtk.accelerator_parse(ACCELERATOR_SWITCH_IME_FOREWARD,
88 out m_switch_keysym, out m_switch_modifiers);
90 if (m_switch_keysym == 0 && m_switch_modifiers == 0) {
91 warning("Parse accelerator '%s' failed!",
92 ACCELERATOR_SWITCH_IME_FOREWARD);
96 keybinding_manager.bind(m_switch_keysym, m_switch_modifiers,
97 (e) => handle_engine_switch(e, false));
99 // accelerator already has Shift mask
100 if ((m_switch_modifiers & Gdk.ModifierType.SHIFT_MASK) != 0)
103 keybinding_manager.bind(m_switch_keysym,
104 m_switch_modifiers | Gdk.ModifierType.SHIFT_MASK,
105 (e) => handle_engine_switch(e, true));
108 private void unbind_switch_shortcut() {
109 var keybinding_manager = KeybindingManager.get_instance();
111 if (m_switch_keysym == 0 && m_switch_modifiers == 0)
114 keybinding_manager.unbind(m_switch_keysym, m_switch_modifiers);
115 keybinding_manager.unbind(m_switch_keysym,
116 m_switch_modifiers | Gdk.ModifierType.SHIFT_MASK);
119 m_switch_modifiers = 0;
122 private void set_custom_font() {
123 Gdk.Display display = Gdk.Display.get_default();
124 Gdk.Screen screen = (display != null) ?
125 display.get_default_screen() : null;
127 if (screen == null) {
128 warning("Could not open display.");
132 bool use_custom_font = false;
133 GLib.Variant var_use_custom_font = m_config.get_value("panel",
136 if (var_use_custom_font != null) {
137 use_custom_font = var_use_custom_font.get_boolean();
140 if (m_css_provider != null) {
141 Gtk.StyleContext.remove_provider_for_screen(screen,
143 m_css_provider = null;
146 if (use_custom_font == false) {
150 string font_name = null;
151 GLib.Variant var_custom_font = m_config.get_value("panel",
153 if (var_custom_font != null) {
154 font_name = var_custom_font.dup_string();
157 if (font_name == null) {
158 warning("No config panel:custom_font.");
162 string data_format = "GtkLabel { font: %s; }";
163 string data = data_format.printf(font_name);
164 m_css_provider = new Gtk.CssProvider();
167 m_css_provider.load_from_data(data, -1);
168 } catch (GLib.Error e) {
169 warning("Failed css_provider_from_data: %s: %s", font_name,
174 Gtk.StyleContext.add_provider_for_screen(screen,
176 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
179 public void set_config(IBus.Config config) {
180 if (m_config != null) {
181 m_config.value_changed.disconnect(config_value_changed_cb);
182 m_config.watch(null, null);
187 if (m_config != null) {
188 m_config.value_changed.connect(config_value_changed_cb);
189 m_config.watch("general", "preload_engines");
190 m_config.watch("general", "engines_order");
191 m_config.watch("panel", "custom_font");
192 m_config.watch("panel", "use_custom_font");
193 update_engines(m_config.get_value("general", "preload_engines"),
194 m_config.get_value("general", "engines_order"));
196 update_engines(null, null);
202 private void switch_engine(int i, bool force = false) {
203 GLib.assert(i >= 0 && i < m_engines.length);
205 // Do not need switch
206 if (i == 0 && !force)
209 IBus.EngineDesc engine = m_engines[i];
211 if (!m_bus.set_global_engine(engine.get_name())) {
212 warning("Switch engine to %s failed.", engine.get_name());
216 string cmdline = "setxkbmap %s".printf(engine.get_layout());
218 if (!GLib.Process.spawn_command_line_sync(cmdline)) {
219 warning("Switch xkb layout to %s failed.",
220 engine.get_layout());
222 } catch (GLib.SpawnError e) {
223 warning("execute setxkblayout failed");
227 private void config_value_changed_cb(IBus.Config config,
231 if (section == "general" && name == "preload_engines") {
232 update_engines(variant, null);
236 if (section == "panel" && (name == "custom_font" ||
237 name == "use_custom_font")) {
243 private void handle_engine_switch(Gdk.Event event, bool revert) {
244 // Do not need switch IME
245 if (m_engines.length <= 1)
248 uint primary_modifiers =
249 KeybindingManager.get_primary_modifier(event.key.state);
251 bool pressed = KeybindingManager.primary_modifier_still_pressed(
252 event, primary_modifiers);
254 int i = revert ? m_engines.length - 1 : 1;
255 i = m_switcher.run(m_switch_keysym, m_switch_modifiers, event,
258 debug("switch cancelled");
260 GLib.assert(i < m_engines.length);
264 int i = revert ? m_engines.length - 1 : 1;
269 private void update_engines(GLib.Variant? var_engines,
270 GLib.Variant? var_order) {
271 string[] engine_names = null;
273 if (var_engines != null)
274 engine_names = var_engines.dup_strv();
275 if (engine_names == null || engine_names.length == 0)
276 engine_names = {"xkb:us::eng"};
278 string[] order_names =
279 (var_order != null) ? var_order.dup_strv() : null;
283 foreach (var name in order_names) {
284 if (name in engine_names)
288 foreach (var name in engine_names) {
294 var engines = m_bus.get_engines_by_names(names);
296 if (m_engines.length == 0) {
298 switch_engine(0, true);
300 var current_engine = m_engines[0];
303 for (i = 0; i < m_engines.length; i++) {
304 if (current_engine.get_name() == engines[i].get_name()) {
309 switch_engine(0, true);
314 private void show_setup_dialog() {
315 if (m_setup_pid != 0) {
316 if (Posix.kill(m_setup_pid, Posix.SIGUSR1) == 0)
321 string binary = GLib.Path.build_filename(BINDIR, "ibus-setup");
323 GLib.Process.spawn_async(null,
324 {binary, "ibus-setup"},
326 GLib.SpawnFlags.DO_NOT_REAP_CHILD,
329 } catch (GLib.SpawnError e) {
330 warning("Execute %s failed! %s", binary, e.message);
334 GLib.ChildWatch.add(m_setup_pid, (pid, state) => {
335 if (pid != m_setup_pid)
338 GLib.Process.close_pid(pid);
342 private void show_about_dialog() {
343 if (m_about_dialog == null) {
344 m_about_dialog = new Gtk.AboutDialog();
345 m_about_dialog.set_program_name("IBus");
346 m_about_dialog.set_version(IBUS_VERSION);
348 string copyright = _(
349 "Copyright (c) 2007-2012 Peng Huang\n" +
350 "Copyright (c) 2007-2010 Red Hat, Inc.\n");
352 m_about_dialog.set_copyright(copyright);
353 m_about_dialog.set_license("LGPL");
354 m_about_dialog.set_comments(_("IBus is an intelligent input bus for Linux/Unix."));
355 m_about_dialog.set_website("http://code.google.com/p/ibus");
356 m_about_dialog.set_authors({"Peng Huang <shawn.p.huang@gmail.com>"});
357 m_about_dialog.set_documenters({"Peng Huang <shawn.p.huang@gmail.com>"});
358 m_about_dialog.set_translator_credits(_("translator-credits"));
359 m_about_dialog.set_logo_icon_name("ibus");
360 m_about_dialog.set_icon_name("ibus");
363 if (!m_about_dialog.get_visible()) {
364 m_about_dialog.run();
365 m_about_dialog.hide();
367 m_about_dialog.present();
371 private void status_icon_popup_menu_cb(Gtk.StatusIcon status_icon,
373 uint activate_time) {
375 if (m_sys_menu == null) {
376 Gtk.ImageMenuItem item;
377 m_sys_menu = new Gtk.Menu();
379 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.PREFERENCES, null);
380 item.activate.connect((i) => show_setup_dialog());
381 m_sys_menu.append(item);
383 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.ABOUT, null);
384 item.activate.connect((i) => show_about_dialog());
385 m_sys_menu.append(item);
387 m_sys_menu.append(new SeparatorMenuItem());
389 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.REFRESH, null);
390 item.set_label(_("Restart"));
391 item.activate.connect((i) => m_bus.exit(true));
392 m_sys_menu.append(item);
394 item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.QUIT, null);
395 item.activate.connect((i) => m_bus.exit(false));
396 m_sys_menu.append(item);
398 m_sys_menu.show_all();
401 m_sys_menu.popup(null,
403 m_status_icon.position_menu,
405 Gtk.get_current_event_time());
408 private void status_icon_activate_cb(Gtk.StatusIcon status_icon) {
409 m_ime_menu = new Gtk.Menu();
411 // Show properties and IME switching menu
412 m_property_manager.create_menu_items(m_ime_menu);
414 m_ime_menu.append(new SeparatorMenuItem());
417 Gtk.icon_size_lookup(Gtk.IconSize.MENU, out width, out height);
420 foreach (var engine in m_engines) {
421 var language = engine.get_language();
422 var longname = engine.get_longname();
423 var item = new Gtk.ImageMenuItem.with_label(
424 "%s - %s".printf (IBus.get_language_name(language), longname));
425 if (engine.get_icon() != "") {
426 var icon = new IconWidget(engine.get_icon(), width);
427 item.set_image(icon);
429 // Make a copy of engine to workaround a bug in vala.
430 // https://bugzilla.gnome.org/show_bug.cgi?id=628336
432 item.activate.connect((item) => {
433 for (int i = 0; i < m_engines.length; i++) {
434 if (e == m_engines[i]) {
440 m_ime_menu.add(item);
443 m_ime_menu.show_all();
445 // Do not take focuse to avoid some focus related issues.
446 m_ime_menu.set_take_focus(false);
447 m_ime_menu.popup(null,
449 m_status_icon.position_menu,
451 Gtk.get_current_event_time());
454 /* override virtual functions */
455 public override void set_cursor_location(int x, int y,
456 int width, int height) {
457 m_candidate_panel.set_cursor_location(x, y, width, height);
460 public override void focus_in(string input_context_path) {
463 public override void focus_out(string input_context_path) {
466 public override void register_properties(IBus.PropList props) {
467 m_property_manager.set_properties(props);
470 public override void update_property(IBus.Property prop) {
471 m_property_manager.update_property(prop);
474 public override void update_preedit_text(IBus.Text text,
478 m_candidate_panel.set_preedit_text(text, cursor_pos);
480 m_candidate_panel.set_preedit_text(null, 0);
483 public override void hide_preedit_text() {
484 m_candidate_panel.set_preedit_text(null, 0);
487 public override void update_auxiliary_text(IBus.Text text,
489 m_candidate_panel.set_auxiliary_text(visible ? text : null);
492 public override void hide_auxiliary_text() {
493 m_candidate_panel.set_auxiliary_text(null);
496 public override void update_lookup_table(IBus.LookupTable table,
498 m_candidate_panel.set_lookup_table(visible ? table : null);
501 public override void hide_lookup_table() {
502 m_candidate_panel.set_lookup_table(null);
505 public override void state_changed() {
506 var icon_name = "ibus-keyboard";
508 var engine = m_bus.get_global_engine();
510 icon_name = engine.get_icon();
512 if (icon_name[0] == '/')
513 m_status_icon.set_from_file(icon_name);
515 m_status_icon.set_from_icon_name(icon_name);
521 for (i = 0; i < m_engines.length; i++) {
522 if (m_engines[i].get_name() == engine.get_name())
526 // engine is first engine in m_engines.
530 // engine is not in m_engines.
531 if (i >= m_engines.length)
534 for (int j = i; j > 0; j--) {
535 m_engines[j] = m_engines[j - 1];
537 m_engines[0] = engine;
540 foreach(var desc in m_engines) {
541 names += desc.get_name();
543 if (m_config != null)
544 m_config.set_value("general",
546 new GLib.Variant.strv(names));