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.

322 lines
11 KiB

  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <commctrl.h>
  4. #include <comctrlp.h>
  5. #include <objbase.h>
  6. #include <wiadebug.h>
  7. #include <simcrack.h>
  8. #include "resource.h"
  9. #include "dbgcdraw.h"
  10. #include "chklistv.h"
  11. #include "lvprops.h"
  12. static HINSTANCE g_hInstance;
  13. static const int c_nMaxImages = 20;
  14. static const int c_nAdditionalMarginX = 10;
  15. static const int c_nAdditionalMarginY = 10;
  16. class CListviewTestDialog
  17. {
  18. private:
  19. HWND m_hWnd;
  20. CCheckedListviewHandler m_CheckedListviewHandler;
  21. SIZE m_sizeImage;
  22. SIZE m_sizeMargin;
  23. private:
  24. CListviewTestDialog(void);
  25. CListviewTestDialog( const CListviewTestDialog & );
  26. CListviewTestDialog &operator=( const CListviewTestDialog & );
  27. private:
  28. explicit CListviewTestDialog( HWND hWnd )
  29. : m_hWnd(hWnd)
  30. {
  31. ZeroMemory(&m_sizeImage,sizeof(SIZE));
  32. m_sizeMargin.cx = c_nAdditionalMarginX;
  33. m_sizeMargin.cy = c_nAdditionalMarginY;
  34. }
  35. ~CListviewTestDialog(void)
  36. {
  37. }
  38. LRESULT OnListClick( WPARAM wParam, LPARAM lParam )
  39. {
  40. m_CheckedListviewHandler.HandleListClick( wParam, lParam );
  41. return 0;
  42. }
  43. LRESULT OnListDblClk( WPARAM wParam, LPARAM lParam )
  44. {
  45. m_CheckedListviewHandler.HandleListDblClk( wParam, lParam );
  46. return 0;
  47. }
  48. LRESULT OnGetCheckState( WPARAM wParam, LPARAM lParam )
  49. {
  50. LRESULT lResult = 0;
  51. NMGETCHECKSTATE *pNmGetCheckState = reinterpret_cast<NMGETCHECKSTATE*>(lParam);
  52. if (pNmGetCheckState)
  53. {
  54. LVITEM LvItem = {0};
  55. LvItem.iItem = pNmGetCheckState->nItem;
  56. LvItem.mask = LVIF_PARAM;
  57. if (ListView_GetItem( pNmGetCheckState->hdr.hwndFrom, &LvItem ))
  58. {
  59. if (LvItem.lParam)
  60. {
  61. lResult = LVCHECKSTATE_CHECKED;
  62. }
  63. else
  64. {
  65. lResult = LVCHECKSTATE_UNCHECKED;
  66. }
  67. }
  68. }
  69. return lResult;
  70. }
  71. LRESULT OnSetCheckState( WPARAM wParam, LPARAM lParam )
  72. {
  73. LRESULT lResult = 0;
  74. NMSETCHECKSTATE *pNmSetCheckState = reinterpret_cast<NMSETCHECKSTATE*>(lParam);
  75. if (pNmSetCheckState)
  76. {
  77. LVITEM LvItem = {0};
  78. LvItem.mask = LVIF_PARAM;
  79. LvItem.iItem = pNmSetCheckState->nItem;
  80. LvItem.lParam = (pNmSetCheckState->nCheck == LVCHECKSTATE_CHECKED) ? 1 : 0;
  81. ListView_SetItem( pNmSetCheckState->hdr.hwndFrom, &LvItem );
  82. }
  83. return 0;
  84. }
  85. LRESULT OnListCustomDraw( WPARAM wParam, LPARAM lParam )
  86. {
  87. LRESULT lResult = CDRF_DODEFAULT;
  88. m_CheckedListviewHandler.HandleListCustomDraw( wParam, lParam, lResult );
  89. return lResult;
  90. }
  91. LRESULT OnListKeyDown( WPARAM wParam, LPARAM lParam )
  92. {
  93. LRESULT lResult = FALSE;
  94. m_CheckedListviewHandler.HandleListKeyDown( wParam, lParam, lResult );
  95. return lResult;
  96. }
  97. LRESULT OnInitDialog( WPARAM, LPARAM )
  98. {
  99. HWND hwndList = GetDlgItem( m_hWnd, IDC_LIST );
  100. if (hwndList)
  101. {
  102. //
  103. // Attach this control to the checkbox handler
  104. //
  105. m_CheckedListviewHandler.Attach(hwndList);
  106. //
  107. // Load an image for the image list
  108. //
  109. struct
  110. {
  111. UINT nResId;
  112. HBITMAP hBitmap;
  113. int nImageIndex;
  114. } Images[] =
  115. {
  116. { IDB_IMAGE1, NULL, 0 },
  117. { IDB_IMAGE2, NULL, 0 },
  118. { IDB_IMAGE3, NULL, 0 },
  119. { IDB_IMAGE4, NULL, 0 },
  120. { IDB_IMAGE5, NULL, 0 }
  121. };
  122. WIA_TRACE((TEXT("line: %d"), __LINE__ ));
  123. bool bSuccess = true;
  124. for (int i=0;i<ARRAYSIZE(Images) && bSuccess;i++)
  125. {
  126. Images[i].hBitmap = reinterpret_cast<HBITMAP>(LoadImage(g_hInstance,MAKEINTRESOURCE(Images[i].nResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION ));
  127. if (!Images[i].hBitmap)
  128. {
  129. bSuccess = false;
  130. }
  131. }
  132. if (bSuccess)
  133. {
  134. //
  135. // Get the image's dimensions
  136. //
  137. BITMAP bm = {0};
  138. if (GetObject( Images[0].hBitmap, sizeof(BITMAP), &bm ))
  139. {
  140. m_sizeImage.cx = bm.bmWidth;
  141. m_sizeImage.cy = bm.bmHeight;
  142. //
  143. // Create the image list
  144. //
  145. HIMAGELIST hImageList = ImageList_Create( m_sizeImage.cx, m_sizeImage.cy, ILC_COLOR24, 1, 1 );
  146. if (hImageList)
  147. {
  148. //
  149. // Set the image list
  150. //
  151. ListView_SetImageList( hwndList, hImageList, LVSIL_NORMAL );
  152. //
  153. // Add the image to the image list
  154. //
  155. bSuccess = true;
  156. for (int i=0;i<ARRAYSIZE(Images) && bSuccess;i++)
  157. {
  158. WIA_TRACE((TEXT("line: %d"), __LINE__ ));
  159. Images[i].nImageIndex = ImageList_Add( hImageList, Images[i].hBitmap, NULL );
  160. if (-1 == Images[i].nImageIndex)
  161. {
  162. bSuccess = false;
  163. }
  164. }
  165. if (bSuccess)
  166. {
  167. WIA_TRACE((TEXT("line: %d"), __LINE__ ));
  168. //
  169. // Tell the listview we don't want labels, and want border selection
  170. //
  171. ListView_SetExtendedListViewStyleEx( hwndList, LVS_EX_DOUBLEBUFFER|LVS_EX_BORDERSELECT|LVS_EX_HIDELABELS|0x00100000|LVS_EX_CHECKBOXES, LVS_EX_DOUBLEBUFFER|LVS_EX_BORDERSELECT|LVS_EX_HIDELABELS|0x00100000|LVS_EX_CHECKBOXES );
  172. ListView_SetIconSpacing( hwndList, m_sizeImage.cx + m_sizeMargin.cx, m_sizeImage.cy + m_sizeMargin.cy );
  173. //
  174. // Insert a few items
  175. //
  176. for (int i=0;i<c_nMaxImages;i++)
  177. {
  178. LVITEM LvItem = {0};
  179. LvItem.mask = LVIF_IMAGE;
  180. LvItem.iImage = Images[i%ARRAYSIZE(Images)].nImageIndex;
  181. LvItem.iItem = i;
  182. ListView_InsertItem( hwndList, &LvItem );
  183. }
  184. //
  185. // Select the first item
  186. //
  187. ListView_SetItemState( hwndList, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED );
  188. }
  189. }
  190. }
  191. }
  192. }
  193. return 0;
  194. }
  195. void OnOK( WPARAM wParam, LPARAM )
  196. {
  197. EndDialog(m_hWnd,LOWORD(wParam));
  198. }
  199. void OnCancel( WPARAM wParam, LPARAM )
  200. {
  201. EndDialog(m_hWnd,LOWORD(wParam));
  202. }
  203. void OnSelectCurr( WPARAM, LPARAM )
  204. {
  205. int nCurrItem = -1;
  206. while (true)
  207. {
  208. nCurrItem = ListView_GetNextItem( GetDlgItem(m_hWnd,IDC_LIST), nCurrItem, LVNI_SELECTED );
  209. if (nCurrItem < 0)
  210. {
  211. break;
  212. }
  213. m_CheckedListviewHandler.Select( GetDlgItem(m_hWnd,IDC_LIST), nCurrItem, LVCHECKSTATE_CHECKED );
  214. }
  215. }
  216. void OnSelectAll( WPARAM, LPARAM )
  217. {
  218. m_CheckedListviewHandler.Select( GetDlgItem(m_hWnd,IDC_LIST), -1, LVCHECKSTATE_CHECKED );
  219. }
  220. void OnSelectNone( WPARAM, LPARAM )
  221. {
  222. m_CheckedListviewHandler.Select( GetDlgItem(m_hWnd,IDC_LIST), -1, LVCHECKSTATE_UNCHECKED );
  223. }
  224. void OnProperties( WPARAM, LPARAM )
  225. {
  226. CListviewPropsDialog::CData Data;
  227. Data.bFullItemSelect = m_CheckedListviewHandler.FullImageHit();
  228. Data.sizeItemSpacing.cx = m_sizeMargin.cx;
  229. Data.sizeItemSpacing.cy = m_sizeMargin.cy;
  230. INT_PTR nRes = DialogBoxParam(g_hInstance,MAKEINTRESOURCE(IDD_LISTVIEW_PROPS_DIALOG), NULL, CListviewPropsDialog::DialogProc, reinterpret_cast<LPARAM>(&Data) );
  231. if (IDOK == nRes)
  232. {
  233. m_CheckedListviewHandler.FullImageHit(Data.bFullItemSelect);
  234. m_sizeMargin.cx = Data.sizeItemSpacing.cx;
  235. m_sizeMargin.cy = Data.sizeItemSpacing.cy;
  236. if (Data.bCustomIcon)
  237. {
  238. m_CheckedListviewHandler.SetCheckboxImages( (HICON)LoadImage( g_hInstance, MAKEINTRESOURCE(IDI_CHECKED), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR ), (HICON)LoadImage( g_hInstance, MAKEINTRESOURCE(IDI_UNCHECKED), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR ) );
  239. }
  240. else
  241. {
  242. m_CheckedListviewHandler.CreateDefaultCheckBitmaps();
  243. }
  244. ListView_SetIconSpacing( GetDlgItem(m_hWnd,IDC_LIST), m_sizeImage.cx + m_sizeMargin.cx, m_sizeImage.cy + m_sizeMargin.cy );
  245. InvalidateRect( GetDlgItem(m_hWnd,IDC_LIST), NULL, TRUE );
  246. UpdateWindow( GetDlgItem(m_hWnd,IDC_LIST) );
  247. }
  248. }
  249. LRESULT OnNotify( WPARAM wParam, LPARAM lParam )
  250. {
  251. SC_BEGIN_NOTIFY_MESSAGE_HANDLERS()
  252. {
  253. }
  254. SC_END_NOTIFY_MESSAGE_HANDLERS();
  255. }
  256. LRESULT OnCommand( WPARAM wParam, LPARAM lParam )
  257. {
  258. SC_BEGIN_COMMAND_HANDLERS()
  259. {
  260. SC_HANDLE_COMMAND(IDOK,OnOK);
  261. SC_HANDLE_COMMAND(IDCANCEL,OnCancel);
  262. SC_HANDLE_COMMAND(IDC_SELECTCURR,OnSelectCurr);
  263. SC_HANDLE_COMMAND(IDC_SELECTALL,OnSelectAll);
  264. SC_HANDLE_COMMAND(IDC_SELECTNONE,OnSelectNone);
  265. SC_HANDLE_COMMAND(IDC_PROPERTIES,OnProperties);
  266. }
  267. SC_END_COMMAND_HANDLERS();
  268. }
  269. public:
  270. static INT_PTR CALLBACK DialogProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  271. {
  272. SC_BEGIN_DIALOG_MESSAGE_HANDLERS(CListviewTestDialog)
  273. {
  274. SC_HANDLE_DIALOG_MESSAGE( WM_INITDIALOG, OnInitDialog );
  275. SC_HANDLE_DIALOG_MESSAGE( WM_COMMAND, OnCommand );
  276. SC_HANDLE_DIALOG_MESSAGE( WM_NOTIFY, OnNotify );
  277. }
  278. SC_END_DIALOG_MESSAGE_HANDLERS();
  279. }
  280. };
  281. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int )
  282. {
  283. WIA_DEBUG_CREATE( hInstance );
  284. g_hInstance = hInstance;
  285. InitCommonControls();
  286. DialogBox(hInstance,MAKEINTRESOURCE(IDD_LISTVIEW_TEST_DIALOG), NULL, CListviewTestDialog::DialogProc );
  287. WIA_DEBUG_DESTROY();
  288. return 0;
  289. }