rtf files

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

rtf files

Post by spacebuddy »

Is there any way to save an rtf file using the EditorGadGet?

I can't find anything in the help file. :oops:
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: rtf files

Post by Danilo »

Try this to get the RTF as string:

Code: Select all

Procedure LoadRTF(gadget, filename.s)
    CocoaMessage(0,GadgetID(gadget),"readRTFDFromFile:$",@filename)
EndProcedure

Procedure.s GetEditorRTF(gadget)
    Protected range.NSRange, result.s, length, theData
    range\location = 0
    range\length   = Len(GetGadgetText(gadget))
    CocoaMessage(@nsData,GadgetID(gadget),"RTFFromRange:@",@range)
    If nsData
        length  = CocoaMessage(0,nsData,"length")
        theData = CocoaMessage(0,nsData,"bytes")
        result = PeekS(theData,length,#PB_UTF8) ; #PB_Ascii or #PB_UTF8 ?
    EndIf
    ProcedureReturn result
EndProcedure

Procedure Btn1_Clicked()
    Protected filename.s = OpenFileRequester("Open RTF","","*.rtf",0)
    If filename
        LoadRTF(0,filename)
    EndIf
EndProcedure

Procedure Btn2_Clicked()
    Protected rtf.s = GetEditorRTF(0)
    Debug rtf
    Debug "-[RTF ends]---------------------------------------------------"
EndProcedure


If OpenWindow(0, 0, 0, 640, 480, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 0, 0, 640, 455)
    
    CocoaMessage(0,GadgetID(0),"setRichText:",#YES)                      ; enable RichText
    CocoaMessage(0,GadgetID(0),"setAutomaticLinkDetectionEnabled:",#YES) ; Link detection
    CocoaMessage(0,GadgetID(0),"setImportsGraphics:",#YES)               ; import images
    ;CocoaMessage(0,GadgetID(0),"setAllowsImageEditing:",#YES)            ; allow image editing
    
    ;CocoaMessage(0,GadgetID(0),"setRulerVisible:",#YES) ; enable Ruler
    ;CocoaMessage(0,GadgetID(0),"setUsesRuler:",#YES)    ; enable Ruler
    ;CocoaMessage(0,GadgetID(0),"updateRuler")           ; update Ruler
    
    ButtonGadget(1,440,455,100,25,"Load RTF")
    BindGadgetEvent(1,@Btn1_Clicked())
    
    ButtonGadget(2,540,455,100,25,"Get RTF")
    BindGadgetEvent(2,@Btn2_Clicked())
    
    For a = 0 To 5
        AddGadgetItem(0, a, "Line "+Str(a))
    Next
    
    SetActiveGadget(0)
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

Re: rtf files

Post by spacebuddy »

Thanks Danilo, that works perfect. But how do I save the rtf file to disk? When I save, it saves in regular text. :shock:
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: rtf files

Post by Danilo »

Just save the string?

Code: Select all

Procedure Btn2_Clicked()
    Protected rtf.s = GetEditorRTF(0)
    Debug rtf
    Debug "-[RTF ends]---------------------------------------------------"
    If CreateFile(0,GetHomeDirectory()+"output_test.rtf")
        WriteString(0,rtf,#PB_UTF8)
        CloseFile(0)
    EndIf
EndProcedure
Another one, writes RTF with attachments to directory:

Code: Select all

Procedure SaveRTF(gadget, directoryName.s)
    CocoaMessage(0,GadgetID(gadget),"writeRTFDToFile:$",@directoryName,"atomically:",#NO)
EndProcedure

SaveRTF(0,GetHomeDirectory()+"output_test_2")
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

Re: rtf files

Post by spacebuddy »

Dear Danilo,

Yes, it is working very goods now.

Thank you :D :D
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: rtf files

Post by wilbert »

It's also possible to use the textStorage object from the EditorGadget.
It makes it possible to use one procedure to allow for writing multiple file types.

Code: Select all

Procedure SaveFormattedText(EditorGadget, FileName.s, Type.s = "NSRTF")
  
  ; Type can be "NSPlainText", "NSRTF", "NSHTML", "NSDocFormat", "NSWordML", "NSOfficeOpenXML", "NSOpenDocument"
  
  Protected.i range.NSRange, attributes, dataObj, textStorage = CocoaMessage(0, GadgetID(EditorGadget), "textStorage")
  CocoaMessage(@range\length, textStorage, "length")
  CocoaMessage(@attributes, 0, "NSDictionary dictionaryWithObject:$", @Type, "forKey:$", @"DocumentType")
  CocoaMessage(@dataObj, textStorage, "dataFromRange:@", @range, "documentAttributes:", attributes, "error:", #Null)
  ProcedureReturn CocoaMessage(0, dataObj, "writeToFile:$", @FileName, "atomically:", #NO)
  
EndProcedure
Example :

Code: Select all

SaveFormattedText(0, "MyFile.rtf", "NSRTF"); save as rtf file
Last edited by wilbert on Thu Jul 10, 2014 6:46 am, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: rtf files

Post by Danilo »

Write the RTF buffer directly, without PeekS()/UTF8 conversion:

Code: Select all

Procedure SaveRTFBuffer(gadget, filename.s)
    Protected range.NSRange, result, length, theData, file
    range\location = 0
    range\length   = Len(GetGadgetText(gadget))
    CocoaMessage(@nsData,GadgetID(gadget),"RTFFromRange:@",@range)
    If nsData
        length  = CocoaMessage(0,nsData,"length")
        theData = CocoaMessage(0,nsData,"bytes")
        If theData
            file = CreateFile(#PB_Any,filename)
            If file
                WriteData(file,theData,length)
                CloseFile(file)
                result = #True
            EndIf
        EndIf
    EndIf
    ProcedureReturn result    
EndProcedure
EDIT: Thanks wilbert, textStorage.length was what I was looking for to replace the ugly "length = Len(GetGadgetText(gadget))". :)
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

Re: rtf files

Post by spacebuddy »

I notice one thing with EditorGadget, it has no undo feature. If I delete text then use Command Z to undo it not work.

Is there a special flag to turn this on?
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: rtf files

Post by Danilo »

Code: Select all

    CocoaMessage(0,GadgetID(0),"setAllowsUndo:",#YES) ; allow UNDO
:)
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

Re: rtf files

Post by spacebuddy »

yes, that work :D

My editor is nearly ready to use

One thing I did notice, if I right click and select "Import" image then try to save this, the image not included? :shock:
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

Re: rtf files

Post by spacebuddy »

I have been playing with the EditorGadget saving to .rtf format. Now I am trying to detect if there is an attachment (picture) in the EditorGadGet. I looked at the NSText Class reference
and could not find anything. Maybe I am missing something? :shock:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: rtf files

Post by wilbert »

You need the textStorage property of the EditorGadget.
On the textStorage object, you can use the methods found in the NSAttributedString AppKit Additions Reference.
https://developer.apple.com/library/mac ... rence.html
Windows (x64)
Raspberry Pi OS (Arm64)
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

Re: rtf files

Post by spacebuddy »

Thanks Wilbert :D
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: rtf files

Post by BarryG »

[Deleted, answer given in another post]
Last edited by BarryG on Sun Sep 22, 2019 11:30 am, edited 1 time in total.
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: rtf files

Post by C87 »

As an aside on RTF files ; they cannot contain a virus load. :D
If it's falling over......just remember the computer is never wrong!
Post Reply