home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / xlisp / XLisp 1.7 ƒ / xlisp sources / macstuff.c < prev    next >
Encoding:
Text File  |  1985-12-21  |  9.6 KB  |  518 lines  |  [TEXT/EDIT]

  1. /* macstuff.c - macintosh interface routines for xlisp */
  2.  
  3. overlay "macstuff"
  4.  
  5. #include <qd.h>  
  6. #include <mem.h>
  7. #include <file.h>
  8. #include <font.h>
  9. #include <win.h>
  10. #include <menu.h>
  11. #include <dialog.h>
  12. #include <event.h>
  13. #include <pack.h>
  14. #include <stdio.h>
  15.  
  16. #define TRUE    1
  17. #define FALSE    0
  18.  
  19. #define appleid        1
  20. #define applemenu     0
  21. #define fileid        256
  22. #define filemenu    1
  23. #define editid        257
  24. #define editmenu    2
  25. #define controlid    258
  26. #define controlmenu     3
  27. #define lastmenu    4
  28.  
  29. extern char *s_unbound;
  30.  
  31. #include <qdvars.h>    /* quickdraw globals */
  32. MenuHandle mymenus[lastmenu];
  33. SFReply loadfile;
  34. OsType filetypes[] = { {'T','E','X','T'} };
  35. Rect dragrect;
  36. int x,y;
  37.  
  38. /* command window */
  39. WindowRecord cwrecord;
  40. WindowPtr cwindow;
  41.  
  42. /* graphics window */
  43. WindowRecord gwrecord;
  44. WindowPtr gwindow;
  45.  
  46. /* window mode */
  47. int splitmode;
  48.  
  49. #define TIMEON    500
  50. #define TIMEOFF    100
  51. int cursortime,cursorstate;
  52.  
  53. /* graphics window */
  54. #define GHORIZONTAL    4
  55. #define GVERTICAL    22
  56. #define GWIDTH        504
  57. #define GHEIGHT        232
  58.  
  59. /* split screen */
  60. #define SSCRW        80
  61. #define SSCRH        5
  62. #define SHORIZONTAL    4
  63. #define SVERTICAL    275
  64. #define SWIDTH        504
  65. #define SHEIGHT        64
  66.  
  67. /* normal screen */
  68. #define NSCRW        80
  69. #define NSCRH        24
  70. #define NHORIZONTAL    4
  71. #define NVERTICAL    41
  72. #define NWIDTH        504
  73. #define NHEIGHT        298
  74.  
  75. /* screen buffer */
  76. #define SCRH    24
  77. #define SCRW    80
  78. char screen[SCRH*SCRW],*topline,*curline;
  79. int scrh,scrw;
  80.  
  81. #define LINEMAX 200
  82. char linebuf[LINEMAX+1],*lineptr;
  83. int linepos[LINEMAX],linelen;
  84.  
  85. #define CHARMAX 100
  86. char charbuf[CHARMAX],*inptr,*outptr;
  87. int charcnt;
  88.  
  89. osinit(name)
  90.   char *name;
  91. {
  92.     Rect screenrect;
  93.     
  94.     /* initialize the toolbox */
  95.     InitGraf(&thePort);
  96.     InitFonts();
  97.     InitWindows();
  98.     InitMenus();
  99.     TEInit();
  100.     InitDialogs(0L);
  101.     InitCursor();
  102.  
  103.     /* setup the menu bar */
  104.     setupmenus();
  105.  
  106.     /* setup the bounds rectangle for dragging windows */
  107.     SetRect(&dragrect,4,24,508,338);
  108.  
  109.     /* Create the graphics and control windows */
  110.     gwindow = GetNewWindow(129,&gwrecord,-1L);
  111.     cwindow = GetNewWindow(128,&cwrecord,-1L);
  112.  
  113.     /* establish the command window as the current port */
  114.     SetPort(cwindow);
  115.  
  116.     /* setup the font, size and writing mode for the command window */
  117.     TextFont(Monaco); TextSize(9); TextMode(srcCopy);
  118.  
  119.     /* setup command mode */
  120.     splitmode = FALSE;
  121.     scrh = SCRH;
  122.     scrw = SCRW;
  123.     scrclear();
  124.  
  125.     /* disable the Cursor */
  126.     cursorstate = -1;
  127.  
  128.     /* setup the input ring buffer */
  129.     inptr = outptr = charbuf;
  130.     charcnt = 0;
  131.     
  132.     /* setup the Line editor */
  133.     linelen = 0;
  134. }
  135.  
  136. int osrand(n)
  137.   int n;
  138. {
  139.     return (rand() % n);
  140. }
  141.  
  142. int osgetc(fp)
  143.   FILE *fp;
  144. {
  145.     if (fp == stdin) return (linegetc());
  146.     else return (getc(fp));
  147. }
  148.  
  149. int osputc(ch,fp)
  150.   int ch; FILE *fp;
  151. {
  152.     oscheck();
  153.     if (fp == stdout) return (lineputc(ch));
  154.     else return (putc(ch,fp));
  155. }
  156.  
  157. setupmenus()
  158. {
  159.     int i;
  160.  
  161.     mymenus[applemenu]   = GetMenu(appleid); AddResMenu(mymenus[applemenu],"DRVR");
  162.     mymenus[filemenu]    = GetMenu(fileid);
  163.     mymenus[editmenu]    = GetMenu(editid);
  164.     mymenus[controlmenu] = GetMenu(controlid);
  165.     for (i = 0; i < lastmenu; i++)
  166.     InsertMenu(mymenus[i],0);
  167.     DrawMenuBar();
  168. }
  169.  
  170. int linegetc()
  171. {
  172.     int ch;
  173.  
  174.     if (linelen--) return (*lineptr++);
  175.     linelen = 0;
  176.     while ((ch = scrgetc()) != '\r')
  177.     switch (ch) {
  178.     case EOF:
  179.         return (linegetc());
  180.     case '\010':
  181.         if (linelen > 0) {
  182.         linelen--;
  183.         while (x > linepos[linelen]) {
  184.             scrputc('\010'); scrputc(' '); scrputc('\010');
  185.         }
  186.         }
  187.         break;
  188.     default:
  189.         if (linelen < LINEMAX) { linebuf[linelen] = ch; linepos[linelen] = x; linelen++; }
  190.         scrputc(ch);
  191.         break;
  192.     }
  193.     scrputc('\r'); scrputc('\n');
  194.     linebuf[linelen] = '\n';
  195.     lineptr = linebuf;
  196.     return (*lineptr++);
  197. }
  198.  
  199. int lineputc(ch)
  200.   int ch;
  201. {
  202.     if (ch == '\n') scrputc('\r');
  203.     scrputc(ch);
  204.     return (1);
  205. }
  206.  
  207. scrclear()
  208. {
  209.     curline = screen;
  210.     for (y = 0; y < SCRH; y++)
  211.     for (x = 0; x < SCRW; x++)
  212.         *curline++ = ' ';
  213.     topline = curline = screen;
  214.     x = y = 0;
  215. }
  216.  
  217. int scrgetc()
  218. {
  219.     CursorOn();
  220.     while (charcnt == 0)
  221.     oscheck();
  222.     CursorOff();
  223.     return (scrnextc());
  224. }
  225.  
  226. int scrnextc()
  227. {
  228.     int ch;
  229.     if (charcnt > 0) {
  230.     ch = *outptr++; charcnt--;
  231.     if (outptr >= &charbuf[CHARMAX])
  232.         outptr = charbuf;
  233.     }
  234.     else {
  235.     charcnt = 0;
  236.     ch = EOF;
  237.     }
  238.     return (ch);
  239. }
  240.  
  241. oscheck()
  242. {
  243.     WindowPtr whichwindow;
  244.     EventRecord myevent;
  245.     long sel;
  246.     
  247.     SystemTask();
  248.     CursorUpdate();
  249.  
  250.     while (GetNextEvent(everyEvent,&myevent))
  251.     switch (myevent.what) {
  252.         case mouseDown:
  253.         switch (FindWindow(&myevent.where,&whichwindow)) {
  254.             case inMenuBar:
  255.             if (sel = MenuSelect(&myevent.where))
  256.                 docommand(sel);
  257.             break;
  258.             case inSysWindow:
  259.             SystemClick(&myevent,whichwindow);
  260.             break;
  261.             case inDrag:
  262.             DragWindow(whichwindow,&myevent.where,&dragrect);
  263.             break;
  264.             case inGoAway:
  265.             if (TrackGoAway(whichwindow,&myevent.where))
  266.                 exit();
  267.             break;
  268.             case inGrow:
  269.             case inContent:
  270.             if (whichwindow != FrontWindow() && whichwindow != gwindow)
  271.                 SelectWindow(whichwindow);
  272.             break;
  273.         }
  274.         break;
  275.         case keyDown:
  276.         case autoKey:
  277.         if (cwindow == FrontWindow()) {
  278.             if (myevent.modifiers & 0x100) {
  279.             if (sel = MenuKey((char)myevent.message))
  280.                 docommand(sel);
  281.             }
  282.             else {
  283.             if (charcnt < CHARMAX) {
  284.                     *inptr++ = myevent.message & 0xFF; charcnt++;
  285.                 if (inptr >= &charbuf[CHARMAX])
  286.                 inptr = charbuf;
  287.             }
  288.             }
  289.         }
  290.         break;
  291.         case activateEvt:
  292.         break;
  293.         case updateEvt:
  294.         whichwindow = (WindowPtr) myevent.message;
  295.         BeginUpdate(whichwindow);
  296.         if (whichwindow == cwindow) 
  297.             doupdate();
  298.         EndUpdate(whichwindow);
  299.         break;
  300.         }
  301. }
  302.  
  303. CursorUpdate()
  304. {
  305.     if (cursorstate != -1)
  306.     if (cursortime-- < 0) {
  307.         scrposition(x,y);
  308.         if (cursorstate) {
  309.         DrawChar(' ');
  310.         cursortime = TIMEOFF;
  311.         cursorstate = 0;
  312.         }
  313.         else {
  314.         DrawChar('_');
  315.         cursortime = TIMEON;
  316.         cursorstate = 1;
  317.         }
  318.     }
  319. }
  320.  
  321. CursorOn()
  322. {
  323.     cursorstate = cursortime = 0;
  324. }
  325.  
  326. CursorOff()
  327. {
  328.     if (cursorstate == 1) {
  329.     scrposition(x,y);
  330.     DrawChar(' ');
  331.     }
  332.     cursorstate = -1;
  333. }
  334.  
  335. scrputc(ch)
  336.   int ch;
  337. {
  338.     if (ch == '\r') x = 0;
  339.     else if (ch == '\n') { nextline(&curline); if (y < scrh-1) y++; else scrollup(); }
  340.     else if (ch == '\t') { scrputc(' '); while (x & 7) scrputc(' '); }
  341.     else if (ch == '\010') { if (x) x--; }
  342.     else if (ch >= 0x20 && ch < 0x7F) {
  343.     scrposition(x,y);
  344.     DrawChar(ch);
  345.     curline[x] = ch;
  346.     if (x < scrw-1) x++;
  347.     else { x = 0; nextline(&curline); if (y < scrh-1) y++; else scrollup(); }
  348.     }
  349. }
  350.  
  351. scrflush()
  352. {
  353.     lineptr = linebuf; linebuf[0] = '\n'; linelen = 1;
  354.     inptr = outptr = charbuf;
  355.     charcnt = -1;
  356. }
  357.  
  358. scrposition(x,y)
  359.   int x,y;
  360. {
  361.     MoveTo((x * 6) + 4,(y * 12) + 12);
  362. }
  363.  
  364. pascal filefilter(pblock)
  365.   paramblkptr pblock;
  366. {
  367.     char *p; int len;
  368.     p = pblock->ioNamePtr; len = *p++ &0xFF;
  369.     return (len >= 4 && strncmp(p+len-4,".lsp",4) == 0 ? 0 : 0x100);
  370. }
  371.  
  372. pascal aboutfilter(theDialog,theEvent,itemHit)
  373.   DialogPtr theDialog; EventRecord *theEvent; int *itemHit;
  374. {
  375.     return (theEvent->what == mouseDown ? 0x100 : 0);
  376. }
  377.  
  378. docommand(themenu,theitem)
  379.   int themenu,theitem;
  380. {
  381.     DialogRecord mydialog;
  382.     char name[256];
  383.     GrafPtr gp;
  384.     Point p;
  385.     int n;
  386.     
  387.     CursorOff();
  388.     HiliteMenu(themenu);
  389.     switch (themenu) {
  390.     case appleid:
  391.     switch (theitem) {
  392.     case 1:
  393.         GetNewDialog(129,&mydialog,-1L);
  394.         ModalDialog(aboutfilter,&n);
  395.         CloseDialog(&mydialog);
  396.         break;
  397.     default:
  398.         GetItem(mymenus[0],theitem,name);
  399.         GetPort(&gp);
  400.         OpenDeskAcc(name);
  401.         SetPort(gp);
  402.         break;
  403.     }
  404.     break;
  405.     case fileid:
  406.     switch (theitem) {
  407.     case 1:    /* load */
  408.     case 2:    /* load noisily */
  409.         p.a.h = 100; p.a.v = 100;
  410.         SFGetFile(&p,"",filefilter,-1,filetypes,0L,&loadfile);
  411.         if (loadfile.good) {
  412.         HiliteMenu(0);
  413.         SetVol(0L,loadfile.vRefNum);
  414.         if (xlload(loadfile.fName,1,(theitem == 1 ? 0 : 1)))
  415.             scrflush();
  416.         else
  417.             xlabort("load error");
  418.         }
  419.         break;
  420.     case 4:    /* quit */
  421.         exit();
  422.     }
  423.     break;
  424.     case editid:
  425.     switch (theitem) {
  426.     case 1:    /* undo */
  427.     case 3:    /* cut */
  428.     case 4:    /* copy */
  429.     case 5:    /* paste */
  430.     case 6:    /* clear */
  431.         SystemEdit(theitem-1);
  432.         break;
  433.     }
  434.     break;
  435.     case controlid:
  436.     scrflush();
  437.     HiliteMenu(0);
  438.     switch (theitem) {
  439.     case 1:    /* break */
  440.         xlbreak("user break",s_unbound);
  441.         break;
  442.     case 2:    /* continue */
  443.         xlcontinue();
  444.         break;
  445.     case 3:    /* clean-up error */
  446.         xlcleanup();
  447.         break;
  448.     case 4:    /* Cancel input */
  449.         xlabort("input canceled");
  450.         break;
  451.     case 5:    /* Top Level */
  452.         xltoplevel();
  453.         break;
  454.     case 7:    /* split screen */
  455.         scrsplit(splitmode ? FALSE : TRUE);
  456.         break;
  457.     }
  458.     break;
  459.     }
  460.     HiliteMenu(0);
  461.     CursorOn();
  462. }
  463.  
  464. scrsplit(split)
  465.   int split;
  466. {
  467.     ShowHide(cwindow,0);
  468.     if (split) {
  469.     CheckItem(mymenus[controlmenu],7,1);
  470.     scrh = SSCRH; scrw = SSCRW;
  471.     scrclear();
  472.     MoveWindow(cwindow,SHORIZONTAL,SVERTICAL,1);
  473.     SizeWindow(cwindow,SWIDTH,SHEIGHT,1);
  474.     EraseRect(&cwindow->portRect);
  475.     ShowHide(gwindow,1);
  476.     }
  477.     else {
  478.     CheckItem(mymenus[controlmenu],7,0);
  479.     scrh = NSCRH; scrw = NSCRW;
  480.     scrclear();
  481.     MoveWindow(cwindow,NHORIZONTAL,NVERTICAL,1);
  482.     SizeWindow(cwindow,NWIDTH,NHEIGHT,1);
  483.     EraseRect(&cwindow->portRect);
  484.     ShowHide(gwindow,0);
  485.     }
  486.     ShowHide(cwindow,1);
  487.     splitmode = split;
  488. }
  489.  
  490. doupdate()
  491. {
  492.     char *Line; int y;
  493.     Line = topline;
  494.     for (y = 0; y < scrh; y++) {
  495.     scrposition(0,y);
  496.     DrawText(Line,0,scrw);
  497.     nextline(&Line);
  498.     }
  499. }
  500.  
  501. nextline(pline)
  502.   char **pline;
  503. {
  504.     if ((*pline += SCRW) >= &screen[SCRH*SCRW]) *pline = screen;
  505. }
  506.  
  507. scrollup()
  508. {
  509.     RgnHandle updateRgn;
  510.     int x;
  511.     updateRgn = NewRgn();
  512.     ScrollRect(&cwindow->portRect,0,-12,updateRgn);
  513.     DisposeRgn(updateRgn);
  514.     for (x = 0; x < SCRW; x++)
  515.     topline[x] = ' ';
  516.     nextline(&topline);
  517. }
  518.