Ticket #3772: cairo_gtk3__almost-ported.diff
File cairo_gtk3__almost-ported.diff, 103.3 KB (added by humitos, 12 years ago) |
---|
-
activity/activity.info
diff --git a/activity/activity.info b/activity/activity.info index 71729d2..869c1cc 100644
a b 1 1 [Activity] 2 name = Typing Turtle3 activity_version = 292 name = Gtk3 Typing Turtle 3 activity_version = 31 4 4 host_version = 1 5 bundle_id = org.laptop.community.TypingTurtle5 bundle_id = gtk3.TypingTurtle 6 6 icon = Activity-typingturtle 7 7 exec = sugar-activity typingturtle.TypingTurtle -
balloongame.py
diff --git a/balloongame.py b/balloongame.py index 56a6a34..2380e95 100644
a b 14 14 # You should have received a copy of the GNU General Public License 15 15 # along with Typing Turtle. If not, see <http://www.gnu.org/licenses/>. 16 16 17 import math 17 18 import random, datetime 19 18 20 from gettext import gettext as _ 19 21 20 import gobject, pygtk, gtk, pango 22 from gi.repository import Gtk 23 from gi.repository import Gdk 24 from gi.repository import GObject 25 from gi.repository import Pango 26 from gi.repository import PangoCairo 21 27 22 28 import medalscreen 23 29 … … class Balloon: 38 44 self.size = max(100, 50 + len(word) * 20) 39 45 self.color = random.choice(BALLOON_COLORS) 40 46 41 class BalloonGame( gtk.VBox):47 class BalloonGame(Gtk.VBox): 42 48 def __init__(self, lesson, activity): 43 gtk.VBox.__init__(self)49 GObject.GObject.__init__(self) 44 50 45 51 self.lesson = lesson 46 52 self.activity = activity 47 53 48 54 # Build title bar. 49 title = gtk.Label()55 title = Gtk.Label() 50 56 title.set_markup("<span size='20000'><b>" + lesson['name'] + "</b></span>") 51 57 title.set_alignment(1.0, 0.0) 52 58 53 stoplabel = gtk.Label(_('Go Back'))54 stopbtn = gtk.Button()59 stoplabel = Gtk.Label(label=_('Go Back')) 60 stopbtn = Gtk.Button() 55 61 stopbtn.add(stoplabel) 56 62 stopbtn.connect('clicked', self.stop_cb) 57 63 58 hbox = gtk.HBox()64 hbox = Gtk.HBox() 59 65 hbox.pack_start(stopbtn, False, False, 10) 60 66 hbox.pack_end(title, False, False, 10) 61 67 62 68 # Build the game drawing area. 63 self.area = gtk.DrawingArea()64 self. area.connect("expose-event", self.expose_cb)69 self.area = Gtk.DrawingArea() 70 self.draw_cb_id = self.area.connect("draw", self.draw_cb) 65 71 66 72 # Connect keyboard grabbing and releasing callbacks. 67 73 self.area.connect('realize', self.realize_cb) 68 74 self.area.connect('unrealize', self.unrealize_cb) 69 75 70 76 self.pack_start(hbox, False, False, 10) 71 self.pack_start(self.area, True, True )77 self.pack_start(self.area, True, True, 0) 72 78 73 79 self.show_all() 74 80 … … class BalloonGame(gtk.VBox): 85 91 self.finished = False 86 92 87 93 # Start the animation loop running. 88 self.update_timer = gobject.timeout_add(20, self.tick, priority=gobject.PRIORITY_HIGH_IDLE+30)94 self.update_timer = GObject.timeout_add(20, self.tick, priority=GObject.PRIORITY_HIGH_IDLE+30) 89 95 90 96 def realize_cb(self, widget): 91 self.activity.add_events( gtk.gdk.KEY_PRESS_MASK)97 self.activity.add_events(Gdk.EventMask.KEY_PRESS_MASK) 92 98 self.key_press_cb_id = self.activity.connect('key-press-event', self.key_cb) 93 99 94 100 # Clear the mouse cursor. 95 #pixmap = gtk.gdk.Pixmap(widget.window, 10, 10)96 #color = gtk.gdk.Color()97 #cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 5, 5)101 #pixmap = Gdk.Pixmap(widget.window, 10, 10) 102 #color = Gdk.Color() 103 #cursor = Gdk.Cursor.new(pixmap, pixmap, color, color, 5, 5) 98 104 #widget.window.set_cursor(cursor) 99 105 100 106 def unrealize_cb(self, widget): … … class BalloonGame(gtk.VBox): 103 109 def stop_cb(self, widget): 104 110 # Stop the animation loop. 105 111 if self.update_timer: 106 gobject.source_remove(self.update_timer)112 GObject.source_remove(self.update_timer) 107 113 108 114 self.activity.pop_screen() 109 115 110 116 def key_cb(self, widget, event): 111 117 # Ignore hotkeys. 112 if event. state & (gtk.gdk.CONTROL_MASK | gtk.gdk.MOD1_MASK):118 if event.get_state() & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK): 113 119 return False 114 120 115 121 # Extract information about the key pressed. 116 key = gtk.gdk.keyval_to_unicode(event.keyval)122 key = Gdk.keyval_to_unicode(event.keyval) 117 123 if key != 0: key = unichr(key) 118 124 119 125 if self.finished: 120 key_name = gtk.gdk.keyval_name(event.keyval)126 key_name = Gdk.keyval_name(event.keyval) 121 127 if key_name == 'Return': 122 128 self.activity.pop_screen() 123 129 … … class BalloonGame(gtk.VBox): 156 162 157 163 def tick(self): 158 164 if self.finished: 159 return 165 return False 160 166 161 167 self.bounds = self.area.get_allocation() 162 168 … … class BalloonGame(gtk.VBox): 192 198 193 199 return True 194 200 195 def draw_results(self, gc):201 def draw_results(self, cr): 196 202 # Draw background. 197 203 w = self.bounds.width - 400 198 204 h = self.bounds.height - 200 199 205 x = self.bounds.width/2 - w/2 200 206 y = self.bounds.height/2 - h/2 201 207 202 gc.foreground = self.area.get_colormap().alloc_color(50000,50000,50000) 203 self.area.window.draw_rectangle(gc, True, x, y, w, h) 204 gc.foreground = self.area.get_colormap().alloc_color(0,0,0) 205 self.area.window.draw_rectangle(gc, False, x, y, w, h) 208 cr.set_source_rgb(0.762, 0.762, 0.762) 209 cr.rectangle(x, y, w, h) 210 cr.fill() 206 211 207 # Draw text 208 gc.foreground = self.area.get_colormap().alloc_color(0,0,0) 212 cr.set_source_rgb(0, 0, 0) 213 cr.rectangle(x, y, w, h) 214 cr.stroke() 209 215 216 # Draw text 210 217 title = _('You finished!') + '\n' 211 layout = self.area.create_pango_layout(title) 212 layout.set_font_description(pango.FontDescription('Serif Bold 16')) 213 size = layout.get_size() 214 tx = x+w/2-(size[0]/pango.SCALE)/2 218 219 cr.set_source_rgb(0, 0, 0) 220 pango_layout = PangoCairo.create_layout(cr) 221 fd = Pango.FontDescription('Serif Bold') 222 fd.set_size(16 * Pango.SCALE) 223 pango_layout.set_font_description(fd) 224 pango_layout.set_text(title.encode('utf-8'), 225 len(title.encode('utf-8'))) 226 size = pango_layout.get_size() 227 tx = x + (w / 2) - (size[0] / Pango.SCALE) / 2 215 228 ty = y + 100 216 self.area.window.draw_layout(gc, tx, ty, layout) 229 cr.move_to(tx, ty) 230 PangoCairo.update_layout(cr, pango_layout) 231 PangoCairo.show_layout(cr, pango_layout) 217 232 218 233 report = '' 219 234 report += _('Your score was %(score)d.') % { 'score': self.score } + '\n' … … class BalloonGame(gtk.VBox): 222 237 report += '\n' 223 238 report += _('Press the ENTER key to continue.') 224 239 225 layout = self.area.create_pango_layout(report) 226 layout.set_font_description(pango.FontDescription('Times 12')) 227 size = layout.get_size() 228 tx = x+w/2-(size[0]/pango.SCALE)/2 229 ty = y + 200 230 self.area.window.draw_layout(gc, tx, ty, layout) 240 cr.set_source_rgb(0, 0, 0) 241 pango_layout = PangoCairo.create_layout(cr) 242 fd = Pango.FontDescription('Times') 243 fd.set_size(12 * Pango.SCALE) 244 pango_layout.set_font_description(fd) 245 pango_layout.set_text(report, len(report)) 246 size = pango_layout.get_size() 247 sx = x + w / 2 - (size[0] / Pango.SCALE) / 2 248 sy = y + 200 249 cr.move_to(sx, sy) 250 PangoCairo.update_layout(cr, pango_layout) 251 PangoCairo.show_layout(cr, pango_layout) 231 252 232 253 def finish_game(self): 233 254 self.finished = True … … class BalloonGame(gtk.VBox): 290 311 h = int(b.size*1.5 + 10) 291 312 self.area.queue_draw_area(x, y, w, h) 292 313 293 def draw_balloon(self, gc, b):314 def draw_balloon(self, cr, b): 294 315 x = int(b.x) 295 316 y = int(b.y) 296 317 297 318 # Draw the string. 298 gc.foreground = self.area.get_colormap().alloc_color(0,0,0)299 self.area.window.draw_line(gc,300 int(b.x), int(b.y+b.size/2),301 int(b.x), int(b.y+b.size))302 319 cr.set_source_rgb(0, 0, 0) 320 cr.move_to(int(b.x), int(b.y + b.size / 2)) 321 cr.line_to(int(b.x), int(b.y + b.size)) 322 cr.stroke() 323 303 324 # Draw the balloon. 304 gc.foreground = self.area.get_colormap().alloc_color(b.color[0],b.color[1],b.color[2]) 305 self.area.window.draw_arc(gc, True, x-b.size/2, y-b.size/2, b.size, b.size, 0, 360*64) 306 307 # Draw the text. 308 gc.foreground = self.area.get_colormap().alloc_color(0,0,0) 309 layout = self.area.create_pango_layout(b.word) 310 layout.set_font_description(pango.FontDescription('Sans 12')) 311 size = layout.get_size() 312 tx = x-(size[0]/pango.SCALE)/2 313 ty = y-(size[1]/pango.SCALE)/2 314 self.area.window.draw_layout(gc, tx, ty, layout) 315 325 cr.save() 326 cr.set_source_rgb(b.color[0], b.color[1], b.color[2]) 327 cr.arc(b.x, b.y, b.size / 2, 0, 2 * math.pi) 328 cr.fill() 329 cr.restore() 330 331 cr.set_source_rgb(0, 0, 0) 332 333 pango_layout = PangoCairo.create_layout(cr) 334 fd = Pango.FontDescription('Sans') 335 fd.set_size(12 * Pango.SCALE) 336 pango_layout.set_font_description(fd) 337 pango_layout.set_text(b.word, len(b.word)) 338 size = pango_layout.get_size() 339 x = x - (size[0] / Pango.SCALE) / 2 340 y = y - (size[1] / Pango.SCALE) / 2 341 cr.move_to(x, y) 342 PangoCairo.update_layout(cr, pango_layout) 343 PangoCairo.show_layout(cr, pango_layout) 344 316 345 def add_score(self, num): 317 346 self.score += num 318 347 self.queue_draw_score() 319 348 320 349 def queue_draw_score(self): 321 350 layout = self.area.create_pango_layout(_('SCORE: %d') % self.score) 322 layout.set_font_description( pango.FontDescription('Times 14'))351 layout.set_font_description(Pango.FontDescription('Times 14')) 323 352 size = layout.get_size() 324 x = self.bounds.width-20-size[0]/ pango.SCALE353 x = self.bounds.width-20-size[0]/Pango.SCALE 325 354 y = 20 326 355 self.queue_draw_area(x, y, x+size[0], y+size[1]) 327 356 328 def draw_score(self, gc): 329 layout = self.area.create_pango_layout(_('SCORE: %d') % self.score) 330 layout.set_font_description(pango.FontDescription('Times 14')) 331 size = layout.get_size() 332 x = self.bounds.width-20-size[0]/pango.SCALE 357 def draw_score(self, cr): 358 cr.set_source_rgb(0, 0, 0) 359 pango_layout = PangoCairo.create_layout(cr) 360 fd = Pango.FontDescription('Times') 361 fd.set_size(14 * Pango.SCALE) 362 pango_layout.set_font_description(fd) 363 text = _('SCORE: %d') % self.score 364 pango_layout.set_text(text, len(text)) 365 366 size = pango_layout.get_size() 367 x = self.bounds.width - 20 - size[0] / Pango.SCALE 333 368 y = 20 334 self.area.window.draw_layout(gc, x, y, layout) 369 cr.move_to(x, y) 370 PangoCairo.update_layout(cr, pango_layout) 371 PangoCairo.show_layout(cr, pango_layout) 335 372 336 def draw_instructions(self, gc):373 def draw_instructions(self, cr): 337 374 # Draw instructions. 338 gc.foreground = self.area.get_colormap().alloc_color(0,0,0) 339 340 layout = self.area.create_pango_layout(_('Type the words to pop the balloons!')) 341 layout.set_font_description(pango.FontDescription('Times 14')) 342 size = layout.get_size() 343 x = (self.bounds.width - size[0]/pango.SCALE)/2 344 y = self.bounds.height-20 - size[1]/pango.SCALE 345 self.area.window.draw_layout(gc, x, y, layout) 346 347 def draw(self): 375 cr.set_source_rgb(0, 0, 0) 376 pango_layout = PangoCairo.create_layout(cr) 377 pango_layout.set_font_description(Pango.FontDescription('Times 14')) 378 text = _('Type the words to pop the balloons!') 379 pango_layout.set_text(text, len(text)) 380 size = pango_layout.get_size() 381 x = (self.bounds.width - size[0] / Pango.SCALE) / 2 382 y = self.bounds.height - 20 - size[1] / Pango.SCALE 383 cr.move_to(x, y) 384 PangoCairo.update_layout(cr, pango_layout) 385 PangoCairo.show_layout(cr, pango_layout) 386 387 def draw(self, cr): 348 388 self.bounds = self.area.get_allocation() 349 389 350 gc = self.area.window.new_gc()351 352 390 # Draw background. 353 gc.foreground = self.area.get_colormap().alloc_color(60000,60000,65535) 354 self.area.window.draw_rectangle(gc, True, 0, 0, self.bounds.width, self.bounds.height) 391 cr.set_source_rgb(0.915, 0.915, 1) 392 cr.rectangle(0, 0, self.bounds.width, self.bounds.height) 393 cr.fill() 355 394 356 395 # Draw the balloons. 357 396 for b in self.balloons: 358 self.draw_balloon( gc, b)397 self.draw_balloon(cr, b) 359 398 360 399 if self.finished: 361 self.draw_results(gc) 362 400 self.draw_results(cr) 363 401 else: 364 self.draw_instructions( gc)402 self.draw_instructions(cr) 365 403 366 self.draw_score( gc)404 self.draw_score(cr) 367 405 368 def expose_cb(self, area, event):369 self.draw( )406 def draw_cb(self, area, cr): 407 self.draw(cr) -
editlessonlistscreen.py
diff --git a/editlessonlistscreen.py b/editlessonlistscreen.py index af67020..6ac2c34 100644
a b from gettext import gettext as _ 22 22 from port import json 23 23 from port import chooser 24 24 25 # Import PyGTK. 26 import gobject, pygtk, gtk, pango 25 from gi.repository import Gtk 26 from gi.repository import GObject 27 27 28 28 # Import Sugar UI modules. 29 import sugar .activity.activity30 import sugar .graphics.style31 import sugar .graphics.alert32 import sugar .mime33 import sugar .datastore.datastore29 import sugar3.activity.activity 30 import sugar3.graphics.style 31 import sugar3.graphics.alert 32 import sugar3.mime 33 import sugar3.datastore.datastore 34 34 35 35 # Import activity modules. 36 36 import editlessonscreen 37 37 38 class EditLessonListScreen( gtk.VBox):38 class EditLessonListScreen(Gtk.VBox): 39 39 def __init__(self, activity, lessons): 40 gtk.VBox.__init__(self)40 GObject.GObject.__init__(self) 41 41 42 42 self.activity = activity 43 43 self.lessons = lessons 44 44 45 45 # Add the header. 46 title = gtk.Label()46 title = Gtk.Label() 47 47 title.set_markup("<span size='20000'><b>" + _("Edit Lessons") + "</b></span>") 48 48 title.set_alignment(1.0, 0.0) 49 49 50 stoplabel = gtk.Label(_('Go Back'))51 stopbtn = gtk.Button()50 stoplabel = Gtk.Label(label=_('Go Back')) 51 stopbtn = Gtk.Button() 52 52 stopbtn.add(stoplabel) 53 53 stopbtn.connect('clicked', self.stop_clicked_cb) 54 54 55 titlebox = gtk.HBox()55 titlebox = Gtk.HBox() 56 56 titlebox.pack_start(stopbtn, False, False, 10) 57 57 titlebox.pack_end(title, False, False, 10) 58 58 59 59 # Add the lesson list. 60 self.treeview = gtk.TreeView()60 self.treeview = Gtk.TreeView() 61 61 self.treeview.set_rules_hint(True) 62 62 self.treeview.set_enable_search(False) 63 63 … … class EditLessonListScreen(gtk.VBox): 66 66 67 67 # Note that the only thing we store in our liststore is the lesson id. 68 68 # All the actual data is in the lessons list. 69 self.liststore = gtk.ListStore(gobject.TYPE_INT)69 self.liststore = Gtk.ListStore(GObject.TYPE_INT) 70 70 self.treeview.set_model(self.liststore) 71 71 72 72 # Construct the columns. 73 renderer = gtk.CellRendererText()74 col = gtk.TreeViewColumn(_('Name'), renderer)73 renderer = Gtk.CellRendererText() 74 col = Gtk.TreeViewColumn(_('Name'), renderer) 75 75 col.set_cell_data_func(renderer, self.name_render_cb) 76 76 self.treeview.append_column(col) 77 77 78 renderer = gtk.CellRendererText()79 col = gtk.TreeViewColumn(_('Description'), renderer)78 renderer = Gtk.CellRendererText() 79 col = Gtk.TreeViewColumn(_('Description'), renderer) 80 80 col.set_cell_data_func(renderer, self.description_render_cb) 81 81 col.set_expand(True) 82 82 self.treeview.append_column(col) 83 83 84 renderer = gtk.CellRendererText()85 col = gtk.TreeViewColumn(_('Type'), renderer)84 renderer = Gtk.CellRendererText() 85 col = Gtk.TreeViewColumn(_('Type'), renderer) 86 86 col.set_cell_data_func(renderer, self.type_render_cb) 87 87 col.set_expand(False) 88 88 self.treeview.append_column(col) 89 89 90 scroll = gtk.ScrolledWindow()91 scroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)90 scroll = Gtk.ScrolledWindow() 91 scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) 92 92 scroll.add(self.treeview) 93 93 94 importlabel = gtk.Label(_('Import Lessons from Journal'))95 self.importbtn = gtk.Button()94 importlabel = Gtk.Label(label=_('Import Lessons from Journal')) 95 self.importbtn = Gtk.Button() 96 96 self.importbtn.add(importlabel) 97 97 self.importbtn.connect('clicked', self.import_clicked_cb) 98 98 99 exportlabel = gtk.Label(_('Export Lessons to Journal'))100 self.exportbtn = gtk.Button()99 exportlabel = Gtk.Label(label=_('Export Lessons to Journal')) 100 self.exportbtn = Gtk.Button() 101 101 self.exportbtn.add(exportlabel) 102 102 self.exportbtn.connect('clicked', self.export_clicked_cb) 103 103 104 exportlabel = gtk.Label(_('Save Lessons to Activity'))105 self.defaultsbtn = gtk.Button()104 exportlabel = Gtk.Label(label=_('Save Lessons to Activity')) 105 self.defaultsbtn = Gtk.Button() 106 106 self.defaultsbtn.add(exportlabel) 107 107 self.defaultsbtn.connect('clicked', self.set_default_clicked_cb) 108 108 109 self.addbtn = gtk.Button()110 self.addbtn.add(sugar .graphics.icon.Icon(icon_name='list-add'))109 self.addbtn = Gtk.Button() 110 self.addbtn.add(sugar3.graphics.icon.Icon(icon_name='list-add')) 111 111 self.addbtn.connect('clicked', self.add_lesson_clicked_cb) 112 self.delbtn = gtk.Button()113 self.delbtn.add(sugar .graphics.icon.Icon(icon_name='list-remove'))112 self.delbtn = Gtk.Button() 113 self.delbtn.add(sugar3.graphics.icon.Icon(icon_name='list-remove')) 114 114 self.delbtn.connect('clicked', self.del_lesson_clicked_cb) 115 115 self.delbtn.set_sensitive(False) 116 self.moveupbtn = gtk.Button()117 self.moveupbtn.add(sugar .graphics.icon.Icon(icon_name='go-up'))116 self.moveupbtn = Gtk.Button() 117 self.moveupbtn.add(sugar3.graphics.icon.Icon(icon_name='go-up')) 118 118 self.moveupbtn.connect('clicked', self.move_lesson_up_clicked_cb) 119 119 self.moveupbtn.set_sensitive(False) 120 self.movedownbtn = gtk.Button()121 self.movedownbtn.add(sugar .graphics.icon.Icon(icon_name='go-down'))120 self.movedownbtn = Gtk.Button() 121 self.movedownbtn.add(sugar3.graphics.icon.Icon(icon_name='go-down')) 122 122 self.movedownbtn.connect('clicked', self.move_lesson_down_clicked_cb) 123 123 self.movedownbtn.set_sensitive(False) 124 124 125 btnbox = gtk.HBox()125 btnbox = Gtk.HBox() 126 126 btnbox.pack_start(self.importbtn, False, False, 10) 127 btnbox.pack_start(self.exportbtn, False, False )127 btnbox.pack_start(self.exportbtn, False, False, 0) 128 128 btnbox.pack_start(self.defaultsbtn, False, False, 10) 129 btnbox.pack_end(self.addbtn, False, False )130 btnbox.pack_end(self.delbtn, False, False )131 btnbox.pack_end(self.moveupbtn, False, False )132 btnbox.pack_end(self.movedownbtn, False, False )129 btnbox.pack_end(self.addbtn, False, False, 0) 130 btnbox.pack_end(self.delbtn, False, False, 0) 131 btnbox.pack_end(self.moveupbtn, False, False, 0) 132 btnbox.pack_end(self.movedownbtn, False, False, 0) 133 133 134 134 self.pack_start(titlebox, False, False, 10) 135 self.pack_start( gtk.HSeparator(), False, False, 0)135 self.pack_start(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), False, False, 0) 136 136 self.pack_start(scroll, True, True, 10) 137 137 self.pack_start(btnbox, False, False, 10) 138 138 … … class EditLessonListScreen(gtk.VBox): 197 197 if len(self.lessons) > 1: 198 198 path = self.treeview.get_cursor()[0] 199 199 if path: 200 msg = sugar .graphics.alert.ConfirmationAlert()200 msg = sugar3.graphics.alert.ConfirmationAlert() 201 201 msg.props.title = _('Delete Lesson?') 202 202 msg.props.msg = _('Deleting the lesson will erase the lesson content.') 203 203 204 204 def alert_response_cb(alert, response_id, self, id): 205 205 self.activity.remove_alert(alert) 206 if response_id is gtk.RESPONSE_OK:206 if response_id is Gtk.ResponseType.OK: 207 207 self.lessons.pop(id) 208 208 del self.liststore[id] 209 209 self.treeview.get_selection().select_path(id) … … class EditLessonListScreen(gtk.VBox): 294 294 295 295 def export_clicked_cb(self, btn): 296 296 # Create the new journal entry 297 fileObject = sugar .datastore.datastore.create()297 fileObject = sugar3.datastore.datastore.create() 298 298 299 299 meta = self.activity.metadata 300 300 fileObject.metadata['title'] = meta['title'] + _(' (Exported Lessons)') … … class EditLessonListScreen(gtk.VBox): 312 312 finally: 313 313 fd.close() 314 314 315 sugar .datastore.datastore.write(fileObject, transfer_ownership=True)315 sugar3.datastore.datastore.write(fileObject, transfer_ownership=True) 316 316 fileObject.destroy() 317 317 del fileObject 318 318 319 319 def set_default_clicked_cb(self, btn): 320 320 code = locale.getdefaultlocale()[0] or 'en_US' 321 path = sugar .activity.activity.get_bundle_path() + '/lessons/%s.lessons' % code321 path = sugar3.activity.activity.get_bundle_path() + '/lessons/%s.lessons' % code 322 322 323 323 fd = open(path, 'w') 324 324 … … class EditLessonListScreen(gtk.VBox): 328 328 329 329 finally: 330 330 fd.close() 331 332 No newline at end of file 331 -
editlessonscreen.py
diff --git a/editlessonscreen.py b/editlessonscreen.py index 4c7ef0b..b2b955a 100644
a b 20 20 import logging, os, math, time, copy, locale, datetime, random, re 21 21 from gettext import gettext as _ 22 22 23 # Import PyGTK. 24 import gobject, pygtk, gtk, pango 23 from gi.repository import Gtk 24 from gi.repository import GObject 25 from gi.repository import Pango 25 26 26 27 # Import Sugar UI modules. 27 import sugar .activity.activity28 import sugar .graphics.style29 import sugar .graphics.icon28 import sugar3.activity.activity 29 import sugar3.graphics.style 30 import sugar3.graphics.icon 30 31 31 32 # Import lessonbuilder functions. 32 33 import lessonbuilder 33 34 34 class EditLessonScreen( gtk.VBox):35 class EditLessonScreen(Gtk.VBox): 35 36 def __init__(self, activity, lesson): 36 gtk.VBox.__init__(self)37 GObject.GObject.__init__(self) 37 38 self.set_border_width(10) 38 39 39 40 self.activity = activity … … class EditLessonScreen(gtk.VBox): 42 43 self.in_build = False 43 44 44 45 # Add the header. 45 title = gtk.Label()46 title = Gtk.Label() 46 47 title.set_markup("<span size='20000'><b>" + _("Edit a Lesson") + "</b></span>") 47 48 title.set_alignment(1.0, 0.0) 48 49 49 stoplabel = gtk.Label(_('Go Back'))50 stopbtn = gtk.Button()50 stoplabel = Gtk.Label(label=_('Go Back')) 51 stopbtn = Gtk.Button() 51 52 stopbtn.add(stoplabel) 52 53 stopbtn.connect('clicked', self.stop_clicked_cb) 53 54 54 titlebox = gtk.HBox()55 titlebox = Gtk.HBox() 55 56 titlebox.pack_start(stopbtn, False, False, 10) 56 57 titlebox.pack_end(title, False, False, 10) 57 58 58 self.vp = gtk.Viewport()59 self.vp = Gtk.Viewport() 59 60 60 self.scroll = gtk.ScrolledWindow()61 self.scroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)61 self.scroll = Gtk.ScrolledWindow() 62 self.scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) 62 63 self.scroll.add(self.vp) 63 64 64 65 self.pack_start(titlebox, False, False, 10) 65 self.pack_start( gtk.HSeparator(), False, False, 0)66 self.pack_start(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), False, False, 0) 66 67 self.pack_start(self.scroll, True, True, 0) 67 68 68 69 self.build() … … class EditLessonScreen(gtk.VBox): 70 71 self.show_all() 71 72 72 73 def build_generate(self): 73 generatebox = gtk.VBox()74 generatebox = Gtk.VBox() 74 75 generatebox.set_spacing(5) 75 76 76 newlabel = gtk.Label(_('New keys'))77 knownlabel = gtk.Label(_('Known keys'))78 lengthlabel = gtk.Label(_('Length'))77 newlabel = Gtk.Label(label=_('New keys')) 78 knownlabel = Gtk.Label(label=_('Known keys')) 79 lengthlabel = Gtk.Label(label=_('Length')) 79 80 80 generatebox.newkeysent = gtk.Entry()81 generatebox.newkeysent = Gtk.Entry() 81 82 generatebox.newkeysent.set_width_chars(8) 82 generatebox.knownkeysent = gtk.Entry()83 generatebox.knownkeysent = Gtk.Entry() 83 84 generatebox.knownkeysent.set_width_chars(15) 84 generatebox.lengthent = gtk.Entry()85 generatebox.lengthent = Gtk.Entry() 85 86 generatebox.lengthent.set_width_chars(5) 86 87 generatebox.lengthent.set_text('60') 87 88 88 oklabel = gtk.Label()89 oklabel = Gtk.Label() 89 90 oklabel.set_markup(_('Generate!')) 90 okbtn = gtk.Button()91 okbtn = Gtk.Button() 91 92 okbtn.add(oklabel) 92 93 okbtn.connect('clicked', self.generate_ok_clicked_cb, generatebox) 93 94 okbtn.set_alignment(0.5, 0.5) 94 95 95 box = gtk.HBox()96 box = Gtk.HBox() 96 97 box.set_spacing(10) 97 box.pack_start(newlabel, expand=False)98 box.pack_start(generatebox.newkeysent, expand=False)99 box.pack_start(knownlabel, expand=False)100 box.pack_start(generatebox.knownkeysent, expand=False)101 box.pack_start(lengthlabel, expand=False)102 box.pack_start(generatebox.lengthent, expand=False)103 box.pack_end(okbtn, expand=False)98 box.pack_start(newlabel, False, True, 0) 99 box.pack_start(generatebox.newkeysent, False, True, 0) 100 box.pack_start(knownlabel, False, True, 0) 101 box.pack_start(generatebox.knownkeysent, False, True, 0) 102 box.pack_start(lengthlabel, False, True, 0) 103 box.pack_start(generatebox.lengthent, False, True, 0) 104 box.pack_end(okbtn, False, True, 0) 104 105 box.show_all() 105 106 106 wordslabel = gtk.Label()107 wordslabel = Gtk.Label() 107 108 wordslabel.set_markup(_('Edit Word List')) 108 wordsbtn = gtk.Button()109 wordsbtn = Gtk.Button() 109 110 wordsbtn.add(wordslabel) 110 111 wordsbtn.connect('clicked', self.generate_words_clicked_cb) 111 112 wordsbtn.set_alignment(0.5, 0.5) 112 113 113 generatebox.pack_start(box )114 generatebox.pack_start(wordsbtn, expand=False, fill=False )114 generatebox.pack_start(box, True, True, 0) 115 generatebox.pack_start(wordsbtn, expand=False, fill=False, padding=0) 115 116 116 117 return generatebox 117 118 118 119 def build_step(self, step, idx): 119 stepbox = gtk.VBox()120 stepbox = Gtk.VBox() 120 121 stepbox.set_spacing(5) 121 122 122 steplabel = gtk.Label()123 steplabel = Gtk.Label() 123 124 steplabel.set_markup("<span size='x-large' weight='bold'>" + (_('Step #%d') % (idx+1)) + "</span>") 124 125 steplabel.set_alignment(0.0, 0.5) 125 126 steplabel.set_padding(10, 0) 126 127 127 128 # Build the step type combo box. 128 stepbox.typecombo = gtk.combo_box_new_text()129 stepbox.typecombo = Gtk.ComboBoxText() 129 130 stepbox.typecombo.append_text(_('Keys')) 130 131 stepbox.typecombo.append_text(_('Words')) 131 132 … … class EditLessonScreen(gtk.VBox): 136 137 stepbox.typecombo.set_active(1) 137 138 138 139 # Build the tool buttons. 139 delstepbtn = gtk.Button()140 delstepbtn.add(sugar .graphics.icon.Icon(icon_name='list-remove'))140 delstepbtn = Gtk.Button() 141 delstepbtn.add(sugar3.graphics.icon.Icon(icon_name='list-remove')) 141 142 delstepbtn.connect('clicked', self.del_step_clicked_cb, idx) 142 addstepbtn = gtk.Button()143 addstepbtn.add(sugar .graphics.icon.Icon(icon_name='list-add'))143 addstepbtn = Gtk.Button() 144 addstepbtn.add(sugar3.graphics.icon.Icon(icon_name='list-add')) 144 145 addstepbtn.connect('clicked', self.add_step_clicked_cb, idx) 145 moveupbtn = gtk.Button()146 moveupbtn.add(sugar .graphics.icon.Icon(icon_name='go-up'))146 moveupbtn = Gtk.Button() 147 moveupbtn.add(sugar3.graphics.icon.Icon(icon_name='go-up')) 147 148 moveupbtn.connect('clicked', self.move_step_up_clicked_cb, idx) 148 movedownbtn = gtk.Button()149 movedownbtn.add(sugar .graphics.icon.Icon(icon_name='go-down'))149 movedownbtn = Gtk.Button() 150 movedownbtn.add(sugar3.graphics.icon.Icon(icon_name='go-down')) 150 151 movedownbtn.connect('clicked', self.move_step_down_clicked_cb, idx) 151 152 152 153 if idx == 0: … … class EditLessonScreen(gtk.VBox): 154 155 if idx == len(self.lesson['steps']) - 1: 155 156 movedownbtn.set_sensitive(False) 156 157 157 btnbox = gtk.HBox()158 btnbox.pack_start(steplabel, False, False )158 btnbox = Gtk.HBox() 159 btnbox.pack_start(steplabel, False, False, 0) 159 160 btnbox.pack_start(stepbox.typecombo, expand=False, padding=10) 160 btnbox.pack_end(addstepbtn, False, False )161 btnbox.pack_end(delstepbtn, False, False )162 btnbox.pack_end(moveupbtn, False, False )163 btnbox.pack_end(movedownbtn, False, False )161 btnbox.pack_end(addstepbtn, False, False, 0) 162 btnbox.pack_end(delstepbtn, False, False, 0) 163 btnbox.pack_end(moveupbtn, False, False, 0) 164 btnbox.pack_end(movedownbtn, False, False, 0) 164 165 165 166 # Build the instructions entry. 166 instlabel = gtk.Label()167 instlabel = Gtk.Label() 167 168 instlabel.set_markup("<span size='large' weight='bold'>" + _('Instructions') + "</span>") 168 169 instlabel.set_alignment(0.0, 0.5) 169 170 instlabel.set_padding(20, 0) 170 171 171 172 self.labelsizegroup.add_widget(instlabel) 172 173 173 stepbox.insttext = gtk.TextView(gtk.TextBuffer())174 stepbox.insttext.props.wrap_mode = gtk.WRAP_WORD175 stepbox.insttext.modify_font( pango.FontDescription('Monospace'))176 instscroll = gtk.ScrolledWindow()177 instscroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)174 stepbox.insttext = Gtk.TextView(Gtk.TextBuffer()) 175 stepbox.insttext.props.wrap_mode = Gtk.WrapMode.WORD 176 stepbox.insttext.modify_font(Pango.FontDescription('Monospace')) 177 instscroll = Gtk.ScrolledWindow() 178 instscroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) 178 179 instscroll.add(stepbox.insttext) 179 180 instscroll.set_size_request(-1, 75) 180 181 stepbox.insttext.get_buffer().set_text(step['instructions']) 181 182 182 instbox = gtk.HBox()183 instbox.pack_start(instlabel, False, False )184 instbox.pack_start(instscroll, True, True )183 instbox = Gtk.HBox() 184 instbox.pack_start(instlabel, False, False, 0) 185 instbox.pack_start(instscroll, True, True, 0) 185 186 186 187 # Build the text entry. 187 textlabel = gtk.Label()188 textlabel = Gtk.Label() 188 189 textlabel.set_markup("<span size='large' weight='bold'>" + _('Text') + "</span>") 189 190 textlabel.set_alignment(0.0, 0.5) 190 191 textlabel.set_padding(20, 0) 191 192 192 193 self.labelsizegroup.add_widget(textlabel) 193 194 194 stepbox.texttext = gtk.TextView(gtk.TextBuffer())195 stepbox.texttext.props.wrap_mode = gtk.WRAP_WORD196 stepbox.texttext.modify_font( pango.FontDescription('monospace'))197 textscroll = gtk.ScrolledWindow()198 textscroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)195 stepbox.texttext = Gtk.TextView(Gtk.TextBuffer()) 196 stepbox.texttext.props.wrap_mode = Gtk.WrapMode.WORD 197 stepbox.texttext.modify_font(Pango.FontDescription('monospace')) 198 textscroll = Gtk.ScrolledWindow() 199 textscroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) 199 200 textscroll.add(stepbox.texttext) 200 201 textscroll.set_size_request(-1, 100) 201 202 stepbox.texttext.get_buffer().set_text(step['text']) 202 203 203 textbox = gtk.HBox()204 textbox.pack_start(textlabel, expand=False)205 textbox.pack_start(textscroll )204 textbox = Gtk.HBox() 205 textbox.pack_start(textlabel, False, True, 0) 206 textbox.pack_start(textscroll, True, True, 0) 206 207 207 sizegroup = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)208 sizegroup = Gtk.SizeGroup(Gtk.SizeGroupMode.HORIZONTAL) 208 209 sizegroup.add_widget(instlabel) 209 210 sizegroup.add_widget(textlabel) 210 211 211 stepbox.pack_start(btnbox, expand=False)212 stepbox.pack_start(instbox, expand=False)213 stepbox.pack_start(textbox, expand=False)212 stepbox.pack_start(btnbox, False, True, 0) 213 stepbox.pack_start(instbox, False, True, 0) 214 stepbox.pack_start(textbox, False, True, 0) 214 215 215 216 return stepbox 216 217 217 218 def build_medal(self, medal, name): 218 box = gtk.HBox()219 box = Gtk.HBox() 219 220 220 label = gtk.Label()221 label = Gtk.Label() 221 222 label.set_markup("<span size='large' weight='bold'>" + name + "</span>") 222 223 label.set_alignment(0.0, 0.5) 223 224 label.set_padding(20, 0) 224 225 225 226 self.labelsizegroup.add_widget(label) 226 227 227 box.pack_start(label, False, False )228 box.pack_start(label, False, False, 0) 228 229 229 230 if self.lesson['type'] == 'normal': 230 acclabel = gtk.Label(_('Accuracy'))231 wpmlabel = gtk.Label(_('WPM'))231 acclabel = Gtk.Label(label=_('Accuracy')) 232 wpmlabel = Gtk.Label(label=_('WPM')) 232 233 233 box.accent = gtk.Entry()234 box.wpment = gtk.Entry()234 box.accent = Gtk.Entry() 235 box.wpment = Gtk.Entry() 235 236 236 237 box.accent.set_text(str(medal['accuracy'])) 237 238 box.wpment.set_text(str(medal['wpm'])) 238 239 239 240 box.pack_start(acclabel, False, False, 10) 240 box.pack_start(box.accent, False, False )241 box.pack_start(box.accent, False, False, 0) 241 242 box.pack_start(wpmlabel, False, False, 10) 242 box.pack_start(box.wpment, False, False )243 box.pack_start(box.wpment, False, False, 0) 243 244 244 245 elif self.lesson['type'] == 'balloon': 245 scorelabel = gtk.Label(_('Score'))246 scorelabel = Gtk.Label(label=_('Score')) 246 247 247 box.scoreent = gtk.Entry()248 box.scoreent = Gtk.Entry() 248 249 box.scoreent.set_text(str(medal['score'])) 249 250 250 251 box.pack_start(scorelabel, False, False, 10) 251 box.pack_start(box.scoreent, False, False )252 box.pack_start(box.scoreent, False, False, 0) 252 253 253 254 return box 254 255 255 256 def build(self): 256 257 self.in_build = True 257 258 258 self.vbox = gtk.VBox()259 self.vbox = Gtk.VBox() 259 260 self.vbox.set_border_width(20) 260 261 self.vbox.set_spacing(5) 261 262 262 self.labelsizegroup = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)263 self.labelsizegroup = Gtk.SizeGroup(Gtk.SizeGroupMode.HORIZONTAL) 263 264 264 265 # Lesson details widgets. 265 detailslabel = gtk.Label()266 detailslabel = Gtk.Label() 266 267 detailslabel.set_markup("<span size='x-large'><b>" + _('Lesson Details') + "</b></span>") 267 268 detailslabel.set_alignment(0.0, 0.5) 268 269 detailslabel.set_padding(10, 0) 269 270 270 namelabel = gtk.Label()271 namelabel = Gtk.Label() 271 272 namelabel.set_markup("<span size='large' weight='bold'>" + _('Name') + "</span>") 272 273 namelabel.set_alignment(0.0, 0.5) 273 274 namelabel.set_padding(20, 0) 274 275 275 self.nameent = gtk.Entry()276 self.nameent = Gtk.Entry() 276 277 self.nameent.set_text(self.lesson['name']) 277 278 278 namebox = gtk.HBox()279 namebox.pack_start(namelabel, expand=False)280 namebox.pack_start(self.nameent )279 namebox = Gtk.HBox() 280 namebox.pack_start(namelabel, False, True, 0) 281 namebox.pack_start(self.nameent, True, True, 0) 281 282 282 typelabel = gtk.Label()283 typelabel = Gtk.Label() 283 284 typelabel.set_markup("<span size='large' weight='bold'>" + _('Type') + "</span>") 284 285 typelabel.set_alignment(0.0, 0.5) 285 286 typelabel.set_padding(20, 0) 286 287 287 self.textradio = gtk.RadioButton(None, _('Normal Lesson'))288 self.textradio = Gtk.RadioButton(None, _('Normal Lesson')) 288 289 self.textradio.connect('toggled', self.type_toggled_cb) 289 290 290 self.balloonradio = gtk.RadioButton(self.textradio, _('Balloon Game'))291 self.balloonradio = Gtk.RadioButton(self.textradio, _('Balloon Game')) 291 292 self.balloonradio.connect('toggled', self.type_toggled_cb) 292 293 293 294 self.textradio.set_active(self.lesson['type'] == 'normal') 294 295 self.balloonradio.set_active(self.lesson['type'] == 'balloon') 295 296 296 typebox = gtk.HBox()297 typebox.pack_start(typelabel, expand=False)298 typebox.pack_start(self.textradio, expand=False)299 typebox.pack_start(self.balloonradio, expand=False)297 typebox = Gtk.HBox() 298 typebox.pack_start(typelabel, False, True, 0) 299 typebox.pack_start(self.textradio, False, True, 0) 300 typebox.pack_start(self.balloonradio, False, True, 0) 300 301 301 desclabel = gtk.Label()302 desclabel = Gtk.Label() 302 303 desclabel.set_markup("<span size='large' weight='bold'>" + _('Description') + "</span>") 303 304 desclabel.set_alignment(0.0, 0.5) 304 305 desclabel.set_padding(20, 0) 305 306 306 self.desctext = gtk.TextView(gtk.TextBuffer())307 self.desctext.props.wrap_mode = gtk.WRAP_WORD308 descscroll = gtk.ScrolledWindow()309 descscroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)307 self.desctext = Gtk.TextView(Gtk.TextBuffer()) 308 self.desctext.props.wrap_mode = Gtk.WrapMode.WORD 309 descscroll = Gtk.ScrolledWindow() 310 descscroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) 310 311 descscroll.add(self.desctext) 311 312 descscroll.set_size_request(-1, 75) 312 313 self.desctext.get_buffer().set_text(self.lesson['description']) 313 314 314 descbox = gtk.HBox()315 descbox.pack_start(desclabel, expand=False)316 descbox.pack_start(descscroll )315 descbox = Gtk.HBox() 316 descbox.pack_start(desclabel, False, True, 0) 317 descbox.pack_start(descscroll, True, True, 0) 317 318 318 319 # Build the options. 319 optslabel = gtk.Label()320 optslabel = Gtk.Label() 320 321 optslabel.set_markup("<span size='large' weight='bold'>" + _('Options') + "</span>") 321 322 optslabel.set_alignment(0.0, 0.5) 322 323 optslabel.set_padding(20, 0) 323 324 324 self.mistakescheck = gtk.CheckButton(_('Allow Mistakes'))325 self.mistakescheck = Gtk.CheckButton(_('Allow Mistakes')) 325 326 self.mistakescheck.set_active(self.lesson.get('options', {}).get('mistakes', True)) 326 self.backspacecheck = gtk.CheckButton(_('Allow Backspace'))327 self.backspacecheck = Gtk.CheckButton(_('Allow Backspace')) 327 328 self.backspacecheck.set_active(self.lesson.get('options', {}).get('backspace', True)) 328 329 329 optsbox = gtk.HBox()330 optsbox.pack_start(optslabel, expand=False)331 optsbox.pack_start(self.backspacecheck, expand=False)332 optsbox.pack_start(self.mistakescheck, expand=False)330 optsbox = Gtk.HBox() 331 optsbox.pack_start(optslabel, False, True, 0) 332 optsbox.pack_start(self.backspacecheck, False, True, 0) 333 optsbox.pack_start(self.mistakescheck, False, True, 0) 333 334 334 335 self.labelsizegroup.add_widget(namelabel) 335 336 self.labelsizegroup.add_widget(typelabel) 336 337 self.labelsizegroup.add_widget(desclabel) 337 338 self.labelsizegroup.add_widget(optslabel) 338 339 339 self.vbox.pack_start(detailslabel, expand=False)340 self.vbox.pack_start(namebox, expand=False)341 self.vbox.pack_start(typebox, expand=False)342 self.vbox.pack_start(descbox, expand=False)343 self.vbox.pack_start(optsbox, expand=False)340 self.vbox.pack_start(detailslabel, False, True, 0) 341 self.vbox.pack_start(namebox, False, True, 0) 342 self.vbox.pack_start(typebox, False, True, 0) 343 self.vbox.pack_start(descbox, False, True, 0) 344 self.vbox.pack_start(optsbox, False, True, 0) 344 345 345 346 # Build the generator. 346 generatelabel = gtk.Label()347 generatelabel = Gtk.Label() 347 348 generatelabel.set_markup("<span size='x-large'><b>" + _('Automatic Lesson Generator') + "</b></span>") 348 349 generatelabel.set_alignment(0.0, 0.5) 349 350 generatelabel.set_padding(10, 0) 350 351 351 352 generatebox = self.build_generate() 352 353 self.vbox.pack_start(generatelabel, expand=False, padding=10) 353 self.vbox.pack_start(generatebox, expand=False)354 self.vbox.pack_start(generatebox, False, True, 0) 354 355 355 356 self.has_normal_widgets = False 356 357 self.has_balloon_widgets = False … … class EditLessonScreen(gtk.VBox): 369 370 stepbox = self.build_step(step, len(self.stepboxes)) 370 371 self.stepboxes.append(stepbox) 371 372 372 self.vbox.pack_start(stepbox, expand=False)373 self.vbox.pack_start(stepbox, False, True, 0) 373 374 374 375 if self.lesson['type'] == 'balloon': 375 376 self.has_balloon_widgets = True … … class EditLessonScreen(gtk.VBox): 377 378 if not self.lesson.has_key('words') or len(self.lesson['words']) == 0: 378 379 self.lesson['words'] = [] 379 380 380 textlabel = gtk.Label()381 textlabel = Gtk.Label() 381 382 textlabel.set_markup("<span size='large' weight='bold'>" + _('Words') + "</span>") 382 383 textlabel.set_alignment(0.0, 0.5) 383 384 textlabel.set_padding(20, 0) 384 385 385 386 self.labelsizegroup.add_widget(textlabel) 386 387 387 self.wordstext = gtk.TextView(gtk.TextBuffer())388 self.wordstext.props.wrap_mode = gtk.WRAP_WORD389 self.wordstext.modify_font( pango.FontDescription('Monospace'))390 textscroll = gtk.ScrolledWindow()391 textscroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)388 self.wordstext = Gtk.TextView(Gtk.TextBuffer()) 389 self.wordstext.props.wrap_mode = Gtk.WrapMode.WORD 390 self.wordstext.modify_font(Pango.FontDescription('Monospace')) 391 textscroll = Gtk.ScrolledWindow() 392 textscroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) 392 393 textscroll.add(self.wordstext) 393 394 textscroll.set_size_request(-1, 200) 394 395 self.wordstext.get_buffer().set_text(' '.join(self.lesson['words'])) 395 396 396 textbox = gtk.HBox()397 textbox.pack_start(textlabel, expand=False)398 textbox.pack_start(textscroll )397 textbox = Gtk.HBox() 398 textbox.pack_start(textlabel, False, True, 0) 399 textbox.pack_start(textscroll, True, True, 0) 399 400 400 self.vbox.pack_start(textbox, expand=False)401 self.vbox.pack_start(textbox, False, True, 0) 401 402 402 403 # Medal requirements widgets. 403 medalslabel = gtk.Label()404 medalslabel = Gtk.Label() 404 405 medalslabel.set_markup("<span size='x-large'><b>" + _('Medal Requirements') + "</b></span>") 405 406 medalslabel.set_alignment(0.0, 0.5) 406 407 medalslabel.set_padding(10, 0) … … class EditLessonScreen(gtk.VBox): 412 413 self.medalboxes.append(self.build_medal(self.lesson['medals'][1], _('Silver'))) 413 414 self.medalboxes.append(self.build_medal(self.lesson['medals'][2], _('Gold'))) 414 415 415 self.vbox.pack_start(self.medalboxes[0], expand=False)416 self.vbox.pack_start(self.medalboxes[1], expand=False)417 self.vbox.pack_start(self.medalboxes[2], expand=False)416 self.vbox.pack_start(self.medalboxes[0], False, True, 0) 417 self.vbox.pack_start(self.medalboxes[1], False, True, 0) 418 self.vbox.pack_start(self.medalboxes[2], False, True, 0) 418 419 419 420 self.vbox.show_all() 420 421 … … class EditLessonScreen(gtk.VBox): 555 556 def generate_words_clicked_cb(self, btn): 556 557 self.activity.push_screen(WordListScreen(self.activity)) 557 558 558 class WordListScreen( gtk.VBox):559 class WordListScreen(Gtk.VBox): 559 560 def __init__(self, activity): 560 gtk.VBox.__init__(self)561 GObject.GObject.__init__(self) 561 562 self.set_border_width(10) 562 563 563 564 self.activity = activity 564 565 565 566 # Add the header. 566 title = gtk.Label()567 title = Gtk.Label() 567 568 title.set_markup("<span size='20000'><b>" + _("Edit Word List") + "</b></span>") 568 569 title.set_alignment(1.0, 0.0) 569 570 570 stoplabel = gtk.Label(_('Go Back'))571 stopbtn = gtk.Button()571 stoplabel = Gtk.Label(label=_('Go Back')) 572 stopbtn = Gtk.Button() 572 573 stopbtn.add(stoplabel) 573 574 stopbtn.connect('clicked', self.stop_clicked_cb) 574 575 575 titlebox = gtk.HBox()576 titlebox = Gtk.HBox() 576 577 titlebox.pack_start(stopbtn, False, False, 10) 577 578 titlebox.pack_end(title, False, False, 10) 578 579 579 subtitle = gtk.Label()580 subtitle = Gtk.Label() 580 581 subtitle.set_markup("<span size='10000'>" + _("Type or paste words here, for the Automatic Lesson Generator. If empty, the dictionary will be used.") + "</span>") 581 582 subtitle.set_alignment(1.0, 0.0) 582 583 583 self.wordlisttext = gtk.TextView(gtk.TextBuffer())584 self.wordlisttext.props.wrap_mode = gtk.WRAP_WORD585 wordlistscroll = gtk.ScrolledWindow()586 wordlistscroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)584 self.wordlisttext = Gtk.TextView(Gtk.TextBuffer()) 585 self.wordlisttext.props.wrap_mode = Gtk.WrapMode.WORD 586 wordlistscroll = Gtk.ScrolledWindow() 587 wordlistscroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) 587 588 wordlistscroll.add(self.wordlisttext) 588 589 wordlistscroll.set_size_request(-1, 75) 589 590 self.wordlisttext.get_buffer().set_text(' '.join(self.activity.wordlist)) 590 591 591 self.pack_start(titlebox, expand=False)592 self.pack_start(subtitle, expand=False)593 self.pack_start( gtk.HSeparator(), expand=False)594 self.pack_start(wordlistscroll )592 self.pack_start(titlebox, False, True, 0) 593 self.pack_start(subtitle, False, True, 0) 594 self.pack_start(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), expand=False, padding=0) 595 self.pack_start(wordlistscroll, True, True, 0) 595 596 596 597 self.show_all() 597 598 -
keyboard.py
diff --git a/keyboard.py b/keyboard.py index 35daeed..2936fb7 100644
a b 16 16 #!/usr/bin/env python 17 17 # vi:sw=4 et 18 18 19 import pygtk 20 pygtk.require('2.0') 21 import gtk 22 import rsvg 19 import cairo 20 import copy 23 21 import os, glob, re 24 import pango 22 23 from gi.repository import Gtk 24 from gi.repository import Pango 25 from gi.repository import PangoCairo 26 from gi.repository import Gdk 27 from gi.repository import GObject 28 from gi.repository import GdkPixbuf 29 from gi.repository import Rsvg 30 31 import StringIO 25 32 from port import json 26 33 import subprocess 27 34 from layouts.olpc import OLPC_LAYOUT … … class KeyboardImages: 125 132 126 133 # This is for not changing all the numbers of olpcm layout, 127 134 # that was made based on the original olpc layout. 135 136 # FIXME FIXME 128 137 scale_width = self.width 129 138 if _is_olpcm_model(): 130 139 scale_width = int(scale_width * 1.1625) 131 140 132 141 for filename in glob.iglob('images/OLPC*.svg'): 133 image = gtk.gdk.pixbuf_new_from_file_at_scale(filename, scale_width, 134 self.height, False) 142 image = Rsvg.Handle.new_from_file(filename) 135 143 name = os.path.basename(filename) 136 144 self.images[name] = image 137 145 … … class KeyboardData: 144 152 self.letter_map = {} 145 153 146 154 # Access the current GTK keymap. 147 self.keymap = gtk.gdk.keymap_get_default()155 self.keymap = Gdk.Keymap.get_default() 148 156 149 157 def set_layout(self, layout): 150 158 self._build_key_list(layout) … … class KeyboardData: 234 242 235 243 def format_key_sig(self, scan, state, group): 236 244 sig = 'scan%d' % scan 237 if state & gtk.gdk.SHIFT_MASK:245 if state & Gdk.ModifierType.SHIFT_MASK: 238 246 sig += ' shift' 239 if state & gtk.gdk.MOD5_MASK:247 if state & Gdk.ModifierType.MOD5_MASK: 240 248 sig += ' altgr' 241 249 if group != 0: 242 250 sig += ' group%d' % group … … class KeyboardData: 249 257 250 258 state = 0 251 259 if m.group('shift'): 252 state |= gtk.gdk.SHIFT_MASK260 state |= Gdk.ModifierType.SHIFT_MASK 253 261 if m.group('altgr'): 254 state |= gtk.gdk.MOD5_MASK262 state |= Gdk.ModifierType.MOD5_MASK 255 263 256 264 scan = int(m.group('scan')) 257 265 … … class KeyboardData: 277 285 best_result = None 278 286 279 287 for sig, l in self.letter_map.items(): 280 if unicode(l) == unicode(letter):288 if l == letter: 281 289 scan, state, group = self.parse_key_sig(sig) 282 290 283 291 # Choose the key with the fewest modifiers. 284 292 score = 0 285 if state & gtk.gdk.SHIFT_MASK: score += 1286 if state & gtk.gdk.MOD5_MASK: score += 1293 if state & Gdk.ModifierType.SHIFT_MASK: score += 1 294 if state & Gdk.ModifierType.MOD5_MASK: score += 1 287 295 if score < best_score: 288 296 best_score = score 289 297 best_result = scan, state, group … … class KeyboardData: 294 302 return k, best_result[1], best_result[2] 295 303 296 304 # Try the GDK keymap. 297 keyval = gtk.gdk.unicode_to_keyval(ord(letter))298 entries = self.keymap.get_entries_for_keyval(keyval)305 keyval = Gdk.unicode_to_keyval(ord(letter)) 306 valid, entries = self.keymap.get_entries_for_keyval(keyval) 299 307 for e in entries: 300 308 for k in self.keys: 301 if k['key-scan'] == e [0]:309 if k['key-scan'] == e.keycode: 302 310 # TODO: Level -> state calculations are hardcoded to what the XO keyboard does. 303 311 # They were discovered through experimentation. 304 312 state = 0 305 if e [2] & 1:306 state |= gtk.gdk.SHIFT_MASK307 if e [2] & 2:308 state |= gtk.gdk.MOD5_MASK309 return k, state, e [1]313 if e.level & 1: 314 state |= Gdk.ModifierType.SHIFT_MASK 315 if e.level & 2: 316 state |= Gdk.ModifierType.MOD5_MASK 317 return k, state, e.group 310 318 311 319 # Fail! 312 320 return None, None, None … … class KeyboardData: 316 324 if self.letter_map.has_key(sig): 317 325 return self.letter_map[sig] 318 326 else: 319 t = self.keymap.translate_keyboard_state(key['key-scan'], self.active_state, self.active_group) 320 if t: 321 return unichr(gtk.gdk.keyval_to_unicode(t[0])) 327 success, keyval, effective_group, level, consumed_modifiers = \ 328 self.keymap.translate_keyboard_state( 329 key['key-scan'], self.active_state, self.active_group) 330 if success: 331 return unichr(Gdk.keyval_to_unicode(keyval)).encode('utf-8') 322 332 323 333 return '' 324 334 325 class KeyboardWidget(KeyboardData, gtk.DrawingArea):335 class KeyboardWidget(KeyboardData, Gtk.DrawingArea): 326 336 """A GTK widget which implements an interactive visual keyboard, with support 327 337 for custom data driven layouts.""" 328 338 329 339 def __init__(self, image, root_window, poll_keys=False): 330 340 KeyboardData.__init__(self) 331 gtk.DrawingArea.__init__(self)341 GObject.GObject.__init__(self) 332 342 333 343 self.image = image 334 344 self.root_window = root_window … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 336 346 # Match the image cache in dimensions. 337 347 self.set_size_request(image.width, image.height) 338 348 339 self.connect(" expose-event", self._expose_cb)349 self.connect("draw", self._draw_cb) 340 350 341 #self.modify_font( pango.FontDescription('Monospace 10'))351 #self.modify_font(Pango.FontDescription('Monospace 10')) 342 352 343 353 # Active language group and modifier state. 344 354 # See http://www.pygtk.org/docs/pygtk/class-gdkkeymap.html for more … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 353 363 354 364 self.draw_hands = False 355 365 356 self.modify_bg( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#d0d0d0'))366 self.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse('#d0d0d0')[1]) 357 367 358 368 # Connect keyboard grabbing and releasing callbacks. 359 369 if poll_keys: … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 362 372 363 373 def _realize_cb(self, widget): 364 374 # Setup keyboard event snooping in the root window. 365 self.root_window.add_events( gtk.gdk.KEY_PRESS_MASK | gtk.gdk.KEY_RELEASE_MASK)375 self.root_window.add_events(Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.KEY_RELEASE_MASK) 366 376 self.key_press_cb_id = self.root_window.connect('key-press-event', self.key_press_release_cb) 367 377 self.key_release_cb_id = self.root_window.connect('key-release-event', self.key_press_release_cb) 368 378 … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 383 393 k['key-width'] = int(k['key-width'] * width_scale) 384 394 k['key-height'] = int(k['key-height'] * height_scale) 385 395 386 self._make_all_key_images() 387 388 def _make_key_images(self, key): 389 key['key-images'] = {} 390 for group in [0, 1]: 391 for state in [0, gtk.gdk.SHIFT_MASK, gtk.gdk.MOD5_MASK, gtk.gdk.SHIFT_MASK|gtk.gdk.MOD5_MASK]: 392 key['key-images'][(state, group)] = self.get_key_image(key, state, group) 396 def _draw_key(self, k, cr): 397 bounds = self.get_allocation() 393 398 394 def _make_all_key_images(self): 395 for key in self.keys: 396 self._make_key_images(key) 399 # HACK: this is a hack used when the widget is not shown yet, 400 # in that case bounds will be gtk.gdk.Rectangle(-1, -1, 1, 1) 401 # and the key will be outside the canvas. This is used only 402 # for the first key that appears below the instructions 403 if bounds.x == -1: 404 screen_x = screen_y = 0 405 else: 406 screen_x = int(bounds.width - self.image.width) / 2 407 screen_y = int(bounds.height - self.image.height) / 2 397 408 398 def _draw_key(self, k, draw, gc, for_pixmap, w=0, h=0): 399 x1 = 0 400 y1 = 0 401 x2 = w 402 y2 = h 409 x1 = k['key-x'] + screen_x 410 y1 = k['key-y'] + screen_y 411 x2 = x1 + k['key-width'] 412 y2 = y1 + k['key-height'] 403 413 404 # Outline rounded box.405 gc.foreground = self.get_colormap().alloc_color(int(0.4*65536),int(0.7*65536),int(0.4*65536))406 407 414 corner = 5 408 415 points = [ 409 (x1 + corner, y1), 416 (x1 + corner, y1), 410 417 (x2 - corner, y1), 411 418 (x2, y1 + corner), 412 419 (x2, y2 - corner), … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 414 421 (x1 + corner, y2), 415 422 (x1, y2 - corner), 416 423 (x1, y1 + corner) 417 ] 418 draw.draw_polygon(gc, True, points) 419 420 # Inner text. 421 gc.foreground = self.get_colormap().alloc_color(int(1.0*65536),int(1.0*65536),int(1.0*65536)) 424 ] 425 426 cr.new_path() 427 cr.set_source_rgb(0.396, 0.698, 0.392) 428 cr.set_line_width(2) 429 for point in points: 430 cr.line_to(*point) 431 cr.close_path() 432 cr.fill_preserve() 433 cr.stroke() 422 434 423 435 text = '' 424 436 if k['key-label']: 425 437 text = k['key-label'] 426 438 else: 427 text = self.get_letter_for_key_state_group(k, self.active_state, self.active_group) 428 429 try: 430 layout = self.create_pango_layout(unicode(text)) 431 layout.set_font_description(pango.FontDescription('Monospace')) 432 draw.draw_layout(gc, x1+8, y2-23, layout) 433 except: 434 pass 435 436 def _expose_hands(self, gc): 439 text = self.get_letter_for_key_state_group( 440 k, self.active_state, self.active_group) 441 442 cr.set_source_rgb(0, 0, 0) 443 pango_layout = PangoCairo.create_layout(cr) 444 fd = Pango.FontDescription('Monospace') 445 fd.set_size(10 * Pango.SCALE) 446 pango_layout.set_font_description(fd) 447 pango_layout.set_text(text, len(text)) 448 449 cr.move_to(x1 + 8, y2 - 23) 450 PangoCairo.update_layout(cr, pango_layout) 451 PangoCairo.show_layout(cr, pango_layout) 452 453 def _expose_hands(self, cr): 437 454 lhand_image = self.image.images['OLPC_Lhand_HOMEROW.svg'] 438 455 rhand_image = self.image.images['OLPC_Rhand_HOMEROW.svg'] 439 456 … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 451 468 rhand_image = handle 452 469 453 470 # Put the other hand on the SHIFT key if needed. 454 if state & gtk.gdk.SHIFT_MASK:471 if state & Gdk.ModifierType.SHIFT_MASK: 455 472 if finger[0] == 'L': 456 473 rhand_image = self.image.images['OLPC_Rhand_SHIFT.svg'] 457 474 else: … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 460 477 # TODO: Do something about ALTGR. 461 478 462 479 bounds = self.get_allocation() 463 screen_x = int(bounds.width-self.image.width)/2464 screen_y = int(bounds.height-self.image.height)/2465 466 self.window.draw_pixbuf(gc, lhand_image, 0, 0, screen_x, screen_y + HAND_YOFFSET)467 self.window.draw_pixbuf(gc, rhand_image, 0, 0, screen_x, screen_y + HAND_YOFFSET)468 469 def _expose_cb(self, area, event):470 gc = self.window.new_gc()471 472 bounds = self.get_allocation()473 screen_x = int(bounds.width-self.image.width)/2474 screen_y = int(bounds.height-self.image.height)/2475 480 481 cr.save() 482 xx = bounds.width / float(lhand_image.props.width) 483 yy = bounds.height / float(lhand_image.props.height) 484 # matrix = cairo.Matrix(xx=xx, yy=yy) 485 cr.scale(xx, yy) 486 lhand_image.set_dpi(250) 487 lhand_image.render_cairo(cr) 488 489 cr.restore() 490 xx = bounds.width / float(rhand_image.props.width) 491 yy = bounds.height / float(rhand_image.props.height) 492 # matrix = cairo.Matrix(xx=xx, yy=yy) 493 # cr.transform(matrix) 494 cr.scale(xx, yy) 495 rhand_image.render_cairo(cr) 496 497 def _draw_cb(self, area, cr): 476 498 # Draw the keys. 477 499 for k in self.keys: 478 x1 = k['key-x'] + screen_x 479 y1 = k['key-y'] + screen_y 480 x2 = x1 + k['key-width'] 481 y2 = y1 + k['key-height'] 482 483 # Index cached key images by state and group. 484 state = self.active_state & (gtk.gdk.SHIFT_MASK|gtk.gdk.MOD5_MASK) 485 index = (state, self.active_group) 486 image = k['key-images'].get(index) 487 488 if image: 489 self.window.draw_image(gc, image, 0, 0, x1, y1, x2-x1, y2-y1) 490 500 self._draw_key(k, cr) 501 491 502 # Draw overlay images. 492 503 if self.draw_hands: 493 self._expose_hands(gc) 494 504 self._expose_hands(cr) 495 505 return True 496 506 497 507 def key_press_release_cb(self, widget, event): 498 508 key = self.key_scan_map.get(event.hardware_keycode) 499 509 if key: 500 key['key-pressed'] = event.type == gtk.gdk.KEY_PRESS510 key['key-pressed'] = event.type == Gdk.EventType.KEY_PRESS 501 511 502 512 # Hack to get the current modifier state - which will not be represented by the event. 503 state = gtk.gdk.device_get_core_pointer().get_state(self.window)[1]513 # state = Gdk.device_get_core_pointer().get_state(self.get_window())[1] 504 514 505 if self.active_group != event.group or self.active_state != state:515 if self.active_group != event.group or self.active_state != event.state: 506 516 self.active_group = event.group 507 self.active_state = state517 self.active_state = event.state 508 518 509 519 self.queue_draw() 510 520 … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 512 522 sig = self.format_key_sig(event.hardware_keycode, event.state, event.group) 513 523 if not self.letter_map.has_key(sig): 514 524 self.letter_map[sig] = event.string 515 self._make_key_images(key)516 525 self.queue_draw() 517 526 518 527 return False 519 528 520 def _keys_changed_cb(self, keymap):521 self._make_key_images()522 523 529 def clear_hilite(self): 524 530 self.hilite_letter = None 525 531 self.queue_draw() … … class KeyboardWidget(KeyboardData, gtk.DrawingArea): 535 541 def get_key_pixbuf(self, key, state=0, group=0, scale=1): 536 542 w = int(key['key-width'] * scale) 537 543 h = int(key['key-height'] * scale) 538 544 539 545 old_state, old_group = self.active_state, self.active_group 540 546 self.active_state, self.active_group = state, group 541 542 pixmap = gtk.gdk.Pixmap(self.root_window.window, w, h)543 gc = pixmap.new_gc()544 545 gc.foreground = self.get_colormap().alloc_color('#d0d0d0')546 pixmap.draw_rectangle(gc, True, 0, 0, w, h)547 547 548 self._draw_key(key, pixmap, gc, True, w, h) 549 550 pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, w, h) 551 pb.get_from_drawable(pixmap, self.root_window.window.get_colormap(), 0, 0, 0, 0,w, h) 552 553 self.active_state, self.active_group = old_state, old_group 548 surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) 549 cr = cairo.Context(surface) 550 cr.set_source_rgb(1, 1, 1) 551 cr.rectangle(0, 0, w, h) 552 cr.fill() 554 553 555 return pb 554 # Duplicate the Key to be able to change its position values 555 key = copy.deepcopy(key) 556 key['key-x'] = 0 557 key['key-y'] = 0 556 558 557 def get_key_image(self, key, state=0, group=0, scale=1): 558 w = int(key['key-width'] * scale) 559 h = int(key['key-height'] * scale) 560 561 old_state, old_group = self.active_state, self.active_group 562 self.active_state, self.active_group = state, group 563 564 pixmap = gtk.gdk.Pixmap(self.root_window.window, w, h) 565 gc = pixmap.new_gc() 566 567 gc.foreground = self.get_colormap().alloc_color('#d0d0d0') 568 pixmap.draw_rectangle(gc, True, 0, 0, w, h) 559 self._draw_key(key, cr) 560 561 # Convert cairo.Surface to Pixbuf 562 pixbuf_data = StringIO.StringIO() 563 surface.write_to_png(pixbuf_data) 564 pxb_loader = GdkPixbuf.PixbufLoader.new_with_type('png') 565 pxb_loader.write(pixbuf_data.getvalue()) 566 temp_pix = pxb_loader.get_pixbuf() 567 pxb_loader.close() 569 568 570 self._draw_key(key, pixmap, gc, True, w, h)571 572 image = pixmap.get_image(0, 0, w, h)573 574 569 self.active_state, self.active_group = old_state, old_group 575 570 576 return image 577 571 return temp_pix -
keybuilder.py
diff --git a/keybuilder.py b/keybuilder.py index bceab0d..28f6f97 100755
a b 19 19 import sys 20 20 import keyboard 21 21 22 import gtk22 from gi.repository import Gtk 23 23 24 window = gtk.Window(gtk.WINDOW_TOPLEVEL)24 window = Gtk.Window(Gtk.WindowType.TOPLEVEL) 25 25 window.set_title("keyboard widget") 26 window.connect("destroy", lambda w: gtk.main_quit())26 window.connect("destroy", lambda w: Gtk.main_quit()) 27 27 window.show_all() 28 28 window.realize() 29 29 … … except: 37 37 pass 38 38 k.set_layout(keyboard.get_layout()) 39 39 40 savebtn = gtk.Button()41 savebtn.add( gtk.Label('Save Keys'))40 savebtn = Gtk.Button() 41 savebtn.add(Gtk.Label(label='Save Keys')) 42 42 savebtn.connect('clicked', lambda w: k.save_letter_map(sys.argv[1])) 43 43 44 quitbtn = gtk.Button()45 quitbtn.add( gtk.Label('Quit'))46 quitbtn.connect('clicked', lambda w: gtk.main_quit())44 quitbtn = Gtk.Button() 45 quitbtn.add(Gtk.Label(label='Quit')) 46 quitbtn.connect('clicked', lambda w: Gtk.main_quit()) 47 47 48 hbox = gtk.HBox()49 hbox.pack_start(savebtn )50 hbox.pack_start(quitbtn )48 hbox = Gtk.HBox() 49 hbox.pack_start(savebtn, True, True, 0) 50 hbox.pack_start(quitbtn, True, True, 0) 51 51 52 vbox = gtk.VBox()53 vbox.pack_start(k )54 vbox.pack_start(hbox )52 vbox = Gtk.VBox() 53 vbox.pack_start(k, True, True, 0) 54 vbox.pack_start(hbox, True, True, 0) 55 55 56 56 window.add(vbox) 57 57 window.show_all() 58 58 59 gtk.main()59 Gtk.main() 60 60 -
lessonbuilder.py
diff --git a/lessonbuilder.py b/lessonbuilder.py index b33a34e..c7c4d1a 100755
a b from gettext import gettext as _ 21 21 from port import json 22 22 23 23 # For modifier constants. 24 import gtk24 from gi.repository import Gtk 25 25 26 26 # Set up remote debugging. 27 27 #import dbgp.client … … def build_key_steps( 338 338 # except: 339 339 # error("The '%s' letter (scan code %x) does not have a finger assigned." % (letter, key['key-scan'])) 340 340 # 341 # if state == gtk.gdk.SHIFT_MASK:341 # if state == Gdk.ModifierType.SHIFT_MASK: 342 342 # # Choose the finger to press the SHIFT key with. 343 343 # if key['key-finger'][0] == 'R': 344 344 # shift_finger = FINGERS['LP'] … … def build_key_steps( 348 348 # instructions = _('Press and hold the SHIFT key with your %(finger)s finger, ') % { 'finger': shift_finger } 349 349 # instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger } 350 350 # 351 # elif state == gtk.gdk.MOD5_MASK:351 # elif state == Gdk.ModifierType.MOD5_MASK: 352 352 # instructions = _('Press and hold the ALTGR key, ') 353 353 # instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger } 354 354 # 355 # elif state == gtk.gdk.SHIFT_MASK | gtk.gdk.MOD5_MASK:355 # elif state == Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.MOD5_MASK: 356 356 # instructions = _('Press and hold the ALTGR and SHIFT keys, ') 357 357 # instructions += _('then press the %(letter)s key with your %(finger)s finger.') % { 'letter': letter, 'finger': finger } 358 358 # -
lessonscreen.py
diff --git a/lessonscreen.py b/lessonscreen.py index cc75ef4..fb38bff 100644
a b 19 19 import logging, os, math, time, copy, locale, datetime, random, re 20 20 from gettext import gettext as _ 21 21 22 # Import PyGTK. 23 import gobject, pygtk, gtk, pango 22 from gi.repository import Gtk 23 from gi.repository import Gdk 24 from gi.repository import GObject 24 25 25 26 # Import Sugar UI modules. 26 import sugar .activity.activity27 from sugar .graphics import *27 import sugar3.activity.activity 28 from sugar3.graphics import * 28 29 29 30 # Import activity modules. 30 31 import keyboard, medalscreen … … FINGERS = { 48 49 'RT': _('right thumb'), 49 50 } 50 51 51 class LessonScreen( gtk.VBox):52 class LessonScreen(Gtk.VBox): 52 53 def __init__(self, lesson, keyboard_images, activity): 53 gtk.VBox.__init__(self)54 GObject.GObject.__init__(self) 54 55 55 56 self.lesson = lesson 56 57 self.keyboard_images = keyboard_images 57 58 self.activity = activity 58 59 59 60 # Build the user interface. 60 title = gtk.Label()61 title = Gtk.Label() 61 62 title.set_markup("<span size='x-large' weight='bold'>" + lesson['name'] + "</span>") 62 63 title.set_alignment(1.0, 0.0) 63 64 64 stoplabel = gtk.Label(_('Go Back'))65 stopbtn = gtk.Button()65 stoplabel = Gtk.Label(label=_('Go Back')) 66 stopbtn = Gtk.Button() 66 67 stopbtn.add(stoplabel) 67 68 stopbtn.connect('clicked', self.stop_cb) 68 69 69 # TODO- These will be replaced by graphical displays using gtk.DrawingArea.70 # TODO- These will be replaced by graphical displays using Gtk.DrawingArea. 70 71 self.wpm = 0 71 72 self.accuracy = 0 72 73 73 self.wpmlabel = gtk.Label()74 self.accuracylabel = gtk.Label()74 self.wpmlabel = Gtk.Label() 75 self.accuracylabel = Gtk.Label() 75 76 76 #self.wpmarea = gtk.DrawingArea()77 #self.wpmarea = Gtk.DrawingArea() 77 78 #self.wpmarea.connect('expose-event', self.wpm_expose_cb) 78 #self.accuracyarea = gtk.DrawingArea()79 #self.accuracyarea = Gtk.DrawingArea() 79 80 #self.accuracyarea.connect('expose-event', self.accuracy_expose_cb) 80 81 81 hbox = gtk.HBox()82 hbox = Gtk.HBox() 82 83 hbox.pack_start(stopbtn, False, False, 10) 83 84 hbox.pack_start(self.wpmlabel, True, False, 10) 84 85 hbox.pack_start(self.accuracylabel, True, False, 10) 85 86 hbox.pack_end(title, False, False, 10) 86 87 87 88 # Set up font styles. 88 self.tagtable = gtk.TextTagTable()89 instructions_tag = gtk.TextTag('instructions')90 instructions_tag.props.justification = gtk.JUSTIFY_CENTER89 self.tagtable = Gtk.TextTagTable() 90 instructions_tag = Gtk.TextTag.new('instructions') 91 instructions_tag.props.justification = Gtk.Justification.CENTER 91 92 self.tagtable.add(instructions_tag) 92 93 93 text_tag = gtk.TextTag('text')94 text_tag = Gtk.TextTag.new('text') 94 95 text_tag.props.family = 'Monospace' 95 96 self.tagtable.add(text_tag) 96 97 97 spacer_tag = gtk.TextTag('spacer')98 spacer_tag = Gtk.TextTag.new('spacer') 98 99 spacer_tag.props.size = 3000 99 100 self.tagtable.add(spacer_tag) 100 101 101 image_tag = gtk.TextTag('image')102 image_tag.props.justification = gtk.JUSTIFY_CENTER102 image_tag = Gtk.TextTag.new('image') 103 image_tag.props.justification = Gtk.Justification.CENTER 103 104 self.tagtable.add(image_tag) 104 105 105 correct_copy_tag = gtk.TextTag('correct-copy')106 correct_copy_tag = Gtk.TextTag.new('correct-copy') 106 107 correct_copy_tag.props.family = 'Monospace' 107 108 correct_copy_tag.props.foreground = '#0000ff' 108 109 self.tagtable.add(correct_copy_tag) 109 110 110 incorrect_copy_tag = gtk.TextTag('incorrect-copy')111 incorrect_copy_tag = Gtk.TextTag.new('incorrect-copy') 111 112 incorrect_copy_tag.props.family = 'Monospace' 112 113 incorrect_copy_tag.props.foreground = '#ff0000' 113 114 self.tagtable.add(incorrect_copy_tag) 114 115 115 116 # Set up the scrolling lesson text view. 116 self.lessonbuffer = gtk.TextBuffer(self.tagtable)117 self.lessontext = gtk.TextView(self.lessonbuffer)117 self.lessonbuffer = Gtk.TextBuffer.new(self.tagtable) 118 self.lessontext = Gtk.TextView.new_with_buffer(self.lessonbuffer) 118 119 self.lessontext.set_editable(False) 119 120 self.lessontext.set_left_margin(20) 120 121 self.lessontext.set_right_margin(20) 121 self.lessontext.set_wrap_mode( gtk.WRAP_WORD)122 self.lessontext.modify_base( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#ffffcc'))122 self.lessontext.set_wrap_mode(Gtk.WrapMode.WORD) 123 self.lessontext.modify_base(Gtk.StateType.NORMAL, Gdk.Color.parse('#ffffcc')[1]) 123 124 124 self.lessonscroll = gtk.ScrolledWindow()125 self.lessonscroll.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)125 self.lessonscroll = Gtk.ScrolledWindow() 126 self.lessonscroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.ALWAYS) 126 127 self.lessonscroll.add(self.lessontext) 127 128 128 frame = gtk.Frame()129 frame = Gtk.Frame() 129 130 frame.add(self.lessonscroll) 130 131 131 132 self.keyboard = keyboard.KeyboardWidget(self.keyboard_images, self.activity) … … class LessonScreen(gtk.VBox): 140 141 self.keyboard.set_layout(keyboard.get_layout()) 141 142 142 143 self.pack_start(hbox, False, False, 10) 143 self.pack_start(frame, True, True )144 self.pack_start(self.keyboard, False )144 self.pack_start(frame, True, True, 0) 145 self.pack_start(self.keyboard, False, True, 0) 145 146 146 147 # Connect keyboard grabbing and releasing callbacks. 147 148 self.connect('realize', self.realize_cb) … … class LessonScreen(gtk.VBox): 154 155 self.begin_lesson() 155 156 156 157 def realize_cb(self, widget): 157 self.activity.add_events( gtk.gdk.KEY_PRESS_MASK|gtk.gdk.KEY_RELEASE_MASK)158 self.activity.add_events(Gdk.EventMask.KEY_PRESS_MASK|Gdk.EventMask.KEY_RELEASE_MASK) 158 159 self.key_press_cb_id = self.activity.connect('key-press-event', self.key_cb) 159 160 self.key_release_cb_id = self.activity.connect('key-release-event', self.key_cb) 160 161 … … class LessonScreen(gtk.VBox): 164 165 165 166 def start_timer(self): 166 167 self.start_time = time.time() 167 self.timer_id = gobject.timeout_add(1000, self.timer_cb)168 self.timer_id = GObject.timeout_add(1000, self.timer_cb) 168 169 169 170 def stop_timer(self): 170 171 if self.timer_id: 171 gobject.source_remove(self.timer_id)172 GObject.source_remove(self.timer_id) 172 173 self.start_time = None 173 174 self.timer_id = None 174 175 … … class LessonScreen(gtk.VBox): 349 350 350 351 # Extract information about the key pressed. 351 352 key = event.string 352 key_name = gtk.gdk.keyval_name(event.keyval)353 key_name = Gdk.keyval_name(event.keyval) 353 354 354 355 # Ignore events which don't produce a character, except backspace. 355 356 if not (key_name == 'BackSpace' or key): 356 357 return True 357 358 358 359 # Ignore either press or release events, depending on mode. 359 if self.mode == 'key' and event.type == gtk.gdk.KEY_PRESS:360 if self.mode == 'key' and event.type == Gdk.EventType.KEY_PRESS: 360 361 return True 361 if self.mode != 'key' and event.type == gtk.gdk.KEY_RELEASE:362 if self.mode != 'key' and event.type == Gdk.EventType.KEY_RELEASE: 362 363 return True 363 364 364 365 # Ignore hotkeys. 365 if event. state & (gtk.gdk.CONTROL_MASK | gtk.gdk.MOD1_MASK):366 if event.get_state() & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK): 366 367 return True 367 368 368 369 # Convert Return keys to paragraph symbols. … … class LessonScreen(gtk.VBox): 474 475 self.lessontext.grab_focus() 475 476 476 477 # Scroll the TextView so the cursor is on screen. 477 self.lessontext.scroll_to_mark(self.lessonbuffer.get_insert(), 0) 478 self.lessontext.scroll_to_mark(self.lessonbuffer.get_insert(), 0, 479 use_align=False, xalign=0.5, yalign=0.5) 478 480 479 481 # In Key mode, display the finger hint and the key image. 480 482 if self.mode == 'key': … … class LessonScreen(gtk.VBox): 499 501 except: 500 502 finger = '' 501 503 502 if state == gtk.gdk.SHIFT_MASK:504 if state == Gdk.ModifierType.SHIFT_MASK: 503 505 # Choose the finger to press the SHIFT key with. 504 506 if key['key-finger'][0] == 'R': 505 507 shift_finger = FINGERS['LP'] … … class LessonScreen(gtk.VBox): 509 511 instructions = _('Press and hold the shift key with your %(finger)s, ') % { 'finger': shift_finger } 510 512 instructions += _('then press the %(letter)s key with your %(finger)s.') % { 'letter': letter, 'finger': finger } 511 513 512 elif state == gtk.gdk.MOD5_MASK:514 elif state == Gdk.ModifierType.MOD5_MASK: 513 515 instructions = _('Press and hold the altgr key, ') 514 516 instructions += _('then press the %(letter)s key with your %(finger)s.') % { 'letter': letter, 'finger': finger } 515 517 516 elif state == gtk.gdk.SHIFT_MASK | gtk.gdk.MOD5_MASK:518 elif state == Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.MOD5_MASK: 517 519 instructions = _('Press and hold the altgr and shift keys, ') 518 520 instructions += _('then press the %(letter)s key with your %(finger)s.') % { 'letter': letter, 'finger': finger } 519 521 … … class LessonScreen(gtk.VBox): 522 524 523 525 self.lessonbuffer.insert(self.lessonbuffer.get_end_iter(), instructions + '\n\n') 524 526 525 if state & gtk.gdk.SHIFT_MASK:527 if state & Gdk.ModifierType.SHIFT_MASK: 526 528 shift_key = self.keyboard.find_key_by_label('shift') 527 529 pixbuf = self.keyboard.get_key_pixbuf(shift_key, scale=1) 528 530 self.lessonbuffer.insert_pixbuf(self.lessonbuffer.get_end_iter(), pixbuf) 529 531 self.lessonbuffer.insert(self.lessonbuffer.get_end_iter(), ' ') 530 532 531 if state & gtk.gdk.MOD5_MASK:533 if state & Gdk.ModifierType.MOD5_MASK: 532 534 altgr_key = self.keyboard.find_key_by_label('altgr') 533 535 pixbuf = self.keyboard.get_key_pixbuf(altgr_key, scale=1) 534 536 self.lessonbuffer.insert_pixbuf(self.lessonbuffer.get_end_iter(), pixbuf) -
mainscreen.py
diff --git a/mainscreen.py b/mainscreen.py index 2762baf..dbc85c6 100644
a b import logging, os, math, time, copy, locale, datetime, random, re, glob 19 19 from gettext import gettext as _ 20 20 from port import json 21 21 22 # Import PyGTK. 23 import gobject, pygtk, gtk, pango 22 from gi.repository import Gtk 23 from gi.repository import Gdk 24 from gi.repository import GdkPixbuf 25 from gi.repository import GObject 24 26 25 27 # Import Sugar UI modules. 26 import sugar .activity.activity27 from sugar .graphics import *28 import sugar3.activity.activity 29 from sugar3.graphics import * 28 30 29 31 # Import activity modules. 30 32 import lessonscreen, medalscreen … … import keyboard 38 40 # http://commons.wikimedia.org/wiki/File:Silver_medal_world_centered.svg 39 41 # http://commons.wikimedia.org/wiki/File:Bronze_medal_world_centered.svg 40 42 41 class MainScreen( gtk.VBox):43 class MainScreen(Gtk.VBox): 42 44 def __init__(self, activity): 43 gtk.VBox.__init__(self)45 GObject.GObject.__init__(self) 44 46 45 47 self.activity = activity 46 48 … … class MainScreen(gtk.VBox): 48 50 self.titlescene = titlescene.TitleScene() 49 51 50 52 # Build lessons list. 51 self.lessonbox = gtk.HBox()53 self.lessonbox = Gtk.HBox() 52 54 53 #nexticon = sugar .graphics.icon.Icon(icon_name='go-next')55 #nexticon = sugar3.graphics.icon.Icon(icon_name='go-next') 54 56 #self.nextlessonbtn.add(nexticon) 55 nextlabel = gtk.Label()57 nextlabel = Gtk.Label() 56 58 nextlabel.set_markup("<span size='large'>" + _('Next') + "</span>") 57 59 58 self.nextlessonbtn = gtk.Button()60 self.nextlessonbtn = Gtk.Button() 59 61 self.nextlessonbtn.add(nextlabel) 60 62 self.nextlessonbtn.connect('clicked', self.next_lesson_clicked_cb) 61 63 62 #previcon = sugar .graphics.icon.Icon(icon_name='go-previous')64 #previcon = sugar3.graphics.icon.Icon(icon_name='go-previous') 63 65 #self.prevlessonbtn.add(previcon) 64 prevlabel = gtk.Label()66 prevlabel = Gtk.Label() 65 67 prevlabel.set_markup("<span size='large'>" + _('Previous') + "</span>") 66 68 67 self.prevlessonbtn = gtk.Button()69 self.prevlessonbtn = Gtk.Button() 68 70 self.prevlessonbtn.add(prevlabel) 69 71 self.prevlessonbtn.connect('clicked', self.prev_lesson_clicked_cb) 70 72 71 lessonlabel = gtk.Label()73 lessonlabel = Gtk.Label() 72 74 lessonlabel.set_markup("<span size='x-large' weight='bold'>" + _('Start Lesson') + "</span>") 73 75 74 lessonbtn = gtk.Button()76 lessonbtn = Gtk.Button() 75 77 lessonbtn.add(lessonlabel) 76 78 lessonbtn.connect('clicked', self.lesson_clicked_cb) 77 lessonbtn.modify_bg( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#60b060'))79 lessonbtn.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse('#60b060')[1]) 78 80 79 81 # Load lessons for this language. 80 82 code = locale.getdefaultlocale()[0] or 'en_US' 81 lessons_path = os.path.join(sugar .activity.activity.get_bundle_path(), 'lessons')83 lessons_path = os.path.join(sugar3.activity.activity.get_bundle_path(), 'lessons') 82 84 lessons_file = os.path.join(lessons_path, code + '.lessons') 83 85 if os.path.isfile(lessons_file): 84 86 self.load_lessons(lessons_file) … … class MainScreen(gtk.VBox): 99 101 self.lessons.sort(lambda x, y: x.get('order', 0) - y.get('order', 0)) 100 102 101 103 # Load all the keyboard images. 102 width = int( gtk.gdk.screen_width())103 height = int( gtk.gdk.screen_height()*0.3)104 width = int(Gdk.Screen.width()) 105 height = int(Gdk.Screen.height()*0.3) 104 106 self.keyboard_images = keyboard.KeyboardImages(width, height) 105 107 self.keyboard_images.load_images() 106 108 107 navbox = gtk.HBox()109 navbox = Gtk.HBox() 108 110 navbox.set_spacing(10) 109 navbox.pack_start(self.prevlessonbtn, True )110 navbox.pack_start(lessonbtn, True )111 navbox.pack_start(self.nextlessonbtn, True )111 navbox.pack_start(self.prevlessonbtn, True, True, 0) 112 navbox.pack_start(lessonbtn, True, True, 0) 113 navbox.pack_start(self.nextlessonbtn, True, True, 0) 112 114 113 lessonbox = gtk.VBox()115 lessonbox = Gtk.VBox() 114 116 lessonbox.set_spacing(10) 115 lessonbox.pack_start(navbox, False )116 lessonbox.pack_start(self.lessonbox )117 lessonbox.pack_start(navbox, False, True, 0) 118 lessonbox.pack_start(self.lessonbox, True, True, 0) 117 119 118 120 self.pack_start(self.titlescene, False, True, 10) 119 self.pack_start(lessonbox, True )121 self.pack_start(lessonbox, True, True, 0) 120 122 121 123 self.show_next_lesson() 122 124 … … class MainScreen(gtk.VBox): 165 167 medal_type = self.activity.data['medals'][lesson['name']]['type'] 166 168 167 169 # Create the lesson button. 168 namelabel = gtk.Label()170 namelabel = Gtk.Label() 169 171 namelabel.set_alignment(0.5, 0.5) 170 172 namelabel.set_markup("<span size='x-large' weight='bold'>" + lesson['name'] + "</span>") 171 desclabel = gtk.Label()173 desclabel = Gtk.Label() 172 174 desclabel.set_alignment(0.5, 0.5) 173 175 desclabel.set_markup("<span size='large' color='#606060'>" + lesson['description'] + "</span>") 174 176 … … class MainScreen(gtk.VBox): 177 179 else: 178 180 hint = '' 179 181 180 #hintlabel = gtk.Label()182 #hintlabel = Gtk.Label() 181 183 #hintlabel.set_alignment(0.0, 0.8) 182 184 #hintlabel.set_markup("<span size='8000' color='#606020'>" + hint + "</span>") 183 185 184 labelbox = gtk.VBox()186 labelbox = Gtk.VBox() 185 187 labelbox.set_spacing(10) 186 188 labelbox.set_border_width(20) 187 labelbox.pack_start(namelabel, False )188 labelbox.pack_start(desclabel, False )189 #labelbox.pack_start(hintlabel )189 labelbox.pack_start(namelabel, False, True, 0) 190 labelbox.pack_start(desclabel, False, True, 0) 191 #labelbox.pack_start(hintlabel, True, True, 0) 190 192 191 193 # Create the medal image. 192 194 images = { … … class MainScreen(gtk.VBox): 196 198 'gold': 'images/gold-medal.svg' 197 199 } 198 200 199 medal_size = int(2.0 * sugar .graphics.style.GRID_CELL_SIZE)200 medalpixbuf = gtk.gdk.pixbuf_new_from_file(images[medal_type])201 medalpixbuf = medalpixbuf.scale_simple(medal_size, medal_size, gtk.gdk.INTERP_BILINEAR)201 medal_size = int(2.0 * sugar3.graphics.style.GRID_CELL_SIZE) 202 medalpixbuf = GdkPixbuf.Pixbuf.new_from_file(images[medal_type]) 203 medalpixbuf = medalpixbuf.scale_simple(medal_size, medal_size, GdkPixbuf.InterpType.BILINEAR) 202 204 203 medalimage = gtk.Image()205 medalimage = Gtk.Image() 204 206 medalimage.set_from_pixbuf(medalpixbuf) 205 207 206 208 names = { … … class MainScreen(gtk.VBox): 209 211 'silver': _('Silver Medal'), 210 212 'gold': _('Gold Medal'), 211 213 } 212 medallabel = gtk.Label(names[medal_type])214 medallabel = Gtk.Label(label=names[medal_type]) 213 215 214 medalbox = gtk.VBox()215 medalbox.pack_start(medalimage )216 medalbox.pack_start(medallabel )216 medalbox = Gtk.VBox() 217 medalbox.pack_start(medalimage, True, True, 0) 218 medalbox.pack_start(medallabel, True, True, 0) 217 219 218 medalbtn = gtk.Button()220 medalbtn = Gtk.Button() 219 221 medalbtn.add(medalbox) 220 222 medalbtn.connect('clicked', self.medal_clicked_cb) 221 223 222 224 # Hilite the button in the direction of the first unmedaled lesson. 223 225 next_index = self.get_next_lesson() 224 226 if next_index > self.lesson_index and index < len(self.lessons)-1: 225 self.nextlessonbtn.modify_bg( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#ff8080'))227 self.nextlessonbtn.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse('#ff8080')[1]) 226 228 else: 227 self.nextlessonbtn.modify_bg( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#40a040'))229 self.nextlessonbtn.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse('#40a040')[1]) 228 230 if next_index < self.lesson_index and index > 0: 229 self.prevlessonbtn.modify_bg( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#ff8080'))231 self.prevlessonbtn.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse('#ff8080')[1]) 230 232 else: 231 self.prevlessonbtn.modify_bg( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#40a040'))233 self.prevlessonbtn.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse('#40a040')[1]) 232 234 233 self.lessonbox.pack_start(labelbox, True )235 self.lessonbox.pack_start(labelbox, True, True, 0) 234 236 if medal_type != 'none': 235 self.lessonbox.pack_start(medalbtn, False )237 self.lessonbox.pack_start(medalbtn, False, True, 0) 236 238 237 239 self.lessonbox.show_all() 238 240 -
medalscreen.py
diff --git a/medalscreen.py b/medalscreen.py index a1d881a..d9cf410 100644
a b 18 18 import logging, os, math, time, copy, locale, datetime, random, re 19 19 from gettext import gettext as _ 20 20 21 # Import PyGTK. 22 import gobject, pygtk, gtk, pango 21 from gi.repository import Gtk 22 from gi.repository import Gdk 23 from gi.repository import GdkPixbuf 24 from gi.repository import GObject 23 25 24 26 # Import Sugar UI modules. 25 import sugar .activity.activity26 import sugar .graphics.style27 import sugar3.activity.activity 28 import sugar3.graphics.style 27 29 28 class MedalScreen( gtk.EventBox):29 MEDAL_SIZE = int(4.5 * sugar .graphics.style.GRID_CELL_SIZE)30 class MedalScreen(Gtk.EventBox): 31 MEDAL_SIZE = int(4.5 * sugar3.graphics.style.GRID_CELL_SIZE) 30 32 31 33 def __init__(self, medal, activity): 32 gtk.EventBox.__init__(self)34 GObject.GObject.__init__(self) 33 35 34 self.modify_bg( gtk.STATE_NORMAL, self.get_colormap().alloc_color('#ffffff'))36 self.modify_bg(Gtk.StateType.NORMAL, Gdk.Color.parse('#ffffff')[1]) 35 37 36 38 self.medal = medal 37 39 self.activity = activity … … class MedalScreen(gtk.EventBox): 43 45 'silver': 'images/silver-medal.svg', 44 46 'gold': 'images/gold-medal.svg' 45 47 } 46 medalpixbuf = gtk.gdk.pixbuf_new_from_file(images[medal_type])47 medalpixbuf = medalpixbuf.scale_simple(MedalScreen.MEDAL_SIZE, MedalScreen.MEDAL_SIZE, gtk.gdk.INTERP_BILINEAR)48 medalpixbuf = GdkPixbuf.Pixbuf.new_from_file(images[medal_type]) 49 medalpixbuf = medalpixbuf.scale_simple(MedalScreen.MEDAL_SIZE, MedalScreen.MEDAL_SIZE, GdkPixbuf.InterpType.BILINEAR) 48 50 49 medalimage = gtk.Image()51 medalimage = Gtk.Image() 50 52 medalimage.set_from_pixbuf(medalpixbuf) 51 53 52 54 # Certifications section. 53 title = gtk.Label()55 title = Gtk.Label() 54 56 title.set_markup(_("<span font_desc='Serif Bold Italic 28'>Certificate of Achievement</span>")) 55 57 56 text0 = gtk.Label()58 text0 = Gtk.Label() 57 59 text0.set_markup(_("<span font_desc='Sans 16'>This certifies that</span>")) 58 60 59 text1 = gtk.Label()61 text1 = Gtk.Label() 60 62 text1.set_markup(_("<span font_desc='Sans 16'><b><u><i>%(nick)s</i></u></b></span>") % medal) 61 63 62 text2 = gtk.Label()64 text2 = Gtk.Label() 63 65 text2.set_markup(_("<span font_desc='Sans 16'>earned a %(type)s medal in </span>") % medal) 64 66 65 text3 = gtk.Label()67 text3 = Gtk.Label() 66 68 text3.set_markup(_("<span font_desc='Sans 16'>in <b><u><i>%(lesson)s</i></u></b></span>") % medal) 67 69 68 text4 = gtk.Label()70 text4 = Gtk.Label() 69 71 text4.set_markup(_("<span font_desc='Sans 16'>on <b><u><i>%(date)s</i></u></b>.</span>") % medal) 70 72 71 textbox = gtk.VBox()72 textbox.pack_start(text0 )73 textbox.pack_start(text1 )74 textbox.pack_start(text2 )75 textbox.pack_start(text3 )76 textbox.pack_start(text4 )73 textbox = Gtk.VBox() 74 textbox.pack_start(text0, True, True, 0) 75 textbox.pack_start(text1, True, True, 0) 76 textbox.pack_start(text2, True, True, 0) 77 textbox.pack_start(text3, True, True, 0) 78 textbox.pack_start(text4, True, True, 0) 77 79 78 medalbox = gtk.HBox()79 medalbox.pack_start(textbox )80 medalbox.pack_end(medalimage )80 medalbox = Gtk.HBox() 81 medalbox.pack_start(textbox, True, True, 0) 82 medalbox.pack_end(medalimage, True, True, 0) 81 83 82 84 # Stats section. 83 statbox = gtk.HBox()85 statbox = Gtk.HBox() 84 86 if medal.has_key('wpm'): 85 stat1 = gtk.Label()87 stat1 = Gtk.Label() 86 88 stat1.set_markup("<span size='18000'>" + (_('<b>Words Per Minute:</b> %(wpm)d') % medal) + "</span>" ) 87 statbox.pack_start(stat1, True )89 statbox.pack_start(stat1, True, True, 0) 88 90 89 stat2 = gtk.Label()91 stat2 = Gtk.Label() 90 92 stat2.set_markup("<span size='18000'>" + (_('<b>Accuracy:</b> %(accuracy)d%%') % medal) + "</span>" ) 91 statbox.pack_start(stat2, True )93 statbox.pack_start(stat2, True, True, 0) 92 94 93 95 elif medal.has_key('score'): 94 stat1 = gtk.Label()96 stat1 = Gtk.Label() 95 97 stat1.set_markup("<span size='18000'>" + (_('<b>SCORE:</b> %(score)d') % medal) + "</span>" ) 96 statbox.pack_start(stat1, True )98 statbox.pack_start(stat1, True, True, 0) 97 99 98 oklabel = gtk.Label()100 oklabel = Gtk.Label() 99 101 oklabel.set_markup("<span size='10000'>" + _('Press the ENTER key to continue.') + '</span>') 100 self.okbtn = gtk.Button()102 self.okbtn = Gtk.Button() 101 103 self.okbtn.add(oklabel) 102 104 self.okbtn.connect('clicked', self.ok_cb) 103 105 104 btnbox = gtk.HBox()106 btnbox = Gtk.HBox() 105 107 btnbox.pack_start(self.okbtn, True, True, 100) 106 108 107 vbox = gtk.VBox()109 vbox = Gtk.VBox() 108 110 109 111 vbox.pack_start(title, False, False, 0) 110 112 vbox.pack_start(medalbox, True, False, 0) 111 vbox.pack_start( gtk.HSeparator(), False, False, 20)113 vbox.pack_start(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), False, False, 20) 112 114 vbox.pack_start(statbox, False, False, 0) 113 115 114 frame = gtk.Frame()116 frame = Gtk.Frame() 115 117 frame.add(vbox) 116 118 frame.set_border_width(10) 117 119 118 box = gtk.VBox()119 box.pack_start(frame, True, True )120 box = Gtk.VBox() 121 box.pack_start(frame, True, True, 0) 120 122 box.pack_start(btnbox, False, False, 40) 121 123 122 124 self.add(box) … … class MedalScreen(gtk.EventBox): 127 129 128 130 def realize_cb(self, widget): 129 131 # For some odd reason, if I do this in the constructor, nothing happens. 130 self.okbtn.set_ flags(gtk.CAN_DEFAULT)132 self.okbtn.set_can_default(True) 131 133 self.okbtn.grab_default() 132 134 133 135 def ok_cb(self, widget): -
port/chooser.py
diff --git a/port/chooser.py b/port/chooser.py index e2df259..1391942 100644
a b 14 14 15 15 """Object chooser method""" 16 16 17 import gtk17 from gi.repository import Gtk 18 18 import logging 19 19 20 from sugar import mime21 from sugar .graphics.objectchooser import ObjectChooser20 from sugar3 import mime 21 from sugar3.graphics.objectchooser import ObjectChooser 22 22 23 23 TEXT = hasattr(mime, 'GENERIC_TYPE_TEXT') and mime.GENERIC_TYPE_TEXT or None 24 24 IMAGE = hasattr(mime, 'GENERIC_TYPE_IMAGE') and mime.GENERIC_TYPE_IMAGE or None … … def pick(cb=None, default=None, parent=None, what=None): 45 45 out = None 46 46 47 47 try: 48 if chooser.run() == gtk.RESPONSE_ACCEPT:48 if chooser.run() == Gtk.ResponseType.ACCEPT: 49 49 jobject = chooser.get_selected_object() 50 50 logging.debug('ObjectChooser: %r' % jobject) 51 51 -
setup.py
diff --git a/setup.py b/setup.py index 77fda74..c17ead5 100755
a b 1 1 #!/usr/bin/env python 2 from sugar .activity import bundlebuilder2 from sugar3.activity import bundlebuilder 3 3 if __name__ == "__main__": 4 4 bundlebuilder.start() -
titlescene.py
diff --git a/titlescene.py b/titlescene.py index 7cc2d68..9739152 100644
a b 18 18 import random 19 19 from gettext import gettext as _ 20 20 21 # Import PyGTK. 22 import gobject, pygtk, gtk, pango 21 from gi.repository import Gtk 22 from gi.repository import Gdk 23 from gi.repository import GObject 24 from gi.repository import Pango 25 from gi.repository import PangoCairo 26 from gi.repository import GdkPixbuf 23 27 24 class TitleScene(gtk.DrawingArea): 28 29 class TitleScene(Gtk.DrawingArea): 25 30 # Maximum portion of the screen the background can fill vertically. 26 31 BACKGROUND_HEIGHT_RATIO = 0.6 27 32 28 33 # Border from top right of screen to draw title at. 29 TITLE_OFFSET = (20, 30)34 TITLE_OFFSET = (20, 50) 30 35 31 36 # Font used to display the title. 32 37 TITLE_FONT = 'Times 45' 33 38 34 39 def __init__(self): 35 gtk.DrawingArea.__init__(self)40 Gtk.DrawingArea.__init__(self) 36 41 37 pbuf = gtk.gdk.pixbuf_new_from_file('images/main-background.jpg')42 pbuf = GdkPixbuf.Pixbuf.new_from_file('images/main-background.jpg') 38 43 39 width_ratio = float( gtk.gdk.screen_width()) / pbuf.get_width()40 height_ratio = float( gtk.gdk.screen_height()*TitleScene.BACKGROUND_HEIGHT_RATIO) / pbuf.get_height()44 width_ratio = float(Gdk.Screen.width()) / pbuf.get_width() 45 height_ratio = float(Gdk.Screen.height()*TitleScene.BACKGROUND_HEIGHT_RATIO) / pbuf.get_height() 41 46 42 47 ratio = min(width_ratio, height_ratio) 43 self.backgroundpixbuf = pbuf.scale_simple(int(pbuf.get_width()*ratio), int(pbuf.get_height()*ratio), gtk.gdk.INTERP_BILINEAR)48 self.backgroundpixbuf = pbuf.scale_simple(int(pbuf.get_width()*ratio), int(pbuf.get_height()*ratio), GdkPixbuf.InterpType.BILINEAR) 44 49 45 50 self.set_size_request(self.backgroundpixbuf.get_width(), self.backgroundpixbuf.get_height()) 46 51 47 self.connect(" expose-event", self.expose_cb)52 self.connect("draw", self.draw_cb) 48 53 49 54 self.title_original = _('Typing Turtle') 50 55 self.title_src = self.title_original 51 56 self.title_text = '' 52 57 53 def expose_cb(self, area, event):58 def draw_cb(self, area, cr): 54 59 bounds = self.get_allocation() 55 56 gc = self.get_style().fg_gc[gtk.STATE_NORMAL]57 60 58 61 # Background picture. 59 62 x = (bounds.width - self.backgroundpixbuf.get_width())/2 60 self.window.draw_pixbuf( 61 gc, self.backgroundpixbuf, 0, 0, 62 x, 0, self.backgroundpixbuf.get_width(), self.backgroundpixbuf.get_height()) 63 pc = self.create_pango_context() 64 65 self.layout = self.create_pango_layout('') 66 self.layout.set_font_description(pango.FontDescription(TitleScene.TITLE_FONT)) 67 68 self.layout.set_text(self.title_original) 69 original_size = self.layout.get_size() 70 self.x_text = (bounds.width-original_size[0]/pango.SCALE)-TitleScene.TITLE_OFFSET[0] 63 64 Gdk.cairo_set_source_pixbuf(cr, self.backgroundpixbuf, 0, 0) 65 cr.rectangle(x, 0, self.backgroundpixbuf.get_width(), 66 self.backgroundpixbuf.get_height()) 67 cr.paint() 68 69 cr.set_source_rgb(0, 0, 0) 70 self.pango_layout = PangoCairo.create_layout(cr) 71 self.pango_layout.set_font_description( 72 Pango.FontDescription(TitleScene.TITLE_FONT)) 73 self.pango_layout.set_text(unicode(self.title_original), 74 len(self.title_original)) 75 76 original_size = self.pango_layout.get_size() 77 self.x_text = (bounds.width - original_size[0] / Pango.SCALE) - \ 78 TitleScene.TITLE_OFFSET[0] 71 79 self.y_text = TitleScene.TITLE_OFFSET[1] 72 gobject.timeout_add(50, self.timer_cb) 80 81 GObject.timeout_add(50, self.timer_cb) 73 82 74 83 def draw_text(self): 75 84 # Animated Typing Turtle title. 76 gc = self.get_style().fg_gc[gtk.STATE_NORMAL] 77 self.layout.set_text(self.title_text) 78 self.window.draw_layout(gc, self.x_text, self.y_text, self.layout) 85 window = self.get_window() 86 if window is None: 87 return 88 cr = window.cairo_create() 89 90 cr.move_to(self.x_text, self.y_text) 91 self.pango_layout.set_text(unicode(self.title_text), 92 len(self.title_text)) 93 PangoCairo.update_layout(cr, self.pango_layout) 94 PangoCairo.show_layout(cr, self.pango_layout) 79 95 80 96 def timer_cb(self): 81 97 if len(self.title_src) > 0: -
typingturtle.py
diff --git a/typingturtle.py b/typingturtle.py index ad6c6cb..b6ad2de 100755
a b from port import json 25 25 #dbgp.client.brkOnExcept(host='192.168.1.104', port=12900) 26 26 27 27 # there is need to set up localization. 28 # since sugar .activity.main already seted up gettext envronment28 # since sugar3.activity.main already seted up gettext envronment 29 29 #locale.setlocale(locale.LC_ALL, '') 30 30 31 # Import PyGTK. 32 import gobject, pygtk, gtk, pango 31 32 from gi.repository import Gtk 33 33 34 34 # Import Sugar UI modules. 35 import sugar.activity.activity 36 from sugar.graphics import * 37 from sugar.graphics import toolbutton 38 from sugar import profile 39 40 OLD_TOOLBAR = False 41 try: 42 from sugar.graphics.toolbarbox import ToolbarBox 43 from sugar.activity.widgets import StopButton 44 from sugar.activity.widgets import ActivityToolbarButton 45 except ImportError: 46 OLD_TOOLBAR = True 35 import sugar3.activity.activity 36 from sugar3.graphics import * 37 from sugar3.graphics import toolbutton 38 from sugar3 import profile 39 40 from sugar3.graphics.toolbarbox import ToolbarBox 41 from sugar3.activity.widgets import StopButton 42 from sugar3.activity.widgets import ActivityToolbarButton 47 43 48 44 # Initialize logging. 49 45 log = logging.getLogger('Typing Turtle') … … log.setLevel(logging.DEBUG) 51 47 logging.basicConfig() 52 48 53 49 # Change to bundle directory. 54 bundle_path = sugar .activity.activity.get_bundle_path()50 bundle_path = sugar3.activity.activity.get_bundle_path() 55 51 os.chdir(bundle_path) 56 52 57 53 # Import activity modules. 58 54 import mainscreen, editlessonlistscreen 59 55 56 from gi.repository import PangoCairo 57 58 59 def _get_screen_dpi(): 60 xft_dpi = Gtk.Settings.get_default().get_property('gtk-xft-dpi') 61 dpi = float(xft_dpi / 1024) 62 logging.debug('Setting dpi to: %f', dpi) 63 # HACK: if the DPI detected is 200.0 it seems we are on an XO, so 64 # we return 133 because the XO manage its resolution in a complex 65 # way. More information here: 66 # http://wiki.laptop.org/go/Display 67 if 200 == int(dpi): 68 return 133 69 return dpi 70 71 dpi = _get_screen_dpi() 72 font_map_default = PangoCairo.font_map_get_default() 73 font_map_default.set_resolution(dpi) 74 75 from gi.repository import Rsvg 76 Rsvg.set_default_dpi(200) 77 # This is the main Typing Turtle activity class. 78 # 79 # It owns the main application window, and all the various toolbars and options. 80 81 82 60 83 # This is the main Typing Turtle activity class. 61 84 # 62 85 # It owns the main application window, and all the various toolbars and options. 63 86 # Activity Screens are stored in a stack, with the currently active screen on top. 64 class TypingTurtle(sugar .activity.activity.Activity):87 class TypingTurtle(sugar3.activity.activity.Activity): 65 88 def __init__ (self, handle): 66 sugar .activity.activity.Activity.__init__(self, handle)89 sugar3.activity.activity.Activity.__init__(self, handle) 67 90 self.set_title(_("Typing Turtle")) 68 91 self.max_participants = 1 69 92 70 93 self.build_toolbox() 71 94 72 95 self.screens = [] 73 self.screenbox = gtk.VBox()96 self.screenbox = Gtk.VBox() 74 97 75 98 self.nick = profile.get_nick_name() 76 99 … … class TypingTurtle(sugar.activity.activity.Activity): 92 115 93 116 self.show_all() 94 117 95 self.editorbtn = sugar .graphics.toolbutton.ToolButton('view-source')118 self.editorbtn = sugar3.graphics.toolbutton.ToolButton('view-source') 96 119 self.editorbtn.set_tooltip(_("Edit Lessons")) 97 120 self.editorbtn.connect('clicked', self.editor_clicked_cb) 98 121 99 if OLD_TOOLBAR: 100 # Hide the sharing button from the activity toolbar since 101 # we don't support sharing. 102 activity_toolbar = self.tbox.get_activity_toolbar() 103 activity_toolbar.share.props.visible = False 104 105 share_idx = activity_toolbar.get_item_index(activity_toolbar.share) 106 activity_toolbar.insert(self.editorbtn, share_idx) 107 else: 108 activity_toolbar = self.toolbar_box.toolbar 109 activity_toolbar.insert(self.editorbtn, 1) 122 activity_toolbar = self.toolbar_box.toolbar 123 activity_toolbar.insert(self.editorbtn, 1) 110 124 111 125 self.editorbtn.show_all() 112 126 113 127 def build_toolbox(self): 114 if OLD_TOOLBAR: 115 self.tbox = sugar.activity.activity.ActivityToolbox(self) 116 self.tbox.show_all() 117 self.set_toolbox(self.tbox) 118 else: 119 self.toolbar_box = ToolbarBox() 128 self.toolbar_box = ToolbarBox() 120 129 121 122 123 130 activity_button = ActivityToolbarButton(self) 131 self.toolbar_box.toolbar.insert(activity_button, 0) 132 activity_button.show() 124 133 125 separator = gtk.SeparatorToolItem()126 127 128 134 separator = Gtk.SeparatorToolItem() 135 separator.props.draw = False 136 separator.set_expand(True) 137 self.toolbar_box.toolbar.insert(separator, -1) 129 138 130 139 self.toolbar_box.toolbar.insert(StopButton(self), -1) 131 140 132 133 141 self.set_toolbar_box(self.toolbar_box) 142 self.toolbar_box.show_all() 134 143 135 144 def editor_clicked_cb(self, btn): 136 145 self.push_screen(editlessonlistscreen.EditLessonListScreen(self, self.mainscreen.lessons)) … … class TypingTurtle(sugar.activity.activity.Activity): 146 155 147 156 self.screenbox.remove(oldscreen) 148 157 149 self.screenbox.pack_start(screen, True, True )158 self.screenbox.pack_start(screen, True, True, 0) 150 159 self.screens.append(screen) 151 160 152 161 try: … … class TypingTurtle(sugar.activity.activity.Activity): 172 181 except: 173 182 pass 174 183 175 self.screenbox.pack_start(screen )184 self.screenbox.pack_start(screen, True, True, 0) 176 185 177 186 def add_history(self, entry): 178 187 self.data['history'].append(entry)