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