Counter Strike : Global Offensive Source Code
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.

334 lines
6.5 KiB

  1. //
  2. // mxToolKit (c) 1999 by Mete Ciragan
  3. //
  4. // file: mxListView.cpp
  5. // implementation: Win32 API
  6. // last modified: May 03 1999, Mete Ciragan
  7. // copyright: The programs and associated files contained in this
  8. // distribution were developed by Mete Ciragan. The programs
  9. // are not in the public domain, but they are freely
  10. // distributable without licensing fees. These programs are
  11. // provided without guarantee or warrantee expressed or
  12. // implied.
  13. //
  14. #include "mxtk/mxListView.h"
  15. #include <windows.h>
  16. #include <commctrl.h>
  17. class mxListView_i
  18. {
  19. public:
  20. HWND d_hwnd;
  21. };
  22. mxListView::mxListView (mxWindow *parent, int x, int y, int w, int h, int id)
  23. : mxWidget (parent, x, y, w, h)
  24. {
  25. if (!parent)
  26. return;
  27. d_this = new mxListView_i;
  28. DWORD dwStyle = LVS_NOSORTHEADER | LVS_REPORT | LVS_SHOWSELALWAYS | WS_VISIBLE | WS_CHILD;
  29. HWND hwndParent = (HWND) ((mxWidget *) parent)->getHandle ();
  30. d_this->d_hwnd = CreateWindowEx (WS_EX_CLIENTEDGE, WC_LISTVIEW, "", dwStyle,
  31. x, y, w, h, hwndParent,
  32. (HMENU) id, (HINSTANCE) GetModuleHandle (NULL), NULL);
  33. SendMessage (d_this->d_hwnd, WM_SETFONT, (WPARAM) (HFONT) GetStockObject (ANSI_VAR_FONT), MAKELPARAM (TRUE, 0));
  34. SetWindowLong (d_this->d_hwnd, GWL_USERDATA, (LONG) this);
  35. setHandle ((void *) d_this->d_hwnd);
  36. setType (MX_LISTVIEW);
  37. setParent (parent);
  38. setId (id);
  39. }
  40. mxListView::~mxListView ()
  41. {
  42. remove (0);
  43. delete d_this;
  44. }
  45. int mxListView::add ( const char *item )
  46. {
  47. if (!d_this)
  48. return 0;
  49. LVITEM lvItem;
  50. memset( &lvItem, 0, sizeof( lvItem ) );
  51. lvItem.iItem = getItemCount();
  52. lvItem.mask = LVIF_TEXT;
  53. lvItem.pszText = (LPSTR) item;
  54. lvItem.cchTextMax = 256;
  55. return ListView_InsertItem( d_this->d_hwnd, &lvItem );
  56. }
  57. void
  58. mxListView::remove ( int index )
  59. {
  60. if (!d_this)
  61. return;
  62. ListView_DeleteItem (d_this->d_hwnd, index );
  63. }
  64. void
  65. mxListView::removeAll ()
  66. {
  67. ListView_DeleteAllItems(d_this->d_hwnd);
  68. }
  69. void
  70. mxListView::setLabel ( int item , int column, const char *label)
  71. {
  72. if (!d_this)
  73. return;
  74. LVITEM lvItem;
  75. memset( &lvItem, 0, sizeof( lvItem ) );
  76. lvItem.mask = LVIF_TEXT;
  77. lvItem.iItem = item;
  78. lvItem.iSubItem = column;
  79. lvItem.pszText = (LPSTR) label;
  80. lvItem.cchTextMax = 256;
  81. ListView_SetItem (d_this->d_hwnd, &lvItem);
  82. }
  83. void mxListView::setLabel( int item, int column, const wchar_t *label )
  84. {
  85. if (!d_this)
  86. return;
  87. LV_ITEMW lvItem;
  88. memset( &lvItem, 0, sizeof( lvItem ) );
  89. lvItem.mask = LVIF_TEXT;
  90. lvItem.iItem = item;
  91. lvItem.iSubItem = column;
  92. lvItem.pszText = (wchar_t *)label;
  93. lvItem.cchTextMax = 256;
  94. SendMessage(d_this->d_hwnd, LVM_SETITEMW, 0, (LPARAM)(const LV_ITEMW FAR*)(&lvItem));
  95. }
  96. void
  97. mxListView::setUserData ( int item, int column, void *userData)
  98. {
  99. if (!d_this)
  100. return;
  101. LVITEM lvItem;
  102. memset( &lvItem, 0, sizeof( lvItem ) );
  103. lvItem.mask = LVIF_PARAM;
  104. lvItem.iItem = item;
  105. lvItem.iSubItem = column;
  106. lvItem.lParam = (LPARAM) userData;
  107. ListView_SetItem (d_this->d_hwnd, &lvItem);
  108. }
  109. void
  110. mxListView::setSelected ( int item, bool b)
  111. {
  112. if (!d_this)
  113. return;
  114. ListView_SetItemState (d_this->d_hwnd, item, b ? ( LVIS_SELECTED | LVIS_FOCUSED ): 0 , LVIS_SELECTED | LVIS_FOCUSED );
  115. }
  116. int mxListView::getItemCount() const
  117. {
  118. if (!d_this)
  119. return 0;
  120. return ListView_GetItemCount( d_this->d_hwnd );
  121. }
  122. int mxListView::getNextSelectedItem( int startitem /*= 0*/ ) const
  123. {
  124. if (!d_this)
  125. return -1;
  126. if ( ListView_GetSelectedCount( d_this->d_hwnd ) == 0 )
  127. return -1;
  128. int c = getItemCount();
  129. int start = startitem + 1;
  130. while ( start < c )
  131. {
  132. if ( isSelected( start ) )
  133. return start;
  134. start++;
  135. }
  136. return -1;
  137. }
  138. int mxListView::getNumSelected() const
  139. {
  140. if (!d_this)
  141. return 0;
  142. return ListView_GetSelectedCount( d_this->d_hwnd );
  143. }
  144. const char*
  145. mxListView::getLabel ( int item, int column ) const
  146. {
  147. static char label[256];
  148. strcpy (label, "");
  149. if (!d_this)
  150. return label;
  151. LVITEM lvItem;
  152. memset( &lvItem, 0, sizeof( lvItem ) );
  153. lvItem.mask = LVIF_TEXT;
  154. lvItem.iItem = item;
  155. lvItem.iSubItem = column;
  156. lvItem.pszText = (LPSTR) label;
  157. lvItem.cchTextMax = 256;
  158. ListView_GetItem (d_this->d_hwnd, &lvItem);
  159. return lvItem.pszText;
  160. }
  161. void*
  162. mxListView::getUserData ( int item, int column ) const
  163. {
  164. if (!d_this)
  165. return 0;
  166. LVITEM lvItem;
  167. memset( &lvItem, 0, sizeof( lvItem ) );
  168. lvItem.mask = LVIF_PARAM;
  169. lvItem.iItem = item;
  170. lvItem.iSubItem = column;
  171. ListView_GetItem (d_this->d_hwnd, &lvItem);
  172. return (void *) lvItem.lParam;
  173. }
  174. bool
  175. mxListView::isSelected ( int index ) const
  176. {
  177. if (!d_this)
  178. return false;
  179. int state = ListView_GetItemState( d_this->d_hwnd, index, LVIS_SELECTED );
  180. if ( state & LVIS_SELECTED )
  181. return true;
  182. return false;
  183. }
  184. void mxListView::setImageList( void *himagelist )
  185. {
  186. ListView_SetImageList(d_this->d_hwnd, (HIMAGELIST)himagelist, LVSIL_SMALL );
  187. }
  188. void mxListView::setImage( int item, int column, int imagenormal )
  189. {
  190. if (!d_this)
  191. return;
  192. LVITEM lvItem;
  193. memset( &lvItem, 0, sizeof( lvItem ) );
  194. lvItem.mask = LVIF_IMAGE;
  195. lvItem.iItem = item;
  196. lvItem.iSubItem = column;
  197. lvItem.iImage = imagenormal;
  198. //lvItem.state = INDEXTOSTATEIMAGEMASK( imagenormal );
  199. //lvItem.stateMask = -1;
  200. ListView_SetItem (d_this->d_hwnd, &lvItem);
  201. }
  202. void mxListView::insertTextColumn( int column, int width, char const *label )
  203. {
  204. if (!d_this)
  205. return;
  206. LVCOLUMN col;
  207. memset( &col, 0, sizeof( col ) );
  208. col.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_ORDER;
  209. col.iOrder = column;
  210. col.pszText = (char *)label;
  211. col.cchTextMax = 256;
  212. col.iSubItem = column;
  213. col.cx = width;
  214. ListView_InsertColumn( d_this->d_hwnd, column, &col );
  215. }
  216. void mxListView::insertImageColumn( int column, int width, int imageindex )
  217. {
  218. if (!d_this)
  219. return;
  220. LVCOLUMN col;
  221. memset( &col, 0, sizeof( col ) );
  222. col.mask = LVCF_IMAGE | LVCF_SUBITEM | LVCF_WIDTH | LVCF_ORDER | LVCF_FMT;
  223. col.fmt = LVCFMT_IMAGE;
  224. col.iOrder = column;
  225. col.iSubItem = column;
  226. col.cx = width;
  227. col.iImage = imageindex;
  228. ListView_InsertColumn( d_this->d_hwnd, column, &col );
  229. }
  230. void mxListView::setDrawingEnabled( bool draw )
  231. {
  232. if (!d_this)
  233. return;
  234. SendMessage( d_this->d_hwnd, WM_SETREDRAW, (WPARAM)draw ? TRUE : FALSE, (LPARAM)0 );
  235. }
  236. void mxListView::deselectAll()
  237. {
  238. if ( !d_this )
  239. return;
  240. setDrawingEnabled( false );
  241. int c = getItemCount();
  242. for ( int i = 0; i < c; i++ )
  243. {
  244. if ( isSelected( i ) )
  245. {
  246. setSelected( i, false );
  247. }
  248. }
  249. setDrawingEnabled( true );
  250. }
  251. void mxListView::scrollToItem( int item )
  252. {
  253. if ( !d_this )
  254. return;
  255. ListView_EnsureVisible( d_this->d_hwnd, item, FALSE );
  256. }