/**************************
**
**	$Id:	gui.c
**
**	Graphic User Interface Module: Amiga Shopper Finder Application.
**	By Toby Simpson
*/

#include	"finder.h"
#include 	"externals.h"

/**************************
**
**	BOOL	NotifyFind(char *file, BOOL log_find)
**
**	Notify that a file was found. The file which matched is passed in
**	and this is then shown on the screen in what every way the program
**	decides. Returns FALSE for an error. (This months version always
**	returns TRUE however). If "log_find" is TRUE, this match is counted.
*/

BOOL	NotifyFind(char *file, BOOL log_find)
{
	struct	Node	*node;
	char					*text_ptr;

	/*
	**	Count matches if required:
	*/
	if (log_find)	files_matched++;
	
	/*
	**	Allocate memory for a list node:
	*/
	if (!(node = (struct Node *)malloc(sizeof(struct Node))))
		return FALSE;

	if (!(text_ptr = malloc(strlen(file) + 1)))
		{
		free(node);
		return FALSE;
		}

	/*
	**	Initialise our new node, and add it to the list:
	*/
	strcpy(text_ptr, file);
	
	node->ln_Name = text_ptr;
	node->ln_Pri = 0;

	AddTail(&find_list, node);

	/*
	**	Update the display gadget so it shows the new list:
	*/
	GT_SetGadgetAttrs(gadget_list[GID_LIST], finder_window, NULL,
		GTLV_Labels, &find_list,
		GTLV_Top, files_matched,
			TAG_DONE);

	return TRUE;
}

/**************************
**
**	void	ClearGUI_List(void)
**
**	Clears out any memory occupied by the list we build of any matches
**	found during the find operation.
*/

void	ClearGUI_List(void)
{
	struct	Node	*node;

	/*
	**	Clear out list if anything in it:
	*/
	while (node = RemHead(&find_list))
		{
		free(node->ln_Name);
		free(node);
		}

	NewList(&find_list);		/* Re-initialise list in case it is used again */

	return;
}

/**************************
**
**	BOOL	OpenGUI(char *in_Drawer, char *in_Search)
**
**	Opens the GUI components for our program. This means opening the
**	window with gadgets on it. Returns TRUE for success, FALSE for a 
**	failure.
*/

BOOL	OpenGUI(char *in_Drawer, char *in_Search)
{
	struct	Screen	*screen;
	void						*vi;
	long						gadget_count = 0;
	long						win_width, win_height, win_x, win_y;
	long						button_width, button_height, button_start;
	long						list_width, list_height, list_start;
	long						string_start, string_width;
	long						window_top, window_inner;
	int							count = 0;
	struct	NewGadget ng;

	/*
	**	Get public screen information and visual info:
	*/
	if (!(screen = LockPubScreen(NULL)))
		{
		printf("Unable to lock default public screen.\n");
		return FALSE;
		}
	if (!(vi = GetVisualInfo(screen, TAG_DONE)))
		{
		printf("Unable to get visual information\n");
		return FALSE;
		}

	/*
	**	Knock up some sensible window dimensions:
	*/
	win_width = screen->Width / 3;
	win_height = screen->Height / 2;
	if (win_width < MIN_WIDTH) 		win_width = MIN_WIDTH;
	if (win_height < MIN_HEIGHT)	win_height = MIN_HEIGHT;

	win_x = (screen->Width / 2) - (win_width / 2);
	win_y = (screen->Height / 2) - (win_height / 2);

	/*
	**	Open the window we are going to use:
	*/
	if (!(finder_window = OpenWindowTags(NULL,
			WA_Title,				"Finder",
			WA_Left,				win_x,
			WA_Top,					win_y,
			WA_Width,				win_width,
			WA_Height,			win_height,
			WA_RMBTrap,			TRUE,
/*			WA_NewLookMenus,TRUE, */
			WA_Activate,		TRUE,
			WA_CloseGadget,	TRUE,
			WA_DepthGadget,	TRUE,
			WA_DragBar,			TRUE,
			WA_IDCMP,				IDCMP_REFRESHWINDOW |
											IDCMP_CLOSEWINDOW |
											LISTVIEWIDCMP |
											IDCMP_GADGETUP |
											BUTTONIDCMP |
											IDCMP_VANILLAKEY |
											IDCMP_RAWKEY,
				TAG_END)))
					{
					printf("Unable to open window!\n");
					return FALSE;
					}

	/*
	**	Create context gadget:
	*/
	first_gadget = NULL;
	context_gadget = CreateContext(&first_gadget);

	/*
	**	Set up defaults:
	*/
	memset(&ng, 0, sizeof(struct NewGadget));
	ng.ng_VisualInfo = vi;
	ng.ng_Flags = 0;
	ng.ng_UserData = NULL;
	ng.ng_TextAttr = screen->Font;
	ng.ng_GadgetID = 0;
	previous_gadget = context_gadget;

	window_top = finder_window->BorderTop + BORDER;
	window_inner = win_height - (window_top) - (finder_window->BorderBottom + BORDER);

	list_width = win_width - (BORDER * 2);
	button_width = (list_width / 3) - BORDER;

	button_height = screen->Font->ta_YSize + BORDER;
	list_height = window_inner - (button_height * 3) - (BORDER * 2); 	/* CHG */

	list_start = window_top;
	button_start = window_top + list_height + BORDER;

	string_width = (button_width * 2) + BORDER;
	string_start = button_width + (BORDER * 2);

	/*
	**	Create list-view first:
	*/
	ng.ng_TopEdge 		= list_start;
	ng.ng_LeftEdge 		=	BORDER;
	ng.ng_Width 			= list_width;
	ng.ng_Height 			= list_height;
	
	gadget_list[gadget_count] = CreateGadget(LISTVIEW_KIND, previous_gadget,
		&ng, GT_Underscore, '_', TAG_DONE);

	previous_gadget = gadget_list[gadget_count];
	gadget_count++;

	/*
	**	Now create our buttons:
	*/
	ng.ng_LeftEdge = string_start;
	ng.ng_TopEdge = button_start;
	ng.ng_Height = button_height;
	ng.ng_GadgetID = ng.ng_GadgetID + 1;
	ng.ng_Width = string_width;

	/*
	**	Put a string gadget in:
	*/
	ng.ng_GadgetText = "_Drawer";

	gadget_list[gadget_count] = CreateGadget(STRING_KIND, previous_gadget, &ng,
		GTST_MaxChars, 255,
		GTST_String, in_Drawer,
		GT_Underscore, '_', TAG_DONE);

	sg_Drawer = gadget_list[gadget_count];

	previous_gadget = gadget_list[gadget_count];
	gadget_count++;

	ng.ng_GadgetID = ng.ng_GadgetID + 1;
	ng.ng_TopEdge += button_height;

	ng.ng_GadgetText = "_Search";

	gadget_list[gadget_count] = CreateGadget(STRING_KIND, previous_gadget, &ng,
		GTST_MaxChars, 255,
		GTST_String, in_Search,
		GT_Underscore, '_', TAG_DONE);

	sg_Search = gadget_list[gadget_count];

	previous_gadget = gadget_list[gadget_count];
	gadget_count++;

	ng.ng_GadgetID = ng.ng_GadgetID + 1;

	ng.ng_LeftEdge = BORDER;
	ng.ng_TopEdge += (button_height + BORDER);
	ng.ng_Width = button_width;

	count = 0;
	while (button_text[count++])
		{
		ng.ng_GadgetText = button_text[count - 1];

		gadget_list[gadget_count] = CreateGadget(BUTTON_KIND, previous_gadget,
			&ng, GT_Underscore, '_', TAG_DONE);

		previous_gadget = gadget_list[gadget_count];
		gadget_count++;
	
		ng.ng_LeftEdge += (button_width + BORDER);
		ng.ng_GadgetID = ng.ng_GadgetID + 1;
		}

	/*
	**	Fall over if gadgets were not created right:
	*/
	if (first_gadget == NULL)	return FALSE;

	/*
	**	Add our buttons to the window:
	*/
	AddGList(finder_window, first_gadget, 0, ~0, NULL);
	RefreshGList(first_gadget, finder_window, NULL, ~0);

	GT_RefreshWindow(finder_window, NULL);

	return TRUE;
}

/**************************
**
**	void	CloseGUI(void)
**
**	Closes any GUI components we opened, such as the window or gadgets
**	for example.
*/

void	CloseGUI(void)
{
	if (finder_window)	CloseWindow(finder_window);
	if (first_gadget)		FreeGadgets(first_gadget);

	return;
}

/**************************
**
**	long	ShowErrorRequester(char *er_text, char *ok_string)
**
**	Shows an error requester on the screen. er_text is the text to be shown
**	in the requester, ok_string is the text for the OK button (if any).
**
**	Return value is the button number pressed, 0 for CANCEL.
*/

long	ShowErrorRequester(char *er_text, char *ok_string)
{
	struct	EasyStruct	my_es =
		{
		sizeof (struct EasyStruct),
		0, "Error",
		"",""
		};
	char	build_buttons[128] = "Cancel";

	/*
	**	If required, build the requester buttons with an OK string also:
	*/
	if (ok_string)
		sprintf(build_buttons, "%s|Cancel", ok_string);

	/*
	**	Set up the easy-request structure:
	*/
	my_es.es_GadgetFormat = build_buttons;
	my_es.es_TextFormat		= er_text;

	/*
	**	And put the requester on the screen:
	*/
	return (EasyRequest(NULL, &my_es, NULL, NULL));
}
