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.

324 lines
9.3 KiB

  1. /*****************************************************************************
  2. *
  3. * chklst.cpp
  4. *
  5. * Wrappers that turn a listview into a checked listbox.
  6. *
  7. * Typical usage:
  8. *
  9. * // at app startup
  10. * CCheckList::Init();
  11. *
  12. * // Dialog template should look like this:
  13. *
  14. * CONTROL "", IDC_TYPE_CHECKLIST, WC_LISTVIEW,
  15. * LVS_REPORT | LVS_SINGLESEL |
  16. * LVS_NOCOLUMNHEADER |
  17. * LVS_SHAREIMAGELISTS |
  18. * WS_TABSTOP | WS_BORDER,
  19. * 7, 17, 127, 117
  20. *
  21. * // Do not use the LVS_SORTASCENDING or LVS_SORTDESCENDING flags.
  22. *
  23. * // in the dialog's WM_INITDIALOG handler
  24. * hwndList = GetDlgItem(hDlg, IDC_TYPE_CHECKLIST);
  25. * CCheckList::OnInitDialog(hwndList);
  26. *
  27. * // The first item added is always item zero, but you can put it
  28. * // into a variable if it makes you feel better
  29. * iFirst = CCheckList::AddString(hwndList,
  30. * "Checkitem, initially checked", TRUE);
  31. *
  32. * // The second item added is always item one, but you can put it
  33. * // into a variable if it makes you feel better
  34. * iSecond = CCheckList::AddString(hwndList,
  35. * "Checkitem, initially unchecked", FALSE);
  36. *
  37. * CCheckList::InitFinish(hwndList);
  38. *
  39. * // To suck out values
  40. * if (CCheckList::GetState(hwndList, iFirst)) {...}
  41. * if (CCheckList::GetState(hwndList, iSecond)) {...}
  42. *
  43. * // At dialog box destruction
  44. * CCheckList::OnDestroy(hwndList);
  45. *
  46. * // at app shutdown
  47. * CCheckList::Term();
  48. *
  49. *****************************************************************************/
  50. #include <windows.h>
  51. #include <commctrl.h>
  52. #include <comdef.h>
  53. #include "crtdbg.h"
  54. #include "resource.h"
  55. #include "chklst.h"
  56. #ifndef STATEIMAGEMASKTOINDEX
  57. #define STATEIMAGEMASKTOINDEX(i) ((i & LVIS_STATEIMAGEMASK) >> 12)
  58. #endif
  59. HIMAGELIST g_himlState;
  60. /*****************************************************************************
  61. *
  62. * CCheckList::Init
  63. *
  64. * One-time initialization. Call this at app startup.
  65. *
  66. * IDB_CHECK should refer to chk.bmp.
  67. *
  68. *****************************************************************************/
  69. extern HINSTANCE g_hInst;
  70. BOOL WINAPI
  71. CCheckList::Init(HWND hwnd)
  72. {
  73. ListView_DeleteAllItems(hwnd);
  74. #ifdef USE_BITMAP_FOR_IMAGES
  75. g_himlState = ImageList_LoadImage(g_hInst, MAKEINTRESOURCE(IDB_CHECK),
  76. 0, 2, RGB(0xFF, 0x00, 0xFF),
  77. IMAGE_BITMAP, 0);
  78. #else
  79. g_himlState = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
  80. GetSystemMetrics(SM_CYSMICON), ILC_COLOR4 , 1, 1);
  81. HICON hiconItem; // icon for list view items
  82. // Add an icon to each image list.
  83. hiconItem = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_BLANK));
  84. ImageList_AddIcon(g_himlState, hiconItem);
  85. hiconItem = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_CHECKED));
  86. ImageList_AddIcon(g_himlState, hiconItem);
  87. hiconItem = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_GRAYCHECKED));
  88. ImageList_AddIcon(g_himlState, hiconItem);
  89. DeleteObject(hiconItem);
  90. #endif USE_BITMAP_FOR_IMAGES
  91. // ListView_SetExtendedListViewStyleEx(hwnd, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
  92. ListView_SetImageList(hwnd, g_himlState, LVSIL_SMALL );
  93. return (BOOL)g_himlState;
  94. }
  95. /*****************************************************************************
  96. *
  97. * CCheckList::Term
  98. *
  99. * One-time shutdown. Call this at app termination.
  100. *
  101. *****************************************************************************/
  102. void WINAPI
  103. CCheckList::Term(void)
  104. {
  105. if (g_himlState) {
  106. ImageList_Destroy(g_himlState);
  107. }
  108. }
  109. /*****************************************************************************
  110. *
  111. * CCheckList::AddString
  112. *
  113. * Add a string and a checkbox.
  114. *
  115. *****************************************************************************/
  116. int WINAPI
  117. CCheckList::AddString(HWND hwnd, LPTSTR ptszText, PSID pSID, LONG lSidLength, CHKMARK chkmrk)
  118. {
  119. LV_ITEM lvi;
  120. ZeroMemory(&lvi, sizeof(lvi));
  121. pSid9X *ppSID9X = new pSid9X;
  122. ppSID9X->length = lSidLength;
  123. ppSID9X->psid = pSID;
  124. lvi.pszText = ptszText;
  125. lvi.lParam = (LONG)ppSID9X;
  126. #ifdef USE_BITMAP_FOR_IMAGES
  127. lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
  128. lvi.state = INDEXTOSTATEIMAGEMASK(chkmrk);
  129. lvi.stateMask = LVIS_STATEIMAGEMASK;
  130. #else
  131. lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
  132. lvi.iImage = chkmrk;
  133. #endif USE_BITMAP_FOR_IMAGES
  134. lvi.iItem = ListView_GetItemCount(hwnd);
  135. return ListView_InsertItem(hwnd, &lvi);
  136. }
  137. /*****************************************************************************
  138. *
  139. * CCheckList::Mark
  140. *
  141. * Check or Uncheck a checkbox.
  142. *
  143. *****************************************************************************/
  144. BOOL WINAPI
  145. CCheckList::Mark(HWND hwnd, int item, CHKMARK chkmrk)
  146. {
  147. LV_ITEM lvi;
  148. ZeroMemory(&lvi, sizeof(lvi));
  149. #ifdef USE_BITMAP_FOR_IMAGES
  150. lvi.mask = LVIF_STATE;
  151. lvi.state = INDEXTOSTATEIMAGEMASK(chkmrk);
  152. lvi.stateMask = LVIS_STATEIMAGEMASK;
  153. #else
  154. lvi.mask = LVIF_IMAGE;
  155. lvi.iImage = chkmrk;
  156. #endif USE_BITMAP_FOR_IMAGES
  157. lvi.iItem = item;
  158. return ListView_SetItem(hwnd, &lvi);
  159. }
  160. /*****************************************************************************
  161. *
  162. * CCheckList::InitFinish
  163. *
  164. * Wind up the initialization. Do this after you've added all the
  165. * strings you plan on adding.
  166. *
  167. *****************************************************************************/
  168. void WINAPI
  169. CCheckList::InitFinish(HWND hwnd)
  170. {
  171. RECT rc;
  172. LV_COLUMN col;
  173. int icol;
  174. /*
  175. * Add the one and only column.
  176. */
  177. GetClientRect(hwnd, &rc);
  178. col.mask = LVCF_WIDTH;
  179. col.cx = rc.right;
  180. icol = ListView_InsertColumn(hwnd, 0, &col);
  181. ListView_SetColumnWidth(hwnd, icol, LVSCW_AUTOSIZE);
  182. }
  183. /*****************************************************************************
  184. *
  185. * CCheckList::GetName
  186. *
  187. *****************************************************************************/
  188. void WINAPI
  189. CCheckList::GetName(HWND hwnd, int iItem, LPTSTR lpsName, int cchTextMax)
  190. {
  191. ListView_GetItemText(hwnd, iItem, 0, lpsName, cchTextMax);
  192. }
  193. /*****************************************************************************
  194. *
  195. * CCheckList::GetSID
  196. *
  197. *****************************************************************************/
  198. void WINAPI
  199. CCheckList::GetSID(HWND hwnd, int iItem, PSID* ppSID, LONG *plengthSID)
  200. {
  201. LV_ITEM lvi;
  202. ZeroMemory(&lvi, sizeof(lvi));
  203. lvi.mask = LVIF_PARAM;
  204. lvi.iItem = iItem;
  205. ListView_GetItem(hwnd, &lvi);
  206. if (lvi.lParam)
  207. {
  208. *ppSID = ((pSid9X *)(lvi.lParam))->psid;
  209. *plengthSID = ((pSid9X *)(lvi.lParam))->length;
  210. }
  211. }
  212. /*****************************************************************************
  213. *
  214. * CCheckList::GetState
  215. *
  216. * Read the state of a checklist item
  217. *
  218. *****************************************************************************/
  219. CHKMARK WINAPI
  220. CCheckList::GetState(HWND hwnd, int iItem)
  221. {
  222. LV_ITEM lvi;
  223. ZeroMemory(&lvi, sizeof(lvi));
  224. lvi.iItem = iItem;
  225. #ifdef USE_BITMAP_FOR_IMAGES
  226. lvi.mask = LVIF_STATE;
  227. lvi.stateMask = LVIS_STATEIMAGEMASK;
  228. ListView_GetItem(hwnd, &lvi);
  229. return (CHKMARK)STATEIMAGEMASKTOINDEX(lvi.state);
  230. #else
  231. lvi.mask = LVIF_IMAGE;
  232. ListView_GetItem(hwnd, &lvi);
  233. return (CHKMARK)lvi.iImage;
  234. #endif USE_BITMAP_FOR_IMAGES
  235. }
  236. /*****************************************************************************
  237. *
  238. * CCheckList::SetState
  239. *
  240. * Sets the state of a checklist item
  241. *
  242. *****************************************************************************/
  243. BOOL WINAPI
  244. CCheckList::SetState(HWND hwnd, int iItem, CHKMARK chkmrk)
  245. {
  246. LV_ITEM lvi;
  247. ZeroMemory(&lvi, sizeof(lvi));
  248. lvi.iItem = iItem;
  249. #ifdef USE_BITMAP_FOR_IMAGES
  250. lvi.mask = LVIF_STATE;
  251. lvi.state = INDEXTOSTATEIMAGEMASK(chkmrk);
  252. lvi.stateMask = LVIS_STATEIMAGEMASK;
  253. #else
  254. lvi.mask = LVIF_IMAGE;
  255. lvi.iImage = chkmrk;
  256. #endif USE_BITMAP_FOR_IMAGES
  257. return ListView_SetItem(hwnd, &lvi);
  258. }
  259. /*****************************************************************************
  260. *
  261. * CCheckList::OnDestroy
  262. *
  263. * Clean up a checklist. Call this before destroying the window.
  264. *
  265. *****************************************************************************/
  266. void WINAPI
  267. CCheckList::OnDestroy(HWND hwnd)
  268. {
  269. BOOL fRes = FALSE;
  270. LV_ITEM lvi;
  271. ZeroMemory(&lvi, sizeof(lvi));
  272. lvi.mask = LVIF_PARAM;
  273. DWORD dwNumSAUsers = ListView_GetItemCount(hwnd);
  274. PSID psidSAUsers;
  275. for(DWORD i=0; i<dwNumSAUsers; i++)
  276. {
  277. lvi.iItem = i;
  278. ListView_GetItem(hwnd, &lvi);
  279. pSid9X *ppSID9X = (pSid9X *)lvi.lParam;
  280. psidSAUsers = ppSID9X->psid;
  281. fRes = HeapFree(GetProcessHeap(), 0, psidSAUsers);
  282. _ASSERTE(fRes);
  283. delete ppSID9X;
  284. }
  285. }