home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / c_earthi.sit < prev    next >
Encoding:
Text File  |  1988-06-20  |  9.9 KB  |  369 lines

  1. 18-Jun-88 14:28:03-MDT,10498;000000000000
  2. Return-Path: <u-lchoqu%sunset@cs.utah.edu>
  3. Received: from cs.utah.edu by SIMTEL20.ARPA with TCP; Sat, 18 Jun 88 14:27:47 MDT
  4. Received: by cs.utah.edu (5.54/utah-2.0-cs)
  5.     id AA22185; Sat, 18 Jun 88 14:27:49 MDT
  6. Received: by sunset.utah.edu (5.54/utah-2.0-leaf)
  7.     id AA24578; Sat, 18 Jun 88 14:27:46 MDT
  8. Date: Sat, 18 Jun 88 14:27:46 MDT
  9. From: u-lchoqu%sunset@cs.utah.edu (Lee Choquette)
  10. Message-Id: <8806182027.AA24578@sunset.utah.edu>
  11. To: rthum@simtel20.arpa
  12. Subject: Display.c
  13.  
  14. /* This sample program allows a user to open a file and print it's contents
  15. ** to a window on the Mac screen.  To give the compiled program an Icon
  16. ** copy the resources from Display.rsrc (use Rmover or Resource Editor) and
  17. ** paste them into the compiled file.  Then use SetFile to change the creator
  18. ** name of the compiled file to DFIL and set the bundle bit.  When you
  19. ** return to the finder the program icon should have an icon.
  20. ** By Greg Corson
  21. ** 19141 Summers Drive
  22. ** South Bend, IN 46637
  23. ** (219) 272-2136
  24. ** UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
  25. ** ARPA: pur-ee!kangaro!milo@Purdue.ARPA
  26. ** EDU: kangaro!milo@ee.Purdue.EDU
  27. ** Or call my BBS at (219) 277-5825
  28. */
  29.  
  30. #include <qd.h>
  31. #include <win.h>
  32. #include <menu.h>
  33. #include <event.h>
  34. #include <pack.h>
  35. #include <font.h>
  36.  
  37. #define LASTMENU 4
  38. #define APPLEMENU 1
  39. #define FILEMENU 256
  40. #define EDITMENU 257
  41. #define STOPMENU 258
  42. #define NULL 0L
  43. #define FALSE 0
  44. #define TRUE 1
  45. #define eoferr (-39)
  46.  
  47. rgnhandle    updateregn;
  48. menuhandle   mymenus[LASTMENU+1];
  49. rect         screenrect, dragrect, prect;
  50. boolean      doneflag, temp;
  51. eventrecord  myevent;
  52. int          code, refnum;
  53. windowrecord wrecord;
  54. windowptr    mywindow, whichwindow;
  55. grafptr      temport;
  56. int          themenu, theitem;
  57. int          fileopen,wide,fd1;
  58. long         count;
  59. char         tempbuf[32];
  60.  
  61. main()
  62. {
  63.    #include <qdvars.h>    /* quickdraw globals */
  64.    int i,j;
  65.  
  66. /* Initialize variables */
  67.  
  68.    j = 0;
  69.    doneflag = FALSE;
  70.    fileopen = FALSE;
  71.  
  72. /* Initialize quickdraw, fonts, events, windows, menus, dialogs and cursor */
  73.  
  74.    initgraf(&theport);
  75.    initfonts();
  76.    flushevents(everyevent, 0);
  77.    initwindows();
  78.    initmenus();
  79.    teinit();
  80.    initdialogs(NULL);
  81.    initcursor();
  82.  
  83. /* Create an empty region pointer for use by scrollrect later */
  84.  
  85.    updateregn=newrgn();
  86.  
  87. /* Setup the menu bar */
  88.  
  89.    setupmenus();
  90.  
  91. /* Setup the drag rectangle so part of the window will always be visible */
  92.  
  93.    setrect(&screenrect, 4, 40, 508, 338);
  94.    setrect(&dragrect, 4, 24, screenrect.a.right-4, screenrect.a.bottom-4);
  95.  
  96. /* Create the window and set the current port to the window port */
  97.  
  98.    mywindow = newwindow(&wrecord, &screenrect, "Display a file", TRUE, 0,
  99.              (long)-1, FALSE, (long)0);
  100.    setport(mywindow);
  101.  
  102. /* get the rectangle for the current window and put it in prect */
  103.  
  104.    blockmove(&theport->portrect, &prect, (long)sizeof prect);
  105.    wide = prect.a.right  - prect.a.left;
  106.  
  107. /* Now that the window and menus are drawn set the window font to monaco 9 */
  108.  
  109.    textfont(monaco);
  110.    textsize(9);
  111.    moveto(prect.a.left+1,prect.a.bottom-2);
  112.  
  113. /* Main loop to process events */
  114.  
  115.    do {
  116.  
  117. /**** If a file is open copy a line to the output window */
  118.  
  119.       if(fileopen)
  120.          {
  121.      count=32;
  122.      fsread(fd1, &count, tempbuf);
  123.      if(count == 0)
  124.         {
  125.         fsclose(fd1);
  126.         fileopen=FALSE;
  127.         moveto(prect.a.left+1,prect.a.bottom-2);
  128.         scrollrect(&prect,0,-11,updateregn);
  129.         drawstring("-------End of File-------");
  130.         scrollrect(&prect,0,-11,updateregn);
  131.         moveto(prect.a.left+1,prect.a.bottom-2);
  132.         }
  133.      else
  134.         {
  135.         for(i = 0; i < count; i++)
  136.            {
  137.            if(tempbuf[i] > 31)
  138.               drawchar(tempbuf[i]);
  139.            else
  140.               {
  141. /**************** Scroll window if we get a carriage return */
  142.                   if(tempbuf[i] == '\r')
  143.              {
  144.              j = 0;
  145.              scrollrect(&prect,0,-11,updateregn);
  146.              moveto(prect.a.left+1,prect.a.bottom-2);
  147.              }
  148. /**************** Expand tabs by outputting spaces */
  149.                   if(tempbuf[i] == '\011')
  150.              {
  151.              drawchar(' ');
  152.              j++;
  153.              for(;j & 07;j++)
  154.                 drawchar(' ');
  155.              }
  156.           }
  157.            }
  158.         }
  159.      }
  160.  
  161. /**** Get the next event */
  162.  
  163.       systemtask();
  164.       temp = getnextevent(everyevent, &myevent);
  165.       switch (myevent.what)
  166.          {
  167.      case mousedown:  /* mouse down, call findwindow to figure out where */
  168.         code = findwindow(&myevent.where, &whichwindow);
  169.         switch (code)
  170.            {
  171.            case inmenubar:    /* in meun bar, execute the menu command */
  172.               docommand(menuselect(&myevent.where));
  173.           break;
  174.                case insyswindow:    /* in desk accessory, call desk manager */
  175.               systemclick(&myevent, whichwindow);
  176.           break;
  177.            case indrag:    /* in drag, call dragwindow to move it */
  178.               dragwindow(whichwindow, &myevent.where, &dragrect);
  179.               break;
  180.            case incontent:    /* in content area, make application window the frontmost */
  181.            if (whichwindow != frontwindow())
  182.               selectwindow(whichwindow);
  183.            break;
  184.            }
  185.         break;
  186.      case keydown:    /* If keydown event, check for menu command key */
  187.         if(myevent.modifiers & cmdkey)
  188.            docommand(menukey((char)(myevent.message & 0377)));
  189.         break;
  190.      case autokey:
  191.         break;
  192.      case activateevt:    /* Application window becomming active, do nothing */
  193.         if((myevent.modifiers & 1)&&(((windowptr)myevent.message) == mywindow))
  194.            {
  195.            disableitem(mymenus[3],0);
  196.            enableitem(mymenus[2],0);
  197.            drawmenubar();
  198.            }
  199.         else
  200.            {
  201.            enableitem(mymenus[3],0);
  202.            disableitem(mymenus[2],0);
  203.            drawmenubar();
  204.            }
  205.         break;
  206.      case updateevt:    /* Update event, update the window frame */
  207.         if(((windowptr)myevent.message) == mywindow)
  208.            {
  209.            beginupdate(mywindow);
  210.            endupdate(mywindow);
  211.            }
  212.         break;
  213.     }
  214.     } while (doneflag == 0);
  215. }
  216.  
  217. /*---------------------------------------------------------------------------*/
  218. /* setupmenus()---This subroutine sets up the menu bar and reads in the desk
  219. ** accessory menu
  220. */
  221.  
  222. setupmenus()
  223. {
  224.    int i;
  225.  
  226. /* Apple menu, \024 is the apple character, adresmenu call loads all type DRVR resources */
  227.    mymenus[1] = newmenu(APPLEMENU, "\024");
  228.    appendmenu(mymenus[1], "About \"Display a File\";(-");
  229.    addresmenu(mymenus[1], "DRVR");
  230. /* File menu with open, close and quit selections */
  231.    mymenus[2] = newmenu(FILEMENU, "File");
  232.    appendmenu(mymenus[2], "Open/O;Close/C;Quit/Q");
  233. /* Edit menu with cut, copy and paste */
  234.    mymenus[3] = newmenu(EDITMENU, "Edit");
  235.    appendmenu(mymenus[3], "Undo;(-;Cut;Copy;Paste;Clear;(-;Show Clipboard");
  236. /* Stop scroll menu */
  237.    mymenus[4] = newmenu(STOPMENU,"Click Here to Pause Printout");
  238.    appendmenu(mymenus[4], "Release mouse button to resume printout");
  239.    for (i=1; i<=LASTMENU; i++)
  240.       insertmenu(mymenus[i], 0);
  241. /* Draw the completed menu bar */
  242.    drawmenubar();
  243. }
  244. /*---------------------------------------------------------------------------*/
  245. /* docommand(themenu, theitem)---this subroutine processes commands from the
  246. ** menu bar.  Themenu is the menu ID, theitem is the item number in the menu
  247. */
  248.  
  249. docommand(themenu, theitem)
  250. int themenu, theitem;
  251. {
  252.    char name[256];
  253.    point  openp;
  254.    sfreply rep;
  255.    sftypelist typelist;
  256.    int i;
  257.  
  258. /* Switch to decide what menu the cursor is in */
  259.  
  260.    switch (themenu)
  261.       {
  262.       case APPLEMENU:    /* Mouse down in apple menu */
  263. /******* Item one is the "about Display a file" box */
  264.          if(theitem == 1)
  265.         {
  266.         textfont(systemfont);
  267.         textsize(12);
  268.         eraserect(&prect);
  269.         moveto(prect.a.left,prect.a.top+70);
  270.         center("Display a file program");
  271.         center("Copyright 1985 by Greg Corson");
  272.         center("Kangaroo Koncepts, Inc.");
  273.         center("19141 Summers Drive");
  274.         center("South Bend, IN 46637");
  275.         center("(219) 277-5306");
  276.         textfont(monaco);
  277.             textsize(9);
  278.         move(0,-3);
  279.         center("Feel free to give this program away to all your friends.");
  280.         center("It should NOT be sold for profit.  Be sure to try our");
  281.         center("Computer Based Communications System \"The Connection\"");
  282.         center("Free demo line (219) 277-5825 available 24 hours at 300 or");
  283.         center("1200 baud.  Be sure to look at the \"MacTech\" special");
  284.         center("interest group for information of interest to Mac");
  285.         center("programmers and the \"macintosh\" SIG for general info.");
  286.         pretty();
  287.         moveto(prect.a.left+1,prect.a.bottom-2);
  288.         }
  289. /******* The rest of the items are desk accessorys */
  290.          else
  291.         {
  292.         getitem(mymenus[1], theitem, name);
  293.             refnum = opendeskacc(name);
  294.         setport(mywindow);
  295.         }
  296.      break;
  297.       case FILEMENU:    /* Mouse down in file menu */
  298.          switch(theitem)
  299.         {
  300.         case 1:        /* Open file */
  301.            openp.a.v = 100;
  302.            openp.a.h = 60;
  303.            strncpy(&typelist[0],"TEXT",4);
  304.            sfgetfile(&openp.a,"",NULL,1, typelist, NULL, &rep);
  305.            if(rep.good)
  306.               {
  307.           if(fileopen)
  308.                  {
  309.              fsclose(fd1);
  310.              fileopen = FALSE;
  311.              }
  312.           if(fsopen(rep.fname,rep.vrefnum,&fd1) == noerr)
  313.              {
  314.              scrollrect(&prect,0,-11,updateregn);
  315.              fileopen=TRUE;
  316.              }
  317.           }
  318.            break;
  319.         case 2:        /* Close file */
  320.            if(fileopen)
  321.               {
  322.           fsclose(fd1);
  323.           fileopen = FALSE;
  324.           }
  325.            break;
  326.         case 3:        /* Quit */
  327.            if(fileopen)
  328.               {
  329.           fsclose(fd1);
  330.           fileopen = FALSE;
  331.           }
  332.            doneflag = 1;
  333.            break;
  334.         }
  335.      break;
  336.       case EDITMENU:    /* Process system edit events */
  337.          systemedit(theitem-1);
  338.      break;
  339.    }
  340.    hilitemenu(0);
  341. }
  342. /* Center a string in the window */
  343. center(str)
  344. char   *str;
  345. {
  346.    move(((wide-stringwidth(str))/2), 0);
  347.    drawstring(str);
  348.    move(-(mywindow->pnloc.a.h),(mywindow->txsize)+2);
  349. }
  350. /* draw a pretty design */
  351. pretty()
  352. {
  353.    int j;
  354.    rect tmprec;
  355.  
  356.    blockmove(&prect, &tmprec, (long)sizeof prect);
  357.    for(j=0;j < 12;j++)
  358.       {
  359.       frameoval(&tmprec);
  360.       insetrect(&tmprec,6,0);
  361.       }
  362.    blockmove(&prect, &tmprec, (long)sizeof prect);
  363.    for(j=0;j < 9;j++)
  364.       {
  365.       frameoval(&tmprec);
  366.       insetrect(&tmprec,0,6);
  367.       }
  368. }
  369.