# simple one child container in GTK+ 3

from gi.repository import Gtk
from gi.repository import Gdk

from sugar3.graphics.icon import Icon
from sugar3.graphics import style
from sugar3.graphics.xocolor import XoColor

class CanvasIcon(Gtk.EventBox):
    def __init__(self, icon_name='computer-xo'):
        Gtk.EventBox.__init__(self)
        self.set_above_child(True)
        self.set_visible_window(False)
        self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
                        Gdk.EventMask.ENTER_NOTIFY_MASK |
                        Gdk.EventMask.LEAVE_NOTIFY_MASK)
        self._icon_name = icon_name
        self._xo_color = None
        self._xo = Icon(pixel_size=style.XLARGE_ICON_SIZE,
                        icon_name=icon_name)
        self.connect('button-press-event', self._button_press_cb)
        self.connect('enter-notify-event', self._enter_notify_cb)
        self.connect('leave-notify-event', self._leave_notify_cb)
        self.add(self._xo)
        self._xo.show()

    def _enter_notify_cb(self, widget, event):
        print 'enter ', self._icon_name

    def _leave_notify_cb(self, widget, event):
        print 'leave ', self._icon_name

    def _button_press_cb(self, widget, event):
        if event.button == 1 and event.type == Gdk.EventType.BUTTON_PRESS:
            print 'pressed ', self._icon_name


class FavoritesContainer(Gtk.Container):
    __gtype_name__ = 'SugarFavoritesContainer'

    def __init__(self, **kwargs):
        Gtk.Container.__init__(self, **kwargs)
        self.set_has_window(False)
        self.child = None

    def do_add(self, widget):
        if self.child is None:
            widget.set_parent(self)
            self.child = widget

    def do_remove(self, widget):
        was_visible = widget.get_visible()
        if self.child == widget:
            widget.unparent()
            if was_visible and self.get_visible():
                self.queue_resize()

    def do_size_allocate(self, allocation):
        self.allocation = allocation
        if self.child:
            # Obtain the preferred size of the widget
            icon_allocation = Gdk.Rectangle()
            minimum, natural = self.get_preferred_size()
            icon_allocation.x = 0
            icon_allocation.y = 0
            icon_allocation.width = natural.width
            icon_allocation.height = natural.height
            # assign a size and position to the child widget
            self.child.size_allocate(icon_allocation)

    def do_forall(self, include_internals, callback, callback_data):
        if self.child:
            print include_internals, callback, callback_data
            callback(self.child, callback_data)

def _destroy_cb(widget, data=None):
    Gtk.main_quit()

Gtk.IconTheme.get_default().append_search_path('icons')
window = Gtk.Window()
window.set_default_size(450, 450)
window.set_border_width(0)
window.connect("destroy", _destroy_cb)

favorites_container = FavoritesContainer()

icon = CanvasIcon(icon_name='activity-read')
favorites_container.add(icon)
icon.show()

window.add(favorites_container)
favorites_container.show()
window.show()

Gtk.main()
