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.