#include "DeskLib:Event.h"
#include "DeskLib:Resource.h"
#include "DeskLib:Core.h"
#include "DeskLib:Icon.h"


/* Function Prototypes */

BOOL click(event_pollblock *event,void *ref);

/* Main Program */

int main(void)
{
  icon_handle baricon;
  int quit = FALSE;

/* Initialise WIMP ***********************************************************/  
  Event_Initialise("Test");
  
/* Initialise Resources path *************************************************/
  Resource_Initialise("!Icon");
  
/* This puts the icon called '!icon' on the right hand side of the icon bar.
 * It also stores the handle of that particular icon for future use. *********/  
  baricon = Icon_BarIcon("!icon", iconbar_RIGHT);

/* This checks for mouse clicks on our icon.  The -2 is the window handler
 * of the icon bar.  Each time the icon is clicked, the function click()
 * is called with a pointer (cast to void) to 'quit' as reference ************/
  Event_Claim(event_CLICK, -2, baricon, click, (void *)&quit);

/* As long as 'quit' is FALSE, keep polling **********************************/
  while(!quit) Event_Poll();

  return(0);
}

BOOL click(event_pollblock *event, void *ref)
{
  BOOL *quit;

/* Cast the reference back from VOID to BOOL so it can be manipulated. *******/
  quit = (BOOL *)ref;
  
/* Check the event structure if the click was with 'select' ******************/
  if(event->data.mouse.button.data.select)
  {
/* If so, close the task down and set the content of 'quit' to FALSE.  
 * Since 'quit' is a pointer to our original 'quit' in the main program, 
 * this will directly affect it! *********************************************/
    Event_CloseDown();
    *quit = TRUE;
  }
/* Return TRUE to main to indicate the event has been processed successfully.*/
  return(TRUE);  
}
