#ifndef FILEIO_H
#define FILEIO_H

#ifdef AZTEC_C
#define NARGS
#endif

/* *** fileio.h  Version 1.9 ******************************************
 *
 * Amiga Programmers' Suite  --  File IO Include File
 *     from Book 1 of the Amiga Programmers' Suite by RJ Mical
 *
 * Copyright (C) 1986, =Robert J. Mical=
 * All Rights Reserved.
 *
 * Created for Amiga developers.
 * Any or all of this code can be used in any program as long as this
 * entire notice is retained, ok?  Thanks.
 *
 * The Amiga Programmer's Suite Book 1 is copyrighted but freely distributable.
 * All copyright notices and all file headers must be retained intact.
 * The Amiga Programmer's Suite Book 1 may be compiled and assembled, and the 
 * resultant object code may be included in any software product.  However, no 
 * portion of the source listings or documentation of the Amiga Programmer's 
 * Suite Book 1 may be distributed or sold for profit or in a for-profit 
 * product without the written authorization of the author, RJ Mical.
 *
 * HISTORY      NAME            DESCRIPTION
 * -----------  --------------  --------------------------------------------
 * 20 Oct 87    - RJ            Added RENAME_RAMDISK to fix what seems to 
 *                              be a bug in AmigaDOS.
 * 27 Sep 87    RJ              Removed reference to alerts.h, brought 
 *                              declarations into this file
 * 12 Aug 86    RJ              Prepare (clean house) for release
 * 14 Feb 86    =RJ Mical=      Created this file.

 From Sept 87 to Dec 89, the original code was rewritten entirely in assembly,
 converted to a disk-based library, and numerous features were added such as:
    1). Filename pattern matching
    2). Support for applications without a window, including BASIC programs
    3). Various string manipulation routines added to replace original
        code.
    4). Improved and debugged error handling and error returns, including
        a better method of recovering from illegal/non-existant path names
    5). Ability to add custom routines in a logical manner
    6). Returns information about the selected file such as whether it exists,
        and what its size is.
    7). Returns free disk space on the selected disk
    8). Automatic handling of filename selection when the requester can't
        open.
    9). Routines for displaying text in the title bar, and getting user input
        via the window title bar.
   10). Improved method of maintaining mounted device and volume names.
   11). A function to parse filenames from AmigaDOS as passed into a program's
        main() function. 
   12). Improved graphic representation for the requester, including
        larger display for filenames and repeat auto-scroll arrows.
   13). Faster text rendering and directory list construction.
   14). Ability to choose the disk name from a list of mounted devices using
        the mouse.
   15). Ability to display custom lists instead of filenames, with all of the
        display features of the requester.
   16). Ability to select multiple filenames.
   17). C, assembly, and Basic examples with support files (INCLUDES).
   18). Smaller size than the original code.
   19). A quick way to save image, PROJECT icons without needing the icon
        library.

   The current version of the library is 1.6 and was created by the programmers
at dissidents. The library is such a significant departure from the original
code that virtually every line of code differs from the original. It really is
a different piece of code than R.J.s, but since he started the ball rolling...

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


#define MAX_NAME_LENGTH 30
#define MAX_DRAWER_LENGTH 132
#define SetFlag(v,f)      ((v)|=(f))
#define ClearFlag(v,f)    ((v)&=~(f))
#define ToggleFlag(v,f)   ((v)^=(f))
#define FlagIsSet(v,f)    ((BOOL)(((v)&(f))!=0))

struct HandlerBlock {
	APTR  StartUpCode;
	APTR  DiskInsertedCode;
	APTR  GadgetCode;
	APTR  KeyCode;
	APTR  MouseMoveCode;
	};

/* === FileIO Structure ========================================== */

struct FileIO {
	USHORT	Flags;

	/* After a successful call to DoFileIO(), these fields will have
	* the names selected by the user.  You should never have to initialize
	* these fields, only read from them, though initializing them won't hurt.
	*/
	UBYTE FileName[MAX_NAME_LENGTH];
	UBYTE Drawer[MAX_DRAWER_LENGTH];
	UBYTE Disk[MAX_NAME_LENGTH];

	/* If a Lock on a disk/dir was obtained, it can be found here. */
	struct DOSLock *Lock;

	/* These are the variables associated with the list of filenames. Also,
		any custom list of strings */
	USHORT NameCount;		/* # of items */
	USHORT NameStart;		/* The number of the first item visible in the req */
	SHORT  CurrPick;		/* The number of the selected item starting at 0. If -1, none selected */
	struct Remember *FileList;

	UBYTE *FileIOText;	/* for SPECIAL_REQ */
	APTR FileIORoutine;	/* for SPECIAL_REQ */

	SHORT MatchType;		/* DiskObject Type to match */
	UBYTE *ToolTypes;		/* If NULL, no string to match */

	UBYTE *Extension;
	USHORT ExtSize;		/* Don't count the terminating NULL */

	struct HandlerBlock *Custom;

	USHORT X;				/* where the req opens when using DoFileIO() */
	USHORT Y;

	ULONG  FreeBytes;		/* on the disk */
	ULONG  FileSize;		/* of the selected file. ID if SPECIAL_REQ */

	UBYTE  *Title;			/* for DoFileIOWindow */

	UBYTE  *Buffer;		/* Where to put the complete DOS path */

	APTR   RawCode;		/* for DoRawkey, PromptUserInput */

	struct DOSLock  *OriginalLock;

	BYTE	Errno;			/* See ERRNO numbers */

	UBYTE  DrawMode;		/* for title bar input */
	UBYTE  PenA;
	UBYTE  PenB;
	};

/* === User FileIO Flag Definitions === */
#define NO_CARE_REDRAW     0x0001  /* Clear if reconstructing display */
#define USE_DEVICE_NAMES   0x0002  /* Set for device instead of volume names */
#define EXTENSION_MATCH    0x0004  /* Only display those that end with
                                      a specified string */
#define DOUBLECLICK_OFF    0x0008  /* Inhibit double-clicking if set */
#define WBENCH_MATCH       0x0010  /* If set check .info files only */
#define MATCH_OBJECTTYPE   0x0020  /* If set with .info also check MatchType */
#define MULTIPLE_FILES     0x0040  /* If set, allow multiple file selection */
#define INFO_SUPPRESS      0x0080  /* No info files listed */
#define CUSTOM_HANDLERS    0x0200  /* Implement custom handlers */
#define NO_ALPHA           0x1000  /* No alphabetize filenames or lists */
#define DISK_HAS_CHANGED   0x2000  /* Disk changed during file selection */
#define SHOW_DISK_NAMES    0x4000  /* Show disk names instead of filenames */
#define SPECIAL_REQ        0x8000  /* For displaying lists of strings */

/* === System FileIO Flags (Don't alter these) === */
#define ALLOCATED_FILEIO   0x0100  /* Not a pre-initialized FileIO struct */
#define WINDOW_OPENED      0x0400  /* DoFileIOWindow() was called */
#define TITLE_CHANGED      0x0800  /* SetTitle() called without ResetTitle() */


/*  FileRequester GadgetIDs - Do not use these IDs for your own gadgets */
#define FILEIO_CANCEL      0x7FA0
#define FILEIO_OK          0x7FA1
#define FILEIO_NAMETEXT    0x7FA2
#define FILEIO_DRAWERTEXT  0x7FA3
#define FILEIO_DISKTEXT    0x7FA4
#define FILEIO_SELECTNAME  0x7FA5
#define FILEIO_UPGADGET    0x7FA6
#define FILEIO_DOWNGADGET  0x7FA7
#define FILEIO_PROPGADGET  0x7FA8
#define FILEIO_NEXTDISK    0x7FA9
#define FILEIO_BACKDROP    0x7FAA

#define NAME_ENTRY_COUNT   7   /* These many names in the SelectName box */

#define REQTITLE_HEIGHT    8

#define REQ_LEFT          8
#define REQ_TOP           15
#define REQ_WIDTH         286
#define REQ_HEIGHT        (110 + REQTITLE_HEIGHT)
#define REQ_LINEHEIGHT    8

#define SELECTNAMES_LEFT    8
#define SELECTNAMES_TOP     (15 + REQTITLE_HEIGHT)
#define SELECTNAMES_WIDTH   122
#define SELECTNAMES_HEIGHT  60


 /* ======= ERRNO numbers returned in FileIO error field ========= */

#define ERR_MANUAL  1   /* the path was entered manually via the title bar
                           with no errors or cancellation. */
#define ERR_SUCCESS 0   /* everything went OK */
#define ERR_CANCEL  -1  /* the filename procedure was CANCELED by the user */
#define ERR_INUSE   -2  /* for SPECIAL_REQ, the requester is in use by another task */
#define ERR_APPGADG -3  /* the requester was CANCELED by an application gadget
                           (via an installed CUSTOM gadget handler returning TRUE) */
#define ERR_WINDOW  -4  /* the window couldn't open (in DoFileIOWindow()) */

/* =============== AutoFileMessage() Numbers =========== */
#define ALERT_OUTOFMEM            0
#define ALERT_BAD_DIRECTORY       1
#define READ_WRITE_ERROR          2 /* Error in reading or writing file */
 /* The next 3 display "YES" and "NO" prompts,
    returning d0=1 for yes, 0 for no */
#define FILE_EXISTS               3 /* File already exists. Overwrite? */
#define SAVE_CHANGES              4 /* Changes have been made. Save them? */
#define REALLY_QUIT               5 /* Do you really want to quit? */

/* ====================== Entry Structure ====================== */
/* Don't use sizeof on these structures. The library makes them for you when
   you call AddEntry. The 3rd field of the Entry structure is a variable length
   string. This structure has been defined simply so that you can get access to
   the EntryID EntryFlags, and EntryString. This structure is used by
   SPECIAL_REQ, and also for the list of filenames. For filenames, each has a
   FileEntry structure. Its EntryID is the filesize. Bit #7 of the EntryFlags
   is set if the filename was selected by the user. The EntryString is the
   NULL-terminated filename (separated from its dir). When the SPECIAL_REQ
   flag is set, the Entry's EntryID is your passed ID to AddEntry().
   All the FileEntry structures are linked together, and the FileIO's
   FileList field points to the first FileEntry in the list. */

struct Entry {
   LONG  EntryID;
   UBYTE EntryFlags;
   UBYTE EntryString[1]; /* size is actually length of null-terminated string */
   };

struct FileEntry {
	struct FileEntry  *nextEntry;
   ULONG  EntrySize;
   struct Entry     *filePart;
   };

/* ============= Requester Library Function Declarations ============= */


#ifndef __ARGS
#ifdef NARGS
#define __ARGS(a) ()
#else
#define __ARGS(a) a
#endif
#endif

extern VOID  SetFileIOHandlers __ARGS(( struct HandlerBlock * ));

extern struct FileIO *GetFileIO __ARGS(( void )); 
extern UBYTE  *DoFileIOWindow __ARGS(( struct FileIO *, struct Screen * )); 
  /* address = DoFileIOWindow( myFileIO, myScreen );
     If myScreen is NULL, then use WB screen */
extern UBYTE  *DoFileIO __ARGS(( struct FileIO *, struct Window * )); 

extern BOOL   AutoFileMessage __ARGS(( ULONG, struct Window * ));
	/* result = AutoFileMessage( 3L, myWindow ); */
extern BOOL   AutoMessage __ARGS(( UBYTE *, struct Window * ));
extern BOOL   AutoMessageLen __ARGS(( UBYTE *, struct Window *, ULONG )); 
extern BOOL   AutoPrompt3 __ARGS(( UBYTE *, UBYTE *,UBYTE *, struct Window * )); 

extern void   ReleaseFileIO __ARGS(( struct FileIO * ));
extern void   SetWaitPointer __ARGS(( struct Window * ));  /* SetWaitPointer( myWindow ); */
extern void   ResetBuffer __ARGS(( struct StringInfo *, ULONG )); 
				/* ResetBuffer( StringInfo, nullFlag ); resets the cursor back to 
				the first char in the stringinfo's buffer. If nullFlag is TRUE, 
				then NULLS the buffer as well. */

extern UBYTE  *UserEntry __ARGS(( ULONG, UBYTE *, struct FileIO *, struct Window * )); 
extern UBYTE  *PromptUserEntry __ARGS(( ULONG, UBYTE *, UBYTE *, struct FileIO *, struct Window * ));
extern UBYTE  *GetFullPathname __ARGS(( struct FileIO *, UBYTE * ));
extern UBYTE  *TypeFilename __ARGS(( struct FileIO *, struct Window * ));
extern UBYTE  *ParseString __ARGS(( struct FileIO *, UBYTE * ));

extern UWORD  GetRawkey __ARGS(( struct Window * ));
extern UWORD  DecodeRawkey __ARGS(( USHORT, USHORT ));
extern void   SetTitle __ARGS(( UBYTE *, UBYTE *, struct FileIO *, struct Window * ));
extern void   ResetTitle __ARGS(( struct FileIO *, struct Window * ));

extern WORD   NewEntryList __ARGS(( struct FileIO * ));
extern WORD   AddEntry __ARGS(( LONG, UBYTE *, struct FileIO * ));
extern WORD   IsEntryThere __ARGS(( UBYTE *, struct FileIO * ));
extern struct FileEntry  *RetrieveEntry __ARGS(( struct FileEntry *, struct FileIO * ));
extern void   ClearEntries __ARGS(( struct FileIO * ));

extern LONG   PutProjIcon __ARGS(( UBYTE *, struct DiskObject * )); 
	/* Just like icon lib's PutIcon(), but for image PROJECT icons only. 
		Returns 0 if success. */


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

	It is recommended that pragmas not be used with the FileIO routines. This
	is because certain calls do a bit more than just fiddle with the stack
	arguments. For the adventurous, it is possible to edit FileInterface.asm
	using only those elements which are somewhat 'funky', and use pragmas for
	the remainder. Since you've got to include the interface anyway, is the pain
	worth the few bytes you'll save? That's up to you...

#ifndef NO_PRAGMAS

#pragma libcall RequesterBase DoFileIOWindow 	1e 9802
#pragma libcall RequesterBase GetFileIO 			24 0
#pragma libcall RequesterBase DoFileIO 			2a 9802
#pragma libcall RequesterBase GetFullPathname 	30 9802
#pragma libcall RequesterBase AutoFileMessage 	36 8102
#pragma libcall RequesterBase ReleaseFileIO 		3c 901
#pragma libcall RequesterBase AutoMessage 		42 8002
#pragma libcall RequesterBase SetWaitPointer 	48 801
#pragma libcall RequesterBase ResetBuffer 		4e 802
#pragma libcall RequesterBase AutoMessageLen 	54 18003
#pragma libcall RequesterBase AutoPrompt3 		5a 8ba904
#pragma libcall RequesterBase UserEntry 			60 ba8004
#pragma libcall RequesterBase PromptUserEntry 	66 ba98005
#pragma libcall RequesterBase GetRawkey 			6c b01
#pragma libcall RequesterBase DecodeRawkey 		72 902
#pragma libcall RequesterBase TypeFilename 		78 9802
#pragma libcall RequesterBase SetTitle 			7e ba9804
#pragma libcall RequesterBase ResetTitle 			84 ba02
#pragma libcall RequesterBase ParseString 		8a 9802
#pragma libcall RequesterBase NewEntryList 		90 901
#pragma libcall RequesterBase AddEntry 			96 98103
#pragma libcall RequesterBase IsEntryThere 		9c 9802
#pragma libcall RequesterBase RetrieveEntry 		a2 9802
#pragma libcall RequesterBase ClearEntries 		a8 9802
#pragma libcall RequesterBase PutProjIcon 		ae 9802
#pragma libcall RequesterBase FindDeleteEntry	b4 9813
#pragma libcall RequesterBase DeleteEntry 		ba 982
#pragma libcall RequesterBase AddFileGadgs 		c0 db83
#pragma libcall RequesterBase Window_BW	 		c6 801
#pragma libcall RequesterBase BW_Restore	 		cc 802

#endif
********************************************************************** */

#endif /* of FILEIO_H */

