/* Implementation of FormCell class
 *
 * Copyright (C)  1993  The Board of Trustees of  
 * The Leland Stanford Junior University.  All Rights Reserved.
 *
 * Authors: Scott Francis, Paul Kunz, Imran Qureshi, and Libing Wang
 *
 * This file is part of an Objective-C class library for a window system
 *
 * FormCell.m,v 1.50 1995/12/13 22:33:01 fedor Exp
 */

#include "FormCell.h"

#include "TextFieldCell.h"
#include "Text.h"
#include <stdlib.h>
#include <string.h>
#include <objc/hashtable.h>
#include "graphics.h"

extern char *FormCellInstanceName(void);

@interface FormCell(WidgetSet)
- (NXCoord)_titleWidth;
@end

@implementation FormCell:ActionCell

- init
{
    [super init];
    titleCell = [[TextFieldCell alloc] _initAsLabel];
    [titleCell setAlignment:NX_RIGHTALIGNED];
    textCell = [[TextFieldCell alloc] init];
    instancename = FormCellInstanceName();
    return self;
}

- initTextCell:(const char *)aString
{
    [self init];
    [titleCell setStringValue:aString];
    return self;
}

- free
{
    [titleCell free];
    [textCell free];
    return [super free];
}


- (NXCoord)titleWidth:(const NXSize *)aSize
{
  [self notImplemented:_cmd];
  return titleWidth;
}

- (NXCoord)titleWidth
{
    return titleWidth;
}

- setTitleWidth:(NXCoord)width
{
    NXSize	size;

    titleWidth = width;
    if ( !widgetid ) {
	size.width = titleWidth;
	size.height = _frame.size.height;
	[titleCell _setSize:&size];
    }
    return self;
}

- (const char *)title
{
    return [titleCell stringValue];
}

- setTitle:(const char *)aTitle
{
    [titleCell setStringValue:aTitle]; 
    return self;
}

- titleFont
{
  return [self notImplemented:_cmd];
}

- setTitleFont:fontObj
{
  return [self notImplemented:_cmd];
}

- (int)titleAlignment
{
    return [titleCell alignment];
}

- setTitleAlignment:(int)mode
{
    [titleCell setAlignment:mode];
    return self;
}

- setEnabled:(BOOL)flag
{
    [titleCell setEnabled:flag];
    [textCell  setEnabled:flag];
    return self;
}

- (BOOL)isOpaque
{
    return YES;
}

- calcCellSize:(NXSize *)theSize inRect:(const NXRect *)aRect
{
  return [self notImplemented:_cmd];
}

- drawInside:(const NXRect *)cellFrame inView:controlView
{
  return self;
}

- drawSelf:(const NXRect *)cellFrame inView:controlView
{
  return self;
}

- (BOOL)trackMouse:(NXEvent*)event inRect:(const NXRect*)aRect 
            ofView:controlView
{
  [self notImplemented:_cmd];
  return 0;
}

- resetCursorRect:(const NXRect *)cellFrame inView:controlView
{
  return [self notImplemented:_cmd];
}

- write:(NXTypedStream *)stream
{
  return self;
}

- read:(TypedStream*)typedStream
{
    char               *cellContents;
    id			oldTitleCell;

    [super read:typedStream];
    objc_read_object(typedStream, &titleCell);

    oldTitleCell = titleCell;
    cellContents = (char *)[oldTitleCell stringValue];

    titleCell = [[TextFieldCell alloc] _initAsLabel];    
    [titleCell setAlignment:NX_RIGHTALIGNED];
    
    textCell = [[TextFieldCell alloc] init];
    [textCell setStringValue:contents];
    
    /* should probably be delayed until "awake" */
    [self setTitle:cellContents];
//    if we free it, objcX will crash in "awake" or "awakeFromNib" 
//    if (nil != oldTitleCell)  [oldTitleCell free];

    return self;
}


// + new;
// + newTextCell;
// + newTextCell:(const char *)aString;

- setStringValue:(const char *)aString
{
    [textCell setStringValue:aString];
    return self;
}

- (const char *)stringValue
{
    return [textCell stringValue];

}
- setIntValue:(int) anInt
{
    [textCell setIntValue:anInt];
    return self;
}

- (int) intValue
{
    return [textCell intValue];
}

- setFloatValue:(float)aFloat
{
    [textCell setFloatValue:aFloat];
    return self;
}

- (float) floatValue
{
    return [textCell floatValue];
}

- (double) doubleValue
{
    return [textCell doubleValue];
}

- setDoubleValue:(double)aDouble
{
    [textCell setDoubleValue:aDouble];
    return self;
}


- titleCell
{
    return titleCell;
}

- textCell
{
    return textCell;
}

- copyFromZone:(NSZone *) zone
{
    FormCell	*newCell;

    newCell = [super copyFromZone:zone];
    [newCell setTitle:[titleCell stringValue]];
    [newCell setStringValue:[textCell stringValue]];
    return newCell;
}

- _managedBy:parent
{
//   [super _managedBy:parent];
    [titleCell _managedBy:controlView];
    [textCell  _managedBy:controlView];

    return self;
}

- _destroy
{
    [super _destroy];
    [titleCell _destroy];
    [textCell _destroy];
    return self;
}
@end
