]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/doodle/doodle.py
oops, forgot to commit wxFontBase::SetNativeFontInfo implementation
[wxWidgets.git] / wxPython / samples / doodle / doodle.py
1 # doodle.py
2
3 """
4 This module contains the DoodleWindow class which is a window that you
5 can do simple drawings upon.
6 """
7
8
9 from wxPython.wx import *
10
11 #----------------------------------------------------------------------
12
13 class DoodleWindow(wxWindow):
14 menuColours = { 100 : 'Black',
15 101 : 'Yellow',
16 102 : 'Red',
17 103 : 'Green',
18 104 : 'Blue',
19 105 : 'Purple',
20 106 : 'Brown',
21 107 : 'Aquamarine',
22 108 : 'Forest Green',
23 109 : 'Light Blue',
24 110 : 'Goldenrod',
25 111 : 'Cyan',
26 112 : 'Orange',
27 113 : 'Navy',
28 114 : 'Dark Grey',
29 115 : 'Light Grey',
30 }
31 maxThickness = 16
32
33
34 def __init__(self, parent, ID):
35 wxWindow.__init__(self, parent, ID)
36 self.SetBackgroundColour(wxWHITE)
37 self.listeners = []
38 self.thickness = 1
39 self.SetColour("Black")
40 self.lines = []
41 self.x = self.y = 0
42 self.MakeMenu()
43
44 # hook some mouse events
45 EVT_LEFT_DOWN(self, self.OnLeftDown)
46 EVT_LEFT_UP(self, self.OnLeftUp)
47 EVT_RIGHT_UP(self, self.OnRightUp)
48 EVT_MOTION(self, self.OnMotion)
49
50 # and the refresh event
51 EVT_PAINT(self, self.OnPaint)
52
53
54 def __del__(self):
55 self.menu.Destroy()
56
57
58 def SetColour(self, colour):
59 """Set a new colour and make a matching pen"""
60 self.colour = colour
61 self.pen = wxPen(wxNamedColour(self.colour), self.thickness, wxSOLID)
62 self.Notify()
63
64
65 def SetThickness(self, num):
66 """Set a new line thickness and make a matching pen"""
67 self.thickness = num
68 self.pen = wxPen(wxNamedColour(self.colour), self.thickness, wxSOLID)
69 self.Notify()
70
71
72 def GetLinesData(self):
73 return self.lines[:]
74
75
76 def SetLinesData(self, lines):
77 self.lines = lines[:]
78 self.Refresh()
79
80
81 def MakeMenu(self):
82 """Make a menu that can be popped up later"""
83 menu = wxMenu()
84 keys = self.menuColours.keys()
85 keys.sort()
86 for k in keys:
87 text = self.menuColours[k]
88 menu.Append(k, text, checkable=true)
89 EVT_MENU_RANGE(self, 100, 200, self.OnMenuSetColour)
90 EVT_UPDATE_UI_RANGE(self, 100, 200, self.OnCheckMenuColours)
91 menu.Break()
92
93 for x in range(1, self.maxThickness+1):
94 menu.Append(x, str(x), checkable=true)
95 EVT_MENU_RANGE(self, 1, self.maxThickness, self.OnMenuSetThickness)
96 EVT_UPDATE_UI_RANGE(self, 1, self.maxThickness, self.OnCheckMenuThickness)
97 self.menu = menu
98
99
100 # These two event handlers are called before the menu is displayed
101 # to determine which items should be checked.
102 def OnCheckMenuColours(self, event):
103 text = self.menuColours[event.GetId()]
104 if text == self.colour:
105 event.Check(true)
106 else:
107 event.Check(false)
108 def OnCheckMenuThickness(self, event):
109 if event.GetId() == self.thickness:
110 event.Check(true)
111 else:
112 event.Check(false)
113
114
115 def OnLeftDown(self, event):
116 """called when the left mouse button is pressed"""
117 self.curLine = []
118 self.x, self.y = event.GetPositionTuple()
119 self.CaptureMouse()
120
121
122 def OnLeftUp(self, event):
123 """called when the left mouse button is released"""
124 self.lines.append( (self.colour, self.thickness, self.curLine) )
125 self.curLine = []
126 self.ReleaseMouse()
127
128
129 def OnRightUp(self, event):
130 """called when the right mouse button is released, will popup the menu"""
131 pt = event.GetPosition()
132 self.PopupMenu(self.menu, pt)
133
134
135
136 def OnMotion(self, event):
137 """
138 Called when the mouse is in motion. If the left button is
139 dragging then draw a line from the last event position to the
140 current one. Save the coordinants for redraws.
141 """
142 if event.Dragging() and event.LeftIsDown():
143 dc = wxClientDC(self)
144 dc.BeginDrawing()
145 dc.SetPen(self.pen)
146 pos = event.GetPositionTuple()
147 coords = (self.x, self.y) + pos
148 self.curLine.append(coords)
149 dc.DrawLine(self.x, self.y, pos[0], pos[1])
150 self.x, self.y = pos
151 dc.EndDrawing()
152
153
154 def OnPaint(self, event):
155 """
156 Called when the window is exposed. Redraws all the lines that have
157 been drawn already.
158 """
159 dc = wxPaintDC(self)
160 dc.BeginDrawing()
161 for colour, thickness, line in self.lines:
162 pen = wxPen(wxNamedColour(colour), thickness, wxSOLID)
163 dc.SetPen(pen)
164 for coords in line:
165 apply(dc.DrawLine, coords)
166 dc.EndDrawing()
167
168
169 # Event handlers for the popup menu, uses the event ID to determine
170 # the colour or the thickness to set.
171 def OnMenuSetColour(self, event):
172 self.SetColour(self.menuColours[event.GetId()])
173
174 def OnMenuSetThickness(self, event):
175 self.SetThickness(event.GetId())
176
177
178 # Observer pattern. Listeners are registered and then notified
179 # whenever doodle settings change.
180 def AddListener(self, listener):
181 self.listeners.append(listener)
182
183 def Notify(self):
184 for other in self.listeners:
185 other.Update(self.colour, self.thickness)
186
187
188 #----------------------------------------------------------------------
189
190 class DoodleFrame(wxFrame):
191 def __init__(self, parent):
192 wxFrame.__init__(self, parent, -1, "Doodle Frame", size=(800,600))
193 self.doodle = DoodleWindow(self, -1)
194
195
196 #----------------------------------------------------------------------
197
198 if __name__ == '__main__':
199 app = wxPySimpleApp()
200 frame = DoodleFrame(None)
201 frame.Show(true)
202 app.MainLoop()
203