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

Daniel Francis francis at sugarlabs.org
Wed Aug 1 22:10:48 EDT 2012


Signed-off-by: Daniel Francis <francis at sugarlabs.org>

---
 src/sugar3/graphics/toggletoolbutton.py | 39 +++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/sugar3/graphics/toggletoolbutton.py b/src/sugar3/graphics/toggletoolbutton.py
index 94cc6ae..f3a9c57 100644
--- a/src/sugar3/graphics/toggletoolbutton.py
+++ b/src/sugar3/graphics/toggletoolbutton.py
@@ -19,6 +19,8 @@
 STABLE.
 """
 
+import logging
+
 from gi.repository import GObject
 from gi.repository import Gtk
 
@@ -26,6 +28,33 @@ 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.
+    tool_button.get_child().add_accelerator('clicked', accel_group, keyval, mask,
+                                      Gtk.AccelFlags.LOCKED | Gtk.AccelFlags.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 +101,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