
/*
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 * David A. Cheslow disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 */

#import <appkit/Panel.h>
#import <appkit/FormCell.h>
#import <appkit/Button.h>
#import "CWAVScrollView.h"
#import "ComplexController.h"
#import "ComplexObject.h"

@implementation ComplexController

- matrixAction:sender
{
    NXRunAlertPanel("Click!","you clicked in the cell for Complex Object # %d", NULL,NULL,NULL,[sender selectedRow]);
    return self;
}

- addName:sender
{
    id newNameObject = [[ComplexObject alloc] init];
    [complexList addObject:newNameObject];
    [scrollingList loadCWAVMatrix];
    [[scrollingList cwavMatrix] scrollCellToVisible:[complexList count]-1 :0];
    [[scrollingList cwavMatrix] selectCellAt:[complexList count]-1:0];
    return self;
}

#define LASTNAMETAG	1
#define FIRSTNAMETAG	2
#define MIDDLEINITTAG	3

#define NOTETAG				4
#define RADIOBUTTONSTAG	5

- loadCWAV:cell from:(unsigned)index
{
	id currentNameObject = [complexList objectAt:index];
	[[cell findControlWithTag:LASTNAMETAG] 
			setStringValue:(const char *)[currentNameObject  lastName]];
	[[cell findControlWithTag:FIRSTNAMETAG] 
			setStringValue:(const char *)[currentNameObject  firstName]];
	[[cell findControlWithTag:MIDDLEINITTAG] 
			setStringValue:(const char *)[currentNameObject  middleInit]];
	[[cell findControlWithTag:NOTETAG] 
			setStringValue:(const char *)[currentNameObject  note]];
	[[cell findControlWithTag:RADIOBUTTONSTAG] 
			selectCellAt:0 :[currentNameObject radioButtonSelected]];
	return self;
}

- (unsigned)count
{
	return [complexList count];
}

-buttonPressed:sender
{
	int indexOfCellWithPressedButton = [sender cellIndexOf:sender];
	NXRunAlertPanel("Button Pressed",
					"You pressed the button in the cell for Complex Object #%d",
					NULL,NULL,NULL, indexOfCellWithPressedButton);
	return self;
}

-radioButtonPressed:sender
{
	unsigned indexOfCellWithPressedButton = [sender cellIndexOf:sender];
	id currentComplexObject;
	currentComplexObject = [complexList objectAt:indexOfCellWithPressedButton];
	NXRunAlertPanel("Radio Changed",
	"You pressed the radio button in the cell for Complex Object #%d, button %d is now pressed",
				NULL, NULL,NULL, indexOfCellWithPressedButton,[sender selectedCol]);
	[currentComplexObject setRadioButtonSelected:[sender selectedCol]];
	return self;
}

- textDidGetKeys:sender isEmpty:(BOOL)flag
{
	unsigned indexOfCellWithTextChanged = [sender cellIndexOf:sender];
	id currentComplexObject;
	id controlWithText = [sender superview];
	currentComplexObject = [complexList objectAt:indexOfCellWithTextChanged];
	if( [controlWithText  isKindOf:[Matrix class]])
	{
		switch ([controlWithText selectedRow])
		{
			case 0:
				[currentComplexObject setLastName:
						(char *)[[controlWithText selectedCell] stringValue]];
				break;
			case 1:
				[currentComplexObject setFirstName:
						(char *)[[controlWithText selectedCell] stringValue]];
				break;
			case 2:
				[currentComplexObject setMiddleInit:
						(char *)[[controlWithText selectedCell] stringValue]];
				break;
		}
	}
	else
	{	
		[currentComplexObject setNote:(char *)[controlWithText stringValue]];
	}
	return self;
}	

@end
