Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Monaj

macrumors regular
Original poster
May 24, 2009
193
0
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
 
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).
 
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-

each time the CameraIconView is dragged and dropped, it is not positioning the dragged CameraIconView to new location, but is creating and showing a new instance within CameraIconEnclosingBox at new location, ie. for the first drag and drop two icons start appearing for the same CameraIconView ... for second drag and drop three icons start appearing and so on....

Can anyone suggest me some way to resolve it?

Thanks,

Monaj
 
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:

Invalid parameter not satisfying: theWriteStream != NULL

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
 
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

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

Also can anyone suggest how to set breakpoint for @CGErrorBreakpoint() so that I can debug and trace its cause?

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
 
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:

when I try to click the NSImageView over CameraIcon, the mouseDown method is not called and the drag operation is not initiated. It is called only if I click on any other portion of CameraIcon or on any text field over it.

This problem is only occurring in Leopard. In Snow Leopard it is working as intended.

Can anyone suggest me some solution for it?

Thanks,

Monaj
Monaj
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.