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
fileDrop.m
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