//
//	AppController.m
//

#import "AppController.h"
#include <limits.h>

@implementation NSAffineTransform (Description)

- (NSString *) description;
{
	NSAffineTransformStruct ts=[self transformStruct];
	return [NSString stringWithFormat:
		@"((%f,%f,%f),(%f,%f,%f),(%f,%f,%f))",
		ts.m11, ts.m21, 0.0,
		ts.m12, ts.m22, 0.0,
		ts.tX, ts.tY, 1.0];		
}

@end

@implementation AppController

- (IBAction) periodic:(id) Sender;
{
	[cont setIntValue:[cont intValue]+1];
	[cont setNeedsDisplay:YES];
}

- (void) awakeFromNib
{
	NSWindow *w;
	NSLog(@"awakeFromNib");
	NSLog(@"NSNotFound=%d %u %08x", NSNotFound, NSNotFound, NSNotFound);
	NSLog(@"NSApp=%@", NSApp);
	NSLog(@"Bundle=%@", [NSBundle bundleForClass:[self class]]);
#if 1
	NSLog(@"scale factor=%lf", [[[NSScreen screens] objectAtIndex:0] userSpaceScaleFactor]);
#endif
#if 0
	NSLog(@"available Font Families=%@", [[NSFontManager sharedFontManager] availableFontFamilies]);
	NSLog(@"available Fonts=%@", [[NSFontManager sharedFontManager] availableFonts]);
	NSLog(@"font collections=%@", [[NSFontManager sharedFontManager] collectionNames]);
	// this one appears to be empty??
	NSLog(@"'All Fonts' collection=%@", [[NSFontManager sharedFontManager] fontDescriptorsInCollection:@"All Fonts"]);
	NSLog(@"'Web' collection=%@", [[NSFontManager sharedFontManager] fontDescriptorsInCollection:@"Web"]);
	NSLog(@"'Undefined' collection=%@", [[NSFontManager sharedFontManager] fontDescriptorsInCollection:@"Undefined"]);
#if 1
	{ // NSFont and NSFontDescriptor
		NSFont *f=[NSFont boldSystemFontOfSize:12];
		NSFontDescriptor *fd;
		NSLog(@"test systemFontOfSize:12 = %@", f);
		NSLog(@"descriptor = %@", [f fontDescriptor]);
		f=[f screenFont];
		NSLog(@"screen font = %@", f);
		NSLog(@"descriptor = %@", [f fontDescriptor]);
		fd=[NSFontDescriptor fontDescriptorWithName:@"Helveticb" size:12.0];
		NSLog(@"fd = %@", fd);
		NSLog(@"match keys=%@", [NSSet setWithArray:[[fd fontAttributes] allKeys]]);
		NSLog(@"fonts1 = %@", [fd matchingFontDescriptorsWithMandatoryKeys:[NSSet setWithArray:[[fd fontAttributes] allKeys]]]);
		NSLog(@"fonts2 = %@", [fd matchingFontDescriptorsWithMandatoryKeys:[[NSSet new] autorelease]]);	// result is array of NSFontDescriptors(!), always matches name, dows not contain size property
	}
#endif	
#endif
#if 0
	v=[NSMenuView alloc];
	NSLog(@"allocated");
	v=[v initWithFrame:NSMakeRect(0.0, 0.0, 100.0, 100.0)];
	[v setMenu:[NSApp mainMenu]];   // use to display the main menu
	NSLog(@"main menu attached");
	NSLog(@"view created: %@", v);
	[[win contentView] addSubview:v];
	[[win contentView] setNeedsDisplay:YES];	// and reflect change
	NSLog(@"added to %@ content View %@ - %@", win, [win contentView], [[win contentView] subviews]);
#endif
//	NSLog(@"PATH_MAX=%d", PATH_MAX);
	w=nil;
	NSLog(@"before alloc %d, %@", [w retainCount], [NSApp windows]);
	w=[NSWindow alloc];
	NSLog(@"after alloc %d, %@", [w retainCount], [NSApp windows]);
	w=[w initWithContentRect:NSMakeRect(10.0, 10.0, 50.0, 50.0) styleMask:0 backing:0 defer:NO];
	NSLog(@"after init %d, %@", [w retainCount], [NSApp windows]);
	[w orderFront:self];
	NSLog(@"after orderFront %d, %@", [w retainCount], [NSApp windows]);
	[w orderBack:self];
	NSLog(@"after orderBack %d, %@", [w retainCount], [NSApp windows]);
	[w orderOut:self];
	NSLog(@"after orderOut %d, %@", [w retainCount], [NSApp windows]);
	[w setReleasedWhenClosed:NO];
	[w close];  // might (auto)release
	NSLog(@"after close %d, %@", [w retainCount], [NSApp windows]);
	[w retain];
	NSLog(@"after retain %d, %@", [w retainCount], [NSApp windows]);
	[w release];
	NSLog(@"after release %d, %@", [w retainCount], [NSApp windows]);
	[w release];
	NSLog(@"after 2nd release %d, %@", [w retainCount], [NSApp windows]);
#if 1
	{ // check that Foundation is working properly
		NSAffineTransform *atm, *atm2;
		int i;
		atm=[NSAffineTransform transform];
		NSLog(@"unity=%@", atm);
		for(i=1; i<3; i++)
			{
			[atm translateXBy:10.0 yBy:20.0];
			NSLog(@"translated by (10,20)=%@", atm);
			[atm scaleXBy:2.0 yBy:3.0];
			NSLog(@"scaled by (2,3)=%@", atm);
			[atm rotateByDegrees:30.0];
			NSLog(@"rotated by (30deg)=%@", atm);
			}
		[atm invert];
		NSLog(@"inverted=%@", atm);
		atm2=[NSAffineTransform transform];
		[atm2 translateXBy:-10.0 yBy:-20.0];
		NSLog(@"atm2 translated by (-10,-20)=%@", atm2);
		[atm2 rotateByDegrees:60.0];
		NSLog(@"atm2 rotated by (90deg)=%@", atm2);
		[atm prependTransform:atm2];
		NSLog(@"atm2 prepended=%@", atm);
		NSLog(@"point %@ -> %@", NSStringFromPoint(NSMakePoint(20.0, 10.0)), NSStringFromPoint([atm transformPoint:NSMakePoint(20.0, 10.0)]));
		NSLog(@"size %@ -> %@", NSStringFromSize(NSMakeSize(30.0, 20.0)), NSStringFromSize([atm transformSize:NSMakeSize(30.0, 20.0)]));
		[atm appendTransform:atm2];
		NSLog(@"atm2 appended=%@", atm);
		NSLog(@"point %@ -> %@", NSStringFromPoint(NSMakePoint(20.0, 10.0)), NSStringFromPoint([atm transformPoint:NSMakePoint(20.0, 10.0)]));
		NSLog(@"size %@ -> %@", NSStringFromSize(NSMakeSize(30.0, 20.0)), NSStringFromSize([atm transformSize:NSMakeSize(30.0, 20.0)]));
	}
#endif
}

- (IBAction) doSomething:(id) Sender;
{
	NSLog(@"doSomething: %@", Sender);
	[v setHorizontal:![v isHorizontal]];
//	[tf setStringValue:[NSString stringWithFormat:@"%@%@\n", [tf stringValue], [Sender title]]];
	[v dataWithPDFInsideRect:[v bounds]];
}

- (IBAction) saveDocumentAs:(id) Sender;
{
	NSSavePanel *o=[NSSavePanel savePanel];
	[o runModal];
}

- (IBAction) openDocument:(id) Sender;
{
	NSOpenPanel *o=[NSOpenPanel openPanel];
	[o runModal];
}

- (int) numberOfRowsInTableView:(NSTableView *)aTableView
{
	if(aTableView == buttonTable)
		return 8*(NSRoundedDisclosureBezelStyle+5);
	return 17;
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
	NSString *ident=[aTableColumn identifier];
	if(aTableView == buttonTable)
		{
		if([ident isEqualToString:@"bezel"])
			return [NSString stringWithFormat:@"%d", rowIndex/8];
		if([ident isEqualToString:@"type"])
			return [NSString stringWithFormat:@"%d", rowIndex%8];
		return @"";
		}
	return [NSString stringWithFormat:@"%d: %@", rowIndex, ident];
}

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
	NSString *ident=[aTableColumn identifier];
	if(aTableView == buttonTable)
		{
		if([ident isEqualToString:@"button"])
			{
			[(NSButtonCell *) aCell setBordered:YES];
			[(NSButtonCell *) aCell setButtonType:rowIndex%8];
			[(NSButtonCell *) aCell setBezelStyle:rowIndex/8];
			[(NSButtonCell *) aCell setTitle:@"Button"];
			}
		if([ident isEqualToString:@"buttonh"])
			{
			[(NSButtonCell *) aCell setBordered:YES];
			[(NSButtonCell *) aCell setButtonType:rowIndex%8];
			[(NSButtonCell *) aCell setBezelStyle:rowIndex/8];
			[(NSButtonCell *) aCell setTitle:@"Button"];
			[(NSButtonCell *) aCell setHighlighted:YES];
			}
		}
}

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id) val forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
	NSLog(@"setObjectValue [%d, %@]: %@", rowIndex, [aTableColumn identifier], val);
}

@end