home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / ddeml / monitor / testsubs.c < prev   
Encoding:
C/C++ Source or Header  |  1990-06-22  |  7.9 KB  |  316 lines

  1. /****************************** Module Header ******************************\
  2. * Module Name: TestSubs.C
  3. *
  4. * Collection of useful routines for test applications
  5. *
  6. * Created: 16-Jan-87
  7. *
  8. * Copyright (c) 1985, 1986, 1987  Microsoft Corporation
  9. *
  10. \***************************************************************************/
  11.  
  12. #define INCL_WINHEAP
  13. #define INCL_WINWINDOWMGR
  14. #define INCL_WINRECTANGLES
  15. #define INCL_WINTIMER
  16. #define INCL_WINSYS
  17. #include <os2.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "monitor.h"
  21.  
  22.  
  23. /***************************************************************************/
  24.  
  25. extern HAB hab;
  26.  
  27. /* StringWindow structure */
  28. typedef struct {
  29.     int cchLine;
  30.     int cLine;
  31.     char *pchBuffer;
  32.     char *pchBufferMax;
  33.     char *pchBottomLine;
  34.     char *pchOutput;
  35. } STRWND;
  36.  
  37. int cyChar;                     /* Height of a line */
  38. int cyDescent;
  39.  
  40. USHORT ticks;                     /* Tick count used with StartTime/StopTime */
  41.  
  42. HHEAP hhp = NULL;
  43.  
  44. /***************************************************************************/
  45.  
  46. /***************************** Public  Function ****************************\
  47. * BOOL InitTestSubs( )
  48. *
  49. * This routine MUST be called before using anything in this file.  Registers
  50. * window classes, loads brushes, etc.  Returns TRUE if successful, FALSE
  51. * otherwise.
  52. *
  53. * Warnings: Must be called AFTER the global variable NULL is initialized.
  54. *
  55. * History:
  56. *  19-Jan-87 by neilk  Created
  57. \***************************************************************************/
  58.  
  59. BOOL InitTestSubs()
  60. {
  61.     cyChar = 14;
  62.     cyDescent = 2;
  63.  
  64.     if (!WinRegisterClass(hab, (PCH)"StringWindow", (PFNWP)StrWndProc,
  65.        CS_SYNCPAINT, sizeof(STRWND *)))
  66.         return(FALSE);
  67.  
  68.     hhp = WinCreateHeap(0, 0, 0, 0, 0, 0);
  69.  
  70.     return(TRUE);
  71. }
  72.  
  73.  
  74. void
  75. NextLine(
  76. STRWND *psw)
  77. {    
  78.     psw->pchBottomLine += psw->cchLine;
  79.     if (psw->pchBottomLine == psw->pchBufferMax)
  80.         psw->pchBottomLine = psw->pchBuffer;
  81.     psw->pchOutput = psw->pchBottomLine;
  82.     *psw->pchOutput = '\0';
  83. }
  84.  
  85.  
  86. /***************************** Public  Function ****************************\
  87. * VOID DrawString(hwnd, sz)
  88. *
  89. * This routine prints a string in the specified StringWindow class window.
  90. * sz is a near pointer to a zero-terminated string, which can be produced
  91. * with sprintf().
  92. *
  93. * History:
  94. *  19-Jan-87 by neilk  Created
  95. \***************************************************************************/
  96.  
  97. VOID DrawString(hwnd, sz)
  98. HWND hwnd;
  99. char *sz;
  100. {
  101.     register STRWND *psw;
  102.     USHORT cLines = 0;
  103.  
  104.     psw = (STRWND *)WinQueryWindowUShort(hwnd, 0);
  105.     NextLine(psw);
  106.     cLines++;
  107.  
  108.     while (*sz) {
  109.         switch (*sz) {
  110.         case 0x0a:
  111.             break;
  112.             
  113.         case 0x09:
  114.             *psw->pchOutput++ = ' ';
  115.             *psw->pchOutput++ = ' ';
  116.             *psw->pchOutput++ = ' ';
  117.             *psw->pchOutput++ = ' ';
  118.             break;
  119.             
  120.         case 0x0d:
  121.             *psw->pchOutput++ = '\0';
  122.             NextLine(psw);
  123.             cLines++;
  124.             break;
  125.             
  126.         default:
  127.             *psw->pchOutput++ = *sz;
  128.         }
  129.         sz++;
  130.     }
  131.     WinScrollWindow(hwnd, 0, cyChar * cLines, (PWRECT)NULL, (PWRECT)NULL, (HRGN)NULL,
  132.       (PWRECT)NULL, SW_INVALIDATERGN);
  133. }
  134.  
  135. /***************************** Public  Function ****************************\
  136. * "StringWindow" window class
  137. *
  138. * Windows of the "StringWindow" window class are simple scrolling text output
  139. * windows that are refreshed properly as windows are rearranged.  A text buffer
  140. * is maintained to store the characters as they are drawn.
  141. *
  142. * When creating a StringWindow window, lpCreateParams is actually a ULONG
  143. * containing the dimensions of the text buffer to be created, if 0L, then
  144. * a 80 by 25 buffer is created.
  145. *
  146. * History:
  147. *  19-Jan-87 by neilk  Created
  148. \***************************************************************************/
  149.  
  150. MRESULT FAR PASCAL StrWndProc(hwnd, msg, mp1, mp2)
  151. register HWND hwnd;
  152. USHORT msg;
  153. MPARAM mp1;
  154. MPARAM mp2;
  155. {
  156.     register STRWND *psw;
  157.     HPS hps;
  158.     RECTL rclPaint;
  159.  
  160.     switch (msg) {
  161.     case WM_CREATE:
  162.         if (mp1     == 0L) {
  163.             mp1     = MAKEULONG(80, 50);
  164.         }
  165.         if (!StrWndCreate(hwnd, SHORT1FROMMP(mp1), SHORT2FROMMP(mp1)))
  166.             return(TRUE);
  167.         break;
  168.  
  169.     case WM_DESTROY:
  170.         if ((psw = (STRWND *)WinQueryWindowUShort(hwnd, 0)) != NULL) {
  171.             WinFreeMem(hhp, (BYTE *)psw->pchBuffer, strlen(psw->pchBuffer));
  172.             WinFreeMem(hhp, (BYTE *)psw, sizeof(STRWND));
  173.         }
  174.         break;
  175.  
  176.     case WM_PAINT:
  177.         hps = WinBeginPaint(hwnd, (HPS)NULL, &rclPaint);
  178.         PaintStrWnd(hwnd, hps, &rclPaint);
  179.         WinEndPaint(hps);
  180.         break;
  181.  
  182.     case WM_CALCVALIDRECTS:
  183. #ifdef LATER
  184.         /*
  185.          * We want to bottom right justify the bits.
  186.          */
  187.         (mp2)->yTop = (mp2)->yBottom -
  188.                 (mp1)->yBottom;
  189. #endif
  190.         break;
  191.  
  192.     default:
  193.         return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  194.         break;
  195.     }
  196.     return(0L);
  197. }
  198.  
  199.  
  200.  
  201. BOOL StrWndCreate(hwnd, cchLine, cLine)
  202. HWND hwnd;
  203. int cchLine, cLine;
  204. {
  205.     register char *pch;
  206.     char *pchEnd;
  207.     STRWND *psw;
  208.  
  209.     if ((psw = (STRWND *)WinAllocMem(hhp, sizeof(STRWND))) == NULL)
  210.         return(FALSE);
  211.     /*
  212.      * Allocate buffer for strings
  213.      */
  214.     if ((pch = (char *)WinAllocMem(hhp, cchLine * cLine)) == NULL) {
  215.         WinFreeMem(hhp, (BYTE *)psw, sizeof(STRWND));
  216.         return(FALSE);
  217.     }
  218.  
  219.     psw->cchLine       = cchLine;
  220.     psw->cLine         = cLine;
  221.     psw->pchBuffer     = pch;
  222.     psw->pchBufferMax  = pch + cchLine * cLine;
  223.     psw->pchBottomLine = pch;
  224.     psw->pchOutput     = pch;
  225.     WinSetWindowUShort(hwnd, 0, (USHORT)psw);
  226.  
  227.     /*
  228.      * Make all the lines empty
  229.      */
  230.     pchEnd = psw->pchBufferMax;
  231.     while (pch != pchEnd) {
  232.         *pch = '\0';
  233.         pch += cchLine;
  234.     }
  235.     return(TRUE);
  236. }
  237.  
  238.  
  239.  
  240. VOID PaintStrWnd(hwnd, hps, prcl)
  241. HWND hwnd;
  242. HPS hps;
  243. PRECTL prcl;
  244. {
  245.     register STRWND *psw;
  246.     register char *pch;
  247.     int x;
  248.     int y;
  249.     WRECT rc;
  250.     RECTL rcl;
  251.  
  252.     psw = (STRWND *)WinQueryWindowUShort(hwnd, 0);
  253.  
  254.     WinQueryWindowRect(hwnd, (PRECTL)&rc);
  255.  
  256.     WinFillRect(hps, (PRECTL)&rc, SYSCLR_WINDOW);
  257.  
  258.     x = rc.xLeft;
  259.     y = rc.yBottom;
  260.     pch = psw->pchBottomLine;
  261.  
  262.     if (prcl != NULL)
  263.         WinIntersectRect(hab, (PRECTL)&rc, (PRECTL)&rc, prcl);
  264.  
  265.     do {
  266.         if (y >= rc.yTop)
  267.             break;
  268.         if (y + cyChar >= rc.yBottom) {
  269.             rcl.xLeft = x;
  270.             rcl.yBottom = y + cyDescent;
  271.             rcl.xRight = 1000;
  272.             rcl.yTop = rcl.yBottom + cyChar;
  273.             WinDrawText(hps, -1, (PSZ)pch, (PRECTL)&rcl, SYSCLR_WINDOWTEXT,
  274.                     SYSCLR_WINDOW, 0);
  275.         }
  276.         y += cyChar;
  277.         /*
  278.          * Back up to previous line
  279.          */
  280.         if (pch == psw->pchBuffer)
  281.             pch = psw->pchBufferMax;
  282.         pch -= psw->cchLine;
  283.     } while (pch != psw->pchBottomLine);
  284. }
  285.  
  286. /***************************** Public  Function ****************************\
  287. * VOID StartTime()
  288. * VOID StopTime(hwndStrWnd, szMsg)
  289. *
  290. * These functions are used to time sections of code.  StartTime() begins the
  291. * timing, and StopTime() stops it, printing out the elapsed time since
  292. * StartTime() was called in the "StringWindow" class window hwndStrWnd, using
  293. * the sprintf() formatting string szMsg.  The elapsed time in milliseconds is
  294. * replaced for a "%d" in szMsg.
  295. *
  296. * History:
  297. *  19-Jan-87 by neilk  Created
  298. \***************************************************************************/
  299.  
  300. VOID StartTime()
  301. {
  302.     ticks = (USHORT)WinGetCurrentTime(hab);
  303. }
  304.  
  305. VOID StopTime(hwndStrWnd, szFmt)
  306. HWND hwndStrWnd;
  307. char *szFmt;
  308. {
  309.     USHORT dt;
  310.     char rgch[80];
  311.  
  312.     dt = (USHORT)(WinGetCurrentTime(hab) - ticks);
  313.     sprintf((char *)rgch, szFmt, dt);
  314.     DrawString(hwndStrWnd, rgch);
  315. }
  316.