Re: drag and drop in same app
Re: drag and drop in same app
- Subject: Re: drag and drop in same app
- From: Henry McGilton <email@hidden>
- Date: Mon, 10 Nov 2003 08:24:13 -0800
On Sunday, November 9, 2003, at 06:45 PM, LANCE GREENWOOD wrote:
I have a program with a window and a drawer that needs to do a
drag-drop in the same application. In the drawer is an NSMatrix of
NSTextFields, each one with a single letter in it (ex A, B, C,..). I
have set each cell in the matrix to selectable. In the Main window
there is an NSTextField the user can type one of the letters. I would
like it so the user could drag one of the letters from the matrix in
the drawer up to the textfield in the main window.
I can not find any docs or examples on doing this. "Building Cocoa
Applications" and "Cocoa programming for Mac OS X" only show examples
of doing it with sub-classed views.
Is it possible to do, and if so how?
I did this by subclassing NSTextField. Here is some code:
ProportionTextField.h
============================================================
/* ProportionTextField */
#import <Cocoa/Cocoa.h>
@interface ProportionTextField : NSTextField
{
SEL theDraggedSelector;
}
- (id)initWithFrame:(NSRect)frameRect;
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
- (void)setDraggedAction:(SEL)aselector;
@end
============================================================
ProportionTextField.m
============================================================
#import "ProportionTextField.h"
@implementation ProportionTextField
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:(NSRect)frameRect];
[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];
[self setStringValue: draggedtext];
if (theDraggedSelector != nil) {
[[self target] performSelector: (SEL)theDraggedSelector
withObject: self];
}
result = YES;
}
return result;
}
@end
============================================================
Then, in whatever controller object you have that actually
creates the main text field, you have code something like this:
ProportionTextField *yourTextField = [[ProportionTextField
alloc] initWithFrame: textfieldframe];
[yourContainerView addSubview: yourTextField];
[yourTextField setDelegate: self]; // if you need to catch
begin or end editing
[yourTextField setTarget: self];
[yourTextField setAction: (SEL)@selector(yourDraggedAction:)];
And then yourDraggedAction is whatever action you wish to take
when text is dragged onto the text field.
Best Wishes,
........ Henry
===============================+============================
Henry McGilton, Boulevardier | Trilithon Software
Objective-C/Java Composer | Seroia Research
-------------------------------+----------------------------
mailto:email@hidden |
http://www.trilithon.com
|
===============================+============================
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.