Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
import os
2
import gtk
3
import gobject
4
import pygame
5
import event
6
7
CANVAS = None
8
9
class PygameCanvas(gtk.EventBox):
10
    def __init__(self, mainwindow):
11
        gtk.EventBox.__init__(self)
12
13
        global CANVAS
14
        assert CANVAS == None, "Only one PygameCanvas can be created, ever."
15
        CANVAS = self
16
17
        self._mainwindow = mainwindow
18
19
        self.set_flags(gtk.CAN_FOCUS)
20
        
21
        self._socket = gtk.Socket()
22
        self.add(self._socket)
23
        self.show_all()
24
        
25
    def run_pygame(self, main_fn):
26
        assert not pygame.display.get_init(), "PygameCanvas.run_pygame can only be called once."
27
        
28
        # Run the main loop after a short delay.  The reason for the delay is that the
29
        # Sugar activity is not properly created until after its constructor returns.
30
        # If the Pygame main loop is called from the activity constructor, the 
31
        # constructor never returns and the activity freezes.
32
        gobject.idle_add(self._run_pygame_cb, main_fn)
33
34
    def _run_pygame_cb(self, main_fn):
35
        # Preinitialize Pygame with the X window ID.
36
        os.environ['SDL_WINDOWID'] = str(self._socket.get_id())
37
        pygame.init()
38
39
        # Restore the default cursor.
40
        self._socket.get_window().set_cursor(None)
41
42
        # Initialize the Pygame window.
43
        pygame.display.set_mode((0, 0), pygame.RESIZABLE)
44
45
        # Hook certain Pygame functions with GTK equivalents.
46
        translator = event.Translator(self._mainwindow, self)
47
        translator.hook_pygame()
48
49
        # Run the Pygame main loop.
50
        main_fn()
51
        return False
52
53
    def get_pygame_widget(self):
54
        return self._socket