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

   Program:    PrLabel
   File:       Support.c
   
   Version:    V1.2
   Date:       14.09.92
   Function:   Generally useful support routines for PrLabel
   
   Copyright:  SciTech Software 1992
   Author:     Andrew C. R. Martin
   Address:    SciTech Software
               23, Stag Leys,
               Ashtead,
               Surrey,
               KT21 2TD.
   Phone:      +44 (0372) 275775
   EMail:      UUCP: cbmuk!cbmuka!scitec!amartin
               JANET: andrew@uk.ac.ox.biop
               
****************************************************************************

   This program is not in the public domain, but it may be freely copied
   and distributed for no charge providing this header is included.
   The code may be modified as required, but any modifications must be
   documented so that the person responsible can be identified. If someone
   else breaks this code, I don't want to be blamed for code that does not
   work! The code may not be sold commercially without prior permission from
   the author, although it may be given away free with commercial products,
   providing it is made clear that this program is free and that the source
   code is provided with the program.

****************************************************************************

   Description:
   ============
   GetAFile()
   This routine attempts to open and use the asl library and V2.0 file
   requester.
   If unavailable, it uses Charlie Heath's file requester.



   N.B. 
   Must be linked with the Heath file requester from Fish Disk 41.


****************************************************************************

   Usage:
   ======
   GetAFile(AslBase,Wind,Scr,prompt,fname,fdir,fbuff)
   
   Inputs:  (struct Library *)AslBase  A pointer to the asl.library base from
                                       OpenLibrary(). If NULL uses the Heath
                                       file requester
            (struct Window *) Wind     A pointer to the Window on which the 
                                       requester is to open
            (struct Screen *) Scr      A pointer to the Screen on which the
                                       requester is to open
            char              *prompt  A pointer to a prompt string
   I/O:     char              *fname   A pointer to a default filename (or NULL).
                                       The chosen filename will be returned here
            char              *fdir    A pointer to a default directory (or NULL).
                                       The chosen directory will be returned here
   Output:  char              *fbuff   Returns a pointer to the complete path and
         
   Returns: int                        1 if a file was selected
                                       0 if the CANCEL gadget was chosen

****************************************************************************

   Revision History:
   =================
   V1.1     09.02.91
   Now frees the file requester memory (oops!)
   V1.2     25.09.91
   Now correctly builds fbuff when no path specified.
   V1.3     28.05.92
   ANSIed and static'd

***************************************************************************/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/dosextens.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuitionbase.h>
#include <libraries/asl.h>

#ifdef LATTICE
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/asl_protos.h>

/* #include "asl_pragmas.h" */

#ifdef STOP_C
int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
int chkabort(void) { return(0); }  /* really */
#endif

#endif /* LATTICE */

/* for file requester */
BOOL   AslRequestTags( APTR request, ULONG tags, ...);

/***************************************************************************/
GetAFile(struct Library *AslBase,
         struct Window  *Wind,
         struct Screen  *Scr,
         char           *prompt,
         char           *fname,
         char           *fdir,
         char           *fbuff)
{
   struct FileRequester *freq = NULL;
   
   struct Process *OurTask;
   struct Window  *old_pr_WindowPtr;
   char   *p;
   int    retval=1,
          j,
          ASL_Done = 0;

   /* If have asl.library, try requester */
   if(AslBase)
   {
      ASL_Done = 1;

      /* Allocate the file requester */
      if(!freq) freq = AllocAslRequest(ASL_FileRequest, NULL);

      if(freq)
      {
         if(AslRequestTags((APTR)freq,
            ASL_Hail,prompt,
            ASL_OKText,"Select",
            ASL_Window, Wind,
            ASL_Dir, fdir,
            ASL_File, fname,
            TAG_DONE ))
         {
            strcpy(fdir,freq->rf_Dir);
            strcpy(fname,freq->rf_File);
         }
         else
         {
            retval=0;
         }
         FreeFileRequest(freq);
      }
      else
      {
         /* Failed to allocate file requester */
         ASL_Done = 0;
      }
      
   }
   
   if(!ASL_Done) /* Either no asl.library or couldn't allocate file requester */
   {
      OurTask = (struct Process *)FindTask(0L);
      old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
      OurTask->pr_WindowPtr = (APTR)Wind;

      if(!get_fname(Wind,Scr,prompt,fname,fdir))
      {
         retval=0;
      }

      OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
   }
      
   if(retval)  /* We got a filename back from one of the file requesters */
   {
      /* Kill any trailing spaces from the path */
      for(j=strlen(fdir)-1; fdir[j] == ' ' && j>=0; j--)
         fdir[j] = '\0';

      p = stpcpy(fbuff,fdir);
      if( strlen(fdir) && *(p-1) != ':')
      {
         *p = '/';
         p++;
      }
      p = stpcpy(p,fname);
   }

   return(retval);
}

/***************************************************************************/
static BOOL AslRequestTags(APTR  request,
                           ULONG tags, ...)
{
   return(AslRequest(request, (struct TagItem *)&tags));
}




