//
//  AppController.m
//  Experiments
//
//  Created by Dr. H. Nikolaus Schaller on 26.02.06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "AppController.h"

@implementation myView

- (void) drawRect:(NSRect) frame
{
	[[NSColor greenColor] set];
	[NSBezierPath fillRect:frame];
	[[tabview selectedTabViewItem] drawLabel:YES inRect:frame];
}

@end

@implementation myCell


- (NSRect)drawingRectForBounds:(NSRect)theRect
{
	NSRect r=[super drawingRectForBounds:theRect];
	NSLog(@"drawingRectForBounds %@ -> %@", NSStringFromRect(theRect), NSStringFromRect(r));
	return r;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
	// here we get passed the same cellFrame rect as for drawWithFrame
	NSLog(@"drawInteriorWithFrame %@", NSStringFromRect(cellFrame));
	[super drawInteriorWithFrame:cellFrame inView:controlView];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
	NSLog(@"drawWithFrame %@", NSStringFromRect(cellFrame));
	[super drawWithFrame:cellFrame inView:controlView];
}


@end

@implementation AppController

- (void) hierarchy:(NSView *) view level:(int) level;
{
	NSEnumerator *e=[[view subviews] objectEnumerator];
	NSView *v;
	NSLog(@"%@%@", [@"" stringByPaddingToLength:2*level withString:@" " startingAtIndex:0], NSStringFromClass([view class]));
	while((v=[e nextObject]))
		[self hierarchy:v level:level+1];
}

- (void) hierarchy:(NSView *) view;
{
	while([view superview])
		view=[view superview];	// find root
	[self hierarchy:view level:0];
}

- (void) awakeFromNib;
{
	NSLog(@"awakeFromNib");
	if(1)
		{ // find out how an NSWindow is organized for its subviews
		[window setToolbar:[[NSToolbar alloc] initWithIdentifier:@"test"]];
		NSLog(@"\nWindow %@", window);
		NSLog(@"frame %@", NSStringFromRect([window frame]));
		NSLog(@"frameRectForContentRect %@", NSStringFromRect([window frameRectForContentRect:[window frame]]));
		NSLog(@"contentRectForFrameRect %@", NSStringFromRect([window contentRectForFrameRect:[window frame]]));
		NSLog(@"contentView %@", [window contentView]);
		
		[self hierarchy:[window contentView]];

		NSLog(@"standardWindowButton:NSWindowCloseButton=%@", [window standardWindowButton:NSWindowCloseButton]);
		NSLog(@"standardWindowButton:NSWindowMiniaturizeButton=%@", [window standardWindowButton:NSWindowMiniaturizeButton]);
		NSLog(@"standardWindowButton:NSWindowZoomButton=%@", [window standardWindowButton:NSWindowZoomButton]);
		NSLog(@"standardWindowButton:NSWindowToolbarButton=%@", [window standardWindowButton:NSWindowToolbarButton]);
		NSLog(@"standardWindowButton:NSWindowDocumentIconButton=%@", [window standardWindowButton:NSWindowDocumentIconButton]);
		NSLog(@"standardWindowButton:NSWindowToolbarButton superview=%@", [[window standardWindowButton:NSWindowToolbarButton] superview]);
			
/* result:
	NSWindow's view Hierarchy turns out to be
		NSThemeFrame (nil superview) or NSGrayFrame for Textured (brushed Metal) windows
			_NSThemeCloseWidget		close: button - always present (but may be disabled) - has probably an iVar to get/set "document edited"
			_NSThemeWidget			hide: - always present (but may be disabled)
			_NSThemeWidget			zoom: - always present (but may be disabled)
			NSView					contentView
 			_NSThemeWidget:			toggleToolbarShown: (optional)
			NSToolbarView:			toolbar (optional)
	a Borderless window has only:
		NSNextStepFrame
			NSView					contentView
 ? but what are NSFrameView, NSTitledFrame drawing the title, toolbar, resize handle?
 ? or is NSFrameView a superclass?
 ? they might be really private instances of NSWindow and not NSViews - just doing drawing
*/
		}
}

- (IBAction) show:(id) sender;
{
	if(1)
		{ // find out how an NSWindow is organized for its subviews after awake. Result: no change
		NSLog(@"\nWindow %@", window);
		NSLog(@"contentView %@", [window contentView]); 
		NSLog(@"contentView superview %@", [[window contentView] superview]); 
		NSLog(@"contentView superview superview %@", [[[window contentView] superview] superview]); 
		NSLog(@"contentView superview subviews %@", [[[window contentView] superview] subviews]); 
		NSLog(@"contentView superview subview[3] subviews %@", [[[[[window contentView] superview] subviews] objectAtIndex:3] subviews]); 
		NSLog(@"contentView superview subview[0] subviews %@", [[[[[window contentView] superview] subviews] objectAtIndex:0] subviews]); 
		// replace by a borderless window
		window=[[NSWindow alloc] initWithContentRect:NSMakeRect(30.0, 30.0, 220.0, 330.0) styleMask:0 backing:0 defer:NO];
		}
}

// sample code that uses NSLayoutManager

- (NSRect) containerSize:(NSString *) text fontname:(NSString *) fontname fontSize:(float) fontSize;
{
	NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:text];
	[textStorage addAttribute:NSFontAttributeName value:[NSFont fontWithName:fontname size:fontSize] range:NSMakeRange(0,[textStorage length])];
	NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
	[textStorage addLayoutManager:layoutManager];
	NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(FLT_MAX,FLT_MAX)];
	[textContainer setLineFragmentPadding:0.0];
	[layoutManager addTextContainer:textContainer];
	[layoutManager glyphRangeForTextContainer:textContainer];	// hier wird es anscheinend gerendert
	NSRect rect = [layoutManager usedRectForTextContainer:textContainer];
	[textContainer release];
	[layoutManager release];
	[textStorage release];
	return rect;
}

@end
