author | Laurent Bessard |
Tue, 14 May 2013 22:31:14 +0200 | |
changeset 1140 | 80a91fc91595 |
parent 1126 | 26baa0ae9fd7 |
child 1178 | 3e2aebc9c7c0 |
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 |
|
1091
5f612651d227
Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
1065
diff
changeset
|
34 |
from controls.CustomStyledTextCtrl import CustomStyledTextCtrl, faces, GetCursorPos |
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 |
|
166 |
self.Highlights = [] |
|
167 |
self.SearchParams = None |
|
168 |
self.SearchResults = None |
|
169 |
self.CurrentFindHighlight = None |
|
170 |
self.InstancePath = instancepath |
|
171 |
self.ContextStack = [] |
|
172 |
self.CallStack = [] |
|
173 |
||
174 |
self.RefreshHighlightsTimer = wx.Timer(self, -1) |
|
175 |
self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer) |
|
176 |
||
177 |
def __del__(self): |
|
178 |
self.RefreshHighlightsTimer.Stop() |
|
179 |
||
180 |
def GetTitle(self): |
|
181 |
if self.Debug or self.TagName == "": |
|
182 |
if len(self.InstancePath) > 15: |
|
183 |
return "..." + self.InstancePath[-12:] |
|
184 |
return self.InstancePath |
|
185 |
return EditorPanel.GetTitle(self) |
|
186 |
||
187 |
def GetInstancePath(self): |
|
188 |
return self.InstancePath |
|
189 |
||
190 |
def IsViewing(self, tagname): |
|
191 |
if self.Debug or self.TagName == "": |
|
192 |
return self.InstancePath == tagname |
|
193 |
else: |
|
194 |
return self.TagName == tagname |
|
195 |
||
196 |
def GetText(self): |
|
197 |
return self.Editor.GetText() |
|
198 |
||
199 |
def SetText(self, text): |
|
200 |
self.Editor.SetText(text) |
|
201 |
||
202 |
def SelectAll(self): |
|
203 |
self.Editor.SelectAll() |
|
204 |
||
205 |
def Colourise(self, start, end): |
|
206 |
self.Editor.Colourise(start, end) |
|
207 |
||
208 |
def StartStyling(self, pos, mask): |
|
209 |
self.Editor.StartStyling(pos, mask) |
|
210 |
||
211 |
def SetStyling(self, length, style): |
|
212 |
self.Editor.SetStyling(length, style) |
|
213 |
||
214 |
def GetCurrentPos(self): |
|
215 |
return self.Editor.GetCurrentPos() |
|
216 |
||
217 |
def OnModification(self, event): |
|
218 |
if not self.DisableEvents: |
|
219 |
mod_type = event.GetModificationType() |
|
220 |
if mod_type&wx.stc.STC_MOD_BEFOREINSERT: |
|
221 |
if self.CurrentAction == None: |
|
222 |
self.StartBuffering() |
|
223 |
elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1: |
|
224 |
self.Controler.EndBuffering() |
|
225 |
self.StartBuffering() |
|
226 |
self.CurrentAction = ("Add", event.GetPosition()) |
|
227 |
wx.CallAfter(self.RefreshModel) |
|
228 |
elif mod_type&wx.stc.STC_MOD_BEFOREDELETE: |
|
229 |
if self.CurrentAction == None: |
|
230 |
self.StartBuffering() |
|
231 |
elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1: |
|
232 |
self.Controler.EndBuffering() |
|
233 |
self.StartBuffering() |
|
234 |
self.CurrentAction = ("Delete", event.GetPosition()) |
|
235 |
wx.CallAfter(self.RefreshModel) |
|
236 |
event.Skip() |
|
237 |
||
238 |
def OnDoDrop(self, event): |
|
239 |
try: |
|
240 |
values = eval(event.GetDragText()) |
|
241 |
except: |
|
242 |
values = event.GetDragText() |
|
243 |
if isinstance(values, tuple): |
|
244 |
message = None |
|
245 |
if values[1] in ["program", "debug"]: |
|
246 |
event.SetDragText("") |
|
247 |
elif values[1] in ["functionBlock", "function"]: |
|
248 |
blocktype = values[0] |
|
249 |
blockname = values[2] |
|
250 |
if len(values) > 3: |
|
251 |
blockinputs = values[3] |
|
252 |
else: |
|
253 |
blockinputs = None |
|
254 |
if values[1] != "function": |
|
255 |
if blockname == "": |
|
256 |
dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE) |
|
257 |
if dialog.ShowModal() == wx.ID_OK: |
|
258 |
blockname = dialog.GetValue() |
|
259 |
else: |
|
260 |
return |
|
261 |
dialog.Destroy() |
|
262 |
if blockname.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
263 |
message = _("\"%s\" pou already exists!")%blockname |
|
264 |
elif blockname.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
265 |
message = _("\"%s\" element for this pou already exists!")%blockname |
|
266 |
else: |
|
267 |
self.Controler.AddEditedElementPouVar(self.TagName, values[0], blockname) |
|
268 |
self.RefreshVariablePanel() |
|
269 |
self.RefreshVariableTree() |
|
270 |
blockinfo = self.Controler.GetBlockType(blocktype, blockinputs, self.Debug) |
|
271 |
hint = ',\n '.join( |
|
272 |
[ " " + fctdecl[0]+" := (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["inputs"]] + |
|
273 |
[ " " + fctdecl[0]+" => (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["outputs"]]) |
|
274 |
if values[1] == "function": |
|
275 |
event.SetDragText(blocktype+"(\n "+hint+")") |
|
276 |
else: |
|
277 |
event.SetDragText(blockname+"(\n "+hint+")") |
|
278 |
elif values[1] == "location": |
|
279 |
pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug) |
|
280 |
if len(values) > 2 and pou_type == "program": |
|
281 |
var_name = values[3] |
|
282 |
if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
283 |
message = _("\"%s\" pou already exists!")%var_name |
|
284 |
elif var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
285 |
message = _("\"%s\" element for this pou already exists!")%var_name |
|
286 |
else: |
|
287 |
location = values[0] |
|
288 |
if not location.startswith("%"): |
|
289 |
dialog = wx.SingleChoiceDialog(self.ParentWindow, |
|
290 |
_("Select a variable class:"), _("Variable class"), |
|
291 |
["Input", "Output", "Memory"], |
|
292 |
wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL) |
|
293 |
if dialog.ShowModal() == wx.ID_OK: |
|
294 |
selected = dialog.GetSelection() |
|
295 |
else: |
|
296 |
selected = None |
|
297 |
dialog.Destroy() |
|
298 |
if selected is None: |
|
299 |
event.SetDragText("") |
|
300 |
return |
|
301 |
if selected == 0: |
|
302 |
location = "%I" + location |
|
303 |
elif selected == 1: |
|
304 |
location = "%Q" + location |
|
305 |
else: |
|
306 |
location = "%M" + location |
|
307 |
if values[2] is not None: |
|
308 |
var_type = values[2] |
|
309 |
else: |
|
310 |
var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0] |
|
311 |
self.Controler.AddEditedElementPouVar(self.TagName, var_type, var_name, location, values[4]) |
|
312 |
self.RefreshVariablePanel() |
|
313 |
self.RefreshVariableTree() |
|
314 |
event.SetDragText(var_name) |
|
315 |
else: |
|
316 |
event.SetDragText("") |
|
317 |
elif values[1] == "Global": |
|
318 |
var_name = values[0] |
|
319 |
if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
320 |
message = _("\"%s\" pou already exists!")%var_name |
|
321 |
else: |
|
322 |
if not var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
323 |
self.Controler.AddEditedElementPouExternalVar(self.TagName, values[2], var_name) |
|
324 |
self.RefreshVariablePanel() |
|
325 |
self.RefreshVariableTree() |
|
326 |
event.SetDragText(var_name) |
|
327 |
elif values[1] == "Constant": |
|
328 |
event.SetDragText(values[0]) |
|
329 |
elif values[3] == self.TagName: |
|
330 |
self.ResetBuffer() |
|
331 |
event.SetDragText(values[0]) |
|
332 |
wx.CallAfter(self.RefreshModel) |
|
333 |
else: |
|
334 |
message = _("Variable don't belong to this POU!") |
|
335 |
if message is not None: |
|
336 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
337 |
dialog.ShowModal() |
|
338 |
dialog.Destroy() |
|
339 |
event.SetDragText("") |
|
340 |
event.Skip() |
|
341 |
||
342 |
def SetTextSyntax(self, syntax): |
|
343 |
self.TextSyntax = syntax |
|
344 |
if syntax in ["ST", "ALL"]: |
|
345 |
self.Editor.SetMarginType(2, wx.stc.STC_MARGIN_SYMBOL) |
|
346 |
self.Editor.SetMarginMask(2, wx.stc.STC_MASK_FOLDERS) |
|
347 |
self.Editor.SetMarginSensitive(2, 1) |
|
348 |
self.Editor.SetMarginWidth(2, 12) |
|
349 |
if syntax == "ST": |
|
350 |
self.BlockStartKeywords = ST_BLOCK_START_KEYWORDS |
|
351 |
self.BlockEndKeywords = ST_BLOCK_START_KEYWORDS |
|
352 |
else: |
|
353 |
self.BlockStartKeywords = IEC_BLOCK_START_KEYWORDS |
|
354 |
self.BlockEndKeywords = IEC_BLOCK_START_KEYWORDS |
|
355 |
else: |
|
356 |
self.BlockStartKeywords = [] |
|
357 |
self.BlockEndKeywords = [] |
|
358 |
||
359 |
def SetKeywords(self, keywords): |
|
360 |
self.Keywords = [keyword.upper() for keyword in keywords] |
|
361 |
self.Colourise(0, -1) |
|
362 |
||
363 |
def RefreshJumpList(self): |
|
364 |
if self.TextSyntax != "IL": |
|
365 |
self.Jumps = [jump.upper() for jump in LABEL_MODEL.findall(self.GetText())] |
|
366 |
self.Colourise(0, -1) |
|
367 |
||
368 |
# Buffer the last model state |
|
369 |
def RefreshBuffer(self): |
|
370 |
self.Controler.BufferProject() |
|
371 |
if self.ParentWindow: |
|
372 |
self.ParentWindow.RefreshTitle() |
|
373 |
self.ParentWindow.RefreshFileMenu() |
|
374 |
self.ParentWindow.RefreshEditMenu() |
|
375 |
||
376 |
def StartBuffering(self): |
|
377 |
self.Controler.StartBuffering() |
|
378 |
if self.ParentWindow: |
|
379 |
self.ParentWindow.RefreshTitle() |
|
380 |
self.ParentWindow.RefreshFileMenu() |
|
381 |
self.ParentWindow.RefreshEditMenu() |
|
382 |
||
383 |
def ResetBuffer(self): |
|
384 |
if self.CurrentAction != None: |
|
385 |
self.Controler.EndBuffering() |
|
386 |
self.CurrentAction = None |
|
387 |
||
388 |
def GetBufferState(self): |
|
389 |
if not self.Debug and self.TextSyntax != "ALL": |
|
390 |
return self.Controler.GetBufferState() |
|
391 |
return False, False |
|
392 |
||
393 |
def Undo(self): |
|
394 |
if not self.Debug and self.TextSyntax != "ALL": |
|
395 |
self.Controler.LoadPrevious() |
|
396 |
self.ParentWindow.CloseTabsWithoutModel() |
|
397 |
||
398 |
def Redo(self): |
|
399 |
if not self.Debug and self.TextSyntax != "ALL": |
|
400 |
self.Controler.LoadNext() |
|
401 |
self.ParentWindow.CloseTabsWithoutModel() |
|
402 |
||
403 |
def HasNoModel(self): |
|
404 |
if not self.Debug and self.TextSyntax != "ALL": |
|
405 |
return self.Controler.GetEditedElement(self.TagName) is None |
|
406 |
return False |
|
407 |
||
408 |
def RefreshView(self, variablepanel=True): |
|
409 |
EditorPanel.RefreshView(self, variablepanel) |
|
410 |
||
411 |
if self.Controler is not None: |
|
412 |
self.ResetBuffer() |
|
413 |
self.DisableEvents = True |
|
414 |
old_cursor_pos = self.GetCurrentPos() |
|
1060
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
415 |
line = self.Editor.GetFirstVisibleLine() |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
416 |
column = self.Editor.GetXOffset() |
814 | 417 |
old_text = self.GetText() |
418 |
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
|
419 |
if old_text != new_text: |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
420 |
self.SetText(new_text) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
421 |
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
|
422 |
self.Editor.LineScroll(column, line) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
423 |
if new_cursor_pos != None: |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
424 |
self.Editor.GotoPos(new_cursor_pos) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
425 |
else: |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
426 |
self.Editor.GotoPos(old_cursor_pos) |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
427 |
self.RefreshJumpList() |
ac9896336b90
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
Laurent Bessard
parents:
1057
diff
changeset
|
428 |
self.Editor.EmptyUndoBuffer() |
814 | 429 |
self.DisableEvents = False |
430 |
||
431 |
self.RefreshVariableTree() |
|
432 |
||
433 |
self.TypeNames = [typename.upper() for typename in self.Controler.GetDataTypes(self.TagName, True, self.Debug)] |
|
434 |
self.EnumeratedValues = [value.upper() for value in self.Controler.GetEnumeratedDataValues()] |
|
435 |
||
436 |
self.Functions = {} |
|
437 |
for category in self.Controler.GetBlockTypes(self.TagName, self.Debug): |
|
438 |
for blocktype in category["list"]: |
|
439 |
blockname = blocktype["name"].upper() |
|
440 |
if blocktype["type"] == "function" and blockname not in self.Keywords and blockname not in self.Variables.keys(): |
|
441 |
interface = dict([(name, {}) for name, type, modifier in blocktype["inputs"] + blocktype["outputs"] if name != '']) |
|
442 |
for param in ["EN", "ENO"]: |
|
443 |
if not interface.has_key(param): |
|
444 |
interface[param] = {} |
|
445 |
if self.Functions.has_key(blockname): |
|
446 |
self.Functions[blockname]["interface"].update(interface) |
|
447 |
self.Functions[blockname]["extensible"] |= blocktype["extensible"] |
|
448 |
else: |
|
449 |
self.Functions[blockname] = {"interface": interface, |
|
450 |
"extensible": blocktype["extensible"]} |
|
451 |
||
452 |
self.Colourise(0, -1) |
|
453 |
||
454 |
def RefreshVariableTree(self): |
|
455 |
words = self.TagName.split("::") |
|
456 |
self.Variables = self.GenerateVariableTree([(variable["Name"], variable["Type"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)]) |
|
457 |
if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL": |
|
458 |
return_type = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug) |
|
459 |
if return_type is not None: |
|
460 |
var_tree, var_dimension = self.Controler.GenerateVarTree(return_type, self.Debug) |
|
461 |
self.Variables[words[-1].upper()] = self.GenerateVariableTree(var_tree) |
|
462 |
else: |
|
463 |
self.Variables[words[-1].upper()] = {} |
|
464 |
||
465 |
def GenerateVariableTree(self, list): |
|
466 |
tree = {} |
|
467 |
for var_name, var_type, (var_tree, var_dimension) in list: |
|
468 |
tree[var_name.upper()] = self.GenerateVariableTree(var_tree) |
|
469 |
return tree |
|
470 |
||
471 |
def IsValidVariable(self, name, context): |
|
472 |
return context is not None and context.get(name, None) is not None |
|
473 |
||
474 |
def IsCallParameter(self, name, call): |
|
475 |
if call is not None: |
|
476 |
return (call["interface"].get(name.upper(), None) is not None or |
|
477 |
call["extensible"] and EXTENSIBLE_PARAMETER.match(name.upper()) is not None) |
|
478 |
return False |
|
479 |
||
480 |
def RefreshLineFolding(self, line_number): |
|
481 |
if self.TextSyntax in ["ST", "ALL"]: |
|
482 |
level = wx.stc.STC_FOLDLEVELBASE + self.Editor.GetLineIndentation(line_number) |
|
483 |
line = self.Editor.GetLine(line_number).strip() |
|
484 |
if line == "": |
|
485 |
if line_number > 0: |
|
486 |
if LineStartswith(self.Editor.GetLine(line_number - 1).strip(), self.BlockEndKeywords): |
|
487 |
level = self.Editor.GetFoldLevel(self.Editor.GetFoldParent(line_number - 1)) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
488 |
else: |
|
489 |
level = self.Editor.GetFoldLevel(line_number - 1) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
490 |
if level != wx.stc.STC_FOLDLEVELBASE: |
|
491 |
level |= wx.stc.STC_FOLDLEVELWHITEFLAG |
|
492 |
elif LineStartswith(line, self.BlockStartKeywords): |
|
493 |
level |= wx.stc.STC_FOLDLEVELHEADERFLAG |
|
494 |
elif LineStartswith(line, self.BlockEndKeywords): |
|
495 |
if LineStartswith(self.Editor.GetLine(line_number - 1).strip(), self.BlockEndKeywords): |
|
496 |
level = self.Editor.GetFoldLevel(self.Editor.GetFoldParent(line_number - 1)) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
497 |
else: |
|
498 |
level = self.Editor.GetFoldLevel(line_number - 1) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
499 |
self.Editor.SetFoldLevel(line_number, level) |
|
500 |
||
501 |
def OnStyleNeeded(self, event): |
|
502 |
self.TextChanged = True |
|
503 |
line_number = self.Editor.LineFromPosition(self.Editor.GetEndStyled()) |
|
504 |
if line_number == 0: |
|
505 |
start_pos = last_styled_pos = 0 |
|
506 |
else: |
|
507 |
start_pos = last_styled_pos = self.Editor.GetLineEndPosition(line_number - 1) + 1 |
|
508 |
self.RefreshLineFolding(line_number) |
|
509 |
end_pos = event.GetPosition() |
|
510 |
self.StartStyling(start_pos, 0xff) |
|
511 |
||
512 |
current_context = self.Variables |
|
513 |
current_call = None |
|
514 |
||
515 |
current_pos = last_styled_pos |
|
516 |
state = SPACE |
|
517 |
line = "" |
|
518 |
word = "" |
|
519 |
while current_pos < end_pos: |
|
520 |
char = chr(self.Editor.GetCharAt(current_pos)).upper() |
|
521 |
line += char |
|
522 |
if char == NEWLINE: |
|
523 |
self.ContextStack = [] |
|
524 |
current_context = self.Variables |
|
525 |
if state == COMMENT: |
|
526 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_COMMENT) |
|
527 |
elif state == NUMBER: |
|
528 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
529 |
elif state == WORD: |
|
530 |
if word in self.Keywords or word in self.TypeNames: |
|
531 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
532 |
elif self.IsValidVariable(word, current_context): |
|
533 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
534 |
elif self.IsCallParameter(word, current_call): |
|
535 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
536 |
elif word in self.Functions: |
|
537 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
538 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
|
539 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
540 |
elif word in self.EnumeratedValues: |
|
541 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
542 |
else: |
|
543 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
544 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
|
545 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
|
546 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
|
547 |
self.StartStyling(current_pos, 0xff) |
|
548 |
else: |
|
549 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
550 |
last_styled_pos = current_pos |
|
1108
1ec5b4d244f3
Fix bug with multiline Pragma syntax highlighting in TextViewer
Laurent Bessard
parents:
1091
diff
changeset
|
551 |
if state != DPRAGMA: |
1ec5b4d244f3
Fix bug with multiline Pragma syntax highlighting in TextViewer
Laurent Bessard
parents:
1091
diff
changeset
|
552 |
state = SPACE |
814 | 553 |
line = "" |
554 |
line_number += 1 |
|
555 |
self.RefreshLineFolding(line_number) |
|
556 |
elif line.endswith("(*") and state != COMMENT: |
|
557 |
self.SetStyling(current_pos - last_styled_pos - 1, 31) |
|
558 |
last_styled_pos = current_pos |
|
559 |
if state == WORD: |
|
560 |
current_context = self.Variables |
|
561 |
state = COMMENT |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
562 |
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
|
563 |
self.SetStyling(current_pos - last_styled_pos, 31) |
814 | 564 |
last_styled_pos = current_pos |
565 |
if state == WORD: |
|
566 |
current_context = self.Variables |
|
567 |
state = PRAGMA |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
568 |
elif line.endswith("{{") and state == PRAGMA: |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
569 |
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
|
570 |
last_styled_pos = current_pos |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
571 |
state = DPRAGMA |
814 | 572 |
elif state == COMMENT: |
573 |
if line.endswith("*)"): |
|
574 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
|
575 |
last_styled_pos = current_pos + 1 |
|
576 |
state = SPACE |
|
577 |
elif state == PRAGMA: |
|
578 |
if line.endswith("}"): |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
579 |
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
|
580 |
last_styled_pos = current_pos |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
581 |
state = SPACE |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
582 |
elif state == DPRAGMA: |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
583 |
if line.endswith("}}"): |
1108
1ec5b4d244f3
Fix bug with multiline Pragma syntax highlighting in TextViewer
Laurent Bessard
parents:
1091
diff
changeset
|
584 |
self.SetStyling(current_pos - last_styled_pos + 1, 31) |
814 | 585 |
last_styled_pos = current_pos + 1 |
586 |
state = SPACE |
|
587 |
elif (line.endswith("'") or line.endswith('"')) and state not in [STRING, WSTRING]: |
|
588 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
589 |
last_styled_pos = current_pos |
|
590 |
if state == WORD: |
|
591 |
current_context = self.Variables |
|
592 |
if line.endswith("'"): |
|
593 |
state = STRING |
|
594 |
else: |
|
595 |
state = WSTRING |
|
596 |
elif state == STRING: |
|
597 |
if line.endswith("'") and not line.endswith("$'"): |
|
598 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
599 |
last_styled_pos = current_pos + 1 |
|
600 |
state = SPACE |
|
601 |
elif state == WSTRING: |
|
602 |
if line.endswith('"') and not line.endswith('$"'): |
|
603 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
604 |
last_styled_pos = current_pos + 1 |
|
605 |
state = SPACE |
|
606 |
elif char in LETTERS: |
|
607 |
if state == NUMBER: |
|
608 |
word = "#" |
|
609 |
state = WORD |
|
610 |
elif state == SPACE: |
|
611 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
612 |
word = char |
|
613 |
last_styled_pos = current_pos |
|
614 |
state = WORD |
|
615 |
else: |
|
616 |
word += char |
|
617 |
elif char in NUMBERS or char == '.' and state != WORD: |
|
618 |
if state == SPACE: |
|
619 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
620 |
last_styled_pos = current_pos |
|
621 |
state = NUMBER |
|
622 |
elif state == WORD and char != '.': |
|
623 |
word += char |
|
624 |
elif char == '(' and state == SPACE: |
|
625 |
self.CallStack.append(current_call) |
|
626 |
current_call = None |
|
627 |
else: |
|
628 |
if state == WORD: |
|
629 |
if word in self.Keywords or word in self.TypeNames: |
|
630 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
631 |
elif self.IsValidVariable(word, current_context): |
|
632 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
633 |
elif self.IsCallParameter(word, current_call): |
|
634 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
635 |
elif word in self.Functions: |
|
636 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
637 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
|
638 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
639 |
elif word in self.EnumeratedValues: |
|
640 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
641 |
else: |
|
642 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
643 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
|
644 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
|
645 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
|
646 |
self.StartStyling(current_pos, 0xff) |
|
647 |
if char == '.': |
|
648 |
if word != "]": |
|
649 |
if current_context is not None: |
|
650 |
current_context = current_context.get(word, None) |
|
651 |
else: |
|
652 |
current_context = None |
|
653 |
elif char == '(': |
|
654 |
self.CallStack.append(current_call) |
|
655 |
current_call = self.Functions.get(word, None) |
|
656 |
if current_call is None and self.IsValidVariable(word, current_context): |
|
657 |
current_call = {"interface": current_context.get(word, {}), |
|
658 |
"extensible": False} |
|
659 |
current_context = self.Variables |
|
660 |
else: |
|
894
a4919f228924
Fixed bug when indexing table in Pragma within ST/IL code
Laurent Bessard
parents:
871
diff
changeset
|
661 |
if char == '[' and current_context is not None: |
814 | 662 |
self.ContextStack.append(current_context.get(word, None)) |
663 |
current_context = self.Variables |
|
664 |
||
665 |
word = "" |
|
666 |
last_styled_pos = current_pos |
|
667 |
state = SPACE |
|
668 |
elif state == NUMBER: |
|
669 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
670 |
last_styled_pos = current_pos |
|
671 |
state = SPACE |
|
672 |
if char == ']': |
|
673 |
if len(self.ContextStack) > 0: |
|
674 |
current_context = self.ContextStack.pop() |
|
675 |
else: |
|
676 |
current_context = self.Variables |
|
677 |
word = char |
|
678 |
state = WORD |
|
679 |
elif char == ')': |
|
680 |
current_context = self.Variables |
|
681 |
if len(self.CallStack) > 0: |
|
682 |
current_call = self.CallStack.pop() |
|
683 |
else: |
|
684 |
current_call = None |
|
685 |
word = char |
|
686 |
state = WORD |
|
687 |
current_pos += 1 |
|
688 |
if state == COMMENT: |
|
689 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
|
690 |
elif state == NUMBER: |
|
691 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
692 |
elif state == WORD: |
|
693 |
if word in self.Keywords or word in self.TypeNames: |
|
694 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
695 |
elif self.IsValidVariable(word, current_context): |
|
696 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
697 |
elif self.IsCallParameter(word, current_call): |
|
698 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
699 |
elif self.TextSyntax == "IL" and word in self.Functions: |
|
700 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
701 |
elif word in self.Jumps: |
|
702 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
703 |
elif word in self.EnumeratedValues: |
|
704 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
705 |
else: |
|
706 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
707 |
else: |
|
708 |
self.SetStyling(current_pos - start_pos, 31) |
|
709 |
self.ShowHighlights(start_pos, end_pos) |
|
710 |
event.Skip() |
|
711 |
||
712 |
def OnMarginClick(self, event): |
|
713 |
if event.GetMargin() == 2: |
|
714 |
line = self.Editor.LineFromPosition(event.GetPosition()) |
|
715 |
if self.Editor.GetFoldLevel(line) & wx.stc.STC_FOLDLEVELHEADERFLAG: |
|
716 |
self.Editor.ToggleFold(line) |
|
717 |
event.Skip() |
|
1126
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
718 |
|
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
719 |
def OnUpdateUI(self, event): |
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
720 |
if self.ParentWindow: |
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
721 |
self.ParentWindow.SetCopyBuffer(self.Editor.GetSelectedText(), True) |
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
722 |
event.Skip() |
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
723 |
|
814 | 724 |
def Cut(self): |
725 |
self.ResetBuffer() |
|
726 |
self.DisableEvents = True |
|
727 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_CUT) |
|
728 |
self.DisableEvents = False |
|
729 |
self.RefreshModel() |
|
730 |
self.RefreshBuffer() |
|
731 |
||
732 |
def Copy(self): |
|
733 |
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
|
734 |
if self.ParentWindow: |
26baa0ae9fd7
Fixed bug with Copy/Paste in Primary Selection in Text Viewers
Laurent Bessard
parents:
1108
diff
changeset
|
735 |
self.ParentWindow.RefreshEditMenu() |
814 | 736 |
|
737 |
def Paste(self): |
|
738 |
self.ResetBuffer() |
|
739 |
self.DisableEvents = True |
|
740 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_PASTE) |
|
741 |
self.DisableEvents = False |
|
742 |
self.RefreshModel() |
|
743 |
self.RefreshBuffer() |
|
744 |
||
1057
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
745 |
def Search(self, criteria): |
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
746 |
return self.Controler.SearchInPou(self.TagName, criteria, self.Debug) |
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
747 |
|
814 | 748 |
def Find(self, direction, search_params): |
749 |
if self.SearchParams != search_params: |
|
750 |
self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT) |
|
751 |
||
752 |
self.SearchParams = search_params |
|
753 |
criteria = { |
|
754 |
"raw_pattern": search_params["find_pattern"], |
|
755 |
"pattern": re.compile(search_params["find_pattern"]), |
|
756 |
"case_sensitive": search_params["case_sensitive"], |
|
757 |
"regular_expression": search_params["regular_expression"], |
|
758 |
"filter": "all"} |
|
759 |
||
760 |
self.SearchResults = [ |
|
761 |
(infos[1:], start, end, SEARCH_RESULT_HIGHLIGHT) |
|
762 |
for infos, start, end, text in |
|
1057
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
763 |
self.Search(criteria)] |
3837e165b3f9
Added support for search in PythonEditor and IECCodeViewer
Laurent Bessard
parents:
980
diff
changeset
|
764 |
self.CurrentFindHighlight = None |
814 | 765 |
|
766 |
if len(self.SearchResults) > 0: |
|
767 |
if self.CurrentFindHighlight is not None: |
|
768 |
old_idx = self.SearchResults.index(self.CurrentFindHighlight) |
|
769 |
if self.SearchParams["wrap"]: |
|
770 |
idx = (old_idx + direction) % len(self.SearchResults) |
|
771 |
else: |
|
772 |
idx = max(0, min(old_idx + direction, len(self.SearchResults) - 1)) |
|
773 |
if idx != old_idx: |
|
774 |
self.RemoveHighlight(*self.CurrentFindHighlight) |
|
775 |
self.CurrentFindHighlight = self.SearchResults[idx] |
|
776 |
self.AddHighlight(*self.CurrentFindHighlight) |
|
777 |
else: |
|
778 |
self.CurrentFindHighlight = self.SearchResults[0] |
|
779 |
self.AddHighlight(*self.CurrentFindHighlight) |
|
780 |
||
781 |
else: |
|
782 |
if self.CurrentFindHighlight is not None: |
|
783 |
self.RemoveHighlight(*self.CurrentFindHighlight) |
|
784 |
self.CurrentFindHighlight = None |
|
785 |
||
786 |
def RefreshModel(self): |
|
787 |
self.RefreshJumpList() |
|
788 |
self.Controler.SetEditedElementText(self.TagName, self.GetText()) |
|
789 |
||
790 |
def OnKeyDown(self, event): |
|
791 |
if self.Controler is not None: |
|
792 |
||
793 |
if self.Editor.CallTipActive(): |
|
794 |
self.Editor.CallTipCancel() |
|
795 |
key = event.GetKeyCode() |
|
796 |
key_handled = False |
|
797 |
||
798 |
line = self.Editor.GetCurrentLine() |
|
799 |
if line == 0: |
|
800 |
start_pos = 0 |
|
801 |
else: |
|
802 |
start_pos = self.Editor.GetLineEndPosition(line - 1) + 1 |
|
803 |
end_pos = self.GetCurrentPos() |
|
804 |
lineText = self.Editor.GetTextRange(start_pos, end_pos).replace("\t", " ") |
|
805 |
||
806 |
# Code completion |
|
807 |
if key == wx.WXK_SPACE and event.ControlDown(): |
|
808 |
||
809 |
words = lineText.split(" ") |
|
810 |
words = [word for i, word in enumerate(words) if word != '' or i == len(words) - 1] |
|
811 |
||
812 |
kw = [] |
|
813 |
||
814 |
if self.TextSyntax == "IL": |
|
815 |
if len(words) == 1: |
|
816 |
kw = self.Keywords |
|
817 |
elif len(words) == 2: |
|
818 |
if words[0].upper() in ["CAL", "CALC", "CALNC"]: |
|
819 |
kw = self.Functions |
|
820 |
elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]: |
|
821 |
kw = self.Jumps |
|
822 |
else: |
|
823 |
kw = self.Variables.keys() |
|
824 |
else: |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
825 |
kw = self.Keywords + self.Variables.keys() + self.Functions.keys() |
814 | 826 |
if len(kw) > 0: |
827 |
if len(words[-1]) > 0: |
|
828 |
kw = [keyword for keyword in kw if keyword.startswith(words[-1])] |
|
829 |
kw.sort() |
|
830 |
self.Editor.AutoCompSetIgnoreCase(True) |
|
831 |
self.Editor.AutoCompShow(len(words[-1]), " ".join(kw)) |
|
832 |
key_handled = True |
|
833 |
elif key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER: |
|
834 |
if self.TextSyntax in ["ST", "ALL"]: |
|
835 |
indent = self.Editor.GetLineIndentation(line) |
|
836 |
if LineStartswith(lineText.strip(), self.BlockStartKeywords): |
|
837 |
indent = (indent / 2 + 1) * 2 |
|
838 |
self.Editor.AddText("\n" + " " * indent) |
|
839 |
key_handled = True |
|
840 |
elif key == wx.WXK_BACK: |
|
841 |
if self.TextSyntax in ["ST", "ALL"]: |
|
842 |
indent = self.Editor.GetLineIndentation(line) |
|
843 |
if lineText.strip() == "" and indent > 0: |
|
844 |
self.Editor.DelLineLeft() |
|
845 |
self.Editor.AddText(" " * ((max(0, indent - 1) / 2) * 2)) |
|
846 |
key_handled = True |
|
847 |
if not key_handled: |
|
848 |
event.Skip() |
|
849 |
||
850 |
def OnKillFocus(self, event): |
|
851 |
self.Editor.AutoCompCancel() |
|
852 |
event.Skip() |
|
853 |
||
854 |
#------------------------------------------------------------------------------- |
|
855 |
# Highlights showing functions |
|
856 |
#------------------------------------------------------------------------------- |
|
857 |
||
858 |
def OnRefreshHighlightsTimer(self, event): |
|
859 |
self.RefreshView() |
|
860 |
event.Skip() |
|
861 |
||
862 |
def ClearHighlights(self, highlight_type=None): |
|
863 |
EditorPanel.ClearHighlights(self, highlight_type) |
|
864 |
||
865 |
if highlight_type is None: |
|
866 |
self.Highlights = [] |
|
867 |
else: |
|
868 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
869 |
if highlight_type is not None: |
|
870 |
self.Highlights = [(infos, start, end, highlight) for (infos, start, end, highlight) in self.Highlights if highlight != highlight_type] |
|
871 |
self.RefreshView() |
|
872 |
||
873 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
874 |
EditorPanel.AddHighlight(self, infos, start, end, highlight_type) |
|
875 |
||
876 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
877 |
if infos[0] == "body" and highlight_type is not None: |
|
878 |
self.Highlights.append((infos[1], start, end, highlight_type)) |
|
879 |
self.Editor.GotoPos(self.Editor.PositionFromLine(start[0]) + start[1]) |
|
880 |
self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) |
|
881 |
||
882 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
883 |
EditorPanel.RemoveHighlight(self, infos, start, end, highlight_type) |
|
884 |
||
885 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
886 |
if (infos[0] == "body" and highlight_type is not None and |
|
887 |
(infos[1], start, end, highlight_type) in self.Highlights): |
|
888 |
self.Highlights.remove((infos[1], start, end, highlight_type)) |
|
889 |
self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) |
|
890 |
||
891 |
def ShowHighlights(self, start_pos, end_pos): |
|
892 |
for indent, start, end, highlight_type in self.Highlights: |
|
893 |
if start[0] == 0: |
|
894 |
highlight_start_pos = start[1] - indent |
|
895 |
else: |
|
896 |
highlight_start_pos = self.Editor.GetLineEndPosition(start[0] - 1) + start[1] - indent + 1 |
|
897 |
if end[0] == 0: |
|
898 |
highlight_end_pos = end[1] - indent + 1 |
|
899 |
else: |
|
900 |
highlight_end_pos = self.Editor.GetLineEndPosition(end[0] - 1) + end[1] - indent + 2 |
|
901 |
if highlight_start_pos < end_pos and highlight_end_pos > start_pos: |
|
902 |
self.StartStyling(highlight_start_pos, 0xff) |
|
903 |
self.SetStyling(highlight_end_pos - highlight_start_pos, highlight_type) |
|
904 |
self.StartStyling(highlight_start_pos, 0x00) |
|
905 |
self.SetStyling(len(self.Editor.GetText()) - highlight_end_pos, wx.stc.STC_STYLE_DEFAULT) |
|
906 |