home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip511.zip / mac / macscreen.c < prev    next >
C/C++ Source or Header  |  1993-12-15  |  8KB  |  323 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, 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 's':
  92.             n |= scrollOption;
  93.             break;
  94.         default:
  95.             break;
  96.         }
  97.         options += 1;
  98.     }
  99.  
  100.     if (setting == 0)
  101.         screenOptions &= (n ^ (-1));
  102.     else
  103.         screenOptions |= n;
  104.  
  105.     if ((pausePosition = maxPosition - currentPosition) == 0)
  106.         pausePosition = maxPosition - fontHeight;
  107.  
  108.     return;
  109. }
  110.  
  111. void screenClose(void) {
  112.     DisposPtr(screenLine);
  113.  
  114.     DisposeWindow(theWindow);
  115.  
  116.     return;
  117. }
  118.  
  119. void screenUpdate(WindowPtr window) {
  120.     GrafPort *savePort;
  121.     int m, n;
  122.  
  123.     if (window == theWindow) {
  124.         BeginUpdate(window);
  125.         if (!EmptyRgn(window->visRgn)) {
  126.             GetPort(&savePort);
  127.             SetPort(window);
  128.             n = startLine;
  129.             for (m = 1; ; m++) {
  130.                 MoveTo(5, m * fontHeight);
  131.                 if (screenLength[n] != 0)
  132.                     DrawText(screenLine[n], 0, screenLength[n]);
  133.                 if (n == endLine) break;
  134.                 if ((n += 1) == screenHeight) n = 0;
  135.             }
  136.             SetPort(savePort);
  137.         }
  138.         EndUpdate(window);
  139.     }
  140.  
  141.     return;
  142. }
  143.  
  144. static void screenNewline(void) {
  145.     MoveTo(5, currentPosition += fontHeight);
  146.     if (currentPosition > maxPosition) {
  147.         if (screenOptions & scrollOption) {
  148.             ScrollRect(&scrollRect, 0, -fontHeight, scrollRgn);
  149.             MoveTo(5, currentPosition = maxPosition);
  150.             if ((startLine += 1) == screenHeight) startLine = 0;
  151.         } else {
  152.             ScrollRect(&scrollRect, 0, -maxPosition + fontHeight, scrollRgn);
  153.             MoveTo(5, currentPosition = fontHeight + fontHeight);
  154.             startLine = endLine;
  155.         }
  156.     }
  157.     pausePosition -= fontHeight;
  158.  
  159.     if ((endLine += 1) == screenHeight) endLine = 0;
  160.     screenLength[endLine] = 0;
  161.  
  162.     return;
  163. }
  164.  
  165. static char waitChar(void) {
  166.     WindowPtr whichWindow;
  167.     EventRecord theEvent;
  168.  
  169.     for ( ; ; ) {
  170.         SystemTask();
  171.         if (GetNextEvent(everyEvent, &theEvent)) {
  172.             switch (theEvent.what) {
  173.             case keyDown:
  174.                 if ((theEvent.modifiers & cmdKey) &&
  175.                     ((theEvent.message & charCodeMask) == '.'))
  176.                     ExitToShell();
  177.                 return(theEvent.message & charCodeMask);
  178.             case mouseDown:
  179.                 if (FindWindow(theEvent.where, &whichWindow) == inSysWindow)
  180.                     SystemClick(&theEvent, whichWindow);
  181.                 break;
  182.             case updateEvt:
  183.                 screenUpdate((WindowPtr)theEvent.message);
  184.                 break;
  185.             }
  186.         }
  187.     }
  188. }
  189.  
  190. static void screenPause(void) {
  191.  
  192.     if (pausePosition == 0) {
  193.         if (screenOptions & pauseOption) {
  194.             DrawText("Press any key to continue ...", 0, 29);
  195.             memcpy(screenLine[endLine], "Press any key to continue ...", 29);
  196.             screenLength[endLine] = 29;
  197.  
  198.             (void)waitChar();
  199.  
  200.             EraseRect(&pauseRect);
  201.             MoveTo(5, currentPosition);
  202.             screenLength[endLine] = 0;
  203.         }
  204.  
  205.         pausePosition = maxPosition - fontHeight;
  206.     }
  207.  
  208.     return;
  209. }
  210.  
  211. void screenDisplay(char *s) {
  212.     GrafPort *savePort;
  213.     int m, n;
  214.     char *t;
  215.  
  216.     GetPort(&savePort);
  217.     SetPort(theWindow);
  218.  
  219.     while (*s) {
  220.         screenPause();
  221.  
  222.         for (t = s; (*s) && (*s != '\n') && (*s != '\r'); s++);
  223.  
  224.         if ((n = s - t) > (m = screenWidth - screenLength[endLine])) n = m;
  225.  
  226.         if (n > 0) {
  227.             DrawText(t, 0, n);
  228.             memcpy(screenLine[endLine] + screenLength[endLine], t, n);
  229.             screenLength[endLine] += n;
  230.         }
  231.  
  232.         if ((*s == '\n') || (*s == '\r')) {
  233.             screenNewline();
  234.             s += 1;
  235.         }
  236.     }
  237.  
  238.     SetPort(savePort);
  239.  
  240.     return;
  241. }
  242.  
  243. void screenDump(char *s, long n) {
  244.     GrafPort *savePort;
  245.     int k, m;
  246.     char *t;
  247.  
  248.     GetPort(&savePort);
  249.     SetPort(theWindow);
  250.  
  251.     while (n) {
  252.         screenPause();
  253.  
  254.         for (t = s; (n) && (*s != '\n') && (*s != '\r'); s++, n--);
  255.  
  256.         if ((k = s - t) > (m = screenWidth - screenLength[endLine])) k = m;
  257.  
  258.         if (k > 0) {
  259.             DrawText(t, 0, k);
  260.             memcpy(screenLine[endLine] + screenLength[endLine], t, k);
  261.             screenLength[endLine] += k;
  262.         }
  263.  
  264.         if ((*s == '\n') || (*s == '\r')) {
  265.             screenNewline();
  266.             s += 1;
  267.             n -= 1;
  268.         }
  269.     }
  270.  
  271.     SetPort(savePort);
  272.  
  273.     return;
  274. }
  275.  
  276. char *wfgets(char *s, int n, FILE *stream) {
  277.     GrafPort *savePort;
  278.     char c, *t = s;
  279.  
  280.     GetPort(&savePort);
  281.     SetPort(theWindow);
  282.  
  283.     for (n -= 1; (n > 0) && ((c = waitChar()) != '\r'); n -= 1) {
  284.         DrawChar(*t++ = c);
  285.         if (screenLength[endLine] < screenWidth)
  286.             screenLine[endLine][screenLength[endLine]++] = c;
  287.     }
  288.  
  289.     if (c == '\r') screenNewline();
  290.  
  291.     *t = '\0';
  292.  
  293.     SetPort(savePort);
  294.  
  295.     return(s);
  296. }
  297.  
  298. void wfprintf(FILE *stream, char *format, ...) {
  299.     char buffer[bufferSize];
  300.     va_list ap;
  301.  
  302.     va_start(ap, format);
  303.     vsprintf(buffer, format, ap);
  304.     va_end(ap);
  305.  
  306.     screenDisplay(buffer);
  307.  
  308.     return;
  309. }
  310.  
  311. void wprintf(char *format, ...) {
  312.     char buffer[bufferSize];
  313.     va_list ap;
  314.  
  315.     va_start(ap, format);
  316.     vsprintf(buffer, format, ap);
  317.     va_end(ap);
  318.  
  319.     screenDisplay(buffer);
  320.  
  321.     return;
  322. }
  323.