5 # Copyright 2001 Adam Feuer and Steve Howell
16 from StatusBar
import *
17 from FrogEditor
import FrogEditor
20 ABOUT_TEXT
= """FrogEdit : Copyright 2001 Adam Feuer and Steve Howell
21 wxEditor component : Copyright 1999 - 2001 Dirk Holtwic, Robin Dunn, Adam Feuer, Steve Howell
23 FrogEdit was built using Python, wxPython, and wxWindows."""
26 ##---------------------------------------------------------------------
29 line
= string
.split(line
,'\n')[0]
30 return string
.split(line
,'\r')[0]
32 ##---------------------------------------------------------------------
34 class OutlinerPanel(wx
.Panel
):
36 def Close(self
, event
):
40 ##----------------------------------------------------------------------
43 class FrogEditFrame(wx
.Frame
):
44 def __init__(self
, parent
, ID
, title
, pos
=wx
.DefaultPosition
,
45 size
=wx
.DefaultSize
, style
=wx
.DEFAULT_FRAME_STYLE
):
47 wx
.Frame
.__init
__(self
, parent
, ID
, title
, pos
, size
, style
)
49 splitter
= wx
.SplitterWindow(self
, -1, style
=wx
.NO_3D|wx
.SP_3D
)
50 win
= OutlinerPanel(splitter
, -1, style
=wx
.CLIP_CHILDREN
)
52 log
= self
.MakeLogWindow(splitter
)
55 self
.MakeEditorWindow(win
, log
)
56 self
.SetUpSplitter(splitter
, win
, log
)
58 self
.MakeMainWindow(win
)
59 self
.RegisterEventHandlers()
65 ##------------- Init Misc
67 def RegisterEventHandlers(self
):
68 self
.Bind(wx
.EVT_CLOSE
,self
.OnCloseWindow
)
70 def InitVariables(self
):
72 self
.edl
.UnTouchBuffer()
75 self
.MainMenu
= wx
.MenuBar()
76 self
.AddMenus(self
.MainMenu
)
77 self
.SetMenuBar(self
.MainMenu
)
79 ##------------- Init Subwindows
81 def MakeEditorWindow(self
, win
, log
):
82 self
.edl
= FrogEditor(win
, -1, style
=wx
.SUNKEN_BORDER
, statusBar
= self
.sb
)
83 self
.edl
.SetControlFuncs
= self
.SetControlFuncs
84 self
.edl
.SetAltFuncs
= self
.SetAltFuncs
85 self
.edl
.SetStatus(log
)
87 def MakeStatusbar(self
):
88 self
.sb
= CustomStatusBar(self
)
89 self
.SetStatusBar(self
.sb
)
91 def MakeLogWindow(self
, container
):
92 log
= wx
.TextCtrl(container
, -1,
93 style
= wx
.TE_MULTILINE|wx
.TE_READONLY|wx
.HSCROLL
)
94 wx
.Log_SetActiveTarget(wx
.LogTextCtrl(log
))
95 wx
.LogMessage('window handle: %s' % self
.GetHandle())
98 def SetUpSplitter(self
, splitter
, win
, log
):
99 splitter
.SplitHorizontally(win
, log
)
100 splitter
.SetSashPosition(360, True)
101 splitter
.SetMinimumPaneSize(40)
103 def MakeToolbar(self
, win
):
104 toolbarBox
= wx
.BoxSizer(wx
.HORIZONTAL
)
105 self
.AddButtons(win
, toolbarBox
)
108 def MakeMainWindow(self
, win
):
109 mainBox
= wx
.BoxSizer(wx
.VERTICAL
)
110 mainBox
.Add(self
.MakeToolbar(win
))
112 mainBox
.Add(self
.edl
, 1, wx
.ALL|wx
.GROW
, borderWidth
)
113 win
.SetSizer(mainBox
)
114 win
.SetAutoLayout(True)
116 ##-------------- Init Menus
118 # override this to add more menus
119 def AddMenus(self
, menu
):
120 self
.AddFileMenu(menu
)
121 self
.AddEditMenu(menu
)
122 self
.AddHelpMenu(menu
)
124 def AddMenuItem(self
, menu
, itemText
, itemDescription
, itemHandler
):
126 menu
.Append(menuId
, itemText
, itemDescription
)
127 self
.Bind(wx
.EVT_MENU
, itemHandler
, id=menuId
)
130 def AddFileMenu(self
, menu
):
132 self
.AddMenuItem(fileMenu
, '&New File\tCtrl-N', 'New File', self
.OnNewFile
)
133 self
.AddMenuItem(fileMenu
, '&Open File\tCtrl-O', 'Open File', self
.OnOpenFile
)
134 self
.AddMenuItem(fileMenu
, '&Save File\tCtrl-S', 'Save File', self
.OnSaveFile
)
135 self
.AddMenuItem(fileMenu
, 'Save File &As\tCtrl-A', 'Save File As',self
.OnSaveFileAs
)
136 self
.AddMenuItem(fileMenu
, 'E&xit\tAlt-X', 'Exit', self
.OnFileExit
)
137 menu
.Append(fileMenu
, 'File')
139 def AddEditMenu(self
, menu
):
141 self
.AddMenuItem(editMenu
, 'Cut\tCtrl-X', 'Cut', self
.edl
.OnCutSelection
)
142 self
.AddMenuItem(editMenu
, '&Copy\tCtrl-C', 'Copy', self
.edl
.OnCopySelection
)
143 self
.AddMenuItem(editMenu
, 'Paste\tCtrl-V', 'Paste', self
.edl
.OnPaste
)
144 self
.AddMenuItem(editMenu
, 'Edit preferences', 'Edit Preferences', self
.OnEditPreferences
)
145 menu
.Append(editMenu
, 'Edit')
147 def AddHelpMenu(self
, menu
):
149 self
.AddMenuItem(helpMenu
, 'About', 'About the program', self
.OnHelpAbout
)
150 menu
.Append(helpMenu
, 'Help')
152 ##---------------- Init Buttons
155 def NewButton(self
, window
, container
, name
, pos
, size
, handler
):
156 buttonId
= wx
.NewId()
157 if pos
== None or size
== None:
158 container
.Add(wx
.Button(window
, buttonId
, name
), 0, 0)
160 container
.Add(wx
.Button(window
, buttonId
, name
, pos
, size
), 0, 0)
161 self
.Bind(wx
.EVT_BUTTON
, handler
, id=buttonId
)
164 # override this to make more buttons
165 def AddButtons(self
, window
, container
):
168 self
.NewButton(window
, container
, "New", buttonPos
, buttonSize
, self
.OnNewFile
)
169 self
.NewButton(window
, container
, "Open", buttonPos
, buttonSize
, self
.OnOpenFile
)
170 self
.NewButton(window
, container
, "Save", buttonPos
, buttonSize
, self
.OnSaveFile
)
173 ##-------------- Init Dialogs
175 def MessageDialog(self
, text
, title
):
176 messageDialog
= wx
.MessageDialog(self
, text
, title
, wx
.OK | wx
.ICON_INFORMATION
)
177 messageDialog
.ShowModal()
178 messageDialog
.Destroy()
180 def OkCancelDialog(self
, text
, title
):
181 dialog
= wx
.MessageDialog(self
, text
, title
, wx
.OK | wx
.CANCEL | wx
.ICON_INFORMATION
)
182 result
= dialog
.ShowModal()
184 if result
== wx
.ID_OK
:
189 def SelectFileDialog(self
, defaultDir
=None, defaultFile
=None, wildCard
=None):
190 if defaultDir
== None:
192 if defaultFile
== None:
197 fileDialog
= wx
.FileDialog(self
, "Choose a file", defaultDir
, defaultFile
, wildCard
, wx
.OPEN|wx
.MULTIPLE
)
198 result
= fileDialog
.ShowModal()
199 if result
== wx
.ID_OK
:
200 fileName
= fileDialog
.GetPath()
201 wx
.LogMessage('You selected: %s\n' % fileName
)
205 def OpenFileError(self
, fileName
):
206 wx
.LogMessage('Open file error.')
207 self
.MessageDialog("Error opening file '%s'!" % fileName
, "Error")
210 def SaveFileError(self
, fileName
):
211 wx
.LogMessage('Save file error.')
212 self
.MessageDialog("Error saving file '%s'!" % fileName
, "Error")
214 ##---------------- Utility functions
217 def SetControlFuncs(self
, action
):
218 "for overriding editor's keys"
219 FrogEditor
.SetControlFuncs(self
.edl
, action
)
220 action
['a'] = self
.OnSaveFileAs
221 action
['o'] = self
.OnOpenFile
222 action
['n'] = self
.OnNewFile
223 action
['s'] = self
.OnSaveFile
225 def SetAltFuncs(self
, action
):
226 FrogEditor
.SetAltFuncs(self
.edl
, action
)
227 action
['x'] = self
.OnFileExit
229 def GetCurrentDir(self
):
230 if self
.fileName
is not None:
231 return os
.path
.split(self
.fileName
)[0]
234 def GetFileName(self
):
235 if self
.fileName
is not None:
236 return os
.path
.split(self
.fileName
)[1]
240 self
.edl
.SetText([""])
242 self
.sb
.setFileName("")
244 def SaveFile(self
, fileName
):
246 contents
= string
.join(self
.edl
.GetText(), '\n')
247 f
= open(fileName
, 'w')
250 self
.edl
.UnTouchBuffer()
251 self
.sb
.setFileName(fileName
)
256 def OpenFile(self
, fileName
):
258 f
= open(fileName
, 'r')
259 contents
= f
.readlines()
261 contents
= [chomp(line
) for line
in contents
]
262 if len(contents
) == 0:
264 self
.edl
.SetText(contents
)
265 self
.fileName
= fileName
266 self
.sb
.setFileName(fileName
)
273 ##---------------- Event handlers
276 def OnCloseWindow(self
, event
):
277 self
.edl
.OnCloseWindow(event
)
280 def OnNewFile(self
, event
):
281 if self
.edl
.BufferWasTouched():
282 if not self
.OkCancelDialog("New file - abandon changes?", "New File"):
287 def OnOpenFile(self
, event
):
288 if self
.edl
.BufferWasTouched():
289 if not self
.OkCancelDialog("Open file - abandon changes?", "Open File"):
291 fileName
= self
.SelectFileDialog(self
.GetCurrentDir())
292 if fileName
is not None:
293 if self
.OpenFile(fileName
) is False:
294 self
.OpenFileError(fileName
)
297 def OnSaveFile(self
, event
):
298 if self
.fileName
is None:
299 return self
.OnSaveFileAs(event
)
300 wx
.LogMessage("Saving %s..." % self
.fileName
)
301 if self
.SaveFile(self
.fileName
) is not True:
302 self
.SaveFileError(self
.fileName
)
305 def OnSaveFileAs(self
, event
):
306 fileName
= self
.SelectFileDialog(self
.GetCurrentDir(),self
.GetFileName())
307 if fileName
is not None:
308 self
.fileName
= fileName
309 wx
.LogMessage("Saving %s..." % self
.fileName
)
310 if self
.SaveFile(self
.fileName
) is not True:
311 self
.SaveFileError(self
.fileName
)
314 def OnFileExit(self
, event
):
315 if self
.edl
.BufferWasTouched():
316 if not self
.OkCancelDialog("Exit program - abandon changes?", "Exit"):
318 self
.OnCloseWindow(event
)
320 def OnEditPreferences(self
, event
):
321 self
.MessageDialog("Edit preferences is not implemented yet.", "Not implemented.")
324 def OnHelpAbout(self
, event
):
325 self
.MessageDialog(ABOUT_TEXT
, "About FrogEdit")
328 def Show(self
, show
):
329 wx
.Frame
.Show(self
, show
)
332 ##------------- Startup stuff
334 def LoadInitialFile(self
, fileName
):
335 if fileName
is not None:
336 if self
.OpenFile(fileName
) is False:
337 self
.OpenFileError(fileName
)
342 ##-------------- Application Launcher utility class
344 class FrogEditLauncher
:
346 def MakeAppFrame(self
):
347 return FrogEditFrame(None, -1, "FrogEdit", size
=(640, 480),
348 style
=wx
.DEFAULT_FRAME_STYLE|wx
.NO_FULL_REPAINT_ON_RESIZE
)
350 def GetArgvFilename(self
):
351 if len(sys
.argv
) > 1:
357 app
= wx
.PySimpleApp()
358 win
= self
.MakeAppFrame()
360 win
.LoadInitialFile(self
.GetArgvFilename())
364 ##-------------- Main program
367 if __name__
== '__main__':
369 launcher
= FrogEditLauncher()