/* Test procedure for AtTextDrawJustified */

#include <X11/Xlib.h>
#include <stdio.h>
#include <strings.h>
#include <At/Text.h>

main(argc, argv)
     int argc;
     char **argv;
{
  Display *display;
  Window window;
  XEvent e;
  int screen;
  int height, width = 500;
  long foreground, background;
  GC gc;
  int wx,wy, h,w, hjust,vjust, x,y=20;
  Bool rotate;
  unsigned int attrib_mask;
  XSetWindowAttributes attrib;
  AtFontFamily *family;
  int size;
  AtText *text;
  char buf[200];

  if ((argc != 5) && (strcmp(argv[5],"vertical") != 0))
    {
      fprintf(stderr, "Usage: justtest family size hjust vjust [vertical]\n");
      exit(0);
    }

  display = XOpenDisplay("unix:0");
  if (display == NULL)
    {
      fprintf(stderr, "Could not open display.\n");
      exit(0);
    }

  rotate =strcmp(argv[5],"vertical") == 0;
  if (rotate) {
    x = 50;
    height = 500;
    h = 400;
    w = 200;
  }
  else {
    x = 10;
    height = 100;
    h = 70;
    w = 400;
  }
  
  screen = DefaultScreen(display);

  foreground = BlackPixel(display, screen);
  background = WhitePixel(display, screen);

  wx = (DisplayWidth(display,screen) - width)/2;
  wy = (DisplayHeight(display,screen) - height)/2;

  attrib.background_pixel = background;
  attrib.border_pixel = foreground;
  attrib_mask = CWBackPixel | CWBorderPixel;

  window = XCreateWindow(display, RootWindow(display, screen),
			 wx,wy, width,height, 3, CopyFromParent,
			 InputOutput, CopyFromParent, 
			 attrib_mask, &attrib);
  XMapWindow(display, window);
  XSync(display, False);

  gc = DefaultGC(display, screen);
  XSetForeground(display, gc, foreground);
  XSetBackground(display, gc, background);

  XSelectInput(display, window, KeyPressMask);

  family = AtFontFamilyGet(display,argv[1]);
  if (strcmp(argv[2],"AtFontSMALLEST") == 0)
    size = AtFontSMALLEST;
  else if (strcmp(argv[2],"AtFontSMALL") == 0)
    size = AtFontSMALL;
  else if (strcmp(argv[2],"AtFontMEDIUM") == 0)
    size = AtFontMEDIUM;
  else if (strcmp(argv[2],"AtFontNORMAL") == 0)
    size = AtFontNORMAL;
  else if (strcmp(argv[2],"AtFontBIG") == 0)
    size = AtFontBIG;
  else if (strcmp(argv[2],"AtFontBIGGEST") == 0)
    size = AtFontBIGGEST;
  else {
    fprintf(stderr, "Incorrect AtFont size\n");
    exit(0);
  }
  if (strcmp(argv[3],"AtTextJUSTIFY_LEFT") == 0)
    hjust = AtTextJUSTIFY_LEFT;
  else if (strcmp(argv[3],"AtTextJUSTIFY_CENTER") == 0)
    hjust = AtTextJUSTIFY_CENTER;
  else if (strcmp(argv[3],"AtTextJUSTIFY_RIGHT") == 0)
    hjust = AtTextJUSTIFY_RIGHT;
  else {
    fprintf(stderr, "Incorrect horizontal justification\n");
    exit(0);
  }
  if (strcmp(argv[4],"AtTextJUSTIFY_TOP") == 0)
    vjust = AtTextJUSTIFY_TOP;
  else if (strcmp(argv[4],"AtTextJUSTIFY_CENTER") == 0)
    vjust = AtTextJUSTIFY_CENTER;
  else if (strcmp(argv[4],"AtTextJUSTIFY_BOTTOM") == 0)
    vjust = AtTextJUSTIFY_BOTTOM;
  else {
    fprintf(stderr, "Incorrect vertical justification\n");
    exit(0);
  }
  XNextEvent(display, &e);

  while (strcmp(buf,"DO NOT DELETE THIS LINE") != 0) {
    gets(buf);
    XClearWindow(display,window);
    XDrawString(display,window,gc,10,15,buf,strlen(buf));
    XDrawLine(display,window,gc,x,y,x+w,y);
    XDrawLine(display,window,gc,x+w,y,x+w,y+h);
    XDrawLine(display,window,gc,x+w,y+h,x,y+h);
    XDrawLine(display,window,gc,x,y+h,x,y);
    text = AtTextCreate(buf,family,size);
    if (rotate)
      AtTextRotate(text);
    AtTextDrawJustified(display,window,gc,text,hjust,vjust,x,y,w,h);

    XNextEvent(display, &e);

    XFlush(display);
  };
  fprintf(stdout, "Test Completed\n");
}
