/*
 * help.c
 * 
 * Opens help/settings window and handles events.
 * For ToolAlias.
 * 
 * Martin W. Scott,  4 February 1993
 */
#include <exec/types.h>
#include <libraries/commodities.h>
#include <intuition/intuition.h>
#include <libraries/gadtools.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <proto/commodities.h>
#include <string.h>

#include "toolalias_gfx.c"	/* NB!!! compile with NO MULTI-INCLUDES */
#include "file.h"
#include "list.h"
#include "window.h"

extern char popstr[];		/* popup-key */
extern ULONG wndsigflag;	/* signal for IDCMP */
static TOOL *curtool = (APTR)(-1L);	/* currently shown tool */
static const char *emptystr = "";

/* initialise individual gadget */
static void
InitGadget(UWORD num, LONG tagtype, LONG tagvalue)
{
	GT_SetGadgetAttrs(ToolAliasGadgets[num], ToolAliasWnd, NULL, tagtype, tagvalue, TAG_DONE);
}

static void
ToggleStates(BOOL how)
{
	UWORD i;

	/* Notez Bien: this depends on the order of the gadget numbers */
	for (i = GDX_OldStr; i < GDX_SaveBut; i++)
		if (i != GDX_AddBut)
			InitGadget(i, GA_Disabled, how);
}

/* initialise all gadgets */
static void
UpdateGadgets()
{
	static BOOL disabled;

	if (curtool)	/* list isn't empty */
	{
		if (disabled)
			ToggleStates(disabled = FALSE);

		InitGadget(GDX_OldStr, GTST_String, curtool->oldname ? (LONG)curtool->oldname : (LONG)emptystr);
		InitGadget(GDX_NewStr, GTST_String, curtool->newname ? (LONG)curtool->newname : (LONG)emptystr);
	}
	else if (!disabled)
	{
		InitGadget(GDX_OldStr, GTST_String, (LONG)emptystr);
		InitGadget(GDX_NewStr, GTST_String, (LONG)emptystr);
		ToggleStates(disabled = TRUE);
	}
}

/* show our help window */
BOOL
ShowWindow()
{
	static char	title[80];

	if (curtool == (APTR)(-1L))
		curtool = get_head();

	WBenchToFront();
	if (ToolAliasWnd)		/* already open... */
	{
		WindowToFront(ToolAliasWnd);
		return TRUE;
	}

	if (SetupScreen())	/* try to get lock on WB */
		return FALSE;

	strcpy(title, "HotKey=<");
	strcat(title, popstr);
	strcat(title, ">");
	ToolAliasWdt = title;

	if (!OpenToolAliasWindow())	/* like the name says... */
	{
		UpdateGadgets();
		wndsigflag = 1 << ToolAliasWnd->UserPort->mp_SigBit;
		return TRUE;
	}
	CloseDownScreen();
	return FALSE;
}

/* hide our ToolAlias window */
void
HideWindow()
{
	if (ToolAliasWnd)
	{
		CloseToolAliasWindow();
		CloseDownScreen();
		wndsigflag = NULL;
		ToolAliasWnd = NULL;
	}
}

/* extract from string-gadget */
#define GadString(gadget)	((struct StringInfo *)(gadget)->SpecialInfo)->Buffer

/* handle window events */
LONG
HandleIDCMP()
{
	struct IntuiMessage *msg;
	struct Gadget   *gadget;
	ULONG           class;

	while (msg = GT_GetIMsg(ToolAliasWnd->UserPort))
	{
		class = msg->Class;
		gadget = (struct Gadget *)msg->IAddress;
		GT_ReplyIMsg(msg);

		switch (class)
		{
		case CLOSEWINDOW:
			HideWindow();
			return GUI_OKAY;

		case GADGETUP:
			switch (gadget->GadgetID)
			{
			case GD_OldStr:
				if (!set_oldname(curtool, GadString(gadget)))
					return GUI_ABEND;
				ActivateGadget(ToolAliasGadgets[GDX_NewStr], ToolAliasWnd, NULL);
				break;

			case GD_NewStr:
				if (!set_newname(curtool, GadString(gadget)))
					return GUI_ABEND;
				break;

			case GD_StartBut:
				curtool = get_head();
				UpdateGadgets();
				break;

			case GD_PrevBut:
				if (curtool && curtool->prev)
				{
					curtool = curtool->prev;
					UpdateGadgets();
				}
				else DisplayBeep(NULL);
				break;

			case GD_AddBut:
				if (curtool = add_tool(curtool))
				{
					UpdateGadgets();
					ActivateGadget(ToolAliasGadgets[GDX_OldStr], ToolAliasWnd, NULL);
				}
				else
					return GUI_ABEND;
				break;

			case GD_NextBut:
				if (curtool && curtool->next)
				{
					curtool = curtool->next;
					UpdateGadgets();
				}
				else DisplayBeep(NULL);
				break;

			case GD_EndBut:
				curtool = get_tail();
				UpdateGadgets();
				break;

			case GD_DelBut:
				if (curtool)
					curtool = rem_tool(curtool);
				UpdateGadgets();
				break;

			case GD_SaveBut:
				if (write_config(DEF_CONFIG))
				{
					HideWindow();
					return GUI_OKAY;
				}
				break;

			case GD_LoadBut:
				if (get_config(DEF_CONFIG))	/* could fail */
				{
					curtool = get_head();
					UpdateGadgets();
				}
				else return GUI_ABEND;
				break;

			case GD_UseBut:
				HideWindow();
				return GUI_OKAY;

			case GD_QuitBut:
				HideWindow();
				return GUI_QUIT;

			} /* switch (gadget->GadgetID) */
			break;

		case REFRESHWINDOW:
			GT_BeginRefresh(ToolAliasWnd);
/*			ToolAliasRender();*/
			GT_EndRefresh(ToolAliasWnd, TRUE);
			break;

		} /* switch (class) */

	} /* while more messages */

	return GUI_OKAY;
}
