[Gambas-user] TextEdit could do with RichTextPos

Mike Crean mike.crean at y7mail.com
Thu Jun 16 17:37:05 CEST 2022


Hey Bruce, its a good idea if you explicitly declare a veritable to declare them all as some of our micro controller friends go off with the pixies from time to time.

eg Dim iLPos As Integer = 0 or   Dim sChar As Stringr = ""

 

    On Thursday, 16 June 2022, 10:18:05 pm AWST, Bruce Steers <bsteers4 at gmail.com> wrote:  
 
 My GetRTPos() function is flawed :(have tried many a thing but cannot seem to perfect it :'(
It seems to work unless the cursor is at the end of a line.
I have attached a WIP richtext editor i have started.the function GetRTPos() is in the _TextEdit_Styles.class
If anyone feels they could improve on what i am doing here please feel free (especially Ben ;) )
my objective is to be able to detect attributes to richtext text from the standard text cursor position.the GetRTPos() function gives the richtext position relative to the text position by skipping any html code apart from <br> and incrementing a char position pointer when reading a standard character.

the Detect() function then backsteps from richtext position to find the <span style=..> code (using defaults if it finds </span> before <span>)

Something like this is sorely needed with TextEdit Ben.the control is more like a richtext "writer" than an "editor" when it comes to coding. writing html code to the control is easy enough but there is no easy way to get attribs.

Just a way to accurately (more than me) get the Richtext position relative to text position would open the doors for some of my sketchy code to detect attribs at pos.but if you think you could easily enough implement somethinjg like this I think it would greatly enhance the control.
RespectsBruceS

On Thu, 16 Jun 2022 at 08:56, Bruce Steers <bsteers4 at gmail.com> wrote:



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...<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "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?
RespectsBruceS

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 -1

End

 
----[ http://gambaswiki.org/wiki/doc/netiquette ]----
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220616/03b8a09d/attachment.htm>


More information about the User mailing list