author | laurent |
Tue, 14 Jun 2011 17:40:02 +0200 | |
changeset 546 | f28df922efbe |
parent 543 | 2f660878c2a7 |
child 549 | b0d6819119c3 |
permissions | -rw-r--r-- |
27 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
58 | 7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
27 | 8 |
# |
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
58 | 19 |
#General Public License for more details. |
27 | 20 |
# |
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
26 |
import wx.stc |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
27 |
from types import * |
27 | 28 |
|
29 |
import re |
|
30 |
||
31 |
#------------------------------------------------------------------------------- |
|
32 |
# Textual programs Viewer class |
|
33 |
#------------------------------------------------------------------------------- |
|
34 |
||
35 |
||
36 |
NEWLINE = "\n" |
|
37 |
NUMBERS = [str(i) for i in xrange(10)] |
|
38 |
LETTERS = ['_'] |
|
39 |
for i in xrange(26): |
|
40 |
LETTERS.append(chr(ord('a') + i)) |
|
41 |
LETTERS.append(chr(ord('A') + i)) |
|
42 |
||
543 | 43 |
[STC_PLC_WORD, STC_PLC_COMMENT, STC_PLC_NUMBER, STC_PLC_STRING, |
546 | 44 |
STC_PLC_VARIABLE, STC_PLC_PARAMETER, STC_PLC_FUNCTION, STC_PLC_JUMP, |
45 |
STC_PLC_ERROR] = range(9) |
|
543 | 46 |
[SPACE, WORD, NUMBER, STRING, WSTRING, COMMENT] = range(6) |
27 | 47 |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
48 |
[ID_TEXTVIEWER, |
27 | 49 |
] = [wx.NewId() for _init_ctrls in range(1)] |
50 |
||
51 |
if wx.Platform == '__WXMSW__': |
|
52 |
faces = { 'times': 'Times New Roman', |
|
53 |
'mono' : 'Courier New', |
|
54 |
'helv' : 'Arial', |
|
55 |
'other': 'Comic Sans MS', |
|
56 |
'size' : 10, |
|
57 |
} |
|
58 |
else: |
|
59 |
faces = { 'times': 'Times', |
|
60 |
'mono' : 'Courier', |
|
61 |
'helv' : 'Helvetica', |
|
62 |
'other': 'new century schoolbook', |
|
63 |
'size' : 12, |
|
64 |
} |
|
65 |
re_texts = {} |
|
66 |
re_texts["letter"] = "[A-Za-z]" |
|
67 |
re_texts["digit"] = "[0-9]" |
|
68 |
re_texts["identifier"] = "((?:%(letter)s|(?:_(?:%(letter)s|%(digit)s)))(?:_?(?:%(letter)s|%(digit)s))*)"%re_texts |
|
69 |
IDENTIFIER_MODEL = re.compile(re_texts["identifier"]) |
|
70 |
LABEL_MODEL = re.compile("[ \t\n]%(identifier)s:[ \t\n]"%re_texts) |
|
543 | 71 |
EXTENSIBLE_PARAMETER = re.compile("IN[1-9][0-9]*$") |
27 | 72 |
|
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
73 |
def GetCursorPos(old, new): |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
74 |
old_length = len(old) |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
75 |
new_length = len(new) |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
76 |
common_length = min(old_length, new_length) |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
77 |
i = 0 |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
78 |
for i in xrange(common_length): |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
79 |
if old[i] != new[i]: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
80 |
break |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
81 |
if old_length < new_length: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
82 |
if common_length > 0 and old[i] != new[i]: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
83 |
return i + new_length - old_length |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
84 |
else: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
85 |
return i + new_length - old_length + 1 |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
86 |
elif old_length > new_length or i < min(old_length, new_length) - 1: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
87 |
if common_length > 0 and old[i] != new[i]: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
88 |
return i |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
89 |
else: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
90 |
return i + 1 |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
91 |
else: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
92 |
return None |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
93 |
|
121 | 94 |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
95 |
class TextViewer(wx.stc.StyledTextCtrl): |
27 | 96 |
|
113 | 97 |
if wx.VERSION < (2, 6, 0): |
98 |
def Bind(self, event, function, id = None): |
|
99 |
if id is not None: |
|
100 |
event(self, id, function) |
|
101 |
else: |
|
102 |
event(self, function) |
|
103 |
||
249 | 104 |
def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""): |
80 | 105 |
wx.stc.StyledTextCtrl.__init__(self, parent, ID_TEXTVIEWER, size=wx.Size(0, 0), style=0) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
106 |
|
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
107 |
self.CmdKeyAssign(ord('+'), wx.stc.STC_SCMOD_CTRL, wx.stc.STC_CMD_ZOOMIN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
108 |
self.CmdKeyAssign(ord('-'), wx.stc.STC_SCMOD_CTRL, wx.stc.STC_CMD_ZOOMOUT) |
27 | 109 |
|
110 |
self.SetViewWhiteSpace(False) |
|
111 |
||
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
112 |
self.SetLexer(wx.stc.STC_LEX_CONTAINER) |
27 | 113 |
|
114 |
# Global default styles for all languages |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
115 |
self.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces) |
27 | 116 |
self.StyleClearAll() # Reset all to be like the default |
117 |
||
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
118 |
self.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,size:%(size)d" % faces) |
27 | 119 |
self.SetSelBackground(1, "#E0E0E0") |
120 |
||
121 |
# Highlighting styles |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
122 |
self.StyleSetSpec(STC_PLC_WORD, "fore:#00007F,bold,size:%(size)d" % faces) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
123 |
self.StyleSetSpec(STC_PLC_VARIABLE, "fore:#7F0000,size:%(size)d" % faces) |
546 | 124 |
self.StyleSetSpec(STC_PLC_PARAMETER, "fore:#7F007F,size:%(size)d" % faces) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
125 |
self.StyleSetSpec(STC_PLC_FUNCTION, "fore:#7F7F00,size:%(size)d" % faces) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
126 |
self.StyleSetSpec(STC_PLC_COMMENT, "fore:#7F7F7F,size:%(size)d" % faces) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
127 |
self.StyleSetSpec(STC_PLC_NUMBER, "fore:#007F7F,size:%(size)d" % faces) |
546 | 128 |
self.StyleSetSpec(STC_PLC_STRING, "fore:#007F00,size:%(size)d" % faces) |
129 |
self.StyleSetSpec(STC_PLC_JUMP, "fore:#FF7FFF,size:%(size)d" % faces) |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
130 |
self.StyleSetSpec(STC_PLC_ERROR, "fore:#FF0000,back:#FFFF00,size:%(size)d" % faces) |
27 | 131 |
|
132 |
# Indicators styles |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
133 |
self.IndicatorSetStyle(0, wx.stc.STC_INDIC_SQUIGGLE) |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
134 |
if window and controler: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
135 |
self.IndicatorSetForeground(0, wx.RED) |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
136 |
else: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
137 |
self.IndicatorSetForeground(0, wx.WHITE) |
27 | 138 |
|
139 |
# Line numbers in the margin |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
140 |
self.SetMarginType(1, wx.stc.STC_MARGIN_NUMBER) |
27 | 141 |
self.SetMarginWidth(1, 50) |
142 |
||
143 |
# Indentation size |
|
144 |
self.SetTabWidth(2) |
|
145 |
self.SetUseTabs(0) |
|
146 |
||
147 |
self.Keywords = [] |
|
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
148 |
self.Variables = {} |
543 | 149 |
self.Functions = {} |
150 |
self.TypeNames = [] |
|
27 | 151 |
self.Jumps = [] |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
122
diff
changeset
|
152 |
self.EnumeratedValues = [] |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
153 |
self.DisableEvents = True |
27 | 154 |
self.TextSyntax = "ST" |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
155 |
self.CurrentAction = None |
121 | 156 |
self.TagName = tagname |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
157 |
self.Errors = [] |
249 | 158 |
self.Debug = debug |
159 |
self.InstancePath = instancepath |
|
546 | 160 |
self.ContextStack = [] |
161 |
self.CallStack = [] |
|
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
162 |
|
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
163 |
self.ParentWindow = window |
27 | 164 |
self.Controler = controler |
165 |
||
411 | 166 |
self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT| |
167 |
wx.stc.STC_MOD_BEFOREDELETE| |
|
168 |
wx.stc.STC_PERFORMED_USER) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
169 |
|
79 | 170 |
self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.OnStyleNeeded, id=ID_TEXTVIEWER) |
116
58b9b84e385f
Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents:
113
diff
changeset
|
171 |
if controler: |
73
f2529e34e4b7
Prevent problems showing ST code after failed build
etisserant
parents:
68
diff
changeset
|
172 |
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
f2529e34e4b7
Prevent problems showing ST code after failed build
etisserant
parents:
68
diff
changeset
|
173 |
self.Bind(wx.stc.EVT_STC_DO_DROP, self.OnDoDrop, id=ID_TEXTVIEWER) |
f2529e34e4b7
Prevent problems showing ST code after failed build
etisserant
parents:
68
diff
changeset
|
174 |
self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus) |
f2529e34e4b7
Prevent problems showing ST code after failed build
etisserant
parents:
68
diff
changeset
|
175 |
self.Bind(wx.stc.EVT_STC_MODIFIED, self.OnModification, id=ID_TEXTVIEWER) |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
176 |
|
121 | 177 |
def SetTagName(self, tagname): |
178 |
self.TagName = tagname |
|
179 |
||
180 |
def GetTagName(self): |
|
181 |
return self.TagName |
|
182 |
||
249 | 183 |
def GetInstancePath(self): |
184 |
return self.InstancePath |
|
185 |
||
121 | 186 |
def IsViewing(self, tagname): |
249 | 187 |
if self.Debug: |
188 |
return self.InstancePath == tagname |
|
189 |
else: |
|
190 |
return self.TagName == tagname |
|
121 | 191 |
|
407
0a324a874981
Adding support for integrating PLCOpenEditor in Beremiz frame
laurent
parents:
391
diff
changeset
|
192 |
def IsDebugging(self): |
0a324a874981
Adding support for integrating PLCOpenEditor in Beremiz frame
laurent
parents:
391
diff
changeset
|
193 |
return self.Debug |
0a324a874981
Adding support for integrating PLCOpenEditor in Beremiz frame
laurent
parents:
391
diff
changeset
|
194 |
|
121 | 195 |
def SetMode(self, mode): |
196 |
pass |
|
197 |
||
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
198 |
def OnModification(self, event): |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
199 |
if not self.DisableEvents: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
200 |
mod_type = event.GetModificationType() |
411 | 201 |
if mod_type&wx.stc.STC_MOD_BEFOREINSERT: |
202 |
if self.CurrentAction == None: |
|
203 |
self.StartBuffering() |
|
204 |
elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1: |
|
205 |
self.Controler.EndBuffering() |
|
206 |
self.StartBuffering() |
|
207 |
self.CurrentAction = ("Add", event.GetPosition()) |
|
208 |
wx.CallAfter(self.RefreshModel) |
|
209 |
elif mod_type&wx.stc.STC_MOD_BEFOREDELETE: |
|
210 |
if self.CurrentAction == None: |
|
211 |
self.StartBuffering() |
|
212 |
elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1: |
|
213 |
self.Controler.EndBuffering() |
|
214 |
self.StartBuffering() |
|
215 |
self.CurrentAction = ("Delete", event.GetPosition()) |
|
216 |
wx.CallAfter(self.RefreshModel) |
|
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
217 |
event.Skip() |
27 | 218 |
|
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
219 |
def OnDoDrop(self, event): |
50 | 220 |
try: |
221 |
values = eval(event.GetDragText()) |
|
222 |
except: |
|
223 |
values = event.GetDragText() |
|
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
224 |
if isinstance(values, tuple): |
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
225 |
message = None |
523 | 226 |
if values[1] in ["program", "debug"]: |
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
227 |
event.SetDragText("") |
523 | 228 |
elif values[1] in ["functionBlock", "function"]: |
229 |
blockname = values[2] |
|
230 |
if len(values) > 3: |
|
231 |
blockinputs = values[3] |
|
232 |
else: |
|
233 |
blockinputs = None |
|
234 |
if values[1] != "function": |
|
235 |
if blockname == "": |
|
236 |
dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE) |
|
237 |
if dialog.ShowModal() == wx.ID_OK: |
|
238 |
blockname = dialog.GetValue() |
|
239 |
else: |
|
240 |
return |
|
241 |
dialog.Destroy() |
|
242 |
if blockname.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
243 |
message = _("\"%s\" pou already exists!")%blockname |
|
244 |
elif blockname.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
245 |
message = _("\"%s\" element for this pou already exists!")%blockname |
|
246 |
else: |
|
247 |
self.Controler.AddEditedElementPouVar(self.TagName, values[0], blockname) |
|
248 |
self.ParentWindow.RefreshVariablePanel(self.TagName) |
|
249 |
self.RefreshVariableTree() |
|
250 |
blockinfo = self.Controler.GetBlockType(values[0], blockinputs, self.Debug) |
|
251 |
hint = ',\n '.join( |
|
252 |
[ " " + fctdecl[0]+" := (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["inputs"]] + |
|
253 |
[ " " + fctdecl[0]+" => (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["outputs"]]) |
|
254 |
event.SetDragText(blockname+"(\n "+hint+")") |
|
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
255 |
elif values[1] == "location": |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
256 |
pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug) |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
257 |
if len(values) > 2 and pou_type == "program": |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
258 |
var_name = values[3] |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
259 |
if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
448 | 260 |
message = _("\"%s\" pou already exists!")%var_name |
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
261 |
elif var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
448 | 262 |
message = _("\"%s\" element for this pou already exists!")%var_name |
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
263 |
else: |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
264 |
if values[2] is not None: |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
265 |
var_type = values[2] |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
266 |
else: |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
267 |
var_type = LOCATIONDATATYPES.get(values[0][2], ["BOOL"])[0] |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
268 |
self.Controler.AddEditedElementPouVar(self.TagName, var_type, var_name, values[0], values[4]) |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
269 |
self.ParentWindow.RefreshVariablePanel(self.TagName) |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
270 |
self.RefreshVariableTree() |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
271 |
event.SetDragText(var_name) |
121 | 272 |
else: |
273 |
event.SetDragText("") |
|
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
274 |
elif values[3] == self.TagName: |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
275 |
self.ResetBuffer() |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
276 |
event.SetDragText(values[0]) |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
277 |
wx.CallAfter(self.RefreshModel) |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
278 |
else: |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
279 |
message = _("Variable don't belong to this POU!") |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
280 |
if message is not None: |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
281 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
282 |
dialog.ShowModal() |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
283 |
dialog.Destroy() |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
284 |
event.SetDragText("") |
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
285 |
event.Skip() |
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
286 |
|
27 | 287 |
def SetTextSyntax(self, syntax): |
288 |
self.TextSyntax = syntax |
|
289 |
||
290 |
def SetKeywords(self, keywords): |
|
291 |
self.Keywords = [keyword.upper() for keyword in keywords] |
|
292 |
self.Colourise(0, -1) |
|
293 |
||
294 |
def RefreshJumpList(self): |
|
543 | 295 |
if self.TextSyntax != "IL": |
296 |
self.Jumps = [jump.upper() for jump in LABEL_MODEL.findall(self.GetText())] |
|
297 |
self.Colourise(0, -1) |
|
27 | 298 |
|
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
299 |
# Buffer the last model state |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
300 |
def RefreshBuffer(self): |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
301 |
self.Controler.BufferProject() |
116
58b9b84e385f
Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents:
113
diff
changeset
|
302 |
if self.ParentWindow: |
58b9b84e385f
Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents:
113
diff
changeset
|
303 |
self.ParentWindow.RefreshTitle() |
485
d5ebb8eac934
Bug on TextViewer fixed : FileMenu not refreshed when text modified
laurent
parents:
448
diff
changeset
|
304 |
self.ParentWindow.RefreshFileMenu() |
116
58b9b84e385f
Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents:
113
diff
changeset
|
305 |
self.ParentWindow.RefreshEditMenu() |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
306 |
|
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
307 |
def StartBuffering(self): |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
308 |
self.Controler.StartBuffering() |
116
58b9b84e385f
Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents:
113
diff
changeset
|
309 |
if self.ParentWindow: |
58b9b84e385f
Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents:
113
diff
changeset
|
310 |
self.ParentWindow.RefreshTitle() |
485
d5ebb8eac934
Bug on TextViewer fixed : FileMenu not refreshed when text modified
laurent
parents:
448
diff
changeset
|
311 |
self.ParentWindow.RefreshFileMenu() |
116
58b9b84e385f
Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents:
113
diff
changeset
|
312 |
self.ParentWindow.RefreshEditMenu() |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
313 |
|
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
314 |
def ResetBuffer(self): |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
315 |
if self.CurrentAction != None: |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
316 |
self.Controler.EndBuffering() |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
317 |
self.CurrentAction = None |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
318 |
|
27 | 319 |
def RefreshView(self): |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
320 |
self.ResetBuffer() |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
321 |
self.DisableEvents = True |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
322 |
old_cursor_pos = self.GetCurrentPos() |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
323 |
old_text = self.GetText() |
249 | 324 |
new_text = self.Controler.GetEditedElementText(self.TagName, self.Debug) |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
325 |
self.SetText(new_text) |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
326 |
new_cursor_pos = GetCursorPos(old_text, new_text) |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
327 |
if new_cursor_pos != None: |
80 | 328 |
self.GotoPos(new_cursor_pos) |
329 |
else: |
|
330 |
self.GotoPos(old_cursor_pos) |
|
331 |
self.ScrollToColumn(0) |
|
27 | 332 |
self.RefreshJumpList() |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
333 |
self.EmptyUndoBuffer() |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
334 |
self.DisableEvents = False |
121 | 335 |
|
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
336 |
self.RefreshVariableTree() |
121 | 337 |
|
543 | 338 |
self.TypeNames = [typename.upper() for typename in self.Controler.GetDataTypes(self.TagName, True, self.Debug)] |
339 |
self.EnumeratedValues = [value.upper() for value in self.Controler.GetEnumeratedDataValues()] |
|
340 |
||
341 |
self.Functions = {} |
|
249 | 342 |
for category in self.Controler.GetBlockTypes(self.TagName, self.Debug): |
121 | 343 |
for blocktype in category["list"]: |
546 | 344 |
blockname = blocktype["name"].upper() |
345 |
if blocktype["type"] == "function" and blockname not in self.Keywords and blockname not in self.Variables.keys(): |
|
346 |
interface = dict([(name, {}) for name, type, modifier in blocktype["inputs"] + blocktype["outputs"] if name != '']) |
|
347 |
if self.Functions.has_key(blockname): |
|
348 |
self.Functions[blockname]["interface"].update(interface) |
|
349 |
self.Functions[blockname]["extensible"] |= blocktype["extensible"] |
|
350 |
else: |
|
351 |
self.Functions[blockname] = {"interface": interface, |
|
352 |
"extensible": blocktype["extensible"]} |
|
121 | 353 |
self.Colourise(0, -1) |
297
e837b67cb184
Adding help menu for inserting complex variable in graphical viewer
lbessard
parents:
295
diff
changeset
|
354 |
|
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
355 |
def RefreshVariableTree(self): |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
356 |
words = self.TagName.split("::") |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
357 |
self.Variables = self.GenerateVariableTree([(variable["Name"], variable["Type"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)]) |
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
358 |
if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL": |
546 | 359 |
return_type = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug) |
360 |
if return_type is not None: |
|
361 |
var_tree, var_dimension = self.Controler.GenerateVarTree(return_type, self.Debug) |
|
362 |
self.Variables[words[-1].upper()] = self.GenerateVariableTree(var_tree) |
|
363 |
else: |
|
364 |
self.Variables[words[-1].upper()] = {} |
|
437
59e33406eea8
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
laurent
parents:
411
diff
changeset
|
365 |
|
297
e837b67cb184
Adding help menu for inserting complex variable in graphical viewer
lbessard
parents:
295
diff
changeset
|
366 |
def GenerateVariableTree(self, list): |
e837b67cb184
Adding help menu for inserting complex variable in graphical viewer
lbessard
parents:
295
diff
changeset
|
367 |
tree = {} |
299
15669fe26e56
Adding support for generating real array dimension in variable blocks
lbessard
parents:
297
diff
changeset
|
368 |
for var_name, var_type, (var_tree, var_dimension) in list: |
300 | 369 |
tree[var_name.upper()] = self.GenerateVariableTree(var_tree) |
297
e837b67cb184
Adding help menu for inserting complex variable in graphical viewer
lbessard
parents:
295
diff
changeset
|
370 |
return tree |
e837b67cb184
Adding help menu for inserting complex variable in graphical viewer
lbessard
parents:
295
diff
changeset
|
371 |
|
145 | 372 |
def RefreshScaling(self, refresh=True): |
373 |
pass |
|
374 |
||
546 | 375 |
def IsValidVariable(self, name, context): |
376 |
return context is not None and context.get(name, None) is not None |
|
377 |
||
378 |
def IsCallParameter(self, name, call): |
|
379 |
if call is not None: |
|
380 |
return (call["interface"].get(name.upper(), None) is not None or |
|
381 |
call["extensible"] and EXTENSIBLE_PARAMETER.match(name.upper()) is not None) |
|
543 | 382 |
return False |
383 |
||
27 | 384 |
def OnStyleNeeded(self, event): |
385 |
self.TextChanged = True |
|
386 |
line = self.LineFromPosition(self.GetEndStyled()) |
|
387 |
if line == 0: |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
388 |
start_pos = last_styled_pos = 0 |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
389 |
else: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
390 |
start_pos = last_styled_pos = self.GetLineEndPosition(line - 1) + 1 |
27 | 391 |
end_pos = event.GetPosition() |
392 |
self.StartStyling(start_pos, 0xff) |
|
393 |
||
546 | 394 |
current_context = self.Variables |
395 |
current_call = None |
|
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
396 |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
397 |
current_pos = last_styled_pos |
27 | 398 |
state = SPACE |
399 |
line = "" |
|
400 |
word = "" |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
401 |
while current_pos < end_pos: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
402 |
char = chr(self.GetCharAt(current_pos)).upper() |
27 | 403 |
line += char |
404 |
if char == NEWLINE: |
|
546 | 405 |
self.ContextStack = [] |
406 |
current_context = self.Variables |
|
27 | 407 |
if state == COMMENT: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
408 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_COMMENT) |
27 | 409 |
elif state == NUMBER: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
410 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
27 | 411 |
elif state == WORD: |
543 | 412 |
if word in self.Keywords or word in self.TypeNames: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
413 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
546 | 414 |
elif self.IsValidVariable(word, current_context): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
415 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
546 | 416 |
elif self.IsCallParameter(word, current_call): |
417 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
27 | 418 |
elif word in self.Functions: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
419 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
543 | 420 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
421 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
122
diff
changeset
|
422 |
elif word in self.EnumeratedValues: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
423 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
27 | 424 |
else: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
425 |
self.SetStyling(current_pos - last_styled_pos, 31) |
546 | 426 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
427 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
428 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
429 |
self.StartStyling(current_pos, 0xff) |
27 | 430 |
else: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
431 |
self.SetStyling(current_pos - last_styled_pos, 31) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
432 |
last_styled_pos = current_pos |
27 | 433 |
state = SPACE |
434 |
line = "" |
|
435 |
elif line.endswith("(*") and state != COMMENT: |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
436 |
self.SetStyling(current_pos - last_styled_pos - 1, 31) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
437 |
last_styled_pos = current_pos |
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
438 |
if state == WORD: |
546 | 439 |
current_context = self.Variables |
27 | 440 |
state = COMMENT |
441 |
elif state == COMMENT: |
|
442 |
if line.endswith("*)"): |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
443 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
444 |
last_styled_pos = current_pos + 1 |
27 | 445 |
state = SPACE |
543 | 446 |
elif (line.endswith("'") or line.endswith('"')) and state not in [COMMENT, STRING, WSTRING]: |
447 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
448 |
last_styled_pos = current_pos |
|
449 |
if state == WORD: |
|
546 | 450 |
current_context = self.Variables |
543 | 451 |
if line.endswith("'"): |
452 |
state = STRING |
|
453 |
else: |
|
454 |
state = WSTRING |
|
455 |
elif state == STRING: |
|
456 |
if line.endswith("'") and not line.endswith("$'"): |
|
457 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
458 |
last_styled_pos = current_pos + 1 |
|
459 |
state = SPACE |
|
460 |
elif state == WSTRING: |
|
461 |
if line.endswith('"') and not line.endswith('$"'): |
|
462 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
463 |
last_styled_pos = current_pos + 1 |
|
464 |
state = SPACE |
|
27 | 465 |
elif char in LETTERS: |
466 |
if state == NUMBER: |
|
467 |
word = "#" |
|
468 |
state = WORD |
|
469 |
elif state == SPACE: |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
470 |
self.SetStyling(current_pos - last_styled_pos, 31) |
27 | 471 |
word = char |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
472 |
last_styled_pos = current_pos |
27 | 473 |
state = WORD |
474 |
else: |
|
475 |
word += char |
|
476 |
elif char in NUMBERS or char == '.' and state != WORD: |
|
477 |
if state == SPACE: |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
478 |
self.SetStyling(current_pos - last_styled_pos, 31) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
479 |
last_styled_pos = current_pos |
27 | 480 |
state = NUMBER |
481 |
if state == WORD and char != '.': |
|
482 |
word += char |
|
543 | 483 |
elif char == '(' and state == SPACE: |
546 | 484 |
self.CallStack.append(current_call) |
485 |
current_call = None |
|
27 | 486 |
else: |
487 |
if state == WORD: |
|
543 | 488 |
if word in self.Keywords or word in self.TypeNames: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
489 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
546 | 490 |
elif self.IsValidVariable(word, current_context): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
491 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
546 | 492 |
elif self.IsCallParameter(word, current_call): |
493 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
27 | 494 |
elif word in self.Functions: |
543 | 495 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
496 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
497 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
122
diff
changeset
|
498 |
elif word in self.EnumeratedValues: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
499 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
27 | 500 |
else: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
501 |
self.SetStyling(current_pos - last_styled_pos, 31) |
546 | 502 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
503 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
504 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
505 |
self.StartStyling(current_pos, 0xff) |
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
506 |
if char == '.': |
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
507 |
if word != "]": |
546 | 508 |
if current_context is not None: |
509 |
current_context = current_context.get(word, None) |
|
510 |
else: |
|
511 |
current_context = None |
|
543 | 512 |
elif char == '(': |
546 | 513 |
self.CallStack.append(current_call) |
514 |
current_call = self.Functions.get(word, None) |
|
515 |
if current_call is None and self.IsValidVariable(word, current_context): |
|
516 |
current_call = {"interface": current_context.get(word, {}), |
|
517 |
"extensible": False} |
|
518 |
current_context = self.Variables |
|
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
519 |
else: |
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
520 |
if char == '[': |
546 | 521 |
self.ContextStack.append(current_context.get(word, None)) |
522 |
current_context = self.Variables |
|
543 | 523 |
|
27 | 524 |
word = "" |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
525 |
last_styled_pos = current_pos |
27 | 526 |
state = SPACE |
527 |
elif state == NUMBER: |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
528 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
529 |
last_styled_pos = current_pos |
27 | 530 |
state = SPACE |
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
531 |
if char == ']': |
546 | 532 |
if len(self.ContextStack) > 0: |
533 |
current_context = self.ContextStack.pop() |
|
534 |
else: |
|
535 |
current_context = self.Variables |
|
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
536 |
word = char |
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
537 |
state = WORD |
543 | 538 |
elif char == ')': |
546 | 539 |
current_context = self.Variables |
540 |
if len(self.CallStack) > 0: |
|
541 |
current_call = self.CallStack.pop() |
|
542 |
else: |
|
543 |
current_call = None |
|
544 |
word = char |
|
545 |
state = WORD |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
546 |
current_pos += 1 |
27 | 547 |
if state == COMMENT: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
548 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
27 | 549 |
elif state == NUMBER: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
550 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
27 | 551 |
elif state == WORD: |
543 | 552 |
if word in self.Keywords or word in self.TypeNames: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
553 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
546 | 554 |
elif self.IsValidVariable(word, current_context): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
555 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
546 | 556 |
elif self.IsCallParameter(word, current_call): |
557 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
543 | 558 |
elif self.TextSyntax == "IL" and word in self.Functions: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
559 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
27 | 560 |
elif word in self.Jumps: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
561 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
125
394d9f168258
Adding support for execution order in PLCGenerator
lbessard
parents:
122
diff
changeset
|
562 |
elif word in self.EnumeratedValues: |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
563 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
564 |
else: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
565 |
self.SetStyling(current_pos - last_styled_pos, 31) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
566 |
else: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
567 |
self.SetStyling(current_pos - start_pos, 31) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
568 |
self.ShowErrors(start_pos, end_pos) |
27 | 569 |
event.Skip() |
570 |
||
571 |
def Cut(self): |
|
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
572 |
self.ResetBuffer() |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
573 |
self.CmdKeyExecute(wx.stc.STC_CMD_CUT) |
136 | 574 |
self.RefreshModel() |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
575 |
self.RefreshBuffer() |
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
576 |
|
27 | 577 |
def Copy(self): |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
578 |
self.CmdKeyExecute(wx.stc.STC_CMD_COPY) |
27 | 579 |
|
580 |
def Paste(self): |
|
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
581 |
self.ResetBuffer() |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
582 |
self.CmdKeyExecute(wx.stc.STC_CMD_PASTE) |
136 | 583 |
self.RefreshModel() |
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
53
diff
changeset
|
584 |
self.RefreshBuffer() |
27 | 585 |
|
586 |
def RefreshModel(self): |
|
136 | 587 |
self.RefreshJumpList() |
588 |
self.Controler.SetEditedElementText(self.TagName, self.GetText()) |
|
27 | 589 |
|
590 |
def OnKeyDown(self, event): |
|
591 |
if self.CallTipActive(): |
|
592 |
self.CallTipCancel() |
|
121 | 593 |
key = event.GetKeyCode() |
27 | 594 |
|
595 |
# Code completion |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
596 |
if key == wx.WXK_SPACE and event.ControlDown(): |
27 | 597 |
|
598 |
line = self.GetCurrentLine() |
|
599 |
if line == 0: |
|
600 |
start_pos = 0 |
|
601 |
else: |
|
602 |
start_pos = self.GetLineEndPosition(line - 1) + 1 |
|
603 |
end_pos = self.GetCurrentPos() |
|
604 |
||
605 |
lineText = self.GetTextRange(start_pos, end_pos).replace("\t", " ") |
|
606 |
words = lineText.split(" ") |
|
607 |
words = [word for i, word in enumerate(words) if word != '' or i == len(words) - 1] |
|
608 |
||
609 |
kw = [] |
|
610 |
||
611 |
if self.TextSyntax == "IL": |
|
612 |
if len(words) == 1: |
|
613 |
kw = self.Keywords |
|
614 |
elif len(words) == 2: |
|
615 |
if words[0].upper() in ["CAL", "CALC", "CALNC"]: |
|
616 |
kw = self.Functions |
|
617 |
elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]: |
|
618 |
kw = self.Jumps |
|
619 |
else: |
|
295
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
620 |
kw = self.Variables.keys() |
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
621 |
else: |
c6ef6d92ce16
Adding support for editing and using struct data types
lbessard
parents:
249
diff
changeset
|
622 |
kw = self.Keywords + self.Variables.keys() + self.Functions |
27 | 623 |
if len(kw) > 0: |
312 | 624 |
if len(words[-1]) > 0: |
625 |
kw = [keyword for keyword in kw if keyword.startswith(words[-1])] |
|
27 | 626 |
kw.sort() |
627 |
self.AutoCompSetIgnoreCase(True) |
|
628 |
self.AutoCompShow(len(words[-1]), " ".join(kw)) |
|
629 |
else: |
|
630 |
event.Skip() |
|
631 |
||
632 |
def OnKillFocus(self, event): |
|
633 |
self.AutoCompCancel() |
|
634 |
event.Skip() |
|
635 |
||
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
636 |
#------------------------------------------------------------------------------- |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
637 |
# Errors showing functions |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
638 |
#------------------------------------------------------------------------------- |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
639 |
|
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
640 |
def ClearErrors(self): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
641 |
self.Errors = [] |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
642 |
self.RefreshView() |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
643 |
|
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
644 |
def AddShownError(self, infos, start, end): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
645 |
if infos[0] == "body": |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
646 |
self.Errors.append((infos[1], start, end)) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
647 |
|
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
648 |
def ShowErrors(self, start_pos, end_pos): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
649 |
for indent, start, end in self.Errors: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
650 |
if start[0] == 0: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
651 |
error_start_pos = start[1] - indent |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
652 |
else: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
653 |
error_start_pos = self.GetLineEndPosition(start[0] - 1) + start[1] - indent + 1 |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
654 |
if end[0] == 0: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
655 |
error_end_pos = end[1] - indent + 1 |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
656 |
else: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
657 |
error_end_pos = self.GetLineEndPosition(end[0] - 1) + end[1] - indent + 2 |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
658 |
if start_pos <= error_start_pos <= end_pos or start_pos <= error_end_pos <= end_pos: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
659 |
self.StartStyling(error_start_pos, 0xff) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
145
diff
changeset
|
660 |
self.SetStyling(error_end_pos - error_start_pos, STC_PLC_ERROR) |