// ListScore.m
// Project: Cribbage
// Stephan Wacker
// 93-10-15


#import "ListScore.h"


@implementation ListScore


- add: (int) value at: (int) tag
{
    id		field = [values findCellWithTag: tag];
    int		newValue = value + [field intValue];
    
    if( newValue == 0 ) {
	[field setStringValue: ZERO_STRING];
    } else {
	[field setIntValue: newValue];
    }
    
    return [self addTotal: value];
}


- set: (int) value at: (int) tag
{
    id		field = [values findCellWithTag: tag];
    
    return [self add: value - [field intValue] at: tag];
}


- fifteen: sender
{
    return [self add: 2 at: [sender selectedTag]];
}

- nobs: sender
{
    return [self add: 1 at: [sender selectedTag]];
}

- pairs: sender		// Matrix of a PopupList
{
    return [self set: [sender selectedTag] at: [pairsButton tag]];
}

- runs: sender		// Matrix of a PopupList
{
    return [self set: [sender selectedTag] at: [runsButton tag]];
}

- flush: sender		// Matrix of a PopupList
{
    return [self set: [sender selectedTag] at: [flushButton tag]];
}


- clearPopupOf: aButton
{
    id		matrix = [[aButton target] itemList];
    
    [matrix selectCellWithTag: 0];
    [aButton setTitle: [[matrix selectedCell] title]];
    
    return self;
}


- clear
{
    [self clearPopupOf: pairsButton];
    [self clearPopupOf: runsButton];
    [self clearPopupOf: flushButton];
    
    return [super clear];
}


- awakeFromNib
{
    [super awakeFromNib];
    
    // Install self as target of the popup lists.  The only way to do this
    // in the InterfaceBuilder is to connect every single list item; here
    // we connect the matrices instead.
    [[[[pairsButton target] itemList] setTarget: self]
				      setAction: @selector(pairs:)];
    [[[[runsButton  target] itemList] setTarget: self]
				      setAction: @selector(runs:)];
    [[[[flushButton target] itemList] setTarget: self]
				      setAction: @selector(flush:)];
    
    return self;
}


@end	// ListScore
