#include <stdio.h>
#include <exec/types.h>
#include "struct.h"
#include "plot.h"
#include "howto1.h"
#include "howto2.h"

extern char *gets();
extern char *getwrd();
extern struct Window *FrontWindow;
extern struct Screen *screen;

extern struct NewWindow NewFrontWindow;
extern struct RastPort *p;
extern struct ViewPort *vp;

extern int debug;

#define LINEPLOT 0
#define POINTPLOT 1
USHORT PlotType;

#define DEFAULT 0
#define CHOOSE 1
USHORT PlotPrefs;

#define STOP 0
#define GO 1
extern int QuitFrontFlag;
short firstcall = TRUE;


void GetHowTo(Pict)
struct Pict *Pict;
{
   short i;
   struct Plot *Plot;
   struct IntuiMessage  *p_message;         /* pointer to message */
   void ProcHowMes(), ProcCustMes(), GadMXSel();

   QuitFrontFlag=GO;

   if (debug) {
      printf("GetHowTo: entry\n");
      printf("   Pict->ErrBar = %d\n",Pict->ErrBar);
      printf("   Pict->ShowErr = %d\n",Pict->ShowErr);
   }
   if (firstcall) {
      firstcall = FALSE;
      /*** ESTABLISH DEFAULTS ***/
      PlotType=LINEPLOT;
      Pict->Grid = FALSE;
      Pict->ShowErr = Pict->ErrBar;
      Pict->Tics->NX = Pict->Tics->NY = 5;
   }

   PlotPrefs=DEFAULT;
   Gadget18.Flags=(GADGHIMAGE+GADGIMAGE);


   if (Pict->Grid == TRUE) Gadget15.Flags=(GADGHIMAGE+SELECTED+GADGIMAGE);
   else Gadget15.Flags=(GADGHIMAGE+GADGIMAGE);

   if (PlotType == POINTPLOT) Gadget16.Flags=(GADGHIMAGE+SELECTED+GADGIMAGE);
   else Gadget16.Flags=(GADGHIMAGE+GADGIMAGE);

   if (Pict->ErrBar==FALSE) Gadget14.Flags=(GADGHIMAGE+SELECTED+GADGDISABLED+GADGIMAGE);
   else if (Pict->ShowErr==FALSE) Gadget14.Flags=(GADGHIMAGE+SELECTED+GADGIMAGE);
   else Gadget14.Flags=(GADGHIMAGE+GADGIMAGE);

   NewFrontWindow.Title = title0;
   NewFrontWindow.FirstGadget = &Gadget13;
   NewFrontWindow.Screen = screen;
   FrontWindow = (struct Window *)OpenWindow(&NewFrontWindow);

   p = FrontWindow->RPort;
   PrintIText(p,&IText11,0,0);

  while (QuitFrontFlag !=STOP)
    {
       Wait(1l<<FrontWindow->UserPort->mp_SigBit);        /* wait for a message */
       while (p_message = (struct IntuiMessage *)GetMsg(FrontWindow->UserPort))
         ProcHowMes(p_message,Pict);

     }

   CloseWindow(FrontWindow);
   if (Gadget17SInfo.LongInt<1)
      {
         Gadget17SInfo.LongInt=1;
         strcpy(Gadget17SIBuff,"1");
      }
   if (Gadget17SInfo.LongInt>20)
      {
         Gadget17SInfo.LongInt=20;
         strcpy(Gadget17SIBuff,"20");
      }

   Pict->Tics->NX = Gadget17SInfo.LongInt;
   Pict->Tics->NY = Pict->Tics->NX;


   if (PlotPrefs==DEFAULT) {

      Plot = Pict->Plot;
      i = 0;
      while (Plot) {
         Plot->Enabled = TRUE;
         Plot->Color = PLOTCOLORBASE + i;
         if (PlotType==POINTPLOT) Plot->PointSize = DEFAULT_POINT_SIZE;
         else  Plot->PointSize = 0;

         while (Plot->Continued) {
            Plot->NextPlot->Color = Plot->Color;
            Plot->NextPlot->PointSize = Plot->PointSize;
            Plot->NextPlot->Enabled = Plot->Enabled;
            Plot = Plot->NextPlot;
         }
         Plot = Plot->NextPlot;
         i++;
      }
   }

   else {
      /* INIT ALL PLOTS TO "DISABLED" */
      Plot = Pict->Plot;
      while (Plot) {Plot->Enabled = FALSE; Plot = Plot->NextPlot;}

      /* GET USER'S INSTRUCTIONS FOR EACH PLOT */
      Plot = Pict->Plot;

    for(i=0;i<Pict->NPlt;i++) {
      IText16.IText=SetText[i];
      NewFrontWindow.Title = title1;
      NewFrontWindow.FirstGadget = &Gadget43;
      NewFrontWindow.Screen = screen;
      FrontWindow = (struct Window *)OpenWindow(&NewFrontWindow);
      if (i==0) GadMXSel(FrontWindow,&Gadget45,&Gadget44,NULL,NULL,NULL,NULL);

      p = FrontWindow->RPort;
      PrintIText(p,&IText16,0,0);

      QuitFrontFlag=GO;

      while (QuitFrontFlag !=STOP)
         {
            Wait(1l<<FrontWindow->UserPort->mp_SigBit);        /* wait for a message */
            while (p_message = (struct IntuiMessage *)GetMsg(FrontWindow->UserPort))
              ProcCustMes(p_message,Pict);

         }

         if (Gadget44.Flags & SELECTED)  Plot->Enabled = FALSE;

         else {
            Plot->Enabled = TRUE;

            if (Gadget45.Flags & SELECTED) Plot->Color = PLOTCOLORBASE + i;
            else Plot->Color = PLOTCOLORBASE + Gadget48SInfo.LongInt;

            if (Gadget47.Flags & SELECTED)  Plot->PointSize = Gadget46SInfo.LongInt;
            else  Plot->PointSize = 0; /* GETS LINE PLOT */
         }

         while (Plot->Continued) {
            Plot->NextPlot->Color = Plot->Color;
            Plot->NextPlot->PointSize = Plot->PointSize;
            Plot->NextPlot->Enabled = Plot->Enabled;
            Plot = Plot->NextPlot;
         }
         Plot = Plot->NextPlot;
         CloseWindow(FrontWindow);
      }
   }
   if (debug) printf("GetHowTo: exit\n");

}


void ProcHowMes(p_message,Pict)
struct IntuiMessage *p_message;
struct Pict *Pict;

{
ULONG MesClass;        /*     Fields for storing      */
USHORT MesCode;        /*     intuimessage data       */
APTR Pointer;          /*                             */
void HandleHowEvent();

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

void ProcCustMes(p_message,Pict)
struct IntuiMessage *p_message;
struct Pict *Pict;

{
ULONG MesClass;        /*     Fields for storing      */
USHORT MesCode;        /*     intuimessage data       */
APTR Pointer;          /*                             */
void HandleCustEvent();

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


void HandleHowEvent(MesClass,MesCode,Pointer,Pict)
struct Pict *Pict;

ULONG MesClass;        /*     Fields for storing      */
USHORT MesCode;        /*     intuimessage data       */
APTR Pointer;          /*                             */
{

  if ( MesClass == GADGETDOWN)
    {

      if (Pointer == (APTR)&Gadget13)  QuitFrontFlag = STOP;
      if (Pointer == (APTR)&Gadget14)
          {
            if (Gadget14.Flags & SELECTED) Pict->ShowErr = FALSE;
            else  Pict->ShowErr = Pict->ErrBar;
          }
      if (Pointer == (APTR)&Gadget15)
          {
            if (Pict->Grid==TRUE) Pict->Grid=FALSE;
            else Pict->Grid=TRUE;
          }
      if (Pointer == (APTR)&Gadget16)
          {
            if (PlotType==LINEPLOT) PlotType=POINTPLOT;
            else PlotType=LINEPLOT;
          }
      if (Pointer == (APTR)&Gadget18)
          {
            if (PlotPrefs==DEFAULT) PlotPrefs=CHOOSE;
            else PlotPrefs=DEFAULT;
          }
      else ;
    }
  if ( MesClass == RAWKEY)
    {
      if (MesCode ==196)  /* RETURN key RELEASED */
         {
            QuitFrontFlag = STOP;
         }
      else ;
    }
  else ;
}

void HandleCustEvent(MesClass,MesCode,Pointer,Pict)
struct Pict *Pict;

ULONG MesClass;        /*     Fields for storing      */
USHORT MesCode;        /*     intuimessage data       */
APTR Pointer;          /*                             */
{
void GadMXSel(), GadMXSD();


   if ( MesClass == GADGETDOWN)
     {
        if (Pointer == (APTR)&Gadget45)
          {
             if (Gadget45.Flags & SELECTED)
               {
                  RemoveGadget(FrontWindow,&Gadget45);
                  Gadget45.Flags &= ~SELECTED;
                  AddGadget(FrontWindow,&Gadget45,-1L);
                  RefreshGadgets(&Gadget45,FrontWindow,NULL);
                }
             else
                {
                   GadMXSel(FrontWindow,&Gadget45,&Gadget44,NULL,NULL,NULL,NULL);
                 }
          }
        else if (Pointer == (APTR)&Gadget44)
          {
             if (Gadget44.Flags & SELECTED)
               {
                  RemoveGadget(FrontWindow,&Gadget44);
                  Gadget44.Flags &= ~SELECTED;
                  AddGadget(FrontWindow,&Gadget44,-1L);
                  RefreshGadgets(&Gadget44,FrontWindow,NULL);
                }
             else
                {
                   GadMXSel(FrontWindow,&Gadget44,&Gadget45,NULL,NULL,NULL,NULL);
                 }
          }

        else if (Pointer == (APTR)&Gadget43) QuitFrontFlag = STOP;
        else ;
     }
   if ( MesClass == RAWKEY)
     {
      if (MesCode ==196)  /* RETURN key RELEASED */
         {
             QuitFrontFlag = STOP;
         }
      else ;
     }
   else ;
}

/**
*
*   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;
}






