Replacing the standard NSTextAttachmentCell in an NSTextView

  • Hello,

    I'm currently researching how to provide a custom NSCell to replace
    the current NSTextAttachentCell in an NSTextView.
    The reason is that I want to have a different type of attachment not
    just for images or file objects, but for actual objects in my
    application.
    IOW, something similar to NSTokens (actually I'm using an
    NSTokenFieldCell subclass) that can represent  actual "objects" in a
    text field...

    So, I have insertion working, and the cell fulfills the basic protocol
    for NSTextAttachment.
    The problem is, when cutting and pasting my object back into the text
    field, it reverts to a standard NSTextAttachmentCell....
    How can I override this to insert my special cell?
    The problem I am running into is that the NSTextAttachment requires an
    NSFileWrapper, but since I am not representing a file object, I tested
    it out using:
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc]
    initWithFileWrapper:nil];
    In the above case, paste then doesn't even work....

    Also, when reading the .h for NSTextAttachment, its states for the
    NSTextAttachmentCell:

    /* Simple class to provide basic attachment cell functionality. By
    default this class causes NSTextView to send out delegate messages
    when the attachment is clicked on or dragged.
      */

    How can I get this functionality in my custom cell?
    I'm assuming that through this method:

    - (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:
    (NSView *)controlView atCharacterIndex:(NSUInteger)charIndex
    untilMouseUp:(BOOL)flag;

    How can I get the aforementioned double-click functionality in my
    custom cell? I know its not "legal" to do something like [[(NSTextView
    *)controlView delegate] SOME_DELEGATE_METHOD] so I'm curious what is
    the best way to go about doing this.... I would like the delegate to
    have a chance to do something as well when double clicked or edited....

    Especially the case of pasted text containing values originally
    representing my special cell, I would like the pasted text to use that
    special cell, currently I can't get that working...

    Thanks for any pointers.

    Andre
  • On Nov 6, 2007, at 6:34 AM, <listposter...> wrote:

    > I'm currently researching how to provide a custom NSCell to replace
    > the current NSTextAttachentCell in an NSTextView.
    > The reason is that I want to have a different type of attachment not
    > just for images or file objects, but for actual objects in my
    > application.
    > IOW, something similar to NSTokens (actually I'm using an
    > NSTokenFieldCell subclass) that can represent  actual "objects" in a
    > text field...
    >
    > So, I have insertion working, and the cell fulfills the basic
    > protocol for NSTextAttachment.
    > The problem is, when cutting and pasting my object back into the
    > text field, it reverts to a standard NSTextAttachmentCell....
    > How can I override this to insert my special cell?
    > The problem I am running into is that the NSTextAttachment requires
    > an NSFileWrapper, but since I am not representing a file object, I
    > tested it out using:
    > NSTextAttachment *textAttachment = [[NSTextAttachment alloc]
    > initWithFileWrapper:nil];
    > In the above case, paste then doesn't even work....

    The issue here has to do with the formats used to store text on the
    pasteboard.  Copy/paste can potentially take place between any two
    applications, so the pasteboard generally uses application-independent
    formats for the interchange of data.  In this case, attachments are
    stored for pasteboard purposes as file contents.  One option for you
    is to decide on a file format to represent the contents of your custom
    attachments, and use that as the contents of the file wrapper for copy/
    paste purposes.  In this, you should consider what you want to happen
    when text is copied and pasted from your application to another
    application--is there some suitable representation for your custom
    attachments that other applications would understand?  Your
    application could then recognize these files by some means--by
    contents, or perhaps by extension--when they are pasted in, and
    substitute your custom attachment.

    Douglas Davidson
  • Thank you Douglas for your reply.

    On $BJ?@.(B 19/11/07, at 2:10, Douglas Davidson wrote:

    >
    > On Nov 6, 2007, at 6:34 AM, <listposter...> wrote:
    >
    >> I'm currently researching how to provide a custom NSCell to replace
    >> the current NSTextAttachentCell in an NSTextView.
    >> The reason is that I want to have a different type of attachment
    >> not just for images or file objects, but for actual objects in my
    >> application.
    >> IOW, something similar to NSTokens (actually I'm using an
    >> NSTokenFieldCell subclass) that can represent  actual "objects" in
    >> a text field...
    >>
    >> So, I have insertion working, and the cell fulfills the basic
    >> protocol for NSTextAttachment.
    >> The problem is, when cutting and pasting my object back into the
    >> text field, it reverts to a standard NSTextAttachmentCell....
    >> How can I override this to insert my special cell?
    >> The problem I am running into is that the NSTextAttachment requires
    >> an NSFileWrapper, but since I am not representing a file object, I
    >> tested it out using:
    >> NSTextAttachment *textAttachment = [[NSTextAttachment alloc]
    >> initWithFileWrapper:nil];
    >> In the above case, paste then doesn't even work....
    >
    > The issue here has to do with the formats used to store text on the
    > pasteboard.  Copy/paste can potentially take place between any two
    > applications, so the pasteboard generally uses application-
    > independent formats for the interchange of data.  In this case,
    > attachments are stored for pasteboard purposes as file contents.
    > One option for you is to decide on a file format to represent the
    > contents of your custom attachments, and use that as the contents of
    > the file wrapper for copy/paste purposes.  In this, you should
    > consider what you want to happen when text is copied and pasted from
    > your application to another application--is there some suitable
    > representation for your custom attachments that other applications
    > would understand?  Your application could then recognize these files
    > by some means--by contents, or perhaps by extension--when they are
    > pasted in, and substitute your custom attachment.

    > The issue here has to do with the formats used to store text on the
    > pasteboard.  Copy/paste can potentially take place between any two
    > applications, so the pasteboard generally uses application-
    > independent formats for the interchange of data.  In this case,
    > attachments are stored for pasteboard purposes as file contents.
    > One option for you is to decide on a file format to represent the
    > contents of your custom attachments, and use that as the contents of
    > the file wrapper for copy/paste purposes.
    Well, since I am using Core Data to represent the attachments in the
    text view, I have decided to copy the URI's of the objects to the clip
    board (one for each text attachment) using the delegate method
    textView:writeCell:atIndex:toPasteboard:type:. The objects will have a
    string value, so I'm thinking I can, when pasting to the pasteboard,
    paste the URL and its text value, then when pasting back, I can read
    the URI and check if its a coredata URI (NSURL).

    The tricky part for me is, when geting the pasteboard data, when its
    pasted back (copy and paste into the same custom text view), I would
    like to make sure those coredata objects are represented with my
    special NSCell subclass. So what I'm curious is: in
    readSelectionFromPasteboard:type: , does it call this for each
    attachment, or once on a per-paste operation basis?

    What I'm hoping I can do is, when the text is pasted back, I can do
    the following:

    Ignore plain text and let the text view paste it in
    When it encounters an attachment, it calls
    readSelectionFromPasteboard:type: for each attachment
    I can then call  -rangeForUserTextChange and insert each NSCell at the
    given insertion point for the attachment....

    Ideally, a finer grained method such as:
    - (NSCell *)textView:(NSTextView *)aTextView
    customCellRepresentingItemFromPBoard:(NSPasteboard *)pboard  ofType:
    (NSString *)type  atIndex:(NSUInteger)charIndex

    Would be ideal........

    Thanks for all you help. Its appreciated.

    Andre

    >
    > Douglas Davidson
    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today