Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SOLVED] Re: NSSliderCells in an NSTableView not playing ball with with drag and drop



Hi,

Ok in the long time period it took for this e-mail to finally show up here (3 days!?) I figured out how to do this and I'll post the code which will hopefully help other people.

First you need to add a couple of additional methods to your table datasource so we can easily turn drag and drop off from the tableview:

dragAndDrop is just a BOOL declared in the header file.

- (void) enableDragAndDrop { dragAndDrop = YES; }
- (void) disableDragAndDrop { dragAndDrop = NO; }

You'll want to check this variable in this method to prevent dragging from occurring when necessary:

- (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray *)rows toPasteboard:(NSPasteboard *)pboard
{
if(!dragAndDrop)
{
return NO;
}
else
{
...
}
}


Then you need to subclass NSTableView if you've not already done so. It's useful to be able to set the column we wish to disable the normal dragging behaviour for using its identifier (usually specified through IB). Here are the accessors for this variable, you'll want draggingDisabledColumn initialised in initWithFrame and declared in the header.

- (void) setDraggingDisabledColumn:(NSString *)identifier
{
  [identifier retain];
  [draggingDisabledColumn release];
  draggingDisabledColumn = identifier;
}

- (NSString *) draggingDisabledColumn { return draggingDisabledColumn; }

Don't forget to call [self setDraggingDisabledColumn: nil]; in your dealloc so we don't leak memory.

The mouseDown: method is where we figure out if we need to disable drag and drop for a particular click.

- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint mouseLocationInWindow = [theEvent locationInWindow];
// Translate the mouse co-ordinates so they are relative to the tableview's...
NSPoint mouseLocationInTable = [self convertPoint: mouseLocationInWindow fromView: [[self window] contentView]];


if([[self dataSource] respondsToSelector: @selector(enableDragAndDrop)])
{
[[self dataSource] enableDragAndDrop];
}


if(![[self draggingDisabledColumn] isEqualToString: @""])
{
int column = [self columnWithIdentifier: [self draggingDisabledColumn]];
if([self columnAtPoint: mouseLocationInTable] == column)
{
if([[self dataSource] respondsToSelector: @selector(disableDragAndDrop)])
{
[[self dataSource] disableDragAndDrop];
}
}
}
[super mouseDown: theEvent];
}


Right that's only part of the problem fixed however. For some reason [super mouseDown: theEvent] doesn't properly send clicks through to the cells, but drags go through fine. To fix this we need to add a method to our subclassed cell:

- (BOOL) trackMouse:(NSEvent *)theEvent
inRect:(NSRect)cellFrame
ofView:(NSView *)controlView
untilMouseUp:(BOOL)untilMouseUp
{
NSPoint tempCoords = [controlView convertPoint: [theEvent locationInWindow] fromView: [[controlView window] contentView]];
NSPoint mouseCoords = NSMakePoint(tempCoords.x - cellFrame.origin.x, tempCoords.y - cellFrame.origin.y);


// Deal with the click however you need to here, for example in a slider cell you can use the mouse x
// coordinate to set the floatValue.


// Dragging won't work unless you still make the call to the super class...
return [super trackMouse: theEvent inRect: cellFrame ofView: controlView untilMouseUp: untilMouseUp];
}


Well that is it, happy coding.

Rory
--

ThinkMac Software
http://www.thinkmac.co.uk

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >NSSliderCells in an NSTableView not playing ball with with drag and drop (From: Rory Prior <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.