/**
 *
 * test23.c
 *
 **/

#include <stdio.h>
#include <Xm/Xm.h>
#include <Xm/Label.h>
#include <Xm/FormP.h>

Boolean GlobalResult = True;

static String FallBack[] = {
	"*geometry: +500+500",
	"*borderWidth: 1",
NULL};

typedef struct {
		XtGeometryResult result;
		XtWidgetGeometry geo;
} QueryResult;

static char *
XdbBoolean2String(int b)
{
    if (b)
    {
	return "True";
    }

    return "False";
}

static char *
XdbGeometryResult2String(XtGeometryResult r)
{
    switch (r)
    {
    case XtGeometryYes:
	return "Yes";

    case XtGeometryNo:
	return "No";

    case XtGeometryAlmost:
	return "Almost";

    case XtGeometryDone:
	return "Done";

    default:
	return "(invalid geometry result)";
    }
}

static char *
XdbWidgetGeometry2String(XtWidgetGeometry *g)
{
    static char o1[128], o2[128], b[20], *out = NULL;
    int i;

    if (g == NULL)
    {
	return "NULL_GEOMETRY";
    }

    if (g->request_mode == 0)
    {
	return "GEOMETRY_NO_FIELDS";
    }

/* Some magic to ensure you can call this sucker twice in one C function call */
    if (out == &o1[0])
    {
	out = &o2[0];
    }
    else
    {
	out = &o1[0];
    }

    out[0] = '\0';
    if (g->request_mode & CWX)
    {
	sprintf(b, "x %d ", g->x);
	strcat(out, b);
    }
    if (g->request_mode & CWY)
    {
	sprintf(b, "y %d ", g->y);
	strcat(out, b);
    }
    if (g->request_mode & CWWidth)
    {
	sprintf(b, "w %d ", g->width);
	strcat(out, b);
    }
    if (g->request_mode & CWHeight)
    {
	sprintf(b, "h %d ", g->height);
	strcat(out, b);
    }
    if (g->request_mode & CWBorderWidth)
    {
	sprintf(b, "bw %d ", g->border_width);
	strcat(out, b);
    }

    for (i = 0; out[i]; i++)
    {
    }

    if (i > 0 && out[i - 1] == ' ')
    {
	out[i - 1] = '\0';
    }

    return out;
}

static Boolean 
MakeGeometry(Widget W, XtWidgetGeometry *request, QueryResult *Expected, XtWidgetGeometry *desired)
{
Boolean status = True;
XtWidgetGeometry OriginalRequest;
XtGeometryResult result;

	if (request)
		OriginalRequest = *request;

	printf("MakeGeometryRequest asked for (%s) ",
		XdbWidgetGeometry2String(request));

	result = XtMakeGeometryRequest(W, request, desired);

	printf("got (%s %s) ",
		XdbWidgetGeometry2String(desired),
		XdbGeometryResult2String(result));
	printf("expected (%s %s) ",
		XdbWidgetGeometry2String(&Expected->geo),
		XdbGeometryResult2String(Expected->result),
		XdbWidgetGeometry2String(request));
	/*
	printf("original return (%s) ",
		XdbWidgetGeometry2String(request));
		*/

	if ((desired->request_mode & Expected->geo.request_mode) == Expected->geo.request_mode &&
	    (!(Expected->geo.request_mode & CWWidth) || (desired->width  == Expected->geo.width))  &&
	    (!(Expected->geo.request_mode & CWHeight) || (desired->height == Expected->geo.height)) &&
	    result == Expected->result)
	{
		printf("Passed");
	}
	else
	{
		printf("Failed");
		status = False;
	}
	printf("\n");
	return(status);
}

static void DoTests(Widget Form)
{
XtWidgetGeometry Preferred;
XtWidgetGeometry desired;

	XtQueryGeometry(Form, NULL, &Preferred);
	printf("Preferred label size (%s)\n", XdbWidgetGeometry2String(&Preferred));
	{ /* Propose what it wants */
	QueryResult Expect;

		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryYes;

		GlobalResult &= MakeGeometry(Form, &Preferred, &Expect, &desired);
	}
	{ /* Propose bigger width */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width += 1;

		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryNo;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose bigger width */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width += 2;

		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryNo;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose bigger height */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.height += 1;

		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryNo;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose bigger height */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.height += 2;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryNo;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose bigger */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width += 1;
		Request.height += 1;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryNo;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose bigger */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width += 2;
		Request.height += 2;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryNo;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose smaller width */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width -= 1;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryYes;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose smaller width */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width -= 2;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryYes;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose smaller height */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.height -= 1;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryYes;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose smaller height */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.height -= 2;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryYes;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose smaller */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width -= 1;
		Request.height -= 1;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryYes;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
	{ /* Propose smaller */
	QueryResult Expect;
	XtWidgetGeometry Request = Preferred;

		Request.width -= 2;
		Request.height -= 2;

		Expect.geo.request_mode = CWHeight;
		Expect.geo.request_mode = XtCWQueryOnly;
		Expect.geo.width = Preferred.width;
		Expect.geo.height = 1;
		Expect.result = XtGeometryYes;

		GlobalResult &= MakeGeometry(Form, &Request, &Expect, &desired);
	}
}

int
main(int argc, char **argv)
{
  Widget toplevel;
  Widget Form;
  Widget label;
  XtAppContext app;

  XtSetLanguageProc(NULL, NULL, NULL);

  toplevel = XtVaAppInitialize(&app, "test23", NULL, 0, &argc, argv, FallBack, NULL);

  Form = XmCreateForm(toplevel, "TestWidget", NULL, 0);

  label = XmCreateLabel(Form, "TestLabelWidget", NULL, 0);

  XtManageChild(label);

  XtManageChild(Form);

  XtRealizeWidget(toplevel);

  LessTifTestWaitForIt(toplevel);
  DoTests(label);


{
XtWidgetGeometry Expected[] = {
   CWWidth | CWHeight            ,  508,  524,   96,   19, 0,0,0, /* TestWidget */
   CWWidth | CWHeight | CWX | CWY,    0,    0,   92,   15, 0,0,0, /* TestWidget */
};
/* toplevel should be replaced with to correct applicationShell */
PrintDetails(toplevel, Expected);
}
LessTifTestMainLoop(toplevel);

  printf("%s\n",GlobalResult ? "All Passed" : "One or more Failed");
      LessTifTestMainLoop(toplevel);

  exit(GlobalResult ? 0 : 1);
}
