I have written a little app to **finally** try and answer a Hillegass challenge, which tries to further my understanding of Drag & Drop.
So, I have a subclassed NSView, which has the following methods implemented.
To register:
On entering ( with the drag)
And on the "drop"
Now, here is the problem:
in drawRect: I set the current graphics context to the fill/stroke color. However, in order to use the "dragged" color, I clearly have to somehow take the "newColor" from the "performDragOperation" and use *that* in drawRect. Initially, I tried to see if there is a way I can interrogate the current Context from drawRect, and in that way, I could set an initial value for NSColor, say from awakeFromNib, but I cannot find a way to do this. Then I thought of extracting the color directly from the draggingPasteBoard, but could not figure out a way of referencing that from drawRect. This is cocoa, so I **know** there is an easy way to do this.

It might well be that my approach is ( in **all** likelyhood )fundamentally flawed but hopefully someone can point me in the correct direction and I can vault another hurdle to the cocoa finishing line!!
Thanks in advance.
So, I have a subclassed NSView, which has the following methods implemented.
To register:
Code:
-(void) awakeFromNib
{
NSArray *pbTypes = [ NSArray arrayWithObjects:NSPasteboardTypeString, NSPasteboardTypeSound, NSColorPboardType, nil];
[self registerForDraggedTypes : pbTypes];
NSLog(@"Registered for drop: \"String\", \"Sound\", \"Color\"");
}
On entering ( with the drag)
Code:
- (NSDragOperation)draggingEntered: (id < NSDraggingInfo >)sender
{
NSDragOperation sourceDragMask;
NSPasteboard *pboard;
pboard = [ sender draggingPasteboard];
sourceDragMask = [ sender draggingSourceOperationMask];
if ( [[ pboard types] containsObject: NSColorPboardType])
{
if ( sourceDragMask & NSDragOperationGeneric)
{
NSLog(@"Will respond to color");
return NSDragOperationGeneric;
}
}
....code to check for StringType......
return NSDragOperationNone;
}
And on the "drop"
Code:
- (BOOL)performDragOperation: (id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [ sender draggingSourceOperationMask];
pboard = [ sender draggingPasteboard];
if ( [[ pboard types] containsObject:NSColorPboardType] )
{
NSColor *newColor = [ NSColor colorFromPasteboard:pboard];
[newColor set];
[self setNeedsDisplay:YES];
....other code....
return YES;
Now, here is the problem:
in drawRect: I set the current graphics context to the fill/stroke color. However, in order to use the "dragged" color, I clearly have to somehow take the "newColor" from the "performDragOperation" and use *that* in drawRect. Initially, I tried to see if there is a way I can interrogate the current Context from drawRect, and in that way, I could set an initial value for NSColor, say from awakeFromNib, but I cannot find a way to do this. Then I thought of extracting the color directly from the draggingPasteBoard, but could not figure out a way of referencing that from drawRect. This is cocoa, so I **know** there is an easy way to do this.
It might well be that my approach is ( in **all** likelyhood )fundamentally flawed but hopefully someone can point me in the correct direction and I can vault another hurdle to the cocoa finishing line!!
Thanks in advance.
Code:
- (void)drawRect: (NSRect)dirtyRect {
[self completeTheDrawing: dirtyRect];
NSRect bounds = [ self bounds];
NSBezierPath *path = [ NSBezierPath bezierPathWithRect: bounds];
[[NSColor redColor] set];
[path fill];
[[NSColor redColor]set];
[path setLineWidth:8];
[path stroke];
[[NSColor blackColor]set];
[path setLineWidth:4];
[path stroke];
}