Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
authorLaurent Bessard
Wed, 24 Apr 2013 10:03:47 +0200
changeset 1060 ac9896336b90
parent 1059 50246061d5c6
child 1061 02f371f3e063
Fixed unexpected scrolling when PythonEditor, TextViewer and CFileEditor get focus
c_ext/CFileEditor.py
editors/TextViewer.py
py_ext/PythonEditor.py
--- a/c_ext/CFileEditor.py	Wed Apr 24 09:30:10 2013 +0200
+++ b/c_ext/CFileEditor.py	Wed Apr 24 10:03:47 2013 +0200
@@ -8,24 +8,7 @@
 from controls import CustomGrid, CustomTable
 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor, SCROLLBAR_UNIT
 from util.BitmapLibrary import GetBitmap
-
-if wx.Platform == '__WXMSW__':
-    faces = { 'times': 'Times New Roman',
-              'mono' : 'Courier New',
-              'helv' : 'Arial',
-              'other': 'Comic Sans MS',
-              'size' : 10,
-              'size2': 8,
-             }
-else:
-    faces = { 'times': 'Times',
-              'mono' : 'Courier',
-              'helv' : 'Helvetica',
-              'other': 'new century schoolbook',
-              'size' : 12,
-              'size2': 10,
-             }
-
+from editors.TextViewer import GetCursorPos, faces
 
 def AppendMenu(parent, help, id, kind, text):
     if wx.VERSION >= (2, 6, 0):
@@ -47,27 +30,6 @@
     "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", 
     "void", "volatile", "wchar_t", "while"]
 
-def GetCursorPos(old, new):
-    old_length = len(old)
-    new_length = len(new)
-    common_length = min(old_length, new_length)
-    i = 0
-    for i in xrange(common_length):
-        if old[i] != new[i]:
-            break
-    if old_length < new_length:
-        if common_length > 0 and old[i] != new[i]:
-            return i + new_length - old_length
-        else:
-            return i + new_length - old_length + 1
-    elif old_length > new_length or i < min(old_length, new_length) - 1:
-        if common_length > 0 and old[i] != new[i]:
-            return i
-        else:
-            return i + 1
-    else:
-        return None
-
 class CppEditor(stc.StyledTextCtrl):
 
     fold_symbols = 3
@@ -165,15 +127,15 @@
         self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,  "fore:#FFFFFF,back:#0000FF,bold")
         self.StyleSetSpec(stc.STC_STYLE_BRACEBAD,    "fore:#000000,back:#FF0000,bold")
 
-        self.StyleSetSpec(stc.STC_C_COMMENT, 'fore:#408060')
-        self.StyleSetSpec(stc.STC_C_COMMENTLINE, 'fore:#408060')
-        self.StyleSetSpec(stc.STC_C_COMMENTDOC, 'fore:#408060')
-        self.StyleSetSpec(stc.STC_C_NUMBER, 'fore:#0076AE')
-        self.StyleSetSpec(stc.STC_C_WORD, 'bold,fore:#800056')
-        self.StyleSetSpec(stc.STC_C_STRING, 'fore:#2a00ff')
-        self.StyleSetSpec(stc.STC_C_PREPROCESSOR, 'bold,fore:#800056')
-        self.StyleSetSpec(stc.STC_C_OPERATOR, 'bold')
-        self.StyleSetSpec(stc.STC_C_STRINGEOL, 'back:#FFD5FF')
+        self.StyleSetSpec(stc.STC_C_COMMENT, 'fore:#408060,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_COMMENTLINE, 'fore:#408060,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_COMMENTDOC, 'fore:#408060,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_NUMBER, 'fore:#0076AE,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_WORD, 'bold,fore:#800056,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_STRING, 'fore:#2a00ff,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_PREPROCESSOR, 'bold,fore:#800056,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_OPERATOR, 'bold,size:%(size)d' % faces)
+        self.StyleSetSpec(stc.STC_C_STRINGEOL, 'back:#FFD5FF,size:%(size)d' % faces)
         
         # register some images for use in the AutoComplete box.
         #self.RegisterImage(1, images.getSmilesBitmap())
@@ -254,16 +216,19 @@
         self.ResetBuffer()
         self.DisableEvents = True
         old_cursor_pos = self.GetCurrentPos()
+        line = self.GetFirstVisibleLine()
+        column = self.GetXOffset()
         old_text = self.GetText()
         new_text = self.Controler.GetPartText(self.Name)
         self.SetText(new_text)
-        new_cursor_pos = GetCursorPos(old_text, new_text)
-        if new_cursor_pos != None:
-            self.GotoPos(new_cursor_pos)
-        else:
-            self.GotoPos(old_cursor_pos)
-        self.ScrollToColumn(0)
-        self.EmptyUndoBuffer()
+        if old_text != new_text:
+            new_cursor_pos = GetCursorPos(old_text, new_text)
+            self.LineScroll(column, line)
+            if new_cursor_pos != None:
+                self.GotoPos(new_cursor_pos)
+            else:
+                self.GotoPos(old_cursor_pos)
+            self.EmptyUndoBuffer()
         self.DisableEvents = False
         
         self.Colourise(0, -1)
--- a/editors/TextViewer.py	Wed Apr 24 09:30:10 2013 +0200
+++ b/editors/TextViewer.py	Wed Apr 24 10:03:47 2013 +0200
@@ -80,6 +80,8 @@
 }
 
 def GetCursorPos(old, new):
+    if old == "":
+        return 0
     old_length = len(old)
     new_length = len(new)
     common_length = min(old_length, new_length)
@@ -445,17 +447,20 @@
             self.ResetBuffer()
             self.DisableEvents = True
             old_cursor_pos = self.GetCurrentPos()
+            line = self.Editor.GetFirstVisibleLine()
+            column = self.Editor.GetXOffset()
             old_text = self.GetText()
             new_text = self.Controler.GetEditedElementText(self.TagName, self.Debug)
-            self.SetText(new_text)
-            new_cursor_pos = GetCursorPos(old_text, new_text)
-            if new_cursor_pos != None:
-                self.Editor.GotoPos(new_cursor_pos)
-            else:
-                self.Editor.GotoPos(old_cursor_pos)
-            self.Editor.ScrollToColumn(0)
-            self.RefreshJumpList()
-            self.Editor.EmptyUndoBuffer()
+            if old_text != new_text:
+                self.SetText(new_text)
+                new_cursor_pos = GetCursorPos(old_text, new_text)
+                self.Editor.LineScroll(column, line)
+                if new_cursor_pos != None:
+                    self.Editor.GotoPos(new_cursor_pos)
+                else:
+                    self.Editor.GotoPos(old_cursor_pos)
+                self.RefreshJumpList()
+                self.Editor.EmptyUndoBuffer()
             self.DisableEvents = False
             
             self.RefreshVariableTree()
--- a/py_ext/PythonEditor.py	Wed Apr 24 09:30:10 2013 +0200
+++ b/py_ext/PythonEditor.py	Wed Apr 24 10:03:47 2013 +0200
@@ -8,6 +8,7 @@
 from plcopen.plcopen import TestTextElement
 from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD
 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor
+from editors.TextViewer import GetCursorPos, faces
 
 [STC_PYTHON_ERROR, STC_PYTHON_SEARCH_RESULT] = range(15, 17)
 
@@ -16,55 +17,9 @@
     SEARCH_RESULT_HIGHLIGHT: STC_PYTHON_SEARCH_RESULT,
 }
 
-if wx.Platform == '__WXMSW__':
-    faces = { 'times': 'Times New Roman',
-              'mono' : 'Courier New',
-              'helv' : 'Arial',
-              'other': 'Comic Sans MS',
-              'size' : 10,
-              'size2': 8,
-             }
-elif wx.Platform == '__WXMAC__':
-    faces = { 'times': 'Times New Roman',
-              'mono' : 'Monaco',
-              'helv' : 'Arial',
-              'other': 'Comic Sans MS',
-              'size' : 12,
-              'size2': 10,
-             }
-else:
-    faces = { 'times': 'Times',
-              'mono' : 'Courier',
-              'helv' : 'Helvetica',
-              'other': 'new century schoolbook',
-              'size' : 12,
-              'size2': 10,
-             }
-
 [ID_PYTHONEDITOR,
 ] = [wx.NewId() for _init_ctrls in range(1)]
 
-def GetCursorPos(old, new):
-    old_length = len(old)
-    new_length = len(new)
-    common_length = min(old_length, new_length)
-    i = 0
-    for i in xrange(common_length):
-        if old[i] != new[i]:
-            break
-    if old_length < new_length:
-        if common_length > 0 and old[i] != new[i]:
-            return i + new_length - old_length
-        else:
-            return i + new_length - old_length + 1
-    elif old_length > new_length or i < min(old_length, new_length) - 1:
-        if common_length > 0 and old[i] != new[i]:
-            return i
-        else:
-            return i + 1
-    else:
-        return None
-
 class PythonEditor(ConfTreeNodeEditor):
 
     fold_symbols = 3
@@ -147,7 +102,7 @@
         self.PythonCodeEditor.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
         self.PythonCodeEditor.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
         self.PythonCodeEditor.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
-
+        
         # Global default style
         if wx.Platform == '__WXMSW__':
             self.PythonCodeEditor.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'fore:#000000,back:#FFFFFF,face:Courier New')
@@ -328,16 +283,19 @@
         self.ResetBuffer()
         self.DisableEvents = True
         old_cursor_pos = self.PythonCodeEditor.GetCurrentPos()
+        line = self.PythonCodeEditor.GetFirstVisibleLine()
+        column = self.PythonCodeEditor.GetXOffset()
         old_text = self.PythonCodeEditor.GetText()
         new_text = self.Controler.GetPythonCode()
-        self.PythonCodeEditor.SetText(new_text)
-        new_cursor_pos = GetCursorPos(old_text, new_text)
-        if new_cursor_pos != None:
-            self.PythonCodeEditor.GotoPos(new_cursor_pos)
-        else:
-            self.PythonCodeEditor.GotoPos(old_cursor_pos)
-        self.PythonCodeEditor.ScrollToColumn(0)
-        self.PythonCodeEditor.EmptyUndoBuffer()
+        if old_text != new_text:
+            self.PythonCodeEditor.SetText(new_text)
+            new_cursor_pos = GetCursorPos(old_text, new_text)
+            self.PythonCodeEditor.LineScroll(column, line)
+            if new_cursor_pos != None:
+                self.PythonCodeEditor.GotoPos(new_cursor_pos)
+            else:
+                self.PythonCodeEditor.GotoPos(old_cursor_pos)
+            self.PythonCodeEditor.EmptyUndoBuffer()
         self.DisableEvents = False
         
         self.PythonCodeEditor.Colourise(0, -1)
@@ -363,7 +321,7 @@
                 self.PythonCodeEditor.AutoCompShow(0, " ".join([word + "?1" for word in keyword.kwlist]))
         else:
             event.Skip()
-
+    
     def OnKillFocus(self, event):
         self.PythonCodeEditor.AutoCompCancel()
         event.Skip()