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