author | Laurent Bessard |
Thu, 10 Oct 2013 17:04:23 +0200 | |
changeset 1355 | 9183fb765f16 |
parent 1347 | 533741e5075c |
child 1406 | 82db84fe88ea |
permissions | -rw-r--r-- |
814 | 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) 2007: 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 |
#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 |
import re |
|
26 |
from types import * |
|
27 |
||
28 |
import wx |
|
29 |
import wx.stc |
|
30 |
||
31 |
from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD |
|
32 |
from plcopen.structures import ST_BLOCK_START_KEYWORDS, ST_BLOCK_END_KEYWORDS, IEC_BLOCK_START_KEYWORDS, IEC_BLOCK_END_KEYWORDS, LOCATIONDATATYPES |
|
33 |
from EditorPanel import EditorPanel |
|
1262
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
34 |
from controls.CustomStyledTextCtrl import CustomStyledTextCtrl, faces, GetCursorPos, NAVIGATION_KEYS |
814 | 35 |
|
36 |
#------------------------------------------------------------------------------- |
|
37 |
# Textual programs Viewer class |
|
38 |
#------------------------------------------------------------------------------- |
|
39 |
||
40 |
||
41 |
NEWLINE = "\n" |
|
42 |
NUMBERS = [str(i) for i in xrange(10)] |
|
43 |
LETTERS = ['_'] |
|
44 |
for i in xrange(26): |
|
45 |
LETTERS.append(chr(ord('a') + i)) |
|
46 |
LETTERS.append(chr(ord('A') + i)) |
|
47 |
||
48 |
[STC_PLC_WORD, STC_PLC_COMMENT, STC_PLC_NUMBER, STC_PLC_STRING, |
|
49 |
STC_PLC_VARIABLE, STC_PLC_PARAMETER, STC_PLC_FUNCTION, STC_PLC_JUMP, |
|
50 |
STC_PLC_ERROR, STC_PLC_SEARCH_RESULT] = range(10) |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
51 |
[SPACE, WORD, NUMBER, STRING, WSTRING, COMMENT, PRAGMA, DPRAGMA] = range(8) |
814 | 52 |
|
53 |
[ID_TEXTVIEWER, ID_TEXTVIEWERTEXTCTRL, |
|
54 |
] = [wx.NewId() for _init_ctrls in range(2)] |
|
55 |
||
56 |
re_texts = {} |
|
57 |
re_texts["letter"] = "[A-Za-z]" |
|
58 |
re_texts["digit"] = "[0-9]" |
|
59 |
re_texts["identifier"] = "((?:%(letter)s|(?:_(?:%(letter)s|%(digit)s)))(?:_?(?:%(letter)s|%(digit)s))*)"%re_texts |
|
60 |
IDENTIFIER_MODEL = re.compile(re_texts["identifier"]) |
|
61 |
LABEL_MODEL = re.compile("[ \t\n]%(identifier)s:[ \t\n]"%re_texts) |
|
62 |
EXTENSIBLE_PARAMETER = re.compile("IN[1-9][0-9]*$") |
|
63 |
||
64 |
HIGHLIGHT_TYPES = { |
|
65 |
ERROR_HIGHLIGHT: STC_PLC_ERROR, |
|
66 |
SEARCH_RESULT_HIGHLIGHT: STC_PLC_SEARCH_RESULT, |
|
67 |
} |
|
68 |
||
69 |
def LineStartswith(line, symbols): |
|
70 |
return reduce(lambda x, y: x or y, map(lambda x: line.startswith(x), symbols), False) |
|
71 |
||
72 |
class TextViewer(EditorPanel): |
|
73 |
||
74 |
ID = ID_TEXTVIEWER |
|
75 |
||
76 |
if wx.VERSION < (2, 6, 0): |
|
77 |
def Bind(self, event, function, id = None): |
|
78 |
if id is not None: |
|
79 |
event(self, id, function) |
|
80 |
else: |
|
81 |
event(self, function) |
|
82 |
||
83 |
def _init_Editor(self, prnt): |
|
1091
5f612651d227
Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
1065
diff
changeset
|
84 |
self.Editor = CustomStyledTextCtrl(id=ID_TEXTVIEWERTEXTCTRL, |
814 | 85 |
parent=prnt, name="TextViewer", size=wx.Size(0, 0), style=0) |
86 |
self.Editor.ParentWindow = self |
|
87 |
||
88 |
self.Editor.CmdKeyAssign(ord('+'), wx.stc.STC_SCMOD_CTRL, wx.stc.STC_CMD_ZOOMIN) |
|
89 |
self.Editor.CmdKeyAssign(ord('-'), wx.stc.STC_SCMOD_CTRL, wx.stc.STC_CMD_ZOOMOUT) |
|
90 |
||
91 |
self.Editor.SetViewWhiteSpace(False) |
|
92 |
||
93 |
self.Editor.SetLexer(wx.stc.STC_LEX_CONTAINER) |
|
94 |
||
95 |
# Global default styles for all languages |
|
96 |
self.Editor.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces) |
|
97 |
self.Editor.StyleClearAll() # Reset all to be like the default |
|
98 |
||
99 |
self.Editor.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,size:%(size)d" % faces) |
|
100 |
self.Editor.SetSelBackground(1, "#E0E0E0") |
|
101 |
||
102 |
# Highlighting styles |
|
103 |
self.Editor.StyleSetSpec(STC_PLC_WORD, "fore:#00007F,bold,size:%(size)d" % faces) |
|
104 |
self.Editor.StyleSetSpec(STC_PLC_VARIABLE, "fore:#7F0000,size:%(size)d" % faces) |
|
105 |
self.Editor.StyleSetSpec(STC_PLC_PARAMETER, "fore:#7F007F,size:%(size)d" % faces) |
|
106 |
self.Editor.StyleSetSpec(STC_PLC_FUNCTION, "fore:#7F7F00,size:%(size)d" % faces) |
|
107 |
self.Editor.StyleSetSpec(STC_PLC_COMMENT, "fore:#7F7F7F,size:%(size)d" % faces) |
|
108 |
self.Editor.StyleSetSpec(STC_PLC_NUMBER, "fore:#007F7F,size:%(size)d" % faces) |
|
109 |
self.Editor.StyleSetSpec(STC_PLC_STRING, "fore:#007F00,size:%(size)d" % faces) |
|
110 |
self.Editor.StyleSetSpec(STC_PLC_JUMP, "fore:#FF7FFF,size:%(size)d" % faces) |
|
111 |
self.Editor.StyleSetSpec(STC_PLC_ERROR, "fore:#FF0000,back:#FFFF00,size:%(size)d" % faces) |
|
112 |
self.Editor.StyleSetSpec(STC_PLC_SEARCH_RESULT, "fore:#FFFFFF,back:#FFA500,size:%(size)d" % faces) |
|
113 |
||
114 |
# Indicators styles |
|
115 |
self.Editor.IndicatorSetStyle(0, wx.stc.STC_INDIC_SQUIGGLE) |
|
116 |
if self.ParentWindow is not None and self.Controler is not None: |
|
117 |
self.Editor.IndicatorSetForeground(0, wx.RED) |
|
118 |
else: |
|
119 |
self.Editor.IndicatorSetForeground(0, wx.WHITE) |
|
120 |
||
121 |
# Line numbers in the margin |
|
122 |
self.Editor.SetMarginType(1, wx.stc.STC_MARGIN_NUMBER) |
|
123 |
self.Editor.SetMarginWidth(1, 50) |
|
124 |
||
125 |
# Folding |
|
126 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEROPEN, wx.stc.STC_MARK_BOXMINUS, "white", "#808080") |
|
127 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDER, wx.stc.STC_MARK_BOXPLUS, "white", "#808080") |
|
128 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERSUB, wx.stc.STC_MARK_VLINE, "white", "#808080") |
|
129 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERTAIL, wx.stc.STC_MARK_LCORNER, "white", "#808080") |
|
130 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEREND, wx.stc.STC_MARK_BOXPLUSCONNECTED, "white", "#808080") |
|
131 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.stc.STC_MARK_BOXMINUSCONNECTED, "white", "#808080") |
|
132 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERMIDTAIL, wx.stc.STC_MARK_TCORNER, "white", "#808080") |
|
133 |
||
134 |
# Indentation size |
|
135 |
self.Editor.SetTabWidth(2) |
|
136 |
self.Editor.SetUseTabs(0) |
|
137 |
||
138 |
self.Editor.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT| |
|
139 |
wx.stc.STC_MOD_BEFOREDELETE| |
|
140 |
wx.stc.STC_PERFORMED_USER) |
|
141 |
||
142 |
self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.OnStyleNeeded, id=ID_TEXTVIEWERTEXTCTRL) |
|
143 |
self.Editor.Bind(wx.stc.EVT_STC_MARGINCLICK, self.OnMarginClick) |
|
1126
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
144 |
self.Editor.Bind(wx.stc.EVT_STC_UPDATEUI, self.OnUpdateUI) |
814 | 145 |
self.Editor.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
146 |
if self.Controler is not None: |
|
147 |
self.Editor.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus) |
|
148 |
self.Bind(wx.stc.EVT_STC_DO_DROP, self.OnDoDrop, id=ID_TEXTVIEWERTEXTCTRL) |
|
149 |
self.Bind(wx.stc.EVT_STC_MODIFIED, self.OnModification, id=ID_TEXTVIEWERTEXTCTRL) |
|
150 |
||
151 |
def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""): |
|
152 |
if tagname != "" and controler is not None: |
|
153 |
self.VARIABLE_PANEL_TYPE = controler.GetPouType(tagname.split("::")[1]) |
|
154 |
||
155 |
EditorPanel.__init__(self, parent, tagname, window, controler, debug) |
|
156 |
||
157 |
self.Keywords = [] |
|
158 |
self.Variables = {} |
|
159 |
self.Functions = {} |
|
160 |
self.TypeNames = [] |
|
161 |
self.Jumps = [] |
|
162 |
self.EnumeratedValues = [] |
|
163 |
self.DisableEvents = True |
|
164 |
self.TextSyntax = None |
|
165 |
self.CurrentAction = None |
|
1178
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
166 |
|
814 | 167 |
self.InstancePath = instancepath |
168 |
self.ContextStack = [] |
|
169 |
self.CallStack = [] |
|
170 |
||
1178
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
171 |
self.ResetSearchResults() |
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
172 |
|
814 | 173 |
self.RefreshHighlightsTimer = wx.Timer(self, -1) |
174 |
self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer) |
|
175 |
||
176 |
def __del__(self): |
|
177 |
self.RefreshHighlightsTimer.Stop() |
|
178 |
||
179 |
def GetTitle(self): |
|
180 |
if self.Debug or self.TagName == "": |
|
181 |
if len(self.InstancePath) > 15: |
|
182 |
return "..." + self.InstancePath[-12:] |
|
183 |
return self.InstancePath |
|
184 |
return EditorPanel.GetTitle(self) |
|
185 |
||
186 |
def GetInstancePath(self): |
|
187 |
return self.InstancePath |
|
188 |
||
189 |
def IsViewing(self, tagname): |
|
190 |
if self.Debug or self.TagName == "": |
|
191 |
return self.InstancePath == tagname |
|
192 |
else: |
|
193 |
return self.TagName == tagname |
|
194 |
||
195 |
def GetText(self): |
|
196 |
return self.Editor.GetText() |
|
197 |
||
198 |
def SetText(self, text): |
|
199 |
self.Editor.SetText(text) |
|
200 |
||
201 |
def SelectAll(self): |
|
202 |
self.Editor.SelectAll() |
|
203 |
||
204 |
def Colourise(self, start, end): |
|
205 |
self.Editor.Colourise(start, end) |
|
206 |
||
207 |
def StartStyling(self, pos, mask): |
|
208 |
self.Editor.StartStyling(pos, mask) |
|
209 |
||
210 |
def SetStyling(self, length, style): |
|
211 |
self.Editor.SetStyling(length, style) |
|
212 |
||
213 |
def GetCurrentPos(self): |
|
214 |
return self.Editor.GetCurrentPos() |
|
215 |
||
1178
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
216 |
def ResetSearchResults(self): |
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
217 |
self.Highlights = [] |
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
218 |
self.SearchParams = None |
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
219 |
self.SearchResults = None |
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
220 |
self.CurrentFindHighlight = None |
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
221 |
|
814 | 222 |
def OnModification(self, event): |
223 |
if not self.DisableEvents: |
|
224 |
mod_type = event.GetModificationType() |
|
225 |
if mod_type&wx.stc.STC_MOD_BEFOREINSERT: |
|
226 |
if self.CurrentAction == None: |
|
227 |
self.StartBuffering() |
|
228 |
elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1: |
|
229 |
self.Controler.EndBuffering() |
|
230 |
self.StartBuffering() |
|
231 |
self.CurrentAction = ("Add", event.GetPosition()) |
|
232 |
wx.CallAfter(self.RefreshModel) |
|
233 |
elif mod_type&wx.stc.STC_MOD_BEFOREDELETE: |
|
234 |
if self.CurrentAction == None: |
|
235 |
self.StartBuffering() |
|
236 |
elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1: |
|
237 |
self.Controler.EndBuffering() |
|
238 |
self.StartBuffering() |
|
239 |
self.CurrentAction = ("Delete", event.GetPosition()) |
|
240 |
wx.CallAfter(self.RefreshModel) |
|
241 |
event.Skip() |
|
242 |
||
243 |
def OnDoDrop(self, event): |
|
244 |
try: |
|
245 |
values = eval(event.GetDragText()) |
|
246 |
except: |
|
247 |
values = event.GetDragText() |
|
248 |
if isinstance(values, tuple): |
|
249 |
message = None |
|
250 |
if values[1] in ["program", "debug"]: |
|
251 |
event.SetDragText("") |
|
252 |
elif values[1] in ["functionBlock", "function"]: |
|
253 |
blocktype = values[0] |
|
254 |
blockname = values[2] |
|
255 |
if len(values) > 3: |
|
256 |
blockinputs = values[3] |
|
257 |
else: |
|
258 |
blockinputs = None |
|
259 |
if values[1] != "function": |
|
260 |
if blockname == "": |
|
261 |
dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE) |
|
262 |
if dialog.ShowModal() == wx.ID_OK: |
|
263 |
blockname = dialog.GetValue() |
|
264 |
else: |
|
265 |
return |
|
266 |
dialog.Destroy() |
|
267 |
if blockname.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
268 |
message = _("\"%s\" pou already exists!")%blockname |
|
269 |
elif blockname.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
270 |
message = _("\"%s\" element for this pou already exists!")%blockname |
|
271 |
else: |
|
272 |
self.Controler.AddEditedElementPouVar(self.TagName, values[0], blockname) |
|
273 |
self.RefreshVariablePanel() |
|
274 |
self.RefreshVariableTree() |
|
275 |
blockinfo = self.Controler.GetBlockType(blocktype, blockinputs, self.Debug) |
|
276 |
hint = ',\n '.join( |
|
277 |
[ " " + fctdecl[0]+" := (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["inputs"]] + |
|
278 |
[ " " + fctdecl[0]+" => (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["outputs"]]) |
|
279 |
if values[1] == "function": |
|
280 |
event.SetDragText(blocktype+"(\n "+hint+")") |
|
281 |
else: |
|
282 |
event.SetDragText(blockname+"(\n "+hint+")") |
|
283 |
elif values[1] == "location": |
|
284 |
pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug) |
|
285 |
if len(values) > 2 and pou_type == "program": |
|
286 |
var_name = values[3] |
|
287 |
if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
288 |
message = _("\"%s\" pou already exists!")%var_name |
|
289 |
elif var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
290 |
message = _("\"%s\" element for this pou already exists!")%var_name |
|
291 |
else: |
|
292 |
location = values[0] |
|
293 |
if not location.startswith("%"): |
|
294 |
dialog = wx.SingleChoiceDialog(self.ParentWindow, |
|
295 |
_("Select a variable class:"), _("Variable class"), |
|
296 |
["Input", "Output", "Memory"], |
|
297 |
wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL) |
|
298 |
if dialog.ShowModal() == wx.ID_OK: |
|
299 |
selected = dialog.GetSelection() |
|
300 |
else: |
|
301 |
selected = None |
|
302 |
dialog.Destroy() |
|
303 |
if selected is None: |
|
304 |
event.SetDragText("") |
|
305 |
return |
|
306 |
if selected == 0: |
|
307 |
location = "%I" + location |
|
308 |
elif selected == 1: |
|
309 |
location = "%Q" + location |
|
310 |
else: |
|
311 |
location = "%M" + location |
|
312 |
if values[2] is not None: |
|
313 |
var_type = values[2] |
|
314 |
else: |
|
315 |
var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0] |
|
316 |
self.Controler.AddEditedElementPouVar(self.TagName, var_type, var_name, location, values[4]) |
|
317 |
self.RefreshVariablePanel() |
|
318 |
self.RefreshVariableTree() |
|
319 |
event.SetDragText(var_name) |
|
320 |
else: |
|
321 |
event.SetDragText("") |
|
322 |
elif values[1] == "Global": |
|
323 |
var_name = values[0] |
|
324 |
if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
325 |
message = _("\"%s\" pou already exists!")%var_name |
|
326 |
else: |
|
327 |
if not var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
328 |
self.Controler.AddEditedElementPouExternalVar(self.TagName, values[2], var_name) |
|
329 |
self.RefreshVariablePanel() |
|
330 |
self.RefreshVariableTree() |
|
331 |
event.SetDragText(var_name) |
|
332 |
elif values[1] == "Constant": |
|
333 |
event.SetDragText(values[0]) |
|
334 |
elif values[3] == self.TagName: |
|
335 |
self.ResetBuffer() |
|
336 |
event.SetDragText(values[0]) |
|
337 |
wx.CallAfter(self.RefreshModel) |
|
338 |
else: |
|
339 |
message = _("Variable don't belong to this POU!") |
|
340 |
if message is not None: |
|
341 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
342 |
dialog.ShowModal() |
|
343 |
dialog.Destroy() |
|
344 |
event.SetDragText("") |
|
345 |
event.Skip() |
|
346 |
||
347 |
def SetTextSyntax(self, syntax): |
|
348 |
self.TextSyntax = syntax |
|
349 |
if syntax in ["ST", "ALL"]: |
|
350 |
self.Editor.SetMarginType(2, wx.stc.STC_MARGIN_SYMBOL) |
|
351 |
self.Editor.SetMarginMask(2, wx.stc.STC_MASK_FOLDERS) |
|
352 |
self.Editor.SetMarginSensitive(2, 1) |
|
353 |
self.Editor.SetMarginWidth(2, 12) |
|
354 |
if syntax == "ST": |
|
355 |
self.BlockStartKeywords = ST_BLOCK_START_KEYWORDS |
|
356 |
self.BlockEndKeywords = ST_BLOCK_START_KEYWORDS |
|
357 |
else: |
|
358 |
self.BlockStartKeywords = IEC_BLOCK_START_KEYWORDS |
|
359 |
self.BlockEndKeywords = IEC_BLOCK_START_KEYWORDS |
|
360 |
else: |
|
361 |
self.BlockStartKeywords = [] |
|
362 |
self.BlockEndKeywords = [] |
|
363 |
||
364 |
def SetKeywords(self, keywords): |
|
365 |
self.Keywords = [keyword.upper() for keyword in keywords] |
|
366 |
self.Colourise(0, -1) |
|
367 |
||
368 |
def RefreshJumpList(self): |
|
369 |
if self.TextSyntax != "IL": |
|
370 |
self.Jumps = [jump.upper() for jump in LABEL_MODEL.findall(self.GetText())] |
|
371 |
self.Colourise(0, -1) |
|
372 |
||
373 |
# Buffer the last model state |
|
374 |
def RefreshBuffer(self): |
|
375 |
self.Controler.BufferProject() |
|
376 |
if self.ParentWindow: |
|
377 |
self.ParentWindow.RefreshTitle() |
|
378 |
self.ParentWindow.RefreshFileMenu() |
|
379 |
self.ParentWindow.RefreshEditMenu() |
|
380 |
||
381 |
def StartBuffering(self): |
|
382 |
self.Controler.StartBuffering() |
|
383 |
if self.ParentWindow: |
|
384 |
self.ParentWindow.RefreshTitle() |
|
385 |
self.ParentWindow.RefreshFileMenu() |
|
386 |
self.ParentWindow.RefreshEditMenu() |
|
387 |
||
388 |
def ResetBuffer(self): |
|
389 |
if self.CurrentAction != None: |
|
390 |
self.Controler.EndBuffering() |
|
391 |
self.CurrentAction = None |
|
392 |
||
393 |
def GetBufferState(self): |
|
394 |
if not self.Debug and self.TextSyntax != "ALL": |
|
395 |
return self.Controler.GetBufferState() |
|
396 |
return False, False |
|
397 |
||
398 |
def Undo(self): |
|
399 |
if not self.Debug and self.TextSyntax != "ALL": |
|
400 |
self.Controler.LoadPrevious() |
|
401 |
self.ParentWindow.CloseTabsWithoutModel() |
|
402 |
||
403 |
def Redo(self): |
|
404 |
if not self.Debug and self.TextSyntax != "ALL": |
|
405 |
self.Controler.LoadNext() |
|
406 |
self.ParentWindow.CloseTabsWithoutModel() |
|
407 |
||
408 |
def HasNoModel(self): |
|
409 |
if not self.Debug and self.TextSyntax != "ALL": |
|
410 |
return self.Controler.GetEditedElement(self.TagName) is None |
|
411 |
return False |
|
412 |
||
413 |
def RefreshView(self, variablepanel=True): |
|
414 |
EditorPanel.RefreshView(self, variablepanel) |
|
415 |
||
416 |
if self.Controler is not None: |
|
417 |
self.ResetBuffer() |
|
418 |
self.DisableEvents = True |
|
419 |
old_cursor_pos = self.GetCurrentPos() |
|
1060
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
420 |
line = self.Editor.GetFirstVisibleLine() |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
421 |
column = self.Editor.GetXOffset() |
814 | 422 |
old_text = self.GetText() |
423 |
new_text = self.Controler.GetEditedElementText(self.TagName, self.Debug) |
|
1060
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
424 |
if old_text != new_text: |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
425 |
self.SetText(new_text) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
426 |
new_cursor_pos = GetCursorPos(old_text, new_text) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
427 |
self.Editor.LineScroll(column, line) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
428 |
if new_cursor_pos != None: |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
429 |
self.Editor.GotoPos(new_cursor_pos) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
430 |
else: |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
431 |
self.Editor.GotoPos(old_cursor_pos) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
432 |
self.RefreshJumpList() |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
433 |
self.Editor.EmptyUndoBuffer() |
814 | 434 |
self.DisableEvents = False |
435 |
||
436 |
self.RefreshVariableTree() |
|
437 |
||
438 |
self.TypeNames = [typename.upper() for typename in self.Controler.GetDataTypes(self.TagName, True, self.Debug)] |
|
439 |
self.EnumeratedValues = [value.upper() for value in self.Controler.GetEnumeratedDataValues()] |
|
440 |
||
441 |
self.Functions = {} |
|
442 |
for category in self.Controler.GetBlockTypes(self.TagName, self.Debug): |
|
443 |
for blocktype in category["list"]: |
|
444 |
blockname = blocktype["name"].upper() |
|
445 |
if blocktype["type"] == "function" and blockname not in self.Keywords and blockname not in self.Variables.keys(): |
|
446 |
interface = dict([(name, {}) for name, type, modifier in blocktype["inputs"] + blocktype["outputs"] if name != '']) |
|
447 |
for param in ["EN", "ENO"]: |
|
448 |
if not interface.has_key(param): |
|
449 |
interface[param] = {} |
|
450 |
if self.Functions.has_key(blockname): |
|
451 |
self.Functions[blockname]["interface"].update(interface) |
|
452 |
self.Functions[blockname]["extensible"] |= blocktype["extensible"] |
|
453 |
else: |
|
454 |
self.Functions[blockname] = {"interface": interface, |
|
455 |
"extensible": blocktype["extensible"]} |
|
456 |
||
457 |
self.Colourise(0, -1) |
|
458 |
||
459 |
def RefreshVariableTree(self): |
|
460 |
words = self.TagName.split("::") |
|
1347
533741e5075c
Fixed pou variables information loading stylesheet
Laurent Bessard
parents:
1308
diff
changeset
|
461 |
self.Variables = self.GenerateVariableTree( |
533741e5075c
Fixed pou variables information loading stylesheet
Laurent Bessard
parents:
1308
diff
changeset
|
462 |
[(variable.Name, variable.Type, variable.Tree) |
533741e5075c
Fixed pou variables information loading stylesheet
Laurent Bessard
parents:
1308
diff
changeset
|
463 |
for variable in self.Controler.GetEditedElementInterfaceVars( |
533741e5075c
Fixed pou variables information loading stylesheet
Laurent Bessard
parents:
1308
diff
changeset
|
464 |
self.TagName, True, self.Debug)]) |
814 | 465 |
if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL": |
1347
533741e5075c
Fixed pou variables information loading stylesheet
Laurent Bessard
parents:
1308
diff
changeset
|
466 |
return_type, (var_tree, var_dimension) = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, True, self.Debug) |
814 | 467 |
if return_type is not None: |
468 |
self.Variables[words[-1].upper()] = self.GenerateVariableTree(var_tree) |
|
469 |
else: |
|
470 |
self.Variables[words[-1].upper()] = {} |
|
471 |
||
472 |
def GenerateVariableTree(self, list): |
|
473 |
tree = {} |
|
474 |
for var_name, var_type, (var_tree, var_dimension) in list: |
|
475 |
tree[var_name.upper()] = self.GenerateVariableTree(var_tree) |
|
476 |
return tree |
|
477 |
||
478 |
def IsValidVariable(self, name, context): |
|
479 |
return context is not None and context.get(name, None) is not None |
|
480 |
||
481 |
def IsCallParameter(self, name, call): |
|
482 |
if call is not None: |
|
483 |
return (call["interface"].get(name.upper(), None) is not None or |
|
484 |
call["extensible"] and EXTENSIBLE_PARAMETER.match(name.upper()) is not None) |
|
485 |
return False |
|
486 |
||
487 |
def RefreshLineFolding(self, line_number): |
|
488 |
if self.TextSyntax in ["ST", "ALL"]: |
|
489 |
level = wx.stc.STC_FOLDLEVELBASE + self.Editor.GetLineIndentation(line_number) |
|
490 |
line = self.Editor.GetLine(line_number).strip() |
|
491 |
if line == "": |
|
492 |
if line_number > 0: |
|
493 |
if LineStartswith(self.Editor.GetLine(line_number - 1).strip(), self.BlockEndKeywords): |
|
494 |
level = self.Editor.GetFoldLevel(self.Editor.GetFoldParent(line_number - 1)) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
495 |
else: |
|
496 |
level = self.Editor.GetFoldLevel(line_number - 1) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
497 |
if level != wx.stc.STC_FOLDLEVELBASE: |
|
498 |
level |= wx.stc.STC_FOLDLEVELWHITEFLAG |
|
499 |
elif LineStartswith(line, self.BlockStartKeywords): |
|
500 |
level |= wx.stc.STC_FOLDLEVELHEADERFLAG |
|
501 |
elif LineStartswith(line, self.BlockEndKeywords): |
|
502 |
if LineStartswith(self.Editor.GetLine(line_number - 1).strip(), self.BlockEndKeywords): |
|
503 |
level = self.Editor.GetFoldLevel(self.Editor.GetFoldParent(line_number - 1)) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
504 |
else: |
|
505 |
level = self.Editor.GetFoldLevel(line_number - 1) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
506 |
self.Editor.SetFoldLevel(line_number, level) |
|
507 |
||
508 |
def OnStyleNeeded(self, event): |
|
509 |
self.TextChanged = True |
|
510 |
line_number = self.Editor.LineFromPosition(self.Editor.GetEndStyled()) |
|
511 |
if line_number == 0: |
|
512 |
start_pos = last_styled_pos = 0 |
|
513 |
else: |
|
514 |
start_pos = last_styled_pos = self.Editor.GetLineEndPosition(line_number - 1) + 1 |
|
515 |
self.RefreshLineFolding(line_number) |
|
516 |
end_pos = event.GetPosition() |
|
517 |
self.StartStyling(start_pos, 0xff) |
|
518 |
||
519 |
current_context = self.Variables |
|
520 |
current_call = None |
|
521 |
||
522 |
current_pos = last_styled_pos |
|
523 |
state = SPACE |
|
524 |
line = "" |
|
525 |
word = "" |
|
526 |
while current_pos < end_pos: |
|
527 |
char = chr(self.Editor.GetCharAt(current_pos)).upper() |
|
528 |
line += char |
|
529 |
if char == NEWLINE: |
|
530 |
self.ContextStack = [] |
|
531 |
current_context = self.Variables |
|
532 |
if state == COMMENT: |
|
533 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_COMMENT) |
|
534 |
elif state == NUMBER: |
|
535 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
536 |
elif state == WORD: |
|
537 |
if word in self.Keywords or word in self.TypeNames: |
|
538 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
539 |
elif self.IsValidVariable(word, current_context): |
|
540 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
541 |
elif self.IsCallParameter(word, current_call): |
|
542 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
543 |
elif word in self.Functions: |
|
544 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
545 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
|
546 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
547 |
elif word in self.EnumeratedValues: |
|
548 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
549 |
else: |
|
550 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
551 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
|
552 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
|
553 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
|
554 |
self.StartStyling(current_pos, 0xff) |
|
555 |
else: |
|
556 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
557 |
last_styled_pos = current_pos |
|
1108
1ec5b4d244f3
Fix bug with multiline Pragma syntax highlighting in TextViewer
Laurent Bessard
parents:
1091
diff
changeset
|
558 |
if state != DPRAGMA: |
1ec5b4d244f3
Fix bug with multiline Pragma syntax highlighting in TextViewer
Laurent Bessard
parents:
1091
diff
changeset
|
559 |
state = SPACE |
814 | 560 |
line = "" |
561 |
line_number += 1 |
|
562 |
self.RefreshLineFolding(line_number) |
|
563 |
elif line.endswith("(*") and state != COMMENT: |
|
564 |
self.SetStyling(current_pos - last_styled_pos - 1, 31) |
|
565 |
last_styled_pos = current_pos |
|
566 |
if state == WORD: |
|
567 |
current_context = self.Variables |
|
568 |
state = COMMENT |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
569 |
elif line.endswith("{") and state not in [PRAGMA, DPRAGMA]: |
858
daafaa8a28fd
Fix bug in TextViewer when starting text with '{' character
Laurent Bessard
parents:
814
diff
changeset
|
570 |
self.SetStyling(current_pos - last_styled_pos, 31) |
814 | 571 |
last_styled_pos = current_pos |
572 |
if state == WORD: |
|
573 |
current_context = self.Variables |
|
574 |
state = PRAGMA |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
575 |
elif line.endswith("{{") and state == PRAGMA: |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
576 |
self.SetStyling(current_pos - last_styled_pos, 31) |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
577 |
last_styled_pos = current_pos |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
578 |
state = DPRAGMA |
814 | 579 |
elif state == COMMENT: |
580 |
if line.endswith("*)"): |
|
581 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
|
582 |
last_styled_pos = current_pos + 1 |
|
583 |
state = SPACE |
|
584 |
elif state == PRAGMA: |
|
585 |
if line.endswith("}"): |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
586 |
self.SetStyling(current_pos - last_styled_pos, 31) |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
587 |
last_styled_pos = current_pos |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
588 |
state = SPACE |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
589 |
elif state == DPRAGMA: |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
590 |
if line.endswith("}}"): |
1108
1ec5b4d244f3
Fix bug with multiline Pragma syntax highlighting in TextViewer
Laurent Bessard
parents:
1091
diff
changeset
|
591 |
self.SetStyling(current_pos - last_styled_pos + 1, 31) |
814 | 592 |
last_styled_pos = current_pos + 1 |
593 |
state = SPACE |
|
594 |
elif (line.endswith("'") or line.endswith('"')) and state not in [STRING, WSTRING]: |
|
595 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
596 |
last_styled_pos = current_pos |
|
597 |
if state == WORD: |
|
598 |
current_context = self.Variables |
|
599 |
if line.endswith("'"): |
|
600 |
state = STRING |
|
601 |
else: |
|
602 |
state = WSTRING |
|
603 |
elif state == STRING: |
|
604 |
if line.endswith("'") and not line.endswith("$'"): |
|
605 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
606 |
last_styled_pos = current_pos + 1 |
|
607 |
state = SPACE |
|
608 |
elif state == WSTRING: |
|
609 |
if line.endswith('"') and not line.endswith('$"'): |
|
610 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
611 |
last_styled_pos = current_pos + 1 |
|
612 |
state = SPACE |
|
613 |
elif char in LETTERS: |
|
614 |
if state == NUMBER: |
|
615 |
word = "#" |
|
616 |
state = WORD |
|
617 |
elif state == SPACE: |
|
618 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
619 |
word = char |
|
620 |
last_styled_pos = current_pos |
|
621 |
state = WORD |
|
622 |
else: |
|
623 |
word += char |
|
624 |
elif char in NUMBERS or char == '.' and state != WORD: |
|
625 |
if state == SPACE: |
|
626 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
627 |
last_styled_pos = current_pos |
|
628 |
state = NUMBER |
|
629 |
elif state == WORD and char != '.': |
|
630 |
word += char |
|
631 |
elif char == '(' and state == SPACE: |
|
632 |
self.CallStack.append(current_call) |
|
633 |
current_call = None |
|
634 |
else: |
|
635 |
if state == WORD: |
|
636 |
if word in self.Keywords or word in self.TypeNames: |
|
637 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
638 |
elif self.IsValidVariable(word, current_context): |
|
639 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
640 |
elif self.IsCallParameter(word, current_call): |
|
641 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
642 |
elif word in self.Functions: |
|
643 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
644 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
|
645 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
646 |
elif word in self.EnumeratedValues: |
|
647 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
648 |
else: |
|
649 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
650 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
|
651 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
|
652 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
|
653 |
self.StartStyling(current_pos, 0xff) |
|
654 |
if char == '.': |
|
655 |
if word != "]": |
|
656 |
if current_context is not None: |
|
657 |
current_context = current_context.get(word, None) |
|
658 |
else: |
|
659 |
current_context = None |
|
660 |
elif char == '(': |
|
661 |
self.CallStack.append(current_call) |
|
662 |
current_call = self.Functions.get(word, None) |
|
663 |
if current_call is None and self.IsValidVariable(word, current_context): |
|
664 |
current_call = {"interface": current_context.get(word, {}), |
|
665 |
"extensible": False} |
|
666 |
current_context = self.Variables |
|
667 |
else: |
|
894
a4919f228924
Fixed bug when indexing table in Pragma within ST/IL code
Laurent Bessard
parents:
871
diff
changeset
|
668 |
if char == '[' and current_context is not None: |
814 | 669 |
self.ContextStack.append(current_context.get(word, None)) |
670 |
current_context = self.Variables |
|
671 |
||
672 |
word = "" |
|
673 |
last_styled_pos = current_pos |
|
674 |
state = SPACE |
|
675 |
elif state == NUMBER: |
|
676 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
677 |
last_styled_pos = current_pos |
|
678 |
state = SPACE |
|
679 |
if char == ']': |
|
680 |
if len(self.ContextStack) > 0: |
|
681 |
current_context = self.ContextStack.pop() |
|
682 |
else: |
|
683 |
current_context = self.Variables |
|
684 |
word = char |
|
685 |
state = WORD |
|
686 |
elif char == ')': |
|
687 |
current_context = self.Variables |
|
688 |
if len(self.CallStack) > 0: |
|
689 |
current_call = self.CallStack.pop() |
|
690 |
else: |
|
691 |
current_call = None |
|
692 |
word = char |
|
693 |
state = WORD |
|
694 |
current_pos += 1 |
|
695 |
if state == COMMENT: |
|
696 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
|
697 |
elif state == NUMBER: |
|
698 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
699 |
elif state == WORD: |
|
700 |
if word in self.Keywords or word in self.TypeNames: |
|
701 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
702 |
elif self.IsValidVariable(word, current_context): |
|
703 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
704 |
elif self.IsCallParameter(word, current_call): |
|
705 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
706 |
elif self.TextSyntax == "IL" and word in self.Functions: |
|
707 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
708 |
elif word in self.Jumps: |
|
709 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
710 |
elif word in self.EnumeratedValues: |
|
711 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
712 |
else: |
|
713 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
714 |
else: |
|
715 |
self.SetStyling(current_pos - start_pos, 31) |
|
716 |
self.ShowHighlights(start_pos, end_pos) |
|
717 |
event.Skip() |
|
718 |
||
719 |
def OnMarginClick(self, event): |
|
720 |
if event.GetMargin() == 2: |
|
721 |
line = self.Editor.LineFromPosition(event.GetPosition()) |
|
722 |
if self.Editor.GetFoldLevel(line) & wx.stc.STC_FOLDLEVELHEADERFLAG: |
|
723 |
self.Editor.ToggleFold(line) |
|
724 |
event.Skip() |
|
1126
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
725 |
|
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
726 |
def OnUpdateUI(self, event): |
1262
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
727 |
selected = self.Editor.GetSelectedText() |
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
728 |
if self.ParentWindow and selected != "": |
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
729 |
self.ParentWindow.SetCopyBuffer(selected, True) |
1126
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
730 |
event.Skip() |
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
731 |
|
814 | 732 |
def Cut(self): |
733 |
self.ResetBuffer() |
|
734 |
self.DisableEvents = True |
|
735 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_CUT) |
|
736 |
self.DisableEvents = False |
|
737 |
self.RefreshModel() |
|
738 |
self.RefreshBuffer() |
|
739 |
||
740 |
def Copy(self): |
|
741 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_COPY) |
|
1126
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
742 |
if self.ParentWindow: |
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
743 |
self.ParentWindow.RefreshEditMenu() |
814 | 744 |
|
745 |
def Paste(self): |
|
746 |
self.ResetBuffer() |
|
747 |
self.DisableEvents = True |
|
748 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_PASTE) |
|
749 |
self.DisableEvents = False |
|
750 |
self.RefreshModel() |
|
751 |
self.RefreshBuffer() |
|
752 |
||
1057
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
753 |
def Search(self, criteria): |
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
754 |
return self.Controler.SearchInPou(self.TagName, criteria, self.Debug) |
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
755 |
|
814 | 756 |
def Find(self, direction, search_params): |
757 |
if self.SearchParams != search_params: |
|
758 |
self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT) |
|
759 |
||
760 |
self.SearchParams = search_params |
|
761 |
criteria = { |
|
762 |
"raw_pattern": search_params["find_pattern"], |
|
763 |
"pattern": re.compile(search_params["find_pattern"]), |
|
764 |
"case_sensitive": search_params["case_sensitive"], |
|
765 |
"regular_expression": search_params["regular_expression"], |
|
766 |
"filter": "all"} |
|
767 |
||
768 |
self.SearchResults = [ |
|
769 |
(infos[1:], start, end, SEARCH_RESULT_HIGHLIGHT) |
|
770 |
for infos, start, end, text in |
|
1057
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
771 |
self.Search(criteria)] |
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
772 |
self.CurrentFindHighlight = None |
814 | 773 |
|
774 |
if len(self.SearchResults) > 0: |
|
775 |
if self.CurrentFindHighlight is not None: |
|
776 |
old_idx = self.SearchResults.index(self.CurrentFindHighlight) |
|
777 |
if self.SearchParams["wrap"]: |
|
778 |
idx = (old_idx + direction) % len(self.SearchResults) |
|
779 |
else: |
|
780 |
idx = max(0, min(old_idx + direction, len(self.SearchResults) - 1)) |
|
781 |
if idx != old_idx: |
|
782 |
self.RemoveHighlight(*self.CurrentFindHighlight) |
|
783 |
self.CurrentFindHighlight = self.SearchResults[idx] |
|
784 |
self.AddHighlight(*self.CurrentFindHighlight) |
|
785 |
else: |
|
786 |
self.CurrentFindHighlight = self.SearchResults[0] |
|
787 |
self.AddHighlight(*self.CurrentFindHighlight) |
|
788 |
||
789 |
else: |
|
790 |
if self.CurrentFindHighlight is not None: |
|
791 |
self.RemoveHighlight(*self.CurrentFindHighlight) |
|
792 |
self.CurrentFindHighlight = None |
|
793 |
||
794 |
def RefreshModel(self): |
|
795 |
self.RefreshJumpList() |
|
796 |
self.Controler.SetEditedElementText(self.TagName, self.GetText()) |
|
1178
3e2aebc9c7c0
Fixed search highlight bug in ST and Code text editors
Laurent Bessard
parents:
1126
diff
changeset
|
797 |
self.ResetSearchResults() |
814 | 798 |
|
799 |
def OnKeyDown(self, event): |
|
1262
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
800 |
key = event.GetKeyCode() |
814 | 801 |
if self.Controler is not None: |
802 |
||
803 |
if self.Editor.CallTipActive(): |
|
804 |
self.Editor.CallTipCancel() |
|
1262
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
805 |
|
814 | 806 |
key_handled = False |
807 |
||
808 |
line = self.Editor.GetCurrentLine() |
|
809 |
if line == 0: |
|
810 |
start_pos = 0 |
|
811 |
else: |
|
812 |
start_pos = self.Editor.GetLineEndPosition(line - 1) + 1 |
|
813 |
end_pos = self.GetCurrentPos() |
|
814 |
lineText = self.Editor.GetTextRange(start_pos, end_pos).replace("\t", " ") |
|
815 |
||
816 |
# Code completion |
|
817 |
if key == wx.WXK_SPACE and event.ControlDown(): |
|
818 |
||
819 |
words = lineText.split(" ") |
|
820 |
words = [word for i, word in enumerate(words) if word != '' or i == len(words) - 1] |
|
821 |
||
822 |
kw = [] |
|
823 |
||
824 |
if self.TextSyntax == "IL": |
|
825 |
if len(words) == 1: |
|
826 |
kw = self.Keywords |
|
827 |
elif len(words) == 2: |
|
828 |
if words[0].upper() in ["CAL", "CALC", "CALNC"]: |
|
829 |
kw = self.Functions |
|
830 |
elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]: |
|
831 |
kw = self.Jumps |
|
832 |
else: |
|
833 |
kw = self.Variables.keys() |
|
834 |
else: |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
835 |
kw = self.Keywords + self.Variables.keys() + self.Functions.keys() |
814 | 836 |
if len(kw) > 0: |
837 |
if len(words[-1]) > 0: |
|
838 |
kw = [keyword for keyword in kw if keyword.startswith(words[-1])] |
|
839 |
kw.sort() |
|
840 |
self.Editor.AutoCompSetIgnoreCase(True) |
|
841 |
self.Editor.AutoCompShow(len(words[-1]), " ".join(kw)) |
|
842 |
key_handled = True |
|
843 |
elif key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER: |
|
844 |
if self.TextSyntax in ["ST", "ALL"]: |
|
845 |
indent = self.Editor.GetLineIndentation(line) |
|
846 |
if LineStartswith(lineText.strip(), self.BlockStartKeywords): |
|
847 |
indent = (indent / 2 + 1) * 2 |
|
848 |
self.Editor.AddText("\n" + " " * indent) |
|
849 |
key_handled = True |
|
850 |
elif key == wx.WXK_BACK: |
|
851 |
if self.TextSyntax in ["ST", "ALL"]: |
|
852 |
indent = self.Editor.GetLineIndentation(line) |
|
853 |
if lineText.strip() == "" and indent > 0: |
|
854 |
self.Editor.DelLineLeft() |
|
855 |
self.Editor.AddText(" " * ((max(0, indent - 1) / 2) * 2)) |
|
856 |
key_handled = True |
|
857 |
if not key_handled: |
|
858 |
event.Skip() |
|
1262
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
859 |
elif key in NAVIGATION_KEYS: |
7b9259945453
Fixed bug with Copy/Paste in generated ST code Viewer
Laurent Bessard
parents:
1178
diff
changeset
|
860 |
event.Skip() |
814 | 861 |
|
862 |
def OnKillFocus(self, event): |
|
863 |
self.Editor.AutoCompCancel() |
|
864 |
event.Skip() |
|
865 |
||
866 |
#------------------------------------------------------------------------------- |
|
867 |
# Highlights showing functions |
|
868 |
#------------------------------------------------------------------------------- |
|
869 |
||
870 |
def OnRefreshHighlightsTimer(self, event): |
|
871 |
self.RefreshView() |
|
872 |
event.Skip() |
|
873 |
||
874 |
def ClearHighlights(self, highlight_type=None): |
|
875 |
EditorPanel.ClearHighlights(self, highlight_type) |
|
876 |
||
877 |
if highlight_type is None: |
|
878 |
self.Highlights = [] |
|
879 |
else: |
|
880 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
881 |
if highlight_type is not None: |
|
882 |
self.Highlights = [(infos, start, end, highlight) for (infos, start, end, highlight) in self.Highlights if highlight != highlight_type] |
|
883 |
self.RefreshView() |
|
884 |
||
885 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
886 |
EditorPanel.AddHighlight(self, infos, start, end, highlight_type) |
|
887 |
||
888 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
889 |
if infos[0] == "body" and highlight_type is not None: |
|
890 |
self.Highlights.append((infos[1], start, end, highlight_type)) |
|
891 |
self.Editor.GotoPos(self.Editor.PositionFromLine(start[0]) + start[1]) |
|
892 |
self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) |
|
893 |
||
894 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
895 |
EditorPanel.RemoveHighlight(self, infos, start, end, highlight_type) |
|
896 |
||
897 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
898 |
if (infos[0] == "body" and highlight_type is not None and |
|
899 |
(infos[1], start, end, highlight_type) in self.Highlights): |
|
900 |
self.Highlights.remove((infos[1], start, end, highlight_type)) |
|
901 |
self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) |
|
902 |
||
903 |
def ShowHighlights(self, start_pos, end_pos): |
|
904 |
for indent, start, end, highlight_type in self.Highlights: |
|
905 |
if start[0] == 0: |
|
906 |
highlight_start_pos = start[1] - indent |
|
907 |
else: |
|
908 |
highlight_start_pos = self.Editor.GetLineEndPosition(start[0] - 1) + start[1] - indent + 1 |
|
909 |
if end[0] == 0: |
|
910 |
highlight_end_pos = end[1] - indent + 1 |
|
911 |
else: |
|
912 |
highlight_end_pos = self.Editor.GetLineEndPosition(end[0] - 1) + end[1] - indent + 2 |
|
913 |
if highlight_start_pos < end_pos and highlight_end_pos > start_pos: |
|
914 |
self.StartStyling(highlight_start_pos, 0xff) |
|
915 |
self.SetStyling(highlight_end_pos - highlight_start_pos, highlight_type) |
|
916 |
self.StartStyling(highlight_start_pos, 0x00) |
|
917 |
self.SetStyling(len(self.Editor.GetText()) - highlight_end_pos, wx.stc.STC_STYLE_DEFAULT) |
|
918 |