#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include "dsc_types.h"
#include "dsname.h"
#include <com_err.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xtlib.h>

char *strdup(), *calloc(), *malloc();
Display *display;
Window root;
char *whoami;

void select_mtg(p)
    caddr_t p;
{
    char *name;
    name = (char *)p;
    printf("select meeting %s\n", name);
}

Window big_w = NULL;

windows()
{
    (void) XSynchronize(display, 1);
    set_up_big_window();
}

unsigned int big_w_height, big_w_width;
Arg vpane_window_args[] = {
    { XtNwidth, (XtArgVal)NULL },
    { XtNheight, (XtArgVal)NULL }
};

static XtEventHandler
finish_map(event, data)
    XEvent *event;
    caddr_t data;
{
    int x, y, border_width, depth, root;
    XGetGeometry(display, big_w, &root, &x, &y, &big_w_width, &big_w_height,
		 &border_width, &depth);
    vpane_window_args[0].value = (XtArgVal) big_w_width;
    vpane_window_args[1].value = (XtArgVal) big_w_height;
    XtVPaneSetValues(display, big_w,
		     vpane_window_args, XtNumber(vpane_window_args));
    set_up_button_box();
    XtDeleteEventHandler(display, big_w, finish_map);
    printf("width %d, height %d\n", big_w_width, big_w_height);
}

set_up_big_window()
{
    static Arg big_w_args[] = {
	{ XtNname, (XtArgVal) "DISCUSS" }
    };
    big_w = XtVPanedWindowCreate(display, root,
				 big_w_args, XtNumber(big_w_args));
    if (!big_w) {
	com_err(whoami, 0, "Can't create window.");
	exit(1);
    }
    XtSetEventHandler(display, big_w, finish_map, ExposureMask, NULL);
    XMapWindow(display, big_w);
}

static Window box = (Window)NULL;

set_up_button_box()
{
    static Arg foo[] = {
	{ XtNwidth, (XtArgVal) NULL },
	{ XtNheight, (XtArgVal) NULL }
    };
    if (box)
	return;
    box = XtButtonBoxCreate(display, big_w, vpane_window_args, 2);
    if (!box) {
	com_err(whoami, 0, "Can't create button box.");
	exit(1);
    }
    XtVPanedWindowAddPane(display, big_w, box, 0,
			  big_w_height/2, big_w_height/2, 1);
    set_up_buttons();
    XtButtonBoxGetValues(display, box, foo, XtNumber(foo));
    printf("button box: width=%d, height=%d\n",
	   foo[0].value, foo[1].value);
    XtVPanedSetMinMax(display, big_w, box, 5, 600);
}

int foo();
set_up_buttons()
{
    Window w;
    name_blk *nbp;
    int code, n_mtgs, i;

    static Arg args[] = {
	{ XtNname, (XtArgVal) NULL },
	{ XtNfunction, (XtArgVal) select_mtg },
	{ XtNparameter, (XtArgVal) NULL }
    };
    static Arg bb_add_args[1];

    dsc_expand_mtg_set((char *)NULL, "*", &nbp, &n_mtgs, &code);
    if (code) {
	com_err(whoami, code, "Can't get list of meetings.");
	exit(1);
    }
    for (i = 0; i < n_mtgs; i++) {
	register char *name, **namep;
	name = *nbp[i].aliases;
	for (namep = nbp[i].aliases; namep[1] != (char *)NULL; namep++)
	    if (strlen(name) > strlen(*namep))
		name = *namep;
	XtSetArg(args[0], XtNname, name);
	XtSetArg(args[2], XtNparameter, name);
	w = XtCommandCreate(display, box, args, XtNumber(args));
	XtSetArg(bb_add_args[0], XtNwindow, w);
	XtButtonBoxAddButton(display, box, bb_add_args, 1);
    }
    foo();
}

Window dbox;

nuke_dbox(msg)
    char *msg;
{
    printf("%s\n", msg);
    printf("value was: %s\n", XtDialogGetValueString(display, dbox));
    XtVPanedWindowDeletePane(display, big_w, dbox);
    XtSendDestroyNotify(display, dbox);
    XDestroyWindow(display, dbox);
}

foo()
{
    static Arg foo[] = {
	{ XtNwidth, (XtArgVal) NULL },
	{ XtNheight, (XtArgVal) NULL }
    };

    dbox = XtDialogCreate(display, /*big_w*/root,
			  "dialog box description goes here",
			  "initial value for dialog box",
			  (Arg *)NULL, 0);
    XtDialogAddButton(display, dbox, "OK", nuke_dbox, "OK");
    XtDialogAddButton(display, dbox, "NO!", nuke_dbox, "NO!");
    XtFormGetValues(display, dbox, foo, XtNumber(foo));
    printf("dialog box: width=%d height=%d\n",
	   foo[0].value, foo[1].value);
    XtVPanedWindowAddPane(display, big_w, dbox, 1,
			  big_w_height/2, big_w_height/2, 1);
    XtVPanedSetMinMax(display, big_w, dbox, 5, 600);
    XMapWindow(display, dbox);
}
