/*
 * AppController.j
 * Tutorial-DotView
 *
 * Created by __Me__ on __Date__.
 * Copyright 2008 __MyCompanyName__. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import "DotView.j"
@import "DotResizeView.j"
@import "DotColorView.j"


var SliderToolbarItemIdentifier = "SliderToolbarItemIdentifier",
    ColorToolbarItemIdentifier = "ColorToolbarItemIdentifier";

@implementation AppController : CPObject
{
	DotView dotView;
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
	var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
    var contentView = [theWindow contentView];
    var bounds = [contentView bounds];

	// Create the toolbar and set its delegate to be the AppController instance.
    var toolbar = [[CPToolbar alloc] initWithIdentifier:"Photos"];    
    [toolbar setDelegate:self];
	[toolbar setVisible:YES];
	// Associate the toolbar with the window
	[theWindow setToolbar:toolbar];

    dotView = [[DotView alloc] initWithFrame:CGRectMake(10, 10, CPRectGetWidth(bounds) - 20, CPRectGetHeight(bounds) - 20)];

    [dotView setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
    [contentView addSubview:dotView];

    [theWindow orderFront:self];
}

- (void)setRadius:(id)sender
{
	[dotView setRadius:sender];
}

- (void)setColor:(id)sender 
{
	[dotView setColor:sender];
}

// **************************** Toolbar delegate code ****************************

// Return an array of toolbar item identifier (all the toolbar items that may be present in the toolbar)
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
   return [CPToolbarFlexibleSpaceItemIdentifier, SliderToolbarItemIdentifier, ColorToolbarItemIdentifier];
}

// Return an array of toolbar item identifier (the default toolbar items that are present in the toolbar)
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
   return [ColorToolbarItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, SliderToolbarItemIdentifier];
}

// Create the toolbar item that is requested by the toolbar.
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
	// Create the toolbar item and associate it with its identifier
    var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier:anItemIdentifier];

    if (anItemIdentifier == SliderToolbarItemIdentifier)
    {
    	// The toolbar is using a custom view (of class DotResizeView)
        [toolbarItem setView:[[DotResizeView alloc] initWithFrame:CGRectMake(0, 0, 180, 50)]];
        
        // We don't associate a target/action with the toolbar item. 
        // This will be done in the slider contained in the DotResizeView.
        [toolbarItem setLabel:"Scale"];

        [toolbarItem setMinSize:CGSizeMake(180, 32)];
        [toolbarItem setMaxSize:CGSizeMake(180, 32)];
    }
    else if (anItemIdentifier == ColorToolbarItemIdentifier)
    {
    	// The toolbar is using a custom view (of class DotColorView)
        [toolbarItem setView:[[DotColorView alloc] initWithFrame:CGRectMake(0, 0, 180, 50)]];
        
        // We don't associate a target/action with the toolbar item. 
        // This will be done in the colorWell contained in the DotColorView.
        [toolbarItem setLabel:"Color"];

        [toolbarItem setMinSize:CGSizeMake(50, 32)];
        [toolbarItem setMaxSize:CGSizeMake(50, 32)];
    }
    
    return toolbarItem;
}

@end
