editors/CodeFileEditor.py
changeset 1104 017cd95bc07e
parent 1101 5a0b439cf576
child 1109 2dfad20abde3
equal deleted inserted replaced
1103:2fc1eef45bda 1104:017cd95bc07e
     7 
     7 
     8 from plcopen.plcopen import TestTextElement
     8 from plcopen.plcopen import TestTextElement
     9 from controls import CustomGrid, CustomTable
     9 from controls import CustomGrid, CustomTable
    10 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
    10 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
    11 from util.BitmapLibrary import GetBitmap
    11 from util.BitmapLibrary import GetBitmap
    12 from controls.CustomStyledTextCtrl import CustomStyledTextCtrl, faces, GetCursorPos
    12 from controls.CustomStyledTextCtrl import CustomStyledTextCtrl, faces, GetCursorPos, NAVIGATION_KEYS
    13 from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD
    13 from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD
    14 
    14 
    15 SECTIONS_NAMES = ["Includes", "Globals", "Init",
    15 SECTIONS_NAMES = ["Includes", "Globals", "Init",
    16                   "CleanUp", "Retrieve", "Publish"]
    16                   "CleanUp", "Retrieve", "Publish"]
    17 
    17 
    18 [STC_CODE_ERROR, STC_CODE_SEARCH_RESULT] = range(15, 17)
    18 [STC_CODE_ERROR, STC_CODE_SEARCH_RESULT, 
       
    19  STC_CODE_SECTION] = range(15, 18)
    19 
    20 
    20 HIGHLIGHT_TYPES = {
    21 HIGHLIGHT_TYPES = {
    21     ERROR_HIGHLIGHT: STC_CODE_ERROR,
    22     ERROR_HIGHLIGHT: STC_CODE_ERROR,
    22     SEARCH_RESULT_HIGHLIGHT: STC_CODE_SEARCH_RESULT,
    23     SEARCH_RESULT_HIGHLIGHT: STC_CODE_SEARCH_RESULT,
    23 }
    24 }
    74         self.StyleClearAll()  # Reset all to be like the default
    75         self.StyleClearAll()  # Reset all to be like the default
    75 
    76 
    76         # Global default styles for all languages
    77         # Global default styles for all languages
    77         self.StyleSetSpec(stc.STC_STYLE_DEFAULT,     "face:%(mono)s,size:%(size)d" % faces)
    78         self.StyleSetSpec(stc.STC_STYLE_DEFAULT,     "face:%(mono)s,size:%(size)d" % faces)
    78         self.StyleSetSpec(stc.STC_STYLE_LINENUMBER,  "back:#C0C0C0,face:%(helv)s,size:%(size)d" % faces)
    79         self.StyleSetSpec(stc.STC_STYLE_LINENUMBER,  "back:#C0C0C0,face:%(helv)s,size:%(size)d" % faces)
    79         self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
    80         self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(mono)s" % faces)
    80         self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,  "fore:#FFFFFF,back:#0000FF,bold")
    81         self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,  "fore:#FFFFFF,back:#0000FF,bold")
    81         self.StyleSetSpec(stc.STC_STYLE_BRACEBAD,    "fore:#000000,back:#FF0000,bold")
    82         self.StyleSetSpec(stc.STC_STYLE_BRACEBAD,    "fore:#000000,back:#FF0000,bold")
    82         
    83         
    83         # Highlighting styles
    84         # Highlighting styles
    84         self.StyleSetSpec(STC_CODE_ERROR, 'fore:#FF0000,back:#FFFF00,size:%(size)d' % faces)
    85         self.StyleSetSpec(STC_CODE_ERROR, 'fore:#FF0000,back:#FFFF00,size:%(size)d' % faces)
    85         self.StyleSetSpec(STC_CODE_SEARCH_RESULT, 'fore:#FFFFFF,back:#FFA500,size:%(size)d' % faces)
    86         self.StyleSetSpec(STC_CODE_SEARCH_RESULT, 'fore:#FFFFFF,back:#FFA500,size:%(size)d' % faces)
       
    87         
       
    88         # Section style
       
    89         self.StyleSetSpec(STC_CODE_SECTION, 'fore:#808080,size:%(size)d')
       
    90         self.StyleSetChangeable(STC_CODE_SECTION, False)
    86         
    91         
    87         # register some images for use in the AutoComplete box.
    92         # register some images for use in the AutoComplete box.
    88         #self.RegisterImage(1, images.getSmilesBitmap())
    93         #self.RegisterImage(1, images.getSmilesBitmap())
    89         self.RegisterImage(1, 
    94         self.RegisterImage(1, 
    90             wx.ArtProvider.GetBitmap(wx.ART_DELETE, size=(16,16)))
    95             wx.ArtProvider.GetBitmap(wx.ART_DELETE, size=(16,16)))
   114         self.RefreshHighlightsTimer = wx.Timer(self, -1)
   119         self.RefreshHighlightsTimer = wx.Timer(self, -1)
   115         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
   120         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
   116         
   121         
   117         self.SectionsComments = {}
   122         self.SectionsComments = {}
   118         for section in SECTIONS_NAMES:
   123         for section in SECTIONS_NAMES:
   119             section_start_comment = "%s %s section\n" % (self.COMMENT_HEADER, section)
   124             section_start_comment = "%s %s section %s" % (
   120             section_end_comment = "\n%s End %s section\n\n" % (self.COMMENT_HEADER, section)
   125                     self.COMMENT_HEADER, section, self.COMMENT_HEADER)
       
   126             section_end_comment = "%s End %s section %s" % (
       
   127                     self.COMMENT_HEADER, section, self.COMMENT_HEADER)
   121             self.SectionsComments[section] = {
   128             self.SectionsComments[section] = {
   122                  "start": section_start_comment,
   129                  "start": section_start_comment,
   123                  "end": section_end_comment,
   130                  "end": section_end_comment,
   124                  "pattern": re.compile(section_start_comment + 
   131                  "pattern": re.compile(section_start_comment + 
   125                                        "(.*)" + 
   132                                        "(.*)" + 
   154                     elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1:
   161                     elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1:
   155                         self.Controler.EndBuffering()
   162                         self.Controler.EndBuffering()
   156                         self.StartBuffering()
   163                         self.StartBuffering()
   157                     self.CurrentAction = ("Delete", event.GetPosition())
   164                     self.CurrentAction = ("Delete", event.GetPosition())
   158                     wx.CallAfter(self.RefreshModel)
   165                     wx.CallAfter(self.RefreshModel)
       
   166         wx.CallAfter(self.RefreshSectionStyling)
   159         event.Skip()
   167         event.Skip()
   160     
   168     
   161     def OnDoDrop(self, event):
   169     def OnDoDrop(self, event):
   162         self.ResetBuffer()
   170         self.ResetBuffer()
   163         wx.CallAfter(self.RefreshModel)
   171         wx.CallAfter(self.RefreshModel)
   189         parts = self.Controler.GetTextParts()
   197         parts = self.Controler.GetTextParts()
   190         text = ""
   198         text = ""
   191         for section in SECTIONS_NAMES:
   199         for section in SECTIONS_NAMES:
   192             section_comments = self.SectionsComments[section]
   200             section_comments = self.SectionsComments[section]
   193             text += section_comments["start"]
   201             text += section_comments["start"]
       
   202             if not parts[section].startswith("\n"):
       
   203                 text += "\n"
   194             text += parts[section]
   204             text += parts[section]
       
   205             if not parts[section].endswith("\n"):
       
   206                 text += "\n"
   195             text += section_comments["end"]
   207             text += section_comments["end"]
       
   208             if section != SECTIONS_NAMES[-1]:
       
   209                  text += "\n\n"
   196         return text
   210         return text
   197 
   211 
   198     def RefreshView(self, scroll_to_highlight=False):
   212     def RefreshView(self, scroll_to_highlight=False):
   199         self.ResetBuffer()
   213         self.ResetBuffer()
   200         self.DisableEvents = True
   214         self.DisableEvents = True
   212             else:
   226             else:
   213                 self.GotoPos(old_cursor_pos)
   227                 self.GotoPos(old_cursor_pos)
   214             self.EmptyUndoBuffer()
   228             self.EmptyUndoBuffer()
   215         self.DisableEvents = False
   229         self.DisableEvents = False
   216         
   230         
       
   231         self.RefreshSectionStyling()
       
   232         
       
   233         self.ShowHighlights()
       
   234 
       
   235     def RefreshSectionStyling(self):
   217         self.Colourise(0, -1)
   236         self.Colourise(0, -1)
   218         
   237         
   219         self.ShowHighlights()
   238         text = self.GetText()
       
   239         for line in xrange(self.GetLineCount()):
       
   240             self.SetLineState(line, 0)
       
   241         
       
   242         last_styled_pos = None
       
   243         end_pos = None
       
   244         for section in SECTIONS_NAMES:
       
   245             section_comments = self.SectionsComments[section]
       
   246             start_pos = text.find(section_comments["start"])
       
   247             end_pos = start_pos + len(section_comments["start"])
       
   248             if last_styled_pos is None:
       
   249                 last_styled_pos = start_pos
       
   250                 self.StartStyling(start_pos, 0xff)
       
   251             self.SetStyling(end_pos - last_styled_pos, STC_CODE_SECTION)
       
   252             for line in xrange(self.LineFromPosition(last_styled_pos),
       
   253                                 self.LineFromPosition(end_pos) + 1):
       
   254                 self.SetLineState(line, 1)
       
   255             start_pos = text.find(section_comments["end"])
       
   256             self.StartStyling(start_pos, 0xff)
       
   257             last_styled_pos = start_pos
       
   258             end_pos = start_pos + len(section_comments["end"])
       
   259             
       
   260         if last_styled_pos is not None and end_pos:
       
   261             self.SetStyling(end_pos - last_styled_pos, STC_CODE_SECTION)
       
   262             for line in xrange(self.LineFromPosition(last_styled_pos),
       
   263                                self.LineFromPosition(end_pos) + 1):
       
   264                 self.SetLineState(line, 1)  
   220 
   265 
   221     def DoGetBestSize(self):
   266     def DoGetBestSize(self):
   222         return self.ParentWindow.GetPanelBestSize()
   267         return self.ParentWindow.GetPanelBestSize()
   223 
   268 
   224     def RefreshModel(self):
   269     def RefreshModel(self):
   235 
   280 
   236     def OnKeyPressed(self, event):
   281     def OnKeyPressed(self, event):
   237         if self.CallTipActive():
   282         if self.CallTipActive():
   238             self.CallTipCancel()
   283             self.CallTipCancel()
   239         key = event.GetKeyCode()
   284         key = event.GetKeyCode()
   240 
   285         
   241         if key == 32 and event.ControlDown():
   286         if (self.GetLineState(self.LineFromPosition(self.GetSelection()[0])) and
       
   287             key not in NAVIGATION_KEYS):
       
   288             return
       
   289         
       
   290         elif key == 32 and event.ControlDown():
   242             pos = self.GetCurrentPos()
   291             pos = self.GetCurrentPos()
   243 
   292 
   244             # Tips
   293             # Tips
   245             if event.ShiftDown():
   294             if event.ShiftDown():
   246                 pass
   295                 pass