/***************************************************************************
 XsFOLB Widget, Form-Option-List-Button 

       A XsFOLB widget is created by calling:

 XsCreateFOLB( Widget wgt_a, char* snam_a, int ibcnt_a, void* cbf() );

 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.

 11/20/92 tth  Original

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

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

/* Local Callbacks */

void xsfolb_opt_cbf( Widget, XsFOLB*, XmAnyCallbackStruct* cbd_a );
void xsfolb_lst_cbf( Widget, XsFOLB*, XmAnyCallbackStruct* cbd_a );
void xsfolb_ebt_cbf( Widget, XsFOLB*, XmAnyCallbackStruct* cbd_a );

XsFOLB* XsCreateFOLB(Widget pwgt_a, const char* snam_a,int ibcnt_a,void(*cbf_a)())
{
  Arg       args[2];
  XsFOLB*   xwgt;
  char*     sptr;
  SINT2     ii;
  XmString label;

  xwgt = XtNew( XsFOLB );
  xwgt->cbf_a = cbf_a;
  xwgt->frm   = XmCreateForm( pwgt_a, pstrcat(snam_a,"frm"), NULL, 0 );

  /* Option and its pulldown */
  xwgt->pdw = XmCreatePulldownMenu(xwgt->frm,pstrcat(snam_a,"pdw"),NULL,0);
  sptr = pstrcat( snam_a, "btn" );
  for ( ii=0; ii<MIN(ibcnt_a,25); ii++ )
  {
   xwgt->btns[ii] = XmCreatePushButton(xwgt->pdw,pstridx(sptr,ii),NULL,0);
   XtAddCallback(xwgt->btns[ii],XmNactivateCallback,
                 (XtCallbackProc) xsfolb_opt_cbf,xwgt);
   XtManageChild(xwgt->btns[ii] );
  };

  ii = 0;
  label = XmStringCreateSimple((char *)" ");
  XtSetArg( args[ii], XmNlabelString, label); ii++;
  XtSetArg( args[ii], XmNsubMenuId, xwgt->pdw); ii++;
  xwgt->opt = XmCreateOptionMenu(xwgt->frm,pstrcat(snam_a,"opt"), args, ii);

  /* Scroll list */
  xwgt->lst = XmCreateScrolledList(xwgt->frm,pstrcat(snam_a,"lst"),NULL,0);
  XtAddCallback(xwgt->lst,XmNsingleSelectionCallback,
                (XtCallbackProc) xsfolb_lst_cbf,xwgt);
  XtVaSetValues( xwgt->lst,   
		XmNselectionPolicy, XmSINGLE_SELECT,
		XmNscrollBarDisplayPolicy, XmSTATIC,
		NULL );

  /* Exit button */
  xwgt->ebt = XmCreatePushButton(xwgt->frm,pstrcat(snam_a,"ebt"),NULL,0);
  XtVaSetValues( xwgt->ebt, XmNalignment, XmALIGNMENT_CENTER, NULL );
  XtAddCallback( xwgt->ebt, XmNactivateCallback, 
                (XtCallbackProc) xsfolb_ebt_cbf, xwgt );

  XsAttach1FFFX( xwgt->opt );
  XsOffsetsLXRB( xwgt->opt, 5, 5, 0 );
  XsAttach1FTFB( XtParent(xwgt->lst), xwgt->opt, xwgt->ebt ); 
  XsOffsetsLTRB( XtParent(xwgt->lst), 5, 5, 5, 5);
  XsAttach1FXFF( xwgt->ebt ); 
  XsOffsetsLXRB( xwgt->ebt, 5, 5, 5 );

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

  XmStringFree(label);
  return xwgt;
}

void xsfolb_opt_cbf(Widget wgt_a,XsFOLB* xwgt_a,XmAnyCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt_a, (char *)"opt", cbd_a);
}
void xsfolb_lst_cbf(Widget wgt_a,XsFOLB* xwgt_a,XmAnyCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt_a, (char *)"lst", cbd_a);
  (xwgt_a->cbf_a)( wgt_a, (char *)"end", cbd_a);
  XtDestroyWidget( xwgt_a->frm );
  XtFree((char *) xwgt_a );
}
void xsfolb_ebt_cbf(Widget wgt_a,XsFOLB* xwgt_a,XmAnyCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt_a, (char *)"end", cbd_a);
  XtDestroyWidget( xwgt_a->frm );
  XtFree((char *) xwgt_a );
}


/*-----------------------------------------------------------------------------
  Dialog version
*/
void xsdfolb_opt_cbf( Widget, XsFOLB*, XmAnyCallbackStruct* cbd_a );
void xsdfolb_lst_cbf( Widget, XsFOLB*, XmAnyCallbackStruct* cbd_a );
void xsdfolb_ebt_cbf( Widget, XsFOLB*, XmAnyCallbackStruct* cbd_a );

XsFOLB* XsCreateDFOLB(Widget pwgt_a, const char* snam_a,int ibcnt_a,void(*cbf_a)())
{
  Arg       args[2];
  XsFOLB*   xwgt;
  char*     sptr;
  SINT2     ii;
  XmString label;

  xwgt = XtNew( XsFOLB );
  xwgt->cbf_a = cbf_a;
  xwgt->frm   = XmCreateFormDialog( pwgt_a, pstrcat(snam_a,"frm"), NULL, 0 );

  /* Option and its pulldown */
  xwgt->pdw = XmCreatePulldownMenu(xwgt->frm,pstrcat(snam_a,"pdw"),NULL,0);
  sptr = pstrcat( snam_a, "btn" );
  for ( ii=0; ii<MIN(ibcnt_a,25); ii++ )
  {
   xwgt->btns[ii] = XmCreatePushButton(xwgt->pdw,pstridx(sptr,ii),NULL,0);
   XtAddCallback(xwgt->btns[ii],XmNactivateCallback,
                 (XtCallbackProc) xsdfolb_opt_cbf,xwgt);
   XtManageChild(xwgt->btns[ii] );
  };

  ii = 0;
  label = XmStringCreateSimple((char *)" ");
  XtSetArg( args[ii], XmNlabelString, label); ii++;
  XtSetArg( args[ii], XmNsubMenuId, xwgt->pdw  ); ii++;
  xwgt->opt = XmCreateOptionMenu(xwgt->frm,pstrcat(snam_a,"opt"), args, ii);

  /* Scroll list */
  xwgt->lst = XmCreateScrolledList(xwgt->frm,pstrcat(snam_a,"lst"),NULL,0);
  XtAddCallback(xwgt->lst,XmNsingleSelectionCallback,
                (XtCallbackProc) xsdfolb_lst_cbf,xwgt);
  XtVaSetValues( xwgt->lst,   
		XmNselectionPolicy, XmSINGLE_SELECT,
		XmNscrollBarDisplayPolicy, XmSTATIC,
		NULL );

  /* Exit button */
  xwgt->ebt = XmCreatePushButton(xwgt->frm,pstrcat(snam_a,"ebt"),NULL,0);
  XtVaSetValues( xwgt->ebt, XmNalignment, XmALIGNMENT_CENTER, NULL );
  XtAddCallback( xwgt->ebt, XmNactivateCallback, 
                 (XtCallbackProc) xsdfolb_ebt_cbf, xwgt );

  XsAttach1FFFX( xwgt->opt );
  XsOffsetsLXRB( xwgt->opt, 5, 5, 0 );
  XsAttach1FTFB( XtParent(xwgt->lst), xwgt->opt, xwgt->ebt ); 
  XsOffsetsLTRB( XtParent(xwgt->lst), 5, 5, 5, 5);
  XsAttach1FXFF( xwgt->ebt ); 
  XsOffsetsLXRB( xwgt->ebt, 5, 5, 5 );

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


  XmStringFree (label);
  return xwgt;
}

void xsdfolb_opt_cbf(Widget wgt_a,XsFOLB* xwgt_a,XmAnyCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt_a, (char *)"opt", cbd_a);
}
void xsdfolb_lst_cbf(Widget wgt_a,XsFOLB* xwgt_a,XmAnyCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt_a, (char *)"lst", cbd_a);
  (xwgt_a->cbf_a)( wgt_a, (char *)"end", cbd_a);
}
void xsdfolb_ebt_cbf(Widget wgt_a,XsFOLB* xwgt_a,XmAnyCallbackStruct* cbd_a)
{
  (xwgt_a->cbf_a)( wgt_a, (char *)"end", cbd_a);
}

