/*******************************************************************************
 Copyright (c) 2005-2006 Jordy Rose
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 
 Except as contained in this notice, the name(s) of the above copyright holders
 shall not be used in advertising or otherwise to promote the sale, use or other
 dealings in this Software without prior authorization.
*******************************************************************************/

//
//  FileInfoView.m
//  Dockyard
//

#import "FileInfoView.h"
#import <float.h>

static BOOL shouldConvertToFileInfoCell = YES;

/*!
 * FileInfoView is a subclass of NSTextField that displays an icon centered above
 * the view's text, similar to an NSButton. However, FileInfoView supports all of
 * NSTextField's more advanced text layout abilities. FileInfoView uses
 * @link FileInfoCell @/link to implement its user interface.
 */
@implementation FileInfoView
+ (void)initialize
{
	[self setCellClass:[FileInfoCell class]];
}

/*!
 * Returns <code>YES</code> if NSTextFieldCells are automatically converted to
 * FileInfoCells when unarchiving a FileInfoView, <code>NO</code> if not. The
 * default is <code>YES</code>.
 *
 * @see #setShouldConvertToFileInfoCell:
 */
+ (BOOL)shouldConvertToFileInfoCell
{
	return shouldConvertToFileInfoCell;
}

/*!
 * Sets whether or not NSTextFieldCells should be converted to FileInfoCells when
 * unarchiving instances of FileInfoView. This is to allow current Interface Builder
 * problems when setting a custom class for NSTextFields -- the cell is not
 * converted. By default, the conversion takes place automatically.
 *
 * @see #shouldConvertToFileInfoCell
 */
+ (void)setShouldConvertToFileInfoCell:(BOOL)newShouldConvert
{
	shouldConvertToFileInfoCell = newShouldConvert;
}

- (id)initWithCoder:(NSCoder *)decoder
{
	BOOL shouldUseSetClass = NO;
	BOOL shouldUseDecodeClassName = NO;
	if ([[self class] shouldConvertToFileInfoCell]) {
		if ([decoder respondsToSelector:@selector(setClass:forClassName:)]) {
			shouldUseSetClass = YES;
			[(NSKeyedUnarchiver *)decoder setClass:[FileInfoCell class] forClassName:@"NSTextFieldCell"];
			
		} else if ([decoder respondsToSelector:@selector(decodeClassName:asClassName:)]) {
			shouldUseDecodeClassName = YES;
			[(NSUnarchiver *)decoder decodeClassName:@"NSTextFieldCell" asClassName:@"FileInfoCell"];
		}
	}

	self = [super initWithCoder:decoder];

	if (shouldUseSetClass) {
		[(NSKeyedUnarchiver *)decoder setClass:[NSTextFieldCell class] forClassName:@"NSTextFieldCell"];
	} else if (shouldUseDecodeClassName) {
		[(NSUnarchiver *)decoder decodeClassName:@"NSTextFieldCell" asClassName:@"NSTextFieldCell"];
	}
	
	return self;
}

- (void)awakeFromNib
{
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:[self window]];
}

- (void)dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[super dealloc];
}

- (void)setObjectValue:(id)value
{
	[super setObjectValue:value];
/*	
	NSCell *cell = [self cell];
	if ([value respondsToSelector:@selector(icon)])
		[cell setImage:[value icon]];
	else if ([value respondsToSelector:@selector(image)])
		[cell setImage:[value image]];
	else
		[cell setImage:nil]; */
	
	NSRect frame = [self frame];
	frame.size.height = FLT_MAX;
	
	NSSize cellSize = [[self cell] cellSizeForBounds:frame];
	cellSize.width = frame.size.width;
	[self setFrameSize:cellSize];
	[self setNeedsDisplay];
}

/*!
 * Returns the image displayed by this view, or <code>nil</code> if there is no
 * image or if the cell does not support images.
 */
- (NSImage *)image
{
	NSCell *cell = [self cell];
	if ([cell respondsToSelector:_cmd]) {
		return [cell image];
	} else return nil;
}

/*!
 * If the view's cell supports images (which it does by default), sets the cell's
 * image and redraws the view.
 */
- (void)setImage:(NSImage *)newImage
{
	NSCell *cell = [self cell];
	if ([cell respondsToSelector:_cmd]) {
		[cell setImage:newImage];
		
		NSRect frame = [self frame];
		frame.size.height = FLT_MAX;
		
		NSSize cellSize = [[self cell] cellSizeForBounds:frame];
		cellSize.width = frame.size.width;
		[self setFrameSize:cellSize];
		[self setNeedsDisplay];
	}
}

- (BOOL)isFlipped
{
	return YES;
}

- (void)windowDidResize:(NSNotification *)note
{	
	NSRect frame = [self frame];
	frame.size.height = FLT_MAX;
	
	NSSize cellSize = [[self cell] cellSizeForBounds:frame];
	cellSize.width = frame.size.width;
	[self setFrameSize:cellSize];
	[self setNeedsDisplay];
}

@end
