/*  Include files  */

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xfwf/Board.h>
#include <Xfwf/Button.h>

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

static String fallback_resources[] = {
    "ButtonT.width: 300",
    "ButtonT.height: 350",
    "*1.location: 40 40 1.0-80 0.3333-40",
    "*2.location: 40 0.3333+20 1.0-80 0.3333-40",
    "*3.location: 40 0.6667 1.0-80 0.3333-40",
    "*3.label: Click here\\nto stop",
    "*XfwfButton.borderWidth: 0",
    "*XfwfButton.frameWidth: 2",
    "*XfwfButton.alignment: bottom right",
    NULL,
};

static Widget toplevel, board, button1, button2, button3;
static XtAppContext app_context;

static count1 = 0, count2 = 0;


/*
 *  Callback
 */
/* ARGSUSED */
static void activate(w, client_data, call_data)
    Widget w;
    XtPointer client_data, call_data;
{
    char *label;

    label = XtMalloc(10);
    if (w == button1) {
	(void) sprintf(label, "# clicks: %d", ++count1);
	XtVaSetValues(w, XtNlabel, label, NULL);
    } else {
	(void) sprintf(label, "# clicks: %d", ++count2);
	XtVaSetValues(w, XtNlabel, label, NULL);
    }
}

/*
 *  Callback
 */
/* ARGSUSED */
static void quit(w, client_data, call_data)
    Widget w;
    XtPointer client_data, call_data;
{
    exit(0);
}

/*
 *  Main program
 */
void main(argc, argv)
    Cardinal argc;
    String *argv;
{
    toplevel = XtVaAppInitialize(&app_context, "ButtonT", NULL, 0,
				  &argc, argv, fallback_resources, NULL);
    board = XtVaCreateManagedWidget("board", xfwfBoardWidgetClass, toplevel,
				      NULL);
    button1 = XtVaCreateManagedWidget("1", xfwfButtonWidgetClass, board,
				      XtNlabel, "# clicks: 0",
				      NULL);
    button2 = XtVaCreateManagedWidget("2", xfwfButtonWidgetClass, board,
				      XtNlabel, "#clicks: 0",
				      NULL);
    button3 = XtVaCreateManagedWidget("3", xfwfButtonWidgetClass, board,
				      NULL);
    XtRealizeWidget(toplevel);
    XtAddCallback(button1, XtNactivate, activate, NULL);
    XtAddCallback(button2, XtNactivate, activate, NULL);
    XtAddCallback(button3, XtNactivate, quit, NULL);
    XtAppMainLoop(app_context);
}
