From 2f4822f1cdcd4b87d29523d54e70bddf4d20c2b9 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Thu, 22 Feb 2007 12:34:38 +0100 Subject: [PATCH] Make OptionMenu use the new Menu and MenuItem classes. --- sugar/graphics/menu.py | 2 +- sugar/graphics/optionmenu.py | 93 +++++++++++------------------------- tests/test-option-menu.py | 9 ++-- 3 files changed, 34 insertions(+), 70 deletions(-) diff --git a/sugar/graphics/menu.py b/sugar/graphics/menu.py index 9dfc6f37..0f154701 100644 --- a/sugar/graphics/menu.py +++ b/sugar/graphics/menu.py @@ -80,7 +80,7 @@ class Menu(Popup): 'action': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([object])) } - def __init__(self, title): + def __init__(self, title=None): Popup.__init__(self) self.props.background_color = color.MENU_BACKGROUND.get_int() diff --git a/sugar/graphics/optionmenu.py b/sugar/graphics/optionmenu.py index f2e4603c..edd1d39e 100644 --- a/sugar/graphics/optionmenu.py +++ b/sugar/graphics/optionmenu.py @@ -24,70 +24,27 @@ import hippo from sugar.graphics import units from sugar.graphics.roundbox import RoundBox +from sugar.graphics.menu import Menu, MenuItem from sugar.graphics import iconbutton from sugar.graphics import color from sugar.graphics import font from sugar.graphics.canvasicon import CanvasIcon -class Menu(hippo.CanvasBox, hippo.CanvasItem): - __gtype_name__ = 'SugarMenu' - - __gsignals__ = { - 'action': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([object])) - } - +class _Menu(Menu): 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, - [action_id, label]) - 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) - - def show(self, x, y): - if not self._window: - self._window = hippo.CanvasWindow(gtk.WINDOW_POPUP) - self._window.move(x, y) - self._window.set_root(self) - self._window.show() - - def hide(self): - if self._window: - self._window.destroy() - self._window = None - - def _item_button_press_event_cb(self, item, event, data): - self.emit('action', data) - self.hide() + Menu.__init__(self) + self._is_visible = False def is_visible(self): - return self._window != None + return self._is_visible + + def popup(self, x, y): + Menu.popup(self, x, y) + self._is_visible = True + + def popdown(self): + Menu.popdown(self) + self._is_visible = False class OptionMenu(hippo.CanvasBox, hippo.CanvasItem): __gtype_name__ = 'SugarOptionMenu' @@ -122,8 +79,9 @@ class OptionMenu(hippo.CanvasBox, hippo.CanvasItem): arrow.props.xalign = hippo.ALIGNMENT_START self._round_box.append(arrow) - self._menu = Menu() + self._menu = _Menu() self._menu.connect('action', self._menu_action_cb) + self._menu.connect('action-completed', self._menu_action_completed_cb) self.connect('button-press-event', self._button_press_event_cb) @@ -135,19 +93,19 @@ class OptionMenu(hippo.CanvasBox, hippo.CanvasItem): if pspec.name == 'value': return self._value - def add_option(self, action_id, label, icon_name=None, icon_color=None): + def add_item(self, menu_item): if not self._value: - self._value = action_id - self._canvas_text.props.text = label + self._value = menu_item.props.action_id + self._canvas_text.props.text = menu_item.props.label - self._menu.add_item(action_id, label, icon_name, icon_color) + self._menu.add_item(menu_item) def add_separator(self): self._menu.add_separator() def _button_press_event_cb(self, box, event): if self._menu.is_visible(): - self._menu.hide() + self._menu.popdown() else: context = self._round_box.get_context() [x, y] = context.translate_to_screen(self._round_box) @@ -156,11 +114,16 @@ class OptionMenu(hippo.CanvasBox, hippo.CanvasItem): self._menu.props.box_width = self.get_width_request() [width, height] = self._round_box.get_allocation() - self._menu.show(x, y + height) + self._menu.popup(x, y + height) - def _menu_action_cb(self, menu, data): - [action_id, label] = data + def _menu_action_cb(self, menu, menu_item): + action_id = menu_item.props.action_id + label = menu_item.props.label + if action_id != self._value: self._value = action_id self._canvas_text.props.text = label self.emit('changed') + + def _menu_action_completed_cb(self, menu): + self._menu.popdown() diff --git a/tests/test-option-menu.py b/tests/test-option-menu.py index a3c6e6dc..bbcc03a0 100755 --- a/tests/test-option-menu.py +++ b/tests/test-option-menu.py @@ -24,6 +24,7 @@ import hippo from sugar.graphics.toolbar import Toolbar from sugar.graphics.optionmenu import OptionMenu +from sugar.graphics.menu import MenuItem from sugar.graphics.iconbutton import IconButton def _option_menu_changed_cb(option_menu): @@ -56,11 +57,11 @@ OPTION_WRITE = 3 OPTION_CHAT = 4 option_menu = OptionMenu() -option_menu.add_option(OPTION_ANYTHING, _('Anything')) +option_menu.add_item(MenuItem(OPTION_ANYTHING, _('Anything'))) option_menu.add_separator() -option_menu.add_option(OPTION_DRAW, _('Draw'), 'theme:stock-close') -option_menu.add_option(OPTION_WRITE, _('Write')) -option_menu.add_option(OPTION_CHAT, _('Chat')) +option_menu.add_item(MenuItem(OPTION_DRAW, _('Draw'), 'theme:stock-close')) +option_menu.add_item(MenuItem(OPTION_WRITE, _('Write'))) +option_menu.add_item(MenuItem(OPTION_CHAT, _('Chat'))) option_menu.connect('changed', _option_menu_changed_cb) toolbar.append(option_menu)