home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / AppMaker 2.0b3 / Demo AppMaker 2.0b3 / Examples / pre-built AMReminder / Procedural / FileM.c < prev    next >
Encoding:
Text File  |  1995-10-05  |  5.5 KB  |  256 lines  |  [TEXT/MMCC]

  1. tus == searching) {
  2.         if (i >= numOpenTypes) {
  3.             status = notFound;
  4.         } else {
  5.             if (fType == openTypeList [i]) {
  6.                 status = found;
  7.             } else {
  8.                 i++;
  9.             }
  10.         }
  11.     } /*while*/
  12.     return (status == found);
  13. } /*OkToOpen*/
  14.  
  15. /*----------*/
  16. static void DoNew (void)
  17. {
  18.     OpenWindows (nil, 0);
  19.     InitAppData ();
  20.  
  21. } /*DoNew*/
  22.  
  23. /*----------*/
  24. void OpenDoc    (FSSpec*    fileSpec)
  25. {
  26. /* This *should* be defined in an Apple interface file: */
  27. #define StationeryFlag    0x0800
  28.  
  29.     Boolean            isStationery;
  30.     FInfo            finderInfo;
  31.     short            fRefNum;
  32.  
  33.     isStationery = false;
  34.     if (FSpGetFInfo (fileSpec, &finderInfo) == noErr) {
  35.         if ((finderInfo.fdFlags & StationeryFlag) != 0) {
  36.             isStationery = true;
  37.         }
  38.     }
  39.     if (OpenAppFile (fileSpec, &fRefNum)) {
  40.         if (isStationery) {
  41.             OpenWindows (nil, 0);
  42.             ReadAppFile (fRefNum);
  43.             CloseAppFile (fRefNum);
  44.         } else {
  45.             OpenWindows (fileSpec, fRefNum);
  46.             ReadAppFile (fRefNum);
  47.         }
  48.     }
  49. } /*OpenDoc*/
  50.  
  51. /*----------*/
  52. static void DoOpen (void)
  53. {
  54.     StandardFileReply    sfInfo;
  55.  
  56.     StandardGetFile (nil, numOpenTypes, openTypeList, &sfInfo);
  57.     if (sfInfo.sfGood) {
  58.         OpenDoc (&sfInfo.sfFile);
  59.     }
  60. } /*DoOpen*/
  61.  
  62. /*----------*/
  63. void OpenApp (void)
  64. {
  65.     DoNew ();
  66. } /*OpenApp*/
  67.  
  68. /*----------*/
  69. static void DoSaveAs (void)
  70. {
  71.     FSSpec            fileSpec;
  72.     short            fRefNum;
  73.     StringHandle    prompt;
  74.     Str255            promptStr;
  75.     Str255            suggestion;
  76.  
  77.     prompt = GetString (STR_SaveAsPrompt);
  78.     if (prompt != nil) {
  79.         BlockMove (&(**prompt), promptStr, GetHandleSize ((Handle) prompt));
  80.     } else {
  81.         promptStr [0] = 0;
  82.     }
  83.     BlockMove (&(**cur->filename), suggestion, GetHandleSize ((Handle) cur->filename));
  84.  
  85.     if (CreateFile (&fileSpec, promptStr, suggestion, kSignature, kFileType)) {
  86.         CloseAppFile (cur->fileNum);
  87.         if (OpenAppFile (&fileSpec, &fRefNum)) {
  88.             SetWTitle (curWindow, fileSpec.name);
  89.             cur->fileNum = fRefNum;
  90.             SetString (cur->filename, fileSpec.name);
  91.             WriteAppFile (cur->fileNum);
  92.             cur->dirty = false;
  93.         } else { /*should never happen*/
  94.             SetWTitle (curWindow, "\p???");
  95.             cur->fileNum = 0;
  96.         }
  97.     }
  98. } /*DoSaveAs*/
  99.  
  100. /*----------*/
  101. static void DoSave (void)
  102. {
  103.     if (cur->fileNum == 0) {
  104.         DoSaveAs ();
  105.     } else {
  106.         WriteAppFile (cur->fileNum);
  107.         cur->dirty = false;
  108.     }
  109. } /*DoSave*/
  110.  
  111. /*----------*/
  112. static void CloseAppWindow (void)
  113. {
  114.     enum {saveItem = 1, cancelItem, discardItem};
  115.  
  116.     Str255            curTitle;
  117.     short            itemNum;
  118.     Boolean            okay;
  119.  
  120.     okay = true;
  121.     SetInfo (FrontWindow ());
  122.     if (cur->dirty) {
  123.         GetWTitle (curWindow, curTitle);
  124.         ParamText (curTitle, "\p", "\p", "\p");
  125.         InitCursor ();
  126.         itemNum = Alert (SaveID, nil);
  127.         switch (itemNum) {
  128.             case saveItem:
  129.                     DoSave ();
  130.                     okay = !errorFlag;
  131.                 break;
  132.             case discardItem:
  133.                    /*Do nothing*/;
  134.                 break;
  135.             case cancelItem:
  136.                     errorFlag = true;
  137.                     okay = false;
  138.                 break;
  139.         } /*switch*/
  140.     }
  141.     if (okay) {
  142.         DisposeAppData ();
  143.         if (cur->windowKind == 1) {        /* 1st or only window in set */
  144.             CloseAppFile (cur->fileNum);
  145.         }
  146.         CloseCurWindow ();
  147.     }
  148. } /*CloseAppWindow*/
  149.  
  150. /*----------*/
  151. void DoClose (void)
  152. {
  153.     WindowPeek        frontPeek;
  154.  
  155.     errorFlag = false;
  156.  
  157.     frontPeek = (WindowPeek) FrontWindow ();
  158.     if (frontPeek->windowKind < 0) {
  159.         CloseDeskAcc (frontPeek->windowKind);
  160.     } else if (frontPeek->windowKind == dialogKind) {
  161.         CloseModelessDialog (FrontWindow ());
  162.     } else {
  163.         CloseAppWindow ();
  164.     }
  165. } /*DoClose*/
  166.  
  167. /*----------*/
  168. void DoQuit (void)
  169. {
  170.     Boolean            quitting;
  171.  
  172.     quitting = true;
  173.     while (quitting && (FrontWindow () != nil)) {
  174.         SystemTask ();
  175.         DoClose ();
  176.         if (errorFlag) {
  177.             quitting = false;
  178.         }
  179.     } /*while*/
  180.  
  181.     if (quitting) {
  182.         quittingTime = true;
  183.     }
  184. } /*DoQuit*/
  185.  
  186. /*----------*/
  187. static void DoRevert ()
  188. {
  189.     Str255            fileName;
  190.     Boolean            okay;
  191.  
  192.     okay = true;
  193.     if (cur->dirty) {
  194.         GetWTitle (curWindow, fileName);
  195.         ParamText (fileName, "\p", "\p", "\p");
  196.         okay = Confirm (RevertID);
  197.     }
  198.     if (okay) {
  199.         cur->dirty = false;
  200.         DisposeAppData ();
  201.         if (cur->fileNum != 0) {
  202.             ReadAppFile (cur->fileNum);
  203.         }
  204.         InvalRect (&curWindow->portRect);
  205.     }
  206. } /*DoRevert*/
  207.  
  208. /*----------*/
  209. static void DoPageSetup (void)
  210. {
  211. } /*DoPageSetup*/
  212.  
  213. /*----------*/
  214. static void DoPrint (void)
  215. {
  216. } /*DoPrint*/
  217.  
  218. /*----------*/
  219. void DoFile    (short        itemNr)
  220. {
  221.     errorFlag = false;
  222.  
  223.     switch (itemNr) {
  224.         case FileNew:
  225.                 DoNew ();
  226.             break;
  227.         case FileOpen:
  228.                 DoOpen ();
  229.             break;
  230.         case FileClose:
  231.                 DoClose ();
  232.             break;
  233.         case FileSave:
  234.                 DoSave ();
  235.             break;
  236.         case FileSaveAs:
  237.                 DoSaveAs ();
  238.             break;
  239.         case FileRevert:
  240.                 DoRevert ();
  241.             break;
  242.         case FilePageSetup:
  243.                 DoPageSetup ();
  244.             break;
  245.         case FilePrint:
  246.                 DoPrint ();
  247.             break;
  248.         case FileQuit:
  249.                 DoQuit ();
  250.             break;
  251.  
  252.     } /*switch*/
  253. } /*DoFile*/
  254.  
  255. /* File */
  256.