home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / util / startups.sit / Stupcr1.c < prev    next >
C/C++ Source or Header  |  1986-05-06  |  5KB  |  247 lines

  1. /* This is the help session compiler */
  2. /* (C) 1986 by Bill Woody, all rights reserved */
  3.  
  4. #include <control.h>
  5. #include <desk.h>
  6. #include <dialog.h>
  7. #include <event.h>
  8. #include <font.h>
  9. #include <fnctl.h>
  10. #include <menu.h>
  11. #include <mem.h>
  12. #include <qd.h>
  13. #include <qdvars.h>
  14. #include <stdio.h>
  15. #include <win.h>
  16. #include <pack.h>
  17. #include <file.h>
  18. #include "stupcr.h"
  19.  
  20.  
  21. /******************************************************************************/
  22. /* Globals */
  23.  
  24. menuhandle applmenuh,filemenuh,editmenuh;
  25. int quitflag;
  26. char *memch;
  27. extern int errno;
  28.  
  29.  
  30. /******************************************************************************/
  31. /* Code */
  32.  
  33.  
  34. initworld()
  35. {
  36.     extern int exittoshell();
  37.     long restype = 'DRVR';
  38.  
  39.     initgraf(&theport);            /* Initialize the quickdraw stuff */
  40.     initfonts();                /* Initialize my ability to draw text */
  41.     initwindows();                /* Now, initialize windows */
  42.     initmenus();                /* and menus */
  43.     teinit();                    /* and the text editor (for dialogs!) */
  44.     initdialogs(exittoshell);    /* When a bomb occures, drop me into finder! */
  45.     flushevents(everyevent,0);    /* Flush all pending events */
  46.     initcursor();                /* Give me a mouse cursor */
  47.     busyflag();
  48.  
  49.     applmenuh = getmenu(APPLMENU);
  50.     addresmenu(applmenuh,&restype);
  51.     insertmenu(applmenuh,0);
  52.     filemenuh = getmenu(FILEMENU);
  53.     insertmenu(filemenuh,0);
  54.     editmenuh = getmenu(EDITMENU);
  55.     insertmenu(editmenuh,0);
  56.     drawmenubar();
  57.  
  58.     readyflag();
  59.  
  60.     if ((memch = newptr(65536L)) == NULL) {
  61.         errmsg(OUTOFMEM,_memerr);                /* Error message */
  62.         quitflag = 1;
  63.     } else quitflag = 0;
  64. }
  65.  
  66. busyflag()            /* Call this when going into a heavy processing loop */
  67. {                    /* And you want the user to wait instead of enter input */
  68.      getcursor(4); 
  69. }
  70.  
  71. readyflag()            /* Call this when ready to accept user input */
  72. {
  73.     initcursor();
  74. }
  75.  
  76. int getfile(l)                /* Do the equivalent of an OPEN command on file */
  77. long l;                        /* with flags set for READ */
  78. {
  79.     point where;
  80.     sfreply reply;
  81.     paramblockrec pb;
  82.  
  83.     where.a.v = 150;
  84.     where.a.h = 75;
  85.     sfgetfile(&where,"Compile what file?",NULL,1,&l,NULL,&reply);
  86.     if (reply.good == 0) return(-1);
  87.  
  88.     pb.ionameptr = &reply.fname;
  89.     pb.iovrefnum = reply.vrefnum;
  90.     pb.paramunion.ioparam.ioversnum = reply.version;
  91.     pb.paramunion.ioparam.iopermssn = O_RDONLY & 4095;
  92.     pb.paramunion.ioparam.iomisc = NULL;
  93.     if (errno = pbopen(&pb,0)) return -1;
  94.     else {
  95.         _binadd(pb.paramunion.ioparam.iorefnum, O_BINARY);
  96.         return pb.paramunion.ioparam.iorefnum;
  97.     }
  98. }
  99.  
  100. int putfile(l,o,def)        /* Do the equivalent of an OPEN command on file */
  101. long l,o;                    /* with flags set for WRITE */
  102. char *def;
  103. {
  104.     point where;
  105.     sfreply reply;
  106.     paramblockrec pb;
  107.     int refnum;
  108.  
  109.     where.a.v = 175;
  110.     where.a.h = 80;
  111.     sfputfile(&where,"Save startup file as:",def,NULL,&reply);
  112.     if (reply.good == 0) return -2;
  113.  
  114.     create(&reply.fname,reply.vrefnum,&o,&l);
  115.  
  116.     pb.ionameptr = &reply.fname;
  117.     pb.iovrefnum = reply.vrefnum;
  118.     pb.paramunion.ioparam.ioversnum = 0;
  119.     pb.paramunion.ioparam.iopermssn = 2;
  120.     pb.paramunion.ioparam.iomisc = NULL;
  121.     pbopen(&pb,0);
  122.     refnum = pb.paramunion.ioparam.iorefnum;
  123.     if (errno = pb.ioresult) {
  124.         return -1;
  125.     }
  126.     else {
  127.         seteof(refnum,0L);
  128.         _binadd(refnum,O_BINARY);
  129.         return refnum;
  130.     }
  131. }
  132.  
  133. doeventloop()        /* Call this to execute the event loop once */
  134. {
  135.     eventrecord theevent;
  136.  
  137.     systemtask();                    /* Give time to the desk accessories */
  138.     if (getnextevent(everyevent,&theevent)) {
  139.         switch (theevent.what) {
  140.             case mousedown:            /* Well, he pressed the (mouse) button */
  141.                 domouseevent(&theevent);
  142.                 break;
  143.             case keydown:
  144.             case autokey:
  145.                 dokeyevent(&theevent);
  146.                 break;
  147.         }
  148.     }
  149. }
  150.  
  151. domouseevent(foo)
  152. eventrecord *foo;
  153. {
  154.     int type;
  155.     long dimens;
  156.     int part;
  157.     point pt;
  158.     windowptr whichwin;
  159.     controlhandle ctrl;
  160.  
  161.     if (0 == (type = findwindow(&(foo->where),&whichwin))) return;
  162.     if ((whichwin != NULL) && (whichwin != frontwindow())) 
  163.         selectwindow(whichwin);
  164.     else switch (type) {
  165.         case inmenubar:
  166.             domenu(menuselect(&(foo->where)));
  167.             break;
  168.         case insyswindow:
  169.             systemclick(foo,whichwin);
  170.             break;
  171.     }
  172. }
  173.  
  174. domenu(foo)
  175. long foo;
  176. {
  177.     int highword,loword,accnum;
  178.     char buffer[255];
  179.  
  180.     highword = foo >> 16;
  181.     loword = foo & 0x0FFFF;
  182.     switch (highword) {
  183.         case APPLMENU:
  184.             if (loword == ABOUTSUBM) {
  185.                 doaboutme();
  186.                 break;
  187.             } else {
  188.                 getitem(applmenuh,loword,buffer);        /* Get the deskacc */
  189.                 accnum = opendeskacc(buffer);            /* and open it */
  190.             }
  191.             break;
  192.         case FILEMENU:
  193.             switch (loword) {
  194.                 case QUITSUBM:
  195.                     endprogram();        /* QUIT option */
  196.                     break;
  197.                 case FTOSSUBM:
  198.                     docompile();        /* file -> startup */
  199.                     break;
  200.                 case STOFSUBM:
  201.                     dodecomp();            /* startup -> file */
  202.                     break;
  203.             }
  204.             break;
  205.         case EDITMENU:
  206.             if (!systemedit(loword - 1))                /* Is it a desk cmd? */
  207.                 sysbeep(10);                            /* Unimplemented edit */
  208.             break;
  209.     }
  210.     hilitemenu(0);        /* Unhighlite the entire menu bar */
  211. }
  212.  
  213. dokeyevent(foo)
  214. eventrecord *foo;
  215. {
  216.     char c;
  217.  
  218.     c = foo->message & 0x0FF;                /* Get key code */
  219.     if ((foo->modifiers & 0x0100) && (foo->what == keydown)) domenu(menukey(c));
  220. }
  221.  
  222. doaboutme()
  223. {
  224.     if (2 == alert(ABOUTMEID,NULL))
  225.         alert(MISCALRT,NULL);                    /* Post the about alert */
  226. }
  227.  
  228. endprogram()
  229. {
  230.     quitflag = 1;
  231. }
  232.  
  233. errmsg(i,j)
  234. int i,j;
  235. {
  236.     char buffer[256],buf2[256];
  237.     getindstring(buffer,ERRMSG,i);
  238.     numtostring((long)j,buf2);                /* Error code */
  239.     paramtext(buffer,buf2,NULL,NULL);
  240.     stopalert(ERRORALRT,NULL);
  241. }
  242.  
  243. main()
  244. {
  245.     initworld();
  246.     while (quitflag == 0) doeventloop();
  247. }