/* An Xt application using the Athena (Xaw) widget set. */
#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Command.h>

static unsigned int clicks;
static Widget status;

static void
update_status(void)
{
    char text[64];
    snprintf(text, sizeof text, "Clicks: %u", clicks);
    XtVaSetValues(status, XtNlabel, text, NULL);
}

static void
count_click(Widget widget, XtPointer data, XtPointer call)
{
    (void)widget; (void)data; (void)call;
    ++clicks;
    update_status();
}

static void
reset_count(Widget widget, XtPointer data, XtPointer call)
{
    (void)widget; (void)data; (void)call;
    clicks = 0;
    update_status();
}

static void
quit(Widget widget, XtPointer data, XtPointer call)
{
    (void)data; (void)call;
    XtAppSetExitFlag(XtWidgetToApplicationContext(widget));
}

static Atom wm_delete;

static void
close_window(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch)
{
    (void)data; (void)dispatch;
    if (event->type == ClientMessage &&
        event->xclient.message_type == XInternAtom(XtDisplay(widget), "WM_PROTOCOLS", False) &&
        (Atom)event->xclient.data.l[0] == wm_delete)
        quit(widget, NULL, NULL);
}

int
main(int argc, char **argv)
{
    XtAppContext app;
    Widget shell, box, buttons, button;
    Display *display;
    String fallback[] = {
        "*title: Xt and Athena",
        "*font: 9x15",
        "*background: #e8e8e8",
        "*foreground: #202020",
        "*box.orientation: vertical",
        "*box.hSpace: 14",
        "*box.vSpace: 12",
        "*Label.borderWidth: 0",
        "*Label.width: 310",
        "*Label.resize: False",
        "*buttons.width: 310",
        "*Command.width: 94",
        "*Command.height: 32",
        NULL
    };

    XtSetLanguageProc(NULL, NULL, NULL);
    shell = XtVaAppInitialize(&app, "AthenaCounter", NULL, 0,
                             &argc, argv, fallback, NULL);
    box = XtVaCreateManagedWidget("box", boxWidgetClass, shell, NULL);
    XtVaCreateManagedWidget("heading", labelWidgetClass, box,
                            XtNlabel, "Xt framework / Athena widgets", NULL);
    status = XtVaCreateManagedWidget("status", labelWidgetClass, box,
                                    XtNlabel, "Clicks: 0", NULL);
    buttons = XtVaCreateManagedWidget("buttons", boxWidgetClass, box,
                                     XtNborderWidth, 0, NULL);
    button = XtVaCreateManagedWidget("Count", commandWidgetClass, buttons, NULL);
    XtAddCallback(button, XtNcallback, count_click, NULL);
    button = XtVaCreateManagedWidget("Reset", commandWidgetClass, buttons, NULL);
    XtAddCallback(button, XtNcallback, reset_count, NULL);
    button = XtVaCreateManagedWidget("Quit", commandWidgetClass, buttons, NULL);
    XtAddCallback(button, XtNcallback, quit, NULL);

    display = XtDisplay(shell);
    XtRealizeWidget(shell);
    wm_delete = XInternAtom(display, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(display, XtWindow(shell), &wm_delete, 1);
    XtAddEventHandler(shell, NoEventMask, True, close_window, NULL);
    XtAppMainLoop(app);
    XtDestroyWidget(shell);
    XtCloseDisplay(display);
    XtDestroyApplicationContext(app);
    return 0;
}
