/*
 *  Source machine generated by GadToolsBox V1.4
 *  which is (c) Copyright 1991,92 Jaba Development
*/

// Ja ja, und ich hab nur zugeschaut...
// LaRev: 19.1.93 - 17:53


#include "Datix.h"
#include "DatixDefs.h"

BOOL OpenLibs (void);
void CloseLibs (void);
BOOL TeilMit (STRPTR);
static void ComputeFont (UWORD, UWORD);

static UBYTE *buf, *savebuf, *FilePath, Timebuffer[5];
static UBYTE Filebuffer[8] = {0x01,0x01,0x07,0xC9,0x0C,0x00,0x00,0x00};
static UBYTE FileName[128] = {"Work:Datix.dax"};
static UWORD count;
static ULONG filehandle, delaycount = 0L;
BOOL SlowButton = TRUE;

extern struct Library *SysBase;
extern struct Library *DOSBase;

void main (int argc, char *argv[])
{
   BOOL FromWB;
   struct WBStartup *WBenchMsg;
   struct WBArg     *wbarg;
   long olddir = 0L;

   FromWB = (argc) ? FALSE : TRUE;

   if (OpenLibs())
   {
      if (FromWB)
      {
         WBenchMsg = (struct WBStartup *) argv;

         wbarg = WBenchMsg->sm_ArgList;

         if (wbarg->wa_Lock && *wbarg->wa_Name)
            olddir = CurrentDir (wbarg->wa_Lock);

         if (DatixIcon = GetDiskObject ("Datix"))
         {
            if (FilePath = FindToolType (DatixIcon->do_ToolTypes, "FILE"))
            {
               strcpy (FileName, FilePath);
            }
            if (FilePath = FindToolType (DatixIcon->do_ToolTypes, "BUTDEL"))
            {
               sscanf (FilePath, "%8d", (char *) &delaycount);
            }
            
            FreeDiskObject (DatixIcon);
         }
         CurrentDir (olddir);
      }

      if (Scr = LockPubScreen (PubScreenName))
      {
         ComputeFont( 0, 0 );

         if (VisualInfo = GetVisualInfo (Scr, TAG_DONE))
         {
            if (OpenDatixWindow())
            {
               TagDings   = DatixGadgets[0];
               MonatDings = DatixGadgets[3];
               JahrDings  = DatixGadgets[1];
               ZeitDings  = DatixGadgets[2];
               buf = (savebuf = Filebuffer);

               if (filehandle = Open (FileName, MODE_OLDFILE))
               {
                  do
                  {
                     count = Read (filehandle, buf, 8);
                  }while (count);

                  Close (filehandle);
               }
               else TeilMit ("Konnte das Datenfile nicht öffnen !");

               GT_SetGadgetAttrs (TagDings,DatixWnd,NULL,GTIN_Number, *buf++, TAG_DONE);
               GT_SetGadgetAttrs (MonatDings,DatixWnd,NULL,GTST_String, months[(*buf++)], TAG_DONE);
               GT_SetGadgetAttrs (JahrDings,DatixWnd,NULL,GTIN_Number, *((UWORD *) buf), TAG_DONE);
               buf += 2;
               sprintf (Timebuffer, "%d:%d", *buf++, *buf);
               GT_SetGadgetAttrs (ZeitDings,DatixWnd,NULL,GTST_String, Timebuffer, TAG_DONE);

               buf = savebuf;
               HandleDatixIDCMP();
            }
         }
         else TeilMit ("Habe kein VisualInfo erhalten !");
      }
      else TeilMit ("Konnte mich für keinen Screen entscheiden !");
   }

   CloseDatixWindow();
   CloseDownScreen();
   CloseLibs();
}

BOOL OpenLibs (void)
{
   if (IntuitionBase = OpenLibrary ("intuition.library", 37))
   {
      if (GfxBase = OpenLibrary ("graphics.library", 37))
      {
         if (GadToolsBase = OpenLibrary ("gadtools.library", 37))
         {
            if (IconBase = OpenLibrary ("icon.library", 33))
               return (TRUE);
            else TeilMit ("Konnte die Icon Library nicht öffnen");
         }
         else TeilMit ("Konnte die GadTools Library nicht öffnen");
      }
      else TeilMit ("Konnte die Graphics Library (V37) nicht öffnen");
   }
   else printf ("Konnte die Intuition Library (V37) nicht öffnen");

   CloseLibs();
   return (FALSE);
}

void CloseLibs (void)
{
   if (IconBase)        CloseLibrary (IconBase);
   if (GadToolsBase)    CloseLibrary (GadToolsBase);
   if (GfxBase)         CloseLibrary (GfxBase);
   if (IntuitionBase)   CloseLibrary (IntuitionBase);
}

BOOL TeilMit (STRPTR Fehlermeldung)
{
   LONG DummyNumber = 0L;

   if (*Fehlermeldung)
   {
      EasyRequest (NULL, &EStruct, NULL, Fehlermeldung, DummyNumber);
   }
   return (TRUE);
}

static UWORD ComputeX( UWORD value )
{
        return((UWORD) (((FontX * value) + 4) >> 3));
}

static UWORD ComputeY( UWORD value )
{
        return((UWORD) (((FontY * value) + 4) >> 3));
}

static void ComputeFont( UWORD width, UWORD height )
{
        Font = &Attr;
        Font->ta_Name = (STRPTR)Scr->RastPort.Font->tf_Message.mn_Node.ln_Name;
        Font->ta_YSize = FontY = Scr->RastPort.Font->tf_YSize;
        FontX = Scr->RastPort.Font->tf_XSize;

        OffX = Scr->WBorLeft;
        OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;

        if ( width && height )
   {
                if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
                        goto UseTopaz;
                if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
                        goto UseTopaz;
        }
        return;

UseTopaz:
        Font->ta_Name = (STRPTR)"topaz.font";
        FontX = FontY = Font->ta_YSize = 8;
}


void CloseDownScreen( void )
{
        if ( VisualInfo ) {
                FreeVisualInfo( VisualInfo );
                VisualInfo = NULL;
        }

        if ( Scr        ) {
                UnlockPubScreen( NULL, Scr );
                Scr = NULL;
        }
}

void DatixRender( void )
{
        struct IntuiText        it;
        UWORD                   cnt;

        ComputeFont( DatixWidth, DatixHeight );

        DrawBevelBox( DatixWnd->RPort, OffX + ComputeX( 14 ),
                                        OffY + ComputeY( 16 ),
                                        ComputeX( 337 ),
                                        ComputeY( 79 ),
                                        GT_VisualInfo, VisualInfo, TAG_DONE );

        for ( cnt = 0; cnt < Datix_TNUM; cnt++ ) {
                CopyMem(( char * )&DatixIText[ cnt ], ( char * )&it, (long)sizeof( struct IntuiText ));
                it.ITextFont = Font;
                it.LeftEdge  = OffX + ComputeX( it.LeftEdge ) - ( IntuiTextLength( &it ) >> 1 );
                it.TopEdge   = OffY + ComputeY( it.TopEdge ) - ( Font->ta_YSize >> 1 );
                PrintIText( DatixWnd->RPort, &it, 0, 0 );
        }
}


int OpenDatixWindow(void)
{
        struct NewGadget        ng;
        struct Gadget   *g;
        UWORD    lc, tc;
        UWORD    wleft = DatixLeft, wtop = DatixTop, ww, wh;

        ComputeFont (DatixWidth, DatixHeight);

        ww = ComputeX (DatixWidth);
        wh = ComputeY (DatixHeight);

        if (( wleft + ww + OffX + Scr->WBorRight ) > Scr->Width )
      wleft = Scr->Width - ww;
        if (( wtop + wh + OffY + Scr->WBorBottom ) > Scr->Height )
      wtop = Scr->Height - wh;

        if (!(g = CreateContext( &DatixGList )))
   {
      TeilMit ("Konnte keinen Context kreieren !");
                return (0);
   }

        for( lc = 0, tc = 0; lc < Datix_CNT; lc++ )
   {
                CopyMem((char * )&DatixNGad[lc], (char *)&ng,
              (long)sizeof( struct NewGadget ));

                ng.ng_VisualInfo = VisualInfo;
                ng.ng_TextAttr   = Font;
                ng.ng_LeftEdge   = OffX + ComputeX( ng.ng_LeftEdge );
                ng.ng_TopEdge    = OffY + ComputeY( ng.ng_TopEdge );
                ng.ng_Width      = ComputeX( ng.ng_Width );
                ng.ng_Height     = ComputeY( ng.ng_Height);

                DatixGadgets[lc] = g = CreateGadgetA(
      (ULONG)DatixGTypes[lc], g, &ng, (struct TagItem *) &DatixGTags[tc]);

                while( DatixGTags[ tc ] ) tc += 2;
                tc++;

                if (!g)
      {
         TeilMit ("Fehler beim Erstellen der Gadgets !");
                        return (0);
      }
        }

        DatixZoom[0] = 0;
        DatixZoom[1] = 0;
        DatixZoom[2] = 168;
        DatixZoom[3] = 11;

        if ( !( DatixWnd = OpenWindowTags(
            NULL,
                                WA_Left,        wleft,
                                WA_Top,         wtop,
                                WA_Width,       ww + OffX + Scr->WBorRight,
                                WA_Height,      wh + OffY + Scr->WBorBottom,
                                WA_IDCMP,       INTEGERIDCMP|STRINGIDCMP|BUTTONIDCMP|IDCMP_GADGETDOWN|IDCMP_INTUITICKS|IDCMP_MOUSEBUTTONS|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
                                WA_Flags,       WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
                                WA_Gadgets,     DatixGList,
                                WA_Title,       DatixWdt,
                                WA_ScreenTitle, "GadToolsBox v1.4 © 1991,92 ",
                                WA_Zoom,        DatixZoom,
                                TAG_DONE )))
   {
      TeilMit ("Konnte Fenster nicht öffnen !");
           return (0);
   }

        DatixZoom[0] = DatixWnd->LeftEdge;
        DatixZoom[1] = DatixWnd->TopEdge;
        DatixZoom[2] = DatixWnd->Width;
        DatixZoom[3] = DatixWnd->Height;

        GT_RefreshWindow( DatixWnd, NULL );

        DatixRender();

        return (1);
}

void CloseDatixWindow( void )
{
        if (DatixWnd)
   {
                CloseWindow (DatixWnd);
                DatixWnd = NULL;
        }

        if (DatixGList)
   {
                FreeGadgets (DatixGList);
                DatixGList = NULL;
        }
}

int HandleDatixIDCMP (void)
{
        struct IntuiMessage     *msg;
   UBYTE Order[30];
   ULONG class;
   UWORD code, CutYear;
   APTR GadID;
   BOOL done = FALSE;

   while (!done)
   {
      Wait (1L << DatixWnd->UserPort->mp_SigBit);

           while (msg = GT_GetIMsg (DatixWnd->UserPort))
      {
         class = msg->Class;
         code =  msg->Code;
         GadID = msg->IAddress;
                   GT_ReplyIMsg (msg);

                   switch (class)
         {
                           case IDCMP_REFRESHWINDOW:
                                   GT_BeginRefresh( DatixWnd );
                                   DatixRender();
                                   GT_EndRefresh( DatixWnd, TRUE );
                           break;

                           case IDCMP_CLOSEWINDOW:
               done = TRUE;
                           break;

                           case IDCMP_VANILLAKEY:

                           break;

                           case IDCMP_GADGETUP:
            switch (((struct Gadget *) GadID)->GadgetID)
            {
               case GD_Save_Button:
               {
                  if (filehandle = Open (FileName, MODE_NEWFILE))
                  {
                     Write (filehandle, Filebuffer, 8);
                     Close (filehandle);
                     done = TRUE;
                  }
               }
               GT_BeginRefresh( DatixWnd );
                                   DatixRender();
                                   GT_EndRefresh( DatixWnd, TRUE );

               CutYear = *((UWORD *) &buf[2]);
               while (CutYear > 100)
                  CutYear -= 100;

               sprintf (Order, "date %d-%d-%d %d:%d",buf[0],buf[1] + 1,CutYear,buf[4],buf[5]);
               Execute (Order, NULL, NULL);
            }
            break;

            case IDCMP_GADGETDOWN:
            SlowButton = TRUE;
            while ( (msg = (struct IntuiMessage *)
            GetMsg (DatixWnd->UserPort))->Class != GADGETUP)
            {
               switch (((struct Gadget *) GadID)->GadgetID)
               {
                  case GD_Day_Inc:
                     if ((*buf)++ > 30)
                     {
                        *buf = 0;
                        if (buf[1]++ > 10)
                        {
                           buf[1] = 0;
                           (*((WORD *) &buf[2]))++;
                        }
                     }
                  break;
                  case GD_Day_Dec:
                     if ((*buf)-- < 1)
                     {
                        *buf = 31;
                        if (buf[1]-- < 1)
                        {
                           buf[1] = 11;
                           (*((WORD *) &buf[2]))--;
                        }
                     }
                  break;
                  case GD_Month_Inc:
                     if (buf[1]++ > 10)
                     {
                        buf[1] = 0;
                        (*((WORD *) &buf[2]))++;
                     }
                  break;
                  case GD_Month_Dec:
                     if (buf[1]-- < 1)
                     {
                        buf[1] = 11;
                        (*((WORD *) &buf[2]))--;
                     }
                  break;
                  case GD_Year_Inc:
                     (*((WORD *) &buf[2]))++;
                  break;
                  case GD_Year_Dec:
                     (*((WORD *) &buf[2]))--;
                  break;
                  case GD_Hour_Inc:
                     if (buf[4]++ > 22) buf[4] = 0;
                  break;
                  case GD_Hour_Dec:
                     if (buf[4]-- < 1) buf[4] = 23;
                  break;
                  case GD_Minute_Inc:
                     if (buf[5]++ > 59)
                     {
                        buf[5] = 0;
                        if (buf[4]++ > 22) buf[4] = 0;
                     }
                  break;
                  case GD_Minute_Dec:
                     if (buf[5]-- < 1)
                     {
                        buf[5] = 59;
                        if (buf[4]-- < 1) buf[4] = 23;
                     }
                  break;

               }
               savebuf = buf;
               GT_SetGadgetAttrs (TagDings,DatixWnd,NULL,GTIN_Number, *buf++, TAG_DONE);
               GT_SetGadgetAttrs (MonatDings,DatixWnd,NULL,GTST_String, months[(*buf++)], TAG_DONE);
               GT_SetGadgetAttrs (JahrDings,DatixWnd,NULL,GTIN_Number, *((UWORD *) buf), TAG_DONE);
               buf += 2;
               sprintf (Timebuffer, "%d:%02d", *buf++, *buf);
               GT_SetGadgetAttrs (ZeitDings,DatixWnd,NULL,GTST_String, Timebuffer, TAG_DONE);
               buf = savebuf;
               
               if (SlowButton) 
               {
                  Delay (delaycount);
                  SlowButton = FALSE;
               }
            }
            break;
         }
      }
   }
}