[Gambas-user] TextEdit could do with RichTextPos

Bruce Steers bsteers4 at gmail.com
Thu Jun 16 09:56:49 CEST 2022

On Wed, 15 Jun 2022 at 12:35, Bruce Steers <bsteers4 at gmail.com> wrote:

> Been playing with Textedit from qt5.ext trying to make a simple RichText
> editor.
> It would be REALLY useful if there was a way to get the cursor position
> relevant to the RichText property too.
> Ie..
> If i set my RichText to *"<font color=red>This message is red</font>" *and
> i position the cursor on the m of the word message
> then Pos is 5 and TextEdit1.Text is just "This message is red"
> But the RichText property will now be this...
> http://www.w3.org/TR/REC-html40/strict.dtd">
> <html><head><meta name="qrichtext" content="1" /><meta
> http-equiv="Content-Type" content="text/html; charset=utf-8" /><style
> type="text/css">
> p, li { white-space: pre-wrap; }
> </style></head><body style=" font-family:'Ubuntu'; font-size:12pt;
> font-weight:400; font-style:normal;">
> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px;
> margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style="
> color:#008000;">This message is green</span></p></body></html>
> And the cursor Pos there is not 5 it is 529
> If i want to do anything fancy with modifying the Richtext or detecting
> styles/colors at cursor pos having a way to get a Richtext position
> relative to the cursor would be very useful.
> Or having a way to get some info about the text at cursor Ie.
> TextEdit1.GetAttribs(Pos) could get info like font/size/color/etc
> I feel i could write it myself but not without finding where the cursor is
> in the Richtext.
> Or is this all just not possible?
> Respects
> BruceS

If anyone wants the same thing I wrote this function...

*'' Get the position of the TextEdit.RichText from TextEdit.Pos*

*Public Sub GetRTPos(Editor As TextEdit, Optional Pos As Integer) As
Integer  If Not Pos Then Pos = Editor.Pos  If Not Editor.RichText Then
Editor.RichText = Editor.Text  Dim sRT As String = Editor.RichText  sRT =
Replace(sRT, ">", ">")  sRT = Replace(sRT, "<", "<")  Dim sRTLines As
String[] = Split(sRT, "\n")  Dim iPos As Integer = 1  Dim iLPos As Integer
Dim iChars As Integer  Dim sChar As String  Dim bInside As Boolean  iPos =
Len(sRTLines[0]) + Len(sRTLines[1]) + Len(sRTLines[2]) + Len(sRTLines[3]) +
3  For cLine As Integer = 4 To sRTLines.Max    Inc iPos ' newline char
sRT = sRTLines[cLine]    iLPos = 1    While iLPos < sRT.Len      sChar =
Mid(sRT, iLPos, 1)      Select sChar        Case "<"          bInside =
True          If Mid(sRT, iLPos, 6) = "<br />" Then Inc iChars        Case
">"          bInside = False      End Select      If Not bInside And If
sChar <> ">" Then Inc iChars      If iChars = Pos + 1 Then Return iPos
Inc iPos      Inc iLPos    Wend  Next  Return -1End*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220616/a9048436/attachment-0001.htm>

More information about the User mailing list