/*
 * 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 <varargs.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "wlib.h"

/*
 *		Creates a pixmap suitable for the display from
 *		bitmap data.
 */
Pixmap
create_pixmap_from_bitmap_data(display,data,width,height)
	Display *display ;
	char *data ;
{
	int screen = DefaultScreen(display) ;
	Window window = RootWindow(display,screen) ;
	unsigned long white = WhitePixel(display,screen) ;
	unsigned long black = BlackPixel(display,screen) ;
	unsigned depth = DefaultDepth(display,screen) ;
	return XCreatePixmapFromBitmapData(display,window,
		data,width,height,black,white,depth) ;
}

/*
 *		Creates an image suitable for the display from bitmap data.
 */
XImage *
create_image_from_bitmap_data(display,data,width,height)
	Display *display ;
	char *data ;
{
	XImage *image = XCreateImage(display,(Visual *)CopyFromParent,1,
		XYBitmap,0,data,(unsigned)width,(unsigned)height,8,0) ;
	image->byte_order = image->bitmap_bit_order = LSBFirst ;
	return image ;
}

/*
 *	Defines a cursor for a widget.
 */
define_cursor(w,cursor)
	Widget w ;
	Cursor cursor ;
{
	XDefineCursor(XtDisplay(w),XtWindow(w),cursor) ;
}

/*
 *	Creates a generic dragging GC.
 */
GC
make_gen_drag_gc(w)
	Widget w ;
{
	Display *dpy = XtDisplay(w) ;
	int screen = DefaultScreen(dpy) ;
	XGCValues values ;
	Pixel white = WhitePixel(dpy,screen) ;
	Pixel black = BlackPixel(dpy,screen) ;
	values.foreground = black ^ white ;
	values.background = white ;
	values.line_width = 0 ;
	values.function = GXxor ;
	values.plane_mask = values.foreground ;
	values.subwindow_mode = IncludeInferiors ;
	return XtGetGC(w,
		(unsigned long)GCForeground |
		(unsigned long)GCBackground |
		(unsigned long)GCLineWidth  |
		(unsigned long)GCSubwindowMode |
		(unsigned long)GCPlaneMask  |
		(unsigned long)GCFunction,
		&values);
}

/*
 *	Gets the string values of a widget.
 */
char *
get_sval(w)
	Widget w ;
{
	char *s ;
	XtVaGetValues(w,
		XtNstring,&s,
		NULL) ;
	return s ;
}

/*
 *	Sets the string values of a widget.
 */
set_sval(w,s)
	Widget w ;
	char *s ;
{
	XtVaSetValues(w,
		XtNstring,s,
		NULL) ;
}

/*
 *	Gets the label value of a widget.
 */
char *
get_lval(w)
	Widget w ;
{
	char *s ;
	XtVaGetValues(w,
		XtNlabel,&s,
		NULL) ;
	return s ;
}

/*
 *	Sets the label value of a widget.
 */
set_lval(w,s)
	Widget w ;
	char *s ;
{
	XtVaSetValues(w,
		XtNlabel,s,
		NULL) ;
}

/***********************************************************************/
/*VARARGS*/
void
ManageKids(va_alist)
/***********************************************************************/
	va_dcl
{
	Widget kids[100];
	va_list pvar;
	Widget w;
	int nkids = 0;

	va_start(pvar);
	while (w = (Widget) va_arg(pvar, Widget))
	{
		kids[nkids] = w;
		nkids++;
	}
	va_end(pvar);

	XtManageChildren(kids, nkids);
}
/***********************************************************************/
/*VARARGS*/
void
wprintf(w, format, va_alist)
/***********************************************************************/
	Widget w;
	char *format;
	va_dcl
{
	va_list args;
	char str[1000];

	va_start(args);
	(void) vsprintf(str, format, args);
	va_end(args);

	XtVaSetValues(w, XtNstring, str, NULL);
}
/*
 *  This version will let you specify what arg the resulting string is
 *  to be stored in.  (i.e. switch between XtNlabel and XtNstring).
 *
 */
/***********************************************************************/
/*VARARGS*/
void
wprintf2(w, argname, format, va_alist)
/***********************************************************************/
	Widget w;
	char *argname, *format;
	va_dcl
{
	va_list args;
	char str[1000];

	va_start(args);
	(void) vsprintf(str, format, args);
	va_end(args);

	XtVaSetValues(w, argname, str, NULL);
}

/*
 *	Finds Lowest window at point xy.  Returns x and y position relative
 *  to that window.
 */
Window
find_window_at_point(dpy,x,y,x1,y1)
	Display *dpy ;
	int *x1 ;
	int *y1 ;
{
    Window root = DefaultRootWindow(dpy) ;
    Window child = root ;
    Window w = root ;
    while(child != None)
    {
		XTranslateCoordinates(dpy,root,w = child,x,y,x1,y1,&child) ;
    }
    return w ;
}
