1 #!/usr/local/bin/python
5 # Copyright 2001 Adam Feuer and Steve Howell
11 from wxPython
.wx
import *
12 from StatusBar
import *
13 from FrogEditor
import FrogEditor
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
21 FrogEdit was built using Python, wxPython, and wxWindows."""
24 ##---------------------------------------------------------------------
27 line
= string
.split(line
,'\n')[0]
28 return string
.split(line
,'\r')[0]
30 ##---------------------------------------------------------------------
32 class OutlinerPanel(wxPanel
):
34 def Close(self
, event
):
38 ##----------------------------------------------------------------------
41 class FrogEditFrame(wxFrame
):
42 def __init__(self
, parent
, ID
, title
, pos
=wxDefaultPosition
,
43 size
=wxDefaultSize
, style
=wxDEFAULT_FRAME_STYLE
):
45 wxFrame
.__init
__(self
, parent
, ID
, title
, pos
, size
, style
)
47 splitter
= wxSplitterWindow(self
, -1, style
=wxNO_3D|wxSP_3D
)
48 win
= OutlinerPanel(splitter
, -1, style
=wxCLIP_CHILDREN
)
50 log
= self
.MakeLogWindow(splitter
)
53 self
.MakeEditorWindow(win
, log
)
54 self
.SetUpSplitter(splitter
, win
, log
)
56 self
.MakeMainWindow(win
)
57 self
.RegisterEventHandlers()
63 ##------------- Init Misc
65 def RegisterEventHandlers(self
):
66 EVT_CLOSE(self
,self
.OnCloseWindow
)
68 def InitVariables(self
):
70 self
.edl
.UnTouchBuffer()
73 self
.MainMenu
= wxMenuBar()
74 self
.AddMenus(self
.MainMenu
)
75 self
.SetMenuBar(self
.MainMenu
)
77 ##------------- Init Subwindows
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
)
85 def MakeStatusbar(self
):
86 self
.sb
= CustomStatusBar(self
)
87 self
.SetStatusBar(self
.sb
)
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())
96 def SetUpSplitter(self
, splitter
, win
, log
):
97 splitter
.SplitHorizontally(win
, log
)
98 splitter
.SetSashPosition(360, true
)
99 splitter
.SetMinimumPaneSize(40)
101 def MakeToolbar(self
, win
):
102 toolbarBox
= wxBoxSizer(wxHORIZONTAL
)
103 self
.AddButtons(win
, toolbarBox
)
106 def MakeMainWindow(self
, win
):
107 mainBox
= wxBoxSizer(wxVERTICAL
)
108 mainBox
.Add(self
.MakeToolbar(win
))
110 mainBox
.Add(self
.edl
, 1, wxALL|wxGROW
, borderWidth
)
111 win
.SetSizer(mainBox
)
112 win
.SetAutoLayout(true
)
114 ##-------------- Init Menus
116 # override this to add more menus
117 def AddMenus(self
, menu
):
118 self
.AddFileMenu(menu
)
119 self
.AddEditMenu(menu
)
120 self
.AddHelpMenu(menu
)
122 def AddMenuItem(self
, menu
, itemText
, itemDescription
, itemHandler
):
124 menu
.Append(menuId
, itemText
, itemDescription
)
125 EVT_MENU(self
, menuId
, itemHandler
)
128 def AddFileMenu(self
, menu
):
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')
137 def AddEditMenu(self
, menu
):
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')
145 def AddHelpMenu(self
, menu
):
147 self
.AddMenuItem(helpMenu
, 'About', 'About the program', self
.OnHelpAbout
)
148 menu
.Append(helpMenu
, 'Help')
150 ##---------------- Init Buttons
153 def NewButton(self
, window
, container
, name
, pos
, size
, handler
):
155 if pos
== None or size
== None:
156 container
.Add(wxButton(window
, buttonId
, name
), 0, 0)
158 container
.Add(wxButton(window
, buttonId
, name
, pos
, size
), 0, 0)
159 EVT_BUTTON(self
, buttonId
, handler
)
162 # override this to make more buttons
163 def AddButtons(self
, window
, container
):
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
)
171 ##-------------- Init Dialogs
173 def MessageDialog(self
, text
, title
):
174 messageDialog
= wxMessageDialog(self
, text
, title
, wxOK | wxICON_INFORMATION
)
175 messageDialog
.ShowModal()
176 messageDialog
.Destroy()
178 def OkCancelDialog(self
, text
, title
):
179 dialog
= wxMessageDialog(self
, text
, title
, wxOK | wxCANCEL | wxICON_INFORMATION
)
180 result
= dialog
.ShowModal()
182 if result
== wxID_OK
:
187 def SelectFileDialog(self
, defaultDir
=None, defaultFile
=None, wildCard
=None):
188 if defaultDir
== None:
190 if defaultFile
== None:
195 fileDialog
= wxFileDialog(self
, "Choose a file", defaultDir
, defaultFile
, wildCard
, wxOPEN|wxMULTIPLE
)
196 result
= fileDialog
.ShowModal()
197 if result
== wxID_OK
:
198 fileName
= fileDialog
.GetPath()
199 wxLogMessage('You selected: %s\n' % fileName
)
203 def OpenFileError(self
, fileName
):
204 wxLogMessage('Open file error.')
205 self
.MessageDialog("Error opening file '%s'!" % fileName
, "Error")
208 def SaveFileError(self
, fileName
):
209 wxLogMessage('Save file error.')
210 self
.MessageDialog("Error saving file '%s'!" % fileName
, "Error")
212 ##---------------- Utility functions
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
223 def SetAltFuncs(self
, action
):
224 FrogEditor
.SetAltFuncs(self
.edl
, action
)
225 action
['x'] = self
.OnFileExit
227 def GetCurrentDir(self
):
228 if self
.fileName
is not None:
229 return os
.path
.split(self
.fileName
)[0]
232 def GetFileName(self
):
233 if self
.fileName
is not None:
234 return os
.path
.split(self
.fileName
)[1]
238 self
.edl
.SetText([""])
240 self
.sb
.setFileName("")
242 def SaveFile(self
, fileName
):
244 contents
= string
.join(self
.edl
.GetText(), '\n')
245 f
= open(fileName
, 'w')
248 self
.edl
.UnTouchBuffer()
249 self
.sb
.setFileName(fileName
)
254 def OpenFile(self
, fileName
):
256 f
= open(fileName
, 'r')
257 contents
= f
.readlines()
259 contents
= [chomp(line
) for line
in contents
]
260 if len(contents
) == 0:
262 self
.edl
.SetText(contents
)
263 self
.fileName
= fileName
264 self
.sb
.setFileName(fileName
)
271 ##---------------- Event handlers
274 def OnCloseWindow(self
, event
):
275 self
.edl
.OnCloseWindow(event
)
278 def OnNewFile(self
, event
):
279 if self
.edl
.BufferWasTouched():
280 if not self
.OkCancelDialog("New file - abandon changes?", "New File"):
285 def OnOpenFile(self
, event
):
286 if self
.edl
.BufferWasTouched():
287 if not self
.OkCancelDialog("Open file - abandon changes?", "Open File"):
289 fileName
= self
.SelectFileDialog(self
.GetCurrentDir())
290 if fileName
is not None:
291 if self
.OpenFile(fileName
) is FALSE
:
292 self
.OpenFileError(fileName
)
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
)
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
)
312 def OnFileExit(self
, event
):
313 if self
.edl
.BufferWasTouched():
314 if not self
.OkCancelDialog("Exit program - abandon changes?", "Exit"):
316 self
.OnCloseWindow(event
)
318 def OnEditPreferences(self
, event
):
319 self
.MessageDialog("Edit preferences is not implemented yet.", "Not implemented.")
322 def OnHelpAbout(self
, event
):
323 self
.MessageDialog(ABOUT_TEXT
, "About FrogEdit")
326 def Show(self
, show
):
327 wxFrame
.Show(self
, show
)
330 ##------------- Startup stuff
332 def LoadInitialFile(self
, fileName
):
333 if fileName
is not None:
334 if self
.OpenFile(fileName
) is FALSE
:
335 self
.OpenFileError(fileName
)
340 ##-------------- Application Launcher utility class
342 class FrogEditLauncher
:
344 def MakeAppFrame(self
):
345 return FrogEditFrame(None, -1, "FrogEdit", size
=(640, 480),
346 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
348 def GetArgvFilename(self
):
349 if len(sys
.argv
) > 1:
355 app
= wxPySimpleApp()
356 win
= self
.MakeAppFrame()
358 win
.LoadInitialFile(self
.GetArgvFilename())
362 ##-------------- Main program
365 if __name__
== '__main__':
367 launcher
= FrogEditLauncher()