/*
 * * Generated by X-Designer
 */
/*
 * *LIBS: -lXm -lXt -lX11
 */

#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>

#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/Form.h>
#include <Xm/PushB.h>
#include <Xm/AtomMgr.h>
#include <X11/Xmu/StdSel.h>
#include <X11/Xfuncs.h>
#include <stdio.h>

extern void     activate_callback();
Widget          appshell = (Widget) NULL;
Widget          form = (Widget) NULL;
Widget          button = (Widget) NULL;

void            request_targets();
void            request_callback();

void 
activate_callback(w, client_data, cd)
	Widget          w;
	XtPointer       client_data;
	XtPointer       cd;
{
	XmAnyCallbackStruct *call_data = (XmAnyCallbackStruct *) cd;
	Atom            TARGETS = XmInternAtom(XtDisplay(w), "TARGETS", False);
	XtGetSelectionValue(w, XA_PRIMARY, TARGETS, request_targets,
			    (XtPointer) call_data->event->xbutton.time,
			    call_data->event->xbutton.time);
}

void
request_targets(w, client_data, selection, type, value, length, format)
	Widget          w;
	XtPointer       client_data;
	Atom           *selection, *type;
	XtPointer       value;
	unsigned long  *length;
	int            *format;
{
	Time            event_time = (Time) client_data;
	Atom            COMPOUND_TEXT = XmInternAtom(XtDisplay(w),
						     "COMPOUND_TEXT", False);

	if (value) {
		int             i;
		Atom           *targets = (Atom *) value;

		for (i = 0; i < *length; i++)
			if (targets[i] == COMPOUND_TEXT) {
				XtFree((char *) value);
				XtGetSelectionValue(w, XA_PRIMARY, COMPOUND_TEXT,
					request_callback, NULL, event_time);
				return;
			}
	}
	XtFree((char *) value);
	/* Fallback position */
	XtGetSelectionValue(w, XA_PRIMARY, XA_STRING, request_callback,
			    NULL, event_time);
}

void
request_callback(w, client_data, selection, type, value, length, format)
	Widget          w;
	XtPointer       client_data;
	Atom           *selection, *type;
	XtPointer       value;
	unsigned long  *length;
	int            *format;
{
	Atom            COMPOUND_TEXT = XmInternAtom(XtDisplay(w),
						     "COMPOUND_TEXT", False);

	if (!value)
		return;

	if (*type == XA_STRING) {
		XTextProperty   tmp_prop;
		char          **tmp_value;
		int             num_vals;
		int             i, status;

		tmp_prop.value = (unsigned char *) value;
		tmp_prop.encoding = *type;
		tmp_prop.format = *format;
		tmp_prop.nitems = *length;
		num_vals = 0;
		status = XmbTextPropertyToTextList(XtDisplay(w), &tmp_prop,
						   &tmp_value, &num_vals);
		if (num_vals && (status == Success || status > 0)) {
			XmString        s1, s2, s3;
			XmString        sep = XmStringSeparatorCreate();

			s1 = NULL;
			for (i = 0; i < num_vals; i++) {
				s2 = XmStringCreateLtoR(tmp_value[i],
						    XmFONTLIST_DEFAULT_TAG);
				if (s1) {
					s3 = XmStringConcat(s1, sep);
					XmStringFree(s1);
					s1 = XmStringConcat(s3, s2);
					XmStringFree(s3);
					XmStringFree(s2);
				} else
					s1 = s2;
			}
			XmStringFree(sep);
			XFreeStringList(tmp_value);
			XtVaSetValues(w, XmNlabelString, s1, 0);
			XmStringFree(s1);
		} else
			return;
	}
	if (*type == COMPOUND_TEXT) {
		XmString        s = XmCvtCTToXmString((char *) value);
		XtVaSetValues(w, XmNlabelString, s, 0);
		XmStringFree(s);
	}
	XtFree((char *) value);
}
void 
create_appshell(display, app_name, app_argc, app_argv)
	Display        *display;
	char           *app_name;
	int             app_argc;
	char          **app_argv;
{
	Arg             al[64];	/* Arg List */
	register int    ac = 0;	/* Arg Count */
	XmString        xmstring;

	XtSetArg(al[ac], XmNallowShellResize, TRUE); ac++;
	XtSetArg(al[ac], XmNtitle, "Importing Primary Selection"); ac++;
	XtSetArg(al[ac], XmNargc, app_argc); ac++;
	XtSetArg(al[ac], XmNargv, app_argv); ac++;
	appshell = XtAppCreateShell(app_name, "XApplication", applicationShellWidgetClass, display, al, ac);
	ac = 0;
	XtSetArg(al[ac], XmNautoUnmanage, FALSE); ac++;
	form = XmCreateForm(appshell, "form", al, ac);
	ac = 0;
	xmstring = XmStringCreateLtoR("Select some text somewhere, then click here", (XmStringCharSet) XmFONTLIST_DEFAULT_TAG);
	XtSetArg(al[ac], XmNlabelString, xmstring); ac++;

	button = XmCreatePushButton(form, "button", al, ac);
	XtAddCallback(button, XmNactivateCallback, activate_callback, NULL);

	ac = 0;
	XmStringFree(xmstring);

	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
	XtSetValues(button, al, ac);

	XtManageChild(button);
	XtManageChild(form);
}

XtAppContext    app_context;
Display        *display;	/* Display             */

int 
main(argc, argv)
	int             argc;
	char          **argv;
{
	XtSetLanguageProc((XtAppContext) NULL, (XtLanguageProc) NULL, (XtPointer) NULL);
	XtToolkitInitialize();
	app_context = XtCreateApplicationContext();
	display = XtOpenDisplay(app_context, NULL, argv[0], "XApplication",
				NULL, 0, &argc, argv);
	if (!display) {
		printf("%s: can't open display, exiting...\n", argv[0]);
		exit(-1);
	}
	create_appshell(display, argv[0], argc, argv);
	XtRealizeWidget(appshell);
	
{
    static XtWidgetGeometry Expected[] = {
   CWWidth | CWHeight            ,   50,   50,  270,   25, 0,0,0, /* form */
   CWWidth | CWHeight | CWX | CWY,    0,    0,  270,   25, 0,0,0, /* button */ 
    };
    PrintDetails(appshell,Expected);
};
	LessTifTestMainLoop(appshell);
	exit(0);
}
