Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/graphics/spreadbox.py
blob: 24e12f39a95f9f8774d7bf102fe54282fbc5b2fa (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
52
53
54
55
56
57
58
59
60
61
62
# Copyright (C) 2006, Red Hat, Inc.
#
# 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 random

import hippo
import gtk

from sugar.graphics import units

_CELL_WIDTH  = units.grid_to_pixels(1)
_CELL_HEIGHT = units.grid_to_pixels(1)
_GRID_WIDTH  = gtk.gdk.screen_width() / _CELL_WIDTH
_GRID_HEIGHT = gtk.gdk.screen_height() / _CELL_HEIGHT

class SpreadBox(hippo.CanvasBox):
    def __init__(self, **kwargs):
        hippo.CanvasBox.__init__(self, **kwargs)

        self._grid = []

        for i in range(0, _GRID_WIDTH * _GRID_HEIGHT):
            self._grid.append(None)

    def add_item(self, item):
        start_pos = int(random.random() * len(self._grid))

        pos = start_pos
        while self._grid[pos] != None:
            pos = pos + 1
            if pos == len(self._grid):
                pos = 0
            elif pos == start_pos:
                break

        self._grid[pos] = item

        self.append(item, hippo.PACK_FIXED)

        cell_y = int(pos / _GRID_WIDTH)
        cell_x = pos - cell_y * _GRID_WIDTH
        self.set_position(item, cell_x * _CELL_WIDTH, cell_y * _CELL_HEIGHT)

    def remove_item(self, item):
        for i in range(0, len(self._grid)):
            if self._grid[i] == item:
                self._grid[i] = None
        self.remove(item)