/* Motif WidgetSet category for 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 X/Motif
 *
 * xmPopUpList.m,v 1.9 1995/04/22 23:12:44 pfkeb Exp
 */
 
#include "PopUpList.h"
#include "xtResponder.h"

#include "Application.h"
#include "Menu.h"
#include <objc/List.h>
#include "MenuCell.h"
#include "Matrix.h"
#include "Motif.h"
#include <Xm/RowColumn.h> /* for XmCreateOptionMenu */
#include <Xm/PushB.h>
#include <Xm/CascadeB.h>
#include <string.h>
#include <stdlib.h>

char *PopUpListInstanceName(void)
{
    return "PopUpList";
}

@implementation PopUpList(WidgetSet)

- _setFrame:(const NXRect *)aFrame inView:(View *)aView
{
    List      *cellList = [matrix cellList];
    MenuCell  *cell;
    NXSize    size;
    int               i;

    frame = *aFrame;
    contentView = aView;      // the controlView;
    if ( aView ) {
	NXRect rect;
	int    y;
	[contentView getFrame:&rect];
	y = rect.size.height - NX_HEIGHT(aFrame) - NX_Y(aFrame);
	[self _addArg:XmNx      :NX_X(aFrame)];
	[self _addArg:XmNy      :y];
	[self _addArg:XmNwidth  :NX_WIDTH(aFrame)];
	[self _addArg:XmNheight :NX_HEIGHT(aFrame)];
    } else {
	fprintf(stderr, "PopUpList error: no View to calculate frame\n");
    }
    size = aFrame->size;
    size.width  -= 28;        // cells are smaller than popup frame
    size.height -= 2;
    _menu_type = 0;
    i = [cellList count];
    while (i-- ) {
	cell = [cellList objectAt:i];
	[cell _setSize:&size];
    }
    cell = [matrix prototype];
    [cell _setFrame:(NXRect *)0 inView:(Control *)contentView];
    [matrix setCellSize:&size];
    return self;
}
- _realizeNewItem
{
    if ( widgetid ) {
        XtUnmanageChild((Widget) widgetid);
	XtDestroyWidget((Widget) widgetid);
    }
    [self _setFrame:&frame inView:contentView];
    [self _managedBy:contentView wid:_parentwidget];
    
    return self;
}

- _managedBy:parent wid:(void *)widget
{
    List	*cellList = [matrix cellList];
    Widget	menu_pane;
    int		i, num;
    XmString	xs;
    char	*fontName = "-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*";
    XFontStruct	*font;
    XmFontList	fontList;
    Display	*disp = [NXApp _X_display];
    Pixel	black;

    num = [cellList count];
    if ( !num ) {
        widgetid = NULL;
	return self;
    }
    if (!widget) {
    	return self;
    }

    xs = XmStringCreateSimple("");
    font = XLoadQueryFont( disp, fontName );
    fontList = XmFontListCreate( font, XmSTRING_DEFAULT_CHARSET );
    black = XBlackPixel(disp, XDefaultScreen(disp));

    [self _addArg:XmNfontList :(XtArgVal)fontList];
    [self _addArg:XmNforeground :black];
    [self _addArg:XmNmarginWidth :0];
    [self _addArg:XmNmarginHeight :0];
    
    if (0 == _menu_type)
    {
	menu_pane = XmCreatePulldownMenu(widget, "menu_pane", NULL, 0);
	for (i = 0; i < num; i++)
		[[cellList objectAt:i] _managedBy:self wid:menu_pane];
    
	// set label to empty string!
	[self _addArg:XmNlabelString :(XtArgVal)xs];
	[self _addArg:XmNsubMenuId :(XtArgVal)menu_pane];

	widgetid = XmCreateOptionMenu(widget, instancename,
					    arglist, numargs);
    }
    else
    {
	widgetid = XmCreatePulldownMenu(widget, instancename,
					    arglist, numargs);
	for (i = 0; i < num; i++)
		[[cellList objectAt:i] _managedBy:self wid:widgetid];
    }

    _parentwidget = widget;
    XtManageChild(widgetid);
    return self;
}

@end
