controls/CustomStyledTextCtrl.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 15 Sep 2017 18:51:42 +0300
changeset 1805 52daabbb6f90
parent 1747 6046ffa2280f
child 1847 6198190bc121
permissions -rw-r--r--
remove builtin BMZ_DBG. It's not used.
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__':
1747
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    29
    faces = {
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    30
        'times': 'Times New Roman',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    31
        'mono':  'Courier New',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    32
        'helv':  'Arial',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    33
        'other': 'Comic Sans MS',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    34
        'size':  10,
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    35
    }
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    36
else:
1747
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    37
    faces = {
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    38
        'times': 'Times',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    39
        'mono':  'Courier',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    40
        'helv':  'Helvetica',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    41
        'other': 'new century schoolbook',
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    42
        'size':  12,
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    43
    }
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    44
1104
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    45
NAVIGATION_KEYS = [
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    46
    wx.WXK_END,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    47
    wx.WXK_HOME,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    48
    wx.WXK_LEFT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    49
    wx.WXK_UP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    50
    wx.WXK_RIGHT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    51
    wx.WXK_DOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    52
    wx.WXK_PAGEUP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    53
    wx.WXK_PAGEDOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    54
    wx.WXK_NUMPAD_HOME,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    55
    wx.WXK_NUMPAD_LEFT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    56
    wx.WXK_NUMPAD_UP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    57
    wx.WXK_NUMPAD_RIGHT,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    58
    wx.WXK_NUMPAD_DOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    59
    wx.WXK_NUMPAD_PAGEUP,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    60
    wx.WXK_NUMPAD_PAGEDOWN,
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    61
    wx.WXK_NUMPAD_END]
017cd95bc07e Added support for excluding code section headers to be modified in CodeFileEditor
Laurent Bessard
parents: 1092
diff changeset
    62
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    63
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    64
def GetCursorPos(old, new):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    65
    if old == "":
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    66
        return 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    67
    old_length = len(old)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    68
    new_length = len(new)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    69
    common_length = min(old_length, new_length)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    70
    i = 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    71
    for i in xrange(common_length):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    72
        if old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    73
            break
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    74
    if old_length < new_length:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    75
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    76
            return i + new_length - old_length
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    77
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    78
            return i + new_length - old_length + 1
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    79
    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
    80
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    81
            return i
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    82
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    83
            return i + 1
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    84
    else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    85
        return None
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    86
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    87
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    88
class CustomStyledTextCtrl(wx.stc.StyledTextCtrl):
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    89
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    90
    def __init__(self, *args, **kwargs):
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
    91
        wx.stc.StyledTextCtrl.__init__(self, *args, **kwargs)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    92
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    93
        self.Bind(wx.EVT_MOTION, self.OnMotion)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    94
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    95
    def OnMotion(self, event):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    96
        if wx.Platform == '__WXMSW__':
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    97
            if not event.Dragging():
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    98
                x, y = event.GetPosition()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    99
                margin_width = reduce(
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   100
                        lambda x, y: x + y,
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   101
                        [self.GetMarginWidth(i)
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   102
                         for i in xrange(3)],
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   103
                        0)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   104
                if x <= margin_width:
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   105
                    self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   106
                else:
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   107
                    self.SetCursor(wx.StockCursor(wx.CURSOR_IBEAM))
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   108
            else:
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   109
                event.Skip()
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   110
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   111
            event.Skip()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   112
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   113
    def AppendText(self, text):
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   114
        self.GotoPos(self.GetLength())
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   115
        self.AddText(text)