home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Demos / Bowers Development / AppMaker 2.0b5 / Examples / Procedural / Gadgets / FileM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-19  |  5.5 KB  |  309 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 "GadgetsData.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 (nil, 0);
  72.     InitAppData ();
  73.  
  74. } /*DoNew*/
  75.  
  76. /*----------*/
  77. void OpenDoc    (FSSpec*    fileSpec)
  78. {
  79. /* This *should* be defined in an Apple interface file: */
  80. #define StationeryFlag    0x0800
  81.  
  82.     Boolean            isStationery;
  83.     FInfo            finderInfo;
  84.     short            fRefNum;
  85.  
  86.     isStationery = false;
  87.     if (FSpGetFInfo (fileSpec, &finderInfo) == noErr) {
  88.         if ((finderInfo.fdFlags & StationeryFlag) != 0) {
  89.             isStationery = true;
  90.         }
  91.     }
  92.     if (OpenAppFile (fileSpec, &fRefNum)) {
  93.         if (isStationery) {
  94.             OpenWindows (nil, 0);
  95.             ReadAppFile (fRefNum);
  96.             CloseAppFile (fRefNum);
  97.         } else {
  98.             OpenWindows (fileSpec, fRefNum);
  99.             ReadAppFile (fRefNum);
  100.         }
  101.     }
  102. } /*OpenDoc*/
  103.  
  104. /*----------*/
  105. static void DoOpen (void)
  106. {
  107.     StandardFileReply    sfInfo;
  108.  
  109.     StandardGetFile (nil, numOpenTypes, openTypeList, &sfInfo);
  110.     if (sfInfo.sfGood) {
  111.         OpenDoc (&sfInfo.sfFile);
  112.     }
  113. } /*DoOpen*/
  114.  
  115. /*----------*/
  116. void OpenApp (void)
  117. {
  118.     DoNew ();
  119. } /*OpenApp*/
  120.  
  121. /*----------*/
  122. static void DoSaveAs (void)
  123. {
  124.     FSSpec            fileSpec;
  125.     short            fRefNum;
  126.     StringHandle    prompt;
  127.     Str255            promptStr;
  128.     Str255            suggestion;
  129.  
  130.     prompt = GetString (STR_SaveAsPrompt);
  131.     if (prompt != nil) {
  132.         BlockMove (&(**prompt), promptStr, GetHandleSize ((Handle) prompt));
  133.     } else {
  134.         promptStr [0] = 0;
  135.     }
  136.     BlockMove (&(**cur->filename), suggestion, GetHandleSize ((Handle) cur->filename));
  137.  
  138.     if (CreateFile (&fileSpec, promptStr, suggestion, kSignature, kFileType)) {
  139.         CloseAppFile (cur->fileNum);
  140.         if (OpenAppFile (&fileSpec, &fRefNum)) {
  141.             SetWTitle (curWindow, fileSpec.name);
  142.             cur->fileNum = fRefNum;
  143.             SetString (cur->filename, fileSpec.name);
  144.             WriteAppFile (cur->fileNum);
  145.             cur->dirty = false;
  146.         } else { /*should never happen*/
  147.             SetWTitle (curWindow, "\p???");
  148.             cur->fileNum = 0;
  149.         }
  150.     }
  151. } /*DoSaveAs*/
  152.  
  153. /*----------*/
  154. static void DoSave (void)
  155. {
  156.     if (cur->fileNum == 0) {
  157.         DoSaveAs ();
  158.     } else {
  159.         WriteAppFile (cur->fileNum);
  160.         cur->dirty = false;
  161.     }
  162. } /*DoSave*/
  163.  
  164. /*----------*/
  165. static void CloseAppWindow (void)
  166. {
  167.     enum {saveItem = 1, cancelItem, discardItem};
  168.  
  169.     Str255            curTitle;
  170.     short            itemNum;
  171.     Boolean            okay;
  172.  
  173.     okay = true;
  174.     SetInfo (FrontWindow ());
  175.     if (cur->dirty) {
  176.         GetWTitle (curWindow, curTitle);
  177.         ParamText (curTitle, "\p", "\p", "\p");
  178.         InitCursor ();
  179.         itemNum = Alert (SaveID, nil);
  180.         switch (itemNum) {
  181.             case saveItem:
  182.                     DoSave ();
  183.                     okay = !errorFlag;
  184.                 break;
  185.             case discardItem:
  186.                    /*Do nothing*/;
  187.                 break;
  188.             case cancelItem:
  189.                     errorFlag = true;
  190.                     okay = false;
  191.                 break;
  192.         } /*switch*/
  193.     }
  194.     if (okay) {
  195.         DisposeAppData ();
  196.         if (cur->windowKind == 1) {        /* 1st or only window in set */
  197.             CloseAppFile (cur->fileNum);
  198.         }
  199.         CloseCurWindow ();
  200.     }
  201. } /*CloseAppWindow*/
  202.  
  203. /*----------*/
  204. void DoClose (void)
  205. {
  206.     WindowPeek        frontPeek;
  207.  
  208.     errorFlag = false;
  209.  
  210.     frontPeek = (WindowPeek) FrontWindow ();
  211.     if (frontPeek->windowKind < 0) {
  212.         CloseDeskAcc (frontPeek->windowKind);
  213.     } else if (frontPeek->windowKind == dialogKind) {
  214.         CloseModelessDialog (FrontWindow ());
  215.     } else {
  216.         CloseAppWindow ();
  217.     }
  218. } /*DoClose*/
  219.  
  220. /*----------*/
  221. void DoQuit (void)
  222. {
  223.     Boolean            quitting;
  224.  
  225.     quitting = true;
  226.     while (quitting && (FrontWindow () != nil)) {
  227.         SystemTask ();
  228.         DoClose ();
  229.         if (errorFlag) {
  230.             quitting = false;
  231.         }
  232.     } /*while*/
  233.  
  234.     if (quitting) {
  235.         quittingTime = true;
  236.     }
  237. } /*DoQuit*/
  238.  
  239. /*----------*/
  240. static void DoRevert ()
  241. {
  242.     Str255            fileName;
  243.     Boolean            okay;
  244.  
  245.     okay = true;
  246.     if (cur->dirty) {
  247.         GetWTitle (curWindow, fileName);
  248.         ParamText (fileName, "\p", "\p", "\p");
  249.         okay = Confirm (RevertID);
  250.     }
  251.     if (okay) {
  252.         cur->dirty = false;
  253.         DisposeAppData ();
  254.         if (cur->fileNum != 0) {
  255.             ReadAppFile (cur->fileNum);
  256.         }
  257.         InvalRect (&curWindow->portRect);
  258.     }
  259. } /*DoRevert*/
  260.  
  261. /*----------*/
  262. static void DoPageSetup (void)
  263. {
  264. } /*DoPageSetup*/
  265.  
  266. /*----------*/
  267. static void DoPrint (void)
  268. {
  269. } /*DoPrint*/
  270.  
  271. /*----------*/
  272. void DoFile    (short        itemNr)
  273. {
  274.     errorFlag = false;
  275.  
  276.     switch (itemNr) {
  277.         case FileNew:
  278.                 DoNew ();
  279.             break;
  280.         case FileOpen:
  281.                 DoOpen ();
  282.             break;
  283.         case FileClose:
  284.                 DoClose ();
  285.             break;
  286.         case FileSave:
  287.                 DoSave ();
  288.             break;
  289.         case FileSaveAs:
  290.                 DoSaveAs ();
  291.             break;
  292.         case FileRevert:
  293.                 DoRevert ();
  294.             break;
  295.         case FilePageSetup:
  296.                 DoPageSetup ();
  297.             break;
  298.         case FilePrint:
  299.                 DoPrint ();
  300.             break;
  301.         case FileQuit:
  302.                 DoQuit ();
  303.             break;
  304.  
  305.     } /*switch*/
  306. } /*DoFile*/
  307.  
  308. /* File */
  309.