/* templates.c */
/* Template loading and memory allocation */

#include <string.h>
#include <stdlib.h>

#include "DeskLib:Error.h"
#include "DeskLib:WimpSWIs.h"

#include "templates.h"

#ifndef SWI_Wimp_CreateWindow
#define SWI_Wimp_CreateWindow 0x400c1
#endif

#define WINSIZE 1024
#define CHARSIZE 1024

window_block *templates_load(char *name,int *index,font_array *font,
                             char **indbuffer,int *indsize)
{
  template_block template;
  char id[12];

  /* Can't rely on RISC OS 3 so use arbitrary sizes */
  template.buffer = malloc(WINSIZE);
  template.workfree = malloc(CHARSIZE);
  template.workend = template.workfree + CHARSIZE;;
  if (font)
    template.font = font;
  else
    template.font = (font_array *)-1;
  strcpy(id,name);
  template.name = id;
  if (index)
    template.index = *index;
  else
    template.index = 0;
  Error_CheckFatal(Wimp_LoadTemplate(&template));

  /* Return size of ind data here, while we know it */
  /* if (indsize)
    *indsize = CHARSIZE;
  if (indbuffer)
    *indbuffer = template.workfree; */

  /* Return info to caller */
  /* if (index)
    *index = template.index; */
  return template.buffer;
}

window_handle templates_create(char *name)
{
  window_block *template;
  window_handle handle;

  template = templates_load(name,0,0,0,0);
  Error_CheckFatal(Wimp_CreateWindow(template,&handle));
  free(template);
  return handle;
}
