Refactored Menu out of Popup.

This commit is contained in:
Tomeu Vizoso 2007-02-21 17:05:41 +01:00
parent c05b179675
commit 3ce2a67304
4 changed files with 92 additions and 130 deletions

View File

@ -19,26 +19,27 @@ from gettext import gettext as _
import hippo import hippo
from sugar.graphics.popup import Popup
from sugar.graphics.menuicon import MenuIcon 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.iconcolor import IconColor
from sugar.graphics.iconbutton import IconButton from sugar.graphics.iconbutton import IconButton
import sugar import sugar
class ActivityPopup(Popup): class ActivityMenu(Menu):
ACTION_SHARE = 1 ACTION_SHARE = 1
ACTION_CLOSE = 2 ACTION_CLOSE = 2
def __init__(self, activity_model): 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(): if not activity_model.get_shared():
self.add_item(ActivityPopup.ACTION_SHARE, _('Share'), self.add_item(MenuItem(ActivityMenu.ACTION_SHARE,
'theme:stock-share-mesh') _('Share'),
'theme:stock-share-mesh'))
self.add_item(ActivityPopup.ACTION_CLOSE, _('Close'), self.add_item(MenuItem(ActivityMenu.ACTION_CLOSE,
'theme:stock-close') _('Close'),
'theme:stock-close'))
class ActivityButton(IconButton): class ActivityButton(IconButton):
def __init__(self, shell, activity_model): def __init__(self, shell, activity_model):
@ -51,16 +52,14 @@ class ActivityButton(IconButton):
IconButton.__init__(self, icon_name=icon_name, color=icon_color) IconButton.__init__(self, icon_name=icon_name, color=icon_color)
def get_popup(self): def get_popup(self):
popup = ActivityPopup(self._activity_model) menu = ActivityMenu(self._activity_model)
#popup.connect('action', self._action_cb) menu.connect('action', self._action_cb)
return popup return menu
def get_popup_context(self): def get_popup_context(self):
return self._shell.get_popup_context() return self._shell.get_popup_context()
def _action_cb(self, menu, data): def _action_cb(self, menu, menu_item):
[action_id, label] = data
# TODO: Wouldn't be better to share/close the activity associated with # TODO: Wouldn't be better to share/close the activity associated with
# this button instead of asking for the current activity? # this button instead of asking for the current activity?
activity = self._shell.get_current_activity() activity = self._shell.get_current_activity()
@ -68,9 +67,9 @@ class ActivityButton(IconButton):
logging.error('No active activity.') logging.error('No active activity.')
return return
if action_id == ActivityPopup.ACTION_SHARE: if menu_item.props.action_id == ActivityMenu.ACTION_SHARE:
activity.share() activity.share()
elif action_id == ActivityPopup.ACTION_CLOSE: elif menu_item.props.action_id == ActivityMenu.ACTION_CLOSE:
activity.close() activity.close()
class ZoomBox(hippo.CanvasBox): class ZoomBox(hippo.CanvasBox):

View File

@ -23,7 +23,6 @@ import hippo
from canvasicon import CanvasIcon from canvasicon import CanvasIcon
from iconcolor import IconColor from iconcolor import IconColor
from sugar.graphics import units from sugar.graphics import units
from sugar.graphics.timeline import Timeline
from sugar import profile from sugar import profile
STANDARD_SIZE = 0 STANDARD_SIZE = 0
@ -87,7 +86,7 @@ class IconButton(CanvasIcon):
elif pspec.name == 'active': elif pspec.name == 'active':
return self._active return self._active
else: else:
return CanvasIcon.get_property(self, pspec) return CanvasIcon.do_get_property(self, pspec)
def _button_press_event_cb(self, widget, event): def _button_press_event_cb(self, widget, event):
if self._active: if self._active:

View File

@ -14,94 +14,92 @@
# License along with this library; if not, write to the # License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA. # Boston, MA 02111-1307, USA.
import sys
import gtk import gtk
import hippo import hippo
import gobject import gobject
from sugar.graphics.canvasicon import CanvasIcon 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): class MenuItem(hippo.CanvasBox):
__gsignals__ = { __gtype_name__ = 'SugarMenuItem'
'action': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([int])), __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): def __init__(self, action_id, label, icon_name=None, icon_color=None):
gtk.Window.__init__(self, gtk.WINDOW_POPUP) hippo.CanvasBox.__init__(self, orientation=hippo.ORIENTATION_HORIZONTAL)
canvas = hippo.Canvas() self._action_id = action_id
self.add(canvas) self.props.padding = 5
canvas.show() self.props.spacing = 5
self._root = hippo.CanvasBox() if icon_name:
canvas.set_root(self._root) icon = CanvasIcon(icon_name=icon_name,
scale=units.SMALL_ICON_SCALE)
if icon_color:
icon.props.color = icon_color
self.append(icon)
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: if title:
self._title_item = hippo.CanvasText(text=title) pass
self._root.append(self._title_item)
else:
self._title_item = None
if content_box: def add_item(self, item):
separator = self._create_separator() item.connect('button-press-event', self._item_button_press_event_cb)
self._root.append(separator) self.append(item)
self._root.append(content_box)
self._action_box = None def add_separator(self):
self._item_box = None 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): def _item_button_press_event_cb(self, menu_item, event):
separator = hippo.CanvasBox() self.emit('action', menu_item)
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)

View File

@ -21,12 +21,6 @@ import gobject
import gtk import gtk
import hippo 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): class Popup(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'SugarPopup' __gtype_name__ = 'SugarPopup'
@ -34,38 +28,10 @@ class Popup(hippo.CanvasBox, hippo.CanvasItem):
'action-completed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])) 'action-completed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([]))
} }
def __init__(self, title): def __init__(self):
hippo.CanvasBox.__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 self._window = None
self.connect('button-press-event', self._button_press_event_cb)
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)
def popup(self, x, y): def popup(self, x, y):
if not self._window: if not self._window:
@ -79,5 +45,5 @@ class Popup(hippo.CanvasBox, hippo.CanvasItem):
self._window.destroy() self._window.destroy()
self._window = None self._window = None
def _item_button_press_event_cb(self, item, event): def _button_press_event_cb(self, menu, event):
self.emit('action-completed') self.emit('action-completed')