/*
		Specific.c for DOS
*/

/// Includes
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <conio.h>
#include "mytypes.h"
#include "tmiga.h"
#include "protos.h"
///
/// Prototypes
Prototype char *ConfigFilename (void);
Prototype void GetArgs (int argc, char **argv);
Prototype void GetArgs_TidyUp (void);
Prototype int AlertUser (char *body, char *gad, ...);
Local     int EasyIntuitionRequest (char *body, char *gad, void *va);
Prototype char *ErrorMessage (void);
///
/// Global variables
extern progPath[];
///

char *ConfigFilename(void)
{
	static char fn[512];
		sprintf(fn, "%s\\TAGOMIGA.CFG", progPath);
	return fn;
}

/// AlertUser
/* ------------------------------ AlertUser ------------------------------

 Comment:

*/

int AlertUser(char *body, char *gad, ...)
{
	char buffer[2001];
	va_list argptr;

	memset(buffer, 0, 2001);
	va_start(argptr, gad);
	vsprintf(buffer, body, argptr);
	va_end(argptr);

	return internalAlertUser(LIGHTGRAY, buffer, gad);
}

int internalAlertUser(int bg, char *buffer, char *gad)
{
	char lbuf[81], *ptr;
	int maxw = 0, maxh = 1, w, x, y, i, cgad, sgad, spos, higad;

	gotoxy(1, 2);
	ptr = buffer;
	w = 0;
	while(*ptr != 0) {
		if(*ptr == '\n') {
			maxh++;
			w = 0;
			ptr++;
			continue;
		}
		w++;
		if(w > maxw) maxw = w;
		ptr++;
	}

	maxw += 4;
	maxh += 4;
	if(maxw > 80) maxw = 80;
	if(maxh > 25) maxh = 25;
	x = 40-(maxw/2);
	y = 12-(maxh/2);
	textbackground(bg);
	textcolor(BLACK);

	/* Line 1: box border */
	gotoxy(x, y); putch('É');
	for(w = 2; w < maxw; w++) putch('Í');
	putch('»');

	/* Line 2..maxh-1: blank */
	for(i = 1; i < maxh-1; i++) {
		gotoxy(x, y+i); putch('º');
		for(w = 2; w < maxw; w++) putch(' ');
		putch('º');
	}

	/* Line maxh: box border */
	gotoxy(x, y+maxh-1); putch('È');
	for(w = 2; w < maxw; w++) putch('Í');
	putch('¼');

	/* Render body text */
	ptr = buffer;
	i = y+1;
	gotoxy(x+2, i);
	while(*ptr != 0) {
		if(*ptr == '\n') {
			i++;
			gotoxy(x+2, i);
		} else {
			putch(*ptr);
		}
		ptr++;
	}

	sgad = 0;
	do {
		/* Render gadget text */
		gotoxy(x+2, y+maxh-2);
		textcolor((sgad==0)?WHITE:BLACK);
		cprintf("< ");
		if(sgad == 0) spos = wherex();
		textcolor(BLACK);
		cgad = 0;
		for(i = 0; gad[i] != 0; i++) {
			if(gad[i] == '|') {
				textcolor((sgad==cgad)?WHITE:BLACK);
				cprintf(" >");
				cgad++;
				textcolor((sgad==cgad)?WHITE:BLACK);
				cprintf(" < ");
				if(sgad == cgad) spos = wherex();
				textcolor(BLACK);
			} else {
				cprintf("%c", gad[i]);
			}
		}
		textcolor((sgad==cgad)?WHITE:BLACK);
		cprintf(" >");
		higad = cgad;
		gotoxy(spos, y+maxh-2);

		i = getch();
		if(i == 0) {
			i = getch();
			if(i == 75 && sgad > 0) sgad--;
			if(i == 77 && sgad < higad) sgad++;
		}

	} while((i != 13) && (i != 32) && (i != 27));

	if(i == 27) return 0;

	return (cgad == higad)?0:cgad;
}

///
/// AlertF
/* ------------------------------ AlertF ------------------------------

 Comment:

*/

ULONG AlertF (char *body, ...)
{
	char buffer[2001];
	va_list argptr;

	va_start(argptr, body);
	vsprintf(buffer, body, argptr);
	va_end(argptr);

	return internalAlertUser(RED, buffer, "OK|Cancel");
}
///
/// ErrorMessage
/* --------------------------------- ErrorMessage ---------------------------------

 Comment: return an error string derived from IoErr()

*/

char *ErrorMessage ()
{
        static char msg[256];
        
		sprintf (msg, "%s\n", sys_errlist[errno]);
        return msg;
}
///

