Re: Drag and drop onto a NSButton
Re: Drag and drop onto a NSButton
- Subject: Re: Drag and drop onto a NSButton
- From: Mark Williams <email@hidden>
- Date: Sun, 26 Jun 2005 12:10:50 -0700
Wow, a lot to go over here, and thank you very much for the example
code.
Mark
On Jun 25, 2005, at 7:34 PM, Henry McGilton wrote:
On Jun 25, 2005, at 9:36 AM, email@hidden wrote:
An app I am working on needs to have drag and drop ability onto a
button. But I cannot seem to figure it out. I have done the
registered for the drag type with it so that much works but I do
not understand how to get the button to acctually recieve the
drop. Meaning, when the item is dropped how do I detect it? I know
what to do with the data once I've detected it but I don't know
how to get the message in the controller. Do I have to subclass
NSButton? Any help would be really appriciated.
You have to sub-class.
Here is some code I wrote to make a button accept dragged text:
ProportionRadioButton.h
/* ProportionRadioButton */
#import <Cocoa/Cocoa.h>
@interface ProportionRadioButton : NSButton
{
SEL theDraggedSelector;
NSString *theDraggedText;
}
- (id)initWithFrame:(NSRect)frameRect;
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
- (void)setDraggedAction:(SEL)aselector;
- (NSString *)draggedText;
@end
setDraggedAction sets the selector that the target will implement
to do whatever it does when the drag completes.
draggedText (in this specific implementation) returns the text
that was dropped. You can fiddle with the code so as to deal
with other dragged types if needed.
Here is ProportionRadioButton.m
#import "ProportionRadioButton.h"
@implementation ProportionRadioButton
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:(NSRect)frameRect];
// register your specific types here . . .
[self registerForDraggedTypes:[NSArray arrayWithObjects:
NSStringPboardType, nil]];
return self;
}
- (void)setDraggedAction:(SEL)aselector
{
theDraggedSelector = aselector;
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask = [sender
draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject: NSStringPboardType]) {
if ((sourceDragMask & NSDragOperationGeneric) != 0) {
return NSDragOperationGeneric;
}
}
return NSDragOperationNone;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *pasteboard = [sender draggingPasteboard];
BOOL result = NO;
if ([[pasteboard types] containsObject: NSStringPboardType]) {
NSString *draggedtext = [pasteboard stringForType:(NSString
*)NSStringPboardType];
theDraggedText = draggedtext;
if (theDraggedSelector != nil) {
[[self target] performSelector: (SEL)theDraggedSelector
withObject: self];
}
result = YES;
}
return result;
}
- (NSString *)draggedText
{
return theDraggedText;
}
@end
Hope this helps,
........ Henry
===============================+============================
Henry McGilton, Boulevardier | Trilithon Software
Objective-C/Java Composer | Seroia Research
-------------------------------+----------------------------
mailto:email@hidden | http://www.trilithon.com
|
===============================+============================
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40fornextsoft.com
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