Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/graphics/menuitem.py
blob: fc77ba05ad1378c17277b606e1012be5df7abac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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

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):
        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()

        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)