barcool.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_CORE3_SEG
  12. #pragma code_seg(AFX_CORE3_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CReBar
  21. BEGIN_MESSAGE_MAP(CReBar, CControlBar)
  22. //{{AFX_MSG_MAP(CReBar)
  23. ON_WM_NCCREATE()
  24. ON_WM_PAINT()
  25. ON_WM_NCCALCSIZE()
  26. ON_WM_ERASEBKGND()
  27. ON_WM_NCPAINT()
  28. ON_NOTIFY_REFLECT(RBN_HEIGHTCHANGE, OnHeightChange)
  29. ON_NOTIFY_REFLECT(RBN_ENDDRAG, OnHeightChange)
  30. ON_MESSAGE(RB_SHOWBAND, OnShowBand)
  31. ON_MESSAGE_VOID(WM_RECALCPARENT, OnRecalcParent)
  32. //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34. CReBar::CReBar()
  35. {
  36. SetBorders();
  37. }
  38. void CReBar::OnRecalcParent()
  39. {
  40. CFrameWnd* pFrameWnd = GetParentFrame();
  41. ASSERT(pFrameWnd != NULL);
  42. pFrameWnd->RecalcLayout();
  43. }
  44. void CReBar::OnHeightChange(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  45. {
  46. // does the CReBar have a frame?
  47. CFrameWnd* pFrameWnd = GetParentFrame();
  48. if (pFrameWnd != NULL)
  49. {
  50. // it does -- tell it to recalc its layout
  51. if (!pFrameWnd->m_bInRecalcLayout)
  52. pFrameWnd->RecalcLayout();
  53. else
  54. PostMessage(WM_RECALCPARENT);
  55. }
  56. *pResult = 0;
  57. }
  58. LRESULT CReBar::OnShowBand(WPARAM wParam, LPARAM)
  59. {
  60. LRESULT lResult = Default();
  61. if (lResult)
  62. {
  63. // keep window visible state in sync with band visible state
  64. REBARBANDINFO rbBand;
  65. rbBand.cbSize = sizeof(rbBand);
  66. rbBand.fMask = RBBIM_CHILD|RBBIM_STYLE;
  67. VERIFY(DefWindowProc(RB_GETBANDINFO, wParam, (LPARAM)&rbBand));
  68. CControlBar* pBar = DYNAMIC_DOWNCAST(CControlBar, CWnd::FromHandlePermanent(rbBand.hwndChild));
  69. BOOL bWindowVisible;
  70. if (pBar != NULL)
  71. bWindowVisible = pBar->IsVisible();
  72. else
  73. bWindowVisible = (::GetWindowLong(rbBand.hwndChild, GWL_STYLE) & WS_VISIBLE) != 0;
  74. BOOL bBandVisible = (rbBand.fStyle & RBBS_HIDDEN) == 0;
  75. if (bWindowVisible != bBandVisible)
  76. VERIFY(::ShowWindow(rbBand.hwndChild, bBandVisible ? SW_SHOW : SW_HIDE));
  77. }
  78. return lResult;
  79. }
  80. BOOL CReBar::_AddBar(CWnd* pBar, REBARBANDINFO* pRBBI)
  81. {
  82. ASSERT_VALID(this);
  83. ASSERT(::IsWindow(m_hWnd));
  84. ASSERT(pBar != NULL);
  85. ASSERT(::IsWindow(pBar->m_hWnd));
  86. pRBBI->cbSize = sizeof(REBARBANDINFO);
  87. pRBBI->fMask |= RBBIM_CHILD | RBBIM_CHILDSIZE;
  88. pRBBI->hwndChild = pBar->m_hWnd;
  89. CSize size;
  90. CControlBar* pTemp = DYNAMIC_DOWNCAST(CControlBar, pBar);
  91. if (pTemp != NULL)
  92. {
  93. size = pTemp->CalcFixedLayout(FALSE, m_dwStyle & CBRS_ORIENT_HORZ);
  94. }
  95. else
  96. {
  97. CRect rect;
  98. pBar->GetWindowRect(&rect);
  99. size = rect.Size();
  100. }
  101. //WINBUG: COMCTL32.DLL is off by 4 pixels in its sizing logic. Whatever
  102. // is specified as the minimum size, the system rebar will allow that band
  103. // to be 4 actual pixels smaller! That's why we add 4 to the size here.
  104. ASSERT(_afxComCtlVersion != -1);
  105. pRBBI->cxMinChild = size.cx + (_afxComCtlVersion < VERSION_IE401 ? 4 : 0);
  106. pRBBI->cyMinChild = size.cy;
  107. BOOL bResult = (BOOL)DefWindowProc(RB_INSERTBAND, (WPARAM)-1, (LPARAM)pRBBI);
  108. CFrameWnd* pFrameWnd = GetParentFrame();
  109. if (pFrameWnd != NULL)
  110. pFrameWnd->RecalcLayout();
  111. return bResult;
  112. }
  113. BOOL CReBar::AddBar(CWnd* pBar, LPCTSTR pszText, CBitmap* pbmp, DWORD dwStyle)
  114. {
  115. REBARBANDINFO rbBand;
  116. rbBand.fMask = RBBIM_STYLE;
  117. rbBand.fStyle = dwStyle;
  118. if (pszText != NULL)
  119. {
  120. rbBand.fMask |= RBBIM_TEXT;
  121. rbBand.lpText = const_cast<LPTSTR>(pszText);
  122. }
  123. if (pbmp != NULL)
  124. {
  125. rbBand.fMask |= RBBIM_BACKGROUND;
  126. rbBand.hbmBack = (HBITMAP)*pbmp;
  127. }
  128. return _AddBar(pBar, &rbBand);
  129. }
  130. BOOL CReBar::AddBar(CWnd* pBar, COLORREF clrFore, COLORREF clrBack, LPCTSTR pszText, DWORD dwStyle)
  131. {
  132. REBARBANDINFO rbBand;
  133. rbBand.fMask = RBBIM_STYLE | RBBIM_COLORS;
  134. rbBand.fStyle = dwStyle;
  135. rbBand.clrFore = clrFore;
  136. rbBand.clrBack = clrBack;
  137. if (pszText != NULL)
  138. {
  139. rbBand.fMask |= RBBIM_TEXT;
  140. rbBand.lpText = const_cast<LPTSTR>(pszText);
  141. }
  142. return _AddBar(pBar, &rbBand);
  143. }
  144. CSize CReBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
  145. {
  146. ASSERT_VALID(this);
  147. ASSERT(::IsWindow(m_hWnd));
  148. // the union of the band rectangles is the total bounding rect
  149. int nCount = DefWindowProc(RB_GETBANDCOUNT, 0, 0);
  150. REBARBANDINFO rbBand;
  151. rbBand.cbSize = sizeof(rbBand);
  152. int nTemp;
  153. // sync up hidden state of the bands
  154. for (nTemp = nCount; nTemp--; )
  155. {
  156. rbBand.fMask = RBBIM_CHILD|RBBIM_STYLE;
  157. VERIFY(DefWindowProc(RB_GETBANDINFO, nTemp, (LPARAM)&rbBand));
  158. CControlBar* pBar = DYNAMIC_DOWNCAST(CControlBar, CWnd::FromHandlePermanent(rbBand.hwndChild));
  159. BOOL bWindowVisible;
  160. if (pBar != NULL)
  161. bWindowVisible = pBar->IsVisible();
  162. else
  163. bWindowVisible = (::GetWindowLong(rbBand.hwndChild, GWL_STYLE) & WS_VISIBLE) != 0;
  164. BOOL bBandVisible = (rbBand.fStyle & RBBS_HIDDEN) == 0;
  165. if (bWindowVisible != bBandVisible)
  166. VERIFY(DefWindowProc(RB_SHOWBAND, nTemp, bWindowVisible));
  167. }
  168. // determine bounding rect of all visible bands
  169. CRect rectBound; rectBound.SetRectEmpty();
  170. for (nTemp = nCount; nTemp--; )
  171. {
  172. rbBand.fMask = RBBIM_STYLE;
  173. VERIFY(DefWindowProc(RB_GETBANDINFO, nTemp, (LPARAM)&rbBand));
  174. if ((rbBand.fStyle & RBBS_HIDDEN) == 0)
  175. {
  176. CRect rect;
  177. VERIFY(DefWindowProc(RB_GETRECT, nTemp, (LPARAM)&rect));
  178. rectBound |= rect;
  179. }
  180. }
  181. // add borders as part of bounding rect
  182. if (!rectBound.IsRectEmpty())
  183. {
  184. CRect rect; rect.SetRectEmpty();
  185. CalcInsideRect(rect, bHorz);
  186. rectBound.right -= rect.Width();
  187. rectBound.bottom -= rect.Height();
  188. }
  189. return CSize((bHorz && bStretch) ? 32767 : rectBound.Width(),
  190. (!bHorz && bStretch) ? 32767 : rectBound.Height());
  191. }
  192. CSize CReBar::CalcDynamicLayout(int /*nLength*/, DWORD dwMode)
  193. {
  194. if (dwMode & LM_HORZDOCK)
  195. ASSERT(dwMode & LM_HORZ);
  196. return CalcFixedLayout(dwMode & LM_STRETCH, dwMode & LM_HORZ);
  197. }
  198. BOOL CReBar::Create(CWnd* pParentWnd, DWORD dwCtrlStyle, DWORD dwStyle, UINT nID)
  199. {
  200. ASSERT_VALID(pParentWnd); // must have a parent
  201. ASSERT (!((dwStyle & CBRS_SIZE_FIXED) && (dwStyle & CBRS_SIZE_DYNAMIC)));
  202. // save the style
  203. m_dwStyle = (dwStyle & CBRS_ALL);
  204. if (nID == AFX_IDW_REBAR)
  205. m_dwStyle |= CBRS_HIDE_INPLACE;
  206. dwStyle &= ~CBRS_ALL;
  207. dwStyle |= CCS_NOPARENTALIGN|CCS_NOMOVEY|CCS_NODIVIDER|CCS_NORESIZE|RBS_VARHEIGHT;
  208. dwStyle |= dwCtrlStyle;
  209. // initialize common controls
  210. VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTL_COOL_REG));
  211. _AfxGetComCtlVersion();
  212. ASSERT(_afxComCtlVersion != -1);
  213. // create the HWND
  214. CRect rect; rect.SetRectEmpty();
  215. if (!CWnd::Create(REBARCLASSNAME, NULL, dwStyle, rect, pParentWnd, nID))
  216. return FALSE;
  217. // Note: Parent must resize itself for control bar to be resized
  218. return TRUE;
  219. }
  220. void CReBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHandler)
  221. {
  222. UpdateDialogControls(pTarget, bDisableIfNoHandler);
  223. }
  224. BOOL CReBar::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
  225. {
  226. if (!CControlBar::OnNcCreate(lpCreateStruct))
  227. return FALSE;
  228. // if the owner was set before the rebar was created, set it now
  229. if (m_hWndOwner != NULL)
  230. DefWindowProc(RB_SETPARENT, (WPARAM)m_hWndOwner, 0);
  231. return TRUE;
  232. }
  233. BOOL CReBar::OnEraseBkgnd(CDC*)
  234. {
  235. return (BOOL)Default();
  236. }
  237. void CReBar::OnNcCalcSize(BOOL /*bCalcValidRects*/, NCCALCSIZE_PARAMS* lpncsp)
  238. {
  239. // calculate border space (will add to top/bottom, subtract from right/bottom)
  240. CRect rect; rect.SetRectEmpty();
  241. BOOL bHorz = (m_dwStyle & CBRS_ORIENT_HORZ) != 0;
  242. CControlBar::CalcInsideRect(rect, bHorz);
  243. // adjust non-client area for border space
  244. lpncsp->rgrc[0].left += rect.left;
  245. lpncsp->rgrc[0].top += rect.top;
  246. lpncsp->rgrc[0].right += rect.right;
  247. lpncsp->rgrc[0].bottom += rect.bottom;
  248. }
  249. void CReBar::OnNcPaint()
  250. {
  251. EraseNonClient();
  252. }
  253. void CReBar::OnPaint()
  254. {
  255. Default();
  256. }
  257. int CReBar::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
  258. {
  259. ASSERT_VALID(this);
  260. ASSERT(::IsWindow(m_hWnd));
  261. HWND hWndChild = _AfxChildWindowFromPoint(m_hWnd, point);
  262. CWnd* pWnd = CWnd::FromHandlePermanent(hWndChild);
  263. if (pWnd == NULL)
  264. return CControlBar::OnToolHitTest(point, pTI);
  265. ASSERT(pWnd->m_hWnd == hWndChild);
  266. return pWnd->OnToolHitTest(point, pTI);
  267. }
  268. LRESULT CReBar::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  269. {
  270. // special handling for certain messages (forwarding to owner/parent)
  271. switch (message)
  272. {
  273. case WM_POPMESSAGESTRING:
  274. case WM_SETMESSAGESTRING:
  275. return GetOwner()->SendMessage(message, wParam, lParam);
  276. }
  277. return CControlBar::WindowProc(message, wParam, lParam);
  278. }
  279. #ifdef AFX_INIT_SEG
  280. #pragma code_seg(AFX_INIT_SEG)
  281. #endif
  282. IMPLEMENT_DYNAMIC(CReBar, CControlBar)
  283. /////////////////////////////////////////////////////////////////////////////