diff options
author | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2007-02-21 16:05:41 (GMT) |
---|---|---|
committer | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2007-02-21 16:05:41 (GMT) |
commit | 3ce2a6730436aaedde4f0abdebf0cd19e4766b07 (patch) | |
tree | 121e3dfdb11af297c301cbd5b787bcae273f8666 /sugar/graphics/menu.py | |
parent | c05b179675afd326ca80540cd55cfd1900e2970f (diff) |
Refactored Menu out of Popup.
Diffstat (limited to 'sugar/graphics/menu.py')
-rw-r--r-- | sugar/graphics/menu.py | 144 |
1 files changed, 71 insertions, 73 deletions
diff --git a/sugar/graphics/menu.py b/sugar/graphics/menu.py index bb77066..29e342d 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 - -class Menu(gtk.Window): - __gsignals__ = { - 'action': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, ([int])), +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 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) - - canvas = hippo.Canvas() - self.add(canvas) - canvas.show() - - self._root = hippo.CanvasBox() - canvas.set_root(self._root) - - if title: - self._title_item = hippo.CanvasText(text=title) - self._root.append(self._title_item) + 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 + + 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._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: - self._title_item = None - - if content_box: - separator = self._create_separator() - self._root.append(separator) - self._root.append(content_box) - - self._action_box = None - self._item_box = None - - def _create_separator(self): - separator = hippo.CanvasBox() - return separator + hippo.CanvasBox.do_set_property(self, pspec, value) - 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") + 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) - # 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) +class Menu(Popup): + __gtype_name__ = 'SugarMenu' - self._item_box.append(text) + __gsignals__ = { + 'action': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([object])) + } - def add_action(self, icon, action_id): - if not self._action_box: - self._create_action_box() + def __init__(self, title): + Popup.__init__(self) - icon.connect('activated', self._action_clicked_cb, action_id) - self._action_box.append(icon) + 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) - def remove_action(self, icon): - self._action_box.remove(icon) + if title: + pass - def _item_clicked_cb(self, icon, event, action): - self.emit('action', action) + def add_item(self, item): + item.connect('button-press-event', self._item_button_press_event_cb) + self.append(item) - def _action_clicked_cb(self, icon, action): - self.emit('action', action) + 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 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) |