#include MUT_H
#include MPH_H
#include "mbk_extern.h"
#include "v_view.h"
#include "v_menu.h"
#include "g_visu.h"
#include "g_extern.c"
#include "v_extern.h"

extern LAMBDA xab2fig, yab2fig;
extern int C_atomicValue;
/* highlight the new reference instance */
void V_drawInsRef ()
{
static phfig_list *fig;
static int xold, yold;
static unsigned int dxold, dyold;
int x, y;
unsigned int dx, dy;

	if (C_atomicValue == 1)
		return;
	if (xvisufig == NULL)
		return;
	if (ptinsref == NULL) {
		dxold = 0;
		dyold = 0;
		return;
	}
	/* unhighlight the previous one */
	if (fig == xvisufig && dxold != 0 && dyold != 0 && ptinsref != NULL) {
		x = xold, y = yold, dx = dxold, dy = dyold;
		if (clip_to_window(&x, &y, &dx, &dy,
				xscreen, yscreen, dxscreen, dyscreen)) {
			dx = (unsigned int)(dx * scale);
			dy = (unsigned int)(dy * scale);
			x = (int)((x - (int)xscreen) * scale);
			y = (int)(YMENU - 1 + (int)((yscreen - y + dyscreen) * scale));
			XDrawRectangle(display, V_window, V_gcCao[GC_INSTANCE], x, y, dx, dy);
		}
	}
	/* retrieves the new values for overwrite purposes */
	fig = xvisufig;
	x = xold = ptinsref->X;
	y = yold = ptinsref->Y;
	dx = dxold = (unsigned int)ptinsref->DX;
	dy = dyold = (unsigned int)ptinsref->DY;
	if (clip_to_window(&x, &y, &dx, &dy, xscreen, yscreen, dxscreen, dyscreen)) {
		dx = (unsigned int)(dx * scale);
		dy = (unsigned int)(dy * scale);
		x = (int)((x - (int)xscreen) * scale);
		y = (int)(YMENU - 1 + (int)((yscreen - y + dyscreen) * scale));
		if (dx == 0 && dy == 0)
			return;
		XSetFillStyle(display, V_gcCao[GC_CONT_POLY], FillSolid);
		XDrawRectangle (display, V_window, V_gcCao[GC_CONT_POLY], x, y, dx, dy);
		XSetFillStyle(display, V_gcCao[GC_CONT_POLY], FillStippled);
		if (scale > .8 && ((C_Config_List.C_VISIBLE >> GC_DEFAULT) & 1) == 1)
			put_str(ptinsref, V_window);
	}
}

/* puts the abutment box around the figure */
void V_drawAB()
{
register int x, y;
register unsigned int dx, dy;
long xt, yt, dxt, dyt; /* lambdas */
extern char run_mode;
unsigned char flag;

	if (C_atomicValue == 1 || run_mode == RUN)
		return;
	xt = xabfig;
	dxt = xab2fig - xabfig;
	dyt = yab2fig - yabfig;
	yt = yab2fig;

	clip_to_window(&xt, &yt, &dxt, &dyt, xscreen, yscreen, dxscreen, dyscreen);

	x = (int)((xt - (int)xscreen) * scale);
	dx = (unsigned int)(dxt * scale);
	dy = (unsigned int)(dyt * scale);
/*
	y = (int)(YMENU - 1 + (int)((yscreen - yt + dyscreen) * scale)) - dy;
*/
	y = (int)(YMENU - 1 + (int)((yscreen - yt + dyscreen) * scale));
	if (dx < 3 || dy < 3)
		return;
	XDrawRectangle(display, V_window, V_gcCao[GC_DEFAULT], x, y, dx, dy);
/*
	XDrawRectangle(display, V_window, V_gcCao[GC_DEFAULT], (int)(x + 1), 
						(int)(y + 1), (unsigned int)(dx - 2), 
						(unsigned int)(dy - 2));
*/
}
