home *** CD-ROM | disk | FTP | other *** search
/ Carousel / CAROUSEL.cdr / mactosh / lang / skel_azt.sha / filemenu.c < prev    next >
C/C++ Source or Header  |  1986-07-08  |  4KB  |  143 lines

  1.  
  2. /*
  3.  *    filemenu.c -- handle selection of an item in the file menu
  4.  */
  5.  
  6. #include <quickdraw.h>
  7. #include <memory.h>
  8. #include <menu.h>
  9. #include <packages.h>
  10. #include <syserr.h>
  11. #include <textedit.h>
  12. #include <window.h>
  13.  
  14. #include "def.h"
  15.  
  16. int
  17. filemenu(item)
  18. {
  19.     OSErr                    e;
  20.     extern    TEHandle        hTE;
  21.     long                    len;
  22.     extern    MenuHandle        mh[NMENUS + 1];
  23.     extern    int                paint;            /* file refnum */
  24.     extern    Point            sfdtl;        /* Standard File dialog window
  25.                                          * top left corner
  26.                                          */
  27.     SFReply                    sfr;
  28.     Handle                    strrsrc;
  29.     extern    int                tf;            /* text file reference number */
  30.     char                    tfname[64];
  31.     extern    WindowPtr        wp[NWINDOWS];
  32.     
  33.     switch(item) {
  34.         case 1:        /* New Text */
  35.             txinit();
  36.             break;
  37.         case 2:        /* Open Text */
  38.             len = 'TEXT';
  39.             SFGetFile(pass(sfdtl), NIL, NIL, 1, &len, NIL, &sfr);
  40.             *((long *)0) = -1L;    /* restore byte zero Apple just
  41.                                  * clobbered!
  42.                                  */
  43.             if (sfr.good)
  44.                 if (e = FSOpen(sfr.fName, sfr.vRefNum, &tf))
  45.                     errmsg(3, e);    /* File Open Error */
  46.                 else {
  47.                     txinit();
  48.                     if (hTE && !GetEOF(tf, &len)  &&
  49.                         (strrsrc = NewHandle(len)) != NIL) {
  50.                             DisposHandle((*hTE)->hText);
  51.                             (*hTE)->teLength = len;
  52.                             FSRead(tf, &len, *strrsrc);
  53.                             FSClose(tf);
  54.                             if (e)
  55.                                 errmsg(4, e);    /* File Read Error */
  56.                             if (len != (*hTE)->teLength)
  57.                                 errmsg(5, e);    /* Incorrect length read:
  58.                                                  */
  59.                             (*hTE)->hText = strrsrc;
  60.                             TECalText(hTE);
  61.                             crsm();    /* convert end-of-line to CR */
  62.                             TECalText(hTE);
  63.                             SetWTitle(wp[1], sfr.fName);
  64.                             SetWRefCon(wp[1], (long)sfr.vRefNum);
  65.                             teadjdest();
  66.                     };
  67.                 };
  68.             break;
  69.         case 4:            /* Save As ... */
  70.             GetWTitle(wp[1], tfname);
  71.             SFPutFile(pass(sfdtl), "\Ptext file", tfname, NIL, &sfr);
  72.             *((long *)0) = -1L;    /* restore byte zero Apple just
  73.                                  * clobbered!
  74.                                  */
  75.             if (sfr.good) {
  76.                 SetWTitle(wp[1], sfr.fName);
  77.                 SetWRefCon(wp[1], (long)sfr.vRefNum);
  78.             }
  79.             else
  80.                 break;
  81.             /* note a successful "Save As" continues into "Save" */
  82.         case 3:            /* Save Text */
  83.             GetWTitle(wp[1], tfname);
  84.             if (e = FSOpen(tfname, (short)GetWRefCon(wp[1]), &tf)) {
  85.                 if (e != fnfErr) {
  86.                     errmsg(3, e);    /* File Open Error */
  87.                     break;
  88.                 };
  89.                 if (e = Create(tfname, (short)GetWRefCon(wp[1]),
  90.                     'SKEL', 'TEXT')) {
  91.                         errmsg(6, e);    /* File Create Error */
  92.                         break;
  93.                 };
  94.                 if (e = FSOpen(tfname, (short)GetWRefCon(wp[1]), &tf)) {
  95.                     errmsg(3, e);    /* File Open Error */
  96.                     break;
  97.                 };
  98.             };
  99.             len = (*hTE)->teLength;
  100.             if (e = FSWrite(tf, &len, *(*hTE)->hText))
  101.                 errmsg(7, e);    /* File Write Error */
  102.             if (len != (*hTE)->teLength)
  103.                 errmsg(8, len);    /* Incorrect length written: */
  104.             FSClose(tf);
  105.             break;
  106.         case 5:            /* Open Paint */
  107.             len = 'PNTG';
  108.             SFGetFile(pass(sfdtl), NIL, NIL, 1, &len, NIL, &sfr);
  109.             *((long *)0) = -1L;    /* restore byte zero Apple just
  110.                                  * clobbered!
  111.                                  */
  112.             if (sfr.good)
  113.                 if (e = FSOpen(sfr.fName, sfr.vRefNum, &paint)) {
  114.                     errmsg(3, e);    /* File Open Error */
  115.                     paint = 0;
  116.                 }
  117.                 else {
  118.                     InvalRect(&wp[0]->portRect);
  119.                     if (wp[0] != FrontWindow())
  120.                         SelectWindow(wp[0]);
  121.                     DisableItem(mh[2], 5);
  122.                     EnableItem(mh[2], 7);
  123.                 };
  124.             break;
  125.         case 6:            /* Close Text */
  126.             txfin();
  127.             break;
  128.         case 7:            /* Close Paint */
  129.             FSClose(paint);
  130.             paint = 0;
  131.             InvalRect(&wp[0]->portRect);
  132.             if (wp[0] != FrontWindow())
  133.                 SelectWindow(wp[0]);
  134.             EnableItem(mh[2], 5);
  135.             DisableItem(mh[2], 7);
  136.             break;
  137.         case 9:            /* Quit */
  138.             return(TRUE);
  139.             break;
  140.     };
  141.     return(FALSE);
  142. } /* end of filemenu */
  143.