controls/CustomStyledTextCtrl.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Mon, 14 Aug 2017 23:27:15 +0300
changeset 1736 7e61baa047f0
parent 1735 c02818d7e29f
child 1739 ec153828ded2
permissions -rw-r--r--
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    61
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    62
def GetCursorPos(old, new):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    63
    if old == "":
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    64
        return 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    65
    old_length = len(old)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    66
    new_length = len(new)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    67
    common_length = min(old_length, new_length)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    68
    i = 0
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    69
    for i in xrange(common_length):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    70
        if old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    71
            break
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    72
    if old_length < new_length:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    73
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    74
            return i + new_length - old_length
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
            return i + new_length - old_length + 1
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    77
    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
    78
        if common_length > 0 and old[i] != new[i]:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    79
            return i
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    80
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    81
            return i + 1
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 None
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    84
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    85
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    86
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
    87
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    88
    def __init__(self, *args, **kwargs):
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
    89
        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
    90
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    91
        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
    92
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    93
    def OnMotion(self, event):
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    94
        if wx.Platform == '__WXMSW__':
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    95
            if not event.Dragging():
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    96
                x, y = event.GetPosition()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    97
                margin_width = reduce(
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
    98
                        lambda x, y: x + y,
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
    99
                        [self.GetMarginWidth(i)
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   100
                         for i in xrange(3)],
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   101
                        0)
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   102
                if x <= margin_width:
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   103
                    self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   104
                else:
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   105
                    self.SetCursor(wx.StockCursor(wx.CURSOR_IBEAM))
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   106
            else:
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   107
                event.Skip()
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   108
        else:
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   109
            event.Skip()
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   110
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   111
    def AppendText(self, text):
1092
e91f2c8d6f51 Fixed bugs in CustomStyledTextCtrl
Laurent Bessard
parents: 1091
diff changeset
   112
        self.GotoPos(self.GetLength())
1091
5f612651d227 Fixed bug with margin cursor in StyledTextCtrl on Windows
Laurent Bessard
parents:
diff changeset
   113
        self.AddText(text)