home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 073.lha / FileIO / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-02  |  6.6 KB  |  229 lines

  1.  
  2. /* *** main.c ***************************************************************
  3.  *
  4.  * File IO Suite  --  Example Main 
  5.  *     from Book 1 of the Amiga Programmers' Suite by RJ Mical
  6.  *
  7.  * Copyright (C) 1986, 1987, Robert J. Mical
  8.  * All Rights Reserved.
  9.  *
  10.  * Created for Amiga developers.
  11.  * Any or all of this code can be used in any program as long as this
  12.  * entire copyright notice is retained, ok?
  13.  *
  14.  * HISTORY      NAME            DESCRIPTION
  15.  * -----------  --------------  --------------------------------------------
  16.  * 4 Feb 87     RJ              Real release
  17.  * 12 Aug 86    RJ >:-{)*       Prepare (clean house) for release
  18.  * 3 May 86     =RJ Mical=      Fix prop gadget for both 1.1 and 1.2
  19.  * 1 Feb 86     =RJ Mical=      Created this file.
  20.  *
  21.  * *********************************************************************** */
  22.  
  23.  
  24. #define FILEIO_SOURCEFILE
  25. #include "fileio.h"
  26.  
  27.  
  28. BOOL TestFileIO();
  29.  
  30.  
  31. struct NewScreen NewFileIOScreen =
  32.     {
  33.     0, 0,   /* LeftEdge, TopEdge */
  34.     320, 200, /* Width, Height */
  35.     2, /* Depth */
  36.     0, 1,   /* Detail/BlockPens */
  37.     NULL,   /* ViewPort Modes (must set/clear HIRES as needed) */
  38.     CUSTOMSCREEN,
  39.     &SafeFont,  /* Font */
  40.     (UBYTE *)"Example FileIO Program's Screen",
  41.     NULL,   /* Gadgets */
  42.     NULL,   /* CustomBitMap */
  43.     };
  44.  
  45. struct NewWindow NewFileIOWindow =
  46.     {
  47.     0, 12,                  /* LeftEdge, TopEdge */
  48.     320, 150,           /* Width, Height */
  49.     -1, -1,             /* Detail/BlockPens */
  50.     NEWSIZE | MOUSEBUTTONS | VANILLAKEY | MENUPICK | CLOSEWINDOW
  51.             | DISKINSERTED,
  52.                             /* IDCMP Flags */
  53.     WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE
  54.             | SIZEBRIGHT | SMART_REFRESH | RMBTRAP | ACTIVATE | NOCAREREFRESH,
  55.                             /* Window Specification Flags */
  56.     NULL,                   /* FirstGadget */
  57.     NULL,                   /* Checkmark */
  58.     (UBYTE *)"FileIO Requester Window",  /* WindowTitle */
  59.     NULL,                   /* Screen */
  60.     NULL,                   /* SuperBitMap */
  61.     96, 30,             /* MinWidth, MinHeight */
  62.     640, 200,           /* MaxWidth, MaxHeight */
  63.     WBENCHSCREEN,
  64.     };
  65.  
  66.  
  67.  
  68. VOID main(argc, argv)
  69. LONG argc;
  70. char **argv;
  71. {
  72.     struct Screen *screen;
  73.     struct Window *window;
  74.     struct FileIOSupport *fileio1, *fileio2;
  75.     BOOL mainswitch, ioswitch, mainsuccess;
  76.     LONG class;
  77.     struct IntuiMessage *message;
  78.     SHORT pick;
  79.  
  80.  
  81.     mainsuccess = FALSE;
  82.  
  83.     screen = NULL;
  84.     window = NULL;
  85.     fileio1 = fileio2 = NULL;
  86.  
  87.     IntuitionBase = (struct IntuitionBase *)
  88.             OpenLibrary("intuition.library", 0);
  89.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  90.     DosBase = (struct DosLibrary *)OpenLibrary("dos.library", 0);
  91.     IconBase = (struct IconBase *)OpenLibrary("icon.library", 0);
  92.  
  93.     if ( (IntuitionBase == NULL)
  94.             || (GfxBase == NULL)
  95.             || (DosBase == NULL)
  96.             || (IconBase == NULL) )
  97.         goto MAIN_DONE;
  98.  
  99.     if (argv)
  100.         {
  101.         /* OK, we started from CLI */
  102.         if (argc > 1)
  103.             {
  104.             if (screen = OpenScreen(&NewFileIOScreen))
  105.                 {
  106.                 NewFileIOWindow.Screen = screen;
  107.                 NewFileIOWindow.Type = CUSTOMSCREEN;
  108.                 }
  109.             }
  110.         }
  111.  
  112.     window = OpenWindow(&NewFileIOWindow);
  113.  
  114.     fileio1 = GetFileIOSupport();
  115.     fileio2 = GetFileIOSupport();
  116.  
  117.     if ((window == NULL) || (fileio1 == NULL) || (fileio2 == NULL))
  118.         goto MAIN_DONE;
  119.  
  120.     SetFlag(fileio2->Flags, WBENCH_MATCH | MATCH_OBJECTTYPE);
  121.     fileio2->DiskObjectType = WBTOOL;
  122.     fileio2->ReqTitle = (UBYTE *)"- Workbench Tools and Drawers -";
  123.  
  124.     SetAPen(window->RPort, 1);
  125.     Move(window->RPort, 25, 40);
  126.     Text(window->RPort, "= Click for FileIO Requester =", 30);
  127.  
  128.     mainswitch = TRUE;
  129.     pick = 0;
  130.  
  131.     while (mainswitch)
  132.         {
  133.         WaitPort(window->UserPort);
  134.  
  135.         ioswitch = FALSE;
  136.         while (message = GetMsg(window->UserPort))
  137.             {
  138.             class = message->Class;
  139.             ReplyMsg(message);
  140.  
  141.             switch (class)
  142.                 {
  143.                 case CLOSEWINDOW:
  144.                     mainswitch = FALSE;
  145.                     break;
  146.                 case DISKINSERTED:
  147.                     /* You should clear the GOOD_FILENAMES flag whenever you
  148.                      * detect that a new disk was inserted.
  149.                      */
  150.                     ClearFlag(fileio1->Flags, GOOD_FILENAMES);
  151.                     ClearFlag(fileio2->Flags, GOOD_FILENAMES);
  152.  
  153.                     /* While I'm here, I'll demo another feature for you. */
  154.                     ToggleFlag(fileio1->Flags, USE_VOLUME_NAMES);
  155.                     ToggleFlag(fileio2->Flags, USE_VOLUME_NAMES);
  156.  
  157.                     break;
  158.                 default:
  159.                     /* If any other event occurs, bring up that old requester! */
  160.                     ioswitch = TRUE;
  161.                     break;
  162.                 }
  163.             }
  164.  
  165.         if (ioswitch)
  166.             {
  167.             if (pick == 0)
  168.                 {
  169.                 if (TestFileIO(fileio1, window))
  170.                     /* Oops, disk swapped, so restart the other */
  171.                     ClearFlag(fileio2->Flags, GOOD_FILENAMES);
  172.                 }
  173.             else
  174.                 {
  175.                 if (TestFileIO(fileio2, window))
  176.                     /* Oops, disk swapped, so restart the other */
  177.                     ClearFlag(fileio1->Flags, GOOD_FILENAMES);
  178.                 }
  179.             pick = 1 - pick;
  180.             }
  181.         }
  182.  
  183.     mainsuccess = TRUE;
  184.  
  185. MAIN_DONE:
  186.     if (NOT mainsuccess) Alert(ALERT_NO_MEMORY, NULL);
  187.  
  188.     if (fileio1) ReleaseFileIO(fileio1);
  189.     if (fileio2) ReleaseFileIO(fileio2);
  190.  
  191.     if (window) CloseWindow(window);
  192.     if (screen) CloseScreen(screen);
  193.  
  194.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  195.     if (GfxBase) CloseLibrary(GfxBase);
  196.     if (DosBase) CloseLibrary(DosBase);
  197.     if (IconBase) CloseLibrary(IconBase);
  198. }
  199.  
  200.  
  201.  
  202. BOOL TestFileIO(fileio, window)
  203. struct FileIOSupport *fileio;
  204. struct Window *window;
  205. /* This guy calls GetFileIOName(), displays the file name selected by the
  206.  * user, and returns TRUE if the user swapped disks during GetFileIOName()
  207.  * (else returns FALSE)
  208.  */
  209. {
  210.     UBYTE name[80];
  211.  
  212.     if (GetFileIOName(fileio, window))
  213.         {
  214.         /* If user was positive, display the name */
  215.         CopyString(&name[0], "[");
  216.         BuildFileIOPathname(fileio, &name[1]);
  217.         ConcatString(&name[0], "]");
  218.         AlertGrunt(&name[0], window);
  219.         }
  220.  
  221.     if (FlagIsSet(fileio->Flags, DISK_HAS_CHANGED))
  222.         {
  223.         ClearFlag(fileio->Flags, DISK_HAS_CHANGED);
  224.         return(TRUE);
  225.         }
  226.     else return(FALSE);
  227. }
  228.  
  229.