[PyCrust-cvs] PyCrust frame.py,1.3,1.4 editor.py,1.7,1.8 base.py,1.2,1.3
Brought to you by:
pobrien
|
From: <po...@us...> - 2003-04-01 20:05:20
|
Update of /cvsroot/pycrust/PyCrust
In directory sc8-pr-cvs1:/tmp/cvs-serv1763
Modified Files:
frame.py editor.py base.py
Log Message:
Added PythonDocument class. Refactored.
Index: frame.py
===================================================================
RCS file: /cvsroot/pycrust/PyCrust/frame.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** frame.py 1 Apr 2003 15:46:52 -0000 1.3
--- frame.py 1 Apr 2003 20:05:03 -0000 1.4
***************
*** 294,298 ****
event.Enable(hasattr(self, 'FileClose'))
elif id == wx.wxID_SAVE:
! event.Enable(hasattr(self, 'FileSave') and self.editor.GetModify())
elif id == wx.wxID_SAVEAS:
event.Enable(hasattr(self, 'FileSaveAs'))
--- 294,298 ----
event.Enable(hasattr(self, 'FileClose'))
elif id == wx.wxID_SAVE:
! event.Enable(hasattr(self, 'FileSave') and self.doc.editor.GetModify())
elif id == wx.wxID_SAVEAS:
event.Enable(hasattr(self, 'FileSaveAs'))
***************
*** 334,337 ****
event.Enable(False)
except AttributeError:
! # object with keyboard focus doesn't support this menu option.
event.Enable(False)
--- 334,337 ----
event.Enable(False)
except AttributeError:
! # This menu option is not supported in the current context.
event.Enable(False)
Index: editor.py
===================================================================
RCS file: /cvsroot/pycrust/PyCrust/editor.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** editor.py 1 Apr 2003 16:48:24 -0000 1.7
--- editor.py 1 Apr 2003 20:05:08 -0000 1.8
***************
*** 25,42 ****
class PythonEditorFrame(frame.Frame):
! """Frame containing the Python editor notebook component."""
def __init__(self, parent=None, id=-1, title='PyAlaMode',
pos=wx.wxDefaultPosition, size=(600, 400),
! style=wx.wxDEFAULT_FRAME_STYLE):
"""Create an EditorFrame instance."""
frame.Frame.__init__(self, parent, id, title, pos, size, style)
! self.SetStatusText(title + ' - the tastiest Python editor.')
! self.notebook = PythonEditorNotebook(parent=self)
! self.shell = self.notebook.shell
! self.editor = self.notebook.editor
! self.interp = self.shell.interp
! self.lastPos = None
! self.lastStatus = ''
wx.EVT_IDLE(self, self.OnIdle)
--- 25,49 ----
class PythonEditorFrame(frame.Frame):
! """Frame containing one or more Python editor notebooks."""
def __init__(self, parent=None, id=-1, title='PyAlaMode',
pos=wx.wxDefaultPosition, size=(600, 400),
! style=wx.wxDEFAULT_FRAME_STYLE, single=True):
"""Create an EditorFrame instance."""
frame.Frame.__init__(self, parent, id, title, pos, size, style)
! self.single = single
! self.docs = {}
! self.doc = None # Current document.
! self.editor = None # Current document editor.
! self.SetTitle('PyAlaMode')
! self.statusText = title + ' - the tastiest Python editor.'
! self.SetStatusText(self.statusText)
!
! if self.single:
! self.notebook = PythonEditorNotebook(parent=self)
! editor = self.notebook.editor
! shell = self.notebook.shell
! interp = shell.interp
! self.doc = PythonDocument(editor, interp)
wx.EVT_IDLE(self, self.OnIdle)
***************
*** 52,57 ****
def OnCloseWindow(self, event):
"""Event handler for closing."""
! if self.editor.GetModify():
! self.FileSuggestSave()
self.Destroy()
--- 59,65 ----
def OnCloseWindow(self, event):
"""Event handler for closing."""
! for doc in self.docs.values():
! if doc.editor.GetModify():
! doc.FileSuggestSave()
self.Destroy()
***************
*** 59,104 ****
"""Event handler for idle time."""
self.updateStatusBar()
! self.updateTitleBar()
! time.sleep(0.05)
event.Skip()
def updateStatusBar(self):
! pos = self.editor.GetCurrentPos()
! line = self.editor.LineFromPosition(pos) + 1
! col = self.editor.GetColumn(pos) + 1
! status = 'PyAlaMode | File: %s | Line: %d | Column: %d' % \
! (self.filepath, line, col)
! if pos != self.lastPos and status != self.lastStatus:
! self.SetStatusText(status)
! self.lastPos = pos
! self.lastStatus = status
!
! def updateTitleBar(self):
! title = self.GetTitle()
! modified = self.editor.GetModify()
! if modified and title[0] != '*':
! self.SetTitle('* ' + title + ' *')
! elif not modified and title[0] == '*':
! self.SetTitle(title[2:-2])
def FileClose(self):
"""Close file in editor."""
! if self.editor.GetModify():
self.FileSuggestSave()
def FileNew(self):
"""Create new file in editor."""
! if self.editor.GetModify():
self.FileSuggestSave()
def FileOpen(self, filename=None):
"""Open file in editor."""
! if self.editor.GetModify():
self.FileSuggestSave()
if filename is None:
return # XXX Prompt for filename.
! self._setFileInfo(filename)
! self.editor.FileOpen(self.filepath)
! self.SetTitle(self.filename)
def FilePrint(self):
--- 67,112 ----
"""Event handler for idle time."""
self.updateStatusBar()
! ## self.updateTitleBar()
event.Skip()
def updateStatusBar(self):
! if self.doc:
! status = self.doc.getStatus()
! text = 'File: %s | Line: %d | Column: %d' % status
! if text != self.statusText:
! self.SetStatusText(text)
! self.statusText = text
+ ## def updateTitleBar(self):
+ ## title = self.GetTitle()
+ ## if self.editor.GetModify():
+ ## if title.startswith('* '):
+ ## pass
+ ## else:
+ ## self.SetTitle('* ' + title)
+ ## else:
+ ## if title.startswith('* '):
+ ## self.SetTitle(title[2:])
+ ## else:
+ ## self.SetTitle(self.filename + ' - PyAlaMode')
+
def FileClose(self):
"""Close file in editor."""
! if self.doc.editor.GetModify():
self.FileSuggestSave()
def FileNew(self):
"""Create new file in editor."""
! if self.doc.editor.GetModify():
self.FileSuggestSave()
def FileOpen(self, filename=None):
"""Open file in editor."""
! if self.doc and self.doc.editor.GetModify():
self.FileSuggestSave()
if filename is None:
return # XXX Prompt for filename.
! if filename:
! self.doc.open(filename)
def FilePrint(self):
***************
*** 112,125 ****
def FileSave(self):
"""Save file in editor."""
! self.editor.FileSave(self.filepath)
def FileSaveAs(self):
"""Save file in editor with new name."""
! if self.editor.GetModify():
self.FileSuggestSave()
# Get new name
filename = '' # XXX
! self._setFileInfo(filename)
! self.FileSave()
def FileSuggestSave(self):
--- 120,132 ----
def FileSave(self):
"""Save file in editor."""
! self.doc.save()
def FileSaveAs(self):
"""Save file in editor with new name."""
! if self.doc.editor.GetModify():
self.FileSuggestSave()
# Get new name
filename = '' # XXX
! ## self.doc.saveAs(filename)
def FileSuggestSave(self):
***************
*** 129,154 ****
self.FileSave()
! def _setFileInfo(self, filename):
! """Set file information."""
! self.filepath = os.path.abspath(filename)
! self.filedir, self.filename = os.path.split(self.filepath)
! self.modulename, self.fileext = os.path.splitext(self.filename)
! if self.filedir not in sys.path:
! sys.path.insert(0, self.filedir)
while True:
try:
! sys.path.remove('')
except ValueError:
break
while True:
try:
! sys.path.remove('.')
except ValueError:
break
def updateNamespace(self):
! """Update the namespace for autocompletion and calltips."""
if self.editor.GetModify():
! self.FileSave()
backup = self.interp.locals
modfile = None
--- 136,189 ----
self.FileSave()
! def updateNamespace(self):
! if self.doc.updateNamespace():
! self.SetStatusText('Namespace updated')
! else:
! self.SetStatusText('Error importing file, unable to update namespace')
!
!
! class PythonDocument:
! """Python Document class."""
!
! def __init__(self, editor, interp, filename=None):
! """Create a PythonDocument instance."""
! self.editor = editor
! self.interp = interp
! self.filename = 'Untitled'
! self.filepath = ''
! self.filedir = ''
! self.fileext = ''
! self.modulename = ''
! self.syspath = sys.path[:]
! self.sysmodules = sys.modules.copy()
while True:
try:
! self.syspath.remove('')
except ValueError:
break
while True:
try:
! self.syspath.remove('.')
except ValueError:
break
+ if filename:
+ self._setFileInfo(filename)
+
+ def _setFileInfo(self, filename):
+ """Set file information."""
+ if not filename:
+ raise AttributeError
+ self.filepath = os.path.abspath(filename)
+ self.filedir, self.filename = os.path.split(self.filepath)
+ self.modulename, self.fileext = os.path.splitext(self.filename)
+ if self.filedir not in self.syspath:
+ self.syspath.insert(0, self.filedir)
def updateNamespace(self):
! """Update the namespace for autocompletion and calltips.
!
! Return False if there was an error."""
if self.editor.GetModify():
! self.save()
backup = self.interp.locals
modfile = None
***************
*** 159,166 ****
except:
self.interp.locals = backup
! self.SetStatusText('Error importing file, unable to update namespace')
else:
self.interp.locals = module.__dict__.copy()
! self.SetStatusText('Namespace updated')
finally:
if modfile:
--- 194,201 ----
except:
self.interp.locals = backup
! return False
else:
self.interp.locals = module.__dict__.copy()
! return True
finally:
if modfile:
***************
*** 171,262 ****
pass
!
! class PythonEditorNotebook(wx.wxNotebook):
! """Combines a Python code editor with a shell."""
!
! def __init__(self, parent):
! """Create a PythonEditorNotebook instance."""
! wx.wxNotebook.__init__(self, parent, id=-1)
! usePanels = True
! if usePanels:
! shellpanel = wx.wxPanel(self, -1)
! editorpanel = wx.wxPanel(self, -1)
! self.shell = Shell(parent=shellpanel,
! style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER)
! self.editor = PythonEditor(interp=self.shell.interp,
! parent=editorpanel)
! self.AddPage(page=editorpanel, text='File', select=True)
! self.AddPage(page=shellpanel, text='Shell')
! # Setup sizers
! shellsizer = wx.wxBoxSizer(wx.wxVERTICAL)
! shellsizer.Add(self.shell, 1, wx.wxEXPAND)
! shellpanel.SetSizer(shellsizer)
! shellpanel.SetAutoLayout(True)
! editorsizer = wx.wxBoxSizer(wx.wxVERTICAL)
! editorsizer.Add(self.editor, 1, wx.wxEXPAND)
! editorpanel.SetSizer(editorsizer)
! editorpanel.SetAutoLayout(True)
! else:
! self.shell = Shell(parent=self,
! style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER)
! self.editor = PythonEditor(interp=self.shell.interp,
! parent=self)
! self.shell.interp.locals['editor'] = self.editor
! self.AddPage(page=self.editor, text='File', select=True)
! self.AddPage(page=self.shell, text='Shell')
! # Set focus to the editor.
! self.editor.SetFocus()
!
!
! class PythonEditor(base.Editor):
! """Editor based on StyledTextCtrl."""
!
! def __init__(self, interp, parent, id=-1, pos=wx.wxDefaultPosition,
! size=wx.wxDefaultSize,
! style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER):
! """Create a PythonEditor instance."""
! base.Editor.__init__(self, parent, id, pos, size, style)
! self.confirmed = False
! self.interp = interp
! # Find out for which keycodes the interpreter will autocomplete.
! self.autoCompleteKeys = self.interp.getAutoCompleteKeys()
! # Assign handlers for keyboard events.
! wx.EVT_CHAR(self, self.OnChar)
! wx.EVT_KEY_DOWN(self, self.OnKeyDown)
!
! def FileNew(self, filepath):
! """New file."""
if not filepath:
return
if os.path.exists(filepath):
! self.confirmed = self.FileOverwriteConfirm(filepath)
else:
self.confirmed = True
! def FileOpen(self, filepath):
"""Open file."""
! if not filepath:
! return
! if os.path.exists(filepath):
! self.FileReadText(filepath)
self.confirmed = True
else:
! self.FileNew(filepath)
! def FileOverwriteConfirm(filepath):
"""Confirm overwriting an existing file."""
return False
! def FileReadText(self, filepath):
! """Replace text with contents of file."""
f = file(filepath, 'rb')
try:
! self.SetText(f.read())
! self.EmptyUndoBuffer()
finally:
f.close()
! def FileSave(self, filepath):
! """Save file."""
if not filepath:
return
--- 206,243 ----
pass
! def new(self, filepath):
! """New document."""
if not filepath:
return
if os.path.exists(filepath):
! self.confirmed = self.overwriteConfirm(filepath)
else:
self.confirmed = True
! def open(self, filename):
"""Open file."""
! self._setFileInfo(filename)
! if os.path.exists(self.filepath):
! self.read(self.filepath)
self.confirmed = True
else:
! self.new(self.filepath)
! def overwriteConfirm(filepath):
"""Confirm overwriting an existing file."""
return False
! def read(self, filepath):
! """Replace editor text with contents of file."""
f = file(filepath, 'rb')
try:
! self.editor.SetText(f.read())
! self.editor.EmptyUndoBuffer()
finally:
f.close()
! def save(self):
! """Save document."""
! filepath = self.filepath
if not filepath:
return
***************
*** 264,280 ****
self.confirmed = True
if not self.confirmed:
! self.confirmed = self.FileOverwriteConfirm(filepath)
if self.confirmed:
! self.FileWriteText(filepath)
! def FileWriteText(self, filepath):
! """Write all text to file."""
try:
f = file(filepath, 'w')
! f.write(self.GetText())
! self.SetSavePoint()
finally:
f.close()
def OnChar(self, event):
"""Keypress event handler.
--- 245,287 ----
self.confirmed = True
if not self.confirmed:
! self.confirmed = self.overwriteConfirm(filepath)
if self.confirmed:
! self.write(filepath)
! def write(self, filepath):
! """Write all editor text to file."""
try:
f = file(filepath, 'w')
! f.write(self.editor.GetText())
! self.editor.SetSavePoint()
finally:
f.close()
+ def getStatus(self):
+ """Return (filepath, line, column) tuple."""
+ editor = self.editor
+ pos = editor.GetCurrentPos()
+ line = editor.LineFromPosition(pos) + 1
+ col = editor.GetColumn(pos) + 1
+ status = (self.filepath, line, col)
+ return status
+
+
+ class PythonEditor(base.Editor):
+ """Editor based on StyledTextCtrl."""
+
+ def __init__(self, interp, parent, id=-1, pos=wx.wxDefaultPosition,
+ size=wx.wxDefaultSize,
+ style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER):
+ """Create a PythonEditor instance."""
+ base.Editor.__init__(self, parent, id, pos, size, style)
+ self.confirmed = False
+ self.interp = interp
+ # Find out for which keycodes the interpreter will autocomplete.
+ self.autoCompleteKeys = self.interp.getAutoCompleteKeys()
+ # Assign handlers for keyboard events.
+ wx.EVT_CHAR(self, self.OnChar)
+ wx.EVT_KEY_DOWN(self, self.OnKeyDown)
+
def OnChar(self, event):
"""Keypress event handler.
***************
*** 369,370 ****
--- 376,414 ----
self.CallTipShow(tippos, tip)
self.CallTipSetHighlight(0, size)
+
+
+ class PythonEditorNotebook(wx.wxNotebook):
+ """Combines a Python code editor with a shell."""
+
+ def __init__(self, parent):
+ """Create a PythonEditorNotebook instance."""
+ wx.wxNotebook.__init__(self, parent, id=-1)
+ usePanels = True
+ if usePanels:
+ shellpanel = wx.wxPanel(self, -1)
+ editorpanel = wx.wxPanel(self, -1)
+ self.shell = Shell(parent=shellpanel,
+ style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER)
+ self.editor = PythonEditor(interp=self.shell.interp,
+ parent=editorpanel)
+ self.AddPage(page=editorpanel, text='File', select=True)
+ self.AddPage(page=shellpanel, text='Shell')
+ # Setup sizers
+ shellsizer = wx.wxBoxSizer(wx.wxVERTICAL)
+ shellsizer.Add(self.shell, 1, wx.wxEXPAND)
+ shellpanel.SetSizer(shellsizer)
+ shellpanel.SetAutoLayout(True)
+ editorsizer = wx.wxBoxSizer(wx.wxVERTICAL)
+ editorsizer.Add(self.editor, 1, wx.wxEXPAND)
+ editorpanel.SetSizer(editorsizer)
+ editorpanel.SetAutoLayout(True)
+ else:
+ self.shell = Shell(parent=self,
+ style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER)
+ self.editor = PythonEditor(interp=self.shell.interp,
+ parent=self)
+ self.shell.interp.locals['editor'] = self.editor
+ self.AddPage(page=self.editor, text='File', select=True)
+ self.AddPage(page=self.shell, text='Shell')
+ # Set focus to the editor.
+ self.editor.SetFocus()
Index: base.py
===================================================================
RCS file: /cvsroot/pycrust/PyCrust/base.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** base.py 1 Apr 2003 06:43:05 -0000 1.2
--- base.py 1 Apr 2003 20:05:10 -0000 1.3
***************
*** 52,62 ****
"""Create an Editor instance."""
stc.wxStyledTextCtrl.__init__(self, parent, id, pos, size, style)
! # Assign handlers for wxSTC events.
stc.EVT_STC_UPDATEUI(self, id, self.OnUpdateUI)
dispatcher.connect(receiver=self._fontsizer, signal='FontIncrease')
dispatcher.connect(receiver=self._fontsizer, signal='FontDecrease')
dispatcher.connect(receiver=self._fontsizer, signal='FontDefault')
- # Configure various defaults and user preferences.
- self.__config()
def _fontsizer(self, signal):
--- 52,60 ----
"""Create an Editor instance."""
stc.wxStyledTextCtrl.__init__(self, parent, id, pos, size, style)
! self.__config()
stc.EVT_STC_UPDATEUI(self, id, self.OnUpdateUI)
dispatcher.connect(receiver=self._fontsizer, signal='FontIncrease')
dispatcher.connect(receiver=self._fontsizer, signal='FontDecrease')
dispatcher.connect(receiver=self._fontsizer, signal='FontDefault')
def _fontsizer(self, signal):
***************
*** 154,158 ****
"""Check for matching braces."""
# If the auto-complete window is up let it do its thing.
! if self.AutoCompActive():
return
braceAtCaret = -1
--- 152,156 ----
"""Check for matching braces."""
# If the auto-complete window is up let it do its thing.
! if self.AutoCompActive() or self.CallTipActive():
return
braceAtCaret = -1
|