/* SizerW.c

   by Fabbian G. Dufoe, III
      350 Ling-A-Mor Terrace South
      St. Petersburg, Florida  33705
      813-823-2350

      GENIE: F.DUFOE3

   This software is public domain.  You may use it any way you wish.

   This code handles I/O between Sizer and the user.

   Revision summary:
   15 October 1991: Initial release

   18 May 1992:
      Changed version number to 1.1.
      Changed CheckClose() and WaitClose() functions to static to match
      prototypes, moved prototypes for static functions to this file.

   13 June 1992:
      Changed version number to 1.2.
      Changed display to show old file system (OFS) blocks and fast file
      system (FFS) blocks.
      Specified TOPAZ_EIGHTY for the display font.

   24 June 1992:
      Removed copyright notice.

 -------------------------------------
|Sizer 1992 by F. G. Dufoe, III       |
|                                     |
|  Bytes:              Directories:   |
|  OFS Blocks:         Files:         |
|  FFS Blocks:                        |
|                                     |
|Program completed.                   |
|Select close gadget to exit.         |
 -------------------------------------

*/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include "Sizer.h"

#define INTUITION_REV 33
#define GRAPHICS_REV 33

#ifndef __NOPROTO                                                             /* 18 May 1992 */
#ifndef __PROTO                                                               /* 18 May 1992 */
#define __PROTO(a) a                                                          /* 18 May 1992 */
#endif                                                                        /* 18 May 1992 */
#else                                                                         /* 18 May 1992 */
#ifndef __PROTO                                                               /* 18 May 1992 */
#define __PROTO(a) ()                                                         /* 18 May 1992 */
#endif                                                                        /* 18 May 1992 */
#endif                                                                        /* 18 May 1992 */

/* Prototypes for functions defined in SizerW.c */                            /* 18 May 1992 */

static int CheckClose __PROTO((struct Window *Window));                       /* 18 May 1992 */
static void WaitClose __PROTO((struct Window *Window));                       /* 18 May 1992 */


/* Use topaz 80 for text display. */                                          /* 13 June 1992 */
static struct TextAttr TOPAZ80 =                                              /* 13 June 1992 */
{                                                                             /* 13 June 1992 */
   (STRPTR)"topaz.font", TOPAZ_EIGHTY, 0, 0                                   /* 13 June 1992 */
};                                                                            /* 13 June 1992 */

/* Initialize IntuiText structures. */
static struct IntuiText Title =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   10,         /* SHORT LeftEdge; */
   12,         /* SHORT TopEdge; */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "Sizer 1992 by F. G. Dufoe, III",                                          /* 24 June 1992 */
               /* UBYTE *IText; */
   NULL        /* struct IntuiText *NextText; */
};

static struct IntuiText Bytes =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   30,         /* SHORT LeftEdge; */
   30,         /* SHORT TopEdge; */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */
   "Bytes:",   /* UBYTE *IText; */
   &Title      /* struct IntuiText *NextText; */
};

static struct IntuiText OBlocks =                                             /* 13 June 1992 */
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   30,         /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   40,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "OFS Blocks:",                                                             /* 13 June 1992 */
               /* UBYTE *IText; */                                            /* 13 June 1992 */
   &Bytes      /* struct IntuiText *NextText; */                              /* 13 June 1992 */
};

static struct IntuiText FBlocks =                                             /* 13 June 1992 */
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   30,         /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   50,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "FFS Blocks:",                                                             /* 13 June 1992 */
               /* UBYTE *IText; */                                            /* 13 June 1992 */
   &OBlocks    /* struct IntuiText *NextText; */                              /* 13 June 1992 */
};

static struct IntuiText Dirs =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   190,        /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   30,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "Directories:",
               /* UBYTE *IText; */
   &FBlocks    /* struct IntuiText *NextText; */                              /* 13 June 1992 */
};

static struct IntuiText Files =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   190,        /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   40,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "Files:",   /* UBYTE *IText; */
   &Dirs       /* struct IntuiText *NextText; */                              /* 13 June 1992 */
};

char ByteCount[20];
static struct IntuiText ByteCt =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   86,         /* SHORT LeftEdge; */
   30,         /* SHORT TopEdge; */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   ByteCount,  /* UBYTE *IText; */
   NULL        /* struct IntuiText *NextText; */
};

char OBlockCount[20];                                                         /* 13 June 1992 */
static struct IntuiText OBlockCt =                                            /* 13 June 1992 */
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */         
   126,        /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   40,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   OBlockCount,                                                               /* 13 June 1992 */
               /* UBYTE *IText; */                                            /* 13 June 1992 */
   &ByteCt     /* struct IntuiText *NextText; */
};

char FBlockCount[20];                                                         /* 13 June 1992 */
static struct IntuiText FBlockCt =                                            /* 13 June 1992 */
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   126,        /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   50,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   FBlockCount,                                                               /* 13 June 1992 */
               /* UBYTE *IText; */                                            /* 13 June 1992 */
   &OBlockCt   /* struct IntuiText *NextText; */                              /* 13 June 1992 */
};

char DirCount[20];
static struct IntuiText DirCt =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   294,        /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   30,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   DirCount,   /* UBYTE *IText; */
   &FBlockCt   /* struct IntuiText *NextText; */                              /* 13 June 1992 */
};

char FileCount[20];
static struct IntuiText FileCt =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   246,        /* SHORT LeftEdge; */                                          /* 13 June 1992 */
   40,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   FileCount,  /* UBYTE *IText; */
   &DirCt      /* struct IntuiText *NextText; */                              /* 13 June 1992 */
};

static struct IntuiText Error =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   5,          /* SHORT LeftEdge; */
   80,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "Sizer failed.  Select close gadget to exit.",
               /* UBYTE *IText; */
   NULL        /* struct IntuiText *NextText; */
};

static struct IntuiText Done2 =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   5,          /* SHORT LeftEdge; */
   80,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "Select close gadget to exit.",
               /* UBYTE *IText; */
   NULL        /* struct IntuiText *NextText; */
};

static struct IntuiText Done =
{
   1, 0,       /* UBYTE FrontPen, BackPen; */
   JAM2,       /* UBYTE DrawMode; */
   5,          /* SHORT LeftEdge; */
   70,         /* SHORT TopEdge; */                                           /* 13 June 1992 */
   &TOPAZ80,   /* struct TextAttr *ITextFont; */                              /* 13 June 1992 */
   "Program completed.",
               /* UBYTE *IText; */
   &Done2      /* struct IntuiText *NextText; */
};

/* Initialize a NewWindow structure. */
static struct NewWindow NewWindow =
{
   0, 10,            /* SHORT LeftEdge, TopEdge */
   350, 90,          /* SHORT Width, Height */                                /* 13 June 1992 */
   0, 1,             /* UBYTE DetailPen, BlockPen */
   CLOSEWINDOW,      /* ULONG IDCMPFlags */
   WINDOWDRAG + WINDOWDEPTH + WINDOWCLOSE + SMART_REFRESH + NOCAREREFRESH,
                     /* ULONG Flags */
   NULL,             /* struct Gadget *FirstGadget */
   NULL,             /* struct Image *CheckMark */
   "Sizer v1.2",     /* UBYTE *Title */                                       /* 13 June 1992 */
   NULL,             /* struct Screen *Screen */
   NULL,             /* struct BitMap *BitMap */
   0, 0,             /* SHORT MinWidth, MinHeight */
   0, 0,             /* USHORT MaxWidth, MaxHeight */
   WBENCHSCREEN      /* USHORT Type */
};


static int                                                                    /* 18 May 1992 */
CheckClose(struct Window *Window)
{
   ULONG class;
   int flag = 0;
   struct IntuiMessage *message = NULL;

   while (message = (struct IntuiMessage *)GetMsg(Window->UserPort))
   {
      class = message->Class;
      ReplyMsg((struct Message *)message);

      if (class == CLOSEWINDOW)
         flag = 1;
   }
   return(flag);
}


void
SizerIO(struct record *Record)
{
   static struct Window *Window = NULL;
   extern struct IntuitionBase *IntuitionBase;

   if (Window == NULL)
   /* If the window isn't already open we'll try to open it.  If we can't we
      will pass a "FAIL" code back to the calling program so it will stop
      executing.
   */
   {
      if (NULL == (IntuitionBase = (struct IntuitionBase *)
                                   OpenLibrary("intuition.library",
                                   INTUITION_REV)))
      {
         Record->code = FAIL;
         return;
      }
      if ((Window = OpenWindow(&NewWindow)) == NULL)
      {
         CloseLibrary((struct Library *)IntuitionBase);
         Record->code = FAIL;
         return;
      }
      /* Write the fixed text to the window.
      */
      PrintIText(Window->RPort, &Files, 0, 0);                                /* 13 June 1992 */
   }

   /* Write the number of bytes and blocks in the window.
   */
   sprintf(ByteCount, "%ld", Record->bytes);
   sprintf(OBlockCount, "%ld", Record->OFSblocks);                            /* 13 June 1992 */
   sprintf(FBlockCount, "%ld", Record->FFSblocks);                            /* 13 June 1992 */
   sprintf(FileCount, "%ld", Record->files);
   sprintf(DirCount, "%ld", Record->dirs);
   PrintIText(Window->RPort, &FileCt, 0, 0);

   switch(Record->code)
   {
   case CLOSEIO:
   /* If Record->code == CLOSEIO the program has finished with the window.
      Close the window and the library.
   */
      CloseWindow(Window);
      Window = NULL;
      CloseLibrary((struct Library *)IntuitionBase);
      break;
   case FAIL:
   /* If Record->code == FAIL write an error message to the window and wait
      for the user to close the window.
   */
      PrintIText(Window->RPort, &Error, 0, 0);
      WaitClose(Window);
      break;
   case DONE:
   /* If Record->code == DONE write a completion message to the window and
      wait for the user to close the window.
   */
      PrintIText(Window->RPort, &Done, 0, 0);
      WaitClose(Window);
      break;
   default:
   /* If the user closed the window set Record->code to FAIL.
   */
      if (CheckClose(Window) == 1)
         Record->code = FAIL;
   }
   /* Return to the calling program.
   */
   return;
}


static void                                                                   /* 18 May 1992 */
WaitClose(struct Window *Window)
{
   int done = 0;

   while (done == 0)
   {
      Wait(1L << Window->UserPort->mp_SigBit);
      done = CheckClose(Window);
   }
}
