home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / audio / midiplyr / uiutils.c < prev   
C/C++ Source or Header  |  1997-10-05  |  9KB  |  270 lines

  1. /*****************************************************************************
  2. *
  3. *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4. *  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  5. *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR
  6. *  A PARTICULAR PURPOSE.
  7. *
  8. *  Copyright (C) 1993 - 1997 Microsoft Corporation. All Rights Reserved.
  9. *
  10. ******************************************************************************
  11. *
  12. * UiUtils.C
  13. *
  14. * UI utility routines
  15. *
  16. *****************************************************************************/
  17.  
  18. #pragma warning(disable:4756)
  19.  
  20. #define _INC_SHELLAPI
  21. #include <windows.h>
  22. #undef _INC_SHELLAPI
  23.  
  24. #include <shellapi.h>
  25. #include <windowsx.h>
  26. #include <mmsystem.h>
  27. #include <commdlg.h>
  28. #include <commctrl.h>
  29. #include <ctype.h>
  30.  
  31. #include "debug.h"
  32.  
  33. #include "MIDIPlyr.H"
  34.  
  35. static char BCODE           gszErrFmt[]             = "%s:\n%s";
  36. static char BCODE           gszFace[]               = "arial";
  37. static char                 gszErrDescTxt[256];
  38. static char                 gszErrCodeTxt[256];
  39. static char                 gszErrStr[512];
  40.  
  41. /*****************************************************************************
  42. *
  43. * Error
  44. *
  45. * Puts up a general error dialog
  46. *
  47. * hWnd                      - Handle of the owner window
  48. * nErrDesc                  - id into the string table of the message
  49. * mmrc                      - Return code from MMSYSTEM or lower layer
  50. *                             which caused the error.
  51. *
  52. * Just formats and puts up an error dialog.
  53. *
  54. * For convenience, all of the sequencer functions return MMSYSERR_ or
  55. * MCIERR_ codes, so we can use mciGetErrorString instead of inventing
  56. * our own error messages again.
  57. *
  58. *****************************************************************************/
  59. VOID FNLOCAL Error(
  60.     HWND                    hWnd,                             
  61.     int                     nErrDesc,
  62.     MMRESULT                mmrc)
  63. {
  64.     LoadString(ghInst, nErrDesc, gszErrDescTxt, sizeof(gszErrDescTxt));
  65.     mciGetErrorString(mmrc, gszErrCodeTxt, sizeof(gszErrCodeTxt));
  66.     
  67.     wsprintf(gszErrStr, gszErrFmt, (LPSTR)gszErrDescTxt, (LPSTR)gszErrCodeTxt);
  68.     MessageBox(hWnd, gszErrStr, gszAppLongName, MB_ICONEXCLAMATION|MB_OK);
  69. }
  70.  
  71. /*****************************************************************************
  72. *
  73. * MessagePump
  74. *
  75. * Process messages
  76. *
  77. * This function is called when, in certain cases, we need to wait for a
  78. * window callback to complete. Since callbacks are posted, not sent, we
  79. * need to process messages while waiting.
  80. *
  81. * Note that some documentation refers to this operation as a 'directed
  82. * yield' if messages are being processed for a particular window. This
  83. * is misleading; Yield() merely allows other tasks in the system to run
  84. * and does absolutely nothing to the message queue.
  85. *
  86. *****************************************************************************/
  87. VOID NEAR PASCAL MessagePump(
  88.     VOID)
  89. {
  90.     MSG                     msg;
  91.  
  92.     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  93.     {
  94.         TranslateMessage(&msg);
  95.         DispatchMessage(&msg); 
  96.     }
  97. }
  98.  
  99. /*****************************************************************************
  100. *
  101. * EmbossedTextOut
  102. *
  103. * Draw embossed text in the given device context
  104. *
  105. * hDC                       - hDC to draw in
  106. * x, y                      - Upper left corner of text
  107. * lpsz                      - Pointer to the text
  108. * cb                        - Length of text
  109. * crText                    - Color for text face
  110. * crShadow                  - Color for text shadow
  111. * cx, cy                    - Offset for shadow
  112. *
  113. * The text will be drawn with the currently selected font.
  114. *
  115. * If cb == -1, the lstrlen(lpsz) will be used.
  116. *
  117. * If crText == -1, COLOR_BTNTEXT will be used.
  118. *
  119. * If crShadow == -1, COLOR_BTNSHADOW will be used.
  120. *
  121. *****************************************************************************/
  122. VOID NEAR PASCAL EmbossedTextOut(
  123.     HDC                     hDC,
  124.     int                     x,
  125.     int                     y,
  126.     LPSTR                   lpsz,
  127.     UINT                    cb,
  128.     COLORREF                crText,
  129.     COLORREF                crShadow,
  130.     int                     cx,
  131.     int                     cy)
  132. {
  133.     COLORREF                crOld;
  134.     UINT                    uMode;
  135.     SIZE                    sizeText;
  136.     RECT                    rcText;
  137.  
  138.     /* If text length is -1, use lstrlen to get the length
  139.     ** of the text.
  140.     */
  141.     if (cb == -1)
  142.         cb = lstrlen(lpsz);
  143.  
  144.     /* If the shadow or text color is -1, use the
  145.     ** system color for that one.
  146.     */
  147.     if (crShadow == (COLORREF)-1)
  148.         crShadow = GetSysColor (COLOR_BTNSHADOW);
  149.     if (crText == (COLORREF)-1)
  150.         crText = GetSysColor (COLOR_BTNTEXT);
  151.  
  152.     /* setup the DC, saving off the old values
  153.     */
  154.     uMode = SetBkMode(hDC, OPAQUE);
  155.     crOld = SetTextColor(hDC, crShadow);
  156.  
  157.     /* Draw the text at the desired offset using the
  158.     ** shadow color, then again at the normal position
  159.     ** using the text color.  This will the text an 'Embossed'
  160.     ** or 'drop shadowed' look depending on what shadow color
  161.     ** and offset are used.
  162.     */
  163.     GetTextExtentPoint32(hDC, lpsz, cb, &sizeText);
  164.     rcText.left   = x;    rcText.right  = x+cx+sizeText.cx; 
  165.     rcText.top    = y;    rcText.bottom = y+cy+sizeText.cy; 
  166.     ExtTextOut(hDC, x+cx, y+cy, ETO_OPAQUE, &rcText, lpsz, cb, NULL);
  167.     SetBkMode(hDC, TRANSPARENT);
  168.     SetTextColor(hDC, crText);
  169.     ExtTextOut(hDC, x, y, 0, NULL, lpsz, cb, NULL);
  170.  
  171.     /* restore the DC
  172.     */
  173.     SetTextColor(hDC, crOld);
  174.     SetBkMode(hDC, uMode);
  175. }
  176.  
  177. /*****************************************************************************
  178. *
  179. * CreateScaledFont
  180. *
  181. * Create a font scaled so that the given string will fit in the given
  182. * rect, but be as large as possible while maintaining correct aspect ratio.
  183. *
  184. * hDC                       - DC to calculate font for
  185. * lpRect                    - Rectangle to fit text into
  186. * lpszFormat                - Format string to fit into rect
  187. * anPosX[]                  - Will contain the X coordinates for each char
  188. * anPosY                    - Will contain the Y coordinate for the string
  189. *
  190. * Returns HFONT or NULL if one could not be created 
  191. *
  192. *****************************************************************************/
  193. HFONT NEAR PASCAL CreateScaledFont(
  194.     HDC                     hDC,
  195.     LPRECT                  lpRect,
  196.     LPSTR                   lpszFormat,
  197.     int                     anPosX[],
  198.     int*                    nPosY)                                                        
  199. {
  200.     LOGFONT                 lf;
  201.     HFONT                   hFont;
  202.     HFONT                   h;
  203.     LONG                    FormatWidth;
  204.     LONG                    ScaledClientWidth;
  205.     LONG                    ScaledClientHeight;
  206.     LONG                    AspectN;
  207.     LONG                    AspectD;
  208.     int                     nPosX;
  209.     UINT                    cb;
  210.     UINT                    ii;
  211.     UINT                    jj;
  212.     SIZE                    size;
  213.  
  214.     ScaledClientHeight =  ((lpRect->bottom - lpRect->top)) * 3 / 4;
  215.     ScaledClientWidth  =  ((lpRect->right  - lpRect->left)) * 3 / 4;
  216.     
  217.     _fmemset(&lf, 0, sizeof(lf));
  218.     lf.lfHeight         = -(int)ScaledClientHeight;
  219.     lf.lfWeight         = FW_BOLD;
  220.     lf.lfCharSet        = ANSI_CHARSET;
  221.     lf.lfClipPrecision  = CLIP_DEFAULT_PRECIS;
  222.     lf.lfQuality        = PROOF_QUALITY;
  223.     lf.lfPitchAndFamily = FF_ROMAN|DEFAULT_PITCH;
  224.     lstrcpy(lf.lfFaceName, gszFace);
  225.     
  226.     hFont = CreateFontIndirect(&lf);
  227.     h = SelectObject(hDC, hFont);
  228.  
  229.     cb = lstrlen(lpszFormat);
  230.     GetTextExtentPoint(hDC, lpszFormat, cb, &size);
  231.  
  232.     AspectN = (LONG)size.cx;
  233.     AspectD = (LONG)size.cy;
  234.     
  235.     FormatWidth = (ScaledClientHeight*AspectN)/AspectD;
  236.  
  237.     if (FormatWidth > ScaledClientWidth)
  238.     {
  239.         ScaledClientHeight =
  240.             (ScaledClientWidth*AspectD)/AspectN;
  241.         SelectObject(hDC, h);
  242.         DeleteObject(hFont);
  243.         
  244.         lf.lfHeight = -(int)ScaledClientHeight;
  245.  
  246.         hFont = CreateFontIndirect(&lf);
  247.  
  248.         SelectObject(hDC, hFont);
  249.         GetTextExtentPoint(hDC, lpszFormat, cb, &size);
  250.     }
  251.     
  252.     *nPosY  = grcTWnd.top  + (grcTWnd.bottom- grcTWnd.top  - size.cy)/2;
  253.     nPosX   = grcTWnd.left + (grcTWnd.right - grcTWnd.left - size.cx)/2;
  254.  
  255.     ii = 0;
  256.     for (jj=0; jj < cb; jj++)
  257.     {
  258.         if (jj != 0)
  259.             GetTextExtentPoint(hDC, lpszFormat, jj, &size);
  260.         else
  261.             size.cx = 0;
  262.         
  263.         anPosX[ii++] = nPosX + size.cx;
  264.     }
  265.  
  266.     SelectObject(hDC, h);
  267.  
  268.     return hFont;
  269. }
  270.