rtf files
-
- Enthusiast
- Posts: 347
- Joined: Thu Jul 02, 2009 5:42 am
rtf files
Is there any way to save an rtf file using the EditorGadGet?
I can't find anything in the help file.
I can't find anything in the help file.
Re: rtf files
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
-
- Enthusiast
- Posts: 347
- Joined: Thu Jul 02, 2009 5:42 am
Re: rtf files
Thanks Danilo, that works perfect. But how do I save the rtf file to disk? When I save, it saves in regular text.
Re: rtf files
Just save the string?
Another one, writes RTF with attachments to directory:
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
Code: Select all
Procedure SaveRTF(gadget, directoryName.s)
CocoaMessage(0,GadgetID(gadget),"writeRTFDToFile:$",@directoryName,"atomically:",#NO)
EndProcedure
SaveRTF(0,GetHomeDirectory()+"output_test_2")
-
- Enthusiast
- Posts: 347
- Joined: Thu Jul 02, 2009 5:42 am
Re: rtf files
Dear Danilo,
Yes, it is working very goods now.
Thank you
Yes, it is working very goods now.
Thank you
Re: rtf files
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.
Example :
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
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)
Raspberry Pi OS (Arm64)
Re: rtf files
Write the RTF buffer directly, without PeekS()/UTF8 conversion:
EDIT: Thanks wilbert, textStorage.length was what I was looking for to replace the ugly "length = Len(GetGadgetText(gadget))".
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
-
- Enthusiast
- Posts: 347
- Joined: Thu Jul 02, 2009 5:42 am
Re: rtf files
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?
Is there a special flag to turn this on?
Re: rtf files
Code: Select all
CocoaMessage(0,GadgetID(0),"setAllowsUndo:",#YES) ; allow UNDO
-
- Enthusiast
- Posts: 347
- Joined: Thu Jul 02, 2009 5:42 am
Re: rtf files
yes, that work
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?
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?
-
- Enthusiast
- Posts: 347
- Joined: Thu Jul 02, 2009 5:42 am
Re: rtf files
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?
and could not find anything. Maybe I am missing something?
Re: rtf files
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
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)
Raspberry Pi OS (Arm64)
Re: rtf files
[Deleted, answer given in another post]
Last edited by BarryG on Sun Sep 22, 2019 11:30 am, edited 1 time in total.
Re: rtf files
As an aside on RTF files ; they cannot contain a virus load.
If it's falling over......just remember the computer is never wrong!