NSTextField: drag-and-drop + Command keypresses

  • I have a NSTextField which I subclassed and enabled with the
    NSDraggingInfo protocol. I set the delegate of this NSTextField and
    its enclosing NSWindow to my custom Controller class.

    Drag and drop works. Specifically I am dragging a file or URL object
    from a web browser into the NSTextField. It inserts the URI into the
    insertion point within the NSTextField.

    However, I would like to do the following:

    1. I would like to replace all the text inside the NSTextField with
    the URI contained inside the NSPasteboard object

    2. Enable Command-C, Command-V etc. keypresses, to be able to copy
    and paste from the NSPasteboard into my NSTextField

    How can I add these features?

    To try to answer 1. I looked at the following method which I obtained
    from http://www.stone.com/The_Cocoa_Files/
    Ins_and_Outs_of_Drag_and_D.html:

    - (unsigned int) draggingEnteredOrUpdated:(id <NSDraggingInfo>)sender
    {
    // we want to ignore drags originating in our own window:
    if ([sender draggingSource] == controllerWindow)
    {
      return NSDragOperationNone;
    }
    else
    {
      unsigned int sourceMask = [sender draggingSourceOperationMask];
      NSPasteboard *pboard = [sender draggingPasteboard];
      NSString *type = [pboard availableTypeFromArray:[self
    acceptableDragTypes]];
      if (type) return sourceMask;
      NSLog (@"Type: %s",type);
      return NSDragOperationNone;
    }
    }

    I tried adding NSLog statements within this method to track what data
    gets passed around. Unfortunately, the above NSLog never gets called
    for some reason, even though the drag-and-drop appears to work. Is
    there another method being called?

    To try to answer 2., I have subclassed the enclosing NSWindow to
    handle sendEvent:

    - (void) sendEvent: (NSEvent *) theEvent
    {
        if (([theEvent type]==NSKeyDown) && ([theEvent keyCode] == 53))
    {
      [[NSNotificationCenter defaultCenter]
    postNotificationName:@"notifyEscapeKeyPressed" object:self];
    }

    [super sendEvent: theEvent];
    }

    Is this overriding my ability to send Command keypresses to the
    custom NSTextField object?

    If I am calling the super's sendEvent:, why wouldn't this just pass
    on Command-keypresses upwards?

    Thanks for any advice!

    Regards,
    Alex
previous month october 2006 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