Dialogs.py
author lbessard
Tue, 18 Sep 2007 18:05:52 +0200
changeset 95 ee66a9a1748b
parent 80 c798a68c5560
child 96 d178cfa9e77f
permissions -rw-r--r--
Corrections for wx version 2.8.4 and Windows
#!/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) 2007: 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
#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

import wx
import wx.grid

from graphics import *

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

[ID_BLOCKPROPERTIESDIALOG, ID_BLOCKPROPERTIESDIALOGNAME, 
 ID_BLOCKPROPERTIESDIALOGTYPETREE, ID_BLOCKPROPERTIESDIALOGTYPEDESC, 
 ID_BLOCKPROPERTIESDIALOGINPUTS, ID_BLOCKPROPERTIESDIALOGPREVIEW, 
 ID_BLOCKPROPERTIESDIALOGSTATICTEXT1, ID_BLOCKPROPERTIESDIALOGSTATICTEXT2, 
 ID_BLOCKPROPERTIESDIALOGSTATICTEXT3, ID_BLOCKPROPERTIESDIALOGSTATICTEXT4, 
] = [wx.NewId() for _init_ctrls in range(10)]

[CATEGORY, BLOCK] = range(2)

class BlockPropertiesDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)

    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)

    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.LeftBoxSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)

    def _init_coll_LeftBoxSizer_Items(self, parent):
        parent.AddWindow(self.TypeTree, 3, border=5, flag=wx.GROW|wx.BOTTOM)
        parent.AddWindow(self.TypeDesc, 1, border=0, flag=wx.GROW)

    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddSizer(self.RightUpGridSizer, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText4, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)

    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(2)

    def _init_coll_RightUpGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW|wx.ALIGN_BOTTOM)
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW|wx.ALIGN_BOTTOM)
        parent.AddWindow(self.BlockName, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Inputs, 0, border=0, flag=wx.GROW)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftBoxSizer = wx.StaticBoxSizer(self.staticbox1, wx.VERTICAL)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=5)
        self.RightUpGridSizer = wx.GridSizer(cols=2, hgap=5, rows=2, vgap=5)
        
        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_LeftBoxSizer_Items(self.LeftBoxSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)
        self._init_coll_RightUpGridSizer_Items(self.RightUpGridSizer)
        
        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_BLOCKPROPERTIESDIALOG,
              name='BlockPropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(600, 360), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
              title='Block Properties')
        self.SetClientSize(wx.Size(600, 360))

        self.staticbox1 = wx.StaticBox(id=ID_BLOCKPROPERTIESDIALOGSTATICTEXT1,
              label='Type:', name='staticBox1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 0), style=0)

        self.staticText2 = wx.StaticText(id=ID_BLOCKPROPERTIESDIALOGSTATICTEXT2,
              label='Name:', name='staticText2', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

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

        self.staticText4 = wx.StaticText(id=ID_BLOCKPROPERTIESDIALOGSTATICTEXT4,
              label='Preview:', name='staticText4', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        if wx.Platform == '__WXMSW__':
            treestyle = wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER
        else:
            treestyle = wx.TR_HAS_BUTTONS|wx.TR_HIDE_ROOT|wx.TR_SINGLE|wx.SUNKEN_BORDER
        self.TypeTree = wx.TreeCtrl(id=ID_BLOCKPROPERTIESDIALOGTYPETREE,
              name='TypeTree', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER)
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTypeTreeItemSelected,
              id=ID_BLOCKPROPERTIESDIALOGTYPETREE)

        self.TypeDesc = wx.TextCtrl(id=ID_BLOCKPROPERTIESDIALOGTYPEDESC,
              name='TypeDesc', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TE_READONLY|wx.TE_MULTILINE)

        self.BlockName = wx.TextCtrl(id=ID_BLOCKPROPERTIESDIALOGNAME, value='',
              name='BlockName', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_TEXT, self.OnNameChanged, id=ID_BLOCKPROPERTIESDIALOGNAME)

        self.Inputs = wx.SpinCtrl(id=ID_BLOCKPROPERTIESDIALOGINPUTS,
              name='Inputs', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=2, max=20)
        self.Bind(wx.EVT_SPINCTRL, self.OnInputsChanged, id=ID_BLOCKPROPERTIESDIALOGINPUTS)

        self.Preview = wx.Panel(id=ID_BLOCKPROPERTIESDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        
        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.BlockName.SetValue("")
        self.BlockName.Enable(False)
        self.Inputs.Enable(False)
        self.Block = None
        self.MinBlockSize = None
        
        self.PouNames = []
        self.PouElementNames = []
    
    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()
        block_name = self.BlockName.GetValue()
        name_enabled = self.BlockName.IsEnabled()
        if not selected.IsOk() or self.TypeTree.GetItemParent(selected) == self.TypeTree.GetRootItem() or selected == self.TypeTree.GetRootItem():
            message = wx.MessageDialog(self, "Form isn't complete. Valid block type must be selected!", "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif name_enabled and block_name == "":
            message = wx.MessageDialog(self, "Form isn't complete. Name must be filled!", "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif name_enabled and not TestIdentifier(block_name):
            message = wx.MessageDialog(self, "\"%s\" is not a valid identifier!"%block_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif name_enabled and block_name.upper() in IEC_KEYWORDS:
            message = wx.MessageDialog(self, "\"%s\" is a keyword. It can't be used!"%block_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif name_enabled and block_name.upper() in self.PouNames:
            message = wx.MessageDialog(self, "\"%s\" pou already exists!"%block_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif name_enabled and block_name.upper() in self.PouElementNames:
            message = wx.MessageDialog(self, "\"%s\" element for this pou already exists!"%block_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wx.ID_OK)

    def SetBlockList(self, blocktypes):
        if wx.Platform == '__WXMSW__':
            root = self.TypeTree.AddRoot("Block Types")
        else:
            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"]])})
        if wx.Platform == '__WXMSW__':
            self.TypeTree.Expand(root)

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

    def SetPouNames(self, pou_names):
        self.PouNames = [pou_name.upper() for pou_name in pou_names]
        
    def SetPouElementNames(self, element_names):
        self.PouElementNames = [element_name.upper() for element_name in element_names]
        
    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.BlockName.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.BlockName.GetValue() != "":
            values["name"] = self.BlockName.GetValue()
        values["width"], values["height"] = self.Block.GetSize()
        values["extension"] = self.Inputs.GetValue()
        return values

    def OnTypeTreeItemSelected(self, event):
        self.BlockName.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.BlockName.Enable(blocktype["type"] != "function")
                self.TypeDesc.SetValue(blocktype["comment"])
                wx.CallAfter(self.RefreshPreview)
            else:
                self.BlockName.Enable(False)
                self.Inputs.Enable(False)
                self.Inputs.SetValue(2)
                self.TypeDesc.SetValue("")
                wx.CallAfter(self.ErasePreview)
        else:
            self.BlockName.Enable(False)
            self.Inputs.Enable(False)
            self.Inputs.SetValue(2)
            self.TypeDesc.SetValue("")
            wx.CallAfter(self.ErasePreview)
        event.Skip()

    def OnNameChanged(self, event):
        if self.BlockName.IsEnabled():
            self.RefreshPreview()
        event.Skip()
    
    def OnInputsChanged(self, event):
        if self.Inputs.IsEnabled():
            self.RefreshPreview()
        event.Skip()
    
    def ErasePreview(self):
        dc = wx.ClientDC(self.Preview)
        dc.Clear()
        self.Block = None
        
    def RefreshPreview(self):
        dc = wx.ClientDC(self.Preview)
        dc.Clear()
        item = self.TypeTree.GetSelection()
        if item.IsOk():
            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.BlockName.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()
        event.Skip()

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

[ID_VARIABLEPROPERTIESDIALOG, ID_VARIABLEPROPERTIESDIALOGSPACER, 
 ID_VARIABLEPROPERTIESDIALOGNAME, ID_VARIABLEPROPERTIESDIALOGCLASS, 
 ID_VARIABLEPROPERTIESDIALOGPREVIEW, ID_VARIABLEPROPERTIESDIALOGEXPRESSION,
 ID_VARIABLEPROPERTIESDIALOGSTATICTEXT1, ID_VARIABLEPROPERTIESDIALOGSTATICTEXT2,
 ID_VARIABLEPROPERTIESDIALOGSTATICTEXT3, ID_VARIABLEPROPERTIESDIALOGSTATICTEXT4,
] = [wx.NewId() for _init_ctrls in range(10)]

class VariablePropertiesDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
    
    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.TopSizer, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText4, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
        
    def _init_coll_MainSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(2)
    
    def _init_coll_TopSizer_Items(self, parent):
        parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
    
    def _init_coll_LeftGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Class, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Expression, 0, border=0, flag=wx.GROW)
    
    def _init_coll_LeftGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(2)
            
    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.VariableName, 0, border=0, flag=wx.GROW)
        
    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)
        
    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=5)
        self.TopSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=5, vgap=5)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_MainSizer_Growables(self.MainSizer)
        self._init_coll_TopSizer_Items(self.TopSizer)
        self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
        self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)
        
        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_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.staticText1 = wx.StaticText(id=ID_VARIABLEPROPERTIESDIALOGSTATICTEXT1,
              label='Class:', name='staticText1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText2 = wx.StaticText(id=ID_VARIABLEPROPERTIESDIALOGSTATICTEXT2,
              label='Expression:', name='staticText2', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText3 = wx.StaticText(id=ID_VARIABLEPROPERTIESDIALOGSTATICTEXT3,
              label='Name:', name='staticText3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText4 = wx.StaticText(id=ID_VARIABLEPROPERTIESDIALOGSTATICTEXT4,
              label='Preview:', name='staticText4', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.Class = wx.Choice(id=ID_VARIABLEPROPERTIESDIALOGCLASS,
              name='Class', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_CHOICE, self.OnClassChanged, id=ID_VARIABLEPROPERTIESDIALOGCLASS)
        
        self.VariableName = wx.ListBox(id=ID_VARIABLEPROPERTIESDIALOGNAME,
              name='Name', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 90), style=wx.LB_SINGLE)
        self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, id=ID_VARIABLEPROPERTIESDIALOGNAME)

        self.Expression = wx.TextCtrl(id=ID_VARIABLEPROPERTIESDIALOGEXPRESSION,
              name='Expression', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_TEXT, self.OnExpressionChanged, id=ID_VARIABLEPROPERTIESDIALOGEXPRESSION)

        self.Spacer = wx.Panel(id=ID_VARIABLEPROPERTIESDIALOGSPACER,
              name='Spacer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)

        self.Preview = wx.Panel(id=ID_VARIABLEPROPERTIESDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.Variable = None
        self.VarList = []
        self.MinVariableSize = None
        self.RefreshNameList()
        
        for choice in ["Input", "Output", "InOut"]:
            self.Class.Append(choice)
        self.Class.SetStringSelection("Input")

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

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

    def SetValues(self, values):
        value_type = values.get("type", None)
        value_name = values.get("name", None)
        if value_type:
            if value_type == INPUT:
                self.Class.SetStringSelection("Input")
            if value_type == OUTPUT:
                self.Class.SetStringSelection("Output")
            if value_type == INOUT:
                self.Class.SetStringSelection("InOut")
            self.RefreshNameList()
        if value_name:
            if self.VariableName.FindString(value_name) != wx.NOT_FOUND:
                self.VariableName.SetStringSelection(value_name)
                self.Expression.Enable(False)
            else:
                self.Expression.SetValue(value_name)
                self.VariableName.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.VariableName.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.VariableName.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.VariableName.Enable(False)
        else:
            self.VariableName.Enable(True)
        self.RefreshPreview()
        event.Skip()
    
    def RefreshPreview(self):
        dc = wx.ClientDC(self.Preview)
        dc.Clear()
        expression = self.Expression.GetValue()
        if self.Expression.IsEnabled() and expression != "":
            name = expression
        else:
            name = self.VariableName.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()
        event.Skip()

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

[ID_CONNECTIONPROPERTIESDIALOG, ID_CONNECTIONPROPERTIESDIALOGSPACER, 
 ID_CONNECTIONPROPERTIESDIALOGNAME, ID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON1, 
 ID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON2, ID_CONNECTIONPROPERTIESDIALOGPREVIEW,
 ID_CONNECTIONPROPERTIESDIALOGSTATICTEXT1, ID_CONNECTIONPROPERTIESDIALOGSTATICTEXT2, 
 ID_CONNECTIONPROPERTIESDIALOGSTATICTEXT3, 
] = [wx.NewId() for _init_ctrls in range(9)]

class ConnectionPropertiesDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
    
    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
    
    def _init_coll_LeftGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.ConnectionName, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
    
    def _init_coll_LeftGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(5)
            
    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
        
    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=5)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
        self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_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.staticText1 = wx.StaticText(id=ID_CONNECTIONPROPERTIESDIALOGSTATICTEXT1,
              label='Type:', name='staticText1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText2 = wx.StaticText(id=ID_CONNECTIONPROPERTIESDIALOGSTATICTEXT2,
              label='Name:', name='staticText2', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText3 = wx.StaticText(id=ID_CONNECTIONPROPERTIESDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

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

        self.radioButton2 = wx.RadioButton(id=ID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON2,
              label='Continuation', name='radioButton2', parent=self, 
              pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_CONNECTIONPROPERTIESDIALOGRADIOBUTTON2)
        self.radioButton2.SetValue(False)
        
        self.ConnectionName = wx.TextCtrl(id=ID_CONNECTIONPROPERTIESDIALOGNAME,
              name='Name', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_TEXT, self.OnNameChanged, id=ID_CONNECTIONPROPERTIESDIALOGNAME)

        self.Preview = wx.Panel(id=ID_CONNECTIONPROPERTIESDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self.Spacer = wx.Panel(id=ID_CONNECTIONPROPERTIESDIALOGSPACER,
              name='Spacer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        
        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.Connection = None
        self.MinConnectionSize = None
            
    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.ConnectionName.SetValue(value)
        self.RefreshPreview()
    
    def GetValues(self):
        values = {}
        if self.radioButton1.GetValue():
            values["type"] = CONNECTOR
        else:
            values["type"] = CONTINUATION
        values["name"] = self.ConnectionName.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 = wx.ClientDC(self.Preview)
        dc.Clear()
        if self.radioButton1.GetValue():
            self.Connection = FBD_Connector(self.Preview, CONNECTOR, self.ConnectionName.GetValue())
        else:
            self.Connection = FBD_Connector(self.Preview, CONTINUATION, self.ConnectionName.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()
        event.Skip()

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


[ID_LDELEMENTDIALOG, ID_LDELEMENTDIALOGSPACER, 
 ID_LDELEMENTDIALOGNAME, ID_LDELEMENTDIALOGRADIOBUTTON1, 
 ID_LDELEMENTDIALOGRADIOBUTTON2, ID_LDELEMENTDIALOGRADIOBUTTON3,
 ID_LDELEMENTDIALOGRADIOBUTTON4, ID_LDELEMENTDIALOGPREVIEW,
 ID_LDELEMENTDIALOGSTATICTEXT1, ID_LDELEMENTDIALOGSTATICTEXT2, 
 ID_LDELEMENTDIALOGSTATICTEXT3, 
] = [wx.NewId() for _init_ctrls in range(11)]

class LDElementDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
    
    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
    
    def _init_coll_LeftGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton4, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.ElementName, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
        
    def _init_coll_LeftGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(7)
            
    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
        
    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=8, vgap=5)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
        self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt, title, labels):
        wx.Dialog.__init__(self, id=ID_LDELEMENTDIALOG,
              name='LDElementDialog', 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.staticText1 = wx.StaticText(id=ID_LDELEMENTDIALOGSTATICTEXT1,
              label='Modifier:', name='staticText1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText2 = wx.StaticText(id=ID_LDELEMENTDIALOGSTATICTEXT2,
              label='Name:', name='staticText2', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText3 = wx.StaticText(id=ID_LDELEMENTDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

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

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

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

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

        self.ElementName = wx.Choice(id=ID_LDELEMENTDIALOGNAME,
              name='Name', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_CHOICE, self.OnNameChanged, id=ID_LDELEMENTDIALOGNAME)

        self.Preview = wx.Panel(id=ID_LDELEMENTDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self.Spacer = wx.Panel(id=ID_LDELEMENTDIALOGSPACER,
              name='Spacer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        
        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, "")
        
    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)
        
    def SetVariables(self, vars):
        self.ElementName.Clear()
        for name in vars:
            self.ElementName.Append(name)
        self.ElementName.Enable(self.ElementName.GetCount() > 0)

    def SetValues(self, values):
        for name, value in values.items():
            if name == "name":
                self.Element.SetName(value)
                self.ElementName.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.ElementName.GetStringSelection())
        self.RefreshPreview()
        event.Skip()

    def RefreshPreview(self):
        dc = wx.ClientDC(self.Preview)
        dc.Clear()
        clientsize = self.Preview.GetClientSize()
        width, height = self.Element.GetSize()
        self.Element.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2)
        self.Element.Draw(dc)

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


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


[ID_LDPOWERRAILDIALOG, ID_LDPOWERRAILDIALOGSPACER, 
 ID_LDPOWERRAILDIALOGTYPE, ID_LDPOWERRAILDIALOGRADIOBUTTON1, 
 ID_LDPOWERRAILDIALOGRADIOBUTTON2, ID_LDPOWERRAILDIALOGPREVIEW,
 ID_LDPOWERRAILDIALOGSTATICTEXT1, ID_LDPOWERRAILDIALOGSTATICTEXT2, 
 ID_LDPOWERRAILDIALOGSTATICTEXT3, ID_LDPOWERRAILDIALOGPINNUMBER,
] = [wx.NewId() for _init_ctrls in range(10)]

class LDPowerRailDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
    
    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
    
    def _init_coll_LeftGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.PinNumber, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
    
    def _init_coll_LeftGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(5)
            
    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
        
    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=5)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
        self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_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.staticText1 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT1,
              label='Type:', name='staticText1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

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

        self.staticText3 = wx.StaticText(id=ID_LDPOWERRAILDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

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

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

        self.PinNumber = wx.SpinCtrl(id=ID_LDPOWERRAILDIALOGPINNUMBER,
              name='PinNumber', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=1, max=20)
        self.Bind(wx.EVT_SPINCTRL, self.OnPinNumberChanged, id=ID_LDPOWERRAILDIALOGPINNUMBER)

        self.Preview = wx.Panel(id=ID_LDPOWERRAILDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self.Spacer = wx.Panel(id=ID_LDELEMENTDIALOGSPACER,
              name='Spacer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        
        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.PowerRailMinSize = (0, 0)
        self.PowerRail = None

    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 = wx.ClientDC(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)
        clientsize = self.Preview.GetClientSize()
        self.PowerRail.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2)
        self.PowerRail.Draw(dc)

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


#-------------------------------------------------------------------------------
#                          Edit Step Content Dialog
#-------------------------------------------------------------------------------

[ID_STEPCONTENTDIALOG, ID_STEPCONTENTDIALOGSPACER, 
 ID_STEPCONTENTDIALOGNAME, ID_STEPCONTENTDIALOGPREVIEW, 
 ID_STEPCONTENTDIALOGCHECKBOX1, ID_STEPCONTENTDIALOGCHECKBOX2,
 ID_STEPCONTENTDIALOGCHECKBOX3, ID_STEPCONTENTDIALOGSTATICTEXT1, 
 ID_STEPCONTENTDIALOGSTATICTEXT2, ID_STEPCONTENTDIALOGSTATICTEXT3, 
] = [wx.NewId() for _init_ctrls in range(10)]

class StepContentDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
    
    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
    
    def _init_coll_LeftGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.StepName, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.checkBox1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.checkBox2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.checkBox3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
        
    def _init_coll_LeftGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(6)
            
    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
        
    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=7, vgap=5)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
        self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_STEPCONTENTDIALOG,
              name='StepContentDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(400, 250), style=wx.DEFAULT_DIALOG_STYLE,
              title='Edit Step')
        self.SetClientSize(wx.Size(400, 250))

        self.staticText1 = wx.StaticText(id=ID_STEPCONTENTDIALOGSTATICTEXT1,
              label='Name:', name='staticText1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText2 = wx.StaticText(id=ID_STEPCONTENTDIALOGSTATICTEXT2,
              label='Connectors:', name='staticText2', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText3 = wx.StaticText(id=ID_STEPCONTENTDIALOGSTATICTEXT3,
              label='Preview:', name='staticText4', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.StepName = wx.TextCtrl(id=ID_STEPCONTENTDIALOGNAME,
              name='Name', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_TEXT, self.OnNameChanged, id=ID_STEPCONTENTDIALOGNAME)

        self.checkBox1 = wx.CheckBox(id=ID_STEPCONTENTDIALOGCHECKBOX1,
              label="Input", name='checkBox1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, id=ID_STEPCONTENTDIALOGCHECKBOX1)
        
        self.checkBox2 = wx.CheckBox(id=ID_STEPCONTENTDIALOGCHECKBOX2,
              label="Output", name='checkBox2', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, id=ID_STEPCONTENTDIALOGCHECKBOX2)
        
        self.checkBox3 = wx.CheckBox(id=ID_STEPCONTENTDIALOGCHECKBOX3,
              label="Action", name='checkBox3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, id=ID_STEPCONTENTDIALOGCHECKBOX3)
        
        self.Spacer = wx.Panel(id=ID_STEPCONTENTDIALOGSPACER,
              name='Spacer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)

        self.Preview = wx.Panel(id=ID_STEPCONTENTDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
        setattr(self.Preview, "RefreshStepModel", lambda x:None)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self._init_sizers()

    def __init__(self, parent, initial = False):
        self._init_ctrls(parent)
        self.Step = None
        self.Initial = initial
        self.MinStepSize = None
    
        self.PouNames = []
        self.Variables = []
        self.StepNames = []
    
    def OnOK(self, event):
        step_name = self.StepName.GetValue()
        if step_name == "":
            message = wx.MessageDialog(self, "You must type a name!", "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif not TestIdentifier(step_name):
            message = wx.MessageDialog(self, "\"%s\" is not a valid identifier!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in IEC_KEYWORDS:
            message = wx.MessageDialog(self, "\"%s\" is a keyword. It can't be used!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.PouNames:
            message = wx.MessageDialog(self, "A pou with \"%s\" as name exists!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.Variables:
            message = wx.MessageDialog(self, "A variable with \"%s\" as name exists!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.StepNames:
            message = wx.MessageDialog(self, "\"%s\" step already exists!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wx.ID_OK)
    
    def SetMinStepSize(self, size):
        self.MinStepSize = size

    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 SetValues(self, values):
        value_name = values.get("name", None)
        if value_name:
            self.StepName.SetValue(value_name)
        else:
            self.StepName.SetValue("")
        self.checkBox1.SetValue(values.get("input", False))
        self.checkBox2.SetValue(values.get("output", False))
        self.checkBox3.SetValue(values.get("action", False))
        self.RefreshPreview()
        
    def GetValues(self):
        values = {}
        values["name"] = self.StepName.GetValue()
        values["input"] = self.checkBox1.IsChecked()
        values["output"] = self.checkBox2.IsChecked()
        values["action"] = self.checkBox3.IsChecked()
        values["width"], values["height"] = self.Step.GetSize()
        return values
    
    def OnConnectorsChanged(self, event):
        self.RefreshPreview()
        event.Skip()

    def OnNameChanged(self, event):
        self.RefreshPreview()
        event.Skip()
    
    def RefreshPreview(self):
        dc = wx.ClientDC(self.Preview)
        dc.Clear()
        self.Step = SFC_Step(self.Preview, self.StepName.GetValue(), self.Initial)
        if self.checkBox1.IsChecked():
            self.Step.AddInput()
        else:
            self.Step.RemoveInput()
        if self.checkBox2.IsChecked():
            self.Step.AddOutput()
        else:
            self.Step.RemoveOutput()
        if self.checkBox3.IsChecked():
            self.Step.AddAction()    
        else:
            self.Step.RemoveAction()
        width, height = self.MinStepSize
        min_width, min_height = self.Step.GetMinSize()
        width, height = max(min_width, width), max(min_height, height)
        self.Step.SetSize(width, height)
        clientsize = self.Preview.GetClientSize()
        x = (clientsize.width - width) / 2
        y = (clientsize.height - height) / 2
        self.Step.SetPosition(x, y)
        self.Step.Draw(dc)

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

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

[ID_TRANSITIONCONTENTDIALOG, ID_TRANSITIONCONTENTDIALOGSPACER, 
 ID_TRANSITIONCONTENTDIALOGREFERENCE, ID_TRANSITIONCONTENTDIALOGINLINE, 
 ID_TRANSITIONCONTENTDIALOGPRIORITY, ID_TRANSITIONCONTENTDIALOGPREVIEW, 
 ID_TRANSITIONCONTENTDIALOGRADIOBUTTON1, ID_TRANSITIONCONTENTDIALOGRADIOBUTTON2, 
 ID_TRANSITIONCONTENTDIALOGRADIOBUTTON3, ID_TRANSITIONCONTENTDIALOGSTATICTEXT1, 
 ID_TRANSITIONCONTENTDIALOGSTATICTEXT2, ID_TRANSITIONCONTENTDIALOGSTATICTEXT3, 
] = [wx.NewId() for _init_ctrls in range(12)]

class TransitionContentDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
    
    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
    
    def _init_coll_LeftGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Reference, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Inline, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Priority, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
    
    def _init_coll_LeftGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(6)
            
    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
        
    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=9, vgap=5)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
        self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_TRANSITIONCONTENTDIALOG,
              name='ProjectDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(350, 300), style=wx.DEFAULT_DIALOG_STYLE,
              title='Edit transition')
        self.SetClientSize(wx.Size(350, 300))

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

        self.staticText2 = wx.StaticText(id=ID_TRANSITIONCONTENTDIALOGSTATICTEXT2,
              label='Preview:', name='staticText2', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.staticText3 = wx.StaticText(id=ID_TRANSITIONCONTENTDIALOGSTATICTEXT3,
              label='Priority:', name='staticText3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
        
        self.radioButton1 = wx.RadioButton(id=ID_TRANSITIONCONTENTDIALOGRADIOBUTTON1,
              label='Reference', name='radioButton1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_TRANSITIONCONTENTDIALOGRADIOBUTTON1)
        self.radioButton1.SetValue(True)

        self.Reference = wx.Choice(id=ID_TRANSITIONCONTENTDIALOGREFERENCE,
              name='Reference', parent=self, pos=wx.Point(0, 0), 
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_CHOICE, self.OnReferenceChanged, id=ID_TRANSITIONCONTENTDIALOGREFERENCE)

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

        self.Inline = wx.TextCtrl(id=ID_TRANSITIONCONTENTDIALOGINLINE,
              name='Inline', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_TEXT, self.OnInlineChanged, id=ID_TRANSITIONCONTENTDIALOGINLINE)

        self.radioButton3 = wx.RadioButton(id=ID_TRANSITIONCONTENTDIALOGRADIOBUTTON3,
              label='Connection', name='radioButton3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
        self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_TRANSITIONCONTENTDIALOGRADIOBUTTON3)
        self.radioButton3.SetValue(False)
        if not self.Connection:
            self.radioButton3.Hide()

        self.Priority = wx.SpinCtrl(id=ID_TRANSITIONCONTENTDIALOGPRIORITY,
              name='Priority', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=0)
        self.Bind(wx.EVT_TEXT, self.OnPriorityChanged, id=ID_TRANSITIONCONTENTDIALOGPRIORITY)

        self.Preview = wx.Panel(id=ID_TRANSITIONCONTENTDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
        setattr(self.Preview, "RefreshTransitionModel", lambda x:None)

        self.Spacer = wx.Panel(id=ID_TRANSITIONCONTENTDIALOGSPACER,
              name='Spacer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        
        self._init_sizers()

    def __init__(self, parent, connection):
        self.Connection = connection
        self._init_ctrls(parent)
        self.Transition = None
        self.MinTransitionSize = None
        
        self.Element = SFC_Transition(self.Preview)
        
    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)
    
    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 = wx.MessageDialog(self, "Form isn't complete. %s must be filled!"%text, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wx.ID_OK)

    def OnTypeChanged(self, event):
        if self.radioButton1.GetValue():
            self.Element.SetType("reference", self.Reference.GetStringSelection())
            self.Reference.Enable(True)
            self.Inline.Enable(False)
        elif self.radioButton2.GetValue():
            self.Element.SetType("inline", self.Inline.GetValue())
            self.Reference.Enable(False)
            self.Inline.Enable(True)
        else:
            self.Element.SetType("connection")
            self.Reference.Enable(False)
            self.Inline.Enable(False)
        self.RefreshPreview()
        event.Skip()

    def OnReferenceChanged(self, event):
        self.Element.SetType("reference", self.Reference.GetStringSelection())
        self.RefreshPreview()
        event.Skip()

    def OnInlineChanged(self, event):
        self.Element.SetType("inline", self.Inline.GetValue())
        self.RefreshPreview()
        event.Skip()

    def OnPriorityChanged(self, event):
        self.Element.SetPriority(int(self.Priority.GetValue()))
        self.RefreshPreview()
        event.Skip()

    def SetTransitions(self, transitions):
        self.Reference.Append("")
        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.radioButton3.SetValue(False)
            self.Reference.Enable(True)
            self.Inline.Enable(False)
            self.Reference.SetStringSelection(values["value"])
            self.Element.SetType("reference", values["value"])
        elif values["type"] == "inline":
            self.radioButton1.SetValue(False)
            self.radioButton2.SetValue(True)
            self.radioButton3.SetValue(False)
            self.Reference.Enable(False)
            self.Inline.Enable(True)
            self.Inline.SetValue(values["value"])
            self.Element.SetType("inline", values["value"])
        elif values["type"] == "connection" and self.Connection:
            self.radioButton1.SetValue(False)
            self.radioButton2.SetValue(False)
            self.radioButton3.SetValue(True)
            self.Reference.Enable(False)
            self.Inline.Enable(False)
            self.Element.SetType("connection")
        self.Element.SetPriority(values["priority"])
        self.RefreshPreview()
        
    def GetValues(self):
        values = {"priority" : int(self.Priority.GetValue())}
        if self.radioButton1.GetValue():
            values["type"] = "reference"
            values["value"] = self.Reference.GetStringSelection()
        elif self.radioButton2.GetValue():
            values["type"] = "inline"
            values["value"] = self.Inline.GetValue()
        else:
            values["type"] = "connection"
            values["value"] = None
        return values

    def RefreshPreview(self):
        dc = wx.ClientDC(self.Preview)
        dc.Clear()
        clientsize = self.Preview.GetClientSize()
        posx, posy = self.Element.GetPosition()
        rect = self.Element.GetBoundingBox()
        diffx, diffy = posx - rect.x, posy - rect.y
        self.Element.SetPosition((clientsize.width - rect.width) / 2 + diffx, (clientsize.height - rect.height) / 2 + diffy)
        self.Element.Draw(dc)

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

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

[ID_DIVERGENCECREATEDIALOG, ID_DIVERGENCECREATEDIALOGSPACER, 
 ID_DIVERGENCECREATEDIALOGRADIOBUTTON1, ID_DIVERGENCECREATEDIALOGRADIOBUTTON2,
 ID_DIVERGENCECREATEDIALOGRADIOBUTTON3, ID_DIVERGENCECREATEDIALOGRADIOBUTTON4, 
 ID_DIVERGENCECREATEDIALOGSEQUENCES, ID_DIVERGENCECREATEDIALOGPREVIEW, 
 ID_DIVERGENCECREATEDIALOGSTATICTEXT1, ID_DIVERGENCECREATEDIALOGSTATICTEXT2, 
 ID_DIVERGENCECREATEDIALOGSTATICTEXT3,  
] = [wx.NewId() for _init_ctrls in range(11)]

class DivergenceCreateDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
    
    def _init_coll_MainSizer_Items(self, parent):
        parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT)
        parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT)
    
    def _init_coll_LeftGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.radioButton4, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Sequences, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW)
    
    def _init_coll_LeftGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(7)
            
    def _init_coll_RightGridSizer_Items(self, parent):
        parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW)
        
    def _init_coll_RightGridSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
        self.MainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=8, vgap=5)
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_MainSizer_Items(self.MainSizer)
        self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
        self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
        self._init_coll_RightGridSizer_Items(self.RightGridSizer)
        self._init_coll_RightGridSizer_Growables(self.RightGridSizer)

        self.SetSizer(self.flexGridSizer1)
    
    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_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, 300))

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

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

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

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

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

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

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

        self.staticText3 = wx.StaticText(id=ID_DIVERGENCECREATEDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)

        self.Preview = wx.Panel(id=ID_DIVERGENCECREATEDIALOGPREVIEW,
              name='Preview', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)

        self.Spacer = wx.Panel(id=ID_TRANSITIONCONTENTDIALOGSPACER,
              name='Spacer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        
        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.Divergence = None
        self.MinSize = (0, 0)
        
    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 = wx.ClientDC(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()
        event.Skip()


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

class ActionTable(wx.grid.PyGridTableBase):
    
    """
    A custom wx.Grid Table using user supplied data
    """
    def __init__(self, parent, data, colnames):
        # The base class must be initialized *first*
        wx.grid.PyGridTableBase.__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):
        """
        (wx.Grid) -> 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(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
            (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
        ]:
            if new < current:
                msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
                grid.ProcessTableMessage(msg)
            elif new > current:
                msg = wx.grid.GridTableMessage(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 = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
        grid.ProcessTableMessage(msg)

    def _updateColAttrs(self, grid):
        """
        wx.Grid -> 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 = wx.grid.GridCellAttr()
            attr.SetAlignment(self.Parent.ColAlignements[col], wx.ALIGN_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 = wx.grid.GridCellChoiceEditor()
                    editor.SetParameters(self.Parent.QualifierList)
                if colname == "Duration":
                    editor = wx.grid.GridCellTextEditor()
                    renderer = wx.grid.GridCellStringRenderer()
                    if self.Parent.DurationList[self.data[row]["Qualifier"]]:
                        readonly = False
                    else:
                        readonly = True
                        self.data[row]["Duration"] = ""
                elif colname == "Type":
                    editor = wx.grid.GridCellChoiceEditor()
                    editor.SetParameters(self.Parent.TypeList)
                elif colname == "Value":
                    type = self.data[row]["Type"]
                    if type == "Action":
                        editor = wx.grid.GridCellChoiceEditor()
                        editor.SetParameters(self.Parent.ActionList)
                    elif type == "Variable":
                        editor = wx.grid.GridCellChoiceEditor()
                        editor.SetParameters(self.Parent.VariableList)
                    elif type == "Inline":
                        editor = wx.grid.GridCellTextEditor()
                        renderer = wx.grid.GridCellStringRenderer()
                elif colname == "Indicator":
                    editor = wx.grid.GridCellChoiceEditor()
                    editor.SetParameters(self.Parent.VariableList)
                    
                grid.SetCellEditor(row, col, editor)
                grid.SetCellRenderer(row, col, renderer)
                grid.SetReadOnly(row, col, readonly)
                
                grid.SetCellBackgroundColour(row, col, wx.WHITE)
    
    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 = []

[ID_ACTIONBLOCKDIALOG, ID_ACTIONBLOCKDIALOGVARIABLESGRID, 
 ID_ACTIONBLOCKDIALOGSTATICTEXT1, ID_ACTIONBLOCKDIALOGADDBUTTON,
 ID_ACTIONBLOCKDIALOGDELETEBUTTON, ID_ACTIONBLOCKDIALOGUPBUTTON, 
 ID_ACTIONBLOCKDIALOGDOWNBUTTON, 
] = [wx.NewId() for _init_ctrls in range(7)]

class ActionBlockDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        parent.AddSizer(self.TopSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.GridButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT)
        parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
    def _init_coll_flexGridSizer1_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(0)
        
    def _init_coll_TopSizer_Items(self, parent):
        parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
        parent.AddWindow(self.ActionsGrid, 0, border=0, flag=wx.GROW)
    
    def _init_coll_TopSizer_Growables(self, parent):
        parent.AddGrowableCol(0)
        parent.AddGrowableRow(1)

    def _init_coll_GridButtonSizer_Items(self, parent):
        parent.AddWindow(self.AddButton, 0, border=10, flag=wx.GROW|wx.LEFT)
        parent.AddWindow(self.DeleteButton, 0, border=10, flag=wx.GROW|wx.LEFT)
        parent.AddWindow(self.UpButton, 0, border=10, flag=wx.GROW|wx.LEFT)
        parent.AddWindow(self.DownButton, 0, border=10, flag=wx.GROW|wx.LEFT)

    def _init_sizers(self):
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
        self.TopSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
        self.GridButtonSizer = wx.BoxSizer(wx.HORIZONTAL)
        
        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
        self._init_coll_TopSizer_Items(self.TopSizer)
        self._init_coll_TopSizer_Growables(self.TopSizer)
        self._init_coll_GridButtonSizer_Items(self.GridButtonSizer)
        
        self.SetSizer(self.flexGridSizer1)
    
    def _init_ctrls(self, prnt):
        wx.Dialog.__init__(self, id=ID_ACTIONBLOCKDIALOG,
              name='ActionBlockDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(500, 300), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
              title='Edit action block properties')
        self.SetClientSize(wx.Size(500, 300))

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

        self.ActionsGrid = wx.grid.Grid(id=ID_ACTIONBLOCKDIALOGVARIABLESGRID,
              name='ActionsGrid', parent=self, pos=wx.Point(0, 0), 
              size=wx.Size(0, 0), style=wx.VSCROLL)
        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)
        self.ActionsGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnActionsGridCellChange)

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

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

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

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

        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        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 = [wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_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(wx.TextEntryDialog):

    def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", 
                       style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition):
        wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
        
        self.PouNames = []
        self.Variables = []
        self.StepNames = []
        
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId())
        
    def OnOK(self, event):
        step_name = self.GetSizer().GetItem(1).GetWindow().GetValue()
        if step_name == "":
            message = wx.MessageDialog(self, "You must type a name!", "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif not TestIdentifier(step_name):
            message = wx.MessageDialog(self, "\"%s\" is not a valid identifier!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in IEC_KEYWORDS:
            message = wx.MessageDialog(self, "\"%s\" is a keyword. It can't be used!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.PouNames:
            message = wx.MessageDialog(self, "A pou with \"%s\" as name exists!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.Variables:
            message = wx.MessageDialog(self, "A variable with \"%s\" as name exists!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        elif step_name.upper() in self.StepNames:
            message = wx.MessageDialog(self, "\"%s\" step already exists!"%step_name, "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wx.ID_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()