advertisement

Article:
  Working With Bitmap Images; Document-Based Application Redux
Subject:   Making the view a dragging destination
Date:   2002-04-27 17:05:11
From:   michele
Here is my implementation of drag and drop.


Making the view a dragging destination



Add the following methods to IAImageView.m:


1 - Register for dragged types when initializing the image view


- (id) initWithCoder: (NSCoder *) coder
{
if (self = [super initWithCoder: coder])
{
[self registerForDraggedTypes: [NSImage imagePasteboardTypes]];
}


return self;
}


2 - At the beginning of the dragging operation, set a flag to highlight the view if the pasteboard contains a valid image and the dragging operation is copying


Declare the flag in IAImageView.h


BOOL highlight;


Implement the method in IAImageView.m


- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) sender
{
if ([NSImage canInitWithPasteboard: [sender draggingPasteboard]] &&
[sender draggingSourceOperationMask] & NSDragOperationCopy)
{
highlight = YES;
[self setNeedsDisplay: YES];
return NSDragOperationCopy;
}


return NSDragOperationNone;
}


3 - Whenever the mouse quits the dragging destination, set a flag to unhighlight the view


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


4 - Check if the pasteboard contains a valid image


- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>) sender
{
highlight = NO;
[self setNeedsDisplay: YES];
return [NSImage canInitWithPasteboard: [sender draggingPasteboard]];
}


5 - Performs the dragging operation


- (BOOL) performDragOperation: (id <NSDraggingInfo>) sender
{
// Don't drag if the source = the destination
if ([sender draggingSource] != self)
{
NSURL *fileURL;

// Check if there is a valid image in pasteboard
if ([NSImage canInitWithPasteboard: [sender draggingPasteboard]])
{
// Insert the image into the view
[[self image] initWithPasteboard: [sender draggingPasteboard]];
// Rescale the image to the current scaling value
[self scaleFrameBy: [[[self window] delegate] getScale]];
}


// Retrieve the name of the file
fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
if (fileURL != NULL)
{
// Set the window title to the name of the file
[[self window] setTitle: [fileURL absoluteString]];
}
else
{
[[self window setTitle: @"(no name)"];
}
}


return YES;
}


6 - Change the drawRect method to display a border around the dragging destination


Add the following at the end of the method


if (highlight)
{
[[NSColor grayColor] set];
[NSBezierPath setDefaultLineWidth: 5];
[NSBezierPath strokeRect: rect];
}


7 - Add a method to IAWindowController.h to get the current scale value


In IAWindowController.h


- (float) getScale;


In IAWindowController.m


- (float) getScale
{
return ([zoomControl floatValue] / 100.0);
}


8 - Add #import "IAWindowController.h" in IAImageView.m for the getScale method



1 to 1 of 1
  1. Making the view a dragging destination
    2002-05-02 21:17:46  psheldon [View]

    • Making the view a dragging destination
      2002-05-03 12:34:15  michele [View]

      • is Programming Topics a pdf?
        2002-05-09 07:08:32  psheldon [View]

      • both Acrobat and Preview show whole pdf
        2002-05-09 06:54:38  psheldon [View]

      • Making the view a dragging destination
        2002-05-05 21:49:21  dallasmac [View]

        • Making the view a dragging destination
          2002-05-06 13:19:45  michele [View]

1 to 1 of 1