/* Implementation of PopUpList class
 *
 * Copyright (C)  1993, 1994, 1995  The Board of Trustees of  
 * The Leland Stanford Junior University.  All Rights Reserved.
 *
 * Authors: Scott Francis, Paul Kunz, and Libing Wang (SLAC)
 *          Mike Kienenberger (Alaska)
 *
 * This file is part of an Objective-C class library for a window system
 *
 * PopUpList.m,v 1.42 1995/04/18 01:43:55 pfkeb Exp
 */
 
#include "PopUpList.h"

#include "Menu.h"
#include <objc/List.h>
#include "MenuCell.h"
#include "Matrix.h"
#include <string.h>
#include <stdlib.h>

extern char *PopUpListInstanceName(void);

@interface PopUpList(WidgetSet)
- _realizeNewItem;
- _managedBy:parent wid:(void*)widget;
@end

@implementation PopUpList:Menu

- init
{
    [super init];
    instancename = PopUpListInstanceName();
    _menu_type = 0;

    return self;
}

- setAction:(SEL)aSelector
{    
    [matrix setAction:aSelector];
    return self;
}

- (SEL)action
{
    return [matrix action];
}

- setTarget:anObject
{
    [matrix setTarget:anObject];
    return self;
}

- target
{
    return [matrix target];
}

- selectCell:aCell;
{
    [matrix selectCell:aCell];
    return self;
}

- (const char *)selectedItem
{
    MenuCell	*cell = [matrix selectedCell];
    return [cell stringValue];
}

- addItem:(const char *)aString andRealize:(BOOL)flag
{
    MenuCell	*cell;
    int		i;

    i = [self indexOfItem:aString];
    if ( i >= 0 ) {
        return [[matrix cellList] objectAt:i];
    }
    [matrix addRow];
    cell = [[matrix cellList] lastObject];
    [cell setStringValue:aString];
    [cell _setControlView:matrix];
    if (flag)
     	[self _realizeNewItem];
    return cell;
}

- addItem:(const char *)aTitle
{    
    return [self addItem:aTitle andRealize:YES];
}

- insertItem:(const char *)aString at:(unsigned int)index
{
    List	*cellList = [matrix cellList];
    MenuCell	*aCell;
    int		i;
    
    i = [self indexOfItem:aString];
    if ( i >= 0 ) {
        aCell = [cellList removeObjectAt:i];
	[cellList insertObject:aCell at:index];
    } else {
        [matrix insertRowAt:index];
	aCell = [[matrix cellList] objectAt:index];
	[aCell setStringValue:aString];
    }
    [self _realizeNewItem];
    return aCell;
}

- removeItemAt:(unsigned int)index
{
    List	*cellList = [matrix cellList];
    id item;

    item = [cellList objectAt:index];
    [matrix removeRowAt:index andFree:YES];
    if (widgetid)
     	[self _realizeNewItem];

    return item;
}

- (int)indexOfItem:(const char *)aString
{
    List	*cellList = [matrix cellList];
    MenuCell	*aCell;
    int		i;
    
    i = [cellList count];
    while( i-- ) {
        aCell = [cellList objectAt:i];
	if ( !strcmp([aCell title], aString) ) {
	    return i;
	}
    }
    return (-1);
}

- read:(TypedStream *)ts
{
    [super read:ts];
#ifdef DEBUG    
    fprintf(stderr, "reading PopUpList\n");
#endif
    return self;
}

- awake
{
  /* PopUpList will be initialized by the Button control */
// [super awake];  
  return self;
}

@end
