/* ------------------------------------------------------------------------- */
/* ----- Let 'em Fly!   V 1.2 ----------- (c) 1991-93 by Oliver Scheel ----- */
/* ------------------------------------------------------------------------- */
/* ----- Module: ltmf_alt.c   new form_alert() & new form_error() ---------- */
/* ------------------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <macros.h>
#include <smallaes.h>
#include <vdi.h>
#include <magx_old.h>
#include <portab.h>

#include "ltmf_str.h"
#include "letemfly.h"
#include "ltmf_def.h"

/* ------------------------------------------------------------------------- */

int ltmf_init(void);

int nfm_do(OBJECT *tree, int startobj);
int nfm_dial(int flag, int xs, int ys, int ws, int hs, int x, int y, int w, int h);
int nfm_center(OBJECT *tree, int *x, int *y, int *w, int *h);

/* ------------------------------------------------------------------------- */

extern	int	gl_wchar,
		gl_hchar,
		gl_wbox,
		gl_hbox;

extern	int	dialno;

extern	int	isalert,
		infoflag;

EXTERN	OBJECT	*alert_dial;

extern	int	initflag;

extern	int	mousecnt;
extern	int	was_used;

extern	MAGX_COOKIE	*magx;

extern	int	global[];

/* ------------------------------------------------------------------------- */

BITBLK	*al_icon[MAXICON];
char	*al_text[5];
char	*al_but[3];

char	al_string[MAXALCH];		/* alert-string buffer */

#ifdef ENGLISH
char	*errstring[18]={"[1][",
			"[1][|Invalid funktion number.|",
			"[1][|File not found.|",
			"[1][|Path not found.|",
			"[1][|Too many open files.|",
			"[1][Access denied.|(name conflict or write|protect)|",
			"[1][|Invalid file handle.|",
			"[1][|Out of memory.|",
			"[1][|Invalid memory block adress.|",
			"[1][|Invalid drive specification.|",
			"[1][|Files are on differrent drives.|",
			"[1][|No more files.|",
			"[1][|Record is locked.|",
			"[1][|No such lock.|",
			"[1][|Range error.|",
			"[1][Internal GEMDOS error.|(Congratulations!)|",
			"[1][|Invalid executable file format.|",
			"[1][|Memory block growth failure.|" };
#else
char	*errstring[18]={"[1][",
			"[1][|Unbekannte Funktionsnummer.|",
			"[1][|Datei nicht gefunden.|",
			"[1][|Pfad nicht gefunden.|",
			"[1][|Zu viele Dateien ge”ffnet.|",
			"[1][Zugriff nicht erlaubt.|(Namenskonflikt oder Schreib-|schutz)|",
			"[1][|Falsches Datei-Handle.|",
			"[1][Nicht gengend Speicher|vorhanden.|",
			"[1][Adresse des Speicherblocks|war nicht korrekt.|",
			"[1][|Ungltige Laufwerksbezeichnung.|",
			"[1][Dateien sind auf verschiedenen|Laufwerken.|",
			"[1][Es k”nnen keine Dateien mehr|gefunden werden.|",
			"[1][|Datei ist verschlossen (locked).|",
			"[1][|`Lock' existiert nicht.|",
			"[1][Dateizeiger in ungltigem|Bereich.|",
			"[1][|Interner GEMDOS-Fehler (Bingo!).|",
			"[1][|Datei ist kein Programm.|",
			"[1][|Speicherblock kann nicht|vergr”žert werden.|" };
#endif

int	err_tab[37] = {	-1,  1,  2,  3,  4,  5,  6,  0,  7,  8,  0,  0,  0,  0,  0,  9,  0,
			10, 11,  0,  0,  0,  0,  0,  0,  0,  0, 12, 13,  0,  0,  0,  0,
			14, 15, 16, 17 };

#ifdef ETV_CRITIC
#ifdef ENGLISH
char	*ceh_str1[6]= {	"[4][|Device not ready!|Is it connected and activated?]",
			"[4][Drive %: not ready!|Please check drive and insert|disk.]",
			"[4][|Data on Drive %: fault?|Please check disk and wiring.]",
			"[5][Disk in Drive %: is write|protected! Please remove|protection.]",
			"[4][|Application can't read disk|in Drive %:.]",
			"[6][|Please insert Disk %: in|Drive A:.][  OK  ]" };
char	*ceh_but = {	"[Cancel|Retry|Ignore]" };
#else
char	*ceh_str1[6]= {	"[4][Ausgabeger„t antwortet nicht!|Ist es angeschlossen und|eingeschaltet?]",
			"[4][Laufwerk %: antwortet nicht!|Bitte berprfen und eine|Disk einlegen.]",
			"[4][Daten auf Disk %: defekt?|Prfen Sie die Disk und die|Verbindungskabel.]",
			"[5][Disk in Laufwerk %: ist schreib-|geschtzt. Vor dem n„chsten|Versuch den Schutz entfernen.]",
			"[4][Die Anwendung kann die Disk|in Laufwerk %: nicht lesen.]",
			"[6][|Bitte Disk %: in Laufwerk A:|einlegen.][  OK  ]" };
char	*ceh_but = {	"[Abbruch|Nochmal|Ignorieren]" };
#endif

char	ceh_tab[18] = {	0, 0, 1, 1, 2, 1, 1, 2, 2, 0, 2, 2, 2, 3, 4, 0, 2, 5 };
#endif

/* ------------------------------------------------------------------------- */
/* ----- new form_alert ---------------------------------------------------- */
/* ------------------------------------------------------------------------- */

int _gettxt(char *dest[], char **source, int maxparts)
{
	int	i,
		cont;
	char	*strbeg;

	i = 0;
	cont = TRUE;
	while(**source && (i < maxparts) && cont)
	{
		strbeg = *source;
		do
		{
			switch(**source)
			{
			    case ']'  :	cont = FALSE;
			    case '|'  : **source = '\0';
			    		break;
			}
		}
		while(*(*source)++);
		dest[i++] = strbeg;
	}
	return(i);
}

int _getmaxlen(char *str[], int cnt)
{
	int	i,
		maxlen;

	maxlen = 0;
	for(i = 0; i < cnt; i++)
	{
		if(strlen(str[i]) > maxlen)
			maxlen = (int)strlen(str[i]);
	}
	return(maxlen);
}
		
/* ------------------------------------------------------------------------- */

int nfm_alert(int but, char *string)
{
	int	i,
		bh,
		icon,
		txtmax, butmax,
		txtlen, butlen,
		box_width, box_height,
		exit_obj;
	int	ms_on, wu_sav;
	int	x, y, w, h;
	char	*strbeg;
	OBJECT	*ad;

	if(letemfly.conf & C_ALERT)
	{
		if(!initflag)
			ltmf_init();
		graf_handle(&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);

		icon = string[1] - '0';
		if(icon <= 3)
		{
			strncpy(al_string, string, MAXALCH);
			al_string[MAXALCH-1] = '\0';
		}
		alert_dial[ALICON].ob_spec.bitblk = al_icon[icon];
/*		alert_dial[ALICON].ob_spec.bitblk->bi_color = letemfly.aicol;
		alert_dial[ALICON].ob_spec.bitblk->bi_pdata = al_icon[icon];
*/
/*		alert_dial[ALICON].ob_y = gl_hchar/2 * 3;
*/
		strbeg = al_string + 4l;
		txtmax = _gettxt(&al_text, &strbeg, 5);
		while(*strbeg++ != '[');
		butmax = _gettxt(&al_but, &strbeg, 3);
		txtlen = _getmaxlen(&al_text, txtmax);
		butlen = _getmaxlen(&al_but, butmax) + 3;

		for(i = 0; i < txtmax; i++)
		{
			ad = &alert_dial[ALTEXT1+i];
			ad->ob_next = ALTEXT1+i+1;
			ad->ob_x = icon ? 64 : 16;
			ad->ob_y = gl_hchar/2 + i * gl_hchar;
			ad->ob_width = (int)strlen(al_text[i]) * gl_wchar;
			ad->ob_spec.free_string = al_text[i];
			ad->ob_flags &= ~HIDETREE;
		}
		alert_dial[ALTEXT1+i-1].ob_next = ALBUT1;
		for(i = txtmax; i < 5; i++)
			alert_dial[ALTEXT1+i].ob_flags |= HIDETREE;
		box_width = max((icon ? 8 : 2) + txtlen + 2, butlen * butmax);
		box_height = max(txtmax, icon ? (alert_dial[ALICON].ob_height / gl_hchar) : 0) + 3;
		bh = (letemfly.conf & C_KEYS) ? 2 : 0;
		for(i = 0; i < butmax; i++)
		{
			ad = &alert_dial[ALBUT1+i];
			ad->ob_next = ALBUT1+i+1;
			ad->ob_flags &= ~(LASTOB|HIDETREE|DEFAULT);
/*			ad->ob_x = ((box_width - butlen * butmax) + butlen * i) * gl_wchar;
*/			ad->ob_x = ((box_width - butlen * butmax) / 2+ butlen * i + 1) * gl_wchar;
			ad->ob_y = (box_height - 2) * gl_hchar + gl_hchar/2;
			ad->ob_width = (butlen-2) * gl_wchar;
			ad->ob_height = gl_hchar + bh;
			ad->ob_spec.free_string = al_but[i];
		}
		i--;
		alert_dial[ROOT].ob_tail = ALBUT1+i;
		alert_dial[ALBUT1+i].ob_next = ROOT;
		alert_dial[ALBUT1+i].ob_flags |= LASTOB;
		for(i = butmax; i < 3; i++)
			alert_dial[ALBUT1+i].ob_flags |= HIDETREE;
		if(but)
			alert_dial[ALBUT1+but-1].ob_flags |= DEFAULT;
		alert_dial->ob_width = box_width * gl_wchar;
		alert_dial->ob_height = box_height * gl_hchar + 2;
		if(icon > 3)
			alert_dial->ob_flags |= NOFLY;
		else
			alert_dial->ob_flags &= ~NOFLY;
		alert_dial->ob_spec.obspec.framesize = letemfly.aframe;
		alert_dial->ob_flags &= ~CENTER;
		isalert = TRUE;
		nfm_center(alert_dial, &x, &y, &w, &h);
		wind_update(BEG_UPDATE);

		if((magx && (!magx->aesvars->moff_cnt) && (magx->aesvars->version == 0x0100)) ||
			(!magx && (AESVersion() < 0x0400) && !mousecnt))
		{
			graf_mouse(M_ON, 0l);
			ms_on = TRUE;
		}
		else
			ms_on = FALSE;

		wu_sav = was_used;
		nfm_dial(FMD_START, 0, 0, 0, 0, x, y, w, h);
		objc_draw(alert_dial, ROOT, MAX_DEPTH, x, y, w, h);

		exit_obj = nfm_do(alert_dial, 0) & 0x7fff;

		nfm_dial(FMD_FINISH, 0, 0, 0, 0, x, y, w, h);
		was_used = wu_sav;
		if(ms_on)
			graf_mouse(M_OFF, 0l);

		wind_update(END_UPDATE);
		isalert = FALSE;
		alert_dial[exit_obj].ob_state &= ~SELECTED;
		return(exit_obj-ALBUT1+1);
	}
	else
		return(form_alert(but, string));
}

/* ------------------------------------------------------------------------- */
/* ----- new form_error ---------------------------------------------------- */
/* ------------------------------------------------------------------------- */

int nfm_error(int err)
{
	int	al;
	char	err_str[128];

	if(letemfly.conf & C_ALERT)
	{
		if((err > 0) && (err < 64))
		{
			al = (err < 37) ? err_tab[err] : 0;
			strcpy(err_str, errstring[al]);
#ifdef ENGLISH
			strcat(err_str, " |TOS Error #");
#else
			strcat(err_str, " |TOS Fehler #");
#endif
    			strcat(err_str, itoa(~(err + 30), "123456", 10));
			strcat(err_str, "][  OK  ]");
			nfm_alert(1, err_str);
		}
		return(0);
	}
	else
		return(form_error(err));
}

/* ------------------------------------------------------------------------- */
/* ----- new etv_critic ---------------------------------------------------- */
/* ------------------------------------------------------------------------- */
#ifdef ETV_CRITIC

long netv_critic(int err, int drive)
{
	long	result;
	int	choice;
	char	*txtp;

	if(err < 0)
	{
		err = -err;
		strcpy(al_string, ceh_str1[ceh_tab[err]]);
		if(err != 17)
			strcat(al_string, ceh_but);
		txtp = al_string;
		while(*txtp && (*txtp != '%'))
			txtp++;
		if(*txtp == '%')
			*txtp = drive + 'A';
		choice = nfm_alert((err == 17) ? 1 : 2, al_string);
		switch(choice)
		{
		    case 1    :	result = (long)-err;
		    		break;
		    case 2    : result = 0x10000L;
		    		break;
		    case 3    : result = 0x0L;
		}
		if(err == 17)
			result = 0x10000L;
		return(result);
	}
	return((long)err);
}

#endif
