[Merged] Simple drag and drop application not working

Discussion in 'Mac Programming' started by Monaj, Jul 14, 2010.

  1. Monaj macrumors regular

    Joined:
    May 24, 2009
    #1
    Hi all,

    I am trying a simple drag and drop application:

    I am creating a CameraIconView (subclass of NSView, containing some image views, text fields and a pop-up button), at run time. This view is enclosed within CameraIconEnclosingBox (subclass of NSBox). Requirement is: user should be able to drag CameraIconView at some other location in CameraIconEnclosingBox. To implement my requirements I am doing this:

    Implemented following method in CameraIconView class-


    Code:
    - (void)mouseDown:(NSEvent *)e{
    	NSPoint location; 
    	NSSize size;
    	NSPasteboard *pb = [NSPasteboard pasteboardWithName:@"CameraIconContainer"];
     
    	location.x =  ([self bounds].size.width - size.width)/2;
    	location.y =  ([self bounds].size.height - size.height)/2;
     
    	NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    	[pb declareTypes:[NSArray arrayWithObject:IconDragDataType] owner:self];
    	[pb setData:data forType:IconDragDataType];
    	[self dragImage:[NSImage imageNamed:@"camera_icon.png"] at:location offset:NSZeroSize event:e pasteboard:pb source:self slideBack:NO];
    }
    2. Implemented following method in CameraIconEnclosingBox class-


    Code:
    - (void)awakeFromNib{
        [self registerForDraggedTypes:[NSArray arrayWithObject:IconDragDataType]];
    }
    - (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender{
        return NSDragOperationEvery;
    }
    - (BOOL)performDragOperation:(id < NSDraggingInfo >)sender{
        return YES;
    }
    - (void)concludeDragOperation:(id < NSDraggingInfo >)sender{
        NSPoint dropLocation = [sender draggingLocation];
     
        float x = dropLocation.x;
        float y = dropLocation.y;
        NSLog(@"dragOperationConcluded! draggingLocation: (%f, %f)",x,y);
     
        NSPasteboard *pb = [sender draggingPasteboard];
        NSLog(@"Pasteboard name- %@",[pb name]);
        NSData *draggedData = [pb dataForType:IconDragDataType];
     
        CameraIconView *object = [NSKeyedUnarchiver unarchiveObjectWithData:draggedData];
        NSLog(@"object - %@ / cameraNo : %@/ operatorName : %@",object,[[object cameraNo] stringValue],[[object operatorName] stringValue]);
        // the cameraNo and operatorName are properties defined within CameraIconView class
        // the above log is returning (null) for both properties
     
        float width  = [object frame].size.width;
        float height = [object frame].size.height;
     
        NSLog(@"width - %f / height - %f",width,height);
        [object setFrame:NSMakeRect(x, y, width, height)];
    }
    After implementing these methods I am able to perform drag but drop operation is not working, although all dragging delegate methods in CameraIconEnclosingBox are called.

    Can anyone suggest where I may be wrong or some other better way to implement my requirements?

    Thanks,

    Monaj
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    I haven't tried this myself, but I don't think returning NSDragOperationEvery from -draggingEntered: works as you expect. I think you have to choose one of the NSDragOperation values that the dragging source allows (if you don't know which one to choose, use NSDragOperationGeneric).
     
  3. Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
    #3
    I have tried this but it is not working
     
  4. Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
    #4
    solved the reported problem but found new problem

    Hi all,

    I am able to resolve it by adding this line of code:

    Code:
    [self addSubview:object];
    before this code:

    Code:
    [object setFrame:NSMakeRect(x, y, width, height)];
    So CameraIconView now is appearing at dropped location. :)

    ... but now I am facing a new problem-

    Can anyone suggest me some way to resolve it?

    Thanks,

    Monaj
     
  5. Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
    #5
    Exception in mouseDown method

    Hi all,

    I am doing drag and drop with NSView.

    In the object to be dragged, which is subclass of NSView, I implemented mouseDown: method as follows:
    Code:
    @try {
        NSPoint location; 
        NSSize size ;
        NSPasteboard *pb = [NSPasteboard pasteboardWithName:@"CameraIconContainer"];
    
        location.x =  ([self bounds].size.width - size.width)/2 - 21.0;
        location.y =  ([self bounds].size.height - size.height)/2 - 7.0;
    
        NSLog(@"mouseDown: location- (%f, %f)",location.x,location.y);
    
    
        NSDictionary *iconViewDict = [[NSDictionary alloc] initWithObjectsAndKeys:[cameraNo stringValue],@"cameraNo",nil];
        NSLog(@"iconViewDict - %@",iconViewDict);
    
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:iconViewDict];
    
    
        [pb declareTypes:[NSArray arrayWithObject:IconDragDataType] owner:self];
        [pb setData:data forType:IconDragDataType];
        [self dragImage:[NSImage imageNamed:@"camera_icon.png"] at:location offset:NSZeroSize event:e pasteboard:pb source:self slideBack:YES];
    }
    @catch (NSException * e) {
        NSLog(@"CameraIconView (-mouseDown:), error - %@",e);
    }
    Most of the time it is working fine but problem is- sometimes it is raising this exception:

    in the mouseDown: method, because of it the dragged image continuously appears over screen, which does not disappear even if some other window is selected.

    Can anyone suggest me why is it occurring and how can I resolve it?

    Thanks,

    Monaj
     
  6. Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
  7. Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
    #7
    kCGErrorFailure while performing drag and drop in NSView

    Hi all,

    I have created a simple drag and drop application in which I am sometimes getting this exception msg, displayed in debugger console:

    Code:
    kCGErrorRangeCheck: CGSNewWindowWithOpaqueShape: Cannot create window
    
    kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.
    
    kCGErrorIllegalArgument: CGSMoveWindow: Invalid window 0x0
    
    kCGErrorIllegalArgument: CGSOrderWindowList
    
    kCGErrorIllegalArgument: CGSOrderWindowList
    
    kCGErrorIllegalArgument: CGSGetWindowBounds: NULL window
    
    kCGErrorIllegalArgument: CGSMoveWindow: Invalid window 0x0
    
    kCGErrorIllegalArgument: CGSOrderWindowList
    
    kCGErrorIllegalArgument: CGSGetWindowBounds: NULL window
    The code in concludeDragOperation is:

    Code:
    NSPoint dropLocation = [sender draggedImageLocation];
    id dragSource = [sender draggingSource];
    NSPasteboard *pb = [sender draggingPasteboard];
    NSData *draggedData = [pb dataForType:IconDragDataType];
    id unarchievedObject = [NSKeyedUnarchiver unarchiveObjectWithData:draggedData];
    [unarchievedObject setFrameOrigin:dropLocation];
    
    Thanks,

    Monaj
     
  8. Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
    #8
    mouseDown: event not recognized in NSImageView over subclass of NSView

    Hi all,

    I am trying a simple drag and drop application.

    I have created a subview of NSView named: CameraIcon, which has an image view and certain text fields over it. In its mouseDown: method, I am initiating drag operation, I have also given a NSLog in it, so that I can notice in debugger when it is clicked.

    Problem is:

    Can anyone suggest me some solution for it?

    Thanks,

    Monaj
    Monaj
     

Share This Page