
#import "EnvWindow.h"
#import "VScheme.h"

@implementation EnvWindow

#define WIDTH  300
#define HEIGHT 200

static int count = 0;
- initWithEnv:(Environment *)env
{
    NSWindow *window;
    Environment *layer; int lind;
    NSRect contentRect = {{0, 0}, {WIDTH, HEIGHT}};
    NSRect winRect = 
    {{250+(count%12)*24, 100+(count%12)*24}, {WIDTH, HEIGHT}};
    NSRect textRect;
    unsigned int style = NSTitledWindowMask | NSClosableWindowMask |
        NSMiniaturizableWindowMask | NSResizableWindowMask;
    NSString *title =              
        [NSString stringWithFormat:@"Scheme Env. # %d", ++count];              

    length = [env chainLength];
    names = NSZoneMalloc([self zone], length*sizeof(id));
    values = NSZoneMalloc([self zone], length*sizeof(id));

    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    
    for(lind=length-1, layer = env; lind>=0; lind--){
        NSMapTable *data = [layer data];

        names[lind] = [NSMutableArray arrayWithArray:NSAllMapTableKeys(data)];
        [names[lind] sortUsingSelector:@selector(compare:)];
	[names[lind] retain];
	
	values[lind] = [NSMutableArray arrayWithCapacity:[names[lind] count]];
	[values[lind] retain];

        NSEnumerator *en = [names[lind] objectEnumerator];
	id key;
        while((key = [en nextObject])!=nil){
            id obj = NSMapGet(data, key);
	    [values[lind] addObject:[VScheme valToString:obj]];
        }
                
        layer = [layer parent];
    }

    [pool release];

    [self initWithContentRect:winRect
          styleMask:style
          backing:NSBackingStoreRetained
          defer:NO];
    [self setMinSize:NSMakeSize(WIDTH, HEIGHT)];
    [self setReleasedWhenClosed:YES];


    NSTableColumn *nameColumn, *valueColumn;

    nameColumn = 
	[(NSTableColumn *)[NSTableColumn alloc] 
			  initWithIdentifier: @"Name"];
    [nameColumn setEditable: NO];
    [[nameColumn headerCell] setStringValue: @"Name"];
    [nameColumn setMinWidth:WIDTH/2];

    valueColumn = 
	[(NSTableColumn *)[NSTableColumn alloc] 
			  initWithIdentifier: @"Value"];
    [valueColumn setEditable: NO];
    [[valueColumn headerCell] setStringValue: @"Value"];
    [valueColumn setMinWidth:WIDTH/2];

    table = 
	[[NSTableView alloc] initWithFrame:contentRect];
    [table addTableColumn:nameColumn]; RELEASE(nameColumn);
    [table addTableColumn:valueColumn]; RELEASE(valueColumn);

    current=length-1;
    [table setDataSource:self];
   
    scrollView = [[NSScrollView alloc] initWithFrame:contentRect];
    [scrollView setHasHorizontalScroller:YES];
    [scrollView setHasVerticalScroller:YES]; 
    [scrollView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
    [[scrollView contentView] 
	setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
    [[scrollView contentView] setAutoresizesSubviews:YES];

    [table setFrameSize:[scrollView contentSize]];
    [scrollView setDocumentView:table];

    [self setContentView:scrollView];
    // RELEASE(scrollView);

    [self setTitle:title];
    [self display];
    [self makeKeyAndOrderFront:nil];

    return self;
}

- up:(id)sender
{
    if(!current){
        NSBeep();
    }
    else{
        current--;
	[table reloadData];
    }

    return self;
}

- down:(id)sender
{
    if(current==length-1){
        NSBeep();
    }
    else{
        current++;
	[table reloadData];
    }

    return self;
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return [names[current] count];
}

-            (id)tableView:(NSTableView *)aTableView 
 objectValueForTableColumn:(NSTableColumn *)aTableColumn 
                       row:(int)rowIndex
{
    if(rowIndex>=[names[current] count]){
	return nil;
    }

    if([[aTableColumn identifier] isEqualToString:@"Name"]){
	return [names[current] objectAtIndex:rowIndex];
    }
    else{
	return [values[current] objectAtIndex:rowIndex];
    }
}

- (void)dealloc
{
    int ind;

    [table release];
    [scrollView release];
    
    for(ind=0; ind<length; ind++){
        // NSLog(@"%@ %d %d\n", self, ind, [tables[ind] retainCount]);
	[names[ind] release];
	[values[ind] release];
    }

    NSZoneFree([self zone], names);
    NSZoneFree([self zone], values);

    [super dealloc];
}
@end

