/* !DLibShell.c.AppHooks */

/* Hooks into user application code */

#include "DeskLib:GFX.h"
#include "DeskLib:WimpSWIs.h"
#include "DeskLib:File.h"
#include "DeskLib:Menu.h"

#include "InterShell.h"

/* Application specific data */

#define MAXBLOBS 50

typedef struct {
  wimp_point Position;
  int Radius;
  int Colour;
} Blob;

Blob DataFile[MAXBLOBS];  /* 50 Blobs in the data file */
int BlobCount;
                                             
/* Application specific functions */

void Example_NewBlob(int,int,int,int);
void Example_ClearBlobs(void);
                               
/*--------------------------------------------------------------------*/
                                                 
/* Called when program starts up */

void Application_Initialise(void)
{
  BlobCount=0;
}          

/*--------------------------------------------------------------------*/
                                      
/* Called when SELECT is pressed in main window */
/* X and Y are work area coordinates */

void Application_ClickSelect(int x,int y)
{
  Example_NewBlob(x,y,35,11);
  Shell_SetModified();
  Shell_ForceRedraw();
}                                                            

/*--------------------------------------------------------------------*/

/* Called when ADJUST is pressed */

void Application_ClickAdjust(int x,int y)
{
  Example_NewBlob(x,y,35,10);
  Shell_SetModified();
  Shell_ForceRedraw();
}
                                          
/*--------------------------------------------------------------------*/

/* Called when SELECT is pressed for the second time */

void Application_DoubleSelect(void)
{
  /* Do something ! */
}

/*--------------------------------------------------------------------*/

/* Called when ADJUST is pressed for the second time */

void Application_DoubleAdjust(void)
{
  /* Do something ! */
}

/*--------------------------------------------------------------------*/

/* Called to draw the contents of the main window */
/* X and Y are the coordinates of the top left of the work area */
/* All drawing should be done relative to X and Y */

void Application_DrawWindowContents(int x,int y)
{                               
  int i;
  
  for(i=0;i<BlobCount;i++) {
    Wimp_SetColour(DataFile[i].Colour);
    GFX_CircleFill(x+DataFile[i].Position.x,y+DataFile[i].Position.y,DataFile[i].Radius);
  }
}

/*--------------------------------------------------------------------*/
                                                        
/* Called when a selection is made from the main menu */ 

void Application_MenuSelect(int sel[])
{
  switch(sel[0]) {
    case 0:
      Shell_StartSave();
      break;
    case 1:
      Example_ClearBlobs();
      Shell_SetModified();
      Shell_ForceRedraw();
      break;
  }
}                                                                                          

/*--------------------------------------------------------------------*/

/* Save a file */
/* Return TRUE if successful */
/* No need to set file type. It is done automatically */

BOOL Application_SaveFile(char *name)
{
  file_handle fh=File_Open(name,file_WRITE);
  int i;
  
  File_Write32(fh,BlobCount);
  for(i=0;i<BlobCount;i++) {
    File_WriteBytes(fh,&(DataFile[i]),sizeof(Blob));
  }
  
  File_Close(fh);
  
  return TRUE;
}                                                

/*--------------------------------------------------------------------*/
                                                                                  
/* Create a new (BLANK) file */

void Application_NewFile(void)
{
  BlobCount=0;
}

/*--------------------------------------------------------------------*/
                                                
/* Called AFTER Application_NewFile if a file is to be loaded */

void Application_LoadFile(char *name)
{
  file_handle fh=File_Open(name,file_READ);
  int i;
  
  BlobCount=File_Read32(fh);
  for(i=0;i<BlobCount;i++) {
    File_ReadBytes(fh,&(DataFile[i]),sizeof(Blob));
  }
  
  File_Close(fh);
}

/*--------------------------------------------------------------------*/

/* Power menu system. Enabled by not returning NULL from MakeMenu */

menu_ptr Application_MakeMenu(void)
{
  return(NULL); /* Return menu pointer for power menus */
}

void Application_SetMenuFlags(void)
{
  /* Set up menu flags. */
}

/**********************************************************************/

/* The next two functions are application specific. */

void Example_NewBlob(int x,int y,int rad,int col)
{
  if(BlobCount<MAXBLOBS) {
    DataFile[BlobCount].Position.x=x;
    DataFile[BlobCount].Position.y=y;
    DataFile[BlobCount].Radius=rad;
    DataFile[BlobCount].Colour=col;
    BlobCount++;
  }
}

void Example_ClearBlobs(void)
{
  BlobCount=0;
}

