home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / mac / macscreen.c < prev    next >
C/C++ Source or Header  |  1997-03-28  |  8KB  |  325 lines

  1. #include <QuickDraw.h>
  2.  
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6.  
  7. #define bufferSize      1024
  8.  
  9. #define screenWindow    128
  10.  
  11. static Rect scrollRect, pauseRect;
  12. static WindowPtr theWindow;
  13. static RgnHandle scrollRgn;
  14.  
  15. static short fontHeight, fontWidth, screenHeight, screenWidth;
  16. static short currentPosition, maxPosition, pausePosition;
  17.  
  18. static short *screenLength, startLine, endLine;
  19. static char *screenImage, **screenLine;
  20.  
  21. static int screenOptions;
  22.  
  23. #define pauseOption     0x0001
  24. #define scrollOption    0x0002
  25.  
  26. void screenOpen(char *Title) {
  27.     FontInfo fontInfo;
  28.     int n;
  29.  
  30.     theWindow = GetNewWindow(screenWindow, nil, (WindowPtr)(-1));
  31.  
  32.     if ((Title != NULL) && (*Title != '\0')) {
  33.         c2pstr(Title);
  34.         SetWTitle(theWindow, (StringPtr)Title);
  35.         p2cstr(Title);
  36.     }
  37.  
  38.     ShowWindow(theWindow);
  39.  
  40.     SetPort(theWindow);
  41.     TextFont(monaco);
  42.     TextSize(9);
  43.  
  44.     GetFontInfo(&fontInfo);
  45.     fontHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
  46.     fontWidth = fontInfo.widMax;
  47.  
  48.     scrollRgn = NewRgn();
  49.  
  50.     screenWidth = (theWindow->portRect.right - theWindow->portRect.left - 10) /
  51.         fontWidth;
  52.     screenHeight = (theWindow->portRect.bottom - theWindow->portRect.top) /
  53.         fontHeight;
  54.     maxPosition = screenHeight * fontHeight;
  55.     pausePosition = maxPosition - (currentPosition = fontHeight);
  56.  
  57.     SetRect(&scrollRect, theWindow->portRect.left, theWindow->portRect.top + fontInfo.descent,
  58.         theWindow->portRect.right, theWindow->portRect.bottom);
  59.     SetRect(&pauseRect, theWindow->portRect.left, pausePosition + fontInfo.descent,
  60.         theWindow->portRect.right, theWindow->portRect.bottom);
  61.  
  62.     MoveTo(5, currentPosition);
  63.  
  64.     n = (sizeof(char *) + sizeof(short) + screenWidth) * screenHeight;
  65.  
  66.     screenLine = (char **)NewPtr(n);
  67.  
  68.     screenLength = (short *)&screenLine[screenHeight];
  69.     screenImage = (char *)&screenLength[screenHeight];
  70.  
  71.     for (n = 0; n < screenHeight; n++) {
  72.         screenLine[n] = &screenImage[n * screenWidth];
  73.         screenLength[n] = 0;
  74.     }
  75.  
  76.     startLine = endLine = 0;
  77.  
  78.     screenOptions = 0;
  79.  
  80.     return;
  81. }
  82.  
  83. void screenControl(options, setting) char *options; int setting; {
  84.     int n = 0;
  85.  
  86.     while (*options) {
  87.         switch (*options) {
  88.         case 'p':
  89.             n |= pauseOption;
  90.             break;
  91.         case 'r':
  92.             currentPosition = maxPosition;
  93.             break;
  94.         case 's':
  95.             n |= scrollOption;
  96.             break;
  97.         default:
  98.             break;
  99.         }
  100.         options += 1;
  101.     }
  102.  
  103.     if (setting == 0)
  104.         screenOptions &= (n ^ (-1));
  105.     else
  106.         screenOptions |= n;
  107.  
  108.     if ((pausePosition = maxPosition - currentPosition) == 0)
  109.         pausePosition = maxPosition - fontHeight;
  110.  
  111.     return;
  112. }
  113.  
  114. void screenClose(void) {
  115.     DisposPtr((Ptr)screenLine);
  116.  
  117.     DisposeWindow(theWindow);
  118.  
  119.     return;
  120. }
  121.  
  122. void screenUpdate(WindowPtr window) {
  123.     GrafPort *savePort;
  124.     int m, n;
  125.  
  126.     if (window == theWindow) {
  127.         BeginUpdate(window);
  128.         if (!EmptyRgn(window->visRgn)) {
  129.             GetPort(&savePort);
  130.             SetPort(window);
  131.             n = startLine;
  132.             for (m = 1; ; m++) {
  133.                 MoveTo(5, m * fontHeight);
  134.                 if (screenLength[n] != 0)
  135.                     DrawText(screenLine[n], 0, screenLength[n]);
  136.                 if (n == endLine) break;
  137.                 if ((n += 1) == screenHeight) n = 0;
  138.             }
  139.             SetPort(savePort);
  140.         }
  141.         EndUpdate(window);
  142.     }
  143.  
  144.     return;
  145. }
  146.  
  147. static void screenNewline(void) {
  148.     MoveTo(5, currentPosition += fontHeight);
  149.     if (currentPosition > maxPosition) {
  150.         if (screenOptions & scrollOption) {
  151.             ScrollRect(&scrollRect, 0, -fontHeight, scrollRgn);
  152.             MoveTo(5, currentPosition = maxPosition);
  153.             if ((startLine += 1) == screenHeight) startLine = 0;
  154.         } else {
  155.             ScrollRect(&scrollRect, 0, -maxPosition + fontHeight, scrollRgn);
  156.             MoveTo(5, currentPosition = fontHeight + fontHeight);
  157.             startLine = endLine;
  158.         }
  159.     }
  160.     pausePosition -= fontHeight;
  161.  
  162.     if ((endLine += 1) == screenHeight) endLine = 0;
  163.     screenLength[endLine] = 0;
  164.  
  165.     return;
  166. }
  167.  
  168. static char waitChar(void) {
  169.     WindowPtr whichWindow;
  170.     EventRecord theEvent;
  171.  
  172.     for ( ; ; ) {
  173.         SystemTask();
  174.         if (GetNextEvent(everyEvent, &theEvent)) {
  175.             switch (theEvent.what) {
  176.             case keyDown:
  177.                 if ((theEvent.modifiers & cmdKey) &&
  178.                     ((theEvent.message & charCodeMask) == '.'))
  179.                     ExitToShell();
  180.                 return(theEvent.message & charCodeMask);
  181.             case mouseDown:
  182.                 if (FindWindow(theEvent.where, &whichWindow) == inSysWindow)
  183.                     SystemClick(&theEvent, whichWindow);
  184.                 break;
  185.             case updateEvt:
  186.                 screenUpdate((WindowPtr)theEvent.message);
  187.                 break;
  188.             }
  189.         }
  190.     }
  191. }
  192.  
  193. static void screenPause(void) {
  194.     if (pausePosition == 0) {
  195.         if (screenOptions & pauseOption) {
  196.             DrawText("Press any key to continue ...", 0, 29);
  197.             memcpy(screenLine[endLine], "Press any key to continue ...", 29);
  198.             screenLength[endLine] = 29;
  199.  
  200.             (void)waitChar();
  201.  
  202.             EraseRect(&pauseRect);
  203.             MoveTo(5, currentPosition);
  204.             screenLength[endLine] = 0;
  205.         }
  206.  
  207.         pausePosition = maxPosition - fontHeight;
  208.     }
  209.  
  210.     return;
  211. }
  212.  
  213. void screenDisplay(char *s) {
  214.     GrafPort *savePort;
  215.     int m, n;
  216.     char *t;
  217.  
  218.     GetPort(&savePort);
  219.     SetPort(theWindow);
  220.  
  221.     while (*s) {
  222.         screenPause();
  223.  
  224.         for (t = s; (*s) && (*s != '\n') && (*s != '\r'); s++);
  225.  
  226.         if ((n = s - t) > (m = screenWidth - screenLength[endLine])) n = m;
  227.  
  228.         if (n > 0) {
  229.             DrawText(t, 0, n);
  230.             memcpy(screenLine[endLine] + screenLength[endLine], t, n);
  231.             screenLength[endLine] += n;
  232.         }
  233.  
  234.         if ((*s == '\n') || (*s == '\r')) {
  235.             screenNewline();
  236.             s += 1;
  237.         }
  238.     }
  239.  
  240.     SetPort(savePort);
  241.  
  242.     return;
  243. }
  244.  
  245. void screenDump(char *s, long n) {
  246.     GrafPort *savePort;
  247.     int k, m;
  248.     char *t;
  249.  
  250.     GetPort(&savePort);
  251.     SetPort(theWindow);
  252.  
  253.     while (n) {
  254.         screenPause();
  255.  
  256.         for (t = s; (n) && (*s != '\n') && (*s != '\r'); s++, n--);
  257.  
  258.         if ((k = s - t) > (m = screenWidth - screenLength[endLine])) k = m;
  259.  
  260.         if (k > 0) {
  261.             DrawText(t, 0, k);
  262.             memcpy(screenLine[endLine] + screenLength[endLine], t, k);
  263.             screenLength[endLine] += k;
  264.         }
  265.  
  266.         if ((*s == '\n') || (*s == '\r')) {
  267.             screenNewline();
  268.             s += 1;
  269.             n -= 1;
  270.         }
  271.     }
  272.  
  273.     SetPort(savePort);
  274.  
  275.     return;
  276. }
  277.  
  278. char *wfgets(char *s, int n, FILE *stream) {
  279.     GrafPort *savePort;
  280.     char c, *t = s;
  281.  
  282.     GetPort(&savePort);
  283.     SetPort(theWindow);
  284.  
  285.     for (n -= 1; (n > 0) && ((c = waitChar()) != '\r'); n -= 1) {
  286.         DrawChar(*t++ = c);
  287.         if (screenLength[endLine] < screenWidth)
  288.             screenLine[endLine][screenLength[endLine]++] = c;
  289.     }
  290.  
  291.     if (c == '\r') screenNewline();
  292.  
  293.     *t = '\0';
  294.  
  295.     SetPort(savePort);
  296.  
  297.     return(s);
  298. }
  299.  
  300. void wfprintf(FILE *stream, char *format, ...) {
  301.     char buffer[bufferSize];
  302.     va_list ap;
  303.  
  304.     va_start(ap, format);
  305.     vsprintf(buffer, format, ap);
  306.     va_end(ap);
  307.  
  308.     screenDisplay(buffer);
  309.  
  310.     return;
  311. }
  312.  
  313. void wprintf(char *format, ...) {
  314.     char buffer[bufferSize];
  315.     va_list ap;
  316.  
  317.     va_start(ap, format);
  318.     vsprintf(buffer, format, ap);
  319.     va_end(ap);
  320.  
  321.     screenDisplay(buffer);
  322.  
  323.     return;
  324. }
  325.