Skip to content

Commit 6bd32bf

Browse files
committed
gtk3 port
1 parent 063c2da commit 6bd32bf

File tree

7 files changed

+206
-242
lines changed

7 files changed

+206
-242
lines changed

LetterMatch.py

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# -*- coding: utf-8 -*-
12
# Copyright (c) 2012, 2013 Walter Bender
23
# Copyright (c) 2013 Aneesh Dogra <lionaneesh@gmail.com>
4+
# Copyright (c) 2013 Ignacio Rodríguez <ignacio@sugarlabs.org>
35

46
# This program is free software; you can redistribute it and/or modify
57
# it under the terms of the GNU General Public License as published by
@@ -12,19 +14,19 @@
1214
# Boston, MA 02111-1307, USA.
1315

1416

15-
import gtk
17+
from gi.repository import Gtk, Gdk, GdkPixbuf
1618

17-
from sugar.activity import activity
18-
from sugar.graphics.toolbarbox import ToolbarBox, ToolbarButton
19-
from sugar.activity.widgets import ActivityToolbarButton
20-
from sugar.activity.widgets import StopButton
21-
from sugar.graphics.toolbutton import ToolButton
22-
from sugar.graphics.combobox import ComboBox
23-
from sugar.graphics.toolcombobox import ToolComboBox
24-
from sugar.datastore import datastore
25-
from sugar import profile
26-
from sugar.graphics.objectchooser import ObjectChooser
27-
from sugar import mime
19+
from sugar3.activity import activity
20+
from sugar3.graphics.toolbarbox import ToolbarBox, ToolbarButton
21+
from sugar3.activity.widgets import ActivityToolbarButton
22+
from sugar3.activity.widgets import StopButton
23+
from sugar3.graphics.toolbutton import ToolButton
24+
from sugar3.graphics.combobox import ComboBox
25+
from sugar3.graphics.toolcombobox import ToolComboBox
26+
from sugar3.datastore import datastore
27+
from sugar3 import profile
28+
from sugar3.graphics.objectchooser import ObjectChooser
29+
from sugar3 import mime
2830
from utils.sprites import Sprites, Sprite
2931

3032
from gettext import gettext as _
@@ -89,10 +91,10 @@ def __init__(self, handle):
8991
str(self.metadata['data_from_journal']))
9092
self._setup_toolbars()
9193

92-
self.canvas = gtk.DrawingArea()
93-
self.canvas.set_size_request(gtk.gdk.screen_width(),
94-
gtk.gdk.screen_height())
95-
self.canvas.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("#000000"))
94+
self.canvas = Gtk.DrawingArea()
95+
self.canvas.set_size_request(Gdk.Screen.width(),
96+
Gdk.Screen.height())
97+
self.canvas.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("#000000"))
9698
self.canvas.show()
9799
self.set_canvas(self.canvas)
98100

@@ -103,7 +105,6 @@ def __init__(self, handle):
103105
parent=self)
104106

105107
def _setup_toolbars(self):
106-
''' Just 0.86+ toolbars '''
107108
self.max_participants = 1 # no sharing
108109

109110
toolbox = ToolbarBox()
@@ -112,6 +113,9 @@ def _setup_toolbars(self):
112113
toolbox.toolbar.insert(activity_button, 0)
113114
activity_button.show()
114115

116+
separator = Gtk.SeparatorToolItem()
117+
toolbox.toolbar.insert(separator, -1)
118+
115119
self.set_toolbar_box(toolbox)
116120
toolbox.show()
117121
primary_toolbar = toolbox.toolbar
@@ -143,8 +147,8 @@ def _setup_toolbars(self):
143147
self._choose_audio_from_journal_cb,
144148
tooltip=_("Import Audio"))
145149

146-
container = gtk.ToolItem()
147-
self.letter_entry = gtk.Entry()
150+
container = Gtk.ToolItem()
151+
self.letter_entry = Gtk.Entry()
148152
self.letter_entry.set_max_length(1)
149153
self.letter_entry.set_width_chars(3) # because 1 char looks funny
150154
self.letter_entry.connect('changed', self._set_letter)
@@ -208,7 +212,7 @@ def _init_preview(self):
208212
x = int(self._page._grid_x_offset + w + 12)
209213
y = int(self._page._grid_y_offset + 40)
210214

211-
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
215+
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
212216
os.path.join(self._images_path,'../drawing.png'), w, h)
213217
self.status.set_text(
214218
_('Please choose image and audio objects from the Journal.'))
@@ -256,7 +260,7 @@ def _choose_audio_from_journal_cb(self, event):
256260
self.audio_id = None
257261
chooser = ObjectChooser(what_filter=mime.GENERIC_TYPE_AUDIO)
258262
result = chooser.run()
259-
if result == gtk.RESPONSE_ACCEPT:
263+
if result == Gtk.ResponseType.ACCEPT:
260264
jobject = chooser.get_selected_object()
261265
self.audio_id = str(jobject._object_id)
262266
self.image_button.set_sensitive(True)
@@ -282,7 +286,7 @@ def _choose_image_from_journal_cb(self, event):
282286
self.image_id = None
283287
chooser = ObjectChooser(what_filter=mime.GENERIC_TYPE_IMAGE)
284288
result = chooser.run()
285-
if result == gtk.RESPONSE_ACCEPT:
289+
if result == Gtk.ResponseType.ACCEPT:
286290
jobject = chooser.get_selected_object()
287291
self.image_id = str(jobject._object_id)
288292

@@ -291,7 +295,7 @@ def _choose_image_from_journal_cb(self, event):
291295
w = self._page._card_width
292296
h = self._page._card_height
293297

294-
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
298+
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
295299
jobject.get_file_path(), w, h)
296300
self.preview_image.set_image(pixbuf)
297301
self.preview_image.move((x, y))

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
5
2+
3+
* GTK3 port by Ignacio Rodríguez
4+
15
3
26

37
* Add customization toolbar (Aneesh Dogra)

activity/activity.info

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Activity]
22
name = Letter Match
3-
activity_version = 3
3+
activity_version = 5
44
license = GPLv3
55
bundle_id = org.sugarlabs.LetterMatch
66
exec = sugar-activity LetterMatch.LetterMatch

page.py

+43-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (c) 2012 Walter Bender
2+
# Copyright (c) 2012 Walter Bender <walter.bender@gmail.com>
33
# Copyright (c) 2013 Aneesh Dogra <lionaneesh@gmail.com>
4+
# Copyright (c) 2013 Ignacio Rodríguez <ignacio@sugarlabs.org>
45
# This program is free software; you can redistribute it and/or modify
56
# it under the terms of the GNU General Public License as published by
67
# the Free Software Foundation; either version 3 of the License, or
@@ -11,25 +12,22 @@
1112
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1213
# Boston, MA 02111-1307, USA.
1314

14-
import gtk
15-
import gobject
15+
16+
from gi.repository import Gtk, Gdk, GObject, GdkPixbuf
1617
import os
1718
import codecs
1819
from random import uniform, choice
1920

2021
from gettext import gettext as _
2122

22-
from sugar.datastore import datastore
23+
from sugar3.datastore import datastore
2324
from utils.play_audio import play_audio_from_file
2425

2526
import logging
2627
_logger = logging.getLogger('lettermatch-activity')
2728

28-
try:
29-
from sugar.graphics import style
30-
GRID_CELL_SIZE = style.GRID_CELL_SIZE
31-
except ImportError:
32-
GRID_CELL_SIZE = 0
29+
from sugar3.graphics import style
30+
GRID_CELL_SIZE = style.GRID_CELL_SIZE
3331

3432
from genpieces import generate_card
3533
from utils.sprites import Sprites, Sprite
@@ -66,18 +64,17 @@ def __init__(self, canvas, lessons_path, images_path, sounds_path,
6664
self._canvas = canvas
6765
self._activity.show_all()
6866

69-
self._canvas.set_flags(gtk.CAN_FOCUS)
70-
self._canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK)
71-
self._canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
72-
self._canvas.connect("expose-event", self._expose_cb)
67+
self._canvas.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
68+
self._canvas.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
69+
self._canvas.connect("draw", self.__draw_cb)
7370
self.button_release_event_id = \
7471
self._canvas.connect("button-release-event", self._button_release_cb)
7572
self.button_press_event_id = \
7673
self._canvas.connect("button-press-event", self._button_press_cb)
7774

7875
self._canvas.connect("key_press_event", self._keypress_cb)
79-
self._width = gtk.gdk.screen_width()
80-
self._height = gtk.gdk.screen_height()
76+
self._width = Gdk.Screen.width()
77+
self._height = Gdk.Screen.height()
8178
self._card_height = int((self._height - GRID_CELL_SIZE) / YDIM) \
8279
- GUTTER * 2
8380
self._card_width = int(self._card_height * 4 / 3.)
@@ -106,7 +103,7 @@ def __init__(self, canvas, lessons_path, images_path, sounds_path,
106103
def new_page(self):
107104
''' Load a page of cards '''
108105
if self.timeout is not None:
109-
gobject.source_remove(self.timeout)
106+
GObject.source_remove(self.timeout)
110107
self._hide_cards()
111108
self.new_target()
112109
x = self._grid_x_offset + self._card_width + GUTTER * 3
@@ -173,7 +170,7 @@ def _alpha_cards(self):
173170
h2 = 1.0 - h1
174171
bot.composite(top, 0, int(h1 * top.get_height()),
175172
top.get_width(), int(h2 * top.get_height()),
176-
0, 0, 1, 1, gtk.gdk.INTERP_NEAREST, 255)
173+
0, 0, 1, 1, GdkPixbuf.InterpType.NEAREST, 255)
177174
self._cards.append(Sprite(self._sprites, 0, 0, top))
178175
else:
179176
stroke = self._test_for_stroke()
@@ -207,8 +204,8 @@ def new_target(self):
207204
self.answers[i] = self.target
208205

209206
if self.timeout is not None:
210-
gobject.source_remove(self.timeout)
211-
self.timeout = gobject.timeout_add(1000, self._play_target_sound)
207+
GObject.source_remove(self.timeout)
208+
self.timeout = GObject.timeout_add(1000, self._play_target_sound)
212209

213210
def _bad_answer(self, i):
214211
''' Make sure answer is unique '''
@@ -275,18 +272,29 @@ def _button_release_cb(self, win, event):
275272

276273
if self.current_card == self.target:
277274
self._activity.status.set_text(_('Very good!'))
275+
self._play(True)
278276
if self.timeout is not None:
279-
gobject.source_remove(self.timeout)
280-
self.timeout = gobject.timeout_add(1000, self.new_page)
277+
GObject.source_remove(self.timeout)
278+
self.timeout = GObject.timeout_add(1000, self.new_page)
281279
else:
282280
self._activity.status.set_text(_('Please try again.'))
281+
self._play(False)
283282
self._play_target_sound()
284-
283+
284+
def _play(self, great):
285+
if great:
286+
play_audio_from_file(os.getcwd() + '/sounds/great.ogg')
287+
else:
288+
play_audio_from_file(os.getcwd() + '/sounds/bad.ogg')
289+
285290
def _keypress_cb(self, area, event):
286291
''' No keyboard shortcuts at the moment. Perhaps jump to the page
287292
associated with the key pressed? '''
288293
return True
289-
294+
295+
def __draw_cb(self, canvas, cr):
296+
self._sprites.redraw_sprites(cr=cr)
297+
290298
def _expose_cb(self, win, event):
291299
''' Callback to handle window expose events '''
292300
self.do_expose_event(event)
@@ -308,12 +316,16 @@ def do_expose_event(self, event):
308316

309317
def _destroy_cb(self, win, event):
310318
''' Make a clean exit. '''
311-
gtk.main_quit()
319+
Gtk.main_quit()
312320

313321
def invalt(self, x, y, w, h):
314322
''' Mark a region for refresh '''
315-
self._canvas.window.invalidate_rect(
316-
gtk.gdk.Rectangle(int(x), int(y), int(w), int(h)), False)
323+
rectangle = Gdk.Rectangle()
324+
rectangle.x = x
325+
rectangle.y = y
326+
rectangle.width = w
327+
rectangle.height = h
328+
self._canvas.window.invalidate_rect(rectangle)
317329

318330
def load_level(self, path):
319331
''' Load a level (CSV) from path: letter, word, color, image,
@@ -371,14 +383,14 @@ def _clear_all(self):
371383

372384
def svg_str_to_pixbuf(svg_string):
373385
''' Load pixbuf from SVG string. '''
374-
pl = gtk.gdk.PixbufLoader('svg')
386+
pl = GdkPixbuf.PixbufLoader.new_with_type('svg')
387+
if type(svg_string) == unicode:
388+
svg_string = svg_string.encode('ascii', 'replace')
375389
pl.write(svg_string)
376390
pl.close()
377-
pixbuf = pl.get_pixbuf()
378-
return pixbuf
391+
return pl.get_pixbuf()
379392

380393

381394
def image_file_to_pixbuf(file_path, w, h):
382395
''' Load pixbuf from file '''
383-
return gtk.gdk.pixbuf_new_from_file_at_size(file_path, int(w), int(h))
384-
396+
return GdkPixbuf.Pixbuf.new_from_file_at_size(file_path, int(w), int(h))

setup.py

+3-31
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,4 @@
11
#!/usr/bin/env python
2-
import os
3-
import sys
4-
5-
if len(sys.argv) > 1 and '--no-sugar' == sys.argv[1]:
6-
# Remove the argument from the stack so we don't cause problems
7-
# for distutils
8-
sys.argv.pop(1)
9-
10-
import glob, os.path, string
11-
from distutils.core import setup
12-
13-
DATA_FILES = [
14-
('icons', glob.glob('icons/*')),
15-
('images', glob.glob('images/*')),
16-
('/usr/share/applications', ['turtleart.desktop'])
17-
]
18-
19-
setup (name = 'Turtle Art',
20-
description = "A LOGO-like tool for teaching programming",
21-
author = "Walter Bender",
22-
author_email = "walter.bender@gmail.com",
23-
version = '0.9.4',
24-
packages = ['TurtleArt'],
25-
scripts = ['turtleart'],
26-
data_files = DATA_FILES,
27-
)
28-
else:
29-
from sugar.activity import bundlebuilder
30-
31-
if __name__ == "__main__":
32-
bundlebuilder.start()
2+
from sugar3.activity import bundlebuilder
3+
if __name__ == "__main__":
4+
bundlebuilder.start()

0 commit comments

Comments
 (0)