// mainscreen.c++  - Screen version  (most general) of maid
// the motif organizatrion was originally adapted from program levitt/motifman/ch04/dynapix.c

#include <CC/iostream.h>
#include "Xgl.h"
#include "drawwindow.h"  // also calls global.h
#include "trace.h"
#include "screen.h"
#include "menu.h" // routines for FILE pulldown
#include "pdbmenu.h" // routines for PDB pulldown
#include "mapmenu.h" // routines for map pulldown
#include "fit.h"  // menus for fitting bones
#include "tordyn.h" // routines for torsiondynamics
#include "sphere.h"  // routines for sphere pull down
#include "math.h"
#include "initialize.h"
#include "initializescreen.h"
#include "peptide.h"  // routines for peptide creation and drawing
#include "pdbtolev.h"
#include "assignseq.h"
#include "editbones.h"
#include "extendfit.h"

static int attribs[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_DOUBLEBUFFER,GLX_DEPTH_SIZE,1, None};

static String fallbackResources[] = {
    "*sgiMode: True",
    "*useSchemes: all",
    "*glxwidget.width: 300", "*glxwidget.height: 300",/*glxwidget = string in drawind*/
    NULL};
Widget toplevel;
String colors[] = { "Black", "Red", "Green", "Blue" };

struct extbones ext;
struct maiddatafile maidfile;//Global variable - I do not know how to pass to menus
struct screenvalue  sc;
struct griddata map[NMAPS];
struct bonedata bone[NMAPS];
struct geometry geo;
struct tordyn tordata;
struct tordyn tordata2; // used only if fixend==3
struct spheredata sp[NSPHERES];
struct pepfit fit[NFIT]; // defined in peptide.h, see trace.c++ for definitin
struct pepset fitset;
struct rotomer rot[30];// rotomer data for each amino acid
float rotpos[NROT][NATOMS][3];// position of atoms in rotomer 
int rottype = PHE;  // Type of rotomer to draw
//int rottype = TRP;  // Type of rotomer to draw
int fitnum[NFIT];// see trace.c++ for definition
FILE *fpout; // = stdout or logfile, depending whether batch or screen version
int verbose = 1; // if = 1,print some stuff;  = 2 print a lot of stuff
int writelog=0; /*Default - write to screen if =1 write to logfile, otherwise, to screen */
int splitscreen =1; // if = 1 then use standard splitscreen stereo, 
//int splitscreen =0; // = 0 use wholescreen - also set geo.linewidth = 4 below

void main(int argc, char *argv[]) {
   
    Arg resources[20];	
    int n;
    XmString file, pdb, help,smap,edit,sphere,build;
    Display        *dpy;
    XVisualInfo    *visinfo;
    Widget          form,frame1,frame2,main_w,menubar, widget;
    XtPointer clientdata;

    fpout=stdout;/* print to console */
    defglob();
    toplevel = XtVaAppInitialize (&sc.appctx, "Demos",
        NULL, 0, &argc, argv,fallbackResources, NULL);
    /* main window contains a MenuBar and a Label displaying a pixmap */
    dpy = XtDisplay(toplevel);
    main_w = XtVaCreateManagedWidget ("main_window",
        xmMainWindowWidgetClass,   toplevel,
        XmNscrollingPolicy,        XmAPPLICATION_DEFINED,
        NULL);
    /* Create a simple MenuBar that contains three menus */
    file = XmStringCreateLocalized ("File");
    pdb = XmStringCreateLocalized ("PDB");
    smap = XmStringCreateLocalized ("MAP");
    build= XmStringCreateLocalized ("BUILD");
    edit = XmStringCreateLocalized ("ACTIVE");
    sphere = XmStringCreateLocalized ("SPHERE");
    help = XmStringCreateLocalized ("Help");
    menubar = XmVaCreateSimpleMenuBar (main_w, "menubar",
        XmVaCASCADEBUTTON, file, 'F',
        XmVaCASCADEBUTTON, pdb, 'P',
        XmVaCASCADEBUTTON, smap, 'M',
        XmVaCASCADEBUTTON, build, 'B',
        XmVaCASCADEBUTTON, edit, 'E',
        XmVaCASCADEBUTTON, sphere, 'S',
        XmVaCASCADEBUTTON, help, 'H',
        NULL);
    XmStringFree (file);
    XmStringFree (pdb);
    XmStringFree (smap);
    XmStringFree (edit);
    XmStringFree (sphere);
    XmStringFree (build);
    /* don't free "help" compound string yet -- reuse it later */
    /* Tell the menubar which button is the help menu  */
    if (widget = XtNameToWidget (menubar, "button_6"))
        XtVaSetValues (menubar, XmNmenuHelpWidget, widget, NULL);

#if 1
    filepulldown(menubar);
    pdbpulldown(menubar);
    mappulldown(menubar);
    buildpulldown(menubar);
    fitpulldown(menubar);
    spherepulldown(menubar);
    /* Third menu is the help menu -- callback is help_cb() */
    XmVaCreateSimplePulldownMenu (menubar, "help_menu", 6, help_cb,
        XmVaPUSHBUTTON, help, 'H', NULL, NULL,
        NULL);
    XmStringFree (help); /* we're done with it; now we can free it */

    XtManageChild (menubar);

    form = XmCreateForm(main_w, "form", NULL, 0);
    XtManageChild(form);


  n = 0;/* left window */
  XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); n++;
  XtSetArg(resources[n], XmNrightAttachment, XmATTACH_POSITION); n++;
  XtSetArg(resources[n], XmNrightPosition, 100); n++;
  if(splitscreen == 1){ // use standard split screen
     XtSetArg(resources[n], XmNrightPosition, 50); n++;
  }
  else{
     XtSetArg(resources[n], XmNrightPosition, 100); n++;
  }
  XtSetArg(resources[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(resources[n], XmNtopWidget, menubar); n++;
  XtSetArg(resources[n], XmNmarginWidth, 0); n++;
  XtSetArg(resources[n], XmNmarginHeight, 0); n++;
  XtSetArg(resources[n], XmNshadowThickness, 0); n++;/* Do not show border */
  frame1 = XmCreateFrame(form, "frame",resources, n);
  XtManageChild(frame1);
  n = 0; /* right window */
  XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); n++;
  XtSetArg(resources[n], XmNleftAttachment, XmATTACH_POSITION); n++;
  XtSetArg(resources[n], XmNleftPosition, 50); n++;
  XtSetArg(resources[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(resources[n], XmNtopWidget, menubar); n++;
  XtSetArg(resources[n], XmNmarginWidth, 0); n++;
  XtSetArg(resources[n], XmNmarginHeight, 0); n++;
  XtSetArg(resources[n], XmNshadowThickness, 0); n++;/* Do not show border */
    frame2 = XmCreateFrame(form, "frame",resources, n);
    XtManageChild(frame2);

    /* specify visual directly */
    if (!(visinfo = glXChooseVisual(dpy, DefaultScreen(dpy), attribs)))
	XtAppError(sc.appctx, "no suitable RGB visual");
   sc.drawwind1 = XtVaCreateManagedWidget("glxwidget", glwMDrawingAreaWidgetClass,
	frame1, GLwNvisualInfo, visinfo, NULL);
//   initialize(sc,geo,fitnum,fit,sp,maidfile,tordata,bone);

//    clientdata=(XtPointer)sc;  // Make sure sc get passed to the call back functions
    XtAddCallback(sc.drawwind1, GLwNresizeCallback, resize,clientdata);
    XtAddCallback(sc.drawwind1, GLwNexposeCallback, expose,clientdata);
    XtAddCallback(sc.drawwind1, GLwNinputCallback, input,clientdata);
    sc.drawwind2 = XtVaCreateManagedWidget("glxwidget", glwMDrawingAreaWidgetClass,
	frame2, GLwNvisualInfo, visinfo, NULL);
    if(splitscreen==1){ // use standard split screen
       XtAddCallback(sc.drawwind2, GLwNexposeCallback, expose,clientdata);
       XtAddCallback(sc.drawwind2, GLwNresizeCallback, resize,clientdata);
    }
// Input from right window - same as input exceept add screewidth to x mouse position
    XtAddCallback(sc.drawwind2, GLwNinputCallback, input2,clientdata);
    XtRealizeWidget(toplevel);
    sc.glxcontext = glXCreateContext(dpy, visinfo, 0, GL_TRUE);/* use same context for both windows*/
    sc.direct = glXIsDirect(dpy, sc.glxcontext);
    GLwDrawingAreaMakeCurrent(sc.drawwind1, sc.glxcontext);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    makeRasterFont(dpy);
    glClearColor(backcolv[0],backcolv[1],backcolv[2],0);//background color :red, green, blue, and alpha = black
#if 0  //Just used for degugging so can call Run  without first loading maidfile
 strcpy(maidfile.name,"temp.maid"); // default setting - use for debugging
 readmaidfile(maidfile,map,core); // This is normally called by "OLD" choice in file menu
 maidfile.start = 0; //maidfile.start = 2 or 3;  from now on draw pdb, not cube
#endif
   srand48(7); // initialize randum number generator (drand48)
   initialize(geo,fitnum,fit,sp,maidfile,tordata,tordata2,bone,fitset,rot);
   initializescreen(sc,geo);
   geo.linewidth = 1.0; 
#if 0 // Use this for making graphics 
   int i;
   geo.linewidth = 6.0;  // line width used for maidfullscreen
   for(i=0;i<=2;i++){
       backcolv[i]=whitev[i];
       writecolv[i]=blackv[i];
   }
   for(i=0;i<=2;i++){ // assign peptide colors
      geo.fitmaincol[i] = redv[i];
      geo.fitsidecol[i] = greenv[i];
      geo.fitcocol[i] = magentav[i];
      geo.geomaincol[i] = greenv[i];
      geo.geosidecol[i] = magentav[i];
      geo.geococol[i] = greenv[i];
   }
   for(i=0;i<=2;i++){ // main and side chain colors for first pdb
     pdb1maincol[i] = blackv[i];
     pdb1sidecol[i] = lightbluev[i];
   }
#endif
#endif
    XtAppMainLoop(sc.appctx);
}

