home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / toolbar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-26  |  8.3 KB  |  229 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. /* Toolbar.c
  13.  *
  14.  * In the C++ versions of Player the MFC class CToolbar handles all of
  15.  * the toolbar implementation, including creating a 'disabled' bitmap from
  16.  * the toolbar bitmap.
  17.  *
  18.  * In this, the pure C version, of Player we insist on a seperate disabled
  19.  * bitmap being supplied by the programmer. This drastically reduces the
  20.  * amount of code required to maintain the toolbar. The buttons in this
  21.  * version are controls with the owner draw attribute set.
  22.  *
  23.  */
  24.  
  25. #include "stdwin.h"
  26. #include "media.h"
  27. #include "resource.h"
  28.  
  29. // Constants for the bitmap
  30. const int nButtonImageWidth = 32;
  31. const int nButtonImageHeight = 32;
  32.  
  33. // Constants for the toolbar implementation
  34. const int nButtonBorder = 8;
  35. const int nSeperatorGap = 6;
  36. const int nToolbarBorderWidth = 5;
  37. const int nToolbarBorderHeight = 3;
  38.  
  39. // The window for each button
  40. struct{
  41.     HWND hwndPlayButton;
  42.     HWND hwndPauseButton;
  43.     HWND hwndStopButton;
  44. } toolbar;
  45.  
  46. void CalcRequiredSize( SIZE *pSize ){
  47.     // Calculate the area required for this toolbar
  48.  
  49.     // size for 3 buttons, 2 borders and a seperator
  50.     // ...but we'll add on a some extra seperators for good measure
  51.     pSize->cx = (nButtonImageWidth + nButtonBorder) * 3
  52.                     + nToolbarBorderWidth * 2 + nSeperatorGap*5;
  53.  
  54.     // size for 1 button and 2 borders
  55.     pSize->cy = nButtonImageHeight + nButtonBorder
  56.                     + nToolbarBorderHeight * 2;
  57.  
  58.  
  59. }
  60.  
  61. void UpdateToolbar( void ){
  62. // Maintains the enabled/disabled state of the buttons
  63. // Must be called periodically and/or whenever there is a change of state.
  64.     EnableWindow( toolbar.hwndPlayButton, CanPlay() );
  65.     EnableWindow( toolbar.hwndPauseButton, CanPause() );
  66.     EnableWindow( toolbar.hwndStopButton, CanStop() );
  67.  
  68. }
  69.  
  70.  
  71.  
  72. BOOL InitToolbar( HINSTANCE hInstance, HWND hwnd ){
  73. /* Create the controls for the buttons */
  74.     int x;                              // Position of the next button
  75.  
  76.     x = nToolbarBorderWidth;
  77.  
  78.     // the 'Play' button
  79.     toolbar.hwndPlayButton = CreateWindow( "BUTTON",
  80.                                            NULL,
  81.                                            BS_OWNERDRAW | WS_VISIBLE | WS_CHILD,
  82.                                            x,
  83.                                            nToolbarBorderHeight,
  84.                                            nButtonImageWidth + nButtonBorder,
  85.                                            nButtonImageHeight + nButtonBorder,
  86.                                            hwnd,
  87.                                            (HMENU) ID_MEDIA_PLAY,
  88.                                            hInstance,
  89.                                            NULL);
  90.  
  91.     x += nButtonImageWidth + nButtonBorder;
  92.  
  93.     // the 'Pause' button
  94.     toolbar.hwndPauseButton = CreateWindow( "BUTTON",
  95.                                             NULL,
  96.                                             BS_OWNERDRAW | WS_VISIBLE | WS_CHILD,
  97.                                             x,
  98.                                             nToolbarBorderHeight,
  99.                                             nButtonImageWidth + nButtonBorder,
  100.                                             nButtonImageHeight + nButtonBorder,
  101.                                             hwnd,
  102.                                             (HMENU) ID_MEDIA_PAUSE,
  103.                                             hInstance,
  104.                                             NULL);
  105.  
  106.     x += nButtonImageWidth + nButtonBorder + nSeperatorGap;
  107.  
  108.     // the 'Stop' button
  109.     toolbar.hwndStopButton = CreateWindow( "BUTTON",
  110.                                            NULL,
  111.                                            BS_OWNERDRAW | WS_VISIBLE | WS_CHILD,
  112.                                            x,
  113.                                            nToolbarBorderHeight,
  114.                                            nButtonImageWidth + nButtonBorder,
  115.                                            nButtonImageHeight + nButtonBorder,
  116.                                            hwnd,
  117.                                            (HMENU) ID_MEDIA_STOP,
  118.                                            hInstance,
  119.                                            NULL);
  120.  
  121.     // We don't call UpdateToolbar to set the button states as
  122.     // as the multimedia variables may not have been initialized yet.
  123.     return TRUE;
  124.  
  125. }
  126.  
  127. void DrawRect( HDC hDC, int left, int top, int right, int bottom, UINT nStockObject ){
  128. /* Draws a filled rectangle using the given stock object.
  129.  * The rectangle starts at left,top and *includes* right,bottom
  130.  */
  131.     RECT rect;
  132.  
  133.     rect.left = left;
  134.     rect.top = top;
  135.     rect.bottom = bottom+1;
  136.     rect.right = right+1;
  137.  
  138.     FillRect( hDC, &rect, GetStockObject( nStockObject ) );
  139. }
  140.  
  141. void DrawButton( HINSTANCE hInstance, DRAWITEMSTRUCT FAR * lpDrawItem ){
  142. /* Called by the main window whenever a button needs to be redrawn */
  143.     HDC hSourceDC = CreateCompatibleDC( NULL );
  144.     HGDIOBJ hgdiOldBitmap;
  145.     int nIndex;
  146.     UINT nUpperBrush, nLowerBrush;
  147.  
  148.     int lFrame = lpDrawItem->rcItem.left;
  149.     int tFrame = lpDrawItem->rcItem.top;
  150.     int rFrame = lpDrawItem->rcItem.right -1;
  151.     int bFrame = lpDrawItem->rcItem.bottom - 1;
  152.  
  153.     // Draw a black, rounded frame around the bottom
  154.     // top & bottom lines
  155.     DrawRect( lpDrawItem->hDC, lFrame+1, tFrame, rFrame-1, tFrame, BLACK_BRUSH );
  156.     DrawRect( lpDrawItem->hDC, lFrame+1, bFrame, rFrame-1, bFrame, BLACK_BRUSH );
  157.  
  158.     // left & right lines
  159.     DrawRect( lpDrawItem->hDC, lFrame, tFrame+1, lFrame, bFrame-1, BLACK_BRUSH );
  160.     DrawRect( lpDrawItem->hDC, rFrame, tFrame+1, rFrame, bFrame-1, BLACK_BRUSH );
  161.  
  162.     // Adjust the pointers to point to the inside of the rectangle
  163.     lFrame++; rFrame--;
  164.     tFrame++; bFrame--;
  165.  
  166.     /* The left and top will be highlighted and the right and bottom will
  167.      * be in shadow if the button is raised (ie, not selected).
  168.      * Otherwise the left and top will be in shadow and the right and bottom
  169.      * will be their normal light gray
  170.      */
  171.     if( lpDrawItem->itemState & ODS_SELECTED ){
  172.         nUpperBrush = GRAY_BRUSH;
  173.         nLowerBrush = LTGRAY_BRUSH;
  174.     } else {
  175.         nUpperBrush = WHITE_BRUSH;
  176.         nLowerBrush = GRAY_BRUSH;
  177.     }
  178.  
  179.     // Draw top & left border highlight/shadow
  180.     DrawRect( lpDrawItem->hDC, lFrame, tFrame, rFrame, tFrame, nUpperBrush );
  181.     DrawRect( lpDrawItem->hDC, lFrame, tFrame, lFrame, bFrame, nUpperBrush );
  182.  
  183.     // Draw bottom & right border highlight/shadow
  184.     DrawRect( lpDrawItem->hDC, rFrame, tFrame+1, rFrame, bFrame, nLowerBrush );
  185.     DrawRect( lpDrawItem->hDC, lFrame+1, bFrame, rFrame, bFrame, nLowerBrush );
  186.  
  187.     // Load the IDR_DISABLED_MAINFRAME bitmap into our source hDC if the
  188.     // button is disabled otherwise load the IDR_MAINFRAME bitmap
  189.     if( lpDrawItem->itemState & ODS_DISABLED )
  190.         hgdiOldBitmap = SelectObject( hSourceDC,
  191.             (HGDIOBJ) LoadBitmap( hInstance, MAKEINTRESOURCE( IDR_DISABLED_MAINFRAME ) ));
  192.     else
  193.         hgdiOldBitmap =
  194.         hgdiOldBitmap = SelectObject( hSourceDC,
  195.             (HGDIOBJ) LoadBitmap( hInstance, MAKEINTRESOURCE( IDR_MAINFRAME ) ));
  196.  
  197.     // Decide which button to blit to the display
  198.     switch( lpDrawItem->CtlID ){
  199.         case ID_MEDIA_PLAY:
  200.             nIndex = 0;
  201.             break;
  202.  
  203.         case ID_MEDIA_PAUSE:
  204.             nIndex = 1;
  205.             break;
  206.  
  207.         case ID_MEDIA_STOP:
  208.             nIndex = 2;
  209.             break;
  210.     }
  211.  
  212.     // ..and blit it
  213.     BitBlt( lpDrawItem->hDC,
  214.             lpDrawItem->rcItem.left + nButtonBorder/2,
  215.             lpDrawItem->rcItem.top  + nButtonBorder/2,
  216.             nButtonImageWidth,
  217.             nButtonImageHeight,
  218.             hSourceDC,
  219.             nIndex * nButtonImageWidth,
  220.             0,
  221.             SRCCOPY
  222.           );
  223.  
  224.     // restore the original bitmap
  225.     SelectObject( hSourceDC, hgdiOldBitmap );
  226.  
  227. }
  228.  
  229.