From 845371f2bc9140ba0c7e04cb994ebb8c3a86df64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Wed, 10 Oct 2012 16:20:05 -0300 Subject: [PATCH] Icon: add new class CanvasIcon - SL #3989 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move code from shell ActivityIcon to new class CanvasIcon, that will allow to reuse it in other icons. This class inherits EventIcon and adds state and drawing handling. The right-click callback for the palette invoker is not needed. Signed-off-by: Manuel QuiƱones Acked-by: Simon Schampijer --- src/sugar3/graphics/icon.py | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/sugar3/graphics/icon.py b/src/sugar3/graphics/icon.py index 9008f3fb..2d9c5314 100644 --- a/src/sugar3/graphics/icon.py +++ b/src/sugar3/graphics/icon.py @@ -679,6 +679,71 @@ class EventIcon(Gtk.EventBox): self.set_palette(Palette(text)) +class CanvasIcon(EventIcon): + """ + An EventIcon with active and prelight states, and a styleable + background. + + If the icon pops up a palette, the prelight state is set until the + palette pops down. + + """ + + __gtype_name__ = 'SugarCanvasIcon' + + def __init__(self, **kwargs): + EventIcon.__init__(self, **kwargs) + + self._in_prelight_state = False + + self.connect('enter-notify-event', self.__enter_notify_event_cb) + self.connect('leave-notify-event', self.__leave_notify_event_cb) + self.connect('button-press-event', self.__button_press_event_cb) + self.connect('button-release-event', self.__button_release_event_cb) + + def connect_to_palette_pop_events(self, palette): + palette.connect('popup', self.__palette_popup_cb) + palette.connect('popdown', self.__palette_popdown_cb) + + def do_draw(self, cr): + """Render a background that fits the allocated space.""" + allocation = self.get_allocation() + context = self.get_style_context() + Gtk.render_background(context, cr, 0, 0, + allocation.width, + allocation.height) + + EventIcon.do_draw(self, cr) + + def __enter_notify_event_cb(self, icon, event): + self._in_prelight_state = True + if self.get_state() != Gtk.StateFlags.ACTIVE: + self.set_state(Gtk.StateFlags.PRELIGHT) + + def __leave_notify_event_cb(self, icon, event): + if self.palette.is_up(): + return + + self._in_prelight_state = False + if self.get_state() != Gtk.StateFlags.ACTIVE: + self.set_state(False) + + def __button_press_event_cb(self, icon, event): + self.set_state(Gtk.StateFlags.ACTIVE) + + def __button_release_event_cb(self, icon, event): + if self._in_prelight_state: + self.set_state(Gtk.StateFlags.PRELIGHT) + else: + self.set_state(False) + + def __palette_popup_cb(self, palette): + self.set_state(Gtk.StateFlags.PRELIGHT) + + def __palette_popdown_cb(self, palette): + self.set_state(False) + + class CellRendererIcon(Gtk.CellRenderer): __gtype_name__ = 'SugarCellRendererIcon'