def init_tools(self): #the following is copied from windowactivatable.py and modified as necessary i = 1 # counting tool actions accel_counter = 1 # counting tool actions without custom accel for tool in self._tool_preferences.tools: # hopefully unique action name name = "Tool%sAction" % i actionlink = "win." + name item = Gio.MenuItem.new(_(tool.label), actionlink) item.set_attribute_value("hidden-when", GLib.Variant.new_string("action-disabled")) self.latex_tools_menu.append_item(item) accelerator = None if tool.accelerator and len(tool.accelerator) > 0: key,mods = Gtk.accelerator_parse(tool.accelerator) if Gtk.accelerator_valid(key,mods): accelerator = tool.accelerator if not accelerator: accelerator = "<Ctrl><Alt>%s" % accel_counter accel_counter += 1 self.app.add_accelerator(accelerator, actionlink, None) i += 1
def parse_accel(self, accel: str) -> Optional[Tuple[int, int]]: """Convert an :ref:`accelerator string <keybinding-syntax>` into the form XGrabKey_ needs. :param accel: The accelerator string. :returns: ``(keycode, modifier_mask)`` or :any:`None` on failure. """ keysym, modmask = Gtk.accelerator_parse(accel) if not Gtk.accelerator_valid(keysym, modmask): logging.error("Invalid keybinding: %s", accel) return None if modmask > 2**16 - 1: logging.error("Modifier out of range for XGrabKey " "(int(modmask) > 65535). " "Did you use <Super> instead of <Mod4>?") return None # Convert to what XGrabKey expects keycode = self.xdisp.keysym_to_keycode(keysym) if isinstance(modmask, Gdk.ModifierType): modmask = modmask.real return keycode, modmask
def do_key_press_event(self, event): """Handles key press events and detects valid accelerators.""" keyval = event.keyval mask = event.state if keyval == Gdk.KEY_Escape: self.destroy() return self.accelerator = Gtk.accelerator_name(keyval, mask) accelerator = Gtk.accelerator_get_label(keyval, mask) self.accelerator_label.set_markup( "<span size='20000'><b>%s</b></span>" % accelerator) valid = Gtk.accelerator_valid(keyval, mask) self.conflicting_action = self.app.shortcuts.get_conflicting_action( self.customised_item.action_name, keyval, mask) if valid and self.conflicting_action: title = self.app.shortcuts.titles[self.conflicting_action] self.conflict_label.set_markup( _("This key combination is already used by <b>%s</b>." " Press Replace to use it for <b>%s</b> instead.") % (title, self.customised_item.title)) # Set visibility according to the booleans set above. self.apply_button.set_visible(valid and not bool(self.conflicting_action)) self.accelerator_label.set_visible(valid) self.conflict_label.set_visible(valid and bool(self.conflicting_action)) self.replace_button.set_visible(valid and bool(self.conflicting_action)) self.invalid_label.set_visible(not valid)
def init_tools(self): #the following is copied from windowactivatable.py and modified as necessary i = 1 # counting tool actions accel_counter = 1 # counting tool actions without custom accel for tool in self._tool_preferences.tools: # hopefully unique action name name = "Tool%sAction" % i actionlink = "win." + name item = Gio.MenuItem.new(_(tool.label), actionlink) item.set_attribute_value( "hidden-when", GLib.Variant.new_string("action-disabled")) self.latex_tools_menu.append_item(item) accelerator = None if tool.accelerator and len(tool.accelerator) > 0: key, mods = Gtk.accelerator_parse(tool.accelerator) if Gtk.accelerator_valid(key, mods): accelerator = tool.accelerator if not accelerator: accelerator = "<Ctrl><Alt>%s" % accel_counter accel_counter += 1 self.app.add_accelerator(accelerator, actionlink, None) i += 1
def do_key_press_event(self, event): """Handles key press events and detects valid accelerators.""" keyval = event.keyval mask = event.state if keyval == Gdk.KEY_Escape: self.destroy() return self.accelerator = Gtk.accelerator_name(keyval, mask) accelerator = Gtk.accelerator_get_label(keyval, mask) self.accelerator_label.set_markup("<span size='20000'><b>%s</b></span>" % accelerator) valid = Gtk.accelerator_valid(keyval, mask) self.conflicting_action = self.app.shortcuts.get_conflicting_action( self.customised_item.action_name, keyval, mask) if valid and self.conflicting_action: title = self.app.shortcuts.titles[self.conflicting_action] self.conflict_label.set_markup( _("This key combination is already used by <b>%s</b>." " Press Replace to use it for <b>%s</b> instead.") % (title, self.customised_item.title)) # Set visibility according to the booleans set above. self.apply_button.set_visible(valid and not bool(self.conflicting_action)) self.accelerator_label.set_visible(valid) self.conflict_label.set_visible(valid and bool(self.conflicting_action)) self.replace_button.set_visible(valid and bool(self.conflicting_action)) self.invalid_label.set_visible(not valid)
def add(self, accel, callback, data=None): num = len(accel.accelerators) mapping = self.accelerators for i in range(num): parsed = Gtk.accelerator_parse(accel.accelerators[i]) if not Gtk.accelerator_valid(*parsed): return named = Gtk.accelerator_name(*parsed) inmap = named in mapping if i == num - 1 and inmap: # Last one cannot be in the map return elif inmap and isinstance(mapping[named], AccelCallback): # It's already mapped... return else: if not inmap: mapping[named] = {} if i == num - 1: mapping[named] = AccelCallback(accel, callback, data) mapping = mapping[named]
def try_to_show(self): accel_name = self.read_accel_from_file() if accel_name != "": key, mods = Gtk.accelerator_parse(accel_name) if Gtk.accelerator_valid(key, mods): self.accel_name = self.accel_to_qt(accel_name) self.entry.set_text(self.accel_name)
def on_key_press_event(self, widget, event): mods = event.get_state() & (Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK | Gdk.ModifierType.SUPER_MASK | Gdk.ModifierType.HYPER_MASK | Gdk.ModifierType.META_MASK) if event.keyval in (Gdk.KEY_Escape, Gdk.KEY_Return) and not mods: if event.keyval == Gdk.KEY_Escape: self.emit("changed", self.key, self.mods) self.end_key_grab() self.set_label() return key = Gdk.keyval_to_lower(event.keyval) if (key == Gdk.KEY_ISO_Left_Tab): key = Gdk.KEY_Tab if Gtk.accelerator_valid(key, mods) or (key == Gdk.KEY_Tab and mods): self.set_label(key, mods) self.end_key_grab() self.key = key self.mods = mods self.emit("changed", self.key, self.mods) return self.set_label(key, mods)
def _init_tool_actions(self): """ - Load defined Tools - create and init ToolActions from them - hook them in the window UI - create a map from extensions to lists of ToolActions """ items_ui = "" self._action_handlers = {} # this is used for enable/disable actions by name (None = every extension) self._tool_action_extensions = {None: []} self._tool_action_group = Gtk.ActionGroup("LaTeXPluginToolActions") i = 1 # counting tool actions accel_counter = 1 # counting tool actions without custom accel for tool in self._tool_preferences.tools: # hopefully unique action name name = "Tool%sAction" % i # update extension-tool mapping for extension in tool.extensions: try: self._tool_action_extensions[extension].append(name) except KeyError: # extension not yet mapped self._tool_action_extensions[extension] = [name] # create action action = ToolAction(tool) gtk_action = Gtk.Action(name, action.label, action.tooltip, action.stock_id) self._action_handlers[gtk_action] = gtk_action.connect("activate", lambda gtk_action, action: action.activate(self._window_context), action) # create simple actions to be used by menu (created in appactivatable.py) simpleaction = Gio.SimpleAction(name=name) simpleaction.connect("activate", lambda _a, _b, action: action.activate(self._window_context), action) self.window.add_action(simpleaction) accelerator = None if tool.accelerator and len(tool.accelerator) > 0: key,mods = Gtk.accelerator_parse(tool.accelerator) if Gtk.accelerator_valid(key,mods): accelerator = tool.accelerator if not accelerator: accelerator = "<Ctrl><Alt>%s" % accel_counter accel_counter += 1 self._tool_action_group.add_action_with_accel(gtk_action, accelerator) # add UI definition items_ui += """<menuitem action="%s" />""" % name i += 1 items_ui +="""<separator/>""" tool_ui = self._tool_ui_template.substitute({ "items": items_ui, "toolbar_name": self._toolbar_name}) self._ui_manager.insert_action_group(self._tool_action_group, -1) self._tool_ui_id = self._ui_manager.add_ui_from_string(tool_ui)
def dialog_key_press_cb(self, source, event): keyval = event.keyval modifier_mask = event.state & gtk.accelerator_get_default_mod_mask() if gtk.accelerator_valid(keyval, modifier_mask): self.set_value(gtk.accelerator_name(keyval, modifier_mask)) self.emit('changed') gdk.Device.ungrab(gtk.get_current_event_device(), gtk.get_current_event_time()) self.dialog.hide()
def on_dialoggetkey_key_release_event(self, widget, event): if not self._press_time: return keyval, state = self.translate_keyboard_event(widget, event) self.update_accelerator_label(0, 0) state = Gdk.ModifierType(state) if Gtk.accelerator_valid(keyval, state): key = Gtk.accelerator_name(keyval, state) if (self._previous_key is not None and key == self._previous_key): self.return_cancel() return if self._check_callback is None or self._check_callback(key): self._return(key) else: self.imagekeybindingaux.show() self.labelkeybindingaux.show()
def remove_real(self, accelerators, accels): if not accels: return parsed = Gtk.accelerator_parse(accels[0]) if not Gtk.accelerator_valid(*parsed): return named = Gtk.accelerator_name(*parsed) if not named in accelerators: return if len(accels) == 1: del accelerators[named] else: self.remove_real(accelerators[named], accels[1:]) if not accelerators[named]: del accelerators[named]
def init_snippet_data(self, node): if node == None: return self.override = node.attrib.get('override') self.properties = {} props = SnippetData.PROPS.copy() # Store all properties present for child in node: if child.tag in props: del props[child.tag] # Normalize accelerator if child.tag == 'accelerator' and child.text != None: keyval, mod = Gtk.accelerator_parse(child.text) if Gtk.accelerator_valid(keyval, mod): child.text = Gtk.accelerator_name(keyval, mod) else: child.text = '' if self.can_modify(): self.properties[child.tag] = child else: self.properties[child.tag] = child.text or '' # Create all the props that were not found so we stay consistent for prop in props: if self.can_modify(): child = et.SubElement(node, prop) child.text = props[prop] self.properties[prop] = child else: self.properties[prop] = props[prop] self.check_validation()
def on_key_press_event(self, widget, event): #mods = event.get_state() & Gtk.accelerator_get_default_mod_mask() mods = event.get_state() if event.keyval in (Gdk.KEY_Escape, Gdk.KEY_Return) and not mods: if event.keyval == Gdk.KEY_Escape: self.emit("changed", self.key, self.mods) self.end_key_grab() self.set_label() return key = Gdk.keyval_to_lower(event.keyval) if (key == Gdk.KEY_ISO_Left_Tab): key = Gdk.KEY_Tab if Gtk.accelerator_valid(key, mods) or (key == Gdk.KEY_Tab and mods): self.set_label(key, mods) self.end_key_grab() self.key = key self.mods = mods self.emit("changed", self.key, self.mods) return self.set_label(key, mods)
def _edit_dialog_key_press_cb(self, dialog, event, editable): if event.type != Gdk.EventType.KEY_PRESS: return False if event.is_modifier: return False if self._USE_NORMAL_DIALOG_KEYS: if event.keyval == Gdk.KEY_Return: dialog.response(Gtk.ResponseType.OK) return True elif event.keyval == Gdk.KEY_Escape: dialog.response(Gtk.ResponseType.CANCEL) return True elif event.keyval == Gdk.KEY_BackSpace: dialog.response(Gtk.ResponseType.REJECT) return True # Stolen from GTK 2.24's gtk/gtkmenu.c (gtk_menu_key_press()) # Figure out what modifiers went into determining the key symbol keymap = Gdk.Keymap.get_default() bound, keyval, effective_group, level, consumed_modifiers = ( keymap.translate_keyboard_state( event.hardware_keycode, event.state, event.group, )) keyval = Gdk.keyval_to_lower(keyval) mods = Gdk.ModifierType( event.state & Gtk.accelerator_get_default_mod_mask() & ~consumed_modifiers) # If lowercasing affects the keysym, then we need to include # SHIFT in the modifiers. We re-upper case when we match against # the keyval, but display and save in caseless form. if keyval != event.keyval: mods |= Gdk.ModifierType.SHIFT_MASK accel_label = Gtk.accelerator_get_label(keyval, mods) # So we get (<Shift>j, Shift+J) but just (plus, +). As I # understand it. if not Gtk.accelerator_valid(keyval, mods): return True clash_accel_path = None clash_action_label = None for path, kv, m, changed in self._get_accel_map_entries(): if (kv, m) == (keyval, mods): clash_accel_path = path clash_action_label = self._action_labels.get( clash_accel_path, _("Unknown Action"), ) break if clash_accel_path == dialog.accel_path: # no change self._edit_dialog_set_standard_hint(dialog) label = str(accel_label) dialog.accel_label_widget.set_text(label) elif clash_accel_path: markup_tmpl = _( "<b>{accel} is already in use for '{action}'. " "The existing assignment will be replaced.</b>" ) markup = markup_tmpl.format( accel=lib.xml.escape(accel_label), action=lib.xml.escape(clash_action_label), ) self._edit_dialog_set_hint(dialog, markup) label = "%s (replace)" % (accel_label,) dialog.accel_label_widget.set_text(str(label)) else: self._edit_dialog_set_standard_hint(dialog) label = "%s (changed)" % (accel_label,) dialog.accel_label_widget.set_text(label) dialog.result_mods = mods dialog.result_keyval = keyval return True
def _edit_dialog_key_press_cb(self, dialog, event, editable): if event.type != Gdk.EventType.KEY_PRESS: return False if event.is_modifier: return False if self._USE_NORMAL_DIALOG_KEYS: if event.keyval == Gdk.KEY_Return: dialog.response(Gtk.ResponseType.OK) return True elif event.keyval == Gdk.KEY_Escape: dialog.response(Gtk.ResponseType.CANCEL) return True elif event.keyval == Gdk.KEY_BackSpace: dialog.response(Gtk.ResponseType.REJECT) return True # Stolen from GTK 2.24's gtk/gtkmenu.c (gtk_menu_key_press()) # Figure out what modifiers went into determining the key symbol keymap = Gdk.Keymap.get_default() bound, keyval, effective_group, level, consumed_modifiers = ( keymap.translate_keyboard_state( event.hardware_keycode, event.state, event.group, )) keyval = Gdk.keyval_to_lower(keyval) mods = Gdk.ModifierType( event.state & Gtk.accelerator_get_default_mod_mask() & ~consumed_modifiers) # If lowercasing affects the keysym, then we need to include # SHIFT in the modifiers. We re-upper case when we match against # the keyval, but display and save in caseless form. if keyval != event.keyval: mods |= Gdk.ModifierType.SHIFT_MASK accel_label = Gtk.accelerator_get_label(keyval, mods) # So we get (<Shift>j, Shift+J) but just (plus, +). As I # understand it. if not Gtk.accelerator_valid(keyval, mods): return True clash_accel_path = None clash_action_label = None for path, kv, m, changed in self._get_accel_map_entries(): if (kv, m) == (keyval, mods): clash_accel_path = path clash_action_label = self._action_labels.get( clash_accel_path, _("Unknown Action"), ) break if clash_accel_path == dialog.accel_path: # no change self._edit_dialog_set_standard_hint(dialog) label = str(accel_label) dialog.accel_label_widget.set_text(label) elif clash_accel_path: markup_tmpl = _( "<b>{accel} is already in use for '{action}'. " "The existing assignment will be replaced.</b>" ) markup = markup_tmpl.format( accel=lib.xml.escape(accel_label.decode("utf-8")), action=lib.xml.escape(clash_action_label.decode("utf-8")), ) self._edit_dialog_set_hint(dialog, markup) label = u"%s (replace)" % (accel_label,) dialog.accel_label_widget.set_text(str(label)) else: self._edit_dialog_set_standard_hint(dialog) label = u"%s (changed)" % (accel_label,) dialog.accel_label_widget.set_text(label) dialog.result_mods = mods dialog.result_keyval = keyval return True
def __valid__(self): return gtk.accelerator_valid(*(gtk.accelerator_parse(self.value)))
def _init_tool_actions(self): """ - Load defined Tools - create and init ToolActions from them - hook them in the window UI - create a map from extensions to lists of ToolActions """ items_ui = "" self._action_handlers = {} # this is used for enable/disable actions by name (None = every extension) self._tool_action_extensions = {None: []} self._tool_action_group = Gtk.ActionGroup("LaTeXPluginToolActions") i = 1 # counting tool actions accel_counter = 1 # counting tool actions without custom accel for tool in self._tool_preferences.tools: # hopefully unique action name name = "Tool%sAction" % i # update extension-tool mapping for extension in tool.extensions: try: self._tool_action_extensions[extension].append(name) except KeyError: # extension not yet mapped self._tool_action_extensions[extension] = [name] # create action action = ToolAction(tool) gtk_action = Gtk.Action(name, action.label, action.tooltip, action.stock_id) self._action_handlers[gtk_action] = gtk_action.connect( "activate", lambda gtk_action, action: action.activate( self._window_context), action) accelerator = None if tool.accelerator and len(tool.accelerator) > 0: key, mods = Gtk.accelerator_parse(tool.accelerator) if Gtk.accelerator_valid(key, mods): accelerator = tool.accelerator if not accelerator: accelerator = "<Ctrl><Alt>%s" % accel_counter accel_counter += 1 self._tool_action_group.add_action_with_accel( gtk_action, accelerator) # add UI definition items_ui += """<menuitem action="%s" />""" % name i += 1 items_ui += """<separator/>""" tool_ui = self._tool_ui_template.substitute({ "items": items_ui, "toolbar_name": self._toolbar_name }) self._ui_manager.insert_action_group(self._tool_action_group, -1) self._tool_ui_id = self._ui_manager.add_ui_from_string(tool_ui)