QListCtrl.cpp 23 KB

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