Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--NEWS10
-rw-r--r--PukllanapacActivity.py110
-rw-r--r--activity/activity.info3
-rw-r--r--card.py6
-rwxr-xr-x[-rw-r--r--]gentiles.py0
-rw-r--r--grid.py5
-rw-r--r--po/cs.po38
-rw-r--r--po/el.po38
-rw-r--r--po/fr.po6
-rw-r--r--po/pl.po39
-rwxr-xr-xsetup.py2
-rw-r--r--sprites.py241
-rw-r--r--toolbar_utils.py37
-rw-r--r--window.py35
14 files changed, 342 insertions, 228 deletions
diff --git a/NEWS b/NEWS
index 28d569e..2d479c1 100644
--- a/NEWS
+++ b/NEWS
@@ -1,13 +1,3 @@
-11
-
-ENHANCEMENTS
-* Ignacio Rodriguez port to gtk3
-
-9
-
-ENHANCEMENT
-* New translations
-
8
ENHANCEMENTS
diff --git a/PukllanapacActivity.py b/PukllanapacActivity.py
index f38891f..9777b5e 100644
--- a/PukllanapacActivity.py
+++ b/PukllanapacActivity.py
@@ -1,5 +1,4 @@
#Copyright (c) 2010 Walter Bender
-#Copyright (c) 2012 Ignacio Rodriguez
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -10,19 +9,27 @@
# License along with this library; if not, write to the
# Free Software Foundation, 51 Franklin Street, Suite 500 Boston, MA
# 02110-1335 USA
-from gi.repository import GObject, Gtk, Gdk
+
import pygtk
pygtk.require('2.0')
+import gtk
+import gobject
+
+import sugar
+from sugar.activity import activity
+try:
+ from sugar.graphics.toolbarbox import ToolbarBox
+ _have_toolbox = True
+except ImportError:
+ _have_toolbox = False
-import sugar3
-from sugar3.activity import activity
-from sugar3.graphics.toolbarbox import ToolbarBox
-from sugar3.bundle.activitybundle import ActivityBundle
-from sugar3.activity.widgets import ActivityToolbarButton
-from sugar3.activity.widgets import StopButton
-from sugar3.graphics.toolbarbox import ToolbarButton
+if _have_toolbox:
+ from sugar.bundle.activitybundle import ActivityBundle
+ from sugar.activity.widgets import ActivityToolbarButton
+ from sugar.activity.widgets import StopButton
+ from sugar.graphics.toolbarbox import ToolbarButton
-from sugar3.datastore import datastore
+from sugar.datastore import datastore
from gettext import gettext as _
import locale
@@ -48,12 +55,12 @@ class PukllanapacActivity(activity.Activity):
self._play_level = 0
self._play_mode = 0
- self._setup_toolbars()
+ self._setup_toolbars(_have_toolbox)
# Create a canvas
- canvas = Gtk.DrawingArea()
- canvas.set_size_request(Gdk.Screen.width(), \
- Gdk.Screen.height())
+ canvas = gtk.DrawingArea()
+ canvas.set_size_request(gtk.gdk.screen_width(), \
+ gtk.gdk.screen_height())
self.set_canvas(canvas)
canvas.show()
self.show_all()
@@ -88,54 +95,62 @@ class PukllanapacActivity(activity.Activity):
self.metadata['rotate' + str(i)] = str(
self.win.grid.card_table[self.win.grid.grid[i]].orientation)
- def _setup_toolbars(self):
+ def _setup_toolbars(self, have_toolbox):
""" Setup the toolbars.. """
- toolbox = ToolbarBox()
+ if have_toolbox:
+ toolbox = ToolbarBox()
+
+ # Activity toolbar
+ activity_button = ActivityToolbarButton(self)
+
+ toolbox.toolbar.insert(activity_button, 0)
+ activity_button.show()
- # Activity toolbar
- activity_button = ActivityToolbarButton(self)
+ self.set_toolbar_box(toolbox)
+ toolbox.show()
+ toolbar = toolbox.toolbar
- toolbox.toolbar.insert(activity_button, 0)
- activity_button.show()
- self.set_toolbar_box(toolbox)
- toolbox.show()
- toolbar = toolbox.toolbar
+ else:
+ # Use pre-0.86 toolbar design
+ games_toolbar = gtk.Toolbar()
+ toolbox = activity.ActivityToolbox(self)
+ self.set_toolbox(toolbox)
+ toolbox.add_toolbar(_('Game'), games_toolbar)
+ toolbox.show()
+ toolbox.set_current_toolbar(1)
+ toolbar = games_toolbar
# Add the buttons and labels to the toolbars
+ self.level_button = button_factory(
+ LEVEL_ICONS[self._play_level], toolbar, self.change_play_level_cb,
+ tooltip=_('Set difficulty level.'))
mode = self._play_mode
mode += 1
if mode == len(GAME_ICONS):
mode = 0
self.game_buttons = []
- group = None
for i in range(len(GAME_ICONS)):
- self.game_buttons.append(radio_factory(GAME_ICONS[i],
- toolbar,
- self.change_play_mode_cb,
- cb_arg=i,
- tooltip=_('Select game.'),
- group=group))
- group = self.game_buttons[0]
+ if i==0:
+ self.game_buttons.append(radio_factory(
+ GAME_ICONS[0], toolbar, self.change_play_mode_cb,
+ cb_arg=0, tooltip=_('Select game.'), group=None))
+ else:
+ self.game_buttons.append(radio_factory(
+ GAME_ICONS[i], toolbar, self.change_play_mode_cb,
+ cb_arg=i, tooltip=_('Select game.'),
+ group=self.game_buttons[0]))
self.game_buttons[mode].set_active(True)
+ separator_factory(toolbar, False, True)
+ self.status_label = label_factory(toolbar, _("drag to swap"))
- separator_factory(toolbar, expand=False, visible=True)
-
- self.level_button = button_factory(LEVEL_ICONS[self._play_level],
- toolbar,
- self.change_play_level_cb,
- tooltip=_('Set difficulty level.'))
-
- self.status_label = label_factory(toolbar,
- _("drag to swap"),
- width=200)
-
- separator_factory(toolbox.toolbar, expand=True, visible=False)
+ if _have_toolbox:
+ separator_factory(toolbox.toolbar, True, False)
- stop_button = StopButton(self)
- stop_button.props.accelerator = '<Ctrl>q'
- toolbox.toolbar.insert(stop_button, -1)
- stop_button.show()
+ stop_button = StopButton(self)
+ stop_button.props.accelerator = '<Ctrl>q'
+ toolbox.toolbar.insert(stop_button, -1)
+ stop_button.show()
def change_play_level_cb(self, button=None, play_level=None):
""" Cycle between levels """
@@ -168,7 +183,6 @@ class PukllanapacActivity(activity.Activity):
self.win.grid.initialize_cards(self.win.sprites, self.win.path,
self.win.card_dim, self.win.scale,
GAME_ICONS[self._play_mode])
- # Only one level (difficult) for modes > 0
if self._play_mode > 0:
self._play_level = len(LEVEL_ICONS) - 1
self.level_button.set_icon(LEVEL_ICONS[self._play_level])
diff --git a/activity/activity.info b/activity/activity.info
index 3785a57..c1f03ed 100644
--- a/activity/activity.info
+++ b/activity/activity.info
@@ -1,9 +1,8 @@
[Activity]
name = Pukllanapac
-activity_version = 11
+activity_version = 8
license = GPLv3
bundle_id = org.sugarlabs.PukllanapacActivity
exec = sugar-activity PukllanapacActivity.PukllanapacActivity
icon = activity-pukllanapac
show_launcher = yes
-summary = a pattern-matching game \ No newline at end of file
diff --git a/card.py b/card.py
index e098980..8b2f489 100644
--- a/card.py
+++ b/card.py
@@ -1,5 +1,4 @@
#Copyright (c) 2010, 2011 Walter Bender
-#Copyright (c) 2012 Ignacio Rodriguez
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -11,15 +10,16 @@
# Free Software Foundation, 51 Franklin Street, Suite 500 Boston, MA
# 02110-1335 USA
-from gi.repository import Gtk, Gdk, GdkPixbuf
import pygtk
pygtk.require('2.0')
+import gtk
+
from sprites import Sprite
def load_image(file, w, h):
""" Convert from file to pixbuf at size w, h """
- return GdkPixbuf.Pixbuf.new_from_file_at_size(file, int(w), int(h))
+ return gtk.gdk.pixbuf_new_from_file_at_size(file, int(w), int(h))
class Card:
diff --git a/gentiles.py b/gentiles.py
index 781080d..781080d 100644..100755
--- a/gentiles.py
+++ b/gentiles.py
diff --git a/grid.py b/grid.py
index a2730b6..38841ed 100644
--- a/grid.py
+++ b/grid.py
@@ -1,5 +1,4 @@
#Copyright (c) 2010, 2011 Walter Bender
-#Copyright (c) 2012 Ignacio Rodriguez
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -10,9 +9,11 @@
# License along with this library; if not, write to the
# Free Software Foundation, 51 Franklin Street, Suite 500 Boston, MA
# 02110-1335 USA
-from gi.repository import GObject, Gtk, Gdk
+
import pygtk
pygtk.require('2.0')
+import gtk
+import gobject
from card import Card
diff --git a/po/cs.po b/po/cs.po
new file mode 100644
index 0000000..e367ffb
--- /dev/null
+++ b/po/cs.po
@@ -0,0 +1,38 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2011-08-03 10:21-0400\n"
+"PO-Revision-Date: 2012-06-29 10:16+0200\n"
+"Last-Translator: jui <appukonrad@gmail.com>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"
+"X-Generator: Pootle 2.0.5\n"
+
+#: activity/activity.info:2
+msgid "Pukllanapac"
+msgstr "Pukllanapac"
+
+#: PukllanapacActivity.py:162
+msgid "Game"
+msgstr "Hra"
+
+#: PukllanapacActivity.py:169
+msgid "Set difficulty level."
+msgstr "Nastavit úroveň obtížnosti."
+
+#: PukllanapacActivity.py:176
+msgid "Select game."
+msgstr "Vybrat hru."
+
+#: PukllanapacActivity.py:179
+msgid "drag to swap"
+msgstr "tažením vyměnit"
diff --git a/po/el.po b/po/el.po
new file mode 100644
index 0000000..b3fd8f3
--- /dev/null
+++ b/po/el.po
@@ -0,0 +1,38 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2011-08-03 10:21-0400\n"
+"PO-Revision-Date: 2012-09-25 12:23+0200\n"
+"Last-Translator: Yannis <kiolalis@gmail.com>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+"X-Generator: Pootle 2.0.5\n"
+
+#: activity/activity.info:2
+msgid "Pukllanapac"
+msgstr "Pukllanapac"
+
+#: PukllanapacActivity.py:162
+msgid "Game"
+msgstr "Παιχνίδι"
+
+#: PukllanapacActivity.py:169
+msgid "Set difficulty level."
+msgstr "Ορισμός επιπέδου δυσκολίας"
+
+#: PukllanapacActivity.py:176
+msgid "Select game."
+msgstr "Επιλογή παιχνιδιού"
+
+#: PukllanapacActivity.py:179
+msgid "drag to swap"
+msgstr "μεταφορά για ανταλλαγή"
diff --git a/po/fr.po b/po/fr.po
index 8fdd46f..c1e420e 100644
--- a/po/fr.po
+++ b/po/fr.po
@@ -7,8 +7,8 @@ msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-08-03 10:21-0400\n"
-"PO-Revision-Date: 2012-02-05 12:40+0200\n"
-"Last-Translator: samy boutayeb <s.boutayeb@free.fr>\n"
+"PO-Revision-Date: 2012-08-15 09:20+0200\n"
+"Last-Translator: Chris <cjl@laptop.org>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
@@ -31,7 +31,7 @@ msgstr "Choisir le niveau de difficulté."
#: PukllanapacActivity.py:176
msgid "Select game."
-msgstr "Choisir le jeu"
+msgstr "Choisir le jeu."
#: PukllanapacActivity.py:179
msgid "drag to swap"
diff --git a/po/pl.po b/po/pl.po
new file mode 100644
index 0000000..b8683ed
--- /dev/null
+++ b/po/pl.po
@@ -0,0 +1,39 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2011-08-03 10:21-0400\n"
+"PO-Revision-Date: 2012-02-01 15:10+0200\n"
+"Last-Translator: lkjlkj <agniechka@o2.pl>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 "
+"|| n%100>=20) ? 1 : 2);\n"
+"X-Generator: Pootle 2.0.5\n"
+
+#: activity/activity.info:2
+msgid "Pukllanapac"
+msgstr "Pukllanapac"
+
+#: PukllanapacActivity.py:162
+msgid "Game"
+msgstr "Gra"
+
+#: PukllanapacActivity.py:169
+msgid "Set difficulty level."
+msgstr "Ustaw poziom trudności."
+
+#: PukllanapacActivity.py:176
+msgid "Select game."
+msgstr "Wybierz grę."
+
+#: PukllanapacActivity.py:179
+msgid "drag to swap"
+msgstr "przeciągnij, aby wymienić"
diff --git a/setup.py b/setup.py
index bdeaed6..bd1e319 100755
--- a/setup.py
+++ b/setup.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-from sugar3.activity import bundlebuilder
+from sugar.activity import bundlebuilder
if __name__ == "__main__":
bundlebuilder.start()
diff --git a/sprites.py b/sprites.py
index 8cad6a6..baae94f 100644
--- a/sprites.py
+++ b/sprites.py
@@ -24,7 +24,7 @@
'''
sprites.py is a simple sprites library for managing graphics objects,
-'sprites', on a Gtk.DrawingArea. It manages multiple sprites with
+'sprites', on a gtk.DrawingArea. It manages multiple sprites with
methods such as move, hide, set_layer, etc.
There are two classes:
@@ -68,7 +68,7 @@ Example usage:
# method for converting SVG to a gtk pixbuf
def svg_str_to_pixbuf(svg_string):
- pl = GdkPixbuf.PixbufLoader('svg')
+ pl = gtk.gdk.PixbufLoader('svg')
pl.write(svg_string)
pl.close()
pixbuf = pl.get_pixbuf()
@@ -76,20 +76,21 @@ def svg_str_to_pixbuf(svg_string):
'''
-import gi
-from gi.repository import Gtk, GdkPixbuf, Gdk
-from gi.repository import Pango, PangoCairo
+import pygtk
+pygtk.require('2.0')
+import gtk
+import pango
+import pangocairo
import cairo
-
class Sprites:
''' A class for the list of sprites and everything they share in common '''
def __init__(self, widget):
''' Initialize an empty array of sprites '''
- self.cr = None
self.widget = widget
self.list = []
+ self.cr = None
def set_cairo_context(self, cr):
''' Cairo context may be set or reset after __init__ '''
@@ -97,7 +98,7 @@ class Sprites:
def get_sprite(self, i):
''' Return a sprint from the array '''
- if i < 0 or i > len(self.list)-1:
+ if i < 0 or i > len(self.list) - 1:
return(None)
else:
return(self.list[i])
@@ -159,13 +160,11 @@ class Sprite:
''' Initialize an individual sprite '''
self._sprites = sprites
self.save_xy = (x, y) # remember initial (x, y) position
- self.rect = [int(x), int(y), 0, 0]
+ self.rect = gtk.gdk.Rectangle(int(x), int(y), 0, 0)
self._scale = [12]
self._rescale = [True]
self._horiz_align = ["center"]
self._vert_align = ["middle"]
- self._x_pos = [None]
- self._y_pos = [None]
self._fd = None
self._bold = False
self._italic = False
@@ -173,7 +172,7 @@ class Sprite:
self._margins = [0, 0, 0, 0]
self.layer = 100
self.labels = []
- self.images = []
+ self.cached_surfaces = []
self._dx = [] # image offsets
self._dy = []
self.type = None
@@ -182,47 +181,54 @@ class Sprite:
def set_image(self, image, i=0, dx=0, dy=0):
''' Add an image to the sprite. '''
- while len(self.images) < i + 1:
- self.images.append(None)
+ while len(self.cached_surfaces) < i + 1:
+ self.cached_surfaces.append(None)
self._dx.append(0)
self._dy.append(0)
- self.images[i] = image
self._dx[i] = dx
self._dy[i] = dy
- if hasattr(self.images[i], 'get_width'):
- w = self.images[i].get_width()
- h = self.images[i].get_height()
+ if isinstance(image, gtk.gdk.Pixbuf):
+ w = image.get_width()
+ h = image.get_height()
else:
- w, h = self.images[i].get_size()
+ w, h = image.get_size()
if i == 0: # Always reset width and height when base image changes.
- self.rect[2] = w + dx
- self.rect[3] = h + dy
+ self.rect.width = w + dx
+ self.rect.height = h + dy
else:
- if w + dx > self.rect[2]:
- self.rect[2] = w + dx
- if h + dy > self.rect[3]:
- self.rect[3] = h + dy
+ if w + dx > self.rect.width:
+ self.rect.width = w + dx
+ if h + dy > self.rect.height:
+ self.rect.height = h + dy
+ surface = cairo.ImageSurface(
+ cairo.FORMAT_ARGB32, self.rect.width, self.rect.height)
+ context = cairo.Context(surface)
+ context = gtk.gdk.CairoContext(context)
+ context.set_source_pixbuf(image, 0, 0)
+ context.rectangle(0, 0, self.rect.width, self.rect.height)
+ context.fill()
+ self.cached_surfaces[i] = surface
def move(self, pos):
''' Move to new (x, y) position '''
self.inval()
- self.rect[0], self.rect[1] = int(pos[0]), int(pos[1])
+ self.rect.x, self.rect.y = int(pos[0]), int(pos[1])
self.inval()
def move_relative(self, pos):
''' Move to new (x+dx, y+dy) position '''
self.inval()
- self.rect[0] += int(pos[0])
- self.rect[1] += int(pos[1])
+ self.rect.x += int(pos[0])
+ self.rect.y += int(pos[1])
self.inval()
def get_xy(self):
''' Return current (x, y) position '''
- return (self.rect[0], self.rect[1])
+ return (self.rect.x, self.rect.y)
def get_dimensions(self):
''' Return current size '''
- return (self.rect[2], self.rect[3])
+ return (self.rect.width, self.rect.height)
def get_layer(self):
''' Return current layer '''
@@ -240,7 +246,7 @@ class Sprite:
if layer is not None:
self.layer = layer
for i in range(self._sprites.length_of_list()):
- if self.layer < self._sprites.get_sprite(i).layer:
+ if layer < self._sprites.get_sprite(i).layer:
self._sprites.insert_in_list(self, i)
self.inval()
return
@@ -273,12 +279,10 @@ class Sprite:
self._rescale.append(self._rescale[0])
self._horiz_align.append(self._horiz_align[0])
self._vert_align.append(self._vert_align[0])
- self._x_pos.append(self._x_pos[0])
- self._y_pos.append(self._y_pos[0])
def set_font(self, font):
''' Set the font for a label '''
- self._fd = Pango.FontDescription(font)
+ self._fd = pango.FontDescription(font)
def set_label_color(self, rgb):
''' Set the font color for a label '''
@@ -296,15 +300,13 @@ class Sprite:
return
def set_label_attributes(self, scale, rescale=True, horiz_align="center",
- vert_align="middle", x_pos=None, y_pos=None, i=0):
+ vert_align="middle", i=0):
''' Set the various label attributes '''
self._extend_labels_array(i)
self._scale[i] = scale
self._rescale[i] = rescale
self._horiz_align[i] = horiz_align
self._vert_align[i] = vert_align
- self._x_pos[i] = x_pos
- self._y_pos[i] = y_pos
def hide(self):
''' Hide a sprite '''
@@ -317,149 +319,136 @@ class Sprite:
def inval(self):
''' Invalidate a region for gtk '''
- # self._sprites.window.invalidate_rect(self.rect, False)
- self._sprites.widget.queue_draw_area(self.rect[0],
- self.rect[1],
- self.rect[2],
- self.rect[3])
+ self._sprites.widget.queue_draw_area(self.rect.x,
+ self.rect.y,
+ self.rect.width,
+ self.rect.height)
def draw(self, cr=None):
''' Draw the sprite (and label) '''
if cr is None:
- cr = self._sprites.cr
- if cr is None:
print 'sprite.draw: no Cairo context.'
return
- for i, img in enumerate(self.images):
- if isinstance(img, GdkPixbuf.Pixbuf):
- Gdk.cairo_set_source_pixbuf(cr, img,
- self.rect[0] + self._dx[i],
- self.rect[1] + self._dy[i])
- cr.rectangle(self.rect[0] + self._dx[i],
- self.rect[1] + self._dy[i],
- self.rect[2],
- self.rect[3])
- cr.fill()
- elif type(img) == cairo.ImageSurface:
- cr.set_source_surface(img, self.rect[0] + self._dx[i],
- self.rect[1] + self._dy[i])
- cr.rectangle(self.rect[0] + self._dx[i],
- self.rect[1] + self._dy[i],
- self.rect[2],
- self.rect[3])
- cr.fill()
- else:
- print 'sprite.draw: source not a pixbuf (%s)' % (type(img))
+ for i, surface in enumerate(self.cached_surfaces):
+ cr.set_source_surface(surface,
+ self.rect.x + self._dx[i],
+ self.rect.y + self._dy[i])
+ cr.rectangle(self.rect.x + self._dx[i],
+ self.rect.y + self._dy[i],
+ self.rect.width,
+ self.rect.height)
+ cr.fill()
if len(self.labels) > 0:
self.draw_label(cr)
def hit(self, pos):
''' Is (x, y) on top of the sprite? '''
x, y = pos
- if x < self.rect[0]:
+ if x < self.rect.x:
return False
- if x > self.rect[0] + self.rect[2]:
+ if x > self.rect.x + self.rect.width:
return False
- if y < self.rect[1]:
+ if y < self.rect.y:
return False
- if y > self.rect[1] + self.rect[3]:
+ if y > self.rect.y + self.rect.height:
return False
return True
def draw_label(self, cr):
''' Draw the label based on its attributes '''
- my_width = self.rect[2] - self._margins[0] - self._margins[2]
+ # Create a pangocairo context
+ cr = pangocairo.CairoContext(cr)
+ my_width = self.rect.width - self._margins[0] - self._margins[2]
if my_width < 0:
my_width = 0
- my_height = self.rect[3] - self._margins[1] - self._margins[3]
+ my_height = self.rect.height - self._margins[1] - self._margins[3]
for i in range(len(self.labels)):
- pl = PangoCairo.create_layout(cr)
- pl.set_text(str(self.labels[i]), -1)
- self._fd.set_size(int(self._scale[i] * Pango.SCALE))
+ pl = cr.create_layout()
+ pl.set_text(str(self.labels[i]))
+ self._fd.set_size(int(self._scale[i] * pango.SCALE))
pl.set_font_description(self._fd)
- w = pl.get_size()[0] / Pango.SCALE
+ w = pl.get_size()[0] / pango.SCALE
if w > my_width:
if self._rescale[i]:
self._fd.set_size(
- int(self._scale[i] * Pango.SCALE * my_width / w))
+ int(self._scale[i] * pango.SCALE * my_width / w))
pl.set_font_description(self._fd)
- w = pl.get_size()[0] / Pango.SCALE
+ w = pl.get_size()[0] / pango.SCALE
else:
j = len(self.labels[i]) - 1
while(w > my_width and j > 0):
pl.set_text(
- "…" + self.labels[i][len(self.labels[i]) - j:], -1)
- self._fd.set_size(int(self._scale[i] * Pango.SCALE))
+ "…" + self.labels[i][len(self.labels[i]) - j:])
+ self._fd.set_size(int(self._scale[i] * pango.SCALE))
pl.set_font_description(self._fd)
- w = pl.get_size()[0] / Pango.SCALE
+ w = pl.get_size()[0] / pango.SCALE
j -= 1
- if self._x_pos[i] is not None:
- x = int(self.rect[0] + self._x_pos[i])
- elif self._horiz_align[i] == "center":
- x = int(self.rect[0] + self._margins[0] + (my_width - w) / 2)
+ if self._horiz_align[i] == "center":
+ x = int(self.rect.x + self._margins[0] + (my_width - w) / 2)
elif self._horiz_align[i] == 'left':
- x = int(self.rect[0] + self._margins[0])
+ x = int(self.rect.x + self._margins[0])
else: # right
- x = int(self.rect[0] + self.rect[2] - w - self._margins[2])
- h = pl.get_size()[1] / Pango.SCALE
- if self._y_pos[i] is not None:
- y = int(self.rect[1] + self._y_pos[i])
- elif self._vert_align[i] == "middle":
- y = int(self.rect[1] + self._margins[1] + (my_height - h) / 2)
+ x = int(self.rect.x + self.rect.width - w - self._margins[2])
+ h = pl.get_size()[1] / pango.SCALE
+ if self._vert_align[i] == "middle":
+ y = int(self.rect.y + self._margins[1] + (my_height - h) / 2)
elif self._vert_align[i] == "top":
- y = int(self.rect[1] + self._margins[1])
+ y = int(self.rect.y + self._margins[1])
else: # bottom
- y = int(self.rect[1] + self.rect[3] - h - self._margins[3])
+ y = int(self.rect.y + self.rect.height - h - self._margins[3])
cr.save()
cr.translate(x, y)
cr.set_source_rgb(self._color[0], self._color[1], self._color[2])
- PangoCairo.update_layout(cr, pl)
- PangoCairo.show_layout(cr, pl)
+ cr.update_layout(pl)
+ cr.show_layout(pl)
cr.restore()
- def label_width(self, cr=None):
+ def label_width(self):
''' Calculate the width of a label '''
- if cr is None:
- cr = self._sprites.cr
- max = 0
- for i in range(len(self.labels)):
- pl = PangoCairo.create_layout(cr)
- pl.set_text(str(self.labels[i]), -1)
- self._fd.set_size(int(self._scale[i] * Pango.SCALE))
- pl.set_font_description(self._fd)
- w = pl.get_size()[0] / Pango.SCALE
- if w > max:
- max = w
- return max
+ cr = pangocairo.CairoContext(self._sprites.cr)
+ if cr is not None:
+ max = 0
+ for i in range(len(self.labels)):
+ pl = cr.create_layout()
+ pl.set_text(self.labels[i])
+ self._fd.set_size(int(self._scale[i] * pango.SCALE))
+ pl.set_font_description(self._fd)
+ w = pl.get_size()[0] / pango.SCALE
+ if w > max:
+ max = w
+ return max
+ else:
+ return self.rect.width
def label_safe_width(self):
''' Return maximum width for a label '''
- return self.rect[2] - self._margins[0] - self._margins[2]
+ return self.rect.width - self._margins[0] - self._margins[2]
def label_safe_height(self):
''' Return maximum height for a label '''
- return self.rect[3] - self._margins[1] - self._margins[3]
+ return self.rect.height - self._margins[1] - self._margins[3]
def label_left_top(self):
''' Return the upper-left corner of the label safe zone '''
return(self._margins[0], self._margins[1])
def get_pixel(self, pos, i=0):
- ''' Return the pixl at (x, y) '''
- x, y = pos
- x = x - self.rect[0]
- y = y - self.rect[1]
- if y > self.images[i].get_height() - 1:
- return(-1, -1, -1, -1)
- try:
- array = self.images[i].get_pixels()
- if array is not None:
- offset = (y * self.images[i].get_width() + x) * 4
- r, g, b, a = ord(array[offset]), ord(array[offset + 1]),\
- ord(array[offset + 2]), ord(array[offset + 3])
- return(r, g, b, a)
- else:
- return(-1, -1, -1, -1)
- except IndexError:
- print "Index Error: %d %d" % (len(array), offset)
+ ''' Return the pixel at (x, y) '''
+ x = int(pos[0] - self.rect.x)
+ y = int(pos[1] - self.rect.y)
+ if x < 0 or x > (self.rect.width - 1) or \
+ y < 0 or y > (self.rect.height - 1):
return(-1, -1, -1, -1)
+
+ # create a new 1x1 cairo surface
+ cs = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1);
+ cr = cairo.Context(cs)
+ cr.set_source_surface(self.cached_surfaces[i], -x, -y)
+ cr.rectangle(0,0,1,1)
+ cr.set_operator(cairo.OPERATOR_SOURCE)
+ cr.fill()
+ cs.flush() # ensure all writing is done
+ # Read the pixel
+ pixels = cs.get_data()
+ return (ord(pixels[2]), ord(pixels[1]), ord(pixels[0]), 0)
+
diff --git a/toolbar_utils.py b/toolbar_utils.py
index 72f7100..94e6883 100644
--- a/toolbar_utils.py
+++ b/toolbar_utils.py
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2011, Walter Bender
-# Port To GTK3:
-# Ignacio Rodriguez <ignaciorodriguez@sugarlabs.org>
+
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
@@ -12,12 +11,12 @@
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
-from gi.repository import Gtk
+import gtk
-from sugar3.graphics.radiotoolbutton import RadioToolButton
-from sugar3.graphics.toolbutton import ToolButton
-from sugar3.graphics.combobox import ComboBox
-from sugar3.graphics.toolcombobox import ToolComboBox
+from sugar.graphics.radiotoolbutton import RadioToolButton
+from sugar.graphics.toolbutton import ToolButton
+from sugar.graphics.combobox import ComboBox
+from sugar.graphics.toolcombobox import ToolComboBox
def combo_factory(combo_array, toolbar, callback, cb_arg=None,
@@ -33,7 +32,7 @@ def combo_factory(combo_array, toolbar, callback, cb_arg=None,
for i, selection in enumerate(combo_array):
combo.append_item(i, selection, None)
combo.show()
- toolitem = Gtk.ToolItem()
+ toolitem = gtk.ToolItem()
toolitem.add(combo)
if hasattr(toolbar, 'insert'): # the main toolbar
toolbar.insert(toolitem, -1)
@@ -47,13 +46,13 @@ def combo_factory(combo_array, toolbar, callback, cb_arg=None,
def entry_factory(default_string, toolbar, tooltip=None, max=3):
''' Factory for adding a text box to a toolbar '''
- entry = Gtk.Entry()
+ entry = gtk.Entry()
entry.set_text(default_string)
if tooltip is not None and hasattr(entry, 'set_tooltip_text'):
entry.set_tooltip_text(tooltip)
entry.set_width_chars(max)
entry.show()
- toolitem = Gtk.ToolItem()
+ toolitem = gtk.ToolItem()
toolitem.add(entry)
if hasattr(toolbar, 'insert'): # the main toolbar
toolbar.insert(toolitem, -1)
@@ -88,7 +87,7 @@ def radio_factory(name, toolbar, callback, cb_arg=None, tooltip=None,
group=None):
''' Add a radio button to a toolbar '''
button = RadioToolButton(group=group)
- button.set_icon_name(name)
+ button.set_named_icon(name)
if callback is not None:
if cb_arg is None:
button.connect('clicked', callback)
@@ -106,12 +105,12 @@ def radio_factory(name, toolbar, callback, cb_arg=None, tooltip=None,
def label_factory(toolbar, label_text, width=None):
''' Factory for adding a label to a toolbar '''
- label = Gtk.Label(label_text)
+ label = gtk.Label(label_text)
label.set_line_wrap(True)
if width is not None:
label.set_size_request(width, -1) # doesn't work on XOs
label.show()
- toolitem = Gtk.ToolItem()
+ toolitem = gtk.ToolItem()
toolitem.add(label)
if hasattr(toolbar, 'insert'): # the main toolbar
toolbar.insert(toolitem, -1)
@@ -123,7 +122,7 @@ def label_factory(toolbar, label_text, width=None):
def separator_factory(toolbar, expand=False, visible=True):
''' add a separator to a toolbar '''
- separator = Gtk.SeparatorToolItem()
+ separator = gtk.SeparatorToolItem()
separator.props.draw = visible
separator.set_expand(expand)
if hasattr(toolbar, 'insert'): # the main toolbar
@@ -135,9 +134,9 @@ def separator_factory(toolbar, expand=False, visible=True):
def image_factory(image, toolbar, tooltip=None):
''' Add an image to the toolbar '''
- img = Gtk.Image()
+ img = gtk.Image()
img.set_from_pixbuf(image)
- img_tool = Gtk.ToolItem()
+ img_tool = gtk.ToolItem()
img_tool.add(img)
if tooltip is not None:
img.set_tooltip_text(tooltip)
@@ -150,12 +149,12 @@ def image_factory(image, toolbar, tooltip=None):
def spin_factory(default, min, max, callback, toolbar):
- spin_adj = Gtk.Adjustment(default, min, max, 1, 32, 0)
- spin = Gtk.SpinButton(spin_adj, 0, 0)
+ spin_adj = gtk.Adjustment(default, min, max, 1, 32, 0)
+ spin = gtk.SpinButton(spin_adj, 0, 0)
spin_id = spin.connect('value-changed', callback)
spin.set_numeric(True)
spin.show()
- toolitem = Gtk.ToolItem()
+ toolitem = gtk.ToolItem()
toolitem.add(spin)
if hasattr(toolbar, 'insert'): # the main toolbar
toolbar.insert(toolitem, -1)
diff --git a/window.py b/window.py
index 7300271..307802d 100644
--- a/window.py
+++ b/window.py
@@ -1,5 +1,4 @@
#Copyright (c) 2010,11 Walter Bender
-#Copyright (c) 2012 Ignacio Rodriguez
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -10,15 +9,20 @@
# License along with this library; if not, write to the
# Free Software Foundation, 51 Franklin Street, Suite 500 Boston, MA
# 02110-1335 USA
-from gi.repository import Gtk, GObject, Gdk
+
import pygtk
pygtk.require('2.0')
+import gtk
+import gobject
from math import sqrt
from gettext import gettext as _
-from sugar3.graphics import style
-GRID_CELL_SIZE = style.GRID_CELL_SIZE
+try:
+ from sugar.graphics import style
+ GRID_CELL_SIZE = style.GRID_CELL_SIZE
+except:
+ GRID_CELL_SIZE = 0
from grid import Grid
from sprites import Sprites
@@ -53,14 +57,15 @@ class Game():
self.canvas = canvas
parent.show_all()
- self.canvas.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
- self.canvas.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
- self.canvas.connect("draw", self.__draw_cb)
+ self.canvas.set_flags(gtk.CAN_FOCUS)
+ self.canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK)
+ self.canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
+ self.canvas.connect("expose-event", self._expose_cb)
self.canvas.connect("button-press-event", self._button_press_cb)
self.canvas.connect("button-release-event", self._button_release_cb)
self.canvas.connect("key_press_event", self._keypress_cb)
- self.width = Gdk.Screen.width()
- self.height = Gdk.Screen.height() - GRID_CELL_SIZE
+ self.width = gtk.gdk.screen_width()
+ self.height = gtk.gdk.screen_height() - GRID_CELL_SIZE
self.card_dim = CARD_DIM
self.scale = 0.6 * self.height / (self.card_dim * 3)
@@ -116,16 +121,18 @@ class Game():
self.release = None
if self.test() == True:
if self.level < 2:
- GObject.timeout_add(3000, self.activity.change_play_level_cb,
+ gobject.timeout_add(3000, self.activity.change_play_level_cb,
None)
return True
def _keypress_cb(self, area, event):
""" Keypress is used to ... """
- k = Gdk.keyval_name(event.keyval)
+ k = gtk.gdk.keyval_name(event.keyval)
- def __draw_cb(self, canvas, cr):
- self.sprites.redraw_sprites(cr=cr)
+ def _expose_cb(self, win, event):
+ ''' Callback to handle window expose events '''
+ self.do_expose_event(event)
+ return True
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
@@ -138,7 +145,7 @@ class Game():
self.sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
- Gtk.main_quit()
+ gtk.main_quit()
def mask(self, level):
""" mask out cards not on play level """