5 # Copyright 2001 Adam Feuer and Steve Howell
11 from wxPython
.wx
import *
12 from StatusBar
import *
13 from FrogEditor
import FrogEditor
16 ABOUT_TEXT
= """FrogEdit : Copyright 2001 Adam Feuer and Steve Howell
17 wxEditor component : Copyright 1999 - 2001 Dirk Holtwic, Robin Dunn, Adam Feuer, Steve Howell
19 FrogEdit was built using Python, wxPython, and wxWindows."""
22 ##---------------------------------------------------------------------
25 line
= string
.split(line
,'\n')[0]
26 return string
.split(line
,'\r')[0]
28 ##---------------------------------------------------------------------
30 class OutlinerPanel(wxPanel
):
32 def Close(self
, event
):
36 ##----------------------------------------------------------------------
39 class FrogEditFrame(wxFrame
):
40 def __init__(self
, parent
, ID
, title
, pos
=wxDefaultPosition
,
41 size
=wxDefaultSize
, style
=wxDEFAULT_FRAME_STYLE
):
43 wxFrame
.__init
__(self
, parent
, ID
, title
, pos
, size
, style
)
45 splitter
= wxSplitterWindow(self
, -1, style
=wxNO_3D|wxSP_3D
)
46 win
= OutlinerPanel(splitter
, -1, style
=wxCLIP_CHILDREN
)
48 log
= self
.MakeLogWindow(splitter
)
51 self
.MakeEditorWindow(win
, log
)
52 self
.SetUpSplitter(splitter
, win
, log
)
54 self
.MakeMainWindow(win
)
55 self
.RegisterEventHandlers()
61 ##------------- Init Misc
63 def RegisterEventHandlers(self
):
64 EVT_CLOSE(self
,self
.OnCloseWindow
)
66 def InitVariables(self
):
68 self
.edl
.UnTouchBuffer()
71 self
.MainMenu
= wxMenuBar()
72 self
.AddMenus(self
.MainMenu
)
73 self
.SetMenuBar(self
.MainMenu
)
75 ##------------- Init Subwindows
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
)
83 def MakeStatusbar(self
):
84 self
.sb
= CustomStatusBar(self
)
85 self
.SetStatusBar(self
.sb
)
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())
94 def SetUpSplitter(self
, splitter
, win
, log
):
95 splitter
.SplitHorizontally(win
, log
)
96 splitter
.SetSashPosition(360, True)
97 splitter
.SetMinimumPaneSize(40)
99 def MakeToolbar(self
, win
):
100 toolbarBox
= wxBoxSizer(wxHORIZONTAL
)
101 self
.AddButtons(win
, toolbarBox
)
104 def MakeMainWindow(self
, win
):
105 mainBox
= wxBoxSizer(wxVERTICAL
)
106 mainBox
.Add(self
.MakeToolbar(win
))
108 mainBox
.Add(self
.edl
, 1, wxALL|wxGROW
, borderWidth
)
109 win
.SetSizer(mainBox
)
110 win
.SetAutoLayout(True)
112 ##-------------- Init Menus
114 # override this to add more menus
115 def AddMenus(self
, menu
):
116 self
.AddFileMenu(menu
)
117 self
.AddEditMenu(menu
)
118 self
.AddHelpMenu(menu
)
120 def AddMenuItem(self
, menu
, itemText
, itemDescription
, itemHandler
):
122 menu
.Append(menuId
, itemText
, itemDescription
)
123 EVT_MENU(self
, menuId
, itemHandler
)
126 def AddFileMenu(self
, menu
):
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')
135 def AddEditMenu(self
, menu
):
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')
143 def AddHelpMenu(self
, menu
):
145 self
.AddMenuItem(helpMenu
, 'About', 'About the program', self
.OnHelpAbout
)
146 menu
.Append(helpMenu
, 'Help')
148 ##---------------- Init Buttons
151 def NewButton(self
, window
, container
, name
, pos
, size
, handler
):
153 if pos
== None or size
== None:
154 container
.Add(wxButton(window
, buttonId
, name
), 0, 0)
156 container
.Add(wxButton(window
, buttonId
, name
, pos
, size
), 0, 0)
157 EVT_BUTTON(self
, buttonId
, handler
)
160 # override this to make more buttons
161 def AddButtons(self
, window
, container
):
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
)
169 ##-------------- Init Dialogs
171 def MessageDialog(self
, text
, title
):
172 messageDialog
= wxMessageDialog(self
, text
, title
, wxOK | wxICON_INFORMATION
)
173 messageDialog
.ShowModal()
174 messageDialog
.Destroy()
176 def OkCancelDialog(self
, text
, title
):
177 dialog
= wxMessageDialog(self
, text
, title
, wxOK | wxCANCEL | wxICON_INFORMATION
)
178 result
= dialog
.ShowModal()
180 if result
== wxID_OK
:
185 def SelectFileDialog(self
, defaultDir
=None, defaultFile
=None, wildCard
=None):
186 if defaultDir
== None:
188 if defaultFile
== 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
)
201 def OpenFileError(self
, fileName
):
202 wxLogMessage('Open file error.')
203 self
.MessageDialog("Error opening file '%s'!" % fileName
, "Error")
206 def SaveFileError(self
, fileName
):
207 wxLogMessage('Save file error.')
208 self
.MessageDialog("Error saving file '%s'!" % fileName
, "Error")
210 ##---------------- Utility functions
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
221 def SetAltFuncs(self
, action
):
222 FrogEditor
.SetAltFuncs(self
.edl
, action
)
223 action
['x'] = self
.OnFileExit
225 def GetCurrentDir(self
):
226 if self
.fileName
is not None:
227 return os
.path
.split(self
.fileName
)[0]
230 def GetFileName(self
):
231 if self
.fileName
is not None:
232 return os
.path
.split(self
.fileName
)[1]
236 self
.edl
.SetText([""])
238 self
.sb
.setFileName("")
240 def SaveFile(self
, fileName
):
242 contents
= string
.join(self
.edl
.GetText(), '\n')
243 f
= open(fileName
, 'w')
246 self
.edl
.UnTouchBuffer()
247 self
.sb
.setFileName(fileName
)
252 def OpenFile(self
, fileName
):
254 f
= open(fileName
, 'r')
255 contents
= f
.readlines()
257 contents
= [chomp(line
) for line
in contents
]
258 if len(contents
) == 0:
260 self
.edl
.SetText(contents
)
261 self
.fileName
= fileName
262 self
.sb
.setFileName(fileName
)
269 ##---------------- Event handlers
272 def OnCloseWindow(self
, event
):
273 self
.edl
.OnCloseWindow(event
)
276 def OnNewFile(self
, event
):
277 if self
.edl
.BufferWasTouched():
278 if not self
.OkCancelDialog("New file - abandon changes?", "New File"):
283 def OnOpenFile(self
, event
):
284 if self
.edl
.BufferWasTouched():
285 if not self
.OkCancelDialog("Open file - abandon changes?", "Open File"):
287 fileName
= self
.SelectFileDialog(self
.GetCurrentDir())
288 if fileName
is not None:
289 if self
.OpenFile(fileName
) is False:
290 self
.OpenFileError(fileName
)
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
)
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
)
310 def OnFileExit(self
, event
):
311 if self
.edl
.BufferWasTouched():
312 if not self
.OkCancelDialog("Exit program - abandon changes?", "Exit"):
314 self
.OnCloseWindow(event
)
316 def OnEditPreferences(self
, event
):
317 self
.MessageDialog("Edit preferences is not implemented yet.", "Not implemented.")
320 def OnHelpAbout(self
, event
):
321 self
.MessageDialog(ABOUT_TEXT
, "About FrogEdit")
324 def Show(self
, show
):
325 wxFrame
.Show(self
, show
)
328 ##------------- Startup stuff
330 def LoadInitialFile(self
, fileName
):
331 if fileName
is not None:
332 if self
.OpenFile(fileName
) is False:
333 self
.OpenFileError(fileName
)
338 ##-------------- Application Launcher utility class
340 class FrogEditLauncher
:
342 def MakeAppFrame(self
):
343 return FrogEditFrame(None, -1, "FrogEdit", size
=(640, 480),
344 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
346 def GetArgvFilename(self
):
347 if len(sys
.argv
) > 1:
353 app
= wxPySimpleApp()
354 win
= self
.MakeAppFrame()
356 win
.LoadInitialFile(self
.GetArgvFilename())
360 ##-------------- Main program
363 if __name__
== '__main__':
365 launcher
= FrogEditLauncher()