]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/frogedit/FrogEdit.py
added wxStringBuffer helper
[wxWidgets.git] / wxPython / samples / frogedit / FrogEdit.py
1 #!/usr/local/bin/python
2
3 # simple text editor
4 #
5 # Copyright 2001 Adam Feuer and Steve Howell
6 #
7 # License: Python
8
9 import os, string, re
10 import sys
11 from wxPython.wx import *
12 from StatusBar import *
13 from FrogEditor import FrogEditor
14
15 TRUE = 1
16 FALSE = 0
17
18 ABOUT_TEXT = """FrogEdit : Copyright 2001 Adam Feuer and Steve Howell
19 wxEditor component : Copyright 1999 - 2001 Dirk Holtwic, Robin Dunn, Adam Feuer, Steve Howell
20
21 FrogEdit was built using Python, wxPython, and wxWindows."""
22
23
24 ##---------------------------------------------------------------------
25
26 def chomp(line):
27 line = string.split(line,'\n')[0]
28 return string.split(line,'\r')[0]
29
30 ##---------------------------------------------------------------------
31
32 class OutlinerPanel(wxPanel):
33
34 def Close(self, event):
35 self.parent.Close()
36 wxPanel.Close(self)
37
38 ##----------------------------------------------------------------------
39
40
41 class FrogEditFrame(wxFrame):
42 def __init__(self, parent, ID, title, pos=wxDefaultPosition,
43 size=wxDefaultSize, style=wxDEFAULT_FRAME_STYLE):
44
45 wxFrame.__init__(self, parent, ID, title, pos, size, style)
46
47 splitter = wxSplitterWindow(self, -1, style=wxNO_3D|wxSP_3D)
48 win = OutlinerPanel(splitter, -1, style=wxCLIP_CHILDREN)
49 win.parent = self
50 log = self.MakeLogWindow(splitter)
51
52 self.MakeStatusbar()
53 self.MakeEditorWindow(win, log)
54 self.SetUpSplitter(splitter, win, log)
55 self.MakeMenus()
56 self.MakeMainWindow(win)
57 self.RegisterEventHandlers()
58 self.InitVariables()
59
60
61 ##------------- Init Misc
62
63 def RegisterEventHandlers(self):
64 EVT_CLOSE(self,self.OnCloseWindow)
65
66 def InitVariables(self):
67 self.fileName = None
68 self.edl.UnTouchBuffer()
69
70 def MakeMenus(self):
71 self.MainMenu = wxMenuBar()
72 self.AddMenus(self.MainMenu)
73 self.SetMenuBar(self.MainMenu)
74
75 ##------------- Init Subwindows
76
77 def MakeEditorWindow(self, win, log):
78 self.edl = FrogEditor(win, -1, style=wxSUNKEN_BORDER, statusBar = self.sb)
79 self.edl.SetControlFuncs = self.SetControlFuncs
80 self.edl.SetAltFuncs = self.SetAltFuncs
81 self.edl.SetStatus(log)
82
83 def MakeStatusbar(self):
84 self.sb = CustomStatusBar(self)
85 self.SetStatusBar(self.sb)
86
87 def MakeLogWindow(self, container):
88 log = wxTextCtrl(container, -1,
89 style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
90 wxLog_SetActiveTarget(wxLogTextCtrl(log))
91 wxLogMessage('window handle: %s' % self.GetHandle())
92 return log
93
94 def SetUpSplitter(self, splitter, win, log):
95 splitter.SplitHorizontally(win, log)
96 splitter.SetSashPosition(360, true)
97 splitter.SetMinimumPaneSize(40)
98
99 def MakeToolbar(self, win):
100 toolbarBox = wxBoxSizer(wxHORIZONTAL)
101 self.AddButtons(win, toolbarBox)
102 return toolbarBox
103
104 def MakeMainWindow(self, win):
105 mainBox = wxBoxSizer(wxVERTICAL)
106 mainBox.Add(self.MakeToolbar(win))
107 borderWidth = 5
108 mainBox.Add(self.edl, 1, wxALL|wxGROW, borderWidth)
109 win.SetSizer(mainBox)
110 win.SetAutoLayout(true)
111
112 ##-------------- Init Menus
113
114 # override this to add more menus
115 def AddMenus(self, menu):
116 self.AddFileMenu(menu)
117 self.AddEditMenu(menu)
118 self.AddHelpMenu(menu)
119
120 def AddMenuItem(self, menu, itemText, itemDescription, itemHandler):
121 menuId = wxNewId()
122 menu.Append(menuId, itemText, itemDescription)
123 EVT_MENU(self, menuId, itemHandler)
124 return menuId
125
126 def AddFileMenu(self, menu):
127 fileMenu = wxMenu()
128 self.AddMenuItem(fileMenu, '&New File\tCtrl-N', 'New File', self.OnNewFile)
129 self.AddMenuItem(fileMenu, '&Open File\tCtrl-O', 'Open File', self.OnOpenFile)
130 self.AddMenuItem(fileMenu, '&Save File\tCtrl-S', 'Save File', self.OnSaveFile)
131 self.AddMenuItem(fileMenu, 'Save File &As\tCtrl-A', 'Save File As',self.OnSaveFileAs)
132 self.AddMenuItem(fileMenu, 'E&xit\tAlt-X', 'Exit', self.OnFileExit)
133 menu.Append(fileMenu, 'File')
134
135 def AddEditMenu(self, menu):
136 editMenu = wxMenu()
137 self.AddMenuItem(editMenu, 'Cut\tCtrl-X', 'Cut', self.edl.OnCutSelection)
138 self.AddMenuItem(editMenu, '&Copy\tCtrl-C', 'Copy', self.edl.OnCopySelection)
139 self.AddMenuItem(editMenu, 'Paste\tCtrl-V', 'Paste', self.edl.OnPaste)
140 self.AddMenuItem(editMenu, 'Edit preferences', 'Edit Preferences', self.OnEditPreferences)
141 menu.Append(editMenu, 'Edit')
142
143 def AddHelpMenu(self, menu):
144 helpMenu = wxMenu()
145 self.AddMenuItem(helpMenu, 'About', 'About the program', self.OnHelpAbout)
146 menu.Append(helpMenu, 'Help')
147
148 ##---------------- Init Buttons
149
150
151 def NewButton(self, window, container, name, pos, size, handler):
152 buttonId = wxNewId()
153 if pos == None or size == None:
154 container.Add(wxButton(window, buttonId, name), 0, 0)
155 else:
156 container.Add(wxButton(window, buttonId, name, pos, size), 0, 0)
157 EVT_BUTTON(self, buttonId, handler)
158 return buttonId
159
160 # override this to make more buttons
161 def AddButtons(self, window, container):
162 buttonPos = None
163 buttonSize = None
164 self.NewButton(window, container, "New", buttonPos, buttonSize, self.OnNewFile)
165 self.NewButton(window, container, "Open", buttonPos, buttonSize, self.OnOpenFile)
166 self.NewButton(window, container, "Save", buttonPos, buttonSize, self.OnSaveFile)
167
168
169 ##-------------- Init Dialogs
170
171 def MessageDialog(self, text, title):
172 messageDialog = wxMessageDialog(self, text, title, wxOK | wxICON_INFORMATION)
173 messageDialog.ShowModal()
174 messageDialog.Destroy()
175
176 def OkCancelDialog(self, text, title):
177 dialog = wxMessageDialog(self, text, title, wxOK | wxCANCEL | wxICON_INFORMATION)
178 result = dialog.ShowModal()
179 dialog.Destroy()
180 if result == wxID_OK:
181 return TRUE
182 else:
183 return FALSE
184
185 def SelectFileDialog(self, defaultDir=None, defaultFile=None, wildCard=None):
186 if defaultDir == None:
187 defaultDir = "."
188 if defaultFile == None:
189 defaultFile = ""
190 if wildCard == None:
191 wildCard = "*.*"
192 fileName = None
193 fileDialog = wxFileDialog(self, "Choose a file", defaultDir, defaultFile, wildCard, wxOPEN|wxMULTIPLE)
194 result = fileDialog.ShowModal()
195 if result == wxID_OK:
196 fileName = fileDialog.GetPath()
197 wxLogMessage('You selected: %s\n' % fileName)
198 fileDialog.Destroy()
199 return fileName
200
201 def OpenFileError(self, fileName):
202 wxLogMessage('Open file error.')
203 self.MessageDialog("Error opening file '%s'!" % fileName, "Error")
204
205
206 def SaveFileError(self, fileName):
207 wxLogMessage('Save file error.')
208 self.MessageDialog("Error saving file '%s'!" % fileName, "Error")
209
210 ##---------------- Utility functions
211
212
213 def SetControlFuncs(self, action):
214 "for overriding editor's keys"
215 FrogEditor.SetControlFuncs(self.edl, action)
216 action['a'] = self.OnSaveFileAs
217 action['o'] = self.OnOpenFile
218 action['n'] = self.OnNewFile
219 action['s'] = self.OnSaveFile
220
221 def SetAltFuncs(self, action):
222 FrogEditor.SetAltFuncs(self.edl, action)
223 action['x'] = self.OnFileExit
224
225 def GetCurrentDir(self):
226 if self.fileName is not None:
227 return os.path.split(self.fileName)[0]
228 return "."
229
230 def GetFileName(self):
231 if self.fileName is not None:
232 return os.path.split(self.fileName)[1]
233 return ""
234
235 def NewFile(self):
236 self.edl.SetText([""])
237 self.fileName = None
238 self.sb.setFileName("")
239
240 def SaveFile(self, fileName):
241 try:
242 contents = string.join(self.edl.GetText(), '\n')
243 f = open(fileName, 'w')
244 f.write(contents)
245 f.close()
246 self.edl.UnTouchBuffer()
247 self.sb.setFileName(fileName)
248 return TRUE
249 except:
250 return FALSE
251
252 def OpenFile(self, fileName):
253 try:
254 f = open(fileName, 'r')
255 contents = f.readlines()
256 f.close()
257 contents = [chomp(line) for line in contents]
258 if len(contents) == 0:
259 contents = [""]
260 self.edl.SetText(contents)
261 self.fileName = fileName
262 self.sb.setFileName(fileName)
263 return TRUE
264 except:
265 return FALSE
266
267
268
269 ##---------------- Event handlers
270
271
272 def OnCloseWindow(self, event):
273 self.edl.OnCloseWindow(event)
274 self.Destroy()
275
276 def OnNewFile(self, event):
277 if self.edl.BufferWasTouched():
278 if not self.OkCancelDialog("New file - abandon changes?", "New File"):
279 return
280 self.NewFile()
281 self.edl.SetFocus()
282
283 def OnOpenFile(self, event):
284 if self.edl.BufferWasTouched():
285 if not self.OkCancelDialog("Open file - abandon changes?", "Open File"):
286 return
287 fileName = self.SelectFileDialog(self.GetCurrentDir())
288 if fileName is not None:
289 if self.OpenFile(fileName) is FALSE:
290 self.OpenFileError(fileName)
291 self.edl.SetFocus()
292
293 def OnSaveFile(self, event):
294 if self.fileName is None:
295 return self.OnSaveFileAs(event)
296 wxLogMessage("Saving %s..." % self.fileName)
297 if self.SaveFile(self.fileName) is not TRUE:
298 self.SaveFileError(self.fileName)
299 self.edl.SetFocus()
300
301 def OnSaveFileAs(self, event):
302 fileName = self.SelectFileDialog(self.GetCurrentDir(),self.GetFileName())
303 if fileName is not None:
304 self.fileName = fileName
305 wxLogMessage("Saving %s..." % self.fileName)
306 if self.SaveFile(self.fileName) is not TRUE:
307 self.SaveFileError(self.fileName)
308 self.edl.SetFocus()
309
310 def OnFileExit(self, event):
311 if self.edl.BufferWasTouched():
312 if not self.OkCancelDialog("Exit program - abandon changes?", "Exit"):
313 return
314 self.OnCloseWindow(event)
315
316 def OnEditPreferences(self, event):
317 self.MessageDialog("Edit preferences is not implemented yet.", "Not implemented.")
318 pass
319
320 def OnHelpAbout(self, event):
321 self.MessageDialog(ABOUT_TEXT, "About FrogEdit")
322 pass
323
324 def Show(self, show):
325 wxFrame.Show(self, show)
326 self.edl.SetFocus()
327
328 ##------------- Startup stuff
329
330 def LoadInitialFile(self, fileName):
331 if fileName is not None:
332 if self.OpenFile(fileName) is FALSE:
333 self.OpenFileError(fileName)
334
335
336
337
338 ##-------------- Application Launcher utility class
339
340 class FrogEditLauncher:
341
342 def MakeAppFrame(self):
343 return FrogEditFrame(None, -1, "FrogEdit", size=(640, 480),
344 style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
345 def GetArgvFilename(self):
346 if len(sys.argv) > 1:
347 return sys.argv[1]
348 else:
349 return None
350
351 def Main(self):
352 win = self.MakeAppFrame()
353 app = wxPySimpleApp()
354 win.Show(true)
355 win.LoadInitialFile(self.GetArgvFilename())
356 app.MainLoop()
357
358
359 ##-------------- Main program
360
361
362 if __name__ == '__main__':
363
364 launcher = FrogEditLauncher()
365 launcher.Main()
366