/* Title: -> c.loadas
 * Purpose: generalised data load to a concurrent wimp program.
 */

#define BOOL int
#define TRUE 1
#define FALSE 0

#include <string.h>
#include <stdio.h>

#include "trace.h"
#include "os.h"
#include "bbc.h"
#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "dbox.h"
#include "loadas.h"
#include "xfersend.h"
#include "fileicon.h"
#include "werr.h"
#include "menu.h"
#include "event.h"
#include "msgs.h"

#define loadas_FOK     0           /* OK action button */
#define loadas_FName   2           /* name field */
#define loadas_FIcon   3           /* icon to drag. */


static int loadas__filetype;
static xfersend_saveproc  loadas__loadproc;
static xfersend_sendproc  loadas__sendproc;
static xfersend_printproc loadas__printproc;
static void *loadas__loadhandle;
static int loadas__estsize = 0;
static char filename[256];
static dbox loadas__d ;




BOOL loadas(int filetype, char *name, int estsize,
            xfersend_saveproc  loadproc,
            xfersend_sendproc  sendproc,
            xfersend_printproc printproc,
            void *handle)
{
  loadas__d = dbox_new("xfer_load");
  if (loadas__d == 0) return TRUE;

  loadas__filetype   = filetype;
  loadas__loadproc   = loadproc;
  loadas__sendproc   = sendproc;
  loadas__printproc  = printproc;
  loadas__loadhandle = handle;
  loadas__estsize    = estsize;

  dbox_show(loadas__d);

  fileicon((wimp_w) dbox_syshandle(loadas__d), loadas_FIcon, filetype);

  dbox_setfield(loadas__d, loadas_FName, name);
  strncpy(filename, name, 256);

  while (dbox_fillin(loadas__d) == loadas_FOK)
  {
   dbox_getfield(loadas__d, loadas_FName, filename, 256);

   /* Check for name with no "." in it, and complain if so. */
   {
    int i = 0;
    BOOL dot = FALSE;
    while ((! dot) && filename[i] != 0) dot = filename[i++] == '.';

    if (! dot)
    {
     werr(FALSE,"To load: please specify full path name and filename");
     continue;
    };
   };

   xfersend_set_fileissafe(TRUE);

   if (loadas__loadproc(filename, loadas__loadhandle) &&
       !dbox_persist()) break;
  };

  tracef0("loadas loop done.\n");
  xfersend_close_on_xfer(FALSE, 0);
  dbox_hide(loadas__d);
  dbox_dispose(&loadas__d);

  return TRUE;
}


/* end loadas.c */
