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