/* **************************************
 * *                                    *
 * *  XsFLB Widget, Form-List-Button    *
 * *                                    *
 * **************************************
 * File: iris/xsig/xsflbwgt.c
 *
 *                        COPYRIGHT (c) 1992, 1999  BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

/***************************************************************************

       A XsFLB widget is created by calling:

 The first argument is the parent widget.  The second argument is the name
 of the child widget to be created. The passed in callback function is
 registered in the widget's private memory. When one of the exiting events
 occurs, the callback is invoked.

*/
#include <stdio.h>
#include <Xm/PushB.h>
#include <Xm/Form.h>
#include <Xm/List.h>

#include "sigtypes.h"
#include "xsig_lib.h"

/* Local Callbacks */
static void xsflb_lst_cbf( Widget, XsFLB*, XmListCallbackStruct* cbd_a );
static void xsflb_lst_noexit_cbf( Widget, XsFLB*, XmListCallbackStruct* cbd_a );
static void xsflb_ebt_cbf( Widget, XsFLB*, XmListCallbackStruct* cbd_a );
static void xsflb_ebt_noexit_cbf( Widget, XsFLB*, XmListCallbackStruct* cbd_a );

XsFLB* XsCreateFLB
(Widget pwgt_a,
 const char* snam_a,
 void(*cbf_a)( Widget wgt, char *tag, XmListCallbackStruct *cbd ))
{
  XsFLB* xwgt;

  xwgt = XtNew( XsFLB );
  xwgt->cbf_a = cbf_a;
  xwgt->frm = XmCreateForm( pwgt_a, pstrcat(snam_a,"frm"),NULL,0);
  xwgt->lst = XmCreateScrolledList( xwgt->frm,pstrcat(snam_a,"lst"),NULL,0);
  XtAddCallback(xwgt->lst,XmNsingleSelectionCallback,
                (XtCallbackProc) xsflb_lst_cbf,xwgt);

  xwgt->ebt = XmCreatePushButton( xwgt->frm,pstrcat(snam_a,"ebt"), NULL,0);
  XtVaSetValues( xwgt->ebt, XmNalignment, XmALIGNMENT_CENTER, NULL );
  XtAddCallback( xwgt->ebt, XmNactivateCallback,
                (XtCallbackProc)  xsflb_ebt_cbf, xwgt );

  XsAttach1FFFB( XtParent(xwgt->lst), xwgt->ebt ); 
  XsOffsetsLTRB( XtParent(xwgt->lst), 5, 5, 5, 5);
  XsAttach1FXFF( xwgt->ebt ); 
  XsOffsetsLXRB( xwgt->ebt, 5, 5, 5 );

/* This was used up intill 12/4/94. but caused crashing on snow server
** The above is identical to NoExit routine. Make sure xwgt->frm stays
** commented out.
  XsAttach1FFFX(XtParent( xwgt->lst)); 
  XsOffsetsLTRB( XtParent(xwgt->lst), 5, 5, 5, 5);
  XsAttach1FTFF( xwgt->ebt, xwgt->lst ); 
  XsOffsetsLXRB( xwgt->ebt, 5, 5, 5 );
*/

  XtVaSetValues( xwgt->lst,   
		XmNselectionPolicy, XmSINGLE_SELECT,
		XmNscrollBarDisplayPolicy, XmSTATIC,
		NULL );

  XtManageChild( xwgt->lst );
  XtManageChild( xwgt->ebt );
/*
  XtManageChild( xwgt->frm );
*/

  return xwgt;
}

XsFLB* XsCreateFLBNoExit(Widget pwgt_a, const char* snam_a,void(*cbf_a)())
{
  XsFLB* xwgt;

  xwgt = XtNew( XsFLB );
  xwgt->cbf_a = cbf_a;
  xwgt->frm = XmCreateForm( pwgt_a, pstrcat(snam_a,"frm"),NULL,0);
  xwgt->lst = XmCreateScrolledList( xwgt->frm,pstrcat(snam_a,"lst"),NULL,0);
  XtAddCallback(xwgt->lst,XmNsingleSelectionCallback,
                (XtCallbackProc) xsflb_lst_noexit_cbf,xwgt);

  xwgt->ebt = XmCreatePushButton( xwgt->frm,pstrcat(snam_a,"ebt"), NULL,0);
  XtVaSetValues( xwgt->ebt, XmNalignment, XmALIGNMENT_CENTER, NULL );
  XtAddCallback( xwgt->ebt, XmNactivateCallback,
                (XtCallbackProc)  xsflb_ebt_cbf, xwgt );

  XsAttach1FFFB( XtParent(xwgt->lst), xwgt->ebt ); 
  XsOffsetsLTRB( XtParent(xwgt->lst), 5, 5, 5, 5);
  XsAttach1FXFF( xwgt->ebt ); 
  XsOffsetsLXRB( xwgt->ebt, 5, 5, 5 );

  XtVaSetValues( xwgt->lst,   
		XmNselectionPolicy, XmSINGLE_SELECT,
		XmNscrollBarDisplayPolicy, XmSTATIC,
		NULL );

  XtManageChild( xwgt->lst );
  XtManageChild( xwgt->ebt );

  return xwgt;
}

XsFLB* XsCreateFLBNoDestroy(Widget pwgt_a, const char* snam_a,void(*cbf_a)())
{
  XsFLB* xwgt;

  xwgt = XtNew( XsFLB );
  xwgt->cbf_a = cbf_a;
  xwgt->frm = XmCreateForm( pwgt_a, pstrcat(snam_a,"frm"),NULL,0);
  xwgt->lst = XmCreateScrolledList( xwgt->frm,pstrcat(snam_a,"lst"),NULL,0);
  XtAddCallback(xwgt->lst,XmNsingleSelectionCallback,
                (XtCallbackProc) xsflb_lst_noexit_cbf,xwgt);

  xwgt->ebt = XmCreatePushButton( xwgt->frm,pstrcat(snam_a,"ebt"), NULL,0);
  XtVaSetValues( xwgt->ebt, XmNalignment, XmALIGNMENT_CENTER, NULL );
  XtAddCallback( xwgt->ebt, XmNactivateCallback,
                (XtCallbackProc)  xsflb_ebt_noexit_cbf, xwgt );

  XsAttach1FFFB( XtParent(xwgt->lst), xwgt->ebt ); 
  XsOffsetsLTRB( XtParent(xwgt->lst), 5, 5, 5, 5);
  XsAttach1FXFF( xwgt->ebt ); 
  XsOffsetsLXRB( xwgt->ebt, 5, 5, 5 );

  XtVaSetValues( xwgt->lst,   
		XmNselectionPolicy, XmSINGLE_SELECT,
		XmNscrollBarDisplayPolicy, XmSTATIC,
		NULL );

  XtManageChild( xwgt->lst );
  XtManageChild( xwgt->ebt );

  return xwgt;
}


static void xsflb_lst_cbf(Widget wgt, XsFLB* xwgt_a, XmListCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt, (char *)"lst", cbd_a);
  (xwgt_a->cbf_a)( wgt, (char *)"end", cbd_a);
  XtDestroyWidget( xwgt_a->frm );
  XtFree((char *) xwgt_a );
}
static void xsflb_lst_noexit_cbf(Widget wgt, XsFLB* xwgt_a, XmListCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt, (char *)"lst", cbd_a);
/*  (xwgt_a->cbf_a)( wgt, "end", cbd_a);

  XtDestroyWidget( xwgt_a->frm );
  XtFree((char *) xwgt_a );
*/
}
static void xsflb_ebt_cbf(Widget wgt, XsFLB* xwgt_a, XmListCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt, (char *)"end", cbd_a);
  XtDestroyWidget( xwgt_a->frm );
  XtFree((char *) xwgt_a );
}

static void xsflb_ebt_noexit_cbf(Widget wgt, XsFLB* xwgt_a, XmListCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt, (char *)"end", cbd_a);
/*
  XtDestroyWidget( xwgt_a->frm );
  XtFree((char *) xwgt_a );
*/
}

/*-----------------------------------------------------------------------------
 Dialog Version
*/
XsFLB* XsCreateDFLB(Widget pwgt_a, const char* snam_a,void(*cbf_a)())
{
  XsFLB* xwgt;

  xwgt = XtNew( XsFLB );
  xwgt->frm = XmCreateFormDialog( pwgt_a, pstrcat(snam_a,"frm"),NULL,0);
  xwgt->lst = XmCreateScrolledList( xwgt->frm,pstrcat(snam_a,"lst"),NULL,0);
  XtAddCallback(xwgt->lst,XmNsingleSelectionCallback,
                (XtCallbackProc) xsflb_lst_cbf,xwgt);

  xwgt->ebt = XmCreatePushButton( xwgt->frm,pstrcat(snam_a,"ebt"), NULL,0);
  XtVaSetValues( xwgt->ebt, XmNalignment, XmALIGNMENT_CENTER, NULL );
  XtAddCallback( xwgt->ebt, XmNactivateCallback, 
                (XtCallbackProc) xsflb_ebt_cbf, xwgt );

  XsAttach1FFFB( XtParent(xwgt->lst), xwgt->ebt ); 
  XsOffsetsLTRB( XtParent(xwgt->lst), 5, 5, 5, 5);
  XsAttach1FXFF( xwgt->ebt ); 
  XsOffsetsLXRB( xwgt->ebt, 5, 5, 5 );

  XtVaSetValues( xwgt->lst,   
		XmNselectionPolicy, XmSINGLE_SELECT,
		XmNscrollBarDisplayPolicy, XmSTATIC,
		NULL );

  XtManageChild( xwgt->lst );
  XtManageChild( xwgt->ebt );
  xwgt->cbf_a = cbf_a;

  return xwgt;
}
