/*----------------------------------------------------------------------------*
 *                                                                            *
 *  AGA-Morph-Manage.c V1.2                                                   *
 *                                                                            *
 *----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/

#include <libraries/diskfont.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/pointerclass.h>
#include <datatypes/pictureclass.h>
#include <datatypes/pictureclassext.h>
#include <graphics/gfx.h>
#include <exec/memory.h>
#include <libraries/asl.h>
#include <clib/macros.h>

#include "AGA-Morph-Language.h"
#include "AGA-Morph-Struct.h"

/*----------------------------------------------------------------------------*/

#define GadgetOn(g,w,r) {Pos=RemoveGadget(w,g); (g)->Flags-=((g)->Flags & GFLG_DISABLED); AddGadget(w,g,Pos); RefreshGList(g,w,r,1);}
#define GadgetOff(g,w,r) {Pos=RemoveGadget(w,g); (g)->Flags=((g)->Flags | GFLG_DISABLED); AddGadget(w,g,Pos); RefreshGList(g,w,r,1);}

/*----------------------------------------------------------------------------*/

extern struct DPoint Points;
extern struct Gadget AddPButton,Change1Button,Change2Button,ChangeColorButton,
                     ChangeSpButton,DeleteAllButton,DelPButton,DPicX,DPicY,
                     EditWindowBar1,EditWindowBar2,ErrorCloseButton,InfoButton,
                     Load1Button,Load2Button,LoadPButton,Pic2Slider,Pic2Val,
                     Poi2Slider,Poi2Val,PointsB,PointsButton,QuitButton,
                     RenderButton,ResetSplinesButton,SavePButton,SavePicButton,
                     Show1Button,Show2Button,ShowRButton,SpLineButton,
                     ViewWindowBar1,ViewWindowBar2,ZoomButton;
extern struct IntuiText ErrorCloseButtonT;
extern struct MPoint *Point1,*Point2;
extern struct Screen *MorphScreen;
extern struct StringInfo PointsBp;
extern struct Window *CommandWindow,*EditWindow,*ErrorWindow,*ViewWindow;
extern LONG i,j,k,Pos;
extern ULONG Height,Width;
extern UBYTE PointsBBuf[15];

/*----------------------------------------------------------------------------*/

void RethinkSelectedPoint();                             /* Internal use only */
void DeletePoints();
void DisableAll();
void EnableAll();
void ShowError(struct IntuiText *Error);
void Hex(char *text,ULONG num);
void SHex(char *text,LONG num);
void Dez(char *text,LONG num);
void WDez(char *text,WORD num);

/*----------------------------------------------------------------------------*/

void
RethinkSelectedPoint()
{
  Pos=RemoveGadget(CommandWindow,&PointsB);
  PointsBp.LongInt=MAX(MIN(PointsBp.LongInt,Points.NumPoints),0);
  if (PointsBp.LongInt==0)
  {
    PointsBp.NumChars=1;
    PointsBBuf[0]=48+0;
    PointsBBuf[1]=0;
  }
  else
  {
    PointsBp.NumChars=0;
    k=PointsBp.LongInt;
    j=1000000000;
    for (i=9;i>=0;i--)
    {
      if (PointsBp.LongInt>=j)
      {
        PointsBBuf[PointsBp.NumChars]=48+k/j;
        PointsBp.NumChars++;
        k=k%j;
      }
      j=j/10;
    }
    PointsBBuf[PointsBp.NumChars]=0;
  }
  AddGadget(CommandWindow,&PointsB,Pos);
  RefreshGList(&PointsB,CommandWindow,0,1);
}

/*----------------------------------------------------------------------------*/

void
DeletePoints()
{
  Points.NumPoints=0;
  Point1=Points.First;
  while (Point1)
  {
    Point2=Point1;
    Point1=Point1->Next;
    FreeVec(Point2);
  }
  RethinkSelectedPoint();
}

/*----------------------------------------------------------------------------*/

void
DisableAll()
{
  GadgetOff(&Load1Button,CommandWindow,0);
  GadgetOff(&Load2Button,CommandWindow,0);
  GadgetOff(&LoadPButton,CommandWindow,0);
  GadgetOff(&SavePicButton,CommandWindow,0);
  GadgetOff(&SavePButton,CommandWindow,0);
  GadgetOff(&Change1Button,CommandWindow,0);
  GadgetOff(&Change2Button,CommandWindow,0);
  GadgetOff(&ChangeSpButton,CommandWindow,0);
  GadgetOff(&AddPButton,CommandWindow,0);
  GadgetOff(&DelPButton,CommandWindow,0);
  GadgetOff(&Pic2Slider,CommandWindow,0);
  GadgetOff(&Pic2Val,CommandWindow,0);
  GadgetOff(&Poi2Slider,CommandWindow,0);
  GadgetOff(&Poi2Val,CommandWindow,0);
  GadgetOff(&SpLineButton,CommandWindow,0);
  GadgetOff(&RenderButton,CommandWindow,0);
  GadgetOff(&Show1Button,CommandWindow,0);
  GadgetOff(&Show2Button,CommandWindow,0);
  GadgetOff(&ShowRButton,CommandWindow,0);
  GadgetOff(&InfoButton,CommandWindow,0);
  GadgetOff(&ZoomButton,CommandWindow,0);
  GadgetOff(&QuitButton,CommandWindow,0);
  GadgetOff(&DPicX,CommandWindow,0);
  GadgetOff(&DPicY,CommandWindow,0);
  GadgetOff(&PointsB,CommandWindow,0);
  GadgetOff(&DeleteAllButton,CommandWindow,0);
  GadgetOff(&ResetSplinesButton,CommandWindow,0);
  GadgetOff(&PointsButton,CommandWindow,0);
  GadgetOff(&ChangeColorButton,CommandWindow,0);

  GadgetOff(&EditWindowBar1,EditWindow,0);
  GadgetOff(&EditWindowBar2,EditWindow,0);

  GadgetOff(&ViewWindowBar1,ViewWindow,0);
  GadgetOff(&ViewWindowBar2,ViewWindow,0);
}

/*----------------------------------------------------------------------------*/

void
EnableAll()
{
  GadgetOn(&Load1Button,CommandWindow,0);
  GadgetOn(&Load2Button,CommandWindow,0);
  GadgetOn(&LoadPButton,CommandWindow,0);
  GadgetOn(&SavePicButton,CommandWindow,0);
  GadgetOn(&SavePButton,CommandWindow,0);
  GadgetOn(&Change1Button,CommandWindow,0);
  GadgetOn(&Change2Button,CommandWindow,0);
  if (SpLineButton.Flags & GFLG_SELECTED)
  {
    GadgetOn(&ChangeSpButton,CommandWindow,0);
  }
  GadgetOn(&AddPButton,CommandWindow,0);
  GadgetOn(&DelPButton,CommandWindow,0);
  GadgetOn(&Pic2Slider,CommandWindow,0);
  GadgetOn(&Pic2Val,CommandWindow,0);
  GadgetOn(&Poi2Slider,CommandWindow,0);
  GadgetOn(&Poi2Val,CommandWindow,0);
  GadgetOn(&SpLineButton,CommandWindow,0);
  GadgetOn(&RenderButton,CommandWindow,0);
  GadgetOn(&Show1Button,CommandWindow,0);
  GadgetOn(&Show2Button,CommandWindow,0);
  GadgetOn(&ShowRButton,CommandWindow,0);
  GadgetOn(&InfoButton,CommandWindow,0);
  GadgetOn(&ZoomButton,CommandWindow,0);
  GadgetOn(&QuitButton,CommandWindow,0);
  GadgetOn(&DPicX,CommandWindow,0);
  GadgetOn(&DPicY,CommandWindow,0);
  GadgetOn(&PointsB,CommandWindow,0);
  GadgetOn(&DeleteAllButton,CommandWindow,0);
  GadgetOn(&PointsButton,CommandWindow,0);
  GadgetOn(&ChangeColorButton,CommandWindow,0);

  GadgetOn(&EditWindowBar1,EditWindow,0);
  GadgetOn(&EditWindowBar2,EditWindow,0);

  GadgetOn(&ViewWindowBar1,ViewWindow,0);
  GadgetOn(&ViewWindowBar2,ViewWindow,0);
}

/*----------------------------------------------------------------------------*/

void
ShowError(struct IntuiText *Error)
{
  struct IntuiMessage *Mess2;
  ErrorCloseButtonT.NextText=Error;
  if (ErrorWindow=(struct Window *) OpenWindowTags(NULL,
                                                   WA_Left,          (Width-240)/2,
                                                   WA_Top,           (Height-100)/2,
                                                   WA_Width,         240,
                                                   WA_Height,        100,
                                                   WA_Title,         Error_Window,
                                                   WA_CustomScreen,  MorphScreen,
                                                   WA_Gadgets,       &ErrorCloseButton,
                                                   WA_Activate,      TRUE,
                                                   WA_GimmeZeroZero, TRUE,
                                                   WA_IDCMP,         IDCMP_GADGETUP,
                                                   TAG_END))
  {
    DisableAll();
    while (Mess2=(struct IntuiMessage *) GetMsg(CommandWindow->UserPort))
    {
      ReplyMsg((struct Message *) Mess2);
    }
  }
  else
  {
    puts(Error_Error);
    puts(Error->IText);
  }
}

/*----------------------------------------------------------------------------*/

void
Hex(char *text,ULONG num)
{
  ULONG d,h;
  WORD i;
  d=num;
  for (i=7;i>=0;i--)
  {
    h=d & 0xF;
    if (h<10)
    {
      *(text+i)=(char) 48+h;
    }
    else
    {
      *(text+i)=(char) 55+h;
    }
    d=d >> 4;
  }
}

/*----------------------------------------------------------------------------*/

void
SHex(char *text,LONG num)
{
  ULONG d,h;
  WORD i;
  if (num<0)
  {
    *(text)=(char) 45;
    d=-num;
  }
  else
  {
    *(text)=(char) 43;
    d=num;
  }
  for (i=7;i>=0;i--)
  {
    h=d & 0xF;
    if (h<10)
    {
      *(text+i+1)=(char) 48+h;
    }
    else
    {
      *(text+i+1)=(char) 55+h;
    }
    d=d >> 4;
  }
}

/*----------------------------------------------------------------------------*/

void
Dez(char *text,LONG num)
{
  ULONG d,h;
  WORD i;
  d=num;
  i=9;
  while ((i>=0) && (d>0))
  {
    h=d % 10;
    *(text+i)=(char) 48+h;
    d=(d-h)/10;
    i--;
  }
  if (i==9)
  {
    *(text+i)=(char) 48;
    i--;
  }
  if (i>=0)
  {
    for (;i>=0;i--)
    {
      *(text+i)=(char) 32;
    }
  }
}

/*----------------------------------------------------------------------------*/

void
WDez(char *text,WORD num)
{
  UWORD d,h;
  WORD i;
  d=num;
  i=4;
  while ((i>=0) && (d>0))
  {
    h=d % 10;
    *(text+i)=(char) 48 +h;
    d=(d-h)/10;
    i--;
  }
  if (i==4)
  {
    *(text+i)=(char) 48;
    i--;
  }
  if (i>=0)
  {
    for (;i>=0;i--)
    {
      *(text+i)=(char) 32;
    }
  }
}

/*----------------------------------------------------------------------------*/

/* End of Text */
