From 428d648fe3e8dea574e0b9df03337f220eb6d84a Mon Sep 17 00:00:00 2001 From: Daniel Francis Date: Tue, 7 Aug 2012 10:04:47 +0200 Subject: [PATCH] Add accelerator functionality to the ToggleToolButton, SL #3774 This code is copying the code from the ToolButton. Signed-off-by: Daniel Francis --- src/sugar3/graphics/toggletoolbutton.py | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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()