#import "IBetterForm.h"
#import <appkit/FormCell.h>
#import <appkit/Text.h>
#import <appkit/Font.h>
#import <stdio.h>
#import "errdebug.h"

// Form for article header.  Terribly complicated since both field titles
// and field values are editable.  The appkit only supports editable
// field values so this terrible kludge was neccessary.  Basically both
// field names and field values are implemented as Forms with the field
// titles hidden.

@implementation IBetterForm

- initFrame:(const NXRect *)frameRect
{
    NXRect titleFrame, valueFrame;
    NXSize titleCellSize, valueCellSize;

    DBG(1, fprintf(stderr, "[IBetterForm initFrame:]\n");)

    [self sizeTo:frameRect->size.width :frameRect->size.height];
    [Form setCellClass:[FormCell class]];
    titleWidth = frameRect->size.width / 4.0;
    titleFrame = valueFrame = *frameRect;
    titleFrame.size.width = titleWidth;
    titleForm = [[Form alloc] initFrame:&titleFrame];
    [titleForm setAutosizeCells:YES];
    titleCellSize.width = 300.0;
    titleCellSize.height = 21.0;
    [titleForm setCellSize:&titleCellSize];
    [titleForm setFont:[Font newFont:"Helvetica-Bold" size:12]];
    [titleForm setTitleAlignment:NX_LEFTALIGNED];
    [titleForm setTextAlignment:NX_LEFTALIGNED];
    [self addSubview:titleForm];
    valueFrame.origin.x += titleWidth;
    valueFrame.size.width -= titleWidth;
    valueForm = [[Form alloc] initFrame:&valueFrame];
    [valueForm setAutosizeCells:YES];
    valueCellSize.width = 1000.0;
    valueCellSize.height = 21.0;
    [valueForm setCellSize:&valueCellSize];
    [valueForm setFont:[Font newFont:"Helvetica" size:12]];
    [titleForm setTitleAlignment:NX_LEFTALIGNED];
    [valueForm setTextAlignment:NX_LEFTALIGNED];
    [self addSubview:valueForm];
    [self addSubview:titleForm];
    return self;
}

- addEntry:(const char *)aTitle
{
    const char *title;
    FormCell *valueCell;

    if (aTitle != NULL) {
        title = aTitle;
    } else {
        title = " ";
    }
    [[[[titleForm addEntry:"1234567890123456789 "] setTitleWidth:1.0]
        setStringValue:title] setEnabled:aTitle == NULL];
//        setBezeled:aTitle == NULL];
    valueCell = [[valueForm addEntry:"1234567890123456789 "]
        setTitleWidth:1.0];
    [valueCell setScrollable:YES];
    return valueCell;
}

- removeEntryAt:(int)index
{
    [titleForm removeEntryAt:index];
    [valueForm removeEntryAt:index];
}

- (const char *)titleAt:(int)index
{
    return [titleForm stringValueAt:index];
}

- (const char *)stringValueAt:(int)index
{
    return [valueForm stringValueAt:index];
}

- cellAt:(int)row :(int)col
{
    return [valueForm cellAt:row :col];
}

- selectCell:aCell
{
    return [valueForm selectCell:aCell];
}

#define OFFSET -140
- sizeToFit
{
    NXRect titleFrame, valueFrame;

    [titleForm sizeToFit];
    [valueForm sizeToFit];
    [titleForm getFrame:&titleFrame];
    [valueForm getFrame:&valueFrame];
    titleWidth = titleFrame.size.width;
    titleFrame.origin.x = frame.origin.x + OFFSET;
    [titleForm setFrame:&titleFrame];
    valueFrame.origin.x = frame.origin.x + titleWidth + 2 * OFFSET;
    [valueForm setFrame:&valueFrame];
    [self sizeTo:titleFrame.size.width + valueFrame.size.width + 2 * OFFSET
        :titleFrame.size.height];
    [valueForm display];
    [titleForm display];
    return self;
}

// set target and action to trip needs saving flag

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

@end
