]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/doodle/superdoodle.py
compilation sample in !WXWIN_COMPATIBILITY_2_2 mode
[wxWidgets.git] / wxPython / samples / doodle / superdoodle.py
1 # superdoodle.py
2
3 """
4 This module implements the SuperDoodle demo application. It takes the
5 DoodleWindow previously presented and reuses it in a much more
6 intelligent Frame. This one has a menu and a statusbar, is able to
7 save and reload doodles, clear the workspace, and has a simple control
8 panel for setting color and line thickness in addition to the popup
9 menu that DoodleWindow provides. There is also a nice About dialog
10 implmented using an wxHtmlWindow.
11 """
12
13 from wxPython.wx import *
14 from doodle import DoodleWindow
15
16 import os, cPickle
17
18
19 #----------------------------------------------------------------------
20
21 idNEW = 11001
22 idOPEN = 11002
23 idSAVE = 11003
24 idSAVEAS = 11004
25 idCLEAR = 11005
26 idEXIT = 11006
27 idABOUT = 11007
28
29
30 class DoodleFrame(wxFrame):
31 """
32 A DoodleFrame contains a DoodleWindow and a ControlPanel and manages
33 their layout with a wxBoxSizer. A menu and associated event handlers
34 provides for saving a doodle to a file, etc.
35 """
36 title = "Do a doodle"
37 def __init__(self, parent):
38 wxFrame.__init__(self, parent, -1, self.title, size=(800,600),
39 style=wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
40 self.CreateStatusBar()
41 self.MakeMenu()
42 self.filename = None
43
44 self.doodle = DoodleWindow(self, -1)
45 cPanel = ControlPanel(self, -1, self.doodle)
46
47 # Create a sizer to layout the two windows side-by-side.
48 # Both will grow vertically, the doodle window will grow
49 # horizontally as well.
50 box = wxBoxSizer(wxHORIZONTAL)
51 box.Add(cPanel, 0, wxEXPAND)
52 box.Add(self.doodle, 1, wxEXPAND)
53
54 # Tell the frame that it should layout itself in response to
55 # size events.
56 self.SetAutoLayout(true)
57 self.SetSizer(box)
58
59
60 def SaveFile(self):
61 if self.filename:
62 data = self.doodle.GetLinesData()
63 f = open(self.filename, 'w')
64 cPickle.dump(data, f)
65 f.close()
66
67
68 def ReadFile(self):
69 if self.filename:
70 try:
71 f = open(self.filename, 'r')
72 data = cPickle.load(f)
73 f.close()
74 self.doodle.SetLinesData(data)
75 except cPickle.UnpicklingError:
76 wxMessageBox("%s is not a doodle file." % self.filename,
77 "oops!", style=wxOK|wxICON_EXCLAMATION)
78
79
80 def MakeMenu(self):
81 # create the file menu
82 menu1 = wxMenu()
83 menu1.Append(idOPEN, "&Open", "Open a doodle file")
84 menu1.Append(idSAVE, "&Save", "Save the doodle")
85 menu1.Append(idSAVEAS, "Save &As", "Save the doodle in a new file")
86 menu1.AppendSeparator()
87 menu1.Append(idCLEAR, "&Clear", "Clear the current doodle")
88 menu1.AppendSeparator()
89 menu1.Append(idEXIT, "E&xit", "Terminate the application")
90
91 # and the help menu
92 menu2 = wxMenu()
93 menu2.Append(idABOUT, "&About", "Display the gratuitous 'about this app' thingamajig")
94
95 # and add them to a menubar
96 menuBar = wxMenuBar()
97 menuBar.Append(menu1, "&File")
98 menuBar.Append(menu2, "&Help")
99 self.SetMenuBar(menuBar)
100
101 EVT_MENU(self, idOPEN, self.OnMenuOpen)
102 EVT_MENU(self, idSAVE, self.OnMenuSave)
103 EVT_MENU(self, idSAVEAS, self.OnMenuSaveAs)
104 EVT_MENU(self, idCLEAR, self.OnMenuClear)
105 EVT_MENU(self, idEXIT, self.OnMenuExit)
106 EVT_MENU(self, idABOUT, self.OnMenuAbout)
107
108
109
110 wildcard = "Doodle files (*.ddl)|*.ddl|All files (*.*)|*.*"
111
112 def OnMenuOpen(self, event):
113 dlg = wxFileDialog(self, "Open doodle file...", os.getcwd(),
114 style=wxOPEN, wildcard = self.wildcard)
115 if dlg.ShowModal() == wxID_OK:
116 self.filename = dlg.GetPath()
117 self.ReadFile()
118 self.SetTitle(self.title + ' -- ' + self.filename)
119 dlg.Destroy()
120
121
122 def OnMenuSave(self, event):
123 if not self.filename:
124 self.OnMenuSaveAs(event)
125 else:
126 self.SaveFile()
127
128
129 def OnMenuSaveAs(self, event):
130 dlg = wxFileDialog(self, "Save doodle as...", os.getcwd(),
131 style=wxSAVE | wxOVERWRITE_PROMPT,
132 wildcard = self.wildcard)
133 if dlg.ShowModal() == wxID_OK:
134 filename = dlg.GetPath()
135 if not os.path.splitext(filename)[1]:
136 filename = filename + '.ddl'
137 self.filename = filename
138 self.SaveFile()
139 self.SetTitle(self.title + ' -- ' + self.filename)
140 dlg.Destroy()
141
142
143 def OnMenuClear(self, event):
144 self.doodle.SetLinesData([])
145 self.SetTitle(self.title)
146
147
148 def OnMenuExit(self, event):
149 self.Close()
150
151
152 def OnMenuAbout(self, event):
153 dlg = DoodleAbout(self)
154 dlg.ShowModal()
155 dlg.Destroy()
156
157
158
159 #----------------------------------------------------------------------
160
161
162 class ControlPanel(wxPanel):
163 """
164 This class implements a very simple control panel for the DoodleWindow.
165 It creates buttons for each of the colours and thickneses supported by
166 the DoodleWindow, and event handlers to set the selected values. There is
167 also a little window that shows an example doodleLine in the selected
168 values. Nested sizers are used for layout.
169 """
170 def __init__(self, parent, ID, doodle):
171 wxPanel.__init__(self, parent, ID, style=wxRAISED_BORDER)
172
173 numCols = 4
174 spacing = 4
175
176 # Make a grid of buttons for each colour. Attach each button
177 # event to self.OnSetColour. The button ID is the same as the
178 # key in the colour dictionary.
179 colours = doodle.menuColours
180 keys = colours.keys()
181 keys.sort()
182 cGrid = wxGridSizer(cols=numCols, hgap=2, vgap=2)
183 for k in keys:
184 bmp = self.MakeBitmap(wxNamedColour(colours[k]))
185 b = wxBitmapButton(self, k, bmp)
186 EVT_BUTTON(self, k, self.OnSetColour)
187 cGrid.Add(b, 0)
188
189 # Save the button size so we can use it for the number buttons
190 btnSize = b.GetSize()
191
192 # Make a grid of buttons for the thicknesses. Attach each button
193 # event to self.OnSetThickness. The button ID is the same as the
194 # thickness value.
195 tGrid = wxGridSizer(cols=numCols, hgap=2, vgap=2)
196 for x in range(1, doodle.maxThickness+1):
197 b = wxButton(self, x, str(x), size=btnSize)
198 EVT_BUTTON(self, x, self.OnSetThickness)
199 tGrid.Add(b, 0)
200
201 # Make a colour indicator window, it is registerd as a listener
202 # with the doodle window so it will be notified when the settings
203 # change
204 ci = ColourIndicator(self)
205 doodle.AddListener(ci)
206 doodle.Notify()
207 self.doodle = doodle
208
209 # Make a box sizer and put the two grids and the indicator
210 # window in it.
211 box = wxBoxSizer(wxVERTICAL)
212 box.Add(cGrid, 0, wxALL, spacing)
213 box.Add(tGrid, 0, wxALL, spacing)
214 box.Add(ci, 0, wxEXPAND|wxALL, spacing)
215 self.SetSizer(box)
216 self.SetAutoLayout(true)
217
218 # Resize this window so it is just large enough for the
219 # minimum requirements of the sizer.
220 box.Fit(self)
221
222
223
224 def MakeBitmap(self, colour):
225 """
226 We can create a bitmap of whatever we want by simply selecting
227 it into a wxMemoryDC and drawing on it. In this case we just set
228 a background brush and clear the dc.
229 """
230 bmp = wxEmptyBitmap(16,16)
231 dc = wxMemoryDC()
232 dc.SelectObject(bmp)
233 dc.SetBackground(wxBrush(colour))
234 dc.Clear()
235 dc.SelectObject(wxNullBitmap)
236 return bmp
237
238
239 def OnSetColour(self, event):
240 """
241 Use the event ID to get the colour, set that colour in the doodle.
242 """
243 colour = self.doodle.menuColours[event.GetId()]
244 self.doodle.SetColour(colour)
245
246
247 def OnSetThickness(self, event):
248 """
249 Use the event ID to set the thickness in the doodle.
250 """
251 self.doodle.SetThickness(event.GetId())
252
253
254 #----------------------------------------------------------------------
255
256 class ColourIndicator(wxWindow):
257 """
258 An instance of this class is used on the ControlPanel to show
259 a sample of what the current doodle line will look like.
260 """
261 def __init__(self, parent):
262 wxWindow.__init__(self, parent, -1, style=wxSUNKEN_BORDER)
263 self.SetBackgroundColour(wxWHITE)
264 self.SetSize(wxSize(-1, 40))
265 self.colour = self.thickness = None
266 EVT_PAINT(self, self.OnPaint)
267
268
269 def Update(self, colour, thickness):
270 """
271 The doodle window calls this method any time the colour
272 or line thickness changes.
273 """
274 self.colour = colour
275 self.thickness = thickness
276 self.Refresh() # generate a paint event
277
278
279 def OnPaint(self, event):
280 """
281 This method is called when all or part of the window needs to be
282 redrawn.
283 """
284 dc = wxPaintDC(self)
285 if self.colour:
286 sz = self.GetClientSize()
287 pen = wxPen(wxNamedColour(self.colour), self.thickness)
288 dc.BeginDrawing()
289 dc.SetPen(pen)
290 dc.DrawLine(10, sz.height/2, sz.width-10, sz.height/2)
291 dc.EndDrawing()
292
293
294 #----------------------------------------------------------------------
295
296 class DoodleAbout(wxDialog):
297 """ An about box that uses an HTML window """
298
299 text = '''
300 <html>
301 <body bgcolor="#ACAA60">
302 <center><table bgcolor="#455481" width="100%" cellspacing="0"
303 cellpadding="0" border="1">
304 <tr>
305 <td align="center"><h1>SuperDoodle</h1></td>
306 </tr>
307 </table>
308 </center>
309 <p><b>SuperDoodle</b> is a demonstration program for <b>wxPython</b> that
310 will hopefully teach you a thing or two. Just follow these simple
311 instructions: </p>
312 <p>
313 <ol>
314 <li><b>Read</b> the Source...
315 <li><b>Learn</b>...
316 <li><b>Do!</b>
317 </ol>
318
319 <p><b>SuperDoodle</b> and <b>wxPython</b> are brought to you by
320 <b>Robin Dunn</b> and <b>Total Control Software</b>, Copyright
321 &copy; 1997-2001.</p>
322 </body>
323 </html>
324 '''
325
326 def __init__(self, parent):
327 wxDialog.__init__(self, parent, -1, 'About SuperDoodle',
328 size=wxSize(420, 380))
329 from wxPython.html import wxHtmlWindow
330
331 html = wxHtmlWindow(self, -1)
332 html.SetPage(self.text)
333 button = wxButton(self, wxID_OK, "Okay")
334
335 # constraints for the html window
336 lc = wxLayoutConstraints()
337 lc.top.SameAs(self, wxTop, 5)
338 lc.left.SameAs(self, wxLeft, 5)
339 lc.bottom.SameAs(button, wxTop, 5)
340 lc.right.SameAs(self, wxRight, 5)
341 html.SetConstraints(lc)
342
343 # constraints for the button
344 lc = wxLayoutConstraints()
345 lc.bottom.SameAs(self, wxBottom, 5)
346 lc.centreX.SameAs(self, wxCentreX)
347 lc.width.AsIs()
348 lc.height.AsIs()
349 button.SetConstraints(lc)
350
351 self.SetAutoLayout(true)
352 self.Layout()
353 self.CentreOnParent(wxBOTH)
354
355
356 #----------------------------------------------------------------------
357
358 class DoodleApp(wxApp):
359 def OnInit(self):
360 frame = DoodleFrame(None)
361 frame.Show(true)
362 self.SetTopWindow(frame)
363 return true
364
365
366 #----------------------------------------------------------------------
367
368 if __name__ == '__main__':
369 app = DoodleApp(0)
370 app.MainLoop()