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.

356 lines
6.3 KiB

  1. //
  2. // mxToolKit (c) 1999 by Mete Ciragan
  3. //
  4. // file: mxTreeView.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/mxTreeView.h"
  15. #include <windows.h>
  16. #include <commctrl.h>
  17. class mxTreeView_i
  18. {
  19. public:
  20. HWND d_hwnd;
  21. };
  22. mxTreeView::mxTreeView (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 mxTreeView_i;
  28. DWORD dwStyle = TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | WS_VISIBLE | WS_CHILD;
  29. HWND hwndParent = (HWND) ((mxWidget *) parent)->getHandle ();
  30. d_this->d_hwnd = CreateWindowEx (WS_EX_CLIENTEDGE, WC_TREEVIEW, "", 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_TREEVIEW);
  37. setParent (parent);
  38. setId (id);
  39. }
  40. mxTreeView::~mxTreeView ()
  41. {
  42. remove (0);
  43. delete d_this;
  44. }
  45. mxTreeViewItem*
  46. mxTreeView::add (mxTreeViewItem *parent, const char *item)
  47. {
  48. if (!d_this)
  49. return 0;
  50. TV_ITEM tvItem;
  51. tvItem.mask = TVIF_TEXT;
  52. tvItem.pszText = (LPSTR) item;
  53. tvItem.cchTextMax = 256;
  54. HTREEITEM hParent;
  55. if (!parent)
  56. hParent = TVI_ROOT;
  57. else
  58. hParent = (HTREEITEM) parent;
  59. TV_INSERTSTRUCT tvInsert;
  60. tvInsert.hParent = hParent;
  61. tvInsert.hInsertAfter = TVI_LAST;
  62. tvInsert.item = tvItem;
  63. return (mxTreeViewItem *) TreeView_InsertItem (d_this->d_hwnd, &tvInsert);
  64. }
  65. void
  66. mxTreeView::remove (mxTreeViewItem *item)
  67. {
  68. if (!d_this)
  69. return;
  70. if (!item)
  71. TreeView_DeleteAllItems (d_this->d_hwnd);
  72. else
  73. TreeView_DeleteItem (d_this->d_hwnd, (HTREEITEM) item);
  74. }
  75. void
  76. mxTreeView::removeAll ()
  77. {
  78. remove( 0 );
  79. }
  80. void
  81. mxTreeView::setLabel (mxTreeViewItem *item, const char *label)
  82. {
  83. if (!d_this)
  84. return;
  85. if (item)
  86. {
  87. TV_ITEM tvItem;
  88. tvItem.mask = TVIF_HANDLE | TVIF_TEXT;
  89. tvItem.hItem = (HTREEITEM) item;
  90. tvItem.pszText = (LPSTR) label;
  91. tvItem.cchTextMax = 256;
  92. TreeView_SetItem (d_this->d_hwnd, &tvItem);
  93. }
  94. }
  95. void
  96. mxTreeView::setUserData (mxTreeViewItem *item, void *userData)
  97. {
  98. if (!d_this)
  99. return;
  100. if (item)
  101. {
  102. TV_ITEM tvItem;
  103. tvItem.mask = TVIF_HANDLE | TVIF_PARAM;
  104. tvItem.hItem = (HTREEITEM) item;
  105. tvItem.lParam = (LPARAM) userData;
  106. TreeView_SetItem (d_this->d_hwnd, &tvItem);
  107. }
  108. }
  109. void
  110. mxTreeView::setOpen (mxTreeViewItem *item, bool b)
  111. {
  112. if (!d_this)
  113. return;
  114. if (item)
  115. TreeView_Expand (d_this->d_hwnd, (HTREEITEM) item, b ? TVE_EXPAND:TVE_COLLAPSE);
  116. }
  117. void
  118. mxTreeView::setSelected (mxTreeViewItem *item, bool b)
  119. {
  120. if (!d_this)
  121. return;
  122. if (item)
  123. TreeView_SelectItem (d_this->d_hwnd, (HTREEITEM) item);
  124. }
  125. mxTreeViewItem*
  126. mxTreeView::getFirstChild (mxTreeViewItem *item) const
  127. {
  128. if (!d_this)
  129. return 0;
  130. return (mxTreeViewItem *) TreeView_GetChild (d_this->d_hwnd, item ? (HTREEITEM) item:TVI_ROOT);
  131. }
  132. mxTreeViewItem*
  133. mxTreeView::getNextChild (mxTreeViewItem *item) const
  134. {
  135. if (!d_this)
  136. return 0;
  137. if (item)
  138. return (mxTreeViewItem *) TreeView_GetNextSibling (d_this->d_hwnd, (HTREEITEM) item);
  139. else
  140. return 0;
  141. }
  142. mxTreeViewItem*
  143. mxTreeView::getSelectedItem () const
  144. {
  145. if (!d_this)
  146. return 0;
  147. return (mxTreeViewItem *) TreeView_GetSelection (d_this->d_hwnd);
  148. }
  149. const char*
  150. mxTreeView::getLabel (mxTreeViewItem *item) const
  151. {
  152. static char label[256];
  153. strcpy (label, "");
  154. if (!d_this)
  155. return label;
  156. if (item)
  157. {
  158. TV_ITEM tvItem;
  159. tvItem.mask = TVIF_HANDLE | TVIF_TEXT;
  160. tvItem.hItem = (HTREEITEM) item;
  161. tvItem.pszText = (LPSTR) label;
  162. tvItem.cchTextMax = 256;
  163. TreeView_GetItem (d_this->d_hwnd, &tvItem);
  164. return tvItem.pszText;
  165. }
  166. return label;
  167. }
  168. void*
  169. mxTreeView::getUserData (mxTreeViewItem *item) const
  170. {
  171. if (!d_this)
  172. return 0;
  173. if (item)
  174. {
  175. TV_ITEM tvItem;
  176. tvItem.mask = TVIF_HANDLE | TVIF_PARAM;
  177. tvItem.hItem = (HTREEITEM) item;
  178. TreeView_GetItem (d_this->d_hwnd, &tvItem);
  179. return (void *) tvItem.lParam;
  180. }
  181. return 0;
  182. }
  183. bool
  184. mxTreeView::isOpen (mxTreeViewItem *item) const
  185. {
  186. if (!d_this)
  187. return false;
  188. if (item)
  189. {
  190. TV_ITEM tvItem;
  191. tvItem.mask = TVIF_HANDLE | TVIF_STATE;
  192. tvItem.hItem = (HTREEITEM) item;
  193. tvItem.stateMask = TVIS_STATEIMAGEMASK;
  194. TreeView_GetItem (d_this->d_hwnd, &tvItem);
  195. return ((tvItem.state & TVIS_EXPANDED) == TVIS_EXPANDED);
  196. }
  197. return false;
  198. }
  199. bool
  200. mxTreeView::isSelected (mxTreeViewItem *item) const
  201. {
  202. if (!d_this)
  203. return false;
  204. if (item)
  205. {
  206. TV_ITEM tvItem;
  207. tvItem.mask = TVIF_HANDLE | TVIF_STATE;
  208. tvItem.hItem = (HTREEITEM) item;
  209. tvItem.stateMask = TVIS_STATEIMAGEMASK;
  210. TreeView_GetItem (d_this->d_hwnd, &tvItem);
  211. return ((tvItem.state & TVIS_SELECTED) == TVIS_SELECTED);
  212. }
  213. return false;
  214. }
  215. mxTreeViewItem *
  216. mxTreeView::getParent (mxTreeViewItem *item) const
  217. {
  218. if (!d_this)
  219. return 0;
  220. if (item)
  221. return (mxTreeViewItem *) TreeView_GetParent (d_this->d_hwnd, (HTREEITEM) item);
  222. return 0;
  223. }
  224. void mxTreeView::setImageList( void *himagelist )
  225. {
  226. TreeView_SetImageList(d_this->d_hwnd, (HIMAGELIST)himagelist, TVSIL_NORMAL);
  227. }
  228. void mxTreeView::setImages(mxTreeViewItem *item, int imagenormal, int imageselected )
  229. {
  230. if (!d_this)
  231. return;
  232. if (item)
  233. {
  234. TV_ITEM tvItem;
  235. tvItem.mask = TVIF_HANDLE | TVIF_SELECTEDIMAGE | TVIF_IMAGE ;
  236. tvItem.hItem = (HTREEITEM) item;
  237. tvItem.iImage = imagenormal;
  238. tvItem.iSelectedImage = imageselected;
  239. tvItem.stateMask = TVIS_STATEIMAGEMASK;
  240. TreeView_SetItem (d_this->d_hwnd, &tvItem);
  241. }
  242. }
  243. void mxTreeView::sortTree( mxTreeViewItem *parent, bool recurse,
  244. void *func, int parameter )
  245. {
  246. if (!d_this)
  247. return;
  248. TVSORTCB cb;
  249. memset( &cb, 0, sizeof( cb ) );
  250. cb.hParent = (HTREEITEM)parent;
  251. cb.lParam = parameter;
  252. cb.lpfnCompare = (int (__stdcall *)(long,long,long)) func;
  253. TreeView_SortChildrenCB( d_this->d_hwnd, &cb, recurse );
  254. }
  255. void mxTreeView::scrollTo( mxTreeViewItem *item )
  256. {
  257. if (!d_this)
  258. return;
  259. TreeView_EnsureVisible( d_this->d_hwnd, item );
  260. }