diff --git a/sugar/graphics/tray.py b/sugar/graphics/tray.py index 99e74653..1845d33f 100644 --- a/sugar/graphics/tray.py +++ b/sugar/graphics/tray.py @@ -22,13 +22,17 @@ from sugar.graphics import style from sugar.graphics.toolbutton import ToolButton from sugar.graphics.icon import Icon +_PREVIOUS_PAGE = 0 +_NEXT_PAGE = 1 + class _TrayViewport(gtk.Viewport): __gproperties__ = { 'can-scroll' : (bool, None, None, False, gobject.PARAM_READABLE), } - def __init__(self): + def __init__(self, orientation): + self.orientation = orientation self._can_scroll = False gobject.GObject.__init__(self) @@ -36,26 +40,47 @@ class _TrayViewport(gtk.Viewport): self.set_shadow_type(gtk.SHADOW_NONE) self.traybar = gtk.Toolbar() + self.traybar.set_orientation(orientation) self.traybar.set_show_arrow(False) self.add(self.traybar) self.traybar.show() self.connect('size_allocate', self._size_allocate_cb) - def scroll_right(self): - adj = self.get_hadjustment() - new_value = adj.value + self.allocation.width - adj.value = min(new_value, adj.upper - self.allocation.width) + def scroll(self, direction): + if direction == _PREVIOUS_PAGE: + self._scroll_previous() + elif direction == _NEXT_PAGE: + self._scroll_next() - def scroll_left(self): - adj = self.get_hadjustment() - new_value = adj.value - self.allocation.width - adj.value = max(adj.lower, new_value) + def _scroll_next(self): + if self.orientation == gtk.ORIENTATION_HORIZONTAL: + adj = self.get_hadjustment() + new_value = adj.value + self.allocation.width + adj.value = min(new_value, adj.upper - self.allocation.width) + else: + adj = self.get_vadjustment() + new_value = adj.value + self.allocation.height + adj.value = min(new_value, adj.upper - self.allocation.height) + + def _scroll_previous(self): + if self.orientation == gtk.ORIENTATION_HORIZONTAL: + adj = self.get_hadjustment() + new_value = adj.value - self.allocation.width + adj.value = max(adj.lower, new_value) + else: + adj = self.get_vadjustment() + new_value = adj.value - self.allocation.height + adj.value = max(adj.lower, new_value) def do_size_request(self, requisition): child_requisition = self.child.size_request() - requisition[0] = 0 - requisition[1] = child_requisition[1] + if self.orientation == gtk.ORIENTATION_HORIZONTAL: + requisition[0] = 0 + requisition[1] = child_requisition[1] + else: + requisition[0] = child_requisition[0] + requisition[1] = 0 def do_get_property(self, pspec): if pspec.name == 'can-scroll': @@ -63,19 +88,23 @@ class _TrayViewport(gtk.Viewport): def _size_allocate_cb(self, viewport, allocation): bar_requisition = self.traybar.get_child_requisition() - if bar_requisition[0] < allocation.width: - can_scroll = False + if self.orientation == gtk.ORIENTATION_HORIZONTAL: + can_scroll = bar_requisition[0] > allocation.width else: - can_scroll = True + can_scroll = bar_requisition[1] > allocation.height if can_scroll != self._can_scroll: self._can_scroll = can_scroll self.notify('can-scroll') class _TrayScrollButton(gtk.Button): - def __init__(self, icon_name): + def __init__(self, icon_name, scroll_direction): gobject.GObject.__init__(self) + self._viewport = None + + self._scroll_direction = scroll_direction + self.set_relief(gtk.RELIEF_NONE) self.set_size_request(style.GRID_CELL_SIZE, style.GRID_CELL_SIZE) @@ -84,34 +113,65 @@ class _TrayScrollButton(gtk.Button): self.set_image(icon) icon.show() + self.connect('clicked', self._clicked_cb) + + def set_viewport(self, viewport): + self._viewport = viewport + self._viewport.connect('notify::can-scroll', + self._viewport_can_scroll_changed_cb) + + def _viewport_can_scroll_changed_cb(self, viewport, pspec): + self.props.visible = self._viewport.props.can_scroll + + def _clicked_cb(self, button): + self._viewport.scroll(self._scroll_direction) + + viewport = property(fset=set_viewport) + class HTray(gtk.HBox): def __init__(self, **kwargs): gobject.GObject.__init__(self, **kwargs) - self._scroll_left = _TrayScrollButton('go-left') - self._scroll_left.connect('clicked', self._scroll_left_cb) - self.pack_start(self._scroll_left, False) + scroll_left = _TrayScrollButton('go-left', _PREVIOUS_PAGE) + self.pack_start(scroll_left, False) - self._viewport = _TrayViewport() - self._viewport.connect('notify::can-scroll', - self._viewport_can_scroll_changed_cb) + self._viewport = _TrayViewport(gtk.ORIENTATION_HORIZONTAL) self.pack_start(self._viewport) self._viewport.show() - self._scroll_right = _TrayScrollButton('go-right') - self._scroll_right.connect('clicked', self._scroll_right_cb) - self.pack_start(self._scroll_right, False) + scroll_right = _TrayScrollButton('go-right', _NEXT_PAGE) + self.pack_start(scroll_right, False) - def _viewport_can_scroll_changed_cb(self, viewport, pspec): - if self._viewport.props.can_scroll: - self._scroll_left.show() - self._scroll_right.show() + scroll_left.viewport = self._viewport + scroll_right.viewport = self._viewport - def _scroll_left_cb(self, button): - self._viewport.scroll_left() + def add_item(self, item, index=-1): + self._viewport.traybar.insert(item, index) - def _scroll_right_cb(self, button): - self._viewport.scroll_right() + def remove_item(self, item): + self._viewport.traybar.remove(item) + + def get_item_index(self, item): + return self._viewport.traybar.get_item_index(item) + +class VTray(gtk.VBox): + def __init__(self, **kwargs): + gobject.GObject.__init__(self, **kwargs) + + # FIXME we need a go-up icon + scroll_left = _TrayScrollButton('go-left', _PREVIOUS_PAGE) + self.pack_start(scroll_left, False) + + self._viewport = _TrayViewport(gtk.ORIENTATION_VERTICAL) + self.pack_start(self._viewport) + self._viewport.show() + + # FIXME we need a go-down icon + scroll_right = _TrayScrollButton('go-right', _NEXT_PAGE) + self.pack_start(scroll_right, False) + + scroll_left.viewport = self._viewport + scroll_right.viewport = self._viewport def add_item(self, item, index=-1): self._viewport.traybar.insert(item, index) diff --git a/tests/graphics/tray.py b/tests/graphics/tray.py index 9bbf0b22..4de9b460 100644 --- a/tests/graphics/tray.py +++ b/tests/graphics/tray.py @@ -22,16 +22,17 @@ Test the sugar.graphics.icon.Icon widget. import gtk from sugar.graphics.tray import HTray +from sugar.graphics.tray import VTray from sugar.graphics.tray import TrayButton import common test = common.Test() -box = gtk.VBox() +vbox = gtk.VBox() tray = HTray() -box.pack_start(tray, False) +vbox.pack_start(tray, False) tray.show() theme_icons = gtk.icon_theme_get_default().list_icons() @@ -42,7 +43,7 @@ for i in range(0, 100): button.show() tray = HTray() -box.pack_start(tray, False) +vbox.pack_start(tray, False) tray.show() for i in range(0, 10): @@ -50,8 +51,31 @@ for i in range(0, 10): tray.add_item(button) button.show() -test.pack_start(box) -box.show() +hbox = gtk.HBox() + +tray = VTray() +hbox.pack_start(tray, False) +tray.show() + +for i in range(0, 100): + button = TrayButton(icon_name=theme_icons[i]) + tray.add_item(button) + button.show() + +tray = VTray() +hbox.pack_start(tray, False) +tray.show() + +for i in range(0, 4): + button = TrayButton(icon_name=theme_icons[i]) + tray.add_item(button) + button.show() + +vbox.pack_start(hbox) +hbox.show() + +test.pack_start(vbox) +vbox.show() test.show()