QListCtrl.cpp 25 KB

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