How to open file in TextArea without losing formatting?

Hello,

I have a window, myWindow, W is used like
Dim W as myWindow
W = New myWindow

W is the the window that contains MyTextArea which is a TextArea - Multiline and Styled are “on”

I have a file, TheFile = and its path is “/Users/len/Documents/my File”
TheFile = GetFolderItem(TheFilePath, FolderItem.PathTypeShell)

I used Call W.MyTextArea.Open(TheFile)
to put TheFile contents into W.MyTextArea, it works well in Carbon but seems to lose formatting with Cocoa

How should I refactor my code to get the contents of TheFile displayed correctly in MyTextArea with Cocoa?

Thanks.

Lennox

[quote=105744:@Lennox Jacob]Hello,

I have a window, myWindow, W is used like
Dim W as myWindow
W = New myWindow

W is the the window that contains MyTextArea which is a TextArea - Multiline and Styled are “on”

I have a file, TheFile = and its path is “/Users/len/Documents/my File”
TheFile = GetFolderItem(TheFilePath, FolderItem.PathTypeShell)

I used Call W.MyTextArea.Open(TheFile)
to put TheFile contents into W.MyTextArea, it works well in Carbon but seems to lose formatting with Cocoa

How should I refactor my code to get the contents of TheFile displayed correctly in MyTextArea with Cocoa?
[/quote]

If you could post the whole code, it would help a great deal.

Textarea.Open is deprecated.
Did you read docs?

http://documentation.xojo.com/index.php/TextArea.Open

Hi Christian,
Thanks, but got the same result.

Hi Michael,
Thanks, its in a larger app, I will simplify it and send you a link to download the project.

Lennox

[quote=105756:@Lennox Jacob]its in a larger app, I will simplify it and send you a link to download the project.
/quote]

If you can keep just the essential, that is fine.

I personally use code derived from the example at http://documentation.xojo.com/index.php/StyledText.RTFData

Hi Michael,

I’ve sent you the details but it seems to have gone under “Customer Service” by the name “just the essential”.

I was trying to send a “private conversation” and that is how it happened.

Thanks.

Lennox

[quote=105790:@Lennox Jacob]Hi Michael,

I’ve sent you the details but it seems to have gone under “Customer Service” by the name “just the essential”.

I was trying to send a “private conversation” and that is how it happened.
[/quote]

It worked fine and I was able to retrieve it. Thank you.

What happens is that your method uses an obsolete text format (SimpleText ?) that is not even recognized by Lion. There is no way to load that as is with the current methods available to Cocoa.

The best course of action if you want to move your app to Cocoa is to first convert to RTF by loading with your current method and saving to RTF. See http://documentation.xojo.com/index.php/StyledText.RTFData

I looked at the file, though, and it appears in a text editor as plain text (why I think it may be SimpleText) so it can be loaded as plain text, but then you will loose the underline. Conversion is probably better.

Thanks Michel,

If that what is displayed in the TextArea under Carbon is printed it will fit nicely on one page of 8.5 x 11 paper, under Cocoa it will go over to two pages and that is not what was intended.

I will look into the link you provided and see how best I can work it out.

Thanks again.

Lennox

Hi Michel,

I see this in the link…
The following code, appended to the end of the previous method, saves the styled text to disk in RTF format. It assumes that the File Type Set, “TextTypes” has one item, TextRTF, that defines the RTF file type.

Dim f as FolderItem=GetSaveFolderItem(TextTypes.TextRtf,“TestSaveRTF”)
If f <> nil then
Dim s as TextOutputStream=TextOutputStream.Create(f)
s.Write OGNPHandoutsEtcText.StyledText.RTFData
s = nil
End if

and I tried to save the document as you recommended…

I created a new FileType
Display Name TextTypes
Object Name TextTypes
MacType RTF
MacCreator RTF
Extensions .RTF
UTI’s

When I try to compile it I got this error
This item does not exist (TextTypes) Dim f as FolderItem=GetSaveFolderItem(TextTypes.TextRtf,“TestSaveRTF”)

I am at a dead-end. How can I proceed?

Thanks.

Lennox

OK michel,

I saved the file as RTF, how do I open it now?

   If Not W.OGNPHandoutsEtcText.Open(TheFile) Then
    MsgBox("Error opening " + TheFile.Name)
  End If

gives text like this
{\rtf1\ansi\ansicpg1252{\fonttbl{\f0\fnil Times Roman;}}{\colortbl\red0\green0\blue0;}\uc0 \ql\b\f0\fs20 \b0 \b\ul Instructions fo…

Thanks.

Lennox

[quote=105844:@Lennox Jacob]I created a new FileType
Display Name TextTypes
Object Name TextTypes
MacType RTF
MacCreator RTF
Extensions .RTF
UTI’s

When I try to compile it I got this error
This item does not exist (TextTypes) Dim f as FolderItem=GetSaveFolderItem(TextTypes.TextRtf,“TestSaveRTF”)
[/quote]

It is because when you create a file type, the application/rtf type is called simply Rtf. For some reason, the LR mentions TextRtf.

Use this and you will be alright. I also added the extension “.rtf” to the file name. While not absolutely mandatory, it is a good idea to keep consistency with recent documents.

Dim f as FolderItem=GetSaveFolderItem(TextTypes.Rtf,"TestSaveRTF.rtf")

[quote=105860:@Lennox Jacob]I saved the file as RTF, how do I open it now?

If Not W.OGNPHandoutsEtcText.Open(TheFile) Then
MsgBox("Error opening " + TheFile.Name )
End If

gives text like this
{\rtf1\ansi\ansicpg1252{\fonttbl{\f0\fnil Times Roman;}}{\colortbl\red0\green0\blue0;}\uc0 \ql\b\f0\fs20 \b0 \b\ul Instructions fo…[/quote]

Here is the method I use :

[code]Function FileLoadLetter() As Boolean
dim f as folderItem
dim dlg as openDialog
dim t as textInputStream

//create a new openDialog
dlg = new OpenDialog
//set what type of file it looks for
dlg.filter=“application/rtf”

//run the dialog
f = dlg.showModal

//check to make sure the user didn’t click cancel
if f <> nil then
t = TextInputStream.Open(f)
//make sure we could open it
if t <> nil then
//Read all of t into myEditField.text
TextArea1.StyledText.RTFData = t.readAll // field is passed as a TextArea
//close the file so that other applications can use it
t.close
else
//the file could not be a read as a text file
msgBox “The selected file is not a text file.”
end if
else
//the user clicked cancel… just ignore it
end if
Return True

End Function
[/code]

Hope this helps.

Thank Michel,

I did that and got the same result…

I compiled the app under Carbon
I opened the file and saved it as RTF as you suggested
I recompiled the app under Cocoa
I used the method you use above (Here is the method I use :slight_smile:

got the same result… it cannot hold on one page because of the formatting.

Lennox

maybe you could try the example here
Forum - RTF Exampe