#import "IHeaderFormScrollView.h"
#import "IBetterForm.h"
#import <appkit/Font.h>
#import <appkit/FormCell.h>
#import <appkit/ScrollView.h>

#import <string.h>

@implementation IHeaderFormScrollView

- init
{
    NXRect contRect = {{0.0, 0.0}, {0.0, 0.0}};

    iForm = [[IBetterForm allocFromZone:[self zone]] initFrame:&contRect];
//    [iForm setTitleFont:[Font newFont:"Helvetica-Bold" size:12]];
//    [iForm setTextFont:[Font newFont:"Helvetica" size:12]];
//    [iForm setBackgroundGray:NX_LTGRAY];
    [iForm notifyAncestorWhenFrameChanged:YES];
//    [iForm setScrollable:YES];
//    [iForm setAutoscroll:NO];
    iCellCount = 0;
    return(self);
}

- showForm:sender
{
    NXSize size;
    NXRect frame;

    [iForm sizeToFit];
//    [[iForm superview] descendantFrameChanged:iForm];
//    [iScrollView descendantFrameChanged:iForm];
//    [iScrollView reflectScroll:[iForm superview]];
    // scroll to top
    [iScrollView getContentSize:&size];
    [[iScrollView docView] getFrame:&frame];
    frame.origin.x = 0.0;
    frame.origin.y = - frame.size.height - size.height;
    [[iScrollView docView] setFrame:&frame];
    [iScrollView setBackgroundGray:NX_LTGRAY];
    [iScrollView display];
    return(self);
}

- initScrollView:scrollView
{
    NXRect	contRect, scrollRect;
    id		oldView;
    
    [super init];
    iScrollView = scrollView;
    [iScrollView setHorizScrollerRequired:YES];
    [iScrollView getFrame:&scrollRect];
    [ScrollView getContentSize:&(contRect.size)
    		forFrameSize:&(scrollRect.size)
		horizScroller:YES
		vertScroller:YES
		borderType:NX_BEZEL];
    contRect.origin.x = contRect.origin.y = 0.0;
    contRect.size.width = 1100.0;
    contRect.size.height = 21.0;
    [iScrollView setAutosizing:NX_WIDTHSIZABLE | NX_HEIGHTSIZABLE];
    oldView = [iScrollView setDocView:iForm];
    if (oldView != nil) {
	[oldView free];
    }
    return(self);
}

- (void)writeToStream:(NXStream *)stream
{
    int		i;
    const char	*key, *value;

    for (i = 0; i < iCellCount; ++i) {
	key = [iForm titleAt:i];
	if ((value = [iForm stringValueAt:i]) != NULL) {
            for (; *value == ' '; ++value);
            if (*value != '\0') {
	        NXPrintf(stream, "%s: %s\r\n", key, value);
            }
	}
    }
}

- (const char *)stringValueAtTitle:(const char *)title
{
    int         i;
    const char  *value;

    for (i = 0; i < iCellCount; i++) {
        if (strcmp([iForm titleAt:i], title) == 0) {
	    if ((value = [iForm stringValueAt:i]) != NULL) {
                for (; *value == ' '; ++value);
                if (*value != '\0') {
                    return(value);
                }
            }
            break;
	}
    }
    return(NULL);		/* no such title */
}

- (FormCell *)setStringValue:(const char *)aValue atTitle:(const char *)title
    setEnabled:(BOOL)flag
{
    const char *value;
    int i;
    FormCell *cell;

    if (aValue == NULL) {
        return(nil);
    } else if (*aValue != '\0') {
        value = aValue;
    } else {
        value = " ";
    }
    for (i = 0; i < iCellCount; i++) {
        if (strcmp([iForm titleAt:i], title) == 0) {
            cell = [iForm cellAt:i :0];
            [cell setStringValue:value];
            [cell setEnabled:flag];
            return(cell);
        }
    }
    cell = [iForm addEntry:title];
    [cell setStringValue:value];
    [cell setEnabled:flag];
    ++iCellCount;
    return(cell);
}

- (FormCell *)selectCellAtTitle:(const char *)title
{
    int i;
    FormCell *cell;

    for (i = 0; i < iCellCount; i++) {
        if (strcmp([iForm titleAt:i], title) == 0) {
            cell = [iForm cellAt:i :0];
            [iForm selectCell:cell];
            return(cell);
        }
    }
    return(nil);
}


- (BOOL)setEnabled:(BOOL)flag atTitle:(const char *)title
{
    int         i;

    for (i = 0; i < iCellCount; i++) {
        if (strcmp([iForm titleAt:i], title) == 0) {
            [[iForm cellAt:i :0] setEnabled:flag];
            return(YES);
        }
    }
    return(NO);
}

- (BOOL)removeCellAtTitle:(const char *)title
{
    int         i;

    for (i = 0; i < iCellCount; i++) {
        if (strcmp([iForm titleAt:i], title) == 0) {
            [iForm removeEntryAt:i];
            --iCellCount;
            return(YES);
        }
    }
    return(NO);
}

- removeAllCells
{
    for (--iCellCount; iCellCount >= 0; --iCellCount) {
        [iForm removeEntryAt:iCellCount];
    }
    iCellCount = 0;
    return(self);
}

- addCellForNewEntry
{
    newCell = [iForm addEntry:NULL];
    [newCell setEnabled:YES];
    ++iCellCount;
    return(self);
}

- setTarget:target andAction:(SEL)action
{
    [iForm setTarget:target andAction:action];
    return(self);
}

@end

