/* **********************************************************************
 * *                                                                    *
 * *   Various Widget array, combinational Widget are created here.     *
 * *                                                                    *
 * **********************************************************************
 * File: iris/xsig/xscreatewgt.c
 *
 *            COPYRIGHT (c) 1992, 1996, 1999, 2000, 2005 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.
 */

#include <stdio.h>

#include <Xm/CascadeB.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Text.h>
#include <Xm/TextF.h>

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

/*-----------------------------------------------------------------------------
  Create a managed pushbutton array 
*/
void XsCreateMPBTNS( Widget parent_a,
		     Widget array_a[], char* sname_a, int icnt_a,
		     XtCallbackProc btn_cbf_a )
{
  char* sptr;
  int  ii;

  sptr = pstrcat( sname_a, "btn" );
  for ( ii=0; ii<icnt_a; ii++ )
  {
    array_a[ii] = XmCreatePushButton( parent_a, pstridx(sptr,ii), NULL, 0 );
    if ( btn_cbf_a != NULL )
      XtAddCallback( array_a[ii], XmNactivateCallback, btn_cbf_a,
                     (XtPointer) ii );
    XtManageChild( array_a[ii] );
  };
}


/* Create a real operational, verse logical or sematical, option menu. An
 X option menu consists two rowcolumn widgets and a set of button widgets.
 The first rowcolumn widget is configurated as an option widget, the 2nd
 rowcolumn widget is set as an pulldown widget. The button widget set is
 contained in the pulldown widget.

   Normally a user will only see the option widget which consists text and
 button. When the user pushes the button, he then see the pulldown widget.
 In the pulldown, there resides a button set.

   To create an operational option menu.  First create the pulldown to hold
 the button set. Second create the button set. And finally create the option
 widget and link the pulldown to it.
*/
  
Widget XsCreateOptMnu(Widget  parent_a,  const char* soptnam_a,
		      Widget* btnwgts_a, int ibtncnt_a, XtCallbackProc btn_cbf )
{
  Arg    args[2];
  char   soptnam[80], spdwnam[80], sbtnnam[80], scasnam[80];
  Widget optwgt, pdwwgt;
  int    ii;
  XmString label;

  strcpy( soptnam, soptnam_a ); strcat( soptnam, "opt" );
  strcpy( spdwnam, soptnam_a ); strcat( spdwnam, "pdw" );
  strcpy( sbtnnam, soptnam_a );
  strcpy( scasnam, soptnam_a ); strcat( scasnam, "opt_cascadeBtn" );

  pdwwgt = XmCreatePulldownMenu( parent_a, spdwnam, NULL, 0 );

  XsCreateMPBTNS( pdwwgt, btnwgts_a, sbtnnam, ibtncnt_a, btn_cbf );

  ii = 0;
  label = XmStringCreateSimple((char *)"\0");
  XtSetArg( args[ii], XmNlabelString, label); ii++;
  XtSetArg( args[ii], XmNsubMenuId, pdwwgt ); ii++;
  optwgt = XmCreateOptionMenu( parent_a, soptnam, args, ii );

  XmStringFree(label);
  return optwgt;
}


/* ----------------------------------------------------------------------------
Create a output only Text widget 
*/
Widget XsCreateOText1( Widget ipid_a, const char* snam_a, void* args_a, int icnt_a )
{
  int ac;
  Arg al[10];
  Widget iwid;
  
  ac = 0;
  XtSetArg(al[ac], XmNeditable, FALSE); ac++;
  XtSetArg(al[ac], XmNcursorPositionVisible, FALSE); ac++;
  XtSetArg(al[ac], XmNshadowThickness, 1); ac++;
  iwid = XmCreateTextField( ipid_a, (char *)snam_a, al, ac );

  return iwid;
}
Widget XsCreateOText ( Widget ipid_a, const char* snam_a, void* args_a, int icnt_a )
{
  int ac;
  Arg al[10];
  Widget iwid;

  ac = 0;
  XtSetArg(al[ac], XmNeditable, FALSE); ac++;
  XtSetArg(al[ac], XmNcursorPositionVisible, FALSE); ac++;
  XtSetArg(al[ac], XmNshadowThickness, 1); ac++;
  iwid = XmCreateText( ipid_a, (char *)snam_a, al, ac );
  return iwid;
}


/* ====================================================================
 * History:
 * 13 Apr 1993 ads Generalized button count argument.
 */
Widget XsCreateBtnsPdw(Widget ipid_a,const char* snam_a,int idx_a, int icnt_a,
		       void(*cbf_a)( Widget, int, XmPushButtonCallbackStruct *) )
{
  Widget wid ;
  Arg arglist[3];
  XtSetArg( arglist[0], XmNbuttonCount   , icnt_a  );
  XtSetArg( arglist[1], XmNsimpleCallback, cbf_a   );
  XtSetArg( arglist[2], XmNpostFromButton, idx_a   );
  wid = XmCreateSimplePulldownMenu( ipid_a, (char *)snam_a, arglist, 3 ) ;
  return wid;
}

/* ====================================================================
 * History:
 * 4 Dec 1992 jmh Generalized button count argument.
 */
Widget XsCreateBtnsPop( Widget ipid_a, const char* snam_a,int icnt_a,
			void(*cbf_a)( Widget, int, XmPushButtonCallbackStruct *) )
{
  Widget wid;
  Arg arglist[2];
  XtSetArg( arglist[0], XmNbuttonCount   , icnt_a );
  XtSetArg( arglist[1], XmNsimpleCallback, cbf_a );
  wid = XmCreateSimplePopupMenu( ipid_a, (char *)snam_a, arglist, 2 );
  return wid;
}

/*---------------------------------------------------------------------------- 
  This routine breaks down a string as tokens and insert the tokens into
   the list.  The number of the tokens is also return.
*/

SINT2 XsStrToList( Widget lstwgt_a, char* str_a )
{
  SINT2    icnt,  iloop;
  char     *sptr, *stok;
  XmString xsitem;

  if ( *str_a == 0 )
     return 0;

  sptr  = str_a;
  iloop = TRUE; 
  icnt  = 0;
  while ( iloop )
  {
    if ( icnt == 0 )
       stok = strtok( sptr, " " );
    else
       stok = strtok( NULL, " " );
    
    if ( stok )
    {
      xsitem = XmStringCreateSimple( stok );
      /* Changed Position to 0 in XmListAddItem. This makes
      **  the new item the last item */ 
      XmListAddItem( lstwgt_a, xsitem, 0 ); 
      XmStringFree( xsitem );
      icnt++;
    }
    else
      iloop = FALSE;
  };

  return icnt;
}


Widget XsCreateBtnBar( Widget pid_a, char* sname_a )
{
  static Widget  wgtbar, wgtbtn;

  wgtbar = XmCreateMenuBar      ( pid_a,  pstrcat(sname_a,"bar"), NULL, 0 );
  wgtbtn = XmCreateCascadeButton( wgtbar, pstrcat(sname_a,"btn"), NULL, 0 );

  XtVaSetValues( wgtbar, XmNresizeHeight, False,
                         XmNresizeWidth,  False,
		         XmNmarginHeight, 0,
                         XmNmarginWidth,  0,
		         XmNheight,       30,
		         XmNwidth,        20, NULL );

  XtVaSetValues( wgtbtn, XmNheight,       24,
		         XmNwidth,        14, NULL );

  return wgtbar;
}

Widget XsCreateBtn1Pdw(Widget bar_a, char* snam_a, int icnt_a, void(*cbf_a)())
{
  char    spdw[XSIG_PULLDOWN_MAX];
  static Widget  wgtpdw;

  strcpy( spdw, snam_a ); 
  strcat( spdw, "pdw" );
  wgtpdw = XsCreateBtnsPdw( bar_a, spdw, 0, icnt_a, cbf_a);

  return wgtpdw;
}

/* ----------------------------------------------------------------------------
  Create a plane push button with activate callback.
*/
Widget XsCreatePBTN(Widget pwgt_a, const char* snam_a, void(*cbf_a)())
{
  Widget wgt;
  wgt = XmCreatePushButton( pwgt_a, (char *)snam_a, NULL, 0 );
  if ( cbf_a != NULL )
    XtAddCallback( wgt, XmNactivateCallback, cbf_a, NULL );
  return wgt;
}

/* ----------------------------------------------------------------------------
  Create a Label, Bared-Button, TextField Combo.
*/
void XsCreatePDW_TXT( Widget   pwgt_a,   
		      XsPDW_TXT* wgtset_a, const char* sname_a, int icnt_a,
		      XtCallbackProc btn_cbf_a, XtCallbackProc txt_cbf_a ) 
{
  int      ac;
  Arg      al[10];
  int      ii;
  char     *sptr;
  Widget   wgtbtns[XSIG_PULLDOWN_MAX];

  wgtset_a->lab = XmCreateLabel  ( pwgt_a, pstrcat(sname_a,"lab"), NULL, 0 );
  wgtset_a->bar = XmCreateMenuBar( pwgt_a, pstrcat(sname_a,"bar"), NULL, 0 );
  XtVaSetValues( wgtset_a->bar, 
		 XmNresizeHeight, False, XmNresizeWidth,  False,
		 XmNmarginHeight, 0,     XmNmarginWidth,  0,
		 XmNheight,       26,    XmNwidth,        20, NULL );

  wgtset_a->pdw = XmCreatePulldownMenu(wgtset_a->bar,
				       pstrcat(sname_a,"pdw"),NULL,0);
  sptr = pstrcat( sname_a, "btn" );
  for ( ii=0; ii<icnt_a; ii++ )
  {
    wgtbtns[ii] = XmCreatePushButton(wgtset_a->pdw,pstridx(sptr,ii),NULL,0);
    if ( btn_cbf_a != NULL )
      XtAddCallback(wgtbtns[ii],XmNactivateCallback, btn_cbf_a, 
                    (XtPointer) ii );
    XtManageChild(wgtbtns[ii] );
  };

  wgtset_a->cas = XmCreateCascadeButton( wgtset_a->bar,
					 pstrcat(sname_a,"cas"), NULL, 0 );

  XtVaSetValues( wgtset_a->cas, 
		 XmNlabelType,     XmPIXMAP,
		 XmNrecomputeSize, False,
		 XmNheight,   24,  XmNwidth,  16,
		 XmNsubMenuId, wgtset_a->pdw, NULL );

  if ( txt_cbf_a != NULL )
    {
      wgtset_a->txt = XmCreateTextField( pwgt_a, pstrcat(sname_a,"txt"),NULL,0);
      XtAddCallback( wgtset_a->txt, XmNactivateCallback, txt_cbf_a, NULL );
      XtAddCallback( wgtset_a->txt, XmNlosingFocusCallback,txt_cbf_a,NULL);
    }
  else
    {
  
      ac = 0;
      XtSetArg(al[ac], XmNeditable, FALSE); ac++;
      XtSetArg(al[ac], XmNcursorPositionVisible, FALSE); ac++;
      XtSetArg(al[ac], XmNshadowThickness, 1); ac++;
      wgtset_a->txt = XmCreateTextField( pwgt_a, pstrcat(sname_a,"txt"),al,ac);
/*
      XtVaSetValues( wgtset_a->txt, XmNeditable, False,
		                    XmNcursorPositionVisible, False,
                                    XmNshadowThickness,       1,     NULL );
*/
    };

  XtManageChild( wgtset_a->cas );

  /* Never manage a popup pdw, a popup is managed by window manager 
  XtManageChild( wgtset_a->pdw ); 
  */
}
/* Same as above, except size*/
void XsCreateSize_PDW_TXT( Widget    pwgt_a,   
			  XsPDW_TXT* wgtset_a, const char* sname_a, 
			  int icnt_a, int ibarw_a, int ibarh_a, 
			  XtCallbackProc btn_cbf_a ) 
{
  int      ac;
  Arg      al[10];
  int      ii;
  char     *sptr;
  Widget   wgtbtns[XSIG_PULLDOWN_MAX];

  wgtset_a->lab = XmCreateLabel  ( pwgt_a, pstrcat(sname_a,"lab"), NULL, 0 );
  wgtset_a->bar = XmCreateMenuBar( pwgt_a, pstrcat(sname_a,"bar"), NULL, 0 );
  XtVaSetValues( wgtset_a->bar, 
		 XmNresizeHeight, False, XmNresizeWidth,  False,
		 XmNmarginHeight,     0, XmNmarginWidth,      0,
		 XmNheight,     ibarh_a, XmNwidth,  ibarw_a, NULL );

  wgtset_a->pdw = 
    XmCreatePulldownMenu(wgtset_a->bar, pstrcat(sname_a,"pdw"),NULL,0);
  sptr = pstrcat( sname_a, "btn" );
  for ( ii=0; ii<icnt_a; ii++ )
    {
      wgtbtns[ii] = XmCreatePushButton(wgtset_a->pdw,pstridx(sptr,ii),NULL,0);
      if ( btn_cbf_a != NULL )
	XtAddCallback(wgtbtns[ii],XmNactivateCallback, btn_cbf_a, (XtPointer)ii );
      XtManageChild(wgtbtns[ii] );
    };

  wgtset_a->cas = 
    XmCreateCascadeButton( wgtset_a->bar, pstrcat(sname_a,"cas"), NULL, 0 );

  XtVaSetValues( wgtset_a->cas, 
		 XmNlabelType,     XmPIXMAP,
		 XmNrecomputeSize, False,
		 XmNheight,        ibarh_a - 6,  
		 XmNwidth,         ibarw_a - 4,
		 XmNsubMenuId,     wgtset_a->pdw, NULL );

  
  ac = 0;
  XtSetArg(al[ac], XmNeditable, FALSE); ac++;
  XtSetArg(al[ac], XmNcursorPositionVisible, FALSE); ac++;
  XtSetArg(al[ac], XmNshadowThickness, 1); ac++;
  wgtset_a->txt = XmCreateTextField( pwgt_a, pstrcat(sname_a,"txt"),al,ac);
  /*
    XtVaSetValues( wgtset_a->txt, XmNeditable, False,
    XmNcursorPositionVisible, False,
    XmNshadowThickness,       1,     NULL );
  */

  XtManageChild( wgtset_a->cas );

  /* Never manage a popup pdw, a popup is managed by window manager 
  XtManageChild( wgtset_a->pdw ); 
  */
}


/* ----------------------------------------------------------------------------
  Create a Label, Plain Button, TextField Combo.
*/
void XsCreateBTN_TXT( Widget         pwgt_a,   
		     XsBTN_TXT* wgtset_a, const char* sname_a, 
		     XtCallbackProc btn_cbf_a, XtCallbackProc txt_cbf_a ) 
{
  wgtset_a->lab = XmCreateLabel     ( pwgt_a, pstrcat(sname_a,"lab"), NULL,0 );
  wgtset_a->btn = XmCreatePushButton( pwgt_a, pstrcat(sname_a,"btn"), NULL,0 );
  wgtset_a->txt = XmCreateText      ( pwgt_a, pstrcat(sname_a,"txt"), NULL,0 );
  if ( btn_cbf_a != NULL )
    {
      XtAddCallback( wgtset_a->btn, XmNactivateCallback,   btn_cbf_a, NULL );
    };

  if ( txt_cbf_a != NULL )
    {
      XtAddCallback( wgtset_a->txt, XmNactivateCallback,   txt_cbf_a, NULL );
      XtAddCallback( wgtset_a->txt, XmNlosingFocusCallback,txt_cbf_a, NULL );
    };

}
void XsCreateBTN_OTXT( Widget pwgt_a,   
		       XsBTN_TXT* wgtset_a, const char* sname_a, XtCallbackProc btn_cbf_a ) 
{
  int ac;
  Arg al[10];

  wgtset_a->lab = XmCreateLabel     ( pwgt_a, pstrcat(sname_a,"lab"), NULL,0 );
  wgtset_a->btn = XmCreatePushButton( pwgt_a, pstrcat(sname_a,"btn"), NULL,0 );
  ac = 0;
  XtSetArg(al[ac], XmNeditable, FALSE); ac++;
  XtSetArg(al[ac], XmNcursorPositionVisible, FALSE); ac++;
  XtSetArg(al[ac], XmNshadowThickness, 1); ac++;
  wgtset_a->txt = XmCreateText      ( pwgt_a, pstrcat(sname_a,"txt"), al,ac );

  if ( btn_cbf_a != NULL )
    XtAddCallback( wgtset_a->btn, XmNactivateCallback,   btn_cbf_a, NULL );
}
