Register FAQ/Rules Forum Spy Search Today's Posts Mark Forums Read

Welcome to the Mac Forums forums. Please read the FAQ if you have questions. Register to participate.

 
Go Back   Mac Forums > Apple Software > Mac Programming
TouchArcade.com - iPhone Game Reviews and News

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 16, 2009, 08:09 AM   #1
giha
macrumors newbie
 
Join Date: Nov 2009
Synchronizing icon of file between button and drag&drop

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

Last edited by giha : Nov 16, 2009 at 09:00 AM.
giha is offline   Reply With Quote
Old Nov 16, 2009, 04:06 PM   #2
kainjow
Demi-God (Moderator)
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   Reply With Quote
Old Nov 16, 2009, 04:53 PM   #3
giha
Thread Starter
macrumors newbie
 
Join Date: Nov 2009
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 Code:
//
//  open.h

#import <Cocoa/Cocoa.h>


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

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

#import <Cocoa/Cocoa.h>


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

@
end 
giha is offline   Reply With Quote
Old Nov 17, 2009, 04:30 PM   #4
kainjow
Demi-God (Moderator)
 
kainjow's Avatar
 
Join Date: Jun 2000
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];

Last edited by kainjow : Nov 17, 2009 at 05:36 PM.
kainjow is offline   Reply With Quote

Reply

Mac Forums > Apple Software > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:47 AM.

Mac News | Mac Rumors | iPhone Game Reviews | iPhone Apps

Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 2002-2010, MacRumors.com, LLC