/* Intuition Interface for Plot Program */


#include "plot.h"


struct TextFont *tf;
FILE *fp1;
char Editor[50]= "ed";



main()
{
void OpenAll(), Work(), CloseDown();

  OpenAll();
  Work();
  CloseDown();
  return(0);
}


void OpenAll()       /*   open libraries, screen, and window   */
{

void DrawScreen(), FixPlotTypeGadgets(), ReadDef();

  DosBase = (struct DosBase *)
          OpenLibrary("dos.library",0);
  if( DosBase == NULL ) exit(10);

  DiskfontBase = (struct DiskfontBase *)
          OpenLibrary("diskfont.library",0);
  if( DiskfontBase == NULL )  exit(10);

  IntuitionBase = (struct IntuitionBase *)
          OpenLibrary("intuition.library",0);
  if( IntuitionBase == NULL )  exit(10);

  GfxBase = (struct GfxBase *)
          OpenLibrary("graphics.library",0);
  if( GfxBase == NULL )  exit(10);

  tf=(struct TextFont *) OpenDiskFont(&gfont);
  ReadDef();
  if( (p_Window = (struct Window *)OpenWindow(&Window))==NULL) exit(10);

  FixPlotTypeGadgets();
  SetMenuStrip(p_Window,&Menu1);
  r = p_Window->RPort;
  PrintIText(r,&IText5,0,0);
}

void Work()
{
struct IntuiMessage  *p_message;         /* pointer to message */
void ProcMes();

  while (QuitFlag !=QUIT)
    {
       Wait(1l<<p_Window->UserPort->mp_SigBit);        /* wait for a message */
       while (p_message = (struct IntuiMessage *)GetMsg(p_Window->UserPort))
         ProcMes(p_message);
     }
}


void ProcMes(p_message)
struct IntuiMessage *p_message;
{
ULONG MesClass;        /*     Fields for storing      */
USHORT MesCode;        /*     intuimessage data       */
APTR Pointer;          /*                             */
void HandleEvent();

   MesClass = p_message->Class;             /* Store values */
   MesCode = p_message->Code;
   Pointer = p_message->IAddress;
   ReplyMsg(p_message);                     /* Reply to message */
   HandleEvent(MesClass,MesCode,Pointer);
}

char Command[200];
char String[50];
char     def_name[100] = "FileName";
char     def_dir[50] = "PLOT:Data/";



void HandleEvent(MesClass,MesCode,Pointer)
ULONG MesClass;        /*     Fields for storing      */
USHORT MesCode;        /*     intuimessage data       */
APTR Pointer;          /*                             */
{
void FileReq(), GadMXSel(), DrawScreen(), Edit(), ShowAdvert();
void SaveDef(), DefineEditor(), LoadDef(), print_err();
BPTR Monitor;
unsigned M0, I0, S0;   /* Menu control index */


  if ( MesClass == CLOSEWINDOW)      QuitFlag=QUIT;
  if ( MesClass == REFRESHWINDOW)    DrawScreen();
  if ( MesClass == MENUPICK) {
    if( MesCode != MENUNULL)
     {
        M0 = MENUNUM(MesCode);
        I0 = ITEMNUM(MesCode);
        S0 = SUBNUM(MesCode);

      switch (M0) {
      case 0 :
        switch (I0) {
        case 0 :  FileReq(); break;
        case 1 :  LoadDef(); break;
        case 2 :  SaveDef(); break;             /* save defaults */
        case 3 :  ShowAdvert(); break;             /* About window  */
        case 4 :  QuitFlag=QUIT; break;
        default :
          print_err("Menu selection reached default in Project menu\n");
          break;
        }
        break;
      case 1 :
        switch (I0) {
          case 0 : DefineEditor(); break;
          case 1 : Edit(); break;
        default :
          print_err("Menu selection reached default in Edit menu\n");
          break;
        }
        break;
      case 2 :
        switch (I0) {
          case 0 :  Form=AEGIS; break;
          case 1 :  Form=INTRO; break;
          case 2 :  Form=MCAD; break;
        default :
          print_err("Menu selection reached default in Form menu\n");
          break;
        }
        break;
      default :
        print_err("Menu selection reached default menu\n");
        break;
      }
     }
  }

  if ( MesClass == GADGETDOWN)
    {

      if (Pointer == (APTR)&PathButton) FileReq();

      if (Pointer == (APTR)&Switch1)
        {
           GadMXSel(p_Window,&Switch1,&Switch4,NULL,NULL,NULL,NULL);
           NET=ON;
        }
      if (Pointer == (APTR)&Switch4)
        {
           GadMXSel(p_Window,&Switch4,&Switch1,NULL,NULL,NULL,NULL);
           NET=OFF;
        }
      if (Pointer == (APTR)&Switch2)
        {
           GadMXSel(p_Window,&Switch2,&Switch5,NULL,NULL,NULL,NULL);
           AXES=ON;
        }
      if (Pointer == (APTR)&Switch5)
        {
           GadMXSel(p_Window,&Switch5,&Switch2,NULL,NULL,NULL,NULL);
           AXES=OFF;
        }
      if (Pointer == (APTR)&Switch3)
        {
           GadMXSel(p_Window,&Switch3,&Switch6,NULL,NULL,NULL,NULL);
           TWOSIDES=ON;
        }
      if (Pointer == (APTR)&Switch6)
        {
           GadMXSel(p_Window,&Switch6,&Switch3,NULL,NULL,NULL,NULL);
           TWOSIDES=OFF;
        }
      if (Pointer == (APTR)&Plot3DButton)
        {
           GadMXSel(p_Window,&Plot3DButton,&Plot2DButton,NULL,NULL,NULL,NULL);
           PlotType=PLOT3D;
        }
      if (Pointer == (APTR)&Plot2DButton)
        {
           GadMXSel(p_Window,&Plot2DButton,&Plot3DButton,NULL,NULL,NULL,NULL);
           PlotType=PLOT2D;
        }
      if (Pointer == (APTR)&Button0)
        {
           GadMXSel(p_Window,&Button0,&Button1,&Button2,&Button3,NULL,NULL);
           x=1;
        }
      if (Pointer == (APTR)&Button1)
        {
           GadMXSel(p_Window,&Button1,&Button0,&Button2,&Button3,NULL,NULL);
           x=2;
        }
      if (Pointer == (APTR)&Button2)
        {
           GadMXSel(p_Window,&Button2,&Button0,&Button1,&Button3,NULL,NULL);
           x=3;
        }
      if (Pointer == (APTR)&Button3)
        {
           GadMXSel(p_Window,&Button3,&Button0,&Button1,&Button2,NULL,NULL);
           x=0;
        }
      if (Pointer == (APTR)&Button4)
        {
           GadMXSel(p_Window,&Button4,&Button5,&Button6,&Button7,NULL,NULL);
           y=1;
        }
      if (Pointer == (APTR)&Button5)
        {
           GadMXSel(p_Window,&Button5,&Button4,&Button6,&Button7,NULL,NULL);
           y=2;
        }
      if (Pointer == (APTR)&Button6)
        {
           GadMXSel(p_Window,&Button6,&Button4,&Button5,&Button7,NULL,NULL);
           y=3;
        }
      if (Pointer == (APTR)&Button7)
        {
           GadMXSel(p_Window,&Button7,&Button4,&Button5,&Button6,NULL,NULL);
           y=0;
        }
      if (Pointer == (APTR)&Button8)
        {
           GadMXSel(p_Window,&Button8,&Button9,&Button10,&Button11,NULL,NULL);
           e=1;
        }
      if (Pointer == (APTR)&Button9)
        {
           GadMXSel(p_Window,&Button9,&Button8,&Button10,&Button11,NULL,NULL);
           e=2;
        }
      if (Pointer == (APTR)&Button10)
        {
           GadMXSel(p_Window,&Button10,&Button8,&Button9,&Button11,NULL,NULL);
           e=3;
        }
      if (Pointer == (APTR)&Button11)
        {
           GadMXSel(p_Window,&Button11,&Button8,&Button9,&Button10,NULL,NULL);
           e=0;
        }
      if (Pointer == (APTR)&StartButton)
        {
          if ((PlotType==PLOT2D)&&(CheckFile()!=NULL))
            {
             sprintf(Command,"copy %s t:plotfile \n Plot:Multiplot -x%dy%de%d t:plotfile \n endcli",FileNameSIBuff,x,y,e);
             Monitor=Open("CON:0/46/640/120/How To",MODE_NEWFILE);
             SetPointer(p_Window,WaitSprite,38,16,-4,-4);
             Execute(Command,Monitor,0);
             Close(Monitor);
             DrawScreen();
             DeleteFile("t:plotfile");
             if (Form==INTRO) sprintf(Command,"Plot:txt_2_icad t:plotfile.txt0 %s.cad",FileNameSIBuff);
             if (Form==AEGIS) sprintf(Command,"Plot:plot2draw t:plotfile.txt0 %s.draw",FileNameSIBuff);
             if (Form==MCAD) sprintf(Command,"Plot:plot2mcad t:plotfile.txt0 %s.mcad",FileNameSIBuff);
             Execute(Command,0,0);
             DeleteFile("t:plotfile.txt0");
             ClearPointer(p_Window);
            }
          if ((PlotType==PLOT3D)&&(CheckFile()!=NULL))
            {
             stpcpy(String,"");
             if (NET==OFF) strcat(String,"-n ");
             if (AXES==OFF) strcat(String,"-a ");
             if (TWOSIDES==ON) strcat(String,"-2 ");
             sprintf(Command,"copy %s t:plotfile \n cd t: \n Plot:dat_2_bin t:plotfile \n Plot:threedplot %st2f.out \n endcli",FileNameSIBuff,String);
             Monitor=Open("CON:0/46/640/120/     ThreeDPlot...    <<  Calculating Screen Image  >>     Please Wait   .",MODE_NEWFILE);
             SetPointer(p_Window,WaitSprite,38,16,-4,-4);
             Execute(Command,Monitor,0);
             Close(Monitor);
             DrawScreen();
             DeleteFile("t:plotfile");
             DeleteFile("t:t2f.out");
             sprintf(Command,"Plot:txt_2_icad t:threedplot.txt %s.cad",FileNameSIBuff);
             Execute(Command,0,0);
             DeleteFile("t:threedplot.txt");
             ClearPointer(p_Window);
           }
        }
      if (Pointer == (APTR)&EditButton)  Edit();

     }
}


void Edit()
 {
   if  ((CheckFile()!=NULL) || AutoRequest(p_Window,&NewFileText1,&NewFileText2,&NewFileText3,NULL,NULL,320,70))
      {
          sprintf(Command,"%s %s",Editor,FileNameSIBuff);
          Execute(Command,0,0);
          DrawScreen();
      }
 }

void FileReq()
{
struct Process  *OurTask;
struct Window   *old_pr_WindowPtr;

           OurTask = (struct Process *)FindTask(0L);
           old_pr_WindowPtr = OurTask->pr_WindowPtr;
           OurTask->pr_WindowPtr = p_Window;
           get_fname(p_Window,p_Screen,"Select Data File",def_name,def_dir);
           OurTask->pr_WindowPtr = old_pr_WindowPtr;
           RemoveGadget(p_Window,&FileName);
           stpcpy(FileNameSIBuff,"");
           strmfp(FileNameSIBuff,def_dir,def_name);
           AddGadget(p_Window,&FileName,-1);
           RefreshGadgets(&FileName,p_Window,NULL);
           DrawBorder(r,&Border3,198,50);
}


/**
*
*   This routine selects gad1 and deselects the previously selected one.
*   Maximum number of gadgets to be mutually excluded is six, obviously.
*   Extension to more gadgets is obvious, too. This routine assumes that
*   only one gadget is selected at a time.
*
**/

void GadMXSel(win,gad1,gad2,gad3,gad4,gad5,gad6)
struct Window *win;
struct Gadget *gad1,*gad2,*gad3,*gad4,*gad5,*gad6;
{
void GadMXSD();
static struct Gadget *gadprev;

   gadprev = NULL;
   if (gad1 != NULL)
      if (gad1->Flags & SELECTED) return;
   if (gad2 != NULL)
      if (gad2->Flags & SELECTED) gadprev = gad2;
   if (gad3 != NULL)
      if (gad3->Flags & SELECTED) gadprev = gad3;
   if (gad4 != NULL)
      if (gad4->Flags & SELECTED) gadprev = gad4;
   if (gad5 != NULL)
      if (gad5->Flags & SELECTED) gadprev = gad5;
   if (gad6 != NULL)
      if (gad6->Flags & SELECTED) gadprev = gad6;

   GadMXSD(win,gad1,gadprev);

   return;
}

/**
*
*   This routine selects gad1 and deselects gad2.
*   Notice, that this version removes gadgets from the gadget list and adds
*   them to the end. If you're sensitive to the location of the gadgets
*   in the gadgetlist, you have to use: gadloc = RemoveGadget... and
*   AddGadget(...,...,(long) gadloc), where gadloc is a USHORT. Then you
*   have to refresh all gadgets to make sure the two you changed get
*   refreshed.
*
**/
void GadMXSD(win,gad1,gad2)
struct Window *win;
struct Gadget *gad1,*gad2;
{
/*
*   First select gad2 (yes!) and refresh.
*/
   if (gad2 != NULL) {
      RemoveGadget(win,gad2);
      gad2->Flags |= SELECTED;
      AddGadget(win,gad2,-1L);
   }

   if (gad2 != NULL) RefreshGadgets(gad2,win,NULL);
/*
*   Now select gad1 and deselect gad2 and refresh.
*/
   if (gad1 != NULL) {
      RemoveGadget(win,gad1);
      gad1->Flags |= SELECTED;
      AddGadget(win,gad1,-1L);
   }

   if (gad2 != NULL) {
      RemoveGadget(win,gad2);
      gad2->Flags &= ~SELECTED;
      AddGadget(win,gad2,-1L);
   }

   if (gad1 != NULL) RefreshGadgets(gad1,win,NULL);

   return;
}

void DrawScreen()
{
  BeginRefresh(p_Window);
  PrintIText(r,&IText5,0,0);
  EndRefresh(p_Window,TRUE);
}

void ShowAdvert()
{
    AutoRequest(p_Window,&AText1,NULL,&AText6,NULL,NULL,328,94);
    AutoRequest(p_Window,&A2Text1,NULL,&AText6,NULL,NULL,328,94);

}


void DefineEditor()
{
USHORT EdQuitFlag, UseFlag;
ULONG EdMesClass;                          /*     Fields for storing      */
APTR EdPointer;                            /*                             */
struct IntuiMessage  *p_Edmessage;         /*      pointer to message     */
void print_err();

  EdQuitFlag=GO;
  UseFlag=OFF;
  stpcpy(EdSIBuff,Editor);
  if( (p_EdWindow = (struct Window *)OpenWindow(&EdWindow))==NULL)
    {
    print_err("Unable to open define editor window");
    exit(10);
    }
  p = p_EdWindow->RPort;
  PrintIText(p,&WText3,0,0);
  while (EdQuitFlag !=QUIT)
    {
       Wait(1l<<p_EdWindow->UserPort->mp_SigBit);        /* wait for a message */
       while (p_Edmessage = (struct IntuiMessage *)GetMsg(p_EdWindow->UserPort))
        {
          EdMesClass = p_Edmessage->Class;             /* Store values */
          EdPointer = p_Edmessage->IAddress;
          ReplyMsg(p_Edmessage);                     /* Reply to message */
          if ( EdMesClass == GADGETDOWN)
           {
             if (EdPointer == (APTR)&WGadget2)
               {
                EdQuitFlag=QUIT;
                UseFlag=ON;
               }
             if (EdPointer == (APTR)&WGadget3)
               {
                EdQuitFlag=QUIT;
               }
           }
        }
    }
   CloseWindow(p_EdWindow);
   if (UseFlag==ON) stpcpy(Editor,EdSIBuff);
}

void FixPlotTypeGadgets()
{
      if (PlotType==PLOT3D)
           GadMXSel(p_Window,&Plot3DButton,&Plot2DButton,NULL,NULL,NULL,NULL);
      if (PlotType == PLOT2D)
           GadMXSel(p_Window,&Plot2DButton,&Plot3DButton,NULL,NULL,NULL,NULL);
}


void LoadDef()
{
void ReadDef();
  ClearMenuStrip(p_Window);
  ReadDef();
  FixPlotTypeGadgets();
  SetMenuStrip(p_Window,&Menu1);
}


void ReadDef()
{
int a,b;

   fp1 = NULL;
   fp1 = fopen("plot.def","r");
   if (fp1==NULL) fp1 = fopen("s:plot.def","r");
   if (fp1!=NULL)
      {
         fscanf(fp1,"%d %d",&a,&b);
         fgets(Editor,50,fp1);
         fclose(fp1);
         if (a==INTRO)
           {
              MenuItem2.Flags= CHECKED+CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
              MenuItem3.Flags= CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
              MenuItem1.Flags= CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
           }
         else if (a==MCAD)
           {
              MenuItem3.Flags= CHECKED+CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
              MenuItem2.Flags= CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
              MenuItem1.Flags= CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
           }
         else
           {
              MenuItem1.Flags= CHECKED+CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
              MenuItem2.Flags= CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
              MenuItem3.Flags= CHECKIT+ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP;
           }

           Form=a;
           PlotType=b;
        }

}


void SaveDef()
{
   fp1 = fopen("s:plot.def","w");
   if (fp1!=NULL)
      {
         fprintf(fp1,"%d %d %s",Form,PlotType,Editor);
         fclose(fp1);
      }
}

CheckFile()
{
BPTR lock;

    lock=Lock(FileNameSIBuff,ACCESS_READ);
    if (lock!=NULL)
      {
        UnLock(lock);
        return(1);
      }
    AutoRequest(p_Window,&FileCheckText1,&FileCheckText2,&FileCheckText3,NULL,NULL,320,70);
    return(0);
}

void print_err(message)
char message[];
{
SetWindowTitles(p_Window,message,NULL);
Delay(200);
SetWindowTitles(p_Window,Window.Title,NULL);
}

void CloseDown()
{
  CloseFont(tf);
  ClearMenuStrip(p_Window);
  CloseWindow(p_Window);       /* close window */
  CloseLibrary(IntuitionBase);
  CloseLibrary(DosBase);
  CloseLibrary(DiskfontBase);
  CloseLibrary(GfxBase);
  exit(TRUE);                /* Good Bye */
}




