#include <Xm/DragDrop.h>
#include <Xm/MessageB.h>

extern void     transfer_bitmap();
void            import_bitmap();
extern Widget   appshell;

static Widget   help_dialog;
static Widget   help_drop_site;
static XmDropProcCallbackStruct help_drop_data;

void
drop_from_help(w, client_data, call_data)
	Widget          w;
	XtPointer       client_data;
	XtPointer       call_data;
{
	/* Proceed with drop - modify client_data and pass to dropProc */
	XmDropProcCallback drop_data = (XmDropProcCallback) client_data;
	drop_data->dropAction = XmDROP;
	XtDestroyWidget(help_dialog);
	import_bitmap(help_drop_site, NULL, client_data);
}

void
cancel_from_help(w, client_data, call_data)
	Widget          w;
	XtPointer       client_data;
	XtPointer       call_data;
{
	/* Cancel drop - modify client_data and pass to dropProc */
	XmDropProcCallback drop_data = (XmDropProcCallback) client_data;
	drop_data->dropAction = XmDROP_CANCEL;
	XtDestroyWidget(help_dialog);
	import_bitmap(help_drop_site, NULL, client_data);
}


void
import_bitmap(w, client_data, call_data)
	Widget          w;
	XtPointer       client_data, call_data;
{
	XmDropProcCallback drop_data;
	XmDropTransferEntryRec transfer_entries[1];
	Arg             al[5];
	int             ac;

	drop_data = (XmDropProcCallback) call_data;
	ac = 0;
	if (drop_data->dropAction == XmDROP_HELP) {
		XmString        s = XmStringCreateLocalized("Drop it here");
		XtSetArg(al[ac], XmNmessageString, s); ac++;
		help_dialog = XmCreateInformationDialog(appshell, "help_dialog", al,
							ac);
		help_drop_site = w;
		ac = 0;
		memcpy(&help_drop_data, drop_data, sizeof
		       (XmDropProcCallbackStruct));
		XtAddCallback(help_dialog, XmNokCallback, drop_from_help,
			      (XtPointer) & help_drop_data);
		XtAddCallback(help_dialog, XmNcancelCallback, cancel_from_help,
			      (XtPointer) & help_drop_data);
		XtManageChild(help_dialog);
		return;
	}
	if (drop_data->dropAction != XmDROP || drop_data->operation !=
	    XmDROP_COPY) {
		XtSetArg(al[ac], XmNtransferStatus, XmTRANSFER_FAILURE); ac++;
	} else {
		Atom           *export_list;
		int             export_count, i;
		Boolean         got_pixmap = False;
		Boolean         got_string = False;

		XtVaGetValues(drop_data->dragContext, XmNexportTargets,
			   &export_list, XmNnumExportTargets, &export_count,
			      0);
		for (i = 0; i < export_count; i++)
			if (export_list[i] == XA_STRING)
				got_string = True;
			else if (export_list[i] == XA_PIXMAP)
				got_pixmap = True;
		/* Favour pixmap over string */
		if (got_pixmap || got_string) {
			if (got_pixmap)
				transfer_entries[0].target = XA_PIXMAP;
			else
				transfer_entries[0].target = XA_STRING;
			transfer_entries[0].client_data = (XtPointer) w;
			XtSetArg(al[ac], XmNdropTransfers, transfer_entries); ac++;
			XtSetArg(al[ac], XmNnumDropTransfers, 1); ac++;
			XtSetArg(al[ac], XmNtransferProc, transfer_bitmap); ac++;
			drop_data->operation = XmDROP_COPY;
		} else {
			XtSetArg(al[ac], XmNtransferStatus, XmTRANSFER_FAILURE); ac++;
		}
	}
	XmDropTransferStart(drop_data->dragContext, al, ac);
}
