From 3ce2a6730436aaedde4f0abdebf0cd19e4766b07 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Wed, 21 Feb 2007 17:05:41 +0100 Subject: [PATCH] Refactored Menu out of Popup. --- shell/view/frame/ZoomBox.py | 31 ++++---- sugar/graphics/iconbutton.py | 3 +- sugar/graphics/menu.py | 148 +++++++++++++++++------------------ sugar/graphics/popup.py | 40 +--------- 4 files changed, 92 insertions(+), 130 deletions(-) diff --git a/shell/view/frame/ZoomBox.py b/shell/view/frame/ZoomBox.py index 80463f6a..4d68a482 100644 --- a/shell/view/frame/ZoomBox.py +++ b/shell/view/frame/ZoomBox.py @@ -19,26 +19,27 @@ from gettext import gettext as _ import hippo -from sugar.graphics.popup import Popup from sugar.graphics.menuicon import MenuIcon -from sugar.graphics.menu import Menu +from sugar.graphics.menu import Menu, MenuItem from sugar.graphics.iconcolor import IconColor from sugar.graphics.iconbutton import IconButton import sugar -class ActivityPopup(Popup): +class ActivityMenu(Menu): ACTION_SHARE = 1 ACTION_CLOSE = 2 def __init__(self, activity_model): - Popup.__init__(self, activity_model.get_title()) + Menu.__init__(self, activity_model.get_title()) if not activity_model.get_shared(): - self.add_item(ActivityPopup.ACTION_SHARE, _('Share'), - 'theme:stock-share-mesh') + self.add_item(MenuItem(ActivityMenu.ACTION_SHARE, + _('Share'), + 'theme:stock-share-mesh')) - self.add_item(ActivityPopup.ACTION_CLOSE, _('Close'), - 'theme:stock-close') + self.add_item(MenuItem(ActivityMenu.ACTION_CLOSE, + _('Close'), + 'theme:stock-close')) class ActivityButton(IconButton): def __init__(self, shell, activity_model): @@ -51,16 +52,14 @@ class ActivityButton(IconButton): IconButton.__init__(self, icon_name=icon_name, color=icon_color) def get_popup(self): - popup = ActivityPopup(self._activity_model) - #popup.connect('action', self._action_cb) - return popup + menu = ActivityMenu(self._activity_model) + menu.connect('action', self._action_cb) + return menu def get_popup_context(self): return self._shell.get_popup_context() - def _action_cb(self, menu, data): - [action_id, label] = data - + def _action_cb(self, menu, menu_item): # TODO: Wouldn't be better to share/close the activity associated with # this button instead of asking for the current activity? activity = self._shell.get_current_activity() @@ -68,9 +67,9 @@ class ActivityButton(IconButton): logging.error('No active activity.') return - if action_id == ActivityPopup.ACTION_SHARE: + if menu_item.props.action_id == ActivityMenu.ACTION_SHARE: activity.share() - elif action_id == ActivityPopup.ACTION_CLOSE: + elif menu_item.props.action_id == ActivityMenu.ACTION_CLOSE: activity.close() class ZoomBox(hippo.CanvasBox): diff --git a/sugar/graphics/iconbutton.py b/sugar/graphics/iconbutton.py index 57c0712e..c2a10465 100644 --- a/sugar/graphics/iconbutton.py +++ b/sugar/graphics/iconbutton.py @@ -23,7 +23,6 @@ import hippo from canvasicon import CanvasIcon from iconcolor import IconColor from sugar.graphics import units -from sugar.graphics.timeline import Timeline from sugar import profile STANDARD_SIZE = 0 @@ -87,7 +86,7 @@ class IconButton(CanvasIcon): elif pspec.name == 'active': return self._active else: - return CanvasIcon.get_property(self, pspec) + return CanvasIcon.do_get_property(self, pspec) def _button_press_event_cb(self, widget, event): if self._active: diff --git a/sugar/graphics/menu.py b/sugar/graphics/menu.py index bb770669..29e342dd 100644 --- a/sugar/graphics/menu.py +++ b/sugar/graphics/menu.py @@ -14,94 +14,92 @@ # License along with this library; if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. +import sys import gtk import hippo import gobject from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.popup import Popup +from sugar.graphics.roundbox import RoundBox +from sugar.graphics import color +from sugar.graphics import font +from sugar.graphics import units -class Menu(gtk.Window): - __gsignals__ = { - 'action': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, ([int])), +class MenuItem(hippo.CanvasBox): + __gtype_name__ = 'SugarMenuItem' + + __gproperties__ = { + 'action-id': (int, None, None, + 0, sys.maxint, 0, + gobject.PARAM_READWRITE), + 'label' : (str, None, None, None, + gobject.PARAM_READWRITE) } - def __init__(self, title=None, content_box=None): - gtk.Window.__init__(self, gtk.WINDOW_POPUP) + def __init__(self, action_id, label, icon_name=None, icon_color=None): + hippo.CanvasBox.__init__(self, orientation=hippo.ORIENTATION_HORIZONTAL) + + self._action_id = action_id + self.props.padding = 5 + self.props.spacing = 5 - canvas = hippo.Canvas() - self.add(canvas) - canvas.show() + if icon_name: + icon = CanvasIcon(icon_name=icon_name, + scale=units.SMALL_ICON_SCALE) + if icon_color: + icon.props.color = icon_color + self.append(icon) - self._root = hippo.CanvasBox() - canvas.set_root(self._root) + self._canvas_text = hippo.CanvasText() + self._canvas_text.props.text = label + self._canvas_text.props.color = color.LABEL_TEXT.get_int() + self._canvas_text.props.font_desc = font.DEFAULT.get_pango_desc() + self.append(self._canvas_text) + + def do_set_property(self, pspec, value): + if pspec.name == 'action-id': + self._action_id = value + elif pspec.name == 'label': + self._canvas_text.props.text = value + else: + hippo.CanvasBox.do_set_property(self, pspec, value) + + def do_get_property(self, pspec): + if pspec.name == 'action-id': + return self._action_id + elif pspec.name == 'label': + return self._canvas_text.props.text + else: + return hippo.CanvasBox.do_get_property(self, pspec) + +class Menu(Popup): + __gtype_name__ = 'SugarMenu' + + __gsignals__ = { + 'action': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([object])) + } + + def __init__(self, title): + Popup.__init__(self) + + self.props.background_color = color.MENU_BACKGROUND.get_int() + self.props.border_color = color.MENU_BORDER.get_int() + self.props.border = units.points_to_pixels(1) if title: - self._title_item = hippo.CanvasText(text=title) - self._root.append(self._title_item) - else: - self._title_item = None + pass - if content_box: - separator = self._create_separator() - self._root.append(separator) - self._root.append(content_box) + def add_item(self, item): + item.connect('button-press-event', self._item_button_press_event_cb) + self.append(item) - self._action_box = None - self._item_box = None + def add_separator(self): + box = hippo.CanvasBox() + box.props.background_color = color.MENU_SEPARATOR.get_int() + box.props.box_height = units.points_to_pixels(1) + self.append(box) - def _create_separator(self): - separator = hippo.CanvasBox() - return separator - - def _create_item_box(self): - if self._title_item: - separator = self._create_separator() - self._root.append(separator) - - self._item_box = hippo.CanvasBox( - orientation=hippo.ORIENTATION_VERTICAL) - self._root.append(self._item_box) - - def _create_action_box(self): - separator = self._create_separator() - self._root.append(separator) - - self._action_box = hippo.CanvasBox( - orientation=hippo.ORIENTATION_HORIZONTAL) - self._root.append(self._action_box) - - def add_item(self, label, action_id=None, wrap=False): - if not self._item_box: - self._create_item_box() - - text = hippo.CanvasText(text=label) - if wrap: - text.set_property("size-mode", "wrap-word") - - # FIXME need a way to make hippo items activable in python - if action_id: - text.connect('button-press-event', self._item_clicked_cb, action_id) - #text.connect('activated', self._action_clicked_cb, action_id) - - self._item_box.append(text) - - def add_action(self, icon, action_id): - if not self._action_box: - self._create_action_box() - - icon.connect('activated', self._action_clicked_cb, action_id) - self._action_box.append(icon) - - def remove_action(self, icon): - self._action_box.remove(icon) - - def _item_clicked_cb(self, icon, event, action): - self.emit('action', action) - - def _action_clicked_cb(self, icon, action): - self.emit('action', action) - - def set_title(self, title): - self._title_item.set_property('text', title) + def _item_button_press_event_cb(self, menu_item, event): + self.emit('action', menu_item) diff --git a/sugar/graphics/popup.py b/sugar/graphics/popup.py index 471be308..09a1fe25 100644 --- a/sugar/graphics/popup.py +++ b/sugar/graphics/popup.py @@ -21,12 +21,6 @@ import gobject import gtk import hippo -from sugar.graphics import units -from sugar.graphics.roundbox import RoundBox -from sugar.graphics import color -from sugar.graphics import font -from sugar.graphics.canvasicon import CanvasIcon - class Popup(hippo.CanvasBox, hippo.CanvasItem): __gtype_name__ = 'SugarPopup' @@ -34,38 +28,10 @@ class Popup(hippo.CanvasBox, hippo.CanvasItem): 'action-completed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])) } - def __init__(self, title): + def __init__(self): hippo.CanvasBox.__init__(self) - self.props.background_color = color.MENU_BACKGROUND.get_int() - self.props.border_color = color.MENU_BORDER.get_int() - self.props.border = units.points_to_pixels(1) self._window = None - - def add_item(self, action_id, label, icon_name=None, icon_color=None): - box = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL) - box.props.padding = 5 - box.props.spacing = 5 - if icon_name: - icon = CanvasIcon(icon_name=icon_name, - scale=units.SMALL_ICON_SCALE) - if icon_color: - icon.props.color = icon_color - box.append(icon) - - canvas_text = hippo.CanvasText() - canvas_text.props.text = label - canvas_text.props.color = color.LABEL_TEXT.get_int() - canvas_text.props.font_desc = font.DEFAULT.get_pango_desc() - box.append(canvas_text) - - box.connect('button-press-event', self._item_button_press_event_cb) - self.append(box) - - def add_separator(self): - box = hippo.CanvasBox() - box.props.background_color = color.MENU_SEPARATOR.get_int() - box.props.box_height = units.points_to_pixels(1) - self.append(box) + self.connect('button-press-event', self._button_press_event_cb) def popup(self, x, y): if not self._window: @@ -79,5 +45,5 @@ class Popup(hippo.CanvasBox, hippo.CanvasItem): self._window.destroy() self._window = None - def _item_button_press_event_cb(self, item, event): + def _button_press_event_cb(self, menu, event): self.emit('action-completed')