Forum Navigation
Forum breadcrumbs - You are here:WeLoveGod RallysPublic Forums: fbcocoaDragNDrop
You need to log in to create posts and topics.

DragNDrop

Posted by: svanvoorst <svanvoorst@...>

For those interested, the following demo is an 'all objc' version of Bernie's hybrid code (part FB, part objc), recently published on the FBList.  Thanks for showing us, Bernie.  It differs in that a special view is not used to mark a 'drop' on the window; instead the background color of the window is changed so that there is visual confirmation.  A window delegate handles the drag and drop event.

'------------ start ---------

include "FBLog.incl"
BeginCDeclaration
@interface WndDelegate:NSObject <NSWindowDelegate>
{
  NSWindow *window;
  NSColor *wndBkgrndColor;
  BOOL dragInside;
}
@property (retain) NSWindow *window;
@property (retain) NSColor *wndBkgrndColor;
@end
EndC
BeginCFunction
@implementation WndDelegate
@synthesize window;
@synthesize wndBkgrndColor;
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender
{
   NSPasteboard *pboard = [sender draggingPasteboard];
   if ( [[pboard types] containsObject:NSFilenamesPboardType] ) 
    {
      dragInside = YES;
      [window setBackgroundColor:[NSColor whiteColor]];
       return NSDragOperationLink;
    }
    return NSDragOperationNone; 
}
- (void)draggingExited:(id < NSDraggingInfo >)sender
{
  dragInside = NO;
  [window setBackgroundColor:wndBkgrndColor];
}
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender
{
   NSPasteboard *pboard = [sender draggingPasteboard];
   if ([[pboard types] containsObject:NSFilenamesPboardType])
   {
     NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
     NSLog(@"file: %@", files);
     [self draggingExited:sender];
     return YES;
   }
  return NO;
}
- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender
{
  return YES;
}
- (BOOL)windowShouldClose:(id)sender
{
  return YES;
}
@end
EndC
BeginCDeclaration
@interface AppDelegate : NSObject
{
  NSWindow *window;
  WndDelegate *wndDelegate;
}
- (void) createMenu;
- (void) createWindow;
@end
EndC
BeginCFunction
@implementation AppDelegate
- (void) dealloc
{
  [wndDelegate release];
  [window release];
  [super dealloc];
}
- (void) createMenu
{
// ******** Menu and menubar ********//
  id menubar = [[NSMenu new] autorelease];
  id appMenuItem = [[NSMenuItem new] autorelease];
  [menubar addItem:appMenuItem];
  [NSApp setMainMenu:menubar];
  id appMenu = [[NSMenu new] autorelease];
  id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"
        action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
  [appMenu addItem:quitMenuItem];
  [appMenuItem setSubmenu:appMenu];
}
- (void) createWindow
{
  // ***** Window ***** //
  #define _wndW  300
  #define _wndH  150
  window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
                       styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask
                       backing: NSBackingStoreBuffered
                       defer: NO];
  [window center];
  [window setTitle: @"DragNDrop Window"];
  NSColor *wndBkgrndColor = [window backgroundColor];  
  wndDelegate = [[WndDelegate alloc] init];
  [wndDelegate setWindow:window];
  [wndDelegate setWndBkgrndColor:wndBkgrndColor];
  [window registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
  [window setDelegate:wndDelegate];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification
{
  [self createMenu];
  [self createWindow];
}
- (void) applicationDidFinishLaunching: (NSNotification *)notification
{
  [window makeKeyAndOrderFront: nil];
}
@end
EndC
BeginCCode
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
  [NSApplication sharedApplication];
  AppDelegate *myDelegate = [[[AppDelegate alloc] init]autorelease];
  [NSApp setDelegate: myDelegate];
  [NSApp run];
  [pool drain];
EndC

'-------- end ----------

Steve Van Voorst


To unsubscribe, e-mail: fbcocoa-unsubscribe@welovegod.org
For additional commands, e-mail: fbcocoa-help@freegroups.net