/* **********************************************************************
 * *                                                                    *
 * *    Set widget's title and initial text                             *
 * *                                                                    *
 * **********************************************************************
 * File: iris/xsig/xssettitle.c
 *
 *                        COPYRIGHT (c) 2000  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 <Mrm/MrmAppl.h>

#include "sigtypes.h"
#include "headers.h"
#include "misc_lib.h"
#include "xsig_lib.h"

void XsSetTitle( Widget wgt_a, const char* site_a, const char* smenu_a, const char* sfile_a )
{
  char     stxt[128];
  XmString xsitem;

  strcpy( stxt, site_a  );
  strcat( stxt, "  "    );
  strcat( stxt, smenu_a );
  strcat( stxt, sfile_a );

  xsitem = XmStringCreateSimple( (char *) stxt );
  XtVaSetValues( wgt_a, XmNdialogTitle, xsitem, NULL );
  XmStringFree( xsitem );
}


void XsSetPromptText( Widget wgt_a, const char* stxt_a )
{
  XmString xsitem;

  xsitem = XmStringCreateSimple( (char *)stxt_a );
  XtVaSetValues( wgt_a, XmNtextString, xsitem, NULL );
  XmStringFree( xsitem );
}

/* Set a widget's label */

void XsSetLabel( Widget wgt_a, const char* snam_a )
{
  XmString xstext;
  xstext = XmStringCreateSimple( (char *)snam_a );
  XtVaSetValues( wgt_a, XmNlabelString, xstext, NULL );
  XmStringFree( xstext );
}

/*---------------------------------------------------------------------------- 
  This routine initialize the buttons labels for a device pulldown menu. 
*/
void XsSetPdwBtnsLabs(Widget pdwwgt_a, char *btnlabs_a, SINT2 icnt_a )
{
  SINT2  ii, ilen;
  Widget wgt;
  char   sbtn[80], *stok;

  icnt_a = MIN( XSIG_PULLDOWN_MAX, icnt_a );
  strcpy( sbtn, "button_0" );
  ilen = strlen( sbtn );

  for ( ii = 0; ii < icnt_a; ii++ )
    {
      if ( ii < 10 )
	{
	  sbtn[ilen-1] = '0' + ii;
	  sbtn[ilen  ] = 0;
	}
      else
	{
	  sbtn[ilen-1] = '0' + ii/10;
	  sbtn[ilen  ] = '0' + ii%10;
	  sbtn[ilen+1] = 0;
	};

      wgt = XtNameToWidget( pdwwgt_a, sbtn );

      if ( ii  == 0 )
         stok = strtok( btnlabs_a, " " );
      else
         stok = strtok( NULL, " " );

      if ( wgt )
        XsSetLabel( wgt, stok );      
    };
}

/*---------------------------------------------------------------------------- 
  This routine initialize the buttons labels for a device pulldown menu. 
*/
void XsSetDevicePdwBtnsLabs(Widget pdwwgt_a,DEVICE_INFO devs_a[],SINT2 icnt_a )
{
  SINT2  ii, ilen, imaxstrlen = 0;
  Widget wgt;
  char   sbtn[80], *str;
  char   formatstr[50], dev_alias[30];

  icnt_a = MIN( XSIG_PULLDOWN_MAX, icnt_a );
  strcpy( sbtn, "button_0" );
  ilen = strlen( sbtn );
  

  for ( ii=0; ii<icnt_a; ii++ )
  {
      str = sdevice_name( devs_a[ii].itype, devs_a[ii].iunit );
      if (imaxstrlen < strlen(str)) 
         imaxstrlen = strlen(str);
  }

  sprintf(formatstr, "%c-%ds %cs", '%', imaxstrlen, '%');
  for ( ii=0; ii<icnt_a; ii++ )
    {
      if ( ii < 10 )
	{
	  sbtn[ilen-1] = '0' + ii;
	  sbtn[ilen  ] = 0;
	}
      else
	{
	  sbtn[ilen-1] = '0' + ii/10;
	  sbtn[ilen  ] = '0' + ii%10;
	  sbtn[ilen+1] = 0;
	};
      wgt = XtNameToWidget( pdwwgt_a, sbtn );
      str = sdevice_name( devs_a[ii].itype, devs_a[ii].iunit );
      /* the formatstr will look something like this
      ** %-9s %s */
      sprintf(dev_alias, formatstr, str, devs_a[ii].salias);
      if ( wgt )
        XsSetLabel( wgt, dev_alias );      
    };
}

/*-----------------------------------------------------------------------------
 Set a text widget's editability
*/
void XsSetEditable(Widget wgt1_a)
{
  XtVaSetValues( wgt1_a, 
		XmNeditable, True, 
		XmNcursorPositionVisible, True,
		XmNshadowThickness, 2, 
		NULL);       
}
void XsSetNonEditable(Widget wgt1_a )
{
  XtVaSetValues( wgt1_a, 
		XmNeditable, False, 
		XmNcursorPositionVisible, False,
		XmNshadowThickness, 1, 
		NULL);       
}


/*-----------------------------------------------------------------------------
  Set option widget's history, skip if no change.
*/
void XsSetOption( Widget optwgt_a, Widget hotwgt_a )
{
  Widget hotwgt;
  XtVaGetValues( optwgt_a, XmNmenuHistory, &hotwgt, NULL );
  if ( hotwgt != hotwgt_a )
    XtVaSetValues( optwgt_a, XmNmenuHistory, hotwgt_a, NULL );    
}
