/* 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"
#include "DeskLib:Window.h"
#include "DeskLib:Handler.h"
#include "DeskLib:Sprite.h"
#include "DeskLib:Sound.h"

/* ANSI includes */

#include "stdlib.h"

/* Global Variables */

icon_handle baricon;
menu_ptr menuhandle;
window_handle window = NULL, display;
char sblck[24576];
BOOL quit;


/* Defines */

#define mainmenu_INFO 0
#define mainmenu_QUIT 1


/* Function Prototypes */

BOOL click(event_pollblock *event,void *ref);
BOOL menu(event_pollblock *event, void *ref);
BOOL proginfo(event_pollblock *event, void *ref);
BOOL close(event_pollblock *event, void *ref);
BOOL open(event_pollblock *event, void *ref);
BOOL redraw(event_pollblock *event, void *ref);
BOOL other(event_pollblock *event, void *ref);
void task_setup(void);
void iconbar_setup(void);
void menu_setup(void);
void open_window(void);
void windows_setup(void);


/* Main Program */

int main(void)
{
  quit = FALSE;
  
  task_setup();
  iconbar_setup();
  menu_setup();
  windows_setup();
  
/* Polling loop. *************************************************************/
  while(!quit) Event_Poll();

  exit(0);
}

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

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

/* Setup Event checking. *****************************************************/
  Event_Claim(event_CLICK, event_ANY, event_ANY, click, NULL);
  Event_Claim(event_MENU, event_ANY, event_ANY, menu, NULL); 
  Event_Claim(event_OPEN, event_ANY, event_ANY, open, NULL);
  Event_Claim(event_REDRAW, event_ANY, event_ANY, redraw, NULL); 
  Event_Claim(event_CLOSE, event_ANY, event_ANY, close, NULL);
  Event_Claim(event_PTRENTER, event_ANY, event_ANY, other, NULL);
  Event_Claim(event_PTRLEAVE, event_ANY, event_ANY, other, NULL);
}

void iconbar_setup(void)
{
/* Put icon on iconbar and store the handler. ********************************/
  baricon = Icon_BarIcon("!eventsys1", iconbar_RIGHT);
}

void menu_setup(void)
{
  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.");
    quit = TRUE;
  }
/* Look up menu title, create menu and store menu handler. *******************/
  Msgs_Lookup("menu.main", menu, 100);
  Msgs_Lookup("title.main:Main", menutitle, 20);
  menuhandle = Menu_New(menutitle, menu);

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

void windows_setup(void)
{
  Sprite_Load((sprite_area)sblck, "<EventSys1$Dir>.sprite");
  display = Window_CreateAndShow("display", 20, open_WHEREVER);
}

BOOL menu(event_pollblock *event, void *ref)
{
  UNUSED(ref);

  Icon_SetText(display, 0, "Menu");
  Sound_SysBeep();

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

  Icon_SetText(display, 0, "Click");
  Sound_SysBeep();
/* Check if the click was on the baricon. ************************************/
  if(event->data.mouse.icon == baricon)
  {
/* 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(menuhandle, event->data.mouse.pos.x, -1);
    }
/* Check if the click was the select button. **********************************/
    if(event->data.mouse.button.data.select)
    {
/* Open the display window.****************************************************/
      open_window();
    }
  }
  return(TRUE);
}

void open_window(void)
{
  if(window == NULL)
  {
    window = Window_Create("SpriteWin", 20);
  }
/* Open choices window and set necessary flags ********************************/
  Window_Show(window, open_CENTERED);
}

BOOL close(event_pollblock *event, void *ref)
{
  UNUSED(event);
  UNUSED(ref);
  
  Icon_SetText(display, 0, "Close");
  Sound_SysBeep();
/* Delete window and set handle back to NULL. ********************************/
  Window_Delete(window);
  window = NULL;
  
  return(TRUE);
}

BOOL open(event_pollblock *event, void *ref)
{
  UNUSED(ref);
  
  Icon_SetText(display, 0, "Open");
  Sound_SysBeep();
  Wimp_OpenWindow(&(event->data.openblock));
  return(TRUE);
}

BOOL redraw(event_pollblock *event, void *ref)
{
  window_redrawblock redraw;
  BOOL more;
  wimp_point point;
  convert_block convert;
  sprite_areainfo *spritearea;

  UNUSED(ref);
  
  Icon_SetText(display, 0, "Redraw");
  Sound_SysBeep();
  spritearea = (sprite_areainfo *)sblck;
  point.x=100;
  point.y=-500;

  redraw.window = event->data.openblock.window;
  Window_GetCoords(redraw.window, &convert);
  Wimp_RedrawWindow(&redraw, &more);

  while (more)
  {
    Sprite_WimpPlot((sprite_area)spritearea, "sprite", &point, &convert, 0);
    Wimp_GetRectangle(&redraw, &more);
  }

  return(TRUE);
}

BOOL other(event_pollblock *event, void *ref)
{
  UNUSED(ref);
  
  switch(event->type)
  {
    case(event_PTRENTER): Icon_SetText(display, 0, "Enter");
    break;
    case(event_PTRLEAVE): Icon_SetText(display, 0, "Leave");
  }
  return(TRUE);
}

/* EOF. */
