/* DeskLib Includes */

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


/* Global Variables */
/* Reference stucture with all necessary parameters to be passed to functions.*/
struct reference
  {
    BOOL quit;
    menu_ptr menuhandle;
  };


/* Defines */

/* Function Prototypes */

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


/* Main Program */

int main(void)
{
  icon_handle baricon;
  char menu[] = "Quit";
  struct reference ref;

  ref.quit = FALSE;

/* Initialise Task and Event processing system *******************************/   Event_Initialise("Test");

/* Initialise resource paths *************************************************/
  Resource_Initialise("!Menu");

/* This is necessary to display the menu correctly. **************************/
  Screen_CacheModeInfo();

/* Put icon on iconbar and store the handler. ********************************/
  baricon = Icon_BarIcon("!menu", iconbar_RIGHT);

/* Create menu and store menu handler. ***************************************/
  ref.menuhandle = Menu_New("Main", menu);
  
/* Check for mouse clicks on icon. *******************************************/
  Event_Claim(event_CLICK, -2, baricon, click, (void *)&ref);

/* Polling loop. *************************************************************/
  while(!ref.quit) Event_Poll();

  return(0);
}

BOOL click(event_pollblock *event, void *ref)
{
  struct reference *ptr;

/* Cast reference structure back from VOID. **********************************/
  ptr = (struct reference *)ref;

/* Check if click was the menu button. ***************************************/
  if(event->data.mouse.button.data.menu)
  {
/* Show the menu at the position of the mouse pointer but on top the icon bar.*/
    Menu_Show(ptr->menuhandle, event->data.mouse.pos.x, -1);

/* Check for menu selections. ************************************************/
    Event_Claim(event_MENU, event_ANY, event_ANY, menu_choice, ref); 
  }
  return(TRUE);  
}

BOOL menu_choice(event_pollblock *event, void *ref)
{
  struct reference *ptr;

/* Cast reference structure back from VOID. **********************************/
  ptr = (struct reference *)ref;

/* Check which menu point was selected ***************************************/
  if(event->data.selection[0] == 0)
  {
/* If 'Quit' was selected, close the task down and end polling loop. *********/
    Event_CloseDown();
    ptr->quit = TRUE;
  }
  return(TRUE);  
}

/* EOF. */
