#include "xthesaurus.h"

#include <X11/Shell.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Viewport.h>
#include <X11/keysym.h>

#include <ctype.h>
#include <sys/file.h>

#ifdef  XFILESEARCHPATH
void AddPathToSearchPath();
#endif 

XtAppContext app_context;

#ifndef APPCLASS
#define APPCLASS "XThesaurus"
#endif

void GetWord(), Help(), Quit(), NukeWindow(), HideWordWin(), FindWord(),
  MakeWordWindow(), MakePopupWindow(), AddActions(), GetFontInfo();
int fontheight, fontwidth, display_width, display_height;
Display* dpy;
char *lookup_word();
char* getenv();

Widget word_window,word_form, input, top_level, top_form,icon;


int main(argc, argv)
     int argc;
     char* argv[];
{

#ifdef XFILESEARCHPATH
  AddPathToSearchPath(XFILESEARCHPATH);
#endif

  top_level = XtVaAppInitialize(&app_context,
				 APPCLASS,
				 0,0,
				 &argc,
				 argv,
				 NULL,	/* fallback resources */
				 NULL);	/* args */

  top_form = XtCreateManagedWidget("topform", formWidgetClass, 
				   top_level, 0,0);
  icon = XtCreateManagedWidget("icon", labelWidgetClass, top_form, 0,0);

  AddActions();
  MakeWordWindow();
  XtRealizeWidget(top_level);
  GetFontInfo();
  openlib();
  XtAppMainLoop(app_context);
  
  return 0;
}


void AddActions()
{
  static XtActionsRec actionTable[] = {
    {"GetWord",      GetWord},
    {"Help",         Help},
    {"Quit",         Quit},
    {"NukeWindow",   NukeWindow},
    {"HideWordWin",  HideWordWin},
    {"FindWord",     FindWord}
  };

  XtAppAddActions(app_context, actionTable, XtNumber(actionTable));
}

void MakeWordWindow()
{
  Widget prompt;
  word_window = XtCreatePopupShell("word", transientShellWidgetClass,
				   top_level, 0,0);
  word_form = XtCreateManagedWidget("form", formWidgetClass, word_window, 0,0);
  prompt = XtCreateManagedWidget("prompt", labelWidgetClass, word_form, 0,0);
  input = XtCreateManagedWidget("input", asciiTextWidgetClass, word_form, 0,0);
  XtVaSetValues(input, XtNeditType, XawtextEdit, 0,0);
  XtRealizeWidget(word_window);
}

void GetFontInfo()
{
  XFontStruct *fs ;

  XtVaGetValues(icon, 
		XtNfont, &fs,
		0,0);
  fontheight = fs->max_bounds.ascent + fs->max_bounds.descent;
  fontwidth = fs->max_bounds.width;

  dpy = XtDisplay(top_level);
  display_height= DisplayHeight(XtDisplay(top_level), 0);
  display_width =DisplayWidth(XtDisplay(top_level), 0);
}

int GetTextHeight(text)
     char* text;
{
  int counter = 1;

  while(*text++) if(*text == '\n') counter++;
  return counter*fontheight;
}

void GetWord()
{
  Position x, y;
  Dimension width, height;
  
  XtVaGetValues(top_level, 
		XtNwidth, &width,
		XtNheight, &height,
		0,0);

  XtTranslateCoords(top_level,
		    width, height, &x,&y);
  
  y -= (2*fontheight);

  XtVaSetValues(word_window, 
		XtNx, x,
		XtNy, y,
		0,0);

  XtVaSetValues(input,
		XtNstring, "",
		0,0);
  
  XtPopup(word_window, XtGrabNone);
}


void MakePopupWindow(name, str)
     char* name;
     char* str;
{
  Widget popup, viewport, label, form;
  Position x,y;
  Dimension height;

  popup = XtCreatePopupShell(name, transientShellWidgetClass,
			     top_level, 0,0);
  viewport = XtCreateManagedWidget("viewport", viewportWidgetClass,
				    popup, 0,0);
  form = XtCreateManagedWidget("form", formWidgetClass,
				    viewport, 0,0);
  label = XtCreateManagedWidget("text", asciiTextWidgetClass,
				form, 0,0);
  XtVaSetValues(label, XtNstring, str, 0,0);

  /* set geometry */
  XtTranslateCoords(top_level,
		    0,0,
		    &x, &y);

  height = GetTextHeight(str) + (2*fontheight);

  XtVaSetValues(label, 
		XtNheight, height,
		0,0);

  if(height > y)
    height = y;

  XtVaSetValues(popup,
		XtNheight, height + 15,
		XtNy, y - height,
		0,0);
  
  XtPopup(popup, XtGrabNone);
}


void Help()
{
  static char *message = "XTHESAURUS INSTRUCTIONS:\n\n\
In the icon window:\n\
      Clicking the left button will get you this help message;\n\
      Clicking the middle button will pop up a 'word window';\n\
      Clicking the right button will cause xthesaurus to exit.\n\n\
In the word window:\n\
      Type in your word.  Control-C will clear everything you've\n\
      entered, and the backspace key works normally.  Hit Return when\n\
      you're done entering the word.  xthesaurus will pop up a\n\
      'synonym window' with synonyms of your word.\n\n\
      Clicking any button in the word window will cause it to disappear.\n\n\
In the synonym window:\n\n\
      Clicking any button in the synonym window will cause it to\n\
      disappear.\n\n\
If you're using an RT, which has no middle button, press both the\n\
buttons simultaneously instead.\n\n\
Clicking any button in this window will cause it to disappear.\n\n";

  MakePopupWindow("help", message);
}

void HideWordWin()
{
  XtPopdown(word_window);
}

void Quit()
{
  exit(0);
}

void NukeWindow(w)
     Widget w;
{
  while(! XtIsTransientShell(w) ) 
    w = XtParent(w);
  XtDestroyWidget(w);
}

void FindWord()
{
  char* word, *syns;
  XtVaGetValues(input, XtNstring, &word, 0,0);
  syns = lookup_word(word);
  MakePopupWindow("synonyms",syns);
  free(syns);
}




/* AddPathToSearch is stolen from jik's xscreensaver code */

#ifdef XFILESEARCHPATH
static void
AddPathToSearchPath(path)
char *path;
{
     char *old, *new;

     old = getenv("XFILESEARCHPATH");
     if (old) {
#if defined(mips) || defined(hpux) || defined(sun) || \
	  (defined(ibm) && defined(SYSV) && defined(i386))
	  /* +1 for =, +2 for :, +3 for null */
	  new = XtMalloc((Cardinal) (strlen("XFILESEARCHPATH") +
				     strlen(old) +
				     strlen(path) + 3));
	  (void) strcpy(new, "XFILESEARCHPATH");
	  (void) strcat(new, "=");
	  (void) strcat(new, old);
	  (void) strcat(new, ":");
	  (void) strcat(new, path);
	  putenv(new);
#else
	  /* +1 for colon, +2 for null */
	  new = XtMalloc((Cardinal) (strlen(old) + strlen(path) + 2));
	  (void) strcpy(new, old);
	  (void) strcat(new, ":");
	  (void) strcat(new, path);
	  setenv("XFILESEARCHPATH", new, 1);
#endif
     }
     else {
#if defined(mips) || defined(hpux) || defined(sun) || \
	  (defined(ibm) && defined(SYSV) && defined(i386))
	  new = XtMalloc((Cardinal) (strlen("XFILESEARCHPATH") +
				     strlen(path) + 2));
	  (void) strcpy(new, "XFILESEARCHPATH");
	  (void) strcat(new, "=");
	  (void) strcat(new, path);
	  putenv(new);
#else
	  setenv("XFILESEARCHPATH", path, 1);
#endif
     }
}
#endif

