Leaked source code of windows server 2003
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.

297 lines
8.3 KiB

  1. // ListRow.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "certmap.h"
  5. #include "ListRow.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define SZ_RES_COLOR_PREFS "Control Panel\\Colors"
  12. #define SZ_RES_COLOR_HILITE "Hilight"
  13. #define SZ_RES_COLOR_HILITETEXT "HilightText"
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CListSelRowCtrl
  16. //-----------------------------------------------------------------------------------
  17. CListSelRowCtrl::CListSelRowCtrl():
  18. m_StartDrawingCol( 0 )
  19. {
  20. }
  21. //-----------------------------------------------------------------------------------
  22. CListSelRowCtrl::~CListSelRowCtrl()
  23. {
  24. }
  25. //-----------------------------------------------------------------------------------
  26. BEGIN_MESSAGE_MAP(CListSelRowCtrl, CListCtrl)
  27. //{{AFX_MSG_MAP(CListSelRowCtrl)
  28. ON_WM_LBUTTONDOWN()
  29. ON_WM_LBUTTONDBLCLK()
  30. //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32. //-----------------------------------------------------------------------------------
  33. void CListSelRowCtrl::GetHiliteColors()
  34. {
  35. // get the hilite color
  36. m_colorHilite = GetSysColor( COLOR_HIGHLIGHT );
  37. // get the hilited text color
  38. m_colorHiliteText = GetSysColor( COLOR_HIGHLIGHTTEXT );
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CListSelRowCtrl message handlers
  42. //-----------------------------------------------------------------------------------
  43. void CListSelRowCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  44. {
  45. CRect rcItem = lpDrawItemStruct->rcItem;
  46. CRect rcSection;
  47. UINT itemID = lpDrawItemStruct->itemID;
  48. UINT cpLeft = rcItem.left;
  49. CString sz;
  50. LV_COLUMN colData;
  51. COLORREF colorTextOld;
  52. COLORREF colorBackOld;
  53. // setup the CDC object
  54. CDC cdc;
  55. cdc.Attach( lpDrawItemStruct->hDC );
  56. #ifdef _DEBUG
  57. if ( m_StartDrawingCol == 0 )
  58. sz.Empty();
  59. #endif
  60. // clear the columnd buffer
  61. ZeroMemory( &colData, sizeof(colData) );
  62. colData.mask = LVCF_WIDTH;
  63. // if this is the selected item, prepare the background and the text color
  64. BOOL fSelected = lpDrawItemStruct->itemState & ODS_SELECTED;
  65. if ( fSelected )
  66. {
  67. GetHiliteColors();
  68. colorTextOld = cdc.SetTextColor( m_colorHiliteText );
  69. colorBackOld = cdc.SetBkColor( m_colorHilite );
  70. }
  71. // starting with the m_StartDrawingCol column, draw the columns
  72. // do it in a loop, just skipping until we hit m_StartDrawingCol
  73. DWORD iCol = 0;
  74. while ( GetColumn(iCol, &colData) )
  75. {
  76. // see if we are ready yet
  77. if ( iCol < m_StartDrawingCol )
  78. {
  79. // set the new left.
  80. cpLeft += colData.cx;
  81. // increment the column counter
  82. iCol++;
  83. continue;
  84. }
  85. // prepare the background but once
  86. if ( iCol == m_StartDrawingCol )
  87. {
  88. // prepare the background
  89. rcSection = rcItem;
  90. rcSection.left = cpLeft;
  91. rcSection.right--;
  92. CBrush brush;
  93. if ( lpDrawItemStruct->itemState & ODS_SELECTED )
  94. brush.CreateSolidBrush( m_colorHilite );
  95. else
  96. brush.CreateSolidBrush( GetSysColor( COLOR_WINDOW ) );
  97. cdc.FillRect( &rcSection, &brush );
  98. }
  99. // display the name
  100. sz = GetItemText( itemID, iCol );
  101. if ( !sz.IsEmpty() )
  102. {
  103. // figure out the sectional rect
  104. rcSection = rcItem;
  105. rcSection.left = cpLeft + 2;
  106. rcSection.right = cpLeft + colData.cx - 1;
  107. // fit the string into the required space
  108. FitString( sz, rcSection.right - rcSection.left, &cdc );
  109. //draw the string
  110. cdc.DrawText( sz, &rcSection, DT_SINGLELINE|DT_LEFT|DT_BOTTOM|DT_NOPREFIX );
  111. }
  112. // set the new left.
  113. cpLeft += colData.cx;
  114. // increment the column counter
  115. iCol++;
  116. }
  117. // if this is the selected item, restore the colors
  118. if ( fSelected )
  119. {
  120. cdc.SetTextColor( colorTextOld );
  121. cdc.SetBkColor( colorBackOld );
  122. }
  123. // cleanup the CDC object
  124. cdc.Detach();
  125. }
  126. //------------------------------------------------------------------------
  127. void CListSelRowCtrl::FitString( CString &sz, int cpWidth, CDC* pcdc )
  128. {
  129. CSize size;
  130. UINT cch;
  131. CString szEllipsis;
  132. // start by testing the existing width
  133. size = pcdc->GetTextExtent( sz );
  134. if ( size.cx <= cpWidth ) return;
  135. // initialize szTrunc and szEllipsis
  136. cch = sz.GetLength();
  137. szEllipsis.LoadString(IDS_ELLIPSIS);
  138. // while we are too big, truncate one letter and add an ellipsis
  139. while( (size.cx > cpWidth) && (cch > 1) )
  140. {
  141. // chop off the last letter of the string - not counting the ...
  142. cch--;
  143. sz = sz.Left( cch );
  144. // add the elipsis (spelling?)
  145. sz += szEllipsis;
  146. // get the length
  147. size = pcdc->GetTextExtent( sz );
  148. }
  149. }
  150. //------------------------------------------------------------------------
  151. void CListSelRowCtrl::HiliteSelectedCells()
  152. {
  153. int iList = -1;
  154. while( (iList = GetNextItem( iList, LVNI_SELECTED )) >= 0 )
  155. HiliteSelectedCell( iList );
  156. }
  157. //------------------------------------------------------------------------
  158. void CListSelRowCtrl::HiliteSelectedCell( int iCell, BOOL fHilite )
  159. {
  160. // if there is no selected cell, do nothing
  161. if ( iCell < 0 )
  162. return;
  163. // get the rect to draw
  164. CRect rect;
  165. if ( !FGetCellRect(iCell, -1, &rect) )
  166. {
  167. ASSERT(FALSE);
  168. return;
  169. }
  170. // get the client rect
  171. CRect rectClient;
  172. GetClientRect( rectClient );
  173. // make sure it fits ok (problems can occur here when scrolled)
  174. // don't want it to draw in the column titles
  175. if ( rect.top < (rect.bottom - rect.top) )
  176. return;
  177. // now prepare to draw
  178. CDC *pdc = GetDC();
  179. // clip to the client area
  180. pdc->IntersectClipRect( rectClient );
  181. // set up the brush
  182. CBrush cbrush;
  183. if ( fHilite )
  184. cbrush.CreateSolidBrush( RGB(192,192,192) );
  185. else
  186. cbrush.CreateSolidBrush( RGB(0xFF,0xFF,0xFF) );
  187. // draw the hilite rect
  188. pdc->FrameRect( rect, &cbrush );
  189. // cleanup
  190. ReleaseDC( pdc );
  191. }
  192. //------------------------------------------------------------------------
  193. BOOL CListSelRowCtrl::FGetCellRect( LONG iRow, LONG iCol, CRect *pcrect )
  194. {
  195. // first, get the rect that the list thinks is appropriate
  196. if ( !GetItemRect(iRow, pcrect, LVIR_BOUNDS) )
  197. return FALSE;
  198. // if iCol < 0, then return the total size of the row
  199. if ( iCol < 0 )
  200. return TRUE;
  201. // trim the horizontal dimension to the correct column positioning
  202. LONG cpLeft;
  203. LONG cpRight = 0;
  204. for ( WORD i = 0; i <= iCol; i++ )
  205. {
  206. // set the left side
  207. cpLeft = cpRight;
  208. // get the right
  209. LONG cpWidth = GetColumnWidth(i);
  210. if ( cpWidth < 0 ) return FALSE;
  211. cpRight += cpWidth;
  212. }
  213. // well, now trim it seeing as we have the right values
  214. pcrect->left = cpLeft;
  215. pcrect->right = cpRight;
  216. // success!
  217. return TRUE;
  218. }
  219. #define MAKE_LPARAM(x,y) ( ((unsigned long)(y)<<16) | ((unsigned long)(x)) )
  220. //------------------------------------------------------------------------
  221. void CListSelRowCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
  222. {
  223. // force the point to be in the right place
  224. point.x = 6;
  225. LPARAM lp = MAKE_LPARAM(point.x, point.y);
  226. // DefWindowProc(WM_LBUTTONDBLCLK, nFlags, lp );
  227. CListCtrl::OnLButtonDblClk( nFlags, point);
  228. }
  229. //------------------------------------------------------------------------
  230. void CListSelRowCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  231. {
  232. point.x = 6;
  233. LPARAM lp = MAKE_LPARAM(point.x, point.y);
  234. // DefWindowProc(WM_LBUTTONDOWN, nFlags, lp );
  235. CListCtrl::OnLButtonDown( nFlags, point);
  236. }