Viewer.py
author lbessard
Fri, 09 Jan 2009 17:09:59 +0100
changeset 297 e837b67cb184
parent 292 800e100038ae
child 299 15669fe26e56
permissions -rw-r--r--
Adding help menu for inserting complex variable in graphical viewer
#!/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
if wx.VERSION >= (2, 8, 0):
    USE_AUI = True
else:
    USE_AUI = False

from plcopen.structures import *
from PLCControler import ITEM_POU

from Dialogs import *

from types import TupleType

SCROLLBAR_UNIT = 10
WINDOW_BORDER = 10
SCROLL_ZONE = 10

def AppendMenu(parent, help, id, kind, text):
    if wx.VERSION >= (2, 6, 0):
        parent.Append(help=help, id=id, kind=kind, text=text)
    else:
        parent.Append(helpString=help, id=id, kind=kind, item=text)

if wx.Platform == '__WXMSW__':
    faces = { 'times': 'Times New Roman',
              'mono' : 'Courier New',
              'helv' : 'Arial',
              'other': 'Comic Sans MS',
              'size' : 20,
             }
else:
    faces = { 'times': 'Times',
              'mono' : 'Courier',
              'helv' : 'Helvetica',
              'other': 'new century schoolbook',
              'size' : 20,
             }

#-------------------------------------------------------------------------------
#                       Graphic elements Viewer base class
#-------------------------------------------------------------------------------

# ID Constants for alignment menu items
[ID_VIEWERALIGNMENTMENUITEMS0, ID_VIEWERALIGNMENTMENUITEMS1,
 ID_VIEWERALIGNMENTMENUITEMS2, ID_VIEWERALIGNMENTMENUITEMS4,
 ID_VIEWERALIGNMENTMENUITEMS5, ID_VIEWERALIGNMENTMENUITEMS6,
] = [wx.NewId() for _init_coll_AlignmentMenu_Items in range(6)]

# ID Constants for contextual menu items
[ID_VIEWERCONTEXTUALMENUITEMS0, ID_VIEWERCONTEXTUALMENUITEMS1,
 ID_VIEWERCONTEXTUALMENUITEMS2, ID_VIEWERCONTEXTUALMENUITEMS3,
 ID_VIEWERCONTEXTUALMENUITEMS5, ID_VIEWERCONTEXTUALMENUITEMS6,
 ID_VIEWERCONTEXTUALMENUITEMS8, ID_VIEWERCONTEXTUALMENUITEMS9,
 ID_VIEWERCONTEXTUALMENUITEMS11, ID_VIEWERCONTEXTUALMENUITEMS12,
 ID_VIEWERCONTEXTUALMENUITEMS14, ID_VIEWERCONTEXTUALMENUITEMS16,
 ID_VIEWERCONTEXTUALMENUITEMS17,
] = [wx.NewId() for _init_coll_ContextualMenu_Items in range(13)]


class ViewerDropTarget(wx.TextDropTarget):
    
    def __init__(self, parent):
        wx.TextDropTarget.__init__(self)
        self.ParentWindow = parent
    
    def OnDropText(self, x, y, data):
        x, y = self.ParentWindow.CalcUnscrolledPosition(x, y)
        scaling = self.ParentWindow.Scaling
        message = None
        try:
            values = eval(data)
        except:
            message = "Invalid value \"%s\" for viewer block"%data
            values = None
        if not isinstance(values, TupleType):
            message = "Invalid value \"%s\" for viewer block"%data
            values = None
        if values is not None:
            if values[1] == "debug":
                pass
            elif values[1] == "program":
                message = "Programs can't be used by other POUs!"
            elif values[1] in ["function", "functionBlock", "program"]:
                name, type = self.ParentWindow.Controler.GetEditedElementType(self.ParentWindow.GetTagName(), self.ParentWindow.Debug)
                words = self.ParentWindow.TagName.split("::")
                if name == values[0]:
                    message = "\"%s\" can't use itself!"%name
                elif type == "function" and values[1] != "function":
                    message = "Function Blocks can't be used in Functions!"
                elif words[0] == "T" and values[1] != "function":
                    message = "Function Blocks can't be used in Transitions!"
                elif self.ParentWindow.Controler.PouIsUsedBy(name, values[0], self.ParentWindow.Debug):
                    message = "\"%s\" is already used by \"%s\"!"%(name, values[0])
                else:
                    blockname = values[2]
                    if len(values) > 3:
                        blockinputs = values[3]
                    else:
                        blockinputs = None
                    if values[1] != "function" and blockname == "":
                        dialog = wx.TextEntryDialog(self.ParentWindow.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE)
                        if dialog.ShowModal() == wx.ID_OK:
                            blockname = dialog.GetValue()
                        else:
                            return
                        dialog.Destroy()
                    if blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
                        message = "\"%s\" pou already exists!"%blockname
                    elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(self.ParentWindow.GetTagName(), self.ParentWindow.Debug)]:
                        message = "\"%s\" element for this pou already exists!"%blockname
                    else:
                        id = self.ParentWindow.GetNewId()
                        block = FBD_Block(self.ParentWindow, values[0], blockname, id, inputs = blockinputs)
                        width, height = block.GetMinSize()
                        if scaling is not None:
                            x = round(float(x) / float(scaling[0])) * scaling[0]
                            y = round(float(y) / float(scaling[1])) * scaling[1]
                            width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
                            height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
                        block.SetPosition(x, y)
                        block.SetSize(width, height)
                        self.ParentWindow.AddBlock(block)
                        self.ParentWindow.Controler.AddEditedElementBlock(self.ParentWindow.GetTagName(), id, values[0], blockname)
                        self.ParentWindow.RefreshBlockModel(block)
                        self.ParentWindow.RefreshBuffer()
                        self.ParentWindow.RefreshScrollBars()
                        self.ParentWindow.RefreshVisibleElements()
                        self.ParentWindow.ParentWindow.RefreshVariablePanel(self.ParentWindow.GetTagName())
                        self.ParentWindow.Refresh(False)
            elif values[1] != "location":
                tagname = self.ParentWindow.GetTagName()
                if values[3] == tagname:
                    if values[1] == "Output":
                        var_class = OUTPUT
                    elif values[1] == "InOut":
                        var_class = INPUT
                    else:
                        var_class = INPUT
                    tree = dict([(var["Name"], var["Tree"]) for var in self.ParentWindow.Controler.GetEditedElementInterfaceVars(tagname, self.ParentWindow.Debug)]).get(values[0], None)
                    if tree is not None:
                        if len(tree) > 0:
                            menu = wx.Menu(title='')
                            self.GenerateTreeMenu(x, y, scaling, menu, "", var_class, [(values[0], values[2], tree)])
                            self.ParentWindow.PopupMenuXY(menu)
                        else:
                            self.AddParentVariableBlock(x, y, scaling, var_class, values[0], values[2])
                    else:
                        message = "Unknown variable \"%s\" this POU!" % values[0]
                else:
                    message = "Variable don't belong to this POU!"
        if message is not None:
            wx.CallAfter(self.ShowMessage, message)

    def GenerateTreeMenu(self, x, y, scaling, menu, base_path, var_class, tree):
        for child_name, child_type, child_tree in tree:
            if base_path:
                child_path = "%s.%s" % (base_path, child_name)
            else:
                child_path = child_name
            if len(child_tree) == 1 and isinstance(child_tree[0], ListType):
                child_path += "[0]"
                child_name += "[]"
                child_tree = child_tree[0]
            new_id = wx.NewId()
            AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=child_name)
            self.ParentWindow.Bind(wx.EVT_MENU, self.GetAddVariableBlockFunction(x, y, scaling, var_class, child_path, child_type), id=new_id)
            if len(child_tree) > 0:
                new_id = wx.NewId()
                child_menu = wx.Menu(title='')
                self.GenerateTreeMenu(x, y, scaling, child_menu, child_path, var_class, child_tree)
                menu.AppendMenu(new_id, "%s." % child_name, child_menu)
            
    def GetAddVariableBlockFunction(self, x, y, scaling, var_class, var_name, var_type):
        def AddVariableFunction(event):
            self.AddParentVariableBlock(x, y, scaling, var_class, var_name, var_type)
        return AddVariableFunction
    
    def AddParentVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
        id = self.ParentWindow.GetNewId()
        variable = FBD_Variable(self.ParentWindow, var_class, var_name, var_type, id)
        width, height = variable.GetMinSize()
        if scaling is not None:
            x = round(float(x) / float(scaling[0])) * scaling[0]
            y = round(float(y) / float(scaling[1])) * scaling[1]
            width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
            height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
        variable.SetPosition(x, y)
        variable.SetSize(width, height)
        self.ParentWindow.AddBlock(variable)
        self.ParentWindow.Controler.AddEditedElementVariable(self.ParentWindow.GetTagName(), id, var_class)
        self.ParentWindow.RefreshVariableModel(variable)
        self.ParentWindow.RefreshBuffer()
        self.ParentWindow.RefreshScrollBars()
        self.ParentWindow.RefreshVisibleElements()
        self.ParentWindow.Refresh(False)
    
    def ShowMessage(self, message):
        message = wx.MessageDialog(self.ParentWindow, message, "Error", wx.OK|wx.ICON_ERROR)
        message.ShowModal()
        message.Destroy()


"""
Class that implements a Viewer based on a wx.ScrolledWindow for drawing and 
manipulating graphic elements
"""

class Viewer(wx.ScrolledWindow):
    
    if wx.VERSION < (2, 6, 0):
        def Bind(self, event, function, id = None):
            if id is not None:
                event(self, id, function)
            else:
                event(self, function)
    
    # Create Alignment Menu items
    def _init_coll_AlignmentMenu_Items(self, parent):
        # Create menu items
        AppendMenu(parent, help='', id=ID_VIEWERALIGNMENTMENUITEMS0,
              kind=wx.ITEM_NORMAL, text=u'Left')
        AppendMenu(parent, help='', id=ID_VIEWERALIGNMENTMENUITEMS1,
              kind=wx.ITEM_NORMAL, text=u'Center')
        AppendMenu(parent, help='', id=ID_VIEWERALIGNMENTMENUITEMS2,
              kind=wx.ITEM_NORMAL, text=u'Right')
        parent.AppendSeparator()
        AppendMenu(parent, help='', id=ID_VIEWERALIGNMENTMENUITEMS4,
              kind=wx.ITEM_NORMAL, text=u'Top')
        AppendMenu(parent, help='', id=ID_VIEWERALIGNMENTMENUITEMS5,
              kind=wx.ITEM_NORMAL, text=u'Middle')
        AppendMenu(parent, help='', id=ID_VIEWERALIGNMENTMENUITEMS6,
              kind=wx.ITEM_NORMAL, text=u'Bottom')
        # Link menu event to corresponding called functions
        self.Bind(wx.EVT_MENU, self.OnAlignLeftMenu,
              id=ID_VIEWERALIGNMENTMENUITEMS0)
        self.Bind(wx.EVT_MENU, self.OnAlignCenterMenu,
              id=ID_VIEWERALIGNMENTMENUITEMS1)
        self.Bind(wx.EVT_MENU, self.OnAlignRightMenu,
              id=ID_VIEWERALIGNMENTMENUITEMS2)
        self.Bind(wx.EVT_MENU, self.OnAlignTopMenu,
              id=ID_VIEWERALIGNMENTMENUITEMS4)
        self.Bind(wx.EVT_MENU, self.OnAlignMiddleMenu,
              id=ID_VIEWERALIGNMENTMENUITEMS5)
        self.Bind(wx.EVT_MENU, self.OnAlignBottomMenu,
              id=ID_VIEWERALIGNMENTMENUITEMS6)
    
    # Create Contextual Menu items
    def _init_coll_ContextualMenu_Items(self, parent):
        # Create menu items
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS0,
              kind=wx.ITEM_RADIO, text=u'No Modifier')
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS1,
              kind=wx.ITEM_RADIO, text=u'Negated')
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS2,
              kind=wx.ITEM_RADIO, text=u'Rising Edge')
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS3,
              kind=wx.ITEM_RADIO, text=u'Falling Edge')
        parent.AppendSeparator()
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS5,
              kind=wx.ITEM_NORMAL, text=u'Add Wire Segment')
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS6,
              kind=wx.ITEM_NORMAL, text=u'Delete Wire Segment')
        parent.AppendSeparator()
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS8,
              kind=wx.ITEM_NORMAL, text=u'Add Divergence Branch')
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS9,
              kind=wx.ITEM_NORMAL, text=u'Delete Divergence Branch')
        parent.AppendSeparator()
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS11,
              kind=wx.ITEM_NORMAL, text=u'Clear Execution Order')
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS12,
              kind=wx.ITEM_NORMAL, text=u'Reset Execution Order')
        parent.AppendSeparator()
        parent.AppendMenu(ID_VIEWERCONTEXTUALMENUITEMS14, "Alignment", self.AlignmentMenu)
        parent.AppendSeparator()
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS16,
              kind=wx.ITEM_NORMAL, text=u'Edit Block')
        AppendMenu(parent, help='', id=ID_VIEWERCONTEXTUALMENUITEMS17,
              kind=wx.ITEM_NORMAL, text=u'Delete')
        # Link menu event to corresponding called functions
        self.Bind(wx.EVT_MENU, self.OnNoModifierMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS0)
        self.Bind(wx.EVT_MENU, self.OnNegatedMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS1)
        self.Bind(wx.EVT_MENU, self.OnRisingEdgeMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS2)
        self.Bind(wx.EVT_MENU, self.OnFallingEdgeMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS3)
        self.Bind(wx.EVT_MENU, self.OnAddSegmentMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS5)
        self.Bind(wx.EVT_MENU, self.OnDeleteSegmentMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS6)
        self.Bind(wx.EVT_MENU, self.OnAddBranchMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS8)
        self.Bind(wx.EVT_MENU, self.OnDeleteBranchMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS9)
        self.Bind(wx.EVT_MENU, self.OnClearExecutionOrderMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS11)
        self.Bind(wx.EVT_MENU, self.OnResetExecutionOrderMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS12)
        self.Bind(wx.EVT_MENU, self.OnEditBlockMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS16)
        self.Bind(wx.EVT_MENU, self.OnDeleteMenu,
              id=ID_VIEWERCONTEXTUALMENUITEMS17)
    
    # Create and initialize Contextual Menu
    def _init_menus(self):
        self.AlignmentMenu = wx.Menu(title='')
        self.ContextualMenu = wx.Menu(title='')
        
        self._init_coll_AlignmentMenu_Items(self.AlignmentMenu)
        self._init_coll_ContextualMenu_Items(self.ContextualMenu)
    
    # Create a new Viewer
    def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""):
        wx.ScrolledWindow.__init__(self, parent, pos=wx.Point(0, 0), size=wx.Size(0, 0), 
            style=wx.HSCROLL | wx.VSCROLL)
        self._init_menus()
        # Adding a rubberband to Viewer
        self.rubberBand = RubberBand(drawingSurface=self)
        self.SetBackgroundColour(wx.Colour(255,255,255))
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.ResetView()
        self.Scaling = None
        self.DrawGrid = True
        self.GridBrush = wx.TRANSPARENT_BRUSH
        self.PageSize = None
        self.PagePen = wx.TRANSPARENT_PEN
        self.DrawingWire = False
        self.current_id = 0
        self.TagName = tagname
        self.Errors = []
        self.Debug = debug
        self.InstancePath = instancepath
        
        # Initialize Block, Wire and Comment numbers
        self.block_id = self.wire_id = self.comment_id = 0
        
        # Initialize Viewer mode to Selection mode
        self.Mode = MODE_SELECTION
        self.SavedMode = False
        self.CurrentLanguage = "FBD"
        
        self.ParentWindow = window
        self.Controler = controler
        
        if not self.Debug:
            self.SetDropTarget(ViewerDropTarget(self))
        
        dc = wx.ClientDC(self)
        font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"])
        dc.SetFont(font)
        width, height = dc.GetTextExtent("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        while width > 260:
            faces["size"] -= 1
            font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"])
            dc.SetFont(font)
            width, height = dc.GetTextExtent("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        self.SetFont(font)
        
        self.ResetView()
        
        # Link Viewer event to corresponding methods
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnViewerLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnViewerLeftUp)
        self.Bind(wx.EVT_LEFT_DCLICK, self.OnViewerLeftDClick)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnViewerRightDown)
        self.Bind(wx.EVT_RIGHT_UP, self.OnViewerRightUp)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveViewer)
        self.Bind(wx.EVT_MOTION, self.OnViewerMotion)
        self.Bind(wx.EVT_CHAR, self.OnChar)
        self.Bind(wx.EVT_SCROLLWIN, self.OnScrollWindow)
        self.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheelWindow)
        self.Bind(wx.EVT_SIZE, self.OnMoveWindow)
    
    def GetScrolledRect(self, rect):
        rect.x, rect.y = self.CalcScrolledPosition(rect.x, rect.y)
        return rect
    
    def GetScaling(self):
        return self.Scaling
    
    def SetTagName(self, tagname):
        self.TagName = tagname
        
    def GetTagName(self):
        return self.TagName
    
    def GetInstancePath(self):
        return self.InstancePath
    
    def IsViewing(self, tagname):
        if self.Debug:
            return self.InstancePath == tagname
        else:
            return self.TagName == tagname
    
    # Returns a new id
    def GetNewId(self):
        self.current_id += 1
        return self.current_id
    
    # Destructor
    def __del__(self):
        self.Flush()
        self.ResetView()

    def GetLogicalDC(self, buffered=False):
        if buffered:
            dc = wx.AutoBufferedPaintDC(self)
        else:
            dc = wx.ClientDC(self)
        dc.SetFont(self.GetFont())
        if wx.VERSION >= (2, 6, 0):
            self.DoPrepareDC(dc)
        else:
            self.PrepareDC(dc)
        return dc

    def GetMiniFont(self):
        font = self.GetFont()
        return wx.Font(font.GetPointSize() * 0.75, wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["helv"])

#-------------------------------------------------------------------------------
#                         Element management functions
#-------------------------------------------------------------------------------

    def AddBlock(self, block):
        self.block_id += 1
        self.Blocks[block] = self.block_id
        
    def AddWire(self, wire):
        self.wire_id += 1
        self.Wires[wire] = self.wire_id
        
    def AddComment(self, comment):
        self.comment_id += 1
        self.Comments[comment] = self.comment_id

    def IsBlock(self, block):
        return self.Blocks.get(block, False)
        
    def IsWire(self, wire):
        return self.Wires.get(wire, False)
        
    def IsComment(self, comment):
        return self.Comments.get(comment, False)

    def RemoveBlock(self, block):
        self.Blocks.pop(block)
        
    def RemoveWire(self, wire):
        self.Wires.pop(wire)
        
    def RemoveComment(self, comment):
        self.Comments.pop(comment)

    def GetElements(self, sort_blocks=False, sort_wires=False, sort_comments=False):
        blocks = self.Blocks.keys()
        wires = self.Wires.keys()
        comments = self.Comments.keys()
        if sort_blocks:
            blocks.sort(lambda x, y: cmp(self.Blocks[x], self.Blocks[y]))
        if sort_wires:
            wires.sort(lambda x, y: cmp(self.Wires[x], self.Wires[y]))
        if sort_comments:
            comments.sort(lambda x, y: cmp(self.Comments[x], self.Comments[y]))
        return blocks + wires + comments

    def RefreshVisibleElements(self, xp = None, yp = None):
        x, y = self.CalcUnscrolledPosition(0, 0)
        if xp is not None:
            x = xp * self.GetScrollPixelsPerUnit()[0]
        if yp is not None:
            y = yp * self.GetScrollPixelsPerUnit()[1]
        width, height = self.GetClientSize()
        screen = wx.Rect(x, y, width, height)
        for comment in self.Comments:
            comment.TestVisible(screen)
        for wire in self.Wires:
            wire.TestVisible(screen)
        for block in self.Blocks:
            block.TestVisible(screen)
            
#-------------------------------------------------------------------------------
#                              Reset functions
#-------------------------------------------------------------------------------

    # Resets Viewer lists
    def ResetView(self):
        self.Blocks = {}
        self.Wires = {}
        self.Comments = {}
        self.Subscribed = {}
        self.SelectedElement = None
        self.HighlightedElement = None
    
    def Flush(self):
        for element, iec_path in self.Subscribed.iteritems():
            self.Controler.UnsubscribeDebugIECVariable(iec_path, element)
        for block in self.Blocks:
            block.Flush()
    
    # Remove all elements
    def CleanView(self):
        for block in self.Blocks.keys():
            block.Clean()
        self.ResetView()
    
    # Changes Viewer mode
    def SetMode(self, mode):
        if self.Mode != mode or mode == MODE_SELECTION:
            self.Mode = mode
            self.SavedMode = False
        else:
            self.SavedMode = True
        # Reset selection
        if self.Mode != MODE_SELECTION and self.SelectedElement:
            self.SelectedElement.SetSelected(False)
            self.SelectedElement = None
    
    # Return current drawing mode
    def GetDrawingMode(self):
        return self.ParentWindow.GetDrawingMode()
    
    # Buffer the last model state
    def RefreshBuffer(self):
        self.Controler.BufferProject()
        self.ParentWindow.RefreshTitle()
        self.ParentWindow.RefreshEditMenu()

    # Refresh the current scaling
    def RefreshScaling(self, refresh=True):
        properties = self.Controler.GetProjectProperties(self.Debug)
        scaling = properties["scaling"][self.CurrentLanguage]
        if scaling != (0, 0):
            self.Scaling = scaling
            if self.DrawGrid:
                bitmap = wx.EmptyBitmap(*scaling)
                dc = wx.MemoryDC(bitmap)
                dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
                dc.Clear()
                dc.SetPen(wx.Pen(wx.Colour(180, 180, 180)))
                dc.DrawPoint(0, 0)
                self.GridBrush = wx.BrushFromBitmap(bitmap)
            else:
                self.GridBrush = wx.TRANSPARENT_BRUSH
        else:
            self.Scaling = None
            self.GridBrush = wx.TRANSPARENT_BRUSH
        page_size = properties["pageSize"]
        if page_size != (0, 0):
            self.PageSize = map(int, page_size)
            self.PagePen = wx.Pen(wx.Colour(180, 180, 180))
        else:
            self.PageSize = None
            self.PagePen = wx.TRANSPARENT_PEN
        if refresh:
            self.Refresh(False)
        
        
#-------------------------------------------------------------------------------
#                          Refresh functions
#-------------------------------------------------------------------------------

    def ResetBuffer(self):
        pass

    # Refresh Viewer elements
    def RefreshView(self):
        self.current_id = 0
        # Start by reseting Viewer
        self.Flush()
        self.ResetView()
        instance = {}
        # List of ids of already loaded blocks
        ids = []
        # Load Blocks until they are all loaded
        while instance is not None:
            instance = self.Controler.GetEditedElementInstanceInfos(self.TagName, exclude = ids, debug = self.Debug)
            if instance is not None:
                self.loadInstance(instance, ids)
        self.RefreshScrollBars()
        
        for wire in self.Wires:
            if not wire.IsConnectedCompatible():
                wire.MarkAsInvalid()
            if self.Debug:
                block = wire.EndConnected.GetParentBlock()
                if isinstance(block, LD_PowerRail):
                    wire.SetValue(True)
                if isinstance(block, FBD_Block):
                    blockname = block.GetName()
                    connectorname = wire.EndConnected.GetName()
                    if blockname != "":
                        iec_path = "%s.%s.%s"%(self.InstancePath, block.GetName(), connectorname)
                    else:
                        if connectorname == "":
                            iec_path = "%s.%s%d"%(self.InstancePath, block.GetType(), block.GetId())
                        else:
                            iec_path = "%s.%s%d_%s"%(self.InstancePath, block.GetType(), block.GetId(), connectorname)
                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), wire) is not None:
                        self.Subscribed[wire] = iec_path.upper()
                    else:
                        wire.SetValue("undefined")
                elif isinstance(block, FBD_Variable):
                    iec_path = "%s.%s"%(self.InstancePath, block.GetName())
                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), wire) is not None:
                        self.Subscribed[wire] = iec_path.upper()
                    else:
                        wire.SetValue("undefined")
                elif isinstance(block, FBD_Connector):
                    wire.SetValue("undefined")

        if self.Debug:
            for block in self.Blocks.keys():
                block.SpreadCurrent()
                if isinstance(block, LD_Contact):
                    iec_path = "%s.%s"%(self.InstancePath, block.GetName())
                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), block) is not None:
                        self.Subscribed[block] = iec_path.upper()
                elif isinstance(block, SFC_Step):
                    iec_path = "%s.%s.X"%(self.InstancePath, block.GetName())
                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), block) is not None:
                        self.Subscribed[block] = iec_path.upper()
                elif isinstance(block, SFC_Transition):
                    connectors = block.GetConnectors()
                    previous_steps = self.GetPreviousSteps(connectors["input"])
                    next_steps = self.GetNextSteps(connectors["output"])
                    iec_path = "%s.%s->%s"%(self.InstancePath, ",".join(previous_steps), ",".join(next_steps))
                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), block) is not None:
                        self.Subscribed[block] = iec_path.upper()
                
        self.RefreshVisibleElements()
        self.ShowErrors()
        self.Refresh(False)
    
    def GetPreviousSteps(self, connector):
        steps = []
        for wire, handle in connector.GetWires():
            previous = wire.GetOtherConnected(connector).GetParentBlock()
            if isinstance(previous, SFC_Step):
                steps.append(previous.GetName())
            elif isinstance(previous, SFC_Divergence) and previous.GetType() in [SIMULTANEOUS_CONVERGENCE, SELECTION_DIVERGENCE]:
                connectors = previous.GetConnectors()
                for input in connectors["inputs"]:
                    steps.extend(self.GetPreviousSteps(input))
        return steps
    
    def GetNextSteps(self, connector):
        steps = []
        for wire, handle in connector.GetWires():
            next = wire.GetOtherConnected(connector).GetParentBlock()
            if isinstance(next, SFC_Step):
                steps.append(next.GetName())
            elif isinstance(next, SFC_Jump):
                steps.append(next.GetTarget())
            elif isinstance(next, SFC_Divergence) and next.GetType() in [SIMULTANEOUS_DIVERGENCE, SELECTION_CONVERGENCE]:
                connectors = next.GetConnectors()
                for output in connectors["outputs"]:
                    steps.extend(self.GetNextSteps(output))
        return steps
    
    def GetMaxSize(self):
        maxx = maxy = 0
        for element in self.GetElements():
            bbox = element.GetBoundingBox()
            maxx = max(maxx, bbox.x + bbox.width)
            maxy = max(maxy, bbox.y + bbox.height)
        return maxx, maxy
    
    def RefreshScrollBars(self):
        xstart, ystart = self.GetViewStart()
        window_size = self.GetClientSize()
        maxx, maxy = self.GetMaxSize()
        maxx = max(maxx + WINDOW_BORDER, xstart * SCROLLBAR_UNIT + window_size[0])
        maxy = max(maxy + WINDOW_BORDER, ystart * SCROLLBAR_UNIT + window_size[1])
        if self.rubberBand.IsShown():
            extent = self.rubberBand.GetCurrentExtent()
            maxx = max(maxx, extent.x + extent.width)
            maxy = max(maxy, extent.y + extent.height)
        self.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, 
            round(maxx / SCROLLBAR_UNIT), round(maxy / SCROLLBAR_UNIT), 
            xstart, ystart, True)
        
    # Load instance from given informations
    def loadInstance(self, instance, ids):
        ids.append(instance["id"])
        self.current_id = max(self.current_id, instance["id"]) 
        if instance["type"] == "input":
            variable = FBD_Variable(self, INPUT, instance["name"], instance["value_type"], instance["id"], instance["executionOrder"])
            variable.SetPosition(instance["x"], instance["y"])
            variable.SetSize(instance["width"], instance["height"])
            self.AddBlock(variable)
            connectors = variable.GetConnectors()
            connectors["output"].SetPosition(wx.Point(*instance["connector"]["position"]))
            if instance["connector"]["negated"]:
                connectors["output"].SetNegated(True)
            if instance["connector"]["edge"]:
                connectors["output"].SetEdge(instance["connector"]["edge"])
        elif instance["type"] == "output":
            variable = FBD_Variable(self, OUTPUT, instance["name"], instance["value_type"], instance["id"], instance["executionOrder"])
            variable.SetPosition(instance["x"], instance["y"])
            variable.SetSize(instance["width"], instance["height"])
            self.AddBlock(variable)
            connectors = variable.GetConnectors()
            connectors["input"].SetPosition(wx.Point(*instance["connector"]["position"]))
            if instance["connector"]["negated"]:
                connectors["input"].SetNegated(True)
            if instance["connector"]["edge"]:
                connectors["input"].SetEdge(instance["connector"]["edge"])
            self.CreateWires(connectors["input"], instance["connector"]["links"], ids)
        elif instance["type"] == "inout":
            variable = FBD_Variable(self, INOUT, instance["name"], instance["value_type"], instance["id"], instance["executionOrder"])
            variable.SetPosition(instance["x"], instance["y"])
            variable.SetSize(instance["width"], instance["height"])
            self.AddBlock(variable)
            connectors = variable.GetConnectors()
            connectors["output"].SetPosition(wx.Point(*instance["connectors"]["output"]["position"]))
            connectors["input"].SetPosition(wx.Point(*instance["connectors"]["input"]["position"]))
            if instance["connectors"]["output"]["negated"]:
                connectors["output"].SetNegated(True)
            if instance["connectors"]["output"]["edge"]:
                connectors["output"].SetEdge(instance["connectors"]["output"]["edge"])
            if instance["connectors"]["input"]["negated"]:
                connectors["input"].SetNegated(True)
            if instance["connectors"]["input"]["edge"]:
                connectors["input"].SetEdge(instance["connectors"]["input"]["edge"])
            self.CreateWires(connectors["input"], instance["connectors"]["input"]["links"], ids)
        elif instance["type"] == "continuation":
            connection = FBD_Connector(self, CONTINUATION, instance["name"], instance["id"])
            connection.SetPosition(instance["x"], instance["y"])
            connection.SetSize(instance["width"], instance["height"])
            self.AddBlock(connection)
            connector = connection.GetConnector()
            connector.SetPosition(wx.Point(*instance["connector"]["position"]))
        elif instance["type"] == "connection":
            connection = FBD_Connector(self, CONNECTOR, instance["name"], instance["id"])
            connection.SetPosition(instance["x"], instance["y"])
            connection.SetSize(instance["width"], instance["height"])
            self.AddBlock(connection)
            connector = connection.GetConnector()
            connector.SetPosition(wx.Point(*instance["connector"]["position"]))
            self.CreateWires(connector, instance["connector"]["links"], ids)
        elif instance["type"] == "comment":
            comment = Comment(self, instance["content"], instance["id"])
            comment.SetPosition(instance["x"], instance["y"])
            comment.SetSize(instance["width"], instance["height"])
            self.AddComment(comment)
        elif instance["type"] == "leftPowerRail":
            leftpowerrail = LD_PowerRail(self, LEFTRAIL, instance["id"], [True for i in range(len(instance["connectors"]))])
            leftpowerrail.SetPosition(instance["x"], instance["y"])
            leftpowerrail.SetSize(instance["width"], instance["height"])
            self.AddBlock(leftpowerrail)
            connectors = leftpowerrail.GetConnectors()
            for i, connector in enumerate(instance["connectors"]):
                connectors[i].SetPosition(wx.Point(*connector["position"]))
        elif instance["type"] == "rightPowerRail":
            rightpowerrail = LD_PowerRail(self, RIGHTRAIL, instance["id"], [True for i in range(len(instance["connectors"]))])
            rightpowerrail.SetPosition(instance["x"], instance["y"])
            rightpowerrail.SetSize(instance["width"], instance["height"])
            self.AddBlock(rightpowerrail)
            connectors = rightpowerrail.GetConnectors()
            for i, connector in enumerate(instance["connectors"]):
                connectors[i].SetPosition(wx.Point(*connector["position"]))
                self.CreateWires(connectors[i], connector["links"], ids)
        elif instance["type"] == "contact":
            if instance["negated"]:
                negated = instance["negated"]
            else:
                negated = False
            if instance["edge"]:
                edge = instance["edge"]
            else:
                edge = "none"
            if negated and edge == "none":
                contact_type = CONTACT_REVERSE
            elif not negated and edge == "rising":
                contact_type = CONTACT_RISING
            elif not negated and edge == "falling":
                contact_type = CONTACT_FALLING
            else:
                contact_type = CONTACT_NORMAL
            contact = LD_Contact(self, contact_type, instance["name"], instance["id"])
            contact.SetPosition(instance["x"], instance["y"])
            contact.SetSize(instance["width"], instance["height"])
            self.AddBlock(contact)
            connectors = contact.GetConnectors()
            connectors["input"].SetPosition(wx.Point(*instance["connectors"]["input"]["position"]))
            self.CreateWires(connectors["input"], instance["connectors"]["input"]["links"], ids)
            connectors["output"].SetPosition(wx.Point(*instance["connectors"]["output"]["position"]))
        elif instance["type"] == "coil":
            if instance["negated"]:
                negated = instance["negated"]
            else:
                negated = False
            if instance["storage"]:
                storage = instance["storage"]
            else:
                storage = "none"
            if instance["edge"]:
                edge = instance["edge"]
            else:
                edge = "none"
            if negated and storage == "none" and edge == "none":
                coil_type = COIL_REVERSE
            elif not negated and edge == "none" and storage == "set":
                coil_type = COIL_SET
            elif not negated and edge == "none" and storage == "reset":
                coil_type = COIL_RESET
            elif not negated and storage == "none" and edge == "rising":
                coil_type = COIL_RISING
            elif not negated and storage == "none" and edge == "falling":
                coil_type = COIL_FALLING
            else:
                coil_type = COIL_NORMAL
            coil = LD_Coil(self, coil_type, instance["name"], instance["id"])
            coil.SetPosition(instance["x"], instance["y"])
            coil.SetSize(instance["width"], instance["height"])
            self.AddBlock(coil)
            connectors = coil.GetConnectors()
            connectors["input"].SetPosition(wx.Point(*instance["connectors"]["input"]["position"]))
            self.CreateWires(connectors["input"], instance["connectors"]["input"]["links"], ids)
            connectors["output"].SetPosition(wx.Point(*instance["connectors"]["output"]["position"]))
        elif instance["type"] == "step":
            if instance["initial"]:
                initial = instance["initial"]
            else:
                initial = False
            step = SFC_Step(self, instance["name"], initial, instance["id"])
            step.SetPosition(instance["x"], instance["y"])
            step.SetSize(instance["width"], instance["height"])
            self.AddBlock(step)
            if "output" in instance["connectors"]:
                step.AddOutput()
            if "action" in instance["connectors"]:
                step.AddAction()
            connectors = step.GetConnectors()
            if connectors["input"]:
                connectors["input"].SetPosition(wx.Point(*instance["connectors"]["input"]["position"]))
                self.CreateWires(connectors["input"], instance["connectors"]["input"]["links"], ids)
            if connectors["output"]:
                connectors["output"].SetPosition(wx.Point(*instance["connectors"]["output"]["position"]))
            if connectors["action"]:
                connectors["action"].SetPosition(wx.Point(*instance["connectors"]["action"]["position"]))
        elif instance["type"] == "transition":
            transition = SFC_Transition(self, instance["condition_type"], instance["condition"], instance["priority"], instance["id"])
            transition.SetPosition(instance["x"], instance["y"])
            transition.SetSize(instance["width"], instance["height"])
            self.AddBlock(transition)
            connectors = transition.GetConnectors()
            connectors["input"].SetPosition(wx.Point(*instance["connectors"]["input"]["position"]))
            self.CreateWires(connectors["input"], instance["connectors"]["input"]["links"], ids)
            connectors["output"].SetPosition(wx.Point(*instance["connectors"]["output"]["position"]))
            if instance["condition_type"] == "connection":
                self.CreateWires(connectors["connection"], instance["connectors"]["connection"]["links"], ids)
        elif instance["type"] in ["selectionDivergence", "selectionConvergence", "simultaneousDivergence", "simultaneousConvergence"]:
            if instance["type"] == "selectionDivergence":
                divergence = SFC_Divergence(self, SELECTION_DIVERGENCE, 
                    len(instance["connectors"]["outputs"]), instance["id"])
            elif instance["type"] == "selectionConvergence":
                divergence = SFC_Divergence(self, SELECTION_CONVERGENCE, 
                    len(instance["connectors"]["inputs"]), instance["id"])
            elif instance["type"] == "simultaneousDivergence":
                divergence = SFC_Divergence(self, SIMULTANEOUS_DIVERGENCE, 
                    len(instance["connectors"]["outputs"]), instance["id"])
            else:
                divergence = SFC_Divergence(self, SIMULTANEOUS_CONVERGENCE, 
                    len(instance["connectors"]["inputs"]), instance["id"])
            divergence.SetPosition(instance["x"], instance["y"])
            divergence.SetSize(instance["width"], instance["height"])
            self.AddBlock(divergence)
            connectors = divergence.GetConnectors()
            for i, input_connector in enumerate(instance["connectors"]["inputs"]):
                connector = connectors["inputs"][i]
                connector.SetPosition(wx.Point(*input_connector["position"]))
                self.CreateWires(connector, input_connector["links"], ids)
            for i, output_connector in enumerate(instance["connectors"]["outputs"]):
                connector = connectors["outputs"][i]
                connector.SetPosition(wx.Point(*output_connector["position"]))
        elif instance["type"] == "jump":
            jump = SFC_Jump(self, instance["target"], instance["id"])
            jump.SetPosition(instance["x"], instance["y"])
            jump.SetSize(instance["width"], instance["height"])
            self.AddBlock(jump)
            connector = jump.GetConnector()
            connector.SetPosition(wx.Point(*instance["connector"]["position"]))
            self.CreateWires(connector, instance["connector"]["links"], ids)
        elif instance["type"] == "actionBlock":
            actionBlock = SFC_ActionBlock(self, instance["actions"], instance["id"])
            actionBlock.SetPosition(instance["x"], instance["y"])
            actionBlock.SetSize(instance["width"], instance["height"])
            self.AddBlock(actionBlock)
            connector = actionBlock.GetConnector()
            connector.SetPosition(wx.Point(*instance["connector"]["position"]))
            self.CreateWires(connector, instance["connector"]["links"], ids)
        else:
            connectors = {"inputs" : [], "outputs" : []}
            executionControl = False
            for input in instance["connectors"]["inputs"]:
                if input["negated"]:
                    connectors["inputs"].append((input["name"], None, "negated"))
                elif input["edge"]:
                    connectors["inputs"].append((input["name"], None, input["edge"]))
                else:
                    connectors["inputs"].append((input["name"], None, "none"))
            for output in instance["connectors"]["outputs"]:
                if output["negated"]:
                    connectors["outputs"].append((output["name"], None, "negated"))
                elif output["edge"]:
                    connectors["outputs"].append((output["name"], None, output["edge"]))
                else:
                    connectors["outputs"].append((output["name"], None, "none"))
            if len(connectors["inputs"]) > 0 and connectors["inputs"][0][0] == "EN":
		connectors["inputs"].pop(0)
	        executionControl = True
            if len(connectors["outputs"]) > 0 and connectors["outputs"][0][0] == "ENO":
                connectors["outputs"].pop(0)
                executionControl = True
            if instance["name"] is None:
                instance["name"] = ""
            block = FBD_Block(self, instance["type"], instance["name"], 
                    instance["id"], len(connectors["inputs"]), 
                    connectors=connectors, executionControl=executionControl, 
                    executionOrder=instance["executionOrder"])
            block.SetPosition(instance["x"], instance["y"])
            block.SetSize(instance["width"], instance["height"])
            self.AddBlock(block)
            connectors = block.GetConnectors()
            for i, input_connector in enumerate(instance["connectors"]["inputs"]):
                if i < len(connectors["inputs"]):
                    connector = connectors["inputs"][i]
                    connector.SetPosition(wx.Point(*input_connector["position"]))
                    if input_connector["negated"]:
                        connector.SetNegated(True)
                    if input_connector["edge"] != "none":
                        connector.SetEdge(input_connector["edge"])
                    self.CreateWires(connector, input_connector["links"], ids)
            for i, output_connector in enumerate(instance["connectors"]["outputs"]):
                if i < len(connectors["outputs"]):
                    connector = connectors["outputs"][i]
                    if output_connector["negated"]:
                        connector.SetNegated(True)
                    if output_connector["edge"] != "none":
                        connector.SetEdge(output_connector["edge"])
                    connector.SetPosition(wx.Point(*output_connector["position"]))
    
    def CreateWires(self, start_connector, links, ids):
        for link in links:
            refLocalId = link["refLocalId"]
            if refLocalId is not None:
                if refLocalId not in ids:
                    new_instance = self.Controler.GetEditedElementInstanceInfos(self.TagName, refLocalId, debug = self.Debug)
                    if new_instance is not None:
                        self.loadInstance(new_instance, ids)
                connected = self.FindElementById(refLocalId)
                if connected is not None:
                    points = link["points"]
                    end_connector = connected.GetConnector(wx.Point(points[-1][0], points[-1][1]), link["formalParameter"])
                    if end_connector is not None:
                        wire = Wire(self)
                        wire.SetPoints(points)
                        start_connector.Connect((wire, 0), False)
                        end_connector.Connect((wire, -1), False)
                        wire.ConnectStartPoint(None, start_connector)
                        wire.ConnectEndPoint(None, end_connector)
                        self.AddWire(wire)

    def IsOfType(self, type, reference):
        return self.Controler.IsOfType(type, reference, self.Debug)
    
    def IsEndType(self, type):
        return self.Controler.IsEndType(type)

    def GetBlockType(self, type, inputs = None):
        return self.Controler.GetBlockType(type, inputs, self.Debug)

#-------------------------------------------------------------------------------
#                          Search Element functions
#-------------------------------------------------------------------------------

    def FindBlock(self, pos):
        for block in self.Blocks:
            if block.HitTest(pos) or block.TestHandle(pos) != (0, 0):
                return block
        return None
    
    def FindWire(self, pos):
        for wire in self.Wires:
            if wire.HitTest(pos) or wire.TestHandle(pos) != (0, 0):
                return wire
        return None
    
    def FindElement(self, pos, exclude_group = False):
        if self.SelectedElement and not (exclude_group and isinstance(self.SelectedElement, Graphic_Group)):
            if self.SelectedElement.HitTest(pos) or self.SelectedElement.TestHandle(pos) != (0, 0):
                return self.SelectedElement
        for element in self.GetElements():
            if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
                return element
        return None
    
    def FindBlockConnector(self, pos, direction = None, exclude = None):
        for block in self.Blocks:
            result = block.TestConnector(pos, direction, exclude)
            if result:
                return result
        return None
    
    def FindElementById(self, id):
        for element in self.Blocks:
            if element.GetId() == id:
                return element
        for element in self.Comments:
            if element.GetId() == id:
                return element
        return None
    
    def SearchElements(self, bbox):
        elements = []
        for element in self.GetElements():
            if element.IsInSelection(bbox):
                elements.append(element)
        return elements

#-------------------------------------------------------------------------------
#                           Popup menu functions
#-------------------------------------------------------------------------------

    def PopupBlockMenu(self, connector = None):
        if connector is not None and connector.IsCompatible("BOOL"):
            type = self.Controler.GetEditedElementType(self.TagName, self.Debug)
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS0, True)
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS1, True)
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS2, type != "function")
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS3, type != "function")
        else:
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS0, False)
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS1, False)
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS2, False)
            self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS3, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS5, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS6, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS8, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS9, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS14, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS16, self.SelectedElement.GetType() in self.Controler.GetProjectPouNames(self.Debug))
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS17, True)
        if connector is not None:
            if connector.IsNegated():
                self.ContextualMenu.Check(ID_VIEWERCONTEXTUALMENUITEMS1, True)
            elif connector.GetEdge() == "rising":
                self.ContextualMenu.Check(ID_VIEWERCONTEXTUALMENUITEMS2, True)
            elif connector.GetEdge() == "falling":
                self.ContextualMenu.Check(ID_VIEWERCONTEXTUALMENUITEMS3, True)
            else:
                self.ContextualMenu.Check(ID_VIEWERCONTEXTUALMENUITEMS0, True)
        self.PopupMenu(self.ContextualMenu)
    
    def PopupWireMenu(self, delete=True):
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS0, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS1, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS2, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS3, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS5, True)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS6, delete)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS8, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS9, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS14, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS16, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS17, True)
        self.PopupMenu(self.ContextualMenu)
    
    def PopupDivergenceMenu(self, connector):
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS0, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS1, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS2, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS3, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS5, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS6, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS8, True)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS9, connector)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS14, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS16, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS17, True)
        self.PopupMenu(self.ContextualMenu)
    
    def PopupGroupMenu(self):
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS0, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS1, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS2, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS3, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS5, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS6, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS8, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS9, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS14, True)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS16, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS17, True)
        self.PopupMenu(self.ContextualMenu)
    
    def PopupDefaultMenu(self, block = True):
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS0, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS1, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS2, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS3, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS5, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS6, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS8, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS9, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS14, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS16, False)
        self.ContextualMenu.Enable(ID_VIEWERCONTEXTUALMENUITEMS17, block)
        self.PopupMenu(self.ContextualMenu)

#-------------------------------------------------------------------------------
#                            Menu items functions
#-------------------------------------------------------------------------------

    def OnAlignLeftMenu(self, event):
        if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
            self.SelectedElement.AlignElements(ALIGN_LEFT, None)
            self.RefreshBuffer()
            self.Refresh(False)
        event.Skip()
    
    def OnAlignCenterMenu(self, event):
        if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
            self.SelectedElement.AlignElements(ALIGN_CENTER, None)
            self.RefreshBuffer()
            self.Refresh(False)
        event.Skip()
    
    def OnAlignRightMenu(self, event):
        if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
            self.SelectedElement.AlignElements(ALIGN_RIGHT, None)
            self.RefreshBuffer()
            self.Refresh(False)
        event.Skip()
    
    def OnAlignTopMenu(self, event):
        if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
            self.SelectedElement.AlignElements(None, ALIGN_TOP)
            self.RefreshBuffer()
            self.Refresh(False)
        event.Skip()
    
    def OnAlignMiddleMenu(self, event):
        if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
            self.SelectedElement.AlignElements(None, ALIGN_MIDDLE)
            self.RefreshBuffer()
            self.Refresh(False)
        event.Skip()
    
    def OnAlignBottomMenu(self, event):
        if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
            self.SelectedElement.AlignElements(None, ALIGN_BOTTOM)
            self.RefreshBuffer()
            self.Refresh(False)
        event.Skip()
        
    def OnNoModifierMenu(self, event):
        if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
            self.SelectedElement.SetConnectorNegated(False)
            self.SelectedElement.Refresh()
            self.RefreshBuffer()
        event.Skip()
    
    def OnNegatedMenu(self, event):
        if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
            self.SelectedElement.SetConnectorNegated(True)
            self.SelectedElement.Refresh()
            self.RefreshBuffer()
        event.Skip()

    def OnRisingEdgeMenu(self, event):
        if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
            self.SelectedElement.SetConnectorEdge("rising")
            self.SelectedElement.Refresh()
            self.RefreshBuffer()
        event.Skip()

    def OnFallingEdgeMenu(self, event):
        if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
            self.SelectedElement.SetConnectorEdge("falling")
            self.SelectedElement.Refresh()
            self.RefreshBuffer()
        event.Skip()

    def OnAddSegmentMenu(self, event):
        if self.SelectedElement is not None and self.IsWire(self.SelectedElement):
            self.SelectedElement.AddSegment()
            self.SelectedElement.Refresh()
        event.Skip()

    def OnDeleteSegmentMenu(self, event):
        if self.SelectedElement is not None and self.IsWire(self.SelectedElement):
            self.SelectedElement.DeleteSegment()
            self.SelectedElement.Refresh()
        event.Skip()

    def OnAddBranchMenu(self, event):
        if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
            self.AddDivergenceBranch(self.SelectedElement)
            self.RefreshBuffer()
        event.Skip()

    def OnDeleteBranchMenu(self, event):
        if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
            self.RemoveDivergenceBranch(self.SelectedElement)
            self.RefreshBuffer()
        event.Skip()

    def OnEditBlockMenu(self, event):
        if self.SelectedElement is not None:
            self.ParentWindow.EditProjectElement(ITEM_POU, "P::%s"%self.SelectedElement.GetType())
        event.Skip()

    def OnDeleteMenu(self, event):
        if self.SelectedElement is not None:
            self.SelectedElement.Delete()
            self.SelectedElement = None
            self.RefreshBuffer()
            self.Refresh(False)
        event.Skip()

    def OnClearExecutionOrderMenu(self, event):
        self.Controler.ClearEditedElementExecutionOrder(self.TagName)
        self.RefreshBuffer()
        self.RefreshView()
        
    def OnResetExecutionOrderMenu(self, event):
        self.Controler.ResetEditedElementExecutionOrder(self.TagName)
        self.RefreshBuffer()
        self.RefreshView()

#-------------------------------------------------------------------------------
#                          Mouse event functions
#-------------------------------------------------------------------------------

    def OnViewerLeftDown(self, event):
        if self.Mode == MODE_SELECTION:
            dc = self.GetLogicalDC()
            pos = event.GetLogicalPosition(dc)
            if event.ControlDown() and self.SelectedElement is not None:
                element = self.FindElement(pos, True)
                if element is not None:
                    if isinstance(self.SelectedElement, Graphic_Group):
                        self.SelectedElement.SetSelected(False)
                        self.SelectedElement.SelectElement(element)
                    elif self.SelectedElement is not None:
                        group = Graphic_Group(self)
                        group.SelectElement(self.SelectedElement)
                        group.SelectElement(element)
                        self.SelectedElement = group
                    elements = self.SelectedElement.GetElements()
                    if len(elements) == 0:
                        self.SelectedElement = element
                    elif len(elements) == 1:
                        self.SelectedElement = elements[0]
                    self.SelectedElement.SetSelected(True)
            else:
                element = self.FindElement(pos)
                if not self.Debug and (element is None or element.TestHandle(pos) == (0, 0)):
                    connector = self.FindBlockConnector(pos)
                else:
                    connector = None
                if not self.Debug and self.DrawingWire:
                    self.DrawingWire = False
                    if self.SelectedElement is not None:
                        if element is None or element.TestHandle(pos) == (0, 0):
                            connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection())
                        if connector is not None:
                            event.Dragging = lambda : True
                            self.SelectedElement.OnMotion(event, self.GetLogicalDC(), self.Scaling)
                        if self.SelectedElement.EndConnected is not None:
                            self.SelectedElement.ResetPoints()
                            self.SelectedElement.GeneratePoints()
                            self.SelectedElement.RefreshModel()
                            self.SelectedElement.SetSelected(True)
                            element = self.SelectedElement
                            self.RefreshBuffer()
                        else:
                            rect = self.SelectedElement.GetRedrawRect()
                            self.SelectedElement.Delete()
                            self.SelectedElement = None
                            element = None
                            self.RefreshRect(self.GetScrolledRect(rect), False)
                elif not self.Debug and connector is not None:
                    self.DrawingWire = True
                    scaled_pos = GetScaledEventPosition(event, self.GetLogicalDC(), self.Scaling)
                    if (connector.GetDirection() == EAST):
                        wire = Wire(self, [wx.Point(pos.x, pos.y), EAST], [wx.Point(scaled_pos.x, scaled_pos.y), WEST])
                    else:
                        wire = Wire(self, [wx.Point(pos.x, pos.y), WEST], [wx.Point(scaled_pos.x, scaled_pos.y), EAST])
                    wire.oldPos = scaled_pos
                    wire.Handle = (HANDLE_POINT, 0)
                    wire.ProcessDragging(0, 0, False, None)
                    wire.Handle = (HANDLE_POINT, 1)
                    self.AddWire(wire)
                    if self.SelectedElement is not None:
                        self.SelectedElement.SetSelected(False)
                    self.SelectedElement = wire
                    self.RefreshVisibleElements()
                    self.SelectedElement.Refresh()
                else:
                    if self.SelectedElement is not None and self.SelectedElement != element:
                        self.SelectedElement.SetSelected(False)
                        self.SelectedElement = None
                    if element is not None:
                        self.SelectedElement = element
                        if self.Debug:
                            Graphic_Element.OnLeftDown(self.SelectedElement, event, dc, self.Scaling)
                        else:
                            self.SelectedElement.OnLeftDown(event, dc, self.Scaling)
                        self.SelectedElement.Refresh()
                    else:
                        self.rubberBand.Reset()
                        self.rubberBand.OnLeftDown(event, dc, self.Scaling)
        elif self.Mode in [MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, 
                           MODE_CONTACT, MODE_COIL, MODE_POWERRAIL, MODE_INITIALSTEP, 
                           MODE_STEP, MODE_TRANSITION, MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION]:
            self.rubberBand.Reset()
            self.rubberBand.OnLeftDown(event, self.GetLogicalDC(), self.Scaling)
        event.Skip()

    def OnViewerLeftUp(self, event):
        if self.rubberBand.IsShown():
            if self.Mode == MODE_SELECTION:
                elements = self.SearchElements(self.rubberBand.GetCurrentExtent())
                self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
                if len(elements) == 1:
                    self.SelectedElement = elements[0]
                    self.SelectedElement.SetSelected(True)
                elif len(elements) > 1:
                    self.SelectedElement = Graphic_Group(self)
                    self.SelectedElement.SetElements(elements)
                    self.SelectedElement.SetSelected(True)
            else:
                bbox = self.rubberBand.GetCurrentExtent()
                self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)                
                if self.Mode == MODE_BLOCK:
                    wx.CallAfter(self.AddNewBlock, bbox)
                elif self.Mode == MODE_VARIABLE:
                    wx.CallAfter(self.AddNewVariable, bbox)
                elif self.Mode == MODE_CONNECTION:
                    wx.CallAfter(self.AddNewConnection, bbox)
                elif self.Mode == MODE_COMMENT:
                    wx.CallAfter(self.AddNewComment, bbox)
                elif self.Mode == MODE_CONTACT:
                    wx.CallAfter(self.AddNewContact, bbox)
                elif self.Mode == MODE_COIL:
                    wx.CallAfter(self.AddNewCoil, bbox)
                elif self.Mode == MODE_POWERRAIL:
                    wx.CallAfter(self.AddNewPowerRail, bbox)
                elif self.Mode == MODE_INITIALSTEP:
                    wx.CallAfter(self.AddNewStep, bbox, True)
                elif self.Mode == MODE_STEP:
                    wx.CallAfter(self.AddNewStep, bbox, False)
                elif self.Mode == MODE_TRANSITION:
                    wx.CallAfter(self.AddNewTransition, bbox)
                elif self.Mode == MODE_DIVERGENCE:
                    wx.CallAfter(self.AddNewDivergence, bbox)
                elif self.Mode == MODE_JUMP:
                    wx.CallAfter(self.AddNewJump, bbox)
                elif self.Mode == MODE_ACTION:
                    wx.CallAfter(self.AddNewActionBlock, bbox)
        elif self.Mode == MODE_SELECTION and self.SelectedElement is not None:
            dc = self.GetLogicalDC()
            if not self.Debug and self.DrawingWire:
                pos = event.GetLogicalPosition(dc)
                connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection())
                if self.SelectedElement.EndConnected is not None:
                    self.DrawingWire = False
                    self.SelectedElement.StartConnected.HighlightParentBlock(False)
                    self.SelectedElement.EndConnected.HighlightParentBlock(False)
                    self.SelectedElement.ResetPoints()
                    self.SelectedElement.OnMotion(event, dc, self.Scaling)
                    self.SelectedElement.GeneratePoints()
                    self.SelectedElement.RefreshModel()
                    if self.HighlightedElement is not None:
                        self.HighlightedElement.SetHighlighted(False)
                        self.HighlightedElement = None
                    self.SelectedElement.SetHighlighted(True)
                    self.HighlightedElement = self.SelectedElement
                    self.SelectedElement.SetSelected(True)
                    self.RefreshBuffer()
                elif connector is None or self.SelectedElement.GetDragging():
                    self.DrawingWire = False
                    rect = self.SelectedElement.GetRedrawRect()
                    self.SelectedElement.Delete()
                    self.SelectedElement = None
                    self.RefreshRect(self.GetScrolledRect(rect), False)
            else:
                if self.Debug:
                    Graphic_Element.OnLeftUp(self.SelectedElement, event, dc, self.Scaling)
                else:
                    self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
                wx.CallAfter(self.SetCursor, wx.NullCursor)
        if self.Mode != MODE_SELECTION and not self.SavedMode:
            wx.CallAfter(self.ParentWindow.ResetCurrentMode)
        event.Skip()
    
    def OnViewerRightDown(self, event):
        if self.Mode == MODE_SELECTION:
            dc = self.GetLogicalDC()
            pos = event.GetLogicalPosition(dc)
            element = self.FindElement(pos)
            if self.SelectedElement is not None and self.SelectedElement != element:
                self.SelectedElement.SetSelected(False)
                self.SelectedElement = None
            if element:
                self.SelectedElement = element
                if self.Debug:
                    Graphic_Element.OnRightDown(self.SelectedElement, event, dc, self.Scaling)
                else:
                    self.SelectedElement.OnRightDown(event, dc, self.Scaling)
                self.SelectedElement.Refresh()
        event.Skip()
    
    def OnViewerRightUp(self, event):
        dc = self.GetLogicalDC()
        if self.SelectedElement is not None:
            if self.Debug:
                Graphic_Element.OnRightUp(self.SelectedElement, event, dc, self.Scaling)
            else:
                self.SelectedElement.OnRightUp(event, dc, self.Scaling)
            wx.CallAfter(self.SetCursor, wx.NullCursor)
        elif not self.Debug:
            self.PopupDefaultMenu(False)
        event.Skip()
    
    def OnViewerLeftDClick(self, event):
        if self.Mode == MODE_SELECTION and self.SelectedElement is not None:
            if self.Debug:
                Graphic_Element.OnLeftDClick(self.SelectedElement, event, self.GetLogicalDC(), self.Scaling)
            elif event.ControlDown() and self.IsBlock(self.SelectedElement) and self.SelectedElement.GetType() in self.Controler.GetProjectPouNames(self.Debug):
                self.ParentWindow.EditProjectElement(ITEM_POU, self.SelectedElement.GetType())
            else:
                self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
        event.Skip()
    
    def OnViewerMotion(self, event):
        refresh = False
        dc = self.GetLogicalDC()
        pos = GetScaledEventPosition(event, dc, self.Scaling)
        if not event.Dragging():
            highlighted = self.FindElement(pos) 
            if self.HighlightedElement is not None and self.HighlightedElement != highlighted:
                self.HighlightedElement.SetHighlighted(False)
                self.HighlightedElement = None
            if highlighted is not None and self.HighlightedElement != highlighted:
                highlighted.SetHighlighted(True)
            self.HighlightedElement = highlighted
        if self.rubberBand.IsShown():
            self.rubberBand.OnMotion(event, dc, self.Scaling)
        elif not self.Debug and self.Mode == MODE_SELECTION and self.SelectedElement is not None:
            if self.DrawingWire:
                connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection(), self.SelectedElement.EndConnected)
                if not connector or self.SelectedElement.EndConnected == None:
                    self.SelectedElement.ResetPoints()
                    movex, movey = self.SelectedElement.OnMotion(event, dc, self.Scaling)
                    self.SelectedElement.GeneratePoints()
                    if movex != 0 or movey != 0:
                        self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
            else:
                movex, movey = self.SelectedElement.OnMotion(event, dc, self.Scaling)
                if movex != 0 or movey != 0:
                    self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
        self.UpdateScrollPos(event)
        event.Skip()

    def OnLeaveViewer(self, event):
        if self.SelectedElement is not None and self.SelectedElement.GetDragging():
            event.Skip()
        elif self.HighlightedElement is not None:
            self.HighlightedElement.SetHighlighted(False)
            self.HighlightedElement = None
        event.Skip()

    def UpdateScrollPos(self, event):
        if (event.Dragging() and self.SelectedElement is not None) or self.rubberBand.IsShown():
            position = event.GetPosition()
            move_window = wx.Point()
            window_size = self.GetClientSize()
            xstart, ystart = self.GetViewStart()
            if position.x < SCROLL_ZONE and xstart > 0:
                move_window.x = -1
            elif position.x > window_size[0] - SCROLL_ZONE:
                move_window.x = 1
            if position.y < SCROLL_ZONE and ystart > 0:
                move_window.y = -1
            elif position.y > window_size[1] - SCROLL_ZONE:
                move_window.y = 1
            if move_window.x != 0 or move_window.y != 0:
                self.RefreshVisibleElements(xp = xstart + move_window.x, yp = ystart + move_window.y)
                self.Scroll(xstart + move_window.x, ystart + move_window.y)
            self.RefreshScrollBars()

#-------------------------------------------------------------------------------
#                          Keyboard event functions
#-------------------------------------------------------------------------------

    def OnChar(self, event):
        xpos, ypos = self.GetScrollPos(wx.HORIZONTAL), self.GetScrollPos(wx.VERTICAL)
        xmax = self.GetScrollRange(wx.HORIZONTAL) - self.GetScrollThumb(wx.HORIZONTAL)
        ymax = self.GetScrollRange(wx.VERTICAL) - self.GetScrollThumb(wx.VERTICAL)
        keycode = event.GetKeyCode()
        if self.Scaling is not None:
            scaling = self.Scaling
        else:
            scaling = (8, 8)
        if not self.Debug and keycode == wx.WXK_DELETE and self.SelectedElement is not None:
            rect = self.SelectedElement.GetRedrawRect(1, 1)
            self.SelectedElement.Delete()
            self.SelectedElement = None
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.SetCursor(wx.NullCursor)
            self.RefreshRect(self.GetScrolledRect(rect), False)
        elif not self.Debug and keycode == wx.WXK_RETURN and self.SelectedElement is not None:
            self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
        elif keycode == wx.WXK_LEFT:
            if event.ControlDown() and event.ShiftDown():
                self.Scroll(0, ypos)
            elif event.ControlDown():
                event.Skip()
            elif not self.Debug and self.SelectedElement is not None:
                self.SelectedElement.Move(-scaling[0], 0)
                self.SelectedElement.RefreshModel()
                self.RefreshBuffer()
                self.RefreshScrollBars()
                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(-scaling[0], 0)), False)
        elif keycode == wx.WXK_RIGHT:
            if event.ControlDown() and event.ShiftDown():
                self.Scroll(xmax, ypos)
            elif event.ControlDown():
                event.Skip()
            elif not self.Debug and self.SelectedElement is not None:
                self.SelectedElement.Move(scaling[0], 0)
                self.SelectedElement.RefreshModel()
                self.RefreshBuffer()
                self.RefreshScrollBars()
                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(scaling[0], 0)), False)
        elif keycode == wx.WXK_UP:
            if event.ControlDown() and event.ShiftDown():
                self.Scroll(xpos, 0)
            elif event.ControlDown():
                event.Skip()
            elif not self.Debug and self.SelectedElement is not None:
                self.SelectedElement.Move(0, -scaling[1])
                self.SelectedElement.RefreshModel()
                self.RefreshBuffer()
                self.RefreshScrollBars()
                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(0, -scaling[1])), False)
        elif keycode == wx.WXK_DOWN:
            if event.ControlDown() and event.ShiftDown():
                self.Scroll(xpos, ymax)
            elif event.ControlDown():
                event.Skip()
            elif not self.Debug and self.SelectedElement is not None:
                self.SelectedElement.Move(0, scaling[1])
                self.SelectedElement.RefreshModel()
                self.RefreshBuffer()
                self.RefreshScrollBars()
                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(0, scaling[1])), False)
        elif not self.Debug and keycode == wx.WXK_SPACE and self.SelectedElement is not None and self.SelectedElement.Dragging:
            if self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement):
                self.CopyBlock(self.SelectedElement, wx.Point(*self.SelectedElement.GetPosition()))
                self.RefreshBuffer()
                self.RefreshScrollBars()
                self.ParentWindow.RefreshVariablePanel(self.TagName)
                self.RefreshVisibleElements()
                self.SelectedElement.Refresh()
            else:
                event.Skip()
        else:
            event.Skip()

#-------------------------------------------------------------------------------
#                          Model adding functions
#-------------------------------------------------------------------------------

    def GetScaledSize(self, width, height):
        if self.Scaling is not None:
            width = round(float(width) / float(self.Scaling[0]) + 0.4) * self.Scaling[0]
            height = round(float(height) / float(self.Scaling[1]) + 0.4) * self.Scaling[1]
        return width, height

    def AddNewBlock(self, bbox):
        dialog = BlockPropertiesDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetBlockList(self.Controler.GetBlockTypes(self.TagName, self.Debug))
        dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
        dialog.SetPouElementNames(self.Controler.GetEditedElementVariables(self.TagName, self.Debug))
        dialog.SetMinBlockSize((bbox.width, bbox.height))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            values.setdefault("name", "")
            block = FBD_Block(self, values["type"], values["name"], id, 
                    values["extension"], values["inputs"], 
                    executionControl = values["executionControl"],
                    executionOrder = values["executionOrder"])
            block.SetPosition(bbox.x, bbox.y)
            block.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            self.AddBlock(block)
            self.Controler.AddEditedElementBlock(self.TagName, id, values["type"], values.get("name", None))
            self.RefreshBlockModel(block)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            self.ParentWindow.RefreshVariablePanel(self.TagName)
            self.ParentWindow.RefreshInstancesTree()
            block.Refresh()
        dialog.Destroy()
    
    def AddNewVariable(self, bbox):
        words = self.TagName.split("::")
        if words[0] == "T":
            dialog = VariablePropertiesDialog(self.ParentWindow, self.Controler, words[2])
        else:
            dialog = VariablePropertiesDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetMinVariableSize((bbox.width, bbox.height))
        varlist = []
        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
        if vars:
            for var in vars:
                if var["Edit"]:
                    varlist.append((var["Name"], var["Class"], var["Type"]))
        returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
        if returntype:
            varlist.append((self.Controler.GetEditedElementName(self.TagName), "Output", returntype))
        dialog.SetVariables(varlist)
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            variable = FBD_Variable(self, values["type"], values["name"], values["value_type"], id)
            variable.SetPosition(bbox.x, bbox.y)
            variable.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            self.AddBlock(variable)
            self.Controler.AddEditedElementVariable(self.TagName, id, values["type"])
            self.RefreshVariableModel(variable)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            variable.Refresh()
        dialog.Destroy()

    def AddNewConnection(self, bbox):
        dialog = ConnectionPropertiesDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetMinConnectionSize((bbox.width, bbox.height))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            connection = FBD_Connector(self, values["type"], values["name"], id)
            connection.SetPosition(bbox.x, bbox.y)
            connection.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            self.AddBlock(connection)
            self.Controler.AddEditedElementConnection(self.TagName, id, values["type"])
            self.RefreshConnectionModel(connection)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            connection.Refresh()
        dialog.Destroy()

    def AddNewComment(self, bbox):
        if wx.VERSION >= (2, 5, 0):
            dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", "", wx.OK|wx.CANCEL|wx.TE_MULTILINE)
        else:
            dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", "", wx.OK|wx.CANCEL)
        if dialog.ShowModal() == wx.ID_OK:
            value = dialog.GetValue()
            id = self.GetNewId()
            comment = Comment(self, value, id)
            comment.SetPosition(bbox.x, bbox.y)
            min_width, min_height = comment.GetMinSize()
            comment.SetSize(*self.GetScaledSize(max(min_width,bbox.width),max(min_height,bbox.height)))
            self.AddComment(comment)
            self.Controler.AddEditedElementComment(self.TagName, id)
            self.RefreshCommentModel(comment)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            comment.Refresh()
        dialog.Destroy()

    def AddNewContact(self, bbox):
        dialog = LDElementDialog(self.ParentWindow, self.Controler, "contact")
        dialog.SetPreviewFont(self.GetFont())
        varlist = []
        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
        if vars:
            for var in vars:
                if var["Type"] == "BOOL":
                    varlist.append(var["Name"])
        dialog.SetVariables(varlist)
        dialog.SetValues({"name":"","type":CONTACT_NORMAL})
        dialog.SetElementSize((bbox.width, bbox.height))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            contact = LD_Contact(self, values["type"], values["name"], id)
            contact.SetPosition(bbox.x, bbox.y)
            contact.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            self.AddBlock(contact)
            self.Controler.AddEditedElementContact(self.TagName, id)
            self.RefreshContactModel(contact)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            contact.Refresh()
        dialog.Destroy()

    def AddNewCoil(self, bbox):
        dialog = LDElementDialog(self.ParentWindow, self.Controler, "coil")
        dialog.SetPreviewFont(self.GetFont())
        varlist = []
        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
        if vars:
            for var in vars:
                if var["Class"] != "Input" and var["Type"] == "BOOL":
                    varlist.append(var["Name"])
        returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
        if returntype == "BOOL":
            varlist.append(self.Controler.GetEditedElementName(self.TagName))
        dialog.SetVariables(varlist)
        dialog.SetValues({"name":"","type":COIL_NORMAL})
        dialog.SetElementSize((bbox.width, bbox.height))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            coil = LD_Coil(self, values["type"], values["name"], id)
            coil.SetPosition(bbox.x, bbox.y)
            coil.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            self.AddBlock(coil)
            self.Controler.AddEditedElementCoil(self.TagName, id)
            self.RefreshCoilModel(coil)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            coil.Refresh()
        dialog.Destroy()

    def AddNewPowerRail(self, bbox):
        dialog = LDPowerRailDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetMinSize((bbox.width, bbox.height))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            powerrail = LD_PowerRail(self, values["type"], id, [True for i in xrange(values["number"])])
            powerrail.SetPosition(bbox.x, bbox.y)
            powerrail.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            self.AddBlock(powerrail)
            self.Controler.AddEditedElementPowerRail(self.TagName, id, values["type"])
            self.RefreshPowerRailModel(powerrail)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            powerrail.Refresh()
        dialog.Destroy()

    def AddNewStep(self, bbox, initial = False):
        dialog = StepContentDialog(self.ParentWindow, self.Controler, initial)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
        dialog.SetVariables(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
        dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
        dialog.SetMinStepSize((bbox.width, bbox.height))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            step = SFC_Step(self, values["name"], initial, id)
            if values["input"]:
                step.AddInput()
            else:
                step.RemoveInput()
            if values["output"]:
                step.AddOutput()
            else:
                step.RemoveOutput()
            if values["action"]:
                step.AddAction()    
            else:
                step.RemoveAction()
            step.SetPosition(bbox.x, bbox.y)
            min_width, min_height = step.GetMinSize()
            step.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
            self.AddBlock(step)
            self.Controler.AddEditedElementStep(self.TagName, id)
            self.RefreshStepModel(step)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            step.Refresh()
        dialog.Destroy()

    def AddNewTransition(self, bbox):
        dialog = TransitionContentDialog(self.ParentWindow, self.Controler, self.GetDrawingMode() == FREEDRAWING_MODE)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetTransitions(self.Controler.GetEditedElementTransitions(self.TagName, self.Debug))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            transition = SFC_Transition(self, values["type"], values["value"], values["priority"], id)
            transition.SetPosition(bbox.x, bbox.y)
            min_width, min_height = transition.GetMinSize()
            transition.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
            self.AddBlock(transition)
            self.Controler.AddEditedElementTransition(self.TagName, id)
            self.RefreshTransitionModel(transition)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            transition.Refresh()
        dialog.Destroy()

    def AddNewDivergence(self, bbox):
        dialog = DivergenceCreateDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetMinSize((bbox.width, bbox.height))
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            values = dialog.GetValues()
            divergence = SFC_Divergence(self, values["type"], values["number"], id)
            divergence.SetPosition(bbox.x, bbox.y)
            min_width, min_height = divergence.GetMinSize(True)
            divergence.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
            self.AddBlock(divergence)
            self.Controler.AddEditedElementDivergence(self.TagName, id, values["type"])
            self.RefreshDivergenceModel(divergence)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            divergence.Refresh()
        dialog.Destroy()

    def AddNewJump(self, bbox):
        choices = []
        for block in self.Blocks:
            if isinstance(block, SFC_Step):
                choices.append(block.GetName())
        dialog = wx.SingleChoiceDialog(self.ParentWindow, "Add a new jump", "Please choose a target", choices, wx.OK|wx.CANCEL)
        if dialog.ShowModal() == wx.ID_OK:
            id = self.GetNewId()
            value = dialog.GetStringSelection()
            jump = SFC_Jump(self, value, id)
            jump.SetPosition(bbox.x, bbox.y)
            min_width, min_height = jump.GetMinSize()
            jump.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
            self.AddBlock(jump)
            self.Controler.AddEditedElementJump(self.TagName, id)
            self.RefreshJumpModel(jump)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            jump.Refresh()
        dialog.Destroy()

    def AddNewActionBlock(self, bbox):
        dialog = ActionBlockDialog(self.ParentWindow)
        dialog.SetQualifierList(self.Controler.GetQualifierTypes())
        dialog.SetActionList(self.Controler.GetEditedElementActions(self.TagName, self.Debug))
        dialog.SetVariableList(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
        if dialog.ShowModal() == wx.ID_OK:
            actions = dialog.GetValues()
            id = self.GetNewId()
            actionblock = SFC_ActionBlock(self, actions, id)
            actionblock.SetPosition(bbox.x, bbox.y)
            min_width, min_height = actionblock.GetMinSize()
            actionblock.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
            self.AddBlock(actionblock)
            self.Controler.AddEditedElementActionBlock(self.TagName, id)
            self.RefreshActionBlockModel(actionblock)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            actionblock.Refresh()
        dialog.Destroy()

#-------------------------------------------------------------------------------
#                          Edit element content functions
#-------------------------------------------------------------------------------

    def EditBlockContent(self, block):
        dialog = BlockPropertiesDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetBlockList(self.Controler.GetBlockTypes(self.TagName, self.Debug))
        dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
        variable_names = self.Controler.GetEditedElementVariables(self.TagName, self.Debug)
        if block.GetName() != "":
            variable_names.remove(block.GetName())
        dialog.SetPouElementNames(variable_names)
        dialog.SetMinBlockSize(block.GetSize())
        old_values = {"name" : block.GetName(), 
                      "type" : block.GetType(), 
                      "extension" : block.GetExtension(), 
                      "inputs" : block.GetInputTypes(), 
                      "executionControl" : block.GetExecutionControl(), 
                      "executionOrder" : block.GetExecutionOrder()}
        dialog.SetValues(old_values)
        if dialog.ShowModal() == wx.ID_OK:
            new_values = dialog.GetValues()
            rect = block.GetRedrawRect(1, 1)
            if "name" in new_values:
                block.SetName(new_values["name"])
            else:
                block.SetName("")
            block.SetSize(*self.GetScaledSize(new_values["width"], new_values["height"]))
            block.SetType(new_values["type"], new_values["extension"], executionControl = new_values["executionControl"])
            block.SetExecutionOrder(new_values["executionOrder"])
            rect = rect.Union(block.GetRedrawRect())
            self.RefreshBlockModel(block)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            self.ParentWindow.RefreshVariablePanel(self.TagName)
            self.ParentWindow.RefreshInstancesTree()
            if old_values["executionOrder"] != new_values["executionOrder"]:
                self.RefreshView()
            else:
                block.Refresh(rect)
        dialog.Destroy()

    def EditVariableContent(self, variable):
        words = self.TagName.split("::")
        if words[0] == "T":
            dialog = VariablePropertiesDialog(self.ParentWindow, self.Controler, words[2])
        else:
            dialog = VariablePropertiesDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetMinVariableSize(variable.GetSize())
        varlist = []
        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
        if vars:
            for var in vars:
                if var["Edit"]:
                    varlist.append((var["Name"], var["Class"], var["Type"]))
        returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
        if returntype:
            varlist.append((self.Controler.GetEditedElementName(self.TagName), "Output", returntype))
        dialog.SetVariables(varlist)
        old_values = {"name" : variable.GetName(), "type" : variable.GetType(), 
            "executionOrder" : variable.GetExecutionOrder()}
        dialog.SetValues(old_values)
        if dialog.ShowModal() == wx.ID_OK:
            new_values = dialog.GetValues()
            rect = variable.GetRedrawRect(1, 1)
            variable.SetName(new_values["name"])
            variable.SetType(new_values["type"], new_values["value_type"])
            variable.SetSize(*self.GetScaledSize(new_values["width"], new_values["height"]))
            variable.SetExecutionOrder(new_values["executionOrder"])
            rect = rect.Union(variable.GetRedrawRect())
            if old_values["type"] != new_values["type"]:
                id = variable.GetId()
                self.Controler.RemoveEditedElementInstance(self.TagName, id)
                self.Controler.AddEditedElementVariable(self.TagName, id, new_values["type"])
            self.RefreshVariableModel(variable)
            if old_values["executionOrder"] != new_values["executionOrder"]:
                self.RefreshView()
            self.RefreshBuffer()
            self.RefreshVisibleElements()
            self.RefreshScrollBars()
            variable.Refresh(rect)
        dialog.Destroy()

    def EditConnectionContent(self, connection):
        dialog = ConnectionPropertiesDialog(self.ParentWindow, self.Controler)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetMinConnectionSize(connection.GetSize())
        values = {"name" : connection.GetName(), "type" : connection.GetType()}
        dialog.SetValues(values)
        if dialog.ShowModal() == wx.ID_OK:
            old_type = connection.GetType()
            values = dialog.GetValues()
            rect = connection.GetRedrawRect(1, 1)
            connection.SetName(values["name"])
            connection.SetType(values["type"])
            connection.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            rect = rect.Union(connection.GetRedrawRect())
            if old_type != values["type"]:
                id = connection.GetId()
                self.Controler.RemoveEditedElementInstance(self.TagName, id)
                self.Controler.AddEditedElementConnection(self.TagName, id, values["type"])
            self.RefreshConnectionModel(connection)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            connection.Refresh(rect)
        dialog.Destroy()

    def EditContactContent(self, contact):
        dialog = LDElementDialog(self.ParentWindow, self.Controler, "contact")
        dialog.SetPreviewFont(self.GetFont())
        varlist = []
        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
        if vars:
            for var in vars:
                if var["Type"] == "BOOL":
                    varlist.append(var["Name"])
        dialog.SetVariables(varlist)
        values = {"name" : contact.GetName(), "type" : contact.GetType()}
        dialog.SetValues(values)
        dialog.SetElementSize(contact.GetSize())
        if dialog.ShowModal() == wx.ID_OK:
            values = dialog.GetValues()
            rect = contact.GetRedrawRect(1, 1)
            contact.SetName(values["name"])
            contact.SetType(values["type"])
            contact.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            rect = rect.Union(contact.GetRedrawRect())
            self.RefreshContactModel(contact)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            contact.Refresh(rect)
        dialog.Destroy()

    def EditCoilContent(self, coil):
        dialog = LDElementDialog(self.ParentWindow, self.Controler, "coil")
        dialog.SetPreviewFont(self.GetFont())
        varlist = []
        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
        if vars:
            for var in vars:
                if var["Class"] != "Input" and var["Type"] == "BOOL":
                    varlist.append(var["Name"])
        returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
        if returntype == "BOOL":
            varlist.append(self.Controler.GetEditedElementName(self.TagName))
        dialog.SetVariables(varlist)
        values = {"name" : coil.GetName(), "type" : coil.GetType()}
        dialog.SetValues(values)
        dialog.SetElementSize(coil.GetSize())
        if dialog.ShowModal() == wx.ID_OK:
            values = dialog.GetValues()
            rect = coil.GetRedrawRect(1, 1)
            coil.SetName(values["name"])
            coil.SetType(values["type"])
            coil.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            rect = rect.Union(coil.GetRedrawRect())
            self.RefreshCoilModel(coil)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            coil.Refresh(rect)
        dialog.Destroy()

    def EditPowerRailContent(self, powerrail):
        dialog = LDPowerRailDialog(self.ParentWindow, self.Controler, powerrail.GetType(), len(powerrail.GetConnectors()))
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetMinSize(powerrail.GetSize())
        if dialog.ShowModal() == wx.ID_OK:
            old_type = powerrail.GetType()
            values = dialog.GetValues()
            rect = powerrail.GetRedrawRect(1, 1)
            powerrail.SetType(values["type"], [True for i in xrange(values["number"])])
            powerrail.SetSize(*self.GetScaledSize(values["width"], values["height"]))
            rect = rect.Union(powerrail.GetRedrawRect())
            if old_type != values["type"]:
                id = powerrail.GetId()
                self.Controler.RemoveEditedElementInstance(self.TagName, id)
                self.Controler.AddEditedElementPowerRail(self.TagName, id, values["type"])
            self.RefreshPowerRailModel(powerrail)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            powerrail.Refresh(rect)
        dialog.Destroy()

    def EditStepContent(self, step):
        dialog = StepContentDialog(self.ParentWindow, self.Controler, step.GetInitial())
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
        dialog.SetVariables(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
        dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step) and block.GetName() != step.GetName()])
        dialog.SetMinStepSize(step.GetSize())
        values = {"name" : step.GetName()}
        connectors = step.GetConnectors()
        values["input"] = connectors["input"] != None
        values["output"] = connectors["output"] != None
        values["action"] = connectors["action"] != None
        dialog.SetValues(values)
        if dialog.ShowModal() == wx.ID_OK:
            values = dialog.GetValues()
            rect = step.GetRedrawRect(1, 1)
            step.SetName(values["name"])
            if values["input"]:
                step.AddInput()
            else:
                step.RemoveInput()
            if values["output"]:
                step.AddOutput()
            else:
                step.RemoveOutput()
            if values["action"]:
                step.AddAction()    
            else:
                step.RemoveAction()
            step.UpdateSize(*self.GetScaledSize(values["width"], values["height"]))
            rect = rect.Union(step.GetRedrawRect())
            self.RefreshStepModel(step)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            step.Refresh(rect)
        
    def EditTransitionContent(self, transition):
        dialog = TransitionContentDialog(self.ParentWindow, self.Controler, self.GetDrawingMode() == FREEDRAWING_MODE)
        dialog.SetPreviewFont(self.GetFont())
        dialog.SetTransitions(self.Controler.GetEditedElementTransitions(self.TagName, self.Debug))
        dialog.SetValues({"type":transition.GetType(),"value":transition.GetCondition(), "priority":transition.GetPriority()})
        dialog.SetElementSize(transition.GetSize())
        if dialog.ShowModal() == wx.ID_OK:
            values = dialog.GetValues()
            rect = transition.GetRedrawRect(1, 1)
            transition.SetType(values["type"],values["value"])
            transition.SetPriority(values["priority"])
            rect = rect.Union(transition.GetRedrawRect())
            self.RefreshTransitionModel(transition)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            transition.Refresh(rect)
        dialog.Destroy()

    def EditJumpContent(self, jump):
        choices = []
        for block in self.Blocks:
            if isinstance(block, SFC_Step):
                choices.append(block.GetName())
        dialog = wx.SingleChoiceDialog(self.ParentWindow, "Edit jump target", "Please choose a target", choices, wx.OK|wx.CANCEL)
        dialog.SetSelection(choices.index(jump.GetTarget()))
        if dialog.ShowModal() == wx.ID_OK:
            value = dialog.GetStringSelection()
            rect = jump.GetRedrawRect(1, 1)
            jump.SetTarget(value)
            rect = rect.Union(jump.GetRedrawRect())
            self.RefreshJumpModel(jump)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            jump.Refresh(rect)
        dialog.Destroy()

    def EditActionBlockContent(self, actionblock):
        dialog = ActionBlockDialog(self.ParentWindow)
        dialog.SetQualifierList(self.Controler.GetQualifierTypes())
        dialog.SetActionList(self.Controler.GetEditedElementActions(self.TagName, self.Debug))
        dialog.SetVariableList(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
        dialog.SetValues(actionblock.GetActions())
        if dialog.ShowModal() == wx.ID_OK:
            actions = dialog.GetValues()
            rect = actionblock.GetRedrawRect(1, 1)
            actionblock.SetActions(actions)
            actionblock.SetSize(*self.GetScaledSize(*actionblock.GetSize()))
            rect = rect.Union(actionblock.GetRedrawRect())
            self.RefreshActionBlockModel(actionblock)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            actionblock.Refresh(rect)
        dialog.Destroy()

    def EditCommentContent(self, comment):
        if wx.VERSION >= (2, 5, 0):
            dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", comment.GetContent(), wx.OK|wx.CANCEL|wx.TE_MULTILINE)
        else:
            dialog = wx.TextEntryDialog(self.ParentWindow, "Edit comment", "Please enter comment text", comment.GetContent(), wx.OK|wx.CANCEL)
        if dialog.ShowModal() == wx.ID_OK:
            value = dialog.GetValue()
            rect = comment.GetRedrawRect(1, 1)
            comment.SetContent(value)
            comment.SetSize(*self.GetScaledSize(*comment.GetSize()))
            rect = rect.Union(comment.GetRedrawRect())
            self.RefreshCommentModel(comment)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            comment.Refresh(rect)
        dialog.Destroy()

#-------------------------------------------------------------------------------
#                          Model update functions
#-------------------------------------------------------------------------------

    def RefreshBlockModel(self, block):
        blockid = block.GetId()
        infos = {}
        infos["type"] = block.GetType()
        infos["name"] = block.GetName()
        if self.CurrentLanguage == "FBD":
            infos["executionOrder"] = block.GetExecutionOrder()
        infos["x"], infos["y"] = block.GetPosition()
        infos["width"], infos["height"] = block.GetSize()
        infos["connectors"] = block.GetConnectors()
        self.Controler.SetEditedElementBlockInfos(self.TagName, blockid, infos)
    
    def RefreshVariableModel(self, variable):
        variableid = variable.GetId()
        infos = {}
        infos["name"] = variable.GetName()
        if self.CurrentLanguage == "FBD":
            infos["executionOrder"] = variable.GetExecutionOrder()
        infos["x"], infos["y"] = variable.GetPosition()
        infos["width"], infos["height"] = variable.GetSize()
        infos["connectors"] = variable.GetConnectors()
        self.Controler.SetEditedElementVariableInfos(self.TagName, variableid, infos)

    def RefreshConnectionModel(self, connection):
        connectionid = connection.GetId()
        infos = {}
        infos["name"] = connection.GetName()
        infos["x"], infos["y"] = connection.GetPosition()
        infos["width"], infos["height"] = connection.GetSize()
        infos["connector"] = connection.GetConnector()
        self.Controler.SetEditedElementConnectionInfos(self.TagName, connectionid, infos)

    def RefreshCommentModel(self, comment):
        commentid = comment.GetId()
        infos = {}
        infos["content"] = comment.GetContent()
        infos["x"], infos["y"] = comment.GetPosition()
        infos["width"], infos["height"] = comment.GetSize()
        self.Controler.SetEditedElementCommentInfos(self.TagName, commentid, infos)

    def RefreshPowerRailModel(self, powerrail):
        powerrailid = powerrail.GetId()
        infos = {}
        infos["x"], infos["y"] = powerrail.GetPosition()
        infos["width"], infos["height"] = powerrail.GetSize()
        infos["connectors"] = powerrail.GetConnectors()
        self.Controler.SetEditedElementPowerRailInfos(self.TagName, powerrailid, infos)

    def RefreshContactModel(self, contact):
        contactid = contact.GetId()
        infos = {}
        infos["name"] = contact.GetName()
        infos["type"] = contact.GetType()
        infos["x"], infos["y"] = contact.GetPosition()
        infos["width"], infos["height"] = contact.GetSize()
        infos["connectors"] = contact.GetConnectors()
        self.Controler.SetEditedElementContactInfos(self.TagName, contactid, infos)

    def RefreshCoilModel(self, coil):
        coilid = coil.GetId()
        infos = {}
        infos["name"] = coil.GetName()
        infos["type"] = coil.GetType()
        infos["x"], infos["y"] = coil.GetPosition()
        infos["width"], infos["height"] = coil.GetSize()
        infos["connectors"] = coil.GetConnectors()
        self.Controler.SetEditedElementCoilInfos(self.TagName, coilid, infos)

    def RefreshStepModel(self, step):
        stepid = step.GetId()
        infos = {}
        infos["name"] = step.GetName()
        infos["initial"] = step.GetInitial()
        infos["x"], infos["y"] = step.GetPosition()
        infos["width"], infos["height"] = step.GetSize()
        infos["connectors"] = step.GetConnectors()
        self.Controler.SetEditedElementStepInfos(self.TagName, stepid, infos)

    def RefreshTransitionModel(self, transition):
        transitionid = transition.GetId()
        infos = {}
        infos["type"] = transition.GetType()
        infos["priority"] = transition.GetPriority()
        infos["condition"] = transition.GetCondition()
        infos["x"], infos["y"] = transition.GetPosition()
        infos["width"], infos["height"] = transition.GetSize()
        infos["connectors"] = transition.GetConnectors()
        self.Controler.SetEditedElementTransitionInfos(self.TagName, transitionid, infos)

    def RefreshDivergenceModel(self, divergence):
        divergenceid = divergence.GetId()
        infos = {}
        infos["x"], infos["y"] = divergence.GetPosition()
        infos["width"], infos["height"] = divergence.GetSize()
        infos["connectors"] = divergence.GetConnectors()
        self.Controler.SetEditedElementDivergenceInfos(self.TagName, divergenceid, infos)

    def RefreshJumpModel(self, jump):
        jumpid = jump.GetId()
        infos = {}
        infos["target"] = jump.GetTarget()
        infos["x"], infos["y"] = jump.GetPosition()
        infos["width"], infos["height"] = jump.GetSize()
        infos["connector"] = jump.GetConnector()
        self.Controler.SetEditedElementJumpInfos(self.TagName, jumpid, infos)

    def RefreshActionBlockModel(self, actionblock):
        actionblockid = actionblock.GetId()
        infos = {}
        infos["actions"] = actionblock.GetActions()
        infos["x"], infos["y"] = actionblock.GetPosition()
        infos["width"], infos["height"] = actionblock.GetSize()
        infos["connector"] = actionblock.GetConnector()
        self.Controler.SetEditedElementActionBlockInfos(self.TagName, actionblockid, infos)


#-------------------------------------------------------------------------------
#                          Model delete functions
#-------------------------------------------------------------------------------


    def DeleteBlock(self, block):
        elements = []
        for output in block.GetConnectors()["outputs"]:
            for element in output.GetConnectedBlocks():
                if element not in elements:
                    elements.append(element)
        block.Clean()
        self.RemoveBlock(block)
        self.Controler.RemoveEditedElementInstance(self.TagName, block.GetId())
        for element in elements:
            element.RefreshModel()
        wx.CallAfter(self.ParentWindow.RefreshVariablePanel, self.TagName)
        wx.CallAfter(self.ParentWindow.RefreshInstancesTree)
        
    def DeleteVariable(self, variable):
        connectors = variable.GetConnectors()
        if connectors["output"]:
            elements = connectors["output"].GetConnectedBlocks()
        else:
            elements = []
        variable.Clean()
        self.RemoveBlock(variable)
        self.Controler.RemoveEditedElementInstance(self.TagName, variable.GetId())
        for element in elements:
            element.RefreshModel()

    def DeleteConnection(self, connection):
        if connection.GetType() == CONTINUATION:
            elements = connection.GetConnector().GetConnectedBlocks()
        else:
            elements = []
        connection.Clean()
        self.RemoveBlock(connection)
        self.Controler.RemoveEditedElementInstance(self.TagName, connection.GetId())
        for element in elements:
            element.RefreshModel()

    def DeleteComment(self, comment):
        self.RemoveComment(comment)
        self.Controler.RemoveEditedElementInstance(self.TagName, comment.GetId())

    def DeleteWire(self, wire):
        if wire in self.Wires:
            connected = wire.GetConnected()
            wire.Clean()
            self.RemoveWire(wire)
            for connector in connected:
                connector.RefreshParentBlock()

    def DeleteContact(self, contact):
        connectors = contact.GetConnectors()
        elements = connectors["output"].GetConnectedBlocks()
        contact.Clean()
        self.RemoveBlock(contact)
        self.Controler.RemoveEditedElementInstance(self.TagName, contact.GetId())
        for element in elements:
            element.RefreshModel()

    def DeleteCoil(self, coil):
        connectors = coil.GetConnectors()
        elements = connectors["output"].GetConnectedBlocks()
        coil.Clean()
        self.RemoveBlock(coil)
        self.Controler.RemoveEditedElementInstance(self.TagName, coil.GetId())
        for element in elements:
            element.RefreshModel()

    def DeletePowerRail(self, powerrail):
        elements = []
        if powerrail.GetType() == LEFTRAIL:
            for connector in powerrail.GetConnectors():
                for element in connector.GetConnectedBlocks():
                    if element not in elements:
                        elements.append(element)
        powerrail.Clean()
        self.RemoveBlock(powerrail)
        self.Controler.RemoveEditedElementInstance(self.TagName, powerrail.GetId())
        for element in elements:
            element.RefreshModel()

    def DeleteStep(self, step):
        elements = []
        connectors = step.GetConnectors()
        if connectors["output"]:
            for element in connectors["output"].GetConnectedBlocks():
                if element not in elements:
                    elements.append(element)
        if connectors["action"]:
            for element in connectors["action"].GetConnectedBlocks():
                if element not in elements:
                    elements.append(element)
        step.Clean()
        self.RemoveBlock(step)
        self.Controler.RemoveEditedElementInstance(self.TagName, step.GetId())
        for element in elements:
            element.RefreshModel()
            
    def DeleteTransition(self, transition):
        elements = []
        connectors = transition.GetConnectors()
        if connectors["output"]:
            for element in connectors["output"].GetConnectedBlocks():
                if element not in elements:
                    elements.append(element)
        transition.Clean()
        self.RemoveBlock(transition)
        self.Controler.RemoveEditedElementInstance(self.TagName, transition.GetId())
        for element in elements:
            element.RefreshModel()

    def DeleteDivergence(self, divergence):
        elements = []
        connectors = divergence.GetConnectors()
        for output in connectors["outputs"]:
            for element in output.GetConnectedBlocks():
                if element not in elements:
                    elements.append(element)
        divergence.Clean()
        self.RemoveBlock(divergence)
        self.Controler.RemoveEditedElementInstance(self.TagName, divergence.GetId())
        for element in elements:
            element.RefreshModel()
    
    def DeleteJump(self, jump):
        jump.Clean()
        self.RemoveBlock(jump)
        self.Controler.RemoveEditedElementInstance(self.TagName, jump.GetId())
    
    def DeleteActionBlock(self, actionblock):
        actionblock.Clean()
        self.RemoveBlock(actionblock)
        self.Controler.RemoveEditedElementInstance(self.TagName, actionblock.GetId())


#-------------------------------------------------------------------------------
#                            Editing functions
#-------------------------------------------------------------------------------
    
    def Cut(self):
        if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)):
            self.ParentWindow.SetCopyBuffer(self.SelectedElement.Clone(self))
            rect = self.SelectedElement.GetRedrawRect(1, 1)
            self.SelectedElement.Delete()
            self.SelectedElement = None
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.ParentWindow.RefreshVariablePanel(self.TagName)
            self.ParentWindow.RefreshInstancesTree()
            self.RefreshRect(self.GetScrolledRect(rect), False)
        
    def Copy(self):
        if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)):
            self.ParentWindow.SetCopyBuffer(self.SelectedElement.Clone(self))
            
    def Paste(self):
        element = self.ParentWindow.GetCopyBuffer()
        if not self.Debug and element is not None and self.CanAddElement(element):
            block = self.CopyBlock(element, wx.Point(*self.CalcUnscrolledPosition(30, 30)))
            if self.SelectedElement is not None:
                self.SelectedElement.SetSelected(False)
            self.SelectedElement = block
            self.SelectedElement.SetSelected(True)
            self.RefreshBuffer()
            self.RefreshScrollBars()
            self.RefreshVisibleElements()
            self.ParentWindow.RefreshVariablePanel(self.TagName)
            self.ParentWindow.RefreshInstancesTree()
        else:
            message = wx.MessageDialog(self, "You can't paste the element in buffer here!", "Error", wx.OK|wx.ICON_ERROR)
            message.ShowModal()
            message.Destroy()

    def CanAddElement(self, block):
        if isinstance(block, Graphic_Group):
            return block.CanAddBlocks(self)
        elif self.CurrentLanguage == "SFC":
            return True
        elif self.CurrentLanguage == "LD" and not isinstance(block, (SFC_Step, SFC_Transition, SFC_Divergence, SFC_Jump, SFC_ActionBlock)):
            return True
        elif self.CurrentLanguage == "FBD" and isinstance(block, (FBD_Block, FBD_Variable, FBD_Connector, Comment)):
            return True
        return False

    def GenerateNewName(self, element):
        if isinstance(element, FBD_Block):
            names = [varname.upper() for varname in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]
            format = "Block%d"
        elif isinstance(element, SFC_Step):
            names = [block.GetName().upper() for block in self.Blocks if isinstance(block, SFC_Step)]
            format = "Step%d"
        i = 1
        while (format%i).upper() in names:
            i += 1
        return format%i

    def IsNamedElement(self, element):
        return isinstance(element, FBD_Block) and element.GetName() != "" or isinstance(element, SFC_Step)

    def CopyBlock(self, element, pos):
        id = self.GetNewId()
        if isinstance(element, Graphic_Group):
            block = element.Clone(self, pos=pos)
        else:
            if self.IsNamedElement(element):
                name = self.GenerateNewName(element)
                block = element.Clone(self, id, name, pos)
            else:
                name = None
                block = element.Clone(self, id, pos=pos)
            self.AddBlockInModel(block)
        return block
    
    def AddBlockInModel(self, block):
        if isinstance(block, Comment):
            self.AddComment(block)
            self.Controler.AddEditedElementComment(self.TagName, block.GetId())
            self.RefreshCommentModel(block)
        else:
            self.AddBlock(block)
            if isinstance(block, FBD_Block):
                self.Controler.AddEditedElementBlock(self.TagName, block.GetId(), block.GetType(), block.GetName())
                self.RefreshBlockModel(block)
            elif isinstance(block, FBD_Variable):
                self.Controler.AddEditedElementVariable(self.TagName, block.GetId(), block.GetType())
                self.RefreshVariableModel(block)
            elif isinstance(block, FBD_Connector):
                self.Controler.AddEditedElementConnection(self.TagName, block.GetId(), block.GetType())
                self.RefreshConnectionModel(block)
            elif isinstance(block, LD_Contact):
                self.Controler.AddEditedElementContact(self.TagName, block.GetId())
                self.RefreshContactModel(block)
            elif isinstance(block, LD_Coil):
                self.Controler.AddEditedElementCoil(self.TagName, block.GetId())
                self.RefreshCoilModel(block)
            elif isinstance(block, LD_PowerRail):
                self.Controler.AddEditedElementPowerRail(self.TagName, block.GetId(), block.GetType())
                self.RefreshPowerRailModel(block)
            elif isinstance(block, SFC_Step):
                self.Controler.AddEditedElementStep(self.TagName, block.GetId())
                self.RefreshStepModel(block)    
            elif isinstance(block, SFC_Transition):
                self.Controler.AddEditedElementTransition(self.TagName, block.GetId())
                self.RefreshTransitionModel(block)       
            elif isinstance(block, SFC_Divergence):
                self.Controler.AddEditedElementDivergence(self.TagName, block.GetId(), block.GetType())
                self.RefreshDivergenceModel(block)
            elif isinstance(block, SFC_Jump):
                self.Controler.AddEditedElementJump(self.TagName, block.GetId())
                self.RefreshJumpModel(block)       
            elif isinstance(block, SFC_ActionBlock):
                self.Controler.AddEditedElementActionBlock(self.TagName, block.GetId())
                self.RefreshActionBlockModel(block)


#-------------------------------------------------------------------------------
#                        Errors showing functions
#-------------------------------------------------------------------------------

    def ClearErrors(self):
        self.Errors = []
        self.RefreshView()

    def AddShownError(self, infos, start, end):
        self.Errors.append((infos, start, end))

    def ShowErrors(self):
        for infos, start, end in self.Errors:
            if infos[0] in ["io_variable", "block", "coil", "contact", "transition", "step", "action_block"]:
                block = self.FindElementById(infos[1])
                if block is not None:
                    block.AddError(infos[2:], start, end)    
        
#-------------------------------------------------------------------------------
#                            Drawing functions
#-------------------------------------------------------------------------------

    def OnScrollWindow(self, event):
        if event.GetOrientation() == wx.HORIZONTAL:
            self.RefreshVisibleElements(xp = event.GetPosition())
        else:
            self.RefreshVisibleElements(yp = event.GetPosition())
        event.Skip()

    def OnMouseWheelWindow(self, event):
        x, y = self.GetViewStart()
        yp = max(0, min(y - event.GetWheelRotation() / event.GetWheelDelta() * 3, self.GetVirtualSize()[1] / self.GetScrollPixelsPerUnit()[1]))
        self.RefreshVisibleElements(yp = yp)
        self.Scroll(x, yp)
        
    def OnMoveWindow(self, event):
        if not USE_AUI:
            self.GetBestSize()
        self.RefreshScrollBars()
        self.RefreshVisibleElements()
        event.Skip()

    def DoDrawing(self, dc, printing = False):
        if printing:
            if getattr(dc, "printing", False):
                font = wx.Font(self.GetFont().GetPointSize(), wx.MODERN, wx.NORMAL, wx.NORMAL)
                dc.SetFont(font)
            else:
                dc.SetFont(self.GetFont())
        else:
            dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
            dc.Clear()
            dc.BeginDrawing()
        if self.Scaling is not None and self.DrawGrid and not printing:
            dc.SetPen(wx.TRANSPARENT_PEN)
            dc.SetBrush(self.GridBrush)
            xstart, ystart = self.GetViewStart()
            window_size = self.GetClientSize()
            width, height = self.GetVirtualSize()
            width = max(width, xstart * SCROLLBAR_UNIT + window_size[0])
            height = max(height, ystart * SCROLLBAR_UNIT + window_size[1])
            dc.DrawRectangle(0, 0, width, height)
        if self.PageSize is not None and not printing:
            dc.SetPen(self.PagePen)
            xstart, ystart = self.GetViewStart()
            window_size = self.GetClientSize()
            for x in xrange(self.PageSize[0] - (xstart * SCROLLBAR_UNIT) % self.PageSize[0], window_size[0], self.PageSize[0]):
                dc.DrawLine(xstart * SCROLLBAR_UNIT + x + 1, ystart * SCROLLBAR_UNIT, 
                            xstart * SCROLLBAR_UNIT + x + 1, ystart * SCROLLBAR_UNIT + window_size[1])
            for y in xrange(self.PageSize[1] - (ystart * SCROLLBAR_UNIT) % self.PageSize[1], window_size[1], self.PageSize[1]):
                dc.DrawLine(xstart * SCROLLBAR_UNIT, ystart * SCROLLBAR_UNIT + y + 1, 
                            xstart * SCROLLBAR_UNIT + window_size[0], ystart * SCROLLBAR_UNIT + y + 1)
        
        # Draw all elements
        for comment in self.Comments:
            if comment != self.SelectedElement and (comment.IsVisible() or printing):
                comment.Draw(dc)
        for wire in self.Wires:
            if wire != self.SelectedElement and (wire.IsVisible() or printing):
                 if not self.Debug or wire.GetValue() != True:
                    wire.Draw(dc)
        if self.Debug:
            for wire in self.Wires:
                if wire != self.SelectedElement and (wire.IsVisible() or printing) and wire.GetValue() == True:
                    wire.Draw(dc)
        for block in self.Blocks:
            if block != self.SelectedElement and (block.IsVisible() or printing):
                block.Draw(dc)
        
        if self.SelectedElement is not None and (self.SelectedElement.IsVisible() or printing):
            self.SelectedElement.Draw(dc)
        
        if not printing:
            if self.rubberBand.IsShown():
                self.rubberBand.Draw(dc)
            dc.EndDrawing()

    def OnPaint(self, event):
        self.DoDrawing(self.GetLogicalDC(True))
        event.Skip()