Add accelerator support to menu items
This commit is contained in:
parent
5a0f575718
commit
33e94723da
@ -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)
|
||||
|
||||
|
@ -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())
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user