]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-06/example1.py
don't use strlen() to verify the length of the string as it can contain embedded...
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-06 / example1.py
1 import wx
2
3
4 class SketchWindow(wx.Window):
5 def __init__(self, parent, ID):
6 wx.Window.__init__(self, parent, ID)
7 self.SetBackgroundColour("White")
8 self.color = "Black"
9 self.thickness = 1
10 self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
11 self.lines = []
12 self.curLine = []
13 self.pos = (0, 0)
14 self.InitBuffer()
15
16 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
17 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
18 self.Bind(wx.EVT_MOTION, self.OnMotion)
19 self.Bind(wx.EVT_SIZE, self.OnSize)
20 self.Bind(wx.EVT_IDLE, self.OnIdle)
21 self.Bind(wx.EVT_PAINT, self.OnPaint)
22
23 def InitBuffer(self):
24 size = self.GetClientSize()
25 #self.buffer = wx.EmptyBitmap(max(1, size.width), max(1, size.height))
26 self.buffer = wx.EmptyBitmap(size.width, size.height)
27 if self.buffer.Ok():
28 dc = wx.BufferedDC(None, self.buffer)
29 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
30 dc.Clear()
31 self.DrawLines(dc)
32 self.reInitBuffer = False
33
34 def GetLinesData(self):
35 return self.lines[:]
36
37 def SetLinesData(self, lines):
38 self.lines = lines[:]
39 self.InitBuffer()
40 self.Refresh()
41
42 def OnLeftDown(self, event):
43 self.curLine = []
44 self.pos = event.GetPositionTuple()
45 self.CaptureMouse()
46
47 def OnLeftUp(self, event):
48 if self.HasCapture():
49 self.lines.append((self.color,
50 self.thickness,
51 self.curLine))
52 self.curLine = []
53 self.ReleaseMouse()
54
55 def OnMotion(self, event):
56 if event.Dragging() and event.LeftIsDown():
57 dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
58 self.drawMotion(dc, event)
59 event.Skip()
60
61 def drawMotion(self, dc, event):
62 dc.SetPen(self.pen)
63 newPos = event.GetPositionTuple()
64 coords = self.pos + newPos
65 self.curLine.append(coords)
66 dc.DrawLine(*coords)
67 self.pos = newPos
68
69 def OnSize(self, event):
70 self.reInitBuffer = True
71
72 def OnIdle(self, event):
73 if self.reInitBuffer:
74 self.InitBuffer()
75 self.Refresh(False)
76
77 def OnPaint(self, event):
78 dc = wx.BufferedPaintDC(self, self.buffer)
79
80 def DrawLines(self, dc):
81 for colour, thickness, line in self.lines:
82 pen = wx.Pen(colour, thickness, wx.SOLID)
83 dc.SetPen(pen)
84 for coords in line:
85 dc.DrawLine(*coords)
86
87 def SetColor(self, color):
88 self.color = color
89 self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
90
91 def SetThickness(self, num):
92 self.thickness = num
93 self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
94
95
96 class SketchFrame(wx.Frame):
97 def __init__(self, parent):
98 wx.Frame.__init__(self, parent, -1, "Sketch Frame",
99 size=(800,600))
100 self.sketch = SketchWindow(self, -1)
101
102 if __name__ == '__main__':
103 app = wx.PySimpleApp()
104 frame = SketchFrame(None)
105 frame.Show(True)
106 app.MainLoop()