Why is RTF support so AWFUL? (and some work arounds)

Well, the idea was simple, display some styled text in a window and I think this will be super easy… Create the text in WordPad, add styles and paste in a AllowStyled TextArea. WRONG, you can not paste Styled text in a TextArea.

Ok, no problem, save the file as RTF, open it like plain text and save that as a constant and then display it with TextArea1.StyledText.RTFData = Constant AWFULY WRONG:

1) You cant put rtf data (plaint text) in a constant.
Yet another of the problems of xojo not using native controls the right way (<https://xojo.com/issue/56415>) is that you cant paste rtf data (plaint text) in the constant editor, this is a RichEdit in windows and if you paste the data, even in plain format, it is pasted as a styled text, and then strips all the styled data to saved just the text. (rtf data is just plaint text, it should be posible to save that, but Even worst, this is random, the editor somethimes pastes the plain text, sometimes the styled text, so, it is easier go stright to the work around)

Work Around: Encode the text in Base64 to store it in a constant or prop

Silly Work Around: prepend the rtfdata with random text and/or remove the { before the copy step.

What xojo should do: Allow and use multiline TextBoxes when Styling is not requiered. Or at least dont allow Styling in the IDE

NEXT step, render the RTF data.

Well, now I have my (Base64 encoded) rtf data in plaint text just to be loaded in the TextArea, give it a try and …

2) StyledText.RTFData is Painfully slow and gives an awful rendering.

This time I had a little experience with the RichEdit API when I change the style (the border xojo uses is not the same as the TextBox, nor has a padding so TextAreas loos awful in general) Code for changing the border and add padding is in the <https://xojo.com/issue/56415>

I ended using some API to load the RTF data

[code]Public Sub SetRTF(RtfData64 As String)

Dim rtfData As cString
Dim mb As New MemoryBlock(8)

Const TM_PLAINTEXT = 1
Const TM_RICHTEXT = 2

Const WM_USER = &H400
Const EM_SETTEXTMODE = (WM_USER + 89)
Const EM_SETTEXTEX = (WM_USER + 97)
Const EM_SETMARGINS = &HD3

Const EC_LEFTMARGIN = 1
Const EC_RIGHTMARGIN = 2

Declare Function SendMessageInt Lib “user32.dll” Alias “SendMessageA” (hWnd As Integer, Msg As Integer, wParam As Integer, lParam As Integer) As Integer
Declare Function SendMessagemb Lib “user32.dll” Alias “SendMessageA” (hWnd As Integer, Msg As Integer, wParam As ptr, lParam As cString) As Integer

'Set 16px margin on Left and Right (983055 is 16 on HiWord and 16 in LoWord)
Call SendMessageInt (TxtContenido.Handle, EM_SETMARGINS, EC_LEFTMARGIN+EC_RIGHTMARGIN, 983055)

'Set RICHTEXT mode
Call SendMessageInt (TxtContenido.Handle, EM_SETTEXTMODE, TM_RICHTEXT, 0)

'Set the RTF text
rtfData = DecodeBase64(RtfData64)
Call SendMessagemb(TxtContenido.Handle, EM_SETTEXTEX, mb, rtfData)

End Sub
[/code]

And the results are quite obvious:

Some notes, I didnt want to use a file, this was a short text and should be fine in a constant. I did load the RTF file and gives exactly the same awful render. This text needs to be formated and in a future dinamically loaded, so, a canvas was not a direct option.

This is another example of what should take 2 minutes, but ended in API calls for the lack of support of a feature. RTF is from the 80’s, how long could it take to have it right?

I Hope this could help someone even if it is a windows only solution.

1 Like

Formatted Text Control has much better RTF support. Allows images, hyperlinks, and allows you to create custom objects you can use in your text. https://www.bkeeney.com/formatted-text-control/

in MBS Plugin we have:

Textarea.RTFDataMBS as Memoryblock

für Mac

Textarea.WinRTFDataMBS(SelectionOnly as boolean = false) as string

für Windows.

And we have RTFDataMBS in StyledText class cross platform.

Missing from his post was maybe if you have more more than just 2-3 pages then the RTF control in Xojo gets turtle slow…as in you may need to wait whole minute just to load or save.

@Bob Keeney and @Christian Schmitz , thanks for the sugestion, but for something so basic I prefer no plugins. For now this solution works great.

“StyledText.RTFData is Painfully slow and gives an awful rendering”. Yes even a single page takes several seconds to load (and it renders wrong), but as you pointed, that quikly scales up to minutes with more pages. Using the declares, you cant even see a delay.

As an alternative for saving, it should be possible to use similar code with the EM_GETTEXTEX message

Yvan,

You are registered for so many years; you cannot be surprised for that.

I am surprised: the same project (Xojo 2015r1) running on Windows 10 1909 / El Capitan display far differently the same rtf coming from the same web site / Firefox: on WIndows 10, it is as you said (raw text), on macOS, it display something that looks like styled text (

Title

) appeeared centered / large text, the next paragraph appears as standard, … a TABLE appears as … table !).

Now, if you’ve made a search in this forum (you know the hard to do search), you would have read many things about how bad is rtf.
(Here, for example: https://forum.xojo.com/55220-rtf-status-both-microsoft-and-in-xojo)

And here’s a conversation with a project that uses Declares (on macOS) to display styled text help):
https://forum.xojo.com/15045-arcshape-demonstration-project

RTF as is on Xojo is a long story.

Do you start to understand why there are so many add-ons for rtf ?

At some point in the past, I was thinking that Xojo is a skeleton and one have to buy a ton of stuff to create good looking applications.

Think. To develop an application you need to buy a grid (read below), some more AddOns, a Developer Account at Apple (or similar), a software to sign, notarize and who knows what Apple will invent (AppWrapper)…

And God know what I forgot in the above list.

Nota: I do not know the situation for the other IDE / Development tools and this is a different subject.

A grid: why ?
as is, the ListBox is not able to (but you can do it yourself, provided you have the idea and time to spend).

Move a Column (say, move Column 10 to Column 5),
Adjust the width of a Column (all Columns),
Sort numerically a column (1, 2, 3, 10, 20, 30, 100, 200, 300),
…/…
And use tricks to get a decent header for your ListBox (I used a 1 Row ListBox to create a… ListBox Header):

Indeed, RTF built-in support is rather limited. It does not manage line spacing, and quite a few other things. Plus past 16K, it goes in permanent meditation.

I have been using Bob Keeney’s Formatted Text Control in my Check Writer for years.

It supports RTF much, much better, including pictures.

Finding feature requests and reported bugs one decade old, Half baked features… Nop, not surprised at all.

However it is kind of intriguing. The TextArea should be a multiline TextBox when StyledText is not requiered, instead xojo uses the big RichEdit even for plain multiline text… And when StyledText is actually needed, the parser sucks and the RichEdit is not even used correctly. Genius!

I actually did, so, I knew how awfull the “styled text” is, the lack of decent RTF support and the worksarounds for mac. That is why this was more a workaround post (for windows) than a question one.

Windows controls are notoriously limited, as compared to Mac.

In the effort to offer true cross platform “code once compile several”, I believe Xojo (well, RealBasic at the time), elected to go for the smallest common denominator.

[quote=467554:@Michel Bujardet]Windows controls are notoriously limited, as compared to Mac.

In the effort to offer true cross platform “code once compile several”, I believe Xojo (well, RealBasic at the time), elected to go for the smallest common denominator.[/quote]

What limitations? As you can see in the example code, the Native control has FULL RTF support out of the box. The whole NotePad in windows is just one multiline “TextBox” (a native Edit control) and the WordPad is just a “TextArea” (Native RichEdit control)

More than limitations, looks like xojo is not using them correctly

Xojo Windows was created before 2000 (when I started with Xojo). At the time only Win32 controls existed. And Xojo has remained pretty much the same, until the Direct2D implementation in 2017. But yet, they are not using the latest version of the controls.

I am not saying Xojo should not have made more efforts to better support RTF since. Just that it is an old implementation.

If you really want better support, use FTC.

Maybe old but, that doesnt mean that they cant do a better job using the native controls. Also, what is the point of xojo being from the 90s? The Edit control and the RichEdit control are both Win32 and are both available since windows 95

Fine. Vent away.

Ignoring this rather sterile discussion.