Professional Web Applications Themes

Move or copy a custom object via drag and drop - Mac Programming

Hello NG, In my app I have two NSTableView representing the name of a custom object. The datasource of the tables are two different instances of the the same class ( containing a array of my object ). Now I want to copy a object from one array to the other via drag and drop. The pasteboard is, as far as I see, not able to handle id's. Could I convert the id into NSData or NSString and back ? What is right way ? Thank you for your time and help...

  1. #1

    Default Move or copy a custom object via drag and drop

    Hello NG,

    In my app I have two NSTableView representing the name of a custom object. The
    datasource of the tables are two different instances of the the same class ( containing a array of
    my object ). Now I want to copy a object from one array to the other via drag and drop. The
    pasteboard is, as far as I see, not able to handle id's. Could I convert the id into NSData or
    NSString and back ? What is right way ?

    Thank you for your time and help

    Florian Guest

  2. #2

    Default Re: Move or copy a custom object via drag and drop

    Florian Zschocke wrote: 

    there's already a system in place to allow drag and drop between
    NSTableViews. look at the NSTableDataSource protocol. a few methods
    like [NSTableDataSource tableView:acceptDrop:row:dropOperation:] must be
    implemented, then the framework will take care of the rest.
    Jhnny Guest

  3. #3

    Default Re: Move or copy a custom object via drag and drop

    Jhnny Fvrt (it means "halo, then resonate") <com> wrote: 
    Thank you Jhnny.

    I did not test that but from reading the examples I would say it will copy only the row
    data and not the hole object.
    Of course I could send a unique string to pasteboard and than write a function to get
    the id from the string -but that would't be nice.

    Florian

    Florian Guest

  4. #4

    Default Re: Move or copy a custom object via drag and drop

    Florian Zschocke wrote:
    I did not test that but from reading the examples I would say it will 

    i haven't tried it either, yet, although i'll eventually have to. and
    my gut feeling is that you're misinterpreting. if you want to "copy the
    whole object," then you can do so in one of those methods! they are
    basically just hooks that get called at various times during a drag
    operation to or from an NSTableView.
     

    you're really barking up the wrong tree there. you don't want to put
    anything like an id or any type of NSObject pointer data on the
    clipboard. an id is just a pointer, which points into your app's memory
    space, therefore it is useless and potentially dangerous to any other
    app.

    the clipboard is a global systemwide place for apps to share things.
    what you're talking about should be handled via local variables. you
    could use the NSTableDataSource hook functions to make note of when a
    drag starts in one place, and then save some kind of magic cookie that
    tells you what's being dragged, and use that again when another hook
    function gets called to indicate a drop. but save it *locally*, not on
    the clipboard.
    Jhnny Guest

  5. #5

    Default Re: Move or copy a custom object via drag and drop

    Jhnny Fvrt (it means "halo, then resonate") <com> wrote: 
    Thank you very much

    .... a few hours later I thought for I me it is fine if I also can have drags from outside.
    So I went over and took the

    -(BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows
    toPasteboard (NSPasteboard*)pboard {

    function to write a NSURL to the pasteboard. The content of my table are newsgroups.
    So i take the name of the newsgroup and make a NSURL like
    news:comp.sys.mac.programmer.help and use the -writeToPasteBord function to write it into the pastebord.
    On the other hand - now - I want a NSWindow to take that drop. I wrote a -draggedEntered,
    perfromDragOperation e.c.t., even registerd the window for NSURLPboardType. Now the window accepts the a
    URL drop from outside but not from my table.

    Thank you - perhaps it is better to make a new news thread but I first will wait for your
    answere.

    Florian Guest

  6. #6

    Default Re: Move or copy a custom object via drag and drop

    Jhnny Fvrt (it means "halo, then resonate") <com> wrote: 
    Yep, you are right. Now I convert the data into a NSURL and write it to pasteboard - so I
    can even drag from outside new addresses into the table.

    Thank you again

    Florian Guest

  7. #7

    Default Re: Move or copy a custom object via drag and drop

    Florian Zschocke wrote:
     

    This is definitely a problem, and I disagree with Johnny that you should
    not put ids on a NSPasteboard because many apps have drag and drop that
    only makes sense within the context of the app. Other apps don't need
    it, so passing pointers is fine.

    What I've usually done to get around this is to create an NSData that
    contains an array of ids. Making the last one NULL gets you easier parsing.

    If you do this, be sure to use a custom pasteboard type, of course.

    -Peter

    --
    Pull out a splinter to reply.
    Peter Guest

  8. #8

    Default Re: Move or copy a custom object via drag and drop

    Peter Ammon wrote: 

    i am not getting how that would be at all productive! the pasteboard is
    supposed to be for inter-app data transfer. yes, there are times when a
    cut and the subsequent paste both occur in the same app, but that's
    incidental.

    if the data you want to transfer is an object pointer, then there is no
    way it could ever be useful to any other app. so why should it be on
    the pasteboard? save it in an app variable instead. all you're doing
    by putting an app pointer on the pasteboard is potentially destroying
    something that the user didn't want overwritten just yet.
    Jhnny Guest

  9. #9

    Default Re: Move or copy a custom object via drag and drop

    In article <40f032fd$0$28607$arcor-online.net>,
    Florian Zschocke <de> wrote:
     

    Probably something simple like not returning/accepting the proper drag
    operation or something like that. Re-read the DnD docs and make sure
    you've done each of the little things, and fiddle with those you're not
    sure of. DnD is a little finicky in Cocoa.

    Oh, and be sure to let us know if you find it. It's always useful to
    know what could be the cause when we come across such a symptom.

    Regarding your original question: There's a rather unpretty way to
    carry along a pointer in a drag: Stuff it in an NSValue or NSNumber. But
    make sure it's a sender-only flavor so it never gets carried along
    outside your application. You could do that *in addition* to the NSURL
    thing. That way, you can detect local drags, treat them specially, and
    e.g. take along some caches or other info when that makes sense.

    OTOH, you can also just keep all that info in your full group list, and
    keep a pointer to the item in there with the newly-created object. Thus
    avoiding lots of unnecessary copies.

    Cheers,
    -- Uli
    http://www.zathras.de
    Uli Guest

  10. #10

    Default Re: Move or copy a custom object via drag and drop

    On Sun, 11 Jul 2004, =?ISO-8859-1?Q?J=F8hnny_F=E4v=F2r=EDt=EA_=28it_means_=22halo= 2C_then_resonate=22=29?= wrote:
     

    There is no "the" pasteboard; there are many. One is used to contain the
    "clipboard", and a completely unrelated one is used to hold drag-and-drop
    data when transferring between applications. Writing something to the DnD
    pasteboard won't affect the user's clipboard.
    Michael Guest

  11. #11

    Default Re: Move or copy a custom object via drag and drop

    Uli Kusterer <de> wrote: 
    Thank you Uli 

    here the code line that was missing:

    [pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType]
    owner:self];

    in the tableView-drag-to-pasteboard function.

     

    Now I found that that is not necessary.
    You can resolve the the data-source in the receiver. In the function:

    -(BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{
    BOOL result = YES;
    id dSource = [sender draggingSource];
    if([dSource isKindOfClass:[NSTableView class]]){
    id dataSource = [dSource dataSource];

    ...e.c.t
    I get back the table source. Additional -draggingSource gives a nil if the sender is
    from outside the application.
     

    That is what I am doing now.

    Thank you. 
    to the dears

    Florian Guest

  12. #12

    Default Re: Move or copy a custom object via drag and drop

    Michael Ash wrote: 

    i knew that you could create your own named clipboards, but i didn't
    know there was a separate one for drag-and-drop. haven't gotten as far
    as adding drag-and-drop to my framework yet, but it will come.
    Jhnny Guest

  13. #13

    Default Re: Move or copy a custom object via drag and drop

    Michael Ash <com> wrote: 
    Thank you Michael 
    But even if it is not a potential risk, I would say it is better to handle the ids inside
    the normal object communication. The <NSDraggingInfo> protocol is very flexible so
    that from my point I can not see any situation were it could be useful to do so. Final I think
    there has to bee a reason why there is no IdPboardType even if can't understand it.

    Florian Guest

  14. #14

    Default Re: Move or copy a custom object via drag and drop

    Michael Ash <com> wrote: 
    Thank you Michael

    But even if it is not a potential risk, I would say it is better to handle the ids inside
    the normal object communication. The <NSDraggingInfo> protocol is very flexible so
    that from my point I can not see any situation were it could be useful to do so. Final I think
    there has to bee a reason why there is no IdPboardType even if can't understand it.

    Florian Guest

  15. #15

    Default Re: Move or copy a custom object via drag and drop

    On Mon, 12 Jul 2004, =?ISO-8859-1?Q?J=F8hnny_F=E4v=F2r=EDt=EA_=28it_means_=22halo= 2C_then_resonate=22=29?= wrote:
     
    >
    > i knew that you could create your own named clipboards, but i didn't
    > know there was a separate one for drag-and-drop. haven't gotten as far
    > as adding drag-and-drop to my framework yet, but it will come.[/ref]

    Simple proof: copy something to the clipboard, then drag something between
    apps, then paste. They're independent, which is rather convenient, both
    for the users and us developers.
    Michael Guest

  16. #16

    Default Re: Move or copy a custom object via drag and drop

    On Mon, 12 Jul 2004, Florian Zschocke wrote:
     
    > Thank you Michael 
    > But even if it is not a potential risk, I would say it is better to handle the ids inside
    > the normal object communication. The <NSDraggingInfo> protocol is very flexible so
    > that from my point I can not see any situation were it could be useful to do so. Final I think
    > there has to bee a reason why there is no IdPboardType even if can't understand it.[/ref]

    I agree 100%. My intention was only to correct that one particular detail,
    not to suggest that writing raw pointers to the pasteboard is a good idea.
    It's not, and your suggest approach is much better.
    Michael Guest

Similar Threads

  1. can custom-made flash components use flex drag-and-drop?
    By jpwrunyan in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: September 29th, 01:22 AM
  2. Custom control won't drag and drop to page
    By Andrea Williams in forum ASP.NET Building Controls
    Replies: 10
    Last Post: March 30th, 05:37 PM
  3. Any Active-X object supporting Drag and drop upload?
    By Eilsa in forum ASP Components
    Replies: 0
    Last Post: November 23rd, 02:33 AM
  4. OPTION + ARROW Doesn't move copy of selected object
    By David_Najdowski@adobeforums.com in forum Adobe Illustrator Macintosh
    Replies: 2
    Last Post: May 27th, 06:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139