2006-10-15 01:08:44 +02:00
|
|
|
# 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.
|
|
|
|
|
2006-10-06 11:11:38 +02:00
|
|
|
import random
|
|
|
|
|
|
|
|
import hippo
|
2007-03-07 12:11:14 +01:00
|
|
|
import gtk
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
from sugar.graphics import units
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-04-16 10:36:15 +02:00
|
|
|
_WIDTH = gtk.gdk.screen_width()
|
|
|
|
_HEIGHT = gtk.gdk.screen_height()
|
2007-03-07 12:11:14 +01:00
|
|
|
_CELL_WIDTH = units.grid_to_pixels(1)
|
|
|
|
_CELL_HEIGHT = units.grid_to_pixels(1)
|
2007-04-16 10:36:15 +02:00
|
|
|
_GRID_WIDTH = _WIDTH / _CELL_WIDTH
|
|
|
|
_GRID_HEIGHT = _HEIGHT / _CELL_HEIGHT
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-04-16 10:36:15 +02:00
|
|
|
class SpreadBox(hippo.CanvasBox, hippo.CanvasItem):
|
|
|
|
__gtype_name__ = 'SugarSpreadBox'
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
hippo.CanvasBox.__init__(self, **kwargs)
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
self._grid = []
|
2007-04-16 10:36:15 +02:00
|
|
|
self._center = None
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
for i in range(0, _GRID_WIDTH * _GRID_HEIGHT):
|
|
|
|
self._grid.append(None)
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-04-16 10:36:15 +02:00
|
|
|
def set_center_item(self, item):
|
|
|
|
if self._center:
|
|
|
|
self.remove(self._center)
|
|
|
|
|
|
|
|
self._center = item
|
|
|
|
self.append(item, hippo.PACK_FIXED)
|
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
def add_item(self, item):
|
|
|
|
start_pos = int(random.random() * len(self._grid))
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
pos = start_pos
|
|
|
|
while self._grid[pos] != None:
|
|
|
|
pos = pos + 1
|
|
|
|
if pos == len(self._grid):
|
|
|
|
pos = 0
|
|
|
|
elif pos == start_pos:
|
|
|
|
break
|
2006-10-07 14:33:08 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
self._grid[pos] = item
|
2006-10-07 14:33:08 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
self.append(item, hippo.PACK_FIXED)
|
2006-10-07 14:33:08 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
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)
|
2006-10-07 14:33:08 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
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)
|
2007-04-16 10:36:15 +02:00
|
|
|
|
|
|
|
def do_allocate(self, width, height, origin_changed):
|
|
|
|
hippo.CanvasBox.do_allocate(self, width, height, origin_changed)
|
|
|
|
|
|
|
|
if self._center:
|
|
|
|
[icon_width, icon_height] = self._center.get_allocation()
|
|
|
|
self.set_position(self._center, (width - icon_width) / 2,
|
|
|
|
(height - icon_height) / 2)
|