/* c.imagwind1
 */
#include <string.h>
#include "wimp.h"        /*  access to WIMP SWIs                      */
#include "wimpt.h"       /*  wimp task facilities                     */
#include "win.h"         /*  registering window handlers              */
#include "event.h"       /*  poll loops, etc                          */
#include "baricon.h"     /*  putting icon on icon bar                 */
/* #include "command.h"       alleen voor max_signals */
#include "sprite.h"      /*  sprite operations                        */
#include "werr.h"        /*  error reporting                          */
#include "res.h"         /*  access to resources                      */
#include "resspr.h"      /*  sprite resources                         */
#include "flex.h"        /*  dynamic mem alloc from WIMP              */
#include "heap.h"        /*  uses flex to keep heap in a flex block   */
#include "template.h"    /*  reading in template file                 */
#include "bbc.h"         /*  olde-style graphics routines             */
#include "colourtran.h"  /*  interface to colour translation module   */
#include "os.h"          /*  low-level RISCOS access                  */
#include "dbox.h"        /*  dialogue box handling                    */
#include "saveas.h"      /*  data export from dbox by icon dragging   */
#include "visdelay.h"    /*  show the hourglass for delay             */

#include "aim_stdlib.h"
#include <math.h>


#include "image.h" 

#include "mymem.h"
#include "c_heap.h"


typedef struct {
 short left,
       bottom,
       right,
       top;
 }os_wblock;
 
typedef struct {
   char b[12];
 } par_block;

extern sprite_pixtrans trans[256];     /* colour translation table     */
extern int magnmult,
           magndiv; 
extern int wimp_mode;
extern max_signals;
extern char * alloc[200];
extern int alloc_ptr,alloc_bottom,alloc_top;
extern BOOL compress;
extern void select_trans(wimp_palettestr *p);
extern VENSTER* vp[MAXSIG];
extern void update_signal_window(wimp_redrawstr r);
extern void open_signal_window(wimp_openstr *o);
extern void init_plot_draw(VENSTER *vp);
extern void redraw_signal_window(wimp_w handle);
extern void image_menu_proc(void *handle,char * hit);

/*************************** Image Pool declaration ************************/

 VENSTER * wind_create(unsigned int alloc_size)
{

 VENSTER *vp;

  /* --- allocate our WINDOW area --- */
 vp = (VENSTER *)malloc(sizeof(VENSTER));
 if (vp == NULL) werr(1,"Error in wind_create: WINDOW workspace failed\r"); 

 Window_DispM(vp)=1;
 DataReal(vp)= (SAMPLE*) heap_alloc((unsigned int)alloc_size*sizeof(SAMPLE));
 DataImag(vp)= (SAMPLE*) heap_alloc((unsigned int)alloc_size*sizeof(SAMPLE));

 if ((DataReal(vp)==NULL)||(DataImag(vp)==NULL))
       werr(1,"Failed to allocate memory for Signals");
 
 return(vp);
 }


VENSTER *Create_ImagePool(unsigned int alloc_size, char * name)
{
  
  void venster_menu_proc(void *handle,char * hit);
  void venster_handler(wimp_eventstr *e, void *handle);
  extern menu hsit;
  VENSTER * vp;
  template *t;
  char nm[80];


  vp=wind_create(alloc_size);
  if (vp==NULL) werr(1,"Unable to create Signal pool");
  
  DataAllocSize(vp) = alloc_size;

   /* Maak een nieuw displaywindow aan */
  
  /* --- find template for our window and create a window from it --- */
    t=template_copy(template_find("signal"));
    wimpt_complain(wimp_create_wind(&t->window,&Window_Handle(vp)));

                                         
   /* Koppel de eventhandler aan het displaywindow*/
  win_register_event_handler(vp->image,venster_handler,(void *)vp);            

   /* Koppel het menu aan het displaywindow */
  event_attachmenu(vp->image,hsit,venster_menu_proc,(void *)vp);

 /* Geef het window de signaalnaam */

    strncpy(DataSignalName(vp),name,40); 
    strncpy(nm,name,40); 
    win_settitle(Window_Handle(vp),nm); 
  
  Window_Diag(vp).data=NULL;
  init_plot_draw(vp);
   

 return(vp);

}

/******************************************************************************
 * Realloc_Venster (VENSTER *vp, unsigned int alloc_size)                     *
 * Input:  venster, alloc_size                                                *
 * Output: Voor het venster wordt voor de data opnieuw geheugen geallocceerd  *
 *         Het oude gereserveerde geheugen wordt vrijgegeven.                 *
 * Returns: 0                                                                 *
 ******************************************************************************/

int Realloc_Venster(VENSTER *vp, unsigned int alloc_size)

{
 
  heap_free(DataReal(vp));        /* Geef oud gereserveerd geheugen vrij */
  heap_free(DataImag(vp));

  DataReal(vp) = NULL;
  DataImag(vp) = NULL;

  /* Vindt nieuw geheugen */
  DataReal(vp) = (SAMPLE*) heap_alloc((unsigned int)alloc_size*sizeof(SAMPLE));
  DataImag(vp) = (SAMPLE*) heap_alloc((unsigned int)alloc_size*sizeof(SAMPLE));
  if ( (DataReal(vp) == NULL) || (DataImag(vp) == NULL) )
    werr(1,"Error in Realloc_Venster: heap_alloc failed\r");

  DataAllocSize(vp) = alloc_size;

  return 0; /* No Error */
}


void venster_handler(wimp_eventstr *e, void *handle)
{

  VENSTER *vp;

  vp =(VENSTER *)handle;
 /* my_sprite =(spr_details*) handle;  */
  
  switch (e->e)
  {
    case wimp_ENULL:
      break;

    case wimp_EREDRAW:
      redraw_signal_window(e->data.o.w);
      break;

    case wimp_EOPEN:
      open_signal_window(&e->data.o); 
      break;

    case wimp_ECLOSE:
      wimpt_noerr(wimp_close_wind(e->data.o.w));
      break;

    case wimp_ESEND:
    case wimp_ESENDWANTACK:    
      WimpMessage(&e->data.msg);
      break;

    case wimp_MMODECHANGE:
      break;                  
    
    default:  /* we're not interested in any other events */
      break;
  }
}
