Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/canvas/CanvasBox.py
blob: b990b895339e6ec6e23e5869bfa8f516c38545e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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]

	def _layout(self, start_item):
		if start_item == -1:
			start_item = self.get_n_children() - 1

		pos = 0
		i = 0
		while i < self.get_n_children():
			item = self.get_child(i)
			[width, height] = self._constraints[item]

			pos += self._padding

			if self._orientation == CanvasBox.VERTICAL:
				x = self._padding
				y = pos
				pos += height + self._padding
			else:
				x = pos
				y = self._padding
				pos += width + self._padding

			if i >= start_item:
				self._grid.set_constraints(item, x, y, width, height)

			i += 1

	def _child_added_cb(self, item, position):
		self._layout(position)

	def _child_removed_cb(self, item, position):
		self._layout(position)