/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
#include <stdio.h>
#include <malloc.h>
#include <Xm/Xm.h>
#include <X11/Shell.h>
#include <Xm/SelectioB.h>

#include "VtP.h"

static char last_file[256];

typedef struct _Export
{
	Widget prompt;
	int (*cb)();
	char *args;
} Export;

extern char *get_xm_string();

static
void
sel_ok(wd,exp)
	Widget wd;
	Export *exp;
{
	XmString text;
	char *str;

	XtVaGetValues(exp->prompt,
		XmNtextString,&text,
		NULL);
	str = get_xm_string(text);

	if (str[0] != '.' && str[0] != '/')
	{
		sprintf(last_file,"%s/%s",getenv("HOME"),str);
	}
	else
	{
		sprintf(last_file,"%s",str);
	}

	exp->cb(exp->args,last_file);
	XtDestroyWidget(exp->prompt);
	return;
}

static
void
destroy(wd,exp)
	Widget wd;
	Export *exp;
{
	free((char *)exp);
}

PUBLIC
void
VtFileExport(parent,msg,cb,data)
	Widget parent;
	char *msg;
	int (*cb)();
	char *data;
{
	Export *exp = (Export *)malloc(sizeof(Export));
	Widget prompt, help, ok;
	XmString xm_str;
	Arg args[3];
	int n;

	xm_str = XmStringCreateLtoR("Export File:",XmSTRING_DEFAULT_CHARSET);

	n = 0;
	XtSetArg(args[n],XmNdialogStyle,XmDIALOG_APPLICATION_MODAL); n++;
	XtSetArg(args[n],XmNselectionLabelString,xm_str); n++;
	prompt = XmCreatePromptDialog(parent,"File Export",args,n);
	XtAddCallback(prompt,XmNdestroyCallback,destroy,(XtPointer)exp);

	XmStringFree(xm_str);

	help = XmSelectionBoxGetChild(prompt,XmDIALOG_HELP_BUTTON);
	XtUnmanageChild(help);

	ok = XmSelectionBoxGetChild(prompt,XmDIALOG_OK_BUTTON);
	XtAddCallback(ok,XmNactivateCallback,sel_ok,(XtPointer)exp);

	exp->prompt = prompt;
	exp->cb = cb;
	exp->args = data;

	XtManageChild(prompt);
}
