[Sugar-devel] [PATCH sugar-toolkit (gtk2)] props.accelerator available at ToggleToolButton SL#3774

Daniel Francis francis at sugarlabs.org
Wed Aug 1 22:11:41 EDT 2012


Signed-off-by: Daniel Francis <francis at sugarlabs.org>
---
 src/sugar/graphics/toggletoolbutton.py | 40 ++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/sugar/graphics/toggletoolbutton.py b/src/sugar/graphics/toggletoolbutton.py
index 012c2b0..63d843f 100644
--- a/src/sugar/graphics/toggletoolbutton.py
+++ b/src/sugar/graphics/toggletoolbutton.py
@@ -19,6 +19,8 @@
 STABLE.
 """
 
+import logging
+
 import gobject
 import gtk
 
@@ -26,6 +28,34 @@ from sugar.graphics.icon import Icon
 from sugar.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.child:
+        return
+
+    # TODO: should we remove the accelerator from the prev top level?
+
+    accel_group = tool_button.get_toplevel().get_data('sugar-accel-group')
+    if not accel_group:
+        logging.warning('No gtk.AccelGroup in the top level window.')
+        return
+
+    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.
+    tool_button.child.add_accelerator('clicked', accel_group, keyval, mask,
+                                      gtk.ACCEL_LOCKED | gtk.ACCEL_VISIBLE)
+
+
+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 +102,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()
-- 
1.7.11.2



More information about the Sugar-devel mailing list