/********************************************************************
 *																	*
 *	WinLIB PRO Revision II: Tools module							*
 *																	*
 *	Copyright (c) 1994, Bitgate Software							*
 *																	*
 ********************************************************************/

#include <aes.h>
#include <stdio.h>
#include <string.h>

#include "winlib.h"

#ifndef __TOOLS__
#define __TOOLS__
#endif

/*
 *	Check for intersection of rectangles r1 and r2.
 *	Intersecting rectangle is returned in r1.
 *
 *	Returns FALSE if no intersection
 */
GLOBAL int rc_intersect(GRECT *r1, GRECT *r2)
{
	r1->g_w = min(r1->g_x + r1->g_w, r2->g_x + r2->g_w );
	r1->g_h = min(r1->g_y + r1->g_h, r2->g_y + r2->g_h );
	r1->g_x = max(r1->g_x, r2->g_x);
	r1->g_y = max(r1->g_y, r2->g_y);
	r1->g_w -= r1->g_x;
	r1->g_h -= r1->g_y;
	return (r1->g_w > 0 && r1->g_h > 0) ? TRUE : FALSE;
}

GLOBAL int rc_inside(int x, int y, GRECT *r)
{
	return (x > r->g_x && x < (r->g_x + r->g_w) &&
			 y > r->g_y && y < (r->g_y + r->g_h));
}

/*
 *	Wait until left mousebutton is released
 *
 *	Returns: nothing
 */
GLOBAL void no_click(void)
{
	int dummy, bstate;

	graf_mkstate(&dummy, &dummy, &bstate, &dummy);
	if (bstate & 1)
		evnt_button(1, 3, 0, &dummy, &dummy, &dummy, &dummy);
}

/*
 *  Check to see if a window is topped
 *
 *  handle = window to check for
 *
 */
GLOBAL int WIsTopped(int handle)
{
	int x;

	wind_get(handle, WF_TOP, &x);
	return((x == handle) ? TRUE : FALSE);
}

/*
 *	Routine to find out if any windows are W_UNUNTOPPABLE
 *
 *	Returns FALSE if no handle, and a non-zero number of the window
 *				 handle that was found.
 */
GLOBAL int WFindUntoppables(void)
{
	WINDOW *win = WindowChain;

	while (win->next) {
		if (win->dominance == D_SWITCHABLE)
			return win->handle;

		win = win->next;
	}

	return FALSE;
}

/*
 *	Find if any windows are on the desktop
 *
 *	Returns TRUE if there are, FALSE if none are opened, skipping the
 *	desktop handle.
 */
GLOBAL BOOL WFindAnyOpen(void)
{
	WINDOW *win = WindowChain;

	while (win->next) {
		if ((win->state & W_OPEN) && !(win->state & W_DESKTOP))
			return TRUE;

		win = win->next;
	}

	return FALSE;
}

/*
 *	Find if a specific window is open (not for internal resources)
 *
 *	Returns TRUE if it's already open, FALSE if no windows match the
 *					given tree number.
 */
GLOBAL BOOL WFindWindow(int treenumber)
{
	WINDOW *win = WindowChain;

	while (win->next) {
		if (win->treenumber == treenumber)
			return TRUE;

		win = win->next;
	}

	return FALSE;
}

/*
 *	Routine to find the most dominant window
 *
 *	Returns the dominant window's handle, or FALSE for no window
 */
GLOBAL int WFindDominance(void)
{
	WINDOW *win = WindowChain;

	while (win->next) {
		if (win->dominance == D_ALWAYSTOP)
			return win->handle;

		win = win->next;
	}

	return FALSE;
}	

/*
 *	Change the text displayed inside an object
 *	Version 2.0 from XAES Pro
 *
 *	obj  = Address of the tree to modify
 *	idx  = Index of the object to change
 *	txt  = Pointer to text array to put in to the text
 *	fnt  = Font size
 *	just = Justification of text
 *
 *	This routine has been updated to make it "smarter", ie. it now
 *	has the ability to change text inside objects that have been
 *	previously modified under the fix_objects routine.  If you don't
 *	use this routine to change text inside a fixed object, you are
 *	most likely to bomb the system.  Use this routine if doubtful.
 */
GLOBAL void ChangeObjectText(OBJECT *obj, int idx, char *txt, int fnt, int just)
{
	switch(obj[idx].ob_type & 0xFF) {
		case G_USERDEF:
			{
				EXTINFO *exinf = (EXTINFO *)(obj[idx].ob_spec.userblk->ub_parm);

				exinf->te_ptext = txt;
				exinf->te_txtlen = (int) strlen(txt);
			}
			break;

		default:
			obj[idx].ob_spec.tedinfo->te_ptext = (char *) txt;
			obj[idx].ob_spec.tedinfo->te_txtlen = (int) strlen(txt);
			if (just>0)
				obj[idx].ob_spec.tedinfo->te_just = just;
			break;
	}
}

/*
 *	Change hotkey drawing level
 */
GLOBAL void WHotkeyLevel(int mode)
{
	hotkeylevel = mode;
}

/*
 *	Check for any speed-up screen routines
 */
GLOBAL BOOL WCheckSpeedup(void)
{
	if (find_cookie('NVDI'))
		return TRUE;

	return FALSE;
}