/* Menu routines for making and drawing a spheres around map region
*/
#include "drawwindow.h"  // also calls global.h
#include "screen.h"
#include "sphere.h"  // routines for sphere pull down
//#include "aux.h"


extern Widget toplevel;
extern struct screenvalue  sc;
extern struct griddata map[NMAPS]; // all the map (denfile),contour data associated with map
extern struct bonedata bone[NMAPS];
extern struct spheredata sp[NSPHERES];
extern struct maiddatafile maidfile;//Global variable - I do not know how to pass to menus

Widget spheremenu;// Global since needed in sphere_cb

void drawonesphere(int isphere)
/*
Modified from old maid (mask.c)
 My crude program for drawling spheres
   as series of nlat circles evenly spaced
*/
{
    int i,j,k;
    float xx[3],lrad,x1;
    float rcir = sp[isphere].radius;
    int nlat = 10;  //# of circle
    int npoints = 100; // # of points on each circle

    glColor3fv(sc.spherecol);  //This the simpler, vector version    
    glLineWidth(1.0);
    for(k=0;k<=2;k++)
        
    for(i= -nlat+1;i<nlat;i++){
       x1=i*rcir/nlat;
       lrad=sqrt(rcir*rcir-x1*x1);
       xx[0]=sp[isphere].cenpos[0]-x1; /* x position of point on lat */
       glBegin(GL_POLYGON);
       for(j=0;j<npoints;j++){
          xx[1]=sp[isphere].cenpos[1]+lrad*cos(2*PI*j/npoints);
          xx[2]=sp[isphere].cenpos[2]+lrad*sin(2*PI*j/npoints);
          glVertex3fv(xx);
       }
       glEnd();
    }

}

void drawspheres()
// Draw all the sphere that have been set = maidfile.nsphere
{
   int i;

   if(sc.drawspheres ==1){
     for(i=1;i<=maidfile.nsphere;i++){
        glPushName(i); /*put pdbfile # on stack*/
        drawonesphere(i);
        glPopName();
     }
   }
}

void savespheres()
{
    FILE *fp; 
    int i;

    fp= fopen(maidfile.spherefile,"w");
    fprintf(fp,"%d\n",maidfile.nsphere);
    for(i=1;i<=maidfile.nsphere;i++)
      fprintf(fp,"%f  %f  %f  %f\n",
          sp[i].radius,sp[i].cenpos[0],sp[i].cenpos[1],sp[i].cenpos[2]);
    fclose(fp);
}

void recallspheres()
{
    FILE *fp;
    int i;
    char line[205];
    Widget widget; 

//printf("Reading %s\n",maidfile.spherefile);
    if( (fp=fopen(maidfile.spherefile,"r")) == NULL)
         errormessage(toplevel,"ERROR: spherefile file cannot be found");
    else{ //spherefile opened
       fgets(line,200,fp); /* read each line*/
       sscanf(line,"%d",&maidfile.nsphere);
       for(i=1;i<=maidfile.nsphere;i++){
          fgets(line,200,fp); /* read each line*/
          sscanf(line,"%f %f %f %f",
               &sp[i].radius,&sp[i].cenpos[0],&sp[i].cenpos[1],&sp[i].cenpos[2]);
       }
       fclose(fp);
    } // spherefile opened
    sc.isphere = maidfile.nsphere; // set active sphere to last read
    sc.drawspheres =1.0;
    if (widget = XtNameToWidget (spheremenu,"button_0")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widget, XmNset,TRUE, NULL);
}



void sphere_cb(Widget widget,XtPointer client_data,XtPointer  call_data)
{
   int item_no = (int) client_data;
//   Widget widg;

   if(item_no==0) // Turn on/off drawing of all spheres
      sc.drawspheres = -sc.drawspheres;   
   else if(item_no==1){ // turn on addsphere, turn off all the rest
      if(sc.addsphere ==1)
          sc.addsphere = -1;
      else
          sc.addsphere = 1;
#if 0
      sc.movesphere = -1;
      if (widg = XtNameToWidget (spheremenu,"button_2")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widg, XmNset, FALSE, NULL);
#endif
printf("sc.isphere = %d pickcenter\n",sc.isphere ); 
   }
   else if(item_no==2){ // MOVE and size, turn off add
      if(sc.movesphere ==1) // signal to move sphere
           sc.movesphere = -1;
      else
           sc.movesphere = 1;
#if 0
      sc.addsphere =  -1;        
      if (widg = XtNameToWidget (spheremenu,"button_1")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widg, XmNset, FALSE, NULL);
#endif
   }
   else if(item_no==3){ // Select sphere
      if(sc.selectsphere==1)
         sc.selectsphere= -1;
      else
         sc.selectsphere= 1;
   }
   else if(item_no==4){ // Select sphere
      if(sc.deletesphere==1)
         sc.deletesphere= -1;
      else
         sc.deletesphere= 1;
   }
   else if(item_no==5){ // Save sphere
     savespheres();
   }
   else if(item_no==6){ // Recall sphere
     recallspheres();
   }
}


void spherepulldown(Widget menubar)
{
   XmString pickcenter,move,save,recall,drawonoff,selectsp,deletesp;
   Widget widget;

   drawonoff = XmStringCreateLocalized ("DRAW SPHERES ON/OFF");
   pickcenter = XmStringCreateLocalized ("ADD SPHERE (pick bone or pdb)");
   move = XmStringCreateLocalized ("MOVE AND SIZE (left win.)(but2: x,y) (but3: 'x'= z;'y'=rad)");
   selectsp = XmStringCreateLocalized ("PICK ACTIVE SPHERE");
   deletesp = XmStringCreateLocalized ("PICK SPHERE TO DELETE");
   save = XmStringCreateLocalized ("SAVE SPHERES (in 'spherefile')");
   recall = XmStringCreateLocalized ("RECALL SPHERES (from 'spherefile')");
   spheremenu=XmVaCreateSimplePulldownMenu (menubar, "sphere_menu", 5, sphere_cb,
        XmVaCHECKBUTTON,drawonoff,NULL, NULL, NULL,  // button_0
        XmVaCHECKBUTTON,pickcenter,NULL, NULL, NULL,  // button_1
        XmVaCHECKBUTTON,move,NULL, NULL, NULL,  // button_2
        XmVaCHECKBUTTON,selectsp,NULL, NULL, NULL,  // button_3
        XmVaCHECKBUTTON,deletesp,NULL, NULL, NULL,  // button_4
        XmVaPUSHBUTTON,save,NULL, NULL, NULL,  // button_5
        XmVaPUSHBUTTON,recall,NULL, NULL, NULL,  // button_6
        NULL);
   XmStringFree (pickcenter); 
   XmStringFree (drawonoff); 
   XmStringFree (move); 
   XmStringFree (selectsp); 
   XmStringFree (save); 
   XmStringFree (deletesp); 
   XmStringFree (recall); 
   if (widget = XtNameToWidget (spheremenu,"button_0")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widget, XmNset, FALSE, NULL);
   if (widget = XtNameToWidget (spheremenu,"button_1")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widget, XmNset, FALSE, NULL);
   if (widget = XtNameToWidget (spheremenu,"button_2")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widget, XmNset, FALSE, NULL);
   if (widget = XtNameToWidget (spheremenu,"button_3")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widget, XmNset, FALSE, NULL);
   if (widget = XtNameToWidget (spheremenu,"button_4")) //DEFAULT:  Turn off check box for label on/off button
            XtVaSetValues (widget, XmNset, FALSE, NULL);
}
