QListCtrl.cpp 28 KB

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