2007-08-27 21:28:20 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# 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 gobject
|
|
|
|
import gtk
|
|
|
|
|
2007-08-29 14:04:46 +02:00
|
|
|
from sugar.graphics import style
|
2007-09-03 01:48:03 +02:00
|
|
|
from sugar.graphics.palette import Palette, ToolInvoker
|
2007-08-27 21:28:20 +02:00
|
|
|
from sugar.graphics.toolbutton import ToolButton
|
|
|
|
from sugar.graphics.icon import Icon
|
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
_PREVIOUS_PAGE = 0
|
|
|
|
_NEXT_PAGE = 1
|
|
|
|
|
2007-08-27 21:28:20 +02:00
|
|
|
class _TrayViewport(gtk.Viewport):
|
2007-08-27 22:10:18 +02:00
|
|
|
__gproperties__ = {
|
2007-12-23 20:42:20 +01:00
|
|
|
'scrollable' : (bool, None, None, False,
|
|
|
|
gobject.PARAM_READABLE),
|
|
|
|
'can-scroll-prev' : (bool, None, None, False,
|
|
|
|
gobject.PARAM_READABLE),
|
|
|
|
'can-scroll-next' : (bool, None, None, False,
|
|
|
|
gobject.PARAM_READABLE),
|
2007-08-27 22:10:18 +02:00
|
|
|
}
|
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
def __init__(self, orientation):
|
|
|
|
self.orientation = orientation
|
2007-12-23 20:42:20 +01:00
|
|
|
self._scrollable = False
|
|
|
|
self._can_scroll_next = False
|
|
|
|
self._can_scroll_prev = False
|
2007-08-27 22:10:18 +02:00
|
|
|
|
2007-08-27 21:28:20 +02:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
|
|
|
self.set_shadow_type(gtk.SHADOW_NONE)
|
|
|
|
|
|
|
|
self.traybar = gtk.Toolbar()
|
2007-08-30 14:29:52 +02:00
|
|
|
self.traybar.set_orientation(orientation)
|
2007-08-27 21:28:20 +02:00
|
|
|
self.traybar.set_show_arrow(False)
|
|
|
|
self.add(self.traybar)
|
|
|
|
self.traybar.show()
|
|
|
|
|
2007-08-27 22:10:18 +02:00
|
|
|
self.connect('size_allocate', self._size_allocate_cb)
|
|
|
|
|
2007-12-23 20:42:20 +01:00
|
|
|
if self.orientation == gtk.ORIENTATION_HORIZONTAL:
|
|
|
|
adj = self.get_hadjustment()
|
|
|
|
else:
|
|
|
|
adj = self.get_vadjustment()
|
|
|
|
adj.connect('changed', self._adjustment_changed_cb)
|
|
|
|
adj.connect('value-changed', self._adjustment_changed_cb)
|
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
def scroll(self, direction):
|
|
|
|
if direction == _PREVIOUS_PAGE:
|
|
|
|
self._scroll_previous()
|
|
|
|
elif direction == _NEXT_PAGE:
|
|
|
|
self._scroll_next()
|
|
|
|
|
|
|
|
def _scroll_next(self):
|
2008-04-19 01:10:48 +02:00
|
|
|
allocation = self.get_allocation()
|
2007-08-30 14:29:52 +02:00
|
|
|
if self.orientation == gtk.ORIENTATION_HORIZONTAL:
|
|
|
|
adj = self.get_hadjustment()
|
2008-04-19 01:10:48 +02:00
|
|
|
new_value = adj.value + allocation.width
|
|
|
|
adj.value = min(new_value, adj.upper - allocation.width)
|
2007-08-30 14:29:52 +02:00
|
|
|
else:
|
|
|
|
adj = self.get_vadjustment()
|
2008-04-19 01:10:48 +02:00
|
|
|
new_value = adj.value + allocation.height
|
|
|
|
adj.value = min(new_value, adj.upper - allocation.height)
|
2007-08-30 14:29:52 +02:00
|
|
|
|
|
|
|
def _scroll_previous(self):
|
2008-04-19 01:10:48 +02:00
|
|
|
allocation = self.get_allocation()
|
2007-08-30 14:29:52 +02:00
|
|
|
if self.orientation == gtk.ORIENTATION_HORIZONTAL:
|
|
|
|
adj = self.get_hadjustment()
|
2008-04-19 01:10:48 +02:00
|
|
|
new_value = adj.value - allocation.width
|
2007-08-30 14:29:52 +02:00
|
|
|
adj.value = max(adj.lower, new_value)
|
|
|
|
else:
|
|
|
|
adj = self.get_vadjustment()
|
2008-04-19 01:10:48 +02:00
|
|
|
new_value = adj.value - allocation.height
|
2007-08-30 14:29:52 +02:00
|
|
|
adj.value = max(adj.lower, new_value)
|
2007-08-27 21:28:20 +02:00
|
|
|
|
2007-08-29 14:04:46 +02:00
|
|
|
def do_size_request(self, requisition):
|
2008-04-19 01:10:48 +02:00
|
|
|
child_requisition = self.get_child().size_request()
|
2007-08-30 14:29:52 +02:00
|
|
|
if self.orientation == gtk.ORIENTATION_HORIZONTAL:
|
|
|
|
requisition[0] = 0
|
|
|
|
requisition[1] = child_requisition[1]
|
|
|
|
else:
|
|
|
|
requisition[0] = child_requisition[0]
|
|
|
|
requisition[1] = 0
|
2007-08-29 14:04:46 +02:00
|
|
|
|
2007-08-27 22:10:18 +02:00
|
|
|
def do_get_property(self, pspec):
|
2007-12-23 20:42:20 +01:00
|
|
|
if pspec.name == 'scrollable':
|
|
|
|
return self._scrollable
|
|
|
|
elif pspec.name == 'can-scroll-next':
|
|
|
|
return self._can_scroll_next
|
|
|
|
elif pspec.name == 'can-scroll-prev':
|
|
|
|
return self._can_scroll_prev
|
2007-08-27 22:10:18 +02:00
|
|
|
|
|
|
|
def _size_allocate_cb(self, viewport, allocation):
|
|
|
|
bar_requisition = self.traybar.get_child_requisition()
|
2007-08-30 14:29:52 +02:00
|
|
|
if self.orientation == gtk.ORIENTATION_HORIZONTAL:
|
2007-12-23 20:42:20 +01:00
|
|
|
scrollable = bar_requisition[0] > allocation.width
|
2007-08-27 22:10:18 +02:00
|
|
|
else:
|
2007-12-23 20:42:20 +01:00
|
|
|
scrollable = bar_requisition[1] > allocation.height
|
2007-08-27 22:10:18 +02:00
|
|
|
|
2007-12-23 20:42:20 +01:00
|
|
|
if scrollable != self._scrollable:
|
|
|
|
self._scrollable = scrollable
|
|
|
|
self.notify('scrollable')
|
2007-08-27 22:10:18 +02:00
|
|
|
|
2007-12-23 20:42:20 +01:00
|
|
|
def _adjustment_changed_cb(self, adjustment):
|
|
|
|
if adjustment.value <= adjustment.lower:
|
|
|
|
can_scroll_prev = False
|
|
|
|
else:
|
|
|
|
can_scroll_prev = True
|
|
|
|
|
|
|
|
if adjustment.value + adjustment.page_size >= adjustment.upper:
|
|
|
|
can_scroll_next = False
|
|
|
|
else:
|
|
|
|
can_scroll_next = True
|
|
|
|
|
|
|
|
if can_scroll_prev != self._can_scroll_prev:
|
|
|
|
self._can_scroll_prev = can_scroll_prev
|
|
|
|
self.notify('can-scroll-prev')
|
2007-08-29 14:04:46 +02:00
|
|
|
|
2007-12-23 20:42:20 +01:00
|
|
|
if can_scroll_next != self._can_scroll_next:
|
|
|
|
self._can_scroll_next = can_scroll_next
|
|
|
|
self.notify('can-scroll-next')
|
|
|
|
|
|
|
|
|
|
|
|
class _TrayScrollButton(ToolButton):
|
|
|
|
def __init__(self, icon_name, scroll_direction):
|
|
|
|
ToolButton.__init__(self)
|
2007-08-30 14:29:52 +02:00
|
|
|
self._viewport = None
|
|
|
|
|
|
|
|
self._scroll_direction = scroll_direction
|
|
|
|
|
2007-08-29 14:04:46 +02:00
|
|
|
self.set_size_request(style.GRID_CELL_SIZE, style.GRID_CELL_SIZE)
|
|
|
|
|
2007-12-23 20:42:20 +01:00
|
|
|
self.icon = Icon(icon_name = icon_name,
|
|
|
|
icon_size=gtk.ICON_SIZE_SMALL_TOOLBAR)
|
|
|
|
# The alignment is a hack to work around gtk.ToolButton code
|
|
|
|
# that sets the icon_size when the icon_widget is a gtk.Image
|
|
|
|
alignment = gtk.Alignment(0.5, 0.5)
|
|
|
|
alignment.add(self.icon)
|
|
|
|
self.set_icon_widget(alignment)
|
|
|
|
alignment.show_all()
|
2007-08-29 14:04:46 +02:00
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
self.connect('clicked', self._clicked_cb)
|
|
|
|
|
|
|
|
def set_viewport(self, viewport):
|
|
|
|
self._viewport = viewport
|
2007-12-23 20:42:20 +01:00
|
|
|
self._viewport.connect('notify::scrollable',
|
|
|
|
self._viewport_scrollable_changed_cb)
|
|
|
|
|
|
|
|
if self._scroll_direction == _PREVIOUS_PAGE:
|
|
|
|
self._viewport.connect('notify::can-scroll-prev',
|
|
|
|
self._viewport_can_scroll_dir_changed_cb)
|
|
|
|
self.set_sensitive(self._viewport.props.can_scroll_prev)
|
|
|
|
else:
|
|
|
|
self._viewport.connect('notify::can-scroll-next',
|
|
|
|
self._viewport_can_scroll_dir_changed_cb)
|
|
|
|
self.set_sensitive(self._viewport.props.can_scroll_next)
|
|
|
|
|
|
|
|
|
|
|
|
def _viewport_scrollable_changed_cb(self, viewport, pspec):
|
|
|
|
self.props.visible = self._viewport.props.scrollable
|
|
|
|
|
|
|
|
def _viewport_can_scroll_dir_changed_cb(self, viewport, pspec):
|
|
|
|
if self._scroll_direction == _PREVIOUS_PAGE:
|
|
|
|
sensitive = self._viewport.props.can_scroll_prev
|
|
|
|
else:
|
|
|
|
sensitive = self._viewport.props.can_scroll_next
|
2007-08-30 14:29:52 +02:00
|
|
|
|
2007-12-23 20:42:20 +01:00
|
|
|
self.set_sensitive(sensitive)
|
2007-08-30 14:29:52 +02:00
|
|
|
|
|
|
|
def _clicked_cb(self, button):
|
|
|
|
self._viewport.scroll(self._scroll_direction)
|
|
|
|
|
|
|
|
viewport = property(fset=set_viewport)
|
|
|
|
|
2007-08-27 21:28:20 +02:00
|
|
|
class HTray(gtk.HBox):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
gobject.GObject.__init__(self, **kwargs)
|
2007-11-09 10:45:36 +01:00
|
|
|
self.set_direction(gtk.TEXT_DIR_LTR)
|
2007-08-27 21:28:20 +02:00
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
scroll_left = _TrayScrollButton('go-left', _PREVIOUS_PAGE)
|
|
|
|
self.pack_start(scroll_left, False)
|
2007-08-27 21:28:20 +02:00
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
self._viewport = _TrayViewport(gtk.ORIENTATION_HORIZONTAL)
|
2007-08-27 21:28:20 +02:00
|
|
|
self.pack_start(self._viewport)
|
|
|
|
self._viewport.show()
|
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
scroll_right = _TrayScrollButton('go-right', _NEXT_PAGE)
|
|
|
|
self.pack_start(scroll_right, False)
|
2007-08-27 22:10:18 +02:00
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
scroll_left.viewport = self._viewport
|
|
|
|
scroll_right.viewport = self._viewport
|
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
def get_children(self):
|
|
|
|
return self._viewport.traybar.get_children()
|
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
def add_item(self, item, index=-1):
|
|
|
|
self._viewport.traybar.insert(item, index)
|
|
|
|
|
|
|
|
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()
|
2007-08-27 21:28:20 +02:00
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
# FIXME we need a go-down icon
|
|
|
|
scroll_right = _TrayScrollButton('go-right', _NEXT_PAGE)
|
|
|
|
self.pack_start(scroll_right, False)
|
2007-08-27 21:28:20 +02:00
|
|
|
|
2007-08-30 14:29:52 +02:00
|
|
|
scroll_left.viewport = self._viewport
|
|
|
|
scroll_right.viewport = self._viewport
|
2007-08-27 21:28:20 +02:00
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
def get_children(self):
|
|
|
|
return self._viewport.traybar.get_children()
|
|
|
|
|
2007-08-27 21:28:20 +02:00
|
|
|
def add_item(self, item, index=-1):
|
|
|
|
self._viewport.traybar.insert(item, index)
|
|
|
|
|
2007-08-29 14:08:12 +02:00
|
|
|
def remove_item(self, item):
|
2007-08-27 21:28:20 +02:00
|
|
|
self._viewport.traybar.remove(item)
|
|
|
|
|
2007-08-28 14:15:51 +02:00
|
|
|
def get_item_index(self, item):
|
|
|
|
return self._viewport.traybar.get_item_index(item)
|
|
|
|
|
2007-08-27 21:28:20 +02:00
|
|
|
class TrayButton(ToolButton):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
ToolButton.__init__(self, **kwargs)
|
2007-09-03 01:48:03 +02:00
|
|
|
|
2007-09-05 13:55:18 +02:00
|
|
|
class _IconWidget(gtk.EventBox):
|
|
|
|
__gtype_name__ = "SugarTrayIconWidget"
|
|
|
|
|
2007-09-03 01:48:03 +02:00
|
|
|
def __init__(self, icon_name=None, xo_color=None):
|
2007-09-05 13:55:18 +02:00
|
|
|
gtk.EventBox.__init__(self)
|
|
|
|
|
|
|
|
self._palette = None
|
2007-09-03 01:48:03 +02:00
|
|
|
|
2007-09-05 13:55:18 +02:00
|
|
|
self.set_app_paintable(True)
|
2007-09-03 01:48:03 +02:00
|
|
|
|
2008-02-25 12:36:58 +01:00
|
|
|
self._icon = Icon(icon_name=icon_name, xo_color=xo_color,
|
|
|
|
icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR)
|
|
|
|
self.add(self._icon)
|
|
|
|
self._icon.show()
|
2007-09-03 01:48:03 +02:00
|
|
|
|
2007-09-05 13:55:18 +02:00
|
|
|
def do_expose_event(self, event):
|
|
|
|
if self._palette and self._palette.is_up():
|
|
|
|
invoker = self._palette.props.invoker
|
|
|
|
invoker.draw_rectangle(event, self._palette)
|
|
|
|
|
|
|
|
gtk.EventBox.do_expose_event(self, event)
|
2007-09-03 01:48:03 +02:00
|
|
|
|
|
|
|
def set_palette(self, palette):
|
2007-09-17 23:42:17 +02:00
|
|
|
if self._palette is not None:
|
|
|
|
self._palette.props.invoker = None
|
2007-09-03 01:48:03 +02:00
|
|
|
self._palette = palette
|
2007-09-05 13:55:18 +02:00
|
|
|
self._palette.props.invoker = ToolInvoker(self)
|
|
|
|
|
2008-02-25 12:36:58 +01:00
|
|
|
def get_icon(self):
|
|
|
|
return self._icon
|
|
|
|
|
2007-09-05 13:55:18 +02:00
|
|
|
class TrayIcon(gtk.ToolItem):
|
|
|
|
__gtype_name__ = "SugarTrayIcon"
|
|
|
|
|
|
|
|
def __init__(self, icon_name=None, xo_color=None):
|
|
|
|
gtk.ToolItem.__init__(self)
|
|
|
|
|
|
|
|
self._icon_widget = _IconWidget(icon_name, xo_color)
|
|
|
|
self.add(self._icon_widget)
|
|
|
|
self._icon_widget.show()
|
|
|
|
|
|
|
|
self.set_size_request(style.GRID_CELL_SIZE, style.GRID_CELL_SIZE)
|
|
|
|
|
|
|
|
def set_palette(self, palette):
|
|
|
|
self._icon_widget.set_palette(palette)
|
2007-09-03 01:48:03 +02:00
|
|
|
|
|
|
|
def set_tooltip(self, text):
|
|
|
|
self.set_palette(Palette(text))
|
2007-09-05 13:55:18 +02:00
|
|
|
|
2008-02-25 12:36:58 +01:00
|
|
|
def get_icon(self):
|
|
|
|
return self._icon_widget.get_icon()
|
2008-03-01 17:39:22 +01:00
|
|
|
icon = property(get_icon, None)
|
2008-02-25 12:36:58 +01:00
|
|
|
|