Drag & Drop on in a NSMatrix

  • Hi all, I'm working on a thumbnail matrix using a (you guessed it)
    NSMatrix. I've got the matrix working fine with NSImageCell and now I'm
    wanting to add in drag and drop support to allow the user to rearrange
    the contents of the matrix. While I've used drag and drop support in a
    NSTableView (which was nice and simple mind you) I don't see any such
    easy way to add in the support to my NSMatrix. The code I used in the
    table view (such as -(BOOL)tableView:(NSTableView*)inTableView
    writeRows:(NSArray*)inRows toPasteboard:(NSPasteboard*)inPasteboard)
    doesn't seem to exist in any form for a matrix.
    Does NSMatrix not support drag and drop like a table view?
    And does anyone know where I can find some sample code for handling
    drag and drop within a matrix and from a matrix to a table view?

    Thanks,
    Lee Morgan
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • You need to subclass NSMatrix, and implement the following selectors.
    Note, this code is taken from a matrix that is used within an
    NSBrowser, but there is no reason this shouldn't work that I know of.
    Let me know if this works for you.

    Cheers,
    tim

    - (void)mouseDown:(NSEvent *)event
    {
        // need to override this because NSMatrix eats drag events
        int row, col;

        if ([self getRow: &row column: &col
                forPoint:[self convertPoint:[event locationInWindow]
    fromView: nil]]) {
            [self selectCellAtRow: row column: col];
            [self sendAction];
            // this is used to deal with NSBrowser issues
            [[self window] makeFirstResponder:self];
        } else {
            [super mouseDown: event];
        }

    }

    - (void)mouseDragged:(NSEvent *)event
    {
        NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard];
        NSPoint dragPoint;
        int row, col;
        NSImage *image = [self imageForString:@"SOME STRING"];

        [self getRow:&row column:&col
            forPoint: [self convertPoint:[event locationInWindow]
    fromView:nil]];

        dragPoint = [self convertPoint: [event locationInWindow]
    fromView:nil];
        dragPoint.x -= ([image size].width * 0.5);
        dragPoint.y += ([image size].height * 0.5);

        [pb declareTypes: [NSArray arrayWithObjects: NSStringPboardType,
    nil]
                    owner: self];
        [pb setString:@"SOME STRING" forType:NSStringPboardType];

        [self dragImage: image
                      at:dragPoint
                  offset: NSZeroSize
                  event: event
              pasteboard: pb
                  source: self
              slideBack: YES];
    }

    - (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)isLocal
    {
        return NSDragOperationCopy;
    }

    - (NSImage *) imageForString:(NSString*)aString {
        NSImage *image = nil;
        NSBitmapImageRep *bits = nil;
        NSRect textFrame;
        NSCell *cell = [[NSCell alloc] initTextCell:aString];

        textFrame.size = [cell cellSize];
        textFrame.origin = NSZeroPoint;

        image = [[NSImage alloc] initWithSize: textFrame.size];
        [image setBackgroundColor:[NSColor blueColor]];

        [image lockFocus];
        [cell drawInteriorWithFrame: textFrame inView: [NSView focusView]];
        bits = [[NSBitmapImageRep alloc] initWithFocusedViewRect:
    textFrame];
        [image unlockFocus];
        [bits release];

        return [image autorelease];
    }

    On Dec 15, 2003, at 1:33 AM, Lee Morgan wrote:

    > Hi all, I'm working on a thumbnail matrix using a (you guessed it)
    > NSMatrix. I've got the matrix working fine with NSImageCell and now
    > I'm wanting to add in drag and drop support to allow the user to
    > rearrange the contents of the matrix. While I've used drag and drop
    > support in a NSTableView (which was nice and simple mind you) I don't
    > see any such easy way to add in the support to my NSMatrix. The code I
    > used in the table view (such as
    > -(BOOL)tableView:(NSTableView*)inTableView writeRows:(NSArray*)inRows
    > toPasteboard:(NSPasteboard*)inPasteboard) doesn't seem to exist in any
    > form for a matrix.
    > Does NSMatrix not support drag and drop like a table view?
    > And does anyone know where I can find some sample code for handling
    > drag and drop within a matrix and from a matrix to a table view?
    >
    > Thanks,
    > Lee Morgan
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.