/*************************************************************************
*                                                                        *
*          MacLibrary Main Message Control and Graphics Example          *
*                                                                        *
*************************************************************************/
#include "intuall.h"
#include "maclib.h"

struct TextAttr NormFont = {"topaz.font",8,0,0};
struct TextAttr SapphFont = {"sapphire.font",19,0,0};

struct Screen *OpenRegScreen();
struct Window *OpenRegWindow();
struct Screen *screen1,*screen2;
struct Window *window1,*window2,*window3;
struct RastPort *rp1,*rp2,*rp3;
struct Rectangle rect1,rectA,rectB;
SHORT gadgetID;
BOOL endflag;
struct IntuiMessage *message;
USHORT class,code,qualifier;
APTR address;

void SetCursor();

/*  Define Text Structures To Be Used As TextBool Gadgets */

struct IntuiText RectText,OvalText,RndRectText,ArcText,LineText,ClearText;
struct IntuiText ThinText, ThickText;

/*  Define Gadget Structures To Be Used As TextBool Gadgets */

struct Gadget Rect,Oval,RndRect,Arc,LineGad,Clear,Thin,Thick;

/*  Define PenState Structure To Be Used By GetPenState and SetPenState */
/*           PenState Structure is defined in maclib.h                  */

struct PenState TempPen;

/*  Define TextBool Gadget Identifiers */

#define RectID    1
#define OvalID    2
#define RndRectID 3
#define ArcID     4
#define ClearID   5
#define ThinID    6
#define ThickID   7
#define LineID    8

#define LowRes    0          /* Low Resolution Screen   */
#define Bitplanes 5          /* 5 Bitplanes - 32 Colors */

main()
{
   Initialize();   /* MacLibrary routine which opens up graphic libaries
                      and sets up memory areas for fill operations */

   screen1 = OpenRegScreen("MacLibrary Example Screen",LowRes,Bitplanes);
   window1 = OpenRegWindow(screen1,"Gadget Window",12,12,170,95);
   rp1 = window1->RPort;
   SetCursor(window1,0);
   window2 = OpenRegWindow(screen1,"Draw Window",12,95,170,100);
   rp2 = window2->RPort;
   SetCursor(window2,1);
   window3 = OpenRegWindow(screen1,"Scale Window",185,20,120,100);
   rp3 = window3->RPort;
   SetCursor(window3,2);

   /* Set Up All Of The TextBool Gadgets */

   RectText.FrontPen = 1;    /* Define Foreground Pen */
   RectText.BackPen  = 5;    /* Define Background Pen */
   TextBoolGadget(window1,&Rect,&RectText,&NormFont,"Rectangle",5,12,RectID);

   OvalText.FrontPen = 22;   /* Define Foreground Pen */
   OvalText.BackPen  = 10;   /* Define Background Pen */
   TextBoolGadget(window1,&Oval,&OvalText,&NormFont,"Oval",5,26,OvalID);

   RndRectText.FrontPen = 3;     /* Define Foreground Pen */
   RndRectText.BackPen  = 25;    /* Define Background Pen */
   TextBoolGadget(window1,&RndRect,&RndRectText,&NormFont,
      "RoundRect",5,40,RndRectID);

   ArcText.FrontPen = 18;    /* Define Foreground Pen */
   ArcText.BackPen  = 31;    /* Define Background Pen */
   TextBoolGadget(window1,&Arc,&ArcText,&NormFont,"Arc",5,54,ArcID);

   LineText.FrontPen = 0;    /* Define Foreground Pen */
   LineText.BackPen  = 9;    /* Define Background Pen */
   TextBoolGadget(window1,&LineGad,&LineText,&NormFont,"Line",5,68,LineID);

   ThinText.FrontPen = 12;   /* Define Foreground Pen */
   ThinText.BackPen  = 21;   /* Define Background Pen */
   TextBoolGadget(window1,&Thin,&ThinText,&NormFont,"Thin",100,12,ThinID);

   ThickText.FrontPen = 7;    /* Define Foreground Pen */
   ThickText.BackPen  = 29;   /* Define Background Pen */
   TextBoolGadget(window1,&Thick,&ThickText,&NormFont,"Thick",100,26,ThickID);

   TextFont(rp1,"SAPPHIRE19");
   ClearText.FrontPen = 1;    /* Define Foreground Pen */
   ClearText.BackPen  = 31;   /* Define Background Pen */
   TextBoolGadget(window1,&Clear,&ClearText,&SapphFont,"Clear",100,58,ClearID);

   endflag = FALSE;

 do {

    if (window1) {
      class = code = qualifier = -1;    
	   if((message = (struct IntuiMessage *)GetMsg(window1->UserPort)) !=0)
	      {
 	      class = message->Class;
	      code = message->Code;
	      qualifier = message->Qualifier;
         address = message->IAddress;
 	      ReplyMsg(message);
   	   Window1Control();
         }
      }

    if (window2) {
       class = code = qualifier = -1;    
	    if((message = (struct IntuiMessage *)GetMsg(window2->UserPort)) !=0)
	      {
 	      class = message->Class;
	      code = message->Code;
	      qualifier = message->Qualifier;
         address = message->IAddress;
 	      ReplyMsg(message);
   	   Window2Control();
         }
      }

   if (window3) {
      class = code = qualifier = -1;    
	   if((message = (struct IntuiMessage *)GetMsg(window3->UserPort)) !=0)
	      {
 	      class = message->Class;
   	   code = message->Code;
	      qualifier = message->Qualifier;
         address = message->IAddress;
    	   ReplyMsg(message);
      	Window3Control();
         }
      }


  }while (!endflag);

  if (window1) {
     RemoveGadget(window1,&Rect);
     RemoveGadget(window1,&Oval);
     RemoveGadget(window1,&RndRect);
     RemoveGadget(window1,&Arc);
     RemoveGadget(window1,&LineGad);
     RemoveGadget(window1,&Clear);
     RemoveGadget(window1,&Thin);
     RemoveGadget(window1,&Thick);
     CloseWindow(window1);
     }
  if (window2)  CloseWindow(window2);
  if (window3) CloseWindow(window3);

  CloseScreen(screen1);

  Finalize();   /* close libraries and deallocate area for fill operations */

} /* end of Main */


/***************************************************************************

                    MESSAGE EVENT CONTROLLER EXAMPLE

****************************************************************************/

Window1Control()
{
     SHORT gadgetID;

     switch(class)
	  {
	  case CLOSEWINDOW:
       endflag = TRUE;
	    break;

  	  case MENUPICK:
        break;

     case RAWKEY:
        break;

     case GADGETDOWN:
        break;

     case GADGETUP:

        gadgetID = (((struct Gadget *)address)->GadgetID);
        switch(gadgetID)
           {
           case RectID:
              SetAPen(rp2,1);
              SetRect(&rect1,20,20,40,40);    /* Define Rectangle 1 */
              FrameRect(rp2,&rect1);          /* Draw Rectangle 1   */ 
              break;

           case OvalID:
              SetAPen(rp2,4);
              SetRect(&rect1,35,30,60,65);    /* Define Rectangle 1   */
              FrameOval(rp2,&rect1);          /* Draw Oval Bounded By
                                                 Rectangle 1          */

              break;

           case RndRectID:
              SetAPen(rp2,12);
              SetRect(&rect1,75,10,120,85);     /* Define Rectangle 1      */
              FrameRoundRect(rp2,&rect1,10,10); /* Draw Rounded Rectangle
                                                   Bounded By Rectangle 1
                                                   With 10 Pixel Ovals In
                                                   The Corners             */
              break;

           case ArcID:
              SetAPen(rp2,15);
              SetRect(&rect1,110,10,150,60);  /* Define Rectangle 1        */
              FrameArc(rp2,&rect1,90,120);    /* Draw Part Of Oval Bounded
                                                 By Rectangle 1 Starting At
                                                 90 Degress And Ending At
                                                 210 Degress               */     
              break;

           case LineID:
              SetAPen(rp2,31);
              GetPenState(rp2,&TempPen);  /* Save Current Pen State       */
              Move(rp2,10,85);
              Line(rp2,90,-65);           /* Draw Line From (10,85) 
                                             to (100,20)                   */
              SetPenState(rp2,&TempPen);  /* Restore Pen To Previous State */
              break;

           case ClearID:
              SetAPen(rp2,0);
              RectFill(rp2,2,10,window2->Width-3,window2->Height-11);
              break;

           case ThinID:
              PenSize(rp2,1,1);  /* Set Pen Width and Pen Height to 1 */
              break;

           case ThickID:
              PenSize(rp2,5,4);  /* Set Pen Width to 5 and Pen Height to 4 */
              break;

           }

  	  case NEWSIZE:
        break;

	  case MOUSEBUTTONS:
        break;

     default:
        break;
     }
return(TRUE);         
}

Window2Control()
{

     switch(class)
	  {
	  case CLOSEWINDOW:
       CloseWindow(window2);    /* Intuition routine which closes window */
       window2 = NULL;
	    break;

  	  case MENUPICK:
        break;

     case RAWKEY:
        break;

     case GADGETDOWN:
        break;

     case GADGETUP:
           break;

  	  case NEWSIZE:
        break;

	  case MOUSEBUTTONS:
        break;

     default:
        break;
     }
return(TRUE);         
}



Window3Control()
{

     switch(class)
	  {
	  case CLOSEWINDOW:
       CloseWindow(window3);    /* Intuition routine which closes window */
       window3 = NULL;
       break;

  	  case MENUPICK:
        break;

     case RAWKEY:
        break;

     case GADGETDOWN:
        break;

     case GADGETUP:
           break;

  	  case NEWSIZE:
        SetRect(&rectA,2,10,window2->Width-3,window2->Height-11);
        SetRect(&rectB,2,10,window3->Width-3,window3->Height-11);
        SetCursor(window3,4);
        CopyBits(rp2,rp3,&rectA,&rectB,0xc0);
        SetCursor(window3,3);
        break;

	  case MOUSEBUTTONS:
        break;

     default:
        break;
     }
return(TRUE);         
}
