/*Amiga Computing*/
/*Code Clinic*/
/*Feb1991- intuition*/
/*A500 upwards*/

/*c source code   -  main program*/

#include "exec/types.h"                    /*to make sense of the structures*/
#include "intuition/intuitionbase.h"       /*with their offsets and flags   */
#include "intuition/intuition.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"

APTR *savewindow;                          /*defined in the main program*/
struct IntuitionBase *IntuitionBase = NULL;
struct Screen *CustomScreen = NULL;
struct Window *MainWindow = NULL;
struct IntuiMessage *message;
struct Process *mproc;

int class,code,quit_flag,timecount;         
char buffer[32];                          /*used for reading in a file*/
char *filename = "windowprogram.lnk";
long *filehandle;

extern struct Menu Menu1;                  /*defined in gadgetsandmenus.c*/
extern struct Gadget Gadget1;
extern struct NewWindow  NewWindowStructure;
extern struct NewScreen  NewScreenStructure;

/*these functions do not return a value*/

void main(),Handle_IDCMP(),cleanup(),Quit(),HandleEvent(),HandleMenuEvent();
void readdisc();

void main()
{
   /*opens screen and window*/

   IntuitionBase = (struct IntuitionBase *)
   OpenLibrary("intuition.library",LIBRARY_VERSION);/*open the library*/
   if (IntuitionBase == NULL) cleanup("no library");
   CustomScreen= (struct Screen *) OpenScreen(&NewScreenStructure);
   if (CustomScreen == 0) cleanup("no screen");      /*open the screen*/
   NewWindowStructure.Screen = CustomScreen;        /*attach window to screen*/
   if ((MainWindow = (struct Window *)OpenWindow(&NewWindowStructure))  == NULL) 
   cleanup("cannot open main window");

  /*diverts the system error message to appear in front of MainWindow*/

   mproc=(struct Process *)FindTask(NULL);
   savewindow=(APTR *)mproc->pr_WindowPtr;
   mproc->pr_WindowPtr=(APTR)MainWindow;

  /*attaches the menustrip to the window*/

   SetMenuStrip(MainWindow,&Menu1);

  /*handles the intuition flags*/

   Handle_IDCMP(); 

  /*cleans up and exits*/

  (APTR *)mproc->pr_WindowPtr = savewindow;
  ClearMenuStrip(MainWindow); 
  cleanup(NULL); 
}

/*leave program*/
void Quit()
   {
    quit_flag=TRUE;
   }


/*Handle keys for window,looks for code for F1,F2,F3 and ESCAPE*/
void HandleKey(code)
long code;
{
code=code&255;
if (code==0x50) {printf("you have pressed key F1\n");code=0;return;}
if (code==0x51) {printf("you have pressed key F2\n");code=0;return;}
if (code==0x45) {Quit();code=0;return;}
if (code==0x52) {readdisc();code=0;return;}
}

/*handle messages for window*/
void Handle_IDCMP()
   {
   APTR object;                              /* NEEDED FOR  EVENT HANDLER */

   for (quit_flag = FALSE;!quit_flag;)
      {
      if (1<<MainWindow->UserPort->mp_SigBit)
         {
      while (!(message=(struct IntuiMessage *)GetMsg(MainWindow->UserPort)))
          {
         /*if there is no message - any background tasks can go in here */ 
         if (timecount > 900)  Quit();
          }
         }
      class = message->Class;    code = message->Code;    /* Get event info */
      object=message->IAddress;
      ReplyMsg((struct Message *) message );
        /* (Clears message area,resets flags)*/

      switch ( class )
         {
         case MENUPICK:          /*menu item selected*/
            HandleMenuEvent(object); 
            break;

         case GADGETUP:          /*gadget selected*/
         HandleEvent(object);            
            break;

         case INTUITICKS:        /*about 3 times a second*/ 
            timecount +=1; 
            break;

         case RAWKEY:           /*key pressed*/   
           HandleKey(code);
           break;
        }                                                    /* end switch */
      }                                                      /* end forloop */
   }


void cleanup(errormsg)                                   
   {                                                   
     printf(errormsg);
     if (MainWindow != 0 ) CloseWindow(MainWindow);
     if (CustomScreen != 0 ) CloseScreen(CustomScreen);/*close screen*/
     if (IntuitionBase !=0) CloseLibrary(IntuitionBase);/*close library*/
     exit(0);
    }

/*read one of the files on the disc*/ 

void readdisc()
    {
      filehandle = (long *)Open(filename,1005);
      if ((long *)filehandle == NULL) return;
      Read(filehandle,&buffer[0],4*8);
      Close(filehandle);
    }
