controls/CustomStyledTextCtrl.py
author Laurent Bessard
Mon, 06 May 2013 10:28:47 +0200
changeset 1091 5f612651d227
child 1092 e91f2c8d6f51
permissions -rw-r--r--
Fixed bug with margin cursor in StyledTextCtrl on Windows
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     3
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     4
import wx
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     5
import wx.stc
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     6
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     7
if wx.Platform == '__WXMSW__':
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     8
    faces = { 'times': 'Times New Roman',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
     9
              'mono' : 'Courier New',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    10
              'helv' : 'Arial',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    11
              'other': 'Comic Sans MS',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    12
              'size' : 10,
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    13
             }
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    14
else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    15
    faces = { 'times': 'Times',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    16
              'mono' : 'Courier',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    17
              'helv' : 'Helvetica',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    18
              'other': 'new century schoolbook',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    19
              'size' : 12,
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    20
             }
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    21
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    22
def GetCursorPos(old, new):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    23
    if old == "":
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    24
        return 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    25
    old_length = len(old)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    26
    new_length = len(new)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    27
    common_length = min(old_length, new_length)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    28
    i = 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    29
    for i in xrange(common_length):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    30
        if old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    31
            break
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    32
    if old_length < new_length:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    33
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    34
            return i + new_length - old_length
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    35
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    36
            return i + new_length - old_length + 1
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    37
    elif old_length > new_length or i < min(old_length, new_length) - 1:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    38
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    39
            return i
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    40
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    41
            return i + 1
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    42
    else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    43
        return None
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    44
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    45
class CustomStyledTextCtrl(wx.stc.StyledTextCtrl):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    46
    
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    47
    def __init__(self, *args, **kwargs):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    48
        wx.stc.StyledTextCtrl(self, *args, **kwargs)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    49
        
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    50
        self.Bind(wx.EVT_MOTION, self.OnMotion)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    51
        
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    52
    def OnMotion(self, event):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    53
        if wx.Platform == '__WXMSW__':
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    54
            if not event.Dragging():
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    55
                x, y = event.GetPosition()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    56
                margin_width = reduce(
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    57
                        lambda x, y: x + y,
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    58
                        [self.LogConsole.GetMarginWidth(i)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    59
                         for i in xrange(3)],
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    60
                        0)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    61
                if x <= margin_width:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    62
                    self.LogConsole.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    63
                else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    64
                    self.LogConsole.SetCursor(wx.StockCursor(wx.CURSOR_IBEAM))
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    65
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    66
            event.Skip()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    67
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    68
    def AppendText(self, text):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    69
        last_position = self.GetLength()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    70
        current_selection = self.GetSelection()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    71
        self.GotoPos(last_position)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    72
        self.AddText(text)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    73
        if current_selection[0] != last_position:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    74
            self.SetSelection(*current_selection)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    75
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    76
            self.ScrollToLine(self.GetLineCount())