Won't Copy To PasteBoard / Outline Around Dragged Image

Discussion in 'Mac Programming' started by Darkroom, Oct 1, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i'm having trouble making the pasteboard accept my cropped version of the NSView's NSColor. kinda lost and it's making me a bit crazy...:eek:

    also, but least important, i'd like to have an outline around my dragged image because you can't really see that it's being dragged until the user drags it outside the window (as it's just a color). how can i add an outline or highlight to the mouseDragged method so that the user can see the dragged image?

    mouseDragged method:

    Code:
    - (void)mouseDragged:(NSEvent *)event
    	{
    	NSPoint down = [mouseDownEvent locationInWindow];
    	NSPoint drag = [event locationInWindow];
    	float distance = hypot(down.x - drag.x, down.y - drag.y);
    		if (distance < 2)
    		{
    		return;
    		}
    	
    	NSSize size = NSMakeSize(64, 64);
    	NSRect imageBounds;
    	imageBounds.origin = NSZeroPoint;
    	imageBounds.size = size;
    	
    	NSImage *anImage = [[NSImage alloc] initWithSize:size];
    
    	[anImage lockFocus];
    	[self drawRect:imageBounds];
    	[anImage unlockFocus];
    	
    	NSPoint p = [self convertPoint:down fromView:nil]; 
    	p.x = p.x - size.width / 2.0;
    	p.y = p.y - size.height / 2.0;
    	
    	NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSDragPboard];
    	[fillColor writeToPasteboard:pb];
    
    	[self dragImage:anImage at:p offset:NSMakeSize(0,0) event:mouseDownEvent pasteboard:pb source:self slideBack:YES];
    	[anImage release];
    	}
    
    warning states that "self" (which is my NSView subclass) may not respond to NSPasteBoard?? it will accept "fillColor" which is my NSColor, but it still doesn't copy it to the pasteboard...
     
  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    ok... i may not have supplied enough info... here's my complete class file:

    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface windowColor : NSView
    	{
    	NSColor *fillColor;
    	IBOutlet NSMenu *dockMenu;
    	NSEvent *mouseDownEvent;
    	}
    
    - (NSColor *)fillColor;
    - (void)rightMouseDown:(NSEvent *)theEvent;
    - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal;
    - (void)mouseDown:(NSEvent *)event;
    - (void)mouseDragged:(NSEvent *)event;
    
    @end
    
    ***************************************************************
    
    #import "windowColor.h"
    
    @implementation windowColor
    
    - (id)initWithFrame:(NSRect)frame
    	{
    	self = [super initWithFrame:frame];
    	if (self)
    		{
    		NSData *defaultViewColorData = [[NSUserDefaults standardUserDefaults] dataForKey:DEFAULTS_VIEW_COLOR_KEY];
    		if (defaultViewColorData != nil)
    			{
    			[fillColor release];
    			fillColor = [[NSUnarchiver unarchiveObjectWithData:defaultViewColorData] retain];
    			}
    		}
    	return self;
    	}
    
    - (NSColor *)fillColor
    	{
    	return fillColor;
    	}
    
    - (void)drawRect:(NSRect)rect
    	{
    	[fillColor set];
    	NSRectFill(rect);
    	}
    
    #pragma mark Right-Click Menu
    
    - (void)rightMouseDown:(NSEvent *)theEvent
    	{
    	[NSMenu popUpContextMenu:dockMenu withEvent: theEvent forView: self];
    	}
    
    #pragma mark Dragging Source
    
    - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
    	{
    	return NSDragOperationCopy;
    	}
    
    - (void)mouseDown:(NSEvent *)event
    	{
    	[event retain];
    	[mouseDownEvent release];
    	mouseDownEvent = event;
    	
    	//For Control+Click Dock Menu (Right-Click Menu)
    	unsigned int flags;
    	flags = [event modifierFlags];
    		if (flags & NSControlKeyMask)
    		{
    		[self rightMouseDown:event];
    		}
    	}
    	
    - (BOOL)acceptsFirstMouse:(NSEvent *)event
    	{
    	return YES;
    	}
    	
     - (void)mouseDragged:(NSEvent *)event
    	{
    	NSPoint down = [mouseDownEvent locationInWindow];
    	NSPoint drag = [event locationInWindow];
    	float distance = hypot(down.x - drag.x, down.y - drag.y);
    		if (distance < 2)
    		{
    		return;
    		}
    	
    	NSSize size = NSMakeSize(64, 64);
    	NSRect imageBounds;
    	imageBounds.origin = NSZeroPoint;
    	imageBounds.size = size;
    	
    	NSImage *anImage = [[NSImage alloc] initWithSize:size];
    
    	[anImage lockFocus];
    	[self drawRect:imageBounds];
    	[anImage unlockFocus];
    	
    	NSPoint point = [self convertPoint:down fromView:nil]; 
    	point.x -= size.width / 2.0;
    	point.y -= size.height / 2.0;
    	
    	NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSDragPboard];
    	[pb declareTypes:[NSArray arrayWithObjects: NSColorPboardType, nil] owner:fillColor];
    	
    	[fillColor writeToPasteboard:pb];
    
    	[self dragImage:anImage at:point offset:NSMakeSize(0,0) event:mouseDownEvent pasteboard:pb source:self slideBack:YES];
    	[anImage release];
    	}
    
    #pragma mark Dealloc
    
    - (void)dealloc
    	{
    	[fillColor release];
    	[NSBitmapImageRep release];
    	[NSAppleScript release];
    	[super dealloc];
    	}
    
    @end
    
    based on what i've read, i'm expecting that i should be able to drag the color onto the desktop, or any other image program... but it just slides back to the view...

    thoughts?
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    ok... so i removed this line of code because i apparently don't need it since i'm not dragging anything onto the view:

    Code:
    [pb declareTypes:[NSArray arrayWithObject: NSTIFFPboardType] owner:fillColor]
    
    now when i drag the view onto the desktop, it will create a picture clipping (Picture clipping.pictClipping) but the clipping is empty. the file size of the picture clipping is 4kb, which is what i expect would be the size of a 64x64 rect. any color (NSColor *fillColor) i make my view (red, green, whatever), dragging it onto the desktop will only produce an empty (white) image clipping.

    is it even possible for me to produce a .PNG or .TIFF file as the drag image, and then save it by dropping it either on the desktop or any application that can read image files (Preview, Safari, etc.)? i'm assuming that if this is possible, dragging it into another app would store the file in the temp folder... i guess what i'm trying to do is instead of "drag and drop", i'm attempting a "create image, drag, drop and save" (if that makes any sense).

    anyway... any response would be appreciated... :eek:
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    does no one have any thoughts? or is this just a really dumb question? :(
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
  6. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #6
    this information looks very promising... thanks again :)
     

Share This Page