author | lbessard |
Sat, 21 Jul 2007 01:20:00 +0200 | |
changeset 53 | 4988262d03e3 |
parent 50 | 4610aafc884e |
child 56 | 7187e1c00975 |
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 |
# |
|
7 |
#Copyright (C): Edouard TISSERANT and Laurent BESSARD |
|
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 |
|
19 |
#Lesser General Public License for more details. |
|
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 |
from wxPython.wx import * |
|
26 |
from wxPython.stc import * |
|
27 |
import wx |
|
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 |
||
43 |
[wxSTC_PLC_WORD, wxSTC_PLC_COMMENT, wxSTC_PLC_NUMBER, wxSTC_PLC_VARIABLE, |
|
44 |
wxSTC_PLC_FUNCTION, wxSTC_PLC_JUMP] = range(6) |
|
45 |
[SPACE, WORD, NUMBER, COMMENT] = range(4) |
|
46 |
||
47 |
[wxID_TEXTVIEWER, |
|
48 |
] = [wx.NewId() for _init_ctrls in range(1)] |
|
49 |
||
50 |
if wx.Platform == '__WXMSW__': |
|
51 |
faces = { 'times': 'Times New Roman', |
|
52 |
'mono' : 'Courier New', |
|
53 |
'helv' : 'Arial', |
|
54 |
'other': 'Comic Sans MS', |
|
55 |
'size' : 10, |
|
56 |
} |
|
57 |
else: |
|
58 |
faces = { 'times': 'Times', |
|
59 |
'mono' : 'Courier', |
|
60 |
'helv' : 'Helvetica', |
|
61 |
'other': 'new century schoolbook', |
|
62 |
'size' : 12, |
|
63 |
} |
|
64 |
re_texts = {} |
|
65 |
re_texts["letter"] = "[A-Za-z]" |
|
66 |
re_texts["digit"] = "[0-9]" |
|
67 |
re_texts["identifier"] = "((?:%(letter)s|(?:_(?:%(letter)s|%(digit)s)))(?:_?(?:%(letter)s|%(digit)s))*)"%re_texts |
|
68 |
IDENTIFIER_MODEL = re.compile(re_texts["identifier"]) |
|
69 |
LABEL_MODEL = re.compile("[ \t\n]%(identifier)s:[ \t\n]"%re_texts) |
|
70 |
||
71 |
class TextViewer(wxStyledTextCtrl): |
|
72 |
||
73 |
def __init__(self, parent, window, controler): |
|
74 |
wxStyledTextCtrl.__init__(self, parent, wxID_TEXTVIEWER, style=0) |
|
75 |
||
76 |
self.CmdKeyAssign(ord('+'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMIN) |
|
77 |
self.CmdKeyAssign(ord('-'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMOUT) |
|
78 |
||
79 |
self.SetViewWhiteSpace(False) |
|
80 |
||
81 |
self.SetLexer(wxSTC_LEX_CONTAINER) |
|
82 |
||
83 |
# Global default styles for all languages |
|
84 |
self.StyleSetSpec(wxSTC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces) |
|
85 |
self.StyleClearAll() # Reset all to be like the default |
|
86 |
||
87 |
self.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "back:#C0C0C0,size:%(size)d" % faces) |
|
88 |
self.SetSelBackground(1, "#E0E0E0") |
|
89 |
||
90 |
# Highlighting styles |
|
91 |
self.StyleSetSpec(wxSTC_PLC_WORD, "fore:#00007F,bold,size:%(size)d" % faces) |
|
92 |
self.StyleSetSpec(wxSTC_PLC_VARIABLE, "fore:#7F0000,size:%(size)d" % faces) |
|
93 |
self.StyleSetSpec(wxSTC_PLC_FUNCTION, "fore:#7F7F00,size:%(size)d" % faces) |
|
94 |
self.StyleSetSpec(wxSTC_PLC_COMMENT, "fore:#7F7F7F,size:%(size)d" % faces) |
|
95 |
self.StyleSetSpec(wxSTC_PLC_NUMBER, "fore:#007F7F,size:%(size)d" % faces) |
|
96 |
self.StyleSetSpec(wxSTC_PLC_JUMP, "fore:#007F00,size:%(size)d" % faces) |
|
97 |
||
98 |
# Indicators styles |
|
99 |
self.IndicatorSetStyle(0, wxSTC_INDIC_SQUIGGLE) |
|
100 |
self.IndicatorSetForeground(0, wxRED) |
|
101 |
||
102 |
# Line numbers in the margin |
|
103 |
self.SetMarginType(1, wxSTC_MARGIN_NUMBER) |
|
104 |
self.SetMarginWidth(1, 50) |
|
105 |
||
106 |
# Indentation size |
|
107 |
self.SetTabWidth(2) |
|
108 |
self.SetUseTabs(0) |
|
109 |
||
110 |
self.Keywords = [] |
|
111 |
self.Variables = [] |
|
112 |
self.Functions = [] |
|
113 |
self.Jumps = [] |
|
114 |
self.TextChanged = False |
|
115 |
self.TextSyntax = "ST" |
|
116 |
||
117 |
self.Controler = controler |
|
118 |
||
119 |
EVT_KEY_DOWN(self, self.OnKeyDown) |
|
120 |
EVT_STC_STYLENEEDED(self, wxID_TEXTVIEWER, self.OnStyleNeeded) |
|
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
121 |
EVT_STC_DO_DROP(self, wxID_TEXTVIEWER, self.OnDoDrop) |
27 | 122 |
EVT_KILL_FOCUS(self, self.OnKillFocus) |
123 |
||
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
124 |
def OnDoDrop(self, event): |
50 | 125 |
try: |
126 |
values = eval(event.GetDragText()) |
|
127 |
except: |
|
128 |
values = event.GetDragText() |
|
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
129 |
if isinstance(values, tuple): |
53 | 130 |
if values[1] in ["functionBlock", "program", "location"]: |
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
131 |
event.SetDragText("") |
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
132 |
else: |
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
133 |
event.SetDragText(values[0]) |
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
134 |
event.Skip() |
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
27
diff
changeset
|
135 |
|
27 | 136 |
def SetTextSyntax(self, syntax): |
137 |
self.TextSyntax = syntax |
|
138 |
||
139 |
def SetKeywords(self, keywords): |
|
140 |
self.Keywords = [keyword.upper() for keyword in keywords] |
|
141 |
self.Colourise(0, -1) |
|
142 |
||
143 |
def SetVariables(self, variables): |
|
144 |
self.Variables = [variable.upper() for variable in variables] |
|
145 |
self.Colourise(0, -1) |
|
146 |
||
147 |
def SetFunctions(self, blocktypes): |
|
148 |
self.Functions = [] |
|
149 |
for category in blocktypes: |
|
150 |
for blocktype in category["list"]: |
|
151 |
if blocktype["name"] not in self.Keywords and blocktype["name"] not in self.Variables: |
|
152 |
self.Functions.append(blocktype["name"].upper()) |
|
153 |
self.Colourise(0, -1) |
|
154 |
||
155 |
def RefreshJumpList(self): |
|
156 |
self.Jumps = [jump.upper() for jump in LABEL_MODEL.findall(self.GetText())] |
|
157 |
self.Colourise(0, -1) |
|
158 |
||
159 |
def RefreshView(self): |
|
160 |
self.SetText(self.Controler.GetCurrentElementEditingText()) |
|
161 |
self.RefreshJumpList() |
|
162 |
||
163 |
def OnStyleNeeded(self, event): |
|
164 |
self.TextChanged = True |
|
165 |
line = self.LineFromPosition(self.GetEndStyled()) |
|
166 |
if line == 0: |
|
167 |
start_pos = 0 |
|
168 |
else: |
|
169 |
start_pos = self.GetLineEndPosition(line - 1) + 1 |
|
170 |
end_pos = event.GetPosition() |
|
171 |
self.StartStyling(start_pos, 0xff) |
|
172 |
||
173 |
i = start_pos |
|
174 |
state = SPACE |
|
175 |
line = "" |
|
176 |
word = "" |
|
177 |
while i < end_pos: |
|
178 |
char = chr(self.GetCharAt(i)).upper() |
|
179 |
line += char |
|
180 |
if char == NEWLINE: |
|
181 |
if state == COMMENT: |
|
182 |
self.SetStyling(i - start_pos + 1, wxSTC_PLC_COMMENT) |
|
183 |
elif state == NUMBER: |
|
184 |
self.SetStyling(i - start_pos, wxSTC_PLC_NUMBER) |
|
185 |
elif state == WORD: |
|
186 |
if word in self.Keywords: |
|
187 |
self.SetStyling(i - start_pos, wxSTC_PLC_WORD) |
|
188 |
elif word in self.Variables: |
|
189 |
self.SetStyling(i - start_pos, wxSTC_PLC_VARIABLE) |
|
190 |
elif word in self.Functions: |
|
191 |
self.SetStyling(i - start_pos, wxSTC_PLC_FUNCTION) |
|
192 |
elif word in self.Jumps: |
|
193 |
self.SetStyling(i - start_pos, wxSTC_PLC_JUMP) |
|
194 |
else: |
|
195 |
self.SetStyling(i - start_pos, 31) |
|
196 |
if self.GetCurrentPos() < start_pos or self.GetCurrentPos() > i: |
|
197 |
self.StartStyling(start_pos, wxSTC_INDICS_MASK) |
|
198 |
self.SetStyling(i - start_pos, wxSTC_INDIC0_MASK) |
|
199 |
self.StartStyling(i, 0xff) |
|
200 |
else: |
|
201 |
self.SetStyling(i - start_pos, 31) |
|
202 |
start_pos = i |
|
203 |
state = SPACE |
|
204 |
line = "" |
|
205 |
elif line.endswith("(*") and state != COMMENT: |
|
206 |
self.SetStyling(i - start_pos - 1, 31) |
|
207 |
start_pos = i |
|
208 |
state = COMMENT |
|
209 |
elif state == COMMENT: |
|
210 |
if line.endswith("*)"): |
|
211 |
self.SetStyling(i - start_pos + 2, wxSTC_PLC_COMMENT) |
|
212 |
start_pos = i + 1 |
|
213 |
state = SPACE |
|
214 |
elif char in LETTERS: |
|
215 |
if state == NUMBER: |
|
216 |
word = "#" |
|
217 |
state = WORD |
|
218 |
elif state == SPACE: |
|
219 |
self.SetStyling(i - start_pos, 31) |
|
220 |
word = char |
|
221 |
start_pos = i |
|
222 |
state = WORD |
|
223 |
else: |
|
224 |
word += char |
|
225 |
elif char in NUMBERS or char == '.' and state != WORD: |
|
226 |
if state == SPACE: |
|
227 |
self.SetStyling(i - start_pos, 31) |
|
228 |
start_pos = i |
|
229 |
state = NUMBER |
|
230 |
if state == WORD and char != '.': |
|
231 |
word += char |
|
232 |
else: |
|
233 |
if state == WORD: |
|
234 |
if word in self.Keywords: |
|
235 |
self.SetStyling(i - start_pos, wxSTC_PLC_WORD) |
|
236 |
elif word in self.Variables: |
|
237 |
self.SetStyling(i - start_pos, wxSTC_PLC_VARIABLE) |
|
238 |
elif word in self.Functions: |
|
239 |
self.SetStyling(i - start_pos, wxSTC_PLC_FUNCTION) |
|
240 |
elif word in self.Jumps: |
|
241 |
self.SetStyling(i - start_pos, wxSTC_PLC_JUMP) |
|
242 |
else: |
|
243 |
self.SetStyling(i - start_pos, 31) |
|
244 |
if self.GetCurrentPos() < start_pos or self.GetCurrentPos() > i: |
|
245 |
self.StartStyling(start_pos, wxSTC_INDICS_MASK) |
|
246 |
self.SetStyling(i - start_pos, wxSTC_INDIC0_MASK) |
|
247 |
self.StartStyling(i, 0xff) |
|
248 |
word = "" |
|
249 |
start_pos = i |
|
250 |
state = SPACE |
|
251 |
elif state == NUMBER: |
|
252 |
self.SetStyling(i - start_pos, wxSTC_PLC_NUMBER) |
|
253 |
start_pos = i |
|
254 |
state = SPACE |
|
255 |
i += 1 |
|
256 |
if state == COMMENT: |
|
257 |
self.SetStyling(i - start_pos + 2, wxSTC_PLC_COMMENT) |
|
258 |
elif state == NUMBER: |
|
259 |
self.SetStyling(i - start_pos, wxSTC_PLC_NUMBER) |
|
260 |
elif state == WORD: |
|
261 |
if word in self.Keywords: |
|
262 |
self.SetStyling(i - start_pos, wxSTC_PLC_WORD) |
|
263 |
elif word in self.Variables: |
|
264 |
self.SetStyling(i - start_pos, wxSTC_PLC_VARIABLE) |
|
265 |
elif word in self.Functions: |
|
266 |
self.SetStyling(i - start_pos, wxSTC_PLC_FUNCTION) |
|
267 |
elif word in self.Jumps: |
|
268 |
self.SetStyling(i - start_pos, wxSTC_PLC_JUMP) |
|
269 |
else: |
|
270 |
self.SetStyling(i - start_pos, 31) |
|
271 |
else: |
|
272 |
self.SetStyling(i - start_pos, 31) |
|
273 |
event.Skip() |
|
274 |
||
275 |
def Cut(self): |
|
276 |
self.CmdKeyExecute(wxSTC_CMD_CUT) |
|
277 |
||
278 |
def Copy(self): |
|
279 |
self.CmdKeyExecute(wxSTC_CMD_COPY) |
|
280 |
||
281 |
def Paste(self): |
|
282 |
self.CmdKeyExecute(wxSTC_CMD_PASTE) |
|
283 |
||
284 |
def RefreshModel(self): |
|
285 |
if self.TextChanged: |
|
286 |
self.RefreshJumpList() |
|
287 |
self.Controler.SetCurrentElementEditingText(self.GetText()) |
|
288 |
||
289 |
def OnKeyDown(self, event): |
|
290 |
if self.CallTipActive(): |
|
291 |
self.CallTipCancel() |
|
292 |
key = event.KeyCode() |
|
293 |
||
294 |
# Code completion |
|
295 |
if key == WXK_SPACE and event.ControlDown(): |
|
296 |
||
297 |
line = self.GetCurrentLine() |
|
298 |
if line == 0: |
|
299 |
start_pos = 0 |
|
300 |
else: |
|
301 |
start_pos = self.GetLineEndPosition(line - 1) + 1 |
|
302 |
end_pos = self.GetCurrentPos() |
|
303 |
||
304 |
lineText = self.GetTextRange(start_pos, end_pos).replace("\t", " ") |
|
305 |
words = lineText.split(" ") |
|
306 |
words = [word for i, word in enumerate(words) if word != '' or i == len(words) - 1] |
|
307 |
||
308 |
kw = [] |
|
309 |
||
310 |
if self.TextSyntax == "IL": |
|
311 |
if len(words) == 1: |
|
312 |
kw = self.Keywords |
|
313 |
elif len(words) == 2: |
|
314 |
if words[0].upper() in ["CAL", "CALC", "CALNC"]: |
|
315 |
kw = self.Functions |
|
316 |
elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]: |
|
317 |
kw = self.Jumps |
|
318 |
else: |
|
319 |
kw = self.Variables |
|
320 |
else: |
|
321 |
kw = self.Keywords + self.Variables + self.Functions |
|
322 |
if len(kw) > 0: |
|
323 |
kw.sort() |
|
324 |
self.AutoCompSetIgnoreCase(True) |
|
325 |
self.AutoCompShow(len(words[-1]), " ".join(kw)) |
|
326 |
else: |
|
327 |
self.TextChanged = False |
|
328 |
wxCallAfter(self.RefreshModel) |
|
329 |
event.Skip() |
|
330 |
||
331 |
def OnKillFocus(self, event): |
|
332 |
self.AutoCompCancel() |
|
333 |
event.Skip() |
|
334 |