• 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: "Alan Smith" <email@hidden>
  • Date: Tue, 4 Jul 2006 09:14:23 -0400

In your drawRect method you draw 'inputNodeImage.' Also in 'setImage'
you release the old and replace it with the new.
You should be able to fix this if you have an array of images and each
time a new one gets dragged in it gets added to the array. Then in
drawRect you would have a loop that would iterate over the array
drawing each image. Note that the array should be a NSMutableArray.

Hope this helps, Alan

On 7/4/06, Ender Wiggins <email@hidden> wrote:
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



--
// Quotes from yours truly -------------------------
"You don't forget, you just don't remember."
"Maturity resides in the mind."
"Silence is the Universe's greatest gift."
"Don't waste your life doing things others have already done."
_______________________________________________
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


References: 
 >Drag and Drop help (From: "Ender Wiggins" <email@hidden>)
 >Re: Re: Drag and Drop help (From: "Ender Wiggins" <email@hidden>)

  • Prev by Date: Re: NSMutableArray compare:
  • Next by Date: Re: NSOpenPanel not respecting file types?
  • Previous by thread: Re: Re: Drag and Drop help
  • Next by thread: test
  • Index(es):
    • Date
    • Thread