From 5086f2835e12ce4feebfa6e7c9d1e288552a1534 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Wed, 13 Sep 2006 13:50:17 +0200 Subject: [PATCH] Forgot to add files --- sugar/canvas/CanvasBox.py | 43 +++++++++++++++++++++++++++++++++++++++ sugar/canvas/Grid.py | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 sugar/canvas/CanvasBox.py create mode 100644 sugar/canvas/Grid.py diff --git a/sugar/canvas/CanvasBox.py b/sugar/canvas/CanvasBox.py new file mode 100644 index 00000000..15877a48 --- /dev/null +++ b/sugar/canvas/CanvasBox.py @@ -0,0 +1,43 @@ +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): + x = self._padding + y = self._padding + + i = 0 + while i < self.get_n_children(): + item = self.get_child(i) + [width, height] = self._constraints[item] + + self._grid.set_constraints(item, x, y, width, height) + + if self._orientation == CanvasBox.VERTICAL: + y += height + self._padding + else: + x += width + self._padding + + i += 1 + + def _child_added_cb(self, item, position): + self._layout() + + def _child_removed_cb(self, item, position): + self._layout() diff --git a/sugar/canvas/Grid.py b/sugar/canvas/Grid.py new file mode 100644 index 00000000..59d2695e --- /dev/null +++ b/sugar/canvas/Grid.py @@ -0,0 +1,39 @@ +import gtk +import goocanvas +import cairo + +class Grid: + COLS = 80.0 + + def set_constraints(self, component, x, y, width=-1, height=-1): + if isinstance(component, gtk.Window): + self._layout_window(component, x, y, width, height) + elif isinstance(component, goocanvas.Item): + self._layout_item(component, x, y, width, height) + elif isinstance(component, goocanvas.CanvasView): + self._layout_canvas(component, x, y, width, height) + + def _layout_window(self, window, x, y, width, height): + scale = gtk.gdk.screen_width() / Grid.COLS + + window.move(int(x * scale), int(y * scale)) + window.resize(int(width * scale), int(height * scale)) + + def _layout_item(self, item, x, y, width, height): + scale = 1200 / Grid.COLS + + matrix = cairo.Matrix(1, 0, 0, 1, 0, 0) + matrix.translate(x * scale, y * scale) + item.set_transform(matrix) + + if width > 0 and height > 0: + try: + item.props.width = width * scale + item.props.height = height * scale + except: + item.props.size = width * scale + + def _layout_canvas(self, canvas, x, y, width, height): + scale = 1200 / Grid.COLS + + canvas.set_bounds(x * scale, y * scale, width * scale, height * scale)