/*
 * 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: goto_page.c
 * Date: 02/28/91
 *
 * Description:
 *   This file contains the functions for creating the goto page
 *   popup for VNS.
 *
 * Revisions:
 *   02/28/91 (BDM): Added Motif version
 ****************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <Xm/Xm.h>
#include <Xm/SelectioB.h>

#include "VtP.h"

typedef struct {
	Widget parent;         /* parent of popup */
	Widget name;           /* page number widget */
	Widget pop;            /* popup widget */
	int nid;               /* notebook id */
	SrvContext *srv;       /* server id */
	void (*cb)();          /* callback function */
	XtPointer data;        /* callback data */
} Goto_Context;

extern char *get_xm_string();

static
void
sel_ok(wd,gcont)
	Widget wd;
	Goto_Context *gcont;
{
	XmString text;
	extern char *get_xm_string();
	char *s;
	int pid;

	XtVaGetValues(gcont->pop,
		XmNtextString,&text,
		NULL);
	s = get_xm_string(text);
	pid = atoi(s);

	if (srv_validate_page(gcont->srv,gcont->nid,pid) == 1)
	{
		(*gcont->cb)(gcont->nid,pid,gcont->data);
		XtDestroyWidget(gcont->pop);
	}
	else
	{
		char msg[256];
		sprintf(msg,"Page %d is not a valid page for the selected notebook.",pid);
		VtError(gcont->parent,msg);
		XtDestroyWidget(gcont->pop);
	}
}

static
void
sel_cancel(wd,gcont)
	Widget wd;
	Goto_Context *gcont;
{
	XtDestroyWidget(gcont->pop);
}

static
void
destroy(wd,gcont)
	Goto_Context *gcont;
{
	free((char *)gcont);
}

static
void
sel_delete(w)
	Widget w;
{
	XtDestroyWidget(w);
}

PUBLIC
void
VtGotoPage(parent,srv,nid,cb,data)
	Widget parent;
	SrvContext *srv;
	int nid;
	void (*cb)();
	XtPointer data;
{
	Goto_Context *gcont = (Goto_Context *)malloc(sizeof *gcont);
	Widget prompt, help, ok;
	XmString xm_str;
	Arg args[10];
	int n;

	xm_str = XmStringCreateLtoR("Page Number:",XmSTRING_DEFAULT_CHARSET);

	n = 0;
	XtSetArg(args[n],XmNdialogStyle,XmDIALOG_APPLICATION_MODAL); n++;
	XtSetArg(args[n],XmNselectionLabelString,xm_str); n++;
	prompt = XmCreatePromptDialog(parent,"Goto A Page",args,n);
	XtAddCallback(prompt,XmNdestroyCallback,destroy,(XtPointer)gcont);

	XmStringFree(xm_str);

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

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

	gcont->parent = parent;
	gcont->pop = prompt;
	gcont->nid = nid;
	gcont->srv = srv;
	gcont->cb = cb;
	gcont->data = data;

	XtManageChild(prompt);
}
