listview.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. ** Copyright (C) 2003 Nullsoft, Inc.
  3. **
  4. ** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held
  5. ** liable for any damages arising from the use of this software.
  6. **
  7. ** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to
  8. ** alter it and redistribute it freely, subject to the following restrictions:
  9. **
  10. ** 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. ** If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  12. **
  13. ** 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. **
  15. ** 3. This notice may not be removed or altered from any source distribution.
  16. **
  17. */
  18. #ifndef _LISTVIEW_H_
  19. #define _LISTVIEW_H_
  20. #include <windows.h>
  21. #include <windowsx.h>
  22. #include <commctrl.h>
  23. #ifndef LVS_EX_DOUBLEBUFFER //this will work XP only
  24. #define LVS_EX_DOUBLEBUFFER 0x00010000
  25. #endif
  26. class W_ListView
  27. {
  28. public:
  29. W_ListView();
  30. W_ListView( HWND hwndView );
  31. W_ListView( HWND hwndDlg, int resourceId );
  32. ~W_ListView();
  33. void InvertSelection();
  34. void SetTextColors( COLORREF foregroundColor, COLORREF backgroundColor );
  35. void SetFont( HFONT newFont );
  36. void setwnd( HWND hwnd );
  37. void AddCol( const wchar_t *text, int w );
  38. void AddCol( const char *text, int w );
  39. void AddAutoCol( LPTSTR text );
  40. void AddImageCol( int w );
  41. void JustifyColumn( int column, int justificationFlag )
  42. {
  43. LVCOLUMN col;
  44. col.mask = LVCF_FMT;
  45. col.fmt = justificationFlag;
  46. ListView_SetColumn( m_hwnd, column, &col );
  47. }
  48. void SetColumnWidth( int column, int width )
  49. {
  50. ListView_SetColumnWidth( m_hwnd, column, width );
  51. }
  52. int GetCount( void )
  53. {
  54. return ListView_GetItemCount( m_hwnd );
  55. }
  56. int GetParam( int p );
  57. void DeleteItem( int n )
  58. {
  59. ListView_DeleteItem( m_hwnd, n );
  60. }
  61. void RefreshItem( int item )
  62. {
  63. ListView_RedrawItems( m_hwnd, item, item );
  64. }
  65. void RefreshAll()
  66. {
  67. ListView_RedrawItems( m_hwnd, 0, GetCount() );
  68. }
  69. void Clear( void )
  70. {
  71. ListView_DeleteAllItems( m_hwnd );
  72. }
  73. int GetSelected( int x )
  74. {
  75. return( ListView_GetItemState( m_hwnd, x, LVIS_SELECTED ) & LVIS_SELECTED ) ? 1 : 0;
  76. }
  77. int GetSelectedCount()
  78. {
  79. return ListView_GetSelectedCount( m_hwnd );
  80. }
  81. int GetNextSelected( int start = -1 )
  82. {
  83. return ListView_GetNextItem( m_hwnd, start, LVNI_ALL | LVNI_SELECTED );
  84. }
  85. int GetSelectionMark()
  86. {
  87. return ListView_GetSelectionMark( m_hwnd );
  88. }
  89. void SetSelected( int x )
  90. {
  91. ListView_SetItemState( m_hwnd, x, LVIS_SELECTED, LVIS_SELECTED );
  92. }
  93. void SelectAll()
  94. {
  95. ListView_SetItemState( m_hwnd, -1, LVIS_SELECTED, LVIS_SELECTED );
  96. }
  97. void UnselectAll()
  98. {
  99. ListView_SetItemState( m_hwnd, -1, 0, LVIS_SELECTED );
  100. }
  101. void Unselect( int x )
  102. {
  103. ListView_SetItemState( m_hwnd, x, 0, LVIS_SELECTED );
  104. }
  105. void EditItem( int x )
  106. {
  107. SetFocus( m_hwnd );
  108. ListView_EditLabel( m_hwnd, x );
  109. }
  110. int AppendItem( LPCWSTR text, LPARAM param );
  111. int InsertItem( int p, const wchar_t *text, LPARAM param );
  112. int InsertItem( int p, const char *text, LPARAM param );
  113. void GetItemRect( int i, RECT *r )
  114. {
  115. ListView_GetItemRect( m_hwnd, i, r, LVIR_BOUNDS );
  116. }
  117. void SetItemText( int p, int si, const wchar_t *text );
  118. void SetItemText( int p, int si, const char *text );
  119. void SetItemParam( int p, int param );
  120. void GetText( int p, int si, char *text, int maxlen );
  121. void GetText( int p, int si, wchar_t *text, int maxlen );
  122. size_t GetTextLength( int p, int si )
  123. {
  124. LVITEM lvItem;
  125. lvItem.cchTextMax = 0;
  126. lvItem.pszText = 0;
  127. lvItem.iSubItem = si;
  128. lvItem.iItem = p;
  129. return SendMessage( m_hwnd, LVM_GETITEMTEXT, p, (LPARAM)&lvItem );
  130. }
  131. int FindItemByParam( int param )
  132. {
  133. LVFINDINFO fi = { LVFI_PARAM,0,param };
  134. return ListView_FindItem( m_hwnd, -1, &fi );
  135. }
  136. int FindItemByPoint( int x, int y );
  137. void SetVirtualCount( int count, DWORD flags = 0 )
  138. {
  139. ListView_SetItemCountEx( m_hwnd, count, flags );
  140. }
  141. void SetVirtualCountAsync( int count, DWORD flags = 0 )
  142. {
  143. if ( m_hwnd )
  144. PostMessage( m_hwnd, LVM_SETITEMCOUNT, count, flags );
  145. }
  146. int GetColumnWidth( int col );
  147. void AutoColumnWidth( int col )
  148. {
  149. ListView_SetColumnWidth( m_hwnd, col, LVSCW_AUTOSIZE_USEHEADER );
  150. }
  151. void AutoSizeColumn( int col )
  152. {
  153. ListView_SetColumnWidth( m_hwnd, col, LVSCW_AUTOSIZE );
  154. }
  155. HWND getwnd( void )
  156. {
  157. return m_hwnd;
  158. }
  159. void ScrollTo( int index )
  160. {
  161. ListView_EnsureVisible( m_hwnd, index, FALSE );
  162. }
  163. void SetDoubleBuffered( bool buffered = true )
  164. {
  165. ListView_SetExtendedListViewStyleEx( m_hwnd, LVS_EX_DOUBLEBUFFER, buffered ? LVS_EX_DOUBLEBUFFER : 0 );
  166. }
  167. bool ColumnExists( int columnNum )
  168. {
  169. LVCOLUMN col;
  170. col.mask = LVCF_WIDTH;
  171. return ListView_GetColumn( m_hwnd, columnNum, &col );
  172. }
  173. void ForceUnicode()
  174. {
  175. SendMessage( m_hwnd, CCM_SETUNICODEFORMAT, TRUE, 0 );
  176. }
  177. protected:
  178. HWND m_hwnd;
  179. HFONT m_font;
  180. int m_col;
  181. };
  182. #endif//_LISTVIEW_H_