/******************************************************************************
*
*  NSSDC/CDF                                    CDFedit, auxiliary routines.
*
*  Version 1.4, 21-Dec-95, Hughes STX.
*
*  Modification history:
*
*   V1.0  19-Nov-93, J Love     Original version.
*   V1.0a  4-Feb-94, J Love     DEC Alpha/OpenVMS port.
*   V1.1  15-Dec-94, J Love     CDF V2.5.
*   V1.1a 23-Jan-95, J Love	IRIX 6.x (64-bit).
*   V1.1b 28-Feb-95, J Love	Pass `char' as `int'.
*   V1.2  21-Mar-95, J Love	POSIX.
*   V1.2a 18-Apr-95, J Love	More POSIX.
*   V1.3   6-Sep-95, J Love	CDFexport-related changes.  FSI key
*				definitions.
*   V1.4  21-Dec-95, J Love	Removed `doupdate' patch (OSF/1 V3.2).
*
******************************************************************************/

#include "cdfedit.h"

/******************************************************************************
* InitializeScreen.
******************************************************************************/

void InitializeScreen () {
  create_pasteboard ();
  set_cursor_mode (CURSORoff);
  return;
}

/******************************************************************************
* CleanupScreen.
******************************************************************************/

void CleanupScreen () {
  delete_pasteboard (ERASE);
  set_cursor_mode (CURSORon);
  return;
}

/******************************************************************************
* ReportStatus.
******************************************************************************/

void ReportStatus (status)
CDFstatus status;
{
  if ((StatusINFO(status) && report[INFOs]) ||
      (StatusWARN(status) && report[WARNs]) ||
      (StatusERROR(status) && report[ERRORs])) CDFstatusWindow (status);
  return;
}

/******************************************************************************
* CDFstatusWindow.
******************************************************************************/

void CDFstatusWindow (status)
CDFstatus status;
{
   static char text[CDF_STATUSTEXT_LEN+1];
   static char *eLines[] = { text };
   static int exitChars[] = { NUL };
   STATIC_ struct EditWindowStruct EW = {
     " Message from CDF library, enter any key to acknowledge. ", 21, 0,
     SCREEN_WIDTH, 0, NULL, 1, 1, eLines, 1, 0, NULL, FALSE, TRUE, exitChars,
     REFRESHkey_FSI, NUL, NUL, NUL, NUL, NUL, NUL
   };
   CDFlib (SELECT_, CDF_STATUS_, status,
	   GET_, STATUS_TEXT_, text,
	   NULL_);
   EditWindow (NEWew, &EW, (Logical) TRUE);
   EditWindow (BEEPew, &EW);
   EditWindow (READew, &EW);
   EditWindow (DELETEew, &EW);
   return;
}

/******************************************************************************
* InputErrorWindow.
******************************************************************************/

void InputErrorWindow (message, center)
char *message;
Logical center;
{
   static int exitChars[] = { NUL };
   STATIC_ struct EditWindowStruct EW = {
     " Input error, enter any key to acknowledge. ",
     0, 0, 0,
     0, NULL, 1, 1, NULL, 1, 0, NULL, FALSE, TRUE, exitChars,
     REFRESHkey_FSI, NUL, NUL, NUL, NUL, NUL, NUL
   };
   if (center) {
     EW.nColsTotal = MAXIMUM((size_t)strlen(EW.label)+4,
			     (size_t)strlen(message)+2);
     EW.ULrow = (SCREEN_HEIGHT - 3) / 2;
     EW.ULcol = (SCREEN_WIDTH - EW.nColsTotal) / 2;
   }
   else {
     EW.nColsTotal = SCREEN_WIDTH;
     EW.ULrow = SCREEN_HEIGHT - 3;
     EW.ULcol = 0;
   }
   EW.eLines = &message;
   EditWindow (NEWew, &EW, (Logical) TRUE);
   EditWindow (BEEPew, &EW);
   EditWindow (READew, &EW);
   EditWindow (DELETEew, &EW);
   return;
}

/******************************************************************************
* ConfirmWindow.
******************************************************************************/

Logical ConfirmWindow (rowN, question1, question2, yesInitial, helpId)
int rowN;
char *question1;
char *question2;        /* If NULL, a one line question. */
Logical yesInitial;     /* If TRUE, <YES> is initial current item. */
int helpId;
{
  static char label[] = " Confirmation ";
  static char *header[] = { NULL, NULL };
  AOSs1 (trailer, "Select: ________   Exit: ________  Help: ________")
  static char *items[] = {
    "            <Yes>                   <No>            "
  };
  static int iLineNs[] = { 0, 0 };
  static int iCols[] = { 12, 36 };
  static int iLens[] = { 5, 4 };
  static int exitChars[] = { SELECTkey_EDIT, EXITkey_FSI, HELPkey_FSI, NUL };
  STATIC_ struct ItemWindowStruct IWt = {
    0, 0, 0, label, 0, header, 2, items, 2, iLineNs, iCols, iLens, 1,
    1, trailer, exitChars, REFRESHkey_FSI, FALSE
  };
  Logical answer;
  EncodeKeyDefinitions (1, trailer, SELECTkey_EDIT, EXITkey_FSI,
			HELPkey_FSI);
  IWt.hLines[0] = question1;
  IWt.NhLines = 1;
  IWt.nColsTotal = MAXIMUM ((size_t)strlen(IWt.iLines[0]),
			    (size_t)strlen(IWt.hLines[0]));
  if (question2 != NULL) {
    header[1] = question2;
    IWt.NhLines = 2;
    IWt.nColsTotal = MAXIMUM ((size_t)IWt.nColsTotal,
			      (size_t)strlen(IWt.hLines[1]));
  }
  IWt.ULrow = rowN;
  IWt.ULcol = (SCREEN_WIDTH - IWt.nColsTotal) / 2;
  ItemWindow (NEWiw, &IWt, BOO(yesInitial,0,1));
  if (EnterIW(&IWt,helpId))
    switch (IWt.itemN) {
      case 0:
	answer = TRUE;
	break;
      case 1:
	answer = FALSE;
	break;
    }
  else
    answer = FALSE;
  ItemWindow (DELETEiw, &IWt);
  return answer;
}

/******************************************************************************
* EnterIW.
******************************************************************************/

Logical EnterIW (IW, helpId)
struct ItemWindowStruct *IW;
int helpId;
{
  for (;;) {
     ItemWindow (READiw, IW);
     switch (IW->key) {
       case EXITkey_FSI:
	 return FALSE;
       case ENTERkey_EDIT:
	 return TRUE;
       case HELPkey_FSI:
	 OnlineHelpWindow (ilhFile, helpId);
	 break;
     }
  }
}

/******************************************************************************
* EnterPW.
******************************************************************************/

Logical EnterPW (PW, helpId)
struct PromptWindowStruct *PW;
int helpId;
{
  for (;;) {
     PromptWindow (READiw, PW);
     switch (PW->key) {
       case EXITkey_FSI:
	 return FALSE;
       case ENTERkey_EDIT:
	 return TRUE;
       case HELPkey_FSI:
	 OnlineHelpWindow (ilhFile, helpId);
	 break;
     }
  }
}

/******************************************************************************
* IndexInList.
******************************************************************************/

int IndexInList (match, nItems, items)
char *match;
int nItems;
char **items;
{
  int i;
  for (i = 0; i < nItems; i++) if (!strcmp(match,items[i])) return i;
  return -1;
}

/******************************************************************************
* DataTypeItemN.
******************************************************************************/

int DataTypeItemN (dataType)
long dataType;
{
  static char *symbols[] = { DTiwSYMBOLS };
  static int columns[] = { DTiwCOLs };
  static int lineNs[] = { DTiwLINEnS };
  static int lengths[] = { DTiwLENs };
  char token[10+1];	/* Assumes `CDF_DOUBLE' is longest data type token. */
  int itemN;
  for (itemN = 0; itemN < 14; itemN++) {
     strcpyX (token, "CDF_", 10);
     strcatX (token, DataTypeToken(dataType), 10);
     if (!strncmp(token,
		  &(symbols[lineNs[itemN]][columns[itemN]]),
		  lengths[itemN])) return itemN;
  }
  return 0;
}
