/*
 * 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: api.c
 * Date: 04/11/91
 *
 * Description:
 *   This file contains the Applications Programming Interface
 *   for the VNS library.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>

#include "VnsP.h"

/****************************************************************
 *                 PRIVATE FUNCTIONS
 ****************************************************************/

/****************************************************************
 *                 PUBLIC FUNCTIONS
 ****************************************************************/

/****************************************************************
 * Function: VnsInitialize 
 * Date: 04/11/91
 *
 * Description: 
 *   This function initializes the VNS library.
 *
 * Linkage: VnsContext *VnsInitialize(app_name)
 *   char *app_name - application name
 *
 * Revisions:
 ****************************************************************/
PUBLIC
VnsContext *
VnsInitialize(app_name)
	char *app_name;
{
	VnsContext *vns = (VnsContext *)malloc(sizeof(VnsContext));
	if (vns == NULL)
	{
		return NULL;
	}

	/* initialize pointer to VnsContext */
	vns->app_name = app_name;
	vns->toplevel = NULL;
	vns->browser = NULL;
	vns->prop_mgr = NULL;
	vns->dpy = NULL;
	vns->first_db = NULL;
	vns->current_db = NULL;
	vns->session = NULL;
	vns->stacking_page = NULL;
	vns->tunnel_page = NULL;
	vns->link_callback = NULL;
	vns->link_data = NULL;
	strcpy(vns->last_font,"-*-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*");
	strcpy(vns->last_color,"black");
	vns->clip_board = NULL;
	vns->default_font = NULL;
	vns->site_printer_file = NULL;

	return vns;
}

/****************************************************************
 * Function: VnsExit 
 * Date: 04/11/91
 *
 * Description: 
 *   This function terminates use of the VNS library and frees the
 *   memory used by the VnsContext pointer.
 *
 * Linkage: VnsContext *VnsExit(vns)
 *   VnsContext *vns - vns library info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsExit(vns)
	VnsContext *vns;
{
	/* first deselect all selected objects */
	deselect_all(vns);

	/* uninstall and free all images used for icons */
	uninstall_images(XtDisplay(vns->toplevel));
}

/****************************************************************
 * Function: VnsDefaultTopLevel
 * Date: 04/11/91
 *
 * Description: 
 *   This function creates the default toplevel application shell
 *   for the VNS program.
 *
 * Linkage: void VnsDefaultTopLevel(vns,argc,argv)
 *   VnsContext *vns - vns library info
 *   int argc        - number of command line arguments
 *   char **argv     - list of command line arguments
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsDefaultTopLevel(vns,argc,argv)
	VnsContext *vns;
	int argc;
	char **argv;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsDefaultTopLevel: VnsContext is NULL\n");
		return;
	}

	vns_default_top_level(vns,argc,argv);
}

/****************************************************************
 * Function: VnsCustomTopLevel
 * Date: 04/11/91
 *
 * Description: 
 *   This function allows front-end applications to provide
 *   their own command panel interface by passing the toplevel
 *   shell widget to the VNS library.
 *
 * Linkage: void VnsCustomTopLevel(vns,toplevel)
 *   VnsContext *vns - vns library info
 *   Widget toplevel - toplevel shell widget (XtAppInitialize)
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsCustomTopLevel(vns,toplevel)
	VnsContext *vns;
	Widget toplevel;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsCustomTopLevel: VnsContext is NULL\n");
		return;
	}
/*
	vns_init_toplevel(toplevel);
*/
	vns->toplevel = toplevel;
}

/****************************************************************
 * Function: VnsLoadAllNotebooks
 * Date: 04/11/91
 *
 * Description: 
 *   This function downloads all notebooks from the VNS server
 *   creates the internal notebook list that is required for
 *   displaying pages.
 *
 * Linkage: void VnsLoadAllNotebooks(vns)
 *   VnsContext *vns - vns library info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsLoadAllNotebooks(vns,db)
	VnsContext *vns;
	VnsDatabase *db;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsLoadAllNotebooks: VnsContext is NULL\n");
		return;
	}
	if (db == NULL)
	{
		fprintf(stderr,"VnsLoadAllNotebooks: VnsDatabase is NULL\n");
		return;
	}

	load_notebooks(db);
}

/****************************************************************
 * Function: VnsDoEverything
 * Date: 04/11/91
 *
 * Description: 
 *   This function does everything!
 *
 * Linkage: void VnsDoEverything(vns,argc,argv)
 *   VnsContext *vns - vns library info
 *   int argc        - number of command line arguments
 *   char **argv     - list of command line arguments
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsDoEverything(vns,argc,argv)
	VnsContext *vns;
	int argc;
	char **argv;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsDoEverything: VnsContext is NULL\n");
		return;
	}

	/* first, call the internal initialization function */
	vns_init(vns,argc,argv);

	return;
}

/****************************************************************
 * Function: VnsMainLoop
 * Date: 04/11/91
 *
 * Description: 
 *   This is the special main loop required by VNS. This function
 *   never returns.
 *
 * Linkage: void VnsMainLoop(vns)
 *   VnsContext *vns - vns library info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsMainLoop(vns)
	VnsContext *vns;
{
	XtAppContext app;

	if (vns == NULL)
	{
		fprintf(stderr,"VnsMainLoop: VnsContext is NULL\n");
		return;
	}

	app = XtWidgetToApplicationContext(vns->toplevel);

	/* our own Main Loop */
	for(;;)
	{
		XEvent event;

		XtAppNextEvent(app,&event);

		if ((event.type == PropertyNotify) &&
			(event.xproperty.window == DefaultRootWindow(XtDisplay(vns->toplevel))))
		{
			handle_client_request(vns,XtDisplay(vns->toplevel),event);
		}
		else
		{
			XtDispatchEvent(event);
		}
	}
}

/****************************************************************
 * Function: VnsMajorVersion
 * Date: 04/11/91
 *
 * Description: 
 *   This function returns the major version number for VNS.
 *
 * Linkage: int VnsMajorVersion(vns)
 *   VnsContext *vns - vns library info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
int
VnsMajorVersion(vns)
	VnsContext *vns;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsMajorVersion: VnsContext is NULL\n");
		return NULL;
	}

	return VNS_MAJOR_VERSION;
}

/****************************************************************
 * Function: VnsMinorVersion
 * Date: 04/11/91
 *
 * Description: 
 *   This function returns the minor version number for VNS.
 *
 * Linkage: int VnsMinorVersion(vns)
 *   VnsContext *vns - vns library info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
int
VnsMinorVersion(vns)
	VnsContext *vns;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsMinorVersion: VnsContext is NULL\n");
		return NULL;
	}

	return VNS_MINOR_VERSION;
}

/****************************************************************
 * Function: VnsRelease
 * Date: 04/11/91
 *
 * Description: 
 *   This function returns the release number for VNS.
 *
 * Linkage: int VnsRelease(vns)
 *   VnsContext *vns - vns library info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
int
VnsRelease(vns)
	VnsContext *vns;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsRelease: VnsContext is NULL\n");
		return NULL;
	}

	return VNS_RELEASE;
}

/****************************************************************
 * Function: VnsPrintVersion
 * Date: 04/11/91
 *
 * Description: 
 *   This function prints the VNS version header.
 *
 * Linkage: char *VnsPrintVersion(vns,fp)
 *   VnsContext *vns - vns library info
 *   FILE *fp;
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsPrintVersion(vns,fp)
	VnsContext *vns;
	FILE *fp;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsPrintVersion: VnsContext is NULL\n");
		return;
	}

	fprintf(fp,"Virtual Notebook System (vns%d.%d.%d)\n",
		VNS_MAJOR_VERSION,VNS_MINOR_VERSION,VNS_RELEASE);
}

/****************************************************************
 * Function: VnsPrintCopyright
 * Date: 04/11/91
 *
 * Description: 
 *   This function prints the copyright notice for VNS.
 *
 * Linkage: void VnsPrintCopyright(vns,fp)
 *   VnsContext *vns - vns library info
 *   FILE *fp        - file pointer 
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
VnsPrintCopyright(vns,fp)
	VnsContext *vns;
	FILE *fp;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsPrintCopyright: VnsContext is NULL\n");
		return;
	}

	fprintf(fp,"Copyright (c) 1990, 1991 Baylor College of Medicine\n");
}

/****************************************************************
 * Function: VnsGetBrowserWidget 
 * Date: 04/11/91
 *
 * Description: 
 *   This function returns the browser widget used in the
 *   command panel.
 *
 * Linkage: Widget VnsGetBrowserWidget(vns)
 *   VnsContext *vns - vns library info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
Widget
VnsGetBrowserWidget(vns)
	VnsContext *vns;
{
	if (vns == NULL)
	{
		fprintf(stderr,"VnsGetBrowserWidget: VnsContext is NULL\n");
		return NULL;
	}
	return vns->browser;
}
