QListCtrl.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. }
  384. if(DrawText(nItem, rcText, pDC) == FALSE)
  385. pDC->DrawText(csText, rcText, DT_VCENTER | DT_EXPANDTABS);
  386. // Draw a focus rect around the item if necessary.
  387. if(bListHasFocus && (rItem.state & LVIS_FOCUSED))
  388. pDC->DrawFocusRect(rcItem);
  389. if( m_bShowTextForFirstTenHotKeys && firstTenNum > 0 )
  390. {
  391. CString cs;
  392. if( firstTenNum == 10 )
  393. cs = "0";
  394. else
  395. cs.Format("%d", firstTenNum);
  396. CRect crClient;
  397. GetWindowRect(crClient);
  398. ScreenToClient(crClient);
  399. CRect crHotKey = rcItem;
  400. crHotKey.right = crHotKey.left + 11;
  401. crHotKey.left += 2;
  402. crHotKey.top += 2;
  403. HFONT hOldFont = (HFONT)pDC->SelectObject(m_SmallFont);
  404. pDC->DrawText(cs, crHotKey, DT_BOTTOM);
  405. pDC->MoveTo(CPoint(rcItem.left + 11, rcItem.top));
  406. pDC->LineTo(CPoint(rcItem.left + 11, rcItem.bottom));
  407. pDC->SelectObject(hOldFont);
  408. }
  409. // restore the previous values
  410. if(pOldPen)
  411. pDC->SelectObject(pOldPen);
  412. if(OldColor > -1)
  413. pDC->SetTextColor(OldColor);
  414. if(nOldBKMode > -1)
  415. pDC->SetBkMode(nOldBKMode);
  416. *pResult = CDRF_SKIPDEFAULT; // We've painted everything.
  417. }
  418. }
  419. BOOL CQListCtrl::DrawText(int nItem, CRect &crRect, CDC *pDC)
  420. {
  421. if(g_Opt.m_bDrawRTF == FALSE)
  422. return FALSE;
  423. static CLIPFORMAT clFormat = GetFormatID(CF_RTF);
  424. BOOL bRet = FALSE;
  425. long lDatabaseID = GetItemData(nItem);
  426. CClipFormat* pThumbnail = &(m_RTFData[lDatabaseID]);
  427. if(pThumbnail == NULL)
  428. return FALSE;
  429. //If it has not been read in
  430. if(pThumbnail->m_cfType != clFormat)
  431. {
  432. pThumbnail->m_cfType = clFormat;
  433. //Get the data from the database
  434. GetClipData(nItem, *pThumbnail);
  435. }
  436. // if there's no data, then we're done.
  437. if( pThumbnail->m_hgData == NULL )
  438. return FALSE;
  439. if(m_pFormatter == NULL)
  440. {
  441. m_pFormatter = new CFormattedTextDraw;
  442. m_pFormatter->Create();
  443. }
  444. if(m_pFormatter)
  445. {
  446. char *pData = (char*)GlobalLock(pThumbnail->m_hgData);
  447. if(pData)
  448. {
  449. CComBSTR bStr(pData);
  450. m_pFormatter->put_RTFText(bStr);
  451. m_pFormatter->Draw(pDC->m_hDC, crRect);
  452. GlobalUnlock(pThumbnail->m_hgData);
  453. bRet = TRUE;
  454. }
  455. }
  456. return bRet;
  457. }
  458. // DrawBitMap loads a DIB from the DB, draws a crRect thumbnail of the image
  459. // to pDC and caches that thumbnail as a DIB in m_ThumbNails[ ItemID ].
  460. // ALL items are cached in m_ThumbNails (those without images are cached with NULL m_hgData)
  461. BOOL CQListCtrl::DrawBitMap(int nItem, CRect &crRect, CDC *pDC)
  462. {
  463. if(g_Opt.m_bDrawThumbnail == FALSE)
  464. return FALSE;
  465. bool bFromDB = false;
  466. long lDatabaseID = GetItemData(nItem);
  467. CClipFormat* pThumbnail = &(m_ThumbNails[lDatabaseID]);
  468. if(pThumbnail == NULL)
  469. return FALSE;
  470. //If it has not been read in
  471. if(pThumbnail->m_cfType != CF_DIB)
  472. {
  473. pThumbnail->m_cfType = CF_DIB;
  474. //Get the data from the database
  475. GetClipData(nItem, *pThumbnail);
  476. //convert to a small bitmap
  477. CBitmap Bitmap;
  478. if( !CBitmapHelper::GetCBitmap(pThumbnail, pDC, &Bitmap, crRect.Height()) )
  479. {
  480. Bitmap.DeleteObject();
  481. // the data is useless, so free it.
  482. pThumbnail->Free();
  483. return FALSE;
  484. }
  485. // delete the large image data loaded from the db
  486. pThumbnail->Free();
  487. pThumbnail->m_cfType = CF_DIB;
  488. //Convert the smaller bitmap back to a dib
  489. HPALETTE hPal = NULL;
  490. pThumbnail->m_hgData = CBitmapHelper::hBitmapToDIB( (HBITMAP)Bitmap, BI_RGB, hPal );
  491. ASSERT( pThumbnail->bDeleteData ); // the map owns the data.
  492. Bitmap.DeleteObject();
  493. }
  494. // if there's no data, then we're done.
  495. if( pThumbnail->m_hgData == NULL )
  496. return TRUE;
  497. //Will return the width of the bitmap in nWidth
  498. int nWidth = 0;
  499. if(CBitmapHelper::DrawDIB(pDC, pThumbnail->m_hgData, crRect.left, crRect.top, nWidth))
  500. {
  501. // adjust the rect so other information can be drawn next to the thumbnail
  502. crRect.left += nWidth + 3;
  503. }
  504. return TRUE;
  505. }
  506. void CQListCtrl::RefreshVisibleRows()
  507. {
  508. int nTopIndex = GetTopIndex();
  509. int nLastIndex = nTopIndex + GetCountPerPage();
  510. RedrawItems(nTopIndex, nLastIndex);
  511. ::UpdateWindow(m_hWnd);
  512. }
  513. void CQListCtrl::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  514. {
  515. if(GetKeyState(VK_RETURN) & 0x800)
  516. GetParent()->SendMessage(NM_PROPERTIES, 0, 0);
  517. else
  518. CListCtrl::OnSysKeyDown(nChar, nRepCnt, nFlags);
  519. }
  520. BOOL CQListCtrl::OnEraseBkgnd(CDC* pDC)
  521. {
  522. // Simply returning TRUE seems OK since we do custom item
  523. // painting. However, there is a pixel buffer around the
  524. // border of this control (not within the item rects)
  525. // which becomes visually corrupt if it is not erased.
  526. // In most cases, I do not notice the erasure, so I have kept
  527. // the call to CListCtrl::OnEraseBkgnd(pDC);
  528. // However, for some reason, bulk erasure is very noticeable when
  529. // shift-scrolling the page to select a block of items, so
  530. // I made a special case for that:
  531. if( GetSelectedCount() >= 2 )
  532. return TRUE;
  533. return CListCtrl::OnEraseBkgnd(pDC);
  534. }
  535. BOOL CQListCtrl::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
  536. {
  537. // need to handle both ANSI and UNICODE versions of the message
  538. TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
  539. TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
  540. CString strTipText;
  541. UINT nID = pNMHDR->idFrom;
  542. if(nID == 0) // Notification in NT from automatically
  543. return FALSE; // created tooltip
  544. ::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
  545. // Use Item's name as the tool tip. Change this for something different.
  546. // Like use its file size, etc.
  547. GetToolTipText(nID-1, strTipText);
  548. //Replace the tabs with spaces, the tooltip didn't like the \t s
  549. strTipText.Replace("\t", " ");
  550. #ifndef _UNICODE
  551. if (pNMHDR->code == TTN_NEEDTEXTA)
  552. {
  553. if(m_pchTip != NULL)
  554. delete m_pchTip;
  555. m_pchTip = new TCHAR[strTipText.GetLength()+1];
  556. lstrcpyn(m_pchTip, strTipText, strTipText.GetLength());
  557. m_pchTip[strTipText.GetLength()] = 0;
  558. pTTTW->lpszText = (WCHAR*)m_pchTip;
  559. }
  560. else
  561. {
  562. if(m_pwchTip != NULL)
  563. delete m_pwchTip;
  564. m_pwchTip = new WCHAR[strTipText.GetLength()+1];
  565. _mbstowcsz(m_pwchTip, strTipText, strTipText.GetLength());
  566. m_pwchTip[strTipText.GetLength()] = 0; // end of text
  567. pTTTW->lpszText = (WCHAR*)m_pwchTip;
  568. }
  569. #else
  570. if(pNMHDR->code == TTN_NEEDTEXTA)
  571. {
  572. if(m_pchTip != NULL)
  573. delete m_pchTip;
  574. m_pchTip = new TCHAR[strTipText.GetLength()+1];
  575. _wcstombsz(m_pchTip, strTipText, strTipText.GetLength());
  576. m_pchTip[strTipText.GetLength()] = 0; // end of text
  577. pTTTA->lpszText = (LPTSTR)m_pchTip;
  578. }
  579. else
  580. {
  581. if(m_pwchTip != NULL)
  582. delete m_pwchTip;
  583. m_pwchTip = new WCHAR[strTipText.GetLength()+1];
  584. lstrcpyn(m_pwchTip, strTipText, strTipText.GetLength());
  585. m_pwchTip[strTipText.GetLength()] = 0;
  586. pTTTA->lpszText = (LPTSTR) m_pwchTip;
  587. }
  588. #endif
  589. *pResult = 0;
  590. return TRUE; // message was handled
  591. }
  592. int CQListCtrl::OnToolHitTest(CPoint point, TOOLINFO * pTI) const
  593. {
  594. CRect rect;
  595. GetClientRect(&rect);
  596. if(rect.PtInRect(point))
  597. {
  598. if(GetItemCount())
  599. {
  600. int nTopIndex = GetTopIndex();
  601. int nBottomIndex = nTopIndex + GetCountPerPage();
  602. if(nBottomIndex > GetItemCount()) nBottomIndex = GetItemCount();
  603. for(int nIndex = nTopIndex; nIndex <= nBottomIndex; nIndex++)
  604. {
  605. GetItemRect(nIndex, rect, LVIR_BOUNDS);
  606. if(rect.PtInRect(point))
  607. {
  608. pTI->hwnd = m_hWnd;
  609. pTI->uId = (UINT)(nIndex+1);
  610. pTI->lpszText = LPSTR_TEXTCALLBACK;
  611. pTI->rect = rect;
  612. return pTI->uId;
  613. }
  614. }
  615. }
  616. }
  617. return -1;
  618. }
  619. int CQListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  620. {
  621. if (CListCtrl::OnCreate(lpCreateStruct) == -1)
  622. return -1;
  623. EnableToolTips();
  624. m_pToolTip = new CToolTipEx;
  625. m_pToolTip->Create(this);
  626. return 0;
  627. }
  628. BOOL CQListCtrl::PreTranslateMessage(MSG* pMsg)
  629. {
  630. DWORD dID;
  631. if(m_Accels.OnMsg(pMsg, dID))
  632. if(GetParent()->SendMessage(NM_SELECT_DB_ID, dID, 0) )
  633. return TRUE;
  634. if(m_pToolTip)
  635. {
  636. if(m_pToolTip->OnMsg(pMsg))
  637. return TRUE;
  638. }
  639. switch(pMsg->message)
  640. {
  641. case WM_KEYDOWN:
  642. WPARAM vk = pMsg->wParam;
  643. // if a number key was pressed
  644. if( '0' <= vk && vk <= '9' )
  645. {
  646. // if <Ctrl> is required but is absent, then break
  647. if( g_Opt.m_bUseCtrlNumAccel && !(GetKeyState(VK_CONTROL) & 0x8000) )
  648. break;
  649. int index = vk - '0';
  650. // '0' is actually 10 in the ditto window
  651. if( index == 0 )
  652. index = 10;
  653. // translate num 1-10 into the actual index (based upon m_bStartTop)
  654. index = GetFirstTenIndex( index );
  655. GetParent()->SendMessage(NM_SELECT_INDEX, index, 0);
  656. return TRUE;
  657. }
  658. switch( vk )
  659. {
  660. case 'X': // Ctrl-X = Cut (prepare for moving the items into a Group)
  661. if(GetKeyState(VK_CONTROL) & 0x8000)
  662. {
  663. LoadCopyOrCutToClipboard();
  664. theApp.IC_Cut(); // uses selection
  665. return TRUE;
  666. }
  667. break;
  668. case 'C': // Ctrl-C = Copy (prepare for copying the items into a Group)
  669. if(GetKeyState(VK_CONTROL) & 0x8000)
  670. {
  671. LoadCopyOrCutToClipboard();
  672. theApp.IC_Copy(); // uses selection
  673. return TRUE;
  674. }
  675. break;
  676. case 'V': // Ctrl-V = Paste (actually performs the copy or move of items into the current Group)
  677. if(GetKeyState(VK_CONTROL) & 0x8000)
  678. {
  679. theApp.IC_Paste();
  680. return TRUE;
  681. }
  682. break;
  683. case 'A': // Ctrl-A = Select All
  684. if(GetKeyState(VK_CONTROL) & 0x8000)
  685. {
  686. int nCount = GetItemCount();
  687. for(int i = 0; i < nCount; i++)
  688. {
  689. SetSelection(i);
  690. }
  691. return TRUE;
  692. }
  693. break;
  694. case VK_F3:
  695. {
  696. ShowFullDescription();
  697. return TRUE;
  698. }
  699. case VK_BACK:
  700. theApp.EnterGroupID( theApp.m_GroupParentID );
  701. return TRUE;
  702. case VK_SPACE:
  703. if(GetKeyState(VK_CONTROL) & 0x8000)
  704. {
  705. theApp.ShowPersistent( !g_Opt.m_bShowPersistent );
  706. return TRUE;
  707. }
  708. break;
  709. } // end switch(vk)
  710. break; // end case WM_KEYDOWN
  711. } // end switch(pMsg->message)
  712. return CListCtrl::PreTranslateMessage(pMsg);
  713. }
  714. void CQListCtrl::LoadCopyOrCutToClipboard()
  715. {
  716. ARRAY arr;
  717. GetSelectionItemData(arr);
  718. int nCount = arr.GetSize();
  719. if(nCount <= 0)
  720. return;
  721. CProcessPaste paste;
  722. //Don't send the paste just load it into memory
  723. paste.m_bSendPaste = false;
  724. if(nCount > 1)
  725. paste.GetClipIDs().Copy(arr);
  726. else
  727. paste.GetClipIDs().Add(arr[0]);
  728. paste.DoPaste();
  729. }
  730. void CQListCtrl::ShowFullDescription(bool bFromAuto)
  731. {
  732. int nItem = GetCaret();
  733. CRect rc, crWindow;
  734. GetWindowRect(&crWindow);
  735. GetItemRect(nItem, rc, LVIR_BOUNDS);
  736. ClientToScreen(rc);
  737. CPoint pt;
  738. if(bFromAuto == false)
  739. {
  740. pt = CPoint(rc.left, rc.bottom);
  741. }
  742. else
  743. pt = CPoint((crWindow.left + (crWindow.right - crWindow.left)/2), rc.bottom);
  744. CString cs;
  745. GetToolTipText(nItem, cs);
  746. if(m_pToolTip)
  747. {
  748. CClipFormat Clip;
  749. Clip.m_cfType = CF_DIB;
  750. static CBitmap *pBitMap = NULL;
  751. if(GetClipData(nItem, Clip) && Clip.m_hgData)
  752. {
  753. pBitMap = new CBitmap;
  754. if(pBitMap)
  755. {
  756. CRect rcItem;
  757. GetWindowRect(rcItem);
  758. CDC *pDC = GetDC();;
  759. CBitmapHelper::GetCBitmap(&Clip, pDC, pBitMap, (rcItem.Width() * 2));
  760. ReleaseDC(pDC);
  761. m_pToolTip->SetBitmap(pBitMap);
  762. }
  763. }
  764. m_pToolTip->SetToolTipText(cs);
  765. m_pToolTip->Show(pt);
  766. }
  767. }
  768. void CQListCtrl::GetToolTipText(int nItem, CString &csText)
  769. {
  770. if((GetStyle() & LVS_OWNERDATA))
  771. {
  772. CWnd* pParent=GetParent();
  773. if(pParent && (pParent->GetSafeHwnd() != NULL))
  774. {
  775. CQListToolTipText info;
  776. memset(&info, 0, sizeof(info));
  777. info.hdr.code = NM_GETTOOLTIPTEXT;
  778. info.hdr.hwndFrom = GetSafeHwnd();
  779. info.hdr.idFrom = GetDlgCtrlID();
  780. info.lItem = nItem;
  781. //plus 100 for extra info - shortcut and such
  782. info.cchTextMax = g_Opt.m_bDescTextSize + 100;
  783. info.pszText = csText.GetBufferSetLength(info.cchTextMax);
  784. pParent->SendMessage(WM_NOTIFY,(WPARAM)info.hdr.idFrom,(LPARAM)&info);
  785. csText.ReleaseBuffer();
  786. }
  787. }
  788. }
  789. BOOL CQListCtrl::GetClipData(int nItem, CClipFormat &Clip)
  790. {
  791. CWnd* pParent=GetParent();
  792. if(pParent && (pParent->GetSafeHwnd() != NULL))
  793. {
  794. if(GetParent()->SendMessage(NM_GET_CLIP_DATA, nItem, (LPARAM) &Clip))
  795. return TRUE;
  796. }
  797. return FALSE;
  798. }
  799. DWORD CQListCtrl::GetItemData(int nItem)
  800. {
  801. if((GetStyle() & LVS_OWNERDATA))
  802. {
  803. CWnd* pParent=GetParent();
  804. if(pParent && (pParent->GetSafeHwnd() != NULL))
  805. {
  806. LV_DISPINFO info;
  807. memset(&info, 0, sizeof(info));
  808. info.hdr.code = LVN_GETDISPINFO;
  809. info.hdr.hwndFrom = GetSafeHwnd();
  810. info.hdr.idFrom = GetDlgCtrlID();
  811. info.item.iItem = nItem;
  812. info.item.lParam = -1;
  813. info.item.mask = LVIF_PARAM;
  814. pParent->SendMessage(WM_NOTIFY,(WPARAM)info.hdr.idFrom,(LPARAM)&info);
  815. return info.item.lParam;
  816. }
  817. }
  818. return CListCtrl::GetItemData(nItem);
  819. }
  820. void CQListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  821. {
  822. CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
  823. }
  824. void CQListCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  825. {
  826. CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
  827. }
  828. void CQListCtrl::DestroyAndCreateAccelerator(BOOL bCreate)
  829. {
  830. if( bCreate )
  831. CMainTable::LoadAcceleratorKeys( m_Accels );
  832. }
  833. void CQListCtrl::OnKillFocus(CWnd* pNewWnd)
  834. {
  835. CListCtrl::OnKillFocus(pNewWnd);
  836. m_pToolTip->Hide();
  837. }
  838. BOOL CQListCtrl::SetItemCountEx(int iCount, DWORD dwFlags /* = LVSICF_NOINVALIDATEALL */)
  839. {
  840. theApp.SetStatus(NULL, TRUE);
  841. return CListCtrl::SetItemCountEx(iCount, dwFlags);
  842. }
  843. #define TIMER_SHOW_PROPERTIES 1
  844. void CQListCtrl::OnSelectionChange(NMHDR* pNMHDR, LRESULT* pResult)
  845. {
  846. NMLISTVIEW *pnmv = (NMLISTVIEW *) pNMHDR;
  847. if((pnmv->uNewState == 3) ||
  848. (pnmv->uNewState == 1))
  849. {
  850. if(g_Opt.m_bAllwaysShowDescription)
  851. {
  852. KillTimer(TIMER_SHOW_PROPERTIES);
  853. SetTimer(TIMER_SHOW_PROPERTIES, 300, NULL);
  854. }
  855. if(GetSelectedCount() > 0 )
  856. theApp.SetStatus(NULL, FALSE);
  857. }
  858. }
  859. void CQListCtrl::OnTimer(UINT nIDEvent)
  860. {
  861. if(nIDEvent == TIMER_SHOW_PROPERTIES)
  862. {
  863. if( theApp.m_bShowingQuickPaste )
  864. ShowFullDescription(true);
  865. KillTimer(TIMER_SHOW_PROPERTIES);
  866. }
  867. CListCtrl::OnTimer(nIDEvent);
  868. }
  869. void CQListCtrl::SetLogFont(LOGFONT &font)
  870. {
  871. m_Font.DeleteObject();
  872. m_Font.CreateFontIndirect(&font);
  873. SetFont(&m_Font);
  874. }