Caret displayed at wrong position in StyledTextCtrl.

The caret is sometimes displayed at the wrong position in a StyledTextCtrl. The problem seems to occur when a long string word wraps. Below is an example of the problem.

wxPython 2.8.8.0 (gtk2-unicode)

Ubuntu 8.10

···

import textwrap

import wx, wx.stc

text = textwrap.dedent("""
Caret is sometimes displayed at the wrong position in StyledTextCtrl when a string word-wraps

Maybe it’s expecting a Monospaced font?
Hold shift and move the caret horizontally. You can see the text jiggle… on heavily wrapped text anyway, with long, unbroken strings like the stuff below.

jlfjda;jf;adksj;dlhsaf;lads;lfel;fjieofi afi;ikedilf w ljai fioj fjej afjea fpeawj f;eiaj fl;ajwfe jaiifo;aei jalefjl fle;lefj ;f w7oieifie aej fiaejfl;e aelilif dk f;dj;kdklgjjdkgljadgk ljd;fijdsj fldfli; jh igjfigjiejaggghj;s h;ie jfj

“”")

app = wx.App(redirect=False)

frame = wx.Frame(None)

panel = wx.Panel(frame)
panel.SetSizer(wx.BoxSizer(wx.VERTICAL))

text_control = wx.stc.StyledTextCtrl(panel)

text_control.SetText(text)
text_control.SetWrapMode(wx.stc.STC_WRAP_WORD)
panel.GetSizer().Add(text_control, flag=wx.EXPAND, proportion=1)

frame.Show(True)

app.MainLoop()

Jesse Aldridge wrote:

The caret is sometimes displayed at the wrong position in a StyledTextCtrl. The problem seems to occur when a long string word wraps. Below is an example of the problem.

STC has/had this problem on OSX too and it was due to having slightly different fractional widths of runs of text in different styles, (selecting text changes the style of the selected text) and the fact that wx truncates to integer values. If GTK/Pango is now reporting the fractional widths in the same way then it can have the same problem. Currently we just have to live with it. :frowning:

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

For what it’s worth, qt doesn’t have this problem. (Not trying to be a dick, just noting the fact for people who have the same issue :slight_smile:

import sys

from qt import *
from qtext import *

text = “”"

Caret is sometimes displayed at the wrong position in StyledTextCtrl when a string word-wraps
Maybe it’s expecting a Monospaced font?
Hold shift and move the caret horizontally. You can see the text jiggle… on heavily wrapped text anyway, with long, unbroken strings like the stuff below.

jlfjda;jf;adksj;dlhsaf;lads;lfel;fjieofi afi;ikedilf w ljai fioj fjej afjea fpeawj f;eiaj fl;ajwfe jaiifo;aei jalefjl fle;lefj ;f w7oieifie aej fiaejfl;e aelilif dk f;dj;kdklgjjdkgljadgk ljd;fijdsj fldfli; jh igjfigjiejaggghj;s h;ie jfj

“”"

app = QApplication(sys.argv)

frame = QMainWindow(None,‘example application main window’,Qt.WDestructiveClose)

text_control = QextScintilla(frame)
text_control.setText(text)

text_control.setWrapMode(QextScintilla.WrapWord)

frame.setCentralWidget(text_control)
frame.resize(300,300)
frame.show()

app.connect(app, SIGNAL(‘lastWindowClosed()’), app, SLOT(‘quit()’))

app.exec_loop()

···

On Mon, Nov 17, 2008 at 6:05 PM, Robin Dunn robin@alldunn.com wrote:

Jesse Aldridge wrote:

The caret is sometimes displayed at the wrong position in a StyledTextCtrl. The problem seems to occur when a long string word wraps. Below is an example of the problem.

STC has/had this problem on OSX too and it was due to having slightly different fractional widths of runs of text in different styles, (selecting text changes the style of the selected text) and the fact that wx truncates to integer values. If GTK/Pango is now reporting the fractional widths in the same way then it can have the same problem. Currently we just have to live with it. :frowning:

Robin Dunn

Software Craftsman

http://wxPython.org Java give you jitters? Relax with wxPython!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

I have just come across a similar problem although in my case it doesn’t involve long unbroken strings, but just happens with some combinations of normal text.

Here is a simple example that demonstrates the issue on linux:

import wx
import wx.stc as stc

TEXT = (
    "Sunny at first, but medium level cloud increased later in the morning. "
    "A weakening cold front then crossed from the west bringing thicker cloud "
    "and outbreaks of rain in the early afternoon. #Weather\n"
)

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((500, 250))
        self.SetTitle("Misaligned caret")

        self.panel_1 = wx.Panel(self, wx.ID_ANY)
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        self.stc = stc.StyledTextCtrl(self.panel_1, wx.ID_ANY, style=wx.TE_MULTILINE)
        sizer_1.Add(self.stc, 1, wx.ALL | wx.EXPAND, 8)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(sizer_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM, 8)
        self.close_button = wx.Button(self.panel_1, wx.ID_ANY, "Close")
        sizer_2.Add(self.close_button, 0, wx.RIGHT, 16)
        self.panel_1.SetSizer(sizer_1)
        self.Layout()

        self.Bind(wx.EVT_BUTTON, self.OnClose, self.close_button)

        self.stc.SetMarginWidth(1, 0)
        self.stc.SetUseHorizontalScrollBar(0)
        self.stc.SetWrapMode(stc.STC_WRAP_WORD)
        self.stc.SetText(TEXT)


    def OnClose(self, _event):
        self.Destroy()


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True


if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

When I run the program, I click on the bottom blank line in the STC. I then press the left-arrow key. Instead of moving to the end of the previous line, the caret appears over the middle of the letter ‘e’. Further presses of the left-arrow key cause the caret to appear over some of the other characters instead of between them. If I press the backspace key, the character that gets deleted is often not the one to the left of the caret, but the one to the left of that, which is confusing for the user.

If I re-run the program and stretch the window so that no text is wrapped, the caret does move between the characters, but the backspace/delete problem still occurs.

If I modify the TEXT variable by replacing the ‘f’ character in the word ‘first’ with another character (‘x’, say), then when I run the program again, the cursor moves correctly and the backspace key does delete the character to the left of the caret. The combination of letters that cause the problem to occur is quite specific, but this is not the only combination that I have seen cause it.

What made me notice the problem in the first place was that I was trying to underline spelling errors, but in several cases the underlining didn’t line up with the word it applied to (it was often displaced one character to the right).

I guess I will have to see if a monospace font will work, although it won’t look as good in the application I am working on.

I am running on Python 3.8.10 + wxPython 4.1.1 gtk3 (phoenix) wxWidgets 3.1.5 + Linux Mint 20.3

Hi, Richard
There is no problem with Windows 10.
Does this problem occur on the latest SciTE 5.1.6/Scintilla:5.1.5 on Linux?
The latest wx.stc is a wrapper of Scintilla 3.7.2

>>> wx.version()
'4.1.1 msw (phoenix) wxWidgets 3.1.5'
>>> wx.stc.StyledTextCtrl.GetLibraryVersionInfo().VersionString
'Scintilla 3.7.2'

Hi Kazuya,

Thanks for the info about Windows 10. My wxPython install has the same version of Scintilla:

wx.version()
'4.1.1 gtk3 (phoenix) wxWidgets 3.1.5'
wx.stc.StyledTextCtrl.GetLibraryVersionInfo().VersionString
'Scintilla 3.7.2'

I don’t know how to test with the latest Scintilla?

My experiments with monospaced fonts look promising - no misplaced caret and the underlining of spelling errors has lined up with the relevant words so far.

I wondered if the current problem is with wx or Linux. The OP was over 10 years ago and is now in Phoenix time.

If we want to test the latest Scintilla with wx.stc, we have to wait for the next build version of Phoenix.
SciTE is a free text editor based on the latest Scintilla, and you can apt-get install it.

I was having the same thoughts.

In the meantime I down loaded the source code and built Scite Version 5.1.6 + Scintilla:5.1.5 + Lexilla:5.1.4, for GTK:3.24.20.

It runs OK. I pasted the paragraph that was giving me problems. As I expected, the caret position & delete work fine in Scite itself.

It looks likely that there is still an incompatibility between fractional character widths in GTK vs integer widths in wx.

Just an update to say that, unlike the default font on my system, not all proportional fonts exhibit the problem :grinning:.

Two that I have found so far that don’t have the problem are ‘Liberation Sans’ and ‘Liberation Serif’.

Which is a relief, as my app didn’t look very good using a monospace font!