QListCtrl.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. m_pToolTip->SetNotifyWnd(GetParent());
  628. return 0;
  629. }
  630. BOOL CQListCtrl::PreTranslateMessage(MSG* pMsg)
  631. {
  632. DWORD dID;
  633. if(m_Accels.OnMsg(pMsg, dID))
  634. if(GetParent()->SendMessage(NM_SELECT_DB_ID, dID, 0) )
  635. return TRUE;
  636. if(m_pToolTip)
  637. {
  638. if(m_pToolTip->OnMsg(pMsg))
  639. return TRUE;
  640. }
  641. switch(pMsg->message)
  642. {
  643. case WM_KEYDOWN:
  644. if(HandleKeyDown(pMsg->wParam, pMsg->lParam))
  645. return TRUE;
  646. break; // end case WM_KEYDOWN
  647. } // end switch(pMsg->message)
  648. return CListCtrl::PreTranslateMessage(pMsg);
  649. }
  650. BOOL CQListCtrl::HandleKeyDown(WPARAM wParam, LPARAM lParam)
  651. {
  652. WPARAM vk = wParam;
  653. // if a number key was pressed
  654. if( '0' <= vk && vk <= '9' )
  655. {
  656. // if <Ctrl> is required but is absent, then break
  657. if( g_Opt.m_bUseCtrlNumAccel && !(GetKeyState(VK_CONTROL) & 0x8000) )
  658. return FALSE;
  659. int index = vk - '0';
  660. // '0' is actually 10 in the ditto window
  661. if( index == 0 )
  662. index = 10;
  663. // translate num 1-10 into the actual index (based upon m_bStartTop)
  664. index = GetFirstTenIndex( index );
  665. GetParent()->SendMessage(NM_SELECT_INDEX, index, 0);
  666. return TRUE;
  667. }
  668. switch( vk )
  669. {
  670. case 'X': // Ctrl-X = Cut (prepare for moving the items into a Group)
  671. if(GetKeyState(VK_CONTROL) & 0x8000)
  672. {
  673. LoadCopyOrCutToClipboard();
  674. theApp.IC_Cut(); // uses selection
  675. return TRUE;
  676. }
  677. break;
  678. case 'C': // Ctrl-C = Copy (prepare for copying the items into a Group)
  679. if(GetKeyState(VK_CONTROL) & 0x8000)
  680. {
  681. LoadCopyOrCutToClipboard();
  682. theApp.IC_Copy(); // uses selection
  683. return TRUE;
  684. }
  685. break;
  686. case 'V': // Ctrl-V = Paste (actually performs the copy or move of items into the current Group)
  687. if(GetKeyState(VK_CONTROL) & 0x8000)
  688. {
  689. theApp.IC_Paste();
  690. return TRUE;
  691. }
  692. break;
  693. case 'A': // Ctrl-A = Select All
  694. if(GetKeyState(VK_CONTROL) & 0x8000)
  695. {
  696. int nCount = GetItemCount();
  697. for(int i = 0; i < nCount; i++)
  698. {
  699. SetSelection(i);
  700. }
  701. return TRUE;
  702. }
  703. break;
  704. case VK_F3:
  705. {
  706. ShowFullDescription();
  707. return TRUE;
  708. }
  709. case VK_BACK:
  710. theApp.EnterGroupID( theApp.m_GroupParentID );
  711. return TRUE;
  712. case VK_SPACE:
  713. if(GetKeyState(VK_CONTROL) & 0x8000)
  714. {
  715. theApp.ShowPersistent( !g_Opt.m_bShowPersistent );
  716. return TRUE;
  717. }
  718. break;
  719. } // end switch(vk)
  720. return FALSE;
  721. }
  722. void CQListCtrl::LoadCopyOrCutToClipboard()
  723. {
  724. ARRAY arr;
  725. GetSelectionItemData(arr);
  726. int nCount = arr.GetSize();
  727. if(nCount <= 0)
  728. return;
  729. CProcessPaste paste;
  730. //Don't send the paste just load it into memory
  731. paste.m_bSendPaste = false;
  732. if(nCount > 1)
  733. paste.GetClipIDs().Copy(arr);
  734. else
  735. paste.GetClipIDs().Add(arr[0]);
  736. paste.DoPaste();
  737. }
  738. void CQListCtrl::ShowFullDescription(bool bFromAuto)
  739. {
  740. int nItem = GetCaret();
  741. CRect rc, crWindow;
  742. GetWindowRect(&crWindow);
  743. GetItemRect(nItem, rc, LVIR_BOUNDS);
  744. ClientToScreen(rc);
  745. CPoint pt;
  746. if(bFromAuto == false)
  747. {
  748. pt = CPoint(rc.left, rc.bottom);
  749. }
  750. else
  751. pt = CPoint((crWindow.left + (crWindow.right - crWindow.left)/2), rc.bottom);
  752. CString cs;
  753. GetToolTipText(nItem, cs);
  754. if(m_pToolTip)
  755. {
  756. m_pToolTip->SetToolTipText(cs);
  757. CClipFormat Clip;
  758. Clip.m_cfType = CF_TEXT;
  759. if(GetClipData(nItem, Clip) && Clip.m_hgData)
  760. {
  761. LPVOID pvData = GlobalLock(Clip.m_hgData);
  762. if(pvData)
  763. {
  764. CString csText = (char*)pvData;
  765. m_pToolTip->SetToolTipText(csText);
  766. }
  767. GlobalUnlock(Clip.m_hgData);
  768. Clip.Free();
  769. Clip.Clear();
  770. }
  771. Clip.m_cfType = RegisterClipboardFormat(CF_RTF);
  772. if(GetClipData(nItem, Clip) && Clip.m_hgData)
  773. {
  774. LPVOID pvData = GlobalLock(Clip.m_hgData);
  775. if(pvData)
  776. {
  777. CString csRTF = (char*)pvData;
  778. m_pToolTip->SetRTFText(csRTF);
  779. }
  780. GlobalUnlock(Clip.m_hgData);
  781. Clip.Free();
  782. Clip.Clear();
  783. }
  784. Clip.m_cfType = CF_DIB;
  785. if(GetClipData(nItem, Clip) && Clip.m_hgData)
  786. {
  787. CBitmap *pBitMap = new CBitmap;
  788. if(pBitMap)
  789. {
  790. CRect rcItem;
  791. GetWindowRect(rcItem);
  792. CDC *pDC = GetDC();;
  793. CBitmapHelper::GetCBitmap(&Clip, pDC, pBitMap, (rcItem.Width() * 2));
  794. ReleaseDC(pDC);
  795. //Tooltip wnd will release
  796. m_pToolTip->SetBitmap(pBitMap);
  797. }
  798. Clip.Free();
  799. Clip.Clear();
  800. }
  801. m_pToolTip->Show(pt);
  802. }
  803. }
  804. void CQListCtrl::GetToolTipText(int nItem, CString &csText)
  805. {
  806. if((GetStyle() & LVS_OWNERDATA))
  807. {
  808. CWnd* pParent=GetParent();
  809. if(pParent && (pParent->GetSafeHwnd() != NULL))
  810. {
  811. CQListToolTipText info;
  812. memset(&info, 0, sizeof(info));
  813. info.hdr.code = NM_GETTOOLTIPTEXT;
  814. info.hdr.hwndFrom = GetSafeHwnd();
  815. info.hdr.idFrom = GetDlgCtrlID();
  816. info.lItem = nItem;
  817. //plus 100 for extra info - shortcut and such
  818. info.cchTextMax = g_Opt.m_bDescTextSize + 100;
  819. info.pszText = csText.GetBufferSetLength(info.cchTextMax);
  820. pParent->SendMessage(WM_NOTIFY,(WPARAM)info.hdr.idFrom,(LPARAM)&info);
  821. csText.ReleaseBuffer();
  822. }
  823. }
  824. }
  825. BOOL CQListCtrl::GetClipData(int nItem, CClipFormat &Clip)
  826. {
  827. CWnd* pParent=GetParent();
  828. if(pParent && (pParent->GetSafeHwnd() != NULL))
  829. {
  830. if(GetParent()->SendMessage(NM_GET_CLIP_DATA, nItem, (LPARAM) &Clip))
  831. return TRUE;
  832. }
  833. return FALSE;
  834. }
  835. DWORD CQListCtrl::GetItemData(int nItem)
  836. {
  837. if((GetStyle() & LVS_OWNERDATA))
  838. {
  839. CWnd* pParent=GetParent();
  840. if(pParent && (pParent->GetSafeHwnd() != NULL))
  841. {
  842. LV_DISPINFO info;
  843. memset(&info, 0, sizeof(info));
  844. info.hdr.code = LVN_GETDISPINFO;
  845. info.hdr.hwndFrom = GetSafeHwnd();
  846. info.hdr.idFrom = GetDlgCtrlID();
  847. info.item.iItem = nItem;
  848. info.item.lParam = -1;
  849. info.item.mask = LVIF_PARAM;
  850. pParent->SendMessage(WM_NOTIFY,(WPARAM)info.hdr.idFrom,(LPARAM)&info);
  851. return info.item.lParam;
  852. }
  853. }
  854. return CListCtrl::GetItemData(nItem);
  855. }
  856. void CQListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  857. {
  858. CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
  859. }
  860. void CQListCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  861. {
  862. CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
  863. }
  864. void CQListCtrl::DestroyAndCreateAccelerator(BOOL bCreate)
  865. {
  866. m_Accels.m_Map.RemoveAll();
  867. if( bCreate )
  868. CMainTable::LoadAcceleratorKeys( m_Accels );
  869. }
  870. void CQListCtrl::OnKillFocus(CWnd* pNewWnd)
  871. {
  872. CListCtrl::OnKillFocus(pNewWnd);
  873. // if(FocusOnToolTip() == FALSE)
  874. // m_pToolTip->Hide();
  875. }
  876. HWND CQListCtrl::GetToolTipHWnd()
  877. {
  878. return m_pToolTip->GetSafeHwnd();
  879. }
  880. BOOL CQListCtrl::SetItemCountEx(int iCount, DWORD dwFlags /* = LVSICF_NOINVALIDATEALL */)
  881. {
  882. theApp.SetStatus(NULL, TRUE);
  883. return CListCtrl::SetItemCountEx(iCount, dwFlags);
  884. }
  885. #define TIMER_SHOW_PROPERTIES 1
  886. void CQListCtrl::OnSelectionChange(NMHDR* pNMHDR, LRESULT* pResult)
  887. {
  888. NMLISTVIEW *pnmv = (NMLISTVIEW *) pNMHDR;
  889. if((pnmv->uNewState == 3) ||
  890. (pnmv->uNewState == 1))
  891. {
  892. if(g_Opt.m_bAllwaysShowDescription)
  893. {
  894. KillTimer(TIMER_SHOW_PROPERTIES);
  895. SetTimer(TIMER_SHOW_PROPERTIES, 300, NULL);
  896. }
  897. if(GetSelectedCount() > 0 )
  898. theApp.SetStatus(NULL, FALSE);
  899. }
  900. }
  901. void CQListCtrl::OnTimer(UINT nIDEvent)
  902. {
  903. if(nIDEvent == TIMER_SHOW_PROPERTIES)
  904. {
  905. if( theApp.m_bShowingQuickPaste )
  906. ShowFullDescription(true);
  907. KillTimer(TIMER_SHOW_PROPERTIES);
  908. }
  909. CListCtrl::OnTimer(nIDEvent);
  910. }
  911. void CQListCtrl::SetLogFont(LOGFONT &font)
  912. {
  913. m_Font.DeleteObject();
  914. m_Font.CreateFontIndirect(&font);
  915. SetFont(&m_Font);
  916. }