/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
#include <stdio.h>
#include <X11/StringDefs.h>
#include <X11/IntrinsicP.h>
#include <malloc.h>

#include "VnsP.h"

PUBLIC
void
vns_watch_on(vns)
	VnsContext *vns;
{
	VnsPage *pl = vns->first_page;

	/* activate watch for the command panel */
	VtWatchOn(vns->toplevel);

	/* activate watch for the property manager */
	VnsPropWatchOn();

	/* activate the watch for all displayed pages */
	while (pl != NULL)
	{
		VtWatchOn(pl->pwd);
		pl = pl->npage;
	}
}

PUBLIC
void
vns_watch_off(vns)
	VnsContext *vns;
{
	VnsPage *pl = vns->first_page;

	/* deactivate watch for the command panel */
	VtWatchOff(vns->toplevel);

	/* deactivate watch for the property manager */
	VnsPropWatchOff();

	/* deactivate the watch for all displayed pages */
	while (pl != NULL)
	{
		VtWatchOff(pl->pwd);
		pl = pl->npage;
	}
}

PUBLIC
XFontStruct *
alloc_named_font(vns,name)
	VnsContext *vns;
	char *name;
{
	XFontStruct *f;
	char *font;

	if (strcmp(name,"-*-*-*-*-*-*-*-*-*-*-*-*-*-*") == 0)
	{
		if (vns->default_font == NULL)
		{
			font = name;
		}
		else
		{
			font = vns->default_font;
		}
	}
	else
	{
		font = name;
	}

	f = XLoadQueryFont(vns->dpy,font);
	if (f == NULL)
	{
		f = XLoadQueryFont(vns->dpy,"variable");
		if (f == NULL)
		{
			f = XLoadQueryFont(vns->dpy,"fixed");
			if (f == NULL)
			{
				fprintf(stderr,"Error(alloc_named_font): unable to allocate font\n");
				exit(-1);
			}
		}
	}
	return f;
}

PUBLIC
void
check_background(dpy,back,fore)
	Display *dpy;
	unsigned long back;
	unsigned long *fore;  /* returned */
{
	if (DefaultDepth(dpy,DefaultScreen(dpy)) == 1)
	{
		if (back == 1)
		{
			*fore = 0;
		}
		else
		{
			*fore = 1;
		}
	}
}

PUBLIC
unsigned long
VnsAllocNamedColor(vns,name)
	VnsContext *vns;
	char *name;
{
	XColor col;
	XColor exact;

	if (!XAllocNamedColor(vns->dpy,DefaultColormap(vns->dpy,DefaultScreen(vns->dpy)),
			name,&col,&exact))
	{
		return BlackPixel(vns->dpy,DefaultScreen(vns->dpy));
	}
	return col.pixel;
}
