/* DeskLib Includes */

#include "DeskLib:Event.h"
#include "DeskLib:EventMsg.h"
#include "DeskLib:Resource.h"
#include "DeskLib:Core.h"
#include "DeskLib:Icon.h"
#include "DeskLib:Menu2.h"
#include "DeskLib:Screen.h"
#include "DeskLib:Msgs.h"
#include "DeskLib:Dialog2.h"
#include "DeskLib:Template.h"
#include "DeskLib:Error.h"
#include "DeskLib:WimpSWIs.h"


/* Global Variables */
  menu_ptr mainmenu;
  menu_ptr submenu1;
  menu_ptr submenu2;
  menu_ptr subsubmenu;
  window_handle display;
  BOOL quit;


/* Defines */

#define mainmenu_INFO 0
#define mainmenu_SUB1 1
#define mainmenu_SUB2 2
#define mainmenu_QUIT 3


/* Function Prototypes */

BOOL click(event_pollblock *event,void *ref);
BOOL proginfo(event_pollblock *event, void *ref);
BOOL menu_choice(event_pollblock *event, void *ref);
void task_setup(void);
void iconbar_setup(void);
void menu_setup(void);


/* Main Program */

int main(void)
{
  quit = FALSE;

  task_setup();
  iconbar_setup();
  menu_setup();

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

  Template_ClearAll();
  return(0);
}

void task_setup(void)
{
  char appname[20];
  
/* Initialise resource paths *************************************************/
  Resource_Initialise("Menu2");

/* Initialise and load Templates *********************************************/
  Template_Initialise();
  Template_LoadFile("Templates");

/* 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);

/* Initialise User messaging system needed for Warnings **********************/
  EventMsg_Initialise();

/* This is necessary to display the menu correctly. **************************/
  Screen_CacheModeInfo();
  display = Window_CreateAndShow("display", 20, open_WHEREVER);
}

void iconbar_setup(void)
{
  icon_handle baricon;

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

/* Check for mouse clicks on icon. *******************************************/
  Event_Claim(event_CLICK, -2, baricon, click, NULL);
}

void menu_setup(void)
{
  char menu[100], menutitle[20];
  dialog2_block *info;

/* Create Dialog box *********************************************************/
  info = Dialog2_CreateDialogBlock( "prog_info", -1, -1, NULL, NULL, NULL);
  Menu_Warn(mainmenu, mainmenu_INFO, TRUE, proginfo, (void *)info);

/* Look up menu descriptions, create menus and store menu handlers. **********/
  Msgs_Lookup("menu.main", menu, 100);
  Msgs_Lookup("title.main: Main", menutitle, 20);
  mainmenu = Menu_New(menutitle, menu);

  Msgs_Lookup("menu.sub1", menu, 100);
  Msgs_Lookup("title.sub1: Submenu1", menutitle, 20);
  submenu1 = Menu_New(menutitle, menu);

  Msgs_Lookup("menu.sub2", menu, 100);
  Msgs_Lookup("title.sub2: Submenu2", menutitle, 20);
  submenu2 = Menu_New(menutitle, menu);

  Msgs_Lookup("menu.subsub", menu, 100);
  Msgs_Lookup("title.subsub: SubSubmenu", menutitle, 20);
  subsubmenu = Menu_New(menutitle, menu);

  Menu_AddSubMenu(mainmenu, 1, submenu1);
  Menu_AddSubMenu(mainmenu, 2, submenu2);
  Menu_AddSubMenu(submenu1, 1, subsubmenu);
  Event_Claim(event_MENU, event_ANY, event_ANY, menu_choice, NULL); 
}

BOOL menu_choice(event_pollblock *event, void *ref)
{
  char result[50];
  UNUSED(ref);

  Wimp_DecodeMenu(mainmenu, event->data.selection, result);
  Icon_SetTextRJ(display, 0, result);
  Icon_SetInteger(display, 5, event->data.selection[0]);
  Icon_SetInteger(display, 6, event->data.selection[1]);
  Icon_SetInteger(display, 7, event->data.selection[2]);
  Icon_SetInteger(display, 8, event->data.selection[3]);

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

  return(TRUE);
}

BOOL click(event_pollblock *event, void *ref)
{
  UNUSED(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(mainmenu, event->data.mouse.pos.x, -1);
  }
  return(TRUE);  
}

/* EOF. */
