home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / CTRLBARS / PALETTE.CP_ / PALETTE.CP
Encoding:
Text File  |  1993-02-08  |  6.5 KB  |  256 lines

  1. // palette.cpp : implementation of the Floating tool palette class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "ctrlbars.h"
  15.  
  16. #include "palette.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #define CYCAPTION 9     /* height of the caption */
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CMainFrame
  27.  
  28. BEGIN_MESSAGE_MAP(CPaletteBar, CToolBar)
  29.     //{{AFX_MSG_MAP(CPaletteBar)
  30.     ON_WM_LBUTTONDOWN()
  31.     ON_WM_MOUSEMOVE()
  32.     ON_WM_LBUTTONUP()
  33.     ON_WM_MOUSEACTIVATE()
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CPaletteBar construction/destruction
  39.  
  40. CPaletteBar::CPaletteBar()
  41. {
  42.     m_bTrackMove = FALSE;
  43.     m_nColumns = 2;
  44.     m_cyTopBorder = CYCAPTION+2;
  45.     m_cxLeftBorder = 3;
  46.     m_cxRightBorder = 3;
  47.     m_cyBottomBorder = 3;
  48. }
  49.  
  50. CPaletteBar::~CPaletteBar()
  51. {
  52. }
  53.  
  54. void CPaletteBar::SetSizes(SIZE sizeButton, SIZE sizeImage, UINT nColumns)
  55. {
  56.     m_nColumns = nColumns;
  57.     RecalcLayout(m_nCount);
  58.     CToolBar::SetSizes(sizeButton, sizeImage);
  59. }
  60.  
  61. BOOL CPaletteBar::SetButtons(const UINT FAR* lpIDArray,
  62.     int nIDCount, UINT nColumns)
  63. {
  64.     m_nColumns = nColumns;
  65.     RecalcLayout(nIDCount);
  66.     return CToolBar::SetButtons(lpIDArray, nIDCount);
  67. }
  68.     
  69. BOOL CPaletteBar::Create(CWnd* pOwnerWnd, int x, int y)
  70. {
  71.     ASSERT(pOwnerWnd != NULL);
  72.     m_pOwnerWnd = pOwnerWnd;
  73.  
  74.     return CreateEx(0, "AfxControlBar", NULL, WS_POPUP,
  75.         x, y, 0, 0, pOwnerWnd->GetSafeHwnd(), NULL, NULL);
  76. }
  77.  
  78. void CPaletteBar::RecalcLayout(UINT nButtonCount)
  79. {
  80.     SetWindowPos(NULL, 0, 0,
  81.         m_cxLeftBorder + (m_sizeButton.cx-1) * m_nColumns + m_cxRightBorder + 1,
  82.         m_cyTopBorder + m_cyBottomBorder + (m_sizeButton.cy-1) *
  83.         ((nButtonCount + m_nColumns - 1) / m_nColumns) + 1,
  84.         SWP_NOZORDER|SWP_NOMOVE);
  85.     Invalidate();
  86. }
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CPaletteBar diagnostics
  90.  
  91. #ifdef _DEBUG
  92. void CPaletteBar::AssertValid() const
  93. {
  94.     CToolBar::AssertValid();
  95. }
  96.  
  97. void CPaletteBar::Dump(CDumpContext& dc) const
  98. {
  99.     CToolBar::Dump(dc);
  100. }
  101.  
  102. #endif //_DEBUG
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CPaletteBar message handlers
  106.  
  107. void CPaletteBar::DoPaint(CDC* pDC)
  108. {
  109.     CControlBar::DoPaint(pDC);      // draws any borders
  110.  
  111.     CRect rect;
  112.     GetClientRect(&rect);
  113.  
  114.     // Draw the frame border
  115.     CBrush brBlack;
  116.     brBlack.CreateSolidBrush(::GetSysColor(COLOR_WINDOWFRAME));
  117.     pDC->FrameRect(rect,&brBlack);
  118.     rect.bottom = CYCAPTION;
  119.     pDC->FrameRect(rect,&brBlack);
  120.  
  121.     // Fill in the caption color
  122.     CBrush brCaption;
  123.     brCaption.CreateSolidBrush(::GetSysColor(COLOR_ACTIVECAPTION));
  124.     rect.InflateRect(-1, -1);
  125.     pDC->FillRect(rect, &brCaption);
  126.  
  127.     // We need to initialize the bitmap selection process.
  128.     DrawState ds;
  129.     if (!PrepareDrawButton(ds))
  130.         return;     // something went wrong
  131.  
  132.     GetClientRect(&rect);
  133.     rect.top = m_cyTopBorder;
  134.     rect.bottom = rect.top + m_sizeButton.cy;
  135.  
  136.     // Now draw each visible button
  137.     for (int iButton = 0; iButton < m_nCount; )
  138.     {
  139.         rect.left = m_cxLeftBorder;
  140.         for (UINT nCol = 0; nCol < m_nColumns; nCol++, iButton++)
  141.         {
  142.             rect.right = rect.left + m_sizeButton.cx;
  143.             if (pDC->RectVisible(&rect))
  144.             {
  145.                 UINT nID, nStyle;
  146.                 int iImage;
  147.                 GetButtonInfo(iButton, nID, nStyle, iImage);
  148.                 DrawButton(pDC->m_hDC, rect.left, rect.top,
  149.                     iImage, nStyle);
  150.             }
  151.             rect.left = rect.right - 1; // prepare for overlap
  152.         }
  153.         rect.top = rect.bottom-1;
  154.         rect.bottom = rect.top + m_sizeButton.cy;
  155.     }
  156.  
  157.     EndDrawButton(ds);
  158. }
  159.  
  160. void CPaletteBar::OnLButtonDown(UINT nFlags, CPoint point)
  161. {
  162.     if (point.y <= m_cyTopBorder)
  163.     {
  164.         m_bTrackMove = TRUE;
  165.         m_ptMouse = point;
  166.         SetCapture();
  167.         ClientToScreen(&point);
  168.         InvertTracker(point);
  169.         m_ptLast = point;
  170.     }
  171.     else
  172.         CToolBar::OnLButtonDown(nFlags, point);
  173. }
  174.  
  175. void CPaletteBar::OnMouseMove(UINT nFlags, CPoint point)
  176. {
  177.     if (m_bTrackMove)
  178.     {
  179.         ClientToScreen(&point);
  180.         InvertTracker(m_ptLast);
  181.         m_ptLast = point;
  182.         InvertTracker(m_ptLast);
  183.     }
  184.     else
  185.         CToolBar::OnMouseMove(nFlags, point);
  186. }
  187.  
  188. void CPaletteBar::InvertTracker(CPoint point)
  189. {
  190.     CRect rectFrame;
  191.     GetWindowRect(&rectFrame);
  192.     CDC dc;
  193.     dc.Attach(::GetDC(NULL));
  194.     
  195.     dc.PatBlt(point.x-m_ptMouse.x, point.y-m_ptMouse.y, rectFrame.Width(),
  196.             2, PATINVERT);
  197.     dc.PatBlt(point.x-m_ptMouse.x+rectFrame.Width(), point.y-m_ptMouse.y,
  198.             2, rectFrame.Height(), PATINVERT);
  199.     dc.PatBlt(point.x-m_ptMouse.x, point.y-m_ptMouse.y+rectFrame.Height(),
  200.             rectFrame.Width()+2, 2, PATINVERT);
  201.     dc.PatBlt(point.x-m_ptMouse.x, point.y-m_ptMouse.y+2, 2,
  202.             rectFrame.Height()-2, PATINVERT);
  203.     ::ReleaseDC(NULL,dc.Detach());
  204. }
  205.  
  206. void CPaletteBar::OnLButtonUp(UINT nFlags, CPoint point)
  207. {
  208.     if (m_bTrackMove)
  209.     {
  210.         m_bTrackMove=FALSE;
  211.         ReleaseCapture();
  212.         InvertTracker(m_ptLast);
  213.         ClientToScreen(&point);
  214.         SetWindowPos(NULL, point.x-m_ptMouse.x, point.y-m_ptMouse.y,0,0,
  215.                 SWP_NOZORDER|SWP_NOSIZE);
  216.         ShowWindow(SW_SHOW);
  217.     }
  218.     else
  219.         CToolBar::OnLButtonUp(nFlags, point);
  220. }
  221.  
  222. void CPaletteBar::GetItemRect(int nIndex, LPRECT lpRect) const
  223. {
  224.     ASSERT(nIndex >= 0 && nIndex < m_nCount);
  225.     ASSERT(AfxIsValidAddress(lpRect, sizeof(RECT)));
  226.  
  227.     lpRect->left = m_cxLeftBorder +
  228.             (nIndex - (nIndex / m_nColumns) * m_nColumns) * (m_sizeButton.cx-1);
  229.     lpRect->right = lpRect->left + m_sizeButton.cx;
  230.  
  231.     lpRect->top = m_cyTopBorder + (nIndex / m_nColumns) * (m_sizeButton.cy-1);
  232.     lpRect->bottom = lpRect->top + m_sizeButton.cy;
  233. }
  234.  
  235. int CPaletteBar::HitTest(CPoint point)  // in window relative coords
  236. {
  237.     if (point.x < m_cxLeftBorder ||
  238.             point.x >= (int)(m_cxLeftBorder + m_sizeButton.cx * m_nColumns))
  239.         return -1;      // no X hit
  240.  
  241.     UINT nRows = (m_nCount + m_nColumns - 1) / m_nColumns;
  242.     
  243.     if (point.y < m_cyTopBorder ||
  244.             point.y >= (int)(m_cyTopBorder + m_sizeButton.cy * nRows))
  245.         return -1;      // no Y hit
  246.  
  247.     int iButton = ((point.y - m_cyTopBorder) / (m_sizeButton.cy-1) * m_nColumns +
  248.             (point.x - m_cxLeftBorder) / (m_sizeButton.cx-1));
  249.     return ( iButton < m_nCount ) ? iButton : -1;
  250. }
  251.  
  252. int CPaletteBar::OnMouseActivate(CWnd*, UINT, UINT)
  253. {
  254.     return MA_NOACTIVATE;
  255. }
  256.