DlgTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // DlgTest.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "test_list_control.h"
  5. #include "DlgTest.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDlgTest dialog
  13. CDlgTest::CDlgTest(CWnd* pParent /*=NULL*/)
  14. : CDialog(CDlgTest::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CDlgTest)
  17. //}}AFX_DATA_INIT
  18. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  19. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  20. m_LabelCount=0;
  21. }
  22. CDlgTest::~CDlgTest()
  23. {
  24. m_arLabels.RemoveAll();
  25. }
  26. void CDlgTest::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. //{{AFX_DATA_MAP(CDlgTest)
  30. DDX_Control(pDX, IDC_LIST2, m_List2);
  31. DDX_Control(pDX, IDC_LIST1, m_List);
  32. //}}AFX_DATA_MAP
  33. }
  34. BEGIN_MESSAGE_MAP(CDlgTest, CDialog)
  35. //{{AFX_MSG_MAP(CDlgTest)
  36. ON_WM_PAINT()
  37. ON_WM_QUERYDRAGICON()
  38. ON_BN_CLICKED(IDC_ADD, OnAdd)
  39. ON_NOTIFY(LVN_GETDISPINFO, IDC_LIST1, GetDispInfo)
  40. ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColClick)
  41. ON_NOTIFY(LVN_ODFINDITEM, IDC_LIST1, OnOdfinditem)
  42. ON_BN_CLICKED(IDC_ADD2, OnAdd2Normal)
  43. ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST2, OnColClick2)
  44. //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CDlgTest message handlers
  48. BOOL CDlgTest::OnInitDialog()
  49. {
  50. CDialog::OnInitDialog();
  51. // Set the icon for this dialog. The framework does this automatically
  52. // when the application's main window is not a dialog
  53. SetIcon(m_hIcon, TRUE); // Set big icon
  54. SetIcon(m_hIcon, FALSE); // Set small icon
  55. // Insert the columns.
  56. CString Header;
  57. int arColWidth[]={80,100};
  58. int iNumCols = 2;
  59. for(int i=0; i<iNumCols; i++)
  60. {
  61. Header.LoadString(IDS_LISTCOL+i);
  62. m_List.InsertColumn(i,Header,LVCFMT_LEFT,arColWidth[i]);
  63. m_List2.InsertColumn(i,Header,LVCFMT_LEFT,arColWidth[i]);
  64. }
  65. // Configure the break icon array.
  66. m_ImageList.Create(16, 16, ILC_COLOR4, 3, 1);
  67. m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_BP_ENABLED));
  68. m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_BP_DISABLED));
  69. m_ImageList.Add(::AfxGetApp()->LoadIcon(IDI_BP_NONE));
  70. m_List.SetImageList(&m_ImageList, LVSIL_SMALL);
  71. // Configure the look & feel.
  72. //const int LVS_EX_LABELTIP = 0x00004000;
  73. m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_LABELTIP);
  74. m_List2.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_LABELTIP);
  75. return TRUE; // return TRUE unless you set the focus to a control
  76. }
  77. // If you add a minimize button to your dialog, you will need the code below
  78. // to draw the icon. For MFC applications using the document/view model,
  79. // this is automatically done for you by the framework.
  80. void CDlgTest::OnPaint()
  81. {
  82. if (IsIconic())
  83. {
  84. CPaintDC dc(this); // device context for painting
  85. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  86. // Center icon in client rectangle
  87. int cxIcon = GetSystemMetrics(SM_CXICON);
  88. int cyIcon = GetSystemMetrics(SM_CYICON);
  89. CRect rect;
  90. GetClientRect(&rect);
  91. int x = (rect.Width() - cxIcon + 1) / 2;
  92. int y = (rect.Height() - cyIcon + 1) / 2;
  93. // Draw the icon
  94. dc.DrawIcon(x, y, m_hIcon);
  95. }
  96. else
  97. {
  98. CDialog::OnPaint();
  99. }
  100. }
  101. // The system calls this to obtain the cursor to display while the user drags
  102. // the minimized window.
  103. HCURSOR CDlgTest::OnQueryDragIcon()
  104. {
  105. return (HCURSOR) m_hIcon;
  106. }
  107. /**************************************************************************
  108. Code for virtual list control.
  109. *************************************************************************/
  110. void CDlgTest::OnAdd()
  111. {
  112. // Fill class data from dialog.
  113. UpdateData(TRUE);
  114. // Define a set of pseudo fn names for example.
  115. char* pszNames[] = {"main","_main","Startup","Fn1","Fn2"};
  116. CString strText;
  117. // DWORD start=GetTickCount();
  118. int i;
  119. ::AfxGetApp()->DoWaitCursor(1);
  120. // Reset column headers (clear sort order ^).
  121. LVCOLUMN Col;
  122. Col.mask=LVCF_TEXT;
  123. for(i=0; i<2; i++)
  124. {
  125. strText.LoadString(IDS_LISTCOL+i);
  126. Col.pszText=strText.GetBuffer(strText.GetLength()+1);
  127. Col.cchTextMax=strText.GetLength()+1;
  128. m_List.SetColumn(i, &Col);
  129. strText.ReleaseBuffer();
  130. }
  131. // Clear and reset the label array.
  132. m_arLabels.RemoveAll();
  133. m_arLabels.SetSize( 5000, 1000 );
  134. // Add the 50,000 items.
  135. CLabelItem Label;
  136. for(i=0; i<50000; i++)
  137. {
  138. Label.m_strText=pszNames[i%5];
  139. Label.m_Addr=((DWORD)100-(i<<1));
  140. m_arLabels.SetAtGrow(i, Label);
  141. }
  142. m_LabelCount=i;
  143. // Tell the list box to update itself.
  144. m_List.SetItemCountEx(m_LabelCount);
  145. m_List.Invalidate();
  146. ::AfxGetApp()->DoWaitCursor(0);
  147. // strText.Format("Time to complete fill was:\n%li ticks", GetTickCount()-start);
  148. // ::AfxMessageBox(strText);
  149. }
  150. void CDlgTest::GetDispInfo(NMHDR* pNMHDR, LRESULT* pResult)
  151. {
  152. LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
  153. LV_ITEM* pItem= &(pDispInfo)->item;
  154. CLabelItem rLabel = m_arLabels.ElementAt(pItem->iItem);
  155. if (pItem->mask & LVIF_TEXT) //valid text buffer?
  156. {
  157. // then display the appropriate column
  158. switch(pItem->iSubItem)
  159. {
  160. case 0:
  161. lstrcpy(pItem->pszText, rLabel.m_strText);
  162. break;
  163. case 1:
  164. sprintf(pItem->pszText, "0x%08LX", rLabel.m_Addr);
  165. break;
  166. default:
  167. ASSERT(0);
  168. break;
  169. }
  170. }
  171. if (pItem->mask & LVIF_IMAGE)
  172. {
  173. // then display the appropriate column
  174. switch(pItem->iSubItem)
  175. {
  176. case 0:
  177. {
  178. /* long index;
  179. BOOL iBPState
  180. BPMgr.Find(rLabel.m_Addr, &index, &iBPState);
  181. */
  182. switch(rLabel.m_Addr)
  183. {
  184. case 0x00000004:
  185. pItem->iImage = 0;
  186. break;
  187. case 0x00000006:
  188. pItem->iImage = 1;
  189. break;
  190. default:
  191. pItem->iImage = 2;
  192. break;
  193. }
  194. }
  195. break;
  196. default:
  197. ASSERT(0);
  198. break;
  199. }
  200. }
  201. *pResult = 0;
  202. }
  203. // This function is called when the column headers are clicked.
  204. // (The event occurs on the mouseup event.)
  205. // Sort the array by the appropriate column contents.
  206. //
  207. void CDlgTest::OnColClick(NMHDR* pNMHDR, LRESULT* pResult)
  208. {
  209. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  210. LVCOLUMN Col;
  211. CString ColName;
  212. // Set text only.
  213. Col.mask=LVCF_TEXT;
  214. // Reset the header columns (to indicate the sort order).
  215. for(int i = 0; i < 2; i++)
  216. {
  217. if( i == pNMListView->iSubItem)
  218. {
  219. ColName.LoadString(IDS_LISTCOLSEL + i);
  220. SortByCol(i);
  221. }
  222. else
  223. ColName.LoadString(IDS_LISTCOL + i);
  224. Col.pszText=ColName.GetBuffer(ColName.GetLength()+1);
  225. Col.cchTextMax=ColName.GetLength()+1;
  226. m_List.SetColumn(i, &Col);
  227. ColName.ReleaseBuffer();
  228. }
  229. // Redraw the list.
  230. m_List.Invalidate();
  231. *pResult = 0;
  232. }
  233. //////////////////////////////////////////////////////////////////////////////
  234. // Compares two label items by their public data members.
  235. // Used by qsort algorithm.
  236. // Must return:
  237. // < 0 elem1 > elem2
  238. // 0 elem1 = elem2
  239. // > 0 elem1 < elem2
  240. //
  241. int __cdecl CompareByLabelName(const void *elem1, const void *elem2)
  242. {
  243. CLabelItem *p1 = (CLabelItem*)elem1;
  244. CLabelItem *p2 = (CLabelItem*)elem2;
  245. return strcmp(p1->m_strText,p2->m_strText);
  246. }
  247. int __cdecl CompareByLabelAddr(const void *elem1, const void *elem2)
  248. {
  249. CLabelItem *p1 = (CLabelItem*)elem1;
  250. CLabelItem *p2 = (CLabelItem*)elem2;
  251. if(p1->m_Addr == p2->m_Addr) return 0;
  252. if(p1->m_Addr > p2->m_Addr) return 1;
  253. return -1;
  254. }
  255. void CDlgTest::SortByCol(const int ColIndex)
  256. {
  257. switch(ColIndex)
  258. {
  259. case 0: // Name - this is relatively slow, so show an hourglass.
  260. ::AfxGetApp()->DoWaitCursor(TRUE);
  261. qsort( static_cast<void*>(&m_arLabels[0]), m_LabelCount, sizeof(CLabelItem), CompareByLabelName );
  262. ::AfxGetApp()->DoWaitCursor(FALSE);
  263. break;
  264. case 1: // Address.
  265. qsort( static_cast<void*>(&m_arLabels[0]), m_LabelCount, sizeof(CLabelItem), CompareByLabelAddr );
  266. break;
  267. default:
  268. ASSERT(0);
  269. break;
  270. }
  271. }
  272. // Handle the quick key event.
  273. // Move to the next item starting with the pressed key.
  274. // Searches from the current item + 1 to the end, then starts from 0 to the current item.
  275. //
  276. void CDlgTest::OnOdfinditem(NMHDR* pNMHDR, LRESULT* pResult)
  277. {
  278. NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)pNMHDR;
  279. LVFINDINFO FindItem = pFindInfo->lvfi;
  280. int i;
  281. if(FindItem.flags & LVFI_STRING)
  282. {
  283. TCHAR chKeyL = FindItem.psz[0];
  284. TCHAR chKeyH = _toupper(FindItem.psz[0]);;
  285. // Search to end.
  286. for( i = pFindInfo->iStart; i < m_LabelCount; i++ )
  287. {
  288. if( ( chKeyL == m_arLabels[i].m_strText[0] )
  289. || ( chKeyH == m_arLabels[i].m_strText[0] )
  290. )
  291. {
  292. *pResult = i;
  293. return;
  294. }
  295. }
  296. // Search from 0 to start.
  297. for( i = 0; i < pFindInfo->iStart; i++ )
  298. {
  299. if( ( chKeyL == m_arLabels[i].m_strText[0] )
  300. || ( chKeyH == m_arLabels[i].m_strText[0] )
  301. )
  302. {
  303. *pResult = i;
  304. return;
  305. }
  306. }
  307. }
  308. *pResult = -1; // Default action.
  309. }
  310. /**************************************************************************
  311. Code for normal list control.
  312. *************************************************************************/
  313. void CDlgTest::OnAdd2Normal()
  314. {
  315. // Fill class data from dialog.
  316. UpdateData(TRUE);
  317. // Define a set of pseudo fn names for example.
  318. char* pszNames[] = {"main","_main","Startup","Fn1","Fn2"};
  319. CString strText;
  320. // DWORD start=GetTickCount();
  321. int i;
  322. ::AfxGetApp()->DoWaitCursor(1);
  323. // Reset column headers (clear sort order ^).
  324. LVCOLUMN Col;
  325. Col.mask=LVCF_TEXT;
  326. for(i=0; i<2; i++)
  327. {
  328. strText.LoadString(IDS_LISTCOL+i);
  329. Col.pszText=strText.GetBuffer(strText.GetLength()+1);
  330. Col.cchTextMax=strText.GetLength()+1;
  331. m_List.SetColumn(i, &Col);
  332. strText.ReleaseBuffer();
  333. }
  334. // Clear and reset the list control content.
  335. m_List2.DeleteAllItems();
  336. // Add the 50,000 items.
  337. CString strAddr;
  338. int index;
  339. for(i=0; i<50000; i++)
  340. {
  341. index=m_List2.InsertItem(i, pszNames[i%5]);
  342. if(-1!=index)
  343. {
  344. strAddr.Format("0x%08lX", 100-(i<<1) );
  345. m_List2.SetItemText(index, 1, strAddr);
  346. m_List2.SetItemData(index, index); // Needed for sort algorithm.
  347. }
  348. }
  349. ::AfxGetApp()->DoWaitCursor(0);
  350. // strText.Format("Time to complete fill was:\n%li ticks", GetTickCount()-start);
  351. // ::AfxMessageBox(strText);
  352. }
  353. void CDlgTest::OnColClick2(NMHDR* pNMHDR, LRESULT* pResult)
  354. {
  355. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  356. LVCOLUMN Col;
  357. CString ColName;
  358. // Set text only.
  359. Col.mask=LVCF_TEXT;
  360. // Reset the header columns (to indicate the sort order).
  361. for(int i = 0; i < 2; i++)
  362. {
  363. if( i == pNMListView->iSubItem)
  364. {
  365. ColName.LoadString(IDS_LISTCOLSEL + i);
  366. Sort2ByCol(i);
  367. }
  368. else
  369. ColName.LoadString(IDS_LISTCOL + i);
  370. Col.pszText=ColName.GetBuffer(ColName.GetLength()+1);
  371. Col.cchTextMax=ColName.GetLength()+1;
  372. m_List2.SetColumn(i, &Col);
  373. ColName.ReleaseBuffer();
  374. }
  375. // Redraw the list.
  376. m_List2.Invalidate();
  377. *pResult = 0;
  378. }
  379. int CALLBACK Compare2ByLabelName(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  380. {
  381. CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
  382. CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
  383. CString strItem2 = pListCtrl->GetItemText(lParam2, 0);
  384. return strcmp(strItem2, strItem1);
  385. }
  386. int CALLBACK Compare2ByLabelAddr(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  387. {
  388. CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
  389. DWORD Addr1 = (DWORD)strtoul(pListCtrl->GetItemText(lParam1, 1),NULL,16);
  390. DWORD Addr2 = (DWORD)strtoul(pListCtrl->GetItemText(lParam2, 1),NULL,16);
  391. if(Addr1 == Addr2) return 0;
  392. if(Addr1 > Addr2) return 1;
  393. return -1;
  394. }
  395. void CDlgTest::Sort2ByCol(const int ColIndex)
  396. {
  397. switch(ColIndex)
  398. {
  399. case 0: // Name - this is relatively slow, so show an hourglass.
  400. ::AfxGetApp()->DoWaitCursor(TRUE);
  401. m_List2.SortItems( Compare2ByLabelName, (DWORD)&m_List2 );
  402. ::AfxGetApp()->DoWaitCursor(FALSE);
  403. break;
  404. case 1: // Address.
  405. ::AfxGetApp()->DoWaitCursor(TRUE);
  406. m_List2.SortItems( Compare2ByLabelAddr, (DWORD)&m_List2 );
  407. ::AfxGetApp()->DoWaitCursor(FALSE);
  408. break;
  409. default:
  410. ASSERT(0);
  411. break;
  412. }
  413. }