andrej@1511: #!/usr/bin/env python andrej@1511: # -*- coding: utf-8 -*- andrej@1511: andrej@1511: # This file is part of Beremiz, a Integrated Development Environment for andrej@1511: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1511: # andrej@1511: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1511: # andrej@1511: # See COPYING file for copyrights details. andrej@1511: # andrej@1511: # This program is free software; you can redistribute it and/or andrej@1511: # modify it under the terms of the GNU General Public License andrej@1511: # as published by the Free Software Foundation; either version 2 andrej@1511: # of the License, or (at your option) any later version. andrej@1511: # andrej@1511: # This program is distributed in the hope that it will be useful, andrej@1511: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1511: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1511: # GNU General Public License for more details. andrej@1511: # andrej@1511: # You should have received a copy of the GNU General Public License andrej@1511: # along with this program; if not, write to the Free Software andrej@1511: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1511: andrej@1881: andrej@1881: from __future__ import absolute_import Laurent@1096: import re lbessard@145: laurent@630: import wx laurent@630: import wx.grid laurent@630: import wx.stc as stc laurent@630: import wx.lib.buttons laurent@630: Laurent@1097: from plcopen.plcopen import TestTextElement Edouard@1412: from plcopen.structures import TestIdentifier, IEC_KEYWORDS, DefaultType laurent@738: from controls import CustomGrid, CustomTable andrej@1834: from controls.CustomStyledTextCtrl import CustomStyledTextCtrl, faces, GetCursorPos, NAVIGATION_KEYS Laurent@1096: from editors.ConfTreeNodeEditor import ConfTreeNodeEditor Laurent@814: from util.BitmapLibrary import GetBitmap andrej@1834: from util.TranslationCatalogs import NoTranslate Laurent@1097: from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD andrej@1834: lbessard@145: Edouard@1412: [STC_CODE_ERROR, STC_CODE_SEARCH_RESULT, Laurent@1104: STC_CODE_SECTION] = range(15, 18) Laurent@1097: Laurent@1097: HIGHLIGHT_TYPES = { Laurent@1097: ERROR_HIGHLIGHT: STC_CODE_ERROR, Laurent@1097: SEARCH_RESULT_HIGHLIGHT: STC_CODE_SEARCH_RESULT, Laurent@1097: } Laurent@1097: Laurent@1161: EDGE_COLUMN = 80 Laurent@1161: andrej@1736: Laurent@1096: class CodeEditor(CustomStyledTextCtrl): Edouard@1412: Laurent@1096: KEYWORDS = [] Laurent@1096: COMMENT_HEADER = "" Laurent@1096: Laurent@1096: def __init__(self, parent, window, controler): Edouard@1412: CustomStyledTextCtrl.__init__(self, parent, -1, wx.DefaultPosition, andrej@1768: wx.Size(-1, 300), 0) Edouard@1412: lbessard@145: self.SetMarginType(1, stc.STC_MARGIN_NUMBER) lbessard@145: self.SetMarginWidth(1, 25) lbessard@145: lbessard@145: self.SetProperty("fold", "1") lbessard@145: self.SetProperty("tab.timmy.whinge.level", "1") andrej@1740: self.SetMargins(0, 0) lbessard@145: lbessard@145: self.SetViewWhiteSpace(False) Edouard@1412: lbessard@145: self.SetEdgeMode(stc.STC_EDGE_BACKGROUND) Laurent@1161: self.SetEdgeColumn(EDGE_COLUMN) lbessard@145: lbessard@145: # Setup a margin to hold fold markers lbessard@145: self.SetMarginType(2, stc.STC_MARGIN_SYMBOL) lbessard@145: self.SetMarginMask(2, stc.STC_MASK_FOLDERS) lbessard@145: self.SetMarginSensitive(2, True) lbessard@145: self.SetMarginWidth(2, 12) lbessard@145: Laurent@1096: # Like a flattened tree control using square headers Laurent@1096: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_BOXMINUS, "white", "#808080") Laurent@1096: self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_BOXPLUS, "white", "#808080") Laurent@1096: self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_VLINE, "white", "#808080") Laurent@1096: self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_LCORNER, "white", "#808080") Laurent@1096: self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_BOXPLUSCONNECTED, "white", "#808080") Laurent@1096: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_BOXMINUSCONNECTED, "white", "#808080") Laurent@1096: self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNER, "white", "#808080") Edouard@1412: lbessard@145: self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI) lbessard@145: self.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick) lbessard@145: self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed) lbessard@145: lbessard@145: # Make some styles, The lexer defines what each style is used for, we lbessard@145: # just have to define what each style looks like. This set is adapted from lbessard@145: # Scintilla sample property files. lbessard@145: lbessard@145: # Global default styles for all languages lbessard@145: self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces) lbessard@145: self.StyleClearAll() # Reset all to be like the default lbessard@145: lbessard@145: # Global default styles for all languages lbessard@145: self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces) Laurent@1066: self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size)d" % faces) Laurent@1104: self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(mono)s" % faces) lbessard@145: self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold") lbessard@145: self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold") Edouard@1412: Laurent@1097: # Highlighting styles Laurent@1097: self.StyleSetSpec(STC_CODE_ERROR, 'fore:#FF0000,back:#FFFF00,size:%(size)d' % faces) Laurent@1097: self.StyleSetSpec(STC_CODE_SEARCH_RESULT, 'fore:#FFFFFF,back:#FFA500,size:%(size)d' % faces) Edouard@1412: Laurent@1104: # Section style Laurent@1104: self.StyleSetSpec(STC_CODE_SECTION, 'fore:#808080,size:%(size)d') Laurent@1104: self.StyleSetChangeable(STC_CODE_SECTION, False) Edouard@1412: lbessard@145: # Indentation size Laurent@1161: self.SetTabWidth(4) lbessard@145: self.SetUseTabs(0) Edouard@1412: Laurent@1096: self.SetCodeLexer() Laurent@1096: self.SetKeyWords(0, " ".join(self.KEYWORDS)) Edouard@1412: lbessard@145: self.Controler = controler lbessard@145: self.ParentWindow = window Edouard@1412: lbessard@145: self.DisableEvents = True lbessard@145: self.CurrentAction = None Edouard@1412: Laurent@1178: self.ResetSearchResults() Edouard@1412: Laurent@1097: self.RefreshHighlightsTimer = wx.Timer(self, -1) Laurent@1097: self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer) Edouard@1412: Laurent@1096: self.SectionsComments = {} Laurent@1124: for section in self.Controler.SECTIONS_NAMES: Laurent@1110: section_comment = " %s section " % (section) Laurent@1161: len_headers = EDGE_COLUMN - len(section_comment) andrej@1767: section_comment = \ andrej@1767: self.COMMENT_HEADER * (len_headers / 2) + \ andrej@1767: section_comment + \ andrej@1767: self.COMMENT_HEADER * (len_headers - len_headers / 2) Edouard@1412: Laurent@1096: self.SectionsComments[section] = { andrej@1878: "comment": section_comment, Laurent@1096: } Edouard@1412: Laurent@1124: for i, section in enumerate(self.Controler.SECTIONS_NAMES): Laurent@1110: section_infos = self.SectionsComments[section] Laurent@1124: if i + 1 < len(self.Controler.SECTIONS_NAMES): Laurent@1124: section_end = self.SectionsComments[ Laurent@1124: self.Controler.SECTIONS_NAMES[i + 1]]["comment"] Laurent@1110: else: Laurent@1110: section_end = "$" Laurent@1110: section_infos["pattern"] = re.compile( Edouard@1412: section_infos["comment"] + "(.*)" + Laurent@1110: section_end, re.DOTALL) Edouard@1412: andrej@1745: self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT | wx.stc.STC_MOD_BEFOREDELETE) lbessard@145: Laurent@1096: self.Bind(wx.stc.EVT_STC_DO_DROP, self.OnDoDrop) lbessard@145: self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus) Laurent@1096: self.Bind(wx.stc.EVT_STC_MODIFIED, self.OnModification) Edouard@1412: Laurent@1096: def SetCodeLexer(self): Laurent@1096: pass Edouard@1412: Laurent@1178: def ResetSearchResults(self): Laurent@1178: self.Highlights = [] Laurent@1178: self.SearchParams = None Laurent@1178: self.SearchResults = None Laurent@1178: self.CurrentFindHighlight = None Edouard@1412: lbessard@145: def OnModification(self, event): lbessard@145: if not self.DisableEvents: lbessard@145: mod_type = event.GetModificationType() andrej@1745: if not (mod_type & wx.stc.STC_PERFORMED_UNDO or mod_type & wx.stc.STC_PERFORMED_REDO): andrej@1745: if mod_type & wx.stc.STC_MOD_BEFOREINSERT: andrej@1743: if self.CurrentAction is None: lbessard@145: self.StartBuffering() lbessard@145: elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1: lbessard@145: self.Controler.EndBuffering() lbessard@145: self.StartBuffering() lbessard@145: self.CurrentAction = ("Add", event.GetPosition()) laurent@658: wx.CallAfter(self.RefreshModel) andrej@1745: elif mod_type & wx.stc.STC_MOD_BEFOREDELETE: andrej@1743: if self.CurrentAction is None: lbessard@145: self.StartBuffering() lbessard@145: elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1: lbessard@145: self.Controler.EndBuffering() lbessard@145: self.StartBuffering() lbessard@145: self.CurrentAction = ("Delete", event.GetPosition()) laurent@658: wx.CallAfter(self.RefreshModel) Laurent@1104: wx.CallAfter(self.RefreshSectionStyling) lbessard@145: event.Skip() Edouard@1412: lbessard@145: def OnDoDrop(self, event): Laurent@1138: try: Laurent@1138: values = eval(event.GetDragText()) andrej@1780: except Exception: Laurent@1138: values = event.GetDragText() Laurent@1138: if isinstance(values, tuple): Laurent@1138: if values[3] == self.Controler.GetCurrentLocation(): Laurent@1138: self.ResetBuffer() Laurent@1138: event.SetDragText(values[0]) Laurent@1138: wx.CallAfter(self.RefreshModel) Laurent@1138: else: Laurent@1138: event.SetDragText("") Laurent@1138: else: Laurent@1138: self.ResetBuffer() Laurent@1138: wx.CallAfter(self.RefreshModel) lbessard@145: event.Skip() lbessard@145: lbessard@145: # Buffer the last model state lbessard@145: def RefreshBuffer(self): Laurent@1096: self.Controler.BufferCodeFile() laurent@658: if self.ParentWindow is not None: lbessard@145: self.ParentWindow.RefreshTitle() Edouard@587: self.ParentWindow.RefreshFileMenu() lbessard@145: self.ParentWindow.RefreshEditMenu() laurent@630: self.ParentWindow.RefreshPageTitles() Edouard@1412: lbessard@145: def StartBuffering(self): lbessard@145: self.Controler.StartBuffering() laurent@658: if self.ParentWindow is not None: lbessard@145: self.ParentWindow.RefreshTitle() Edouard@587: self.ParentWindow.RefreshFileMenu() lbessard@145: self.ParentWindow.RefreshEditMenu() laurent@630: self.ParentWindow.RefreshPageTitles() Edouard@1412: lbessard@145: def ResetBuffer(self): andrej@1743: if self.CurrentAction is not None: lbessard@145: self.Controler.EndBuffering() lbessard@145: self.CurrentAction = None lbessard@145: Laurent@1096: def GetCodeText(self): Laurent@1096: parts = self.Controler.GetTextParts() Laurent@1096: text = "" Laurent@1124: for section in self.Controler.SECTIONS_NAMES: Laurent@1096: section_comments = self.SectionsComments[section] Laurent@1110: text += section_comments["comment"] Laurent@1117: if parts[section] == "": Laurent@1104: text += "\n" Laurent@1117: else: Laurent@1117: if not parts[section].startswith("\n"): Laurent@1117: text += "\n" Laurent@1117: text += parts[section] Laurent@1117: if not parts[section].endswith("\n"): Laurent@1117: text += "\n" Laurent@1096: return text Laurent@1096: Laurent@1101: def RefreshView(self, scroll_to_highlight=False): lbessard@145: self.ResetBuffer() lbessard@145: self.DisableEvents = True lbessard@145: old_cursor_pos = self.GetCurrentPos() Laurent@1060: line = self.GetFirstVisibleLine() Laurent@1060: column = self.GetXOffset() lbessard@145: old_text = self.GetText() Laurent@1096: new_text = self.GetCodeText() Laurent@1060: if old_text != new_text: Laurent@1101: self.SetText(new_text) Laurent@1060: new_cursor_pos = GetCursorPos(old_text, new_text) Laurent@1060: self.LineScroll(column, line) andrej@1743: if new_cursor_pos is not None: Laurent@1060: self.GotoPos(new_cursor_pos) Laurent@1060: else: Laurent@1060: self.GotoPos(old_cursor_pos) Laurent@1060: self.EmptyUndoBuffer() lbessard@145: self.DisableEvents = False Edouard@1412: Laurent@1104: self.RefreshSectionStyling() Edouard@1412: Laurent@1104: self.ShowHighlights() Edouard@1412: Laurent@1104: def RefreshSectionStyling(self): lbessard@145: self.Colourise(0, -1) Edouard@1412: Laurent@1104: text = self.GetText() Laurent@1104: for line in xrange(self.GetLineCount()): Laurent@1104: self.SetLineState(line, 0) Edouard@1412: Laurent@1124: for section in self.Controler.SECTIONS_NAMES: Laurent@1104: section_comments = self.SectionsComments[section] Laurent@1110: start_pos = text.find(section_comments["comment"]) Laurent@1110: end_pos = start_pos + len(section_comments["comment"]) Laurent@1104: self.StartStyling(start_pos, 0xff) Laurent@1110: self.SetStyling(end_pos - start_pos, STC_CODE_SECTION) Laurent@1110: self.SetLineState(self.LineFromPosition(start_pos), 1) Edouard@1412: Laurent@1110: self.StartStyling(end_pos, 0x00) Laurent@1110: self.SetStyling(len(self.GetText()) - end_pos, stc.STC_STYLE_DEFAULT) lbessard@145: laurent@630: def DoGetBestSize(self): laurent@630: return self.ParentWindow.GetPanelBestSize() laurent@630: lbessard@145: def RefreshModel(self): Laurent@1096: text = self.GetText() Laurent@1096: parts = {} Laurent@1124: for section in self.Controler.SECTIONS_NAMES: Laurent@1096: section_comments = self.SectionsComments[section] Laurent@1096: result = section_comments["pattern"].search(text) Laurent@1096: if result is not None: Laurent@1096: parts[section] = result.group(1) Laurent@1096: else: Laurent@1096: parts[section] = "" Laurent@1096: self.Controler.SetTextParts(parts) Laurent@1178: self.ResetSearchResults() Edouard@1412: lbessard@145: def OnKeyPressed(self, event): lbessard@145: if self.CallTipActive(): lbessard@145: self.CallTipCancel() lbessard@145: key = event.GetKeyCode() Laurent@1124: current_pos = self.GetCurrentPos() Laurent@1124: selected = self.GetSelection() Laurent@1124: text_selected = selected[0] != selected[1] Edouard@1412: Laurent@1139: # Test if caret is before Windows like new line Laurent@1139: text = self.GetText() Laurent@1150: if current_pos < len(text) and ord(text[current_pos]) == 13: Laurent@1139: newline_size = 2 Laurent@1139: else: Laurent@1139: newline_size = 1 Edouard@1412: Laurent@1124: # Disable to type any character in section header lines andrej@1878: if self.GetLineState(self.LineFromPosition(current_pos)) and \ andrej@1878: not text_selected and \ andrej@1878: key not in NAVIGATION_KEYS + [wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER]: Laurent@1104: return Edouard@1412: Laurent@1124: # Disable to delete line between code and header lines Laurent@1124: elif (self.GetCurLine()[0].strip() != "" and not text_selected and Laurent@1124: (key == wx.WXK_BACK and Laurent@1124: self.GetLineState(self.LineFromPosition(max(0, current_pos - 1))) or Laurent@1124: key in [wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE] and Laurent@1139: self.GetLineState(self.LineFromPosition(min(len(text), current_pos + newline_size))))): Laurent@1124: return Edouard@1412: Laurent@1104: elif key == 32 and event.ControlDown(): lbessard@145: # Tips lbessard@145: if event.ShiftDown(): lbessard@145: pass lbessard@145: # Code completion lbessard@145: else: lbessard@145: self.AutoCompSetIgnoreCase(False) # so this needs to match Edouard@1412: Laurent@1136: keywords = self.KEYWORDS + [var["Name"] Laurent@1136: for var in self.Controler.GetVariables()] Laurent@1136: keywords.sort() Laurent@1136: self.AutoCompShow(0, " ".join(keywords)) lbessard@145: else: lbessard@145: event.Skip() lbessard@145: lbessard@145: def OnKillFocus(self, event): lbessard@145: self.AutoCompCancel() lbessard@145: event.Skip() lbessard@145: Laurent@1126: def OnUpdateUI(self, event): lbessard@145: # check for matching braces lbessard@145: braceAtCaret = -1 lbessard@145: braceOpposite = -1 lbessard@145: charBefore = None lbessard@145: caretPos = self.GetCurrentPos() lbessard@145: lbessard@145: if caretPos > 0: lbessard@145: charBefore = self.GetCharAt(caretPos - 1) lbessard@145: styleBefore = self.GetStyleAt(caretPos - 1) lbessard@145: lbessard@145: # check before lbessard@145: if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR: lbessard@145: braceAtCaret = caretPos - 1 lbessard@145: lbessard@145: # check after lbessard@145: if braceAtCaret < 0: lbessard@145: charAfter = self.GetCharAt(caretPos) lbessard@145: styleAfter = self.GetStyleAt(caretPos) lbessard@145: lbessard@145: if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR: lbessard@145: braceAtCaret = caretPos lbessard@145: lbessard@145: if braceAtCaret >= 0: lbessard@145: braceOpposite = self.BraceMatch(braceAtCaret) lbessard@145: andrej@1774: if braceAtCaret != -1 and braceOpposite == -1: lbessard@145: self.BraceBadLight(braceAtCaret) lbessard@145: else: lbessard@145: self.BraceHighlight(braceAtCaret, braceOpposite) Edouard@1412: Laurent@1146: selected_text = self.GetSelectedText() Laurent@1146: if selected_text: Laurent@1146: self.ParentWindow.SetCopyBuffer(selected_text, True) Edouard@1412: Laurent@1126: def OnMarginClick(self, event): lbessard@145: # fold and unfold as needed andrej@1872: if event.GetMargin() == 2: andrej@1872: if event.GetShift() and event.GetControl(): lbessard@145: self.FoldAll() lbessard@145: else: andrej@1872: lineClicked = self.LineFromPosition(event.GetPosition()) lbessard@145: lbessard@145: if self.GetFoldLevel(lineClicked) & stc.STC_FOLDLEVELHEADERFLAG: andrej@1872: if event.GetShift(): lbessard@145: self.SetFoldExpanded(lineClicked, True) lbessard@145: self.Expand(lineClicked, True, True, 1) andrej@1872: elif event.GetControl(): lbessard@145: if self.GetFoldExpanded(lineClicked): lbessard@145: self.SetFoldExpanded(lineClicked, False) lbessard@145: self.Expand(lineClicked, False, True, 0) lbessard@145: else: lbessard@145: self.SetFoldExpanded(lineClicked, True) lbessard@145: self.Expand(lineClicked, True, True, 100) lbessard@145: else: lbessard@145: self.ToggleFold(lineClicked) Laurent@1126: event.Skip() lbessard@145: lbessard@145: def FoldAll(self): lbessard@145: lineCount = self.GetLineCount() lbessard@145: expanding = True lbessard@145: lbessard@145: # find out if we are folding or unfolding lbessard@145: for lineNum in range(lineCount): lbessard@145: if self.GetFoldLevel(lineNum) & stc.STC_FOLDLEVELHEADERFLAG: lbessard@145: expanding = not self.GetFoldExpanded(lineNum) lbessard@145: break lbessard@145: lbessard@145: lineNum = 0 lbessard@145: lbessard@145: while lineNum < lineCount: lbessard@145: level = self.GetFoldLevel(lineNum) lbessard@145: if level & stc.STC_FOLDLEVELHEADERFLAG and \ lbessard@145: (level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE: lbessard@145: lbessard@145: if expanding: lbessard@145: self.SetFoldExpanded(lineNum, True) lbessard@145: lineNum = self.Expand(lineNum, True) lbessard@145: lineNum = lineNum - 1 lbessard@145: else: lbessard@145: lastChild = self.GetLastChild(lineNum, -1) lbessard@145: self.SetFoldExpanded(lineNum, False) lbessard@145: lbessard@145: if lastChild > lineNum: lbessard@145: self.HideLines(lineNum+1, lastChild) lbessard@145: lbessard@145: lineNum = lineNum + 1 lbessard@145: lbessard@145: def Expand(self, line, doExpand, force=False, visLevels=0, level=-1): lbessard@145: lastChild = self.GetLastChild(line, level) lbessard@145: line = line + 1 lbessard@145: lbessard@145: while line <= lastChild: lbessard@145: if force: lbessard@145: if visLevels > 0: lbessard@145: self.ShowLines(line, line) lbessard@145: else: lbessard@145: self.HideLines(line, line) lbessard@145: else: lbessard@145: if doExpand: lbessard@145: self.ShowLines(line, line) lbessard@145: lbessard@145: if level == -1: lbessard@145: level = self.GetFoldLevel(line) lbessard@145: lbessard@145: if level & stc.STC_FOLDLEVELHEADERFLAG: lbessard@145: if force: lbessard@145: if visLevels > 1: lbessard@145: self.SetFoldExpanded(line, True) lbessard@145: else: lbessard@145: self.SetFoldExpanded(line, False) lbessard@145: lbessard@145: line = self.Expand(line, doExpand, force, visLevels-1) lbessard@145: lbessard@145: else: lbessard@145: if doExpand and self.GetFoldExpanded(line): lbessard@145: line = self.Expand(line, True, force, visLevels-1) lbessard@145: else: lbessard@145: line = self.Expand(line, False, force, visLevels-1) lbessard@145: else: lbessard@145: line = line + 1 lbessard@145: lbessard@145: return line lbessard@145: laurent@637: def Cut(self): laurent@637: self.ResetBuffer() laurent@637: self.DisableEvents = True laurent@637: self.CmdKeyExecute(wx.stc.STC_CMD_CUT) laurent@637: self.DisableEvents = False laurent@637: self.RefreshModel() laurent@637: self.RefreshBuffer() Edouard@1412: laurent@637: def Copy(self): laurent@637: self.CmdKeyExecute(wx.stc.STC_CMD_COPY) Laurent@1124: self.ParentWindow.RefreshEditMenu() Edouard@1412: laurent@637: def Paste(self): laurent@637: self.ResetBuffer() laurent@637: self.DisableEvents = True laurent@637: self.CmdKeyExecute(wx.stc.STC_CMD_PASTE) laurent@637: self.DisableEvents = False laurent@637: self.RefreshModel() laurent@637: self.RefreshBuffer() laurent@637: Laurent@1097: def Find(self, direction, search_params): Laurent@1097: if self.SearchParams != search_params: Laurent@1097: self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT) Edouard@1412: Laurent@1097: self.SearchParams = search_params Edouard@1412: Laurent@1097: self.SearchResults = [ Laurent@1097: (start, end, SEARCH_RESULT_HIGHLIGHT) andrej@1847: for start, end, _text in surkovsv93@1556: TestTextElement(self.GetText(), search_params)] Laurent@1097: self.CurrentFindHighlight = None Edouard@1412: Laurent@1097: if len(self.SearchResults) > 0: Laurent@1097: if self.CurrentFindHighlight is not None: Laurent@1097: old_idx = self.SearchResults.index(self.CurrentFindHighlight) Laurent@1097: if self.SearchParams["wrap"]: Laurent@1097: idx = (old_idx + direction) % len(self.SearchResults) Laurent@1097: else: Laurent@1097: idx = max(0, min(old_idx + direction, len(self.SearchResults) - 1)) Laurent@1097: if idx != old_idx: Laurent@1097: self.RemoveHighlight(*self.CurrentFindHighlight) Laurent@1097: self.CurrentFindHighlight = self.SearchResults[idx] Laurent@1097: self.AddHighlight(*self.CurrentFindHighlight) Laurent@1097: else: Laurent@1178: caret_pos = self.GetCurrentPos() Laurent@1178: if self.SearchParams["wrap"]: Laurent@1178: self.CurrentFindHighlight = self.SearchResults[0] Laurent@1178: else: Laurent@1178: self.CurrentFindHighlight = None Laurent@1178: for start, end, highlight_type in self.SearchResults: Laurent@1178: if start[0] == 0: Laurent@1178: highlight_start_pos = start[1] Laurent@1178: else: Laurent@1178: highlight_start_pos = self.GetLineEndPosition(start[0] - 1) + start[1] + 1 Laurent@1178: if highlight_start_pos >= caret_pos: Laurent@1178: self.CurrentFindHighlight = (start, end, highlight_type) Laurent@1178: break Laurent@1178: if self.CurrentFindHighlight is not None: Laurent@1178: self.AddHighlight(*self.CurrentFindHighlight) Edouard@1412: Laurent@1101: self.ScrollToLine(self.CurrentFindHighlight[0][0]) Edouard@1412: Laurent@1097: else: Laurent@1097: if self.CurrentFindHighlight is not None: Laurent@1097: self.RemoveHighlight(*self.CurrentFindHighlight) Laurent@1097: self.CurrentFindHighlight = None Laurent@1097: andrej@1782: # ------------------------------------------------------------------------------- andrej@1782: # Highlights showing functions andrej@1782: # ------------------------------------------------------------------------------- Laurent@1097: Laurent@1097: def OnRefreshHighlightsTimer(self, event): Laurent@1101: self.RefreshView(True) Laurent@1097: event.Skip() Laurent@1097: Laurent@1097: def ClearHighlights(self, highlight_type=None): Laurent@1097: if highlight_type is None: Laurent@1097: self.Highlights = [] Laurent@1097: else: Laurent@1097: highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) Laurent@1097: if highlight_type is not None: Laurent@1097: self.Highlights = [(start, end, highlight) for (start, end, highlight) in self.Highlights if highlight != highlight_type] Laurent@1097: self.RefreshView() Laurent@1097: Laurent@1097: def AddHighlight(self, start, end, highlight_type): Laurent@1097: highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) Laurent@1097: if highlight_type is not None: Laurent@1097: self.Highlights.append((start, end, highlight_type)) Laurent@1097: self.GotoPos(self.PositionFromLine(start[0]) + start[1]) Laurent@1097: self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) Laurent@1097: self.RefreshView() Laurent@1097: Laurent@1097: def RemoveHighlight(self, start, end, highlight_type): Laurent@1097: highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) andrej@1766: if highlight_type is not None and \ andrej@1766: (start, end, highlight_type) in self.Highlights: Laurent@1097: self.Highlights.remove((start, end, highlight_type)) Laurent@1097: self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) Edouard@1412: Laurent@1097: def ShowHighlights(self): Laurent@1097: for start, end, highlight_type in self.Highlights: Laurent@1097: if start[0] == 0: Laurent@1097: highlight_start_pos = start[1] Laurent@1097: else: Laurent@1097: highlight_start_pos = self.GetLineEndPosition(start[0] - 1) + start[1] + 1 Laurent@1097: if end[0] == 0: Laurent@1178: highlight_end_pos = end[1] + 1 Laurent@1097: else: Laurent@1097: highlight_end_pos = self.GetLineEndPosition(end[0] - 1) + end[1] + 2 Laurent@1097: self.StartStyling(highlight_start_pos, 0xff) Laurent@1097: self.SetStyling(highlight_end_pos - highlight_start_pos, highlight_type) Laurent@1178: self.StartStyling(highlight_end_pos, 0x00) Laurent@1097: self.SetStyling(len(self.GetText()) - highlight_end_pos, stc.STC_STYLE_DEFAULT) Laurent@1097: lbessard@145: andrej@1782: # ------------------------------------------------------------------------------- lbessard@145: # Helper for VariablesGrid values andrej@1782: # ------------------------------------------------------------------------------- lbessard@145: Edouard@2226: class AllGridCellEditor(wx.grid.GridCellTextEditor): Edouard@2226: def __init__(self, table, row, col): Edouard@2226: wx.grid.GridCellTextEditor.__init__(self) Edouard@2226: Edouard@2249: Edouard@2226: class ClassGridCellEditor(wx.grid.GridCellChoiceEditor): Edouard@2226: def __init__(self, table, row, col): Edouard@2226: wx.grid.GridCellChoiceEditor.__init__(self) Edouard@2226: self.SetParameters("input,memory,output") Edouard@2226: Edouard@2226: laurent@651: class VariablesTable(CustomTable): Edouard@2226: __defaultColumnType = dict( Edouard@2249: [(name, AllGridCellEditor) for name in Edouard@2249: ["Name", "Initial", "Description", "OnChange", "Options"]] + Edouard@2249: [('Class', ClassGridCellEditor), ('Type', None)]) Edouard@2226: Edouard@2226: def __init__(self, *args, **kwargs): Edouard@2226: my_columns = kwargs.pop("additional_columns") Edouard@2226: super(VariablesTable, self).__init__(*args, **kwargs) Edouard@2226: self.columnTypes = dict(self.__defaultColumnType) Edouard@2226: if my_columns is not None: Edouard@2226: for key in my_columns.keys(): Edouard@2226: if key in self.columnTypes.keys(): Edouard@2226: self.columnTypes[key] = my_columns[key] Edouard@1412: lbessard@145: def GetValue(self, row, col): lbessard@145: if row < self.GetNumberRows(): lbessard@145: if col == 0: lbessard@145: return row + 1 lbessard@145: else: andrej@1699: return unicode(self.data[row].get(self.GetColLabelValue(col, False), "")) Edouard@1412: lbessard@145: def _updateColAttrs(self, grid): lbessard@145: """ lbessard@145: wxGrid -> update the column attributes to add the lbessard@145: appropriate renderer given the column name. lbessard@145: lbessard@145: Otherwise default to the default renderer. lbessard@145: """ Edouard@1412: lbessard@145: for row in range(self.GetNumberRows()): lbessard@145: for col in range(self.GetNumberCols()): lbessard@145: editor = None lbessard@145: renderer = None laurent@665: colname = self.GetColLabelValue(col, False) Edouard@1412: Edouard@2226: editortype = self.columnTypes.get(colname, None) Edouard@2226: if editortype is not None: Edouard@2226: editor = editortype(self, row, col) Edouard@1412: lbessard@145: grid.SetCellEditor(row, col, editor) lbessard@145: grid.SetCellRenderer(row, col, renderer) Edouard@1412: lbessard@145: grid.SetCellBackgroundColour(row, col, wx.WHITE) laurent@651: self.ResizeRow(grid, row) laurent@848: lbessard@145: lbessard@145: class VariablesEditor(wx.Panel): Edouard@1412: Edouard@2226: def __init__(self, parent, window, controler, additional_columns=None): laurent@848: wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL) Edouard@1412: Laurent@1161: main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=4) Laurent@1161: main_sizer.AddGrowableCol(1) Laurent@1161: main_sizer.AddGrowableRow(0) Edouard@1412: Laurent@1161: controls_sizer = wx.BoxSizer(wx.VERTICAL) Laurent@1161: main_sizer.AddSizer(controls_sizer, border=5, flag=wx.ALL) Edouard@1412: laurent@848: for name, bitmap, help in [ laurent@848: ("AddVariableButton", "add_element", _("Add variable")), laurent@848: ("DeleteVariableButton", "remove_element", _("Remove variable")), laurent@848: ("UpVariableButton", "up", _("Move variable up")), laurent@848: ("DownVariableButton", "down", _("Move variable down"))]: Edouard@1412: button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), andrej@1768: size=wx.Size(28, 28), style=wx.NO_BORDER) laurent@848: button.SetToolTipString(help) laurent@848: setattr(self, name, button) Laurent@1161: controls_sizer.AddWindow(button, border=5, flag=wx.BOTTOM) Edouard@1412: Laurent@1152: self.VariablesGrid = CustomGrid(self, style=wx.VSCROLL) Laurent@1097: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnVariablesGridCellChange) Laurent@1097: self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnVariablesGridCellLeftClick) Laurent@1097: self.VariablesGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN, self.OnVariablesGridEditorShown) Laurent@1097: main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW) Edouard@1412: laurent@848: self.SetSizer(main_sizer) Edouard@1412: lbessard@145: self.ParentWindow = window lbessard@145: self.Controler = controler Edouard@1412: andrej@1740: self.VariablesDefaultValue = { surkovsv93@1969: "Name": "LocalVar0", andrej@1740: "Type": DefaultType, andrej@1740: "Initial": "", andrej@1740: "Description": "", andrej@1740: "OnChange": "", andrej@1740: "Options": "" andrej@1740: } Edouard@2226: Edouard@2226: self.Table = VariablesTable(self, [], self.GetVariableTableColnames(), additional_columns=additional_columns) Edouard@1448: self.ColAlignements = [wx.ALIGN_RIGHT] + \ Edouard@1448: [wx.ALIGN_LEFT]*(len(self.VariablesDefaultValue)) andrej@1495: self.ColSizes = [20, 150] + [130]*(len(self.VariablesDefaultValue)-1) lbessard@145: self.VariablesGrid.SetTable(self.Table) laurent@626: self.VariablesGrid.SetButtons({"Add": self.AddVariableButton, laurent@626: "Delete": self.DeleteVariableButton, laurent@626: "Up": self.UpVariableButton, laurent@626: "Down": self.DownVariableButton}) Edouard@1412: laurent@626: def _AddVariable(new_row): Laurent@1146: if new_row > 0: Laurent@1146: row_content = self.Table.data[new_row - 1].copy() surkovsv93@1969: old_name = row_content['Name'] surkovsv93@1969: row_content['Name'] =\ surkovsv93@1969: self.Controler.GenerateNewName(old_name, old_name+'%d') Laurent@1146: else: Laurent@1146: row_content = self.VariablesDefaultValue.copy() Laurent@1146: self.Table.InsertRow(new_row, row_content) laurent@626: self.RefreshModel() laurent@626: self.RefreshView() laurent@626: return new_row laurent@626: setattr(self.VariablesGrid, "_AddRow", _AddVariable) Edouard@1412: laurent@626: def _DeleteVariable(row): laurent@626: self.Table.RemoveRow(row) laurent@626: self.RefreshModel() laurent@626: self.RefreshView() laurent@626: setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable) Edouard@1412: laurent@626: def _MoveVariable(row, move): laurent@626: new_row = self.Table.MoveRow(row, move) laurent@626: if new_row != row: laurent@626: self.RefreshModel() laurent@626: self.RefreshView() laurent@626: return new_row laurent@626: setattr(self.VariablesGrid, "_MoveRow", _MoveVariable) Edouard@1412: lbessard@145: self.VariablesGrid.SetRowLabelSize(0) lbessard@145: for col in range(self.Table.GetNumberCols()): lbessard@145: attr = wx.grid.GridCellAttr() lbessard@145: attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE) lbessard@145: self.VariablesGrid.SetColAttr(col, attr) lbessard@145: self.VariablesGrid.SetColSize(col, self.ColSizes[col]) lbessard@145: self.Table.ResetView(self.VariablesGrid) lbessard@145: andrej@1704: def GetVariableTableColnames(self): andrej@1762: _ = NoTranslate andrej@1772: return ["#", andrej@1772: _("Name"), andrej@1772: _("Type"), andrej@1772: _("Initial"), andrej@1772: _("Description"), andrej@1772: _("OnChange"), andrej@1772: _("Options")] andrej@1704: lbessard@145: def RefreshModel(self): lbessard@145: self.Controler.SetVariables(self.Table.GetData()) lbessard@145: self.RefreshBuffer() Edouard@1412: lbessard@145: # Buffer the last model state lbessard@145: def RefreshBuffer(self): Laurent@1096: self.Controler.BufferCodeFile() lbessard@145: self.ParentWindow.RefreshTitle() Edouard@587: self.ParentWindow.RefreshFileMenu() lbessard@145: self.ParentWindow.RefreshEditMenu() laurent@630: self.ParentWindow.RefreshPageTitles() lbessard@145: lbessard@145: def RefreshView(self): lbessard@145: self.Table.SetData(self.Controler.GetVariables()) lbessard@145: self.Table.ResetView(self.VariablesGrid) laurent@626: self.VariablesGrid.RefreshButtons() Edouard@1412: laurent@630: def DoGetBestSize(self): laurent@630: return self.ParentWindow.GetPanelBestSize() Edouard@1412: andrej@1658: def ShowErrorMessage(self, message): andrej@1745: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) andrej@1658: dialog.ShowModal() andrej@1658: dialog.Destroy() andrej@1658: lbessard@145: def OnVariablesGridCellChange(self, event): Laurent@1146: row, col = event.GetRow(), event.GetCol() Laurent@1146: colname = self.Table.GetColLabelValue(col, False) Laurent@1146: value = self.Table.GetValue(row, col) Laurent@1146: message = None Edouard@1412: Laurent@1146: if colname == "Name" and value != "": Laurent@1146: if not TestIdentifier(value): Laurent@1146: message = _("\"%s\" is not a valid identifier!") % value Laurent@1146: elif value.upper() in IEC_KEYWORDS: Laurent@1146: message = _("\"%s\" is a keyword. It can't be used!") % value Edouard@1412: elif value.upper() in [var["Name"].upper() Edouard@1412: for var_row, var in enumerate(self.Table.data) Laurent@1146: if var_row != row]: Laurent@1146: message = _("A variable with \"%s\" as name already exists!") % value Laurent@1146: else: Laurent@1146: self.RefreshModel() Laurent@1146: wx.CallAfter(self.RefreshView) Laurent@1146: else: Laurent@1146: self.RefreshModel() Laurent@1146: wx.CallAfter(self.RefreshView) Edouard@1412: Laurent@1146: if message is not None: Laurent@1146: event.Veto() andrej@1658: wx.CallAfter(self.ShowErrorMessage, message) Laurent@1146: else: Laurent@1146: event.Skip() lbessard@145: lbessard@145: def OnVariablesGridEditorShown(self, event): Edouard@1412: row, col = event.GetRow(), event.GetCol() laurent@801: if self.Table.GetColLabelValue(col, False) == "Type": lbessard@145: type_menu = wx.Menu(title='') lbessard@145: base_menu = wx.Menu(title='') lbessard@145: for base_type in self.Controler.GetBaseTypes(): lbessard@145: new_id = wx.NewId() Laurent@1096: base_menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=base_type) lbessard@145: self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(base_type), id=new_id) lbessard@145: type_menu.AppendMenu(wx.NewId(), "Base Types", base_menu) lbessard@145: datatype_menu = wx.Menu(title='') Laurent@1097: for datatype in self.Controler.GetDataTypes(): lbessard@145: new_id = wx.NewId() Laurent@1096: datatype_menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=datatype) lbessard@145: self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(datatype), id=new_id) lbessard@145: type_menu.AppendMenu(wx.NewId(), "User Data Types", datatype_menu) lbessard@145: rect = self.VariablesGrid.BlockToDeviceRect((row, col), (row, col)) Edouard@1412: lbessard@145: self.VariablesGrid.PopupMenuXY(type_menu, rect.x + rect.width, rect.y + self.VariablesGrid.GetColLabelSize()) laurent@708: type_menu.Destroy() lbessard@145: event.Veto() lbessard@145: else: lbessard@145: event.Skip() lbessard@145: lbessard@145: def GetVariableTypeFunction(self, base_type): lbessard@145: def VariableTypeFunction(event): lbessard@145: row = self.VariablesGrid.GetGridCursorRow() lbessard@145: self.Table.SetValueByName(row, "Type", base_type) lbessard@145: self.Table.ResetView(self.VariablesGrid) lbessard@145: self.RefreshModel() lbessard@145: self.RefreshView() lbessard@145: event.Skip() lbessard@145: return VariableTypeFunction lbessard@145: lbessard@145: def OnVariablesGridCellLeftClick(self, event): lbessard@145: if event.GetCol() == 0: lbessard@145: row = event.GetRow() lbessard@145: data_type = self.Table.GetValueByName(row, "Type") laurent@401: var_name = self.Table.GetValueByName(row, "Name") Edouard@1412: data = wx.TextDataObject(str((var_name, "Global", data_type, andrej@1768: self.Controler.GetCurrentLocation()))) lbessard@145: dragSource = wx.DropSource(self.VariablesGrid) lbessard@145: dragSource.SetData(data) lbessard@145: dragSource.DoDragDrop() Laurent@874: return lbessard@145: event.Skip() Edouard@1412: lbessard@145: andrej@1782: # ------------------------------------------------------------------------------- Laurent@1096: # CodeFileEditor Main Frame Class andrej@1782: # ------------------------------------------------------------------------------- lbessard@145: Laurent@1096: class CodeFileEditor(ConfTreeNodeEditor): Edouard@1412: Laurent@1138: CONFNODEEDITOR_TABS = [] Laurent@1138: CODE_EDITOR = None Edouard@2226: COLUMNS_TYPE = None Edouard@1412: Laurent@1138: def _create_CodePanel(self, prnt): Laurent@1138: self.CodeEditorPanel = wx.SplitterWindow(prnt) Laurent@1138: self.CodeEditorPanel.SetMinimumPaneSize(1) Edouard@1412: Edouard@1412: self.VariablesPanel = VariablesEditor(self.CodeEditorPanel, andrej@1768: self.ParentWindow, Edouard@2226: self.Controler, Edouard@2226: self.COLUMNS_TYPE) Edouard@1412: Laurent@1138: if self.CODE_EDITOR is not None: Edouard@1412: self.CodeEditor = self.CODE_EDITOR(self.CodeEditorPanel, andrej@1768: self.ParentWindow, self.Controler) Edouard@1412: Edouard@1412: self.CodeEditorPanel.SplitHorizontally(self.VariablesPanel, andrej@1768: self.CodeEditor, 150) Laurent@1138: else: Laurent@1138: self.CodeEditorPanel.Initialize(self.VariablesPanel) Edouard@1412: Laurent@1138: return self.CodeEditorPanel Edouard@1412: laurent@630: def __init__(self, parent, controler, window): laurent@743: ConfTreeNodeEditor.__init__(self, parent, controler, window) Edouard@1412: Laurent@1138: wx.CallAfter(self.CodeEditorPanel.SetSashPosition, 150) Edouard@1412: laurent@630: def GetBufferState(self): laurent@630: return self.Controler.GetBufferState() Edouard@1412: laurent@630: def Undo(self): laurent@630: self.Controler.LoadPrevious() laurent@630: self.RefreshView() Edouard@1412: laurent@630: def Redo(self): laurent@630: self.Controler.LoadNext() laurent@630: self.RefreshView() Edouard@1412: laurent@630: def RefreshView(self): laurent@751: ConfTreeNodeEditor.RefreshView(self) Edouard@1412: Laurent@1096: self.VariablesPanel.RefreshView() Laurent@1138: self.CodeEditor.RefreshView() Edouard@1412: Laurent@1138: def Find(self, direction, search_params): Laurent@1138: self.CodeEditor.Find(direction, search_params)