/*
  
Title:	MsgEd
  
File:	areas.c
  
Author:	Jim Nutt
	
Copr:	1988 by Jim Nutt
  
Description:
	
	This file contains the routines necessary to select individual
	areas for reading, etc.
	
Revision History:
  
	0.00	16 January 1988 	First draft
	0.10	30 January 1988		Added random access to areas
	
Support Files:
  
	msged.h
  
*/

#include "msged.h"

#define MENUWIDTH 40

void    setlast()

{
	FILE   *fp;
        char    path[PATHLEN];

	sprintf(path, "%s\\lastread", arealist[area].path);
	fp = fopen(path, "wb");
        fwrite((char *) &arealist[area].lastread, sizeof(arealist[area].lastread), 1, fp);
        fwrite((char *) &arealist[area].current, sizeof(arealist[area].current), 1, fp);
        fclose(fp);
}

int     selectarea()
{
	int     i, y, f, l, ch, a, p, b;
        char    maybe[MENUWIDTH];
	char   *desc;

	cls();
        memset(maybe, 0, sizeof(maybe));
	p = 0;
	a = area;
	gotoxy(1, 1);
	bputs("Select a message area");
	gotoxy(1, 8);
	bputs("  position cursor on desired area");
	gotoxy(1, 9);
	bputs("  with the <up>/<down> arrow keys,");
	gotoxy(1, 10);
	bputs("  then press <return> to select");
	gotoxy(1, 12);
	bputs("  <ALT><S> to scan for new messages");

	if (arealist[a].messages == 0) {
		gotoxy(1, 15);
		intense(TRUE);
		bputs("  no messages in current area");
		intense(FALSE);
	}

	for (y = 1; y < (maxy + 1); y++) {
		gotoxy(38, y);
                bputc('º');
	}

	if (a > maxy)
		f = a;
	else
		f = 0;
	y = 0;
	i = f;
	while ((i < areas) && (y < maxy)) {
		gotoxy(39, ++y);
                bputc((unsigned char)((arealist[i].lastread < arealist[i].last)?'*':' '));
		bprintf("%s", arealist[i].description);
		l = i++;
	}

	i = a;
        ch = 0;

	while ((ch != ENTER) && (ch != ABORT)) {
                gotoxy(39, a - f + 1);
                clreol();
                background(normfore);
                foreground(normback);
                bputc((unsigned char)((arealist[a].lastread < arealist[a].last) ? '*' : ' '));
                bputs(arealist[a].description);
                background(normback);
                foreground(normfore);

                switch (ch = getkey()) {

                case SCAN:
			{
                        int j = area;

                        memset(maybe,0,sizeof(maybe));
                        p = 0;
			gotoxy(1, 17);
			blink(TRUE);
			intense(TRUE);
			bputs("  scanning");
			blink(FALSE);
			intense(FALSE);
			for (area = 0; area < areas; area++) {
				arealist[area].messages = scanmail(arealist[area].path);
				if ((area >= f) && (area <= l) && arealist[area].lastread < arealist[area].last) {
					gotoxy(39,area - f + 1);
					bputc('*');
				}
			}
			area = j;
			arealist[area].messages = scanmail(arealist[area].path);
			gotoxy(1, 17);
			bputs("          ");
                        }
                        break;

                case PGUP:
                        memset(maybe, 0, sizeof(maybe));
                        p = 0;
                        if (a > maxy) {
                                int i = (a -= (maxy - 1));
                                clrwnd(39,1,maxx,maxy);
                                y = 1;
                                for (;i < min(areas,i + maxy); i++,y++) {
                                        gotoxy(39,y);
                                        bputc((unsigned char)((arealist[i].lastread < arealist[i].last)?'*':' '));
                                        bprintf("%s", arealist[i].description);
                                }
                                f = a;
                                l = i - 1;
			}
			else {
				gotoxy(39, a - f + 1);
				clreol();
                                bputc((unsigned char)((arealist[a].lastread < arealist[a].last)?'*':' '));
				bputs(arealist[a].description);
				a = f;
			}
                        break;

                case PGDN:
                        memset(maybe, 0, sizeof(maybe));
                        p = 0;
                        if ((a + maxy) < areas) {
                                int i = (a += (maxy - 1));
                                clrwnd(39,1,maxx,maxy);
                                y = 1;
                                for (;i < min(areas,i + maxy); i++,y++) {
                                        gotoxy(39,y);
                                        bputc((unsigned char)((arealist[i].lastread < arealist[i].last)?'*':' '));
                                        bprintf("%s", arealist[i].description);
                                }
                                f = a;
                                l = i - 1;
			}
			else {
				gotoxy(39, a - f + 1);
				clreol();
                                bputc((unsigned char)((arealist[a].lastread < arealist[a].last)?'*':' '));
				bputs(arealist[a].description);
				a = l;
			}
                        break;

		case UP:
                        memset(maybe, 0, sizeof(maybe));
			p = 0;
			if (a > 0) {
				gotoxy(39, a - f + 1);
				clreol();
                                bputc((unsigned char)((arealist[a].lastread < arealist[a].last)?'*':' '));
				bputs(arealist[a].description);
				a--;
				if (a < f) {
					scrolldown(39, 1, maxx, maxy, 1);
                                        f--;
                                        if ((l - f) >= maxy)
                                                l--;
				}
			}
			break;

                case DOWN:
                        memset(maybe, 0, sizeof(maybe));
			p = 0;
                        if (a < (areas-1)) {
				gotoxy(39, a - f + 1);
				clreol();
                                bputc((unsigned char)((arealist[a].lastread < arealist[a].last)?'*':' '));
				bputs(arealist[a].description);
                                a++;
				if (a > l) {
                                        scrollup(39, 1, maxx, maxy, 1);
					l++;
					f++;
				}
			}
                        break;

                case ENTER:
                        return(a);

		case ABORT:
                        return (i);

		default:
                        maybe[p++] = (unsigned char) (ch & 0xff);
                        maybe[p] = '\0';
			for (b = 0; b < areas; b++) {
                                desc = strlwr(strdup(arealist[b].description));
				if (strncmp(strlwr(maybe), desc, strlen(maybe)) == 0) {
					f = l = a = b;
					clrwnd(39, 1, maxx, maxy);
					for (; (b < areas) && ((b - f) < maxy); b++) {
						gotoxy(39, b - f + 1);
						clreol();
                                                bputc((unsigned char)((arealist[b].lastread < arealist[b].last)?'*':' '));
						bputs(arealist[b].description);
						l = b;
                                        }
                                        b = areas + 1;
				}
				free(desc);
			}
			if (b == areas) {
                                memset(maybe, 0, sizeof(maybe));
				p = 0;
			}
		}
        }
        return(area);
}
