/* ------------------------------------------------------------------------ */
/*                                ssthelp.c                                 */
/*                   high-level help interface routines                     */
/*                                                                          */
/*      CopyRight (C) 1991,1992  Steven Lutrov.   All rights reserved.      */
/* ------------------------------------------------------------------------ */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#include "sstvid.h"
#include "sstwin.h"
#include "sstkey.h"

#define MAXLINE     80          /* maximum help line width  */
#define MAXHELPS    25          /* maximum help topics */

typedef struct {
      char *title;
      int  tjust;
      int bdr;
      int colour[4];
} _hwin;

static struct helps {
	char hname [9];     /* help name */
	int h, w;           /* height and width */
	long hptr;          /* pointer to help text */
} hps [MAXHELPS+1];


static int hp = 0;          /* help counter */
static int ch = 0;          /* */
static int hx, hy;          /* x and y coordinates for help screen */
FILE   *helpfp = NULL;      /* help file */
char   helpname[64];        /* help tag name */

static _hwin hw = {            /* set default to monochrome attributes */
   NULL, JUST_C, BRD_SINGLE,
       { CLR(BLACK,     LIGHTGRAY, DIM),
	 CLR(BLACK,     LIGHTGRAY, DIM),
	 CLR(LIGHTGRAY, BLACK,     BRIGHT),
	 CLR(BLACK,     LIGHTGRAY, DIM)
       }
};

#define HWIN_BORDER	   (hw.colour[WIN_BORDER])
#define HWIN_TITLEC	   (hw.colour[WIN_TITLE])
#define HWIN_ACCENT        (hw.colour[WIN_ACCENT])
#define HWIN_FACE          (hw.colour[WIN_FACE])
#define HCOLOUR            (hw.colour)

/* ------------------------------------------------------------------------ */
/*                        local function declerartions                      */
/* ------------------------------------------------------------------------ */

void   getline      (char *lineh);
void   help         (void);

/* ------------------------------------------------------------------------ */
/*                      load the HELP! definition file                      */
/* ------------------------------------------------------------------------ */
void Hload(char *hn)
{
	extern void (*helpfunc)();
	extern int helpkey;
	char lineh [MAXLINE];

	if (strcmp(helpname, hn) == 0)
		return;
	helpfunc = help;
	helpkey = F1;
	hp = 0;
	strcpy(helpname, hn);
	if ((helpfp = fopen(helpname, "r")) == NULL)
		return;
	getline(lineh);
	while (1)	{
		if (hp == MAXHELPS)
			break;
		if (strncmp(lineh, "<end>", 5) == 0)
			break;
		if (*lineh != '<')
			continue;
		hps[hp].h = 3;
		hps[hp].w = 18;
		strncpy(hps[hp].hname, lineh+1,8);
		hps[hp].hptr = ftell(helpfp);
		getline(lineh);
		while (*lineh != '<')	{
			hps[hp].h++;
			hps[hp].w = max(hps[hp].w, strlen(lineh)+2);
			getline(lineh);
		}
		hp++;
	}
}

/* ------------------------------------------------------------------------ */
/*                  get a line of text from the help file                   */
/* ------------------------------------------------------------------------ */
static void getline(char *lineh)
{
    if (fgets(lineh, MAXLINE, helpfp) == NULL)
	strcpy(lineh, "<end>");
}

/* ------------------------------------------------------------------------ */
/*                   set the current active help screen                     */
/* ------------------------------------------------------------------------ */
void Hset(char *s, int x, int y)
{
    for (ch = 0; ch < hp; ch++)
		if (strncmp(s, hps[ch].hname,8) == 0)
			break;
    hx = x;
    hy = y;
}
/* ------------------------------------------------------------------------ */
/*                    display the current help window                       */
/* ------------------------------------------------------------------------ */
void help(void)
{
    char ln [MAXLINE];
    int i;
    WINDOW *wnd;
    extern int helpkey;

    if (hp && ch != hp)	{
	 vpushcur();
	 vhidecur();
	 if ((wnd = Westablish(hx, hy, hps[ch].h, hps[ch].w)) == NULL)
		return;
	 Wsettitle(wnd, hw.title,hw.tjust);
	 WBORDER = HWIN_BORDER;
	 WTITLEC = HWIN_TITLEC;
	 WACCENT = HWIN_ACCENT;
	 WFACE = HWIN_FACE;
	 Wsetborder(wnd, hw.bdr);
	 Wshow(wnd);
	 fseek(helpfp, hps[ch].hptr, 0);
	 for (i = 0; i < hps[ch].h-3; i++)	{
		getline(ln);
		Wprintf(wnd, ln);
	    }
	 kgetch();
	 Wdelete(wnd);
	 vpopcur();
	}
}

/* ------------------------------------------------------------------------ */
/*                   set the border typecurrent active help screen                     */
/* ------------------------------------------------------------------------ */
void Hsetborder(int b)

{
  hw.bdr = b;
}


/* ------------------------------------------------------------------------ */
/*                  set the help windows colour characteristics             */
/* ------------------------------------------------------------------------ */
void Hsetcolour(int area, int bg, int fg, int inten)

{
  if (area == WIN_ALL)
    while (area)
       HCOLOUR [--area] = CLR(bg, fg, inten);
    else
       HCOLOUR [area] = CLR(bg, fg, inten);
}

/* ------------------------------------------------------------------------ */
/*                         set the help windows title                       */
/* ------------------------------------------------------------------------ */
void Hsettitle(char *t, int j)

{
  hw.title = t;
  hw.tjust =j;
}
