scyther/gui/Gui/Mainwindow.py

294 lines
9.8 KiB
Python
Raw Normal View History

2006-08-02 13:59:57 +01:00
#!/usr/bin/python
#---------------------------------------------------------------------------
""" Import externals """
import wx
import os.path
#---------------------------------------------------------------------------
""" Import scyther-gui components """
2006-08-11 09:34:22 +01:00
import Settingswindow
2006-08-02 13:59:57 +01:00
import Scytherthread
import Icon
2006-08-11 11:43:28 +01:00
import About
2007-01-30 11:01:17 +00:00
import Editor
2006-08-02 13:59:57 +01:00
#---------------------------------------------------------------------------
""" Some constants """
ID_VERIFY = 100
ID_AUTOVERIFY = 101
ID_CHARACTERIZE = 102
2006-08-02 13:59:57 +01:00
ID_CHECK = 103
#---------------------------------------------------------------------------
class MainWindow(wx.Frame):
def __init__(self, opts, args):
2006-08-02 13:59:57 +01:00
super(MainWindow, self).__init__(None, size=(600,800))
self.opts = opts
self.args = args
self.dirname = os.path.abspath('.')
2006-08-02 13:59:57 +01:00
self.filename = 'noname.spdl'
self.load = False
# test
if opts.test:
self.filename = 'scythergui-default.spdl'
2006-08-02 13:59:57 +01:00
self.load = True
# if there is an argument (file), we load it
if len(args) > 0:
filename = args[0]
if filename != '' and os.path.isfile(filename):
(self.dirname,self.filename) = os.path.split(filename)
self.load = True
2006-08-02 13:59:57 +01:00
Icon.ScytherIcon(self)
self.CreateInteriorWindowComponents()
self.CreateExteriorWindowComponents()
aTable = wx.AcceleratorTable([
2006-08-10 15:42:51 +01:00
(wx.ACCEL_CTRL, ord('Q'), wx.ID_EXIT),
2006-08-02 13:59:57 +01:00
(wx.ACCEL_NORMAL, wx.WXK_F1,
ID_VERIFY),
(wx.ACCEL_NORMAL, wx.WXK_F2,
ID_CHARACTERIZE),
2006-08-02 13:59:57 +01:00
(wx.ACCEL_NORMAL, wx.WXK_F5,
ID_CHECK),
(wx.ACCEL_NORMAL, wx.WXK_F6,
ID_AUTOVERIFY),
])
self.SetAcceleratorTable(aTable)
self.claimlist = []
self.pnglist = []
#self.SetTitle(self.title)
self.firstCommand()
2006-08-02 13:59:57 +01:00
def CreateInteriorWindowComponents(self):
''' Create "interior" window components. In this case it is just a
simple multiline text control. '''
2006-08-09 11:21:30 +01:00
## Make zoom buttons
#sizer = wx.BoxSizer(wx.VERTICAL)
#buttons = wx.BoxSizer(wx.HORIZONTAL)
#bt = wx.Button(self,ID_VERIFY)
#buttons.Add(bt,0)
#self.Bind(wx.EVT_BUTTON, self.OnVerify, bt)
#bt = wx.Button(self,ID_CHARACTERIZE)
2006-08-09 11:21:30 +01:00
#buttons.Add(bt,0)
#self.Bind(wx.EVT_BUTTON, self.OnCharacterize, bt)
2006-08-09 11:21:30 +01:00
#sizer.Add(buttons, 0, wx.ALIGN_LEFT)
2006-08-02 13:59:57 +01:00
# Top: input
2006-08-03 13:06:43 +01:00
self.top = wx.Notebook(self,-1)
2007-01-30 11:01:17 +00:00
# Editor there
self.editor = Editor.selectEditor(self.top)
2007-01-29 16:32:35 +00:00
2006-08-02 13:59:57 +01:00
if self.load:
textfile = open(os.path.join(self.dirname, self.filename), 'r')
2007-01-30 11:01:17 +00:00
self.editor.SetText(textfile.read())
if self.dirname != "":
os.chdir(self.dirname)
2006-08-02 13:59:57 +01:00
textfile.close()
self.editor.SetOpened()
2007-01-30 11:01:17 +00:00
self.top.AddPage(self.editor.control,"Protocol description")
2006-08-11 09:34:22 +01:00
self.settings = Settingswindow.SettingsWindow(self.top,self)
2006-08-11 15:22:45 +01:00
self.top.AddPage(self.settings,"Settings")
2006-08-02 13:59:57 +01:00
2006-08-09 11:21:30 +01:00
#sizer.Add(self.top,1,wx.EXPAND,1)
#self.SetSizer(sizer)
2006-08-02 13:59:57 +01:00
def CreateExteriorWindowComponents(self):
''' Create "exterior" window components, such as menu and status
bar. '''
self.CreateMenus()
self.SetTitle()
def CreateMenu(self, bar, name, list):
fileMenu = wx.Menu()
for id, label, helpText, handler in list:
if id == None:
fileMenu.AppendSeparator()
else:
item = fileMenu.Append(id, label, helpText)
self.Bind(wx.EVT_MENU, handler, item)
bar.Append(fileMenu, name) # Add the fileMenu to the MenuBar
def CreateMenus(self):
menuBar = wx.MenuBar()
self.CreateMenu(menuBar, '&File', [
(wx.ID_OPEN, '&Open', 'Open a new file', self.OnOpen),
(wx.ID_SAVE, '&Save', 'Save the current file', self.OnSave),
(wx.ID_SAVEAS, 'Save &As', 'Save the file under a different name',
self.OnSaveAs),
(None, None, None, None),
2006-08-10 15:42:51 +01:00
(wx.ID_EXIT, 'E&xit\tCTRL-Q', 'Terminate the program',
2006-08-02 13:59:57 +01:00
self.OnExit)])
self.CreateMenu(menuBar, '&Verify',
[(ID_VERIFY, '&Verify protocol\tF1','Verify the protocol in the buffer using Scyther',
self.OnVerify) ,
(ID_CHARACTERIZE, '&Characterize roles\tF2','TODO' ,
self.OnCharacterize) ,
2006-08-02 13:59:57 +01:00
(None, None, None, None),
2006-08-11 11:43:28 +01:00
### Disabled for now (given that it is not reliable enough yet)
#(ID_CHECK, '&Check protocol\tF5','TODO',
# self.OnCheck) ,
2006-08-02 13:59:57 +01:00
(ID_AUTOVERIFY, 'Verify &automatic claims\tF6','TODO',
2006-08-09 10:41:14 +01:00
self.OnAutoVerify)
2006-08-02 13:59:57 +01:00
])
self.CreateMenu(menuBar, '&Help',
[(wx.ID_ABOUT, '&About', 'Information about this program',
self.OnAbout) ])
self.SetMenuBar(menuBar) # Add the menuBar to the Frame
def SetTitle(self):
# MainWindow.SetTitle overrides wx.Frame.SetTitle, so we have to
# call it using super:
2006-08-09 11:21:30 +01:00
super(MainWindow, self).SetTitle('Scyther: %s'%self.filename)
2006-08-02 13:59:57 +01:00
# Helper methods:
def defaultFileDialogOptions(self):
''' Return a dictionary with file dialog options that can be
used in both the save file dialog as well as in the open
file dialog. '''
return dict(message='Choose a file', defaultDir=self.dirname,
wildcard='*.spdl')
def askUserForFilename(self, **dialogOptions):
dialog = wx.FileDialog(self, **dialogOptions)
if dialog.ShowModal() == wx.ID_OK:
userProvidedFilename = True
self.filename = dialog.GetFilename()
self.dirname = dialog.GetDirectory()
self.SetTitle() # Update the window title with the new filename
else:
userProvidedFilename = False
dialog.Destroy()
return userProvidedFilename
# Are we dropping a changed file?
def ConfirmLoss(self,text=None):
"""
Try to drop the current file. If it was changed, try to save
(as)
Returns true after the user seems to be happy either way, false
if we need to cancel this.
"""
if self.editor.GetChanged():
# File changed, we need to confirm this
title = "Unsaved changes"
if text:
title = "%s - " + title
txt = "The protocol file '%s' has been modified.\n\n" % (self.filename)
txt = txt + "Do you want to"
txt = txt + " save your changes (Yes)"
txt = txt + " or"
txt = txt + " discard them (No)"
txt = txt + "?"
dialog = wx.MessageDialog(self,txt,title,wx.YES_NO | wx.CANCEL | wx.ICON_EXCLAMATION)
result = dialog.ShowModal()
dialog.Destroy()
if result == wx.ID_NO:
# Drop changes
return True
elif result == wx.ID_YES:
# First save(as)!
if self.OnSaveAs(None):
# Succeeded, we can continue with the operation
return True
else:
# Save did not succeed
return False
else:
# Assume cancel (wx.ID_CANCEL) otherwise
return False
else:
# File was not changed, so we can just proceed
return True
# Event handlers
2006-08-02 13:59:57 +01:00
def OnAbout(self, event):
2006-08-11 11:43:28 +01:00
dlg = About.AboutScyther(self)
dlg.ShowModal()
dlg.Destroy()
2006-08-02 13:59:57 +01:00
def OnExit(self, event):
if self.ConfirmLoss("Exit"):
self.Close() # Close the main window.
return True
return False
2006-08-02 13:59:57 +01:00
def OnSave(self, event):
textfile = open(os.path.join(self.dirname, self.filename), 'w')
2007-01-30 11:01:17 +00:00
textfile.write(self.editor.GetText())
2006-08-02 13:59:57 +01:00
textfile.close()
self.editor.SetSaved()
return True
2006-08-02 13:59:57 +01:00
def OnOpen(self, event):
if self.ConfirmLoss("Open"):
if self.askUserForFilename(style=wx.OPEN,
**self.defaultFileDialogOptions()):
textfile = open(os.path.join(self.dirname, self.filename), 'r')
self.editor.SetText(textfile.read())
textfile.close()
self.editor.SetOpened()
return True
return False
2006-08-02 13:59:57 +01:00
def OnSaveAs(self, event):
if self.askUserForFilename(defaultFile=self.filename, style=wx.SAVE,
**self.defaultFileDialogOptions()):
self.OnSave(event)
os.chdir(self.dirname)
return True
return False
2006-08-02 13:59:57 +01:00
def RunScyther(self, mode):
2007-01-31 15:45:05 +00:00
# Clear errors before verification
self.editor.SetErrors(None)
# Verify spdl
2007-01-30 11:01:17 +00:00
spdl = self.editor.GetText()
2007-01-31 15:45:05 +00:00
s = Scytherthread.ScytherRun(self,mode,spdl,self.editor.SetErrors)
2006-08-02 13:59:57 +01:00
def OnVerify(self, event):
self.RunScyther("verify")
def OnAutoVerify(self, event):
self.RunScyther("autoverify")
def OnCharacterize(self, event):
self.RunScyther("characterize")
2006-08-02 13:59:57 +01:00
def OnCheck(self, event):
self.RunScyther("check")
def firstCommand(self):
if self.opts.command:
# Trigger a command automatically
2006-08-09 11:21:30 +01:00
self.Show(True)
self.RunScyther(self.opts.command)
2006-08-02 13:59:57 +01:00
#---------------------------------------------------------------------------