#include <intuition/intuisup.h>
#include <intuition/intuitionbase.h>

extern struct Library *DOSBase;
struct Library *IntuitionBase, *GfxBase, *IconBase;

#ifdef ISUP_RUNTIME
struct   Library  *iSupBase;
#else
struct   Device   *ConsoleDevice;
struct   IOStdReq  ConsoleMsg;
#endif


static BOOL TestFileIO(fileio, window)
struct FileIOSupport *fileio;
struct Window *window;
/* This function calls GetFileIOName wich pops up the requester and waits  */
/* for user input, then builds the complete selected pathname through      */
/* BuildFileIOPathName() and displays it with a AutoSmartRequester().      */
/* Moreover, it looks at the flags in the FileIOSupport struct upon return */
/* and says TRUE if the user swapped disks while the file req. was on the  */
/* screen and FALSE otherwise.                                             */
{
   UBYTE name[80];

   if (GetFileIOName(fileio, window, 6, 15))
      {
      /* If user was positive, display the name */
      StrCpy(&name[0], "[");
      BuildFileIOPathname(fileio, &name[1]);
      StrCat(&name[0], "]");
      AutoSmartRequest(window, 1, &name[0], 0, "Ok", NULL, NULL, NULL);
      }

   /* if disk were swapped, tell it             */
   if (FlagIsSet(fileio->Flags, FR_DISK_HAS_CHANGED))
          {
          ClearFlag(fileio->Flags, FR_DISK_HAS_CHANGED);
          return(TRUE);
          }
   else return(FALSE);
}


VOID main(argc, argv)
LONG argc;
char **argv;
{
   extern struct IntuiMessage *GetMsg();
   extern struct Window       *OpenStdWindow();

   BOOL   mainswitch = TRUE, ioswitch;
   LONG   class;
   SHORT  pick = 0;
   struct Window        *window;
   struct IntuiMessage  *message;
   struct FileIOSupport *fileio1, *fileio2;


   if (  !(GfxBase       = (struct Library *)OpenLibrary("graphics.library", 0))
      || !(IntuitionBase = (struct Library *)OpenLibrary("intuition.library", 0))
      || !(IconBase      = (struct Library *)OpenLibrary("icon.library", 0))
#ifdef ISUP_RUNTIME
      || !(iSupBase      = (struct Library *)OpenLibrary("isup.library", 0))
#else
      || !(!OpenDevice("console.device", -1, &ConsoleMsg, 0) && (ConsoleDevice=ConsoleMsg.io_Device))
#endif
      ) goto MAIN_DONE;

   if (!(window=OpenStdWindow("File Requester Example", 0x1B, 15, 65, 350, 70, NULL))) goto MAIN_DONE;
   Emit(window, "Click me for a file i/o requester",  42, 40, 2);

   /* Get a FileIOSupport for each requester                      */
   fileio1 = GetFileIOSupport(NULL);  /* NULL and "" are the same */
   fileio2 = GetFileIOSupport("");    /* and mean current dir.    */
   if (!fileio1 || !fileio2) goto MAIN_DONE;

   /* Alter some of the default values                            */
   fileio2->ReqTitle   = (UBYTE *)"Select Spreadsheet Name";
   fileio2->ReqPattern = "*.c";
   SetFlag(fileio1->Flags, FR_DRAG_BAR_OFF | FR_AUTO_CENTER );
   SetFlag(fileio2->Flags, FR_AUTO_CENTER);

   while (mainswitch)
         {
         WaitPort(window->UserPort);

         ioswitch = FALSE;
         while (message = GetMsg(window->UserPort))
               {
               class   = message->Class;
               ReplyMsg(message);

               switch (class)
                      {
                      case CLOSEWINDOW: mainswitch = FALSE; break;

                      case DISKINSERTED:
                         /* You should clear the GOOD_FILENAMES flag whenever you   */
                         /* detect that a disk was inserted                         */
                         ClearFlag(fileio1->Flags, FR_GOOD_FILENAMES);
                         ClearFlag(fileio2->Flags, FR_GOOD_FILENAMES);
                      break;

                      default:
                         /* If any other event occurs, bring up that old requester! */
                         ioswitch = TRUE;
                         break;
                      }
               }

         if (ioswitch)
            {
            if (pick == 0)
               {
               if (TestFileIO(fileio1, window))
                  ClearFlag(fileio2->Flags, FR_GOOD_FILENAMES);

               /* Alternate a normal and a dir requester */
               ToggleFlag(fileio1->Flags, FR_DIR_ONLY);
               ClearFlag(fileio1->Flags, FR_GOOD_FILENAMES);
               }
            else
               {
               TestFileIO(fileio2, window);
                 /* Dont need to test if disk swapped for we clear */
                 /* the flag on fileio1 each time anyway.          */
                 /* ClearFlag(fileio1->Flags, FR_GOOD_FILENAMES);  */
               }
            pick = 1 - pick;
            }
         }

   MAIN_DONE:
      if (fileio1)       ReleaseFileIO(fileio1);
      if (fileio2)       ReleaseFileIO(fileio2);
      if (window)        CloseStdWindow(window);

#ifdef ISUP_RUNTIME
      if (iSupBase)      CloseLibrary(iSupBase);
#else
      if (ConsoleDevice) CloseDevice(&ConsoleMsg);
#endif
      if (IntuitionBase) CloseLibrary(IntuitionBase);
      if (GfxBase)       CloseLibrary(GfxBase);
      if (DOSBase)       CloseLibrary(DOSBase);
      if (IconBase)      CloseLibrary(IconBase);
      exit(0);
}

