/*
 * 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.
 */
/****************************************************************
 * File: import.c
 * Date: 02/28/91
 *
 * Description:
 *   This file contains the code for the file browser used for
 *   selecting import objects within VNS.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <Xm/Xm.h>
#include <X11/Shell.h>
#include <Xm/FileSB.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "VtP.h"

typedef struct {
	Widget pop ;
	Widget fspec ;
	int (*cb)() ;
	char *args ;
} *Prop ;

extern char *get_xm_string();

PRIVATE
void
sel_ok(wd,prop)
	Widget wd ;
	Prop prop ;
{
	struct stat st ;
	XmString xmfname ;
	char *fname ;

	XtVaGetValues(prop->pop,
		XmNtextString,&xmfname,
		NULL) ;
	fname = get_xm_string(xmfname) ;
	if (stat(fname,&st) < 0)
	{
		return ;
	}
	if (!(st.st_mode & S_IFDIR))
	{
		VtWatchOn(prop->pop);
		prop->cb(prop->args,fname) ;
		VtWatchOff(prop->pop);
		XtDestroyWidget(prop->pop) ;
		return ;
	}
	chdir(fname) ;
	XmFileSelectionDoSearch(prop->pop,NULL) ;
}

/*ARGSUSED*/
PRIVATE
void
sel_cancel(wd,prop)
	Widget wd ;
	Prop prop ;
{
	XtDestroyWidget(prop->pop) ;
}

/*ARGSUSED*/
PRIVATE
void
destroy(wd,prop)
	Widget wd ;
	Prop prop ;
{
	free((char *)prop) ;
}

PUBLIC
void
VtFileBrowser(parent,cb,args)
	Widget parent ;
	int (*cb)() ;
	char *args ;
{
	Prop prop = (Prop)malloc(sizeof *prop) ;

	Widget pop = XmCreateFileSelectionDialog(parent,"File Import",NULL,0);
	XtAddCallback(pop,XmNdestroyCallback,destroy,(XtPointer)prop) ;
	XtAddCallback(pop,XmNokCallback,sel_ok,(XtPointer)prop) ;
	XtAddCallback(pop,XmNcancelCallback,sel_cancel,(XtPointer)prop) ;

	prop->pop = pop;
	prop->cb = cb;
	prop->args = args;

	XtManageChild(pop);
}
