/* $Id: map.c,v 1.5 1997/07/01 14:44:43 archer Exp $
 *	MAP.C - Handle the Map Window (the main component of the UI)
 */
#include "lorenai.h"
#include <Widget/Widgets.h>
#include <Widget/Shell.h>
#include <Widget/Form.h>
#include <Widget/MenuBar.h>
#include <Widget/MenuItem.h>
#include <Widget/Menu.h>
#include <Widget/Separator.h>
#include <Widget/Scroller.h>
#include <Widget/Manage.h>
#include <Widget/Label.h>
#include "LorMap.h"


/**
 ** Special prototypes
 **/
extern void	write_guild_map(FILE *);


/**
 ** Private variables
 **/
static X11_widget	col, row;
static X11_widget	map;
static X11_widget	h_market, h_recruit;


/**
 ** QUIT_TOOL
 **	As it says
 **/
static void quit_tool(X11_widget dummy, void *save)
{
/*
 * Need to save the cache?
 */
	if (save)
		write_guild_map(fopen("cache.map", "w"));
	exit(0);
}


/**
 ** Internal: UNHIGHLIGHT
 **	Remove all map highlights
 **/
static void unhighlight(X11_widget dummy, void *ignore)
{
int	i, j;
int	b;
/*
 * Parse map
 */
	b = 0;
	for (i = 1; i <= MAX_MAP; i++)
		for (j = 1; j <= MAX_MAP; j++)
			if (lorenai_map[i][j].highlighted) {
				b = 1;
				lorenai_map[i][j].highlighted = 0;
				lorenai_map[i][j].update_needed = 1;
			}
/*
 * Need a refresh?
 */
	if (b)
		Lor_Map_Refresh(map);
}


/**
 ** Internal: OWNED
 **	Is the type of item in the list of market/possessions
 **/
static int owned(poss_s *p, item_s *t)
{
	while (p) {
		if (p->kind == t)
			return 1;
		p = p->next;
	}
	return 0;
}


/**
 ** Internal: HIGHLIGHT_RACES
 **	Show all the areas with the relevant people
 **/
static void highlight_races(X11_widget ignore, void *type)
{
area_s	*a;
int	i, j;
char	h, u;
/*
 * Loop on all areas
 */
	u = 0;
	for (i = 1; i < MAX_MAP; i++)
		for (j = 1; j < MAX_MAP; j++) {
			a = &lorenai_map[i][j];
			if (owned(a->sold, type))
				h = 1;
			else
				h = 0;
			if (h != a->highlighted) {
				a->highlighted   = h;
				a->update_needed = 1;
				u = 1;
			}
		}
/*
 * Any refresh?
 */
	if (u)
		Lor_Map_Refresh(map);
}


/**
 ** Internal: HIGHLIGHT_ITEMS
 **	Show all the areas with the relevant item
 **/
static void highlight_items(X11_widget ignore, void *type)
{
area_s	*a;
int	i, j;
char	h, u;
/*
 * Loop on all areas
 */
	u = 0;
	for (i = 1; i < MAX_MAP; i++)
		for (j = 1; j < MAX_MAP; j++) {
			a = &lorenai_map[i][j];
			if (owned(a->wanted, type))
				h = 2;
			else
				if (owned(a->sold, type) ||
				    owned(a->products, type))
					h = 1;
				else
					h = 0;
			if (h != a->highlighted) {
				a->highlighted   = h;
				a->update_needed = 1;
				u = 1;
			}
		}
/*
 * Any refresh?
 */
	if (u)
		Lor_Map_Refresh(map);
}


/**
 ** Internal: LIST_MENS
 **	List all the races in a menu
 **/
static void lists_mens(X11_widget menu, X11_callback item_call)
{
item_s	*i;
/*
 * Numbered
 */
	for (i = known_items; i; i = i->next)
		if (i->man && !i->leader)
			X11_MenuItem(menu, "list", i->singular, 0, item_call, i);
}


/**
 ** Internal: LIST_ITEMS
 **	List all the items in a menu
 **/
static void lists_items(X11_widget menu, X11_callback item_call)
{
item_s	*i;
/*
 * Numbered
 */
	for (i = known_items; i; i = i->next)
		if (!i->man)
			X11_MenuItem(menu, "list", i->singular, 0, item_call, i);
}


/**
 ** MAKE_UI
 **	Build the UI for the tool
 **/
void make_ui(void)
{
X11_widget	shell;
X11_widget	form, bar, menu;
/*
 * Create the shell
 */
	shell = X11_Shell("map", "Lorenai Tool 0.3", 0);
	form  = X11_RowColumn(shell, "panel");
/*
 * Create the Menu bar
 */
	bar  = X11_MenuBar(form);
	menu = X11_SubMenuItem(bar, "file", "File", 'F');
	X11_MenuItem(menu, "exit", "Exit (with save)", 'E', quit_tool, menu);
	X11_MenuItem(menu, "quit", "Quit", 'Q', quit_tool, 0);
	menu = X11_SubMenuItem(bar, "scale", "Scale", 'S');
	X11_MenuItem(menu, "world", "Worldview", 'W', 0, 0);
	menu = X11_SubMenuItem(bar, "select", "Highlight", 'H');
	X11_MenuItem(menu, "none", "None", 'N', unhighlight, 0);
	X11_Separator(menu);
	h_market  = X11_SubMenuItem(menu, "markets", "Markets", 'M');
	lists_items(h_market, highlight_items);
	h_recruit = X11_SubMenuItem(menu, "recruits", "Recruits", 'R');
	lists_mens(h_recruit, highlight_races);
	X11_MenuItem(bar,  "about", "About", 'A', 0, 0);
/*
 * Scrolling map
 */
	map = Lor_map(form);
/*
 * Coordinates
 */
	X11_FormLabel(X11_Label(form, "openp", "("));
	col = X11_Label(form, "column", "0");
	X11_FormLabel(col);
	X11_FormLabel(X11_Label(form, "comma", ","));
	row = X11_Label(form, "row", "0");
	X11_FormLabel(row);
	X11_Label(form, "closep", ")");
/*
 * Done
 */
	X11_Manage(shell);
}
