]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-06/example6.py
don't use invalid wxIconBundles, it results in asserts after recent changes
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-06 / example6.py
1 import wx
2 import cPickle
3 import os
4
5 from example1 import SketchWindow
6
7
8 class SketchFrame(wx.Frame):
9 def __init__(self, parent):
10 self.title = "Sketch Frame"
11 wx.Frame.__init__(self, parent, -1, self.title,
12 size=(800,600))
13 self.filename = ""
14 self.sketch = SketchWindow(self, -1)
15 self.sketch.Bind(wx.EVT_MOTION, self.OnSketchMotion)
16 self.initStatusBar()
17 self.createMenuBar()
18 self.createToolBar()
19
20 def initStatusBar(self):
21 self.statusbar = self.CreateStatusBar()
22 self.statusbar.SetFieldsCount(3)
23 self.statusbar.SetStatusWidths([-1, -2, -3])
24
25 def OnSketchMotion(self, event):
26 self.statusbar.SetStatusText("Pos: %s" %
27 str(event.GetPositionTuple()), 0)
28 self.statusbar.SetStatusText("Current Pts: %s" %
29 len(self.sketch.curLine), 1)
30 self.statusbar.SetStatusText("Line Count: %s" %
31 len(self.sketch.lines), 2)
32 event.Skip()
33
34 def menuData(self):
35 return [("&File", (
36 ("&New", "New Sketch file", self.OnNew),
37 ("&Open", "Open sketch file", self.OnOpen),
38 ("&Save", "Save sketch file", self.OnSave),
39 ("", "", ""),
40 ("&Color", (
41 ("&Black", "", self.OnColor, wx.ITEM_RADIO),
42 ("&Red", "", self.OnColor, wx.ITEM_RADIO),
43 ("&Green", "", self.OnColor, wx.ITEM_RADIO),
44 ("&Blue", "", self.OnColor, wx.ITEM_RADIO),
45 ("&Other...", "", self.OnOtherColor, wx.ITEM_RADIO))),
46 ("", "", ""),
47 ("&Quit", "Quit", self.OnCloseWindow)))]
48
49 def createMenuBar(self):
50 menuBar = wx.MenuBar()
51 for eachMenuData in self.menuData():
52 menuLabel = eachMenuData[0]
53 menuItems = eachMenuData[1]
54 menuBar.Append(self.createMenu(menuItems), menuLabel)
55 self.SetMenuBar(menuBar)
56
57 def createMenu(self, menuData):
58 menu = wx.Menu()
59 for eachItem in menuData:
60 if len(eachItem) == 2:
61 label = eachItem[0]
62 subMenu = self.createMenu(eachItem[1])
63 menu.AppendMenu(wx.NewId(), label, subMenu)
64 else:
65 self.createMenuItem(menu, *eachItem)
66 return menu
67
68 def createMenuItem(self, menu, label, status, handler, kind=wx.ITEM_NORMAL):
69 if not label:
70 menu.AppendSeparator()
71 return
72 menuItem = menu.Append(-1, label, status, kind)
73 self.Bind(wx.EVT_MENU, handler, menuItem)
74
75 def createToolBar(self):
76 toolbar = self.CreateToolBar()
77 for each in self.toolbarData():
78 self.createSimpleTool(toolbar, *each)
79 toolbar.AddSeparator()
80 for each in self.toolbarColorData():
81 self.createColorTool(toolbar, each)
82 toolbar.Realize()
83
84 def createSimpleTool(self, toolbar, label, filename, help, handler):
85 if not label:
86 toolbar.AddSeparator()
87 return
88 bmp = wx.Image(filename, wx.BITMAP_TYPE_BMP).ConvertToBitmap()
89 tool = toolbar.AddSimpleTool(-1, bmp, label, help)
90 self.Bind(wx.EVT_MENU, handler, tool)
91
92 def toolbarData(self):
93 return (("New", "new.bmp", "Create new sketch", self.OnNew),
94 ("", "", "", ""),
95 ("Open", "open.bmp", "Open existing sketch", self.OnOpen),
96 ("Save", "save.bmp", "Save existing sketch", self.OnSave))
97
98 def createColorTool(self, toolbar, color):
99 bmp = self.MakeBitmap(color)
100 tool = toolbar.AddRadioTool(-1, bmp, shortHelp=color)
101 self.Bind(wx.EVT_MENU, self.OnColor, tool)
102
103 def MakeBitmap(self, color):
104 bmp = wx.EmptyBitmap(16, 15)
105 dc = wx.MemoryDC()
106 dc.SelectObject(bmp)
107 dc.SetBackground(wx.Brush(color))
108 dc.Clear()
109 dc.SelectObject(wx.NullBitmap)
110 return bmp
111
112 def toolbarColorData(self):
113 return ("Black", "Red", "Green", "Blue")
114
115 def OnNew(self, event): pass
116
117 def OnColor(self, event):
118 menubar = self.GetMenuBar()
119 itemId = event.GetId()
120 item = menubar.FindItemById(itemId)
121 if not item:
122 toolbar = self.GetToolBar()
123 item = toolbar.FindById(itemId)
124 color = item.GetShortHelp()
125 else:
126 color = item.GetLabel()
127 self.sketch.SetColor(color)
128
129 def OnCloseWindow(self, event):
130 self.Destroy()
131
132 def SaveFile(self):
133 if self.filename:
134 data = self.sketch.GetLinesData()
135 f = open(self.filename, 'w')
136 cPickle.dump(data, f)
137 f.close()
138
139 def ReadFile(self):
140 if self.filename:
141 try:
142 f = open(self.filename, 'r')
143 data = cPickle.load(f)
144 f.close()
145 self.sketch.SetLinesData(data)
146 except cPickle.UnpicklingError:
147 wx.MessageBox("%s is not a sketch file." % self.filename,
148 "oops!", style=wx.OK|wx.ICON_EXCLAMATION)
149
150 wildcard = "Sketch files (*.sketch)|*.sketch|All files (*.*)|*.*"
151
152 def OnOpen(self, event):
153 dlg = wx.FileDialog(self, "Open sketch file...", os.getcwd(),
154 style=wx.OPEN, wildcard=self.wildcard)
155 if dlg.ShowModal() == wx.ID_OK:
156 self.filename = dlg.GetPath()
157 self.ReadFile()
158 self.SetTitle(self.title + ' -- ' + self.filename)
159 dlg.Destroy()
160
161 def OnSave(self, event):
162 if not self.filename:
163 self.OnSaveAs(event)
164 else:
165 self.SaveFile()
166
167 def OnSaveAs(self, event):
168 dlg = wx.FileDialog(self, "Save sketch as...", os.getcwd(),
169 style=wx.SAVE | wx.OVERWRITE_PROMPT,
170 wildcard = self.wildcard)
171 if dlg.ShowModal() == wx.ID_OK:
172 filename = dlg.GetPath()
173 if not os.path.splitext(filename)[1]:
174 filename = filename + '.sketch'
175 self.filename = filename
176 self.SaveFile()
177 self.SetTitle(self.title + ' -- ' + self.filename)
178 dlg.Destroy()
179
180 def OnOtherColor(self, event):
181 dlg = wx.ColourDialog(frame)
182 dlg.GetColourData().SetChooseFull(True)
183 if dlg.ShowModal() == wx.ID_OK:
184 self.sketch.SetColor(dlg.GetColourData().GetColour())
185 dlg.Destroy()
186
187
188 if __name__ == '__main__':
189 app = wx.PySimpleApp()
190 frame = SketchFrame(None)
191 frame.Show(True)
192 app.MainLoop()