TextEdit features in my Cocoa app

  • I recently discovered the very slick D&D support inherent in the
    Apple TextEdit application. Among its' features are the ability to
    D&D finder folders full of all kinds of nested entities. Not only can
    it save all that neat stuff as an RTFD, you can also D&D from the
    text document these "folder objects" back to the finder. Very slick!

    So slick, it makes me want to support the features I need in my
    program to emulate this behavior. Using an NSTextView is where I
    actually discovered this behavior. And it seems to be completely
    automagical ala Coca. Except ...

    D&D from an NSTextView to the finder isn't natively supported. Can
    one of you fine folks on the list just give a hint as to what I must
    do to add this additional capability? I'm just looking for your basic
    best approach angle to achieve this behavior.

    Thanks.

    Chris
  • On 18/09/2007, Chris Heimark <cjheimark...> wrote:
    > I recently discovered the very slick D&D support inherent in the
    > Apple TextEdit application.
    ...
    > So slick, it makes me want to support the features I need in my
    > program to emulate this behavior.

    /Developer/Examples/AppKit/TextEdit

    -- Finlay
  • On Sep 17, 2007, at 5:07 PM, Chris Heimark wrote:

    > D&D from an NSTextView to the finder isn't natively supported. Can
    > one of you fine folks on the list just give a hint as to what I
    > must do to add this additional capability? I'm just looking for
    > your basic best approach angle to achieve this behavior.

    The delegate needs to implement
    textView:writablePasteboardTypesForCell:atIndex: and
    textView:writeCell:atIndex:toPasteboard:type:.  You should be able to
    look at /Developer/Examples/AppKit/TextEdit for examples.  Probably
    the main problem is that you need to have a file in the filesystem in
    order to use NSFilenamesPboardType.

    Douglas Davidson
  • On Sep 17, 2007, at 5:34 PM, Chris Heimark wrote:

    > Douglas, what do you mean by: "Probably the main problem is that
    > you need to have a file in the filesystem in order to use
    > NSFilenamesPboardType." ?
    >

    What I mean is that an attachment in the text system ordinarily has
    an in-memory copy of the file contents.  If you wish to use
    NSFilenamesPboardType on the pasteboard in order to copy this to
    Finder, you will need to have an on-disk representation.  For
    example, TextEdit's dragging of attachments in this way works if the
    attachment has been saved in an RTFD, but not if it has not been saved.

    Douglas Davidson
  • Thanks Douglas and Finlay.

    Douglas, what do you mean by: "Probably the main problem is that you
    need to have a file in the filesystem in order to use
    NSFilenamesPboardType." ?

    On Sep 17, 2007, at 8:16 PM, Douglas Davidson wrote:

    >
    > On Sep 17, 2007, at 5:07 PM, Chris Heimark wrote:
    >
    >> D&D from an NSTextView to the finder isn't natively supported. Can
    >> one of you fine folks on the list just give a hint as to what I
    >> must do to add this additional capability? I'm just looking for
    >> your basic best approach angle to achieve this behavior.
    >
    > The delegate needs to implement
    > textView:writablePasteboardTypesForCell:atIndex: and
    > textView:writeCell:atIndex:toPasteboard:type:.  You should be able
    > to look at /Developer/Examples/AppKit/TextEdit for examples.
    > Probably the main problem is that you need to have a file in the
    > filesystem in order to use NSFilenamesPboardType.
    >
    > Douglas Davidson
    >
  • Finally got around to implementing advice in this thread. Namely, I
    added two drag delegate methods per the TextEdit sample code, per
    below. The sample code runs as expected.

    Same code in my application animates but refuses to light up the +
    sign, indicating willingness to drop unto Finder, though it will
    light a + sign over editor window of XCode (drop leaves no traces
    that drop happened). I can open the very same underlying rtfd file
    (used in my application) in TextEdit.app and it allows me to drag/
    drop any cell element just about anywhere. Visually, my NSTextView
    and TextEdit views look identical with same underlying RTFD.

    Is there some magical setting I am not making to my NSTextView object
    that I am missing? I have noticed one peculiar thing, which I don't
    know if it is related, but I noticed writablePasteboardTypesForCell
    is called twice, with same parameters, at the beginning of the drag.
    Is this a hint to me?

    Puzzled, once again...

    - (NSArray *)textView:(NSTextView *)aTextView
    writablePasteboardTypesForCell:(id <NSTextAttachmentCell>)cell
    atIndex:(unsigned)charIndex
    {
    NSArray *types = nil;
    NSString *name = [[[cell attachment] fileWrapper] filename];
    if (name && ![name isEqualToString:@""])
    {
      types = [NSArray arrayWithObject:NSFilenamesPboardType];
    }
    return types;
    }

    - (BOOL)textView:(NSTextView *)aTextView writeCell:(id
    <NSTextAttachmentCell>)cell atIndex:(unsigned)charIndex toPasteboard:
    (NSPasteboard *)pboard type:(NSString *)type
    {
    BOOL success = NO;
    NSString *name = [[[cell attachment] fileWrapper] filename];
    if (name && ![name isEqualToString:@""] && [type
    isEqualToString:NSFilenamesPboardType])
    {
      NSString *fullPath = [[NSString stringWithFormat:kCHRTFDirectory,
    tempDir] stringByAppendingPathComponent:name];
      success = [pboard setPropertyList:[NSArray
    arrayWithObject:fullPath] forType:NSFilenamesPboardType];
    }
    return success;
    }

    On Sep 17, 2007, at 8:40 PM, Douglas Davidson wrote:

    >
    > On Sep 17, 2007, at 5:34 PM, Chris Heimark wrote:
    >
    >> Douglas, what do you mean by: "Probably the main problem is that
    >> you need to have a file in the filesystem in order to use
    >> NSFilenamesPboardType." ?
    >>
    >
    > What I mean is that an attachment in the text system ordinarily has
    > an in-memory copy of the file contents.  If you wish to use
    > NSFilenamesPboardType on the pasteboard in order to copy this to
    > Finder, you will need to have an on-disk representation.  For
    > example, TextEdit's dragging of attachments in this way works if
    > the attachment has been saved in an RTFD, but not if it has not
    > been saved.
    >
    > Douglas Davidson
  • Seems that difficulty lay in use of NSFileWrapper methods
    serializedRepresentation & initWithSerializedRepresentation - calls
    made on the two sides of a network transport of NSTextView data.
    Because I was testing received goods on the same machine as sending
    machine, but using different directory structure when sending and
    receiving, I needed to perform an NSFileWrapper updateFromPath prior
    to laying RTFD back down to disk and subsequent use by receiving
    NSTextView. Wow, that was a mouthful!

    Anyway, the D&D's are now working correctly.

    On Oct 19, 2007, at 10:24 AM, Chris Heimark wrote:

    > Finally got around to implementing advice in this thread. Namely, I
    > added two drag delegate methods per the TextEdit sample code, per
    > below. The sample code runs as expected.
    >
    > Same code in my application animates but refuses to light up the +
    > sign, indicating willingness to drop unto Finder, though it will
    > light a + sign over editor window of XCode (drop leaves no traces
    > that drop happened). I can open the very same underlying rtfd file
    > (used in my application) in TextEdit.app and it allows me to drag/
    > drop any cell element just about anywhere. Visually, my NSTextView
    > and TextEdit views look identical with same underlying RTFD.
    >
    > Is there some magical setting I am not making to my NSTextView
    > object that I am missing? I have noticed one peculiar thing, which
    > I don't know if it is related, but I noticed
    > writablePasteboardTypesForCell is called twice, with same
    > parameters, at the beginning of the drag. Is this a hint to me?
    >
    > SNIP...

    > On Sep 17, 2007, at 8:40 PM, Douglas Davidson wrote:
    >
    >>
    >> On Sep 17, 2007, at 5:34 PM, Chris Heimark wrote:
    >>
    >>> Douglas, what do you mean by: "Probably the main problem is that
    >>> you need to have a file in the filesystem in order to use
    >>> NSFilenamesPboardType." ?
    >>>
    >>
    >> What I mean is that an attachment in the text system ordinarily
    >> has an in-memory copy of the file contents.  If you wish to use
    >> NSFilenamesPboardType on the pasteboard in order to copy this to
    >> Finder, you will need to have an on-disk representation.  For
    >> example, TextEdit's dragging of attachments in this way works if
    >> the attachment has been saved in an RTFD, but not if it has not
    >> been saved.
    >>
    >> Douglas Davidson
    >
previous month september 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