/*
 * menu.c
 *
 * Verwaltung die Menleiste der Applikation.
 * ACHTUNG: Nicht re-entrant, d.h. jede Applikation nur ein Kreuz, h Men!
*/
#include "intern.h"

static OBJECT		*tree = NULL;
static int			disabled = FALSE;
static int			dis_cnt = 0;
static KEYTAB		*keys = NULL;

/*****************************************************************************/
int create_menu(OBJECT *t)
{
	if (keys == NULL)
		keys = (KEYTAB *)Keytbl((void *)-1, (void *)-1, (void *)-1);

	if (tree == NULL)
	{
		tree = t;
		disabled = FALSE;
		dis_cnt = 0;

		fix_menu(tree);
		menu_bar(tree, 1);
		return TRUE;
	}
	else
		form_alert(1, "[3][CF-Lib Panic: create_menu()|Kein zweites Men mglich!][Abbruch]");
	return FALSE;
}

/*****************************************************************************/
void delete_menu(void)
{
	if (tree != NULL)
	{
		menu_bar(tree, 0);
		tree = NULL;
	}
}

/*****************************************************************************/
static void toggle_menu(int dis)
{
	if ((tree != NULL) && (disabled != dis))
	{
		int	titel;

		wind_update(BEG_UPDATE);
		titel = tree[3].ob_next;				/* 3: Desk-Titel */
		while (titel != 2)						/* 2: Box, die die Titel umgibt */
		{
			set_state(tree, titel, DISABLED, dis);
			titel = tree[titel].ob_next;
		}
		menu_bar(tree, TRUE);

		/* Ersten String (About) suchen */
		while (!(tree[titel].ob_type == G_STRING || tree[titel].ob_type == G_SHORTCUT))
			titel++;
		menu_ienable(tree, titel, !dis);

		disabled = dis;

		wind_update(END_UPDATE);
	}
}

void enable_menu(void)
{
	if (tree != NULL)
	{
		if (dis_cnt > 0)
			dis_cnt--;
		if (dis_cnt == 0)
			toggle_menu(FALSE);
	}
}

void disable_menu(void)
{
	if (tree != NULL)
	{
		dis_cnt++;
		if (dis_cnt == 1)
			toggle_menu(TRUE);
	}
}


/*****************************************************************************/
static int check_item_key(char *str, int kstate, int kreturn)
{
	int	found = FALSE;
	int	len, i, r, ks;
	char	s[50], ascii;
	int	scan;
	
	scan = (kreturn & 0xff00) >> 8;
	if (scan > 127)
		return FALSE;
	if (scan >= 0x78 && scan < 0x80)
		ascii = keys->capslock[scan - 0x76];
	else
		ascii = keys->capslock[scan];

	if (str[0] != EOS)
	{
		len = (int)strlen(str);
		i = len - 1;
		r = 0;
		while (str[i] == ' ')
		{
			i--;
			r++;
		}
		if (r < 2)
		{
			str[len-r] = EOS;			/* Blanks am Ende lschen */

			while ((i >= 0) && (str[i] != ' '))
				i--;
			strcpy(s, str+i+1);
			str_toupper(s);

			ks = 0;
			i = 0;
			found = FALSE;
			while (!found)
			{
				switch (s[i])
				{
					case '^' :
						ks |= K_CTRL;
						i++;
						break;
					case '' :
						ks |= K_ALT;
						i++;
						break;
					case '' :
						ks |= K_LSHIFT;
						i++;
						break;
					default:
						found = TRUE;
				}
			}
			
			found = FALSE;
			if (ks == kstate)			/* Sondertasten mssen gleich sein */
			{
				if (s[i+1] == EOS)								/* nur einzelnes Zeichen */
				{
					if (s[i] == 0x1B && scan == 1)			/*  */
						found = TRUE;
					else if (s[i] == ascii)
						found = TRUE;
				}
				else if ((strcmp(s+i, "DEL") == 0) && (scan == 83))
					found = TRUE;
				else if ((strcmp(s+i, "ESC") == 0) && (scan == 1))
					found = TRUE;
				else if ((strcmp(s+i, "HELP") == 0) && (scan == 98))
					found = TRUE;
				else if ((strcmp(s+i, "HOME") == 0) && (scan == 71))
					found = TRUE;
				else if ((strcmp(s+i, "INS") == 0) && (scan == 82))
					found = TRUE;
				else if ((strcmp(s+i, "TAB") == 0) && (scan == 15))
					found = TRUE;
				else if ((strcmp(s+i, "UNDO") == 0) && (scan == 97))
					found = TRUE;
				else if ((s[i] == 'F') && (s[i+1] >= '1') && (s[i+1] <= '9'))
				{
					if (s[i+2] == '0' && scan == 68)		/* F10 */
						found = TRUE;
					else
					{
						if (ks == K_LSHIFT || ks == K_RSHIFT)
						{
							if ((scan - 25) == (s[i+1] + 10))
								found = TRUE;
						}
						else
						{
							if (scan	== (s[i+1] + 10))
								found = TRUE;
						}
					}
				}
			}
		}
	}
	return found;
}

int is_menu_key(int kreturn, int kstate, int *title, int *item)
{
	int	menu_box, i, t;
	char	str[50];
	int	found = FALSE;
	
	if ((tree != NULL) && !disabled)
	{
		menu_box = tree[0].ob_tail;
		menu_box = tree[menu_box].ob_head;
		t = 3;										/* 3: Desktop-Titel */
		do
		{
			i = tree[menu_box].ob_head;
			do
			{
				if (((tree[i].ob_type == G_STRING) || (tree[i].ob_type == G_SHORTCUT))  
				    && !(tree[i].ob_state & DISABLED))
				{
					get_string(tree, i, str);
					if (!(str[0] == '-') && (strncmp(str, "  Desk", 6) != 0))
					{
						found = check_item_key(str, kstate, kreturn);
						if (found)
						{
							*title = t;
							*item = i;
							menu_tnormal(tree, *title, 0);
							return TRUE;
						}
					}
				}
				i = tree[i].ob_next;
			}
			while (i != menu_box);
			menu_box = tree[menu_box].ob_next;
			t = tree[t].ob_next;
		}
		while (t != 2);							/* 2: Box, die die Titel umgibt */
	}
	return FALSE;
}
