Fix #4856 and cleanup of the alert code

This commit is contained in:
Simon Schampijer 2008-02-17 13:40:14 +01:00
parent fc3b70b366
commit 324a9b549d

View File

@ -27,7 +27,7 @@ from sugar.graphics.icon import Icon
_ = lambda msg: gettext.dgettext('sugar', msg)
class Alert(gtk.EventBox, gobject.GObject):
class Alert(gtk.EventBox):
"""UI interface for Alerts
Alerts are used inside the activity window instead of being a
@ -60,42 +60,43 @@ class Alert(gtk.EventBox, gobject.GObject):
}
def __init__(self, **kwargs):
gobject.GObject.__init__(self)
self.set_visible_window(True)
self._hbox = gtk.HBox()
self._hbox.set_border_width(style.DEFAULT_SPACING)
self._hbox.set_spacing(style.DEFAULT_SPACING)
self.add(self._hbox)
self._title = None
self._msg = None
self._icon = None
self._buttons = {}
self._hbox = gtk.HBox()
self._hbox.set_border_width(style.DEFAULT_SPACING)
self._hbox.set_spacing(style.DEFAULT_SPACING)
self._msg_box = gtk.VBox()
self._title_label = gtk.Label()
self._title_label.set_alignment(0, 0.5)
self._msg_box.pack_start(self._title_label, False)
self._title_label.show()
self._msg_label = gtk.Label()
self._msg_label.set_alignment(0, 0.5)
self._msg_box.pack_start(self._msg_label, False)
self._hbox.pack_start(self._msg_box, False)
self._msg_label.show()
self._buttons_box = gtk.HButtonBox()
self._buttons_box.set_layout(gtk.BUTTONBOX_END)
self._buttons_box.set_spacing(style.DEFAULT_SPACING)
self._hbox.pack_start(self._buttons_box)
self._buttons_box.show()
gtk.EventBox.__init__(self, **kwargs)
self.set_visible_window(True)
self.add(self._hbox)
self._title_label.show()
self._msg_label.show()
self._buttons_box.show()
self._msg_box.show()
self._hbox.show()
self.show()
def do_set_property(self, pspec, value):
def do_set_property(self, pspec, value):
if pspec.name == 'title':
if self._title != value:
self._title = value
@ -139,17 +140,17 @@ class Alert(gtk.EventBox, gobject.GObject):
return button
def remove_button(self, response_id):
"""Remove a button from the alert by the given button id"""
self._buttons_box.remove(self._buttons[id])
"""Remove a button from the alert by the given response id"""
self._buttons_box.remove(self._buttons[response_id])
def _response(self, id):
def _response(self, response_id):
"""Emitting response when we have a result
A result can be that a user has clicked a button or
a timeout has occured, the id identifies the button
that has been clicked and -1 for a timeout
"""
self.emit('response', id)
self.emit('response', response_id)
def __button_clicked_cb(self, button, response_id):
self._response(response_id)
@ -162,15 +163,16 @@ class ConfirmationAlert(Alert):
Alert.__init__(self, **kwargs)
icon = Icon(icon_name='dialog-cancel')
cancel_button = self.add_button(gtk.RESPONSE_CANCEL, _('Cancel'), icon)
self.add_button(gtk.RESPONSE_CANCEL, _('Cancel'), icon)
icon.show()
icon = Icon(icon_name='dialog-ok')
ok_button = self.add_button(gtk.RESPONSE_OK, _('Ok'), icon)
self.add_button(gtk.RESPONSE_OK, _('Ok'), icon)
icon.show()
class _TimeoutIcon(hippo.CanvasText, hippo.CanvasItem):
"""An icon with a round border"""
__gtype_name__ = 'AlertTimeoutIcon'
def __init__(self, **kwargs):
@ -183,12 +185,12 @@ class _TimeoutIcon(hippo.CanvasText, hippo.CanvasItem):
def do_paint_background(self, cr, damaged_box):
[width, height] = self.get_allocation()
x = width * 0.5
y = height * 0.5
xval = width * 0.5
yval = height * 0.5
radius = min(width * 0.5, height * 0.5)
hippo.cairo_set_source_rgba32(cr, self.props.background_color)
cr.arc(x, y, radius, 0, 2*math.pi)
cr.arc(xval, yval, radius, 0, 2*math.pi)
cr.fill_preserve()
@ -204,7 +206,7 @@ class TimeoutAlert(Alert):
self._timeout = timeout
icon = Icon(icon_name='dialog-cancel')
cancel_button = self.add_button(gtk.RESPONSE_CANCEL, _('Cancel'), icon)
self.add_button(gtk.RESPONSE_CANCEL, _('Cancel'), icon)
icon.show()
self._timeout_text = _TimeoutIcon(
@ -242,7 +244,7 @@ class NotifyAlert(Alert):
canvas = hippo.Canvas()
canvas.set_root(self._timeout_text)
canvas.show()
self.add_button(gtk.RESPONSE_OK, _('OK'), canvas)
self.add_button(gtk.RESPONSE_OK, _('Ok'), canvas)
gobject.timeout_add(1000, self.__timeout)