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.

289 lines
7.1 KiB

  1. //-----------------------------------------------------------------------------
  2. // ProgList.h
  3. //-----------------------------------------------------------------------------
  4. #ifndef _PROGLIST_H
  5. #define _PROGLIST_H
  6. //-----------------------------------------------------------------------------
  7. // state of the item
  8. //-----------------------------------------------------------------------------
  9. typedef enum
  10. {
  11. IS_NONE = 0, // the item hasn't been run yet
  12. IS_SUCCEEDED, // the item succeeded
  13. IS_FAILED // the item failed
  14. } ItemState;
  15. //-----------------------------------------------------------------------------
  16. // an entry in the list
  17. //-----------------------------------------------------------------------------
  18. class CProgressItem
  19. {
  20. public:
  21. CProgressItem() :
  22. m_strText(_T("")),
  23. m_bActive(FALSE),
  24. m_eState(IS_NONE),
  25. m_lData(0)
  26. {
  27. }
  28. TSTRING m_strText; // display string of the item
  29. BOOL m_bActive; // is the item active / currently processing
  30. ItemState m_eState; // state of the item
  31. LPARAM m_lData; // misc item data
  32. }; // class CProgressItem
  33. //-----------------------------------------------------------------------------
  34. // the list itself
  35. //-----------------------------------------------------------------------------
  36. class CProgressList : public CWindowImpl< CProgressList, CListViewCtrl >
  37. {
  38. //-----------------------------------------------------------------------------
  39. // functions
  40. //-----------------------------------------------------------------------------
  41. public:
  42. BEGIN_MSG_MAP(CProgressList)
  43. END_MSG_MAP()
  44. typedef CWindowImpl< CProgressList, CListViewCtrl > BC;
  45. VOID Attach( HWND h )
  46. {
  47. // call the base class to attach to the control
  48. BC::Attach( h );
  49. // initialize the background of the control
  50. SetBkColor( GetSysColor(COLOR_BTNFACE) );
  51. // create the fonts for the control
  52. HFONT hf;
  53. LOGFONT lf;
  54. ZeroMemory( &lf, sizeof(lf) );
  55. hf = GetFont();
  56. ::GetObject( hf, sizeof(lf), &lf );
  57. // create the normal/text font from the current font
  58. m_hfText = CreateFontIndirect( &lf );
  59. // create the bold font
  60. lf.lfWeight = FW_BOLD;
  61. m_hfBoldText = CreateFontIndirect( &lf );
  62. // make the symbol font
  63. lf.lfHeight = min( GetSystemMetrics(SM_CXVSCROLL), GetSystemMetrics(SM_CYVSCROLL) ) - 4;
  64. lf.lfHeight = MulDiv( lf.lfHeight, 3, 2 );
  65. lf.lfWeight = FW_NORMAL;
  66. lf.lfCharSet = SYMBOL_CHARSET;
  67. _tcscpy( lf.lfFaceName, _T("Marlett") );
  68. m_hfSymbol = CreateFontIndirect( &lf );
  69. // need to use a larger font for one of the symbols
  70. lf.lfHeight -= ( lf.lfHeight / 4 );
  71. m_hfLargeSymbol = CreateFontIndirect( &lf );
  72. // add a column that is the width of the control
  73. RECT rc;
  74. GetWindowRect( &rc );
  75. LVCOLUMN lvC;
  76. lvC.mask = LVCF_TEXT | LVCF_WIDTH;
  77. lvC.cx = (rc.right - rc.left) - GetSystemMetrics(SM_CXVSCROLL) - 2;
  78. lvC.pszText = _T("");
  79. InsertColumn( 0, &lvC );
  80. }
  81. INT AddItem( TSTRING strText )
  82. {
  83. // create a new progress item and insert into the list control and internal list
  84. CProgressItem* pItem = new CProgressItem();
  85. if( !pItem ) return -1;
  86. pItem->m_strText = strText;
  87. m_lItems.push_back( pItem );
  88. LVITEM lvI;
  89. ZeroMemory( &lvI, sizeof(lvI) );
  90. lvI.mask = LVIF_PARAM;
  91. lvI.iItem = GetItemCount();
  92. lvI.lParam = (LPARAM)pItem;
  93. return InsertItem( &lvI );
  94. }
  95. VOID OnMeasureItem( LPARAM lParam )
  96. {
  97. MEASUREITEMSTRUCT* p = (MEASUREITEMSTRUCT*)lParam;
  98. if( !p ) return;
  99. HDC hDC = GetDC();
  100. TEXTMETRIC tm;
  101. GetTextMetrics( hDC, &tm );
  102. p->itemWidth = 0;
  103. p->itemHeight = tm.tmHeight + 2;
  104. ReleaseDC( hDC );
  105. }
  106. VOID OnDrawItem( LPARAM lParam )
  107. {
  108. // get the data we need and stop redrawing
  109. DRAWITEMSTRUCT* p = (DRAWITEMSTRUCT*)lParam;
  110. if( !p ) return;
  111. CProgressItem* pI = (CProgressItem*)p->itemData;
  112. if( !pI ) return;
  113. SetRedraw( FALSE );
  114. // get the symbol to draw next to the item
  115. TCHAR ch = 0;
  116. COLORREF crText = GetSysColor( COLOR_WINDOWTEXT );
  117. HFONT hfSymbol = m_hfSymbol;
  118. if( pI->m_bActive )
  119. {
  120. ch = _T('4');
  121. }
  122. else if( pI->m_eState == IS_SUCCEEDED )
  123. {
  124. ch = _T('a');
  125. crText = RGB( 0, 128, 0 );
  126. }
  127. else if( pI->m_eState == IS_FAILED )
  128. {
  129. ch = _T('r');
  130. crText = RGB( 128, 0, 0 );
  131. hfSymbol = m_hfLargeSymbol;
  132. }
  133. // setup the text and draw the symbol if there is one
  134. if( ch )
  135. {
  136. HFONT hfPre = (HFONT)SelectObject( p->hDC, hfSymbol );
  137. COLORREF crPre = ::SetTextColor( p->hDC, crText );
  138. DrawText( p->hDC, &ch, 1, &p->rcItem, DT_SINGLELINE | DT_VCENTER );
  139. SelectObject( p->hDC, hfPre );
  140. ::SetTextColor( p->hDC, crPre );
  141. }
  142. // draw the item text, inc the RECT for the symbol
  143. p->rcItem.left += 20;
  144. HFONT hfPre = (HFONT)SelectObject( p->hDC, ch == _T('4') ? m_hfBoldText : m_hfText );
  145. COLORREF crPre = ::SetTextColor( p->hDC, GetSysColor(COLOR_WINDOWTEXT) );
  146. DrawText( p->hDC, pI->m_strText.c_str(), pI->m_strText.length(), &p->rcItem, DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS | DT_NOPREFIX );
  147. SelectObject( p->hDC, hfPre );
  148. ::SetTextColor( p->hDC, crPre );
  149. // allow the redraws to happen
  150. SetRedraw( TRUE );
  151. }
  152. VOID SetItemState( INT iIndex, ItemState eState, BOOL bRedraw = TRUE )
  153. {
  154. // get the CProgressItem and set the state
  155. CProgressItem* pI = GetProgressItem( iIndex );
  156. if( pI )
  157. {
  158. pI->m_eState = eState;
  159. }
  160. if( bRedraw )
  161. {
  162. SendMessage( LVM_REDRAWITEMS, iIndex, iIndex );
  163. UpdateWindow();
  164. }
  165. }
  166. VOID ToggleActive( INT iIndex, BOOL bRedraw = TRUE )
  167. {
  168. // get the CProgress Item and toggle it's 'active' flag
  169. CProgressItem* pI = GetProgressItem( iIndex );
  170. if( pI )
  171. {
  172. pI->m_bActive = !pI->m_bActive;
  173. }
  174. if( bRedraw )
  175. {
  176. SendMessage( LVM_REDRAWITEMS, iIndex, iIndex );
  177. UpdateWindow();
  178. }
  179. }
  180. CProgressItem* GetProgressItem( INT iIndex )
  181. {
  182. LVITEM lvI;
  183. ZeroMemory( &lvI, sizeof(lvI) );
  184. lvI.mask = LVIF_PARAM;
  185. lvI.iItem = iIndex;
  186. if( !SendMessage(LVM_GETITEM, 0, (LPARAM)&lvI) )
  187. {
  188. return NULL;
  189. }
  190. return (CProgressItem*)lvI.lParam;
  191. }
  192. CProgressList() :
  193. m_hfText(NULL),
  194. m_hfBoldText(NULL),
  195. m_hfSymbol(NULL),
  196. m_hfLargeSymbol(NULL)
  197. {
  198. }
  199. ~CProgressList()
  200. {
  201. if( m_hfText ) DeleteObject( m_hfText );
  202. if( m_hfBoldText ) DeleteObject( m_hfBoldText );
  203. if( m_hfSymbol ) DeleteObject( m_hfSymbol );
  204. if( m_hfLargeSymbol ) DeleteObject( m_hfLargeSymbol );
  205. EmptyList();
  206. }
  207. VOID EmptyList( VOID )
  208. {
  209. // cleanup the progress list items
  210. for( list<CProgressItem*>::iterator it = m_lItems.begin(); it != m_lItems.end(); ++it )
  211. {
  212. delete (*it);
  213. }
  214. m_lItems.clear();
  215. }
  216. BOOL DeleteAllItems( VOID )
  217. {
  218. EmptyList();
  219. return BC::DeleteAllItems();
  220. }
  221. private:
  222. //-----------------------------------------------------------------------------
  223. // variables
  224. //-----------------------------------------------------------------------------
  225. public:
  226. private:
  227. HFONT m_hfText;
  228. HFONT m_hfBoldText;
  229. HFONT m_hfSymbol;
  230. HFONT m_hfLargeSymbol;
  231. list< CProgressItem* > m_lItems;
  232. }; // class CProgressList
  233. #endif // _PROGLIST_H