]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-06/example5.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 / example5.py
1 import wx
2 from example1 import SketchWindow
3
4
5 class SketchFrame(wx.Frame):
6 def __init__(self, parent):
7 wx.Frame.__init__(self, parent, -1, "Sketch Frame",
8 size=(800,600))
9 self.sketch = SketchWindow(self, -1)
10 self.sketch.Bind(wx.EVT_MOTION, self.OnSketchMotion)
11 self.initStatusBar()
12 self.createMenuBar()
13 self.createToolBar()
14
15 def initStatusBar(self):
16 self.statusbar = self.CreateStatusBar()
17 self.statusbar.SetFieldsCount(3)
18 self.statusbar.SetStatusWidths([-1, -2, -3])
19
20 def OnSketchMotion(self, event):
21 self.statusbar.SetStatusText("Pos: %s" %
22 str(event.GetPositionTuple()), 0)
23 self.statusbar.SetStatusText("Current Pts: %s" %
24 len(self.sketch.curLine), 1)
25 self.statusbar.SetStatusText("Line Count: %s" %
26 len(self.sketch.lines), 2)
27 event.Skip()
28
29 def menuData(self):
30 return [("&File", (
31 ("&New", "New Sketch file", self.OnNew),
32 ("&Open", "Open sketch file", self.OnOpen),
33 ("&Save", "Save sketch file", self.OnSave),
34 ("", "", ""),
35 ("&Color", (
36 ("&Black", "", self.OnColor, wx.ITEM_RADIO),
37 ("&Red", "", self.OnColor, wx.ITEM_RADIO),
38 ("&Green", "", self.OnColor, wx.ITEM_RADIO),
39 ("&Blue", "", self.OnColor, wx.ITEM_RADIO))),
40 ("", "", ""),
41 ("&Quit", "Quit", self.OnCloseWindow)))]
42
43 def createMenuBar(self):
44 menuBar = wx.MenuBar()
45 for eachMenuData in self.menuData():
46 menuLabel = eachMenuData[0]
47 menuItems = eachMenuData[1]
48 menuBar.Append(self.createMenu(menuItems), menuLabel)
49 self.SetMenuBar(menuBar)
50
51 def createMenu(self, menuData):
52 menu = wx.Menu()
53 for eachItem in menuData:
54 if len(eachItem) == 2:
55 label = eachItem[0]
56 subMenu = self.createMenu(eachItem[1])
57 menu.AppendMenu(wx.NewId(), label, subMenu)
58 else:
59 self.createMenuItem(menu, *eachItem)
60 return menu
61
62 def createMenuItem(self, menu, label, status, handler, kind=wx.ITEM_NORMAL):
63 if not label:
64 menu.AppendSeparator()
65 return
66 menuItem = menu.Append(-1, label, status, kind)
67 self.Bind(wx.EVT_MENU, handler, menuItem)
68
69 def createToolBar(self):
70 toolbar = self.CreateToolBar()
71 for each in self.toolbarData():
72 self.createSimpleTool(toolbar, *each)
73 toolbar.AddSeparator()
74 for each in self.toolbarColorData():
75 self.createColorTool(toolbar, each)
76 toolbar.Realize()
77
78 def createSimpleTool(self, toolbar, label, filename, help, handler):
79 if not label:
80 toolbar.AddSeparator()
81 return
82 bmp = wx.Image(filename, wx.BITMAP_TYPE_BMP).ConvertToBitmap()
83 tool = toolbar.AddSimpleTool(-1, bmp, label, help)
84 self.Bind(wx.EVT_MENU, handler, tool)
85
86 def toolbarData(self):
87 return (("New", "new.bmp", "Create new sketch", self.OnNew),
88 ("", "", "", ""),
89 ("Open", "open.bmp", "Open existing sketch", self.OnOpen),
90 ("Save", "save.bmp", "Save existing sketch", self.OnSave))
91
92 def createColorTool(self, toolbar, color):
93 bmp = self.MakeBitmap(color)
94 tool = toolbar.AddRadioTool(-1, bmp, shortHelp=color)
95 self.Bind(wx.EVT_MENU, self.OnColor, tool)
96
97 def MakeBitmap(self, color):
98 bmp = wx.EmptyBitmap(16, 15)
99 dc = wx.MemoryDC()
100 dc.SelectObject(bmp)
101 dc.SetBackground(wx.Brush(color))
102 dc.Clear()
103 dc.SelectObject(wx.NullBitmap)
104 return bmp
105
106 def toolbarColorData(self):
107 return ("Black", "Red", "Green", "Blue")
108
109 def OnNew(self, event): pass
110 def OnOpen(self, event): pass
111 def OnSave(self, event): pass
112
113 def OnColor(self, event):
114 menubar = self.GetMenuBar()
115 itemId = event.GetId()
116 item = menubar.FindItemById(itemId)
117 if not item:
118 toolbar = self.GetToolBar()
119 item = toolbar.FindById(itemId)
120 color = item.GetShortHelp()
121 else:
122 color = item.GetLabel()
123 self.sketch.SetColor(color)
124
125 def OnCloseWindow(self, event):
126 self.Destroy()
127
128 if __name__ == '__main__':
129 app = wx.PySimpleApp()
130 frame = SketchFrame(None)
131 frame.Show(True)
132 app.MainLoop()