Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

giha

macrumors newbie
Original poster
Nov 16, 2009
4
0
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:NSFilenamesPboardType, nil]];
	}
	return self;
}

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

- (void)setFile:(NSString *)path
{
	if (mFilePath) {
		[mFilePath release];
	}
	
	mFilePath = [[NSString stringWithString:path] retain];
	NSImage * image = [[NSWorkspace sharedWorkspace] iconForFile:mFilePath];
	[image setSize:NSMakeSize(48.0, 48.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 grayColor] set];
		[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 types] containsObject: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
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

giha

macrumors newbie
Original poster
Nov 16, 2009
4
0
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
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.