diff --git a/src/sugar3/graphics/toggletoolbutton.py b/src/sugar3/graphics/toggletoolbutton.py index 94cc6aef..c8653323 100644 --- a/src/sugar3/graphics/toggletoolbutton.py +++ b/src/sugar3/graphics/toggletoolbutton.py @@ -1,4 +1,5 @@ # Copyright (C) 2007, Red Hat, Inc. +# Copyright (C) 2012, Daniel Francis # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -19,6 +20,8 @@ STABLE. """ +import logging + from gi.repository import GObject from gi.repository import Gtk @@ -26,6 +29,34 @@ from sugar3.graphics.icon import Icon from sugar3.graphics.palette import Palette, ToolInvoker +def _add_accelerator(tool_button): + if not tool_button.props.accelerator or not tool_button.get_toplevel() or \ + not tool_button.get_child(): + return + + # TODO: should we remove the accelerator from the prev top level? + if not hasattr(tool_button.get_toplevel(), 'sugar_accel_group'): + logging.warning('No Gtk.AccelGroup in the top level window.') + return + + accel_group = tool_button.get_toplevel().sugar_accel_group + keyval, mask = Gtk.accelerator_parse(tool_button.props.accelerator) + # the accelerator needs to be set at the child, so the Gtk.AccelLabel + # in the palette can pick it up. + accel_flags = Gtk.AccelFlags.LOCKED | Gtk.AccelFlags.VISIBLE + tool_button.get_child().add_accelerator('clicked', accel_group, + keyval, mask, accel_flags) + + +def _hierarchy_changed_cb(tool_button, previous_toplevel): + _add_accelerator(tool_button) + + +def setup_accelerator(tool_button): + _add_accelerator(tool_button) + tool_button.connect('hierarchy-changed', _hierarchy_changed_cb) + + class ToggleToolButton(Gtk.ToggleToolButton): __gtype_name__ = 'SugarToggleToolButton' @@ -72,6 +103,16 @@ class ToggleToolButton(Gtk.ToggleToolButton): def set_tooltip(self, text): self.set_palette(Palette(text)) + def set_accelerator(self, accelerator): + self._accelerator = accelerator + setup_accelerator(self) + + def get_accelerator(self): + return self._accelerator + + accelerator = GObject.property(type=str, setter=set_accelerator, + getter=get_accelerator) + def do_expose_event(self, event): allocation = self.get_allocation() child = self.get_child()