#include <stdio.h>
#include <X11/StringDefs.h>
#include <X11/IntrinsicP.h>
#include <X11/CoreP.h>
#include <Xm/Xm.h>

/* scales are currently handled as a special case of ScrollBar. */

int
xdsScrollBarNameToXy( widget, name, attr, xp, yp)
	Widget widget;
	char * name;
	char * attr;
	int  * xp;
	int * yp;
{
	Widget pa = XtParent( widget);

	if (xdsIsScale( pa))
		return xdsScaleNameToXy( pa, name, attr, xp, yp);

	return xdsScrollBarNameToXy1( widget, name, attr, xp, yp);
}

int
xdsScrollBarXyToName( widget, x, y, namep, attrp)
	Widget widget;
	int x, y;
	char ** namep;
	char ** attrp;
{
	Widget pa = XtParent( widget);

	if (xdsIsScale( pa))
		return xdsScaleXyToName( pa, x, y, namep, attrp);

	return xdsScrollBarXyToName1( widget, x, y, namep, attrp);
}

int
xdsScrollBarNameToXy1( widget, name, attr, xp, yp)
	Widget widget;
	char * name;
	char * attr;
	int  * xp;
	int * yp;
{
	int value;
	int slider_size;
	int increment;
	int page_increment;

	*xp = -1;
	*yp = -1; /* dont send events */

	XmScrollBarGetValues( widget, &value, &slider_size, &increment,
				&page_increment);

	value = atoi( attr);

	XmScrollBarSetValues( widget, value, slider_size, increment,
				page_increment, True);

	return 1;
}


int
xdsScrollBarXyToName1( widget, x, y, namep, attrp)
	Widget widget;
	int x, y;
	char ** namep;
	char ** attrp;
{
	Arg args[2];
	Cardinal ac;
	int value;
	static char val[20];

	char * s = XtName( widget);

	if (s && *s && (strcmp(s, "Scrollbar") == 0)) {
		if (xdsIsScaleChild( widget))
			return xdsScaleXyToName( XtParent(widget), x, y, namep,attrp);
	}

	ac = 0;

	XtSetArg( args[ac], XmNvalue, &value); ac++;
	XtGetValues( widget, args, ac);

	(void) sprintf ( val, "%d", value);

	*namep = "value";
	*attrp = val;

	return 1;
}

int
xdsIsScaleChild( w)
	Widget w;
{
	if (!(w = XtParent(w)))
		return 0;
	return xdsIsScale(w);
}

int
xdsIsScale( w)
	Widget w;
{
	return xdsIsType( w, "XmScale");
}

int
xdsIsType( w, type)
	Widget w;
	char * type;
{
	CorePart *  cp;
	WidgetClass wc;
	CoreClassPart * cdata;

	cp = &(w->core);
	wc = cp->widget_class;
	cdata = &(wc->core_class);

	return (strcmp(cdata->class_name, type) == 0);
}


int
xdsScaleNameToXy( widget, name, attr, xp, yp)
	Widget widget;
	char * name;
	char * attr;
	int  * xp;
	int * yp;
{
	int value;
	int slider_size;
	int increment;
	int page_increment;

	*xp = -1;
	*yp = -1; /* dont send events */

	value = atoi( attr);

	XmScaleSetValue( widget, value);

	return 1;
}


int
xdsScaleXyToName( widget, x, y, namep, attrp)
	Widget widget;
	int x, y;
	char ** namep;
	char ** attrp;
{
	Arg args[2];
	Cardinal ac;
	int value;
	static char val[20];

	ac = 0;

	XtSetArg( args[ac], XmNvalue, &value); ac++;
	XtGetValues( widget, args, ac);

	(void) sprintf ( val, "%d", value);

	*namep = "value";
	*attrp = val;

	return 1;
}


int
xdsDaNameToXy( widget, name, attr, xp, yp)
	Widget widget;
	char * name;
	char * attr;
	int  * xp;
	int * yp;
{
	int value;
	int slider_size;
	int increment;
	int page_increment;

	if (!isdigit(*name))
		return 0;
	if (!isdigit(*attr))
		return 0;

	*xp = atoi(name);

	*yp = atoi(attr);

	return 1;
}


int
xdsDaXyToName( widget, x, y, namep, attrp)
	Widget widget;
	int x, y;
	char ** namep;
	char ** attrp;
{
	Arg args[2];
	Cardinal ac;
	int value;
	static char xs[8];
	static char ys[8];

	ac = 0;
	(void) sprintf ( xs, "%d", x);
	(void) sprintf ( ys, "%d", y);

	*namep = xs;
	*attrp = ys;

	return 1;
}
