/*
 *	$Source: /u1/X/xman/RCS/main.c,v $
 *	$Header: main.c,v 1.3 87/02/03 15:35:26 swick Exp $
 */

#ifndef lint
static char *rcsid_main_c = "$Header: main.c,v 1.3 87/02/03 15:35:26 swick Exp $";
#endif	lint

#include "xman.h"
#include "extern.c"

/*
 * These are standard cursors from X
 */
#include <X/cursors/center_ptr.cursor>
#include <X/cursors/center_ptr_mask.cursor>
#include <X/cursors/double_arrow.cursor>
#include <X/cursors/double_arrow_mask.cursor>
#include <X/cursors/target.cursor>
#include <X/cursors/target_mask.cursor>
/*
 * The icon
 */
#include "xman.icon"
#include "xman_mask.icon"

/*ARGSUSED*/
Fatal(disp) Display *disp;
{
  perror("Fatal:");
  exit(1);
}
/*ARGSUSED*/
Warning(disp,err) Display *disp; XErrorEvent *err;
{
  eprintf("Warning: %s\n",XErrDescrip(err->error_code));
  return;
}
eprintf(fmt,a,b,c,d,e,f,g,h) char *fmt; char *a,*b,*c,*d,*e,*f,*g,*h;
{
  if(errorfp == NULL) errorfp = stderr;
  fprintf(stderr,fmt,a,b,c,d,e,f,g,h);
}
/*ARGSUSED*/
main(argc,argv) int argc; char **argv;
{
  char *def;
  Color color_def;
  OpaqueFrame frame;
  int minwidth, minheight;
  char geom_buf[100];

  if((prog = rindex(argv[0],'/')) != NULL)
    ++prog;
  else prog = argv[0];

  /*
   * Set default X error handlers so we get a message
   */
  XIOErrorHandler(Fatal);
  XErrorHandler(Warning);
  /*
   * Give namei a break
   */
  if(chdir(MANDIR) < 0) {
    eprintf("Can't chdir to %s?\n",MANDIR);
    exit(1);
  }
  /*
   * Analyze the manual area
   */
  man();
  /*
   * If you enable this it will print what man() figured out
   * on the stdout.
   */
#ifdef notdef
  printman();
  exit(0);
#endif
  if(XOpenDisplay(NULL) == NULL) {
    eprintf("No X?!\n");
    exit(1);
  }
  /*
   * Process defaults (boy is this crufty)
   */
  if(((def = XGetDefault(prog,"Foreground")) != NULL) &&
     (XParseColor(def,&color_def)) && (XGetHardwareColor(&color_def)))
    fg = color_def.pixel;
  else fg = DEF_FOREGROUND;

  if(((def = XGetDefault(prog,"Background")) != NULL) &&
     (XParseColor(def,&color_def)) && (XGetHardwareColor(&color_def)))
    bg = color_def.pixel;
  else bg = DEF_BACKGROUND;

  if((def = XGetDefault(prog,"Reverse")) != NULL) {
    Pixmap tmp;

    tmp = fg;
    fg = bg;
    bg = tmp;
  }
  /* There should probably be more */

  /*
   * Set up the fonts (see notes in config.h as to why we can't
   * really let the user just choose fonts
   */
  if((bodynormal = XOpenFont(BODYNORMAL)) == NULL) {
    eprintf("could not open font %s\n",BODYNORMAL);
    exit(1);
  }
  if((bodybold = XOpenFont(BODYBOLD)) == NULL) {
    eprintf("Could not open font %s\n",BODYBOLD);
    exit(1);
  }
  if((bodyitalic = XOpenFont(BODYITALIC)) == NULL) {
    eprintf("Could not open font %s\n",BODYITALIC);
    exit(1);
  }
  if((menufont = XOpenFont(MENUFONT)) == NULL) {
    eprintf("Could not open font %s\n",MENUFONT);
    exit(1);
  }
  bodyheight = bodynormal->height;
  
  /* The main window */
  /*
   * Due to various size restrictions inherent in the application
   * we encourage the user to only place it and use default sizes.
   */
  mincol = MINCOL;
  bzero(&frame,sizeof(frame));
  frame.bdrwidth = 2;
  frame.border = fg;
  frame.background = bg;
  minwidth = NCHARS*bodybold->width+mincol;
  minheight = NLINES*bodynormal->height;
  sprintf(geom_buf,"=%dx%d+0+0",minwidth,minheight);
  w = XCreate("Visual Courseware Manual",
	      prog,
	      "",geom_buf,
	      &frame,
	      minwidth,minheight);
  /*
   * The icon, we'll set the xy position later
   */
  xicon = XCreateWindow(RootWindow,0,0,xman_width,xman_height,0,fg,bg);
  XSetIconWindow(w,xicon);
  /* redisplay handler, also called for
   * mouse events indirectly when in manual page mode
   */
  Sx_HandlerCreate(w,ExposeWindow,ReDisplayProc,(ClientData) 0);
  Sx_HandlerCreate(xicon,ExposeWindow,ReIconProc,(ClientData) 0);
  /*
   * This is the main man page pseudo-menu handler, also calls out
   * to ReDisplayProc() to page forward and backwards when we are
   * displaying a man page(s)
   */
  Sx_HandlerCreate(w,ButtonPressed|ButtonReleased|MouseMoved,
		   MyButtonProc,(ClientData) NULL);

  /*
   * Set up the top title bar
   */
  if((titlefont = XOpenFont(TITLEFONT)) == NULL) {
    XDestroyWindow(w);
    Sx_Panic("Couldn't open a font!");
  }
  titleheight = Sx_DefaultHeight(titlefont);
  title = Sx_TitleCreate(w,SX_TOP,
			 titleheight,TITLEBORDER,titlefont,
			 WhitePixel,BlackPixel,NULL,
			 "Visual Courseware Manual",NULL);

  Sx_HandlerCreate(title,ButtonPressed,IconifyProc,(ClientData) TRUE);
  Sx_HandlerCreate(xicon,ButtonPressed,IconifyProc,(ClientData) FALSE);

  /*
   * The section pull-down, mostly set up dynamically
   */
  setupSections();
  menubar = Sx_CreatePacked(w,SX_TOP,titleheight,1,(Window)NULL,
			    (Window)NULL,fg,bg);
  sectionMenu = Sx_MenuCreate(menubar,SECTION,nsections,sections,menufont,
			      BlackPixel,WhitePixel);
  optionsMenu = Sx_MenuCreate(menubar,OPTIONS,NOPTIONS,options,menufont,
			      BlackPixel,WhitePixel);
  infoMenu =    Sx_MenuCreate(menubar,INFO,NINFO,info,menufont,
  			      BlackPixel,WhitePixel);
  /*
   * Set up cursors
   */
  xman_cursor = XCreateCursor(center_ptr_width,center_ptr_height,
			      center_ptr_bits,center_ptr_mask_bits,
			      center_ptr_x_hot,center_ptr_y_hot,
			      BlackPixel,WhitePixel,GXcopy);
  xpage_cursor = XCreateCursor(double_arrow_width,double_arrow_height,
			      double_arrow_bits,double_arrow_mask_bits,
			      double_arrow_x_hot,double_arrow_y_hot,
			      BlackPixel,WhitePixel,GXcopy);
  xicon_cursor = XCreateCursor(target_width,target_height,
			       target_bits,target_mask_bits,
			       target_x_hot,target_y_hot,
			       BlackPixel,WhitePixel,GXcopy);
  XDefineCursor(w,xman_cursor);
  XDefineCursor(xicon,xicon_cursor);
  XMapWindow(w);
  XQueryWindow(w,&w_info);
  /*
   * Put the icon at the upper-left corner of the main window
   */
  XMoveWindow(xicon,w_info.x,w_info.y);
  XQueryWindow(title,&title_info);
  XQueryWindow(menubar,&menubar_info);
  /*
   * The problem here is I need to know where to start putting
   * text in the window after the title and menu bars, I notice
   * he punts (uses a constant) in sxDemo.c, I sort of punt.
   */
#ifdef notdef
  /* doesn't work, why? */
  minrow = title_info.height+2*title_info.bdrwidth
    +menubar_info.height+2*menubar_info.bdrwidth;
#endif
  minrow = 2 * titleheight;
  /*
   * MAIN LOOP
   */
  for(;;) {
    XEvent event;

    XNextEvent(&event);
    Sx_HandleEvent(&event);
  }
}
