#include <string.h>
#include <stdio.h>
#include <malloc.h>

#include "panel.h"

FILE *handle;
char fn[20];
char *text[500];
char *newln;
char buf[80];
unsigned aid = 0;
int lines, cur_line;

void process_topic(int);
void move(int);


/*************  main  ***************/

main()
    {
    int topic = 1;

    panel_activate("helpmenu");

    while (aid != ESC)
        {
        put_field("stat", 1,
        " [Esc]=Exit  []=Prior topic  []=Next topic  [Enter]=Process ");

        modify_field_attr("topic", topic, "pr6");
        aid = get_key();
        modify_field_attr("topic", topic, "ph6");
        switch (aid)
            {
            case UP:
                if (topic > 1)
                    topic--;
                break;
            case DOWN:
                if (topic < 6)
                    topic++;
                break;
            case ENTER:
                if (topic < 6)
                    process_topic(topic);
                else
                    {
                    panel_activate("ordform");
                    while (ESC != panel_execute("", 1, 0));
                    destroy_panel();
                    }
                break;
            }

        }

        destroy_panel();

    }


/*************  process_topic  ***************/

void process_topic(topic)
int topic;
    {
    int i;

    sprintf(fn, "help%d.txt", topic);
    if ((handle = fopen(fn, "r")) == NULL)
        {
        sprintf(buf, "%s - file not found!", fn);
        pan_error(16, 0, buf);
        }

    put_field("name", 1, fn);
    put_field("stat", 1,
    "  [Esc]=Exit  [PgDn]=Next page  [PgUp]=Prior page  [F4]=Menu  ");

    lines = 0;
    fgets(buf, 80, handle);
    while ((!feof(handle)) && (lines < 500))
        {
        newln = strrchr(buf, '\n');
        if (newln != NULL)
            *newln = NULL;

        text[lines] = (char *)malloc(80);
        if (text[lines] == NULL)
            {
            pan_error(16, 0, "Out of memory!");
            }

        strcpy(text[lines], buf);

        lines++;
        fgets(buf, 80, handle);
        }

    fclose(handle);

    panel_activate("helpskel");

    move(cur_line = 0);

    while ((aid = get_key()) != F4)
        {
        if (aid == ESC)
            break;

        switch (aid)
            {
            case PGDN:
                if (cur_line < lines - 19)
                    cur_line += 20;
                move(cur_line);
                break;
            case PGUP:
                if (cur_line >= 20)
                    cur_line -= 20;
                else
                    if (cur_line > 0)
                        cur_line = 0;
                move(cur_line);
                break;
            case F1:
                if (topic == 3)
                   {
                    panel_activate("helpex1");
                    panel_execute("", 1, 0);
                    destroy_panel();
                    }
                break;
            case F2:
                if (topic == 3)
                   {
                    panel_activate("helpex2");
                    panel_execute("", 1, 0);
                    destroy_panel();
                    }
                break;
            case F3:
                if (topic == 3)
                   {
                    panel_activate("helpex3");
                    get_key();
                    destroy_panel();
                   }
                break;
            }

        }

    destroy_panel();

    for (i=0; i<lines; i++)
        free(text[i]);

    put_field("name", 1, "");
    }


/*************  move  ***************/

void move(sub)
int sub;
    {
    int i, j;

    for (i=1; i<=20; i++)
        if ((i+sub) > lines)
            {
            put_field("line", i, "");
            modify_field_attr("line", i, "p7");
            }
        else
            {
            j = i + sub - 1;
            if (strlen(text[j]) > 1)
                {
                if (*text[j] == '+')
                    modify_field_attr("line", i, "ph6");
                else
                    modify_field_attr("line", i, "p7");
                put_field("line", i, text[j]+1);
                }
            else
                {
                put_field("line", i, "");
                modify_field_attr("line", i, "p7");
                }
            }

    }
