/* DeskLib Includes */

#include "DeskLib:Event.h"
#include "DeskLib:EventMsg.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:Dialog2.h"
#include "DeskLib:Template.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_INFO 0
#define mainmenu_QUIT 1


/* Function Prototypes */

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


/* Main Program */

int main(void)
{
  struct reference ref;

  ref.quit = FALSE;

  task_setup();
  iconbar_setup(&ref);
  menu_setup(&ref);

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

  return(0);
}

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

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

void iconbar_setup(struct reference *ref)
{
  icon_handle baricon;

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

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

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

/* Look up menu descriptions, create menu and store menu handler. ************/
  if(!Msgs_Lookup("menu.main", menu, 100))
  {
    Error_Report(1, "Can't find menu description.");
    ref->quit = TRUE;
  }
  Msgs_Lookup("menu.main", menu, 100);
  Msgs_Lookup("title.main:Main", menutitle, 20);
  ref->menuhandle = Menu_New(menutitle, menu);

/* Create Dialog box and set a warning to make it pop up when selected *******/
  info = Dialog2_CreateDialogBlock( "progInfo", -1, -1, NULL, NULL, NULL);
  Menu_Warn(ref->menuhandle, mainmenu_INFO, TRUE, proginfo, (void *)info);

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

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] == mainmenu_QUIT)
  {
/* If 'Quit' was selected, close the task down and end polling loop. *********/
    Event_CloseDown();
    ptr->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)
{
  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);
  }
  return(TRUE);  
}

/* EOF. */
