Synchronizing icon of file between button and drag&drop

Discussion in 'Mac Programming' started by giha, Nov 16, 2009.

  1. giha macrumors newbie

    Joined:
    Nov 16, 2009
    #1
    Hi all,

    I have 2 separate files:
    - open.m using button to open file
    - fileDrop.m using drag&drop without button to open file

    If i drag&drop a file i got the icon of this file displayed in the NSView. Everything is OK with fileDrop.m
    Now, i would like to do the same with open.m with the button:
    - If i open a file, it shows the icon of the file in the NSView
    - The button is using a custom image. And when i open a file, this image should be changed with the icon of file i am opening.

    Synchronizing icon of file between button and drag&drop:
    - And if i drag&drop a file, i got the icon of the file displayed in the NSView. At the same time it displays this icon (from drag&drop) in the button, too.

    I am a newbee, and do not have much experiences how it could be realized.
    Thank you for any your help!


    open.m
    PHP:
    //
    //  open.m

    #import "open.h"

    @implementation open

    - (IBAction)mButton:(id)sender1 {
        
    // Open panel
        
    NSOpenPanel *tNSOpenPanelObj    = [NSOpenPanel openPanel];
        [
    tvarNSOpenPanelObj runModalForTypes:nil];
        
    NSString *tDirpath = [tNSOpenPanelObj directory];
        
    NSLog(@"doOpen dirpath = %@"tDirpath);
        
    NSString *tFilepath = [tvarNSOpenPanelObj filename];
        
    NSLog(@"doOpen filepath = %@"tFilepath);
    }

    @
    end
    fileDrop.m
    PHP:
    //
    //  fileDrop.m

    #import "fileDrop.h"


    @implementation fileDrop


    - (id)initWithCoder:(NSCoder *)coder
    {
        
    self = [super initWithCoder:coder];
        if (
    self) {
            [
    self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardTypenil]];
        }
        return 
    self;
    }

    - (
    id)initWithFrame:(NSRect)frame
    {
        
    self = [super initWithFrame:frame];
        if (
    self) {
            [
    self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardTypenil]];
        }
        return 
    self;
    }

    - (
    void)setFile:(NSString *)path
    {
        if (
    mFilePath) {
            [
    mFilePath release];
        }
        
        
    mFilePath = [[NSString stringWithString:pathretain];
        
    NSImage image = [[NSWorkspace sharedWorkspaceiconForFile:mFilePath];
        [
    image setSize:NSMakeSize(48.048.0)];
        [
    self setImage:image];
    }

    - (
    NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
    {
        if ([
    self isEnabled]) {
            
    highlight YES;
            [
    self setNeedsDisplay:YES];
            return 
    NSDragOperationCopy;
        }
        
        return 
    NSDragOperationNone;
    }

    - (
    void)draggingExited:(id <NSDraggingInfo>)sender
    {
        
    highlight NO;
        [
    self setNeedsDisplay:YES];
    }

    ///////////////////////////////////////////////////////////
    //    Draw method is overridden to do drop highlighing
    ///////////////////////////////////////////////////////////

    - (void)drawRect:(NSRect)rect
    {
        
    // Draw the normal frame first
        
    [super drawRect:rect];
        
        
    // Then do the highlighting
        
    if (highlight) {
            [[
    NSColor grayColorset];
            [
    NSBezierPath setDefaultLineWidth:5];
            [
    NSBezierPath strokeRect:rect];
        }
    }

    - (BOOL)
    prepareForDragOperation:(id <NSDraggingInfo>)sender
    {
        
    highlight NO;
        [
    self setNeedsDisplay:YES];
        return 
    YES;


    - (BOOL)
    performDragOperation:(id <NSDraggingInfo>)info
    {
        
    NSPasteboard *pboard = [info draggingPasteboard];
        
        
    // Dragging Filenames From Finder or the List
        
    if ([[pboard typescontainsObject:NSFilenamesPboardType]) {
            
            
    NSArray files = [pboard propertyListForType:NSFilenamesPboardType];
            
    NSEnumerator enumerator = [files objectEnumerator];
            
    NSString filePath;
            
            
    // Get the First File
            
    if (filePath = [enumerator nextObject]) {
                [
    self setFile:filePath];
            }
            return 
    YES;
        }
        
        return 
    NO;
    }

    @
    end
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    In your open class, create an IBOutlet for your fileDrop view and connect it in the nib. Then you will have a reference to that view for calling the setFile: method.
     
  3. giha thread starter macrumors newbie

    Joined:
    Nov 16, 2009
    #3
    In the fileDrop.h there is a IBOutlet id delegate, i connect it with the button but it is not working. I really don't know how to connect with IBOutlet in open.h since i am total new with this language. Please help me, Thanks!


    PHP:
    //
    //  open.h

    #import <Cocoa/Cocoa.h>


    @interface open NSObject {
        
    }
    - (
    IBAction)mButton:(id)sender1;

    @
    end
    fileDrop.h
    PHP:
    //
    //  fileDrop.h

    #import <Cocoa/Cocoa.h>


    @interface fileDrop NSImageView {
        
    IBOutlet id delegate;
        
    BOOL highlight;
        
    NSString mFilePath;
    }

    @
    end
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    In open.h, declare
    Code:
    IBOutlet fileDrop *drop;
    In fileDrop.h declare your setFile: method public so your open class can see it.

    Then, in your mButton: method, you can call
    Code:
    [drop setFile:tFilepath];
     

Share This Page