From 33e94723da52e34b5df0524f5b98e49fe4c9c238 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Fri, 5 Dec 2008 11:51:40 +0100 Subject: [PATCH] Add accelerator support to menu items --- src/sugar/graphics/menuitem.py | 59 ++++++++++++++++++++++++++++++---- src/sugar/graphics/palette.py | 4 +++ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/sugar/graphics/menuitem.py b/src/sugar/graphics/menuitem.py index 70c08f78..fc77ba05 100644 --- a/src/sugar/graphics/menuitem.py +++ b/src/sugar/graphics/menuitem.py @@ -19,20 +19,65 @@ STABLE. """ -import gtk -from sugar.graphics.icon import Icon +import logging +import gobject import pango +import gtk + +from sugar.graphics.icon import Icon class MenuItem(gtk.ImageMenuItem): def __init__(self, text_label=None, icon_name=None, text_maxlen=0): - gtk.ImageMenuItem.__init__(self, text_label) + gobject.GObject.__init__(self) + self._accelerator = None + + label = gtk.AccelLabel(text_label) + label.set_alignment(0.0, 0.5) + label.set_accel_widget(self) + if text_maxlen > 0: + label.set_ellipsize(pango.ELLIPSIZE_MIDDLE) + label.set_max_width_chars(text_maxlen) + self.add(label) + label.show() + if icon_name: icon = Icon(icon_name=icon_name, icon_size=gtk.ICON_SIZE_MENU) self.set_image(icon) icon.show() - if text_maxlen > 0: - child = self.get_child() - child.set_ellipsize(pango.ELLIPSIZE_MIDDLE) - child.set_max_width_chars(text_maxlen) + self.connect('can-activate-accel', self.__can_activate_accel_cb) + self.connect('hierarchy-changed', self.__hierarchy_changed_cb) + + def __hierarchy_changed_cb(self, widget, previous_toplevel): + self._add_accelerator() + + def __can_activate_accel_cb(self, widget, signal_id): + # Accept activation via accelerators regardless of this widget's state + return True + + def _add_accelerator(self): + if self._accelerator is None or self.get_toplevel() is None: + return + + # TODO: should we remove the accelerator from the prev top level? + + accel_group = self.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(self._accelerator) + self.add_accelerator('activate', accel_group, keyval, mask, + gtk.ACCEL_LOCKED | gtk.ACCEL_VISIBLE) + + def set_accelerator(self, accelerator): + self._accelerator = accelerator + self._add_accelerator() + + def get_accelerator(self): + return self._accelerator + + accelerator = gobject.property(type=str, setter=set_accelerator, + getter=get_accelerator) + diff --git a/src/sugar/graphics/palette.py b/src/sugar/graphics/palette.py index 706bc1c8..5ddaca8b 100644 --- a/src/sugar/graphics/palette.py +++ b/src/sugar/graphics/palette.py @@ -230,6 +230,10 @@ class Palette(gtk.Window): # Just assume xthickness and ythickness are the same self.set_border_width(self.get_style().xthickness) + accel_group = gtk.AccelGroup() + self.set_data('sugar-accel-group', accel_group) + self.add_accel_group(accel_group) + primary_box.set_size_request(-1, style.zoom(style.GRID_CELL_SIZE) - 2 * self.get_border_width())