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