Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.7 KiB

  1. // HMHeaderCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HMHeaderCtrl.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CHMHeaderCtrl
  12. CHMHeaderCtrl::CHMHeaderCtrl()
  13. {
  14. m_iLastColumn = -1;
  15. }
  16. CHMHeaderCtrl::~CHMHeaderCtrl()
  17. {
  18. m_up.DeleteObject();
  19. m_down.DeleteObject();
  20. }
  21. inline HBITMAP CHMHeaderCtrl::GetArrowBitmap(bool bAscending)
  22. {
  23. if( bAscending )
  24. {
  25. if( ! m_up.GetSafeHandle() )
  26. {
  27. CreateUpArrowBitmap();
  28. }
  29. return (HBITMAP)m_up.GetSafeHandle();
  30. }
  31. else
  32. {
  33. if( ! m_down.GetSafeHandle() )
  34. {
  35. CreateDownArrowBitmap();
  36. }
  37. return (HBITMAP)m_down.GetSafeHandle();
  38. }
  39. return NULL;
  40. }
  41. inline void CHMHeaderCtrl::CreateUpArrowBitmap()
  42. {
  43. CDC MemDC;
  44. CClientDC dc(this);
  45. CRect r(0,0,8,8);
  46. // create offscreen dc and offscreen bitmap
  47. MemDC.CreateCompatibleDC(&dc);
  48. m_up.CreateCompatibleBitmap(&dc,r.Width(),r.Height());
  49. CBitmap* pOldBitmap = MemDC.SelectObject(&m_up);
  50. // Draw the background
  51. MemDC.FillSolidRect(r, ::GetSysColor(COLOR_3DFACE));
  52. // Set up pens to use for drawing the triangle
  53. CPen penLight(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT));
  54. CPen penShadow(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
  55. CPen *pOldPen = MemDC.SelectObject( &penLight );
  56. // Draw triangle pointing upwards
  57. MemDC.MoveTo( r.left, r.bottom-1 );
  58. MemDC.LineTo( r.right, r.bottom-1 );
  59. MemDC.LineTo( r.right/2, r.top );
  60. MemDC.SelectObject( &penShadow );
  61. MemDC.LineTo( r.left, r.bottom-1 );
  62. // clean up
  63. MemDC.SelectObject(pOldPen);
  64. MemDC.SelectObject(pOldBitmap);
  65. }
  66. inline void CHMHeaderCtrl::CreateDownArrowBitmap()
  67. {
  68. CDC MemDC;
  69. CClientDC dc(this);
  70. CRect r(0,0,8,8);
  71. // create offscreen dc and offscreen bitmap
  72. MemDC.CreateCompatibleDC(&dc);
  73. m_down.CreateCompatibleBitmap(&dc,r.Width(),r.Height());
  74. CBitmap* pOldBitmap = MemDC.SelectObject(&m_down);
  75. // Draw the background
  76. MemDC.FillSolidRect(r, ::GetSysColor(COLOR_3DFACE));
  77. // Set up pens to use for drawing the triangle
  78. CPen penLight(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT));
  79. CPen penShadow(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
  80. CPen *pOldPen = MemDC.SelectObject( &penShadow );
  81. // Draw triangle pointing downwards
  82. MemDC.MoveTo( r.right, r.top );
  83. MemDC.LineTo( r.left, r.top );
  84. MemDC.LineTo( r.right/2, r.bottom );
  85. MemDC.SelectObject( &penLight );
  86. MemDC.LineTo( r.right, r.top );
  87. // clean up
  88. MemDC.SelectObject(pOldPen);
  89. MemDC.SelectObject(pOldBitmap);
  90. }
  91. int CHMHeaderCtrl::SetSortImage( int nColumn, bool bAscending )
  92. {
  93. int nPrevCol = m_iLastColumn;
  94. m_bSortAscending = bAscending;
  95. // set the passed column to display the appropriate sort indicator
  96. HDITEM hditem;
  97. hditem.mask = HDI_FORMAT;
  98. GetItem( nColumn, &hditem );
  99. hditem.mask = HDI_BITMAP | HDI_FORMAT;
  100. hditem.fmt |= HDF_BITMAP;
  101. hditem.fmt |= HDF_BITMAP_ON_RIGHT;
  102. hditem.hbm = (HBITMAP)GetArrowBitmap(bAscending);
  103. SetItem( nColumn, &hditem );
  104. // save off the last column the user clikced on
  105. m_iLastColumn = nColumn;
  106. return nPrevCol;
  107. }
  108. void CHMHeaderCtrl::RemoveSortImage(int nColumn)
  109. {
  110. // clear the sort indicator from the previous column
  111. HDITEM hditem;
  112. hditem.mask = HDI_FORMAT;
  113. GetItem( nColumn, &hditem );
  114. hditem.mask = HDI_FORMAT;
  115. hditem.fmt &= ~HDF_BITMAP;
  116. hditem.fmt &= ~HDF_BITMAP_ON_RIGHT;
  117. SetItem( nColumn, &hditem );
  118. }
  119. void CHMHeaderCtrl::RemoveAllSortImages()
  120. {
  121. int iCount = GetItemCount();
  122. for( int i = 0; i < iCount; i++ )
  123. {
  124. RemoveSortImage(i);
  125. }
  126. }
  127. BEGIN_MESSAGE_MAP(CHMHeaderCtrl, CHeaderCtrl)
  128. //{{AFX_MSG_MAP(CHMHeaderCtrl)
  129. //}}AFX_MSG_MAP
  130. END_MESSAGE_MAP()
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CHMHeaderCtrl message handlers