/usr/share/ibus/setup/keyboardshortcut.py is in ibus 1.5.5-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | # vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2007-2010 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
__all__ = (
"KeyboardShortcutSelection",
"KeyboardShortcutSelectionDialog",
);
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import IBus
from gi.repository import Pango
from i18n import _, N_
MAX_HOTKEY = 6
class KeyboardShortcutSelection(Gtk.VBox):
def __init__(self, shortcuts = None):
super(KeyboardShortcutSelection, self).__init__()
self.__init_ui()
self.set_shortcuts(shortcuts)
def __init_ui(self):
# label = Gtk.Label(_("Keyboard shortcuts:"))
# label.set_justify(Gtk.Justification.LEFT)
# label.set_alignment(0.0, 0.5)
# self.pack_start(label, False, True, 4)
# shortcuts view
self.__shortcut_view = Gtk.TreeView(Gtk.ListStore(GObject.TYPE_STRING))
self.__shortcut_view.set_size_request(-1, 100)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(_("Keyboard shortcuts"), renderer, text = 0)
self.__shortcut_view.append_column(column)
self.__shortcut_view.connect("cursor-changed", self.__shortcut_view_cursor_changed_cb)
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
scrolledwindow.add(self.__shortcut_view)
scrolledwindow.set_shadow_type(Gtk.ShadowType.IN)
self.pack_start(scrolledwindow, True, True, 4)
# key code
hbox = Gtk.HBox()
label = Gtk.Label(_("Key code:"))
label.set_justify(Gtk.Justification.LEFT)
label.set_alignment(0.0, 0.5)
hbox.pack_start(label, False, True, 4)
self.__keycode_entry = Gtk.Entry()
self.__keycode_entry.connect("notify::text", self.__keycode_entry_notify_cb)
hbox.pack_start(self.__keycode_entry, True, True, 4)
self.__keycode_button = Gtk.Button("...")
self.__keycode_button.connect("clicked", self.__keycode_button_clicked_cb)
hbox.pack_start(self.__keycode_button, False, True, 4)
self.pack_start(hbox, False, True, 4)
# modifiers
hbox = Gtk.HBox()
label = Gtk.Label(_("Modifiers:"))
label.set_justify(Gtk.Justification.LEFT)
label.set_alignment(0.0, 0.5)
hbox.pack_start(label, False, True, 4)
table = Gtk.Table(4, 2)
self.__modifier_buttons = []
self.__modifier_buttons.append(("Control",
Gtk.CheckButton.new_with_mnemonic("_Control"),
Gdk.ModifierType.CONTROL_MASK))
self.__modifier_buttons.append(("Alt",
Gtk.CheckButton.new_with_mnemonic("A_lt"),
Gdk.ModifierType.MOD1_MASK))
self.__modifier_buttons.append(("Shift",
Gtk.CheckButton.new_with_mnemonic("_Shift"),
Gdk.ModifierType.SHIFT_MASK))
self.__modifier_buttons.append(("Meta",
Gtk.CheckButton.new_with_mnemonic("_Meta"),
Gdk.ModifierType.META_MASK))
self.__modifier_buttons.append(("Super",
Gtk.CheckButton.new_with_mnemonic("S_uper"),
Gdk.ModifierType.SUPER_MASK))
self.__modifier_buttons.append(("Hyper",
Gtk.CheckButton.new_with_mnemonic("_Hyper"),
Gdk.ModifierType.HYPER_MASK))
# <CapsLock> is not parsed by gtk_accelerator_parse()
# FIXME: Need to check if ibus gtk panel can enable <Release>.
self.__modifier_buttons.append(("Release",
Gtk.CheckButton.new_with_mnemonic("_Release"),
Gdk.ModifierType.RELEASE_MASK))
for name, button, mask in self.__modifier_buttons:
button.connect("toggled", self.__modifier_button_toggled_cb, name)
table.attach(self.__modifier_buttons[0][1], 0, 1, 0, 1)
table.attach(self.__modifier_buttons[1][1], 1, 2, 0, 1)
table.attach(self.__modifier_buttons[2][1], 2, 3, 0, 1)
table.attach(self.__modifier_buttons[3][1], 3, 4, 0, 1)
table.attach(self.__modifier_buttons[4][1], 0, 1, 1, 2)
table.attach(self.__modifier_buttons[5][1], 1, 2, 1, 2)
table.attach(self.__modifier_buttons[6][1], 2, 3, 1, 2)
hbox.pack_start(table, True, True, 4)
self.pack_start(hbox, False, True, 4)
# buttons
hbox = Gtk.HBox()
# add button
self.__add_button = Gtk.Button(stock = Gtk.STOCK_ADD)
self.__add_button.set_sensitive(False)
self.__add_button.connect("clicked", self.__add_button_clicked_cb)
hbox.pack_start(self.__add_button, False, True, 0)
# apply button
self.__apply_button = Gtk.Button(stock = Gtk.STOCK_APPLY)
self.__apply_button.set_sensitive(False)
self.__apply_button.connect("clicked", self.__apply_button_clicked_cb)
hbox.pack_start(self.__apply_button, False, True, 0)
# delete button
self.__delete_button = Gtk.Button(stock = Gtk.STOCK_DELETE)
self.__delete_button.set_sensitive(False)
self.__delete_button.connect("clicked", self.__delete_button_clicked_cb)
hbox.pack_start(self.__delete_button, False, True, 0)
self.pack_start(hbox, False, True, 4)
def set_shortcuts(self, shortcuts = None):
if shortcuts == None:
shortcuts = []
model = self.__shortcut_view.get_model()
model.clear()
added = []
for shortcut in shortcuts:
if shortcut not in added:
it = model.insert(0)
model[it][0] = shortcut
added.append(shortcut)
def get_shortcuts(self):
model = self.__shortcut_view.get_model()
try:
return [i[0] for i in model]
except:
return []
def add_shortcut(self, shortcut):
model = self.__shortcut_view.get_model()
if len(model) > MAX_HOTKEY:
return
if shortcut in self.get_shortcuts():
return
it = model.insert(0)
model[it][0] = shortcut
self.__add_button.set_sensitive(False)
path = model.get_path(it)
self.__shortcut_view.set_cursor(path, None, False)
def __get_shortcut_from_buttons(self):
modifiers = []
keycode = self.__keycode_entry.get_text()
if Gdk.keyval_from_name(keycode) == 0:
return None
for name, button, mask in self.__modifier_buttons:
if button.get_active():
modifiers.append(name)
if keycode.startswith("_"):
keycode = keycode[1:]
shortcut = "".join(map(lambda m: '<' + m + '>', modifiers))
shortcut += keycode
return shortcut
def __set_shortcut_to_buttons(self, shortcut):
(keyval, state) = Gtk.accelerator_parse(shortcut)
if keyval == 0 and state == 0:
return
for name, button, mask in self.__modifier_buttons:
if state & mask:
button.set_active(True)
else:
button.set_active(False)
self.__keycode_entry.set_text(shortcut.rsplit('>', 1)[-1])
def __get_selected_shortcut(self):
model = self.__shortcut_view.get_model()
path, column = self.__shortcut_view.get_cursor()
if path == None:
return None
return model[path.get_indices()[0]][0]
def __set_selected_shortcut(self, shortcut):
model = self.__shortcut_view.get_model()
path, column = self.__shortcut_view.get_cursor()
model[path[0]][0] = shortcut
self.__update_add_and_apply_buttons()
def __del_selected_shortcut(self):
model = self.__shortcut_view.get_model()
path, column = self.__shortcut_view.get_cursor()
model.remove(model.get_iter(path))
self.__update_add_and_apply_buttons()
def __shortcut_view_cursor_changed_cb(self, treeview):
shortcut = self.__get_selected_shortcut()
if shortcut != None:
self.__set_shortcut_to_buttons(shortcut)
self.__delete_button.set_sensitive(True)
else:
self.__delete_button.set_sensitive(False)
def __update_add_and_apply_buttons(self):
shortcut = self.__get_shortcut_from_buttons()
selected_shortcut = self.__get_selected_shortcut()
shortcuts = self.get_shortcuts()
can_add = shortcut != None and \
shortcut not in shortcuts \
and len(shortcuts) < MAX_HOTKEY
self.__add_button.set_sensitive(can_add)
can_apply = shortcut != selected_shortcut and \
shortcut != None and \
selected_shortcut != None and \
shortcut not in shortcuts
self.__apply_button.set_sensitive(can_apply)
def __modifier_button_toggled_cb(self, button, name):
self.__update_add_and_apply_buttons()
def __keycode_entry_notify_cb(self, entry, arg):
self.__update_add_and_apply_buttons()
def __keycode_button_clicked_cb(self, button):
out = []
dlg = Gtk.MessageDialog(parent = self.get_toplevel(), buttons = Gtk.ButtonsType.CLOSE)
message = _("Please press a key (or a key combination).\nThe dialog will be closed when the key is released.")
dlg.set_markup(message)
dlg.set_title(_("Please press a key (or a key combination)"))
sw = Gtk.ScrolledWindow()
def __accel_edited_cb(c, path, keyval, state, keycode):
out.append(keyval)
out.append(state)
out.append(keycode)
dlg.response(Gtk.ResponseType.OK)
model = Gtk.ListStore(GObject.TYPE_INT,
GObject.TYPE_UINT,
GObject.TYPE_UINT)
accel_view = Gtk.TreeView(model)
sw.add(accel_view)
column = Gtk.TreeViewColumn()
renderer = Gtk.CellRendererAccel(accel_mode=Gtk.CellRendererAccelMode.OTHER,
editable=True)
renderer.connect('accel-edited', __accel_edited_cb)
column.pack_start(renderer, True)
column.add_attribute(renderer, 'accel-mods', 0)
column.add_attribute(renderer, 'accel-key', 1)
column.add_attribute(renderer, 'keycode', 2)
accel_view.append_column(column)
it = model.append(None)
area = dlg.get_message_area()
area.pack_end(sw, True, True, 0)
sw.show_all()
id = dlg.run()
dlg.destroy()
if id != Gtk.ResponseType.OK or len(out) < 3:
return
keyval = out[0]
state = out[1]
keycode = out[2]
for name, button, mask in self.__modifier_buttons:
if state & mask:
button.set_active(True)
else:
button.set_active(False)
shortcut = Gtk.accelerator_name_with_keycode(None,
keyval,
keycode,
state)
shortcut = shortcut.replace('<Primary>', '<Control>')
self.__keycode_entry.set_text(shortcut.rsplit('>', 1)[-1])
def __add_button_clicked_cb(self, button):
shortcut = self.__get_shortcut_from_buttons()
self.add_shortcut(shortcut)
def __apply_button_clicked_cb(self, button):
shortcut = self.__get_shortcut_from_buttons()
self.__set_selected_shortcut(shortcut)
def __delete_button_clicked_cb(self, button):
self.__del_selected_shortcut()
self.__delete_button.set_sensitive(False)
self.__apply_button.set_sensitive(False)
class KeyboardShortcutSelectionDialog(Gtk.Dialog):
def __init__(self, title = None, parent = None, flags = 0, buttons = None):
super(KeyboardShortcutSelectionDialog, self).__init__(title, parent, flags, buttons)
self.__selection_view = KeyboardShortcutSelection()
self.vbox.pack_start(self.__selection_view, False, True, 0)
self.vbox.show_all()
def set_shortcuts(self, shotrcuts = None):
self.__selection_view.set_shortcuts(shotrcuts)
def add_shortcut(self, shotrcut):
self.__selection_view.add_shortcut(shotrcut)
def get_shortcuts(self):
return self.__selection_view.get_shortcuts()
if __name__ == "__main__":
dlg = KeyboardShortcutSelectionDialog(
title = "Select test",
buttons = (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
dlg.add_shortcut("Control+Shift+space")
dlg.set_shortcuts(None)
print dlg.run()
print dlg.get_shortcuts()
|