/*
*	display.c
*
*	NewMenus, NewWindow and WinTexts for ppdata v1.0,
*	and routines for opening display.
*
*	MWS, 3/92.
*/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/gadtools.h>
#include <proto/gadtools.h>
#include "wintext.h"
#include "display.h"

static struct NewMenu mainitems[] = {
	{ NM_TITLE, "Project",	NULL, 0, 0, NULL },
	{ NM_ITEM,  "Load",	"L",  0, 0, NULL },
	{ NM_ITEM,  "Save",	"S",  0, 0, NULL },
	{ NM_ITEM,  "Batch",	NULL, 0, 0, NULL },
	{ NM_ITEM,  "Delete",	NULL, 0, 0, NULL },
	{ NM_ITEM,  "About",	NULL, 0, 0, NULL },
	{ NM_ITEM,  "Quit",	"Q",  0, 0, NULL },

	{ NM_TITLE, "Prefs",	NULL, 0, 0, NULL },
	{ NM_ITEM,  "Color",	NULL,  0, 0, NULL },
	{ NM_SUB,   "Color 0",	NULL, CHECKIT, ~1, NULL },
	{ NM_SUB,   "Color 1",	NULL, CHECKIT, ~2, NULL },
	{ NM_SUB,   "Pointer",	NULL, CHECKIT, ~4, NULL },
	{ NM_SUB,   "Scroll",	NULL, CHECKIT, ~8, NULL },
	{ NM_SUB,   "None",	NULL, CHECKIT, ~16, NULL },

	{ NM_ITEM,  "Efficiency",NULL,  0, 0, NULL },
	{ NM_SUB,   "Fast",	NULL, CHECKIT, ~1, NULL },
	{ NM_SUB,   "Mediocre",	NULL, CHECKIT, ~2, NULL },
	{ NM_SUB,   "Good",	NULL, CHECKIT, ~4, NULL },
	{ NM_SUB,   "Very Good",NULL, CHECKIT, ~8, NULL },
	{ NM_SUB,   "Best",	NULL, CHECKIT, ~16, NULL },

	{ NM_ITEM,  "Speedup",	NULL,  0, 0, NULL },
	{ NM_SUB,   "Large",	NULL, CHECKIT, ~1, NULL },
	{ NM_SUB,   "Medium",	NULL, CHECKIT, ~2, NULL },
	{ NM_SUB,   "Small",	NULL, CHECKIT, ~4, NULL },

	{ NM_ITEM,  "Suffix",	NULL,  MENUTOGGLE+CHECKIT, 0, NULL },
	{ NM_ITEM,  "Overwrite",NULL,  MENUTOGGLE+CHECKIT, 0, NULL },
	{ NM_ITEM,  "Save Prefs",NULL,  0, 0, NULL },

	{ NM_TITLE, "Control",		NULL, 0, 0, NULL },
	{ NM_ITEM,  "Always decrunch",	NULL, CHECKIT, ~1, NULL },
	{ NM_ITEM,  "Always recrunch",	NULL, CHECKIT, ~2, NULL },
	{ NM_ITEM,  "Ignore crunched",	NULL, CHECKIT, ~4, NULL },
	{ NM_ITEM,  "Query decrunch",	NULL, CHECKIT, ~8, NULL },

	{ NM_END }
};

static struct NewMenu abortitems[] = {
	{ NM_TITLE, "Project",	NULL, 0, 0, NULL },
	{ NM_ITEM,  "Abort",	"A", 0, 0, NULL },
	{ NM_END }
};

struct Menu *mainmenu, *abortmenu;
static APTR vi;		/* visual info for gadtools */

struct NewWindow new_window = {
  0,0,0,0,0,1,
  CLOSEWINDOW+MENUPICK,
  WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+NOCAREREFRESH+SMART_REFRESH+ACTIVATE,
  NULL,NULL,
  (UBYTE *)"PPData v1.0",NULL,NULL,
  100,10,-1,-1,WBENCHSCREEN };

/***** not at moment - window redraw hassles
WORD zipdata[4]; ****/
struct TagItem window_tags[] = {
/***	{ WA_Zoom, (LONG)&zipdata[0] },***/
	{ TAG_DONE }
};

struct Window *window;
struct TextFont *font;

WINTEXTINFO	wtinfo;			/* to be initialised later */
WINTEXT wt_initial[] = {
	{ &wt_initial[1], "File Name    :", 0, 0, 3, JAM1, -1 },
	{ &wt_initial[2], "File Length  :", 0, 1, 3, JAM1, -1 },
	{ NULL,		  "(De)Crunched :", 0, 2, 3, JAM1, -1 }
};

/********************* DISPLAY OPEN/CLOSE ROUTINES ****************************/

BOOL OpenDisplay()	/* alloc wintext memory, set zip window, open window */
{
	if (InitWinTextInfo(&wtinfo, &new_window, TEXT_ROWS, TEXT_COLUMNS))
	{
/****		zipdata[0] = new_window.LeftEdge;
		zipdata[1] = new_window.TopEdge;
		zipdata[2] = new_window.Width;
		zipdata[3] = wtinfo.toffset - 1;
****/
		if (window = OpenWindowTagList(&new_window, window_tags))
		{
			wtinfo.window = window;
			if (font = OpenFont(wtinfo.tattr))
			{
				SetFont(window->RPort, font);
				RenderWinTexts(&wtinfo, wt_initial);

				return TRUE;
			}
			CloseWindow(window);
		}
		FinishWinText(&wtinfo);
	}
	return FALSE;
}

void CloseDisplay()	/* free wintext memory, close window */
{
	CloseFont(font);
	FinishWinText(&wtinfo);
	CloseWindow(window);
}


/********************* ALLOCATION/DEALLOCATION ROUTINES ***********************/

/* static prototypes */
static void ClearMsgPort(struct MsgPort *);

extern struct Window *window;


static void ClearMsgPort(struct MsgPort *mp)	/* clear existing messages from */
{						/* message port (ignoring contents) */
	struct Message *msg;

	while (msg = GetMsg(mp))
		ReplyMsg(msg);
}

void SetPPDataMenu(int which)		/* clear message port and set new menu */
{
	Forbid();
		ClearMsgPort(window->UserPort);

		if (which == MAINMENU)
			SetMenuStrip(window,mainmenu);
		else /* which == ABORTMENU */
			SetMenuStrip(window,abortmenu);
	Permit();
}

void FreePPDataMenus()			/* free all menustrips and related data */
{
	if (mainmenu)	FreeMenus(mainmenu);
	if (abortmenu)	FreeMenus(abortmenu);
	if (vi)		FreeVisualInfo(vi);
}

BOOL AllocPPDataMenus()			/* allocate menustrips required by program */
{
	if ((mainmenu = CreateMenusA(mainitems, NULL)) &&
	    (abortmenu = CreateMenusA(abortitems, NULL)) &&
	    (vi = GetVisualInfoA(window->WScreen, NULL)) &&
 	    (LayoutMenusA(mainmenu, vi, NULL)) &&
	    (LayoutMenusA(abortmenu, vi, NULL)))
		return TRUE;

	FreePPDataMenus();	/* something failed... */
	return FALSE;
}
