toolbar.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 14k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // TOOLBAR.CPP
  3. //
  4. // Implementation of CToolbarWindow
  5. //
  6. // History:
  7. //
  8. // Author   Date        Description
  9. // ------   ----        -----------
  10. // jaym     05/15/97    Created
  11. // jaym     07/02/97    Updated to new UI
  12. // jaym     07/30/97    Updated to newer UI
  13. /////////////////////////////////////////////////////////////////////////////
  14. #include "precomp.h"
  15. #include "resource.h"
  16. #include "toolbar.h"
  17. extern BOOL g_bPasswordEnabled;
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Design constants
  20. /////////////////////////////////////////////////////////////////////////////
  21. #define ID_PROPERTIES_BUTTON    0x0201
  22. #define ID_CLOSE_BUTTON         0x0202
  23.     // Child window IDs
  24. static const TBBUTTON s_tbbProperties =
  25. {
  26.     IDTB_PROPERTIES,
  27.     IDM_TOOLBAR_PROPERTIES,
  28.     TBSTATE_ENABLED,
  29.     TBSTYLE_BUTTON,
  30.     0L,
  31.     0
  32. };
  33. static const TBBUTTON s_tbbClose =
  34. {
  35.     IDTB_CLOSE,
  36.     IDM_TOOLBAR_CLOSE,
  37.     TBSTATE_ENABLED,
  38.     TBSTYLE_BUTTON,
  39.     0L,
  40.     0
  41. };
  42.     // Toolbar buttons
  43. #define CX_PROPERTIES           27
  44. #define CY_PROPERTIES           27
  45. #define CX_PROPERTIES_INDENT    2
  46. #define CY_PROPERTIES_INDENT    1
  47. #define CX_CLOSE                20
  48. #define CY_CLOSE                18
  49. #define CX_CLOSE_INDENT         2
  50. #define CY_CLOSE_INDENT         1
  51. #define CY_SLIDE_INCREMENT      1
  52.     // Toolbar button tweaks
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CToolbarWindow
  55. /////////////////////////////////////////////////////////////////////////////
  56. CToolbarWindow::CToolbarWindow
  57. (
  58. )
  59. {
  60.     m_pSSWnd            = NULL;
  61.     m_cy                = 0;
  62.     m_hwndProperties    = NULL;
  63.     m_hwndClose         = NULL;
  64. }
  65. CToolbarWindow::~CToolbarWindow
  66. (
  67. )
  68. {
  69.     if  (
  70.         (m_hwndProperties != NULL)
  71.         &&
  72.         IsWindow(m_hwndProperties)
  73.         )
  74.     {
  75.         DestroyWindow(m_hwndProperties);
  76.     }
  77.     if  (
  78.         (m_hwndClose != NULL)
  79.         &&
  80.         IsWindow(m_hwndClose)
  81.         )
  82.     {
  83.         DestroyWindow(m_hwndClose);
  84.     }
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CToolbarWindow::Create
  88. /////////////////////////////////////////////////////////////////////////////
  89. BOOL CToolbarWindow::Create
  90. (
  91.     const RECT &            rect,
  92.     CScreenSaverWindow *    pParentWnd
  93. )
  94. {
  95.     BOOL bResult = FALSE;
  96.     ASSERT(pParentWnd != NULL);
  97.     TraceMsg(TF_ALWAYS, "CToolbarWindow::Create(hwndParent=%x)", (pParentWnd) ? pParentWnd->m_hWnd : 0);
  98.     // Initalize the Common Controls.
  99.     INITCOMMONCONTROLSEX icc;
  100.     icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
  101.     icc.dwICC = ICC_USEREX_CLASSES | ICC_COOL_CLASSES;  // REVIEW: COOL? [jm]
  102.     EVAL(SUCCEEDED(InitCommonControlsEx(&icc)));
  103.     for (;;)
  104.     {
  105.         if (!CWindow::CreateEx( NULL,
  106.                                 0,
  107.                                 WS_POPUP
  108.                                     | WS_CLIPCHILDREN
  109.                                     | WS_CLIPSIBLINGS,
  110.                                 rect,
  111.                                 pParentWnd->m_hWnd,
  112.                                 0))
  113.         {
  114.             break;
  115.         }
  116.         m_cy = (rect.bottom - rect.top);
  117.         // Properties button
  118.         TraceMsg(TF_ALWAYS, "CToolbarWindow::Create(m_hWnd=%x)", m_hWnd);
  119.         if ((m_hwndProperties = CreateWindowEx( WS_EX_TOOLWINDOW,
  120.                                                 TOOLBARCLASSNAME,
  121.                                                 NULL,
  122.                                                 WS_CHILD
  123.                                                     | WS_CLIPCHILDREN
  124.                                                     | WS_CLIPSIBLINGS
  125.                                                     | TBSTYLE_TRANSPARENT
  126.                                                     | TBSTYLE_FLAT
  127.                                                     | TBSTYLE_TOOLTIPS
  128.                                                     | CCS_NORESIZE
  129.                                                     | CCS_NODIVIDER
  130.                                                     | CCS_NOPARENTALIGN,
  131.                                                 CX_PROPERTIES_INDENT,
  132.                                                 CY_PROPERTIES_INDENT,
  133.                                                 CX_PROPERTIES,
  134.                                                 CY_PROPERTIES,
  135.                                                 m_hWnd,
  136.                                                 (HMENU)ID_PROPERTIES_BUTTON,
  137.                                                 _pModule->GetModuleInstance(),
  138.                                                 NULL)) == NULL)
  139.         {
  140.             break;
  141.         }
  142.         // #63544: Toolbox doesn't appear when mouse is moved, NT only
  143.         ::SendMessage(m_hwndProperties, TB_GETTOOLTIPS, 0, 0);
  144.         // Set the default toolbar properties button image.
  145.         HIMAGELIST hImgList;
  146.         hImgList = ImageList_LoadImage( _pModule->GetModuleInstance(),
  147.                                         MAKEINTRESOURCE(IDB_PROPERTIES_DEFAULT),
  148.                                         CX_PROPERTIES_BUTTONBITMAP,
  149.                                         0,
  150.                                         RGB(255, 0, 255),
  151.                                         IMAGE_BITMAP,
  152.                                         LR_CREATEDIBSECTION);
  153.         ImageList_SetBkColor(hImgList, RGB(0, 0, 0));
  154.         ::SendMessage(m_hwndProperties, TB_SETIMAGELIST, 0, (LPARAM)hImgList);
  155.         // Set the hot (rollover) toolbar properties button image.
  156.         hImgList = ImageList_LoadImage( _pModule->GetModuleInstance(),
  157.                                         MAKEINTRESOURCE(IDB_PROPERTIES),
  158.                                         CX_PROPERTIES_BUTTONBITMAP,
  159.                                         0,
  160.                                         RGB(255, 0, 255),
  161.                                         IMAGE_BITMAP,
  162.                                         LR_CREATEDIBSECTION);
  163.         ImageList_SetBkColor(hImgList, RGB(0, 0, 0));
  164.         ::SendMessage(m_hwndProperties, TB_SETHOTIMAGELIST, 0, (LPARAM)hImgList);
  165.         // Add the button to the toolbar.
  166.         ::SendMessage(m_hwndProperties, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
  167.         ::SendMessage(m_hwndProperties, TB_ADDBUTTONS, 1, (LPARAM)(LPCTBBUTTON)&s_tbbProperties);
  168.         // Close button
  169.         if ((m_hwndClose = CreateWindowEx(  WS_EX_TOOLWINDOW,
  170.                                             TOOLBARCLASSNAME,
  171.                                             NULL,
  172.                                             WS_CHILD
  173.                                                 | WS_CLIPCHILDREN
  174.                                                 | WS_CLIPSIBLINGS
  175.                                                 | TBSTYLE_TRANSPARENT
  176.                                                 | TBSTYLE_FLAT
  177.                                                 | TBSTYLE_TOOLTIPS
  178.                                                 | CCS_NORESIZE
  179.                                                 | CCS_NODIVIDER
  180.                                                 | CCS_NOPARENTALIGN,
  181.                                             CX_PROPERTIES_INDENT + CX_PROPERTIES + CX_CLOSE_INDENT,
  182.                                             CY_CLOSE_INDENT,
  183.                                             CX_CLOSE,
  184.                                             CY_CLOSE,
  185.                                             m_hWnd,
  186.                                             (HMENU)ID_CLOSE_BUTTON,
  187.                                             _pModule->GetModuleInstance(),
  188.                                             NULL)) == NULL)
  189.         {
  190.             break;
  191.         }
  192.         // #63544: Toolbox doesn't appear when mouse is moved, NT only
  193.         ::SendMessage(m_hwndClose, TB_GETTOOLTIPS, 0, 0);
  194.         // Set the hot (rollover) toolbar close button image.
  195. #if 0
  196.         hImgList = ImageList_LoadImage( _pModule->GetModuleInstance(),
  197.                                         MAKEINTRESOURCE(IDB_CLOSE),
  198.                                         CX_CLOSE_BUTTONBITMAP,
  199.                                         0,
  200.                                         RGB(255, 0, 255),
  201.                                         IMAGE_BITMAP,
  202.                                         LR_CREATEDIBSECTION);
  203.         ImageList_SetBkColor(hImgList, RGB(0, 0, 0)); 
  204.         
  205. #endif
  206.         // #65186: button image is invisible with High Contrast Black scheme
  207.     if ((hImgList = ImageList_Create(CX_CLOSE_BUTTONBITMAP,
  208.                                          CY_CLOSE_BUTTONBITMAP,
  209.                                          ILC_COLOR | ILC_MASK,
  210.                                          1, 0)) == NULL)
  211.     {
  212.         break;
  213.         }
  214.         
  215.         HBITMAP hBitmap;
  216.         if ((hBitmap = (HBITMAP)CreateMappedBitmap(_pModule->GetModuleInstance(),
  217.                                                 IDB_CLOSE,
  218.                                                 0,
  219.                                                 NULL,
  220.                                                 0)) != NULL)
  221.         {
  222.             EVAL(ImageList_AddMasked(hImgList, hBitmap, CLR_DEFAULT) != -1);
  223.             DeleteObject(hBitmap);
  224.         }
  225.         else
  226.             break;
  227.         ::SendMessage(m_hwndClose, TB_SETIMAGELIST, 0, (LPARAM)hImgList);
  228.         // Add the button to the toolbar.
  229.         ::SendMessage(m_hwndClose, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
  230.         ::SendMessage(m_hwndClose, TB_ADDBUTTONS, 1, (LPARAM)(LPCTBBUTTON)&s_tbbClose);
  231.         //
  232.         // Disable properties if not interactive or screensaver is password 
  233.         // protected
  234.         //
  235.         if (g_bPasswordEnabled || !pParentWnd->UserIsInteractive())
  236.             ::SendMessage(m_hwndProperties, TB_SETSTATE, IDM_TOOLBAR_PROPERTIES, 0);
  237.         m_pSSWnd = pParentWnd;
  238.         bResult = TRUE;
  239.         break;
  240.     }
  241.     return bResult;
  242. }
  243. /////////////////////////////////////////////////////////////////////////////
  244. // CToolbarWindow::OnCommand
  245. /////////////////////////////////////////////////////////////////////////////
  246. BOOL CToolbarWindow::OnCommand
  247. (
  248.     WPARAM wParam,
  249.     LPARAM lParam
  250. )
  251. {
  252.     switch (wParam)
  253.     {
  254.         case IDM_TOOLBAR_PROPERTIES:
  255.         {
  256.             m_pSSWnd->ShowPropertiesDlg(m_hWnd);
  257.             break;
  258.         }
  259.         case IDM_TOOLBAR_CLOSE:
  260.         {
  261.             m_pSSWnd->Quit();
  262.             break;
  263.         }
  264.         default:
  265.         {
  266.             ASSERT(FALSE);
  267.             return FALSE;
  268.         }
  269.     }
  270.     return TRUE;
  271. }
  272. /////////////////////////////////////////////////////////////////////////////
  273. // CToolbarWindow::OnEraseBkgnd
  274. /////////////////////////////////////////////////////////////////////////////
  275. BOOL CToolbarWindow::OnEraseBkgnd
  276. (
  277.     HDC hDC
  278. )
  279. {
  280.     TraceMsg(TF_ALWAYS, "CToolbarWindow::OnEraseBkgnd");
  281.     // Erase the background.
  282.     RECT rectClient;
  283.     GetClientRect(&rectClient);
  284.     DrawEdge(   hDC,
  285.                 &rectClient,
  286.                 EDGE_RAISED,
  287.                 BF_BOTTOMLEFT | BF_MIDDLE | BF_SOFT);
  288.                 
  289.     return TRUE;
  290. }
  291. /////////////////////////////////////////////////////////////////////////////
  292. // CToolbarWindow::OnNotify
  293. /////////////////////////////////////////////////////////////////////////////
  294. BOOL CToolbarWindow::OnNotify
  295. (
  296.     WPARAM      wParam,
  297.     LPARAM      lParam,
  298.     LRESULT *   plResult
  299. )
  300. {
  301.     TOOLTIPTEXT * pttt = (TOOLTIPTEXT *)lParam;
  302.     switch (pttt->hdr.code)
  303.     {
  304.         case TTN_NEEDTEXT:
  305.         {
  306.             // Handle ToolTip notification sent by Toolbar.
  307.             pttt->hinst = _pModule->GetResourceInstance();
  308.             pttt->lpszText = MAKEINTRESOURCE(wParam);
  309.             return TRUE;
  310.         }
  311.         default:
  312.             break;
  313.     }
  314.     return FALSE;
  315. }
  316. /////////////////////////////////////////////////////////////////////////////
  317. // CToolbarWindow::ShowToolbar
  318. /////////////////////////////////////////////////////////////////////////////
  319. void CToolbarWindow::ShowToolbar
  320. (
  321.     BOOL bShow
  322. )
  323. {
  324.    TraceMsg(TF_ALWAYS, "CToolbarWindow::ShowToolbar(m_hWnd=%x, hWndParent=%x)", m_hWnd, ::GetParent(m_hWnd));
  325.    if (bShow)
  326.         ShowWindow(SW_SHOW);
  327.     for (;;)
  328.     {
  329.         RECT rect;
  330.         GetWindowRect(&rect);
  331.         if (bShow)
  332.         {
  333.             rect.top += CY_SLIDE_INCREMENT;
  334.             if (rect.top <= 0)
  335.             {
  336.                 SetWindowPos(   NULL,
  337.                                 rect.left, rect.top,
  338.                                 0, 0,
  339.                                 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
  340.                 UpdateWindow(m_hWnd);
  341.                 Sleep(10);
  342.             }
  343.             else
  344.                 break;
  345.         }
  346.         else
  347.         {
  348.             rect.top -= CY_SLIDE_INCREMENT;
  349.             if (rect.top >= -m_cy)
  350.             {
  351.                 SetWindowPos(   NULL,
  352.                                 rect.left, rect.top,
  353.                                 0, 0,
  354.                                 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
  355.                 UpdateWindow(m_hWnd);
  356.                 UpdateWindow(::GetParent(m_hWnd));
  357.                 Sleep(10);
  358.             }
  359.             else
  360.             {
  361.                 ShowWindow(SW_HIDE);
  362.                 break;
  363.             }
  364.         }
  365.     }
  366. }
  367. /////////////////////////////////////////////////////////////////////////////
  368. // CToolbarWindow::OnShowWindow
  369. /////////////////////////////////////////////////////////////////////////////
  370. void CToolbarWindow::OnShowWindow
  371. (
  372.     BOOL    bShow,
  373.     int     nStatus
  374. )
  375. {
  376.     if  (
  377.         (m_hwndProperties != NULL)
  378.         &&
  379.         IsWindow(m_hwndProperties)
  380.         )
  381.     {
  382.         TraceMsg(TF_ALWAYS, "CToolbarWindow::OnShowWindow(Properties)");
  383.         ::ShowWindow(m_hwndProperties, (bShow ? SW_SHOW : SW_HIDE));
  384.     }
  385.     if  (
  386.         (m_hwndClose != NULL)
  387.         &&
  388.         IsWindow(m_hwndClose)
  389.         )
  390.     {
  391.         TraceMsg(TF_ALWAYS, "CToolbarWindow::OnShowWindow(Close)");
  392.         ::ShowWindow(m_hwndClose, (bShow ? SW_SHOW : SW_HIDE));
  393.     }
  394. }