/* C example application:

cc +p ShowPic.c
as -cd FileInterface.asm
as -cd IlbmInterface.asm
ln -o ShowPic ShowPic.o FileInterface.o IlbmInterface.o ColorInterface.o -lc32

 This program presents the dissidents FileIO requester upon being run. This
 is a filename selection requester based upon R.J. Mical's ProSuite code.
 The user can select the name of an ilbm picture file and click on OK. This
 program will then load the picture into a window using the dissidents ilbm
 library. This library is used in loading or saving IFF files and is based
 upon the Electronic Arts code. If the user clicks the mouse button, then
 the picture will disappear and the FileIO requester will be presented
 once again. When the user wishes to exit, he should select the FileIO's
 Cancel gadget. If the user presses any key while a picture is being
 displayed, the dissidents ColorTool window will appear. This is a library
 that allows adjusting the colors of any viewport via a color requester
 pallete. These 3 libraries and approprite doc files are all available on
 Fred Fish discs. The requester and ilbm libraries are public domain. The
 color.library is copyrighted by dissidents, but can be freely distributed.
 */

#include "math.h"
#include "functions.h"    /* Manx C declarations */
#include "intuition/intuition.h"

#include "exec/tasks.h"
#include "exec/types.h"
#include "exec/memory.h"
#include "graphics/gfxbase.h"
#include "graphics/rastport.h"
#include "graphics/gfx.h"
#include "graphics/view.h"
#include "graphics/text.h"
#include "intuition/intuitionbase.h"

 /* These INCLUDE files come with the ilbm and requester libs */
#include "ILBM_Lib.h"
#include "FileIO.h"

/*-------------------------------defines---------------------------------*/

#define INTUITION_REV 33L
#define GRAPHICS_REV  33L

struct IntuitionBase *IntuitionBase=0L;
struct GfxBase       *GfxBase=0L;
struct Window        *back_wind=0L;
struct Screen        *main_scrn=0L;

 /* Data for the Dissidents Requester library */
struct RequesterBase *RequesterBase=0L;
struct FileIO        *myFileIO=0L;

 /* Data for the Dissidents ILBM library */
struct ILBMBase      *ILBMBase=0L;
struct ILBMFrame     myILBMFrame;

 /* Data for the color requester lib */
struct ColorBase     *ColorBase=0L;

USHORT def_color_table[32]={0x000,0x009,0x480,0x660,0x840,\
                     0xa00,0x804,0x706,0x608,0x60a,0x40a,\
                     0x06a,0x088,0x0a6,0x700,0x070,0x0,\
                     0x800,0x555,0x0a0,0x0a4,0x0a6,0x0a8,0x0aa,\
                     0x0ac,0x0ae,0x0b0,0x0b2,0x0b4,0x0b6,0x0b8,0x0ba};

BYTE DoColor();

struct TextAttr my_font_attr={(UBYTE *)"topaz.font",TOPAZ_EIGHTY,\
                         FS_NORMAL, FPF_ROMFONT};


VOID open_all(), damp_mop();

/*------------------------------start of main()----------------------------*/

main( argc, argv )
LONG argc;
UBYTE *argv[];
{
   UBYTE Filename[260];
   UBYTE *string;
   IFFP  Result;
   struct IntuiMessage *message;
   UBYTE flag;
   BYTE  error;

   open_all();

 /* The FileIO is for the file requester lib. See Fish Disc #203 for the
    documentation and examples */
   myFileIO->X = 6;
   myFileIO->Y = 11;
   myFileIO->DrawMode = JAM2;
   myFileIO->PenA = 0;
   myFileIO->PenB = 1;
   myFileIO->Buffer = &Filename[0];

   /* Get the filename to load */

   while( (string = DoFileIOWindow(myFileIO, 0L)) == &Filename[0] )
   {
      /* If user was positive and fileio opened ok, load the Filename */


 /* We can open our own window/screen and load pictures there by setting
    the ILBMFrame's iWindow and iScreen fields to this address. Any pic
    that is larger than our window/screen dimensions will be scaled to
    fit. Also, any picture intended for a different view mode than our
    screen (or with more colors) may come out "weird". The main advantage
    of loading into an open window is that we are certain what dimensions
    the screen/window will be. If you had a program which loaded a company
    logo in an IFF ILBM FORM when it started, you would certainly make sure
    that the ILBM file was the proper dimensions for your opened screen.
    In a dissidents product, SpeakerSim, there is a feature for loading in
    an ILBM beneath the speaker plot graph. Since the mouse coordinates
    figure prominantly in obtaining info from the user, it would have been
    too much overhead to tailor all functions to work in both HIRES, LORES,
    and INTERLACE simultaneously. So, there are several "versions" of
    SpeakerSim, each one's graphing routines optimized for the dimensions
    of the main screen. Since the ILBM is meant to be a low-key, background
    beneath the speaker plot, the picture is scaled to fit. Obviously, its
    best to load an ILBM that fits that version of SpeakerSim, but the em-
    phasis is on the graphing, not the background. This is an example of why
    you might load into an existing window.
    Otherwise, we could have the lib open an appropriately sized
    backdrop window/screen by setting iWindow and iScreen to 0. In this
    case, the lib will place the addresses in the ILBMFrame fields. We
    could then do a ModifyIDCMP after the load. Eventually, we'll have
    to CloseWindow and CloseScreen. With this method, the image is never
    scaled (currently the lib doesn't allow for overscan images) as long
    the pic's BMHD w=pageWidth and h=pageHeight, and the image is a standard
    amiga screen dimension. Unfortunately, you won't know whether you have
    a HAM, LORES, HIRES, etc until after the load.

    We are going to let the ilbm lib open the window for us. See Fred Fish
    disk for the ilbm.library and the docs */

      myILBMFrame.iScreen = 0;
      myILBMFrame.iWindow = 0;
      myILBMFrame.iUserFlags = 3;     /* no pointer, and hidden title bar */

      Result = LoadIFFToWindow( &Filename[0], &myILBMFrame );

      main_scrn = myILBMFrame.iScreen;
      back_wind = myILBMFrame.iWindow;

      if( !Result )
      {
         /* Successful picture load. Now wait for user to click buttons or
         rawkey */
         ModifyIDCMP(back_wind, RAWKEY|MOUSEBUTTONS);

         flag=1;  /* to indicate when to close up the window and get next pic
                */

         while( back_wind )
         {

            while( message=(struct IntuiMessage *)GetMsg(back_wind->UserPort) )
            {
               ULONG  class = message->Class;
               USHORT code = message->Code;
               APTR   address = message->IAddress;
               USHORT qualifier = message->Qualifier;

               ReplyMsg( message );
               switch( class )
               {
                case MOUSEBUTTONS:
                  if( code == SELECTDOWN )
                     flag = 0;
                  break;

                case RAWKEY:
                     /* If we pass a NULL instead of our def color table, the
                        default gadget will do nothing */
                     error = DoColor( &def_color_table[0], main_scrn );
                     if (error<0)  printf("Error in opening ColorTool\n");
                  break;
               }
               break;
            }

            if( !flag )
            {
              /* drain the IDCMP and close back_wind and screen */
                while( message=(struct IntuiMessage *)GetMsg(back_wind->UserPort))
                   ReplyMsg(message);
                CloseWindow( back_wind );
                back_wind=0;

                CloseScreen( main_scrn );
                main_scrn=0;
            }
            else  /* Wait for msg from user */
            {
               WaitPort( back_wind->UserPort );
            }

         }  /* end of while() */

      }
      else
      {
         /* Get the IFFP error message and display it */
         printf( "%s", GetIFFPMsg( Result ));
         if( back_wind )
         {
             CloseWindow( back_wind );
             back_wind=0;
         }

         if( main_scrn )
         {
             CloseScreen( main_scrn );
             main_scrn=0;
         }
      }
   }

   if (!string)    printf("Error in opening FileIO requester\n");
   if (string==-1) printf("User aborted FileIO\n");
   damp_mop();

}          /* end of main() */


/*------opens Intuition, graphics, requester, color, and ilbm libs--------*/

VOID open_all()
{

 if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library", INTUITION_REV)))
    damp_mop();

 if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library", GRAPHICS_REV)))
    damp_mop();

 if(!(RequesterBase=(struct RequesterBase *)OpenLibrary("requester.library", 0L)))
   {
    printf("Need the dissidents requester.library on boot disk\n");
    damp_mop();
   }

 if(!(ILBMBase=(struct ILBMBase *)OpenLibrary("ilbm.library", 0L)))
   {
    printf("Need the dissidents ilbm.library on boot disk\n");
    damp_mop();
   }

   if (!(ColorBase = (struct ColorBase *)OpenLibrary("color.library", 0L)))
   {
    printf("Need the dissidents color.library on boot disk\n");
    damp_mop();
   }

 /* The FileIO is for the file requester lib. See Fish Disc #203 for the
    documentation and examples */
   if(!(myFileIO = GetFileIO()))
   {
    printf("Can't get a fileio structure\n");
    damp_mop();
   }

}


/*---------------closes windows, screen, libs---------------*/

VOID damp_mop()
{
 struct IntuiMessage *mes;

 if( RequesterBase )
 {
 ReleaseFileIO( myFileIO ); /* ReleaseFileIO checks for myFileIO==NULL */
 }

 if(back_wind)
 {                /* drain the IDCMP */
   while( mes=(struct IntuiMessage *)GetMsg(back_wind->UserPort))
     ReplyMsg(mes);
   CloseWindow( back_wind );
 }

 if( main_scrn )     CloseScreen( main_scrn );
 if( ColorBase )     CloseLibrary( ColorBase );
 if( ILBMBase )      CloseLibrary( ILBMBase );
 if( RequesterBase ) CloseLibrary( RequesterBase );
 if( GfxBase )       CloseLibrary( GfxBase );
 if( IntuitionBase ) CloseLibrary( IntuitionBase );

 exit(FALSE);

}
