/*
 *  WEXAMPLE1.C     Example program for wIconify.
 *                  This program simply adds an icon to its window, and
 *                  then iconifies it.  It's pretty trivial otherwise.
 */

#define INTUITION_PREFERENCES_H             /* don't need 'em */
#include <intuition/intuition.h>

#include "wIcon.h"

/*
 *  The revision levels needed to the libraries
 */

#define INTUITION_REV   0
#define GRAPHICS_REV    0
extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;


/*
 *  Return codes for error and OK
 */

#define ERROR_EXIT      10
#define OK_EXIT         0


/*
 *  Some routines we need
 */

extern struct Window *OpenWindow();
extern struct IntuiMessage *GetMsg();
extern APTR OpenLibrary();


/*
 *  The definition for our window
 */

static struct NewWindow NewWindow =
{
   160,50, 320,100, 0,1, CLOSEWINDOW| REFRESHWINDOW,
   WINDOWCLOSE| WINDOWDRAG| WINDOWDEPTH| WINDOWSIZING| SIMPLE_REFRESH,
   NULL, NULL, "wExample1", NULL, NULL, 20,10, -1,-1, WBENCHSCREEN
};
static struct Window *myWindow;


/*
 *  This is the image to be used by the icon for our window.
 *  The image data should be in Chip ram, but we'll just compile the
 *  whole program into chip, since this is just an example.
 */

#define ICONWIDTH        41
#define ICONHEIGHT       20
#define ICONDEPTH        3
#define WORDSPERROW      3

static USHORT IconData[ICONDEPTH][ICONHEIGHT*WORDSPERROW] =
{
  {
    0x1FFF, 0xFFFF, 0x8000,
    0x1C70, 0x003F, 0xC000,
    0x0FFF, 0xFFFF, 0xC000,
    0x0700, 0x0001, 0xC000,
    0x0300, 0x0001, 0xC000,
    0x010F, 0x0781, 0xC000,
    0x0093, 0x0983, 0xC100,
    0x0080, 0x0003, 0x8080,
    0x0080, 0x0003, 0x8080,
    0x0083, 0x0003, 0x1080,
    0x1F80, 0xFE04, 0x0080,
    0x0680, 0x0004, 0x0180,
    0x0680, 0x007F, 0xFF00,
    0x7D00, 0x00FE, 0x0000,
    0x03FF, 0xFFFC, 0x0000,
    0x0187, 0xF0F8, 0x0000,
    0x0004, 0x0080, 0x0000,
    0x0010, 0x0100, 0x0000,
    0x0006, 0x0038, 0x0000,
    0x01FE, 0x0FF8, 0x0000,
  },
  {
    0x0000, 0x0001, 0x8000,
    0x0000, 0x000C, 0xC000,
    0x0000, 0x0000, 0xC000,
    0x0000, 0x0000, 0xC000,
    0x0000, 0x0000, 0xC000,
    0x000F, 0x0780, 0xC000,
    0x0010, 0x0801, 0xDF00,
    0x0000, 0x0001, 0xBF80,
    0x7F00, 0x0001, 0x8780,
    0xFF00, 0x0001, 0xF780,
    0xFF00, 0x0003, 0xFF80,
    0xFE00, 0x0003, 0xFF80,
    0xFE00, 0x0007, 0xFF00,
    0x7C00, 0x000E, 0x0000,
    0x0000, 0x001C, 0x0000,
    0x01FF, 0xFFF8, 0x0000,
    0x00FC, 0x0F80, 0x0000,
    0x01F0, 0x1F00, 0x0000,
    0x03FE, 0x3FF8, 0x0000,
    0x01FE, 0x0FF8, 0x0000,
  }
};

static struct Image IconImage =
{
   0,0,
   ICONWIDTH,ICONHEIGHT, ICONDEPTH,
   &IconData[0][0], 0x03,0, NULL
};

/*
 *  This is the Icon Mask data.  It should be in CHIP RAM, too.
 */

static UWORD IconMask[ICONHEIGHT*WORDSPERROW] =
{
  0x1FFF, 0xFFFF, 0x8000,
  0x1FFF, 0xFFFF, 0xC000,
  0x0FFF, 0xFFFF, 0xC000,
  0x07FF, 0xFFFF, 0xC000,
  0x03FF, 0xFFFF, 0xC000,
  0x01FF, 0xFFFF, 0xC000,
  0x00FF, 0xFFFF, 0xDF00,
  0x00FF, 0xFFFF, 0xBF80,
  0x7FFF, 0xFFFF, 0x8780,
  0xFFFF, 0xFFFF, 0xF780,
  0xFFFF, 0xFFFF, 0xFF80,
  0xFEFF, 0xFFFF, 0xFF80,
  0xFEFF, 0xFFFF, 0xFF00,
  0x7DFF, 0xFFFE, 0x0000,
  0x03FF, 0xFFFC, 0x0000,
  0x01FF, 0xFFF8, 0x0000,
  0x00FC, 0x0F80, 0x0000,
  0x01F0, 0x1F00, 0x0000,
  0x03FE, 0x3FF8, 0x0000,
  0x01FE, 0x0FF8, 0x0000,
};


/*
 *  This is the icon definition itself.  It is pretty simple; no special
 *  flags or reports required by this one.
 */

static WICON myIcon =
{
   "Super Icon!",       /* The name for the icon (default is the window name) */
   &IconImage,          /* The icon's Image */
   NULL,                /* No Select image (use inverted image) */
   &IconMask[0],        /* The icon's Mask */
   0,0,                 /* Let wIconify place it where it wants */
   0,0,                 /* No special flags or reports */
   NULL                 /* No IconPort needed since we're not getting reports */
};



/*
 *  DoExit()
 *
 *  Print an error message, if necessary, and then clean up any allocated
 *  memory, close the windows and libraries, etc.  Finally, exit.
 */

static void DoExit(s,x1,x2,x3)
char *s, *x1,*x2,*x3;
{
   int status = OK_EXIT;

   if (s)
   {
      printf(s,x1,x2,x3);
      printf("\n");
      status = ERROR_EXIT;
   }
   if (myWindow)        CloseWindow(myWindow);
   if (IntuitionBase)   CloseLibrary(IntuitionBase);
   if (GfxBase)         CloseLibrary(GfxBase);
   exit(status);
}


/*
 *  CheckLibOpen()
 *
 *  Check to see if the specified library can be openned, and exit with
 *  an error if not.
 */

static void CheckLibOpen(lib,name,rev)
APTR *lib;
char *name;
int rev;
{
   extern APTR OpenLibrary();
   
   if ((*lib = OpenLibrary(name,(ULONG)rev)) == NULL)
      DoExit("Can't open '%s'",name);
}


/*
 *  OpenMyWindow()
 *
 *  Attempt to open the window (error if unsuccessful).
 *  Add the icon to the window once it's open, then iconify the window.
 */

static void OpenMyWindow()
{
   myWindow = OpenWindow(&NewWindow);
   if (myWindow == NULL) DoExit("Can't open my window");
   wSetIcon(myWindow,&myIcon);
   wIconify(myWindow);
}


/*
 *  DisplayMessage()
 *
 *  Set the pen color, and write out the message telling the user
 *  what he should do with this window.  Also show the icon for fun!
 */

static void DisplayMessage()
{
   struct RastPort *rp = myWindow->RPort;

   SetAPen(rp,3);
   Move(rp,110,35);
   Text(rp,"Iconify Me!",11);
   Move(rp,42,51);
   Text(rp,"(Close me when you're done.)",28);
   DrawImage(rp,&IconImage,
      (myWindow->Width-ICONWIDTH)/2-4,myWindow->Height-ICONHEIGHT-10);
}


/*
 *  WaitForAction()
 *
 *  While there is still more to do
 *    Wait for a message to arrive at the window's UserPort
 *    While there are more messages in the port
 *      If the message class is a CLOSEWINDOW message, we're done
 *      If its a REFRESH message, draw the text again.
 *      Reply to the message
 */

static void WaitForAction()
{
   struct IntuiMessage *theMessage;
   short NotDone = TRUE;
   struct MsgPort *thePort = myWindow->UserPort;
   long SignalMask = (1 << thePort->mp_SigBit);

   while (NotDone)
   {
      Wait(SignalMask);
      while (theMessage = GetMsg(thePort))
      {
         switch(theMessage->Class)
         {
            case CLOSEWINDOW:
               NotDone = FALSE;
               break;

            case REFRESHWINDOW:
               BeginRefresh(myWindow);
               DisplayMessage();
               EndRefresh(myWindow);
               break;
         }
         ReplyMsg(theMessage);
      }
   }
}


/*
 *  main()
 *
 *  If wIconify is running
 *    Open the libraries needed by the program
 *    Open the window and add its icon
 *    Display the message in the window
 *    Wait for something to happen
 *  Otherwise
 *    Print an error message
 *  Clean up afterwords
 */

void main()
{
   if (wIconifyActive())
   {
      CheckLibOpen(&IntuitionBase,"intuition.library",INTUITION_REV);
      CheckLibOpen(&GfxBase,"graphics.library",GRAPHICS_REV);
      OpenMyWindow();
      DisplayMessage();
      printf("wExample1 window is open and iconified\n");
      WaitForAction();
   } else printf("wIconify not running or version mismatch\n");
   DoExit(NULL);
}
