0 | |
# Copyright 2012 One Laptop Per Child
|
1 | |
#
|
2 | |
# This program is free software; you can redistribute it and/or modify
|
3 | |
# it under the terms of the GNU General Public License as published by
|
4 | |
# the Free Software Foundation; either version 2 of the License, or
|
5 | |
# (at your option) any later version.
|
6 | |
#
|
7 | |
# This program is distributed in the hope that it will be useful,
|
8 | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9 | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10 | |
# GNU General Public License for more details.
|
11 | |
#
|
12 | |
# You should have received a copy of the GNU General Public License
|
13 | |
# along with this program; if not, write to the Free Software
|
14 | |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
15 | |
|
16 | |
from gi.repository import GObject
|
17 | |
from gi.repository import Gtk
|
18 | |
|
19 | |
from sugar3.graphics.icon import Icon
|
20 | |
from sugar3.graphics import style
|
21 | |
|
22 | |
|
23 | |
class SugarMenuItem(Gtk.EventBox):
|
24 | |
|
25 | |
__gsignals__ = {
|
26 | |
'clicked': (GObject.SignalFlags.RUN_FIRST, None, [])
|
27 | |
}
|
28 | |
|
29 | |
def __init__(self, text_label='', icon_name=None):
|
30 | |
Gtk.EventBox.__init__(self)
|
31 | |
self._sensitive = True
|
32 | |
vbox = Gtk.VBox()
|
33 | |
hbox = Gtk.HBox()
|
34 | |
vbox.set_border_width(style.DEFAULT_PADDING)
|
35 | |
if icon_name is not None:
|
36 | |
self.icon = Icon()
|
37 | |
self.icon.props.icon_name = icon_name
|
38 | |
hbox.pack_start(self.icon, expand=False, fill=False,
|
39 | |
padding=style.DEFAULT_PADDING)
|
40 | |
align = Gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0)
|
41 | |
text = '<span foreground="%s">' % style.COLOR_WHITE.get_html() + \
|
42 | |
text_label + '</span>'
|
43 | |
self.label = Gtk.Label()
|
44 | |
self.label.set_use_markup(True)
|
45 | |
self.label.set_markup(text)
|
46 | |
align.add(self.label)
|
47 | |
hbox.pack_start(align, expand=True, fill=True,
|
48 | |
padding=style.DEFAULT_PADDING)
|
49 | |
vbox.pack_start(hbox, expand=False, fill=False,
|
50 | |
padding=style.DEFAULT_PADDING)
|
51 | |
self.add(vbox)
|
52 | |
self.id_bt_release_cb = self.connect('button-release-event',
|
53 | |
self.__button_release_cb)
|
54 | |
self.id_enter_notify_cb = self.connect('enter-notify-event',
|
55 | |
self.__enter_notify_cb)
|
56 | |
self.id_leave_notify_cb = self.connect('leave-notify-event',
|
57 | |
self.__leave_notify_cb)
|
58 | |
self.modify_bg(Gtk.StateType.NORMAL, style.COLOR_BLACK.get_gdk_color())
|
59 | |
self.show_all()
|
60 | |
self.set_above_child(True)
|
61 | |
|
62 | |
def __button_release_cb(self, widget, event):
|
63 | |
self.emit('clicked')
|
64 | |
|
65 | |
def __enter_notify_cb(self, widget, event):
|
66 | |
self.modify_bg(Gtk.StateType.NORMAL,
|
67 | |
style.COLOR_BUTTON_GREY.get_gdk_color())
|
68 | |
|
69 | |
def __leave_notify_cb(self, widget, event):
|
70 | |
self.modify_bg(Gtk.StateType.NORMAL, style.COLOR_BLACK.get_gdk_color())
|
71 | |
|
72 | |
def set_icon(self, icon_name):
|
73 | |
self.icon.props.icon_name = icon_name
|
74 | |
|
75 | |
def set_label(self, text_label):
|
76 | |
text = '<span foreground="%s">' % style.COLOR_WHITE.get_html() + \
|
77 | |
text_label + '</span>'
|
78 | |
self.label.set_markup(text)
|
79 | |
|
80 | |
def set_sensitive(self, sensitive):
|
81 | |
if self._sensitive == sensitive:
|
82 | |
return
|
83 | |
|
84 | |
self._sensitive = sensitive
|
85 | |
if sensitive:
|
86 | |
self.handler_unblock(self.id_bt_release_cb)
|
87 | |
self.handler_unblock(self.id_enter_notify_cb)
|
88 | |
self.handler_unblock(self.id_leave_notify_cb)
|
89 | |
else:
|
90 | |
self.handler_block(self.id_bt_release_cb)
|
91 | |
self.handler_block(self.id_enter_notify_cb)
|
92 | |
self.handler_block(self.id_leave_notify_cb)
|
93 | |
self.modify_bg(Gtk.StateType.NORMAL,
|
94 | |
style.COLOR_BLACK.get_gdk_color())
|