TextArea and RTF

Moved this conversation over to here from the Facebook page, to make it easier to share code and involve more people.

Basically someone on Facebook was enquiring a way to save RTF from a Xojo app, quicker than Xojo’s methods. Here’s some code that will save a RTFD (which can include images) to disk.

[code] Dim nsd as new saveAsDialog
nsd.suggestedFileName = “test.rtfd”

Dim f as folderitem = nsd.showModalwithin( self )

if f <> nil then
declare function documentView lib “AppKit” selector “documentView” ( TextArea as integer ) as integer
declare function writeRTFDToFile lib “AppKit” selector “writeRTFDToFile:atomically:” ( TextArea as integer, file as CFStringRef, atomically as boolean ) as boolean

if writeRTFDToFile( documentView( TextArea1.handle ), f.nativePath, true ) = false then
  MsgBox "Sorry, but an error occured."
end if

end if

Return True[/code]

Reading a RTFD file into a TextArea…

[code] Dim nod as new openDialog
nod.filter = “???”

Dim f as folderitem = nod.showModalWithin( self )
if f <> nil then
declare function documentView lib “AppKit” selector “documentView” ( TextArea as integer ) as integer
declare function readRTFDFromFile lib “AppKit” selector “readRTFDFromFile:” ( TextArea as integer, file as CFStringRef ) as boolean

if readRTFDFromFile( documentView( TextArea1.handle ), f.nativePath ) = false then
  MsgBox "Sorry, but an error occured."
end if

end if

Return True
[/code]

[quote=60542:@Sam Rowlands]Moved this conversation over to here from the Facebook page, to make it easier to share code and involve more people.
[/quote]
Someone should point that person here as I never use Facebook so had no idea folks would / were asking technical questions there

Sure - quote from the original Facebook post.

[quote]Antoon Verleysen
Hey everybody. I am new here. I am 67 years old and I live in Belgium. Normaly I speek Dutch. Since many years I program application in VB6 with SQL Server as database. Now I am trying out xojo. There are some good things, but I have a big problem with the use of rich text with a textarea control. (veryyyyyy sloooowwww ! Can I ask help here, or have I to go the xojo-forum. Kind regards. Antoon.[/quote]

And I already provided similar code o couple of times here:
https://forum.xojo.com/761-please-fix-extraordinarily-slow-parser-for-rtfdata

Here is that guy from Facebook. Hallo everybody !
I read all the posts regarding this problem. But all the samples use a folderitem.
Is it possible to adapt this code so I can read from and write to a blob- or textfield in a sqlite-databasetable ?
Kind regards. Antoon from Belgium (Aalst)

Sam, I tried out your code. It works GREAT. The code is very fast even for very big data.
But how can I adapt this code to read the data from a database and not from a file.
If that is possible, I should be in seventh heaven !!!

Is there a way to insert an image into a text area (perhaps by dropping an image onto it)?

[quote=60666:@Antoon Verleysen]
But how can I adapt this code to read the data from a database and not from a file.
If that is possible, I should be in seventh heaven !!![/quote]

For RTF only (not RTFD) check the code I linked above. There are a couple of methods for get/set data from/to a string.
For RTFD just save the files in a temporary folder and then read it with above method.

Sam, i assume those code you provide is valid for Mac OSX only and not for cross platform use which include Window machine.

@Antoon Verleysen, If you check the link that Massimo pasted above you should find the code for pure RTF. I should have looked in the forums before I posted this as Massimo had solved it a while ago.

@Richard Duke, You got it, OS X only I’m afraid. This code relies on accessing the underlying NSTextView functions on the Mac. I have no idea if there are similar functions on Windows, otherwise just use the Xojo RTF functions or maybe someone else has written one.

I’ve not tried to add an image in a Xojo application. If I drag a image file in, I just get the file name, and I can’t paste images in… Maybe @Joe Ranieri can shed a little bit of light as I’d love to allow my users to insert images too.

Especially now that I know I can load and save RTFD files with images.

Massimo, I pasted your code in my program. But I run it , the following errors occurs :
Module.RTFValue, line 8 - This item does not exist - range.lenght = Len(t.Text)
(in the Function RTFValue…)
Module.RTFValue, line 9 - This item does not exist - range.lenght = Len(t.Text)
(in the Sub RTFValue…)
However in both cases the line just above the ‘error-line’ = Dim range as NSRange

What did I wrong ?

[quote=60890:@Antoon Verleysen]
What did I wrong ?[/quote]

You didn’t read entirely my post.
A NSRange structure is needed and the post explain how to create it.

Massimo, I created a NSRange Structure with the following declarations :
location as integer
lenght as integer
For the scope I used Global.

[quote=60894:@Antoon Verleysen]Massimo, I created a NSRange Structure with the following declarations :
location as integer
lenght as integer
For the scope I used Global.[/quote]

And it should work now. Right?

Massimo, no it’s not working. I did the creation of the structure before the first run.

Hmm, weird.
Please check the spelling on “length” vs “lenght”

Massimo, if this could help, I give you the layout of the added Module

Module1
Methods
ReadRTFDFromFile
RTFValue
(extends t As TextArea)
(extends t As TextArea, assigns value as String)
WriteRTFDtoFile
Structures
NSRange

Massimo, you had right !
The spelling was wrong.
Now it works for reading the data.
But all text is plain.
Colors en Boldtext has disapeared.