Drag an drop in a custom NSView

  • Ok,
    I have constructed a subclass of NSView so that I can set the delegate.
    Anyway. The NSView Subclass contains a bunch of subviews which are
    NSButtons. I am having two problems though with drag and drop. I
    registered for drag types, both file names and a custom internal type
    like I normally do with outline views. However i get no focus ring
    nor does it seem I get any reaction at all when I drag a file to the
    view. I've tried registering the enclosing scroll view for drag
    types, but still nothing. So basically its obvious I don't know how
    to tell the view to detect the drag, or if I have, I don't know how
    to gain the data from the drag.
    The other problem is in the view containing NSButtons. Each button is
    inside an NSView however if I add the nsviews to the subclass of
    NSView I've been using inside the scroller then I get nothing at all,
    so I have to create the button, and then only add it. This makes it
    impossible to drag the buttons. However I need to be able to drag the
    buttons, or at least represent dragging them, so that the files they
    correspond to can be moved to new folders.
    I tried to use just an NSImageView instead of buttons but I couldn't
    seen to get it to accept mouse clicks which I also need it to do so
    that I can have the file represented open on the screen.

    Does any one know of some information on doing this (if i've made
    clear what I'm trying to do.) or maybe so examples? I've spent the
    last couple of hours on the developer site and google and cannot find
    anything that appears relevant.

    The only other alternative I see is to use a tableview and monkey
    with the row heights so I can display the document thumbs...
    something I dont want to do.

    Thanks
  • On 10/9/07, Development <development...> wrote:
    > Ok,
    < huge, resounding SNIP! >

      Okay, first and foremost, your entire post is lacking crucial
    details and any help you receive will be just as vague and probably
    not too helpful. ABSOLUTELY POSITIVELY read this (because it will help
    you to get good, solid help):

    http://www.catb.org/~esr/faqs/smart-questions.html

      Second, drag and drop in general:

    http://developer.apple.com/documentation/Cocoa/Conceptual/DragandDrop/index
    .html


      Third, dragging things like buttons is a semi-FAQ and you can find
    many threads in this list's archives if you search around a bit:
    http://www.cocoabuilder.com/

      Fourth, focus rings ... where are you expecting them to show up?
    Under what conditions? Are you sure a focus ring is what you want or
    do you want some kind of other indicator (which you'd have to daw
    yourself: http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide
    /index.html

    ).

      Fifth, what did you search for? Perhaps the terminology is the
    problem. We don't know for sure because you haven't told us.

      Sixth and finally .... *what are you trying to do*?! If you tell us
    what your overall goal is, we might be able to suggest a better (and
    perhaps even easier) approach to your problem.

      If none of this is helpful, again, read the first link I mentioned
    above and post back with questions we can answer.

    --
    I.S.
  • On Oct 9, 2007, at 3:05 PM, Development wrote:

    > Ok,
    > I have constructed a subclass of NSView so that I can set the
    > delegate.
    > Anyway. The NSView Subclass contains a bunch of subviews which are
    > NSButtons. I am having two problems though with drag and drop. I
    > registered for drag types, both file names and a custom internal
    > type like I normally do with outline views. However i get no focus
    > ring nor does it seem I get any reaction at all when I drag a file
    > to the view. I've tried registering the enclosing scroll view for
    > drag types, but still nothing. So basically its obvious I don't
    > know how to tell the view to detect the drag, or if I have, I don't
    > know how to gain the data from the drag.
    >

    Your NSView subclass has to draw the focus ring itself.  It also has
    to override these methods:

    - (BOOL)acceptsFirstResponder
    {
    return YES;
    }
    - (BOOL)becomeFirstResponder
    {
    [self setNeedsDisplay:YES];
    return YES;
    }
    - (BOOL)resignFirstResponder
    {
    [self setNeedsDisplay:YES];
    return YES;
    }

    - (void)drawRect:(NSRect)rect
    {
    if (mDragInside || self == [[self window] firstResponder])
    {
      // showsFirstResponder is set for us by the NSControl that is
    drawing us.
      NSRect focusRingFrame = [self frame];
      focusRingFrame.origin = NSZeroPoint;
      focusRingFrame.size.height -= 2.0;
      [NSGraphicsContext saveGraphicsState];
      NSSetFocusRingStyle(NSFocusRingOnly);
      [[NSBezierPath bezierPathWithRect: NSInsetRect(focusRingFrame, 4,
    4)] fill];
      [NSGraphicsContext restoreGraphicsState];
    }
    }

    You need to implement draggingEntered  and draggingExited and set
    mDragInside and then call [self display] along with perhaps calling
    your delegate.

    --
    Brian Stern
    <brians99...>
  • aside from Idiot Savant's advice..

    On Oct 9, 2007, at 3:05 PM, Development wrote:

    > However i get no focus ring nor does it seem I get any reaction at
    > all when I drag a file to the view.

    focus rings don't indicate drops.  they indicate which view will get
    the current key events.
  • On 10/9/07 4:25 PM, Scott Anguish said:

    >> However i get no focus ring nor does it seem I get any reaction at
    >> all when I drag a file to the view.
    >
    >
    > focus rings don't indicate drops.  they indicate which view will get
    > the current key events.

    Which isn't to say that views should not provide a visible indication
    that they accept drops, see:

    <http://developer.apple.com/documentation/UserExperience/Conceptual/
    OSXHIGuidelines/XHIGDragDrop/chapter_12_section_6.html
    >

    The Finder shows something that very much resembles a focus ring when
    you drag a file to a window.  I'm not sure if it has a name.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • Hillegass's book Cocoa Programming for Mac OS X has an extensive
    example that you should work through (the BigLetterView in the typing
    tutor).

    Hal
  • you're right. but it isn't the focus ring

    (Yay someone reads the hig!)

    On Oct 9, 2007, at 4:46 PM, Sean McBride wrote:

    > On 10/9/07 4:25 PM, Scott Anguish said:
    >
    >>> However i get no focus ring nor does it seem I get any reaction at
    >>> all when I drag a file to the view.
    >>
    >>
    >> focus rings don't indicate drops.  they indicate which view will get
    >> the current key events.
    >
    > Which isn't to say that views should not provide a visible indication
    > that they accept drops, see:
    >
    > <http://developer.apple.com/documentation/UserExperience/Conceptual/
    > OSXHIGuidelines/XHIGDragDrop/chapter_12_section_6.html>
    >
    > The Finder shows something that very much resembles a focus ring when
    > you drag a file to a window.  I'm not sure if it has a name.
    >
  • >
    > (Yay someone reads the hig!)

      The what-now? ;-)

    --
    I.S.
  • All right, I suppose that my question was poorly asked, mostly
    because as near as I can tell I don't know what to ask. I'm still
    struggling with that part.

    However, with the information from the responses I have gotten I have
    made  number of changes to the code in question. It will require many
    more. BUT I have managed to get it to drag if I use the mouseDown:
    method in the subclass. The problem is, I don't want it to start with
    a simple mouse down.

    Here is the code as I am attempting to use it: (This is within my
    NSView subclass)

    - (void)mouseDragged:(NSEvent *)theEvent
    {
    NSLog(@"Dragged");
    NSPasteboard *pBoard = [NSPasteboard pasteboardWithName:NSDragPboard];
    [pBoard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
    owner:self];
    [pBoard setData:[imageToThumb TIFFRepresentation]
    forType:NSTIFFPboardType];

    NSSize offset = NSMakeSize(0.0,0.0);
    NSRect location = [self frame];
    [self dragImage:imageToThumb at:[self bounds].origin
      offset:offset event:theEvent pasteboard:pBoard
      source:self slideBack:YES];
    }

    This code does not work. Nothing happens when I click on the item and
    drag. If i change it to mouseDown: then it works. But I don't want
    that to be the case. When this problem is solved I will implement
    code to have the view perform a predefined function when it is
    clicked like a button. (Should I just make a subclass of NSButton for
    this instead?)
previous month october 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 31        
Go to today