/*
 * 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.
 */
/****************************************************************
 * File: color.c
 * Date: 04/02/91
 *
 * Description:
 *   This file contains several functions for handling color in X.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <Xm/Xm.h>
#include <X11/StringDefs.h>
#include <X11/IntrinsicP.h>
#include <Xm/ScrolledW.h>
#include <Xm/Label.h>
#include <VList.h>
#include <Color.h>

#include "VtP.h"

static char *colors[] = {
	"red",
	"orange",
	"yellow",
	"green",
	"blue",
	"cyan",
	"magenta",
	"hot pink",
	"purple",
	"grey",
	"white",
	"black",
};

/****************************************************************
 *                     Public Functions
 ****************************************************************/

/****************************************************************
 * Function: VtColorPanel  
 * Date: 04/02/91
 *
 * Description:
 *   This function creates a color selection panel using the
 *   Color widget contained in the Vw library.
 *
 * Linkage: Widget VtColorPanel(parent,callback)
 *   Widget parent           - the parent for the color panel
 *   XtCallbackProc callback - color selection callback
 *
 * Notes:
 * - The callback is the same format as XtCallbackProc as described
 *   in the Xt reference manual.
 * - The color name is also used as the name of the widget.
 *
 * Revisions:
 ****************************************************************/
PUBLIC
Widget
VtColorPanel(parent,callback)
	Widget parent;
	XtCallbackProc callback;
{
	XmString xm_str;
	Widget sw, frm;
	int i;

	sw = XtVaCreateWidget("ColorViewport",xmScrolledWindowWidgetClass,parent,
		XmNscrollingPolicy,XmAUTOMATIC,
		XmNvisualPolicy,XmVARIABLE,
		XtNstretchHoriz,True,
		XtNstretchVert,True,
		NULL);
	frm = XtVaCreateManagedWidget("Colors",vListWidgetClass,sw,
		XtNborderWidth,0,
		NULL);
	xm_str = XmStringCreateLtoR("Color Selection",XmSTRING_DEFAULT_CHARSET);
	XtVaCreateManagedWidget("ColorLabel",xmLabelWidgetClass,frm,
		XmNlabelString,xm_str,
		XtNhAlign,VL_CENTER,
		NULL);
	XmStringFree(xm_str);
	for(i = 0; i < XtNumber(colors); i++)
	{
		Widget wd;
		wd = XtVaCreateManagedWidget(colors[i],colorWidgetClass,frm,
			XtNcolorName,colors[i],
			XtNwidth,135,
			XtNcolorSet,!strcmp("white",colors[i]),
			NULL);
		XtAddCallback(wd,XtNcolorSelect,callback,(XtPointer)NULL);
	}

	return(sw);
}

/****************************************************************
 * Function: VtSetBackColor
 * Date: 04/02/91
 *
 * Description:
 *   This function recursively sets the background color for a
 *   tree of widgets.
 *
 * Linkage: void VtSetBackColor(wd,col)
 *   Widget wd         - the top widget in the widget tree
 *   unsigned long col - background color value
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VtSetBackColor(wd,col)
	Widget wd;
	unsigned long col;
{
	XtVaSetValues(wd,
		XtNbackground,col,
		NULL);
	if (XtIsComposite(wd))
	{
		Widget *children = ((CompositeWidget)wd)->composite.children;
		Cardinal nchildren = ((CompositeWidget)wd)->composite.num_children;
		int i;
		for(i = 0; i < nchildren; i++)
		{
			VtSetBackColor(children[i],col);
		}
	}
}

/****************************************************************
 * Function: VtSetForeColor
 * Date: 04/02/91
 *
 * Description:
 *   This function recursively sets the foreground color for a
 *   tree of widgets.
 *
 * Linkage: void VtSetForeColor(wd,col)
 *   Widget wd         - the top widget in the widget tree
 *   unsigned long col - foreground color value
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VtSetForeColor(wd,col)
	Widget wd;
	unsigned long col;
{
	XtVaSetValues(wd,
		XtNforeground,col,
		NULL);
	if (XtIsComposite(wd))
	{
		Widget *children = ((CompositeWidget)wd)->composite.children;
		Cardinal nchildren = ((CompositeWidget)wd)->composite.num_children;
		int i;
		for(i = 0; i < nchildren; i++)
		{
			VtSetForeColor(children[i],col);
		}
	}
}
