Merge branch 'master' of git://dev.laptop.org/sugar
commit
48951dc1cb
@ -1,95 +0,0 @@
|
||||
import gtk
|
||||
import goocanvas
|
||||
import gobject
|
||||
|
||||
from sugar.canvas.CanvasView import CanvasView
|
||||
from sugar.canvas.CanvasBox import CanvasBox
|
||||
from sugar.canvas.IconItem import IconItem
|
||||
|
||||
class Menu(gtk.Window):
|
||||
__gsignals__ = {
|
||||
'action': (gobject.SIGNAL_RUN_FIRST,
|
||||
gobject.TYPE_NONE, ([int])),
|
||||
}
|
||||
|
||||
def __init__(self, grid, title):
|
||||
gtk.Window.__init__(self, gtk.WINDOW_POPUP)
|
||||
|
||||
self._width = 15
|
||||
self._height = 3
|
||||
self._grid = grid
|
||||
self._action_box = None
|
||||
|
||||
self._canvas = CanvasView()
|
||||
self.add(self._canvas)
|
||||
self._canvas.show()
|
||||
|
||||
model = goocanvas.CanvasModelSimple()
|
||||
self._root = model.get_root_item()
|
||||
|
||||
self._rect = goocanvas.Rect(fill_color='black', line_width=0)
|
||||
self._root.add_child(self._rect)
|
||||
|
||||
text = goocanvas.Text(text=title, font="Sans bold 18",
|
||||
fill_color='white', anchor=gtk.ANCHOR_SW)
|
||||
self._grid.set_constraints(text, 1, self._height, self._width, 2)
|
||||
self._root.add_child(text)
|
||||
self._height += 1
|
||||
|
||||
self._update_constraints()
|
||||
|
||||
self._canvas.set_model(model)
|
||||
|
||||
def _create_action_box(self):
|
||||
separator = goocanvas.Path(data='M 15 0 L 215 0', line_width=3,
|
||||
stroke_color='white')
|
||||
self._grid.set_constraints(separator, 0, self._height)
|
||||
self._root.add_child(separator)
|
||||
self._height += 1
|
||||
|
||||
box = CanvasBox(self._grid, CanvasBox.HORIZONTAL)
|
||||
self._grid.set_constraints(box, 0, self._height)
|
||||
self._height += 5
|
||||
|
||||
return box
|
||||
|
||||
def get_grid(self):
|
||||
return self._grid
|
||||
|
||||
def add_image(self, image_item, width, height):
|
||||
"""width & height in grid units"""
|
||||
separator = goocanvas.Path(data='M 15 0 L 215 0', line_width=3,
|
||||
stroke_color='white')
|
||||
self._grid.set_constraints(separator, 0, self._height)
|
||||
self._root.add_child(separator)
|
||||
self._height += 1
|
||||
|
||||
self._grid.set_constraints(image_item, x=5, y=self._height, width=width, height=height)
|
||||
self._root.add_child(image_item)
|
||||
self._height += height + 1
|
||||
self._update_constraints()
|
||||
|
||||
def add_action(self, icon, action_id):
|
||||
if self._action_box == None:
|
||||
self._action_box = self._create_action_box()
|
||||
self._root.add_child(self._action_box)
|
||||
self._update_constraints()
|
||||
|
||||
icon.connect('clicked', self._action_clicked_cb, action_id)
|
||||
self._action_box.set_constraints(icon, 5, 5)
|
||||
self._action_box.add_child(icon)
|
||||
|
||||
def _action_clicked_cb(self, icon, action):
|
||||
self.emit('action', action)
|
||||
|
||||
def _update_constraints(self):
|
||||
self._grid.set_constraints(self._canvas, 0, 0,
|
||||
self._width, self._height)
|
||||
self._grid.set_constraints(self._rect, 0, 0,
|
||||
self._width, self._height)
|
||||
|
||||
def get_width(self):
|
||||
return self._width
|
||||
|
||||
def get_height(self):
|
||||
return self._height
|
@ -1,121 +0,0 @@
|
||||
import goocanvas
|
||||
import gobject
|
||||
|
||||
from sugar.canvas.IconItem import IconItem
|
||||
from sugar.canvas.Grid import Grid
|
||||
|
||||
class _MenuStrategy:
|
||||
def get_menu_position(self, menu, grid_x1, grid_y1, grid_x2, grid_y2):
|
||||
grid_x = grid_x2
|
||||
if grid_x + menu.get_width() > Grid.COLS:
|
||||
grid_x = grid_x1 - menu.get_width() + 1
|
||||
|
||||
grid_y = grid_y1
|
||||
|
||||
if grid_y < 0:
|
||||
grid_y = 0
|
||||
if grid_y + menu.get_width() > Grid.ROWS:
|
||||
grid_y = Grid.ROWS - menu.get_width()
|
||||
|
||||
return [grid_x, grid_y]
|
||||
|
||||
class MenuIcon(IconItem, goocanvas.Item):
|
||||
def __init__(self, menu_shell, **kwargs):
|
||||
IconItem.__init__(self, **kwargs)
|
||||
|
||||
self._menu_shell = menu_shell
|
||||
self._grid = menu_shell.get_grid()
|
||||
self._menu = None
|
||||
self._hover_menu = False
|
||||
self._popdown_on_leave = False
|
||||
self._popdown_sid = 0
|
||||
self._menu_strategy = _MenuStrategy()
|
||||
|
||||
def popdown(self):
|
||||
if self._menu:
|
||||
self._menu.destroy()
|
||||
self._menu = None
|
||||
self._menu_shell.set_active(None)
|
||||
|
||||
def set_menu_strategy(self, strategy):
|
||||
self._menu_strategy = strategy
|
||||
|
||||
def _popup(self, x1, y1, x2, y2):
|
||||
self.popdown()
|
||||
|
||||
self._menu_shell.set_active(None)
|
||||
|
||||
grid = self._shell.get_grid()
|
||||
self._menu = self.create_menu()
|
||||
self._menu.connect('enter-notify-event',
|
||||
self._menu_enter_notify_event_cb)
|
||||
self._menu.connect('leave-notify-event',
|
||||
self._menu_leave_notify_event_cb)
|
||||
|
||||
[grid_x1, grid_y1] = grid.convert_from_screen(x1, y1)
|
||||
[grid_x2, grid_y2] = grid.convert_from_screen(x2, y2)
|
||||
|
||||
strategy = self._menu_strategy
|
||||
[grid_x, grid_y] = strategy.get_menu_position(self._menu,
|
||||
grid_x1, grid_y1,
|
||||
grid_x2, grid_y2)
|
||||
|
||||
grid.set_constraints(self._menu, grid_x, grid_y,
|
||||
self._menu.get_width(), self._menu.get_height())
|
||||
|
||||
self._menu.show()
|
||||
|
||||
self._menu_shell.set_active(self)
|
||||
|
||||
def _menu_enter_notify_event_cb(self, widget, event):
|
||||
self._hover_menu = True
|
||||
|
||||
def _menu_leave_notify_event_cb(self, widget, event):
|
||||
self._hover_menu = False
|
||||
if self._popdown_on_leave:
|
||||
self.popdown()
|
||||
|
||||
def do_create_view(self, canvas, parent_view):
|
||||
view = IconItem.do_create_view(self, canvas, parent_view)
|
||||
view.connect('enter-notify-event', self._enter_notify_event_cb, canvas)
|
||||
view.connect('leave-notify-event', self._leave_notify_event_cb)
|
||||
return view
|
||||
|
||||
def _start_popdown_timeout(self):
|
||||
self._stop_popdown_timeout()
|
||||
self._popdown_sid = gobject.timeout_add(1000, self._popdown_timeout_cb)
|
||||
|
||||
def _stop_popdown_timeout(self):
|
||||
if self._popdown_sid > 0:
|
||||
gobject.source_remove(self._popdown_sid)
|
||||
self._popdown_sid = 0
|
||||
|
||||
def _enter_notify_event_cb(self, view, target, event, canvas):
|
||||
self._stop_popdown_timeout()
|
||||
|
||||
[x1, y1] = canvas.convert_to_pixels(view.get_bounds().x1,
|
||||
view.get_bounds().y1)
|
||||
[x2, y2] = canvas.convert_to_pixels(view.get_bounds().x2,
|
||||
view.get_bounds().y2)
|
||||
|
||||
[window_x, window_y] = canvas.window.get_origin()
|
||||
|
||||
x1 += window_x
|
||||
y1 += window_y
|
||||
x2 += window_x
|
||||
y2 += window_y
|
||||
|
||||
self._popup(int(x1), int(y1), int(x2), int(y2))
|
||||
|
||||
def _popdown_timeout_cb(self):
|
||||
self._popdown_sid = 0
|
||||
|
||||
if not self._hover_menu:
|
||||
self.popdown()
|
||||
else:
|
||||
self._popdown_on_leave = True
|
||||
|
||||
return False
|
||||
|
||||
def _leave_notify_event_cb(self, view, target, event):
|
||||
self._start_popdown_timeout()
|
Loading…
Reference in New Issue