/*
 * AppController.j
 *
 * Created by Philippe Laval on 2009/03/11.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>


@implementation AppController : CPObject
{
	CPSplitView verticalSplitter;
	CPSplitView horizontalSplitter;
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
     /* Enable Logging (DEBUG) */
    // CPLogRegister(CPLogPopup);

    var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
    var contentView = [theWindow contentView];



	var button1 = [[CPButton alloc] initWithFrame:CGRectMake(10, 10, 100, 24)];
	[button1 setTitle:@"button1"];
	var button2 = [[CPButton alloc] initWithFrame:CGRectMake(10, 40, 100, 24)];
	[button2 setTitle:@"button2"];

    	
    verticalSplitter = [[CPSplitView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([contentView bounds]), CGRectGetHeight([contentView bounds]))];
	[verticalSplitter setDelegate:self];
	[verticalSplitter setVertical:YES]; 
	[verticalSplitter setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable ]; 
	[horizontalSplitter setIsPaneSplitter:NO];
	[contentView addSubview:verticalSplitter];

	var leftView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 200, CGRectGetHeight([verticalSplitter bounds]))];
	[bottomView setAutoresizingMask:CPViewHeightSizable]; 
	
	var rightView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([verticalSplitter bounds]) - 200, CGRectGetHeight([verticalSplitter bounds]))];
	[rightView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable]; 

	[verticalSplitter addSubview:leftView];
	[verticalSplitter addSubview:rightView];

	horizontalSplitter = [[CPSplitView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([rightView bounds]), CGRectGetHeight([rightView bounds]))];
	[horizontalSplitter setDelegate:self];
	[horizontalSplitter setVertical:NO]; 
	[horizontalSplitter setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable ]; 
	// 1 pixel size for the splitter
	[horizontalSplitter setIsPaneSplitter:YES];

	[leftView addSubview:button1];
	[leftView addSubview:button2];
	[rightView addSubview:horizontalSplitter];

	var topView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([horizontalSplitter bounds]), 100)];
	[bottomView setAutoresizingMask:CPViewWidthSizable]; 
	
	var bottomView = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([horizontalSplitter bounds]), CGRectGetHeight([horizontalSplitter bounds]) - 100)];
	[bottomView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable ]; 

	[horizontalSplitter addSubview:topView];
	[horizontalSplitter addSubview:bottomView];


    var label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];

    [label setStringValue:@"Hello World!"];
    [label setFont:[CPFont boldSystemFontOfSize:24.0]];
    [label sizeToFit];

    [label setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [label setFrameOrigin:CGPointMake((CGRectGetWidth([bottomView bounds]) - CGRectGetWidth([label frame])) / 2.0, 
						(CGRectGetHeight([bottomView bounds]) - CGRectGetHeight([label frame])) / 2.0)];

    [bottomView addSubview:label];



	var button3 = [[CPButton alloc] initWithFrame:CGRectMake(0, 0, 200, 24)];
	[button3 setTitle:@"Option1"];
//	[button3 setButtonType:CPSwitchButton];
//	[button3 setButtonType:CPRadioButton];
//	[button3 setButtonType:CPOnOffButton];
//	[button3 setState:CPOnState];
//	[button3 sizeToFit];

	var button4 = [[CPButton alloc] initWithFrame:CGRectMake(0, 40, 200, 24)];
	[button4 setTitle:@"Option2"];
//	[button4 setButtonType:CPSwitchButton];
//	[button4 setButtonType:CPRadioButton];
//	[button4 setButtonType:CPOnOffButton];
//	[button4 setState:CPOffState];
//	[button4 sizeToFit];

	[topView addSubview:button3];
	[topView addSubview:button4];



    [theWindow orderFront:self];


    // Uncomment the following line to turn on the standard menu bar.
    [CPMenu setMenuBarVisible:YES];
}

-(void) splitViewDidResizeSubviews:(CPNotification)notification
{
	var splitView = [notification object];

	CPLog.trace(@"splitViewDidResizeSubviews:");

	if (splitView  == verticalSplitter)
	{
		CPLog.trace(@"Vertical splitter: " + [splitView description]);
	}
	else
	{
		CPLog.trace(@"Horizontal splitter: " + [splitView description]);
	}

}

-(void) splitViewWillResizeSubviews:(CPNotification)notification
{
	var splitView = [notification object];

	CPLog.trace(@"splitViewWillResizeSubviews:");

	if (splitView == verticalSplitter)
	{
		CPLog.trace(@"Vertical splitter: " + [splitView description]);
	}
	else
	{
		CPLog.trace(@"Horizontal splitter: " + [splitView description]);
	}

}

/*
-(CGRect)splitView:(CPSplitView)splitView effectiveRect:(CGRect)effectiveRect forDrawnRect:(CGRect)drawnRect ofDividerAtIndex:(int)anIndex
{
}
*/

// Add additional size for tracking the splitter
/*
-(CGRect) splitView:(CPSplitView)splitView additionalEffectiveRectOfDividerAtIndex:(int)anIndex
{
	CPLog.trace(@"splitView:additionalEffectiveRectOfDividerAtIndex:"); 

	if (splitView == horizontalSplitter)
	{
		CPLog.trace(@"Horizontal");

		var rect = [self rectOfDividerAtIndex:anIndex];
		CPLog.trace(CPStringFromRect(rect));
		
		//var newRect = CPRectInset(rect, -5, -5);

		newRect = CGRectMake(0, 0, 500, 500)
		CPLog.trace(CPStringFromRect(newRect));
		return newRect;
	}
	else
	{
		CPLog.trace(@"Vertical");

		var newRect = CGRectMake(0, 0, 0, 0);
		CPLog.trace(CPStringFromRect(newRect));
		//return newRect;
		return null;
	}
}
*/

-(BOOL) splitView:(CPSplitView)splitView shouldCollapseSubview:(CPView)view forDoubleClickOnDividerAtIndex:(int)anIndex
{
	CPLog.trace(@"splitView:shouldCollapseSubview:forDoubleClickOnDividerAtIndex:"); 
	return YES;
}

-(BOOL) splitView:(CPSplitView)splitView canCollapseSubView:(CPView)view
{
//	debugger;
	CPLog.trace(@"splitView:canCollapseSubView:"); 
	return YES;
}

@end
