- Factored out editor component.

This commit is contained in:
ccremers 2007-01-30 11:01:17 +00:00
parent 6f5846d09f
commit 17d1898b29
2 changed files with 90 additions and 43 deletions

82
gui/Gui/Editor.py Normal file
View File

@ -0,0 +1,82 @@
#!/usr/bin/python
#---------------------------------------------------------------------------
""" Import externals """
import wx
# Use Scintilla editor?
useStc = True # It looks nicer!
#useStc = False # It is sometimes buggy, claims the internet
# Test Scintilla and if it fails, get rid of it
if useStc:
try:
from wx.stc import *
except:
useStc = False
#---------------------------------------------------------------------------
""" Import scyther-gui components """
#---------------------------------------------------------------------------
""" Some constants """
#---------------------------------------------------------------------------
def selectEditor(parent):
"""
Pick an editor (Scintilla or default) and return the object.
"""
if useStc:
return EditorStc(parent)
else:
return EditorNormal(parent)
#---------------------------------------------------------------------------
class Editor(object):
def __init__(self, parent):
# Empty start
self.SetText("")
#---------------------------------------------------------------------------
class EditorNormal(Editor):
def __init__(self, parent):
self.control = wx.TextCtrl(parent, style=wx.TE_MULTILINE)
# Call parent
Editor.__init__(self,parent)
def GetText(self):
return self.control.GetValue()
def SetText(self, txt):
self.control.SetValue(txt)
#---------------------------------------------------------------------------
class EditorStc(Editor):
def __init__(self, parent):
# Scintilla layout with line numbers
self.control = StyledTextCtrl(parent)
self.control.SetMarginType(1, STC_MARGIN_NUMBER)
self.control.SetMarginWidth(1, 30)
# Call parent
Editor.__init__(self,parent)
def GetText(self):
return self.control.GetText()
def SetText(self, txt):
self.control.SetText(txt)
#---------------------------------------------------------------------------

View File

@ -6,17 +6,6 @@
import wx
import os.path
# Use Scintilla editor?
useStc = True # It looks nicer!
#useStc = False # It is sometimes buggy!
# Test Scintilla and if it fails, get rid of it
if useStc:
try:
import wx.stc as stc
except:
useStc = False
#---------------------------------------------------------------------------
""" Import scyther-gui components """
@ -24,6 +13,7 @@ import Settingswindow
import Scytherthread
import Icon
import About
import Editor
#---------------------------------------------------------------------------
@ -86,24 +76,6 @@ class MainWindow(wx.Frame):
self.firstCommand()
def GetText(self):
"""
Interface for both types of editor
"""
if useStc:
return self.control.GetText()
else:
return self.control.GetValue()
def SetText(self,txt):
"""
Interface for both types of editor
"""
if useStc:
self.control.SetText(txt)
else:
self.control.SetValue(txt)
def CreateInteriorWindowComponents(self):
''' Create "interior" window components. In this case it is just a
simple multiline text control. '''
@ -121,22 +93,15 @@ class MainWindow(wx.Frame):
# Top: input
self.top = wx.Notebook(self,-1)
if useStc:
# Scintilla layout with line numbers
self.control = stc.StyledTextCtrl(self.top)
self.control.SetMarginType(1, stc.STC_MARGIN_NUMBER)
self.control.SetMarginWidth(1, 30)
else:
# Simpler default
self.control = wx.TextCtrl(self.top, style=wx.TE_MULTILINE)
# Editor there
self.editor = Editor.selectEditor(self.top)
if self.load:
textfile = open(os.path.join(self.dirname, self.filename), 'r')
self.SetText(textfile.read())
self.editor.SetText(textfile.read())
os.chdir(self.dirname)
textfile.close()
self.top.AddPage(self.control,"Protocol description")
self.top.AddPage(self.editor.control,"Protocol description")
self.settings = Settingswindow.SettingsWindow(self.top,self)
self.top.AddPage(self.settings,"Settings")
@ -227,14 +192,14 @@ class MainWindow(wx.Frame):
def OnSave(self, event):
textfile = open(os.path.join(self.dirname, self.filename), 'w')
textfile.write(self.GetText())
textfile.write(self.editor.GetText())
textfile.close()
def OnOpen(self, event):
if self.askUserForFilename(style=wx.OPEN,
**self.defaultFileDialogOptions()):
textfile = open(os.path.join(self.dirname, self.filename), 'r')
self.SetText(textfile.read())
self.editor.SetText(textfile.read())
textfile.close()
def OnSaveAs(self, event):
@ -244,7 +209,7 @@ class MainWindow(wx.Frame):
os.chdir(self.dirname)
def RunScyther(self, mode):
spdl = self.GetText()
spdl = self.editor.GetText()
s = Scytherthread.ScytherRun(self,mode,spdl)
def OnVerify(self, event):