home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Demos / Tools / AppMaker / Examples / pre-built AMReminder / Procedural / FileM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-04  |  5.7 KB  |  314 lines  |  [TEXT/MMCC]

  1. /* FileM.c */
  2. /* Created 01/01/95 12:01 PM by AppMaker */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Dialogs.h>
  8. #include <Events.h>
  9. #include <Lists.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include <Desk.h>
  13. #include <Packages.h>
  14. #include <ToolUtils.h>
  15. #include "ResourceDefs.h"
  16. #include "Dispatcher.h"
  17. #include "Miscellany.h"
  18. #include "Globals.h"
  19. #include "AMReminderData.h"
  20. #include "FileM.h"
  21.  
  22. #define dialogTop        75
  23. #define dialogLeft        85
  24.  
  25.  
  26. short            numOpenTypes;
  27. SFTypeList        openTypeList;
  28.  
  29. static void        CloseAppWindow    (void);
  30. static void        DoNew (void);
  31. static void        DoOpen (void);
  32. static void        DoSave (void);
  33. static void        DoSaveAs (void);
  34. static void        DoRevert (void);
  35. static void        DoPageSetup (void);
  36. static void        DoPrint (void);
  37.  
  38. /*----------*/
  39. void InitFileM (void)
  40. {
  41.     numOpenTypes = 1;
  42.     openTypeList [0] = kFileType;
  43. } /*InitFileM*/
  44.  
  45. /*----------*/
  46. Boolean OkToOpen (OSType    fType)
  47. {
  48.     short            i;
  49.     enum {searching, found, notFound}
  50.                     status;
  51.  
  52.     i = 0;
  53.     status = searching;
  54.     while (status == searching) {
  55.         if (i >= numOpenTypes) {
  56.             status = notFound;
  57.         } else {
  58.             if (fType == openTypeList [i]) {
  59.                 status = found;
  60.             } else {
  61.                 i++;
  62.             }
  63.         }
  64.     } /*while*/
  65.     return (status == found);
  66. } /*OkToOpen*/
  67.  
  68. /*----------*/
  69. static void DoNew (void)
  70. {
  71.     OpenWindows ("\p", 0, 0);
  72.     InitAppData ();
  73.  
  74. } /*DoNew*/
  75.  
  76. /*----------*/
  77. void OpenDoc    (Str255        fileName,
  78.                  short        vRefNum)
  79. {
  80. /* This *should* be defined in an Apple interface file: */
  81. #define StationeryFlag    0x0800
  82.  
  83.     Boolean            isStationery;
  84.     FInfo            finderInfo;
  85.     short            fRefNum;
  86.  
  87.     isStationery = false;
  88.     if (GetFInfo (fileName, vRefNum, &finderInfo) == noErr) {
  89.         if ((finderInfo.fdFlags & StationeryFlag) != 0) {
  90.             isStationery = true;
  91.         }
  92.     }
  93.     if (OpenAppFile (vRefNum, fileName, &fRefNum)) {
  94.         if (isStationery) {
  95.             OpenWindows ("\p", 0, 0);
  96.             ReadAppFile (fRefNum);
  97.             CloseAppFile (fRefNum);
  98.         } else {
  99.             OpenWindows (fileName, vRefNum, fRefNum);
  100.             ReadAppFile (fRefNum);
  101.         }
  102.     }
  103. } /*OpenDoc*/
  104.  
  105. /*----------*/
  106. static void DoOpen (void)
  107. {
  108.     Point            dialogOrigin;
  109.     SFReply            sfInfo;
  110.  
  111.     SetPt (&dialogOrigin, dialogLeft, dialogTop);
  112.     SFGetFile (dialogOrigin, "\p", nil, numOpenTypes, openTypeList, nil, &sfInfo);
  113.     if (sfInfo.good) {
  114.         OpenDoc (sfInfo.fName, sfInfo.vRefNum);
  115.     }
  116. } /*DoOpen*/
  117.  
  118. /*----------*/
  119. void OpenApp (void)
  120. {
  121.     DoNew ();
  122. } /*OpenApp*/
  123.  
  124. /*----------*/
  125. static void DoSaveAs (void)
  126. {
  127.     SFReply            sfInfo;
  128.     short            fRefNum;
  129.     StringHandle    prompt;
  130.     Str255            promptStr;
  131.     Str255            suggestion;
  132.  
  133.     prompt = GetString (STR_SaveAsPrompt);
  134.     if (prompt != nil) {
  135.         BlockMove (&(**prompt), promptStr, GetHandleSize ((Handle) prompt));
  136.     } else {
  137.         promptStr [0] = 0;
  138.     }
  139.     BlockMove (&(**cur->filename), suggestion, GetHandleSize ((Handle) cur->filename));
  140.  
  141.     if (CreateFile (&sfInfo, promptStr, suggestion, kSignature, kFileType)) {
  142.         CloseAppFile (cur->fileNum);
  143.         if (OpenAppFile (sfInfo.vRefNum, sfInfo.fName, &fRefNum)) {
  144.             SetWTitle (curWindow, sfInfo.fName);
  145.             cur->fileNum = fRefNum;
  146.             cur->volNum = sfInfo.vRefNum;
  147.             SetString (cur->filename, sfInfo.fName);
  148.             WriteAppFile (cur->fileNum);
  149.             cur->dirty = false;
  150.         } else { /*should never happen*/
  151.             SetWTitle (curWindow, "\p???");
  152.             cur->fileNum = 0;
  153.             cur->volNum = 0;
  154.         }
  155.     }
  156. } /*DoSaveAs*/
  157.  
  158. /*----------*/
  159. static void DoSave (void)
  160. {
  161.     if (cur->fileNum == 0) {
  162.         DoSaveAs ();
  163.     } else {
  164.         WriteAppFile (cur->fileNum);
  165.         cur->dirty = false;
  166.     }
  167. } /*DoSave*/
  168.  
  169. /*----------*/
  170. static void CloseAppWindow (void)
  171. {
  172.     enum {saveItem = 1, cancelItem, discardItem};
  173.  
  174.     Str255            curTitle;
  175.     short            itemNum;
  176.     Boolean            okay;
  177.  
  178.     okay = true;
  179.     SetInfo (FrontWindow ());
  180.     if (cur->dirty) {
  181.         GetWTitle (curWindow, curTitle);
  182.         ParamText (curTitle, "\p", "\p", "\p");
  183.         InitCursor ();
  184.         itemNum = Alert (SaveID, nil);
  185.         switch (itemNum) {
  186.             case saveItem:
  187.                     DoSave ();
  188.                     okay = !errorFlag;
  189.                 break;
  190.             case discardItem:
  191.                    /*Do nothing*/;
  192.                 break;
  193.             case cancelItem:
  194.                     errorFlag = true;
  195.                     okay = false;
  196.                 break;
  197.         } /*switch*/
  198.     }
  199.     if (okay) {
  200.         DisposeAppData ();
  201.         if (cur->windowKind == 1) {        /* 1st or only window in set */
  202.             CloseAppFile (cur->fileNum);
  203.         }
  204.         CloseCurWindow ();
  205.     }
  206. } /*CloseAppWindow*/
  207.  
  208. /*----------*/
  209. void DoClose (void)
  210. {
  211.     WindowPeek        frontPeek;
  212.  
  213.     errorFlag = false;
  214.  
  215.     frontPeek = (WindowPeek) FrontWindow ();
  216.     if (frontPeek->windowKind < 0) {
  217.         CloseDeskAcc (frontPeek->windowKind);
  218.     } else if (frontPeek->windowKind == dialogKind) {
  219.         CloseModelessDialog (FrontWindow ());
  220.     } else {
  221.         CloseAppWindow ();
  222.     }
  223. } /*DoClose*/
  224.  
  225. /*----------*/
  226. void DoQuit (void)
  227. {
  228.     Boolean            quitting;
  229.  
  230.     quitting = true;
  231.     while (quitting && (FrontWindow () != nil)) {
  232.         SystemTask ();
  233.         DoClose ();
  234.         if (errorFlag) {
  235.             quitting = false;
  236.         }
  237.     } /*while*/
  238.  
  239.     if (quitting) {
  240.         quittingTime = true;
  241.     }
  242. } /*DoQuit*/
  243.  
  244. /*----------*/
  245. static void DoRevert ()
  246. {
  247.     Str255            fileName;
  248.     Boolean            okay;
  249.  
  250.     okay = true;
  251.     if (cur->dirty) {
  252.         GetWTitle (curWindow, fileName);
  253.         ParamText (fileName, "\p", "\p", "\p");
  254.         okay = Confirm (RevertID);
  255.     }
  256.     if (okay) {
  257.         cur->dirty = false;
  258.         DisposeAppData ();
  259.         if (cur->fileNum != 0) {
  260.             ReadAppFile (cur->fileNum);
  261.         }
  262.         InvalRect (&curWindow->portRect);
  263.     }
  264. } /*DoRevert*/
  265.  
  266. /*----------*/
  267. static void DoPageSetup (void)
  268. {
  269. } /*DoPageSetup*/
  270.  
  271. /*----------*/
  272. static void DoPrint (void)
  273. {
  274. } /*DoPrint*/
  275.  
  276. /*----------*/
  277. void DoFile    (short        itemNr)
  278. {
  279.     errorFlag = false;
  280.  
  281.     switch (itemNr) {
  282.         case FileNew:
  283.                 DoNew ();
  284.             break;
  285.         case FileOpen:
  286.                 DoOpen ();
  287.             break;
  288.         case FileClose:
  289.                 DoClose ();
  290.             break;
  291.         case FileSave:
  292.                 DoSave ();
  293.             break;
  294.         case FileSaveAs:
  295.                 DoSaveAs ();
  296.             break;
  297.         case FileRevert:
  298.                 DoRevert ();
  299.             break;
  300.         case FilePageSetup:
  301.                 DoPageSetup ();
  302.             break;
  303.         case FilePrint:
  304.                 DoPrint ();
  305.             break;
  306.         case FileQuit:
  307.                 DoQuit ();
  308.             break;
  309.  
  310.     } /*switch*/
  311. } /*DoFile*/
  312.  
  313. /* File */
  314.