#include <exec/types.h>
#include <intuition/intuitionbase.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <arpbase.h>
#include <proto/arp.h>
#include <stdlib.h>
#include <string.h>

#include "wingad.h"

struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase = NULL;
extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;
struct Window *window = NULL;
struct ArpBase *ArpBase = NULL;

short mouse_x( void );
short mouse_y( void );

short mouse_x()
{
   return( IntuitionBase->MouseX );
}

short mouse_y()
{
   return( IntuitionBase->MouseY );
}

void fr_Function(LONGBITS, CPTR);

void fr_Function(mask, object)
LONGBITS	mask;
CPTR object;
{
}

char fr_hailtext[] = "JPEG Viewer 1.0";
char fr_filename[FCHARS+1] = "";
char fr_dirname[LONG_DSIZE+1] = "";
char filename[FCHARS+LONG_DSIZE+2] = "";

struct FileRequester	fr = {
	fr_hailtext,
   fr_filename,
   fr_dirname,
   NULL, /* window */
   0, /* flags */
   FR2F_LongPath, /* new flags */
	fr_Function,
   0,
   0
};

int CloseAll( void );

int CloseAll()
{
   if( window ) CloseWindow( window );
   if( GfxBase ) CloseLibrary( (struct Library *)GfxBase );
   if( IntuitionBase ) CloseLibrary( (struct Library *)IntuitionBase );
   if( ArpBase ) CloseLibrary( (struct Library *)ArpBase );
   window = NULL;
   GfxBase = NULL;
   IntuitionBase = NULL;
   ArpBase = NULL;
   return(0);
}

void OpenLibs( void );

void OpenLibs()
{
   if( (ArpBase = (struct ArpBase *)OpenLibrary( "arp.library", 39L )) == NULL )
      exit(0);
   if( (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L)) == NULL ) {
      CloseLibrary( (struct Library *)ArpBase );
      exit(0);
   }
   if( (GfxBase = (struct GxfBase *)OpenLibrary("graphics.library", 0L)) == NULL ) {
      CloseLibrary( (struct Library *)IntuitionBase );
      CloseLibrary( (struct Library *)ArpBase );
      exit(0);
   }
}

int glob_pixel_smoothing = FALSE;
int glob_block_smoothing = FALSE;
int glob_hires = FALSE;
int glob_grayscale = FALSE;
USHORT glob_treshold = 8;

extern int glob_pixel_smoothing;
extern int glob_block_smoothing;
extern int glob_hires;
extern int glob_grayscale;
extern USHORT glob_treshold;

extern int read_JPEG_file (char *);

extern int main( void );

main()
{
   struct IntuiMessage *msgptr, msg;
   struct Gadget *gad;
   
   if( onexit( CloseAll ) == 0 ) exit(0);
   OpenLibs();
   window = OpenWindow(&NewWindowStructure1);
   while( 1 ) {
      WaitPort(window->UserPort);
      while( (msgptr = (struct IntuiMessage *)GetMsg(window->UserPort)) != NULL ) {
         msg = *msgptr;
         ReplyMsg((struct Message *)msgptr);
         switch(msg.Class) {
            case CLOSEWINDOW: /* close gadget */ 
               exit(0);
               break;
            case GADGETUP:
               gad = (struct Gadget *)msg.IAddress;
               switch( gad->GadgetID ) {
                  case 1:  /* view gadget */
                     if( FileRequest( &fr ) == NULL )
                        break;
                     strcpy( filename, fr.fr_Dir );
                     TackOn( filename, fr.fr_File );
                     read_JPEG_file( filename );
                     break;
                  case 2: /* glob_hires */
                     if( gad->Flags & SELECTED )
                        glob_hires = TRUE;
                     else
                        glob_hires = FALSE;
                     break;
                  case 3: /* pixel smoothing */
                     if( gad->Flags & SELECTED )
                        glob_pixel_smoothing = TRUE;
                     else
                        glob_pixel_smoothing = FALSE;
                     break;
                  case 4: /* block smoothing */
                     if( gad->Flags & SELECTED )
                        glob_block_smoothing = TRUE;
                     else
                        glob_block_smoothing = FALSE;
                     break;
                  case 5: /* grayscale */
                     if( gad->Flags & SELECTED )
                        glob_grayscale = TRUE;
                     else
                        glob_grayscale = FALSE;
                     break;
                  case 6: /* gray/color treshold */
                     glob_treshold = ((struct PropInfo *)gad->SpecialInfo)->HorizPot /
                                     ((struct PropInfo *)gad->SpecialInfo)->HPotRes;
                     break;
            }
         }
      }
   }
}
