/* 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"
#include "DeskLib:Msgs.h"
#include "DeskLib:Error.h"


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


/* Defines */

#define mainmenu_QUIT 0


/* 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[100], appname[20], menutitle[20];
  struct reference ref;

  ref.quit = FALSE;

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

/* Load messages file and look up application name for initialisation.
   The default application name is set to 'Tester'. **************************/
  Msgs_LoadFile("messages");
  Msgs_Lookup("app.name:Tester", appname, 20);

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

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

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

/* Check if menu description is there.  If not, report an error. *************/
  if(!Msgs_Lookup("menu.main", menu, 100))
  {
    Error_Report(1, "Can't find menu description.");
    ref.quit = TRUE;
  }
/* Look up menu title, create menu and store menu handler. *******************/
  Msgs_Lookup("title.main:Main", menutitle, 20);
  ref.menuhandle = Menu_New(menutitle, 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 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);  
}

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);  
}

/* EOF. */
