|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +A test of drawing while the mouse is moving. |
| 5 | +
|
| 6 | +The old way of doing this is with a wxClientDC, but that does |
| 7 | +not work right, or at least not well on OS-X. So this shows how |
| 8 | +to do it with Refresh();Update(). |
| 9 | +""" |
| 10 | + |
| 11 | +import wx |
| 12 | + |
| 13 | +print "running with version:", wx.__version__ |
| 14 | +import random |
| 15 | + |
| 16 | + |
| 17 | +class DrawWindow(wx.Window): |
| 18 | + def __init__(self, parent, id = -1): |
| 19 | + ## Any data the Draw() function needs must be initialized before |
| 20 | + ## calling BufferedWindow.__init__, as it will call the Draw |
| 21 | + ## function. |
| 22 | + wx.Window.__init__(self, parent, id) |
| 23 | + |
| 24 | + self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) |
| 25 | + self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) |
| 26 | + self.Bind(wx.EVT_MOTION, self.OnMove) |
| 27 | + |
| 28 | + wx.EVT_PAINT(self, self.OnPaint) |
| 29 | + wx.EVT_SIZE(self, self.OnSize) |
| 30 | + |
| 31 | + # OnSize called to make sure the buffer is initialized. |
| 32 | + # This might result in OnSize getting called twice on some |
| 33 | + # platforms at initialization, but little harm done. |
| 34 | + |
| 35 | + self.mouse_line = None |
| 36 | + self.OnSize(None) |
| 37 | + |
| 38 | + def OnPaint(self, event): |
| 39 | + # This draws the buffer to the screen, then optionally the mouse_line |
| 40 | + dc = wx.PaintDC(self) |
| 41 | + dc.DrawBitmap(self._Buffer,0,0) |
| 42 | + if self.mouse_line is not None: |
| 43 | + dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH)) |
| 44 | + dc.DrawLinePoint( *self.mouse_line ) |
| 45 | + |
| 46 | + def OnSize(self,event): |
| 47 | + # The Buffer init is done here, to make sure the buffer is always |
| 48 | + # the same size as the Window |
| 49 | + Size = self.GetClientSizeTuple() |
| 50 | + |
| 51 | + # Make sure we don't try to create a 0 size bitmap |
| 52 | + Size = (max(Size[0], 1), max(Size[1], 1)) |
| 53 | + self._Buffer = wx.EmptyBitmap(Size[0],Size[1]) |
| 54 | + self.Draw() |
| 55 | + |
| 56 | + def Draw(self): |
| 57 | + """ |
| 58 | + This draws the backgound image on the buffer |
| 59 | +
|
| 60 | + """ |
| 61 | + # update the buffer |
| 62 | + dc = wx.MemoryDC() |
| 63 | + dc.SelectObject(self._Buffer) |
| 64 | + |
| 65 | + coords = ((40,40),(200,220),(210,120),(120,300)) |
| 66 | + dc.BeginDrawing() |
| 67 | + dc.SetBackground( wx.Brush("Blue") ) |
| 68 | + dc.Clear() # make sure you clear the bitmap! |
| 69 | + |
| 70 | + dc.SetPen(wx.RED_PEN) |
| 71 | + dc.SetBrush(wx.CYAN_BRUSH) |
| 72 | + |
| 73 | + dc.DrawPolygon(coords) |
| 74 | + |
| 75 | + |
| 76 | + def OnLeftDown(self, event): |
| 77 | + self.CaptureMouse() |
| 78 | + self.mouse_line = [ event.GetPosition(), None] |
| 79 | + |
| 80 | + def OnLeftUp(self, event): |
| 81 | + self.mouse_line = None |
| 82 | + if self.HasCapture(): |
| 83 | + self.ReleaseMouse() |
| 84 | + self.Refresh() |
| 85 | + self.Update() |
| 86 | + |
| 87 | + def OnMove(self, event): |
| 88 | + if event.Dragging() and event.LeftIsDown() and (self.mouse_line is not None): |
| 89 | + self.mouse_line[1] = event.GetPosition() |
| 90 | + self.Refresh() |
| 91 | + ## note: "Update() is not recommended on wxMac -- but response is slower without it... " |
| 92 | + #self.Update() |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + app = wx.App(False) |
| 97 | + frame = wx.Frame(None, size = (400,500), title="Mouse Move Drawing Test") |
| 98 | + draw_window = DrawWindow(frame) |
| 99 | + frame.Show(True) |
| 100 | + app.MainLoop() |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
0 commit comments