
This is only on a best-effort basis; the code will be in a broken state after this patch and need to be fixed manually. The purpose of committing the intermediate, non-working output is to make it reproducible. It's impractical to manually review the changes. The exact version used was 4f637212f13b197a95c824967a58496b9e3b877c from the main pygobject repository [1] plus a custom patch [2] that hasn't been sent upstream yet. [1] git://git.gnome.org/pygobject [2] https://sascha.silbe.org/patches/pygobject-convert-sugar-20111122.patch Signed-off-by: Sascha Silbe <silbe@activitycentral.com>
96 lines
3.2 KiB
Python
96 lines
3.2 KiB
Python
# Copyright (C) 2007, Eduardo Silva <edsiper@gmail.com>
|
|
#
|
|
# This library is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
# License as published by the Free Software Foundation; either
|
|
# version 2 of the License, or (at your option) any later version.
|
|
#
|
|
# This library is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
# Lesser General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
# License along with this library; if not, write to the
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
"""
|
|
STABLE.
|
|
"""
|
|
|
|
import logging
|
|
|
|
from gi.repository import GObject
|
|
from gi.repository import Pango
|
|
from gi.repository import Gtk
|
|
|
|
from sugar3.graphics.icon import Icon
|
|
|
|
|
|
class MenuItem(Gtk.ImageMenuItem):
|
|
|
|
def __init__(self, text_label=None, icon_name=None, text_maxlen=60,
|
|
xo_color=None, file_name=None):
|
|
GObject.GObject.__init__(self)
|
|
self._accelerator = None
|
|
|
|
label = Gtk.AccelLabel(label=text_label)
|
|
label.set_alignment(0.0, 0.5)
|
|
label.set_accel_widget(self)
|
|
if text_maxlen > 0:
|
|
label.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
|
|
label.set_max_width_chars(text_maxlen)
|
|
self.add(label)
|
|
label.show()
|
|
|
|
if icon_name is not None:
|
|
icon = Icon(icon_name=icon_name,
|
|
icon_size=Gtk.IconSize.SMALL_TOOLBAR)
|
|
if xo_color is not None:
|
|
icon.props.xo_color = xo_color
|
|
self.set_image(icon)
|
|
icon.show()
|
|
|
|
elif file_name is not None:
|
|
icon = Icon(file=file_name, icon_size=Gtk.IconSize.SMALL_TOOLBAR)
|
|
if xo_color is not None:
|
|
icon.props.xo_color = xo_color
|
|
self.set_image(icon)
|
|
icon.show()
|
|
|
|
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.AccelFlags.LOCKED | Gtk.AccelFlags.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)
|