-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufwin.py
59 lines (46 loc) · 2 KB
/
bufwin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# bufwin.py
"""Buffered window for drawing on."""
import wx
class BufferedWindow(wx.Window):
"""Handle all the double-buffering mechanics.
Subclass this and define a render() method that takes a gc to draw to, and
performs the actual drawing. The window will automatically be double
buffered, and the screen will be automatically updated when a Paint event
is received.
When the drawing needs to change, the user application needs to call the
update_view() method, which will cause render(gc) to be called on the next
idle event.
"""
def __init__(self, *args, **kwargs):
super().__init__( *args, **kwargs)
# Setup event handlers
self.Bind(wx.EVT_PAINT, self.on_paint)
self.Bind(wx.EVT_IDLE, self.on_idle)
self.Bind(wx.EVT_SIZE, self.on_size)
# Initialize the bitmap with the right size. Note that ClientSize has
# type 'wx.Size', not tuple, this breaks pillow code so we fix it.
self.size = self.ClientSize.width, self.ClientSize.height
self.bitmap = wx.Bitmap(self.size)
self.redraw_needed = False
self.layering = False
def on_paint(self, _):
"""Copy the bitmap to the screen"""
dc = wx.BufferedPaintDC(self, self.bitmap)
def on_size(self, _):
"""Mark redraw as needed because the size has changed."""
# Convert wx.Size to tuple
sz = self.ClientSize.width, self.ClientSize.height
if sz != self.size:
self.size = sz
self.update_view()
def on_idle(self, _):
"""Rebuild the bitmap, update drawing, if needed."""
if self.redraw_needed:
# render_bitmap is implemented in the derived classes
self.bitmap = self.render_bitmap(self.layering)
self.redraw_needed = False
self.Refresh()
def update_view(self, layering=False):
"""Outside world's interface to request a redraw."""
self.redraw_needed = True
self.layering = layering