/*
 * fs.c - PathMaster File Selector Demo launch module.
 * Copyright © 1989 by Justin V. McCormick.  All Rights Reserved.
 */

/* #define FILEFUNC 1 */ 	/* Uncomment this for filefunction demo */
/* #define SPECFUNC 1 */	/* Uncomment this for delete gadget demo */

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/ports.h>
#include <exec/io.h>
#include <exec/libraries.h>
#include <exec/interrupts.h>
#include <exec/memory.h>
#include <devices/timer.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <libraries/filehandler.h>
#include <stdio.h>

#include "fs.h"

#ifdef AZTEC_C
#include <functions.h>
#endif

#ifdef LATTICE
#define strlen strlen
#include <string.h>
#include <stdlib.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#endif


/* Extern CODE */
extern LONG FileSelect __ARGS((struct FSRequest *));
extern VOID __stdargs ReleaseFileSelect __ARGS((VOID));

/* Local CODE */
int main __ARGS((int, BYTE **));
VOID DoFSTest __ARGS((VOID));
LONG DoFileSelect __ARGS((VOID));

/* Special functions, see defines above */
#ifdef FILEFUNC
LONG __stdargs MyFileFunc __ARGS((struct FSRequest *, struct Window *));
#endif
#ifdef SPECFUNC
LONG __stdargs MySpecGadFunc __ARGS((struct FSRequest *, struct Window *, struct Gadget *));
#endif

/* Local DATA */
struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;
struct FSRequest MyFSReq;
BYTE thePath[PATHSTRSIZE+FILESTRSIZE+12]; /* Room for entire path returned by FS */

/* -------------------------------------------------------------------- */
int main(argc, argv)
  int argc;
  BYTE **argv;
{
  if ((GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L)) != 0)
  {
    if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 33L)) != 0)
    {
      DoFSTest();
      CloseLibrary((struct Library *)IntuitionBase);
    }
    CloseLibrary((struct Library *)GfxBase);
  }
  return(0);
}

/* -------------------------------------------------------------------- */
VOID DoFSTest()
{
/* Set our request preferences */

/* Match all filenames */
  (VOID)strcpy(MyFSReq.matchpattern, "*");

/* Set window positions */
  MyFSReq.leftedge 	= 0;
  MyFSReq.topedge 	= 11;

/* Want alphabetized sorting */
  MyFSReq.sorttype 	= 0;

/* Show directory names first, delay sorting till user scrolls */
  MyFSReq.flags		= FS_SHOW_DIRS_FIRST | FS_DELAYED_SORT;

/* Put the pathname result here */
  MyFSReq.fullname 	= thePath;

/* Do not show filename that match this pattern */
  MyFSReq.ignorepattern = "*.info";

/* Title for the selector window */
  MyFSReq.titlestr 	= "PathMaster Demo>";

/* Text for the OK and CANCEL gadgets */
  MyFSReq.selectstr	= "Done";
  MyFSReq.cancelstr	= "Cancel";

#ifdef FILEFUNC
  MyFSReq.filefunc 	= MyFileFunc;
#endif

/* Set up the black-box gadget to delete files */
#ifdef SPECFUNC
  MyFSReq.specgadfunc 	= MySpecGadFunc;
  MyFSReq.specgadstr 	= "Delete";
#endif

/* Invoke the file selector until user selects CANCEL */
  while (DoFileSelect() != 0)
    continue;

/* Deallocate and release file selector stuff */
  ReleaseFileSelect();
}

/* -------------------------------------------------------------------- */
LONG DoFileSelect()
{
  LONG status;

  status = FileSelect (&MyFSReq);
  if (status == 1)
  {
    if (MyFSReq.fullname[0] == 0)
    {
      printf("FileSelect Canceled\n");
      status = 0;
    }
    else
      printf("%s\n", MyFSReq.fullname);
  }
  else
    printf("FileSelect Failed!\n");
  return (status);
}

#ifdef FILEFUNC
/* -------------------------------------------------------------------- */
LONG __stdargs MyFileFunc(fsreq, fswin)
  struct FSRequest *fsreq;
  struct Window *fswin;
{
  printf("Clicked on '%s'\n", fsreq->fullname);
  return(0L);
}
#endif

#ifdef SPECFUNC
/* -------------------------------------------------------------------- */
LONG __stdargs MySpecGadFunc(fsreq, fswin, fsgad)
  struct FSRequest *fsreq;
  struct Window *fswin;
  struct Gadget *fsgad;
{
  printf("Delete Gadget clicked, current pathname: %s\n", fsreq->fullname);
  (VOID) DeleteFile(fsreq->fullname);
  return(1L);	/* Force FileSelect to re-read directory */
}
#endif
