• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Re: Drag and Drop help
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: Drag and Drop help


  • Subject: Re: Re: Drag and Drop help
  • From: "Ender Wiggins" <email@hidden>
  • Date: Tue, 4 Jul 2006 02:13:55 -0500

Yes, I subclassed NSView and followed the example from the book 'Cocoa
Programming'.

I really don't know how to draw any sort of graph and I guess it's
things like this I'm trying to find out. Here's what's in my paste
board file. I really have no clue where to go next from here.

---

@interface PasteBoard : NSView
{
	BOOL dragSessionInProgress;
	NSImage *inputNodeImage;
}

- (void) setImage:(NSImage *)newImage;
- (NSImage *)image;

@end

---

@implementation PasteBoard

- (id)initWithFrame:(NSRect)frameRect
{
	if ((self = [super initWithFrame:frameRect]) != nil) {
		[self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
			NSFilenamesPboardType, nil]];
	}

	dragSessionInProgress = NO;

	return self;
}

- (void)drawRect:(NSRect)rect
{
	NSRect ourBounds = [self bounds];
   NSImage *image = [self image];

   [super drawRect:rect];
   [image compositeToPoint:(ourBounds.origin) operation:NSCompositeSourceOver];

	if (dragSessionInProgress) {
		[[NSColor redColor] set];
		NSFrameRect([self bounds]);
	}
}

- (void)dealloc
{
   [self unregisterDraggedTypes];
   [super dealloc];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
   if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
               == NSDragOperationGeneric) {

		NSLog(@"draggingEntered: %d", [sender draggingSequenceNumber]);
		dragSessionInProgress = YES;

       return NSDragOperationGeneric;
   }
   else {
       return NSDragOperationNone;
   }
}

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
	dragSessionInProgress = NO;
	[self setNeedsDisplay:YES];
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
   if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
                   == NSDragOperationGeneric) {

       return NSDragOperationGeneric;
   }
   else {
       return NSDragOperationNone;
   }
}

- (void)draggingEnded:(id <NSDraggingInfo>)sender
{ }

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
   return YES;
}

- (void)slideDraggedImageTo:(NSPoint)aPoint
{ }

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
   NSPasteboard *paste = [sender draggingPasteboard];
   NSArray *types = [NSArray arrayWithObjects:NSTIFFPboardType,
		NSFilenamesPboardType, nil];
   NSString *desiredType = [paste availableTypeFromArray:types];
   NSData *carriedData = [paste dataForType:desiredType];

   if (nil == carriedData) {
       NSRunAlertPanel(@"Paste Error", @"Paste operation failed",
           nil, nil, nil);

       return NO;
   }
   else {
       if ([desiredType isEqualToString:NSTIFFPboardType]) {
           NSImage *newImage = [[NSImage alloc] initWithData:carriedData];
           [self setImage:newImage];
           [newImage release];
       }
       else if ([desiredType isEqualToString:NSFilenamesPboardType]) {
           NSArray *fileArray =
               [paste propertyListForType:@"NSFilenamesPboardType"];

           NSString *path = [fileArray objectAtIndex:0];

           NSImage *newImage = [[NSImage alloc] initWithContentsOfFile:path];

           if (nil == newImage) {

               NSRunAlertPanel(@"File Reading Error",
                   [NSString stringWithFormat:
                   @"Failed to open the file at \"%@\"",
                   path], nil, nil, nil);

               return NO;
           }
           else {
               [self setImage:newImage];
           }

           [newImage release];
       }
       else {
           NSAssert(NO, @"Should never pass here.");

			return NO;
       }
   }

   [self setNeedsDisplay:YES];

   return YES;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
	dragSessionInProgress = NO;
   [self setNeedsDisplay:YES];
}

- (void)setImage:(NSImage *)newImage
{
   NSImage *temp = [newImage retain];
   [inputNodeImage release];
   inputNodeImage = temp;
}

- (NSImage *)image
{
   return inputNodeImage;
}

@end


On 7/4/06, Fredrik Olsson <email@hidden> wrote:
Ender Wiggins skrev:
> Hi all,
>
> I've just implemented the basic NSDraggingDestination informal
> protocol from an example of a book and it works. Though it only
> accepts one image. When I drag another image to the view, it replaces
> the first image and is placed in the lower left corner of my view. But
> now my books go no further.
>
Is the view a NSImageView? If so it is only meant to display a single
image. You could do a subclass of NSView and handle the drawing of the
graph yourself.

Or maybe an easier solution is to have a NSView, and instantiate new
NSImageView sub-views on drops?
You will need to subclass both NSView and NSImageView to handle drop
targets, to be able to drag NSImageViews around, and drawing connection
lines, and so forth.


// Fredrik Olsson > What I'm trying to eventually do is drag images from a palette onto > the view and connect them in order to make a larger drawing. Very much > like what OmniGraffle does. > > Can anyone sort of guide me to what I need to be looking at next. > > I still can't move the image around in my view and place it in any > location. > And I want to be able to connect other images to this one. > > Any help much appreciated. > _______________________________________________ > 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


_______________________________________________
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


  • Follow-Ups:
    • Re: Re: Drag and Drop help
      • From: "Alan Smith" <email@hidden>
References: 
 >Drag and Drop help (From: "Ender Wiggins" <email@hidden>)

  • Prev by Date: test
  • Next by Date: NSViewAnimation with curved path
  • Previous by thread: Drag and Drop help
  • Next by thread: Re: Re: Drag and Drop help
  • Index(es):
    • Date
    • Thread