Dialogs.py
author lbessard
Thu, 19 Jul 2007 15:04:41 +0200
changeset 46 4379e98a30aa
parent 45 42637f721b5b
child 56 7187e1c00975
permissions -rw-r--r--
Bug on SFC generation fixed
Adding Action and Transition remove functions
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from wxPython.wx import *
from wxPython.grid import *
import wx

from graphics import *

#-------------------------------------------------------------------------------
#                          Create New Block Dialog
#-------------------------------------------------------------------------------

[wxID_BLOCKPROPERTIESDIALOG, wxID_BLOCKPROPERTIESDIALOGMAINPANEL, 
 wxID_BLOCKPROPERTIESDIALOGNAME, wxID_BLOCKPROPERTIESDIALOGTYPETREE, 
 wxID_BLOCKPROPERTIESDIALOGTYPEDESC, wxID_BLOCKPROPERTIESDIALOGINPUTS, 
 wxID_BLOCKPROPERTIESDIALOGPREVIEW, wxID_BLOCKPROPERTIESDIALOGSTATICTEXT1, 
 wxID_BLOCKPROPERTIESDIALOGSTATICTEXT2, wxID_BLOCKPROPERTIESDIALOGSTATICTEXT3, 
 wxID_BLOCKPROPERTIESDIALOGSTATICTEXT4, 
] = [wx.NewId() for _init_ctrls in range(11)]

[CATEGORY, BLOCK] = range(2)

class BlockPropertiesDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_BLOCKPROPERTIESDIALOG,
              name='BlockPropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(600, 360), style=wx.DEFAULT_DIALOG_STYLE,
              title='Block Properties')
        self.SetClientSize(wx.Size(600, 360))

        self.MainPanel = wx.Panel(id=wxID_BLOCKPROPERTIESDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(600, 320), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticbox1 = wx.StaticBox(id=wxID_BLOCKPROPERTIESDIALOGSTATICTEXT1,
              label='Type:', name='staticBox1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(245, 280), style=0)

        self.staticText2 = wx.StaticText(id=wxID_BLOCKPROPERTIESDIALOGSTATICTEXT2,
              label='Name:', name='staticText2', parent=self.MainPanel,
              pos=wx.Point(274, 24), size=wx.Size(70, 17), style=0)

        self.staticText3 = wx.StaticText(id=wxID_BLOCKPROPERTIESDIALOGSTATICTEXT2,
              label='Inputs:', name='staticText4', parent=self.MainPanel,
              pos=wx.Point(424, 24), size=wx.Size(70, 17), style=0)

        self.staticText4 = wx.StaticText(id=wxID_BLOCKPROPERTIESDIALOGSTATICTEXT4,
              label='Preview:', name='staticText4', parent=self.MainPanel,
              pos=wx.Point(274, 80), size=wx.Size(100, 17), style=0)

        self.TypeTree = wx.TreeCtrl(id=wxID_BLOCKPROPERTIESDIALOGTYPETREE,
              name='TypeTree', parent=self.MainPanel, pos=wx.Point(34, 44),
              size=wx.Size(225, 180), style=wx.TR_HAS_BUTTONS|wx.TR_HIDE_ROOT|wx.TR_SINGLE|wx.SUNKEN_BORDER)
        EVT_TREE_SEL_CHANGED(self, wxID_BLOCKPROPERTIESDIALOGTYPETREE, self.OnTypeTreeItemSelected)

        self.TypeDesc = wx.TextCtrl(id=wxID_BLOCKPROPERTIESDIALOGTYPEDESC,
              name='TypeDesc', parent=self.MainPanel, pos=wx.Point(34, 230),
              size=wx.Size(225, 65), style=wx.TE_READONLY|wx.TE_MULTILINE)

        self.Name = wx.TextCtrl(id=wxID_BLOCKPROPERTIESDIALOGNAME, value='',
              name='Name', parent=self.MainPanel, pos=wx.Point(274, 48),
              size=wx.Size(145, 24), style=0)
        EVT_TEXT(self, wxID_BLOCKPROPERTIESDIALOGNAME, self.OnNameChanged)

        self.Inputs = wx.SpinCtrl(id=wxID_BLOCKPROPERTIESDIALOGINPUTS,
              name='Inputs', parent=self.MainPanel, pos=wx.Point(424, 48),
              size=wx.Size(145, 24), style=wxSP_ARROW_KEYS, min=2, max=20)
        EVT_SPINCTRL(self, wxID_BLOCKPROPERTIESDIALOGINPUTS, self.OnInputsChanged)

        self.Preview = wx.Panel(id=wxID_BLOCKPROPERTIESDIALOGPREVIEW,
              name='Preview', parent=self.MainPanel, pos=wx.Point(274, 104),
              size=wx.Size(300, 200), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wxColour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        self.Name.SetValue("")
        self.Name.Enable(False)
        self.Inputs.Enable(False)
        self.Block = None
        self.MinBlockSize = None
        
        EVT_PAINT(self, self.OnPaint)
        EVT_BUTTON(self, self.ButtonSizer.GetAffirmativeButton().GetId(), self.OnOK)
    
    def FindTreeItem(self, root, name, inputs = None):
        if root.IsOk():
            pydata = self.TypeTree.GetPyData(root)
            if inputs and "inputs" in pydata:
                same_inputs = pydata["inputs"] == inputs
            else:
                same_inputs = True
            if self.TypeTree.GetItemText(root) == name and same_inputs:
                return root
            else:
                item, root_cookie = self.TypeTree.GetFirstChild(root)
                while item.IsOk():
                    result = self.FindTreeItem(item, name, inputs)
                    if result:
                        return result
                    item, root_cookie = self.TypeTree.GetNextChild(root, root_cookie)
        return None
    
    def OnOK(self, event):
        error = []
        selected = self.TypeTree.GetSelection()
        if not selected.IsOk() or self.TypeTree.GetItemParent(selected) == self.TypeTree.GetRootItem() or selected == self.TypeTree.GetRootItem():
            message = wxMessageDialog(self, "Form isn't complete. Valid block type must be selected!", "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif self.Name.IsEnabled() and self.Name.GetValue() == "":
            message = wxMessageDialog(self, "Form isn't complete. Name must be filled!", "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wxID_OK)

    def SetBlockList(self, blocktypes):
        root = self.TypeTree.AddRoot("")
        self.TypeTree.SetPyData(root, {"type" : CATEGORY})
        for category in blocktypes:
            category_item = self.TypeTree.AppendItem(root, category["name"])
            self.TypeTree.SetPyData(category_item, {"type" : CATEGORY})
            for blocktype in category["list"]:
                blocktype_item = self.TypeTree.AppendItem(category_item, blocktype["name"])
                self.TypeTree.SetPyData(blocktype_item, {"type" : BLOCK, "inputs" : tuple([type for name, type, modifier in blocktype["inputs"]])})

    def SetMinBlockSize(self, size):
        self.MinBlockSize = size

    def SetValues(self, values):
        for name, value in values.items():
            if name == "type":
                inputs = None
                if "inputs" in values:
                    inputs = values["inputs"]
                item = self.FindTreeItem(self.TypeTree.GetRootItem(), value, inputs)
                if item:
                    self.TypeTree.SelectItem(item)
            elif name == "name":
                self.Name.SetValue(value)
            elif name == "extension":
                self.Inputs.SetValue(value)
        self.RefreshPreview()

    def GetValues(self):
        values = {}
        item = self.TypeTree.GetSelection()
        values["type"] = self.TypeTree.GetItemText(item)
        values["inputs"] = self.TypeTree.GetPyData(item)["inputs"]
        if self.Name.GetValue() != "":
            values["name"] = self.Name.GetValue()
        values["width"], values["height"] = self.Block.GetSize()
        values["extension"] = self.Inputs.GetValue()
        return values

    def OnTypeTreeItemSelected(self, event):
        self.Name.SetValue("")
        selected = event.GetItem()
        pydata = self.TypeTree.GetPyData(selected)
        if pydata["type"] != CATEGORY:
            blocktype = GetBlockType(self.TypeTree.GetItemText(selected), pydata["inputs"])
            if blocktype:
                self.Inputs.SetValue(len(blocktype["inputs"]))
                self.Inputs.Enable(blocktype["extensible"])
                self.Name.Enable(blocktype["type"] != "function")
                self.TypeDesc.SetValue(blocktype["comment"])
                wxCallAfter(self.RefreshPreview)
            else:
                self.Name.Enable(False)
                self.Inputs.Enable(False)
                self.Inputs.SetValue(2)
                self.TypeDesc.SetValue("")
                wxCallAfter(self.ErasePreview)
        else:
            self.Name.Enable(False)
            self.Inputs.Enable(False)
            self.Inputs.SetValue(2)
            self.TypeDesc.SetValue("")
            wxCallAfter(self.ErasePreview)
        event.Skip()

    def OnNameChanged(self, event):
        if self.Name.IsEnabled():
            self.RefreshPreview()
        event.Skip()
    
    def OnInputsChanged(self, event):
        if self.Inputs.IsEnabled():
            self.RefreshPreview()
        event.Skip()
    
    def ErasePreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        self.Block = None
        
    def RefreshPreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        item = self.TypeTree.GetSelection()
        pydata = self.TypeTree.GetPyData(item)
        if pydata["type"] == CATEGORY:
            self.Block = None
        else:
            blocktype = self.TypeTree.GetItemText(item)
            if blocktype:
                self.Block = FBD_Block(self.Preview, blocktype, self.Name.GetValue(), extension = self.Inputs.GetValue(), inputs = pydata["inputs"])
                width, height = self.MinBlockSize
                min_width, min_height = self.Block.GetMinSize()
                width, height = max(min_width, width), max(min_height, height)
                self.Block.SetSize(width, height)
                clientsize = self.Preview.GetClientSize()
                x = (clientsize.width - width) / 2
                y = (clientsize.height - height) / 2
                self.Block.SetPosition(x, y)
                self.Block.Draw(dc)
            else:
                self.Block = None

    def OnPaint(self, event):
        if self.Block:
            self.RefreshPreview()


#-------------------------------------------------------------------------------
#                          Create New Variable Dialog
#-------------------------------------------------------------------------------

[wxID_VARIABLEPROPERTIESDIALOG, wxID_VARIABLEPROPERTIESDIALOGMAINPANEL, 
 wxID_VARIABLEPROPERTIESDIALOGNAME, wxID_VARIABLEPROPERTIESDIALOGCLASS, 
 wxID_VARIABLEPROPERTIESDIALOGPREVIEW, wxID_VARIABLEPROPERTIESDIALOGEXPRESSION,
 wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT1, wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT2,
 wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT3, wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT4,
] = [wx.NewId() for _init_ctrls in range(10)]

class VariablePropertiesDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_VARIABLEPROPERTIESDIALOG,
              name='VariablePropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(400, 380), style=wx.DEFAULT_DIALOG_STYLE,
              title='Variable Properties')
        self.SetClientSize(wx.Size(400, 380))

        self.MainPanel = wx.Panel(id=wxID_VARIABLEPROPERTIESDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(400, 280), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT1,
              label='Class:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(70, 17), style=0)

        self.staticText2 = wx.StaticText(id=wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT2,
              label='Expression:', name='staticText2', parent=self.MainPanel,
              pos=wx.Point(24, 90), size=wx.Size(100, 17), style=0)

        self.staticText3 = wx.StaticText(id=wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT3,
              label='Name:', name='staticText3', parent=self.MainPanel,
              pos=wx.Point(204, 24), size=wx.Size(70, 17), style=0)

        self.staticText4 = wx.StaticText(id=wxID_VARIABLEPROPERTIESDIALOGSTATICTEXT4,
              label='Preview:', name='staticText4', parent=self.MainPanel,
              pos=wx.Point(24, 144), size=wx.Size(100, 17), style=0)

        self.Class = wx.Choice(id=wxID_VARIABLEPROPERTIESDIALOGCLASS,
              name='Class', parent=self.MainPanel, pos=wx.Point(24, 48),
              size=wx.Size(145, 24), style=0)
        EVT_CHOICE(self, wxID_VARIABLEPROPERTIESDIALOGCLASS, self.OnClassChanged)
        
        self.Name = wx.ListBox(id=wxID_VARIABLEPROPERTIESDIALOGNAME,
              name='Name', parent=self.MainPanel, pos=wx.Point(204, 48),
              size=wx.Size(145, 90), style=wx.LB_SINGLE)
        EVT_LISTBOX(self, wxID_VARIABLEPROPERTIESDIALOGNAME, self.OnNameChanged)

        self.Expression = wx.TextCtrl(id=wxID_VARIABLEPROPERTIESDIALOGEXPRESSION,
              name='Expression', parent=self.MainPanel, pos=wx.Point(24, 114),
              size=wx.Size(145, 24), style=0)
        EVT_TEXT(self, wxID_VARIABLEPROPERTIESDIALOGEXPRESSION, self.OnExpressionChanged)

        self.Preview = wx.Panel(id=wxID_VARIABLEPROPERTIESDIALOGPREVIEW,
              name='Preview', parent=self.MainPanel, pos=wx.Point(24, 170),
              size=wx.Size(350, 150), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wxColour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        self.Variable = None
        self.VarList = []
        self.MinVariableSize = None
        self.RefreshNameList()
        
        for choice in ["Input", "Output", "InOut"]:
            self.Class.Append(choice)
        self.Class.SetStringSelection("Input")
        
        EVT_PAINT(self, self.OnPaint)

    def RefreshNameList(self):
        selected = self.Name.GetStringSelection()
        self.Name.Clear()
        self.Name.Append("")
        for name, var_type, value_type in self.VarList:
            if var_type in ["Local","Temp","Global","External"]:
                self.Name.Append(name)
            elif var_type == "Input" and self.Class.GetStringSelection() == "Input":
                self.Name.Append(name)
            elif var_type == "Output" and self.Class.GetStringSelection() == "Output":
                self.Name.Append(name)
            elif var_type == "InOut" and self.Class.GetStringSelection() == "InOut":
                self.Name.Append(name)
        if self.Name.FindString(selected) != wxNOT_FOUND:
            self.Name.SetStringSelection(selected)
        else:
            self.Name.SetStringSelection("")
        self.Name.Enable(self.Name.GetCount() > 0)
            
    def SetMinVariableSize(self, size):
        self.MinVariableSize = size

    def SetVariables(self, vars):
        self.VarList = vars
        self.RefreshNameList()

    def SetValues(self, values):
        for name, value in values.items():
            if name == "type":
                if value == INPUT:
                    self.Class.SetStringSelection("Input")
                if value == OUTPUT:
                    self.Class.SetStringSelection("Output")
                if value == INOUT:
                    self.Class.SetStringSelection("InOut")
            elif name == "name" and value != "":
                if self.Name.FindString(value) != wxNOT_FOUND:
                    self.Name.SetStringSelection(value)
                    self.Expression.Enable(False)
                else:
                    self.Expression.SetValue(value)
                    self.Name.Enable(False)
        self.RefreshPreview()
        
    def GetValues(self):
        values = {}
        classtype = self.Class.GetStringSelection()
        if classtype == "Input":
            values["type"] = INPUT
        elif classtype == "Output":
            values["type"] = OUTPUT
        elif classtype == "InOut":
            values["type"] = INOUT
        expression = self.Expression.GetValue()
        if self.Expression.IsEnabled() and expression != "":
            values["name"] = expression
        else:
            values["name"] = self.Name.GetStringSelection()
        values["value_type"] = ""
        for var_name, var_type, value_type in self.VarList:
            if var_name == values["name"]:
                values["value_type"] = value_type
        values["width"], values["height"] = self.Variable.GetSize()
        return values

    def OnClassChanged(self, event):
        self.RefreshNameList()
        if self.Class.GetStringSelection() == "Input":
            self.Expression.Enable(True)
        else:
            self.Expression.Enable(False)
        self.RefreshPreview()
        event.Skip()

    def OnNameChanged(self, event):
        if self.Name.GetStringSelection() != "":
            self.Expression.Enable(False)
        elif self.Class.GetStringSelection() == "Input":
            self.Expression.Enable(True)
        self.RefreshPreview()
        event.Skip()
    
    def OnExpressionChanged(self, event):
        if self.Expression.GetValue() != "":
            self.Name.Enable(False)
        else:
            self.Name.Enable(True)
        self.RefreshPreview()
        event.Skip()
    
    def RefreshPreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        expression = self.Expression.GetValue()
        if self.Expression.IsEnabled() and expression != "":
            name = expression
        else:
            name = self.Name.GetStringSelection()
        type = ""
        for var_name, var_type, value_type in self.VarList:
            if var_name == name:
                type = value_type
        classtype = self.Class.GetStringSelection()
        if classtype == "Input":
            self.Variable = FBD_Variable(self.Preview, INPUT, name, type)
        elif classtype == "Output":
            self.Variable = FBD_Variable(self.Preview, OUTPUT, name, type)
        elif classtype == "InOut":
            self.Variable = FBD_Variable(self.Preview, INOUT, name, type)
        width, height = self.MinVariableSize
        min_width, min_height = self.Variable.GetMinSize()
        width, height = max(min_width, width), max(min_height, height)
        self.Variable.SetSize(width, height)
        clientsize = self.Preview.GetClientSize()
        x = (clientsize.width - width) / 2
        y = (clientsize.height - height) / 2
        self.Variable.SetPosition(x, y)
        self.Variable.Draw(dc)

    def OnPaint(self, event):
        self.RefreshPreview()

#-------------------------------------------------------------------------------
#                          Create New Connection Dialog
#-------------------------------------------------------------------------------

[wxID_CONNECTIONPROPERTIESDIALOG, wxID_CONNECTIONPROPERTIESDIALOGMAINPANEL, 
 wxID_CONNECTIONPROPERTIESDIALOGNAME, wxID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON1, 
 wxID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON2, wxID_CONNECTIONPROPERTIESDIALOGPREVIEW,
 wxID_CONNECTIONPROPERTIESDIALOGSTATICTEXT1, wxID_CONNECTIONPROPERTIESDIALOGSTATICTEXT2, 
 wxID_CONNECTIONPROPERTIESDIALOGSTATICTEXT3, 
] = [wx.NewId() for _init_ctrls in range(9)]

class ConnectionPropertiesDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_CONNECTIONPROPERTIESDIALOG,
              name='ConnectionPropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(350, 220), style=wx.DEFAULT_DIALOG_STYLE,
              title='Connection Properties')
        self.SetClientSize(wx.Size(350, 220))

        self.MainPanel = wx.Panel(id=wxID_CONNECTIONPROPERTIESDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(340, 360), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_CONNECTIONPROPERTIESDIALOGSTATICTEXT1,
              label='Type:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(70, 17), style=0)

        self.staticText2 = wx.StaticText(id=wxID_CONNECTIONPROPERTIESDIALOGSTATICTEXT2,
              label='Name:', name='staticText2', parent=self.MainPanel,
              pos=wx.Point(24, 104), size=wx.Size(70, 17), style=0)

        self.staticText3 = wx.StaticText(id=wxID_CONNECTIONPROPERTIESDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self.MainPanel,
              pos=wx.Point(174, 24), size=wx.Size(100, 17), style=0)

        self.radioButton1 = wx.RadioButton(id=wxID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON1,
              label='Connector', name='radioButton1', parent=self.MainPanel,
              pos=wx.Point(24, 48), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON1, self.OnTypeChanged)
        self.radioButton1.SetValue(True)

        self.radioButton2 = wx.RadioButton(id=wxID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON2,
              label='Continuation', name='radioButton2', parent=self.MainPanel, 
              pos=wx.Point(24, 72), size=wx.Size(128, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON2, self.OnTypeChanged)
        self.radioButton2.SetValue(False)
        
        self.Name = wx.TextCtrl(id=wxID_CONNECTIONPROPERTIESDIALOGNAME,
              name='Name', parent=self.MainPanel, pos=wx.Point(24, 130),
              size=wx.Size(145, 24), style=0)
        EVT_TEXT(self, wxID_CONNECTIONPROPERTIESDIALOGNAME, self.OnNameChanged)

        self.Preview = wx.Panel(id=wxID_CONNECTIONPROPERTIESDIALOGPREVIEW,
              name='Preview', parent=self.MainPanel, pos=wx.Point(174, 48),
              size=wx.Size(150, 100), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wxColour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        self.Connection = None
        self.MinConnectionSize = None
        
        EVT_PAINT(self, self.OnPaint)
            
    def SetMinConnectionSize(self, size):
        self.MinConnectionSize = size
    
    def SetValues(self, values):
        for name, value in values.items():
            if name == "type":
                if value == CONNECTOR:
                    self.radioButton1.SetValue(True)
                elif value == CONTINUATION:
                    self.radioButton2.SetValue(True)
            elif name == "name":
                self.Name.SetValue(value)
        self.RefreshPreview()
    
    def GetValues(self):
        values = {}
        if self.radioButton1.GetValue():
            values["type"] = CONNECTOR
        else:
            values["type"] = CONTINUATION
        values["name"] = self.Name.GetValue()
        values["width"], values["height"] = self.Connection.GetSize()
        return values

    def OnTypeChanged(self, event):
        self.RefreshPreview()
        event.Skip()

    def OnNameChanged(self, event):
        self.RefreshPreview()
        event.Skip()
        
    def RefreshPreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        if self.radioButton1.GetValue():
            self.Connection = FBD_Connector(self.Preview, CONNECTOR, self.Name.GetValue())
        else:
            self.Connection = FBD_Connector(self.Preview, CONTINUATION, self.Name.GetValue())
        width, height = self.MinConnectionSize
        min_width, min_height = self.Connection.GetMinSize()
        width, height = max(min_width, width), max(min_height, height)
        self.Connection.SetSize(width, height)
        clientsize = self.Preview.GetClientSize()
        x = (clientsize.width - width) / 2
        y = (clientsize.height - height) / 2
        self.Connection.SetPosition(x, y)
        self.Connection.Draw(dc)

    def OnPaint(self, event):
        self.RefreshPreview()


#-------------------------------------------------------------------------------
#                        Edit Ladder Element Properties Dialog
#-------------------------------------------------------------------------------


[wxID_LDELEMENTDIALOG, wxID_LDELEMENTDIALOGMAINPANEL, 
 wxID_LDELEMENTDIALOGNAME, wxID_LDELEMENTDIALOGRADIOBUTTON1, 
 wxID_LDELEMENTDIALOGRADIOBUTTON2, wxID_LDELEMENTDIALOGRADIOBUTTON3,
 wxID_LDELEMENTDIALOGRADIOBUTTON4, wxID_LDELEMENTDIALOGPREVIEW,
 wxID_LDELEMENTDIALOGSTATICTEXT1, wxID_LDELEMENTDIALOGSTATICTEXT2, 
 wxID_LDELEMENTDIALOGSTATICTEXT3, 
] = [wx.NewId() for _init_ctrls in range(11)]

class LDElementDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt, title, labels):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_LDELEMENTDIALOG,
              name='VariablePropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(350, 260), style=wx.DEFAULT_DIALOG_STYLE,
              title=title)
        self.SetClientSize(wx.Size(350, 260))

        self.MainPanel = wx.Panel(id=wxID_LDELEMENTDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(340, 200), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_LDELEMENTDIALOGSTATICTEXT1,
              label='Modifier:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(70, 17), style=0)

        self.staticText2 = wx.StaticText(id=wxID_LDELEMENTDIALOGSTATICTEXT2,
              label='Name:', name='staticText2', parent=self.MainPanel,
              pos=wx.Point(24, 150), size=wx.Size(70, 17), style=0)

        self.staticText3 = wx.StaticText(id=wxID_LDELEMENTDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self.MainPanel,
              pos=wx.Point(174, 24), size=wx.Size(100, 17), style=0)

        self.radioButton1 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON1,
              label=labels[0], name='radioButton1', parent=self.MainPanel,
              pos=wx.Point(24, 48), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON1, self.OnTypeChanged)
        self.radioButton1.SetValue(True)

        self.radioButton2 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON2,
              label=labels[1], name='radioButton2', parent=self.MainPanel, 
              pos=wx.Point(24, 72), size=wx.Size(128, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON2, self.OnTypeChanged)

        self.radioButton3 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON3,
              label=labels[2], name='radioButton3', parent=self.MainPanel,
              pos=wx.Point(24, 96), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON3, self.OnTypeChanged)

        self.radioButton4 = wx.RadioButton(id=wxID_LDELEMENTDIALOGRADIOBUTTON4,
              label=labels[3], name='radioButton4', parent=self.MainPanel, 
              pos=wx.Point(24, 120), size=wx.Size(128, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_LDELEMENTDIALOGRADIOBUTTON4, self.OnTypeChanged)

        self.Name = wx.Choice(id=wxID_LDELEMENTDIALOGNAME,
              name='Name', parent=self.MainPanel, pos=wx.Point(24, 174),
              size=wx.Size(145, 24), style=0)
        EVT_CHOICE(self, wxID_LDELEMENTDIALOGNAME, self.OnNameChanged)

        self.Preview = wx.Panel(id=wxID_LDELEMENTDIALOGPREVIEW,
              name='Preview', parent=self.MainPanel, pos=wx.Point(174, 48),
              size=wx.Size(150, 150), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wxColour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self._init_sizers()

    def __init__(self, parent, type):
        self.Type = type
        if type == "contact":
            self._init_ctrls(parent, "Edit Contact Values", ['Normal','Negate','Rising Edge','Falling Edge'])
            self.Element = LD_Contact(self.Preview, CONTACT_NORMAL, "")
        elif type == "coil":
            self._init_ctrls(parent, "Edit Coil Values", ['Normal','Negate','Set','Reset'])
            self.Element = LD_Coil(self.Preview, COIL_NORMAL, "")
        self.Element.SetPosition((150 - LD_ELEMENT_SIZE[0]) / 2, (150 - LD_ELEMENT_SIZE[1]) / 2)

        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)

        EVT_PAINT(self, self.OnPaint)

    def SetElementSize(self, size):
        min_width, min_height = self.Element.GetMinSize()
        width, height = max(min_width, size[0]), max(min_height, size[1])
        self.Element.SetSize(width, height)
        self.Element.SetPosition((150 - width) / 2, (150 - height) / 2)

    def SetVariables(self, vars):
        self.Name.Clear()
        for name in vars:
            self.Name.Append(name)
        self.Name.Enable(self.Name.GetCount() > 0)

    def SetValues(self, values):
        for name, value in values.items():
            if name == "name":
                self.Element.SetName(value)
                self.Name.SetStringSelection(value)
            elif name == "type":
                self.Element.SetType(value)
                if self.Type == "contact":
                    if value == CONTACT_NORMAL:
                        self.radioButton1.SetValue(True)
                    elif value == CONTACT_REVERSE:
                        self.radioButton2.SetValue(True)
                    elif value == CONTACT_RISING:
                        self.radioButton3.SetValue(True)
                    elif value == CONTACT_FALLING:
                        self.radioButton4.SetValue(True)
                elif self.Type == "coil":
                    if value == COIL_NORMAL:
                        self.radioButton1.SetValue(True)
                    elif value == COIL_REVERSE:
                        self.radioButton2.SetValue(True)
                    elif value == COIL_SET:
                        self.radioButton3.SetValue(True)
                    elif value == COIL_RESET:
                        self.radioButton4.SetValue(True)

    def GetValues(self):
        values = {}
        values["name"] = self.Element.GetName()
        values["type"] = self.Element.GetType()
        values["width"], values["height"] = self.Element.GetSize()
        return values

    def OnTypeChanged(self, event):
        if self.Type == "contact":
            if self.radioButton1.GetValue():
                self.Element.SetType(CONTACT_NORMAL)
            elif self.radioButton2.GetValue():
                self.Element.SetType(CONTACT_REVERSE)
            elif self.radioButton3.GetValue():
                self.Element.SetType(CONTACT_RISING)
            elif self.radioButton4.GetValue():
                self.Element.SetType(CONTACT_FALLING)
        elif self.Type == "coil":
            if self.radioButton1.GetValue():
                self.Element.SetType(COIL_NORMAL)
            elif self.radioButton2.GetValue():
                self.Element.SetType(COIL_REVERSE)
            elif self.radioButton3.GetValue():
                self.Element.SetType(COIL_SET)
            elif self.radioButton4.GetValue():
                self.Element.SetType(COIL_RESET)
        self.RefreshPreview()
        event.Skip()

    def OnNameChanged(self, event):
        self.Element.SetName(self.Name.GetStringSelection())
        self.RefreshPreview()
        event.Skip()

    def RefreshPreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        self.Element.Draw(dc)

    def OnPaint(self, event):
        self.RefreshPreview()
        event.Skip()


#-------------------------------------------------------------------------------
#                      Edit Ladder Power Rail Properties Dialog
#-------------------------------------------------------------------------------


[wxID_LDPOWERRAILDIALOG, wxID_LDPOWERRAILDIALOGMAINPANEL, 
 wxID_LDPOWERRAILDIALOGTYPE, wxID_LDPOWERRAILDIALOGRADIOBUTTON1, 
 wxID_LDPOWERRAILDIALOGRADIOBUTTON2, wxID_LDPOWERRAILDIALOGPREVIEW,
 wxID_LDPOWERRAILDIALOGSTATICTEXT1, wxID_LDPOWERRAILDIALOGSTATICTEXT2, 
 wxID_LDPOWERRAILDIALOGSTATICTEXT3, wxID_LDPOWERRAILDIALOGPINNUMBER,
] = [wx.NewId() for _init_ctrls in range(10)]

class LDPowerRailDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_LDPOWERRAILDIALOG,
              name='PowerRailDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(350, 260), style=wx.DEFAULT_DIALOG_STYLE,
              title='Power Rail Properties')
        self.SetClientSize(wx.Size(350, 260))

        self.MainPanel = wx.Panel(id=wxID_LDPOWERRAILDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(340, 200), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_LDPOWERRAILDIALOGSTATICTEXT1,
              label='Type:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(70, 17), style=0)

        self.staticText2 = wx.StaticText(id=wxID_LDPOWERRAILDIALOGSTATICTEXT2,
              label='Pin number:', name='staticText2', parent=self.MainPanel,
              pos=wx.Point(24, 100), size=wx.Size(70, 17), style=0)

        self.staticText3 = wx.StaticText(id=wxID_LDPOWERRAILDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self.MainPanel,
              pos=wx.Point(174, 24), size=wx.Size(100, 17), style=0)

        self.radioButton1 = wx.RadioButton(id=wxID_LDPOWERRAILDIALOGRADIOBUTTON1,
              label='Left PowerRail', name='radioButton1', parent=self.MainPanel,
              pos=wx.Point(24, 48), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_LDPOWERRAILDIALOGRADIOBUTTON1, self.OnTypeChanged)
        self.radioButton1.SetValue(True)

        self.radioButton2 = wx.RadioButton(id=wxID_LDPOWERRAILDIALOGRADIOBUTTON2,
              label='Right PowerRail', name='radioButton2', parent=self.MainPanel, 
              pos=wx.Point(24, 72), size=wx.Size(128, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_LDPOWERRAILDIALOGRADIOBUTTON2, self.OnTypeChanged)

        self.PinNumber = wx.SpinCtrl(id=wxID_LDPOWERRAILDIALOGPINNUMBER,
              name='PinNumber', parent=self.MainPanel, pos=wx.Point(24, 124),
              size=wx.Size(145, 24), style=wxSP_ARROW_KEYS, min=1, max=20)
        EVT_SPINCTRL(self, wxID_LDPOWERRAILDIALOGPINNUMBER, self.OnPinNumberChanged)

        self.Preview = wx.Panel(id=wxID_LDPOWERRAILDIALOGPREVIEW,
              name='Preview', parent=self.MainPanel, pos=wx.Point(174, 48),
              size=wx.Size(150, 150), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wxColour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self._init_sizers()

    def __init__(self, parent, type = LEFTRAIL, number = 1):
        self._init_ctrls(parent)
        self.Type = type
        if type == LEFTRAIL:
            self.radioButton1.SetValue(True)
        elif type == RIGHTRAIL:
            self.radioButton2.SetValue(True)
        self.PinNumber.SetValue(number)
        
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)

        self.PowerRailMinSize = (0, 0)
        self.PowerRail = None

        EVT_PAINT(self, self.OnPaint)

    def SetMinSize(self, size):
        self.PowerRailMinSize = size
        self.RefreshPreview()    

    def GetValues(self):
        values = {}
        values["type"] = self.Type
        values["number"] = self.PinNumber.GetValue()
        values["width"], values["height"] = self.PowerRail.GetSize()
        return values

    def OnTypeChanged(self, event):
        if self.radioButton1.GetValue():
            self.Type = LEFTRAIL
        elif self.radioButton2.GetValue():
            self.Type = RIGHTRAIL
        self.RefreshPreview()
        event.Skip()

    def OnPinNumberChanged(self, event):
        self.RefreshPreview()
        event.Skip()

    def RefreshPreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        self.PowerRail = LD_PowerRail(self.Preview, self.Type, connectors = [True for i in xrange(self.PinNumber.GetValue())])
        min_width, min_height = self.PowerRail.GetMinSize()
        width, height = max(min_width, self.PowerRailMinSize[0]), max(min_height, self.PowerRailMinSize[1])
        self.PowerRail.SetSize(width, height)
        self.PowerRail.RefreshConnectors()
        self.PowerRail.SetPosition((150 - width) / 2, (150 - height) / 2)
        self.PowerRail.Draw(dc)

    def OnPaint(self, event):
        self.RefreshPreview()
        event.Skip()


#-------------------------------------------------------------------------------
#                          Edit Transition Content Dialog
#-------------------------------------------------------------------------------

[wxID_TRANSITIONCONTENTDIALOG, wxID_TRANSITIONCONTENTDIALOGMAINPANEL, 
 wxID_TRANSITIONCONTENTDIALOGREFERENCE, wxID_TRANSITIONCONTENTDIALOGINLINE, 
 wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON1, wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON2, 
] = [wx.NewId() for _init_ctrls in range(6)]

class TransitionContentDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_TRANSITIONCONTENTDIALOG,
              name='ProjectDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE,
              title='Edit transition')
        self.SetClientSize(wx.Size(300, 200))

        self.MainPanel = wx.Panel(id=wxID_TRANSITIONCONTENTDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(300, 200), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.radioButton1 = wx.RadioButton(id=wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON1,
              label='Reference', name='radioButton1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON1, self.OnTypeChanged)
        self.radioButton1.SetValue(True)

        self.Reference = wx.Choice(id=wxID_TRANSITIONCONTENTDIALOGREFERENCE,
              name='Reference', parent=self.MainPanel, pos=wx.Point(48, 48), 
              size=wx.Size(200, 24), style=0)

        self.radioButton2 = wx.RadioButton(id=wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON2,
              label='Inline', name='radioButton2', parent=self.MainPanel,
              pos=wx.Point(24, 72), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON2, self.OnTypeChanged)
        self.radioButton2.SetValue(False)

        self.Inline = wx.TextCtrl(id=wxID_TRANSITIONCONTENTDIALOGINLINE,
              name='Inline', parent=self.MainPanel, pos=wx.Point(48, 96),
              size=wx.Size(200, 24), style=0)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        
        EVT_BUTTON(self, self.ButtonSizer.GetAffirmativeButton().GetId(), self.OnOK)
    
    def OnOK(self, event):
        error = []
        if self.radioButton1.GetValue() and self.Reference.GetStringSelection() == "":
            error.append("Reference")
        if self.radioButton2.GetValue() and self.Inline.GetValue() == "":
            error.append("Inline")
        if len(error) > 0:
            text = ""
            for i, item in enumerate(error):
                if i == 0:
                    text += item
                elif i == len(error) - 1:
                    text += " and %s"%item
                else:
                    text += ", %s"%item 
            message = wxMessageDialog(self, "Form isn't complete. %s must be filled!"%text, "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wxID_OK)

    def OnTypeChanged(self, event):
        if self.radioButton1.GetValue():
            self.Reference.Enable(True)
            self.Inline.Enable(False)
        else:
            self.Reference.Enable(False)
            self.Inline.Enable(True)
        event.Skip()

    def SetTransitions(self, transitions):
        for transition in transitions:
            self.Reference.Append(transition)

    def SetValues(self, values):
        if values["type"] == "reference":
            self.radioButton1.SetValue(True)
            self.radioButton2.SetValue(False)
            self.Reference.Enable(True)
            self.Inline.Enable(False)
            self.Reference.SetStringSelection(values["value"])
        elif values["type"] == "inline":
            self.radioButton1.SetValue(False)
            self.radioButton2.SetValue(True)
            self.Reference.Enable(False)
            self.Inline.Enable(True)
            self.Inline.SetValue(values["value"])
                
    def GetValues(self):
        values = {}
        if self.radioButton1.GetValue():
            values["type"] = "reference"
            values["value"] = self.Reference.GetStringSelection()
        else:
            values["type"] = "inline"
            values["value"] = self.Inline.GetValue()
        return values

#-------------------------------------------------------------------------------
#                         Create New Divergence Dialog
#-------------------------------------------------------------------------------

[wxID_DIVERGENCECREATEDIALOG, wxID_DIVERGENCECREATEDIALOGMAINPANEL, 
 wxID_DIVERGENCECREATEDIALOGRADIOBUTTON1, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON2,
 wxID_DIVERGENCECREATEDIALOGRADIOBUTTON3, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON4, 
 wxID_DIVERGENCECREATEDIALOGSEQUENCES, wxID_DIVERGENCECREATEDIALOGPREVIEW, 
 wxID_DIVERGENCECREATEDIALOGSTATICTEXT1, wxID_DIVERGENCECREATEDIALOGSTATICTEXT2, 
 wxID_DIVERGENCECREATEDIALOGSTATICTEXT3,  
] = [wx.NewId() for _init_ctrls in range(11)]

class DivergenceCreateDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIVERGENCECREATEDIALOG,
              name='DivergencePropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(500, 300), style=wx.DEFAULT_DIALOG_STYLE,
              title='Create a new divergence or convergence')
        self.SetClientSize(wx.Size(500, 260))

        self.MainPanel = wx.Panel(id=wxID_DIVERGENCECREATEDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(600, 220), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_DIVERGENCECREATEDIALOGSTATICTEXT1,
              label='Type:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(200, 17), style=0)

        self.radioButton1 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON1,
              label='Selection Divergence', name='radioButton1', parent=self.MainPanel,
              pos=wx.Point(24, 48), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON1, self.OnTypeChanged)
        self.radioButton1.SetValue(True)

        self.radioButton2 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON2,
              label='Selection Convergence', name='radioButton2', parent=self.MainPanel, 
              pos=wx.Point(24, 72), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON2, self.OnTypeChanged)
        self.radioButton2.SetValue(False)

        self.radioButton3 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON3,
              label='Simultaneous Divergence', name='radioButton3', parent=self.MainPanel,
              pos=wx.Point(24, 96), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON3, self.OnTypeChanged)
        self.radioButton3.SetValue(False)

        self.radioButton4 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON4,
              label='Simultaneous Convergence', name='radioButton4', parent=self.MainPanel, 
              pos=wx.Point(24, 120), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON4, self.OnTypeChanged)
        self.radioButton4.SetValue(False)

        self.staticText2 = wx.StaticText(id=wxID_DIVERGENCECREATEDIALOGSTATICTEXT2,
              label='Number of sequences:', name='staticText2', parent=self.MainPanel,
              pos=wx.Point(24, 150), size=wx.Size(200, 17), style=0)

        self.Sequences = wx.SpinCtrl(id=wxID_DIVERGENCECREATEDIALOGSEQUENCES,
              name='Sequences', parent=self.MainPanel, pos=wx.Point(24, 174),
              size=wx.Size(200, 24), style=0, min=2, max=20)
        EVT_SPINCTRL(self, wxID_DIVERGENCECREATEDIALOGSEQUENCES, self.OnSequencesChanged)

        self.staticText3 = wx.StaticText(id=wxID_DIVERGENCECREATEDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self.MainPanel,
              pos=wx.Point(250, 24), size=wx.Size(100, 17), style=0)

        self.Preview = wx.Panel(id=wxID_DIVERGENCECREATEDIALOGPREVIEW,
              name='Preview', parent=self.MainPanel, pos=wx.Point(250, 48),
              size=wx.Size(225, 150), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wxColour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        
        self.Divergence = None
        self.MinSize = (0, 0)
        
        EVT_PAINT(self, self.OnPaint)

    def GetValues(self):
        values = {}
        if self.radioButton1.GetValue():
            values["type"] = SELECTION_DIVERGENCE
        elif self.radioButton2.GetValue():
            values["type"] = SELECTION_CONVERGENCE
        elif self.radioButton3.GetValue():
            values["type"] = SIMULTANEOUS_DIVERGENCE
        else:
            values["type"] = SIMULTANEOUS_CONVERGENCE
        values["number"] = self.Sequences.GetValue()
        return values

    def SetMinSize(self, size):
        self.MinSize = size

    def OnTypeChanged(self, event):
        self.RefreshPreview()
        event.Skip()

    def OnSequencesChanged(self, event):
        self.RefreshPreview()
        event.Skip()
        
    def RefreshPreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        if self.radioButton1.GetValue():
            self.Divergence = SFC_Divergence(self.Preview, SELECTION_DIVERGENCE, self.Sequences.GetValue())
        elif self.radioButton2.GetValue():
            self.Divergence = SFC_Divergence(self.Preview, SELECTION_CONVERGENCE, self.Sequences.GetValue())
        elif self.radioButton3.GetValue():
            self.Divergence = SFC_Divergence(self.Preview, SIMULTANEOUS_DIVERGENCE, self.Sequences.GetValue())
        else:
            self.Divergence = SFC_Divergence(self.Preview, SIMULTANEOUS_CONVERGENCE, self.Sequences.GetValue())
        width, height = self.Divergence.GetSize()
        min_width, min_height = max(width, self.MinSize[0]), max(height, self.MinSize[1])
        self.Divergence.SetSize(min_width, min_height)
        clientsize = self.Preview.GetClientSize()
        x = (clientsize.width - min_width) / 2
        y = (clientsize.height - min_height) / 2
        self.Divergence.SetPosition(x, y)
        self.Divergence.Draw(dc)

    def OnPaint(self, event):
        self.RefreshPreview()


#-------------------------------------------------------------------------------
#                            Action Block Dialog
#-------------------------------------------------------------------------------

class ActionTable(wxPyGridTableBase):
    
    """
    A custom wxGrid Table using user supplied data
    """
    def __init__(self, parent, data, colnames):
        # The base class must be initialized *first*
        wxPyGridTableBase.__init__(self)
        self.data = data
        self.colnames = colnames
        self.Parent = parent
        # XXX
        # we need to store the row length and collength to
        # see if the table has changed size
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()
    
    def GetNumberCols(self):
        return len(self.colnames)
        
    def GetNumberRows(self):
        return len(self.data)

    def GetColLabelValue(self, col):
        if col < len(self.colnames):
            return self.colnames[col]

    def GetRowLabelValues(self, row):
        return row

    def GetValue(self, row, col):
        if row < self.GetNumberRows():
            name = str(self.data[row].get(self.GetColLabelValue(col), ""))
            return name
    
    def GetValueByName(self, row, colname):
        return self.data[row].get(colname)

    def SetValue(self, row, col, value):
        if col < len(self.colnames):
            self.data[row][self.GetColLabelValue(col)] = value
        
    def ResetView(self, grid):
        """
        (wxGrid) -> Reset the grid view.   Call this to
        update the grid if rows and columns have been added or deleted
        """
        grid.BeginBatch()
        for current, new, delmsg, addmsg in [
            (self._rows, self.GetNumberRows(), wxGRIDTABLE_NOTIFY_ROWS_DELETED, wxGRIDTABLE_NOTIFY_ROWS_APPENDED),
            (self._cols, self.GetNumberCols(), wxGRIDTABLE_NOTIFY_COLS_DELETED, wxGRIDTABLE_NOTIFY_COLS_APPENDED),
        ]:
            if new < current:
                msg = wxGridTableMessage(self,delmsg,new,current-new)
                grid.ProcessTableMessage(msg)
            elif new > current:
                msg = wxGridTableMessage(self,addmsg,new-current)
                grid.ProcessTableMessage(msg)
                self.UpdateValues(grid)
        grid.EndBatch()

        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()
        # update the column rendering scheme
        self._updateColAttrs(grid)

        # update the scrollbars and the displayed part of the grid
        grid.AdjustScrollbars()
        grid.ForceRefresh()

    def UpdateValues(self, grid):
        """Update all displayed values"""
        # This sends an event to the grid table to update all of the values
        msg = wxGridTableMessage(self, wxGRIDTABLE_REQUEST_VIEW_GET_VALUES)
        grid.ProcessTableMessage(msg)

    def _updateColAttrs(self, grid):
        """
        wxGrid -> update the column attributes to add the
        appropriate renderer given the column name.

        Otherwise default to the default renderer.
        """
        
        for col in range(self.GetNumberCols()):
            attr = wxGridCellAttr()
            attr.SetAlignment(self.Parent.ColAlignements[col], wxALIGN_CENTRE)
            grid.SetColAttr(col, attr)
            grid.SetColSize(col, self.Parent.ColSizes[col])
        
        typelist = None
        accesslist = None
        for row in range(self.GetNumberRows()):
            for col in range(self.GetNumberCols()):
                editor = None
                renderer = None
                readonly = False
                colname = self.GetColLabelValue(col)
                if colname == "Qualifier":
                    editor = wxGridCellChoiceEditor()
                    editor.SetParameters(self.Parent.QualifierList)
                if colname == "Duration":
                    editor = wxGridCellTextEditor()
                    renderer = wxGridCellStringRenderer()
                    if self.Parent.DurationList[self.data[row]["Qualifier"]]:
                        readonly = False
                    else:
                        readonly = True
                        self.data[row]["Duration"] = ""
                elif colname == "Type":
                    editor = wxGridCellChoiceEditor()
                    editor.SetParameters(self.Parent.TypeList)
                elif colname == "Value":
                    type = self.data[row]["Type"]
                    if type == "Action":
                        editor = wxGridCellChoiceEditor()
                        editor.SetParameters(self.Parent.ActionList)
                    elif type == "Variable":
                        editor = wxGridCellChoiceEditor()
                        editor.SetParameters(self.Parent.VariableList)
                    elif type == "Inline":
                        editor = wxGridCellTextEditor()
                        renderer = wxGridCellStringRenderer()
                elif colname == "Indicator":
                    editor = wxGridCellChoiceEditor()
                    editor.SetParameters(self.Parent.VariableList)
                    
                grid.SetCellEditor(row, col, editor)
                grid.SetCellRenderer(row, col, renderer)
                grid.SetReadOnly(row, col, readonly)
                
                grid.SetCellBackgroundColour(row, col, wxWHITE)
    
    def SetData(self, data):
        self.data = data
    
    def GetData(self):
        return self.data
    
    def GetCurrentIndex(self):
        return self.CurrentIndex
    
    def SetCurrentIndex(self, index):
        self.CurrentIndex = index
    
    def AppendRow(self, row_content):
        self.data.append(row_content)

    def RemoveRow(self, row_index):
        self.data.pop(row_index)
        
    def MoveRow(self, row_index, move, grid):
        new_index = max(0, min(row_index + move, len(self.data) - 1))
        if new_index != row_index:
            self.data.insert(new_index, self.data.pop(row_index))
            grid.SetGridCursor(new_index, grid.GetGridCursorCol())

    def Empty(self):
        self.data = []
        self.editors = []

[wxID_ACTIONBLOCKDIALOG, wxID_ACTIONBLOCKDIALOGMAINPANEL, 
 wxID_ACTIONBLOCKDIALOGVARIABLESGRID, wxID_ACTIONBLOCKDIALOGSTATICTEXT1, 
 wxID_ACTIONBLOCKDIALOGADDBUTTON,wxID_ACTIONBLOCKDIALOGDELETEBUTTON, 
 wxID_ACTIONBLOCKDIALOGUPBUTTON, wxID_ACTIONBLOCKDIALOGDOWNBUTTON, 
] = [wx.NewId() for _init_ctrls in range(8)]

class ActionBlockDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_ACTIONBLOCKDIALOG,
              name='ActionBlockDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(500, 300), style=wx.DEFAULT_DIALOG_STYLE,
              title='Edit action block properties')
        self.SetClientSize(wx.Size(500, 300))

        self.MainPanel = wx.Panel(id=wxID_ACTIONBLOCKDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(500, 300), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_ACTIONBLOCKDIALOGSTATICTEXT1,
              label='Actions:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(95, 17), style=0)

        self.ActionsGrid = wx.grid.Grid(id=wxID_ACTIONBLOCKDIALOGVARIABLESGRID,
              name='ActionsGrid', parent=self.MainPanel, pos=wx.Point(24, 44), 
              size=wx.Size(450, 150), style=wxVSCROLL)
        self.ActionsGrid.SetFont(wx.Font(12, 77, wx.NORMAL, wx.NORMAL, False,
              'Sans'))
        self.ActionsGrid.SetLabelFont(wx.Font(10, 77, wx.NORMAL, wx.NORMAL,
              False, 'Sans'))
        self.ActionsGrid.DisableDragGridSize()
        self.ActionsGrid.EnableScrolling(False, True)
        EVT_GRID_CELL_CHANGE(self.ActionsGrid, self.OnActionsGridCellChange)

        self.AddButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGADDBUTTON, label='Add',
              name='AddButton', parent=self.MainPanel, pos=wx.Point(245, 204),
              size=wx.Size(72, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGADDBUTTON, self.OnAddButton)

        self.DeleteButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGDELETEBUTTON, label='Delete',
              name='DeleteButton', parent=self.MainPanel, pos=wx.Point(325, 204),
              size=wx.Size(72, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGDELETEBUTTON, self.OnDeleteButton)

        self.UpButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGUPBUTTON, label='^',
              name='UpButton', parent=self.MainPanel, pos=wx.Point(405, 204),
              size=wx.Size(32, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGUPBUTTON, self.OnUpButton)

        self.DownButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGDOWNBUTTON, label='v',
              name='DownButton', parent=self.MainPanel, pos=wx.Point(445, 204),
              size=wx.Size(32, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGDOWNBUTTON, self.OnDownButton)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        
        self.DefaultValue = {"Qualifier" : "N", "Duration" : "", "Type" : "Action", "Value" : "", "Indicator" : ""}
        self.Table = ActionTable(self, [], ["Qualifier","Duration","Type","Value","Indicator"])
        self.TypeList = "Action,Variable,Inline"
        self.ColSizes = [60, 90, 80, 110, 80]
        self.ColAlignements = [wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT]
        
        self.ActionsGrid.SetTable(self.Table)
        self.ActionsGrid.SetRowLabelSize(0)
        
        self.Table.ResetView(self.ActionsGrid)

    def OnAddButton(self, event):
        self.Table.AppendRow(self.DefaultValue.copy())
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnDeleteButton(self, event):
        row = self.ActionsGrid.GetGridCursorRow()
        self.Table.RemoveRow(row)
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnUpButton(self, event):
        row = self.ActionsGrid.GetGridCursorRow()
        self.Table.MoveRow(row, -1, self.ActionsGrid)
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnDownButton(self, event):
        row = self.ActionsGrid.GetGridCursorRow()
        self.Table.MoveRow(row, 1, self.ActionsGrid)
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnActionsGridCellChange(self, event):
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def SetQualifierList(self, list):
        self.QualifierList = ""
        sep = ""
        for qualifier in list.keys():
            self.QualifierList += "%s%s"%(sep, qualifier)
            sep = ","
        self.DurationList = list

    def SetVariableList(self, list):
        self.VariableList = ""
        sep = ""
        for variable in list:
            self.VariableList += "%s%s"%(sep, variable["Name"])
            sep = ","

    def SetActionList(self, list):
        self.ActionList = ""
        sep = ""
        for action in list:
            self.ActionList += "%s%s"%(sep, action)
            sep = ","

    def SetValues(self, actions):
        for action in actions:
            row = {"Qualifier" : action["qualifier"], "Value" : action["value"]}
            if action["type"] == "reference":
                if action["value"] in self.ActionList:
                    row["Type"] = "Action"
                elif action["value"] in self.VariableList:
                    row["Type"] = "Variable"
                else:
                    row["Type"] = "Inline"
            else:
                row["Type"] = "Inline"
            if "duration" in action:
                row["Duration"] = action["duration"]
            else:
                row["Duration"] = ""
            if "indicator" in action:
                row["Indicator"] = action["indicator"]
            else:
                row["Indicator"] = ""
            self.Table.AppendRow(row)
        self.Table.ResetView(self.ActionsGrid)
    
    def GetValues(self):
        values = []
        for data in self.Table.GetData():
            action = {"qualifier" : data["Qualifier"], "value" : data["Value"]}
            if data["Type"] in ["Action", "Variable"]:
                action["type"] = "reference"
            else:
                action["type"] = "inline"
            if data["Duration"] != "":
                action["duration"] = data["Duration"]
            if data["Indicator"] != "":
                action["indicator"] = data["Indicator"]
            values.append(action)
        return values


#-------------------------------------------------------------------------------
#                          Edit Step Name Dialog
#-------------------------------------------------------------------------------

class StepNameDialog(wxTextEntryDialog):

    def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", 
                       style = wxOK|wxCANCEL|wxCENTRE, pos = wxDefaultPosition):
        wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
        
        self.PouNames = []
        self.Variables = []
        self.StepNames = []
        
        EVT_BUTTON(self, self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId(), self.OnOK)
        
    def OnOK(self, event):
        step_name = self.GetSizer().GetItem(1).GetWindow().GetValue()
        if step_name == "":
            message = wxMessageDialog(self, "You must type a name!", "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif not TestIdentifier(step_name):
            message = wxMessageDialog(self, "\"%s\" is not a valid identifier!"%step_name, "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in IEC_KEYWORDS:
            message = wxMessageDialog(self, "\"%s\" is a keyword. It can't be used!"%step_name, "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.PouNames:
            message = wxMessageDialog(self, "A pou with \"%s\" as name exists!"%step_name, "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.Variables:
            message = wxMessageDialog(self, "A variable with \"%s\" as name exists!"%step_name, "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.StepNames:
            message = wxMessageDialog(self, "\"%s\" step already exists!"%step_name, "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wxID_OK)

    def SetPouNames(self, pou_names):
        self.PouNames = [pou_name.upper() for pou_name in pou_names]

    def SetVariables(self, variables):
        self.Variables = [var["Name"].upper() for var in variables]

    def SetStepNames(self, step_names):
        self.StepNames = [step_name.upper() for step_name in step_names]

    def GetValue(self):
        return self.GetSizer().GetItem(1).GetWindow().GetValue()