QListCtrl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. // QListCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "QListCtrl.h"
  6. #include "ProcessPaste.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #define ROW_BOTTOM_BORDER 2
  13. #define ROW_LEFT_BORDER 3
  14. #define COLOR_SHADOW RGB(245, 245, 245)
  15. #define DUMMY_COL_WIDTH 1
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CQListCtrl
  18. CQListCtrl::CQListCtrl()
  19. {
  20. m_pchTip = NULL;
  21. m_pwchTip = NULL;
  22. LOGFONT lf;
  23. lf.lfHeight = -9;
  24. lf.lfWidth = 0;
  25. lf.lfEscapement = 0;
  26. lf.lfOrientation = 0;
  27. lf.lfWeight = FW_LIGHT;
  28. lf.lfItalic = FALSE;
  29. lf.lfUnderline = FALSE;
  30. lf.lfStrikeOut = FALSE;
  31. lf.lfCharSet = ANSI_CHARSET;
  32. lf.lfOutPrecision = OUT_STRING_PRECIS;
  33. lf.lfClipPrecision = CLIP_STROKE_PRECIS;
  34. lf.lfQuality = DEFAULT_QUALITY;
  35. lf.lfPitchAndFamily = VARIABLE_PITCH | FF_DONTCARE;
  36. lstrcpy(lf.lfFaceName, "Small Font");
  37. m_SmallFont = ::CreateFontIndirect(&lf);
  38. m_bShowTextForFirstTenHotKeys = true;
  39. m_bStartTop = true;
  40. }
  41. CQListCtrl::~CQListCtrl()
  42. {
  43. if(m_pchTip != NULL)
  44. delete m_pchTip;
  45. if(m_pwchTip != NULL)
  46. delete m_pwchTip;
  47. if( m_SmallFont )
  48. ::DeleteObject( m_SmallFont );
  49. DestroyAndCreateAccelerator(FALSE);
  50. }
  51. // returns the position 1-10 if the index is in the FirstTen block else -1
  52. int CQListCtrl::GetFirstTenNum( int index )
  53. {
  54. // set firstTenNum to the first ten number (1-10) corresponding to the given index
  55. int firstTenNum = -1; // -1 means that nItem is not in the FirstTen block.
  56. int count = GetItemCount();
  57. if( m_bStartTop )
  58. {
  59. if( 0 <= index && index <= 9 )
  60. firstTenNum = index + 1;
  61. }
  62. else // we are starting at the bottom and going up
  63. {
  64. int idxStartFirstTen = count-10; // start of the FirstTen block
  65. // if index is within the FirstTen block
  66. if( idxStartFirstTen <= index && index < count )
  67. firstTenNum = count - index;
  68. }
  69. return firstTenNum;
  70. }
  71. // returns the list index corresponding to the given FirstTen position number.
  72. // (ret < 0) means that "num" is not in the FirstTen block
  73. int CQListCtrl::GetFirstTenIndex( int num )
  74. {
  75. if( num <= 0 || num > 10 )
  76. return -1;
  77. if( m_bStartTop )
  78. return num-1;
  79. // else we are starting at the bottom and going up
  80. int count = GetItemCount();
  81. return count - num;
  82. }
  83. BEGIN_MESSAGE_MAP(CQListCtrl, CListCtrl)
  84. //{{AFX_MSG_MAP(CQListCtrl)
  85. ON_NOTIFY_REFLECT(LVN_KEYDOWN, OnKeydown)
  86. ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
  87. ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdrawList)
  88. ON_WM_SYSKEYDOWN()
  89. ON_WM_ERASEBKGND()
  90. ON_WM_CREATE()
  91. ON_WM_VSCROLL()
  92. ON_WM_HSCROLL()
  93. ON_WM_TIMER()
  94. ON_WM_WINDOWPOSCHANGED()
  95. ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnSelectionChange)
  96. ON_WM_SIZE()
  97. //}}AFX_MSG_MAP
  98. ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
  99. ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
  100. ON_WM_KILLFOCUS()
  101. END_MESSAGE_MAP()
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CQListCtrl message handlers
  104. void CQListCtrl::OnKeydown(NMHDR* pNMHDR, LRESULT* pResult)
  105. {
  106. LV_KEYDOWN* pLVKeyDown = (LV_KEYDOWN*)pNMHDR;
  107. switch (pLVKeyDown->wVKey)
  108. {
  109. case VK_RETURN:
  110. {
  111. ARRAY arr;
  112. GetSelectionIndexes(arr);
  113. SendSelection(arr);
  114. }
  115. break;
  116. case VK_ESCAPE:
  117. GetParent()->SendMessage(NM_END, 0, 0);
  118. break;
  119. case VK_RIGHT:
  120. {
  121. int nItem = GetNextItem(-1, LVNI_SELECTED);
  122. if (nItem != -1)
  123. GetParent()->SendMessage(NM_RIGHT, nItem, 0);
  124. }
  125. break;
  126. case VK_LEFT:
  127. GetParent()->SendMessage(NM_LEFT, 0, 0);
  128. break;
  129. case VK_DELETE:
  130. GetParent()->SendMessage(NM_DELETE, 0, 0);
  131. break;
  132. }
  133. *pResult = 0;
  134. }
  135. void CQListCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
  136. {
  137. LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE) pNMHDR;
  138. UINT Flags;
  139. int nItem = -1;
  140. if ((nItem = HitTest(lpnmItem->ptAction, &Flags)) != -1)
  141. {
  142. if (Flags | LVHT_ONITEM)
  143. SendSelection(nItem);
  144. }
  145. *pResult = 0;
  146. }
  147. void CQListCtrl::SendSelection(int nItem)
  148. {
  149. GetParent()->SendMessage(NM_SELECT, 1, (LPARAM) &nItem);
  150. }
  151. void CQListCtrl::SendSelection(ARRAY &arrItems)
  152. {
  153. GetParent()->SendMessage(NM_SELECT, arrItems.GetSize(), (LPARAM) arrItems.GetData());
  154. }
  155. void CQListCtrl::GetSelectionIndexes(ARRAY &arr)
  156. {
  157. arr.RemoveAll();
  158. POSITION pos = GetFirstSelectedItemPosition();
  159. while (pos)
  160. arr.Add(GetNextSelectedItem(pos));
  161. /*
  162. int nItem = GetNextItem(-1, LVNI_SELECTED);
  163. while (nItem != -1)
  164. {
  165. arr.Add(nItem);
  166. nItem = GetNextItem(nItem, LVNI_SELECTED);
  167. }
  168. */
  169. }
  170. void CQListCtrl::GetSelectionItemData(ARRAY &arr)
  171. {
  172. DWORD dwData;
  173. int i;
  174. arr.RemoveAll();
  175. POSITION pos = GetFirstSelectedItemPosition();
  176. while (pos)
  177. {
  178. i = GetNextSelectedItem(pos);
  179. dwData = GetItemData(i);
  180. arr.Add( dwData );
  181. }
  182. /*
  183. int nItem = GetNextItem(-1, LVNI_SELECTED);
  184. while (nItem != -1)
  185. {
  186. arr.Add((int)GetItemData(nItem));
  187. nItem = GetNextItem(nItem, LVNI_SELECTED);
  188. }
  189. */
  190. }
  191. void CQListCtrl::RemoveAllSelection()
  192. {
  193. POSITION pos = GetFirstSelectedItemPosition();
  194. while (pos)
  195. {
  196. SetSelection(GetNextSelectedItem(pos), FALSE);
  197. }
  198. }
  199. BOOL CQListCtrl::SetSelection(int nRow, BOOL bSelect)
  200. {
  201. if(bSelect)
  202. return SetItemState(nRow, LVIS_SELECTED, LVIS_SELECTED);
  203. else
  204. return SetItemState(nRow, ~LVIS_SELECTED, LVIS_SELECTED);
  205. }
  206. BOOL CQListCtrl::SetText(int nRow, int nCol, CString cs)
  207. {
  208. return SetItemText(nRow, nCol, cs);
  209. }
  210. BOOL CQListCtrl::SetCaret(int nRow, BOOL bFocus)
  211. {
  212. if(bFocus)
  213. return SetItemState(nRow, LVIS_FOCUSED, LVIS_FOCUSED);
  214. else
  215. return SetItemState(nRow, ~LVIS_FOCUSED, LVIS_FOCUSED);
  216. }
  217. long CQListCtrl::GetCaret()
  218. {
  219. return GetNextItem(-1, LVNI_FOCUSED);
  220. }
  221. // moves the caret to the given index, selects it, and ensures it is visible.
  222. BOOL CQListCtrl::SetListPos( int index )
  223. {
  224. if( index < 0 || index >= GetItemCount() )
  225. return FALSE;
  226. RemoveAllSelection();
  227. SetCaret(index);
  228. SetSelection(index);
  229. EnsureVisible(index,FALSE);
  230. return TRUE;
  231. }
  232. BOOL CQListCtrl::SetFormattedText(int nRow, int nCol, LPCTSTR lpszFormat,...)
  233. {
  234. CString csText;
  235. va_list vlist;
  236. ASSERT(AfxIsValidString(lpszFormat));
  237. va_start(vlist,lpszFormat);
  238. csText.FormatV(lpszFormat,vlist);
  239. va_end(vlist);
  240. return SetText(nRow,nCol,csText);
  241. }
  242. void CQListCtrl::SetNumberOfLinesPerRow(int nLines)
  243. {
  244. CDC *pDC = GetDC();
  245. CRect crRect(0, 0, 0, 0);
  246. CFont *pOldFont = pDC->SelectObject(GetFont());
  247. //Get the height to draw one character
  248. pDC->DrawText("W", crRect, DT_VCENTER | DT_EXPANDTABS | DT_CALCRECT);
  249. pDC->SelectObject(pOldFont);
  250. //Get the total height of each row
  251. int nHeight = (crRect.Height() * nLines) + ROW_BOTTOM_BORDER;
  252. //Create a image list of that height and set it to the list box
  253. CImageList imglist;
  254. imglist.Create(DUMMY_COL_WIDTH, nHeight, ILC_COLOR16 | ILC_MASK, 1, 1);
  255. SetImageList(&imglist, LVSIL_SMALL );
  256. }
  257. void CQListCtrl::OnCustomdrawList(NMHDR* pNMHDR, LRESULT* pResult)
  258. {
  259. NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
  260. *pResult = 0;
  261. // Request item-specific notifications if this is the
  262. // beginning of the paint cycle.
  263. if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
  264. {
  265. *pResult = CDRF_NOTIFYITEMDRAW;
  266. }
  267. else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
  268. {
  269. LVITEM rItem;
  270. int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
  271. CDC* pDC = CDC::FromHandle ( pLVCD->nmcd.hdc );
  272. COLORREF crBkgnd;
  273. BOOL bListHasFocus;
  274. CRect rcItem;
  275. bListHasFocus = ( GetSafeHwnd() == ::GetFocus() );
  276. // Get the image index and selected/focused state of the
  277. // item being drawn.
  278. ZeroMemory ( &rItem, sizeof(LVITEM) );
  279. rItem.mask = LVIF_STATE;
  280. rItem.iItem = nItem;
  281. rItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  282. GetItem(&rItem);
  283. // Get the rect that bounds the text label.
  284. GetItemRect(nItem, rcItem, LVIR_LABEL);
  285. rcItem.left -= DUMMY_COL_WIDTH;
  286. CPen cpWhite;
  287. cpWhite.CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
  288. CPen *pOldPen = NULL;
  289. COLORREF OldColor = -1;
  290. int nOldBKMode = -1;
  291. // Draw the background of the list item. Colors are selected
  292. // according to the item's state.
  293. if(rItem.state & LVIS_SELECTED)
  294. {
  295. if(bListHasFocus)
  296. {
  297. crBkgnd = GetSysColor(COLOR_HIGHLIGHT);
  298. OldColor = pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
  299. pOldPen = pDC->SelectObject((CPen*)&cpWhite);
  300. }
  301. else
  302. {
  303. crBkgnd = GetSysColor(COLOR_BTNFACE);
  304. OldColor = pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT));
  305. }
  306. }
  307. else
  308. {
  309. //Shade alternating Rows
  310. if((nItem % 2) == 0)
  311. crBkgnd = COLOR_SHADOW;
  312. else
  313. crBkgnd = GetSysColor(COLOR_WINDOW);
  314. OldColor = pDC->SetTextColor(GetSysColor(COLOR_BTNTEXT));
  315. }
  316. pDC->FillSolidRect(rcItem, crBkgnd);
  317. nOldBKMode = pDC->SetBkMode(TRANSPARENT);
  318. CRect rcText = rcItem;
  319. rcText.left += ROW_LEFT_BORDER;
  320. rcText.top++;
  321. // Draw the text.
  322. //CString csText = GetItemText(nItem, 0);
  323. CString csText;
  324. LPTSTR lpszText = csText.GetBufferSetLength(g_Opt.m_bDescTextSize);
  325. GetItemText(nItem, 0, lpszText, g_Opt.m_bDescTextSize);
  326. csText.ReleaseBuffer();
  327. // extract symbols
  328. CString strSymbols;
  329. int nSymEnd = csText.Find('|');
  330. if( nSymEnd >= 0 )
  331. {
  332. strSymbols = csText.Left(nSymEnd);
  333. csText = csText.Mid(nSymEnd+1);
  334. }
  335. // set firstTenNum to the first ten number (1-10) corresponding to
  336. // the current nItem.
  337. // -1 means that nItem is not in the FirstTen block.
  338. int firstTenNum = GetFirstTenNum(nItem);
  339. if( m_bShowTextForFirstTenHotKeys && firstTenNum > 0 )
  340. {
  341. rcText.left += 12;
  342. }
  343. // if we are inside a group, don't display the "in group" flag
  344. if( theApp.m_GroupID > 0 )
  345. {
  346. int nFlag = strSymbols.Find("!");
  347. if( nFlag >= 0 )
  348. strSymbols.Delete(nFlag);
  349. }
  350. // draw the symbol box
  351. if( strSymbols.GetLength() > 0 )
  352. {
  353. strSymbols = " " + strSymbols + " "; // leave space for box
  354. // add spaces to leave room for the symbols
  355. CRect rectSym(rcText.left, rcText.top+1, rcText.left, rcText.top+1);
  356. CRect rectSpace(0,0,0,0);
  357. //Get text bounds
  358. pDC->DrawText(" ", &rectSpace, DT_VCENTER | DT_EXPANDTABS | DT_CALCRECT);
  359. pDC->DrawText(strSymbols, &rectSym, DT_VCENTER | DT_EXPANDTABS | DT_CALCRECT);
  360. VERIFY( rectSpace.Width() > 0 );
  361. int numSpaces = rectSym.Width() / rectSpace.Width();
  362. numSpaces++;
  363. csText = CString(' ',numSpaces) + csText;
  364. // draw the symbols
  365. pDC->FillSolidRect( rectSym, GetSysColor(COLOR_ACTIVECAPTION) );
  366. //pDC->FillSolidRect( rectSym, RGB(0,255,255) );
  367. pDC->Draw3dRect(rectSym, GetSysColor(COLOR_3DLIGHT), GetSysColor(COLOR_3DDKSHADOW));
  368. // COLORREF crOld = pDC->SetTextColor(GetSysColor(COLOR_INFOTEXT));
  369. COLORREF crOld = pDC->SetTextColor(RGB(255, 255, 255));
  370. pDC->DrawText(strSymbols, rectSym, DT_VCENTER | DT_EXPANDTABS);
  371. pDC->SetTextColor(crOld);
  372. }
  373. pDC->DrawText(csText, rcText, DT_VCENTER | DT_EXPANDTABS);
  374. // Draw a focus rect around the item if necessary.
  375. if(bListHasFocus && (rItem.state & LVIS_FOCUSED))
  376. pDC->DrawFocusRect(rcItem);
  377. if( m_bShowTextForFirstTenHotKeys && firstTenNum > 0 )
  378. {
  379. CString cs;
  380. if( firstTenNum == 10 )
  381. cs = "0";
  382. else
  383. cs.Format("%d", firstTenNum);
  384. CRect crClient;
  385. GetWindowRect(crClient);
  386. ScreenToClient(crClient);
  387. CRect crHotKey = rcItem;
  388. crHotKey.right = crHotKey.left + 11;
  389. crHotKey.left += 2;
  390. crHotKey.top += 2;
  391. HFONT hOldFont = (HFONT)pDC->SelectObject(m_SmallFont);
  392. pDC->DrawText(cs, crHotKey, DT_BOTTOM);
  393. pDC->MoveTo(CPoint(rcItem.left + 11, rcItem.top));
  394. pDC->LineTo(CPoint(rcItem.left + 11, rcItem.bottom));
  395. pDC->SelectObject(hOldFont);
  396. }
  397. // restore the previous values
  398. if(pOldPen)
  399. pDC->SelectObject(pOldPen);
  400. if(OldColor > -1)
  401. pDC->SetTextColor(OldColor);
  402. if(nOldBKMode > -1)
  403. pDC->SetBkMode(nOldBKMode);
  404. *pResult = CDRF_SKIPDEFAULT; // We've painted everything.
  405. }
  406. }
  407. void CQListCtrl::RefreshVisibleRows()
  408. {
  409. int nTopIndex = GetTopIndex();
  410. int nLastIndex = nTopIndex + GetCountPerPage();
  411. RedrawItems(nTopIndex, nLastIndex);
  412. ::UpdateWindow(m_hWnd);
  413. }
  414. void CQListCtrl::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  415. {
  416. if(GetKeyState(VK_RETURN) & 0x800)
  417. GetParent()->SendMessage(NM_PROPERTIES, 0, 0);
  418. else
  419. CListCtrl::OnSysKeyDown(nChar, nRepCnt, nFlags);
  420. }
  421. BOOL CQListCtrl::OnEraseBkgnd(CDC* pDC)
  422. {
  423. // Simply returning TRUE seems OK since we do custom item
  424. // painting. However, there is a pixel buffer around the
  425. // border of this control (not within the item rects)
  426. // which becomes visually corrupt if it is not erased.
  427. // In most cases, I do not notice the erasure, so I have kept
  428. // the call to CListCtrl::OnEraseBkgnd(pDC);
  429. // However, for some reason, bulk erasure is very noticeable when
  430. // shift-scrolling the page to select a block of items, so
  431. // I made a special case for that:
  432. if( GetSelectedCount() >= 2 )
  433. return TRUE;
  434. return CListCtrl::OnEraseBkgnd(pDC);
  435. }
  436. BOOL CQListCtrl::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
  437. {
  438. // need to handle both ANSI and UNICODE versions of the message
  439. TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
  440. TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
  441. CString strTipText;
  442. UINT nID = pNMHDR->idFrom;
  443. if(nID == 0) // Notification in NT from automatically
  444. return FALSE; // created tooltip
  445. ::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
  446. // Use Item's name as the tool tip. Change this for something different.
  447. // Like use its file size, etc.
  448. GetToolTipText(nID-1, strTipText);
  449. //Replace the tabs with spaces, the tooltip didn't like the \t s
  450. strTipText.Replace("\t", " ");
  451. #ifndef _UNICODE
  452. if (pNMHDR->code == TTN_NEEDTEXTA)
  453. {
  454. if(m_pchTip != NULL)
  455. delete m_pchTip;
  456. m_pchTip = new TCHAR[strTipText.GetLength()+1];
  457. lstrcpyn(m_pchTip, strTipText, strTipText.GetLength());
  458. m_pchTip[strTipText.GetLength()] = 0;
  459. pTTTW->lpszText = (WCHAR*)m_pchTip;
  460. }
  461. else
  462. {
  463. if(m_pwchTip != NULL)
  464. delete m_pwchTip;
  465. m_pwchTip = new WCHAR[strTipText.GetLength()+1];
  466. _mbstowcsz(m_pwchTip, strTipText, strTipText.GetLength());
  467. m_pwchTip[strTipText.GetLength()] = 0; // end of text
  468. pTTTW->lpszText = (WCHAR*)m_pwchTip;
  469. }
  470. #else
  471. if(pNMHDR->code == TTN_NEEDTEXTA)
  472. {
  473. if(m_pchTip != NULL)
  474. delete m_pchTip;
  475. m_pchTip = new TCHAR[strTipText.GetLength()+1];
  476. _wcstombsz(m_pchTip, strTipText, strTipText.GetLength());
  477. m_pchTip[strTipText.GetLength()] = 0; // end of text
  478. pTTTA->lpszText = (LPTSTR)m_pchTip;
  479. }
  480. else
  481. {
  482. if(m_pwchTip != NULL)
  483. delete m_pwchTip;
  484. m_pwchTip = new WCHAR[strTipText.GetLength()+1];
  485. lstrcpyn(m_pwchTip, strTipText, strTipText.GetLength());
  486. m_pwchTip[strTipText.GetLength()] = 0;
  487. pTTTA->lpszText = (LPTSTR) m_pwchTip;
  488. }
  489. #endif
  490. *pResult = 0;
  491. return TRUE; // message was handled
  492. }
  493. int CQListCtrl::OnToolHitTest(CPoint point, TOOLINFO * pTI) const
  494. {
  495. CRect rect;
  496. GetClientRect(&rect);
  497. if(rect.PtInRect(point))
  498. {
  499. if(GetItemCount())
  500. {
  501. int nTopIndex = GetTopIndex();
  502. int nBottomIndex = nTopIndex + GetCountPerPage();
  503. if(nBottomIndex > GetItemCount()) nBottomIndex = GetItemCount();
  504. for(int nIndex = nTopIndex; nIndex <= nBottomIndex; nIndex++)
  505. {
  506. GetItemRect(nIndex, rect, LVIR_BOUNDS);
  507. if(rect.PtInRect(point))
  508. {
  509. pTI->hwnd = m_hWnd;
  510. pTI->uId = (UINT)(nIndex+1);
  511. pTI->lpszText = LPSTR_TEXTCALLBACK;
  512. pTI->rect = rect;
  513. return pTI->uId;
  514. }
  515. }
  516. }
  517. }
  518. return -1;
  519. }
  520. int CQListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  521. {
  522. if (CListCtrl::OnCreate(lpCreateStruct) == -1)
  523. return -1;
  524. EnableToolTips();
  525. m_Popup.Init();
  526. // m_Popup.SetTTWnd( GetToolTips()->m_hWnd );
  527. // m_Popup.m_TI.hwnd = m_hWnd;
  528. return 0;
  529. }
  530. BOOL CQListCtrl::PreTranslateMessage(MSG* pMsg)
  531. {
  532. DWORD dID;
  533. if(m_Accels.OnMsg(pMsg, dID))
  534. if(GetParent()->SendMessage(NM_SELECT_DB_ID, dID, 0) )
  535. return TRUE;
  536. switch(pMsg->message)
  537. {
  538. case WM_KEYDOWN:
  539. WPARAM vk = pMsg->wParam;
  540. if(m_Popup.m_bIsShowing)
  541. {
  542. m_Popup.Hide();
  543. if(vk == VK_ESCAPE)
  544. return TRUE;
  545. }
  546. // if a number key was pressed
  547. if( '0' <= vk && vk <= '9' )
  548. {
  549. // if <Ctrl> is required but is absent, then break
  550. if( g_Opt.m_bUseCtrlNumAccel && !(GetKeyState(VK_CONTROL) & 0x8000) )
  551. break;
  552. int index = vk - '0';
  553. // '0' is actually 10 in the ditto window
  554. if( index == 0 )
  555. index = 10;
  556. // translate num 1-10 into the actual index (based upon m_bStartTop)
  557. index = GetFirstTenIndex( index );
  558. GetParent()->SendMessage(NM_SELECT_INDEX, index, 0);
  559. return TRUE;
  560. }
  561. switch( vk )
  562. {
  563. case 'X': // Ctrl-X = Cut (prepare for moving the items into a Group)
  564. if(GetKeyState(VK_CONTROL) & 0x8000)
  565. {
  566. LoadCopyOrCutToClipboard();
  567. theApp.IC_Cut(); // uses selection
  568. return TRUE;
  569. }
  570. break;
  571. case 'C': // Ctrl-C = Copy (prepare for copying the items into a Group)
  572. if(GetKeyState(VK_CONTROL) & 0x8000)
  573. {
  574. LoadCopyOrCutToClipboard();
  575. theApp.IC_Copy(); // uses selection
  576. return TRUE;
  577. }
  578. break;
  579. case 'V': // Ctrl-V = Paste (actually performs the copy or move of items into the current Group)
  580. if(GetKeyState(VK_CONTROL) & 0x8000)
  581. {
  582. theApp.IC_Paste();
  583. return TRUE;
  584. }
  585. break;
  586. case 'A': // Ctrl-A = Select All
  587. if(GetKeyState(VK_CONTROL) & 0x8000)
  588. {
  589. int nCount = GetItemCount();
  590. for(int i = 0; i < nCount; i++)
  591. {
  592. SetSelection(i);
  593. }
  594. return TRUE;
  595. }
  596. break;
  597. case VK_F3:
  598. {
  599. ShowFullDescription();
  600. return TRUE;
  601. }
  602. case VK_BACK:
  603. theApp.EnterGroupID( theApp.m_GroupParentID );
  604. return TRUE;
  605. case VK_SPACE:
  606. if(GetKeyState(VK_CONTROL) & 0x8000)
  607. {
  608. theApp.ShowPersistent( !g_Opt.m_bShowPersistent );
  609. return TRUE;
  610. }
  611. break;
  612. } // end switch(vk)
  613. break; // end case WM_KEYDOWN
  614. } // end switch(pMsg->message)
  615. return CListCtrl::PreTranslateMessage(pMsg);
  616. }
  617. void CQListCtrl::LoadCopyOrCutToClipboard()
  618. {
  619. ARRAY arr;
  620. GetSelectionItemData(arr);
  621. int nCount = arr.GetSize();
  622. if(nCount <= 0)
  623. return;
  624. CProcessPaste paste;
  625. //Don't send the paste just load it into memory
  626. paste.m_bSendPaste = false;
  627. if(nCount > 1)
  628. paste.GetClipIDs().Copy(arr);
  629. else
  630. paste.GetClipIDs().Add(arr[0]);
  631. paste.DoPaste();
  632. }
  633. void CQListCtrl::ShowFullDescription(bool bFromAuto)
  634. {
  635. int nItem = GetCaret();
  636. CRect rc, crWindow;
  637. GetWindowRect(&crWindow);
  638. GetItemRect(nItem, rc, LVIR_BOUNDS);
  639. ClientToScreen(rc);
  640. if(bFromAuto == false)
  641. {
  642. m_Popup.m_Pos = CPoint(rc.left, rc.bottom);
  643. }
  644. else
  645. m_Popup.m_Pos = CPoint((crWindow.left + (crWindow.right - crWindow.left)/2), rc.bottom);
  646. CString cs;
  647. GetToolTipText(nItem, cs);
  648. m_Popup.Show( cs );
  649. }
  650. void CQListCtrl::GetToolTipText(int nItem, CString &csText)
  651. {
  652. if((GetStyle() & LVS_OWNERDATA))
  653. {
  654. CWnd* pParent=GetParent();
  655. if(pParent && (pParent->GetSafeHwnd() != NULL))
  656. {
  657. CQListToolTipText info;
  658. memset(&info, 0, sizeof(info));
  659. info.hdr.code = NM_GETTOOLTIPTEXT;
  660. info.hdr.hwndFrom = GetSafeHwnd();
  661. info.hdr.idFrom = GetDlgCtrlID();
  662. info.lItem = nItem;
  663. //plus 100 for extra info - shortcut and such
  664. info.cchTextMax = g_Opt.m_bDescTextSize + 100;
  665. info.pszText = csText.GetBufferSetLength(info.cchTextMax);
  666. pParent->SendMessage(WM_NOTIFY,(WPARAM)info.hdr.idFrom,(LPARAM)&info);
  667. csText.ReleaseBuffer();
  668. }
  669. }
  670. }
  671. DWORD CQListCtrl::GetItemData(int nItem)
  672. {
  673. if((GetStyle() & LVS_OWNERDATA))
  674. {
  675. CWnd* pParent=GetParent();
  676. if(pParent && (pParent->GetSafeHwnd() != NULL))
  677. {
  678. LV_DISPINFO info;
  679. memset(&info, 0, sizeof(info));
  680. info.hdr.code = LVN_GETDISPINFO;
  681. info.hdr.hwndFrom = GetSafeHwnd();
  682. info.hdr.idFrom = GetDlgCtrlID();
  683. info.item.iItem = nItem;
  684. info.item.lParam = -1;
  685. info.item.mask = LVIF_PARAM;
  686. pParent->SendMessage(WM_NOTIFY,(WPARAM)info.hdr.idFrom,(LPARAM)&info);
  687. return info.item.lParam;
  688. }
  689. }
  690. return CListCtrl::GetItemData(nItem);
  691. }
  692. void CQListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  693. {
  694. CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
  695. }
  696. void CQListCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  697. {
  698. CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
  699. }
  700. void CQListCtrl::DestroyAndCreateAccelerator(BOOL bCreate)
  701. {
  702. if( bCreate )
  703. CMainTable::LoadAcceleratorKeys( m_Accels );
  704. }
  705. void CQListCtrl::OnKillFocus(CWnd* pNewWnd)
  706. {
  707. CListCtrl::OnKillFocus(pNewWnd);
  708. m_Popup.Hide();
  709. }
  710. BOOL CQListCtrl::SetItemCountEx(int iCount, DWORD dwFlags /* = LVSICF_NOINVALIDATEALL */)
  711. {
  712. theApp.SetStatus(NULL, TRUE);
  713. return CListCtrl::SetItemCountEx(iCount, dwFlags);
  714. }
  715. #define TIMER_SHOW_PROPERTIES 1
  716. void CQListCtrl::OnSelectionChange(NMHDR* pNMHDR, LRESULT* pResult)
  717. {
  718. NMLISTVIEW *pnmv = (NMLISTVIEW *) pNMHDR;
  719. if((pnmv->uNewState == 3) ||
  720. (pnmv->uNewState == 1))
  721. {
  722. if(m_Popup.m_bIsShowing)
  723. {
  724. m_Popup.Hide();
  725. }
  726. if(g_Opt.m_bAllwaysShowDescription)
  727. {
  728. KillTimer(TIMER_SHOW_PROPERTIES);
  729. SetTimer(TIMER_SHOW_PROPERTIES, 300, NULL);
  730. }
  731. if(GetSelectedCount() > 0 )
  732. theApp.SetStatus(NULL, FALSE);
  733. }
  734. }
  735. void CQListCtrl::OnTimer(UINT nIDEvent)
  736. {
  737. if(nIDEvent == TIMER_SHOW_PROPERTIES)
  738. {
  739. if( theApp.m_bShowingQuickPaste )
  740. ShowFullDescription(true);
  741. KillTimer(TIMER_SHOW_PROPERTIES);
  742. }
  743. CListCtrl::OnTimer(nIDEvent);
  744. }
  745. void CQListCtrl::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)
  746. {
  747. CListCtrl::OnWindowPosChanged(lpwndpos);
  748. }
  749. void CQListCtrl::OnSize(UINT nType, int cx, int cy)
  750. {
  751. CListCtrl::OnSize(nType, cx, cy);
  752. }