Help with Drag and drop NSBrowser
Help with Drag and drop NSBrowser
- Subject: Help with Drag and drop NSBrowser
- From: "Ryan D. Moniz" <email@hidden>
- Date: Mon, 01 Aug 2005 00:13:09 -0600
Hello,
I've searched the archives and have tried for the last few days
to get an Drag and drop NSBrowser to work. The drag source works,
but I can't get NSBrowser to become a drag destination. I've
subclassed NSMatrix, but I still can't get it to work to become a
drag destination.
Thanks,
Sincerely,
Ryan D. Moniz
//MyDocument.m
//inside -windowControllerDidLoadNib:
//setup the browser view
// Make the browser user our custom browser cell.
[browser setCellClass: [FSBrowserCell class]];
// Make the browser user our custom browser cell.
[browser setCellClass: [FSBrowserCell class]];
//we need to intercept dragging events to subclass NSMatrix.
[browser setMatrixClass:[BrowserMatrix class]];
// Tell the browser to send us messages when it is clicked.
[browser setTarget: self];
// [browser setAction: @selector(browserSingleClick:)];
[browser setDoubleAction: @selector(browserDoubleClick:)];
// Configure the number of visible columns (default max visible
columns is 1).
[browser setMaxVisibleColumns: MAX_VISIBLE_COLUMNS];
[browser setMinColumnWidth: NSWidth([browser bounds])/(float)
MAX_VISIBLE_COLUMNS];
[browser setAllowsEmptySelection:YES];
// Prime the browser with an initial load of data.
[self updateUI];
//BrowserMatrix.h
@interface BrowserMatrix : NSMatrix {
}
-(NSImage *)imageForString:(NSString *)aString;
-(NSImage *)imageForCell:(NSCell *)aCell;
//BrowserMatrix.m
@implementation BrowserMatrix
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)mouseDown:(NSEvent *)event {
BOOL isInside = YES;
NSPoint mouseLoc;
event = [[self window] nextEventMatchingMask: NSLeftMouseUpMask |
NSLeftMouseDraggedMask];
mouseLoc = [self convertPoint:[event locationInWindow]
fromView:nil];
isInside = [self mouse:mouseLoc inRect:[self bounds]];
int row, col;
switch([event type]) {
case NSLeftMouseDragged:
[self getRow:&row column:&col forPoint:[self convertPoint:
[event locationInWindow] fromView:nil]];
[self selectCellAtRow:row column:col];
[self sendAction];
[[self window] makeFirstResponder:self];
break;
case NSLeftMouseUp:
if(isInside) {
[super mouseDown:event];
}
break;
default:
//ignore any other kind of event.
break;
}
}
- (void)mouseDragged:(NSEvent *)event {
//Created by Timothy Ritchey
NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSDragPboard];
NSPoint dragPoint;
int col, row;
[self getRow: &row column:&col forPoint:[self convertPoint:
[event locationInWindow] fromView:nil]];
NSImage *cellImage = [self imageForCell:[self cellAtRow:row
column:col]];
dragPoint = [self convertPoint:[event locationInWindow]
fromView:nil];
[pb declareTypes:[NSArray arrayWithObjects:NSStringPboardType,
nil] owner:self];
[pb setString:[self stringValue] forType:NSStringPboardType];
[self dragImage:cellImage at:dragPoint offset:NSZeroSize
event:event pasteboard:pb source:self slideBack:YES];
}
-(unsigned int)draggingSourceOperationMaskForLocal:(BOOL)aFlag {
if(aFlag) {
NSLog(@"aFlag is true.");
return NSDragOperationEvery;
}
return NSDragOperationNone;
}
-(NSImage *)imageForString:(NSString *)aString {
//Created by Timothy Ritchey
NSImage *image = nil;
NSBitmapImageRep *bits = nil;
NSRect textFrame;
NSCell *cell = [[NSCell alloc] initTextCell:aString];
//NSCell *cell = [[NSCell alloc] initImageCell:[self image]];
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];
}
-(NSImage *)imageForCell:(NSCell *)aCell {
NSImage *image = nil;
NSBitmapImageRep *bits = nil;
NSRect textFrame;
textFrame.size = [aCell cellSize];
textFrame.origin = NSZeroPoint;
image = [[NSImage alloc] initWithSize:textFrame.size];
[image setBackgroundColor:[NSColor blueColor]];
[image lockFocus];
[aCell drawInteriorWithFrame:textFrame inView:[NSView focusView]];
bits = [[NSBitmapImageRep alloc]
initWithFocusedViewRect:textFrame];
[image unlockFocus];
[bits release];
return [image autorelease];
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden