TextViewer.py
author lbessard
Tue, 10 Jul 2007 14:29:31 +0200
changeset 31 d833bf7567b1
parent 27 dae55dd9ee14
child 47 2b2f8d88e6d3
permissions -rw-r--r--
Bug on variable location fixed
27
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     3
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     5
#based on the plcopen standard. 
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     6
#
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     7
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     8
#
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    10
#
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    15
#
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    19
#Lesser General Public License for more details.
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    20
#
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    24
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    25
from wxPython.wx import *
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    26
from wxPython.stc import * 
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    27
import wx
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    28
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    29
import re
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    30
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    31
#-------------------------------------------------------------------------------
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    32
#                         Textual programs Viewer class
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    33
#-------------------------------------------------------------------------------
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    34
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    35
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    36
NEWLINE = "\n"
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    37
NUMBERS = [str(i) for i in xrange(10)]
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    38
LETTERS = ['_']
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    39
for i in xrange(26):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    40
    LETTERS.append(chr(ord('a') + i))
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    41
    LETTERS.append(chr(ord('A') + i))
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    42
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    43
[wxSTC_PLC_WORD, wxSTC_PLC_COMMENT, wxSTC_PLC_NUMBER, wxSTC_PLC_VARIABLE, 
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    44
 wxSTC_PLC_FUNCTION, wxSTC_PLC_JUMP] = range(6)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    45
[SPACE, WORD, NUMBER, COMMENT] = range(4)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    46
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    47
[wxID_TEXTVIEWER,
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    48
] = [wx.NewId() for _init_ctrls in range(1)]
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    49
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    50
if wx.Platform == '__WXMSW__':
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    51
    faces = { 'times': 'Times New Roman',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    52
              'mono' : 'Courier New',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    53
              'helv' : 'Arial',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    54
              'other': 'Comic Sans MS',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    55
              'size' : 10,
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    56
             }
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    57
else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    58
    faces = { 'times': 'Times',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    59
              'mono' : 'Courier',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    60
              'helv' : 'Helvetica',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    61
              'other': 'new century schoolbook',
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    62
              'size' : 12,
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    63
             }
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    64
re_texts = {}
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    65
re_texts["letter"] = "[A-Za-z]"
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    66
re_texts["digit"] = "[0-9]"
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    67
re_texts["identifier"] = "((?:%(letter)s|(?:_(?:%(letter)s|%(digit)s)))(?:_?(?:%(letter)s|%(digit)s))*)"%re_texts
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    68
IDENTIFIER_MODEL = re.compile(re_texts["identifier"])
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    69
LABEL_MODEL = re.compile("[ \t\n]%(identifier)s:[ \t\n]"%re_texts)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    70
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    71
class TextViewer(wxStyledTextCtrl):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    72
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    73
    def __init__(self, parent, window, controler):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    74
        wxStyledTextCtrl.__init__(self, parent, wxID_TEXTVIEWER, style=0)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    75
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    76
        self.CmdKeyAssign(ord('+'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMIN)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    77
        self.CmdKeyAssign(ord('-'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMOUT)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    78
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    79
        self.SetViewWhiteSpace(False)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    80
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    81
        self.SetLexer(wxSTC_LEX_CONTAINER)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    82
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    83
        # Global default styles for all languages
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    84
        self.StyleSetSpec(wxSTC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    85
        self.StyleClearAll()  # Reset all to be like the default
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    86
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    87
        self.StyleSetSpec(wxSTC_STYLE_LINENUMBER,  "back:#C0C0C0,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    88
        self.SetSelBackground(1, "#E0E0E0")
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    89
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    90
        # Highlighting styles
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    91
        self.StyleSetSpec(wxSTC_PLC_WORD, "fore:#00007F,bold,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    92
        self.StyleSetSpec(wxSTC_PLC_VARIABLE, "fore:#7F0000,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    93
        self.StyleSetSpec(wxSTC_PLC_FUNCTION, "fore:#7F7F00,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    94
        self.StyleSetSpec(wxSTC_PLC_COMMENT, "fore:#7F7F7F,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    95
        self.StyleSetSpec(wxSTC_PLC_NUMBER, "fore:#007F7F,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    96
        self.StyleSetSpec(wxSTC_PLC_JUMP, "fore:#007F00,size:%(size)d" % faces)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    97
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    98
        # Indicators styles
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
    99
        self.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   100
        self.IndicatorSetForeground(0, wxRED)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   101
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   102
        # Line numbers in the margin
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   103
        self.SetMarginType(1, wxSTC_MARGIN_NUMBER)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   104
        self.SetMarginWidth(1, 50)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   105
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   106
        # Indentation size
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   107
        self.SetTabWidth(2)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   108
        self.SetUseTabs(0)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   109
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   110
        self.Keywords = []
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   111
        self.Variables = []
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   112
        self.Functions = []
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   113
        self.Jumps = []
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   114
        self.TextChanged = False
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   115
        self.TextSyntax = "ST"
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   116
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   117
        self.Controler = controler
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   118
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   119
        EVT_KEY_DOWN(self, self.OnKeyDown)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   120
        EVT_STC_STYLENEEDED(self, wxID_TEXTVIEWER, self.OnStyleNeeded)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   121
        EVT_KILL_FOCUS(self, self.OnKillFocus)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   122
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   123
    def SetTextSyntax(self, syntax):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   124
        self.TextSyntax = syntax
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   125
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   126
    def SetKeywords(self, keywords):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   127
        self.Keywords = [keyword.upper() for keyword in keywords]
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   128
        self.Colourise(0, -1)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   129
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   130
    def SetVariables(self, variables):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   131
        self.Variables = [variable.upper() for variable in variables]
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   132
        self.Colourise(0, -1)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   133
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   134
    def SetFunctions(self, blocktypes):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   135
        self.Functions = []
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   136
        for category in blocktypes:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   137
            for blocktype in category["list"]:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   138
                if blocktype["name"] not in self.Keywords and blocktype["name"] not in self.Variables:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   139
                    self.Functions.append(blocktype["name"].upper())
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   140
        self.Colourise(0, -1)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   141
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   142
    def RefreshJumpList(self):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   143
        self.Jumps = [jump.upper() for jump in LABEL_MODEL.findall(self.GetText())]
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   144
        self.Colourise(0, -1)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   145
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   146
    def RefreshView(self):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   147
        self.SetText(self.Controler.GetCurrentElementEditingText())
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   148
        self.RefreshJumpList()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   149
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   150
    def OnStyleNeeded(self, event):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   151
        self.TextChanged = True
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   152
        line = self.LineFromPosition(self.GetEndStyled())
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   153
        if line == 0:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   154
            start_pos = 0
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   155
        else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   156
            start_pos = self.GetLineEndPosition(line - 1) + 1
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   157
        end_pos = event.GetPosition()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   158
        self.StartStyling(start_pos, 0xff)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   159
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   160
        i = start_pos
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   161
        state = SPACE
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   162
        line = ""
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   163
        word = ""
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   164
        while i < end_pos:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   165
            char = chr(self.GetCharAt(i)).upper()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   166
            line += char
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   167
            if char == NEWLINE:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   168
                if state == COMMENT:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   169
                    self.SetStyling(i - start_pos + 1, wxSTC_PLC_COMMENT)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   170
                elif state == NUMBER:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   171
                    self.SetStyling(i - start_pos, wxSTC_PLC_NUMBER)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   172
                elif state == WORD:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   173
                    if word in self.Keywords:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   174
                        self.SetStyling(i - start_pos, wxSTC_PLC_WORD)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   175
                    elif word in self.Variables:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   176
                        self.SetStyling(i - start_pos, wxSTC_PLC_VARIABLE)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   177
                    elif word in self.Functions:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   178
                        self.SetStyling(i - start_pos, wxSTC_PLC_FUNCTION)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   179
                    elif word in self.Jumps:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   180
                        self.SetStyling(i - start_pos, wxSTC_PLC_JUMP)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   181
                    else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   182
                        self.SetStyling(i - start_pos, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   183
                        if self.GetCurrentPos() < start_pos or self.GetCurrentPos() > i:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   184
                            self.StartStyling(start_pos, wxSTC_INDICS_MASK)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   185
                            self.SetStyling(i - start_pos, wxSTC_INDIC0_MASK)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   186
                            self.StartStyling(i, 0xff)    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   187
                else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   188
                    self.SetStyling(i - start_pos, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   189
                start_pos = i
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   190
                state = SPACE
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   191
                line = ""
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   192
            elif line.endswith("(*") and state != COMMENT:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   193
                self.SetStyling(i - start_pos - 1, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   194
                start_pos = i
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   195
                state = COMMENT
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   196
            elif state == COMMENT:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   197
                if line.endswith("*)"):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   198
                    self.SetStyling(i - start_pos + 2, wxSTC_PLC_COMMENT)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   199
                    start_pos = i + 1
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   200
                    state = SPACE
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   201
            elif char in LETTERS:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   202
                if state == NUMBER:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   203
                    word = "#"
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   204
                    state = WORD
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   205
                elif state == SPACE:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   206
                    self.SetStyling(i - start_pos, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   207
                    word = char
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   208
                    start_pos = i
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   209
                    state = WORD
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   210
                else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   211
                    word += char
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   212
            elif char in NUMBERS or char == '.' and state != WORD:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   213
                if state == SPACE:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   214
                    self.SetStyling(i - start_pos, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   215
                    start_pos = i
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   216
                    state = NUMBER
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   217
                if state == WORD and char != '.':
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   218
                    word += char
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   219
            else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   220
                if state == WORD:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   221
                    if word in self.Keywords:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   222
                        self.SetStyling(i - start_pos, wxSTC_PLC_WORD)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   223
                    elif word in self.Variables:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   224
                        self.SetStyling(i - start_pos, wxSTC_PLC_VARIABLE)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   225
                    elif word in self.Functions:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   226
                        self.SetStyling(i - start_pos, wxSTC_PLC_FUNCTION)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   227
                    elif word in self.Jumps:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   228
                        self.SetStyling(i - start_pos, wxSTC_PLC_JUMP)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   229
                    else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   230
                        self.SetStyling(i - start_pos, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   231
                        if self.GetCurrentPos() < start_pos or self.GetCurrentPos() > i:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   232
                            self.StartStyling(start_pos, wxSTC_INDICS_MASK)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   233
                            self.SetStyling(i - start_pos, wxSTC_INDIC0_MASK)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   234
                            self.StartStyling(i, 0xff)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   235
                    word = ""
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   236
                    start_pos = i
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   237
                    state = SPACE
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   238
                elif state == NUMBER:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   239
                    self.SetStyling(i - start_pos, wxSTC_PLC_NUMBER)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   240
                    start_pos = i
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   241
                    state = SPACE
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   242
            i += 1
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   243
        if state == COMMENT:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   244
            self.SetStyling(i - start_pos + 2, wxSTC_PLC_COMMENT)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   245
        elif state == NUMBER:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   246
            self.SetStyling(i - start_pos, wxSTC_PLC_NUMBER)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   247
        elif state == WORD:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   248
            if word in self.Keywords:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   249
                self.SetStyling(i - start_pos, wxSTC_PLC_WORD)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   250
            elif word in self.Variables:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   251
                self.SetStyling(i - start_pos, wxSTC_PLC_VARIABLE)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   252
            elif word in self.Functions:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   253
                self.SetStyling(i - start_pos, wxSTC_PLC_FUNCTION)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   254
            elif word in self.Jumps:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   255
                self.SetStyling(i - start_pos, wxSTC_PLC_JUMP)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   256
            else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   257
                self.SetStyling(i - start_pos, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   258
        else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   259
            self.SetStyling(i - start_pos, 31)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   260
        event.Skip()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   261
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   262
    def Cut(self):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   263
        self.CmdKeyExecute(wxSTC_CMD_CUT)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   264
        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   265
    def Copy(self):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   266
        self.CmdKeyExecute(wxSTC_CMD_COPY)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   267
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   268
    def Paste(self):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   269
        self.CmdKeyExecute(wxSTC_CMD_PASTE)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   270
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   271
    def RefreshModel(self):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   272
        if self.TextChanged:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   273
            self.RefreshJumpList()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   274
            self.Controler.SetCurrentElementEditingText(self.GetText())
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   275
    
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   276
    def OnKeyDown(self, event):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   277
        if self.CallTipActive():
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   278
            self.CallTipCancel()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   279
        key = event.KeyCode()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   280
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   281
        # Code completion
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   282
        if key == WXK_SPACE and event.ControlDown():
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   283
            
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   284
            line = self.GetCurrentLine()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   285
            if line == 0:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   286
                start_pos = 0
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   287
            else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   288
                start_pos = self.GetLineEndPosition(line - 1) + 1
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   289
            end_pos = self.GetCurrentPos()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   290
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   291
            lineText = self.GetTextRange(start_pos, end_pos).replace("\t", " ")
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   292
            words = lineText.split(" ")
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   293
            words = [word for i, word in enumerate(words) if word != '' or i == len(words) - 1]
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   294
                        
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   295
            kw = []
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   296
            
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   297
            if self.TextSyntax == "IL":
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   298
                if len(words) == 1:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   299
                    kw = self.Keywords
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   300
                elif len(words) == 2:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   301
                    if words[0].upper() in ["CAL", "CALC", "CALNC"]:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   302
                        kw = self.Functions
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   303
                    elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   304
                        kw = self.Jumps
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   305
                    else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   306
                        kw = self.Variables
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   307
            else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   308
                kw = self.Keywords + self.Variables + self.Functions
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   309
            if len(kw) > 0:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   310
                kw.sort()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   311
                self.AutoCompSetIgnoreCase(True)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   312
                self.AutoCompShow(len(words[-1]), " ".join(kw))
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   313
        else:
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   314
            self.TextChanged = False
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   315
            wxCallAfter(self.RefreshModel)
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   316
            event.Skip()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   317
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   318
    def OnKillFocus(self, event):
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   319
        self.AutoCompCancel()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   320
        event.Skip()
dae55dd9ee14 Current developping version
lbessard
parents:
diff changeset
   321