home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / perfmon / status.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-13  |  13.6 KB  |  459 lines

  1. #include "perfmon.h"
  2. #include "status.h"  // External declarations for this file
  3. #include <stdio.h>   // for sprintf.
  4. #include <stdarg.h>  // For ANSI variable args. Dont use UNIX <varargs.h>
  5.  
  6. #include "log.h"        // for LogCollecting
  7. #include "perfmops.h"   // for SmallFileSizeString
  8. #include "playback.h"   // for PlayingBackLog
  9. #include "utils.h"
  10.  
  11. //================================//
  12. // Options for PaintStatusBar     //
  13. //================================//
  14. #define     PaintText         1
  15. #define     PaintIcons        2
  16. #define     PaintBoundary     4
  17. #define     PaintAll          (PaintText + PaintIcons + PaintBoundary)
  18.  
  19. //==========================================================================//
  20. //                                  Constants                               //
  21. //==========================================================================//
  22.  
  23. #define szStatusClass          TEXT("PerfmonStatusClass")
  24. #define dwStatusClassStyle     (CS_HREDRAW | CS_VREDRAW | CS_OWNDC)
  25. #define iStatusClassExtra      (0)
  26. #define iStatusWindowExtra     (0)
  27. #define dwStatusWindowStyle    (WS_CHILD | WS_VISIBLE) 
  28.  
  29.  
  30. #define szAlertMax            TEXT(" 99 ")
  31. #define szAlertFormat         TEXT(" %2d ")
  32. #define szAlertOverflow       TEXT(" ++")
  33.  
  34. #define szLogMax              TEXT(" 9,999.9M ")
  35.  
  36. //==========================================================================//
  37. //                                Local Data                                //
  38. //==========================================================================//
  39.  
  40.  
  41. HDC            hStatusDC ;                   // for OWN_DC
  42.  
  43. HDC            hLogBitmapDC ;
  44. int            xLogBitmapWidth ;     
  45. int            yLogBitmapHeight ;
  46.  
  47. HDC            hAlertBitmapDC ;
  48. int            xAlertBitmapWidth ;     
  49. int            yAlertBitmapHeight ;     
  50.  
  51. int            yStatusHeight ;
  52. int            xStatusAlertWidth ;           // of alert bm and num alerts
  53. int            xStatusLogWidth ;             // of log bitmap and file size
  54.  
  55. int            szStatusLineLen ;             // no. of char. in szStatusLine
  56. TCHAR          szStatusLine [MessageLen+ResourceStringLen] ;
  57. TCHAR          szCurrentActivity [ResourceStringLen] ;
  58. TCHAR          szStatusFormat  [ResourceStringLen] ;
  59. TCHAR          szStatusFormat2 [ResourceStringLen] ;
  60.  
  61. HBITMAP        hBitmapAlertStatus ;
  62. HBITMAP        hBitmapLogStatus ;
  63.  
  64.  
  65.  
  66. //==========================================================================//
  67. //                                   Macros                                 //
  68. //==========================================================================//
  69.  
  70.  
  71. #define StatusTopMargin()     (2)
  72. #define StatusLeftMargin()    (2)
  73.  
  74.  
  75. //==========================================================================//
  76. //                              Local Functions                             //
  77. //==========================================================================//
  78.  
  79.  
  80. void DrawAlerts (HDC hDC,
  81.                  LPRECT lpRect)
  82.    {
  83.    TCHAR          szText [10] ;
  84.    RECT           rectText ;
  85.    int            yBitmapTop ;     
  86.  
  87.    if (!iUnviewedAlerts)
  88.       return ;
  89.  
  90.    yBitmapTop = lpRect->top +
  91.                 (lpRect->bottom - 
  92.                  lpRect->top - 
  93.                  yAlertBitmapHeight) / 2 ;
  94.  
  95.    SetTextColor (hDC, crLastUnviewedAlert) ;
  96.  
  97.    BitBlt (hDC,                           // DC for Destination surface
  98.       lpRect->right - xStatusAlertWidth,  // x pos for Destination surface
  99.       yBitmapTop,                         // y for Destination surface
  100.       xAlertBitmapWidth,                  // width of bitmap
  101.       yAlertBitmapHeight,                 // height of bitmap
  102.       hAlertBitmapDC,                     // DC for source surface
  103.       0, 0,                               // location in source surface
  104.       SRCCOPY) ;                          // ROP code
  105.    
  106.    SetTextColor (hDC, crBlack) ;
  107.  
  108.    if (iUnviewedAlerts > 99)
  109.       lstrcpy (szText, szAlertOverflow) ;
  110.    else
  111.       TSPRINTF (szText, szAlertFormat, iUnviewedAlerts) ;
  112.  
  113.    rectText.left = lpRect->right - xStatusAlertWidth + xAlertBitmapWidth ;
  114.    rectText.top = lpRect->top + 1 ;
  115.    rectText.right = lpRect->right - 1 ;
  116.    rectText.bottom = lpRect->bottom - 1 ;
  117.  
  118.    ExtTextOut (hDC, rectText.left, rectText.top, ETO_CLIPPED | ETO_OPAQUE,
  119.       &rectText, szText, lstrlen (szText), NULL) ;
  120.  
  121.    lpRect->right -= (xStatusAlertWidth + (xAlertBitmapWidth >> 2)) ;
  122.    }  // DrawAlerts
  123.  
  124.  
  125. void DrawLog (HDC hDC,
  126.               LPRECT lpRect)
  127.    {
  128.    TCHAR          szText [10] ;
  129.    RECT           rectText ;
  130.    int            yBitmapTop ;     
  131.  
  132.    if (!LogCollecting (hWndLog))
  133.       return ;
  134.  
  135.    yBitmapTop = lpRect->top +
  136.                 (lpRect->bottom - 
  137.                  lpRect->top - 
  138.                  yLogBitmapHeight) / 2 ;
  139.    BitBlt (hDC,                           // DC for Destination surface
  140.       lpRect->right - xStatusLogWidth,    // x pos for Destination surface
  141.       yBitmapTop,                         // y for Destination surface
  142.       xLogBitmapWidth,                    // width of bitmap
  143.       yLogBitmapHeight,                   // height of bitmap
  144.       hLogBitmapDC,                       // DC for source surface
  145.       0, 0,                               // location in source surface
  146.       SRCCOPY) ;                          // ROP code
  147.  
  148.  
  149.    SmallFileSizeString (LogFileSize (hWndLog), szText) ;
  150.  
  151.    rectText.left = lpRect->right - xStatusLogWidth +
  152.       xLogBitmapWidth + 1 ;
  153.    rectText.top = lpRect->top + 1 ;
  154.    rectText.right = lpRect->right - 1 ;
  155.    rectText.bottom = lpRect->bottom - 1 ;
  156.  
  157.    ExtTextOut (hDC, rectText.left, rectText.top, ETO_CLIPPED | ETO_OPAQUE,
  158.       &rectText, szText, lstrlen (szText), NULL) ;
  159.  
  160.    lpRect->right -= xStatusLogWidth ;
  161.    }  // DrawLog
  162.  
  163.  
  164. //==========================================================================//
  165. //                              Message Handlers                            //
  166. //==========================================================================//
  167.  
  168.  
  169. void static OnCreate (HWND hWnd)
  170. /*
  171.    Effect:        Perform any actions needed when a status window is created.
  172.                   In particular, set the instance data to initial values,
  173.                   determine the size and placement of the various elements
  174.                   of the status display.
  175.  
  176.    Called By:     StatusWndProc only, in response to a WM_CREATE message.
  177. */
  178.    {  // OnCreate
  179.    HDC            hDC ;
  180.  
  181.  
  182.    hBitmapAlertStatus = LoadBitmap (hInstance, idBitmapAlertStatus) ;
  183.    hBitmapLogStatus = LoadBitmap (hInstance, idBitmapLogStatus) ;
  184.  
  185.    hDC = hStatusDC = GetDC (hWnd) ;
  186.    SelectFont (hDC, hFontScales) ;
  187.    SetBkColor (hDC, ColorBtnFace) ;
  188.    SetTextAlign (hDC, TA_LEFT) ;
  189.    SetBkMode (hDC, OPAQUE) ;
  190.  
  191.    yStatusHeight = 2 * StatusTopMargin () + 
  192.                    FontHeight (hDC, TRUE) +
  193.                    2 * ThreeDPad ;
  194.    
  195.    BitmapDimemsion (hBitmapLogStatus, &yLogBitmapHeight, &xLogBitmapWidth) ;
  196.    BitmapDimemsion (hBitmapAlertStatus, &yAlertBitmapHeight, &xAlertBitmapWidth) ;
  197.  
  198.    // pre-load the log and alert bitmaps for perfmormance
  199.  
  200.    hLogBitmapDC = CreateCompatibleDC (hDC) ;
  201.    SelectObject (hLogBitmapDC, hBitmapLogStatus) ;
  202.  
  203.    hAlertBitmapDC = CreateCompatibleDC (hDC) ;
  204.    SelectObject (hAlertBitmapDC, hBitmapAlertStatus) ;
  205.  
  206.  
  207.    xStatusAlertWidth = xAlertBitmapWidth + 1 +
  208.                        TextWidth (hDC, szAlertMax) ;
  209.    xStatusLogWidth = xLogBitmapWidth +
  210.                      TextWidth (hDC, szLogMax) ;
  211.  
  212.  
  213.    StringLoad (IDS_CURRENTACTIVITY, szCurrentActivity) ;
  214.    StringLoad (IDS_STATUSFORMAT, szStatusFormat) ;
  215.    StringLoad (IDS_STATUSFORMAT2, szStatusFormat2) ;
  216.  
  217.    StatusLineReady (hWnd) ;
  218.    }  // OnCreate
  219.  
  220. void static OnDestroy (HWND hWnd)
  221.    {
  222.    if (hBitmapAlertStatus)
  223.       {
  224.       DeleteObject (hBitmapAlertStatus) ;
  225.       hBitmapAlertStatus = 0 ;
  226.       }
  227.  
  228.    if (hBitmapLogStatus)
  229.       {
  230.       DeleteObject (hBitmapLogStatus) ;
  231.       hBitmapLogStatus = 0 ;
  232.       }
  233.    ReleaseDC (hWnd, hStatusDC) ;
  234.  
  235.    }  // OnDestroy
  236.  
  237. void static PaintStatusBar (HWND hWnd, HDC hDC, int PaintOptions)
  238. /*
  239.    Effect:        Paint the invalid surface of hWnd. Draw each label, each
  240.                   recessed value box, and each value.
  241.  
  242.    Called By:     StatusWndProc only, in response to a WM_PAINT message.
  243. */
  244.    {
  245.    RECT           rectClient ;
  246.  
  247.    if (bPerfmonIconic)
  248.       {
  249.       // no need to draw anything if iconic
  250.       return ;
  251.       }
  252.  
  253.    GetClientRect (hWnd, &rectClient) ;
  254.  
  255.    RectContract (&rectClient, StatusTopMargin (), StatusLeftMargin ()) ;
  256.    
  257.    if (PaintOptions == PaintAll)
  258.       {
  259.       ThreeDConcave1 (hDC,
  260.                      rectClient.left, rectClient.top,
  261.                      rectClient.right, rectClient.bottom) ;
  262.       }
  263.  
  264.    rectClient.left += StatusLeftMargin () ;
  265.  
  266.    // Always draw the icons and need to draw log before Alerts!
  267.    DrawLog (hDC, &rectClient) ;
  268.    DrawAlerts (hDC, &rectClient) ;
  269.  
  270.    if (PaintOptions & PaintText)
  271.       {
  272.       rectClient.left += 1 ;
  273.       rectClient.top += 1 ;
  274.       rectClient.right -= 1 ;
  275.       rectClient.bottom -= 1 ;
  276.       ExtTextOut (hDC, rectClient.left, rectClient.top, ETO_CLIPPED | ETO_OPAQUE,
  277.          &rectClient, szStatusLine, szStatusLineLen, NULL) ;
  278.       }
  279.  
  280.    }  // PaintStatusBar
  281.  
  282.  
  283.  
  284.  
  285. LRESULT APIENTRY StatusWndProc (HWND hWnd,
  286.                                 WORD wMsg,
  287.                                 WPARAM wParam,
  288.                                 LPARAM lParam)
  289.    {  // StatusWndProc
  290.    BOOL           bCallDefProc ;
  291.    LRESULT        lReturnValue ;
  292.    HDC            hDC ;
  293.    PAINTSTRUCT    ps ;
  294.  
  295.  
  296.    bCallDefProc = FALSE ;
  297.    lReturnValue = 0L ;
  298.  
  299.    switch (wMsg)
  300.       {  // switch
  301.       case WM_PAINT:
  302.          hDC = BeginPaint (hWnd, &ps) ;
  303.          PaintStatusBar (hWnd, hDC, PaintAll) ;
  304.          EndPaint (hWnd, &ps) ;
  305.          break ;
  306.  
  307.       case WM_CREATE:
  308.          OnCreate (hWnd) ;
  309.          break ;
  310.  
  311.       case WM_DESTROY:
  312.          OnDestroy (hWnd) ;
  313.          break ;
  314.  
  315.       default:
  316.          bCallDefProc = TRUE ;
  317.       }  // switch
  318.  
  319.  
  320.    if (bCallDefProc)
  321.       lReturnValue = DefWindowProc (hWnd, wMsg, wParam, lParam) ;
  322.  
  323.    return (lReturnValue);
  324.    }  // StatusWndProc
  325.  
  326.  
  327. int StatusHeight (HWND hWnd)
  328. /*
  329.    Effect:        A status window has a preferred height, based on the font
  330.                   used in its display. Return the preferred height, determined
  331.                   when the window was created.
  332.  
  333.    Assert:        OnCreate has already been called, and it set 
  334.                   StatusData.yHeight.
  335. */  
  336.    {
  337.    return (yStatusHeight) ;
  338.    }
  339.  
  340.  
  341.  
  342.  
  343. HWND CreatePMStatusWindow (HWND hWnd)
  344.    {
  345.    return (CreateWindow (szStatusClass,       // class
  346.                          NULL,                // caption
  347.                          dwStatusWindowStyle, // window style
  348.                          0, 0,                // position
  349.                          0, 0,                // size
  350.                          hWnd,                // parent window
  351.                          NULL,                // menu
  352.                          hInstance,           // program instance
  353.                          NULL)) ;             // user-supplied data
  354.    }  // CreateStatusWindow
  355.  
  356.  
  357.  
  358.  
  359. BOOL StatusInitializeApplication (void)
  360. /*
  361.    Called By:     InitializeApplication only
  362. */
  363.    {
  364.    WNDCLASS       wc ;
  365.  
  366.    wc.style          = dwStatusClassStyle ;
  367.    wc.lpfnWndProc    = (WNDPROC) StatusWndProc ;
  368.    wc.hInstance      = hInstance ;
  369.    wc.cbClsExtra     = iStatusClassExtra ;
  370.    wc.cbWndExtra     = iStatusWindowExtra ;
  371.    wc.hIcon          = NULL ;
  372.    wc.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
  373.    wc.hbrBackground  = hbLightGray ;
  374.    wc.lpszMenuName   = NULL ;
  375.    wc.lpszClassName  = szStatusClass ;
  376.  
  377.    return (RegisterClass (&wc)) ;
  378.    }
  379.    
  380. BOOL _cdecl StatusLine (HWND hWnd,
  381.                         WORD wStringID, ...)
  382.    {
  383.    TCHAR          szFormat [MessageLen] ;
  384.    va_list        vaList ;
  385.  
  386.    if (wStringID == 0)
  387.       {
  388.       return (TRUE) ;
  389.       }
  390.  
  391.    strclr (szStatusLine) ;
  392.  
  393.    if (LoadString (hInstance, wStringID, szFormat, MessageLen))
  394.       {
  395.       va_start (vaList, wStringID) ;
  396.       TSPRINTF (szStatusLine, szFormat, vaList) ;
  397. //      wvsprintf (szStatusLine, szFormat, vaList) ;
  398.       va_end (vaList) ;
  399.       dwCurrentMenuID = MenuIDToHelpID (wStringID) ;
  400.       szStatusLineLen = lstrlen (szStatusLine) ;
  401.       }
  402.    else
  403.       {
  404.       dwCurrentMenuID = 0 ;
  405.       szStatusLineLen = 0 ;
  406.       }
  407.    PaintStatusBar (hWndStatus, hStatusDC, PaintText + PaintIcons) ;
  408.  
  409.    return (TRUE) ;
  410.    }  // StatusLine
  411.  
  412.  
  413.  
  414. void StatusLineReady (HWND hWnd)
  415.    {
  416.    int         stringLen ;
  417.    LPTSTR      pFileName = NULL ;
  418.  
  419.    TSPRINTF (szStatusLine, szStatusFormat, 
  420.              PlayingBackLog () ? 
  421.                PlaybackLog.szFileTitle : szCurrentActivity) ;
  422.  
  423.    switch (iPerfmonView)
  424.       {
  425.       case IDM_VIEWCHART:
  426.          pFileName = pChartFileName ;
  427.          break ;
  428.  
  429.       case IDM_VIEWALERT:
  430.          pFileName = pAlertFileName ;
  431.          break ;
  432.  
  433.       case IDM_VIEWLOG:
  434.          pFileName = pLogFileName ;
  435.          break ;
  436.  
  437.       case IDM_VIEWREPORT:
  438.          pFileName = pReportFileName ;
  439.          break ;
  440.       }
  441.  
  442.    if (pFileName)
  443.       {
  444.       stringLen = lstrlen (szStatusLine) ;
  445.       TSPRINTF (&szStatusLine[stringLen], szStatusFormat2, pFileName) ;
  446.       }
  447.  
  448.    szStatusLineLen = lstrlen (szStatusLine) ;
  449.  
  450.    PaintStatusBar (hWndStatus, hStatusDC, PaintText + PaintIcons) ;
  451.    }
  452.  
  453.  
  454. void StatusUpdateIcons (HWND hWndStatus)
  455.    {  
  456.    PaintStatusBar (hWndStatus, hStatusDC, PaintIcons) ;
  457.    }
  458.  
  459.