1
0

listview.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include <windows.h>
  19. #include <commctrl.h>
  20. #include "listview.h"
  21. void W_ListView::AddImageCol( int w )
  22. {
  23. LVCOLUMN lvc = { 0, };
  24. lvc.mask = LVCF_WIDTH | LVCF_FMT | LVCF_SUBITEM;
  25. lvc.fmt = LVCFMT_IMAGE;
  26. lvc.iSubItem = m_col;
  27. if ( w )
  28. lvc.cx = w;
  29. ListView_InsertColumn( m_hwnd, m_col, &lvc );
  30. ++m_col;
  31. }
  32. int W_ListView::GetColumnWidth( int col )
  33. {
  34. if ( col < 0 || col >= m_col )
  35. return 0;
  36. return ListView_GetColumnWidth( m_hwnd, col );
  37. }
  38. int W_ListView::GetParam( int p )
  39. {
  40. LVITEM lvi = { 0, };
  41. lvi.mask = LVIF_PARAM;
  42. lvi.iItem = p;
  43. ListView_GetItem( m_hwnd, &lvi );
  44. return (int)(INT_PTR)lvi.lParam;
  45. }
  46. void W_ListView::SetItemParam( int p, int param )
  47. {
  48. LVITEM lvi = { 0, };
  49. lvi.iItem = p;
  50. lvi.mask = LVIF_PARAM;
  51. lvi.lParam = param;
  52. ListView_SetItem( m_hwnd, &lvi );
  53. }
  54. void W_ListView::SetFont( HFONT newFont )
  55. {
  56. if ( m_font )
  57. {
  58. if ( m_hwnd )
  59. SetWindowFont( m_hwnd, NULL, FALSE );
  60. DeleteFont( m_font );
  61. }
  62. m_font = NULL;
  63. if ( m_hwnd )
  64. {
  65. SetWindowFont( m_hwnd, newFont, FALSE );
  66. InvalidateRect( m_hwnd, NULL, TRUE );
  67. }
  68. }
  69. int W_ListView::FindItemByPoint( int x, int y )
  70. {
  71. int l = GetCount();
  72. for ( int i = 0; i < l; i++ )
  73. {
  74. RECT r;
  75. GetItemRect( i, &r );
  76. if ( r.left <= x && r.right >= x && r.top <= y && r.bottom >= y )
  77. return i;
  78. }
  79. return -1;
  80. }
  81. W_ListView::W_ListView()
  82. {
  83. m_hwnd = NULL;
  84. m_col = 0;
  85. m_font = NULL;
  86. }
  87. W_ListView::W_ListView( HWND hwnd )
  88. {
  89. m_hwnd = NULL;
  90. m_col = 0;
  91. m_font = NULL;
  92. if ( IsWindow( hwnd ) )
  93. {
  94. m_hwnd = hwnd;
  95. ListView_SetExtendedListViewStyleEx( m_hwnd, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
  96. }
  97. }
  98. W_ListView::W_ListView( HWND hwndDlg, int resourceId )
  99. {
  100. m_hwnd = NULL;
  101. m_col = 0;
  102. m_font = NULL;
  103. m_hwnd = GetDlgItem( hwndDlg, resourceId );
  104. if ( IsWindow( m_hwnd ) )
  105. {
  106. ListView_SetExtendedListViewStyleEx( m_hwnd, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
  107. }
  108. }
  109. W_ListView::~W_ListView()
  110. {
  111. if ( m_font )
  112. DeleteFont( m_font );
  113. m_font = 0;
  114. }
  115. void W_ListView::SetTextColors(COLORREF foregroundColor, COLORREF backgroundColor)
  116. {
  117. ListView_SetTextColor(m_hwnd, foregroundColor);
  118. ListView_SetTextBkColor(m_hwnd, backgroundColor);
  119. }
  120. void W_ListView::InvertSelection()
  121. {
  122. int n = GetCount();
  123. for (int i = 0; i < n; i++)
  124. {
  125. if (GetSelected(i))
  126. Unselect(i);
  127. else
  128. SetSelected(i);
  129. }
  130. }
  131. /* unicode / ansi trouble spots go below this line */
  132. void W_ListView::AddAutoCol(LPTSTR text)
  133. {
  134. LVCOLUMN lvc = {0};
  135. lvc.mask = LVCF_TEXT;
  136. lvc.pszText = text;
  137. ListView_InsertColumn(m_hwnd, m_col, &lvc);
  138. m_col++;
  139. }
  140. void W_ListView::AddCol(const wchar_t *text, int w)
  141. {
  142. LVCOLUMN lvc = {0};
  143. lvc.mask = LVCF_TEXT | LVCF_WIDTH;
  144. lvc.pszText = (LPTSTR)text;
  145. if (w)
  146. lvc.cx = w;
  147. SendMessageW(m_hwnd, LVM_INSERTCOLUMNW, (WPARAM)m_col, (LPARAM)&lvc);
  148. m_col++;
  149. }
  150. void W_ListView::AddCol(const char *text, int w)
  151. {
  152. LVCOLUMN lvc = {0};
  153. lvc.mask = LVCF_TEXT | LVCF_WIDTH;
  154. lvc.pszText = (LPTSTR) text;
  155. if (w) lvc.cx = w;
  156. SendMessageA(m_hwnd, LVM_INSERTCOLUMNA, (WPARAM)m_col, (LPARAM)&lvc);
  157. m_col++;
  158. }
  159. int W_ListView::AppendItem(LPCWSTR text, LPARAM param)
  160. {
  161. LVITEM lvi = {0};
  162. lvi.mask = LVIF_TEXT | LVIF_PARAM;
  163. lvi.iItem = GetCount();
  164. lvi.pszText = (LPTSTR) text;
  165. lvi.cchTextMax = wcslen(text);
  166. lvi.lParam = param;
  167. return (int)(INT_PTR)SendMessageW(m_hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
  168. }
  169. int W_ListView::InsertItem(int p, const wchar_t *text, LPARAM param)
  170. {
  171. LVITEM lvi = {0};
  172. lvi.mask = LVIF_TEXT | LVIF_PARAM;
  173. lvi.iItem = p;
  174. lvi.pszText = (LPTSTR) text;
  175. lvi.cchTextMax = wcslen(text);
  176. lvi.lParam = param;
  177. return (int)(INT_PTR)SendMessageW(m_hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
  178. }
  179. int W_ListView::InsertItem(int p, const char *text, LPARAM param)
  180. {
  181. LVITEM lvi = {0};
  182. lvi.mask = LVIF_TEXT | LVIF_PARAM;
  183. lvi.iItem = p;
  184. lvi.pszText = (LPTSTR) text;
  185. lvi.cchTextMax = lstrlenA(text);
  186. lvi.lParam = param;
  187. return (int)(INT_PTR)SendMessageA(m_hwnd, LVM_INSERTITEMA, 0, (LPARAM)&lvi);
  188. }
  189. void W_ListView::SetItemText(int p, int si, const wchar_t *text)
  190. {
  191. LVITEM lvi = {0};
  192. lvi.iItem = p;
  193. lvi.iSubItem = si;
  194. lvi.mask = LVIF_TEXT;
  195. lvi.pszText = (LPTSTR)text;
  196. lvi.cchTextMax = wcslen(text);
  197. SendMessageW(m_hwnd, LVM_SETITEMW, 0, (LPARAM)&lvi);
  198. }
  199. void W_ListView::SetItemText(int p, int si, const char *text)
  200. {
  201. LVITEM lvi = {0};
  202. lvi.iItem = p;
  203. lvi.iSubItem = si;
  204. lvi.mask = LVIF_TEXT;
  205. lvi.pszText = (LPTSTR)text;
  206. lvi.cchTextMax = lstrlenA(text);
  207. SendMessageA(m_hwnd, LVM_SETITEMA, 0, (LPARAM)&lvi);
  208. }
  209. void W_ListView::setwnd (HWND hwnd)
  210. {
  211. m_hwnd = hwnd;
  212. if (IsWindow(hwnd))
  213. {
  214. ListView_SetExtendedListViewStyleEx(hwnd, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
  215. #if defined(_UNICODE) || defined(UNICODE)
  216. SendMessageW(hwnd, CCM_SETUNICODEFORMAT, TRUE, 0);
  217. #endif
  218. }
  219. }
  220. void W_ListView::GetText(int p, int si, wchar_t *text, int maxlen)
  221. {
  222. LVITEM lvi = {0};
  223. lvi.iItem = p;
  224. lvi.iSubItem = si;
  225. lvi.mask = LVIF_TEXT;
  226. lvi.pszText = (LPTSTR)text;
  227. lvi.cchTextMax = maxlen;
  228. SendMessageW(m_hwnd, LVM_GETITEMTEXTW, p, (LPARAM)&lvi);
  229. }
  230. void W_ListView::GetText(int p, int si, char *text, int maxlen)
  231. {
  232. LVITEM lvi = {0};
  233. lvi.iItem = p;
  234. lvi.iSubItem = si;
  235. lvi.mask = LVIF_TEXT;
  236. lvi.pszText = (LPTSTR)text;
  237. lvi.cchTextMax = maxlen;
  238. SendMessageA(m_hwnd, LVM_GETITEMTEXTA, p, (LPARAM)&lvi);
  239. }