sugar-toolkit-gtk3/sugar/canvas/CanvasBox.py

52 lines
1.1 KiB
Python
Raw Normal View History

2006-09-13 13:50:17 +02:00
import goocanvas
class CanvasBox(goocanvas.Group):
VERTICAL = 0
HORIZONTAL = 1
def __init__(self, grid, orientation, padding=0):
goocanvas.Group.__init__(self)
self._grid = grid
self._orientation = orientation
self._padding = padding
self._constraints = {}
self.connect('child-added', self._child_added_cb)
self.connect('child-removed', self._child_removed_cb)
def set_constraints(self, item, width, height):
self._constraints[item] = [width, height]
2006-09-17 03:35:14 +02:00
def _layout(self, start_item):
if start_item == -1:
start_item = self.get_n_children() - 1
2006-09-13 13:50:17 +02:00
2006-09-17 03:35:14 +02:00
pos = 0
2006-09-13 13:50:17 +02:00
i = 0
while i < self.get_n_children():
item = self.get_child(i)
[width, height] = self._constraints[item]
2006-09-17 03:35:14 +02:00
pos += self._padding
2006-09-13 13:50:17 +02:00
if self._orientation == CanvasBox.VERTICAL:
2006-09-17 03:35:14 +02:00
x = self._padding
y = pos
pos += height + self._padding
2006-09-13 13:50:17 +02:00
else:
2006-09-17 03:35:14 +02:00
x = pos
y = self._padding
pos += width + self._padding
if i >= start_item:
self._grid.set_constraints(item, x, y, width, height)
2006-09-13 13:50:17 +02:00
i += 1
def _child_added_cb(self, item, position):
2006-09-17 03:35:14 +02:00
self._layout(position)
2006-09-13 13:50:17 +02:00
def _child_removed_cb(self, item, position):
2006-09-17 03:35:14 +02:00
self._layout(position)