#if ( !defined(lint) && !defined(SABER))
  static char rcs_version[] = "$Athena: main.c,v 4.5 88/12/19 13:47:28 kit Exp $";
#endif

#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Text.h>
#include <strings.h>
#include "instructions.h"


#ifndef ZERO
#include <X11/Xaw/Cardinals.h>
#endif

#ifndef XWEBSTER_SEARCH_PATHS
#define XWEBSTER_SEARCH_PATHS "/usr/sipb/lib/%T/%N%S:/afs/athena.mit.edu/contrib/sipb/lib/%T/%N%S:./%N%S:/afs/.athena.mit.edu/contrib/sipb/lib/%T/%N%S:./%N%S"
#endif

String fallback_resources[] = {
	"*icon.label: Cannot find xwebster resource file.  Click to exit.",
	"*icon.translations: #override \n <Btn1Down>: Set() \n <Btn1Up>: Exit()",
	NULL,
};

/* Action Procedure declarations */
void Exit(), Define(), clear(), Instructions(), WordBox();

Widget icon, top, instructions_shell, instructions, word_shell, word_box,
     word_label, word_text;
char *whoami;

typedef struct _defaults {
	String server;
} Defaults;

#define offset(field) XtOffset(Defaults *, field)
static XtResource resources[] = {
{"server", "Server", XtRString, sizeof(String), offset(server),
 XtRString, ""},
};
#undef offset
     
main(argc, argv)
int argc;
char *argv[];
{
     XtAppContext app_con;
     Defaults defs;
     Arg arglist[10];
     int i = 0;

     static XtActionsRec actions[] = {
          {"Exit", (XtActionProc) Exit}, 
          {"Define", (XtActionProc) Define},
          {"clear", (XtActionProc) clear},
          {"Instructions", (XtActionProc) Instructions},
          {"WordBox", (XtActionProc) WordBox}
     };

     static XrmOptionDescRec options[] = {
         {"-server", "server", XrmoptionSepArg, (caddr_t) NULL}
     };

     whoami = argv[0];

     /* Set XFILESEARCHPATH to find xwebster's resource file */
     /* XXX This is gross XXX */
     {
	  char *path1, *path2;
	  
	  path1 = (char *) getenv("XFILESEARCHPATH");
	  if (! path1) path1 = "";
	  path2 = (char *) malloc(strlen(path1) +
				  strlen(XWEBSTER_SEARCH_PATHS) + 2);
	  sprintf(path2, "%s:%s", path1, XWEBSTER_SEARCH_PATHS);
	  setenv("XFILESEARCHPATH", path2, 1);
	  free(path2);
     }
     
     top = XtAppInitialize(&app_con, "XWebster", options, XtNumber(options),
			   &argc, argv, fallback_resources,
			   NULL, ZERO);

     XtGetApplicationResources(top, (XtPointer) &defs, resources,
			       XtNumber(resources), NULL, ZERO);
     
     XtAppAddActions(app_con, actions, XtNumber(actions));

     icon = XtCreateManagedWidget("icon", commandWidgetClass, top, NULL, ZERO);
     instructions_shell = XtCreatePopupShell("instructionsShell",
					     shellWidgetClass, top,
					     NULL, ZERO);

     arglist[0].name = XtNstring;
     arglist[0].value = (XtArgVal) instruction_string;
     arglist[1].name = XtNuseStringInPlace;
     arglist[1].value = True;
     arglist[2].name = XtNwrap;
     arglist[2].value = XawtextWrapWord;
     i = 3;
     
     instructions = XtCreateManagedWidget("instructions",
					  asciiTextWidgetClass,
					  instructions_shell, arglist,
					  i);

     word_shell = XtCreatePopupShell("wordShell", applicationShellWidgetClass,
				     top, NULL, ZERO);

     word_box = XtCreateManagedWidget("word", formWidgetClass, word_shell,
				      NULL, ZERO);

     word_label = XtCreateManagedWidget("label", labelWidgetClass, word_box,
					NULL, ZERO);
     arglist[0].name = XtNeditType;
     arglist[0].value = XawtextEdit;
     arglist[1].name = XtNstring;
     arglist[1].value = (XtArgVal) "";
     i = 2;
     
     word_text = XtCreateManagedWidget("text", asciiTextWidgetClass,
				       word_box, arglist, i);
     
     XtRealizeWidget(top);
     XtAppMainLoop(app_con);
}


/* ARGSUSED */
void Exit(w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
     XtDestroyWidget(top);
     exit(0);
}


/* ARGSUSED */
void Define(w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
     char *ptr, *word_string = (char *) NULL;
     Arg argle[1];

     XtSetArg(argle[0], XtNstring, &word_string);
     
     XtGetValues(w, argle, ONE);
     
     if (ptr = index(word_string, '\n'))
	  *ptr = '\0';
     if (*word_string) {
	  printf("Word to define: %s\n", word_string);
     }
     XawAsciiSourceFreeString(word_string);
}


/* ARGSUSED */
void clear (w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
     Arg argle[1];
	
     argle[0].name = XtNstring;
     argle[0].value = (XtArgVal) NULL;
     
     XtSetValues(w, argle, ONE);
     XawTextSetInsertionPoint(word_text, 0);
     XawTextSetLastPos(word_text, 0);
}



/* ARGSUSED */
void Instructions (w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
     XtPopup(instructions_shell, XtGrabNone);
}



/* ARGSUSED */
void WordBox (w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
     XtPopup(word_shell, XtGrabNone);
}
