controls/CustomStyledTextCtrl.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 08 Jun 2017 17:30:23 +0300
changeset 1694 acb767d6ac85
parent 1511 91538d0c242c
child 1735 c02818d7e29f
permissions -rw-r--r--
make adjustable size of ArrayTypeDialog
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
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
     6
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
     8
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
     9
# See COPYING file for copyrights details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    10
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    11
# This program is free software; you can redistribute it and/or
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    12
# modify it under the terms of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    13
# as published by the Free Software Foundation; either version 2
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    14
# of the License, or (at your option) any later version.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    15
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    16
# This program is distributed in the hope that it will be useful,
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    19
# GNU General Public License for more details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    20
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    21
# You should have received a copy of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    22
# along with this program; if not, write to the Free Software
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1104
diff changeset
    24
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    25
import wx
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    26
import wx.stc
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    27
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    28
if wx.Platform == '__WXMSW__':
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    29
    faces = { 'times': 'Times New Roman',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    30
              'mono' : 'Courier New',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    31
              'helv' : 'Arial',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    32
              'other': 'Comic Sans MS',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    33
              'size' : 10,
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    34
             }
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
    faces = { 'times': 'Times',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    37
              'mono' : 'Courier',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    38
              'helv' : 'Helvetica',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    39
              'other': 'new century schoolbook',
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    40
              'size' : 12,
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    41
             }
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    42
1104
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    43
NAVIGATION_KEYS = [
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    44
    wx.WXK_END,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    45
    wx.WXK_HOME,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    46
    wx.WXK_LEFT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    47
    wx.WXK_UP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    48
    wx.WXK_RIGHT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    49
    wx.WXK_DOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    50
    wx.WXK_PAGEUP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    51
    wx.WXK_PAGEDOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    52
    wx.WXK_NUMPAD_HOME,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    53
    wx.WXK_NUMPAD_LEFT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    54
    wx.WXK_NUMPAD_UP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    55
    wx.WXK_NUMPAD_RIGHT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    56
    wx.WXK_NUMPAD_DOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    57
    wx.WXK_NUMPAD_PAGEUP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    58
    wx.WXK_NUMPAD_PAGEDOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    59
    wx.WXK_NUMPAD_END]
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    60
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    61
def GetCursorPos(old, new):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    62
    if old == "":
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    63
        return 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    64
    old_length = len(old)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    65
    new_length = len(new)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    66
    common_length = min(old_length, new_length)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    67
    i = 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    68
    for i in xrange(common_length):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    69
        if old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    70
            break
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    71
    if old_length < new_length:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    72
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    73
            return i + new_length - old_length
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    74
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    75
            return i + new_length - old_length + 1
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    76
    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
    77
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    78
            return i
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    79
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    80
            return i + 1
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    81
    else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    82
        return None
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    83
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    84
class CustomStyledTextCtrl(wx.stc.StyledTextCtrl):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    85
    
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    86
    def __init__(self, *args, **kwargs):
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
    87
        wx.stc.StyledTextCtrl.__init__(self, *args, **kwargs)
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    88
        
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    89
        self.Bind(wx.EVT_MOTION, self.OnMotion)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    90
        
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    91
    def OnMotion(self, event):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    92
        if wx.Platform == '__WXMSW__':
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    93
            if not event.Dragging():
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    94
                x, y = event.GetPosition()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    95
                margin_width = reduce(
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    96
                        lambda x, y: x + y,
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
    97
                        [self.GetMarginWidth(i)
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    98
                         for i in xrange(3)],
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    99
                        0)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   100
                if x <= margin_width:
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   101
                    self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   102
                else:
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   103
                    self.SetCursor(wx.StockCursor(wx.CURSOR_IBEAM))
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   104
            else:
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   105
                event.Skip()
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   106
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   107
            event.Skip()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   108
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   109
    def AppendText(self, text):
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   110
        self.GotoPos(self.GetLength())
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   111
        self.AddText(text)