py_ext/PythonEditor.py
changeset 1060 ac9896336b90
parent 1058 bb8ac451c28a
child 1091 5f612651d227
equal deleted inserted replaced
1059:50246061d5c6 1060:ac9896336b90
     6 import wx.stc as stc
     6 import wx.stc as stc
     7 
     7 
     8 from plcopen.plcopen import TestTextElement
     8 from plcopen.plcopen import TestTextElement
     9 from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD
     9 from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD
    10 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
    10 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
       
    11 from editors.TextViewer import GetCursorPos, faces
    11 
    12 
    12 [STC_PYTHON_ERROR, STC_PYTHON_SEARCH_RESULT] = range(15, 17)
    13 [STC_PYTHON_ERROR, STC_PYTHON_SEARCH_RESULT] = range(15, 17)
    13 
    14 
    14 HIGHLIGHT_TYPES = {
    15 HIGHLIGHT_TYPES = {
    15     ERROR_HIGHLIGHT: STC_PYTHON_ERROR,
    16     ERROR_HIGHLIGHT: STC_PYTHON_ERROR,
    16     SEARCH_RESULT_HIGHLIGHT: STC_PYTHON_SEARCH_RESULT,
    17     SEARCH_RESULT_HIGHLIGHT: STC_PYTHON_SEARCH_RESULT,
    17 }
    18 }
    18 
    19 
    19 if wx.Platform == '__WXMSW__':
       
    20     faces = { 'times': 'Times New Roman',
       
    21               'mono' : 'Courier New',
       
    22               'helv' : 'Arial',
       
    23               'other': 'Comic Sans MS',
       
    24               'size' : 10,
       
    25               'size2': 8,
       
    26              }
       
    27 elif wx.Platform == '__WXMAC__':
       
    28     faces = { 'times': 'Times New Roman',
       
    29               'mono' : 'Monaco',
       
    30               'helv' : 'Arial',
       
    31               'other': 'Comic Sans MS',
       
    32               'size' : 12,
       
    33               'size2': 10,
       
    34              }
       
    35 else:
       
    36     faces = { 'times': 'Times',
       
    37               'mono' : 'Courier',
       
    38               'helv' : 'Helvetica',
       
    39               'other': 'new century schoolbook',
       
    40               'size' : 12,
       
    41               'size2': 10,
       
    42              }
       
    43 
       
    44 [ID_PYTHONEDITOR,
    20 [ID_PYTHONEDITOR,
    45 ] = [wx.NewId() for _init_ctrls in range(1)]
    21 ] = [wx.NewId() for _init_ctrls in range(1)]
    46 
       
    47 def GetCursorPos(old, new):
       
    48     old_length = len(old)
       
    49     new_length = len(new)
       
    50     common_length = min(old_length, new_length)
       
    51     i = 0
       
    52     for i in xrange(common_length):
       
    53         if old[i] != new[i]:
       
    54             break
       
    55     if old_length < new_length:
       
    56         if common_length > 0 and old[i] != new[i]:
       
    57             return i + new_length - old_length
       
    58         else:
       
    59             return i + new_length - old_length + 1
       
    60     elif old_length > new_length or i < min(old_length, new_length) - 1:
       
    61         if common_length > 0 and old[i] != new[i]:
       
    62             return i
       
    63         else:
       
    64             return i + 1
       
    65     else:
       
    66         return None
       
    67 
    22 
    68 class PythonEditor(ConfTreeNodeEditor):
    23 class PythonEditor(ConfTreeNodeEditor):
    69 
    24 
    70     fold_symbols = 3
    25     fold_symbols = 3
    71     CONFNODEEDITOR_TABS = [
    26     CONFNODEEDITOR_TABS = [
   145 
   100 
   146 
   101 
   147         self.PythonCodeEditor.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
   102         self.PythonCodeEditor.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
   148         self.PythonCodeEditor.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
   103         self.PythonCodeEditor.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
   149         self.PythonCodeEditor.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
   104         self.PythonCodeEditor.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
   150 
   105         
   151         # Global default style
   106         # Global default style
   152         if wx.Platform == '__WXMSW__':
   107         if wx.Platform == '__WXMSW__':
   153             self.PythonCodeEditor.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'fore:#000000,back:#FFFFFF,face:Courier New')
   108             self.PythonCodeEditor.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'fore:#000000,back:#FFFFFF,face:Courier New')
   154         elif wx.Platform == '__WXMAC__':
   109         elif wx.Platform == '__WXMAC__':
   155             # TODO: if this looks fine on Linux too, remove the Mac-specific case 
   110             # TODO: if this looks fine on Linux too, remove the Mac-specific case 
   326         ConfTreeNodeEditor.RefreshView(self)
   281         ConfTreeNodeEditor.RefreshView(self)
   327         
   282         
   328         self.ResetBuffer()
   283         self.ResetBuffer()
   329         self.DisableEvents = True
   284         self.DisableEvents = True
   330         old_cursor_pos = self.PythonCodeEditor.GetCurrentPos()
   285         old_cursor_pos = self.PythonCodeEditor.GetCurrentPos()
       
   286         line = self.PythonCodeEditor.GetFirstVisibleLine()
       
   287         column = self.PythonCodeEditor.GetXOffset()
   331         old_text = self.PythonCodeEditor.GetText()
   288         old_text = self.PythonCodeEditor.GetText()
   332         new_text = self.Controler.GetPythonCode()
   289         new_text = self.Controler.GetPythonCode()
   333         self.PythonCodeEditor.SetText(new_text)
   290         if old_text != new_text:
   334         new_cursor_pos = GetCursorPos(old_text, new_text)
   291             self.PythonCodeEditor.SetText(new_text)
   335         if new_cursor_pos != None:
   292             new_cursor_pos = GetCursorPos(old_text, new_text)
   336             self.PythonCodeEditor.GotoPos(new_cursor_pos)
   293             self.PythonCodeEditor.LineScroll(column, line)
   337         else:
   294             if new_cursor_pos != None:
   338             self.PythonCodeEditor.GotoPos(old_cursor_pos)
   295                 self.PythonCodeEditor.GotoPos(new_cursor_pos)
   339         self.PythonCodeEditor.ScrollToColumn(0)
   296             else:
   340         self.PythonCodeEditor.EmptyUndoBuffer()
   297                 self.PythonCodeEditor.GotoPos(old_cursor_pos)
       
   298             self.PythonCodeEditor.EmptyUndoBuffer()
   341         self.DisableEvents = False
   299         self.DisableEvents = False
   342         
   300         
   343         self.PythonCodeEditor.Colourise(0, -1)
   301         self.PythonCodeEditor.Colourise(0, -1)
   344         
   302         
   345         self.ShowHighlights()
   303         self.ShowHighlights()
   361 
   319 
   362                 # Images are specified with a appended "?type"
   320                 # Images are specified with a appended "?type"
   363                 self.PythonCodeEditor.AutoCompShow(0, " ".join([word + "?1" for word in keyword.kwlist]))
   321                 self.PythonCodeEditor.AutoCompShow(0, " ".join([word + "?1" for word in keyword.kwlist]))
   364         else:
   322         else:
   365             event.Skip()
   323             event.Skip()
   366 
   324     
   367     def OnKillFocus(self, event):
   325     def OnKillFocus(self, event):
   368         self.PythonCodeEditor.AutoCompCancel()
   326         self.PythonCodeEditor.AutoCompCancel()
   369         event.Skip()
   327         event.Skip()
   370 
   328 
   371     def OnUpdateUI(self, evt):
   329     def OnUpdateUI(self, evt):