Hi,
I have examined with interest the Alex project : “Any way to insert graphic into TextArea??” here : https://forum.xojo.com/conversation/post/180824
I need 3 routines :
- Import a flattened (serialized, with embedded pictures) rtfd from a database (or a file) into a Textarea
- Export a flattened rtfd to a field in a database or a file
- Insert a Xojo Picture into a TextArea, and later export the whole flattened RTFD to database
To insert an image, I use this code :
dim p as Picture = Canvas1.Backdrop
CGImage1 = p.CopyOSHandle( Picture.HandleType.MacCGImage)
dim zeroSize as Cocoa.NSSize // (zeroSize to create an NSImage of the same size as the CGImage)
Dim NSImage1 As Ptr = initWithCGImage(alloc(Cocoa.NSClassFromString(“NSImage”)), CGImage1, zeroSize)
Dim NSData1 As Ptr = TIFFRepresentation (NSImage1)
Dim NSFileWrapper1 As Ptr = initRegularFileWithContentsShort ( alloc ( Cocoa.NSClassFromString (“NSFileWrapper”)), NSData1)
setPreferredFilename (NSFileWrapper1, “NSFileWrapper1.tiff”)
// construct an NSTextAttachment for the NSFileWrapper
pNSTextAttachment = initWithFileWrapper ( alloc ( Cocoa.NSClassFromString (“NSTextAttachment”)), NSFileWrapper1)
pNSAttributedString = attributedStringWithAttachment (Cocoa.NSClassFromString (“NSAttributedString”), pNSTextAttachment)
If containsAttachments (pNSAttributedString) then
dim myTextAreaPtr as ptr = documentView(fldText.Handle)
dim NSTextStorage1 as Ptr = instanceTextStorage ( myTextAreaPtr )
dim range as Cocoa.NSRange
range.location = 0
range.length = len(fldText.Text)
[b]replaceCharactersInRangeAttStr[/b] (NSTextStorage1, range, pNSAttributedString)
OR
[b]appendAttributedString[/b] (NSTextStorage1, pNSAttributedString)
OR
[b]setAttributedString[/b] (NSTextStorage1, pNSAttributedString)
End If
Both replaceCharactersInRangeAttStr and appendAttributedString work properly, but the first one allow insertion where I want. The problem is that replaceCharactersInRange comes from Carbon libs :
From Carbon => has -withAttributedString parameter
declare sub replaceCharactersInRangeAttStr lib CarbonLib selector “replaceCharactersInRange:withAttributedString:” ( obj_id As Ptr, aRange As Cocoa.NSRange, attrString As Ptr )
From Cocoa => There is another function from Cocoa Lib with the same name allowing only 3 possible parameters (-WithRTF, -WithRTFD, -WithString) and no -withAttributedString
declare sub replaceCharactersInRangeRTF lib “Cocoa” selector “replaceCharactersInRange:withRTF::” (obj_id as Ptr, range as Cocoa.NSRange, rtfData as Ptr)
So do I have to use a Carbon function in my Cocoa app ?
Any other way to insert a NSAttributedString into a NSTextStorage (into a Textarea) ?
Thanks