#import "controller.h"
#import "tableDataSource.h"

@implementation controller
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
		//[panel makeKeyAndOrderFront:self];
		//[progressBar setUsesThreadedAnimation:1];	
		//[progressBar startAnimation:self];

		//[dataSource loadData:self];
		
		//[panel close];
		
		[NSApp beginSheet:panel modalForWindow:mainWin
		modalDelegate:self didEndSelector:NULL contextInfo:nil];
		[progressBar setUsesThreadedAnimation:1];	
		[progressBar startAnimation:self];
		[dataSource loadData:self];
		[progressBar stopAnimation:self];
		// Do some stuff with the array
		[panel orderOut:self];
		[NSApp endSheet:panel];
	}

- (void)awakeFromNib{
    /* Adding toolbar to the windows */
    [self addToolbar:mainWin];

}

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag{
	[mainWin makeKeyAndOrderFront:self];
	return YES;
}

#pragma mark Toolbar settings

#define        TB_sHeight        @"tb_sortHeight"
#define        TB_sWidth         @"tb_sortWidth"
#define		   TB_fontSize		 @"tb_fontSize"
#define        TB_id             @"tb_toolbar"

 
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar {
    /* Item default in the toolbar */    
    return [NSArray arrayWithObjects:   
        //NSToolbarFlexibleSpaceItemIdentifier,  
        TB_sHeight,             // Shot
        TB_sWidth,
        NSToolbarFlexibleSpaceItemIdentifier,    // flexible space
		TB_fontSize,
        NSToolbarCustomizeToolbarItemIdentifier, // customize item
        nil]; 
}

- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar{
    /* item allowed in the toolbar */
    return [NSArray arrayWithObjects:
        TB_sHeight,
        NSToolbarFlexibleSpaceItemIdentifier,     // flexible space
        TB_sWidth,
		TB_fontSize,
        NSToolbarCustomizeToolbarItemIdentifier,  // customize item
        nil]; 
}

- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar 
    itemForItemIdentifier: (NSString *) itemIdent
    willBeInsertedIntoToolbar:(BOOL) willBeInserted{
        
        NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease]; 
    if ([itemIdent isEqual: TB_sWidth]){
        /* Shot icon definition */
        [toolbarItem setLabel: @"Sort by W"];
        [toolbarItem setPaletteLabel: @"Sort by width"];
        [toolbarItem setToolTip: @"Sort fonts by width"];
        [toolbarItem setImage: [NSImage imageNamed: @"width.png"]];
        [toolbarItem setTarget: dataSource];
        [toolbarItem setAction: @selector(sortData:)];
        return toolbarItem;
        }
    if ([itemIdent isEqual: TB_sHeight]){
        /* Shot icon definition */
        [toolbarItem setLabel: @"Sort by H"];
        [toolbarItem setPaletteLabel: @"Sort by height"];
        [toolbarItem setToolTip: @"Sort fonts by height"];
        [toolbarItem setImage: [NSImage imageNamed: @"height.png"]];
        [toolbarItem setTarget: dataSource];
		[toolbarItem setAction: @selector(sortData:)];
        //[toolbarItem setAction: NSSelectorFromString(@"sortFont:")];
        return toolbarItem;
        }
    if ([itemIdent isEqual: TB_fontSize]){
        /* Shot icon definition */
        [toolbarItem setLabel: @"Font Size"];
        [toolbarItem setPaletteLabel: @"Font Size"];
        [toolbarItem setToolTip: @"Select font size in pt."];
		[toolbarItem setView:fontSizeView];
		[toolbarItem setMaxSize:NSMakeSize(64.0f, 32.0f)];
		[toolbarItem setMinSize:NSMakeSize(64.0f, 32.0f)];

        //[toolbarItem setTarget: dataSource];
		//[toolbarItem setAction: @selector(sortData:)];
        return toolbarItem;
        }

        return nil;
    }
    

- (void) addToolbar: (NSWindow *) win{
    // costruisco la nuova istanza della toolbar
    // la faccio autorelease perche' sara' poi ritenuta dalla finestra
    NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier: TB_id] autorelease];

    // imposto le proprieta' di base della toolbar
    // permetto che l'utente la modifichi
    [toolbar setAllowsUserCustomization: YES];
    // lascio che conservi lo stato tra successivi lanci
    [toolbar setAutosavesConfiguration: YES];
    // dico che mostri solo le icone
    //[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
    [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];

    // dico che il delegato della toolbar e' il documento stesso
    [toolbar setDelegate: self];
    // attacco la toolbar alla finestra
    [ win setToolbar: toolbar ];
}


@end
