]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/samples/frogedit/FrogEdit.py
RemoveItem should return the same object it is passed, but adjusted
[wxWidgets.git] / wxPython / samples / frogedit / FrogEdit.py
index 68f3e27a049802a2194ff21398d615efbc87cc58..a9dceafef99cca11e0887a1a9046827ed038a870 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/local/bin/python
+#!/usr/bin/env python
 
 # simple text editor
 #
@@ -6,14 +6,16 @@
 #
 # License: Python
 
-import os, string, re
-import sys
-from wxPython.wx         import *
+import  os
+import  re
+import  string
+import  sys
+
+import  wx
+
 from StatusBar           import *
 from FrogEditor          import FrogEditor
 
-TRUE  = 1
-FALSE = 0
 
 ABOUT_TEXT = """FrogEdit : Copyright 2001 Adam Feuer and Steve Howell
 wxEditor component : Copyright 1999 - 2001 Dirk Holtwic, Robin Dunn, Adam Feuer, Steve Howell
@@ -29,26 +31,26 @@ def chomp(line):
 
 ##---------------------------------------------------------------------
 
-class OutlinerPanel(wxPanel):
+class OutlinerPanel(wx.Panel):
 
     def Close(self, event):
         self.parent.Close()
-        wxPanel.Close(self)
+        wx.Panel.Close(self)
 
 ##----------------------------------------------------------------------
-    
 
-class FrogEditFrame(wxFrame):
-    def __init__(self, parent, ID, title, pos=wxDefaultPosition,
-                 size=wxDefaultSize, style=wxDEFAULT_FRAME_STYLE):
 
-        wxFrame.__init__(self, parent, ID, title, pos, size, style)
+class FrogEditFrame(wx.Frame):
+    def __init__(self, parent, ID, title, pos=wx.DefaultPosition,
+                 size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
+
+        wx.Frame.__init__(self, parent, ID, title, pos, size, style)
 
-        splitter = wxSplitterWindow(self, -1, style=wxNO_3D|wxSP_3D)
-        win = OutlinerPanel(splitter, -1, style=wxCLIP_CHILDREN)
+        splitter = wx.SplitterWindow(self, -1, style=wx.NO_3D|wx.SP_3D)
+        win = OutlinerPanel(splitter, -1, style=wx.CLIP_CHILDREN)
         win.parent = self
         log = self.MakeLogWindow(splitter)
-        
+
         self.MakeStatusbar()
         self.MakeEditorWindow(win, log)
         self.SetUpSplitter(splitter, win, log)
@@ -57,25 +59,27 @@ class FrogEditFrame(wxFrame):
         self.RegisterEventHandlers()
         self.InitVariables()
 
+        win.Layout()
+
 
 ##------------- Init Misc
 
     def RegisterEventHandlers(self):
-        EVT_CLOSE(self,self.OnCloseWindow)
+        self.Bind(wx.EVT_CLOSE,self.OnCloseWindow)
 
     def InitVariables(self):
         self.fileName = None
         self.edl.UnTouchBuffer()
-        
+
     def MakeMenus(self):
-        self.MainMenu = wxMenuBar()
+        self.MainMenu = wx.MenuBar()
         self.AddMenus(self.MainMenu)
         self.SetMenuBar(self.MainMenu)
 
 ##------------- Init Subwindows
 
     def MakeEditorWindow(self, win, log):
-        self.edl = FrogEditor(win, -1, style=wxSUNKEN_BORDER, statusBar = self.sb)
+        self.edl = FrogEditor(win, -1, style=wx.SUNKEN_BORDER, statusBar = self.sb)
         self.edl.SetControlFuncs = self.SetControlFuncs
         self.edl.SetAltFuncs = self.SetAltFuncs
         self.edl.SetStatus(log)
@@ -85,29 +89,29 @@ class FrogEditFrame(wxFrame):
         self.SetStatusBar(self.sb)
 
     def MakeLogWindow(self, container):
-        log = wxTextCtrl(container, -1,
-                         style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
-        wxLog_SetActiveTarget(wxLogTextCtrl(log))
-        wxLogMessage('window handle: %s' % self.GetHandle())
+        log = wx.TextCtrl(container, -1,
+                         style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
+        wx.Log_SetActiveTarget(wx.LogTextCtrl(log))
+        wx.LogMessage('window handle: %s' % self.GetHandle())
         return log
 
     def SetUpSplitter(self, splitter, win, log):
         splitter.SplitHorizontally(win, log)
-        splitter.SetSashPosition(360, true)
+        splitter.SetSashPosition(360, True)
         splitter.SetMinimumPaneSize(40)
 
     def MakeToolbar(self, win):
-        toolbarBox = wxBoxSizer(wxHORIZONTAL)
+        toolbarBox = wx.BoxSizer(wx.HORIZONTAL)
         self.AddButtons(win, toolbarBox)
         return toolbarBox
 
     def MakeMainWindow(self, win):
-        mainBox = wxBoxSizer(wxVERTICAL)
+        mainBox = wx.BoxSizer(wx.VERTICAL)
         mainBox.Add(self.MakeToolbar(win))
         borderWidth = 5
-        mainBox.Add(self.edl, 1, wxALL|wxGROW, borderWidth)
+        mainBox.Add(self.edl, 1, wx.ALL|wx.GROW, borderWidth)
         win.SetSizer(mainBox)
-        win.SetAutoLayout(true)
+        win.SetAutoLayout(True)
 
 ##-------------- Init Menus
 
@@ -118,13 +122,13 @@ class FrogEditFrame(wxFrame):
         self.AddHelpMenu(menu)
 
     def AddMenuItem(self, menu, itemText, itemDescription, itemHandler):
-        menuId = wxNewId()
+        menuId = wx.NewId()
         menu.Append(menuId, itemText, itemDescription)
-        EVT_MENU(self, menuId, itemHandler)
+        self.Bind(wx.EVT_MENU, itemHandler, id=menuId)
         return menuId
 
     def AddFileMenu(self, menu):
-        fileMenu = wxMenu()
+        fileMenu = wx.Menu()
         self.AddMenuItem(fileMenu, '&New File\tCtrl-N', 'New File', self.OnNewFile)
         self.AddMenuItem(fileMenu, '&Open File\tCtrl-O', 'Open File', self.OnOpenFile)
         self.AddMenuItem(fileMenu, '&Save File\tCtrl-S', 'Save File', self.OnSaveFile)
@@ -133,7 +137,7 @@ class FrogEditFrame(wxFrame):
         menu.Append(fileMenu, 'File')
 
     def AddEditMenu(self, menu):
-        editMenu = wxMenu()
+        editMenu = wx.Menu()
         self.AddMenuItem(editMenu, 'Cut\tCtrl-X', 'Cut', self.edl.OnCutSelection)
         self.AddMenuItem(editMenu, '&Copy\tCtrl-C', 'Copy', self.edl.OnCopySelection)
         self.AddMenuItem(editMenu, 'Paste\tCtrl-V', 'Paste', self.edl.OnPaste)
@@ -141,7 +145,7 @@ class FrogEditFrame(wxFrame):
         menu.Append(editMenu, 'Edit')
 
     def AddHelpMenu(self, menu):
-        helpMenu = wxMenu()
+        helpMenu = wx.Menu()
         self.AddMenuItem(helpMenu, 'About', 'About the program', self.OnHelpAbout)
         menu.Append(helpMenu, 'Help')
 
@@ -149,12 +153,12 @@ class FrogEditFrame(wxFrame):
 
 
     def NewButton(self, window, container, name, pos, size, handler):
-        buttonId = wxNewId()
+        buttonId = wx.NewId()
         if pos == None or size == None:
-            container.Add(wxButton(window, buttonId, name), 0, 0)
+            container.Add(wx.Button(window, buttonId, name), 0, 0)
         else:
-            container.Add(wxButton(window, buttonId, name, pos, size), 0, 0)
-        EVT_BUTTON(self, buttonId, handler)
+            container.Add(wx.Button(window, buttonId, name, pos, size), 0, 0)
+        self.Bind(wx.EVT_BUTTON, handler, id=buttonId)
         return buttonId
 
     # override this to make more buttons
@@ -169,18 +173,18 @@ class FrogEditFrame(wxFrame):
 ##-------------- Init Dialogs
 
     def MessageDialog(self, text, title):
-        messageDialog = wxMessageDialog(self, text, title, wxOK | wxICON_INFORMATION)
+        messageDialog = wx.MessageDialog(self, text, title, wx.OK | wx.ICON_INFORMATION)
         messageDialog.ShowModal()
         messageDialog.Destroy()
-    
+
     def OkCancelDialog(self, text, title):
-        dialog = wxMessageDialog(self, text, title, wxOK | wxCANCEL | wxICON_INFORMATION)
+        dialog = wx.MessageDialog(self, text, title, wx.OK | wx.CANCEL | wx.ICON_INFORMATION)
         result = dialog.ShowModal()
         dialog.Destroy()
-        if result == wxID_OK:
-            return TRUE
+        if result == wx.ID_OK:
+            return True
         else:
-            return FALSE
+            return False
 
     def SelectFileDialog(self, defaultDir=None, defaultFile=None, wildCard=None):
         if defaultDir == None:
@@ -190,21 +194,21 @@ class FrogEditFrame(wxFrame):
         if wildCard == None:
             wildCard = "*.*"
         fileName = None
-        fileDialog = wxFileDialog(self, "Choose a file", defaultDir, defaultFile, wildCard, wxOPEN|wxMULTIPLE)
-        result = fileDialog.ShowModal() 
-        if result == wxID_OK:
+        fileDialog = wx.FileDialog(self, "Choose a file", defaultDir, defaultFile, wildCard, wx.OPEN|wx.MULTIPLE)
+        result = fileDialog.ShowModal()
+        if result == wx.ID_OK:
             fileName = fileDialog.GetPath()
-            wxLogMessage('You selected: %s\n' % fileName)
-        fileDialog.Destroy()       
+            wx.LogMessage('You selected: %s\n' % fileName)
+        fileDialog.Destroy()
         return fileName
 
     def OpenFileError(self, fileName):
-        wxLogMessage('Open file error.')
+        wx.LogMessage('Open file error.')
         self.MessageDialog("Error opening file '%s'!" % fileName, "Error")
 
 
     def SaveFileError(self, fileName):
-        wxLogMessage('Save file error.')
+        wx.LogMessage('Save file error.')
         self.MessageDialog("Error saving file '%s'!" % fileName, "Error")
 
 ##---------------- Utility functions
@@ -220,13 +224,13 @@ class FrogEditFrame(wxFrame):
 
     def SetAltFuncs(self, action):
         FrogEditor.SetAltFuncs(self.edl, action)
-        action['x'] = self.OnFileExit 
+        action['x'] = self.OnFileExit
 
     def GetCurrentDir(self):
         if self.fileName is not None:
             return os.path.split(self.fileName)[0]
         return "."
-            
+
     def GetFileName(self):
         if self.fileName is not None:
             return os.path.split(self.fileName)[1]
@@ -245,9 +249,9 @@ class FrogEditFrame(wxFrame):
             f.close()
             self.edl.UnTouchBuffer()
             self.sb.setFileName(fileName)
-            return TRUE
+            return True
         except:
-            return FALSE
+            return False
 
     def OpenFile(self, fileName):
         try:
@@ -260,10 +264,10 @@ class FrogEditFrame(wxFrame):
             self.edl.SetText(contents)
             self.fileName = fileName
             self.sb.setFileName(fileName)
-            return TRUE
+            return True
         except:
-            return FALSE
-        
+            return False
+
 
 
 ##---------------- Event handlers
@@ -279,35 +283,35 @@ class FrogEditFrame(wxFrame):
                 return
         self.NewFile()
         self.edl.SetFocus()
-    
+
     def OnOpenFile(self, event):
         if self.edl.BufferWasTouched():
             if not self.OkCancelDialog("Open file - abandon changes?", "Open File"):
                 return
         fileName = self.SelectFileDialog(self.GetCurrentDir())
         if fileName is not None:
-            if self.OpenFile(fileName) is FALSE:
+            if self.OpenFile(fileName) is False:
                 self.OpenFileError(fileName)
         self.edl.SetFocus()
-    
+
     def OnSaveFile(self, event):
         if self.fileName is None:
             return self.OnSaveFileAs(event)
-        wxLogMessage("Saving %s..." % self.fileName)
-        if self.SaveFile(self.fileName) is not TRUE:
+        wx.LogMessage("Saving %s..." % self.fileName)
+        if self.SaveFile(self.fileName) is not True:
             self.SaveFileError(self.fileName)
         self.edl.SetFocus()
-    
+
     def OnSaveFileAs(self, event):
         fileName = self.SelectFileDialog(self.GetCurrentDir(),self.GetFileName())
         if fileName is not None:
             self.fileName = fileName
-            wxLogMessage("Saving %s..." % self.fileName)
-            if self.SaveFile(self.fileName) is not TRUE:
+            wx.LogMessage("Saving %s..." % self.fileName)
+            if self.SaveFile(self.fileName) is not True:
                 self.SaveFileError(self.fileName)
         self.edl.SetFocus()
-    
-    def OnFileExit(self, event):        
+
+    def OnFileExit(self, event):
         if self.edl.BufferWasTouched():
             if not self.OkCancelDialog("Exit program - abandon changes?", "Exit"):
                 return
@@ -316,21 +320,21 @@ class FrogEditFrame(wxFrame):
     def OnEditPreferences(self, event):
         self.MessageDialog("Edit preferences is not implemented yet.", "Not implemented.")
         pass
-    
+
     def OnHelpAbout(self, event):
         self.MessageDialog(ABOUT_TEXT, "About FrogEdit")
         pass
-    
+
     def Show(self, show):
-        wxFrame.Show(self, show)
+        wx.Frame.Show(self, show)
         self.edl.SetFocus()
 
 ##------------- Startup stuff
 
     def LoadInitialFile(self, fileName):
         if fileName is not None:
-            if self.OpenFile(fileName) is FALSE:
-                self.OpenFileError(fileName)        
+            if self.OpenFile(fileName) is False:
+                self.OpenFileError(fileName)
 
 
 
@@ -341,7 +345,8 @@ class FrogEditLauncher:
 
     def MakeAppFrame(self):
         return FrogEditFrame(None, -1, "FrogEdit", size=(640, 480),
-                             style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)    
+                             style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
+
     def GetArgvFilename(self):
         if len(sys.argv) > 1:
             return sys.argv[1]
@@ -349,9 +354,9 @@ class FrogEditLauncher:
             return None
 
     def Main(self):
+        app = wx.PySimpleApp()
         win = self.MakeAppFrame()
-        app = wxPySimpleApp()
-        win.Show(true)
+        win.Show(True)
         win.LoadInitialFile(self.GetArgvFilename())
         app.MainLoop()
 
@@ -363,4 +368,3 @@ if __name__ == '__main__':
 
     launcher = FrogEditLauncher()
     launcher.Main()
-