]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/samples/wxPIA_book/Chapter-05/goodExample.py
Added the sample code from wxPython In Action to the samples dir
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-05 / goodExample.py
diff --git a/wxPython/samples/wxPIA_book/Chapter-05/goodExample.py b/wxPython/samples/wxPIA_book/Chapter-05/goodExample.py
new file mode 100644 (file)
index 0000000..adb7e54
--- /dev/null
@@ -0,0 +1,96 @@
+#!/usr/bin/env python
+
+import wx
+
+class RefactorExample(wx.Frame):
+
+    def __init__(self, parent, id):
+        wx.Frame.__init__(self, parent, id, 'Refactor Example',
+                size=(340, 200))
+        panel = wx.Panel(self, -1)
+        panel.SetBackgroundColour("White")
+        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
+        self.createMenuBar()
+        self.createButtonBar(panel)
+        self.createTextFields(panel)
+
+    def menuData(self):
+        return (("&File",
+                    ("&Open", "Open in status bar", self.OnOpen),
+                    ("&Quit", "Quit", self.OnCloseWindow)),
+                ("&Edit",
+                    ("&Copy", "Copy", self.OnCopy),
+                    ("C&ut", "Cut", self.OnCut),
+                    ("&Paste", "Paste", self.OnPaste),
+                    ("", "", ""),
+                    ("&Options...", "DisplayOptions", self.OnOptions)))
+
+    def createMenuBar(self):
+        menuBar = wx.MenuBar()
+        for eachMenuData in self.menuData():
+            menuLabel = eachMenuData[0]
+            menuItems = eachMenuData[1:]
+            menuBar.Append(self.createMenu(menuItems), menuLabel)
+        self.SetMenuBar(menuBar)
+
+    def createMenu(self, menuData):
+        menu = wx.Menu()
+        for eachLabel, eachStatus, eachHandler in menuData:
+            if not eachLabel:
+                menu.AppendSeparator()
+                continue
+            menuItem = menu.Append(-1, eachLabel, eachStatus)
+            self.Bind(wx.EVT_MENU, eachHandler, menuItem)
+        return menu
+
+    def buttonData(self):
+        return (("First", self.OnFirst),
+                ("<< PREV", self.OnPrev),
+                ("NEXT >>", self.OnNext),
+                ("Last", self.OnLast))
+
+    def createButtonBar(self, panel, yPos = 0):
+        xPos = 0
+        for eachLabel, eachHandler in self.buttonData():
+            pos = (xPos, yPos)
+            button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
+            xPos += button.GetSize().width
+
+    def buildOneButton(self, parent, label, handler, pos=(0,0)):
+        button = wx.Button(parent, -1, label, pos)
+        self.Bind(wx.EVT_BUTTON, handler, button)
+        return button
+
+    def textFieldData(self):
+        return (("First Name", (10, 50)),
+                ("Last Name", (10, 80)))
+
+    def createTextFields(self, panel):
+        for eachLabel, eachPos in self.textFieldData():
+            self.createCaptionedText(panel, eachLabel, eachPos)
+
+    def createCaptionedText(self, panel, label, pos):
+        static = wx.StaticText(panel, wx.NewId(), label, pos)
+        static.SetBackgroundColour("White")
+        textPos = (pos[0] + 75, pos[1])
+        wx.TextCtrl(panel, wx.NewId(), "", size=(100, -1), pos=textPos)
+
+    # Just grouping the empty event handlers together
+    def OnPrev(self, event): pass
+    def OnNext(self, event): pass
+    def OnLast(self, event): pass
+    def OnFirst(self, event): pass
+    def OnOpen(self, event): pass
+    def OnCopy(self, event): pass
+    def OnCut(self, event): pass
+    def OnPaste(self, event): pass
+    def OnOptions(self, event): pass
+    def OnCloseWindow(self, event):
+        self.Destroy()
+
+if __name__ == '__main__':
+    app = wx.PySimpleApp()
+    frame = RefactorExample(parent=None, id=-1)
+    frame.Show()
+    app.MainLoop()
+