Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11095 lines
353 KiB

  1. #include "ctlspriv.h"
  2. #include "listview.h"
  3. #include "image.h"
  4. #include <mlang.h>
  5. #include <inetreg.h>
  6. #define __IOleControl_INTERFACE_DEFINED__ // There is a conflict with the IOleControl's def of CONTROLINFO
  7. #include "shlobj.h"
  8. #define IE_SETTINGS TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced")
  9. #define USE_DBL_CLICK_TIMER TEXT("UseDoubleClickTimer")
  10. int g_bUseDblClickTimer;
  11. #define LVMP_WINDOWPOSCHANGED (WM_USER + 1)
  12. HRESULT WINAPI UninitializeFlatSB(HWND hwnd);
  13. #define COLORISLIGHT(clr) ((5*GetGValue((clr)) + 2*GetRValue((clr)) + GetBValue((clr))) > 8*128)
  14. void NEAR ListView_HandleMouse(LV* plv, BOOL fDoubleClick, int x, int y, UINT keyFlags, BOOL bMouseWheel);
  15. /// function table setup
  16. const PFNLISTVIEW_DRAWITEM pfnListView_DrawItem[4] = {
  17. ListView_IDrawItem,
  18. ListView_RDrawItem,
  19. ListView_IDrawItem,
  20. ListView_LDrawItem,
  21. };
  22. void ListView_HandleStateIconClick(LV* plv, int iItem);
  23. DWORD ListView_IApproximateViewRect(LV* ,int, int, int);
  24. DWORD ListView_RApproximateViewRect(LV* ,int, int, int);
  25. DWORD ListView_LApproximateViewRect(LV* ,int, int, int);
  26. const PFNLISTVIEW_APPROXIMATEVIEWRECT pfnListView_ApproximateViewRect[4] = {
  27. ListView_IApproximateViewRect,
  28. ListView_RApproximateViewRect,
  29. ListView_IApproximateViewRect,
  30. ListView_LApproximateViewRect,
  31. };
  32. const PFNLISTVIEW_UPDATESCROLLBARS pfnListView_UpdateScrollBars[4] = {
  33. ListView_IUpdateScrollBars,
  34. ListView_RUpdateScrollBars,
  35. ListView_IUpdateScrollBars,
  36. ListView_LUpdateScrollBars,
  37. };
  38. const PFNLISTVIEW_ITEMHITTEST pfnListView_ItemHitTest[4] = {
  39. ListView_IItemHitTest,
  40. ListView_RItemHitTest,
  41. ListView_SItemHitTest,
  42. ListView_LItemHitTest,
  43. };
  44. const PFNLISTVIEW_ONSCROLL pfnListView_OnScroll[4] = {
  45. ListView_IOnScroll,
  46. ListView_ROnScroll,
  47. ListView_IOnScroll,
  48. ListView_LOnScroll,
  49. };
  50. const PFNLISTVIEW_SCROLL2 pfnListView_Scroll2[4] = {
  51. ListView_IScroll2,
  52. ListView_RScroll2,
  53. ListView_IScroll2,
  54. ListView_LScroll2,
  55. };
  56. const PFNLISTVIEW_GETSCROLLUNITSPERLINE pfnListView_GetScrollUnitsPerLine[4] = {
  57. ListView_IGetScrollUnitsPerLine,
  58. ListView_RGetScrollUnitsPerLine,
  59. ListView_IGetScrollUnitsPerLine,
  60. ListView_LGetScrollUnitsPerLine,
  61. };
  62. // redefine to trace at most calls to ListView_SendChange
  63. #define DM_LVSENDCHANGE 0
  64. // BUGBUG -- penwin.h is messed up; define local stuff for now
  65. #define HN_BEGINDIALOG 40 // Lens/EditText/garbage detection dialog is about
  66. // to come up on this hedit/bedit
  67. #define HN_ENDDIALOG 41 // Lens/EditText/garbage detection dialog has
  68. // just been destroyed
  69. //---------------------------------------------------------
  70. // no way am I gonna make TWO function calls where I can do FOUR comparisons!
  71. //
  72. #define RECTS_IN_SIZE( sz, r2 ) (!RECTS_NOT_IN_SIZE( sz, r2 ))
  73. #define RECTS_NOT_IN_SIZE( sz, r2 ) (\
  74. ( (sz).cx <= (r2).left ) ||\
  75. ( 0 >= (r2).right ) ||\
  76. ( (sz).cy <= (r2).top ) ||\
  77. ( 0 >= (r2).bottom ) )
  78. //---------------------------------------------------------
  79. void NEAR ListView_OnUpdate(LV* plv, int i);
  80. void NEAR ListView_OnDestroy(LV* plv);
  81. BOOL NEAR PASCAL ListView_ValidateScrollParams(LV* plv, int FAR * dx, int FAR *dy);
  82. UINT LV_IsItemOnViewEdge(LV* plv, LISTITEM *pitem);
  83. void PASCAL ListView_ButtonSelect(LV* plv, int iItem, UINT keyFlags, BOOL bSelected);
  84. void NEAR ListView_DeselectAll(LV* plv, int iDontDeselect);
  85. void ListView_LRInvalidateBelow(LV* plv, int i, int fSmoothScroll);
  86. void ListView_IInvalidateBelow(LV* plv, int i);
  87. void NEAR ListView_InvalidateFoldedItem(LV* plv, int iItem, BOOL fSelectionOnly, UINT fRedraw);
  88. void ListView_ReleaseBkImage(LV *plv);
  89. void ListView_RecalcRegion(LV *plv, BOOL fForce, BOOL fRedraw);
  90. BOOL g_fSlowMachine = -1;
  91. #pragma code_seg(CODESEG_INIT)
  92. BOOL FAR ListView_Init(HINSTANCE hinst)
  93. {
  94. WNDCLASS wc;
  95. wc.lpfnWndProc = ListView_WndProc;
  96. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  97. wc.hIcon = NULL;
  98. wc.lpszMenuName = NULL;
  99. wc.hInstance = hinst;
  100. wc.lpszClassName = c_szListViewClass;
  101. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // NULL;
  102. wc.style = CS_DBLCLKS | CS_GLOBALCLASS;
  103. wc.cbWndExtra = sizeof(LV*);
  104. wc.cbClsExtra = 0;
  105. RegisterClass(&wc);
  106. return TRUE;
  107. }
  108. #pragma code_seg()
  109. BOOL NEAR ListView_GetRegIASetting(BOOL *pb)
  110. {
  111. HKEY hkey;
  112. BOOL bRet = FALSE;
  113. BOOL bValue = TRUE;
  114. if (RegOpenKeyEx(HKEY_CURRENT_USER, IE_SETTINGS, 0, KEY_READ, &hkey) == ERROR_SUCCESS)
  115. {
  116. DWORD dwType;
  117. DWORD dwValue;
  118. DWORD cbValue = sizeof(DWORD);
  119. if (RegQueryValueEx(hkey, (LPTSTR)USE_DBL_CLICK_TIMER, 0, &dwType, (LPBYTE)&dwValue, &cbValue) == ERROR_SUCCESS)
  120. {
  121. bValue = (BOOL)dwValue;
  122. bRet = TRUE;
  123. }
  124. RegCloseKey(hkey);
  125. }
  126. *pb = bValue;
  127. return bRet;
  128. }
  129. BOOL NEAR ListView_NotifyCacheHint(LV* plv, int iFrom, int iTo)
  130. {
  131. NM_CACHEHINT nm;
  132. ASSERT( iFrom <= iTo );
  133. if (iFrom <= iTo)
  134. {
  135. nm.iFrom = iFrom;
  136. nm.iTo = iTo;
  137. return !(BOOL)CCSendNotify(&plv->ci, LVN_ODCACHEHINT, &nm.hdr);
  138. }
  139. return FALSE;
  140. }
  141. void NEAR ListView_LazyCreateObjects(LV *plv, int iMin, int iMax)
  142. {
  143. for ( ; iMin < iMax; iMin++)
  144. MyNotifyWinEvent(EVENT_OBJECT_CREATE, plv->ci.hwnd, OBJID_CLIENT, 1 + iMin);
  145. }
  146. //
  147. // Owner-data causes MSAA lots of grief, because there is no way to tell
  148. // MSAA "I just created 25 million items". You have to tell it one at a
  149. // time. Instead of sending out 25 million "add item" notifications, we
  150. // just send them out as they scroll into view.
  151. //
  152. // plv->iMSAAMin and plv->iMSAAMax are the range of items we most
  153. // recently told MSAA about. MSAAMax is *exclusive*, just like RECTs.
  154. // It makes the math easier.
  155. //
  156. // We use iMSAAMin and iMSAAMax to avoid sending blatantly redundant
  157. // notifications, which would other happen very frequently.
  158. //
  159. void NEAR ListView_LazyCreateWinEvents(LV *plv, int iFrom, int iTo)
  160. {
  161. int iMin = iFrom;
  162. int iMax = iTo+1; // Convert from [From,To] to [Min,Max)
  163. #ifdef LVDEBUG
  164. DebugMsg(TF_LISTVIEW, TEXT("lv.CWE old: [%d,%d), new=[%d,%d)"),
  165. plv->iMSAAMin, plv->iMSAAMax,
  166. iMin, iMax);
  167. #endif
  168. //
  169. // If the incoming range is entirely contained within the existing
  170. // range, then there is nothing to do. This happens a lot.
  171. //
  172. if (iMin >= plv->iMSAAMin && iMax <= plv->iMSAAMax)
  173. return;
  174. //
  175. // If the incoming range is adjacent to or overlaps the low end
  176. // of the existing range... (This happens when scrolling backwards.)
  177. //
  178. if (iMin <= plv->iMSAAMin && iMax >= plv->iMSAAMin) {
  179. // Notify the low end.
  180. ListView_LazyCreateObjects(plv, iMin, plv->iMSAAMin);
  181. // Extend the list of things we've notified.
  182. plv->iMSAAMin = iMin;
  183. // Remove it from the things left to be notified.
  184. iMin = plv->iMSAAMax;
  185. }
  186. //
  187. // Now do the same thing to the top end.
  188. // (This happens when scrolling forwards.)
  189. //
  190. if (iMax >= plv->iMSAAMax && iMin <= plv->iMSAAMax) {
  191. // Notify the top end.
  192. ListView_LazyCreateObjects(plv, plv->iMSAAMax, iMax);
  193. // Extend the list of things we've notified.
  194. plv->iMSAAMax = iMax;
  195. // Remove it from the things left to be notified.
  196. iMax = plv->iMSAAMin;
  197. }
  198. //
  199. // If there are still things to be notified, then it means that the
  200. // incoming range isn't contiguous with the previous range, so throw
  201. // away the old range and just set it to the current range.
  202. // (This happens when you grab the scrollbar and jump to a completely
  203. // unrelated part of the listview.)
  204. //
  205. if (iMin < iMax) {
  206. plv->iMSAAMin = iMin;
  207. plv->iMSAAMax = iMax;
  208. ListView_LazyCreateObjects(plv, iMin, iMax);
  209. }
  210. #ifdef LVDEBUG
  211. DebugMsg(TF_LISTVIEW, TEXT("lv.CWE aft: [%d,%d)"), plv->iMSAAMin, plv->iMSAAMax);
  212. #endif
  213. }
  214. LRESULT NEAR ListView_RequestFindItem(LV* plv, CONST LV_FINDINFO* plvfi, int iStart)
  215. {
  216. NM_FINDITEM nm;
  217. nm.lvfi = *plvfi;
  218. nm.iStart = iStart;
  219. return CCSendNotify(&plv->ci, LVN_ODFINDITEM, &nm.hdr);
  220. }
  221. BOOL NEAR ListView_SendChange(LV* plv, int i, int iSubItem, int code, UINT oldState, UINT newState,
  222. UINT changed, LPARAM lParam)
  223. {
  224. NM_LISTVIEW nm;
  225. nm.iItem = i;
  226. nm.iSubItem = iSubItem;
  227. nm.uNewState = newState;
  228. nm.uOldState = oldState;
  229. nm.uChanged = changed;
  230. nm.ptAction.x = 0;
  231. nm.ptAction.y = 0;
  232. nm.lParam = lParam;
  233. return !CCSendNotify(&plv->ci, code, &nm.hdr);
  234. }
  235. void NEAR ListView_SendODChangeAndInvalidate(LV* plv, int iFrom, int iTo, UINT oldState,
  236. UINT newState)
  237. {
  238. NM_ODSTATECHANGE nm;
  239. nm.iFrom = iFrom;
  240. nm.iTo = iTo;
  241. nm.uNewState = newState;
  242. nm.uOldState = oldState;
  243. CCSendNotify(&plv->ci, LVN_ODSTATECHANGED, &nm.hdr);
  244. // Tell accessibility, "Selection changed in a complex way"
  245. MyNotifyWinEvent(EVENT_OBJECT_SELECTIONWITHIN, plv->ci.hwnd, OBJID_CLIENT, CHILDID_SELF);
  246. // considerable speed increase less than 100 to do this method
  247. // while over 100, the other method works faster
  248. if ((iTo - iFrom) > 100)
  249. {
  250. InvalidateRect( plv->ci.hwnd, NULL, FALSE );
  251. }
  252. else
  253. {
  254. while (iFrom <= iTo)
  255. {
  256. ListView_InvalidateItem(plv, iFrom, TRUE, RDW_INVALIDATE);
  257. iFrom++;
  258. }
  259. }
  260. }
  261. BOOL NEAR ListView_Notify(LV* plv, int i, int iSubItem, int code)
  262. {
  263. NM_LISTVIEW nm;
  264. nm.iItem = i;
  265. nm.iSubItem = iSubItem;
  266. nm.uNewState = nm.uOldState = 0;
  267. nm.uChanged = 0;
  268. nm.lParam = 0;
  269. if (!ListView_IsOwnerData( plv )) {
  270. if (code == LVN_DELETEITEM) {
  271. LISTITEM FAR * pItem = ListView_GetItemPtr(plv, i);
  272. if (pItem) {
  273. nm.lParam = pItem->lParam;
  274. }
  275. }
  276. }
  277. return (BOOL)CCSendNotify(&plv->ci, code, &nm.hdr);
  278. }
  279. BOOL NEAR ListView_GetEmptyText(LV* plv)
  280. {
  281. NMLVDISPINFO nm;
  282. BOOL ret;
  283. TCHAR szText[80];
  284. if (plv->fNoEmptyText)
  285. return FALSE;
  286. if (plv->pszEmptyText)
  287. return TRUE;
  288. // For each listview control, we will only send this notify
  289. // once if necessary.
  290. memset(&nm, 0, SIZEOF(nm));
  291. nm.item.mask = LVIF_TEXT;
  292. nm.item.cchTextMax = ARRAYSIZE(szText);
  293. nm.item.pszText = szText;
  294. szText[0] = TEXT('\0');
  295. ret = (BOOL)CCSendNotify(&plv->ci, LVN_GETEMPTYTEXT, &nm.hdr);
  296. if (ret)
  297. // save the text so we don't notify again.
  298. Str_Set(&plv->pszEmptyText, szText);
  299. else
  300. // set a flag so we don't notify again.
  301. plv->fNoEmptyText = TRUE;
  302. return ret;
  303. }
  304. void NEAR ListView_NotifyFocusEvent(LV *plv)
  305. {
  306. if (plv->iFocus != -1 && IsWindowVisible(plv->ci.hwnd) && GetFocus() == plv->ci.hwnd)
  307. MyNotifyWinEvent(EVENT_OBJECT_FOCUS, plv->ci.hwnd, OBJID_CLIENT,
  308. plv->iFocus+1);
  309. }
  310. //
  311. // Call this function when the listview has changed in a radical manner.
  312. // It notifies MSAA that "Whoa, things are completely different now."
  313. //
  314. void NEAR ListView_NotifyRecreate(LV *plv)
  315. {
  316. MyNotifyWinEvent(EVENT_OBJECT_DESTROY, plv->ci.hwnd, OBJID_CLIENT, CHILDID_SELF);
  317. MyNotifyWinEvent(EVENT_OBJECT_CREATE, plv->ci.hwnd, OBJID_CLIENT, CHILDID_SELF);
  318. plv->iMSAAMin = plv->iMSAAMax = 0;
  319. }
  320. int NEAR ListView_OnSetItemCount(LV *plv, int iItems, DWORD dwFlags)
  321. {
  322. BOOL frt = TRUE;
  323. // For compatability we assume 0 for flags implies old (Athena) type of functionality and
  324. // does a Invalidate all otherwise if low bit is set we try to be a bit smarter. First pass
  325. // If the first added item is visible invalidate all. Yes we can do better...
  326. if (ListView_IsOwnerData( plv )) {
  327. int iItem;
  328. int cTotalItemsOld = plv->cTotalItems;
  329. BOOL fInvalidateAll = ((dwFlags & LVSICF_NOINVALIDATEALL) == 0);
  330. if ((iItems >= 0) && (iItems <= MAX_LISTVIEWITEMS)) {
  331. plv->cTotalItems = iItems;
  332. // check focus
  333. if (plv->iFocus >= iItems)
  334. plv->iFocus = -1;
  335. if (plv->iDropHilite >= iItems)
  336. plv->iDropHilite = -1;
  337. // check mark
  338. if (plv->iMark >= iItems)
  339. plv->iMark = -1;
  340. // make sure no selections above number of items
  341. plv->plvrangeCut->lpVtbl->ExcludeRange(plv->plvrangeCut, iItems, SELRANGE_MAXVALUE );
  342. if (FAILED(plv->plvrangeSel->lpVtbl->ExcludeRange(plv->plvrangeSel, iItems, SELRANGE_MAXVALUE ))) {
  343. //BUGBUG: Return low memory status
  344. //MemoryLowDlg( plv->ci.hwnd );
  345. return FALSE;
  346. }
  347. plv->rcView.left = RECOMPUTE; // recompute view rect
  348. if ( ListView_IsSmallView(plv) || ListView_IsIconView(plv) ) {
  349. // Call off to the arrange function.
  350. ListView_OnArrange(plv, LVA_DEFAULT);
  351. if (!fInvalidateAll)
  352. {
  353. // Try to be smart and invalidate only what we need to.
  354. // Add a little logic to erase any message like no items found when
  355. // the view was previously empty...
  356. if (cTotalItemsOld < iItems)
  357. iItem = cTotalItemsOld;
  358. else
  359. iItem = iItems - 1; // Get the index
  360. if ((iItem >= 0) && (cTotalItemsOld > 0))
  361. ListView_IInvalidateBelow(plv, iItem);
  362. else
  363. fInvalidateAll = TRUE;
  364. }
  365. } else {
  366. ListView_Recompute(plv);
  367. // if we have empty text and old count was zero... then we should redraw all
  368. if (plv->pszEmptyText && (cTotalItemsOld == 0) && (iItems > 0))
  369. fInvalidateAll = TRUE;
  370. // Try to do smart invalidates...
  371. if (!fInvalidateAll)
  372. {
  373. // Try to be smart and invalidate only what we need to.
  374. if (cTotalItemsOld < iItems)
  375. iItem = cTotalItemsOld;
  376. else
  377. iItem = iItems - 1; // Get the index
  378. if (iItem >= 0)
  379. ListView_LRInvalidateBelow(plv, iItem, FALSE);
  380. }
  381. // We may try to resize the column
  382. ListView_MaybeResizeListColumns(plv, 0, ListView_Count(plv)-1);
  383. // For compatability we assume 0 for flags implies old type
  384. // of functionality and scrolls the important item into view.
  385. // If second bit is set, we leave the scroll position alone.
  386. if ((dwFlags & LVSICF_NOSCROLL) == 0) {
  387. // what is the important item
  388. iItem = (plv->iFocus >= 0) ?
  389. plv->iFocus :
  390. ListView_OnGetNextItem(plv, -1, LVNI_SELECTED);
  391. iItem = max(0, iItem);
  392. // make important item visable
  393. ListView_OnEnsureVisible(plv, iItem, FALSE);
  394. }
  395. }
  396. if (fInvalidateAll)
  397. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  398. ListView_UpdateScrollBars(plv);
  399. ListView_NotifyRecreate(plv);
  400. ListView_NotifyFocusEvent(plv);
  401. } else {
  402. frt = FALSE;
  403. }
  404. } else {
  405. if (plv->hdpaSubItems)
  406. {
  407. int iCol;
  408. for (iCol = plv->cCol - 1; iCol >= 0; iCol--)
  409. {
  410. HDPA hdpa = ListView_GetSubItemDPA(plv, iCol);
  411. if (hdpa) // this is optional, call backs don't have them
  412. DPA_Grow(hdpa, iItems);
  413. }
  414. }
  415. DPA_Grow(plv->hdpa, iItems);
  416. DPA_Grow(plv->hdpaZOrder, iItems);
  417. }
  418. return frt;
  419. }
  420. typedef struct _LVSortInfo
  421. {
  422. LV* plv;
  423. BOOL fSortIndices;
  424. PFNLVCOMPARE pfnCompare;
  425. LPARAM lParam;
  426. BOOL bPassLP;
  427. } LVSortInfo;
  428. int CALLBACK ListView_SortCallback(LPVOID dw1, LPVOID dw2, LPARAM lParam)
  429. {
  430. LISTITEM FAR *pitem1;
  431. LISTITEM FAR *pitem2;
  432. LVSortInfo FAR *pSortInfo = (LVSortInfo FAR *)lParam;
  433. ASSERT(!ListView_IsOwnerData(pSortInfo->plv));
  434. // determine whether dw1 and dw2 are indices or the real items
  435. // and assign pitem? accordingly
  436. if (pSortInfo->fSortIndices) {
  437. pitem1 = ListView_GetItemPtr(pSortInfo->plv, PtrToUlong(dw1));
  438. pitem2 = ListView_GetItemPtr(pSortInfo->plv, PtrToUlong(dw2));
  439. } else {
  440. pitem1 = (LISTITEM FAR *)dw1;
  441. pitem2 = (LISTITEM FAR *)dw2;
  442. }
  443. if (!pSortInfo->pfnCompare) {
  444. // Treat NULL pszText like null string.
  445. LPCTSTR pszText1 = pitem1->pszText ? pitem1->pszText : c_szNULL;
  446. LPCTSTR pszText2 = pitem2->pszText ? pitem2->pszText : c_szNULL;
  447. // bugbug, should allow callbacks in text
  448. if (pszText1 != LPSTR_TEXTCALLBACK &&
  449. pszText2 != LPSTR_TEXTCALLBACK )
  450. {
  451. return lstrcmpi(pitem1->pszText, pitem2->pszText);
  452. }
  453. RIPMSG(0, "LVM_SORTITEM(EX): Cannot combine NULL callback with LPSTR_TEXTCALLBACK");
  454. return -1;
  455. } else
  456. {
  457. if (pSortInfo->bPassLP)
  458. return(pSortInfo->pfnCompare(pitem1->lParam, pitem2->lParam, pSortInfo->lParam));
  459. else
  460. {
  461. if (pSortInfo->fSortIndices)
  462. return(pSortInfo->pfnCompare((LPARAM)dw1, (LPARAM)dw2, pSortInfo->lParam));
  463. else
  464. {
  465. // we want to sort by the indices, but all we've got are pointers to the items
  466. // and there is no way to get back from that pointer to an index
  467. ASSERT(0);
  468. return -1;
  469. }
  470. }
  471. }
  472. ASSERT(0);
  473. return -1;
  474. }
  475. VOID ListView_InvalidateTTLastHit(LV* plv, int iNewHit)
  476. {
  477. if (plv->iTTLastHit == iNewHit)
  478. {
  479. plv->iTTLastHit = -1;
  480. if (plv->pszTip && plv->pszTip != LPSTR_TEXTCALLBACK)
  481. {
  482. plv->pszTip[0] = 0;
  483. }
  484. }
  485. }
  486. BOOL NEAR PASCAL ListView_SortAllColumns(LV* plv, LVSortInfo FAR * psi)
  487. {
  488. ASSERT(!ListView_IsOwnerData(plv));
  489. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  490. // don't do this optimization if we will need the indices to sort by
  491. if (psi->bPassLP && ((!plv->hdpaSubItems) || !DPA_GetPtrCount(plv->hdpaSubItems))) {
  492. psi->fSortIndices = FALSE;
  493. return (DPA_Sort(plv->hdpa, ListView_SortCallback, (LPARAM)psi));
  494. } else {
  495. // if we need to sort several hdpa's, create one DPA of just indices
  496. // and sort that, then fix up all the dpa's
  497. BOOL fReturn = FALSE;
  498. HDPA hdpa;
  499. int i;
  500. int iMax;
  501. void FAR * FAR * ph;
  502. void FAR * FAR *pNewIndices;
  503. // initialize the hdpa with indices
  504. hdpa = DPA_Clone(plv->hdpa, NULL);
  505. if (hdpa) {
  506. ASSERT(DPA_GetPtrCount(plv->hdpa) == DPA_GetPtrCount(hdpa));
  507. ph = pNewIndices = DPA_GetPtrPtr(hdpa);
  508. iMax = DPA_GetPtrCount(hdpa);
  509. for( i = 0; i < iMax; ph++, i++) {
  510. *ph = IntToPtr(i);
  511. }
  512. psi->fSortIndices = TRUE;
  513. if (DPA_Sort(hdpa, ListView_SortCallback, (LPARAM)psi)) {
  514. ph = LocalAlloc(LPTR, sizeof(LPVOID) * iMax);
  515. if (ph) {
  516. int j;
  517. void FAR * FAR *pSubItems;
  518. // we could get here because bPassLP is false, even if we don't have subitems
  519. if (plv->hdpaSubItems && DPA_GetPtrCount(plv->hdpaSubItems))
  520. {
  521. for (i = DPA_GetPtrCount(plv->hdpaSubItems) - 1; i >= 0; i--) {
  522. HDPA hdpaSubItem = ListView_GetSubItemDPA(plv, i);
  523. if (hdpaSubItem) {
  524. // make sure it's of the right size
  525. while (DPA_GetPtrCount(hdpaSubItem) < iMax) {
  526. if (DPA_InsertPtr(hdpaSubItem, iMax, NULL) == -1)
  527. goto Bail;
  528. }
  529. // actually copy across the dpa with the new indices
  530. pSubItems = DPA_GetPtrPtr(hdpaSubItem);
  531. for (j = 0; j < iMax; j++) {
  532. ph[j] = pSubItems[PtrToUlong(pNewIndices[j])];
  533. }
  534. // finally, copy it all back to the pSubItems;
  535. hmemcpy(pSubItems, ph, sizeof(LPVOID) * iMax);
  536. }
  537. }
  538. }
  539. // now do the main hdpa
  540. pSubItems = DPA_GetPtrPtr(plv->hdpa);
  541. for (j = 0; j < iMax; j++) {
  542. ph[j] = pSubItems[PtrToUlong(pNewIndices[j])];
  543. }
  544. // finally, copy it all back to the pSubItems;
  545. hmemcpy(pSubItems, ph, sizeof(LPVOID) * iMax);
  546. fReturn = TRUE;
  547. Bail:
  548. LocalFree(ph);
  549. }
  550. }
  551. DPA_Destroy(hdpa);
  552. }
  553. return fReturn;
  554. }
  555. }
  556. DWORD ListView_OnApproximateViewRect(LV* plv, int iCount, int iWidth, int iHeight)
  557. {
  558. if (iCount == -1)
  559. iCount = ListView_Count(plv);
  560. if (iWidth == -1)
  561. iWidth = plv->sizeClient.cx;
  562. if (iHeight == -1)
  563. iHeight = plv->sizeClient.cy;
  564. return _ListView_ApproximateViewRect(plv, iCount, iWidth, iHeight);
  565. }
  566. DWORD ListView_OnSetLVRangeObject(LV* plv, int iWhich, ILVRange *plvrange)
  567. {
  568. ILVRange **pplvrange;
  569. switch (iWhich)
  570. {
  571. case LVSR_SELECTION:
  572. pplvrange = &plv->plvrangeSel;
  573. break;
  574. case LVSR_CUT:
  575. pplvrange = &plv->plvrangeCut;
  576. break;
  577. default:
  578. return FALSE;
  579. }
  580. if (*pplvrange)
  581. {
  582. // Release the old one
  583. (*pplvrange)->lpVtbl->Release(*pplvrange);
  584. }
  585. *pplvrange = plvrange;
  586. // Hold onto the pointer...
  587. if (plvrange)
  588. plvrange->lpVtbl->AddRef(plvrange);
  589. return TRUE;
  590. }
  591. BOOL NEAR PASCAL ListView_OnSortItems(LV *plv, LPARAM lParam, PFNLVCOMPARE pfnCompare, BOOL bPassLP)
  592. {
  593. LVSortInfo SortInfo;
  594. LISTITEM FAR *pitemFocused;
  595. SortInfo.pfnCompare = pfnCompare;
  596. SortInfo.lParam = lParam;
  597. SortInfo.plv = plv;
  598. SortInfo.bPassLP = bPassLP;
  599. if (ListView_IsOwnerData(plv)) {
  600. RIPMSG(0, "LVM_SORTITEMS: Invalid for owner-data listview");
  601. return FALSE;
  602. }
  603. ListView_DismissEdit(plv, TRUE); // cancel edits
  604. // we're going to mess with the indices, so stash away the pointer to the
  605. // focused item.
  606. if (plv->iFocus != -1) {
  607. pitemFocused = ListView_GetItemPtr(plv, plv->iFocus);
  608. } else
  609. pitemFocused = NULL;
  610. if (ListView_SortAllColumns(plv, &SortInfo)) {
  611. // restore the focused item.
  612. if (pitemFocused) {
  613. int i;
  614. for (i = ListView_Count(plv) - 1; i >= 0 ; i--) {
  615. if (ListView_GetItemPtr(plv, i) == pitemFocused) {
  616. plv->iFocus = i;
  617. plv->iMark = i;
  618. }
  619. }
  620. }
  621. if (ListView_IsSmallView(plv) || ListView_IsIconView(plv))
  622. {
  623. ListView_CommonArrange(plv, LVA_DEFAULT, plv->hdpa);
  624. }
  625. else if (ListView_IsReportView(plv) || ListView_IsListView(plv))
  626. {
  627. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  628. }
  629. // The items in the view have moved around; let apps know
  630. MyNotifyWinEvent(EVENT_OBJECT_REORDER, plv->ci.hwnd, OBJID_CLIENT, 0);
  631. return(TRUE);
  632. }
  633. return FALSE;
  634. }
  635. void PASCAL ListView_EnableWindow(LV* plv, BOOL wParam)
  636. {
  637. if (wParam) {
  638. if (plv->ci.style & WS_DISABLED) {
  639. plv->ci.style &= ~WS_DISABLED; // enabled
  640. ListView_OnSetBkColor(plv, plv->clrBkSave);
  641. }
  642. } else {
  643. if (!(plv->ci.style & WS_DISABLED)) {
  644. plv->clrBkSave = plv->clrBk;
  645. plv->ci.style |= WS_DISABLED; // disabled
  646. ListView_OnSetBkColor(plv, g_clrBtnFace);
  647. }
  648. }
  649. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  650. }
  651. BOOL NEAR ListView_IsItemVisible(LV* plv, int i)
  652. // Assumes parmss ok etc for speed. Called inside region calc code.
  653. {
  654. RECT rcBounds;
  655. RECT rc, rcScratch;
  656. // get bounding rect of item
  657. ListView_GetRects(plv, i, NULL, NULL, &rcBounds, NULL);
  658. // setup rect for listview client. Should perf this up for
  659. // multimonitor case where there are dead zones in work area...
  660. rc.left = 0;
  661. rc.top = 0;
  662. rc.right = plv->sizeClient.cx;
  663. rc.bottom= plv->sizeClient.cy;
  664. return IntersectRect(&rcScratch, &rc, &rcBounds);
  665. }
  666. // Helper for ListView_RecalcRegion
  667. #define BitOn(lpbits, x, y, cx) (*((BYTE *)(lpbits + ((y * cx) + (x / 8)))) & (0x80 >> (x % 8)))
  668. void ListView_RecalcRegion(LV* plv, BOOL fForce, BOOL fRedraw)
  669. {
  670. HRGN hrgnUnion = NULL;
  671. HRGN hrgn = NULL;
  672. int i;
  673. HDC hdc = NULL;
  674. BYTE * lpBits = NULL;
  675. HBITMAP hbmp = NULL, hbmpOld = NULL;
  676. RECT rc, rcIcon;
  677. LISTITEM FAR * pitem;
  678. BITMAP bm;
  679. // Bail out if we don't need to do any work
  680. if (!(plv->exStyle & LVS_EX_REGIONAL) || !ListView_RedrawEnabled(plv) ||
  681. (plv->flags & LVF_INRECALCREGION))
  682. return;
  683. // To prevent recursion
  684. plv->flags |= LVF_INRECALCREGION;
  685. if ((ListView_Count(plv) > 0)) {
  686. int cxIcon, cyIcon;
  687. int dxOffset, dyOffset;
  688. // Run through first to see if anything changed - bail if not!
  689. if (!fForce) {
  690. for (i = 0; i < ListView_Count(plv); i++) {
  691. pitem = ListView_FastGetItemPtr(plv, i);
  692. if (!ListView_IsItemVisible(plv, i))
  693. {
  694. if (pitem->hrgnIcon == (HANDLE)-1 || !pitem->hrgnIcon)
  695. // Item was invisible and still is. Nothing changed.
  696. continue;
  697. if (pitem->hrgnIcon)
  698. {
  699. // Item was visible and now is invisible... Something
  700. // changed.
  701. pitem->ptRgn.x = RECOMPUTE;
  702. pitem->ptRgn.y = RECOMPUTE;
  703. DeleteObject(pitem->hrgnIcon);
  704. pitem->hrgnIcon = NULL;
  705. }
  706. }
  707. ListView_GetRects(plv, i, NULL, &rc, NULL, NULL);
  708. // If the location of the icon or the text rectangle have
  709. // changed, then we need to continue so that we can recalculate
  710. // the region.
  711. if ((pitem->pt.x != pitem->ptRgn.x) ||
  712. (pitem->pt.y != pitem->ptRgn.y) ||
  713. (!pitem->hrgnIcon) ||
  714. !EqualRect((CONST RECT *)&pitem->rcTextRgn, (CONST RECT *)&rc))
  715. goto changed;
  716. }
  717. // If we go through all the items and nothing changed, then
  718. // we can return without doing any work!
  719. ASSERT(i == ListView_Count(plv));
  720. goto exit;
  721. changed:;
  722. }
  723. // Figure out the dimensions of the Icon rectangle - assumes
  724. // each Icon rectangle is the same size.
  725. ListView_GetRects(plv, 0, &rcIcon, NULL, NULL, NULL);
  726. // Center the icon in the rectangle
  727. ImageList_GetIconSize(plv->himl, &cxIcon, &cyIcon);
  728. dxOffset = (rcIcon.right - rcIcon.left - cxIcon) / 2;
  729. dyOffset = (rcIcon.bottom - rcIcon.top - cyIcon) / 2;
  730. cxIcon = rcIcon.right - rcIcon.left;
  731. cyIcon = rcIcon.bottom - rcIcon.top;
  732. if (!(hdc = CreateCompatibleDC(NULL)) ||
  733. (!(hbmp = CreateBitmap(cxIcon, cyIcon, 1, 1, NULL)))) {
  734. goto BailOut;
  735. }
  736. GetObject(hbmp, SIZEOF(bm), &bm);
  737. if (!(lpBits = (BYTE *)GlobalAlloc(GPTR, bm.bmWidthBytes * bm.bmHeight)))
  738. goto BailOut;
  739. hbmpOld = SelectObject(hdc, hbmp);
  740. PatBlt(hdc, 0, 0, cxIcon, cyIcon, WHITENESS);
  741. if (hrgnUnion = CreateRectRgn(0, 0, 0, 0)) {
  742. for (i = 0; i < ListView_Count(plv); i++)
  743. {
  744. int x, y, iResult;
  745. BOOL fStarted = FALSE;
  746. LPRECT lprc;
  747. pitem = ListView_FastGetItemPtr(plv, i);
  748. if (pitem->pt.y == RECOMPUTE)
  749. continue;
  750. if (!ListView_IsItemVisible(plv, i))
  751. {
  752. // ignore invisible items
  753. if (pitem->hrgnIcon && pitem->hrgnIcon!=(HANDLE)-1)
  754. {
  755. pitem->ptRgn.x = RECOMPUTE;
  756. pitem->ptRgn.y = RECOMPUTE;
  757. DeleteObject(pitem->hrgnIcon);
  758. pitem->hrgnIcon = (HANDLE)-1;
  759. }
  760. continue;
  761. }
  762. // Add the region for the icon text first
  763. ListView_GetRects(plv, i, &rcIcon, &rc, NULL, NULL);
  764. // If we're in edit mode always use rcTextRgn
  765. if (i == plv->iEdit)
  766. lprc = &pitem->rcTextRgn;
  767. else
  768. lprc = &rc;
  769. if (!(hrgn = CreateRectRgnIndirect(lprc)))
  770. goto Error;
  771. iResult = CombineRgn(hrgnUnion, hrgn, hrgnUnion, RGN_OR);
  772. DeleteObject(hrgn);
  773. if (iResult == ERROR) {
  774. // Error case - out of memory. Just select in a NULL region.
  775. Error:
  776. DeleteObject(hrgnUnion);
  777. hrgnUnion = NULL;
  778. break;
  779. }
  780. // Succeeded, copy the rectangle to rcTextRgn so we
  781. // can test against it in the future. Don't copy over
  782. // it if we are in edit mode, the rectangle is used to
  783. // store the edit window in that case.
  784. if (plv->iEdit != i)
  785. CopyRect(&pitem->rcTextRgn, (CONST RECT *)&rc);
  786. // Now create a region for the icon mask - or use the cached one
  787. if (!pitem->hrgnIcon || pitem->hrgnIcon == (HANDLE)-1) {
  788. // (pitem->pt.x != pitem->ptRgn.x) ||
  789. // (pitem->pt.y != pitem->ptRgn.y)) {
  790. HRGN hrgnIcon = NULL;
  791. // On slow machines, we'll just wrap the icon with a rectangle. But on
  792. // faster machines, we'll build a region that corresponds to the
  793. // mask for the icon so it looks sweet.
  794. if (g_fSlowMachine) {
  795. // Modify the rectangle slightly so it looks better
  796. // Glue the icon and text rectangles together
  797. rcIcon.bottom = rc.top;
  798. // Shrink the width of the rectangle so it's only as big as the icon itself
  799. InflateRect(&rcIcon, -dxOffset, 0);
  800. hrgnIcon = CreateRectRgnIndirect(&rcIcon);
  801. } else {
  802. // If the image isn't around, get it now.
  803. if (pitem->iImage == I_IMAGECALLBACK) {
  804. LV_ITEM item;
  805. item.iItem = i;
  806. item.iSubItem = 0;
  807. item.mask = LVIF_IMAGE;
  808. item.stateMask = LVIS_ALL;
  809. item.pszText = NULL;
  810. item.cchTextMax = 0;
  811. // BOGUS - do we need to worry about our state
  812. // getting messed up during the callback?
  813. ListView_OnGetItem(plv, &item);
  814. }
  815. ImageList_Draw(plv->himl, pitem->iImage, hdc, 0, 0, ILD_MASK | (pitem->state & LVIS_OVERLAYMASK));
  816. GetBitmapBits(hbmp, bm.bmWidthBytes * bm.bmHeight, (LPVOID)lpBits);
  817. for (y = 0; y < cyIcon; y++) {
  818. for (x = 0; x < cxIcon; x++) {
  819. if (!fStarted && !BitOn(lpBits, x, y, bm.bmWidthBytes)) {
  820. rc.left = x;
  821. rc.top = y;
  822. rc.bottom = y + 1;
  823. fStarted = TRUE;
  824. if (x == (cxIcon - 1)) {
  825. x++;
  826. goto AddIt;
  827. } else {
  828. continue;
  829. }
  830. }
  831. if (fStarted && BitOn(lpBits, x, y, bm.bmWidthBytes)) {
  832. AddIt:
  833. rc.right = x;
  834. //
  835. // Mirror the region so that the icons get displayed ok. [samera]
  836. //
  837. if (plv->ci.dwExStyle & RTL_MIRRORED_WINDOW)
  838. {
  839. int iLeft = rc.left;
  840. rc.left = (cxIcon - (rc.right+1));
  841. rc.right = (cxIcon - (iLeft+1));
  842. OffsetRect(&rc, rcIcon.left - dxOffset, rcIcon.top + dyOffset);
  843. }
  844. else
  845. OffsetRect(&rc, rcIcon.left + dxOffset, rcIcon.top + dyOffset);
  846. if (hrgn = CreateRectRgnIndirect(&rc)) {
  847. if (hrgnIcon || (hrgnIcon = CreateRectRgn(0, 0, 0, 0)))
  848. iResult = CombineRgn(hrgnIcon, hrgn, hrgnIcon, RGN_OR);
  849. else
  850. iResult = ERROR;
  851. DeleteObject(hrgn);
  852. }
  853. if (!hrgn || (iResult == ERROR)) {
  854. if (hrgnIcon)
  855. DeleteObject(hrgnIcon);
  856. goto Error;
  857. }
  858. fStarted = FALSE;
  859. }
  860. }
  861. }
  862. }
  863. if (hrgnIcon) {
  864. // Cache it since it takes a long time to build it
  865. if (pitem->hrgnIcon && pitem->hrgnIcon != (HANDLE)-1)
  866. DeleteObject(pitem->hrgnIcon);
  867. pitem->hrgnIcon = hrgnIcon;
  868. pitem->ptRgn = pitem->pt;
  869. // Add it to the accumulated window region
  870. if (ERROR == CombineRgn(hrgnUnion, hrgnIcon, hrgnUnion, RGN_OR))
  871. goto Error;
  872. }
  873. } else {
  874. OffsetRgn(pitem->hrgnIcon, pitem->pt.x - pitem->ptRgn.x, pitem->pt.y - pitem->ptRgn.y);
  875. pitem->ptRgn = pitem->pt;
  876. if (ERROR == CombineRgn(hrgnUnion, pitem->hrgnIcon, hrgnUnion, RGN_OR))
  877. goto Error;
  878. }
  879. }
  880. }
  881. }
  882. BailOut:
  883. if (lpBits)
  884. GlobalFree((HGLOBAL)lpBits);
  885. if (hbmp) {
  886. SelectObject(hdc, hbmpOld);
  887. DeleteObject(hbmp);
  888. }
  889. if (hdc)
  890. DeleteDC(hdc);
  891. // Windows takes ownership of the region when we select it in to the window
  892. SetWindowRgn(plv->ci.hwnd, hrgnUnion, fRedraw);
  893. exit:
  894. plv->flags &= ~LVF_INRECALCREGION;
  895. }
  896. HIMAGELIST CreateCheckBoxImagelist(HIMAGELIST himl, BOOL fTree, BOOL fUseColorKey, BOOL fMirror)
  897. {
  898. int cxImage, cyImage;
  899. HBITMAP hbm;
  900. HBITMAP hbmTemp;
  901. COLORREF clrMask;
  902. HDC hdcDesk = GetDC(NULL);
  903. HDC hdc;
  904. RECT rc;
  905. int nImages = fTree ? 3 : 2;
  906. if (!hdcDesk)
  907. return NULL;
  908. hdc = CreateCompatibleDC(hdcDesk);
  909. ReleaseDC(NULL, hdcDesk);
  910. if (!hdc)
  911. return NULL;
  912. // Must protect against ImageList_GetIconSize failing in case app
  913. // gave us a bad himl
  914. if (himl && ImageList_GetIconSize(himl, &cxImage, &cyImage)) {
  915. // cxImage and cyImage are okay
  916. } else {
  917. cxImage = g_cxSmIcon;
  918. cyImage = g_cySmIcon;
  919. }
  920. himl = ImageList_Create(cxImage, cyImage, ILC_MASK, 0, nImages);
  921. hbm = CreateColorBitmap(cxImage * nImages, cyImage);
  922. if (fUseColorKey)
  923. {
  924. clrMask = RGB(255,000,255); // magenta
  925. if (clrMask == g_clrWindow)
  926. clrMask = RGB(000,000,255); // blue
  927. }
  928. else
  929. {
  930. clrMask = g_clrWindow;
  931. }
  932. // fill
  933. hbmTemp = SelectObject(hdc, hbm);
  934. rc.left = rc.top = 0;
  935. rc.bottom = cyImage;
  936. rc.right = cxImage * nImages;
  937. FillRectClr(hdc, &rc, clrMask);
  938. rc.right = cxImage;
  939. // now draw the real controls on
  940. InflateRect(&rc, -g_cxEdge, -g_cyEdge);
  941. rc.right++;
  942. rc.bottom++;
  943. if (fTree)
  944. OffsetRect(&rc, cxImage, 0);
  945. DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_FLAT |
  946. (fUseColorKey? 0 : DFCS_TRANSPARENT));
  947. OffsetRect(&rc, cxImage, 0);
  948. // [msadek]; For the mirrored case, there is an off-by-one somewhere in MirrorIcon() or System API.
  949. // Since I will not be touching MirrorIcon() by any mean and no chance to fix a system API,
  950. // let's compensate for it here.
  951. if(fMirror)
  952. {
  953. OffsetRect(&rc, -1, 0);
  954. }
  955. DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_FLAT | DFCS_CHECKED |
  956. (fUseColorKey? 0 : DFCS_TRANSPARENT));
  957. SelectObject(hdc, hbmTemp);
  958. if (fUseColorKey)
  959. {
  960. ImageList_AddMasked(himl, hbm, clrMask);
  961. }
  962. else
  963. {
  964. ImageList_Add(himl, hbm, NULL);
  965. }
  966. if(fMirror)
  967. {
  968. HICON hIcon = ImageList_ExtractIcon(0, himl, nImages-1);
  969. MirrorIcon(&hIcon, NULL);
  970. ImageList_ReplaceIcon(himl, nImages-1, hIcon);
  971. }
  972. DeleteDC(hdc);
  973. DeleteObject( hbm );
  974. return himl;
  975. }
  976. void ListView_InitCheckBoxes(LV* plv, BOOL fInitializeState)
  977. {
  978. HIMAGELIST himlCopy = (plv->himlSmall ? plv->himlSmall : plv->himl);
  979. HIMAGELIST himl;
  980. BOOL fNoColorKey = FALSE; // Backwards: If Cleartype is turned on, then we don't use colorkey.
  981. BOOL bMirror = FALSE;
  982. if (g_bRunOnNT5)
  983. {
  984. #ifdef CLEARTYPE // Don't use SPI_CLEARTYPE because it's defined because of APIThk, but not in NT.
  985. SystemParametersInfo(SPI_GETCLEARTYPE, 0, &fNoColorKey, 0);
  986. #endif
  987. }
  988. // [msadek], CheckBoxed need not to be mirrored.
  989. // mirroer it during imagelist creation time so that it displays correctly
  990. himl = CreateCheckBoxImagelist(himlCopy, FALSE, !fNoColorKey, IS_WINDOW_RTL_MIRRORED(plv->ci.hwnd));
  991. ImageList_SetBkColor(himl, fNoColorKey ? (CLR_NONE) : (plv->clrBk));
  992. ListView_OnSetImageList(plv, himl, LVSIL_STATE);
  993. if (fInitializeState)
  994. ListView_OnSetItemState(plv, -1, INDEXTOSTATEIMAGEMASK(1), LVIS_STATEIMAGEMASK);
  995. }
  996. void ListView_PopBubble(LV *plv)
  997. {
  998. if (plv->hwndToolTips)
  999. SendMessage( plv->hwndToolTips, TTM_POP, 0, 0 );
  1000. }
  1001. DWORD NEAR PASCAL ListView_ExtendedStyleChange(LV* plv, DWORD dwNewStyle, DWORD dwExMask)
  1002. {
  1003. DWORD dwOldStyle = plv->exStyle;
  1004. // this messge didn't come till 3, so version is at least 3
  1005. if (plv->ci.iVersion < 3) {
  1006. plv->ci.iVersion = 3;
  1007. // this will change the listview report size and painting algorithm
  1008. // because of the leading edge, so need to re-update scroll bars
  1009. // and repaint everything
  1010. if (ListView_IsReportView(plv))
  1011. {
  1012. ListView_RUpdateScrollBars(plv);
  1013. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  1014. }
  1015. }
  1016. // Change of styles may also changes tooltip policy, so pop it
  1017. ListView_PopBubble(plv);
  1018. if (dwExMask)
  1019. dwNewStyle = (plv->exStyle & ~ dwExMask) | (dwNewStyle & dwExMask);
  1020. // Currently, the LVS_EX_REGIONAL style is only supported for large icon view
  1021. if (!ListView_IsIconView(plv)) {
  1022. dwNewStyle &= ~(LVS_EX_REGIONAL | LVS_EX_MULTIWORKAREAS);
  1023. }
  1024. // LVS_EX_REGIONAL is not supported for ownerdata
  1025. if (ListView_IsOwnerData(plv)) {
  1026. dwNewStyle &= ~LVS_EX_REGIONAL;
  1027. }
  1028. plv->exStyle = dwNewStyle;
  1029. // do any invalidation or whatever is needed here.
  1030. if ((dwOldStyle ^ dwNewStyle) & LVS_EX_GRIDLINES) {
  1031. if (ListView_IsReportView(plv)) {
  1032. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  1033. }
  1034. }
  1035. if ((dwOldStyle ^ dwNewStyle) & (LVS_EX_UNDERLINEHOT | LVS_EX_UNDERLINECOLD |
  1036. LVS_EX_ONECLICKACTIVATE | LVS_EX_TWOCLICKACTIVATE |
  1037. LVS_EX_SUBITEMIMAGES)) {
  1038. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  1039. }
  1040. if ((dwOldStyle ^ dwNewStyle) & LVS_EX_CHECKBOXES) {
  1041. if (dwNewStyle & LVS_EX_CHECKBOXES) {
  1042. ListView_InitCheckBoxes(plv, TRUE);
  1043. } else {
  1044. // destroy the check boxes!
  1045. HIMAGELIST himl = ListView_OnSetImageList(plv, NULL, LVSIL_STATE);
  1046. if (himl)
  1047. ImageList_Destroy(himl);
  1048. }
  1049. }
  1050. if ((dwOldStyle ^ dwNewStyle) & LVS_EX_FLATSB) {
  1051. if (dwNewStyle & LVS_EX_FLATSB) {
  1052. InitializeFlatSB(plv->ci.hwnd);
  1053. } else {
  1054. UninitializeFlatSB(plv->ci.hwnd);
  1055. }
  1056. }
  1057. if ((dwOldStyle ^ dwNewStyle) & LVS_EX_REGIONAL) {
  1058. if (g_fSlowMachine == -1) {
  1059. #ifdef NEVER
  1060. // Because some Alpha machines and faster pentiums were detected
  1061. // as slow machines (bug #30972 in IE4 database), it was decided
  1062. // to turn off this code.
  1063. SYSTEM_INFO si;
  1064. GetSystemInfo(&si);
  1065. g_fSlowMachine = (BOOL)((si.dwProcessorType == PROCESSOR_INTEL_386) ||
  1066. (si.dwProcessorType == PROCESSOR_INTEL_486));
  1067. #else
  1068. g_fSlowMachine = FALSE;
  1069. #endif
  1070. }
  1071. if (dwNewStyle & LVS_EX_REGIONAL) {
  1072. ListView_RecalcRegion(plv, TRUE, TRUE);
  1073. } else {
  1074. int i;
  1075. LISTITEM FAR * pitem;
  1076. // Delete all the cached regions, then NULL out our selected region.
  1077. for (i = 0; i < ListView_Count(plv); i++) {
  1078. pitem = ListView_FastGetItemPtr(plv, i);
  1079. if (pitem->hrgnIcon && pitem->hrgnIcon!=(HANDLE)-1) {
  1080. DeleteObject(pitem->hrgnIcon);
  1081. }
  1082. pitem->hrgnIcon = NULL;
  1083. }
  1084. SetWindowRgn(plv->ci.hwnd, (HRGN)NULL, TRUE);
  1085. }
  1086. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  1087. }
  1088. return dwOldStyle;
  1089. }
  1090. // BUGBUG raymondc v6.0: Doesn't detect WM_WINDOWPOSCHANGING as a way
  1091. // of being shown. NT5 defview has to hack around it pretty grossly.
  1092. // Fix for v6.0.
  1093. void NEAR PASCAL LV_OnShowWindow(LV* plv, BOOL fShow)
  1094. {
  1095. if (fShow) {
  1096. if (!(plv->flags & LVF_VISIBLE)) {
  1097. plv->flags |= LVF_VISIBLE;
  1098. ListView_UpdateScrollBars(plv);
  1099. }
  1100. } else
  1101. plv->flags &= ~LVF_VISIBLE;
  1102. }
  1103. LRESULT NEAR PASCAL ListView_OnHelp(LV* plv, LPHELPINFO lpHelpInfo)
  1104. {
  1105. // If we're seeing WM_HELP because of our child header control, then
  1106. // munge the HELPINFO structure to use the ListView's control id.
  1107. // win\core\user\combo.c has similiar code to handle the child edit
  1108. // control of a combo box.
  1109. if ((lpHelpInfo != NULL) && ((plv->ci.style & LVS_TYPEMASK) == LVS_REPORT) &&
  1110. (lpHelpInfo->iCtrlId == LVID_HEADER)) {
  1111. lpHelpInfo->hItemHandle = plv->ci.hwnd;
  1112. lpHelpInfo->iCtrlId = GetWindowID(plv->ci.hwnd);
  1113. // Shouldn't have to do this: USER would have filled in the appropriate
  1114. // context id by walking up the parent hwnd chain.
  1115. //lpHelpInfo->dwContextId = GetContextHelpId(hwnd);
  1116. }
  1117. return DefWindowProc(plv->ci.hwnd, WM_HELP, 0, (LPARAM)lpHelpInfo);
  1118. }
  1119. DWORD NEAR PASCAL ListView_OnSetIconSpacing(LV* plv, LPARAM lParam)
  1120. {
  1121. DWORD dwOld = MAKELONG(plv->cxIconSpacing, plv->cyIconSpacing);
  1122. if (lParam == (LPARAM)-1) {
  1123. // go back to using defaults
  1124. plv->flags &= ~LVF_ICONSPACESET;
  1125. plv->cxIconSpacing = (plv->cxIcon + (g_cxIconSpacing - g_cxIcon));
  1126. plv->cyIconSpacing = (plv->cyIcon + (g_cyIconSpacing - g_cyIcon));
  1127. } else {
  1128. if (LOWORD(lParam))
  1129. plv->cxIconSpacing = LOWORD(lParam);
  1130. if (HIWORD(lParam))
  1131. plv->cyIconSpacing = HIWORD(lParam);
  1132. plv->flags |= LVF_ICONSPACESET;
  1133. }
  1134. plv->iFreeSlot = -1;
  1135. return dwOld;
  1136. }
  1137. BOOL ListView_OnSetCursorMsg(LV* plv)
  1138. {
  1139. if (plv->exStyle & (LVS_EX_ONECLICKACTIVATE|LVS_EX_TWOCLICKACTIVATE)) {
  1140. if (plv->iHot != -1) {
  1141. if (((plv->exStyle & LVS_EX_ONECLICKACTIVATE && plv->fOneClickOK)) ||
  1142. ListView_OnGetItemState(plv, plv->iHot, LVIS_SELECTED)) {
  1143. if (!plv->hCurHot)
  1144. plv->hCurHot = LoadHandCursor(0);
  1145. SetCursor(plv->hCurHot);
  1146. return TRUE;
  1147. }
  1148. }
  1149. }
  1150. return FALSE;
  1151. }
  1152. void ListView_OnSetHotItem(LV* plv, int iItem)
  1153. {
  1154. UINT maskChanged;
  1155. if (iItem != plv->iHot) {
  1156. BOOL fSelectOnly;
  1157. UINT fRedraw = RDW_INVALIDATE;
  1158. #ifndef DONT_UNDERLINE
  1159. if (plv->clrTextBk == CLR_NONE)
  1160. fRedraw |= RDW_ERASE;
  1161. #endif
  1162. fSelectOnly = ListView_FullRowSelect(plv);
  1163. maskChanged = (plv->exStyle & LVS_EX_BORDERSELECT) ? LVIF_TEXT | LVIF_IMAGE : LVIF_TEXT;
  1164. ListView_InvalidateItemEx(plv, plv->iHot, fSelectOnly, fRedraw, maskChanged);
  1165. ListView_InvalidateItemEx(plv, iItem, fSelectOnly, RDW_INVALIDATE, maskChanged);
  1166. plv->iHot = iItem;
  1167. }
  1168. }
  1169. /// Usability test prototype
  1170. // CHEEBUGBUG
  1171. BOOL fShouldFirstClickActivate()
  1172. {
  1173. static BOOL fInited = FALSE;
  1174. static BOOL fActivate = TRUE;
  1175. if (!fInited) {
  1176. long cb = 0;
  1177. if (RegQueryValue(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\NoFirstClickActivate"),
  1178. NULL, &cb) == ERROR_SUCCESS)
  1179. fActivate = FALSE;
  1180. fInited = TRUE;
  1181. }
  1182. return fActivate;
  1183. }
  1184. BOOL ChildOfDesktop(HWND hwnd)
  1185. {
  1186. return IsChild(GetShellWindow(), hwnd);
  1187. }
  1188. void ListView_OnMouseMove(LV* plv, int x, int y, UINT uFlags)
  1189. {
  1190. if (plv->exStyle & (LVS_EX_TRACKSELECT|LVS_EX_ONECLICKACTIVATE|LVS_EX_TWOCLICKACTIVATE)
  1191. // CHEEBUGBUG: for usability testing
  1192. && (ChildOfActiveWindow(plv->ci.hwnd) || fShouldFirstClickActivate() ||
  1193. ChildOfDesktop(plv->ci.hwnd))
  1194. ) {
  1195. int iItem;
  1196. LV_HITTESTINFO ht;
  1197. NMLISTVIEW nm;
  1198. ht.pt.x = x;
  1199. ht.pt.y = y;
  1200. iItem = ListView_OnSubItemHitTest(plv, &ht);
  1201. if (ht.iSubItem != 0) {
  1202. // if we're not in full row select,
  1203. // hitting on a subitem is like hitting on nowhere
  1204. // also, in win95, ownerdraw fixed effectively had full row select
  1205. if (!ListView_FullRowSelect(plv) &&
  1206. !(plv->ci.style & LVS_OWNERDRAWFIXED)) {
  1207. iItem = -1;
  1208. ht.flags = LVHT_NOWHERE;
  1209. }
  1210. }
  1211. if (ht.flags & LVHT_NOWHERE ||
  1212. ht.flags & LVHT_ONITEMSTATEICON) {
  1213. iItem = -1; // this is possible in the list mode
  1214. }
  1215. nm.iItem = iItem;
  1216. nm.iSubItem = ht.iSubItem;
  1217. nm.uChanged = 0;
  1218. nm.ptAction.x = x;
  1219. nm.ptAction.y = y;
  1220. if (!CCSendNotify(&plv->ci, LVN_HOTTRACK, &nm.hdr)) {
  1221. #ifdef DEBUG
  1222. if ((nm.iItem != -1) && nm.iSubItem != 0)
  1223. nm.iItem = -1;
  1224. #endif
  1225. ListView_OnSetHotItem(plv, nm.iItem);
  1226. // Ensure our cursor is correct now since the WM_SETCURSOR
  1227. // message was already generated for this mouse event.
  1228. ListView_OnSetCursorMsg(plv);
  1229. // this lets us know when we've left an item
  1230. // and can then reselect/toggle it on hover events
  1231. if (iItem != plv->iNoHover) {
  1232. plv->iNoHover = -1;
  1233. }
  1234. }
  1235. }
  1236. }
  1237. BOOL EditBoxHasFocus()
  1238. {
  1239. HWND hwndFocus = GetFocus();
  1240. if (hwndFocus) {
  1241. if (SendMessage(hwndFocus, WM_GETDLGCODE, 0, 0) & DLGC_HASSETSEL)
  1242. return TRUE;
  1243. }
  1244. return FALSE;
  1245. }
  1246. void ListView_OnMouseHover(LV* plv, int x, int y, UINT uFlags)
  1247. {
  1248. int iItem;
  1249. BOOL bSelected;
  1250. LV_HITTESTINFO ht;
  1251. BOOL fControl;
  1252. BOOL fShift;
  1253. BOOL fNotifyReturn = FALSE;
  1254. if (GetCapture() || !ChildOfActiveWindow(plv->ci.hwnd) ||
  1255. EditBoxHasFocus())
  1256. return; // ignore hover while editing or any captured (d/d) operation
  1257. if (CCSendNotify(&plv->ci, NM_HOVER, NULL)) {
  1258. return;
  1259. }
  1260. // REVIEW: right button implies no shift or control stuff
  1261. // Single selection style also implies no modifiers
  1262. //if (RIGHTBUTTON(keyFlags) || (plv->ci.style & LVS_SINGLESEL))
  1263. if ((plv->ci.style & LVS_SINGLESEL)) {
  1264. fControl = FALSE;
  1265. fShift = FALSE;
  1266. } else {
  1267. fControl = GetAsyncKeyState(VK_CONTROL) < 0;
  1268. fShift = GetAsyncKeyState(VK_SHIFT) < 0;
  1269. }
  1270. ht.pt.x = x;
  1271. ht.pt.y = y;
  1272. iItem = ListView_OnHitTest(plv, &ht);
  1273. if (iItem == -1 ||
  1274. iItem == plv->iNoHover)
  1275. return;
  1276. //before we hover select we launch any pending item
  1277. //this prevents clicking on one item and hover selecting other before
  1278. //the timer goes off which result in wrong item being launched
  1279. if (plv->exStyle & LVS_EX_ONECLICKACTIVATE && plv->fOneClickHappened && plv->fOneClickOK)
  1280. {
  1281. HWND hwnd = plv->ci.hwnd;
  1282. KillTimer(plv->ci.hwnd, IDT_ONECLICKHAPPENED);
  1283. plv->fOneClickHappened = FALSE;
  1284. CCSendNotify(&plv->ci, LVN_ITEMACTIVATE, &(plv->nmOneClickHappened.hdr));
  1285. if (!IsWindow(hwnd))
  1286. return;
  1287. }
  1288. plv->iNoHover = iItem;
  1289. bSelected = ListView_OnGetItemState(plv, iItem, LVIS_SELECTED);
  1290. if (ht.flags & (LVHT_ONITEMLABEL | LVHT_ONITEMICON))
  1291. {
  1292. UINT keyFlags = 0;
  1293. if (fShift)
  1294. keyFlags |= MK_SHIFT;
  1295. if (fControl)
  1296. keyFlags |= MK_CONTROL;
  1297. if (!bSelected) {
  1298. // if it wasn't selected, we're about to select it... play
  1299. // a little ditty for us...
  1300. CCPlaySound(c_szSelect);
  1301. }
  1302. ListView_ButtonSelect(plv, iItem, keyFlags, bSelected);
  1303. if (fControl)
  1304. {
  1305. ListView_SetFocusSel(plv, iItem, !fShift, FALSE, !fShift);
  1306. }
  1307. if (!fShift)
  1308. plv->iMark = iItem;
  1309. ListView_OnSetCursorMsg(plv);
  1310. SetFocus(plv->ci.hwnd); // activate this window
  1311. }
  1312. }
  1313. BOOL EqualRects(LPRECT prcNew, LPRECT prcOld, int nRects)
  1314. {
  1315. int i;
  1316. for (i = 0; i < nRects; i++)
  1317. if (!EqualRect(&prcNew[i], &prcOld[i]))
  1318. return FALSE;
  1319. return TRUE;
  1320. }
  1321. BOOL ListView_FindWorkArea(LV * plv, POINT pt, short * piWorkArea)
  1322. {
  1323. int iWork;
  1324. for (iWork = 0; iWork < plv->nWorkAreas; iWork++)
  1325. {
  1326. if (PtInRect(&plv->prcWorkAreas[iWork], pt))
  1327. {
  1328. *piWorkArea = (short)iWork;
  1329. return TRUE;
  1330. }
  1331. }
  1332. // (dli) default case is the primary work area
  1333. *piWorkArea = 0;
  1334. return FALSE;
  1335. }
  1336. void ListView_BullyIconsOnWorkarea(LV * plv, HDPA hdpaLostItems)
  1337. {
  1338. int ihdpa;
  1339. int iFree = -1; // the last free slot number
  1340. LVFAKEDRAW lvfd;
  1341. LV_ITEM item;
  1342. // Caller should've filtered this case out
  1343. ASSERT(DPA_GetPtrCount(hdpaLostItems) > 0);
  1344. // Set up in case caller is customdraw
  1345. ListView_BeginFakeCustomDraw(plv, &lvfd, &item);
  1346. item.mask = LVIF_PARAM;
  1347. item.iSubItem = 0;
  1348. // Go through my hdpa list of lost icons and try to place them within bound
  1349. for (ihdpa = 0; ihdpa < DPA_GetPtrCount(hdpaLostItems); ihdpa++)
  1350. {
  1351. POINT ptNew, pt;
  1352. RECT rcBound;
  1353. int cxBound, cyBound;
  1354. int iWidth, iHeight;
  1355. int iItem;
  1356. LISTITEM FAR * pitem;
  1357. iItem = PtrToUlong(DPA_GetPtr(hdpaLostItems, ihdpa));
  1358. pitem = ListView_FastGetItemPtr(plv, iItem);
  1359. pt = pitem->pt;
  1360. iWidth = RECTWIDTH(plv->prcWorkAreas[pitem->iWorkArea]);
  1361. iHeight = RECTHEIGHT(plv->prcWorkAreas[pitem->iWorkArea]);
  1362. ListView_GetRects(plv, iItem, NULL, NULL, &rcBound, NULL);
  1363. cxBound = RECTWIDTH(rcBound);
  1364. cyBound = RECTHEIGHT(rcBound);
  1365. pt.x -= plv->prcWorkAreas[pitem->iWorkArea].left;
  1366. pt.y -= plv->prcWorkAreas[pitem->iWorkArea].top;
  1367. if (pt.x < (-cxBound / 2)) {
  1368. ptNew.x = 0;
  1369. } else if (pt.x > (iWidth - (cxBound / 2))) {
  1370. ptNew.x = iWidth - cxBound;
  1371. } else
  1372. ptNew.x = pt.x;
  1373. if (pt.y < (-cyBound/2)) {
  1374. ptNew.y = 0;
  1375. } else if (pt.y > (iHeight - (cyBound / 2))) {
  1376. ptNew.y = iHeight - cyBound;
  1377. } else
  1378. ptNew.y = pt.y;
  1379. if ((ptNew.x != pt.x) || (ptNew.y != pt.y))
  1380. {
  1381. BOOL fUpdate;
  1382. RECT rcTest;
  1383. ptNew.x += plv->prcWorkAreas[pitem->iWorkArea].left;
  1384. ptNew.y += plv->prcWorkAreas[pitem->iWorkArea].top;
  1385. // See if the potential rectangle intersects other items.
  1386. rcTest.left = ptNew.x - plv->ptOrigin.x;
  1387. rcTest.top = ptNew.y - plv->ptOrigin.y;
  1388. rcTest.right = rcTest.left + cxBound;
  1389. rcTest.bottom = rcTest.top + cyBound;
  1390. item.iItem = iItem;
  1391. item.lParam = pitem->lParam;
  1392. ListView_BeginFakeItemDraw(&lvfd);
  1393. if (!ListView_IsCleanRect(plv, &rcTest, iItem, &fUpdate, lvfd.nmcd.nmcd.hdc))
  1394. {
  1395. // doh! We hit another item, let's try to find an available location
  1396. // for this item
  1397. BOOL fUpdateSB;
  1398. BOOL fAppendAtEnd = FALSE;
  1399. int cSlots = ListView_GetSlotCountEx(plv, FALSE, pitem->iWorkArea);
  1400. iFree = ListView_FindFreeSlot(plv, iItem, iFree + 1, cSlots, &fUpdateSB, &fAppendAtEnd, lvfd.nmcd.nmcd.hdc);
  1401. if (iFree == -1)
  1402. goto SetFirstGuess;
  1403. ListView_SetIconPos(plv, pitem, iFree, cSlots);
  1404. ListView_EndFakeItemDraw(&lvfd);
  1405. continue;
  1406. }
  1407. SetFirstGuess:
  1408. ListView_EndFakeItemDraw(&lvfd);
  1409. ListView_OnSetItemPosition(plv, iItem, ptNew.x, ptNew.y);
  1410. }
  1411. }
  1412. ListView_EndFakeCustomDraw(&lvfd);
  1413. }
  1414. #define DPA_LAST 0x7fffffff
  1415. //
  1416. // ListView_OnSetWorkAreas
  1417. //
  1418. // set the "work areas" for the list view.
  1419. // the "work areas" are a group of sub rectanges of the list view client rect
  1420. // where icons are aranged, and parked by default.
  1421. //
  1422. void NEAR ListView_OnSetWorkAreas(LV* plv, int nWorkAreas, LPRECT prc)
  1423. {
  1424. int nOldWorkAreas;
  1425. int iWork;
  1426. BOOL bAutoArrange;
  1427. HDPA hdpaLostItems = NULL;
  1428. RECT rcOldWorkAreas[LV_MAX_WORKAREAS];
  1429. bAutoArrange = plv->ci.style & LVS_AUTOARRANGE;
  1430. nOldWorkAreas = plv->nWorkAreas;
  1431. if (nOldWorkAreas > 0)
  1432. {
  1433. ASSERT(plv->prcWorkAreas != NULL);
  1434. hmemcpy(&rcOldWorkAreas[0], &plv->prcWorkAreas[0], SIZEOF(RECT) * nOldWorkAreas);
  1435. }
  1436. // for the mirrored case, the coordinates are reversed. IsRectEmpty() will always succeed
  1437. if (nWorkAreas == 0 || prc == NULL || ((IsRectEmpty(prc)) && !(plv->ci.dwExStyle & RTL_MIRRORED_WINDOW)))
  1438. plv->nWorkAreas = 0;
  1439. else
  1440. {
  1441. plv->nWorkAreas = min(nWorkAreas, LV_MAX_WORKAREAS);
  1442. if (plv->prcWorkAreas == NULL)
  1443. plv->prcWorkAreas = (LPRECT)LocalAlloc(LPTR, SIZEOF(RECT) * LV_MAX_WORKAREAS);
  1444. if (plv->prcWorkAreas == NULL)
  1445. return;
  1446. //BUGBUG: Should we check if they intersect? This problem is sort of
  1447. // solved (or made more confusing) by ListView_GetFreeSlot since it checks all of the icons for
  1448. // intersection instead of just the ones in the workarea.
  1449. for (iWork = 0; iWork < plv->nWorkAreas; iWork++)
  1450. CopyRect(&plv->prcWorkAreas[iWork], &prc[iWork]);
  1451. }
  1452. // We don't support workareas for owner-data because our icon placement
  1453. // algorithm (ListView_IGetRectsOwnerData) completely ignores workareas
  1454. // and just dumps the icons in a rectangular array starting at (0,0).
  1455. if (!ListView_IsOwnerData(plv) &&
  1456. plv->nWorkAreas > 0 &&
  1457. ((plv->nWorkAreas != nOldWorkAreas) ||
  1458. (!EqualRects(&plv->prcWorkAreas[0], &rcOldWorkAreas[0], nOldWorkAreas))))
  1459. {
  1460. int iItem;
  1461. LISTITEM FAR * pitem;
  1462. //
  1463. // Subtle - ListView_Recompute cleans up all the RECOMPUTE icons,
  1464. // but in order to do that, it needs to have valid work area
  1465. // rectangles. So the call must happen after the CopyRect but before
  1466. // the loop that checks the icon positions.
  1467. //
  1468. ListView_Recompute(plv);
  1469. for (iItem = 0; iItem < ListView_Count(plv); iItem++)
  1470. {
  1471. pitem = ListView_FastGetItemPtr(plv, iItem);
  1472. if (pitem->pt.x == RECOMPUTE || pitem->pt.y == RECOMPUTE)
  1473. {
  1474. // ListView_Recompute should've fixed these if we were in
  1475. // an iconical view.
  1476. ASSERT(!(ListView_IsIconView(plv) || ListView_IsSmallView(plv)));
  1477. continue;
  1478. }
  1479. // Try to move me to the same location relative to the same workarea.
  1480. // This will give the cool shift effect when tools bars take the border areas.
  1481. // And we only want to do this for the workareas that changed
  1482. // Don't bully the icons on the workareas, Autoarrange will do the work for us
  1483. if (nOldWorkAreas > 0)
  1484. {
  1485. int iOldWorkArea;
  1486. iOldWorkArea = pitem->iWorkArea;
  1487. if (iOldWorkArea >= plv->nWorkAreas)
  1488. {
  1489. // My workarea is gone, put me on the primary workarea i.e. #0
  1490. pitem->iWorkArea = 0;
  1491. if (!bAutoArrange)
  1492. {
  1493. // If this item point location is already in the new primary workarea,
  1494. // move it out, and let ListView_BullyIconsOnWorkarea arrange it to the
  1495. // right place. NOTE: this could happen in the case the old secondary monitor
  1496. // is to the left of the old primary monitor, and user kills the secondary monitor
  1497. if (PtInRect(&plv->prcWorkAreas[0], pitem->pt))
  1498. {
  1499. pitem->pt.x = plv->prcWorkAreas[0].right + 1;
  1500. plv->iFreeSlot = -1; // an item moved -- old slot info is invalid
  1501. }
  1502. goto InsertLostItemsArray;
  1503. }
  1504. }
  1505. else if ((!bAutoArrange) && (!EqualRect(&plv->prcWorkAreas[iOldWorkArea], &rcOldWorkAreas[iOldWorkArea])))
  1506. {
  1507. RECT rcBound;
  1508. POINT ptCenter;
  1509. pitem->pt.x += plv->prcWorkAreas[iOldWorkArea].left - rcOldWorkAreas[iOldWorkArea].left;
  1510. pitem->pt.y += plv->prcWorkAreas[iOldWorkArea].top - rcOldWorkAreas[iOldWorkArea].top;
  1511. // Use the center of this icon to determine whether it's out of bound
  1512. ListView_GetRects(plv, iItem, NULL, NULL, &rcBound, NULL);
  1513. ptCenter.x = pitem->pt.x + RECTWIDTH(rcBound) / 2;
  1514. ptCenter.y = pitem->pt.y + RECTHEIGHT(rcBound) / 2;
  1515. // If this shifted me out of bounds, register to be bullied on the workarea
  1516. if (!PtInRect(&plv->prcWorkAreas[iOldWorkArea], ptCenter))
  1517. {
  1518. InsertLostItemsArray:
  1519. if (!hdpaLostItems)
  1520. {
  1521. hdpaLostItems = DPA_Create(4);
  1522. if (!hdpaLostItems)
  1523. // we ran out of memory
  1524. ASSERT(0);
  1525. }
  1526. if (hdpaLostItems)
  1527. DPA_InsertPtr(hdpaLostItems, DPA_LAST, IntToPtr(iItem));
  1528. }
  1529. }
  1530. }
  1531. else
  1532. {
  1533. // My first time in a multi-workarea system, so find out my workarea
  1534. if (!ListView_FindWorkArea(plv, pitem->pt, &(pitem->iWorkArea)) && !bAutoArrange)
  1535. goto InsertLostItemsArray;
  1536. }
  1537. if ((plv->exStyle & LVS_EX_REGIONAL) && (pitem->hrgnIcon))
  1538. {
  1539. if (pitem->hrgnIcon != (HANDLE)-1)
  1540. DeleteObject(pitem->hrgnIcon);
  1541. pitem->hrgnIcon = NULL;
  1542. }
  1543. }
  1544. if (hdpaLostItems)
  1545. {
  1546. ASSERT(!bAutoArrange);
  1547. if (DPA_GetPtrCount(hdpaLostItems) > 0)
  1548. ListView_BullyIconsOnWorkarea(plv, hdpaLostItems);
  1549. DPA_Destroy(hdpaLostItems);
  1550. }
  1551. if (plv->exStyle & LVS_EX_REGIONAL)
  1552. ListView_RecalcRegion(plv, TRUE, TRUE);
  1553. if ((plv->ci.style & LVS_AUTOARRANGE) &&
  1554. (ListView_IsSmallView(plv) || ListView_IsIconView(plv)))
  1555. ListView_OnArrange(plv, LVA_DEFAULT);
  1556. }
  1557. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  1558. }
  1559. void NEAR ListView_OnGetNumberOfWorkAreas(LV* plv, int * pnWorkAreas)
  1560. {
  1561. if (pnWorkAreas)
  1562. *pnWorkAreas = plv->nWorkAreas;
  1563. }
  1564. void NEAR ListView_OnGetWorkAreas(LV* plv, int nWorkAreas, LPRECT prc)
  1565. {
  1566. int i;
  1567. ASSERT(prc);
  1568. for (i = 0; i < min(plv->nWorkAreas, nWorkAreas); i++)
  1569. {
  1570. if (i < plv->nWorkAreas)
  1571. {
  1572. CopyRect(&prc[i], &plv->prcWorkAreas[i]);
  1573. }
  1574. else
  1575. {
  1576. // Set the workareas to all zeros if we don't have it.
  1577. ZeroMemory(&prc[i], sizeof(RECT));
  1578. }
  1579. }
  1580. }
  1581. // test an item to see if it is unfolded (because it is focused)
  1582. BOOL ListView_IsItemUnfolded(LV *plv, int item)
  1583. {
  1584. return plv && (item >= 0) && ListView_IsIconView(plv) &&
  1585. (plv->flags & LVF_UNFOLDED) && (plv->iFocus == item);
  1586. }
  1587. BOOL ListView_IsItemUnfoldedPtr(LV *plv, LISTITEM *pitem)
  1588. {
  1589. return plv && pitem && ListView_IsIconView(plv) &&
  1590. (plv->flags & LVF_UNFOLDED) && (pitem->state & LVIS_FOCUSED);
  1591. }
  1592. // Returns TRUE if unfolding the item will be worthwhile
  1593. BOOL ListView_GetUnfoldedRect(LV* plv, int iItem, RECT *prc)
  1594. {
  1595. ListView_GetRects(plv, iItem, NULL, prc, NULL, NULL);
  1596. return ListView_UnfoldRects(plv, iItem, NULL, prc, NULL, NULL);
  1597. }
  1598. LRESULT ListView_OnNotify(LV* plv, WPARAM wParam, LPNMHDR pnmh)
  1599. {
  1600. // we can't switch on the control ID because the tooltip is a WS_POPUP window
  1601. // and does not have a control ID. (header and tooltip both have 0 as ID)
  1602. if (plv->hwndHdr && (plv->hwndHdr == pnmh->hwndFrom))
  1603. {
  1604. // this is a notify for the header, deal with it as needed
  1605. return ListView_HeaderNotify(plv, (HD_NOTIFY *)pnmh);
  1606. }
  1607. else if (plv->hwndToolTips && (plv->hwndToolTips == pnmh->hwndFrom))
  1608. {
  1609. // implement unfolding the text for items as well as info tip support
  1610. switch (pnmh->code)
  1611. {
  1612. case TTN_NEEDTEXT:
  1613. {
  1614. POINT pt;
  1615. UINT uFlags;
  1616. int iNewHit;
  1617. int iNewSubHit;
  1618. NMTTDISPINFO *pttt = (NMTTDISPINFO *)pnmh;
  1619. GetMessagePosClient(plv->ci.hwnd, &pt);
  1620. iNewHit = _ListView_ItemHitTest(plv, pt.x, pt.y, &uFlags, &iNewSubHit);
  1621. if (iNewHit != plv->iTTLastHit || iNewSubHit != plv->iTTLastSubHit)
  1622. {
  1623. plv->fPlaceTooltip = FALSE; // Set it to TRUE only if Unfolding tip is set
  1624. Str_Set(&plv->pszTip, NULL); // clear the old tip
  1625. plv->iTTLastHit = iNewHit;
  1626. plv->iTTLastSubHit = iNewSubHit;
  1627. if ((iNewHit >= 0) && (plv->iEdit == -1))
  1628. {
  1629. TCHAR szBuf[INFOTIPSIZE], szBuf2[INFOTIPSIZE];
  1630. BOOL bItemUnfolded;
  1631. BOOL fInfoTip = FALSE;
  1632. LPTSTR pszTip = szBuf; // Use this one first
  1633. szBuf[0] = 0;
  1634. szBuf2[0] = 0;
  1635. // preload the tip text for folder items. this
  1636. // may be overridden by callback below
  1637. bItemUnfolded = ListView_IsItemUnfolded2(plv, plv->iTTLastHit, plv->iTTLastSubHit, szBuf, ARRAYSIZE(szBuf));
  1638. // Backup the unfolding text
  1639. StringCchCopy(szBuf2, ARRAYSIZE(szBuf2), szBuf);
  1640. if (ListView_IsInfoTip(plv) && iNewSubHit == 0)
  1641. {
  1642. NMLVGETINFOTIP git;
  1643. git.dwFlags = bItemUnfolded ? LVGIT_UNFOLDED : 0;
  1644. git.pszText = szBuf;
  1645. git.cchTextMax = ARRAYSIZE(szBuf);
  1646. git.iItem = plv->iTTLastHit;
  1647. git.iSubItem = 0;
  1648. git.lParam = 0;
  1649. // for folded items pszText is prepopulated with the
  1650. // item text, clients should append to this
  1651. CCSendNotify(&plv->ci, LVN_GETINFOTIP, &git.hdr);
  1652. // Sometimes defview gets confused and nulls out the
  1653. // buffer instead of leaving it alone (sigh)
  1654. if (szBuf[0] == TEXT('\0'))
  1655. {
  1656. pszTip = szBuf2; // Use the original text
  1657. }
  1658. else if (lstrcmp(szBuf, szBuf2) != 0)
  1659. {
  1660. // App changed something - there is a real infotip
  1661. fInfoTip = TRUE;
  1662. }
  1663. }
  1664. //
  1665. // Set the margins now before the TTN_SHOW because it will be too late then.
  1666. //
  1667. // We want fat margins if we're an infotip, thin margins if we're an
  1668. // in-place tooltip.
  1669. //
  1670. if (fInfoTip)
  1671. {
  1672. static const RECT rcMargin = {4, 4, 4, 4};
  1673. SendMessage(plv->hwndToolTips, TTM_SETMARGIN, 0, (LPARAM)&rcMargin);
  1674. CCSetInfoTipWidth(plv->ci.hwnd, plv->hwndToolTips);
  1675. }
  1676. else
  1677. {
  1678. static const RECT rcMargin = {0, 0, 0, 0};
  1679. plv->fPlaceTooltip = TRUE;
  1680. SendMessage(plv->hwndToolTips, TTM_SETMARGIN, 0, (LPARAM)&rcMargin);
  1681. CCResetInfoTipWidth(plv->ci.hwnd, plv->hwndToolTips);
  1682. }
  1683. Str_Set(&plv->pszTip, pszTip);
  1684. }
  1685. }
  1686. pttt->lpszText = plv->pszTip; // here it is...
  1687. }
  1688. break;
  1689. // Handle custom draw as we want the tooltip painted as a multi-line that
  1690. // matches the formatting used by the list view.
  1691. case NM_CUSTOMDRAW:
  1692. {
  1693. LPNMTTCUSTOMDRAW pnm = (LPNMTTCUSTOMDRAW) pnmh;
  1694. if (plv->fPlaceTooltip &&
  1695. (pnm->nmcd.dwDrawStage == CDDS_PREPAINT ||
  1696. pnm->nmcd.dwDrawStage == CDDS_ITEMPREPAINT))
  1697. {
  1698. DWORD dwCustom = 0;
  1699. //
  1700. // Set up the customdraw DC to match the font of the LV item.
  1701. //
  1702. if (plv->iTTLastHit != -1)
  1703. {
  1704. LVFAKEDRAW lvfd;
  1705. LV_ITEM item;
  1706. ListView_BeginFakeCustomDraw(plv, &lvfd, &item);
  1707. item.iItem = plv->iTTLastHit;
  1708. item.iSubItem = plv->iTTLastSubHit;
  1709. item.mask = LVIF_PARAM;
  1710. ListView_OnGetItem(plv, &item);
  1711. dwCustom = ListView_BeginFakeItemDraw(&lvfd);
  1712. // If client changed the font, then transfer the font
  1713. // from our private hdc into the tooltip's HDC. We use
  1714. // a private HDC because we only want to let the app change
  1715. // the font, not the colors or anything else.
  1716. if (dwCustom & CDRF_NEWFONT)
  1717. {
  1718. SelectObject(pnm->nmcd.hdc, GetCurrentObject(lvfd.nmcd.nmcd.hdc, OBJ_FONT));
  1719. }
  1720. ListView_EndFakeItemDraw(&lvfd);
  1721. ListView_EndFakeCustomDraw(&lvfd);
  1722. }
  1723. //
  1724. // The Large Icon tooltip needs to be drawn specially.
  1725. //
  1726. if (ListView_IsIconView(plv))
  1727. {
  1728. pnm->uDrawFlags &= ~(DT_SINGLELINE|DT_LEFT);
  1729. pnm->uDrawFlags |= DT_CENTER|DT_LVWRAP;
  1730. if ( pnm->uDrawFlags & DT_CALCRECT )
  1731. {
  1732. pnm->nmcd.rc.right = pnm->nmcd.rc.left + (lv_cxIconSpacing - g_cxLabelMargin * 2);
  1733. pnm->nmcd.rc.bottom = pnm->nmcd.rc.top + 0x10000; // big number, no limit!
  1734. }
  1735. }
  1736. // Don't return other wacky flags to TT, since all we
  1737. // did was change the font (if even that)
  1738. return dwCustom & CDRF_NEWFONT;
  1739. }
  1740. }
  1741. break;
  1742. case TTN_SHOW:
  1743. if (plv->iTTLastHit != -1)
  1744. {
  1745. if (plv->fPlaceTooltip)
  1746. {
  1747. LPNMTTSHOWINFO psi = (LPNMTTSHOWINFO)pnmh;
  1748. RECT rcLabel;
  1749. // In case we're doing subitem hit-testing
  1750. rcLabel.top = plv->iTTLastSubHit;
  1751. rcLabel.left = LVIR_LABEL;
  1752. // reposition to allign with the text rect and
  1753. // set it to topmost
  1754. if (plv->iTTLastSubHit && ListView_OnGetSubItemRect(plv, plv->iTTLastHit, &rcLabel)) {
  1755. LV_ITEM item;
  1756. // we got the subitem rect. When we draw subitems, we give
  1757. // them SHDT_EXTRAMARGIN, so we have to also
  1758. rcLabel.left += g_cxLabelMargin * 3;
  1759. rcLabel.right -= g_cxLabelMargin * 3;
  1760. // And take the image into account, too.
  1761. // ListView_OnGetItem will worry about LVS_EX_SUBITEMIMAGES.
  1762. item.mask = LVIF_IMAGE;
  1763. item.iImage = -1;
  1764. item.iItem = plv->iTTLastHit;
  1765. item.iSubItem = plv->iTTLastSubHit;
  1766. ListView_OnGetItem(plv, &item);
  1767. if (item.iImage != -1)
  1768. rcLabel.left += plv->cxSmIcon;
  1769. } else { // a tip from subitem zero
  1770. ListView_GetUnfoldedRect(plv, plv->iTTLastHit, &rcLabel);
  1771. // SHDrawText actually leaves a g_cxLabelMargin margin
  1772. rcLabel.left += g_cxLabelMargin;
  1773. rcLabel.right -= g_cxLabelMargin;
  1774. }
  1775. // In report and list views, SHDrawText does vertical
  1776. // centering (without consulting the custom-draw client,
  1777. // even, so it just centers by a random amount).
  1778. if (ListView_IsListView(plv) || ListView_IsReportView(plv))
  1779. {
  1780. rcLabel.top += (rcLabel.bottom - rcLabel.top - plv->cyLabelChar) / 2;
  1781. }
  1782. SendMessage(plv->hwndToolTips, TTM_ADJUSTRECT, TRUE, (LPARAM)&rcLabel);
  1783. MapWindowRect(plv->ci.hwnd, HWND_DESKTOP, &rcLabel);
  1784. if (!ListView_IsIconView(plv))
  1785. {
  1786. // In non-large-icon view, the label size may be greater than the rect returned by ListView_GetUnfoldedRect.
  1787. // So don't specify the size
  1788. SetWindowPos(plv->hwndToolTips, HWND_TOP,
  1789. rcLabel.left, rcLabel.top,
  1790. 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_HIDEWINDOW);
  1791. }
  1792. else
  1793. {
  1794. SetWindowPos(plv->hwndToolTips, HWND_TOP,
  1795. rcLabel.left, rcLabel.top,
  1796. (rcLabel.right - rcLabel.left), (rcLabel.bottom - rcLabel.top),
  1797. SWP_NOACTIVATE | SWP_HIDEWINDOW);
  1798. }
  1799. // This is an inplace tooltip, so disable animation.
  1800. psi->dwStyle |= TTS_NOANIMATE;
  1801. return TRUE;
  1802. }
  1803. }
  1804. break;
  1805. }
  1806. }
  1807. return 0;
  1808. }
  1809. // Pass the focus to the given window, and then check to see if it exists.
  1810. // Passing focus can cause the window to be destroyed (by the Explorer
  1811. // when renaming).
  1812. BOOL NEAR ListView_SetFocus(HWND hwnd)
  1813. {
  1814. SetFocus(hwnd);
  1815. return IsWindow(hwnd);
  1816. }
  1817. void NEAR ListView_Realize(LV* plv, HDC hdcParam, BOOL fBackground, BOOL fForceRepaint)
  1818. {
  1819. if (plv->hpalHalftone)
  1820. {
  1821. HDC hdc = hdcParam ? hdcParam : GetDC(plv->ci.hwnd);
  1822. if (hdc)
  1823. {
  1824. BOOL fRepaint;
  1825. SelectPalette(hdc, plv->hpalHalftone, fBackground);
  1826. fRepaint = RealizePalette(hdc) || fForceRepaint;
  1827. if (!hdcParam)
  1828. ReleaseDC(plv->ci.hwnd, hdc);
  1829. if (fRepaint)
  1830. {
  1831. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  1832. }
  1833. }
  1834. }
  1835. }
  1836. BOOL RectInRect(const RECT* prcOuter, const RECT* prcInner)
  1837. {
  1838. #ifdef COMPLETELYINRECT
  1839. return (prcOuter->left <= prcInner->left ) &&
  1840. (prcOuter->top <= prcInner->top ) &&
  1841. (prcOuter->right >= prcInner->right ) &&
  1842. (prcOuter->bottom >= prcInner->bottom);
  1843. #else
  1844. RECT rcDummy;
  1845. return IntersectRect(&rcDummy, prcOuter, prcInner);
  1846. #endif
  1847. }
  1848. LRESULT LVGenerateDragImage(LV* plv, SHDRAGIMAGE* pshdi)
  1849. {
  1850. LRESULT lRet = 0;
  1851. int iNumSelected = plv->nSelected;
  1852. int iIndex;
  1853. int iSelectedItem;
  1854. RECT rc = {0, 0, 0, 0};
  1855. RECT rcVisRect;
  1856. HBITMAP hbmpOld = NULL;
  1857. HDC hdcDragImage;
  1858. // First loop through can get the selection rect
  1859. if (ListView_IsOwnerData( plv ))
  1860. {
  1861. plv->plvrangeSel->lpVtbl->CountIncluded(plv->plvrangeSel, &iNumSelected);
  1862. }
  1863. if (iNumSelected == 0)
  1864. return FALSE;
  1865. GetClientRect(plv->ci.hwnd, &rcVisRect);
  1866. // Loop Through and calculate the enclosing rect.
  1867. for (iIndex = iNumSelected - 1, iSelectedItem = -1; iIndex >= 0; iIndex--)
  1868. {
  1869. iSelectedItem = ListView_OnGetNextItem(plv, iSelectedItem, LVNI_SELECTED);
  1870. if (iSelectedItem != -1)
  1871. {
  1872. RECT rcItemBounds;
  1873. // Make sure this is in the visible region
  1874. if (ListView_GetItemRect(plv->ci.hwnd, iSelectedItem, &rcItemBounds, LVIR_SELECTBOUNDS) &&
  1875. RectInRect(&rcVisRect, &rcItemBounds))
  1876. {
  1877. UnionRect(&rc, &rc, &rcItemBounds);
  1878. }
  1879. }
  1880. }
  1881. hdcDragImage = CreateCompatibleDC(NULL);
  1882. if (!hdcDragImage)
  1883. return 0;
  1884. // After this rc contains the bounds of all the items in Client Coordinates.
  1885. //
  1886. // Mirror the the DC, if the listview is mirrored.
  1887. //
  1888. if (plv->ci.dwExStyle & RTL_MIRRORED_WINDOW)
  1889. {
  1890. SET_DC_RTL_MIRRORED(hdcDragImage);
  1891. }
  1892. #define MAX_DRAG_RECT_WIDTH 300
  1893. #define MAX_DRAG_RECT_HEIGHT 300
  1894. // If this rect is too big, fix it.
  1895. if (RECTWIDTH(rc) > MAX_DRAG_RECT_WIDTH)
  1896. {
  1897. int iLeft = MAX_DRAG_RECT_WIDTH / 2;
  1898. int iRight = MAX_DRAG_RECT_WIDTH /2;
  1899. int iRectOriginalLeft = rc.left;
  1900. // Is the left boundry outside the visible rect?
  1901. if (rc.left < plv->ptCapture.x - iLeft)
  1902. {
  1903. // Yes, then we have to clip it.
  1904. rc.left = plv->ptCapture.x - iLeft;
  1905. }
  1906. else
  1907. {
  1908. // No? Well then shift the visible rect to the right, so that we have
  1909. // more room.
  1910. iRight += rc.left - (plv->ptCapture.x - iLeft);
  1911. }
  1912. // Is the right boundry outside the visible rect?
  1913. if (rc.right > plv->ptCapture.x + iRight)
  1914. {
  1915. // Yes, then we have to clip it.
  1916. rc.right = plv->ptCapture.x + iRight;
  1917. }
  1918. else
  1919. {
  1920. // No? Then try and add it to the left
  1921. if (rc.left > iRectOriginalLeft)
  1922. {
  1923. rc.left -= iRight - (rc.right - plv->ptCapture.x);
  1924. if (rc.left < iRectOriginalLeft)
  1925. rc.left = iRectOriginalLeft;
  1926. }
  1927. }
  1928. }
  1929. if (RECTHEIGHT(rc) > MAX_DRAG_RECT_HEIGHT)
  1930. {
  1931. // same for top and bottom:
  1932. // Is the top boundry outside the visible rect?
  1933. int iTop = MAX_DRAG_RECT_HEIGHT / 2;
  1934. int iBottom = MAX_DRAG_RECT_HEIGHT /2;
  1935. int iRectOriginalTop = rc.top;
  1936. if (rc.top < plv->ptCapture.y - iTop)
  1937. {
  1938. // Yes, then we have to clip it.
  1939. rc.top = plv->ptCapture.y - iTop;
  1940. }
  1941. else
  1942. {
  1943. // No? Well then shift the visible rect to the right, so that we have
  1944. // more room.
  1945. iBottom += rc.top - (plv->ptCapture.y - iTop);
  1946. }
  1947. // Is the right boundry outside the visible rect?
  1948. if (rc.bottom > plv->ptCapture.y + iBottom)
  1949. {
  1950. // Yes, then we have to clip it.
  1951. rc.bottom = plv->ptCapture.y + iBottom;
  1952. }
  1953. else
  1954. {
  1955. // No? Then try and add it to the top
  1956. if (rc.top > iRectOriginalTop)
  1957. {
  1958. rc.top -= iBottom - (rc.bottom - plv->ptCapture.y);
  1959. if (rc.top < iRectOriginalTop)
  1960. rc.top = iRectOriginalTop;
  1961. }
  1962. }
  1963. }
  1964. pshdi->sizeDragImage.cx = RECTWIDTH(rc);
  1965. pshdi->sizeDragImage.cy = RECTHEIGHT(rc);
  1966. pshdi->hbmpDragImage = CreateBitmap( pshdi->sizeDragImage.cx, pshdi->sizeDragImage.cy,
  1967. GetDeviceCaps(hdcDragImage, PLANES), GetDeviceCaps(hdcDragImage, BITSPIXEL),
  1968. NULL);
  1969. if (pshdi->hbmpDragImage)
  1970. {
  1971. LVDRAWITEM lvdi;
  1972. DWORD dwType;
  1973. int cItem;
  1974. RECT rcImage = {0, 0, pshdi->sizeDragImage.cx, pshdi->sizeDragImage.cy};
  1975. hbmpOld = SelectObject(hdcDragImage, pshdi->hbmpDragImage);
  1976. pshdi->crColorKey = RGB(0xFF, 0x00, 0x55);
  1977. FillRectClr(hdcDragImage, &rcImage, pshdi->crColorKey);
  1978. pshdi->crColorKey = GetPixel(hdcDragImage, 0, 0);
  1979. // Calculate the offset... The cursor should be in the bitmap rect.
  1980. if (plv->ci.dwExStyle & RTL_MIRRORED_WINDOW)
  1981. pshdi->ptOffset.x = rc.right - plv->ptCapture.x;
  1982. else
  1983. pshdi->ptOffset.x = plv->ptCapture.x - rc.left;
  1984. pshdi->ptOffset.y = plv->ptCapture.y - rc.top;
  1985. lvdi.prcClip = NULL;
  1986. lvdi.plv = plv;
  1987. lvdi.nmcd.nmcd.hdc = hdcDragImage;
  1988. lvdi.pitem = NULL;
  1989. dwType = plv->ci.style & LVS_TYPEMASK;
  1990. cItem = ListView_Count(plv);
  1991. // Now loop through again for the paint cycle
  1992. for (iIndex = cItem - 1, iSelectedItem = -1; iIndex >= 0; iIndex--)
  1993. {
  1994. if (ListView_IsOwnerData( plv ))
  1995. {
  1996. iSelectedItem++;
  1997. plv->plvrangeSel->lpVtbl->NextSelected(plv->plvrangeSel, iSelectedItem, &iSelectedItem);
  1998. }
  1999. else
  2000. {
  2001. LISTITEM FAR* pitem;
  2002. iSelectedItem = (int)(UINT_PTR)DPA_FastGetPtr(plv->hdpaZOrder, iIndex);
  2003. pitem = ListView_FastGetItemPtr(plv, iSelectedItem);
  2004. if (!(pitem->state & LVIS_SELECTED))
  2005. iSelectedItem = -1;
  2006. }
  2007. if (iSelectedItem != -1)
  2008. {
  2009. int iOldItemDrawing;
  2010. COLORREF crSave;
  2011. POINT ptOrigin = {-rc.left, -rc.top}; //Offset the rects by...
  2012. RECT rcItemBounds;
  2013. RECT rcTemp;
  2014. iOldItemDrawing = plv->iItemDrawing;
  2015. plv->iItemDrawing = iSelectedItem;
  2016. lvdi.nmcd.nmcd.dwItemSpec = iSelectedItem;
  2017. ListView_GetRects(plv, iSelectedItem, NULL, NULL, &rcItemBounds, NULL);
  2018. // Make sure this is in the visible region
  2019. if (IntersectRect(&rcTemp, &rcVisRect, &rcItemBounds))
  2020. {
  2021. ptOrigin.x += rcItemBounds.left;
  2022. ptOrigin.y += rcItemBounds.top;
  2023. // these may get changed
  2024. lvdi.lpptOrg = &ptOrigin;
  2025. lvdi.flags = 0;
  2026. lvdi.nmcd.clrText = plv->clrText;
  2027. lvdi.nmcd.clrTextBk = plv->clrTextBk;
  2028. // Save the Background color!
  2029. crSave = plv->clrBk;
  2030. plv->clrBk = pshdi->crColorKey;
  2031. ListView_DrawItem(&lvdi);
  2032. plv->clrBk = crSave;
  2033. }
  2034. plv->iItemDrawing = iOldItemDrawing;
  2035. }
  2036. }
  2037. SelectObject(hdcDragImage, hbmpOld);
  2038. DeleteDC(hdcDragImage);
  2039. // We're passing back the created HBMP.
  2040. return 1;
  2041. }
  2042. return lRet;
  2043. }
  2044. LRESULT CALLBACK ListView_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  2045. {
  2046. LV* plv = ListView_GetPtr(hwnd);
  2047. if (plv == NULL)
  2048. {
  2049. if (uMsg == WM_NCCREATE)
  2050. {
  2051. plv = (LV*)NearAlloc(sizeof(LV));
  2052. if (!plv)
  2053. {
  2054. DebugMsg(DM_ERROR, TEXT("ListView: Out of near memory"));
  2055. return 0L; // fail the window create
  2056. }
  2057. plv->ci.hwnd = hwnd;
  2058. plv->flags = LVF_REDRAW; // assume that redrawing enabled!
  2059. plv->iFocus = -1; // no focus
  2060. plv->iMark = -1;
  2061. plv->iSelCol = -1;
  2062. plv->iDropHilite = -1; // Assume no item has drop hilite...
  2063. plv->cyItem = plv->cyItemSave = 1; // never let these be zero, not even for a moment
  2064. plv->hheap = GetProcessHeap();
  2065. ListView_SetPtr(hwnd, plv);
  2066. }
  2067. goto DoDefault;
  2068. }
  2069. if ((uMsg >= WM_MOUSEFIRST) && (uMsg <= WM_MOUSELAST)) {
  2070. if (plv->exStyle & (LVS_EX_TRACKSELECT|LVS_EX_ONECLICKACTIVATE|LVS_EX_TWOCLICKACTIVATE)) {
  2071. TRACKMOUSEEVENT tme;
  2072. tme.cbSize = sizeof(tme);
  2073. tme.hwndTrack = plv->ci.hwnd;
  2074. tme.dwHoverTime = plv->dwHoverTime;
  2075. tme.dwFlags = TME_LEAVE | TME_HOVER | TME_QUERY;
  2076. // see what's set
  2077. TrackMouseEvent(&tme);
  2078. tme.dwFlags &= TME_HOVER | TME_LEAVE;
  2079. // set these bits if they aren't already set
  2080. tme.dwFlags ^= TME_LEAVE;
  2081. if (plv->exStyle & LVS_EX_TRACKSELECT) {
  2082. tme.dwFlags ^= TME_HOVER;
  2083. }
  2084. tme.cbSize = sizeof(tme);
  2085. tme.hwndTrack = plv->ci.hwnd;
  2086. tme.dwHoverTime = plv->dwHoverTime;
  2087. // set it if there's anything to set
  2088. if (tme.dwFlags & (TME_HOVER | TME_LEAVE)) {
  2089. TrackMouseEvent(&tme);
  2090. }
  2091. }
  2092. }
  2093. if (uMsg == g_uDragImages)
  2094. {
  2095. return LVGenerateDragImage(plv, (SHDRAGIMAGE*)lParam);
  2096. }
  2097. switch (uMsg)
  2098. {
  2099. HANDLE_MSG(plv, WM_CREATE, ListView_OnCreate);
  2100. HANDLE_MSG(plv, WM_DESTROY, ListView_OnDestroy);
  2101. HANDLE_MSG(plv, WM_ERASEBKGND, ListView_OnEraseBkgnd);
  2102. HANDLE_MSG(plv, WM_COMMAND, ListView_OnCommand);
  2103. HANDLE_MSG(plv, WM_SETFOCUS, ListView_OnSetFocus);
  2104. HANDLE_MSG(plv, WM_KILLFOCUS, ListView_OnKillFocus);
  2105. HANDLE_MSG(plv, WM_HSCROLL, ListView_OnHScroll);
  2106. HANDLE_MSG(plv, WM_VSCROLL, ListView_OnVScroll);
  2107. HANDLE_MSG(plv, WM_GETDLGCODE, ListView_OnGetDlgCode);
  2108. HANDLE_MSG(plv, WM_SETFONT, ListView_OnSetFont);
  2109. HANDLE_MSG(plv, WM_GETFONT, ListView_OnGetFont);
  2110. HANDLE_MSG(plv, WM_TIMER, ListView_OnTimer);
  2111. HANDLE_MSG(plv, WM_SETREDRAW, ListView_OnSetRedraw);
  2112. HANDLE_MSG(plv, WM_NCDESTROY, ListView_OnNCDestroy);
  2113. case WM_SETCURSOR:
  2114. if (ListView_OnSetCursorMsg(plv))
  2115. return TRUE;
  2116. break;
  2117. case WM_PALETTECHANGED:
  2118. if ((HWND)wParam == hwnd)
  2119. break;
  2120. case WM_QUERYNEWPALETTE:
  2121. // Want to pass FALSE if WM_QUERYNEWPALETTE...
  2122. ListView_Realize(plv, NULL, uMsg == WM_PALETTECHANGED, uMsg == WM_PALETTECHANGED);
  2123. return TRUE;
  2124. case LVMP_WINDOWPOSCHANGED:
  2125. case WM_WINDOWPOSCHANGED:
  2126. HANDLE_WM_WINDOWPOSCHANGED(plv, wParam, lParam, ListView_OnWindowPosChanged);
  2127. break;
  2128. case WM_MBUTTONDOWN:
  2129. if (ListView_SetFocus(hwnd) && plv->hwndToolTips)
  2130. RelayToToolTips(plv->hwndToolTips, hwnd, uMsg, wParam, lParam);
  2131. break;
  2132. case WM_LBUTTONDBLCLK:
  2133. case WM_RBUTTONDBLCLK:
  2134. if (plv->hwndToolTips)
  2135. RelayToToolTips(plv->hwndToolTips, hwnd, uMsg, wParam, lParam);
  2136. ListView_OnButtonDown(plv, TRUE, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), (UINT) wParam);
  2137. break;
  2138. case WM_LBUTTONDOWN:
  2139. case WM_RBUTTONDOWN:
  2140. if (plv->hwndToolTips)
  2141. RelayToToolTips(plv->hwndToolTips, hwnd, uMsg, wParam, lParam);
  2142. ListView_OnButtonDown(plv, FALSE, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), (UINT) wParam);
  2143. break;
  2144. case WM_LBUTTONUP:
  2145. case WM_RBUTTONUP:
  2146. case WM_MBUTTONUP:
  2147. case WM_NCMOUSEMOVE:
  2148. if (plv->hwndToolTips)
  2149. RelayToToolTips(plv->hwndToolTips, hwnd, uMsg, wParam, lParam);
  2150. break;
  2151. case WM_PRINTCLIENT:
  2152. case WM_PAINT:
  2153. ListView_OnPaint(plv, (HDC)wParam);
  2154. return(0);
  2155. case WM_SHOWWINDOW:
  2156. LV_OnShowWindow(plv, BOOLFROMPTR(wParam));
  2157. break;
  2158. case WM_MOUSEHOVER:
  2159. ListView_OnMouseHover(plv, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), (UINT) wParam);
  2160. break;
  2161. case WM_MOUSELEAVE:
  2162. ListView_OnSetHotItem(plv, -1);
  2163. plv->iNoHover = -1;
  2164. break;
  2165. case WM_KEYUP:
  2166. plv->iScrollCount = 0;
  2167. break;
  2168. case WM_KEYDOWN:
  2169. HANDLE_WM_KEYDOWN(plv, wParam, lParam, ListView_OnKey);
  2170. break;
  2171. #if defined(FE_IME)
  2172. case WM_IME_COMPOSITION:
  2173. // Now only Korean version is interested in incremental search with composition string.
  2174. if (g_fDBCSInputEnabled) {
  2175. if (((ULONG_PTR)GetKeyboardLayout(0L) & 0xF000FFFFL) == 0xE0000412L)
  2176. {
  2177. if (ListView_OnImeComposition(plv, wParam, lParam))
  2178. {
  2179. lParam &= ~GCS_RESULTSTR;
  2180. break;
  2181. }
  2182. else
  2183. return 0;
  2184. }
  2185. }
  2186. break;
  2187. #endif
  2188. case WM_CHAR:
  2189. if (plv->iPuntChar) {
  2190. plv->iPuntChar--;
  2191. return TRUE;
  2192. } else {
  2193. return HANDLE_WM_CHAR(plv, wParam, lParam, ListView_OnChar);
  2194. }
  2195. case WM_WININICHANGE:
  2196. ListView_OnWinIniChange(plv, wParam, lParam);
  2197. break;
  2198. case WM_NOTIFYFORMAT:
  2199. return CIHandleNotifyFormat(&plv->ci, lParam);
  2200. case WM_ENABLE:
  2201. // HACK: we don't get WM_STYLECHANGE on EnableWindow()
  2202. ListView_EnableWindow(plv, BOOLFROMPTR(wParam));
  2203. break;
  2204. case WM_SYSCOLORCHANGE:
  2205. InitGlobalColors();
  2206. if (plv->ci.style & WS_DISABLED)
  2207. {
  2208. if (!(plv->flags & LVF_USERBKCLR))
  2209. plv->clrBkSave = g_clrWindow;
  2210. ListView_OnSetBkColor(plv, g_clrBtnFace);
  2211. }
  2212. else if (!(plv->flags & LVF_USERBKCLR))
  2213. {
  2214. ListView_OnSetBkColor(plv, g_clrWindow);
  2215. }
  2216. if (plv->exStyle & LVS_EX_CHECKBOXES)
  2217. {
  2218. ListView_InitCheckBoxes(plv, FALSE);
  2219. }
  2220. // 98/11/19 #249967 vtan: Always invalidate the list view
  2221. // rectangle so that the color change causes a refresh.
  2222. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  2223. break;
  2224. // don't use HANDLE_MSG because this needs to go to the default handler
  2225. case WM_SYSKEYDOWN:
  2226. HANDLE_WM_SYSKEYDOWN(plv, wParam, lParam, ListView_OnKey);
  2227. break;
  2228. case WM_UPDATEUISTATE:
  2229. {
  2230. DWORD dwUIStateMask = MAKEWPARAM(0xFFFF, UISF_HIDEFOCUS);
  2231. // we care only about focus not accel, and redraw only if changed
  2232. if (CCOnUIState(&(plv->ci), WM_UPDATEUISTATE, wParam & dwUIStateMask, lParam))
  2233. {
  2234. if(plv->iFocus >= 0)
  2235. {
  2236. // an item has the focus, invalidate it
  2237. ListView_InvalidateItem(plv, plv->iFocus, FALSE, RDW_INVALIDATE | RDW_ERASE);
  2238. }
  2239. }
  2240. goto DoDefault;
  2241. }
  2242. case LVM_GETITEMA:
  2243. return (LRESULT)ListView_OnGetItemA(plv, (LV_ITEMA *)lParam);
  2244. case LVM_SETITEMA:
  2245. return (LRESULT)ListView_OnSetItemA(plv, (LV_ITEMA *)lParam);
  2246. case LVM_INSERTITEMA:
  2247. return (LRESULT)ListView_OnInsertItemA(plv, (LV_ITEMA *)lParam);
  2248. case LVM_FINDITEMA:
  2249. return (LRESULT)ListView_OnFindItemA(plv, (int)wParam, (LV_FINDINFOA *)lParam);
  2250. case LVM_GETSTRINGWIDTHA:
  2251. return (LRESULT)ListView_OnGetStringWidthA(plv, (LPCSTR)lParam, NULL);
  2252. case LVM_GETCOLUMNA:
  2253. return (LRESULT)ListView_OnGetColumnA(plv, (int)wParam, (LV_COLUMNA *)lParam);
  2254. case LVM_SETCOLUMNA:
  2255. return (LRESULT)ListView_OnSetColumnA(plv, (int)wParam, (LV_COLUMNA *)lParam);
  2256. case LVM_INSERTCOLUMNA:
  2257. return (LRESULT)ListView_OnInsertColumnA(plv, (int)wParam, (LV_COLUMNA *)lParam);
  2258. case LVM_GETITEMTEXTA:
  2259. return (LRESULT)ListView_OnGetItemTextA(plv, (int)wParam, (LV_ITEMA FAR *)lParam);
  2260. case LVM_SETITEMTEXTA:
  2261. if (!lParam)
  2262. return FALSE;
  2263. return (LRESULT)ListView_OnSetItemTextA(plv, (int)wParam,
  2264. ((LV_ITEMA *)lParam)->iSubItem,
  2265. (LPCSTR)((LV_ITEMA FAR *)lParam)->pszText);
  2266. case LVM_GETBKIMAGEA:
  2267. return (LRESULT)ListView_OnGetBkImageA(plv, (LPLVBKIMAGEA)lParam);
  2268. case LVM_SETBKIMAGEA:
  2269. return (LRESULT)ListView_OnSetBkImageA(plv, (LPLVBKIMAGEA)lParam);
  2270. case WM_STYLECHANGING:
  2271. ListView_OnStyleChanging(plv, (UINT)wParam, (LPSTYLESTRUCT)lParam);
  2272. return 0;
  2273. case WM_STYLECHANGED:
  2274. ListView_OnStyleChanged(plv, (UINT) wParam, (LPSTYLESTRUCT)lParam);
  2275. return 0L;
  2276. case WM_HELP:
  2277. return ListView_OnHelp(plv, (LPHELPINFO)lParam);
  2278. case LVM_GETIMAGELIST:
  2279. return (LRESULT)(UINT_PTR)(ListView_OnGetImageList(plv, (int)wParam));
  2280. case LVM_SETIMAGELIST:
  2281. return (LRESULT)(UINT_PTR)ListView_OnSetImageList(plv, (HIMAGELIST)lParam, (int)wParam);
  2282. case LVM_GETBKCOLOR:
  2283. return (LRESULT)(plv->ci.style & WS_DISABLED ? plv->clrBkSave : plv->clrBk);
  2284. case LVM_SETBKCOLOR:
  2285. plv->flags |= LVF_USERBKCLR;
  2286. if (plv->ci.style & WS_DISABLED) {
  2287. plv->clrBkSave = (COLORREF)lParam;
  2288. return TRUE;
  2289. } else {
  2290. return (LRESULT)ListView_OnSetBkColor(plv, (COLORREF)lParam);
  2291. }
  2292. case LVM_GETTEXTCOLOR:
  2293. return (LRESULT)plv->clrText;
  2294. case LVM_SETTEXTCOLOR:
  2295. plv->clrText = (COLORREF)lParam;
  2296. return TRUE;
  2297. case LVM_GETTEXTBKCOLOR:
  2298. return (LRESULT)plv->clrTextBk;
  2299. case LVM_SETTEXTBKCOLOR:
  2300. plv->clrTextBk = (COLORREF)lParam;
  2301. return TRUE;
  2302. case LVM_GETHOTLIGHTCOLOR:
  2303. return (LRESULT)plv->clrHotlight;
  2304. case LVM_SETHOTLIGHTCOLOR:
  2305. plv->clrHotlight = (COLORREF)lParam;
  2306. return(TRUE);
  2307. case LVM_GETITEMCOUNT:
  2308. if (ListView_IsOwnerData(plv))
  2309. return((LRESULT)plv->cTotalItems);
  2310. else if (!plv->hdpa)
  2311. return(0);
  2312. else
  2313. return((LRESULT)DPA_GetPtrCount(plv->hdpa));
  2314. break;
  2315. case LVM_GETITEM:
  2316. return (LRESULT)ListView_OnGetItem(plv, (LV_ITEM FAR*)lParam);
  2317. case LVM_GETITEMSTATE:
  2318. return (LRESULT)ListView_OnGetItemState(plv, (int)wParam, (UINT)lParam);
  2319. case LVM_SETITEMSTATE:
  2320. if (!lParam)
  2321. return FALSE;
  2322. return (LRESULT)ListView_OnSetItemState(plv, (int)wParam,
  2323. ((LV_ITEM FAR *)lParam)->state,
  2324. ((LV_ITEM FAR *)lParam)->stateMask);
  2325. case LVM_SETITEMTEXT:
  2326. if (!lParam)
  2327. return FALSE;
  2328. return (LRESULT)ListView_OnSetItemText(plv, (int)wParam,
  2329. ((LV_ITEM FAR *)lParam)->iSubItem,
  2330. (LPCTSTR)((LV_ITEM FAR *)lParam)->pszText);
  2331. case LVM_GETITEMTEXT:
  2332. return (LRESULT)ListView_OnGetItemText(plv, (int)wParam, (LV_ITEM FAR *)lParam);
  2333. case LVM_GETBKIMAGE:
  2334. return (LRESULT)ListView_OnGetBkImage(plv, (LPLVBKIMAGE)lParam);
  2335. case LVM_SETBKIMAGE:
  2336. return (LRESULT)ListView_OnSetBkImage(plv, (LPLVBKIMAGE)lParam);
  2337. case LVM_SETITEM:
  2338. return (LRESULT)ListView_OnSetItem(plv, (const LV_ITEM FAR*)lParam);
  2339. case LVM_INSERTITEM:
  2340. return (LRESULT)ListView_OnInsertItem(plv, (const LV_ITEM FAR*)lParam);
  2341. case LVM_DELETEITEM:
  2342. return (LRESULT)ListView_OnDeleteItem(plv, (int)wParam);
  2343. case LVM_UPDATE:
  2344. ListView_OnUpdate(plv, (int)wParam);
  2345. UpdateWindow(plv->ci.hwnd);
  2346. return TRUE;
  2347. case LVM_DELETEALLITEMS:
  2348. lParam = (LRESULT)ListView_OnDeleteAllItems(plv);
  2349. // Optimization: Instead of sending out a zillion EVENT_OBJECT_DESTROY's,
  2350. // we send out a destroy of ourselves followed by a fresh create.
  2351. // For compatibility with IE4, we still send out the REORDER notification.
  2352. MyNotifyWinEvent(EVENT_OBJECT_REORDER, hwnd, OBJID_CLIENT, 0);
  2353. ListView_NotifyRecreate(plv);
  2354. return(lParam);
  2355. case LVM_GETITEMRECT:
  2356. return (LRESULT)ListView_OnGetItemRect(plv, (int)wParam, (RECT FAR*)lParam);
  2357. case LVM_GETSUBITEMRECT:
  2358. return (LRESULT)ListView_OnGetSubItemRect(plv, (int)wParam, (LPRECT)lParam);
  2359. case LVM_SUBITEMHITTEST:
  2360. return (LRESULT)ListView_OnSubItemHitTest(plv, (LPLVHITTESTINFO)lParam);
  2361. case LVM_GETISEARCHSTRINGA:
  2362. if (GetFocus() == plv->ci.hwnd)
  2363. return (LRESULT)GetIncrementSearchStringA(&plv->is, plv->ci.uiCodePage, (LPSTR)lParam);
  2364. else
  2365. return 0;
  2366. case LVM_GETISEARCHSTRING:
  2367. if (GetFocus() == plv->ci.hwnd)
  2368. return (LRESULT)GetIncrementSearchString(&plv->is, (LPTSTR)lParam);
  2369. else
  2370. return 0;
  2371. case LVM_GETITEMSPACING:
  2372. if (wParam)
  2373. return MAKELONG(plv->cxItem, plv->cyItem);
  2374. else
  2375. return MAKELONG(lv_cxIconSpacing, lv_cyIconSpacing);
  2376. case LVM_GETNEXTITEM:
  2377. return (LRESULT)ListView_OnGetNextItem(plv, (int)wParam, (UINT)lParam);
  2378. case LVM_FINDITEM:
  2379. return (LRESULT)ListView_OnFindItem(plv, (int)wParam, (const LV_FINDINFO FAR*)lParam);
  2380. case LVM_SETSELECTIONMARK:
  2381. {
  2382. int iOldMark = plv->iMark;
  2383. int iNewMark = (int)lParam;
  2384. if (iNewMark == -1 || ListView_IsValidItemNumber(plv, iNewMark)) {
  2385. plv->iMark = iNewMark;
  2386. }
  2387. return iOldMark;
  2388. }
  2389. case LVM_GETSELECTIONMARK:
  2390. return plv->iMark;
  2391. case LVM_GETITEMPOSITION:
  2392. return (LRESULT)ListView_OnGetItemPosition(plv, (int)wParam,
  2393. (POINT FAR*)lParam);
  2394. case LVM_SETITEMPOSITION:
  2395. return (LRESULT)ListView_OnSetItemPosition(plv, (int)wParam,
  2396. GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  2397. case LVM_SETITEMPOSITION32:
  2398. if (!lParam)
  2399. return FALSE;
  2400. return (LRESULT)ListView_OnSetItemPosition(plv, (int)wParam,
  2401. ((LPPOINT)lParam)->x, ((LPPOINT)lParam)->y);
  2402. case LVM_SCROLL:
  2403. {
  2404. int dx = (int)wParam;
  2405. int dy = (int)lParam;
  2406. return (LRESULT)
  2407. (ListView_ValidateScrollParams(plv, &dx, &dy) &&
  2408. ListView_OnScroll(plv, dx, dy));
  2409. }
  2410. case LVM_ENSUREVISIBLE:
  2411. return (LRESULT)ListView_OnEnsureVisible(plv, (int)wParam, BOOLFROMPTR(lParam));
  2412. case LVM_REDRAWITEMS:
  2413. return (LRESULT)ListView_OnRedrawItems(plv, (int)wParam, (int)lParam);
  2414. case LVM_ARRANGE:
  2415. return (LRESULT)ListView_OnArrange(plv, (UINT)wParam);
  2416. case LVM_GETEDITCONTROL:
  2417. return (LRESULT)(UINT_PTR)plv->hwndEdit;
  2418. case LVM_EDITLABELA:
  2419. {
  2420. LPWSTR lpEditString = NULL;
  2421. HWND hRet;
  2422. if (lParam) {
  2423. lpEditString = ProduceWFromA(plv->ci.uiCodePage, (LPSTR)lParam);
  2424. }
  2425. hRet = ListView_OnEditLabel(plv, (int)wParam, lpEditString);
  2426. if (lpEditString) {
  2427. FreeProducedString(lpEditString);
  2428. }
  2429. return (LRESULT)hRet;
  2430. }
  2431. case LVM_EDITLABEL:
  2432. return (LRESULT)(UINT_PTR)ListView_OnEditLabel(plv, (int)wParam, (LPTSTR)lParam);
  2433. case LVM_HITTEST:
  2434. return (LRESULT)ListView_OnHitTest(plv, (LV_HITTESTINFO FAR*)lParam);
  2435. case LVM_GETSTRINGWIDTH:
  2436. return (LRESULT)ListView_OnGetStringWidth(plv, (LPCTSTR)lParam, NULL);
  2437. case LVM_GETCOLUMN:
  2438. return (LRESULT)ListView_OnGetColumn(plv, (int)wParam, (LV_COLUMN FAR*)lParam);
  2439. case LVM_SETCOLUMN:
  2440. return (LRESULT)ListView_OnSetColumn(plv, (int)wParam, (const LV_COLUMN FAR*)lParam);
  2441. case LVM_SETCOLUMNORDERARRAY:
  2442. return SendMessage(plv->hwndHdr, HDM_SETORDERARRAY, wParam, lParam);
  2443. case LVM_GETCOLUMNORDERARRAY:
  2444. return SendMessage(plv->hwndHdr, HDM_GETORDERARRAY, wParam, lParam);
  2445. case LVM_GETHEADER:
  2446. {
  2447. HWND hwndOld = plv->hwndHdr;
  2448. if (lParam && IsWindow((HWND)lParam)) {
  2449. plv->hwndHdr = (HWND)lParam;
  2450. }
  2451. return (LRESULT)hwndOld;
  2452. }
  2453. case LVM_INSERTCOLUMN:
  2454. return (LRESULT)ListView_OnInsertColumn(plv, (int)wParam, (const LV_COLUMN FAR*)lParam);
  2455. case LVM_DELETECOLUMN:
  2456. return (LRESULT)ListView_OnDeleteColumn(plv, (int)wParam);
  2457. case LVM_CREATEDRAGIMAGE:
  2458. return (LRESULT)(UINT_PTR)ListView_OnCreateDragImage(plv, (int)wParam, (LPPOINT)lParam);
  2459. case LVMI_PLACEITEMS:
  2460. if (plv->uUnplaced) {
  2461. ListView_Recompute(plv);
  2462. ListView_UpdateScrollBars(plv);
  2463. }
  2464. return 0;
  2465. case LVM_GETVIEWRECT:
  2466. if (!lParam)
  2467. return FALSE;
  2468. ListView_GetViewRect2(plv, (RECT FAR*)lParam, plv->sizeClient.cx, plv->sizeClient.cy);
  2469. return (LPARAM)TRUE;
  2470. case LVM_GETCOLUMNWIDTH:
  2471. return (LPARAM)ListView_OnGetColumnWidth(plv, (int)wParam);
  2472. case LVM_SETCOLUMNWIDTH:
  2473. return (LPARAM)ListView_ISetColumnWidth(plv, (int)wParam,
  2474. GET_X_LPARAM(lParam), TRUE);
  2475. case LVM_SETCALLBACKMASK:
  2476. plv->stateCallbackMask = (UINT)wParam;
  2477. return (LPARAM)TRUE;
  2478. case LVM_GETCALLBACKMASK:
  2479. return (LPARAM)(UINT)plv->stateCallbackMask;
  2480. case LVM_GETTOPINDEX:
  2481. return (LPARAM)ListView_OnGetTopIndex(plv);
  2482. case LVM_GETCOUNTPERPAGE:
  2483. return (LPARAM)ListView_OnGetCountPerPage(plv);
  2484. case LVM_GETORIGIN:
  2485. return (LPARAM)ListView_OnGetOrigin(plv, (POINT FAR*)lParam);
  2486. case LVM_SETITEMCOUNT:
  2487. return ListView_OnSetItemCount(plv, (int)wParam, (DWORD)lParam);
  2488. case LVM_GETSELECTEDCOUNT:
  2489. if (ListView_IsOwnerData( plv )) {
  2490. plv->plvrangeSel->lpVtbl->CountIncluded(plv->plvrangeSel, &plv->nSelected);
  2491. }
  2492. return plv->nSelected;
  2493. case LVM_SORTITEMS:
  2494. return ListView_OnSortItems(plv, (LPARAM)wParam, (PFNLVCOMPARE)lParam, TRUE);
  2495. case LVM_SORTITEMSEX:
  2496. return ListView_OnSortItems(plv, (LPARAM)wParam, (PFNLVCOMPARE)lParam, FALSE);
  2497. case LVM_SETEXTENDEDLISTVIEWSTYLE:
  2498. return ListView_ExtendedStyleChange(plv, (DWORD) lParam, (DWORD) wParam);
  2499. case LVM_GETEXTENDEDLISTVIEWSTYLE:
  2500. return plv->exStyle;
  2501. case LVM_GETHOVERTIME:
  2502. return plv->dwHoverTime;
  2503. case LVM_SETHOVERTIME:
  2504. {
  2505. DWORD dwRet = plv->dwHoverTime;
  2506. plv->dwHoverTime = (DWORD)lParam;
  2507. return dwRet;
  2508. }
  2509. case LVM_GETTOOLTIPS:
  2510. return (LRESULT)plv->hwndToolTips;
  2511. case LVM_SETTOOLTIPS:
  2512. {
  2513. HWND hwndToolTips = plv->hwndToolTips;
  2514. plv->hwndToolTips = (HWND)wParam;
  2515. return (LRESULT)hwndToolTips;
  2516. }
  2517. case LVM_SETICONSPACING:
  2518. {
  2519. DWORD dwRet = ListView_OnSetIconSpacing(plv, lParam);
  2520. // rearrange as necessary
  2521. if (ListView_RedrawEnabled(plv) &&
  2522. ((plv->ci.style & LVS_AUTOARRANGE) &&
  2523. (ListView_IsSmallView(plv) || ListView_IsIconView(plv))))
  2524. {
  2525. // Call off to the arrange function.
  2526. ListView_OnArrange(plv, LVA_DEFAULT);
  2527. }
  2528. return dwRet;
  2529. }
  2530. case LVM_SETHOTITEM:
  2531. {
  2532. int iOld = plv->iHot;
  2533. int iNew = (int)wParam;
  2534. if (iNew == -1 || ListView_IsValidItemNumber(plv, iNew)) {
  2535. ListView_OnSetHotItem(plv, (int)wParam);
  2536. }
  2537. return iOld;
  2538. }
  2539. case LVM_GETHOTITEM:
  2540. return plv->iHot;
  2541. // hCurHot is used iff LVS_EX_TRACKSELECT
  2542. case LVM_SETHOTCURSOR:
  2543. {
  2544. HCURSOR hCurOld = plv->hCurHot;
  2545. plv->hCurHot = (HCURSOR)lParam;
  2546. return (LRESULT)hCurOld;
  2547. }
  2548. case LVM_GETHOTCURSOR:
  2549. if (!plv->hCurHot)
  2550. plv->hCurHot = LoadHandCursor(0);
  2551. return (LRESULT)plv->hCurHot;
  2552. case LVM_APPROXIMATEVIEWRECT:
  2553. return ListView_OnApproximateViewRect(plv, (int)wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  2554. case LVM_SETLVRANGEOBJECT:
  2555. return ListView_OnSetLVRangeObject(plv, (int)wParam, (ILVRange *)lParam);
  2556. #ifdef LVM_SETWORKAREAS // until the headers are in ssync.....
  2557. case LVM_SETWORKAREAS:
  2558. ListView_OnSetWorkAreas(plv, (int)wParam, (RECT FAR *)lParam);
  2559. return 0;
  2560. case LVM_GETWORKAREAS:
  2561. ListView_OnGetWorkAreas(plv, (int)wParam, (RECT FAR *)lParam);
  2562. return 0;
  2563. case LVM_GETNUMBEROFWORKAREAS:
  2564. ListView_OnGetNumberOfWorkAreas(plv, (int *)lParam);
  2565. return 0;
  2566. case LVM_RESETEMPTYTEXT:
  2567. plv->fNoEmptyText = FALSE;
  2568. Str_Set(&plv->pszEmptyText, NULL);
  2569. if (ListView_Count(plv) == 0)
  2570. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  2571. return 1;
  2572. #endif
  2573. case WM_SIZE:
  2574. if (plv)
  2575. {
  2576. if (plv->hwndToolTips) {
  2577. TOOLINFO ti;
  2578. if (ListView_IsLabelTip(plv))
  2579. {
  2580. // A truncated label may have been exposed or vice versa.
  2581. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  2582. }
  2583. ti.cbSize = sizeof(ti);
  2584. ti.hwnd = plv->ci.hwnd;
  2585. ti.uId = 0;
  2586. // Resize the tooltip control so that it covers the entire
  2587. // area of the window when its parent gets resized.
  2588. GetClientRect( plv->ci.hwnd, &ti.rect );
  2589. SendMessage( plv->hwndToolTips, TTM_NEWTOOLRECT, 0, (LPARAM) &ti );
  2590. }
  2591. // if we're supposed to center the image,
  2592. // we need to do a full redraw on each size
  2593. if ((plv->ulBkImageFlags & LVBKIF_SOURCE_MASK) &&
  2594. (plv->xOffsetPercent || plv->yOffsetPercent)) {
  2595. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  2596. }
  2597. }
  2598. break;
  2599. case WM_NOTIFY:
  2600. return ListView_OnNotify(plv, wParam, (LPNMHDR)lParam);
  2601. case WM_MOUSEMOVE:
  2602. if (plv->hwndToolTips)
  2603. {
  2604. UINT uFlags;
  2605. int iHit, iSubHit;
  2606. RelayToToolTips(plv->hwndToolTips, hwnd, uMsg, wParam, lParam);
  2607. // check that we are still on the hit item, pop it!
  2608. iHit = _ListView_ItemHitTest( plv, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &uFlags, &iSubHit );
  2609. if (iHit != plv->iTTLastHit || iSubHit != plv->iTTLastSubHit)
  2610. ListView_PopBubble(plv);
  2611. }
  2612. ListView_OnMouseMove(plv, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), (UINT) wParam);
  2613. break;
  2614. case WM_GETOBJECT:
  2615. if( lParam == OBJID_QUERYCLASSNAMEIDX )
  2616. return MSAA_CLASSNAMEIDX_LISTVIEW;
  2617. break;
  2618. default:
  2619. {
  2620. LRESULT lres;
  2621. if (CCWndProc(&plv->ci, uMsg, wParam, lParam, &lres))
  2622. return lres;
  2623. }
  2624. // Special handling of magellan mouse message
  2625. if (uMsg == g_msgMSWheel) {
  2626. BOOL fScroll;
  2627. BOOL fDataZoom;
  2628. DWORD dwStyle;
  2629. int sb;
  2630. SCROLLINFO si;
  2631. int cScrollUnitsPerLine;
  2632. int cPage;
  2633. int cLinesPerDetant;
  2634. int cDetants;
  2635. int dPos;
  2636. int iWheelDelta;
  2637. if (g_bRunOnNT || g_bRunOnMemphis)
  2638. {
  2639. iWheelDelta = (int)(short)HIWORD(wParam);
  2640. fScroll = !(wParam & (MK_SHIFT | MK_CONTROL));
  2641. fDataZoom = (BOOL) (wParam & MK_SHIFT);
  2642. }
  2643. else
  2644. {
  2645. iWheelDelta = (int)wParam;
  2646. fDataZoom = (GetKeyState(VK_SHIFT) < 0);
  2647. fScroll = !fDataZoom && GetKeyState(VK_CONTROL) >= 0;
  2648. }
  2649. // Update count of scroll amount
  2650. gcWheelDelta -= iWheelDelta;
  2651. cDetants = gcWheelDelta / WHEEL_DELTA;
  2652. if (cDetants != 0) {
  2653. gcWheelDelta %= WHEEL_DELTA;
  2654. }
  2655. if (fScroll) {
  2656. if (g_ucScrollLines > 0 &&
  2657. cDetants != 0 &&
  2658. ((WS_VSCROLL | WS_HSCROLL) & (dwStyle = ListView_GetWindowStyle(plv)))) {
  2659. sb = (dwStyle & WS_VSCROLL) ? SB_VERT : SB_HORZ;
  2660. // Get the scroll amount of one line
  2661. cScrollUnitsPerLine = _ListView_GetScrollUnitsPerLine(plv, sb);
  2662. ASSERT(cScrollUnitsPerLine > 0);
  2663. si.cbSize = sizeof(SCROLLINFO);
  2664. si.fMask = SIF_PAGE | SIF_POS;
  2665. if (!ListView_GetScrollInfo(plv, sb, &si))
  2666. return 1;
  2667. // The size of a page is at least one line, and
  2668. // leaves one line of overlap
  2669. cPage = (max(cScrollUnitsPerLine, (int)si.nPage - cScrollUnitsPerLine)) / cScrollUnitsPerLine;
  2670. // Don't scroll more than one page per detant
  2671. cLinesPerDetant = (int) min((ULONG) cPage, (ULONG) g_ucScrollLines);
  2672. dPos = cLinesPerDetant * cDetants * cScrollUnitsPerLine;
  2673. ListView_DismissEdit(plv, FALSE);
  2674. ListView_ComOnScroll(
  2675. plv, SB_THUMBTRACK, si.nPos + dPos, sb, cScrollUnitsPerLine, - 1);
  2676. ListView_UpdateScrollBars(plv);
  2677. // After scrolling, the tooltip might need to change
  2678. // so send the tooltip a fake mousemove message to force
  2679. // a recompute. We use WM_NCMOUSEMOVE since our lParam
  2680. // is in screen coordinates, not client coordinates.
  2681. ListView_PopBubble(plv);
  2682. RelayToToolTips(plv->hwndToolTips, plv->ci.hwnd,
  2683. WM_NCMOUSEMOVE, HTCLIENT, lParam);
  2684. }
  2685. return 1;
  2686. } else if (fDataZoom) {
  2687. LV_HITTESTINFO ht;
  2688. ht.pt.x = GET_X_LPARAM(lParam);
  2689. ht.pt.y = GET_Y_LPARAM(lParam);
  2690. ScreenToClient(hwnd, &(ht.pt));
  2691. // If we are rolling forward and we hit an item then navigate
  2692. // into that item (simulate dblclk which will open it). Otherwise
  2693. // just fall through so it isn't handled. In that case if we
  2694. // are being hosted in explorer it will do a backwards
  2695. // history navigation.
  2696. if ((iWheelDelta > 0) && (ListView_OnSubItemHitTest(plv, &ht) >= 0) &&
  2697. (ht.flags & LVHT_ONITEM) && cDetants != 0) {
  2698. BYTE aKeyState[256];
  2699. // This is a bit yucky but when ListView_HandleMouse sends the
  2700. // notification to the listview owner we need to make sure that
  2701. // it doesn't think the shift key is down. Otherwise it may
  2702. // perform some "alternate" action but in this case we always
  2703. // want it to perform the default open action.
  2704. //
  2705. // Strip the high bit of VK_SHIFT so that the shift key is
  2706. // not down.
  2707. if (GetKeyboardState(aKeyState))
  2708. {
  2709. aKeyState[VK_SHIFT] &= 0x7f;
  2710. SetKeyboardState(aKeyState);
  2711. }
  2712. ListView_HandleMouse(plv, FALSE, ht.pt.x, ht.pt.y, 0, TRUE);
  2713. ListView_HandleMouse(plv, TRUE, ht.pt.x, ht.pt.y, 0, TRUE);
  2714. return 1;
  2715. }
  2716. // else fall through
  2717. }
  2718. }
  2719. break;
  2720. }
  2721. DoDefault:
  2722. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  2723. }
  2724. void NEAR ListView_OnWinIniChange(LV* plv, WPARAM wParam, LPARAM lParam)
  2725. {
  2726. // BUGBUG: will this also catch sysparametersinfo?
  2727. // we need a general way of handling this, not
  2728. // just relying on the listview.
  2729. InitGlobalMetrics(wParam);
  2730. switch (wParam) {
  2731. case 0:
  2732. case SPI_SETNONCLIENTMETRICS:
  2733. case SPI_SETICONTITLELOGFONT:
  2734. case SPI_SETICONMETRICS:
  2735. // If wParam is 0, only reload settings if lParam is 0 too. This catches the wild-card scenario
  2736. // (like the old plus tab which does WM_WININICHANGE, 0, 0) but allows us to ignore wParam = 0
  2737. // and lParam = lpszSectionName. Reduces unecessary flashing.
  2738. if (wParam || !lParam)
  2739. {
  2740. if (!(plv->flags & LVF_ICONSPACESET))
  2741. ListView_OnSetIconSpacing(plv, (LPARAM)-1);
  2742. if (plv->flags & LVF_FONTCREATED)
  2743. ListView_OnSetFont(plv, NULL, TRUE);
  2744. // Force a recalc of all the icon regions by stripping and
  2745. // then adding back the LVS_EX_REGIONAL bit.
  2746. if (plv->exStyle & LVS_EX_REGIONAL) {
  2747. ListView_ExtendedStyleChange(plv, 0, LVS_EX_REGIONAL);
  2748. ListView_ExtendedStyleChange(plv, LVS_EX_REGIONAL, LVS_EX_REGIONAL);
  2749. }
  2750. }
  2751. break;
  2752. default:
  2753. break;
  2754. }
  2755. // If we are in an Iconic view and the user is in autoarrange mode,
  2756. // then we need to arrange the items.
  2757. //
  2758. if ((ListView_IsOwnerData( plv ) || (plv->ci.style & LVS_AUTOARRANGE)) &&
  2759. (ListView_IsSmallView(plv) || ListView_IsIconView(plv)))
  2760. {
  2761. // Call off to the arrange function.
  2762. ListView_OnArrange(plv, LVA_DEFAULT);
  2763. }
  2764. }
  2765. BOOL NEAR ListView_OnCreate(LV* plv, CREATESTRUCT FAR* lpCreateStruct)
  2766. {
  2767. CCCreateWindow();
  2768. InitDitherBrush();
  2769. CIInitialize(&plv->ci, plv->ci.hwnd, lpCreateStruct);
  2770. #ifdef DEBUG
  2771. if (GetAsyncKeyState(VK_SHIFT) < 0 &&
  2772. GetAsyncKeyState(VK_CONTROL) < 0) {
  2773. //plv->exStyle |= LVS_EX_SUBITEMIMAGES;
  2774. plv->exStyle |= LVS_EX_FULLROWSELECT;
  2775. plv->ci.style |= LVS_SHOWSELALWAYS;
  2776. SetWindowLong(plv->ci.hwnd, GWL_STYLE, plv->ci.style);
  2777. }
  2778. #endif
  2779. plv->dwExStyle = lpCreateStruct->dwExStyle;
  2780. if (plv->ci.style & WS_VISIBLE)
  2781. plv->flags |= LVF_VISIBLE;
  2782. ListView_GetRegIASetting(&g_bUseDblClickTimer);
  2783. if (ListView_IsOwnerData(plv))
  2784. {
  2785. // ownerdata initialization
  2786. plv->plvrangeSel = LVRange_Create();
  2787. if (NULL == plv->plvrangeSel)
  2788. goto error0;
  2789. plv->plvrangeCut = LVRange_Create();
  2790. if (NULL == plv->plvrangeCut)
  2791. goto error0;
  2792. }
  2793. else
  2794. {
  2795. ASSERT(plv->plvrangeSel == NULL);
  2796. ASSERT(plv->plvrangeCut == NULL);
  2797. plv->hdpa = DPA_CreateEx(LV_HDPA_GROW, plv->hheap);
  2798. if (!plv->hdpa)
  2799. goto error0;
  2800. plv->hdpaZOrder = DPA_CreateEx(LV_HDPA_GROW, plv->hheap);
  2801. if (!plv->hdpaZOrder)
  2802. goto error1;
  2803. }
  2804. ASSERT(plv->nWorkAreas == 0);
  2805. ASSERT(plv->prcWorkAreas == NULL);
  2806. plv->iNoHover = -1;
  2807. plv->dwHoverTime = HOVER_DEFAULT;
  2808. plv->iHot = -1;
  2809. plv->iEdit = -1;
  2810. plv->iFocus = -1;
  2811. plv->iDrag = -1;
  2812. plv->iTTLastHit = -1;
  2813. plv->iFreeSlot = -1;
  2814. plv->rcView.left = RECOMPUTE;
  2815. ASSERT(plv->iMSAAMin == plv->iMSAAMax);
  2816. plv->sizeClient.cx = lpCreateStruct->cx;
  2817. plv->sizeClient.cy = lpCreateStruct->cy;
  2818. // Setup flag to say if positions are in small or large view
  2819. if (ListView_IsSmallView(plv))
  2820. plv->flags |= LVF_ICONPOSSML;
  2821. // force calculation of listview metrics
  2822. ListView_OnSetFont(plv, NULL, FALSE);
  2823. plv->cxItem = 16 * plv->cxLabelChar + plv->cxSmIcon;
  2824. // if we're in ownerdraw report mode, the size got saved to cyItemSave
  2825. // at creation time, both need to have this
  2826. if ((plv->ci.style & LVS_OWNERDRAWFIXED) && ListView_IsReportView(plv))
  2827. plv->cyItem = plv->cyItemSave;
  2828. else
  2829. plv->cyItemSave = plv->cyItem;
  2830. ListView_OnSetIconSpacing(plv, (LPARAM)-1);
  2831. ListView_UpdateScrollBars(plv); // sets plv->cItemCol
  2832. plv->clrBk = CLR_NONE;
  2833. plv->clrText = CLR_DEFAULT;
  2834. plv->clrTextBk = CLR_DEFAULT;
  2835. plv->clrHotlight = CLR_DEFAULT;
  2836. // create the bk brush, and set the imagelists colors if needed
  2837. ListView_OnSetBkColor(plv, g_clrWindow);
  2838. // Initialize report view fields
  2839. plv->xTotalColumnWidth = RECOMPUTE;
  2840. if (ListView_IsReportView(plv))
  2841. ListView_RInitialize(plv, FALSE);
  2842. if (plv->ci.style & WS_DISABLED) {
  2843. plv->ci.style &= ~WS_DISABLED;
  2844. ListView_EnableWindow(plv, FALSE);
  2845. }
  2846. // tooltip for unfolding name lables
  2847. plv->hwndToolTips = CreateWindow(TOOLTIPS_CLASS, NULL,
  2848. WS_POPUP|TTS_NOPREFIX, 0, 0, 0, 0,
  2849. NULL, NULL, g_hinst, NULL);
  2850. if ( plv->hwndToolTips )
  2851. {
  2852. TOOLINFO ti;
  2853. ti.cbSize = sizeof(ti);
  2854. ti.uFlags = TTF_TRANSPARENT;
  2855. ti.hwnd = plv->ci.hwnd;
  2856. ti.uId = 0;
  2857. ti.hinst = NULL;
  2858. ti.lpszText = LPSTR_TEXTCALLBACK;
  2859. GetClientRect( plv->ci.hwnd, &ti.rect );
  2860. SendMessage( plv->hwndToolTips, TTM_ADDTOOL, 0, (LPARAM) &ti );
  2861. /* Ensure that the tooltips use the same font as the view */
  2862. FORWARD_WM_SETFONT(plv->hwndToolTips, plv->hfontLabel, FALSE, SendMessage);
  2863. }
  2864. ASSERT(plv->hwndToolTips);
  2865. ASSERT(FALSE == plv->fOneClickOK);
  2866. SetTimer(plv->ci.hwnd, IDT_ONECLICKOK, GetDoubleClickTime(), NULL);
  2867. return TRUE;
  2868. error1:
  2869. DPA_Destroy(plv->hdpa);
  2870. error0:
  2871. if ( plv->plvrangeSel )
  2872. plv->plvrangeSel->lpVtbl->Release( plv->plvrangeSel );
  2873. if ( plv->plvrangeCut)
  2874. plv->plvrangeCut->lpVtbl->Release( plv->plvrangeCut );
  2875. return FALSE;
  2876. }
  2877. void NEAR PASCAL ListView_DeleteHrgnInval(LV* plv)
  2878. {
  2879. if (plv->hrgnInval && plv->hrgnInval != (HRGN)ENTIRE_REGION)
  2880. DeleteObject(plv->hrgnInval);
  2881. plv->hrgnInval = NULL;
  2882. }
  2883. void NEAR ListView_OnDestroy(LV* plv)
  2884. {
  2885. //
  2886. // The tooltip window may or may not exist at this point. It
  2887. // depends if the owning window of the tips is also being destroy.
  2888. // If so, then the tips are gone already.
  2889. //
  2890. if (IsWindow(plv->hwndToolTips))
  2891. DestroyWindow(plv->hwndToolTips);
  2892. if (plv->hCurHot)
  2893. DestroyCursor(plv->hCurHot);
  2894. plv->hwndToolTips = NULL;
  2895. Str_Set(&plv->pszTip, NULL);
  2896. Str_Set(&plv->pszEmptyText, NULL);
  2897. TerminateDitherBrush();
  2898. if (!ListView_IsOwnerData(plv)) {
  2899. // Make sure to notify the app
  2900. ListView_OnDeleteAllItems(plv);
  2901. }
  2902. if ((plv->flags & LVF_FONTCREATED) && plv->hfontLabel) {
  2903. DeleteObject(plv->hfontLabel);
  2904. // plv->flags &= ~LVF_FONTCREATED;
  2905. // plv->hwfontLabel = NULL;
  2906. }
  2907. if (plv->hFontHot)
  2908. DeleteObject(plv->hFontHot);
  2909. ListView_DeleteHrgnInval(plv);
  2910. if (plv->prcWorkAreas)
  2911. {
  2912. // This assert is bogus: If the app created work areas then deleted
  2913. // them, nWorkAreas will be 0 but prcWorkAreas will be non-NULL.
  2914. // ASSERT(plv->nWorkAreas > 0);
  2915. LocalFree(plv->prcWorkAreas);
  2916. }
  2917. }
  2918. void NEAR ListView_OnNCDestroy(LV* plv)
  2919. {
  2920. CCDestroyWindow();
  2921. if ((!(plv->ci.style & LVS_SHAREIMAGELISTS)) || ListView_CheckBoxes(plv)) {
  2922. if (plv->himlState &&
  2923. (plv->himlState != plv->himl) &&
  2924. (plv->himlState != plv->himlSmall))
  2925. {
  2926. ImageList_Destroy(plv->himlState);
  2927. }
  2928. }
  2929. if (!(plv->ci.style & LVS_SHAREIMAGELISTS))
  2930. {
  2931. if (plv->himl)
  2932. ImageList_Destroy(plv->himl);
  2933. if (plv->himlSmall)
  2934. ImageList_Destroy(plv->himlSmall);
  2935. }
  2936. if (ListView_IsOwnerData(plv)) {
  2937. plv->plvrangeSel->lpVtbl->Release( plv->plvrangeSel );
  2938. plv->plvrangeCut->lpVtbl->Release( plv->plvrangeCut );
  2939. plv->cTotalItems = 0;
  2940. }
  2941. ListView_ReleaseBkImage(plv);
  2942. if (plv->hbrBk)
  2943. DeleteBrush(plv->hbrBk);
  2944. if (plv->hdpa)
  2945. DPA_Destroy(plv->hdpa);
  2946. if (plv->hdpaZOrder)
  2947. DPA_Destroy(plv->hdpaZOrder);
  2948. ListView_RDestroy(plv);
  2949. IncrementSearchFree(&plv->is);
  2950. ListView_SetPtr(plv->ci.hwnd, NULL);
  2951. NearFree(plv);
  2952. }
  2953. // sets the background color for the listview
  2954. //
  2955. // this creats the brush for drawing the background as well
  2956. // as sets the imagelists background color if needed
  2957. BOOL NEAR ListView_OnSetBkColor(LV* plv, COLORREF clrBk)
  2958. {
  2959. if (plv->clrBk != clrBk)
  2960. {
  2961. if (plv->hbrBk)
  2962. {
  2963. DeleteBrush(plv->hbrBk);
  2964. plv->hbrBk = NULL;
  2965. }
  2966. if (clrBk != CLR_NONE)
  2967. {
  2968. plv->hbrBk = CreateSolidBrush(clrBk);
  2969. if (!plv->hbrBk)
  2970. return FALSE;
  2971. }
  2972. // don't mess with the imagelist color if things are shared
  2973. if (!(plv->ci.style & LVS_SHAREIMAGELISTS)) {
  2974. if (plv->himl)
  2975. ImageList_SetBkColor(plv->himl, clrBk);
  2976. if (plv->himlSmall)
  2977. ImageList_SetBkColor(plv->himlSmall, clrBk);
  2978. if (plv->himlState)
  2979. ImageList_SetBkColor(plv->himlState, clrBk);
  2980. }
  2981. plv->clrBk = clrBk;
  2982. }
  2983. return TRUE;
  2984. }
  2985. void PASCAL InitBrushOrg(LV* plv, HDC hdc)
  2986. {
  2987. int x;
  2988. if (ListView_IsSmallView(plv) || ListView_IsIconView(plv)) {
  2989. x = plv->ptOrigin.x;
  2990. } else if (ListView_IsListView(plv)) {
  2991. x = plv->xOrigin;
  2992. } else {
  2993. x = (int)plv->ptlRptOrigin.x;
  2994. }
  2995. SetBrushOrgEx(hdc, -x, 0, NULL);
  2996. }
  2997. void NEAR PASCAL ListView_InvalidateRegion(LV* plv, HRGN hrgn)
  2998. {
  2999. if (hrgn) {
  3000. if (plv->hrgnInval == NULL) {
  3001. plv->hrgnInval = hrgn;
  3002. } else {
  3003. // union it in if the entire region isn't marked for invalidate
  3004. if (plv->hrgnInval != (HRGN)ENTIRE_REGION) {
  3005. UnionRgn(plv->hrgnInval, plv->hrgnInval, hrgn);
  3006. }
  3007. DeleteObject(hrgn);
  3008. }
  3009. }
  3010. }
  3011. // ----------------------------------------------------------------------------
  3012. //
  3013. // LVSeeThruScroll
  3014. //
  3015. // Used when a watermark is the listview's background (detected via clrTextBk
  3016. // being CLR_NONE) to perform a flicker-free scroll of the client area, using
  3017. // an offscreen bitmap
  3018. //
  3019. // potential perf issue -- caching DC and/or bitmap instead of create/destroy
  3020. // on each call
  3021. //
  3022. // jeffbog 2/29/96
  3023. //
  3024. // ----------------------------------------------------------------------------
  3025. void LVSeeThruScroll(LV *plv, LPRECT lprcUpdate)
  3026. {
  3027. HDC hdcOff;
  3028. HBITMAP hbmpOff;
  3029. int x,y,cx,cy;
  3030. HDC hdc = GetDC(plv->ci.hwnd);
  3031. if (!lprcUpdate)
  3032. {
  3033. x = y = 0;
  3034. cx = plv->sizeClient.cx;
  3035. cy = plv->sizeClient.cy;
  3036. }
  3037. else
  3038. {
  3039. x = lprcUpdate->left;
  3040. y = lprcUpdate->top;
  3041. cx = lprcUpdate->right - x;
  3042. cy = lprcUpdate->bottom - y;
  3043. }
  3044. hdcOff = CreateCompatibleDC(hdc);
  3045. hbmpOff = CreateCompatibleBitmap(hdc, plv->sizeClient.cx, plv->sizeClient.cy);
  3046. SelectObject(hdcOff, hbmpOff);
  3047. SendMessage(plv->ci.hwnd, WM_PRINT, (WPARAM)hdcOff, PRF_CLIENT | PRF_ERASEBKGND);
  3048. BitBlt(hdc, x, y, cx, cy, hdcOff, x, y, SRCCOPY);
  3049. ReleaseDC(plv->ci.hwnd, hdc);
  3050. DeleteDC(hdcOff);
  3051. DeleteObject(hbmpOff);
  3052. }
  3053. void NEAR ListView_OnPaint(LV* plv, HDC hdc)
  3054. {
  3055. PAINTSTRUCT ps;
  3056. RECT rcUpdate;
  3057. // Before handling WM_PAINT, go ensure everything's recomputed...
  3058. //
  3059. if (plv->rcView.left == RECOMPUTE)
  3060. ListView_Recompute(plv);
  3061. // If we're in report view, update the header window: it looks
  3062. // better this way...
  3063. //
  3064. if (ListView_IsReportView(plv) && plv->hwndHdr)
  3065. UpdateWindow(plv->hwndHdr);
  3066. // If nothing to do (i.e., we recieved a WM_PAINT because
  3067. // of an RDW_INTERNALPAINT, and we didn't invalidate anything)
  3068. // don't bother with the Begin/EndPaint.
  3069. //
  3070. if (hdc || GetUpdateRect(plv->ci.hwnd, &rcUpdate, FALSE))
  3071. {
  3072. if (!(plv->flags & LVF_VISIBLE)) {
  3073. plv->flags |= LVF_VISIBLE;
  3074. // We may try to resize the column
  3075. ListView_MaybeResizeListColumns(plv, 0, ListView_Count(plv)-1);
  3076. ListView_UpdateScrollBars(plv);
  3077. }
  3078. // this needs to be done before the beginpaint because it clears
  3079. // out the update region
  3080. if (!(plv->flags & LVF_REDRAW)) {
  3081. // add this region to our local invalidate region
  3082. HRGN hrgn = CreateRectRgn(0, 0, 0,0);
  3083. if (hrgn) {
  3084. // ok if GetUpdateRgn fails... then hrgn will still be
  3085. // and empty region..
  3086. GetUpdateRgn(plv->ci.hwnd, hrgn, FALSE);
  3087. ListView_InvalidateRegion(plv, hrgn);
  3088. }
  3089. }
  3090. if (hdc)
  3091. {
  3092. InitBrushOrg(plv, hdc);
  3093. SetRect(&ps.rcPaint, 0, 0, plv->sizeClient.cx, plv->sizeClient.cy);
  3094. if (ListView_RedrawEnabled(plv))
  3095. ListView_Redraw(plv, hdc, &ps.rcPaint);
  3096. }
  3097. else
  3098. {
  3099. hdc = BeginPaint(plv->ci.hwnd, &ps);
  3100. InitBrushOrg(plv, hdc);
  3101. if (ListView_RedrawEnabled(plv))
  3102. ListView_Redraw(plv, hdc, &ps.rcPaint);
  3103. EndPaint(plv->ci.hwnd, &ps);
  3104. }
  3105. }
  3106. }
  3107. void ListView_DrawSimpleBackground(LV *plv, HDC hdc, RECT *prcClip)
  3108. {
  3109. if (plv->clrBk != CLR_NONE)
  3110. {
  3111. //
  3112. // We just have a simple background color.
  3113. //
  3114. FillRect(hdc, prcClip, plv->hbrBk);
  3115. }
  3116. else
  3117. {
  3118. //
  3119. // Parent HWND draws the background for us.
  3120. //
  3121. SendMessage(plv->ci.hwndParent, WM_ERASEBKGND, (WPARAM)hdc, 0);
  3122. }
  3123. }
  3124. void ListView_DrawBackground(LV *plv, HDC hdc, RECT *prcClip)
  3125. {
  3126. HRGN hrgnClipSave;
  3127. RECT rcClip;
  3128. // Optimize the common/simple case
  3129. if (!(plv->pImgCtx && plv->fImgCtxComplete))
  3130. {
  3131. ListView_DrawSimpleBackground(plv, hdc, prcClip);
  3132. return;
  3133. }
  3134. //
  3135. // Save the old clipping region,
  3136. // since we whack on it a lot.
  3137. //
  3138. hrgnClipSave = CreateRectRgnIndirect(prcClip);
  3139. if (hrgnClipSave)
  3140. {
  3141. if (GetClipRgn(hdc, hrgnClipSave) <= 0)
  3142. {
  3143. DeleteObject(hrgnClipSave);
  3144. hrgnClipSave = NULL;
  3145. }
  3146. }
  3147. //
  3148. // Clip the clipping region to the caller's rectangle,
  3149. // and save the final clipping rectangle in rcClip.
  3150. //
  3151. if (prcClip != NULL)
  3152. {
  3153. IntersectClipRect(hdc, prcClip->left, prcClip->top,
  3154. prcClip->right, prcClip->bottom);
  3155. }
  3156. GetClipBox(hdc, &rcClip);
  3157. //
  3158. // If we have an image to draw, go draw it and
  3159. // exclue it from the clipping region.
  3160. //
  3161. if (plv->pImgCtx && plv->fImgCtxComplete)
  3162. {
  3163. RECT rcImage, rcClient;
  3164. ULONG ulState;
  3165. SIZE sizeImg;
  3166. POINT ptBackOrg;
  3167. //
  3168. // Compute ptBackOrg (aka scrolling offset), based on view style.
  3169. //
  3170. switch (plv->ci.style & LVS_TYPEMASK)
  3171. {
  3172. case LVS_LIST:
  3173. ptBackOrg.x = -plv->xOrigin;
  3174. ptBackOrg.y = 0;
  3175. break;
  3176. case LVS_REPORT:
  3177. ptBackOrg.x = -plv->ptlRptOrigin.x;
  3178. ptBackOrg.y = -plv->ptlRptOrigin.y + plv->yTop;
  3179. break;
  3180. default:
  3181. ptBackOrg.x = -plv->ptOrigin.x;
  3182. ptBackOrg.y = -plv->ptOrigin.y;
  3183. break;
  3184. }
  3185. ListView_Realize(plv, hdc, TRUE, FALSE);
  3186. switch (plv->ulBkImageFlags & LVBKIF_STYLE_MASK)
  3187. {
  3188. case LVBKIF_STYLE_TILE:
  3189. IImgCtx_Tile(plv->pImgCtx, hdc, &ptBackOrg, prcClip, NULL);
  3190. ExcludeClipRect(hdc, prcClip->left, prcClip->top,
  3191. prcClip->right, prcClip->bottom);
  3192. break;
  3193. case LVBKIF_STYLE_NORMAL:
  3194. //
  3195. // Start with the base image.
  3196. //
  3197. IImgCtx_GetStateInfo(plv->pImgCtx, &ulState, &sizeImg, FALSE);
  3198. rcImage.left = 0;
  3199. rcImage.top = 0;
  3200. rcImage.right = sizeImg.cx;
  3201. rcImage.bottom = sizeImg.cy;
  3202. //
  3203. // Adjust for caller offsets.
  3204. //
  3205. GetClientRect(plv->ci.hwnd, &rcClient);
  3206. if (plv->xOffsetPercent)
  3207. {
  3208. LONG dx = plv->xOffsetPercent * (rcClient.right - sizeImg.cx) / 100;
  3209. rcImage.left += dx;
  3210. rcImage.right += dx;
  3211. }
  3212. if (plv->yOffsetPercent)
  3213. {
  3214. LONG dy = plv->yOffsetPercent * (rcClient.bottom - sizeImg.cy) / 100;
  3215. rcImage.top += dy;
  3216. rcImage.bottom += dy;
  3217. }
  3218. //
  3219. // Adjust for ptBackOrg (scrolling offset).
  3220. //
  3221. rcImage.left += ptBackOrg.x;
  3222. rcImage.top += ptBackOrg.y;
  3223. rcImage.right += ptBackOrg.x;
  3224. rcImage.bottom += ptBackOrg.y;
  3225. //
  3226. // Draw the image, if necessary.
  3227. //
  3228. if (RectVisible(hdc, &rcImage))
  3229. {
  3230. IImgCtx_Draw(plv->pImgCtx, hdc, &rcImage);
  3231. ExcludeClipRect(hdc, rcImage.left, rcImage.top,
  3232. rcImage.right, rcImage.bottom);
  3233. }
  3234. break;
  3235. }
  3236. }
  3237. //
  3238. // Now draw the rest of the background.
  3239. //
  3240. if (RectVisible(hdc, prcClip))
  3241. {
  3242. ListView_DrawSimpleBackground(plv, hdc, prcClip);
  3243. }
  3244. //
  3245. // Restore old clipping region.
  3246. //
  3247. SelectClipRgn(hdc, hrgnClipSave);
  3248. if (hrgnClipSave)
  3249. {
  3250. DeleteObject(hrgnClipSave);
  3251. }
  3252. }
  3253. BOOL NEAR ListView_OnEraseBkgnd(LV *plv, HDC hdc)
  3254. {
  3255. RECT rcClip;
  3256. // Regional listviews only need to erase if we're on a slow machine
  3257. if (!(plv->exStyle & LVS_EX_REGIONAL) || g_fSlowMachine) {
  3258. //
  3259. // We draw our own background, erase with it.
  3260. //
  3261. GetClipBox(hdc, &rcClip);
  3262. ListView_DrawBackground(plv, hdc, &rcClip);
  3263. }
  3264. return TRUE;
  3265. }
  3266. void NEAR ListView_OnCommand(LV* plv, int id, HWND hwndCtl, UINT codeNotify)
  3267. {
  3268. if (hwndCtl == plv->hwndEdit)
  3269. {
  3270. switch (codeNotify)
  3271. {
  3272. case EN_UPDATE:
  3273. #if defined(FE_IME)
  3274. // We don't want flicker during replacing current selection
  3275. // as we use selection for IME composition.
  3276. //
  3277. if ((g_fDBCSInputEnabled) && (plv->flags & LVF_INSERTINGCOMP))
  3278. break;
  3279. #endif
  3280. // We will use the ID of the window as a Dirty flag...
  3281. if (IsWindowVisible(plv->hwndEdit)) {
  3282. SetWindowID(plv->hwndEdit, 1);
  3283. ListView_SetEditSize(plv);
  3284. }
  3285. break;
  3286. case EN_KILLFOCUS:
  3287. // We lost focus, so dismiss edit and save changes
  3288. // (Note that the owner might reject the change and restart
  3289. // edit mode, which traps the user. Owners need to give the
  3290. // user a way to get out.)
  3291. //
  3292. if (!ListView_DismissEdit(plv, FALSE))
  3293. return;
  3294. break;
  3295. case HN_BEGINDIALOG: // pen windows is bringing up a dialog
  3296. ASSERT(GetSystemMetrics(SM_PENWINDOWS)); // only on a pen system
  3297. plv->fNoDismissEdit = TRUE;
  3298. break;
  3299. case HN_ENDDIALOG: // pen windows has destroyed dialog
  3300. ASSERT(GetSystemMetrics(SM_PENWINDOWS)); // only on a pen system
  3301. plv->fNoDismissEdit = FALSE;
  3302. break;
  3303. }
  3304. // Forward edit control notifications up to parent
  3305. //
  3306. if (IsWindow(hwndCtl))
  3307. FORWARD_WM_COMMAND(plv->ci.hwndParent, id, hwndCtl, codeNotify, SendMessage);
  3308. }
  3309. }
  3310. void NEAR ListView_OnWindowPosChanged(LV* plv, const WINDOWPOS FAR* lpwpos)
  3311. {
  3312. if (!lpwpos || !(lpwpos->flags & SWP_NOSIZE))
  3313. {
  3314. RECT rc;
  3315. int iOldSlots;
  3316. if (ListView_IsOwnerData(plv) &&
  3317. (ListView_IsSmallView(plv) || ListView_IsIconView(plv)))
  3318. {
  3319. iOldSlots = ListView_GetSlotCount(plv, TRUE);
  3320. }
  3321. GetClientRect(plv->ci.hwnd, &rc);
  3322. plv->sizeClient.cx = rc.right;
  3323. plv->sizeClient.cy = rc.bottom;
  3324. if ((plv->ci.style & LVS_AUTOARRANGE) &&
  3325. (ListView_IsSmallView(plv) || ListView_IsIconView(plv)))
  3326. {
  3327. // Call off to the arrange function.
  3328. ListView_OnArrange(plv, LVA_DEFAULT);
  3329. }
  3330. if (ListView_IsOwnerData(plv))
  3331. {
  3332. plv->rcView.left = RECOMPUTE;
  3333. ListView_Recompute(plv);
  3334. ListView_DismissEdit(plv, FALSE);
  3335. if (ListView_IsSmallView(plv) || ListView_IsIconView(plv))
  3336. {
  3337. // Uses the
  3338. int iNewSlots = ListView_GetSlotCount(plv, TRUE);
  3339. if ((iNewSlots != iOldSlots) && (ListView_Count(plv) > min(iNewSlots, iOldSlots)))
  3340. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  3341. }
  3342. }
  3343. ListView_RInitialize(plv, TRUE);
  3344. // Always make sure the scrollbars are updated to the new size
  3345. ListView_UpdateScrollBars(plv);
  3346. }
  3347. }
  3348. void ListView_InvalidateSelectedOrCutOwnerData(LV* plv, ILVRange *plvrangeSel)
  3349. {
  3350. UINT rdwFlags = RDW_INVALIDATE;
  3351. int cItem = ListView_Count( plv );
  3352. DWORD dwType = plv->ci.style & LVS_TYPEMASK;
  3353. int i;
  3354. RECT rcView;
  3355. ASSERT(ListView_IsOwnerData(plv));
  3356. ASSERT(plv );
  3357. GetClientRect( plv->ci.hwnd, &rcView );
  3358. if (plv->clrTextBk == CLR_NONE
  3359. || (plv->himl && (plv->clrBk != ImageList_GetBkColor(plv->himl)))) {
  3360. // always do an erase, otherwise the text background won't paint right
  3361. rdwFlags |= RDW_ERASE;
  3362. }
  3363. // calculate start of items and end of items visible on the view
  3364. //
  3365. switch (dwType)
  3366. {
  3367. case LVS_REPORT:
  3368. i = ListView_RYHitTest(plv, rcView.top);
  3369. cItem = ListView_RYHitTest(plv, rcView.bottom) + 1;
  3370. break;
  3371. case LVS_LIST:
  3372. i = ListView_LCalcViewItem(plv, rcView.left, rcView.top );
  3373. cItem = ListView_LCalcViewItem( plv, rcView.right, rcView.bottom ) + 1;
  3374. break;
  3375. default:
  3376. ListView_CalcMinMaxIndex( plv, &rcView, &i, &cItem );
  3377. break;
  3378. }
  3379. i = max( i, 0 );
  3380. cItem = min( ListView_Count( plv ), cItem );
  3381. if (cItem > i)
  3382. {
  3383. ListView_NotifyCacheHint( plv, i, cItem-1 );
  3384. }
  3385. for (; i < cItem; i++)
  3386. {
  3387. if (plvrangeSel->lpVtbl->IsSelected(plvrangeSel, i ) == S_OK)
  3388. {
  3389. ListView_InvalidateItem(plv, i, FALSE, rdwFlags);
  3390. }
  3391. }
  3392. }
  3393. void NEAR ListView_RedrawSelection(LV* plv)
  3394. {
  3395. if (ListView_IsOwnerData(plv)) {
  3396. ListView_InvalidateSelectedOrCutOwnerData( plv, plv->plvrangeSel );
  3397. } else {
  3398. int i = -1;
  3399. while ((i = ListView_OnGetNextItem(plv, i, LVNI_SELECTED)) != -1) {
  3400. ListView_InvalidateItem(plv, i, TRUE, RDW_INVALIDATE);
  3401. }
  3402. if (ListView_IsReportView(plv)) {
  3403. int iEnd = ListView_RYHitTest(plv, plv->sizeClient.cy) + 1;
  3404. iEnd = min(iEnd, ListView_Count(plv));
  3405. // if we're in report mode, sub items may have selection focus
  3406. for (i = ListView_RYHitTest(plv, 0); i < iEnd; i++) {
  3407. int iCol;
  3408. for (iCol = 1; iCol < plv->cCol; iCol++) {
  3409. LISTSUBITEM lsi;
  3410. ListView_GetSubItem(plv, i, iCol, &lsi);
  3411. if (lsi.state & LVIS_SELECTED) {
  3412. ListView_InvalidateItem(plv, i, FALSE, RDW_INVALIDATE);
  3413. }
  3414. break;
  3415. }
  3416. }
  3417. }
  3418. }
  3419. UpdateWindow( plv->ci.hwnd );
  3420. }
  3421. void NEAR ListView_OnSetFocus(LV* plv, HWND hwndOldFocus)
  3422. {
  3423. ASSERT(gcWheelDelta == 0);
  3424. // due to the way listview call SetFocus on themselves on buttondown,
  3425. // the window can get a strange sequence of focus messages: first
  3426. // set, then kill, and then set again. since these are not really
  3427. // focus changes, ignore them and only handle "real" cases.
  3428. //
  3429. // But still send out the accessibility notification because USER
  3430. // has already pushed focus back to the listview instead of to the
  3431. // focus item.
  3432. if (hwndOldFocus == plv->ci.hwnd)
  3433. {
  3434. ListView_NotifyFocusEvent(plv);
  3435. return;
  3436. }
  3437. plv->flags |= LVF_FOCUSED | LVF_UNFOLDED;
  3438. if (IsWindowVisible(plv->ci.hwnd))
  3439. {
  3440. if (plv->iFocus != -1)
  3441. {
  3442. ListView_InvalidateItem(plv, plv->iFocus, TRUE, RDW_INVALIDATE);
  3443. ListView_NotifyFocusEvent(plv);
  3444. }
  3445. ListView_RedrawSelection(plv);
  3446. }
  3447. // Let the parent window know that we are getting the focus.
  3448. CCSendNotify(&plv->ci, NM_SETFOCUS, NULL);
  3449. }
  3450. void NEAR ListView_OnKillFocus(LV* plv, HWND hwndNewFocus)
  3451. {
  3452. // Reset wheel scroll amount
  3453. gcWheelDelta = 0;
  3454. // due to the way listview call SetFocus on themselves on buttondown,
  3455. // the window can get a strange sequence of focus messages: first
  3456. // set, then kill, and then set again. since these are not really
  3457. // focus changes, ignore them and only handle "real" cases.
  3458. if (!plv || hwndNewFocus == plv->ci.hwnd)
  3459. return;
  3460. plv->flags &= ~(LVF_FOCUSED|LVF_UNFOLDED);
  3461. // Blow this off if we are not currently visible (being destroyed!)
  3462. if (IsWindowVisible(plv->ci.hwnd))
  3463. {
  3464. if (plv->iFocus != -1)
  3465. {
  3466. UINT fRedraw = RDW_INVALIDATE;
  3467. if (plv->clrTextBk == CLR_NONE)
  3468. fRedraw |= RDW_ERASE;
  3469. ListView_InvalidateFoldedItem( plv, plv->iFocus, TRUE, fRedraw );
  3470. }
  3471. ListView_RedrawSelection(plv);
  3472. }
  3473. // Let the parent window know that we are losing the focus.
  3474. CCSendNotify(&plv->ci, NM_KILLFOCUS, NULL);
  3475. IncrementSearchString(&plv->is, 0, NULL);
  3476. }
  3477. void NEAR ListView_DeselectAll(LV* plv, int iDontDeselect)
  3478. {
  3479. int i = -1;
  3480. int nSkipped = 0;
  3481. BOOL fWasSelected = FALSE;
  3482. if (iDontDeselect != -1) {
  3483. if (ListView_OnGetItemState(plv, iDontDeselect, LVIS_SELECTED))
  3484. fWasSelected = TRUE;
  3485. }
  3486. if (ListView_IsOwnerData(plv)) {
  3487. // if there's only one item selected, and that item is the iDontDeselect
  3488. // then our work is done...
  3489. plv->plvrangeSel->lpVtbl->CountIncluded(plv->plvrangeSel, &plv->nSelected);
  3490. if (plv->nSelected == 1 && fWasSelected)
  3491. return;
  3492. ListView_InvalidateSelectedOrCutOwnerData(plv, plv->plvrangeSel);
  3493. ListView_OnSetItemState(plv, -1, 0, LVIS_SELECTED);
  3494. if (fWasSelected) {
  3495. ListView_OnSetItemState(plv, iDontDeselect, LVIS_SELECTED, LVIS_SELECTED);
  3496. nSkipped = 1;
  3497. }
  3498. } else {
  3499. if (iDontDeselect != plv->iFocus) {
  3500. ListView_OnSetItemState(plv, plv->iFocus, 0, LVIS_SELECTED);
  3501. }
  3502. while ((plv->nSelected - nSkipped) && (i = ListView_OnGetNextItem(plv, i, LVNI_SELECTED)) != -1) {
  3503. if (i != iDontDeselect) {
  3504. ListView_OnSetItemState(plv, i, 0, LVIS_SELECTED);
  3505. } else {
  3506. if (fWasSelected) {
  3507. nSkipped++;
  3508. }
  3509. }
  3510. }
  3511. }
  3512. ASSERT((plv->nSelected - nSkipped) == 0);
  3513. plv->nSelected = nSkipped;
  3514. }
  3515. // toggle the selection state of an item
  3516. void NEAR ListView_ToggleSelection(LV* plv, int iItem)
  3517. {
  3518. UINT cur_state;
  3519. if (iItem != -1) {
  3520. cur_state = ListView_OnGetItemState(plv, iItem, LVIS_SELECTED);
  3521. ListView_OnSetItemState(plv, iItem, cur_state ^ LVIS_SELECTED, LVIS_SELECTED);
  3522. }
  3523. }
  3524. // Selects (or toggles) a range of items in the list.
  3525. // The curent iFocus is the starting location
  3526. // iItem - is the ending item
  3527. // fToggle - Well set all of the selection state of all of the items to
  3528. // inverse the starting location
  3529. //
  3530. void NEAR ListView_SelectRangeTo(LV* plv, int iItem, BOOL fResetRest)
  3531. {
  3532. int iMin, iMax;
  3533. int i = -1;
  3534. UINT uSelVal = LVIS_SELECTED;
  3535. if (plv->iMark == -1)
  3536. {
  3537. ListView_SetFocusSel(plv, iItem, TRUE, TRUE, FALSE);
  3538. return;
  3539. }
  3540. if (!fResetRest)
  3541. uSelVal = ListView_OnGetItemState(plv, plv->iMark, LVIS_SELECTED);
  3542. // If we are in report view or list view we simply walk through the
  3543. // indexes to see which items to select or deselect. otherwise it
  3544. // is is based off of the location of the objects being within the
  3545. // rectangle that is defined by
  3546. if (ListView_IsListView(plv) || ListView_IsReportView(plv))
  3547. {
  3548. iMin = min(iItem, plv->iMark);
  3549. iMax = max(iItem, plv->iMark);
  3550. if (ListView_IsOwnerData( plv )) {
  3551. if (fResetRest)
  3552. {
  3553. ListView_DeselectAll( plv, -1 );
  3554. }
  3555. if (iMax > iMin)
  3556. {
  3557. if (LVIS_SELECTED & uSelVal)
  3558. {
  3559. if (FAILED(plv->plvrangeSel->lpVtbl->IncludeRange(plv->plvrangeSel, iMin, iMax )))
  3560. return;
  3561. }
  3562. else
  3563. {
  3564. if (FAILED(plv->plvrangeSel->lpVtbl->ExcludeRange(plv->plvrangeSel, iMin, iMax )))
  3565. return;
  3566. }
  3567. ListView_SendODChangeAndInvalidate(plv, iMin, iMax, uSelVal ^ LVIS_SELECTED, uSelVal);
  3568. }
  3569. else
  3570. {
  3571. ListView_OnSetItemState(plv, iMin, uSelVal, LVIS_SELECTED);
  3572. }
  3573. }
  3574. else
  3575. {
  3576. if (fResetRest)
  3577. {
  3578. while ((i = ListView_OnGetNextItem(plv, i, LVNI_SELECTED)) != -1)
  3579. {
  3580. if (i < iMin || i > iMax)
  3581. ListView_OnSetItemState(plv, i, 0, LVIS_SELECTED);
  3582. }
  3583. }
  3584. while (iMin <= iMax)
  3585. {
  3586. ListView_OnSetItemState(plv, iMin, uSelVal, LVIS_SELECTED);
  3587. iMin++;
  3588. }
  3589. }
  3590. }
  3591. else
  3592. {
  3593. // Iconic views first calculate the bounding rectangle of the two
  3594. // items.
  3595. RECT rcTemp;
  3596. RECT rcTemp2;
  3597. RECT rcBounding;
  3598. POINT pt; //
  3599. ListView_GetRects(plv, plv->iMark, NULL, NULL, NULL, &rcTemp);
  3600. ListView_GetRects(plv, iItem, NULL, NULL, NULL, &rcTemp2);
  3601. UnionRect(&rcBounding, &rcTemp, &rcTemp2);
  3602. // since ownerdata icon views are always arranged, we can assume that
  3603. // all items are in order and we can search only those between the
  3604. // indexes found using the bouding rect
  3605. //
  3606. if (ListView_IsOwnerData( plv ))
  3607. {
  3608. ListView_CalcMinMaxIndex( plv, &rcBounding, &iMin, &iMax );
  3609. if (fResetRest)
  3610. {
  3611. ListView_DeselectAll( plv, -1 );
  3612. }
  3613. iMax = min( iMax, ListView_Count( plv ) );
  3614. iMin = max( iMin, 0 );
  3615. }
  3616. else
  3617. {
  3618. iMax = ListView_Count(plv);
  3619. iMin = 0;
  3620. }
  3621. if (ListView_IsOwnerData(plv) && (iMax > iMin))
  3622. {
  3623. if (LVIS_SELECTED & uSelVal)
  3624. {
  3625. if (FAILED(plv->plvrangeSel->lpVtbl->IncludeRange(plv->plvrangeSel, iMin, iMax - 1 )))
  3626. return;
  3627. }
  3628. else
  3629. {
  3630. if (FAILED(plv->plvrangeSel->lpVtbl->ExcludeRange(plv->plvrangeSel, iMin, iMax - 1 )))
  3631. return;
  3632. }
  3633. ListView_SendODChangeAndInvalidate(plv, iMin, iMax, uSelVal ^ LVIS_SELECTED, uSelVal);
  3634. } else {
  3635. for (i = iMin; i < iMax; i++)
  3636. {
  3637. ListView_GetRects(plv, i, NULL, NULL, NULL, &rcTemp2);
  3638. pt.x = (rcTemp2.right + rcTemp2.left) / 2; // center of item
  3639. pt.y = (rcTemp2.bottom + rcTemp2.top) / 2;
  3640. if (PtInRect(&rcBounding, pt))
  3641. {
  3642. int iZ;
  3643. if (!ListView_IsOwnerData( plv ))
  3644. {
  3645. iZ = ListView_ZOrderIndex(plv, i);
  3646. if (iZ > 0)
  3647. DPA_InsertPtr(plv->hdpaZOrder, 0, DPA_DeletePtr(plv->hdpaZOrder, iZ));
  3648. }
  3649. ListView_OnSetItemState(plv, i, uSelVal, LVIS_SELECTED);
  3650. }
  3651. else if (fResetRest)
  3652. ListView_OnSetItemState(plv, i, 0, LVIS_SELECTED);
  3653. }
  3654. }
  3655. }
  3656. }
  3657. // makes an item the focused item and optionally selects it
  3658. //
  3659. // in:
  3660. // iItem item to get the focus
  3661. // fSelectAlso select this item as well as set it as the focus
  3662. // fDeselectAll deselect all items first
  3663. // fToggleSel toggle the selection state of the item
  3664. //
  3665. // returns:
  3666. // index of focus item (if focus change was refused)
  3667. // Bugbug::this is getting to have a lot of parameters
  3668. int NEAR ListView_SetFocusSel(LV* plv, int iItem, BOOL fSelectAlso,
  3669. BOOL fDeselectAll, BOOL fToggleSel)
  3670. {
  3671. int iFocus = plv->iFocus;
  3672. // if we're single sel mode, don't bother with this because
  3673. // the set item will do it for us
  3674. if (!(plv->ci.style & LVS_SINGLESEL) && (fDeselectAll))
  3675. ListView_DeselectAll(plv, -1);
  3676. if (iItem != plv->iFocus)
  3677. {
  3678. // remove the old focus
  3679. if (plv->iFocus != -1)
  3680. {
  3681. // If he refuses to give up the focus, bail out.
  3682. if (!ListView_OnSetItemState(plv, plv->iFocus, 0, LVIS_FOCUSED))
  3683. return plv->iFocus;
  3684. }
  3685. }
  3686. if (!ListView_IsOwnerData( plv )) {
  3687. if (fSelectAlso)
  3688. {
  3689. if (ListView_IsIconView(plv) || ListView_IsSmallView(plv))
  3690. {
  3691. int iZ = ListView_ZOrderIndex(plv, iItem);
  3692. if (iZ > 0)
  3693. DPA_InsertPtr(plv->hdpaZOrder, 0, DPA_DeletePtr(plv->hdpaZOrder, iZ));
  3694. }
  3695. }
  3696. }
  3697. /* Ensure that when moving focus that we refresh the previous focus
  3698. owner properly. */
  3699. if (iFocus != -1 && iFocus != plv->iFocus && ( plv->flags & LVF_UNFOLDED ) )
  3700. ListView_InvalidateFoldedItem( plv, iFocus, FALSE, RDW_INVALIDATE );
  3701. if (plv->iMark == -1)
  3702. plv->iMark = iItem;
  3703. SetTimer(plv->ci.hwnd, IDT_SCROLLWAIT, GetDoubleClickTime(), NULL);
  3704. plv->flags |= LVF_SCROLLWAIT;
  3705. if (fToggleSel)
  3706. {
  3707. ListView_ToggleSelection(plv, iItem);
  3708. ListView_OnSetItemState(plv, iItem, LVIS_FOCUSED, LVIS_FOCUSED);
  3709. }
  3710. else
  3711. {
  3712. UINT flags;
  3713. flags = ((fSelectAlso || plv->ci.style & LVS_SINGLESEL) ?
  3714. (LVIS_SELECTED | LVIS_FOCUSED) : LVIS_FOCUSED);
  3715. ListView_OnSetItemState(plv, iItem, flags, flags);
  3716. }
  3717. return iItem;
  3718. }
  3719. UINT GetLVKeyFlags()
  3720. {
  3721. UINT uFlags = 0;
  3722. if (GetKeyState(VK_MENU) < 0)
  3723. uFlags |= LVKF_ALT;
  3724. if (GetKeyState(VK_CONTROL) < 0)
  3725. uFlags |= LVKF_CONTROL;
  3726. if (GetKeyState(VK_SHIFT) < 0)
  3727. uFlags |= LVKF_SHIFT;
  3728. return uFlags;
  3729. }
  3730. void NEAR ListView_OnKey(LV* plv, UINT vk, BOOL fDown, int cRepeat, UINT flags)
  3731. {
  3732. UINT lvni = 0;
  3733. int iNewFocus;
  3734. BOOL fCtlDown;
  3735. BOOL fShiftDown;
  3736. LV_KEYDOWN nm;
  3737. HWND hwnd = plv->ci.hwnd;
  3738. if (!fDown)
  3739. return;
  3740. // Swap the left and right arrow key if the control is mirrored.
  3741. vk = RTLSwapLeftRightArrows(&plv->ci, vk);
  3742. //prevent any change in selected items before the dbl click timer goes off
  3743. //so that we don't launch wrong item(s)
  3744. if (plv->exStyle & LVS_EX_ONECLICKACTIVATE && plv->fOneClickHappened && plv->fOneClickOK)
  3745. {
  3746. //if a key is pressed with a mouse click with one click activate and double click
  3747. //timer, we end up setting up a timer and then processing the keydown
  3748. //this causes an item to be launched right away (from this code) and in case
  3749. //of return being pressed it causes double activation
  3750. //prevent these cases:
  3751. if (vk == VK_SHIFT || vk == VK_CONTROL || vk == VK_MENU || vk == VK_RETURN)
  3752. return;
  3753. KillTimer(plv->ci.hwnd, IDT_ONECLICKHAPPENED);
  3754. plv->fOneClickHappened = FALSE;
  3755. CCSendNotify(&plv->ci, LVN_ITEMACTIVATE, &(plv->nmOneClickHappened.hdr));
  3756. if (!IsWindow(hwnd))
  3757. return;
  3758. }
  3759. // Notify
  3760. nm.wVKey = (WORD) vk;
  3761. nm.flags = flags;
  3762. if (CCSendNotify(&plv->ci, LVN_KEYDOWN, &nm.hdr)) {
  3763. plv->iPuntChar++;
  3764. return;
  3765. } else if (plv->iPuntChar) {
  3766. // this is tricky... if we want to punt the char, just increment the
  3767. // count. if we do NOT, then we must clear the queue of WM_CHAR's
  3768. // this is to preserve the iPuntChar to mean "punt the next n WM_CHAR messages
  3769. MSG msg;
  3770. while(plv->iPuntChar && PeekMessage(&msg, plv->ci.hwnd, WM_CHAR, WM_CHAR, PM_REMOVE)) {
  3771. plv->iPuntChar--;
  3772. }
  3773. ASSERT(!plv->iPuntChar);
  3774. }
  3775. if (ListView_Count(plv) == 0) // don't blow up on empty list
  3776. return;
  3777. fCtlDown = GetKeyState(VK_CONTROL) < 0;
  3778. fShiftDown = GetKeyState(VK_SHIFT) < 0;
  3779. switch (vk)
  3780. {
  3781. case VK_SPACE:
  3782. #ifdef DEBUG
  3783. if (fCtlDown && fShiftDown) {
  3784. SendMessage(plv->ci.hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
  3785. (SendMessage(plv->ci.hwnd, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0) ^ LVS_EX_GRIDLINES) | LVS_EX_CHECKBOXES);
  3786. }
  3787. #endif
  3788. // If shift (extend) or control (disjoint) select,
  3789. // then toggle selection state of focused item.
  3790. if (fCtlDown)
  3791. {
  3792. plv->iMark = plv->iFocus;
  3793. ListView_ToggleSelection(plv, plv->iFocus);
  3794. plv->iPuntChar++;
  3795. }
  3796. // BUGBUG: Implement me
  3797. if ( fShiftDown) {
  3798. ListView_SelectRangeTo(plv, plv->iFocus, TRUE);
  3799. }
  3800. if (ListView_CheckBoxes(plv)) {
  3801. if (plv->iFocus != -1)
  3802. ListView_HandleStateIconClick(plv, plv->iFocus);
  3803. }
  3804. //notify of navigation key usage
  3805. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  3806. return;
  3807. case VK_RETURN:
  3808. #ifdef DEBUG
  3809. if (fCtlDown && fShiftDown) {
  3810. if (plv->iFocus != -1) {
  3811. LV_ITEM lvi;
  3812. lvi.iSubItem = 1;
  3813. lvi.iItem = plv->iFocus;
  3814. lvi.iImage = 3;
  3815. lvi.state = LVIS_SELECTED;
  3816. lvi.stateMask = LVIS_SELECTED;
  3817. lvi.mask = LVIF_STATE | LVIF_IMAGE;
  3818. SendMessage(plv->ci.hwnd, LVM_SETITEM, 0, (LPARAM)&lvi);
  3819. }
  3820. return;
  3821. }
  3822. #endif
  3823. CCSendNotify(&plv->ci, NM_RETURN, NULL);
  3824. /// some (comdlg32 for example) destroy on double click
  3825. // we need to bail if that happens because plv is no longer valid
  3826. if (!IsWindow(hwnd))
  3827. return;
  3828. {
  3829. NMITEMACTIVATE nm;
  3830. nm.iItem = plv->iFocus;
  3831. nm.iSubItem = 0;
  3832. nm.uChanged = 0;
  3833. nm.ptAction.x = -1;
  3834. nm.ptAction.y = -1;
  3835. nm.uKeyFlags = GetLVKeyFlags();
  3836. CCSendNotify(&plv->ci, LVN_ITEMACTIVATE, &nm.hdr);
  3837. if (!IsWindow(hwnd))
  3838. return;
  3839. }
  3840. //notify of navigation key usage
  3841. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  3842. return;
  3843. case VK_ADD:
  3844. if (ListView_IsReportView(plv) && (GetKeyState(VK_CONTROL) < 0))
  3845. {
  3846. HCURSOR hcurPrev;
  3847. int i;
  3848. hcurPrev = SetCursor(LoadCursor(NULL, IDC_WAIT));
  3849. for (i=0; i < plv->cCol; i++)
  3850. {
  3851. ListView_RSetColumnWidth(plv, i, -1);
  3852. }
  3853. SetCursor(hcurPrev);
  3854. //notify of navigation key usage
  3855. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  3856. return;
  3857. }
  3858. }
  3859. if (GetKeyState(VK_MENU) < 0)
  3860. return;
  3861. // For a single selection listview, disable extending the selection
  3862. // by turning off the keyboard modifiers.
  3863. if (plv->ci.style & LVS_SINGLESEL) {
  3864. fCtlDown = FALSE;
  3865. fShiftDown = FALSE;
  3866. }
  3867. //
  3868. // Let the Arrow function attempt to process the key.
  3869. //
  3870. iNewFocus = ListView_Arrow(plv, plv->iFocus, vk);
  3871. // If control (disjoint) selection, don't change selection.
  3872. // If shift (extend) or control selection, don't deselect all.
  3873. //
  3874. if (iNewFocus != -1) {
  3875. if (fShiftDown)
  3876. {
  3877. ListView_SelectRangeTo(plv, iNewFocus, TRUE);
  3878. ListView_SetFocusSel(plv, iNewFocus, FALSE, FALSE, FALSE);
  3879. }
  3880. else {
  3881. if (!fCtlDown)
  3882. plv->iMark = iNewFocus;
  3883. ListView_SetFocusSel(plv, iNewFocus, !fCtlDown, !fShiftDown && !fCtlDown, FALSE);
  3884. }
  3885. IncrementSearchString(&plv->is, 0, NULL);
  3886. CCPlaySound(c_szSelect);
  3887. }
  3888. // on keyboard movement, scroll immediately.
  3889. if (ListView_CancelScrollWait(plv)) {
  3890. ListView_OnEnsureVisible(plv, plv->iFocus, FALSE);
  3891. UpdateWindow(plv->ci.hwnd);
  3892. }
  3893. //notify of navigation key usage
  3894. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  3895. }
  3896. //
  3897. // LVN_INCREMENTALSEARCH gives the app the opportunity to customize
  3898. // incremental search. For example, if the items are numeric,
  3899. // the app can do numerical search instead of string search.
  3900. //
  3901. // App sets pnmfi->lvfi.lParam to the result of the incremental search,
  3902. // or to -2 to fai the search and just beep.
  3903. //
  3904. // App can return 2 to indicate that all processing should stop, if
  3905. // app wants to take over incremental search completely.
  3906. //
  3907. BOOL ListView_IncrementalSearch(LV *plv, int iStartFrom, LPNMLVFINDITEM pnmfi, int *pi)
  3908. {
  3909. INT_PTR fRc;
  3910. ASSERT(!(pnmfi->lvfi.flags & LVFI_PARAM));
  3911. pnmfi->lvfi.lParam = -1;
  3912. fRc = CCSendNotify(&plv->ci, LVN_INCREMENTALSEARCH, &pnmfi->hdr);
  3913. *pi = (int)pnmfi->lvfi.lParam;
  3914. // Cannot just return fRc because some apps return 1 to all WM_NOTIFY's
  3915. return fRc == 2;
  3916. }
  3917. #if defined(FE_IME)
  3918. // Now only Korean version is interested in incremental search with composition string.
  3919. LPTSTR GET_COMP_STRING(HIMC hImc, DWORD dwFlags)
  3920. {
  3921. LONG iNumComp;
  3922. PTSTR pszCompStr;
  3923. iNumComp = ImmGetCompositionString(hImc, dwFlags, NULL, 0);
  3924. pszCompStr = (PTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(iNumComp+1));
  3925. if (pszCompStr)
  3926. {
  3927. if (iNumComp)
  3928. ImmGetCompositionString(hImc, dwFlags, pszCompStr, iNumComp+1);
  3929. pszCompStr[iNumComp] = TEXT('\0');
  3930. }
  3931. return pszCompStr;
  3932. }
  3933. #define FREE_COMP_STRING(pszCompStr) LocalFree((HLOCAL)(pszCompStr))
  3934. BOOL NEAR ListView_OnImeComposition(LV* plv, WPARAM wParam, LPARAM lParam)
  3935. {
  3936. LPTSTR lpsz;
  3937. NMLVFINDITEM nmfi;
  3938. int i;
  3939. int iStartFrom = -1;
  3940. int iLen;
  3941. int iCount;
  3942. HIMC hImc;
  3943. TCHAR *pszCompStr;
  3944. BOOL fRet = TRUE;
  3945. iCount = ListView_Count(plv);
  3946. if (!iCount || plv->iFocus == -1)
  3947. return fRet;
  3948. if (hImc = ImmGetContext(plv->ci.hwnd))
  3949. {
  3950. if (lParam & GCS_RESULTSTR)
  3951. {
  3952. fRet = FALSE;
  3953. pszCompStr = GET_COMP_STRING(hImc, GCS_RESULTSTR);
  3954. if (pszCompStr)
  3955. {
  3956. IncrementSearchImeCompStr(&plv->is, FALSE, pszCompStr, &lpsz);
  3957. FREE_COMP_STRING(pszCompStr);
  3958. }
  3959. }
  3960. if (lParam & GCS_COMPSTR)
  3961. {
  3962. fRet = TRUE;
  3963. pszCompStr = GET_COMP_STRING(hImc, GCS_COMPSTR);
  3964. if (pszCompStr)
  3965. {
  3966. if (IncrementSearchImeCompStr(&plv->is, TRUE, pszCompStr, &lpsz))
  3967. iStartFrom = plv->iFocus;
  3968. else
  3969. iStartFrom = ((plv->iFocus - 1) + iCount)% iCount;
  3970. nmfi.lvfi.flags = LVFI_SUBSTRING | LVFI_STRING | LVFI_WRAP;
  3971. nmfi.lvfi.psz = lpsz;
  3972. iLen = lstrlen(lpsz);
  3973. // special case space as the first character
  3974. if ((iLen == 1) && (*lpsz == TEXT(' '))) {
  3975. if (plv->iFocus != -1) {
  3976. ListView_OnSetItemState(plv, plv->iFocus, LVIS_SELECTED, LVIS_SELECTED);
  3977. IncrementSearchString(&plv->is, 0, NULL);
  3978. }
  3979. //notify of navigation key usage
  3980. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  3981. return fRet;
  3982. }
  3983. // Give caller full string in case they want to do something custom
  3984. if (ListView_IncrementalSearch(plv, iStartFrom, &nmfi, &i))
  3985. return fRet;
  3986. if (iLen > 0 && SameChars(lpsz, lpsz[0])) {
  3987. //
  3988. // The user has been typing the same char over and over again.
  3989. // Switch from incremental search to Windows 3.1 style search.
  3990. //
  3991. iStartFrom = plv->iFocus;
  3992. nmfi.lvfi.psz = lpsz + iLen - 1;
  3993. }
  3994. if (i == -1)
  3995. i = ListView_OnFindItem(plv, iStartFrom, &nmfi.lvfi);
  3996. if (!ListView_IsValidItemNumber(plv, i)) {
  3997. i = -1;
  3998. }
  3999. #ifdef LVDEBUG
  4000. DebugMsg(TF_LISTVIEW, TEXT("CIme listsearch %d %s %d"), (LPTSTR)lpsz, (LPTSTR)lpsz, i);
  4001. #endif
  4002. if (i != -1) {
  4003. ListView_SetFocusSel(plv, i, TRUE, TRUE, FALSE);
  4004. plv->iMark = i;
  4005. if (ListView_CancelScrollWait(plv))
  4006. ListView_OnEnsureVisible(plv, i, FALSE);
  4007. } else {
  4008. // Don't beep on spaces, we use it for selection.
  4009. IncrementSearchBeep(&plv->is);
  4010. }
  4011. //notify of navigation key usage
  4012. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  4013. FREE_COMP_STRING(pszCompStr);
  4014. }
  4015. }
  4016. ImmReleaseContext(plv->ci.hwnd, hImc);
  4017. }
  4018. return fRet;
  4019. }
  4020. #endif FE_IME
  4021. // REVIEW: We will want to reset ichCharBuf to 0 on certain conditions,
  4022. // such as: focus change, ENTER, arrow key, mouse click, etc.
  4023. //
  4024. void NEAR ListView_OnChar(LV* plv, UINT ch, int cRepeat)
  4025. {
  4026. LPTSTR lpsz;
  4027. NMLVFINDITEM nmfi;
  4028. int i;
  4029. int iStartFrom = -1;
  4030. int iLen;
  4031. int iCount;
  4032. iCount = ListView_Count(plv);
  4033. if (!iCount || plv->iFocus == -1)
  4034. return;
  4035. // Don't search for chars that cannot be in a file name (like ENTER and TAB)
  4036. // BUGBUG raymondc fix for v6: The Polish keyboard layout uses CTRL+ALT to
  4037. // enter some normal letters, so don't punt if the CTRL key is down or
  4038. // people in Poland are in trouble!
  4039. if (ch < TEXT(' ') || GetKeyState(VK_CONTROL) < 0)
  4040. {
  4041. IncrementSearchString(&plv->is, 0, NULL);
  4042. return;
  4043. }
  4044. if (IncrementSearchString(&plv->is, ch, &lpsz))
  4045. iStartFrom = plv->iFocus;
  4046. else
  4047. iStartFrom = ((plv->iFocus - 1) + iCount)% iCount;
  4048. nmfi.lvfi.flags = LVFI_SUBSTRING | LVFI_STRING | LVFI_WRAP;
  4049. nmfi.lvfi.psz = lpsz;
  4050. iLen = lstrlen(lpsz);
  4051. // special case space as the first character
  4052. if ((iLen == 1) && (*lpsz == ' ')) {
  4053. if (plv->iFocus != -1) {
  4054. ListView_OnSetItemState(plv, plv->iFocus, LVIS_SELECTED, LVIS_SELECTED);
  4055. IncrementSearchString(&plv->is, 0, NULL);
  4056. }
  4057. //notify of navigation key usage
  4058. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  4059. return;
  4060. }
  4061. // Give caller full string in case they want to do something custom
  4062. if (ListView_IncrementalSearch(plv, iStartFrom, &nmfi, &i))
  4063. return;
  4064. if (iLen > 0 && SameChars(lpsz, lpsz[0])) {
  4065. //
  4066. // The user has been typing the same char over and over again.
  4067. // Switch from incremental search to Windows 3.1 style search.
  4068. //
  4069. iStartFrom = plv->iFocus;
  4070. nmfi.lvfi.psz = lpsz + iLen - 1;
  4071. }
  4072. if (i == -1)
  4073. i = ListView_OnFindItem(plv, iStartFrom, &nmfi.lvfi);
  4074. if (!ListView_IsValidItemNumber(plv, i)) {
  4075. i = -1;
  4076. }
  4077. #ifdef LVDEBUG
  4078. DebugMsg(TF_LISTVIEW, TEXT("listsearch %d %s %d"), (LPTSTR)lpsz, (LPTSTR)lpsz, i);
  4079. #endif
  4080. if (i != -1) {
  4081. ListView_SetFocusSel(plv, i, TRUE, TRUE, FALSE);
  4082. plv->iMark = i;
  4083. if (ListView_CancelScrollWait(plv))
  4084. ListView_OnEnsureVisible(plv, i, FALSE);
  4085. } else {
  4086. // Don't beep on spaces, we use it for selection.
  4087. IncrementSearchBeep(&plv->is);
  4088. }
  4089. //notify of navigation key usage
  4090. CCNotifyNavigationKeyUsage(&(plv->ci), UISF_HIDEFOCUS);
  4091. }
  4092. BOOL FAR PASCAL SameChars(LPTSTR lpsz, TCHAR c)
  4093. {
  4094. while (*lpsz) {
  4095. if (*lpsz++ != c)
  4096. return FALSE;
  4097. }
  4098. return TRUE;
  4099. }
  4100. UINT NEAR ListView_OnGetDlgCode(LV* plv, MSG FAR* lpmsg)
  4101. {
  4102. return DLGC_WANTARROWS | DLGC_WANTCHARS;
  4103. }
  4104. int ListView_ComputeCYItemSize(LV* plv)
  4105. {
  4106. int cy;
  4107. cy = max(plv->cyLabelChar, plv->cySmIcon);
  4108. if (plv->himlState)
  4109. cy = max(cy, plv->cyState);
  4110. cy += g_cyBorder;
  4111. ASSERT(cy);
  4112. return cy;
  4113. }
  4114. void NEAR ListView_InvalidateCachedLabelSizes(LV* plv)
  4115. {
  4116. int i;
  4117. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  4118. // Label wrapping has changed, so we need to invalidate the
  4119. // size of the items, such that they will be recomputed.
  4120. //
  4121. if (!ListView_IsOwnerData( plv ))
  4122. {
  4123. for (i = ListView_Count(plv) - 1; i >= 0; i--)
  4124. {
  4125. LISTITEM FAR* pitem = ListView_FastGetItemPtr(plv, i);
  4126. ListView_SetSRecompute(pitem);
  4127. }
  4128. }
  4129. plv->rcView.left = RECOMPUTE;
  4130. if ((plv->ci.style & LVS_OWNERDRAWFIXED) && ListView_IsReportView(plv))
  4131. plv->cyItemSave = ListView_ComputeCYItemSize(plv);
  4132. else {
  4133. plv->cyItem = ListView_ComputeCYItemSize(plv);
  4134. }
  4135. }
  4136. int LV_GetNewColWidth(LV* plv, int iFirst, int iLast);
  4137. void NEAR ListView_OnStyleChanging(LV* plv, UINT gwl, LPSTYLESTRUCT pinfo)
  4138. {
  4139. if (gwl == GWL_STYLE) {
  4140. // Don't allow LVS_OWNERDATA to change after creation
  4141. DWORD stylePreserve = LVS_OWNERDATA;
  4142. // Don't allow a LVS_EX_REGIONAL listview to change type, since
  4143. // it must be LVS_ICON
  4144. if (plv->exStyle & LVS_EX_REGIONAL)
  4145. stylePreserve |= LVS_TYPEMASK;
  4146. // Preserve the bits that must be preserved
  4147. pinfo->styleNew ^= (pinfo->styleNew ^ pinfo->styleOld) & stylePreserve;
  4148. }
  4149. }
  4150. void NEAR ListView_OnStyleChanged(LV* plv, UINT gwl, LPSTYLESTRUCT pinfo)
  4151. {
  4152. // Style changed: redraw everything...
  4153. //
  4154. // try to do this smartly, avoiding unnecessary redraws
  4155. if (gwl == GWL_STYLE)
  4156. {
  4157. BOOL fRedraw = FALSE, fShouldScroll = FALSE;
  4158. DWORD changeFlags, styleOld;
  4159. ListView_DismissEdit(plv, FALSE); // BUGBUG: FALSE == apply edits. Is this correct?
  4160. changeFlags = plv->ci.style ^ pinfo->styleNew;
  4161. styleOld = plv->ci.style;
  4162. // (dli) Setting the small icon width here and only in the case when we go
  4163. // from large icon view to some other view because of three reasons:
  4164. // 1. According to chee, we want to set this before we change the style bit in
  4165. // plv or after we scale.
  4166. // 2. We don't want to do it after we scale because we want to set the width to
  4167. // the maximum value so that the items in this listview do not cover each other
  4168. // 3. we do it from large icon view because large icon view has fixed width for
  4169. // each item, small icon view width can be scaled.
  4170. //
  4171. if ((changeFlags & LVS_TYPEMASK) && ((styleOld & LVS_TYPEMASK) == LVS_ICON))
  4172. ListView_ISetColumnWidth(plv, 0,
  4173. LV_GetNewColWidth(plv, 0, ListView_Count(plv)-1), FALSE);
  4174. plv->ci.style = pinfo->styleNew; // change our version
  4175. if (changeFlags & (WS_BORDER | WS_CAPTION | WS_THICKFRAME)) {
  4176. // the changing of these bits affect the size of the window
  4177. // but not until after this message is handled
  4178. // so post ourself a message.
  4179. PostMessage(plv->ci.hwnd, LVMP_WINDOWPOSCHANGED, 0, 0);
  4180. }
  4181. if (changeFlags & LVS_NOCOLUMNHEADER) {
  4182. if (plv->hwndHdr) {
  4183. SetWindowBits(plv->hwndHdr, GWL_STYLE, HDS_HIDDEN,
  4184. (plv->ci.style & LVS_NOCOLUMNHEADER) ? HDS_HIDDEN : 0);
  4185. fRedraw = TRUE;
  4186. fShouldScroll = TRUE;
  4187. }
  4188. }
  4189. if (changeFlags & LVS_NOLABELWRAP)
  4190. {
  4191. ListView_InvalidateCachedLabelSizes(plv);
  4192. fShouldScroll = TRUE;
  4193. fRedraw = TRUE;
  4194. }
  4195. if (changeFlags & LVS_TYPEMASK)
  4196. {
  4197. ListView_TypeChange(plv, styleOld);
  4198. fShouldScroll = TRUE;
  4199. fRedraw = TRUE;
  4200. }
  4201. if ((changeFlags & LVS_AUTOARRANGE) && (plv->ci.style & LVS_AUTOARRANGE))
  4202. {
  4203. ListView_OnArrange(plv, LVA_DEFAULT);
  4204. fRedraw = TRUE;
  4205. }
  4206. // bugbug, previously, this was the else to
  4207. // (changeFlags & LVS_AUTOARRANGE && (plv->ci.style & LVS_AUTOARRANGE))
  4208. // I'm not sure that was really the right thing..
  4209. if (fShouldScroll)
  4210. {
  4211. // Else we would like to make the most important item to still
  4212. // be visible. So first we will look for a cursorered item
  4213. // if this fails, we will look for the first selected item,
  4214. // else we will simply ask for the first item (assuming the
  4215. // count > 0
  4216. //
  4217. int i;
  4218. // And make sure the scrollbars are up to date Note this
  4219. // also updates some variables that some views need
  4220. ListView_UpdateScrollBars(plv);
  4221. i = (plv->iFocus >= 0) ? plv->iFocus : ListView_OnGetNextItem(plv, -1, LVNI_SELECTED);
  4222. if ((i == -1) && (ListView_Count(plv) > 0))
  4223. i = 0;
  4224. if (i != -1)
  4225. ListView_OnEnsureVisible(plv, i, TRUE);
  4226. }
  4227. if (fRedraw)
  4228. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  4229. }
  4230. else if (gwl == GWL_EXSTYLE)
  4231. {
  4232. //
  4233. // If the RTL_MIRROR extended style bit had changed, let's
  4234. // repaint the control window.
  4235. //
  4236. if ((plv->ci.dwExStyle&RTL_MIRRORED_WINDOW) != (pinfo->styleNew&RTL_MIRRORED_WINDOW))
  4237. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  4238. //
  4239. // Save the new ex-style bits
  4240. //
  4241. plv->ci.dwExStyle = pinfo->styleNew;
  4242. }
  4243. // Change of styles also changes tooltip policy, so pop it
  4244. ListView_PopBubble(plv);
  4245. }
  4246. void NEAR ListView_TypeChange(LV* plv, DWORD styleOld)
  4247. {
  4248. RECT rc;
  4249. //
  4250. // Invalidate all cached string metrics because customdraw clients
  4251. // may draw differently depending on the type. This happens more
  4252. // often than you might think, not on purpose, but because apps are
  4253. // buggy.
  4254. //
  4255. // APP COMPAT! You'd think this was completely safe. After all,
  4256. // all we're doing is invalidating our cache so we ask the parent
  4257. // afresh the next time we need the strings. But noooooooo,
  4258. // Outlook98 will FAULT if you ask it for information that it thinks
  4259. // you by all rights already know. Sigh. So guard this with a v5.
  4260. //
  4261. if (plv->ci.iVersion >= 5 && !ListView_IsOwnerData(plv))
  4262. {
  4263. int i;
  4264. for (i = 0; i < ListView_Count(plv); i++)
  4265. {
  4266. LISTITEM *pitem = ListView_FastGetItemPtr(plv, i);
  4267. ListView_SetSRecompute(pitem);
  4268. }
  4269. }
  4270. switch (styleOld & LVS_TYPEMASK)
  4271. {
  4272. case LVS_REPORT:
  4273. ShowWindow(plv->hwndHdr, SW_HIDE);
  4274. if (styleOld & LVS_OWNERDRAWFIXED) {
  4275. // swap cyItem and cyFixed;
  4276. int temp = plv->cyItem;
  4277. plv->cyItem = plv->cyItemSave;
  4278. plv->cyItemSave = temp;
  4279. }
  4280. break;
  4281. default:
  4282. break;
  4283. }
  4284. // Now handle any special setup needed for the new view
  4285. switch (plv->ci.style & LVS_TYPEMASK)
  4286. {
  4287. case (UINT)LVS_ICON:
  4288. ListView_ScaleIconPositions(plv, FALSE);
  4289. break;
  4290. case (UINT)LVS_SMALLICON:
  4291. ListView_ScaleIconPositions(plv, TRUE);
  4292. break;
  4293. case (UINT)LVS_LIST:
  4294. // We may need to resize the columns
  4295. ListView_MaybeResizeListColumns(plv, 0, ListView_Count(plv)-1);
  4296. break;
  4297. case (UINT)LVS_REPORT:
  4298. // if it's owner draw fixed, we may have to do funky stuff
  4299. if ((styleOld & LVS_TYPEMASK) != LVS_REPORT) {
  4300. plv->cyItemSave = plv->cyItem;
  4301. }
  4302. ListView_RInitialize(plv, FALSE);
  4303. break;
  4304. default:
  4305. break;
  4306. }
  4307. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  4308. GetClientRect(plv->ci.hwnd, &rc);
  4309. plv->sizeClient.cx = rc.right;
  4310. plv->sizeClient.cy = rc.bottom;
  4311. }
  4312. int NEAR ListView_OnHitTest(LV* plv, LV_HITTESTINFO FAR* pinfo)
  4313. {
  4314. UINT flags;
  4315. int x, y;
  4316. if (!pinfo) return -1;
  4317. x = pinfo->pt.x;
  4318. y = pinfo->pt.y;
  4319. pinfo->iItem = -1;
  4320. flags = 0;
  4321. if (x < 0)
  4322. flags |= LVHT_TOLEFT;
  4323. else if (x >= plv->sizeClient.cx)
  4324. flags |= LVHT_TORIGHT;
  4325. if (y < 0)
  4326. flags |= LVHT_ABOVE;
  4327. else if (y >= plv->sizeClient.cy)
  4328. flags |= LVHT_BELOW;
  4329. if (flags == 0)
  4330. {
  4331. pinfo->iItem = _ListView_ItemHitTest(plv, x, y, &flags, NULL);
  4332. }
  4333. pinfo->flags = flags;
  4334. if (pinfo->iItem >= ListView_Count(plv)) {
  4335. pinfo->iItem = -1;
  4336. pinfo->flags = LVHT_NOWHERE;
  4337. }
  4338. return pinfo->iItem;
  4339. }
  4340. int NEAR ScrollAmount(int large, int iSmall, int unit)
  4341. {
  4342. return (((large - iSmall) + (unit - 1)) / unit) * unit;
  4343. }
  4344. // NOTE: this is duplicated in shell32.dll
  4345. //
  4346. // checks to see if we are at the end position of a scroll bar
  4347. // to avoid scrolling when not needed (avoid flashing)
  4348. //
  4349. // in:
  4350. // code SB_VERT or SB_HORZ
  4351. // bDown FALSE is up or left
  4352. // TRUE is down or right
  4353. BOOL NEAR PASCAL CanScroll(LV* plv, int code, BOOL bDown)
  4354. {
  4355. SCROLLINFO si;
  4356. si.cbSize = sizeof(SCROLLINFO);
  4357. si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
  4358. if (ListView_GetScrollInfo(plv, code, &si))
  4359. {
  4360. if (bDown)
  4361. {
  4362. if (si.nPage)
  4363. si.nMax -= si.nPage - 1;
  4364. return si.nPos < si.nMax;
  4365. }
  4366. else
  4367. {
  4368. return si.nPos > si.nMin;
  4369. }
  4370. }
  4371. else
  4372. {
  4373. return FALSE;
  4374. }
  4375. }
  4376. // detect if we should auto scroll the window
  4377. //
  4378. // in:
  4379. // pt cursor pos in hwnd's client coords
  4380. // out:
  4381. // pdx, pdy ammount scrolled in x and y
  4382. //
  4383. // REVIEW, this should make sure a certain amount of time has passed
  4384. // before scrolling.
  4385. void NEAR ScrollDetect(LV* plv, POINT pt, int FAR *pdx, int FAR *pdy)
  4386. {
  4387. int dx, dy;
  4388. *pdx = *pdy = 0;
  4389. if (!(plv->ci.style & (WS_HSCROLL | WS_VSCROLL)))
  4390. return;
  4391. dx = dy = plv->cyIcon / 16;
  4392. if (ListView_IsReportView(plv)) {
  4393. dy = plv->cyItem; // we scroll in units of items...
  4394. if (!dx)
  4395. dx = plv->cxSmIcon;
  4396. }
  4397. if (ListView_IsListView(plv))
  4398. dx = plv->cxItem;
  4399. if (!dx)
  4400. dx = 1;
  4401. if (!dy)
  4402. dy = 1;
  4403. // we need to check if we can scroll before acutally doing it
  4404. // since the selection rect is adjusted based on how much
  4405. // we scroll by
  4406. if (plv->ci.style & WS_VSCROLL) { // scroll vertically?
  4407. if (pt.y >= plv->sizeClient.cy) {
  4408. if (CanScroll(plv, SB_VERT, TRUE))
  4409. *pdy = ScrollAmount(pt.y, plv->sizeClient.cy, dy); // down
  4410. } else if (pt.y <= 0) {
  4411. if (CanScroll(plv, SB_VERT, FALSE))
  4412. *pdy = -ScrollAmount(0, pt.y, dy); // up
  4413. }
  4414. }
  4415. if (plv->ci.style & WS_HSCROLL) { // horizontally
  4416. if (pt.x >= plv->sizeClient.cx) {
  4417. if (CanScroll(plv, SB_HORZ, TRUE))
  4418. *pdx = ScrollAmount(pt.x, plv->sizeClient.cx, dx); // right
  4419. } else if (pt.x <= 0) {
  4420. if (CanScroll(plv, SB_HORZ, FALSE))
  4421. *pdx = -ScrollAmount(0, pt.x, dx); // left
  4422. }
  4423. }
  4424. // BUGBUG: this will potentially scroll outside the bounds of the
  4425. // listview. we should bound the scroll amount in CanScroll()
  4426. // or ScrollAmount().
  4427. if (*pdx || *pdy) {
  4428. ListView_ValidateScrollParams(plv, pdx, pdy);
  4429. }
  4430. }
  4431. #define swap(pi1, pi2) {int i = *(pi1) ; *(pi1) = *(pi2) ; *(pi2) = i ;}
  4432. void NEAR OrderRect(RECT FAR *prc)
  4433. {
  4434. if (prc->left > prc->right)
  4435. swap(&prc->left, &prc->right);
  4436. if (prc->bottom < prc->top)
  4437. swap(&prc->bottom, &prc->top);
  4438. }
  4439. // in:
  4440. // x, y starting point in client coords
  4441. #define SCROLL_FREQ (GetDoubleClickTime()/2) // 1/5 of a second between scrolls
  4442. //----------------------------------------------------------------------------
  4443. BOOL ShouldScroll(LV* plv, LPPOINT ppt, LPRECT lprc)
  4444. {
  4445. ASSERT(ppt);
  4446. if (plv->ci.style & WS_VSCROLL)
  4447. {
  4448. if (ppt->y >= lprc->bottom)
  4449. {
  4450. if (CanScroll(plv, SB_VERT, TRUE))
  4451. return TRUE;
  4452. }
  4453. else if (ppt->y <= lprc->top)
  4454. {
  4455. if (CanScroll(plv, SB_VERT, FALSE))
  4456. return TRUE;
  4457. }
  4458. }
  4459. if (plv->ci.style & WS_HSCROLL)
  4460. {
  4461. if (ppt->x >= lprc->right)
  4462. {
  4463. if (CanScroll(plv, SB_HORZ, TRUE))
  4464. return TRUE;
  4465. }
  4466. else if (ppt->x <= lprc->left)
  4467. {
  4468. if (CanScroll(plv, SB_HORZ, FALSE))
  4469. return TRUE;
  4470. }
  4471. }
  4472. return FALSE;
  4473. }
  4474. //----------------------------------------------------------------------------
  4475. void NEAR ListView_DragSelect(LV *plv, int x, int y)
  4476. {
  4477. RECT rc, rcWindow, rcOld, rcUnion, rcTemp2, rcClip;
  4478. POINT pt;
  4479. MSG32 msg32;
  4480. HDC hdc;
  4481. HWND hwnd = plv->ci.hwnd;
  4482. int i, iEnd, dx, dy;
  4483. BOOL bInOld, bInNew = FALSE, bLocked = FALSE;
  4484. DWORD dwTime, dwNewTime;
  4485. HRGN hrgnUpdate = NULL, hrgnLV = NULL;
  4486. rc.left = rc.right = x;
  4487. rc.top = rc.bottom = y;
  4488. rcOld = rc;
  4489. UpdateWindow(plv->ci.hwnd);
  4490. if (plv->exStyle & LVS_EX_REGIONAL) {
  4491. if ((hrgnUpdate = CreateRectRgn(0,0,0,0)) &&
  4492. (hrgnLV = CreateRectRgn(0,0,0,0)) &&
  4493. (LockWindowUpdate(GetParent(hwnd)))) {
  4494. hdc = GetDCEx(hwnd, NULL, DCX_PARENTCLIP | DCX_LOCKWINDOWUPDATE);
  4495. bLocked = TRUE;
  4496. } else {
  4497. goto BailOut;
  4498. }
  4499. } else {
  4500. hdc = GetDC(hwnd);
  4501. }
  4502. SetCapture(hwnd);
  4503. DrawFocusRect(hdc, &rc);
  4504. GetClientRect(hwnd, &rcClip);
  4505. GetWindowRect(hwnd, &rcWindow);
  4506. dwTime = GetTickCount();
  4507. for (;;)
  4508. {
  4509. // WM_CANCELMODE messages will unset the capture, in that
  4510. // case I want to exit this loop
  4511. if (GetCapture() != hwnd)
  4512. {
  4513. break;
  4514. }
  4515. if (!PeekMessage32(&msg32, NULL, 0, 0, PM_REMOVE, TRUE)) {
  4516. // if the cursor is outside of the window rect
  4517. // we need to generate messages to make autoscrolling
  4518. // keep going
  4519. if (!PtInRect(&rcWindow, msg32.pt))
  4520. {
  4521. // If we may be able to scroll, generate a mouse move.
  4522. if (ShouldScroll(plv, &msg32.pt, &rcWindow))
  4523. SetCursorPos(msg32.pt.x, msg32.pt.y);
  4524. }
  4525. else
  4526. {
  4527. WaitMessage();
  4528. }
  4529. continue;
  4530. }
  4531. // See if the application wants to process the message...
  4532. if (CallMsgFilter32(&msg32, MSGF_COMMCTRL_DRAGSELECT, TRUE) != 0)
  4533. continue;
  4534. switch (msg32.message)
  4535. {
  4536. case WM_LBUTTONUP:
  4537. case WM_RBUTTONUP:
  4538. case WM_LBUTTONDOWN:
  4539. case WM_MBUTTONDOWN:
  4540. case WM_MBUTTONUP:
  4541. case WM_RBUTTONDOWN:
  4542. CCReleaseCapture(&plv->ci);
  4543. goto EndOfLoop;
  4544. case WM_TIMER:
  4545. if (msg32.wParam != IDT_MARQUEE)
  4546. goto DoDefault;
  4547. // else fall through
  4548. case WM_MOUSEMOVE:
  4549. {
  4550. int dMax = -1;
  4551. pt = msg32.pt;
  4552. ScreenToClient(hwnd, &pt);
  4553. dwNewTime = GetTickCount();
  4554. // if (1 || (dwNewTime - dwTime) > SCROLL_FREQ)
  4555. // {
  4556. dwTime = dwNewTime; // reset scroll timer
  4557. ScrollDetect(plv, pt, &dx, &dy);
  4558. // }
  4559. // else
  4560. // {
  4561. // dx = dy = 0;
  4562. // }
  4563. //SetTimer(plv->ci.hwnd, IDT_MARQUEE, SCROLL_FREQ, NULL);
  4564. y -= dy; // scroll up/down
  4565. x -= dx; // scroll left/right
  4566. rc.left = x;
  4567. rc.top = y;
  4568. rc.right = pt.x;
  4569. rc.bottom = pt.y;
  4570. // clip drag rect to the window
  4571. //
  4572. if (rc.right > rcClip.right)
  4573. rc.right = rcClip.right;
  4574. if (rc.right < rcClip.left)
  4575. rc.right = rcClip.left;
  4576. if (rc.bottom > rcClip.bottom)
  4577. rc.bottom = rcClip.bottom;
  4578. if (rc.bottom < rcClip.top)
  4579. rc.bottom = rcClip.top;
  4580. OrderRect(&rc);
  4581. if (EqualRect(&rc, &rcOld))
  4582. break;
  4583. // move the old rect
  4584. DrawFocusRect(hdc, &rcOld); // erase old
  4585. if (dx || dy)
  4586. ListView_OnScroll(plv, dx, dy);
  4587. OffsetRect(&rcOld, -dx, -dy);
  4588. //
  4589. // For Report and List view, we can speed things up by
  4590. // only searching through those items that are visible. We
  4591. // use the hittest to calculate the first item to paint.
  4592. // BUGBUG:: We are using state specific info here...
  4593. //
  4594. UnionRect(&rcUnion, &rc, &rcOld);
  4595. if (ListView_IsReportView(plv))
  4596. {
  4597. i = (int)((plv->ptlRptOrigin.y + rcUnion.top - plv->yTop)
  4598. / plv->cyItem);
  4599. iEnd = (int)((plv->ptlRptOrigin.y + rcUnion.bottom - plv->yTop)
  4600. / plv->cyItem) + 1;
  4601. }
  4602. else if (ListView_IsListView(plv))
  4603. {
  4604. i = ((plv->xOrigin + rcUnion.left)/ plv->cxItem)
  4605. * plv->cItemCol + rcUnion.top / plv->cyItem;
  4606. iEnd = ((plv->xOrigin + rcUnion.right)/ plv->cxItem)
  4607. * plv->cItemCol + rcUnion.bottom / plv->cyItem + 1;
  4608. }
  4609. else
  4610. {
  4611. if (ListView_IsOwnerData( plv ))
  4612. {
  4613. ListView_CalcMinMaxIndex( plv, &rcUnion, &i, &iEnd );
  4614. }
  4615. else
  4616. {
  4617. i = 0;
  4618. iEnd = ListView_Count(plv);
  4619. }
  4620. }
  4621. // make sure our endpoint is in range.
  4622. if (iEnd > ListView_Count(plv))
  4623. iEnd = ListView_Count(plv);
  4624. if (i < 0)
  4625. i = 0;
  4626. if (ListView_IsOwnerData(plv) && (i < iEnd)) {
  4627. ListView_NotifyCacheHint(plv, i, iEnd-1);
  4628. }
  4629. if (bInNew && !(msg32.wParam & (MK_CONTROL | MK_SHIFT))) {
  4630. plv->iMark = -1;
  4631. }
  4632. for (; i < iEnd; i++) {
  4633. RECT dummy;
  4634. ListView_GetRects(plv, i, NULL, NULL, NULL, &rcTemp2);
  4635. // don't do this infaltion if we're in report&full row mode
  4636. // in that case, just touching is good enough
  4637. if (!(ListView_IsReportView(plv) && ListView_FullRowSelect(plv))) {
  4638. int cxInflate = (rcTemp2.right - rcTemp2.left) / 4;
  4639. if (ListView_IsListView(plv)) {
  4640. cxInflate = min(cxInflate, plv->cxSmIcon);
  4641. }
  4642. InflateRect(&rcTemp2, -cxInflate, -(rcTemp2.bottom - rcTemp2.top) / 4);
  4643. }
  4644. bInOld = (IntersectRect(&dummy, &rcOld, &rcTemp2) != 0);
  4645. bInNew = (IntersectRect(&dummy, &rc, &rcTemp2) != 0);
  4646. if (msg32.wParam & MK_CONTROL) {
  4647. if (bInOld != bInNew) {
  4648. ListView_ToggleSelection(plv, i);
  4649. }
  4650. } else {
  4651. // was there a change?
  4652. if (bInOld != bInNew) {
  4653. ListView_OnSetItemState(plv, i, bInOld ? 0 : LVIS_SELECTED, LVIS_SELECTED);
  4654. }
  4655. // if no alternate keys are down.. set the mark to
  4656. // the item furthest from the cursor
  4657. if (bInNew && !(msg32.wParam & (MK_CONTROL | MK_SHIFT))) {
  4658. int dItem;
  4659. dItem = (rcTemp2.left - pt.x) * (rcTemp2.left - pt.x) +
  4660. (rcTemp2.top - pt.y) * (rcTemp2.top - pt.y);
  4661. // if it's further away, set this as the mark
  4662. //DebugMsg(TF_LISTVIEW, "dItem = %d, dMax = %d", dItem, dMax);
  4663. if (dItem > dMax) {
  4664. //DebugMsg(TF_LISTVIEW, "taking dItem .. iMark = %d", i);
  4665. dMax = dItem;
  4666. plv->iMark = i;
  4667. }
  4668. }
  4669. }
  4670. }
  4671. //DebugMsg(TF_LISTVIEW, "Final iMark = %d", plv->iMark);
  4672. if (bLocked) {
  4673. if (GetUpdateRgn(plv->ci.hwnd, hrgnUpdate, FALSE) > NULLREGION) {
  4674. ValidateRect(plv->ci.hwnd, NULL);
  4675. GetWindowRgn(plv->ci.hwnd, hrgnLV);
  4676. CombineRgn(hrgnUpdate, hrgnUpdate, hrgnLV, RGN_AND);
  4677. SelectClipRgn(hdc, hrgnUpdate);
  4678. SendMessage(plv->ci.hwnd, WM_PRINTCLIENT, (WPARAM)hdc, 0);
  4679. SelectClipRgn(hdc, NULL);
  4680. }
  4681. } else {
  4682. UpdateWindow(plv->ci.hwnd); // make selection draw
  4683. }
  4684. DrawFocusRect(hdc, &rc);
  4685. rcOld = rc;
  4686. break;
  4687. }
  4688. case WM_KEYDOWN:
  4689. switch (msg32.wParam) {
  4690. case VK_ESCAPE:
  4691. ListView_DeselectAll(plv, -1);
  4692. goto EndOfLoop;
  4693. }
  4694. case WM_CHAR:
  4695. case WM_KEYUP:
  4696. // don't process thay keyboard stuff during marquee
  4697. break;
  4698. default:
  4699. // don't process mouse wheel stuff
  4700. if (msg32.message == g_msgMSWheel)
  4701. break;
  4702. DoDefault:
  4703. TranslateMessage32(&msg32, TRUE);
  4704. DispatchMessage32(&msg32, TRUE);
  4705. }
  4706. }
  4707. EndOfLoop:
  4708. DrawFocusRect(hdc, &rcOld); // erase old
  4709. ReleaseDC(hwnd, hdc);
  4710. BailOut:
  4711. if (hrgnUpdate)
  4712. DeleteObject(hrgnUpdate);
  4713. if (hrgnLV)
  4714. DeleteObject(hrgnLV);
  4715. if (bLocked)
  4716. LockWindowUpdate(NULL);
  4717. }
  4718. #define SHIFT_DOWN(keyFlags) (keyFlags & MK_SHIFT)
  4719. #define CONTROL_DOWN(keyFlags) (keyFlags & MK_CONTROL)
  4720. #define RIGHTBUTTON(keyFlags) (keyFlags & MK_RBUTTON)
  4721. void PASCAL ListView_ButtonSelect(LV* plv, int iItem, UINT keyFlags, BOOL bSelected)
  4722. {
  4723. if (SHIFT_DOWN(keyFlags))
  4724. {
  4725. ListView_SelectRangeTo(plv, iItem, !CONTROL_DOWN(keyFlags));
  4726. ListView_SetFocusSel(plv, iItem, TRUE, FALSE, FALSE);
  4727. }
  4728. else if (!CONTROL_DOWN(keyFlags))
  4729. {
  4730. ListView_SetFocusSel(plv, iItem, TRUE, !bSelected, FALSE);
  4731. }
  4732. }
  4733. void ListView_HandleStateIconClick(LV* plv, int iItem)
  4734. {
  4735. int iState =
  4736. ListView_OnGetItemState(plv, iItem, LVIS_STATEIMAGEMASK);
  4737. iState = STATEIMAGEMASKTOINDEX(iState) -1;
  4738. iState++;
  4739. iState %= ImageList_GetImageCount(plv->himlState);
  4740. iState++;
  4741. ListView_OnSetItemState(plv, iItem, INDEXTOSTATEIMAGEMASK(iState), LVIS_STATEIMAGEMASK);
  4742. }
  4743. BOOL ListView_RBeginMarquee(LV* plv, int x, int y, LPLVHITTESTINFO plvhti)
  4744. {
  4745. if (ListView_FullRowSelect(plv) &&
  4746. ListView_IsReportView(plv) &&
  4747. !(plv->ci.style & LVS_SINGLESEL) &&
  4748. !ListView_OwnerDraw(plv) &&
  4749. plvhti->iSubItem == 0) {
  4750. // can only begin marquee in column 0.
  4751. if (plvhti->flags == LVHT_ONITEM) {
  4752. return TRUE;
  4753. }
  4754. }
  4755. return FALSE;
  4756. }
  4757. void NEAR ListView_HandleMouse(LV* plv, BOOL fDoubleClick, int x, int y, UINT keyFlags, BOOL bMouseWheel)
  4758. {
  4759. LV_HITTESTINFO ht;
  4760. NMITEMACTIVATE nm;
  4761. int iItem, click, drag;
  4762. BOOL bSelected, fHadFocus, fNotifyReturn = FALSE;
  4763. BOOL fActive;
  4764. HWND hwnd = plv->ci.hwnd;
  4765. if (plv->fButtonDown)
  4766. return;
  4767. plv->fButtonDown = TRUE;
  4768. if (plv->exStyle & LVS_EX_ONECLICKACTIVATE && plv->fOneClickHappened && plv->fOneClickOK)
  4769. {
  4770. KillTimer(plv->ci.hwnd, IDT_ONECLICKHAPPENED);
  4771. plv->fOneClickHappened = FALSE;
  4772. CCSendNotify(&plv->ci, LVN_ITEMACTIVATE, &(plv->nmOneClickHappened.hdr));
  4773. if (!IsWindow(hwnd))
  4774. return;
  4775. }
  4776. fHadFocus = (GetFocus() == plv->ci.hwnd);
  4777. click = RIGHTBUTTON(keyFlags) ? NM_RCLICK : NM_CLICK;
  4778. drag = RIGHTBUTTON(keyFlags) ? LVN_BEGINRDRAG : LVN_BEGINDRAG;
  4779. fActive = ChildOfActiveWindow(plv->ci.hwnd) || fShouldFirstClickActivate() ||
  4780. ChildOfDesktop(plv->ci.hwnd);
  4781. #ifdef LVDEBUG
  4782. DebugMsg(TF_LISTVIEW, TEXT("ListView_OnButtonDown %d"), fDoubleClick);
  4783. #endif
  4784. SetCapture(plv->ci.hwnd);
  4785. plv->ptCapture.x = x;
  4786. plv->ptCapture.y = y;
  4787. if (!ListView_DismissEdit(plv, FALSE) && GetCapture() != plv->ci.hwnd)
  4788. goto EndButtonDown;
  4789. CCReleaseCapture(&plv->ci);
  4790. // REVIEW: right button implies no shift or control stuff
  4791. // Single selection style also implies no modifiers
  4792. //if (RIGHTBUTTON(keyFlags) || (plv->ci.style & LVS_SINGLESEL))
  4793. if ((plv->ci.style & LVS_SINGLESEL))
  4794. keyFlags &= ~(MK_SHIFT | MK_CONTROL);
  4795. ht.pt.x = x;
  4796. ht.pt.y = y;
  4797. iItem = ListView_OnSubItemHitTest(plv, &ht);
  4798. if (ht.iSubItem != 0) {
  4799. // if we're not in full row select,
  4800. // hitting on a subitem is like hitting on nowhere
  4801. // also, in win95, ownerdraw fixed effectively had full row select
  4802. if (!ListView_FullRowSelect(plv) &&
  4803. !(plv->ci.style & LVS_OWNERDRAWFIXED)) {
  4804. iItem = -1;
  4805. ht.flags = LVHT_NOWHERE;
  4806. }
  4807. }
  4808. nm.iItem = iItem;
  4809. nm.iSubItem = ht.iSubItem;
  4810. nm.uChanged = 0;
  4811. nm.ptAction.x = x;
  4812. nm.ptAction.y = y;
  4813. nm.uKeyFlags = GetLVKeyFlags();
  4814. // FProt Profesional assumed that if the notification structure pointer + 14h bytes
  4815. // had a value 2 that it was a displayinfo structure and they then used offset +2c as lparam...
  4816. nm.uNewState = 0;
  4817. plv->iNoHover = iItem;
  4818. bSelected = (iItem >= 0) && ListView_OnGetItemState(plv, iItem, LVIS_SELECTED);
  4819. if (fDoubleClick)
  4820. {
  4821. //
  4822. // Cancel any name editing that might happen.
  4823. //
  4824. ListView_CancelPendingEdit(plv);
  4825. KillTimer(plv->ci.hwnd, IDT_SCROLLWAIT);
  4826. if (ht.flags & LVHT_NOWHERE) {
  4827. // this would have been done in the first click in win95 except
  4828. // now we blow off the first click on focus change
  4829. if (!SHIFT_DOWN(keyFlags) && !CONTROL_DOWN(keyFlags))
  4830. ListView_DeselectAll(plv, -1);
  4831. }
  4832. click = RIGHTBUTTON(keyFlags) ? NM_RDBLCLK : NM_DBLCLK ;
  4833. if (CCSendNotify(&plv->ci, click, &nm.hdr))
  4834. goto EndButtonDown;
  4835. /// some (comdlg32 for example) destroy on double click
  4836. // we need to bail if that happens because plv is no longer valid
  4837. if (!IsWindow(hwnd))
  4838. return;
  4839. if (click == NM_DBLCLK)
  4840. {
  4841. // these shift control flags are to mirror when we don't send out the activate on the single click,
  4842. // but are in the oneclick activate mode (see below)
  4843. if (ht.flags & (LVHT_ONITEMLABEL | LVHT_ONITEMICON))
  4844. {
  4845. // possible scenarios below:
  4846. // 1) we're using classic windows style so double click => launch
  4847. // 2) we're using single click activate
  4848. // a) shift is down and item is selected => launch
  4849. // this implies that the first click selected it
  4850. // b) control is down => launch
  4851. // the first click toggled the selection so if the item was
  4852. // the only item selected and we double clicked on it
  4853. // the first click deselects it and no item is selected
  4854. // so nothing will be launched - this is win95 behavior
  4855. if (!(plv->exStyle & LVS_EX_ONECLICKACTIVATE && plv->fOneClickOK) ||
  4856. (plv->exStyle & LVS_EX_ONECLICKACTIVATE && plv->fOneClickOK &&
  4857. (SHIFT_DOWN(keyFlags) || CONTROL_DOWN(keyFlags))))
  4858. {
  4859. CCSendNotify(&plv->ci, LVN_ITEMACTIVATE, &nm.hdr);
  4860. }
  4861. }
  4862. // Double-click on checkbox state icon cycles it just like single click
  4863. else if ((ht.flags & LVHT_ONITEMSTATEICON) && ListView_CheckBoxes(plv)) {
  4864. ListView_HandleStateIconClick(plv, iItem);
  4865. }
  4866. }
  4867. if (!IsWindow(hwnd))
  4868. return;
  4869. goto EndButtonDown;
  4870. }
  4871. if (ht.flags & (LVHT_ONITEMLABEL | LVHT_ONITEMICON))
  4872. {
  4873. // if it wasn't selected, we're about to select it... play
  4874. // a little ditty for us...
  4875. CCPlaySound(c_szSelect);
  4876. if (!RIGHTBUTTON(keyFlags) || (!CONTROL_DOWN(keyFlags) && !SHIFT_DOWN(keyFlags)))
  4877. ListView_ButtonSelect(plv, iItem, keyFlags, bSelected);
  4878. // handle full row select
  4879. // If single-select listview, disable marquee selection.
  4880. //
  4881. // Careful - CheckForDragBegin yields and the app may have
  4882. // destroyed the item we were thinking about dragging!
  4883. //
  4884. if (!bMouseWheel && CheckForDragBegin(plv->ci.hwnd, x, y))
  4885. {
  4886. // should we do a marquee?
  4887. if (ListView_RBeginMarquee(plv, x, y, &ht) &&
  4888. !CCSendNotify(&plv->ci, LVN_MARQUEEBEGIN, &nm.hdr))
  4889. {
  4890. ListView_DragSelect(plv, x, y);
  4891. fNotifyReturn = !CCSendNotify(&plv->ci, click, &nm.hdr);
  4892. }
  4893. else
  4894. {
  4895. // Before we start dragging, make it sure that it is
  4896. // selected and has the focus.
  4897. ListView_SetFocusSel(plv, iItem, TRUE, FALSE, FALSE);
  4898. if (!SHIFT_DOWN(keyFlags))
  4899. plv->iMark = iItem;
  4900. // Then, we need to update the window before start dragging
  4901. // to show the selection chagne.
  4902. UpdateWindow(plv->ci.hwnd);
  4903. CCSendNotify(&plv->ci, drag, &nm.hdr);
  4904. goto EndButtonDown;
  4905. }
  4906. }
  4907. // CheckForDragBegin yields, so revalidate before continuing
  4908. else if (IsWindow(hwnd))
  4909. {
  4910. // button came up and we are not dragging
  4911. if (!RIGHTBUTTON(keyFlags))
  4912. {
  4913. if (CONTROL_DOWN(keyFlags))
  4914. {
  4915. // do this on the button up so that ctrl-dragging a range
  4916. // won't toggle the select.
  4917. if (SHIFT_DOWN(keyFlags))
  4918. ListView_SetFocusSel(plv, iItem, FALSE, FALSE, FALSE);
  4919. else
  4920. {
  4921. ListView_SetFocusSel(plv, iItem, TRUE, FALSE, TRUE);
  4922. }
  4923. }
  4924. }
  4925. if (!SHIFT_DOWN(keyFlags))
  4926. plv->iMark = iItem;
  4927. if (!ListView_SetFocus(plv->ci.hwnd)) // activate this window
  4928. return;
  4929. // now do the deselect stuff
  4930. if (!SHIFT_DOWN(keyFlags) && !CONTROL_DOWN(keyFlags) && !RIGHTBUTTON(keyFlags))
  4931. {
  4932. ListView_DeselectAll(plv, iItem);
  4933. if ((ht.flags & LVHT_ONITEMLABEL) && bSelected &&
  4934. !(plv->exStyle & (LVS_EX_ONECLICKACTIVATE|LVS_EX_TWOCLICKACTIVATE)))
  4935. {
  4936. // doing this check for ownerdrawfixed is for compatability.
  4937. // we don't want to go into edit mode if the user just happened to click
  4938. // to this window when a different one had focus,
  4939. // but ms hammer relied upon the notification being sent (and we
  4940. // don't go into edit mode anyways for ownerdraw)
  4941. if (fHadFocus ||
  4942. (plv->ci.style & LVS_OWNERDRAWFIXED)) {
  4943. // Click on item label. It was selected and
  4944. // no modifier keys were pressed and no drag operation
  4945. // So setup for name edit mode. Still need to wait
  4946. // to make sure user is not doing double click.
  4947. //
  4948. ListView_SetupPendingNameEdit(plv);
  4949. }
  4950. }
  4951. }
  4952. fNotifyReturn = !CCSendNotify(&plv->ci, click, &nm.hdr);
  4953. if (!IsWindow(hwnd))
  4954. return;
  4955. if (plv->exStyle & (LVS_EX_ONECLICKACTIVATE|LVS_EX_TWOCLICKACTIVATE))
  4956. {
  4957. if (!RIGHTBUTTON(keyFlags))
  4958. {
  4959. // We don't ItemActivate within one double-click time of creating
  4960. // this listview. This is a common occurence for people used to
  4961. // double-clicking. The first click pops up a new window which
  4962. // receives the second click and ItemActivates the item...
  4963. //
  4964. if ((plv->exStyle & LVS_EX_ONECLICKACTIVATE && plv->fOneClickOK) || bSelected)
  4965. {
  4966. if (fActive)
  4967. {
  4968. // condition: if we're in a single click activate mode
  4969. // don't launch if control or shift keys are pressed
  4970. BOOL bCond = plv->exStyle & LVS_EX_ONECLICKACTIVATE && !CONTROL_DOWN(keyFlags) && !SHIFT_DOWN(keyFlags);
  4971. if ((bSelected && plv->exStyle & LVS_EX_TWOCLICKACTIVATE) ||
  4972. (bCond && !g_bUseDblClickTimer))
  4973. {
  4974. CCSendNotify(&plv->ci, LVN_ITEMACTIVATE, &nm.hdr);
  4975. if (!IsWindow(hwnd))
  4976. return;
  4977. }
  4978. else if (bCond && g_bUseDblClickTimer)
  4979. {
  4980. plv->fOneClickHappened = TRUE;
  4981. plv->nmOneClickHappened = nm;
  4982. SetTimer(plv->ci.hwnd, IDT_ONECLICKHAPPENED, GetDoubleClickTime(), NULL);
  4983. }
  4984. }
  4985. }
  4986. }
  4987. }
  4988. }
  4989. else
  4990. {
  4991. // IsWindow() failed. Bail.
  4992. return;
  4993. }
  4994. }
  4995. else if (ht.flags & LVHT_ONITEMSTATEICON)
  4996. {
  4997. // Should activate window and send notificiation to parent...
  4998. if (!ListView_SetFocus(plv->ci.hwnd)) // activate this window
  4999. return;
  5000. fNotifyReturn = !CCSendNotify(&plv->ci, click, &nm.hdr);
  5001. if (fNotifyReturn && ListView_CheckBoxes(plv)) {
  5002. ListView_HandleStateIconClick(plv, iItem);
  5003. }
  5004. }
  5005. else if (ht.flags & LVHT_NOWHERE)
  5006. {
  5007. if (!ListView_SetFocus(plv->ci.hwnd)) // activate this window
  5008. return;
  5009. // If single-select listview, disable marquee selection.
  5010. if (!(plv->ci.style & LVS_SINGLESEL) && CheckForDragBegin(plv->ci.hwnd, x, y) &&
  5011. !CCSendNotify(&plv->ci, LVN_MARQUEEBEGIN, &nm.hdr))
  5012. {
  5013. if (!SHIFT_DOWN(keyFlags) && !CONTROL_DOWN(keyFlags))
  5014. ListView_DeselectAll(plv, -1);
  5015. ListView_DragSelect(plv, x, y);
  5016. fNotifyReturn = !CCSendNotify(&plv->ci, click, &nm.hdr);
  5017. } else if (IsWindow(hwnd)) {
  5018. // if we didn't have focus and aren't showing selection always,
  5019. // make the first click just set focus
  5020. BOOL fDoFirstClickSelection = (fHadFocus || plv->ci.style & LVS_SHOWSELALWAYS ||
  5021. CONTROL_DOWN(keyFlags) || SHIFT_DOWN(keyFlags) ||
  5022. RIGHTBUTTON(keyFlags));
  5023. if (fDoFirstClickSelection && fActive) {
  5024. if (!SHIFT_DOWN(keyFlags) && !CONTROL_DOWN(keyFlags))
  5025. ListView_DeselectAll(plv, -1);
  5026. fNotifyReturn = !CCSendNotify(&plv->ci, click, &nm.hdr);
  5027. }
  5028. }
  5029. else
  5030. {
  5031. // IsWindow() failed. Bail.
  5032. return;
  5033. }
  5034. }
  5035. // re-check the key state so we don't get confused by multiple clicks
  5036. // this needs to check the GetKeyState stuff only when we've gone into
  5037. // a modal loop waiting for the rbutton up.
  5038. if (fNotifyReturn && (click == NM_RCLICK)) // && (GetKeyState(VK_RBUTTON)>=0))
  5039. {
  5040. POINT pt = { x, y };
  5041. ClientToScreen(plv->ci.hwnd, &pt);
  5042. FORWARD_WM_CONTEXTMENU(plv->ci.hwnd, plv->ci.hwnd, pt.x, pt.y, SendMessage);
  5043. }
  5044. EndButtonDown:
  5045. if (IsWindow(hwnd))
  5046. plv->fButtonDown = FALSE;
  5047. }
  5048. void NEAR ListView_OnButtonDown(LV* plv, BOOL fDoubleClick, int x, int y, UINT keyFlags)
  5049. {
  5050. ListView_HandleMouse(plv, fDoubleClick, x, y, keyFlags, FALSE);
  5051. }
  5052. #define ListView_CancelPendingEdit(plv) ListView_CancelPendingTimer(plv, LVF_NMEDITPEND, IDT_NAMEEDIT)
  5053. #define ListView_CancelScrollWait(plv) ListView_CancelPendingTimer(plv, LVF_SCROLLWAIT, IDT_SCROLLWAIT)
  5054. BOOL NEAR ListView_CancelPendingTimer(LV* plv, UINT fFlags, int idTimer)
  5055. {
  5056. if (plv->flags & fFlags)
  5057. {
  5058. KillTimer(plv->ci.hwnd, idTimer);
  5059. plv->flags &= ~fFlags;
  5060. return TRUE;
  5061. }
  5062. return FALSE;
  5063. }
  5064. //
  5065. // ListView_OnTimer:
  5066. // process the WM_TIMER message. If the timer id is thta
  5067. // of the name editing, we should then start the name editing mode.
  5068. //
  5069. void NEAR ListView_OnTimer(LV* plv, UINT id)
  5070. {
  5071. KillTimer(plv->ci.hwnd, id);
  5072. if (id == IDT_NAMEEDIT)
  5073. {
  5074. // Kill the timer as we wont need any more messages from it.
  5075. if (ListView_CancelPendingEdit(plv)) {
  5076. // And start name editing mode.
  5077. if (!ListView_OnEditLabel(plv, plv->iFocus, NULL))
  5078. {
  5079. ListView_DismissEdit(plv, FALSE);
  5080. ListView_SetFocusSel(plv, plv->iFocus, TRUE, TRUE, FALSE);
  5081. }
  5082. }
  5083. } else if (id == IDT_SCROLLWAIT) {
  5084. if (ListView_CancelScrollWait(plv)) {
  5085. ListView_OnEnsureVisible(plv, plv->iFocus, TRUE);
  5086. }
  5087. } else if (id == IDT_ONECLICKOK) {
  5088. plv->fOneClickOK = TRUE;
  5089. } else if (id == IDT_ONECLICKHAPPENED) {
  5090. //if (!g_bUseDblClickTimer)
  5091. //{
  5092. //// EnableWindow(plv->ci.hwnd, TRUE);
  5093. // SetWindowBits(plv->ci.hwnd, GWL_STYLE, WS_DISABLED, 0);
  5094. // plv->fOneClickHappened = FALSE;
  5095. //}
  5096. // check the bit just in case they double-clicked
  5097. //else
  5098. if (plv->fOneClickHappened)
  5099. {
  5100. plv->fOneClickHappened = FALSE;
  5101. CCSendNotify(&plv->ci, LVN_ITEMACTIVATE, &(plv->nmOneClickHappened.hdr));
  5102. }
  5103. }
  5104. }
  5105. //
  5106. // ListView_SetupPendingNameEdit:
  5107. // Sets up a timer to begin name editing at a delayed time. This
  5108. // will allow the user to double click on the already selected item
  5109. // without going into name editing mode, which is especially important
  5110. // in those views that only show a small icon.
  5111. //
  5112. void NEAR ListView_SetupPendingNameEdit(LV* plv)
  5113. {
  5114. SetTimer(plv->ci.hwnd, IDT_NAMEEDIT, GetDoubleClickTime(), NULL);
  5115. plv->flags |= LVF_NMEDITPEND;
  5116. }
  5117. void NEAR PASCAL ListView_OnHVScroll(LV* plv, UINT code, int pos, int sb)
  5118. {
  5119. int iScrollCount = 0;
  5120. #ifdef SIF_TRACKPOS
  5121. SCROLLINFO si;
  5122. si.cbSize = sizeof(SCROLLINFO);
  5123. si.fMask = SIF_TRACKPOS;
  5124. // if we're in 32bits, don't trust the pos since it's only 16bit's worth
  5125. if (ListView_GetScrollInfo(plv, sb, &si))
  5126. pos = (int)si.nTrackPos;
  5127. #endif
  5128. ListView_DismissEdit(plv, FALSE);
  5129. _ListView_OnScroll(plv, code, pos, sb);
  5130. switch (code) {
  5131. case SB_PAGELEFT:
  5132. case SB_PAGERIGHT:
  5133. if (plv->iScrollCount < SMOOTHSCROLLLIMIT)
  5134. plv->iScrollCount += 3;
  5135. break;
  5136. case SB_LINELEFT:
  5137. case SB_LINERIGHT:
  5138. if (plv->iScrollCount < SMOOTHSCROLLLIMIT)
  5139. plv->iScrollCount++;
  5140. break;
  5141. case SB_ENDSCROLL:
  5142. plv->iScrollCount = 0;
  5143. break;
  5144. }
  5145. }
  5146. void NEAR ListView_OnVScroll(LV* plv, HWND hwndCtl, UINT code, int pos)
  5147. {
  5148. ListView_OnHVScroll(plv, code, pos, SB_VERT);
  5149. }
  5150. void NEAR ListView_OnHScroll(LV* plv, HWND hwndCtl, UINT code, int pos)
  5151. {
  5152. ListView_OnHVScroll(plv, code, pos, SB_HORZ);
  5153. }
  5154. int ListView_ValidateOneScrollParam(LV* plv, int iDirection, int dx)
  5155. {
  5156. SCROLLINFO si;
  5157. si.cbSize = sizeof(SCROLLINFO);
  5158. si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
  5159. if (!ListView_GetScrollInfo(plv, iDirection, &si))
  5160. return 0;
  5161. si.nMax -= (si.nPage - 1);
  5162. si.nPos += dx;
  5163. if (si.nPos < si.nMin) {
  5164. dx += (int)(si.nMin - si.nPos);
  5165. } else if (si.nPos > si.nMax) {
  5166. dx -= (int)(si.nPos - si.nMax);
  5167. }
  5168. return dx;
  5169. }
  5170. BOOL NEAR PASCAL ListView_ValidateScrollParams(LV* plv, int FAR * pdx, int FAR *pdy)
  5171. {
  5172. int dx = *pdx;
  5173. int dy = *pdy;
  5174. if (plv->ci.style & LVS_NOSCROLL)
  5175. return FALSE;
  5176. if (ListView_IsListView(plv))
  5177. {
  5178. ListView_MaybeResizeListColumns(plv, 0, ListView_Count(plv)-1);
  5179. #ifdef COLUMN_VIEW
  5180. if (dx < 0)
  5181. dx = (dx - plv->cxItem - 1) / plv->cxItem;
  5182. else
  5183. dx = (dx + plv->cxItem - 1) / plv->cxItem;
  5184. if (dy)
  5185. return FALSE;
  5186. #else
  5187. if (dy < 0)
  5188. dy = (dy - plv->cyItem - 1) / plv->cyItem;
  5189. else
  5190. dy = (dy + plv->cyItem - 1) / plv->cyItem;
  5191. if (dx)
  5192. return FALSE;
  5193. #endif
  5194. }
  5195. else if (ListView_IsReportView(plv))
  5196. {
  5197. //
  5198. // Note: This function expects that dy is in number of lines
  5199. // and we are working with pixels so do a conversion use some
  5200. // rounding up and down to make it right
  5201. if (dy > 0)
  5202. dy = (dy + plv->cyItem/2) / plv->cyItem;
  5203. else
  5204. dy = (dy - plv->cyItem/2) / plv->cyItem;
  5205. }
  5206. if (dy) {
  5207. dy = ListView_ValidateOneScrollParam(plv, SB_VERT, dy);
  5208. if (ListView_IsReportView(plv)
  5209. #ifndef COLUMN_VIEW
  5210. || ListView_IsListView(plv)
  5211. #endif
  5212. ) {
  5213. // convert back to pixels
  5214. dy *= plv->cyItem;
  5215. }
  5216. *pdy = dy;
  5217. }
  5218. if (dx) {
  5219. dx = ListView_ValidateOneScrollParam(plv, SB_HORZ, dx);
  5220. #ifdef COLUMN_VIEW
  5221. if (ListView_IsListView(plv)) {
  5222. dx *= plv->cxItem;
  5223. }
  5224. #endif
  5225. *pdx = dx;
  5226. }
  5227. return TRUE;
  5228. }
  5229. BOOL NEAR ListView_OnScroll(LV* plv, int dx, int dy)
  5230. {
  5231. if (plv->ci.style & LVS_NOSCROLL)
  5232. return FALSE;
  5233. if (ListView_IsListView(plv))
  5234. {
  5235. // Scale pixel count to column count
  5236. //
  5237. #ifdef COLUMN_VIEW
  5238. if (dx < 0)
  5239. dx -= plv->cxItem - 1;
  5240. else
  5241. dx += plv->cxItem - 1;
  5242. dx = dx / plv->cxItem;
  5243. if (dy)
  5244. return FALSE;
  5245. #else
  5246. if (dy < 0)
  5247. dy -= plv->cyItem - 1;
  5248. else
  5249. dy += plv->cyItem - 1;
  5250. dy = dy / plv->cyItem;
  5251. if (dx)
  5252. return FALSE;
  5253. #endif
  5254. }
  5255. else if (ListView_IsReportView(plv))
  5256. {
  5257. //
  5258. // Note: This function expects that dy is in number of lines
  5259. // and we are working with pixels so do a conversion use some
  5260. // rounding up and down to make it right
  5261. if (dy > 0)
  5262. dy = (dy + plv->cyItem/2) / plv->cyItem;
  5263. else
  5264. dy = (dy - plv->cyItem/2) / plv->cyItem;
  5265. }
  5266. _ListView_Scroll2(plv, dx, dy, 0);
  5267. ListView_UpdateScrollBars(plv);
  5268. return TRUE;
  5269. }
  5270. BOOL NEAR ListView_OnEnsureVisible(LV* plv, int i, BOOL fPartialOK)
  5271. {
  5272. RECT rcBounds;
  5273. RECT rc;
  5274. int dx, dy;
  5275. if (!ListView_IsValidItemNumber(plv, i) || plv->ci.style & LVS_NOSCROLL)
  5276. return FALSE;
  5277. // we need to do this again inside because some callers don't do it.
  5278. // other callers that do this need to do it outside so that
  5279. // they can know not to call us if there's not wait pending
  5280. ListView_CancelScrollWait(plv);
  5281. if (ListView_IsReportView(plv))
  5282. return ListView_ROnEnsureVisible(plv, i, fPartialOK);
  5283. ListView_GetRects(plv, i, &rc, NULL, &rcBounds, NULL);
  5284. if (!fPartialOK)
  5285. rc = rcBounds;
  5286. // If any part of rc is outside of rcClient, then
  5287. // scroll so that all of rcBounds is visible.
  5288. //
  5289. dx = 0;
  5290. if (rc.left < 0 || rc.right >= plv->sizeClient.cx)
  5291. {
  5292. dx = rcBounds.left - 0;
  5293. if (dx >= 0)
  5294. {
  5295. dx = rcBounds.right - plv->sizeClient.cx;
  5296. if (dx <= 0)
  5297. dx = 0;
  5298. else if ((rcBounds.left - dx) < 0)
  5299. dx = rcBounds.left - 0; // Not all fits...
  5300. }
  5301. }
  5302. dy = 0;
  5303. if (rc.top < 0 || rc.bottom >= plv->sizeClient.cy)
  5304. {
  5305. dy = rcBounds.top - 0;
  5306. if (dy >= 0)
  5307. {
  5308. dy = rcBounds.bottom - plv->sizeClient.cy;
  5309. if (dy < 0)
  5310. dy = 0;
  5311. }
  5312. }
  5313. if (dx | dy)
  5314. return ListView_OnScroll(plv, dx, dy);
  5315. return TRUE;
  5316. }
  5317. void NEAR ListView_UpdateScrollBars(LV* plv)
  5318. {
  5319. RECT rc;
  5320. DWORD dwStyle;
  5321. if ((plv->ci.style & LVS_NOSCROLL) ||
  5322. (!(ListView_RedrawEnabled(plv))))
  5323. return;
  5324. _ListView_UpdateScrollBars(plv);
  5325. GetClientRect(plv->ci.hwnd, &rc);
  5326. plv->sizeClient.cx = rc.right;
  5327. plv->sizeClient.cy = rc.bottom;
  5328. dwStyle = ListView_GetWindowStyle(plv);
  5329. plv->ci.style = (plv->ci.style & ~(WS_HSCROLL | WS_VSCROLL)) | (dwStyle & WS_HSCROLL | WS_VSCROLL);
  5330. }
  5331. void NEAR ListView_OnSetFont(LV* plv, HFONT hfont, BOOL fRedraw)
  5332. {
  5333. HDC hdc;
  5334. SIZE siz;
  5335. LOGFONT lf;
  5336. HFONT hfontPrev;
  5337. if ((plv->flags & LVF_FONTCREATED) && plv->hfontLabel) {
  5338. DeleteObject(plv->hfontLabel);
  5339. plv->flags &= ~LVF_FONTCREATED;
  5340. }
  5341. if (hfont == NULL) {
  5342. SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, FALSE);
  5343. // B#210235 - because NT4 initializes icontitle logfont with Ansi charset
  5344. // no matter what font is selected, following A/W conversion would fail
  5345. // on non US environment if we use this logfont to get codepage.
  5346. // The ACP is guaranteed to work with any Ansi apps because these apps
  5347. // assume ACP to be matching to their desired codepage.
  5348. if (staticIsOS(OS_NT4ORGREATER) && !staticIsOS(OS_WIN2000ORGREATER))
  5349. {
  5350. CHARSETINFO csi;
  5351. TranslateCharsetInfo(IntToPtr_(DWORD *, g_uiACP), &csi, TCI_SRCCODEPAGE);
  5352. lf.lfCharSet = (BYTE)csi.ciCharset;
  5353. }
  5354. hfont = CreateFontIndirect(&lf);
  5355. plv->flags |= LVF_FONTCREATED;
  5356. }
  5357. hdc = GetDC(HWND_DESKTOP);
  5358. hfontPrev = SelectFont(hdc, hfont);
  5359. GetTextExtentPoint(hdc, TEXT("0"), 1, &siz);
  5360. plv->cyLabelChar = siz.cy;
  5361. plv->cxLabelChar = siz.cx;
  5362. GetTextExtentPoint(hdc, c_szEllipses, CCHELLIPSES, &siz);
  5363. plv->cxEllipses = siz.cx;
  5364. SelectFont(hdc, hfontPrev);
  5365. ReleaseDC(HWND_DESKTOP, hdc);
  5366. plv->hfontLabel = hfont;
  5367. plv->ci.uiCodePage = GetCodePageForFont(hfont);
  5368. ListView_InvalidateCachedLabelSizes(plv);
  5369. /* Ensure that our tooltip control uses the same font as the list view is using, therefore
  5370. / avoiding any nasty formatting problems. */
  5371. if ( plv->hwndToolTips )
  5372. {
  5373. FORWARD_WM_SETFONT( plv->hwndToolTips, plv->hfontLabel, FALSE, SendMessage );
  5374. }
  5375. // If we have a header window, we need to forward this to it also
  5376. // as we have destroyed the hfont that they are using...
  5377. if (plv->hwndHdr) {
  5378. FORWARD_WM_SETFONT(plv->hwndHdr, plv->hfontLabel, FALSE, SendMessage);
  5379. ListView_UpdateScrollBars(plv);
  5380. }
  5381. if (plv->hFontHot) {
  5382. DeleteObject(plv->hFontHot);
  5383. plv->hFontHot = NULL;
  5384. }
  5385. CCGetHotFont(plv->hfontLabel, &plv->hFontHot);
  5386. plv->iFreeSlot = -1;
  5387. if (fRedraw)
  5388. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  5389. }
  5390. HFONT NEAR ListView_OnGetFont(LV* plv)
  5391. {
  5392. return plv->hfontLabel;
  5393. }
  5394. // This function process the WM_SETREDRAW message by setting or clearing
  5395. // a bit in the listview structure, which several places in the code will
  5396. // check...
  5397. //
  5398. // REVIEW: Should probably forward to DefWindowProc()
  5399. //
  5400. void NEAR ListView_OnSetRedraw(LV* plv, BOOL fRedraw)
  5401. {
  5402. if (fRedraw)
  5403. {
  5404. BOOL fChanges = FALSE;
  5405. // Only do work if we're turning redraw back on...
  5406. //
  5407. if (!(plv->flags & LVF_REDRAW))
  5408. {
  5409. plv->flags |= LVF_REDRAW;
  5410. // deal with any accumulated invalid regions
  5411. if (plv->hrgnInval)
  5412. {
  5413. UINT fRedraw = (plv->flags & LVF_ERASE) ? RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW : RDW_UPDATENOW|RDW_INVALIDATE;
  5414. if (plv->hrgnInval == (HRGN)ENTIRE_REGION)
  5415. plv->hrgnInval = NULL;
  5416. RedrawWindow(plv->ci.hwnd, NULL, plv->hrgnInval, fRedraw);
  5417. ListView_DeleteHrgnInval(plv);
  5418. fChanges = TRUE;
  5419. }
  5420. plv->flags &= ~LVF_ERASE;
  5421. // now deal with the optimized stuff
  5422. if (ListView_IsListView(plv) || ListView_IsReportView(plv))
  5423. {
  5424. if (plv->iFirstChangedNoRedraw != -1)
  5425. {
  5426. // We may try to resize the column
  5427. if (!ListView_MaybeResizeListColumns(plv, plv->iFirstChangedNoRedraw,
  5428. ListView_Count(plv)-1))
  5429. ListView_OnUpdate(plv, plv->iFirstChangedNoRedraw);
  5430. }
  5431. else
  5432. ListView_UpdateScrollBars(plv);
  5433. } else {
  5434. int iCount;
  5435. if (plv->iFirstChangedNoRedraw != -1) {
  5436. for (iCount = ListView_Count(plv) ; plv->iFirstChangedNoRedraw < iCount; plv->iFirstChangedNoRedraw++) {
  5437. ListView_InvalidateItem(plv, plv->iFirstChangedNoRedraw, FALSE, RDW_INVALIDATE);
  5438. }
  5439. fChanges = TRUE;
  5440. }
  5441. if (fChanges)
  5442. ListView_RecalcRegion(plv, TRUE, TRUE);
  5443. if ((plv->ci.style & LVS_AUTOARRANGE) && fChanges) {
  5444. ListView_OnUpdate(plv, plv->iFirstChangedNoRedraw);
  5445. } else {
  5446. ListView_UpdateScrollBars(plv);
  5447. }
  5448. }
  5449. }
  5450. }
  5451. else
  5452. {
  5453. plv->iFirstChangedNoRedraw = -1;
  5454. plv->flags &= ~LVF_REDRAW;
  5455. }
  5456. }
  5457. HIMAGELIST NEAR ListView_OnGetImageList(LV* plv, int iImageList)
  5458. {
  5459. switch (iImageList)
  5460. {
  5461. case LVSIL_NORMAL:
  5462. return plv->himl;
  5463. case LVSIL_SMALL:
  5464. return plv->himlSmall;
  5465. case LVSIL_STATE:
  5466. return plv->himlState;
  5467. }
  5468. ASSERT(0);
  5469. return NULL;
  5470. }
  5471. HIMAGELIST NEAR ListView_OnSetImageList(LV* plv, HIMAGELIST himl, int iImageList)
  5472. {
  5473. HIMAGELIST hImageOld = NULL;
  5474. switch (iImageList)
  5475. {
  5476. case LVSIL_NORMAL:
  5477. hImageOld = plv->himl;
  5478. plv->himl = himl;
  5479. if (himl) {
  5480. ImageList_GetIconSize(himl, &plv->cxIcon , &plv->cyIcon);
  5481. if (!(plv->flags & LVF_ICONSPACESET)) {
  5482. ListView_OnSetIconSpacing(plv, (LPARAM)-1);
  5483. }
  5484. }
  5485. break;
  5486. case LVSIL_SMALL:
  5487. hImageOld = plv->himlSmall;
  5488. plv->himlSmall = himl;
  5489. if (himl)
  5490. ImageList_GetIconSize(himl, &plv->cxSmIcon , &plv->cySmIcon);
  5491. plv->cxItem = 16 * plv->cxLabelChar + plv->cxSmIcon;
  5492. plv->cyItem = ListView_ComputeCYItemSize(plv);
  5493. if (plv->hwndHdr)
  5494. SendMessage(plv->hwndHdr, HDM_SETIMAGELIST, 0, (LPARAM)himl);
  5495. break;
  5496. case LVSIL_STATE:
  5497. if (himl) {
  5498. ImageList_GetIconSize(himl, &plv->cxState , &plv->cyState);
  5499. } else {
  5500. plv->cxState = 0;
  5501. }
  5502. hImageOld = plv->himlState;
  5503. plv->himlState = himl;
  5504. plv->cyItem = ListView_ComputeCYItemSize(plv);
  5505. break;
  5506. default:
  5507. #ifdef LVDEBUG
  5508. DebugMsg(TF_LISTVIEW, TEXT("sh TR - LVM_SETIMAGELIST: unrecognized iImageList"));
  5509. #endif
  5510. break;
  5511. }
  5512. if (himl && !(plv->ci.style & LVS_SHAREIMAGELISTS))
  5513. ImageList_SetBkColor(himl, plv->clrBk);
  5514. if (ListView_Count(plv) > 0)
  5515. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  5516. return hImageOld;
  5517. }
  5518. BOOL NEAR ListView_OnGetItemA(LV* plv, LV_ITEMA *plvi) {
  5519. LPWSTR pszW = NULL;
  5520. LPSTR pszC = NULL;
  5521. BOOL fRet;
  5522. //HACK ALERT -- this code assumes that LV_ITEMA is exactly the same
  5523. // as LV_ITEMW except for the pointer to the string.
  5524. ASSERT(sizeof(LV_ITEMA) == sizeof(LV_ITEMW))
  5525. if (!plvi)
  5526. return FALSE;
  5527. if ((plvi->mask & LVIF_TEXT) && (plvi->pszText != NULL)) {
  5528. pszC = plvi->pszText;
  5529. pszW = LocalAlloc(LMEM_FIXED, plvi->cchTextMax * sizeof(WCHAR));
  5530. if (pszW == NULL)
  5531. return FALSE;
  5532. plvi->pszText = (LPSTR)pszW;
  5533. }
  5534. fRet = ListView_OnGetItem(plv, (LV_ITEM *) plvi);
  5535. if (pszW)
  5536. {
  5537. if (plvi->pszText != LPSTR_TEXTCALLBACKA)
  5538. {
  5539. if (fRet && plvi->cchTextMax)
  5540. ConvertWToAN(plv->ci.uiCodePage, pszC, plvi->cchTextMax, (LPWSTR)plvi->pszText, -1);
  5541. plvi->pszText = pszC;
  5542. }
  5543. LocalFree(pszW);
  5544. }
  5545. return fRet;
  5546. }
  5547. BOOL NEAR ListView_OnGetItem(LV* plv, LV_ITEM FAR* plvi)
  5548. {
  5549. UINT mask;
  5550. LISTITEM FAR* pitem = NULL;
  5551. LV_DISPINFO nm;
  5552. if (!plvi)
  5553. {
  5554. RIPMSG(0, "LVM_GET(ITEM|ITEMTEXT): Invalid pitem = NULL");
  5555. return FALSE;
  5556. }
  5557. if (!ListView_IsValidItemNumber(plv, plvi->iItem))
  5558. {
  5559. #ifdef DEBUG
  5560. // owner data views (e.g. docfind) may change the number of items in listview
  5561. // while we are doing something, thus hitting this rip
  5562. if (!ListView_IsOwnerData(plv))
  5563. RIPMSG(0, "LVM_GET(ITEM|ITEMTEXT|ITEMSTATE): item=%d does not exist", plvi->iItem);
  5564. #endif
  5565. return FALSE;
  5566. }
  5567. nm.item.mask = 0;
  5568. mask = plvi->mask;
  5569. if (!ListView_IsOwnerData(plv))
  5570. {
  5571. // Standard listviews
  5572. pitem = ListView_FastGetItemPtr(plv, plvi->iItem);
  5573. ASSERT(pitem);
  5574. // Handle sub-item cases for report view
  5575. //
  5576. if (plvi->iSubItem != 0)
  5577. {
  5578. LISTSUBITEM lsi;
  5579. ListView_GetSubItem(plv, plvi->iItem, plvi->iSubItem, &lsi);
  5580. if (mask & LVIF_TEXT)
  5581. {
  5582. if (lsi.pszText != LPSTR_TEXTCALLBACK)
  5583. {
  5584. Str_GetPtr0(lsi.pszText, plvi->pszText, plvi->cchTextMax);
  5585. } else {
  5586. // if this is LVIF_NORECOMPUTE we will update pszText later
  5587. nm.item.mask |= LVIF_TEXT;
  5588. }
  5589. }
  5590. if ((mask & LVIF_IMAGE) && (plv->exStyle & LVS_EX_SUBITEMIMAGES))
  5591. {
  5592. plvi->iImage = lsi.iImage;
  5593. if (lsi.iImage == I_IMAGECALLBACK)
  5594. nm.item.mask |= LVIF_IMAGE;
  5595. }
  5596. if (mask & LVIF_STATE) {
  5597. if (ListView_FullRowSelect(plv)) {
  5598. // if we're in full row select,
  5599. // the state bit for select and focus follows column 0.
  5600. lsi.state |= pitem->state & (LVIS_SELECTED | LVIS_FOCUSED | LVIS_DROPHILITED);
  5601. }
  5602. plvi->state = lsi.state & plvi->stateMask;
  5603. if (plv->stateCallbackMask)
  5604. {
  5605. nm.item.stateMask = (plvi->stateMask & plv->stateCallbackMask);
  5606. if (nm.item.stateMask)
  5607. {
  5608. nm.item.mask |= LVIF_STATE;
  5609. nm.item.state = 0;
  5610. }
  5611. }
  5612. }
  5613. } else {
  5614. if (mask & LVIF_TEXT)
  5615. {
  5616. if (pitem->pszText != LPSTR_TEXTCALLBACK)
  5617. {
  5618. Str_GetPtr0(pitem->pszText, plvi->pszText, plvi->cchTextMax);
  5619. } else {
  5620. // if this is LVIF_NORECOMPUTE we will update pszText later
  5621. nm.item.mask |= LVIF_TEXT;
  5622. }
  5623. }
  5624. if (mask & LVIF_IMAGE)
  5625. {
  5626. plvi->iImage = pitem->iImage;
  5627. if (pitem->iImage == I_IMAGECALLBACK)
  5628. nm.item.mask |= LVIF_IMAGE;
  5629. }
  5630. if (mask & LVIF_INDENT)
  5631. {
  5632. plvi->iIndent = pitem->iIndent;
  5633. if (pitem->iIndent == I_INDENTCALLBACK)
  5634. nm.item.mask |= LVIF_INDENT;
  5635. }
  5636. if (mask & LVIF_STATE)
  5637. {
  5638. plvi->state = (pitem->state & plvi->stateMask);
  5639. if (plv->stateCallbackMask)
  5640. {
  5641. nm.item.stateMask = (plvi->stateMask & plv->stateCallbackMask);
  5642. if (nm.item.stateMask)
  5643. {
  5644. nm.item.mask |= LVIF_STATE;
  5645. nm.item.state = 0;
  5646. }
  5647. }
  5648. }
  5649. }
  5650. if (mask & LVIF_PARAM)
  5651. plvi->lParam = pitem->lParam;
  5652. }
  5653. else
  5654. {
  5655. // Complete call back for info...
  5656. // Handle sub-item cases for report view
  5657. //
  5658. if (plvi->iSubItem != 0)
  5659. {
  5660. // if there are no subitem images, don't query for them
  5661. if (!(plv->exStyle & LVS_EX_SUBITEMIMAGES))
  5662. mask &= ~LVIF_IMAGE;
  5663. // don't allow indent on the non-0th column
  5664. mask &= ~LVIF_INDENT;
  5665. }
  5666. if (mask & LVIF_PARAM)
  5667. plvi->lParam = 0L; // Dont have any to return now...
  5668. if (mask & LVIF_STATE)
  5669. {
  5670. plvi->state = 0;
  5671. if ((plvi->iSubItem == 0) || ListView_FullRowSelect(plv))
  5672. {
  5673. if (plvi->iItem == plv->iFocus)
  5674. plvi->state |= LVIS_FOCUSED;
  5675. if (plv->plvrangeSel->lpVtbl->IsSelected(plv->plvrangeSel, plvi->iItem) == S_OK)
  5676. plvi->state |= LVIS_SELECTED;
  5677. if (plv->plvrangeCut->lpVtbl->IsSelected(plv->plvrangeCut, plvi->iItem) == S_OK)
  5678. plvi->state |= LVIS_CUT;
  5679. if (plvi->iItem == plv->iDropHilite)
  5680. plvi->state |= LVIS_DROPHILITED;
  5681. plvi->state &= plvi->stateMask;
  5682. }
  5683. if (plv->stateCallbackMask)
  5684. {
  5685. nm.item.stateMask = (plvi->stateMask & plv->stateCallbackMask);
  5686. if (nm.item.stateMask)
  5687. {
  5688. nm.item.mask |= LVIF_STATE;
  5689. nm.item.state = 0;
  5690. }
  5691. }
  5692. }
  5693. nm.item.mask |= (mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_INDENT));
  5694. }
  5695. if (mask & LVIF_NORECOMPUTE)
  5696. {
  5697. if (nm.item.mask & LVIF_TEXT)
  5698. plvi->pszText = LPSTR_TEXTCALLBACK;
  5699. }
  5700. else if (nm.item.mask)
  5701. {
  5702. nm.item.iItem = plvi->iItem;
  5703. nm.item.iSubItem = plvi->iSubItem;
  5704. if (ListView_IsOwnerData( plv ))
  5705. nm.item.lParam = 0L;
  5706. else
  5707. nm.item.lParam = pitem->lParam;
  5708. // just in case LVIF_IMAGE is set and callback doesn't fill it in
  5709. // ... we'd rather have a -1 than whatever garbage is on the stack
  5710. nm.item.iImage = -1;
  5711. nm.item.iIndent = 0;
  5712. if (nm.item.mask & LVIF_TEXT)
  5713. {
  5714. RIPMSG(plvi->pszText != NULL, "LVM_GET(ITEM|ITEMTEXT) null string pointer");
  5715. if (plvi->pszText) {
  5716. nm.item.pszText = plvi->pszText;
  5717. nm.item.cchTextMax = plvi->cchTextMax;
  5718. // Make sure the buffer is zero terminated...
  5719. if (nm.item.cchTextMax)
  5720. *nm.item.pszText = 0;
  5721. } else {
  5722. // Don't make caller smash null pointer
  5723. nm.item.mask &= ~LVIF_TEXT;
  5724. }
  5725. }
  5726. CCSendNotify(&plv->ci, LVN_GETDISPINFO, &nm.hdr);
  5727. // use nm.item.mask to give the app a chance to change values
  5728. if (nm.item.mask & LVIF_INDENT)
  5729. plvi->iIndent = nm.item.iIndent;
  5730. if (nm.item.mask & LVIF_STATE)
  5731. plvi->state ^= ((plvi->state ^ nm.item.state) & nm.item.stateMask);
  5732. if (nm.item.mask & LVIF_IMAGE)
  5733. plvi->iImage = nm.item.iImage;
  5734. if (nm.item.mask & LVIF_TEXT)
  5735. if (mask & LVIF_TEXT)
  5736. plvi->pszText = CCReturnDispInfoText(nm.item.pszText, plvi->pszText, plvi->cchTextMax);
  5737. else
  5738. plvi->pszText = nm.item.pszText;
  5739. if (pitem && (nm.item.mask & LVIF_DI_SETITEM))
  5740. {
  5741. // BUGBUG HACKHACK
  5742. //
  5743. // The SendNotify above can set about a terrible series of events
  5744. // whereby asking for DISPINFO causes the shell to look around
  5745. // (call peekmessage) to see if its got a new async icon for the
  5746. // listview. This lets other messages be delivered, such as an
  5747. // UPDATEIMAGE of Index == -1 (if the user is changing icon sizing
  5748. // at the same time). This causes a re-enumeration of the desktop
  5749. // and hence this very listview is torn down and rebuilt while
  5750. // we're sitting here for the DISPINFO to finish. Thus, as a cheap
  5751. // and dirty solution, I check to see if the item I think I have
  5752. // is the same one I had when I made the notify, and if not, I
  5753. // bail. Don't blame me, I'm just cleaning up the mess.
  5754. if (!EVAL(pitem == ListView_GetItemPtr(plv, plvi->iItem)))
  5755. {
  5756. return FALSE;
  5757. }
  5758. if (nm.item.iSubItem == 0)
  5759. {
  5760. //DebugMsg(TF_LISTVIEW, "SAVING ITEMS!");
  5761. if (nm.item.mask & LVIF_IMAGE)
  5762. pitem->iImage = (short) nm.item.iImage;
  5763. if (nm.item.mask & LVIF_INDENT)
  5764. pitem->iIndent = (short) nm.item.iIndent;
  5765. if (nm.item.mask & LVIF_TEXT)
  5766. if (nm.item.pszText) {
  5767. Str_Set(&pitem->pszText, nm.item.pszText);
  5768. }
  5769. if (nm.item.mask & LVIF_STATE)
  5770. pitem->state ^= ((pitem->state ^ nm.item.state) & nm.item.stateMask);
  5771. }
  5772. else
  5773. {
  5774. ListView_SetSubItem(plv, &nm.item);
  5775. }
  5776. }
  5777. }
  5778. return TRUE;
  5779. }
  5780. BOOL NEAR ListView_OnSetItemA(LV* plv, LV_ITEMA FAR* plvi) {
  5781. LPWSTR pszW = NULL;
  5782. LPSTR pszC = NULL;
  5783. BOOL fRet;
  5784. // Let ListView_OnSetItem() handle owner-data validation
  5785. //HACK ALERT -- this code assumes that LV_ITEMA is exactly the same
  5786. // as LV_ITEMW except for the pointer to the string.
  5787. ASSERT(sizeof(LV_ITEMA) == sizeof(LV_ITEMW));
  5788. if (!plvi)
  5789. return FALSE;
  5790. if ((plvi->mask & LVIF_TEXT) && (plvi->pszText != NULL)) {
  5791. pszC = plvi->pszText;
  5792. pszW = ProduceWFromA(plv->ci.uiCodePage, pszC);
  5793. if (pszW == NULL)
  5794. return FALSE;
  5795. plvi->pszText = (LPSTR)pszW;
  5796. }
  5797. fRet = ListView_OnSetItem(plv, (const LV_ITEM FAR*) plvi);
  5798. if (pszW != NULL) {
  5799. plvi->pszText = pszC;
  5800. FreeProducedString(pszW);
  5801. }
  5802. return fRet;
  5803. }
  5804. BOOL NEAR ListView_OnSetItem(LV* plv, const LV_ITEM FAR* plvi)
  5805. {
  5806. LISTITEM FAR* pitem = NULL;
  5807. UINT mask;
  5808. UINT maskChanged;
  5809. UINT rdwFlags=RDW_INVALIDATE;
  5810. int i;
  5811. UINT stateOld, stateNew;
  5812. BOOL fFocused = FALSE;
  5813. BOOL fSelected = FALSE;
  5814. BOOL fStateImageChanged = FALSE;
  5815. if (ListView_IsOwnerData(plv)) {
  5816. RIPMSG(0, "LVM_SETITEM: Invalid for owner-data listview");
  5817. return FALSE;
  5818. }
  5819. if (!plvi)
  5820. return FALSE;
  5821. ASSERT(plvi->iSubItem >= 0);
  5822. if (plv->himl && (plv->clrBk != ImageList_GetBkColor(plv->himl)))
  5823. rdwFlags |= RDW_ERASE;
  5824. mask = plvi->mask;
  5825. if (!mask)
  5826. return TRUE;
  5827. // If we're setting a subitem, handle it elsewhere...
  5828. //
  5829. if (plvi->iSubItem > 0)
  5830. return ListView_SetSubItem(plv, plvi);
  5831. i = plvi->iItem;
  5832. ListView_InvalidateTTLastHit(plv, i);
  5833. pitem = ListView_GetItemPtr(plv, i);
  5834. if (!pitem)
  5835. return FALSE;
  5836. //REVIEW: This is a BOGUS HACK, and should be fixed.
  5837. //This incorrectly calculates the old state (since we may
  5838. // have to send LVN_GETDISPINFO to get it).
  5839. //
  5840. stateOld = stateNew = 0;
  5841. if (mask & LVIF_STATE)
  5842. {
  5843. stateOld = pitem->state & plvi->stateMask;
  5844. stateNew = plvi->state & plvi->stateMask;
  5845. }
  5846. // Prevent multiple selections in a single-select listview.
  5847. if ((plv->ci.style & LVS_SINGLESEL) && (mask & LVIF_STATE) && (stateNew & LVIS_SELECTED)) {
  5848. ListView_DeselectAll(plv, i);
  5849. // Refresh the old state information
  5850. stateOld = pitem->state & plvi->stateMask;
  5851. }
  5852. if (!ListView_SendChange(plv, i, 0, LVN_ITEMCHANGING, stateOld, stateNew, mask, pitem->lParam))
  5853. return FALSE;
  5854. maskChanged = 0;
  5855. if (mask & LVIF_STATE)
  5856. {
  5857. UINT change = (pitem->state ^ plvi->state) & plvi->stateMask;
  5858. if (change)
  5859. {
  5860. pitem->state ^= change;
  5861. maskChanged |= LVIF_STATE;
  5862. // the selection state has changed.. update selected count
  5863. if (change & LVIS_SELECTED)
  5864. {
  5865. fSelected = TRUE;
  5866. if (pitem->state & LVIS_SELECTED) {
  5867. plv->nSelected++;
  5868. } else {
  5869. if (plv->nSelected > 0)
  5870. plv->nSelected--;
  5871. }
  5872. }
  5873. // For some bits we can only invert the label area...
  5874. // fSelectOnlyChange = ((change & ~(LVIS_SELECTED | LVIS_FOCUSED | LVIS_DROPHILITED)) == 0);
  5875. // fEraseItem = ((change & ~(LVIS_SELECTED | LVIS_DROPHILITED)) != 0);
  5876. // try to steal focus from the previous guy.
  5877. if (change & LVIS_FOCUSED)
  5878. {
  5879. BOOL fUnfolded = ListView_IsItemUnfolded(plv, plv->iFocus);
  5880. int iOldFocus = plv->iFocus;
  5881. RECT rcLabel;
  5882. fFocused = TRUE;
  5883. if (plv->iFocus != i) {
  5884. if ((plv->iFocus == -1) || ListView_OnSetItemState(plv, plv->iFocus, 0, LVIS_FOCUSED)) {
  5885. ASSERT(pitem->state & LVIS_FOCUSED);
  5886. plv->iFocus = i;
  5887. if (plv->iMark == -1)
  5888. plv->iMark = i;
  5889. } else {
  5890. fFocused = FALSE;
  5891. pitem->state &= ~LVIS_FOCUSED;
  5892. }
  5893. } else {
  5894. ASSERT(!(pitem->state & LVIS_FOCUSED));
  5895. plv->iFocus = -1;
  5896. }
  5897. // If we were previously unfolded and we move the focus we must
  5898. // attempt to refresh the previous focus owner to referect this change.
  5899. if (fUnfolded && !ListView_IsItemUnfolded(plv, iOldFocus) && (plv->iItemDrawing != iOldFocus))
  5900. {
  5901. ListView_GetUnfoldedRect(plv, iOldFocus, &rcLabel);
  5902. RedrawWindow(plv->ci.hwnd, &rcLabel, NULL, RDW_INVALIDATE|RDW_ERASE);
  5903. }
  5904. // Kill the tooltip if focus moves, it causes us headaches otherwise!
  5905. ListView_PopBubble(plv);
  5906. }
  5907. if (change & LVIS_CUT ||
  5908. plv->clrTextBk == CLR_NONE)
  5909. rdwFlags |= RDW_ERASE;
  5910. if (change & LVIS_OVERLAYMASK) {
  5911. // Overlay changed, so need to blow away icon region cache
  5912. if (pitem->hrgnIcon) {
  5913. if (pitem->hrgnIcon != (HANDLE) -1)
  5914. DeleteObject(pitem->hrgnIcon);
  5915. pitem->hrgnIcon = NULL;
  5916. }
  5917. }
  5918. fStateImageChanged = (change & LVIS_STATEIMAGEMASK);
  5919. }
  5920. }
  5921. if (mask & LVIF_TEXT)
  5922. {
  5923. // need to do this now because we're changing the text
  5924. // so we need to get the rect of the thing before the text changes
  5925. // but don't redraw the item we are currently painting
  5926. if (plv->iItemDrawing != i)
  5927. {
  5928. ListView_InvalidateItemEx(plv, i, FALSE,
  5929. RDW_INVALIDATE | RDW_ERASE, LVIF_TEXT);
  5930. }
  5931. if (!Str_Set(&pitem->pszText, plvi->pszText))
  5932. return FALSE;
  5933. plv->rcView.left = RECOMPUTE;
  5934. ListView_SetSRecompute(pitem);
  5935. maskChanged |= LVIF_TEXT;
  5936. }
  5937. if (mask & LVIF_INDENT) {
  5938. if (pitem->iIndent != plvi->iIndent)
  5939. {
  5940. pitem->iIndent = (short) plvi->iIndent;
  5941. maskChanged |= LVIF_INDENT;
  5942. if (ListView_IsReportView(plv))
  5943. rdwFlags |= RDW_ERASE;
  5944. }
  5945. }
  5946. if (mask & LVIF_IMAGE)
  5947. {
  5948. if (pitem->iImage != plvi->iImage)
  5949. {
  5950. pitem->iImage = (short) plvi->iImage;
  5951. maskChanged |= LVIF_IMAGE;
  5952. if (pitem->hrgnIcon) {
  5953. if (pitem->hrgnIcon != (HANDLE) -1)
  5954. DeleteObject(pitem->hrgnIcon);
  5955. pitem->hrgnIcon = NULL;
  5956. }
  5957. // erase if there was a set image
  5958. if (pitem->iImage != I_IMAGECALLBACK)
  5959. rdwFlags |= RDW_ERASE;
  5960. }
  5961. }
  5962. if (mask & LVIF_PARAM)
  5963. {
  5964. if (pitem->lParam != plvi->lParam)
  5965. {
  5966. pitem->lParam = plvi->lParam;
  5967. maskChanged |= LVIF_PARAM;
  5968. }
  5969. }
  5970. if (maskChanged)
  5971. {
  5972. // don't redraw the item we are currently painting
  5973. if (plv->iItemDrawing != i)
  5974. ListView_InvalidateItemEx(plv, i, FALSE, rdwFlags, maskChanged);
  5975. DebugMsg(DM_LVSENDCHANGE, TEXT("LV - SendChange %d %d %d %d"), i, stateOld, stateNew, maskChanged);
  5976. ListView_SendChange(plv, i, 0, LVN_ITEMCHANGED, stateOld, stateNew, maskChanged, pitem->lParam);
  5977. if (maskChanged & LVIF_TEXT)
  5978. MyNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, plv->ci.hwnd, OBJID_CLIENT, i+1);
  5979. if (maskChanged & LVIF_STATE)
  5980. {
  5981. if (fFocused)
  5982. ListView_NotifyFocusEvent(plv);
  5983. if (fSelected)
  5984. {
  5985. if (stateNew & LVIS_SELECTED)
  5986. {
  5987. MyNotifyWinEvent((plv->nSelected == 1) ? EVENT_OBJECT_SELECTION :
  5988. EVENT_OBJECT_SELECTIONADD, plv->ci.hwnd, OBJID_CLIENT, i+1);
  5989. }
  5990. else
  5991. {
  5992. MyNotifyWinEvent(EVENT_OBJECT_SELECTIONREMOVE, plv->ci.hwnd, OBJID_CLIENT, i+1);
  5993. }
  5994. }
  5995. if (fStateImageChanged)
  5996. MyNotifyWinEvent(EVENT_OBJECT_STATECHANGE, plv->ci.hwnd, OBJID_CLIENT, i+1);
  5997. }
  5998. }
  5999. return TRUE;
  6000. }
  6001. UINT NEAR PASCAL ListView_OnGetItemState(LV* plv, int i, UINT mask)
  6002. {
  6003. LV_ITEM lvi;
  6004. lvi.mask = LVIF_STATE;
  6005. lvi.stateMask = mask;
  6006. lvi.iItem = i;
  6007. lvi.iSubItem = 0;
  6008. if (!ListView_OnGetItem(plv, &lvi))
  6009. return 0;
  6010. return lvi.state;
  6011. }
  6012. BOOL NEAR PASCAL ListView_OnSetItemState(LV* plv, int i, UINT data, UINT mask)
  6013. {
  6014. UINT rdwFlags = RDW_INVALIDATE;
  6015. LV_ITEM lvi;
  6016. lvi.mask = LVIF_STATE;
  6017. lvi.state = data;
  6018. lvi.stateMask = mask;
  6019. lvi.iItem = i;
  6020. lvi.iSubItem = 0;
  6021. // if the item is -1, we will do it for all items. We special case
  6022. // a few cases here as to speed it up. For example if the mask is
  6023. // LVIS_SELECTED and data is zero it implies that we will deselect
  6024. // all items...
  6025. //
  6026. if (ListView_IsOwnerData( plv ))
  6027. {
  6028. UINT uOldData = 0;
  6029. // these are the only two we handled
  6030. mask &= (LVIS_SELECTED | LVIS_FOCUSED | LVIS_CUT | LVIS_DROPHILITED);
  6031. if (!mask)
  6032. return TRUE;
  6033. if (plv->clrTextBk == CLR_NONE
  6034. || (plv->himl && (plv->clrBk != ImageList_GetBkColor(plv->himl))))
  6035. {
  6036. rdwFlags |= RDW_ERASE;
  6037. }
  6038. if (i == -1)
  6039. {
  6040. // request selection state change for all
  6041. if (mask & LVIS_SELECTED)
  6042. {
  6043. if (data & LVIS_SELECTED)
  6044. { // set selection
  6045. if ((plv->ci.style & LVS_SINGLESEL))
  6046. { // cant make multiple selections in a single-select listview.
  6047. return( FALSE );
  6048. }
  6049. if (plv->cTotalItems)
  6050. {
  6051. if (FAILED(plv->plvrangeSel->lpVtbl->IncludeRange(plv->plvrangeSel, 0, plv->cTotalItems - 1)))
  6052. return( FALSE );
  6053. }
  6054. RedrawWindow( plv->ci.hwnd, NULL, NULL, rdwFlags );
  6055. }
  6056. else
  6057. { // clear selection
  6058. if (plv->nSelected > 0) {
  6059. ListView_InvalidateSelectedOrCutOwnerData(plv, plv->plvrangeSel);
  6060. if (FAILED(plv->plvrangeSel->lpVtbl->Clear( plv->plvrangeSel )))
  6061. return FALSE;
  6062. } else {
  6063. // if nothing was selected, then there's nothing to clear
  6064. // no change.
  6065. mask &= ~ LVIS_SELECTED;
  6066. }
  6067. }
  6068. uOldData |= (LVIS_SELECTED & (mask ^ data));
  6069. // Update our internal count to what the list thinks is the number selected...
  6070. plv->plvrangeSel->lpVtbl->CountIncluded(plv->plvrangeSel, &plv->nSelected);
  6071. }
  6072. // can maybe combine with above code...
  6073. if (mask & LVIS_CUT)
  6074. {
  6075. if (data & LVIS_CUT)
  6076. { // set selection
  6077. if (plv->cTotalItems)
  6078. if (FAILED(plv->plvrangeCut->lpVtbl->IncludeRange(plv->plvrangeCut, 0, plv->cTotalItems - 1)))
  6079. return( FALSE );
  6080. RedrawWindow( plv->ci.hwnd, NULL, NULL, rdwFlags );
  6081. }
  6082. else
  6083. { // clear selection
  6084. if (plv->plvrangeCut->lpVtbl->IsEmpty(plv->plvrangeCut) != S_OK) {
  6085. ListView_InvalidateSelectedOrCutOwnerData(plv, plv->plvrangeCut);
  6086. if (FAILED(plv->plvrangeCut->lpVtbl->Clear( plv->plvrangeCut)))
  6087. return( FALSE );
  6088. } else {
  6089. // if nothing was selected, then there's nothing to clear
  6090. // no change.
  6091. mask &= ~ LVIS_CUT;
  6092. }
  6093. }
  6094. uOldData |= (LVIS_CUT & (mask ^ data));
  6095. }
  6096. // request focus state change
  6097. if (mask & LVIS_FOCUSED)
  6098. {
  6099. if (data & LVIS_FOCUSED)
  6100. { // cant set focus to all
  6101. return( FALSE );
  6102. }
  6103. else if (plv->iFocus != -1)
  6104. {
  6105. int iOldFocus = plv->iFocus;
  6106. // clear focus
  6107. uOldData |= (LVIS_FOCUSED & (mask ^ data));
  6108. plv->iFocus = -1;
  6109. // notify that the old focus is being lost
  6110. DebugMsg(DM_LVSENDCHANGE, TEXT("VLV: LVN_ITEMCHANGED: %d %d %d"), iOldFocus, LVIS_FOCUSED, 0);
  6111. ListView_SendChange(plv, iOldFocus, 0, LVN_ITEMCHANGED, LVIS_FOCUSED, 0, LVIF_STATE, 0);
  6112. ListView_InvalidateFoldedItem(plv, iOldFocus, TRUE, RDW_INVALIDATE |RDW_ERASE);
  6113. }
  6114. }
  6115. if (mask & LVIS_DROPHILITED)
  6116. {
  6117. if (data & LVIS_DROPHILITED)
  6118. { // cant set focus to all
  6119. return( FALSE );
  6120. }
  6121. else if (plv->iDropHilite != -1)
  6122. {
  6123. int iOldDropHilite = plv->iDropHilite;
  6124. // clear focus
  6125. uOldData |= (LVIS_FOCUSED & (mask ^ data));
  6126. plv->iDropHilite = -1;
  6127. // notify that the old focus is being lost
  6128. ListView_SendChange(plv, iOldDropHilite, 0, LVN_ITEMCHANGED, LVIS_DROPHILITED, 0, LVIF_STATE, 0);
  6129. ListView_InvalidateFoldedItem(plv, iOldDropHilite, TRUE, RDW_INVALIDATE |RDW_ERASE);
  6130. }
  6131. }
  6132. // invalidate and notify if there was a change
  6133. if (uOldData ^ (data & mask)) {
  6134. DebugMsg(DM_LVSENDCHANGE, TEXT("VLV: LVN_ITEMCHANGED: %d %d %d"), i, uOldData, data);
  6135. ListView_SendChange(plv, i, 0, LVN_ITEMCHANGED, uOldData, data, LVIF_STATE, 0);
  6136. if (mask & LVIS_SELECTED)
  6137. {
  6138. // Tell accessibility, "Selection changed in a complex way"
  6139. // (There is no "select all" or "select none" notification)
  6140. MyNotifyWinEvent(EVENT_OBJECT_SELECTIONWITHIN, plv->ci.hwnd, OBJID_CLIENT, CHILDID_SELF);
  6141. }
  6142. }
  6143. }
  6144. else
  6145. {
  6146. if (!ListView_IsValidItemNumber(plv, i))
  6147. return (FALSE);
  6148. // request selection state change
  6149. // and the selection state is new...
  6150. if ((mask & LVIS_SELECTED)) {
  6151. if (((plv->plvrangeSel->lpVtbl->IsSelected(plv->plvrangeSel, i) == S_OK) ? LVIS_SELECTED : 0) ^ (data & LVIS_SELECTED))
  6152. {
  6153. if (data & LVIS_SELECTED)
  6154. { // set selection
  6155. if ((plv->ci.style & LVS_SINGLESEL))
  6156. {
  6157. // in single selection mode, we need to deselect everything else
  6158. if (!ListView_OnSetItemState(plv, -1, 0, LVIS_SELECTED))
  6159. return FALSE;
  6160. }
  6161. // now select the new item
  6162. if (FAILED(plv->plvrangeSel->lpVtbl->IncludeRange(plv->plvrangeSel, i, i)))
  6163. return FALSE;
  6164. }
  6165. else
  6166. { // clear selection
  6167. if (FAILED(plv->plvrangeSel->lpVtbl->ExcludeRange(plv->plvrangeSel, i, i )))
  6168. return( FALSE );
  6169. }
  6170. // something actually changed (or else we wouldn't be in this
  6171. // if block
  6172. uOldData |= (LVIS_SELECTED & (mask ^ data));
  6173. } else {
  6174. // nothing changed... so make the uOldData be the same for this bit
  6175. // else make this the same as
  6176. uOldData |= (LVIS_SELECTED & (mask & data));
  6177. }
  6178. // Update our internal count to what the list thinks is the number selected...
  6179. plv->plvrangeSel->lpVtbl->CountIncluded(plv->plvrangeSel, &plv->nSelected);
  6180. }
  6181. if ((mask & LVIS_CUT)) {
  6182. if (((plv->plvrangeCut->lpVtbl->IsSelected(plv->plvrangeCut, i) == S_OK) ? LVIS_CUT : 0) ^ (data & LVIS_CUT))
  6183. {
  6184. if (data & LVIS_CUT)
  6185. {
  6186. // now select the new item
  6187. if (FAILED(plv->plvrangeCut->lpVtbl->IncludeRange(plv->plvrangeCut, i, i )))
  6188. return FALSE;
  6189. }
  6190. else
  6191. { // clear selection
  6192. if (FAILED(plv->plvrangeCut->lpVtbl->ExcludeRange(plv->plvrangeCut, i, i )))
  6193. return( FALSE );
  6194. }
  6195. // something actually changed (or else we wouldn't be in this
  6196. // if block
  6197. uOldData |= (LVIS_CUT & (mask ^ data));
  6198. rdwFlags |= RDW_ERASE;
  6199. } else {
  6200. // nothing changed... so make the uOldData be the same for this bit
  6201. // else make this the same as
  6202. uOldData |= (LVIS_CUT & (mask & data));
  6203. }
  6204. }
  6205. // request focus state change
  6206. if (mask & LVIS_FOCUSED)
  6207. {
  6208. int iOldFocus = plv->iFocus;
  6209. if (data & LVIS_FOCUSED)
  6210. { // set focus
  6211. if (i != plv->iFocus)
  6212. {
  6213. // we didn't have the focus before
  6214. plv->iFocus = i;
  6215. if (plv->iMark == -1)
  6216. plv->iMark = i;
  6217. if (iOldFocus != -1) {
  6218. // we're stealing it from someone
  6219. // notify of the change
  6220. DebugMsg(DM_LVSENDCHANGE, TEXT("VLV: LVN_ITEMCHANGED: %d %d %d"), iOldFocus, LVIS_FOCUSED, 0);
  6221. ListView_SendChange(plv, iOldFocus, 0, LVN_ITEMCHANGED, LVIS_FOCUSED, 0, LVIF_STATE, 0);
  6222. }
  6223. } else {
  6224. // we DID have the focus before
  6225. uOldData |= LVIS_FOCUSED;
  6226. }
  6227. }
  6228. else
  6229. { // clear focus
  6230. if (i == plv->iFocus)
  6231. {
  6232. plv->iFocus = -1;
  6233. uOldData |= LVIS_FOCUSED;
  6234. }
  6235. }
  6236. }
  6237. // request focus state change
  6238. if (mask & LVIS_DROPHILITED)
  6239. {
  6240. int iOldDropHilite = plv->iDropHilite;
  6241. if (data & LVIS_DROPHILITED)
  6242. { // set Drop Hilite
  6243. if (i != plv->iDropHilite)
  6244. {
  6245. // we didn't have the Drop Hilite before
  6246. plv->iDropHilite = i;
  6247. if (iOldDropHilite != -1) {
  6248. // we're stealing it from someone
  6249. // notify of the change
  6250. ListView_SendChange(plv, iOldDropHilite, 0, LVN_ITEMCHANGED, LVIS_DROPHILITED, 0, LVIF_STATE, 0);
  6251. ListView_InvalidateFoldedItem(plv, iOldDropHilite, TRUE, RDW_INVALIDATE |RDW_ERASE);
  6252. }
  6253. } else {
  6254. // we DID have the Drop Hilite before
  6255. uOldData |= LVIS_DROPHILITED;
  6256. }
  6257. }
  6258. else
  6259. { // clear Drop Hilite
  6260. if (i == plv->iDropHilite)
  6261. {
  6262. plv->iDropHilite = -1;
  6263. uOldData |= LVIS_DROPHILITED;
  6264. }
  6265. }
  6266. }
  6267. // invalidate and notify if there was a change
  6268. if (uOldData ^ (data & mask)) {
  6269. DebugMsg(DM_LVSENDCHANGE, TEXT("VLV: LVN_ITEMCHANGED: %d %d %d"), i, uOldData, data);
  6270. ListView_SendChange(plv, i, 0, LVN_ITEMCHANGED, uOldData, data, LVIF_STATE, 0);
  6271. ListView_InvalidateItem(plv, i, TRUE, rdwFlags);
  6272. // Kill the tooltip if focus moves, it causes us headaches otherwise!
  6273. if ((uOldData ^ (data & mask)) & LVIS_FOCUSED)
  6274. {
  6275. ListView_PopBubble(plv);
  6276. ListView_NotifyFocusEvent(plv);
  6277. }
  6278. // Tell accessibility about the changes
  6279. if (mask & LVIS_SELECTED) {
  6280. UINT event;
  6281. if (data & LVIS_SELECTED) {
  6282. if (plv->nSelected == 1)
  6283. event = EVENT_OBJECT_SELECTION; // this object is the entire selection
  6284. else
  6285. event = EVENT_OBJECT_SELECTIONADD; // this object is selected
  6286. } else
  6287. event = EVENT_OBJECT_SELECTIONREMOVE; // this object is unselected
  6288. MyNotifyWinEvent(event, plv->ci.hwnd, OBJID_CLIENT, i + 1);
  6289. }
  6290. }
  6291. }
  6292. } else {
  6293. if (i != -1) {
  6294. return ListView_OnSetItem(plv, &lvi);
  6295. } else {
  6296. UINT flags = LVNI_ALL;
  6297. if (data == 0)
  6298. {
  6299. switch (mask)
  6300. {
  6301. case LVIS_SELECTED:
  6302. flags = LVNI_SELECTED;
  6303. break;
  6304. case LVIS_CUT:
  6305. flags = LVNI_CUT;
  6306. break;
  6307. }
  6308. }
  6309. else if ((plv->ci.style & LVS_SINGLESEL) && (mask == LVIS_SELECTED))
  6310. return FALSE; /* can't select all in single-select listview */
  6311. else if ((mask & data) & LVIS_FOCUSED) {
  6312. return FALSE; // can't set focus to everything
  6313. }
  6314. //
  6315. // Now iterate over all of the items that match our criteria and
  6316. // set their new value.
  6317. //
  6318. while ((lvi.iItem = ListView_OnGetNextItem(plv, lvi.iItem,
  6319. flags)) != -1) {
  6320. ListView_OnSetItem(plv, &lvi);
  6321. }
  6322. }
  6323. }
  6324. return(TRUE);
  6325. }
  6326. //
  6327. // Returns TRUE if the label of an item is not truncated (is unfolded) and FALSE
  6328. // otherwise. If FALSE is returned, it also fills the Unfolding text in pszText.
  6329. // If TRUE is returned, pszText is set to empty string.
  6330. //
  6331. BOOL ListView_IsItemUnfolded2(LV* plv, int iItem, int iSubItem, LPTSTR pszText, int cchTextMax)
  6332. {
  6333. BOOL bItemUnfolded = ListView_IsItemUnfolded(plv, iItem);
  6334. if (pszText && cchTextMax > 0) // Sanity checks on input params.
  6335. {
  6336. pszText[0] = 0;
  6337. if (!bItemUnfolded)
  6338. {
  6339. RECT rcLabel;
  6340. LV_ITEM item;
  6341. item.iItem = iItem;
  6342. item.iSubItem = iSubItem;
  6343. item.mask = LVIF_TEXT | LVIF_PARAM;
  6344. if (!ListView_IsIconView(plv))
  6345. {
  6346. if (ListView_IsLabelTip(plv) || ListView_IsInfoTip(plv))
  6347. {
  6348. BOOL fSuccess;
  6349. rcLabel.left = LVIR_LABEL;
  6350. if (iSubItem) {
  6351. rcLabel.top = iSubItem;
  6352. fSuccess = ListView_OnGetSubItemRect(plv, iItem, &rcLabel);
  6353. } else {
  6354. fSuccess = ListView_OnGetItemRect(plv, iItem, &rcLabel);
  6355. }
  6356. if (fSuccess)
  6357. {
  6358. TCHAR szText[INFOTIPSIZE];
  6359. item.pszText = szText;
  6360. item.cchTextMax = min(ARRAYSIZE(szText), cchTextMax);
  6361. if (ListView_OnGetItem(plv, &item) && item.pszText != LPSTR_TEXTCALLBACK)
  6362. {
  6363. SIZE siz;
  6364. LVFAKEDRAW lvfd;
  6365. int cx;
  6366. ListView_BeginFakeCustomDraw(plv, &lvfd, &item);
  6367. ListView_BeginFakeItemDraw(&lvfd);
  6368. // ---------Label width----------- ---Client width---
  6369. cx = min(rcLabel.right - g_cxLabelMargin, plv->sizeClient.cx);
  6370. if (GetTextExtentPoint32(lvfd.nmcd.nmcd.hdc, item.pszText, lstrlen(item.pszText), &siz) &&
  6371. (rcLabel.left + g_cxLabelMargin + siz.cx) > cx)
  6372. {
  6373. StringCchCopy(pszText, item.cchTextMax, item.pszText);
  6374. }
  6375. else
  6376. {
  6377. // Not truncated after all
  6378. bItemUnfolded = TRUE;
  6379. }
  6380. ListView_EndFakeItemDraw(&lvfd);
  6381. ListView_EndFakeCustomDraw(&lvfd);
  6382. }
  6383. }
  6384. }
  6385. }
  6386. else
  6387. {
  6388. // Large icon view is the only one that folds
  6389. if (ListView_GetUnfoldedRect(plv, iItem, &rcLabel))
  6390. {
  6391. item.pszText = pszText;
  6392. item.cchTextMax = cchTextMax;
  6393. ListView_OnGetItem(plv, &item);
  6394. }
  6395. else
  6396. {
  6397. // Item was never folded
  6398. bItemUnfolded = TRUE;
  6399. }
  6400. }
  6401. }
  6402. }
  6403. return bItemUnfolded;
  6404. }
  6405. // Rather than thunking to ListView_OnGetItemText, we let ListView_GetItemA
  6406. // do the work.
  6407. int NEAR PASCAL ListView_OnGetItemTextA(LV* plv, int i, LV_ITEMA FAR *plvi)
  6408. {
  6409. if (!plvi)
  6410. return 0;
  6411. RIPMSG(plvi->pszText != NULL, "LVM_GETITEMTEXT null string pointer");
  6412. plvi->mask = LVIF_TEXT;
  6413. plvi->iItem = i;
  6414. if (!ListView_OnGetItemA(plv, plvi))
  6415. return 0;
  6416. return lstrlenA(plvi->pszText);
  6417. }
  6418. int NEAR PASCAL ListView_OnGetItemText(LV* plv, int i, LV_ITEM FAR *plvi)
  6419. {
  6420. if (!plvi)
  6421. return 0;
  6422. RIPMSG(plvi->pszText != NULL, "LVM_GETITEMTEXT null string pointer");
  6423. plvi->mask = LVIF_TEXT;
  6424. plvi->iItem = i;
  6425. if (!ListView_OnGetItem(plv, plvi))
  6426. return 0;
  6427. return lstrlen(plvi->pszText);
  6428. }
  6429. BOOL WINAPI ListView_OnSetItemTextA(LV* plv, int i, int iSubItem, LPCSTR pszText) {
  6430. LPWSTR pszW = NULL;
  6431. BOOL fRet;
  6432. // Let ListView_OnSetItemText() handle owner-data validation
  6433. if (pszText != NULL) {
  6434. pszW = ProduceWFromA(plv->ci.uiCodePage, pszText);
  6435. if (pszW == NULL) {
  6436. return FALSE;
  6437. }
  6438. }
  6439. fRet = ListView_OnSetItemText(plv, i, iSubItem, pszW);
  6440. FreeProducedString(pszW);
  6441. return fRet;
  6442. }
  6443. BOOL WINAPI ListView_OnSetItemText(LV* plv, int i, int iSubItem, LPCTSTR pszText)
  6444. {
  6445. LV_ITEM lvi;
  6446. if (ListView_IsOwnerData(plv))
  6447. {
  6448. RIPMSG(0, "LVM_SETITEMTEXT: Invalid for owner-data listview");
  6449. return FALSE;
  6450. }
  6451. ListView_InvalidateTTLastHit(plv, i);
  6452. lvi.mask = LVIF_TEXT;
  6453. lvi.pszText = (LPTSTR)pszText;
  6454. lvi.iItem = i;
  6455. lvi.iSubItem = iSubItem;
  6456. return ListView_OnSetItem(plv, &lvi);
  6457. }
  6458. VOID CALLBACK ImgCtxCallback(void * pvImgCtx, void * pvArg)
  6459. {
  6460. LV *plv = (LV *)pvArg;
  6461. ULONG ulState;
  6462. SIZE sizeImg;
  6463. IImgCtx *pImgCtx = plv->pImgCtx;
  6464. IImgCtx_GetStateInfo(pImgCtx, &ulState, &sizeImg, TRUE);
  6465. if (ulState & (IMGLOAD_STOPPED | IMGLOAD_ERROR))
  6466. {
  6467. TraceMsg(TF_BKIMAGE, "Error!");
  6468. plv->fImgCtxComplete = FALSE;
  6469. }
  6470. else if (ulState & IMGCHG_COMPLETE)
  6471. {
  6472. TraceMsg(TF_BKIMAGE, "Complete!");
  6473. plv->fImgCtxComplete = TRUE;
  6474. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  6475. }
  6476. }
  6477. void ListView_ReleaseBkImage(LV *plv)
  6478. {
  6479. if (plv->pImgCtx)
  6480. {
  6481. IImgCtx_Release(plv->pImgCtx);
  6482. plv->pImgCtx = NULL;
  6483. if (plv->hpalHalftone)
  6484. {
  6485. // No need to delete the half tone palette since we really
  6486. // share it with the image context and it will clean up.
  6487. plv->hpalHalftone = NULL;
  6488. }
  6489. }
  6490. if (plv->hbmBkImage)
  6491. {
  6492. DeleteObject(plv->hbmBkImage);
  6493. plv->hbmBkImage = NULL;
  6494. }
  6495. if (plv->pszBkImage)
  6496. {
  6497. LocalFree(plv->pszBkImage);
  6498. plv->pszBkImage = NULL;
  6499. }
  6500. }
  6501. BOOL WINAPI ListView_OnSetBkImage(LV* plv, LPLVBKIMAGE pbi)
  6502. {
  6503. LPCTSTR pszImage = pbi->pszImage;
  6504. BOOL fRet = FALSE;
  6505. LONG fl;
  6506. switch (pbi->ulFlags & LVBKIF_SOURCE_MASK)
  6507. {
  6508. case LVBKIF_SOURCE_NONE:
  6509. TraceMsg(TF_BKIMAGE, "LV SetBkImage to none");
  6510. ListView_ReleaseBkImage(plv);
  6511. break;
  6512. case LVBKIF_SOURCE_HBITMAP:
  6513. TraceMsg(TF_BKIMAGE, "LV SetBkImage to hBitmap %08lX", pbi->hbm);
  6514. ListView_ReleaseBkImage(plv);
  6515. if (pbi->hbm)
  6516. {
  6517. plv->hbmBkImage = pbi->hbm;
  6518. ASSERT(0); // KenSy hasn't implemented init from bitmap yet...
  6519. }
  6520. else
  6521. {
  6522. pbi->ulFlags &= ~LVBKIF_SOURCE_HBITMAP;
  6523. }
  6524. break;
  6525. case LVBKIF_SOURCE_URL:
  6526. TraceMsg(TF_BKIMAGE, "LV SetBkImage to URL");
  6527. ListView_ReleaseBkImage(plv);
  6528. if (pszImage && pszImage[0])
  6529. {
  6530. HRESULT (*pfnCoCreateInstance)(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID *);
  6531. HRESULT hr;
  6532. HMODULE hmodOLE;
  6533. DWORD cch = lstrlen(pszImage) + 1;
  6534. plv->pszBkImage = LocalAlloc(LPTR, cch * sizeof(TCHAR));
  6535. if (plv->pszBkImage == NULL)
  6536. {
  6537. TraceMsg(TF_BKIMAGE, "Wow, could not allocate memory for string!");
  6538. return FALSE;
  6539. }
  6540. StringCchCopy(plv->pszBkImage, cch, pszImage);
  6541. if (((hmodOLE = GetModuleHandle(TEXT("OLE32"))) == NULL) ||
  6542. ((pfnCoCreateInstance = (HRESULT (*)(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID *))GetProcAddress(hmodOLE, "CoCreateInstance")) == NULL))
  6543. {
  6544. TraceMsg(TF_BKIMAGE, "Could not find CoCreateInstance!");
  6545. TraceMsg(TF_BKIMAGE, "Did the caller remember to call CoInitialize?");
  6546. return FALSE;
  6547. }
  6548. hr = pfnCoCreateInstance(&CLSID_IImgCtx, NULL, CLSCTX_INPROC_SERVER,
  6549. &IID_IImgCtx, (LPVOID *)&plv->pImgCtx);
  6550. if (FAILED(hr))
  6551. {
  6552. TraceMsg(TF_BKIMAGE, "Could not create a pImgCtx!");
  6553. TraceMsg(TF_BKIMAGE, "Did you remember to register IEIMAGE.DLL?");
  6554. return FALSE;
  6555. }
  6556. //
  6557. // Mirror the downloaded image if the listview window is RTL mirrored,
  6558. // so that it would be displayed as is. [samera]
  6559. //
  6560. fl = ((IS_WINDOW_RTL_MIRRORED(plv->ci.hwnd)) ? DWN_MIRRORIMAGE : 0);
  6561. hr = IImgCtx_Load(plv->pImgCtx, pszImage, fl);
  6562. if (FAILED(hr))
  6563. {
  6564. IImgCtx_Release(plv->pImgCtx);
  6565. plv->pImgCtx = NULL;
  6566. TraceMsg(TF_BKIMAGE, "Could not init a pImgCtx!");
  6567. return FALSE;
  6568. }
  6569. }
  6570. else
  6571. {
  6572. pbi->ulFlags &= ~LVBKIF_SOURCE_URL;
  6573. }
  6574. break;
  6575. default:
  6576. RIPMSG(0, "LVM_SETBKIMAGE: Unsupported image type %d", pbi->ulFlags & LVBKIF_SOURCE_MASK);
  6577. return FALSE;
  6578. }
  6579. plv->ulBkImageFlags = pbi->ulFlags;
  6580. plv->xOffsetPercent = pbi->xOffsetPercent;
  6581. plv->yOffsetPercent = pbi->yOffsetPercent;
  6582. //
  6583. // If we actually created a pImgCtx, initialize it here.
  6584. //
  6585. if (plv->pImgCtx)
  6586. {
  6587. if (plv->hpalHalftone == NULL)
  6588. {
  6589. IImgCtx_GetPalette(plv->pImgCtx, &plv->hpalHalftone);
  6590. }
  6591. plv->fImgCtxComplete = FALSE;
  6592. IImgCtx_SetCallback(plv->pImgCtx, ImgCtxCallback, plv);
  6593. IImgCtx_SelectChanges(plv->pImgCtx, IMGCHG_COMPLETE, 0, TRUE);
  6594. TraceMsg(TF_BKIMAGE, " SUCCESS!");
  6595. fRet = TRUE;
  6596. }
  6597. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  6598. return fRet;
  6599. }
  6600. BOOL WINAPI ListView_OnSetBkImageA(LV* plv, LPLVBKIMAGEA pbiA)
  6601. {
  6602. BOOL fProducedString = FALSE;
  6603. BOOL fRet;
  6604. LVBKIMAGEW biW;
  6605. CopyMemory(&biW, pbiA, SIZEOF(LVBKIMAGE));
  6606. switch (biW.ulFlags & LVBKIF_SOURCE_MASK)
  6607. {
  6608. case LVBKIF_SOURCE_NONE:
  6609. case LVBKIF_SOURCE_HBITMAP:
  6610. break;
  6611. case LVBKIF_SOURCE_URL:
  6612. if (biW.pszImage != NULL)
  6613. {
  6614. biW.pszImage = ProduceWFromA(plv->ci.uiCodePage, (LPCSTR)biW.pszImage);
  6615. if (biW.pszImage == (LPARAM)NULL)
  6616. {
  6617. return FALSE;
  6618. }
  6619. fProducedString = TRUE;
  6620. }
  6621. break;
  6622. default:
  6623. // Let ListView_OnSetBkImage() complain about the invalid parameter
  6624. break;
  6625. }
  6626. fRet = ListView_OnSetBkImage(plv, &biW);
  6627. if (fProducedString)
  6628. {
  6629. FreeProducedString((LPVOID)biW.pszImage);
  6630. }
  6631. return fRet;
  6632. }
  6633. BOOL WINAPI ListView_OnGetBkImage(LV* plv, LPLVBKIMAGE pbi)
  6634. {
  6635. BOOL fRet = FALSE;
  6636. if (!IsBadWritePtr(pbi, sizeof(*pbi)))
  6637. {
  6638. pbi->ulFlags = plv->ulBkImageFlags;
  6639. switch (plv->ulBkImageFlags & LVBKIF_SOURCE_MASK)
  6640. {
  6641. case LVBKIF_SOURCE_NONE:
  6642. fRet = TRUE;
  6643. break;
  6644. case LVBKIF_SOURCE_HBITMAP:
  6645. pbi->hbm = plv->hbmBkImage;
  6646. fRet = TRUE;
  6647. break;
  6648. case LVBKIF_SOURCE_URL:
  6649. if (!IsBadWritePtr(pbi->pszImage, pbi->cchImageMax * SIZEOF(TCHAR)))
  6650. {
  6651. StringCchCopy(pbi->pszImage, pbi->cchImageMax, plv->pszBkImage);
  6652. fRet = TRUE;
  6653. }
  6654. break;
  6655. default:
  6656. ASSERT(0);
  6657. break;
  6658. }
  6659. pbi->xOffsetPercent = plv->xOffsetPercent;
  6660. pbi->yOffsetPercent = plv->yOffsetPercent;
  6661. }
  6662. return fRet;
  6663. }
  6664. BOOL WINAPI ListView_OnGetBkImageA(LV* plv, LPLVBKIMAGEA pbiA)
  6665. {
  6666. BOOL fRet = FALSE;
  6667. if (!IsBadWritePtr(pbiA, sizeof(*pbiA)))
  6668. {
  6669. pbiA->ulFlags = plv->ulBkImageFlags;
  6670. switch (plv->ulBkImageFlags & LVBKIF_SOURCE_MASK)
  6671. {
  6672. case LVBKIF_SOURCE_NONE:
  6673. fRet = TRUE;
  6674. break;
  6675. case LVBKIF_SOURCE_HBITMAP:
  6676. pbiA->hbm = plv->hbmBkImage;
  6677. fRet = TRUE;
  6678. break;
  6679. case LVBKIF_SOURCE_URL:
  6680. if (!IsBadWritePtr(pbiA->pszImage, pbiA->cchImageMax))
  6681. {
  6682. ConvertWToAN(plv->ci.uiCodePage, pbiA->pszImage,
  6683. pbiA->cchImageMax, plv->pszBkImage, -1);
  6684. fRet = TRUE;
  6685. }
  6686. break;
  6687. default:
  6688. ASSERT(0);
  6689. break;
  6690. }
  6691. pbiA->xOffsetPercent = plv->xOffsetPercent;
  6692. pbiA->yOffsetPercent = plv->yOffsetPercent;
  6693. }
  6694. return fRet;
  6695. }
  6696. void ListView_FreeSubItem(PLISTSUBITEM plsi)
  6697. {
  6698. if (plsi) {
  6699. Str_Set(&plsi->pszText, NULL);
  6700. LocalFree(plsi);
  6701. }
  6702. }
  6703. int NEAR ListView_GetCxScrollbar(LV* plv)
  6704. {
  6705. int cx;
  6706. if (((plv->exStyle & LVS_EX_FLATSB) == 0) ||
  6707. !FlatSB_GetScrollProp(plv->ci.hwnd, WSB_PROP_CXVSCROLL, &cx))
  6708. {
  6709. cx = g_cxScrollbar;
  6710. }
  6711. return cx;
  6712. }
  6713. int NEAR ListView_GetCyScrollbar(LV* plv)
  6714. {
  6715. int cy;
  6716. if (((plv->exStyle & LVS_EX_FLATSB) == 0) ||
  6717. !FlatSB_GetScrollProp(plv->ci.hwnd, WSB_PROP_CYHSCROLL, &cy))
  6718. {
  6719. cy = g_cyScrollbar;
  6720. }
  6721. return cy;
  6722. }
  6723. DWORD NEAR ListView_GetWindowStyle(LV* plv)
  6724. {
  6725. DWORD dwStyle;
  6726. if (((plv->exStyle & LVS_EX_FLATSB) == 0) ||
  6727. !FlatSB_GetScrollProp(plv->ci.hwnd, WSB_PROP_WINSTYLE, (LPINT)&dwStyle))
  6728. {
  6729. dwStyle = GetWindowStyle(plv->ci.hwnd);
  6730. }
  6731. return dwStyle;
  6732. }
  6733. int ListView_SetScrollInfo(LV *plv, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw)
  6734. {
  6735. int iRc;
  6736. if (plv->exStyle & LVS_EX_FLATSB)
  6737. {
  6738. iRc = FlatSB_SetScrollInfo(plv->ci.hwnd, fnBar, lpsi, fRedraw);
  6739. }
  6740. else
  6741. {
  6742. iRc = SetScrollInfo(plv->ci.hwnd, fnBar, lpsi, fRedraw);
  6743. }
  6744. //
  6745. // You'd think we were finished, but in fact the game is only half over.
  6746. //
  6747. // Some apps (e.g., Font Folder) will do
  6748. //
  6749. // SetWindowLong(hwnd, GWL_STYLE, newStyle);
  6750. //
  6751. // where newStyle toggles the WS_HSCROLL and/or WS_VSCROLL bits.
  6752. // This causes USER's internal bookkeeping to go completely out
  6753. // of whack: The ScrollInfo says that there is a scrollbar, but
  6754. // the window style says there isn't, or vice versa. The result
  6755. // is that we get a scrollbar when we shouldn't or vice versa.
  6756. //
  6757. // So each time we tweak the scroll info in a manner that changes
  6758. // the range and page, we kick USER in the head to make sure USER's
  6759. // view of the world (via style bits) is the same as the scroll
  6760. // bar's view of the world (via SCROLLINFO).
  6761. //
  6762. //
  6763. // We should always change SIF_PAGE and SIF_RANGE at the same time.
  6764. //
  6765. ASSERT((lpsi->fMask & (SIF_PAGE | SIF_RANGE)) == 0 ||
  6766. (lpsi->fMask & (SIF_PAGE | SIF_RANGE)) == (SIF_PAGE | SIF_RANGE));
  6767. if ((lpsi->fMask & (SIF_PAGE | SIF_RANGE)) == (SIF_PAGE | SIF_RANGE))
  6768. {
  6769. BOOL fShow;
  6770. fShow = lpsi->nMax && (int)lpsi->nPage <= lpsi->nMax;
  6771. #ifdef DEBUG
  6772. {
  6773. DWORD dwStyle, dwScroll, dwWant;
  6774. dwScroll = (fnBar == SB_VERT) ? WS_VSCROLL : WS_HSCROLL;
  6775. //
  6776. // We can short-circuit some logic with secret knowledge about how
  6777. // ListView uses SetScrollInfo.
  6778. //
  6779. ASSERT(lpsi->nMin == 0);
  6780. dwWant = fShow ? dwScroll : 0;
  6781. dwStyle = ListView_GetWindowStyle(plv);
  6782. if ((dwStyle & dwScroll) != dwWant)
  6783. {
  6784. TraceMsg(TF_LISTVIEW, "ListView_SetScrollInfo: App twiddled WS_[VH]SCROLL");
  6785. }
  6786. }
  6787. #endif
  6788. if (plv->exStyle & LVS_EX_FLATSB)
  6789. FlatSB_ShowScrollBar(plv->ci.hwnd, fnBar, fShow);
  6790. else
  6791. ShowScrollBar(plv->ci.hwnd, fnBar, fShow);
  6792. }
  6793. return iRc;
  6794. }
  6795. // Add/remove/replace item
  6796. BOOL NEAR ListView_FreeItem(LV* plv, LISTITEM FAR* pitem)
  6797. {
  6798. ASSERT( !ListView_IsOwnerData(plv));
  6799. if (pitem)
  6800. {
  6801. Str_Set(&pitem->pszText, NULL);
  6802. if (pitem->hrgnIcon && pitem->hrgnIcon!=(HANDLE)-1)
  6803. DeleteObject(pitem->hrgnIcon);
  6804. // NOTE: We never remove items from the image list; that's
  6805. // the app's responsibility.
  6806. // REVIEW: Should we do this? Or should we just provide
  6807. // a message that will adjust image indices for the guy
  6808. // when one is removed?
  6809. //
  6810. ControlFree(plv->hheap, pitem);
  6811. }
  6812. return FALSE;
  6813. }
  6814. LISTITEM FAR* NEAR ListView_CreateItem(LV* plv, const LV_ITEM FAR* plvi)
  6815. {
  6816. LISTITEM FAR* pitem = ControlAlloc(plv->hheap, sizeof(LISTITEM));
  6817. ASSERT(!ListView_IsOwnerData(plv));
  6818. if (pitem)
  6819. {
  6820. if (plvi->mask & LVIF_STATE) {
  6821. if (plvi->state & ~LVIS_ALL) {
  6822. DebugMsg(DM_ERROR, TEXT("ListView: Invalid state: %04x"), plvi->state);
  6823. return NULL;
  6824. }
  6825. // If adding a selected item to a single-select listview, deselect
  6826. // any other items.
  6827. if ((plv->ci.style & LVS_SINGLESEL) && (plvi->state & LVIS_SELECTED))
  6828. ListView_DeselectAll(plv, -1);
  6829. pitem->state = (plvi->state & ~(LVIS_FOCUSED | LVIS_SELECTED));
  6830. }
  6831. if (plvi->mask & LVIF_PARAM)
  6832. pitem->lParam = plvi->lParam;
  6833. if (plvi->mask & LVIF_IMAGE)
  6834. pitem->iImage = (short) plvi->iImage;
  6835. if (plvi->mask & LVIF_INDENT)
  6836. pitem->iIndent = (short) plvi->iIndent;
  6837. pitem->pt.x = pitem->pt.y = RECOMPUTE;
  6838. ListView_SetSRecompute(pitem);
  6839. pitem->pszText = NULL;
  6840. if (plvi->mask & LVIF_TEXT) {
  6841. if (!Str_Set(&pitem->pszText, plvi->pszText))
  6842. {
  6843. ListView_FreeItem(plv, pitem);
  6844. return NULL;
  6845. }
  6846. }
  6847. }
  6848. return pitem;
  6849. }
  6850. // HACK ALERT!! -- fSmoothScroll is an added parameter! It allows for smooth
  6851. // scrolling when deleting items. ListView_LRInvalidateBelow is only currently
  6852. // called from ListView_OnUpdate and ListView_OnDeleteItem. Both these calls
  6853. // have been modified to work correctly and be backwards compatible.
  6854. //
  6855. void ListView_LRInvalidateBelow(LV* plv, int i, int fSmoothScroll)
  6856. {
  6857. if (ListView_IsListView(plv) || ListView_IsReportView(plv)) {
  6858. RECT rcItem;
  6859. if (!ListView_RedrawEnabled(plv) ||
  6860. (ListView_IsReportView(plv) && (plv->pImgCtx != NULL)))
  6861. fSmoothScroll = FALSE;
  6862. if (i >= 0)
  6863. {
  6864. ListView_GetRects(plv, i, NULL, NULL, &rcItem, NULL);
  6865. }
  6866. else
  6867. {
  6868. rcItem.left = rcItem.top = 0;
  6869. rcItem.right = plv->sizeClient.cx;
  6870. rcItem.bottom = plv->sizeClient.cy;
  6871. }
  6872. // Don't try to scroll over the header part
  6873. if (ListView_IsReportView(plv) && rcItem.top < plv->yTop)
  6874. rcItem.top = plv->yTop;
  6875. // For both List and report view need to erase the item and
  6876. // below. Note: do simple test to see if there is anything
  6877. // to redraw
  6878. // we can't check for bottom/right > 0 because if we nuked something
  6879. // above or to the left of the view, it may affect us all
  6880. if ((rcItem.top <= plv->sizeClient.cy) &&
  6881. (rcItem.left <= plv->sizeClient.cx))
  6882. {
  6883. rcItem.bottom = plv->sizeClient.cy;
  6884. if (ListView_RedrawEnabled(plv)) {
  6885. if ((plv->clrBk == CLR_NONE) && (plv->pImgCtx == NULL))
  6886. {
  6887. LVSeeThruScroll(plv, &rcItem);
  6888. }
  6889. else if (ListView_IsReportView(plv) && fSmoothScroll)
  6890. {
  6891. SMOOTHSCROLLINFO si =
  6892. {
  6893. sizeof(si),
  6894. SSIF_MINSCROLL,
  6895. plv->ci.hwnd,
  6896. 0,
  6897. -(plv->cyItem),
  6898. &rcItem,
  6899. &rcItem,
  6900. NULL,
  6901. NULL,
  6902. SW_INVALIDATE|SW_ERASE,
  6903. SSI_DEFAULT,
  6904. 1,
  6905. 1,
  6906. };
  6907. SmoothScrollWindow(&si);
  6908. } else {
  6909. RedrawWindow(plv->ci.hwnd, &rcItem, NULL, RDW_INVALIDATE | RDW_ERASE);
  6910. }
  6911. } else {
  6912. RedrawWindow(plv->ci.hwnd, &rcItem, NULL, RDW_INVALIDATE | RDW_ERASE);
  6913. }
  6914. if (ListView_IsListView(plv))
  6915. {
  6916. RECT rcClient;
  6917. // For Listview we need to erase the other columns...
  6918. rcClient.left = rcItem.right;
  6919. rcClient.top = 0;
  6920. rcClient.bottom = plv->sizeClient.cy;
  6921. rcClient.right = plv->sizeClient.cx;
  6922. RedrawWindow(plv->ci.hwnd, &rcClient, NULL, RDW_INVALIDATE | RDW_ERASE);
  6923. }
  6924. }
  6925. }
  6926. }
  6927. // Used in Ownerdata Icon views to try to not invalidate the whole world...
  6928. void ListView_IInvalidateBelow(LV* plv, int i)
  6929. {
  6930. RECT rcItem;
  6931. if (i >= 0)
  6932. {
  6933. ListView_GetRects(plv, i, NULL, NULL, &rcItem, NULL);
  6934. }
  6935. else
  6936. {
  6937. rcItem.left = rcItem.top = 0;
  6938. rcItem.right = plv->sizeClient.cx;
  6939. rcItem.bottom = plv->sizeClient.cy;
  6940. }
  6941. // For Iconviews we need to invalidate everything to the right of us in this
  6942. // row and everything below the row...
  6943. // below. Note: do simple test to see if there is anything
  6944. // to redraw
  6945. if ((rcItem.top <= plv->sizeClient.cy) &&
  6946. (rcItem.left <= plv->sizeClient.cx))
  6947. {
  6948. rcItem.right = plv->sizeClient.cx;
  6949. RedrawWindow(plv->ci.hwnd, &rcItem, NULL, RDW_INVALIDATE | RDW_ERASE);
  6950. // Now erase everything below...
  6951. rcItem.top = rcItem.bottom;
  6952. rcItem.bottom = plv->sizeClient.cy;
  6953. rcItem.left = 0;
  6954. RedrawWindow(plv->ci.hwnd, &rcItem, NULL, RDW_INVALIDATE | RDW_ERASE);
  6955. }
  6956. }
  6957. void NEAR ListView_OnUpdate(LV* plv, int i)
  6958. {
  6959. // If in icon/small view, don't call InvalidateItem, since that'll force
  6960. // FindFreeSlot to get called, which is pig-like. Instead, just
  6961. // force a WM_PAINT message, which we'll catch and call Recompute with.
  6962. //
  6963. if (ListView_IsIconView(plv) || ListView_IsSmallView(plv))
  6964. {
  6965. if (plv->ci.style & LVS_AUTOARRANGE)
  6966. ListView_OnArrange(plv, LVA_DEFAULT);
  6967. else
  6968. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INTERNALPAINT | RDW_NOCHILDREN);
  6969. }
  6970. else
  6971. {
  6972. // HACK ALERT!! -- The third parameter is new. It allows for
  6973. // smooth scrolling when items are deleted in reportview.
  6974. // Passing 0, tells it NOT to scroll.
  6975. //
  6976. ListView_LRInvalidateBelow(plv, i, 0);
  6977. }
  6978. ListView_UpdateScrollBars(plv);
  6979. }
  6980. int NEAR ListView_OnInsertItemA(LV* plv, LV_ITEMA FAR* plvi) {
  6981. LPWSTR pszW = NULL;
  6982. LPSTR pszC = NULL;
  6983. int iRet;
  6984. //HACK ALERT -- this code assumes that LV_ITEMA is exactly the same
  6985. // as LV_ITEMW except for the pointer to the string.
  6986. ASSERT(sizeof(LV_ITEMA) == sizeof(LV_ITEMW));
  6987. if (!plvi)
  6988. {
  6989. return -1;
  6990. }
  6991. if ((plvi->mask & LVIF_TEXT) && (plvi->pszText != NULL)) {
  6992. pszC = plvi->pszText;
  6993. pszW = ProduceWFromA(plv->ci.uiCodePage, pszC);
  6994. if (pszW == NULL)
  6995. return -1;
  6996. plvi->pszText = (LPSTR)pszW;
  6997. }
  6998. iRet = ListView_OnInsertItem(plv, (const LV_ITEM FAR*) plvi);
  6999. if (pszW != NULL) {
  7000. plvi->pszText = pszC;
  7001. FreeProducedString(pszW);
  7002. }
  7003. return iRet;
  7004. }
  7005. int NEAR ListView_OnInsertItem(LV* plv, const LV_ITEM FAR* plvi)
  7006. {
  7007. int iItem;
  7008. if (plvi == NULL)
  7009. {
  7010. RIPMSG(0, "ListView_InsertItem: Do not pass a NULL LV_ITEM.");
  7011. return -1;
  7012. }
  7013. if (plvi->iSubItem != 0) // can only insert the 0th item
  7014. {
  7015. RIPMSG(0, "ListView_InsertItem: iSubItem must be 0 (app passed %d)", plvi->iSubItem);
  7016. return -1;
  7017. }
  7018. // If sorted, then insert sorted.
  7019. //
  7020. if (plv->ci.style & (LVS_SORTASCENDING | LVS_SORTDESCENDING)
  7021. && !ListView_IsOwnerData( plv ))
  7022. {
  7023. if (plvi->pszText == LPSTR_TEXTCALLBACK)
  7024. {
  7025. DebugMsg(DM_ERROR, TEXT("Don't use LPSTR_TEXTCALLBACK with LVS_SORTASCENDING or LVS_SORTDESCENDING"));
  7026. return -1;
  7027. }
  7028. iItem = ListView_LookupString(plv, plvi->pszText, LVFI_SUBSTRING | LVFI_NEARESTXY, 0);
  7029. }
  7030. else
  7031. iItem = plvi->iItem;
  7032. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  7033. if ( !ListView_IsOwnerData(plv))
  7034. {
  7035. int iZ;
  7036. LISTITEM FAR *pitem = ListView_CreateItem(plv, plvi);
  7037. UINT uSelMask = plvi->mask & LVIF_STATE ?
  7038. (plvi->state & (LVIS_FOCUSED | LVIS_SELECTED))
  7039. : 0;
  7040. UINT uSel = uSelMask;
  7041. if (!pitem)
  7042. return -1;
  7043. iItem = DPA_InsertPtr(plv->hdpa, iItem, pitem);
  7044. if (iItem == -1)
  7045. {
  7046. ListView_FreeItem(plv, pitem);
  7047. return -1;
  7048. }
  7049. plv->cTotalItems++;
  7050. if (plv->hdpaSubItems)
  7051. {
  7052. int iCol;
  7053. // slide all the colum DPAs down to match the location of the
  7054. // inserted item
  7055. //
  7056. for (iCol = plv->cCol - 1; iCol >= 0; iCol--)
  7057. {
  7058. HDPA hdpa = ListView_GetSubItemDPA(plv, iCol);
  7059. if (hdpa) // this is optional, call backs don't have them
  7060. {
  7061. // insert a blank item (REVIEW: should this be callback?)
  7062. // since this can be a tail sparce array,
  7063. // we need to make sure enough items are there.
  7064. if (iItem >= DPA_GetPtrCount(hdpa))
  7065. DPA_SetPtr(hdpa, iItem, NULL);
  7066. else if (DPA_InsertPtr(hdpa, iItem, NULL) != iItem)
  7067. goto Failure;
  7068. // Bad assert since hdpa can be tail sparse
  7069. // ASSERT(ListView_Count(plv) == DPA_GetPtrCount(hdpa));
  7070. ASSERT(ListView_Count(plv) >= DPA_GetPtrCount(hdpa));
  7071. }
  7072. }
  7073. }
  7074. // Add item to end of z order
  7075. //
  7076. iZ = DPA_InsertPtr(plv->hdpaZOrder, ListView_Count(plv), IntToPtr(iItem));
  7077. if (iZ == -1)
  7078. {
  7079. Failure:
  7080. DebugMsg(TF_LISTVIEW, TEXT("ListView_OnInsertItem() failed"));
  7081. if (DPA_DeletePtr(plv->hdpa, iItem))
  7082. plv->cTotalItems--;
  7083. ListView_FreeItem(plv, pitem);
  7084. return -1;
  7085. }
  7086. // if we inserted before the focus point, move the focus point up one
  7087. if (iItem <= plv->iFocus)
  7088. plv->iFocus++;
  7089. // do the same thing for the mark
  7090. if (iItem <= plv->iMark)
  7091. plv->iMark++;
  7092. // If the item was not added at the end of the list we need
  7093. // to update the other indexes in the list
  7094. if (iItem != ListView_Count(plv) - 1)
  7095. {
  7096. int i2;
  7097. for (i2 = iZ - 1; i2 >= 0; i2--)
  7098. {
  7099. int iItemZ = (int)(UINT_PTR)DPA_FastGetPtr(plv->hdpaZOrder, i2);
  7100. if (iItemZ >= iItem)
  7101. DPA_SetPtr(plv->hdpaZOrder, i2, (LPVOID)(UINT_PTR)(iItemZ + 1));
  7102. }
  7103. }
  7104. if (ListView_CheckBoxes(plv)) {
  7105. uSelMask |= LVIS_STATEIMAGEMASK;
  7106. uSel |= INDEXTOSTATEIMAGEMASK(1);
  7107. }
  7108. if (uSelMask) {
  7109. // we masked off these in the createitem above.
  7110. // because turning these on means more than setting the bits.
  7111. ListView_OnSetItemState(plv, iItem, uSel, uSelMask);
  7112. }
  7113. }
  7114. else
  7115. {
  7116. //
  7117. // simply adjust selection and count
  7118. //
  7119. if ((iItem >= 0) && (iItem <= MAX_LISTVIEWITEMS))
  7120. {
  7121. if (FAILED(plv->plvrangeSel->lpVtbl->InsertItem(plv->plvrangeSel, iItem )))
  7122. {
  7123. return( -1 );
  7124. }
  7125. plv->cTotalItems++;
  7126. plv->rcView.left = RECOMPUTE;
  7127. ListView_Recompute(plv);
  7128. if (!ListView_IsReportView(plv) && !ListView_IsListView(plv))
  7129. {
  7130. // We need to erase the background so that we don't leave
  7131. // turds from wrapped labels in large icon mode. This could
  7132. // be optimized by only invalidating to the right of and
  7133. // below the inserted item.
  7134. InvalidateRect( plv->ci.hwnd, NULL, TRUE );
  7135. }
  7136. // if we inserted before the focus point, move the focus point up
  7137. if (iItem <= plv->iFocus)
  7138. plv->iFocus++;
  7139. // do the same thing for the mark
  7140. if (iItem <= plv->iMark)
  7141. plv->iMark++;
  7142. }
  7143. }
  7144. if (!ListView_IsOwnerData(plv))
  7145. ASSERT(ListView_Count(plv) == DPA_GetPtrCount(plv->hdpaZOrder));
  7146. if (ListView_RedrawEnabled(plv))
  7147. {
  7148. // Update region
  7149. ListView_RecalcRegion(plv, TRUE, TRUE);
  7150. // The Maybe resize colmns may resize things in which case the next call
  7151. // to Update is not needed.
  7152. if (!ListView_MaybeResizeListColumns(plv, iItem, iItem))
  7153. ListView_OnUpdate(plv, iItem);
  7154. // this trick makes inserting lots of items cheap
  7155. // even if redraw is enabled.... don't calc or position items
  7156. // until this postmessage comes around
  7157. if (!plv->uUnplaced) {
  7158. PostMessage(plv->ci.hwnd, LVMI_PLACEITEMS, 0, 0);
  7159. }
  7160. plv->uUnplaced++;
  7161. }
  7162. else
  7163. {
  7164. //
  7165. // Special case code to make using SetRedraw work reasonably well
  7166. // for adding items to a listview which is in a non layout mode...
  7167. //
  7168. if ((plv->iFirstChangedNoRedraw == -1) ||
  7169. (iItem < plv->iFirstChangedNoRedraw))
  7170. plv->iFirstChangedNoRedraw = iItem;
  7171. }
  7172. ListView_Notify(plv, iItem, 0, LVN_INSERTITEM);
  7173. MyNotifyWinEvent(EVENT_OBJECT_CREATE, plv->ci.hwnd, OBJID_CLIENT, iItem+1);
  7174. return iItem;
  7175. }
  7176. BOOL NEAR ListView_OnDeleteItem(LV* plv, int iItem)
  7177. {
  7178. int iCount = ListView_Count(plv);
  7179. if (!ListView_IsValidItemNumber(plv, iItem))
  7180. return FALSE; // out of range
  7181. MyNotifyWinEvent(EVENT_OBJECT_DESTROY, plv->ci.hwnd, OBJID_CLIENT, iItem+1);
  7182. ListView_DismissEdit(plv, TRUE); // cancel edits
  7183. ListView_OnSetItemState(plv, iItem, 0, LVIS_SELECTED);
  7184. if (plv->iFocus == iItem)
  7185. ListView_OnSetItemState(plv, (iItem==iCount-1 ? iItem-1 : iItem+1), LVIS_FOCUSED, LVIS_FOCUSED);
  7186. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  7187. if (!ListView_IsOwnerData(plv))
  7188. {
  7189. LISTITEM FAR* pitem;
  7190. int iZ;
  7191. if ((plv->rcView.left != RECOMPUTE) && (ListView_IsIconView(plv) || ListView_IsSmallView(plv))) {
  7192. pitem = ListView_FastGetItemPtr(plv, iItem);
  7193. if (LV_IsItemOnViewEdge(plv, pitem)) {
  7194. plv->rcView.left = RECOMPUTE;
  7195. }
  7196. }
  7197. // We don't need to invalidate the item in report view because we
  7198. // will be scrolling on top of it.
  7199. //
  7200. if (!ListView_IsReportView(plv))
  7201. ListView_InvalidateItem(plv, iItem, FALSE, RDW_INVALIDATE | RDW_ERASE);
  7202. // this notify must be done AFTER the Invalidate because some items need callbacks
  7203. // to calculate the rect, but the notify might free it out
  7204. ListView_Notify(plv, iItem, 0, LVN_DELETEITEM);
  7205. // During the notify, the app might've done something to the listview
  7206. // so revalidate the item number pointer so we don't fault
  7207. #ifdef DEBUG
  7208. // Validate internally because DPA_DeletePtr will ASSERT if you ask it
  7209. // to delete something that doesn't exist.
  7210. if (!ListView_IsValidItemNumber(plv, iItem))
  7211. pitem = NULL;
  7212. else
  7213. #endif
  7214. pitem = DPA_DeletePtr(plv->hdpa, iItem);
  7215. if (!pitem)
  7216. {
  7217. RIPMSG(0, "Something strange happened during LVN_DELETEITEM; abandoning LVM_DELETEITEM");
  7218. return FALSE;
  7219. }
  7220. plv->cTotalItems = DPA_GetPtrCount(plv->hdpa);
  7221. // remove from the z-order, this is a linear search to find this!
  7222. DPA_DeletePtr(plv->hdpaZOrder, ListView_ZOrderIndex(plv, iItem));
  7223. //
  7224. // As the Z-order hdpa is a set of indexes we also need to decrement
  7225. // all indexes that exceed the one we are deleting.
  7226. //
  7227. for (iZ = ListView_Count(plv) - 1; iZ >= 0; iZ--)
  7228. {
  7229. int iItemZ = (int)(UINT_PTR)DPA_FastGetPtr(plv->hdpaZOrder, iZ);
  7230. if (iItemZ > iItem)
  7231. DPA_SetPtr(plv->hdpaZOrder, iZ, IntToPtr(iItemZ - 1));
  7232. }
  7233. // remove from sub item DPAs if necessary
  7234. if (plv->hdpaSubItems)
  7235. {
  7236. int iCol;
  7237. for (iCol = plv->cCol - 1; iCol >= 0; iCol--)
  7238. {
  7239. HDPA hdpa = ListView_GetSubItemDPA(plv, iCol);
  7240. if (hdpa) { // this is optional, call backs don't have them
  7241. PLISTSUBITEM plsi;
  7242. // These DPAs are tail sparse, so don't get upset if we
  7243. // try to delete something that's past the end of the list
  7244. #ifdef DEBUG
  7245. plsi = iItem < DPA_GetPtrCount(hdpa) ? DPA_DeletePtr(hdpa, iItem) : NULL;
  7246. #else
  7247. plsi = DPA_DeletePtr(hdpa, iItem);
  7248. #endif
  7249. ListView_FreeSubItem(plsi);
  7250. }
  7251. }
  7252. }
  7253. ListView_FreeItem(plv, pitem); // ... finaly the item pointer
  7254. }
  7255. else
  7256. {
  7257. //
  7258. // simply notify and then fixup selection state and count
  7259. //
  7260. if ((iItem >= 0) && (iItem <= MAX_LISTVIEWITEMS))
  7261. {
  7262. ListView_Notify(plv, iItem, 0, LVN_DELETEITEM);
  7263. if (FAILED(plv->plvrangeSel->lpVtbl->RemoveItem(plv->plvrangeSel, iItem)))
  7264. {
  7265. // BUGBUG: return out of memory status
  7266. //MemoryLowDlg( plv->ci.hwnd );
  7267. return FALSE;
  7268. }
  7269. plv->cTotalItems--;
  7270. plv->rcView.left = RECOMPUTE;
  7271. ListView_Recompute(plv);
  7272. if (!ListView_IsReportView(plv) && !ListView_IsListView(plv))
  7273. {
  7274. // We need to erase the background so that the last item gets
  7275. // erased in both icon modes and so that we don't leave turds
  7276. // from wrapped labels in large icon mode. This could be
  7277. // optimized by only invalidating to the right of and below
  7278. // the deleted item.
  7279. InvalidateRect( plv->ci.hwnd, NULL, TRUE );
  7280. }
  7281. }
  7282. else
  7283. {
  7284. return FALSE;
  7285. }
  7286. }
  7287. iCount = ListView_Count(plv); // regrab count incase someone updated item...
  7288. if (!ListView_IsOwnerData(plv))
  7289. ASSERT(ListView_Count(plv) == DPA_GetPtrCount(plv->hdpaZOrder));
  7290. if (plv->iFocus == iItem) {
  7291. if (plv->iFocus >= iCount) {
  7292. plv->iFocus = iCount - 1;
  7293. }
  7294. } if (plv->iFocus > iItem) {
  7295. plv->iFocus--; // slide the focus index down
  7296. }
  7297. // same with the mark
  7298. if (plv->iMark == iItem) { // deleted the mark item
  7299. if (plv->iMark >= iCount) // did we nuke the last item?
  7300. plv->iMark = iCount - 1;
  7301. } else if (plv->iMark > iItem)
  7302. plv->iMark--; // slide the mark index down
  7303. // Deleting an icon invalidates the icon positioning cache
  7304. plv->iFreeSlot = -1;
  7305. // HACK ALERT!! -- This construct with ReportView steals code from
  7306. // ListView_OnUpdate. Currently, it will work exactly the same as before,
  7307. // EXCEPT, that it won't call ListView_OnUpdate. This is to allow us to
  7308. // send a flag to ListView_LRUpdateBelow to tell it we're scrolling up.
  7309. //
  7310. if (ListView_IsReportView(plv)) {
  7311. // if the new count is zero and we will be showing empty text, simply invalidate the
  7312. // rect and redraw, else go through the invalidate below code...
  7313. // we don't know if we are going to show empty text if pszEmptyText is NULL, or not
  7314. // because we may get one through notify, so if iCount is 0 invalidate everything
  7315. if (iCount == 0)
  7316. InvalidateRect( plv->ci.hwnd, NULL, TRUE );
  7317. else
  7318. ListView_LRInvalidateBelow(plv,iItem,1);
  7319. if (ListView_RedrawEnabled(plv))
  7320. ListView_UpdateScrollBars(plv);
  7321. else {
  7322. //
  7323. // Special case code to make using SetRedraw work reasonably well
  7324. // for adding items to a listview which is in a non layout mode...
  7325. //
  7326. if ((plv->iFirstChangedNoRedraw != -1) && (iItem < plv->iFirstChangedNoRedraw))
  7327. plv->iFirstChangedNoRedraw--;
  7328. }
  7329. }
  7330. else {
  7331. if (ListView_RedrawEnabled(plv))
  7332. ListView_OnUpdate(plv, iItem);
  7333. else
  7334. {
  7335. ListView_LRInvalidateBelow(plv, iItem, 0);
  7336. //
  7337. // Special case code to make using SetRedraw work reasonably well
  7338. // for adding items to a listview which is in a non layout mode...
  7339. //
  7340. if ((plv->iFirstChangedNoRedraw != -1) && (iItem < plv->iFirstChangedNoRedraw))
  7341. plv->iFirstChangedNoRedraw--;
  7342. }
  7343. }
  7344. ListView_RecalcRegion(plv, TRUE, TRUE);
  7345. return TRUE;
  7346. }
  7347. BOOL NEAR ListView_OnDeleteAllItems(LV* plv)
  7348. {
  7349. int i;
  7350. BOOL bAlreadyNotified;
  7351. BOOL fHasItemData;
  7352. fHasItemData = !ListView_IsOwnerData(plv);
  7353. ListView_DismissEdit(plv, TRUE); // cancel edits
  7354. // Must neutralize the focus because some apps will call
  7355. // ListView_OnGetNextItem(LVNI_FOCUSED) during delete notifications,
  7356. // so we need to make sure the focus is in a safe place.
  7357. // May as well neutralize the mark, too.
  7358. plv->iMark = plv->iFocus = -1;
  7359. // Also nuke the icon positioning cache
  7360. plv->iFreeSlot = -1;
  7361. bAlreadyNotified = (BOOL)ListView_Notify(plv, -1, 0, LVN_DELETEALLITEMS);
  7362. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  7363. if (fHasItemData || !bAlreadyNotified)
  7364. {
  7365. for (i = ListView_Count(plv) - 1; i >= 0; i--)
  7366. {
  7367. if (!bAlreadyNotified)
  7368. ListView_Notify(plv, i, 0, LVN_DELETEITEM);
  7369. if (fHasItemData)
  7370. {
  7371. ListView_FreeItem(plv, ListView_FastGetItemPtr(plv, i));
  7372. //
  7373. // CAREFUL! Applications such as NT Backup call back
  7374. // into ListView during the LVN_DELETEITEM notification,
  7375. // so we need to kill this item or we will fault at the
  7376. // next iteration because everybody relies on
  7377. // ListView_Count for validation.
  7378. //
  7379. DPA_FastDeleteLastPtr(plv->hdpa);
  7380. plv->cTotalItems--;
  7381. }
  7382. }
  7383. }
  7384. if (ListView_IsOwnerData( plv ))
  7385. {
  7386. if (FAILED(plv->plvrangeSel->lpVtbl->Clear( plv->plvrangeSel )))
  7387. {
  7388. // BUGBUG: return low memory status
  7389. //MemoryLowDlg( plv->ci.hwnd );
  7390. }
  7391. plv->cTotalItems = 0;
  7392. }
  7393. else
  7394. {
  7395. DPA_DeleteAllPtrs(plv->hdpa);
  7396. DPA_DeleteAllPtrs(plv->hdpaZOrder);
  7397. plv->cTotalItems = 0;
  7398. if (plv->hdpaSubItems)
  7399. {
  7400. int iCol;
  7401. for (iCol = plv->cCol - 1; iCol >= 0; iCol--)
  7402. {
  7403. HDPA hdpa = ListView_GetSubItemDPA(plv, iCol);
  7404. if (hdpa) {
  7405. DPA_EnumCallback(hdpa, ListView_FreeColumnData, 0);
  7406. DPA_DeleteAllPtrs(hdpa);
  7407. }
  7408. }
  7409. }
  7410. }
  7411. plv->rcView.left = RECOMPUTE;
  7412. plv->xOrigin = 0;
  7413. plv->nSelected = 0;
  7414. plv->ptlRptOrigin.x = 0;
  7415. plv->ptlRptOrigin.y = 0;
  7416. // reset the cxItem width
  7417. if (!(plv->flags & LVF_COLSIZESET))
  7418. plv->cxItem = 16 * plv->cxLabelChar + plv->cxSmIcon;
  7419. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  7420. ListView_UpdateScrollBars(plv);
  7421. return TRUE;
  7422. }
  7423. int PASCAL ListView_IFindNearestItem(LV* plv, int left, int top, UINT vk)
  7424. {
  7425. int iMin = -1;
  7426. if (ListView_IsOwnerData( plv ))
  7427. {
  7428. POINT pt;
  7429. int cSlots;
  7430. ASSERT( !ListView_IsReportView( plv ) && !ListView_IsListView( plv ) );
  7431. pt.x = left + plv->ptOrigin.x;
  7432. pt.y = top + plv->ptOrigin.y;
  7433. cSlots = ListView_GetSlotCount( plv, TRUE );
  7434. iMin = ListView_CalcHitSlot( plv, pt, cSlots );
  7435. switch( vk )
  7436. {
  7437. case VK_HOME:
  7438. iMin = 0;
  7439. break;
  7440. case VK_END:
  7441. iMin = ListView_Count( plv ) - 1;
  7442. break;
  7443. case VK_LEFT:
  7444. if (iMin % cSlots)
  7445. iMin -= 1;
  7446. break;
  7447. case VK_RIGHT:
  7448. if ((iMin + 1) % cSlots)
  7449. iMin += 1;
  7450. break;
  7451. case VK_UP:
  7452. if (iMin >= cSlots)
  7453. iMin -= cSlots;
  7454. break;
  7455. case VK_DOWN:
  7456. if (iMin + cSlots < ListView_Count( plv ))
  7457. iMin += cSlots;
  7458. break;
  7459. default: ;
  7460. }
  7461. iMin = max( 0, iMin );
  7462. iMin = min( ListView_Count( plv ) - 1, iMin );
  7463. }
  7464. else
  7465. {
  7466. DWORD dMin = 0;
  7467. int cyItem;
  7468. int yEnd = 0, yLimit = 0, xEnd = 0;
  7469. int iCount;
  7470. int i;
  7471. if (ListView_IsIconView(plv)) {
  7472. cyItem = plv->cyIcon;
  7473. } else {
  7474. cyItem = plv->cyItem;
  7475. }
  7476. iCount = ListView_Count(plv);
  7477. if (iCount == 1)
  7478. return 0;
  7479. if (vk == VK_HOME) {
  7480. yEnd = yLimit = plv->rcView.bottom;
  7481. xEnd = plv->rcView.right;
  7482. } else if (vk == VK_END) {
  7483. yEnd = yLimit = plv->rcView.top;
  7484. xEnd = plv->rcView.left;
  7485. }
  7486. for (i = 0; i < iCount; i++)
  7487. {
  7488. RECT rc;
  7489. int dx;
  7490. DWORD dxAbs, dyAbs;
  7491. int dy;
  7492. DWORD dOffset;
  7493. ListView_GetRects(plv, i, &rc, NULL, NULL, NULL);
  7494. dx = rc.left - left;
  7495. dxAbs = (DWORD)(dx < 0 ? -dx : dx);
  7496. dy = rc.top - top;
  7497. dyAbs = (DWORD)(dy < 0 ? -dy : dy);
  7498. if ((vk == VK_LEFT) && (dxAbs < dyAbs || dx >= 0))
  7499. continue;
  7500. else if ((vk == VK_RIGHT) && (dxAbs < dyAbs || dx <= 0))
  7501. continue;
  7502. else if ((vk == VK_UP) && (dxAbs > dyAbs || dy >= 0))
  7503. continue;
  7504. else if ((vk == VK_DOWN) && (dxAbs > dyAbs || dy <= 0))
  7505. continue;
  7506. if (vk == VK_HOME || vk == VK_END) {
  7507. // home is not the nearest to the top corner, it's the leftmost of the top row.
  7508. // ditto (reversed) for end. thus we can't use the stuff below. bummer
  7509. if (vk == VK_HOME) {
  7510. if ((rc.top + cyItem < yEnd) || // if it's fully above the highest line so far, take it!
  7511. ((rc.top < yLimit) && // if it's on the same row as the top item to date
  7512. (rc.left < xEnd))) {
  7513. iMin = i;
  7514. xEnd = rc.left;
  7515. yEnd = rc.top;
  7516. if (rc.top + cyItem < yLimit)
  7517. yLimit = rc.top + cyItem;
  7518. }
  7519. } else {
  7520. if ((rc.top > yEnd) || //if it's full below the lowest row
  7521. ((rc.top + cyItem > yLimit) && // if it's on the same row
  7522. (rc.right > xEnd))) {
  7523. iMin = i;
  7524. xEnd = rc.right;
  7525. yEnd = rc.top;
  7526. if (rc.top > yLimit)
  7527. yLimit = rc.top;
  7528. }
  7529. }
  7530. } else {
  7531. dOffset = ((dxAbs * dxAbs) + (dyAbs * dyAbs));
  7532. if (iMin == -1 || (dMin > dOffset))
  7533. {
  7534. dMin = dOffset;
  7535. iMin = i;
  7536. }
  7537. }
  7538. }
  7539. }
  7540. return iMin;
  7541. }
  7542. int NEAR ListView_Arrow(LV* plv, int iStart, UINT vk)
  7543. {
  7544. RECT rcFocus;
  7545. int i;
  7546. int dx;
  7547. int iCount;
  7548. //
  7549. // The algorithm to find which item depends if we are in a view
  7550. // that is arrange(layout) oriented or a sorted (list) view.
  7551. // For the sorted views we will use some optimizations to make
  7552. // it faster
  7553. //
  7554. iCount = ListView_Count(plv);
  7555. if (ListView_IsReportView(plv) || ListView_IsListView(plv))
  7556. {
  7557. //
  7558. // For up and down arrows, simply increment or decrement the
  7559. // index. Note: in listview this will cause it to wrap columns
  7560. // which is fine as it is compatible with the file manager
  7561. //
  7562. // Assumes only one of these flags is set...
  7563. switch (vk)
  7564. {
  7565. case VK_LEFT:
  7566. if (ListView_IsReportView(plv))
  7567. {
  7568. ListView_ROnScroll(plv, (GetAsyncKeyState(VK_CONTROL) < 0) ? SB_PAGELEFT : SB_LINELEFT, 0, SB_HORZ);
  7569. }
  7570. else
  7571. iStart -= plv->cItemCol;
  7572. break;
  7573. case VK_RIGHT:
  7574. if (ListView_IsReportView(plv))
  7575. {
  7576. // Make this horizontally scroll the report view
  7577. ListView_ROnScroll(plv, (GetAsyncKeyState(VK_CONTROL) < 0) ? SB_PAGERIGHT : SB_LINERIGHT, 0, SB_HORZ);
  7578. }
  7579. else
  7580. iStart += plv->cItemCol;
  7581. break;
  7582. case VK_UP:
  7583. iStart--;
  7584. break;
  7585. case VK_DOWN:
  7586. iStart++;
  7587. break;
  7588. case VK_HOME:
  7589. iStart = 0;
  7590. break;
  7591. case VK_END:
  7592. iStart = iCount -1;
  7593. break;
  7594. case VK_NEXT:
  7595. if (ListView_IsReportView(plv))
  7596. {
  7597. i = iStart; // save away to make sure we dont go wrong way!
  7598. // First go to end of page...
  7599. iStart = (int)(((LONG)(plv->sizeClient.cy - (plv->cyItem)
  7600. - plv->yTop) + plv->ptlRptOrigin.y) / plv->cyItem);
  7601. // If Same item, increment by page size.
  7602. if (iStart <= i)
  7603. iStart = i + max(
  7604. (plv->sizeClient.cy - plv->yTop)/ plv->cyItem - 1,
  7605. 1);
  7606. if (iStart >= iCount)
  7607. iStart = iCount - 1;
  7608. } else {
  7609. // multiply by 2/3 to give a good feel.. when the item is mostly shown
  7610. // you want to go to the next column
  7611. dx = (plv->sizeClient.cx + (plv->cxItem*2)/3) / plv->cxItem;
  7612. if (!dx)
  7613. dx = 1;
  7614. iStart += plv->cItemCol * dx;
  7615. if (plv->cItemCol) {
  7616. while (iStart >= iCount)
  7617. iStart -= plv->cItemCol;
  7618. }
  7619. }
  7620. break;
  7621. case VK_PRIOR:
  7622. if (ListView_IsReportView(plv))
  7623. {
  7624. i = iStart; // save away to make sure we dont go wrong way!
  7625. // First go to end of page...
  7626. iStart = (int)(plv->ptlRptOrigin.y / plv->cyItem);
  7627. // If Same item, increment by page size.
  7628. if (iStart >= i)
  7629. iStart = i - max(
  7630. (plv->sizeClient.cy - plv->yTop)/ plv->cyItem - 1,
  7631. 1);
  7632. if (iStart < 0)
  7633. iStart = 0;
  7634. } else {
  7635. dx = (plv->sizeClient.cx + (plv->cxItem*2)/3) / plv->cxItem;
  7636. if (!dx)
  7637. dx = 1;
  7638. iStart -= plv->cItemCol * dx;
  7639. if (plv->cItemCol) {
  7640. while (iStart < 0)
  7641. iStart += plv->cItemCol;
  7642. }
  7643. }
  7644. break;
  7645. default:
  7646. return -1; // Out of range
  7647. }
  7648. // Make sure it is in range!.
  7649. if ((iStart >= 0) && (iStart < iCount))
  7650. return iStart;
  7651. else if (iCount == 1)
  7652. return 0;
  7653. else
  7654. return -1;
  7655. }
  7656. else
  7657. {
  7658. //
  7659. // Layout type view. we need to use the position of the items
  7660. // to figure out the next item
  7661. //
  7662. if (ListView_IsOwnerData( plv ))
  7663. {
  7664. iStart = max( 0, iStart );
  7665. // if it does not matches any of the entries in the case statement below
  7666. // this is done to skip the call back by the GetRects
  7667. //
  7668. if ( vk != VK_LEFT &&
  7669. vk != VK_RIGHT &&
  7670. vk != VK_UP &&
  7671. vk != VK_DOWN &&
  7672. vk != VK_HOME &&
  7673. vk != VK_END &&
  7674. vk != VK_NEXT &&
  7675. vk != VK_PRIOR )
  7676. {
  7677. return -1;
  7678. }
  7679. ListView_GetRects(plv, iStart, &rcFocus, NULL, NULL, NULL);
  7680. }
  7681. else
  7682. {
  7683. if (iStart != -1) {
  7684. ListView_GetRects(plv, iStart, &rcFocus, NULL, NULL, NULL);
  7685. }
  7686. }
  7687. switch (vk)
  7688. {
  7689. // For standard arrow keys just fall out of here.
  7690. case VK_LEFT:
  7691. case VK_RIGHT:
  7692. case VK_UP:
  7693. case VK_DOWN:
  7694. if (ListView_IsOwnerData( plv ))
  7695. {
  7696. break;
  7697. }
  7698. else
  7699. {
  7700. if (iStart != -1) {
  7701. // all keys map to VK_HOME except VK_END
  7702. break;
  7703. }
  7704. // Fall through
  7705. vk = VK_HOME;
  7706. }
  7707. case VK_HOME:
  7708. rcFocus.left = - plv->ptOrigin.x;
  7709. rcFocus.top = - plv->ptOrigin.y;
  7710. break;
  7711. case VK_END:
  7712. rcFocus.left = plv->rcView.right;
  7713. rcFocus.top = plv->rcView.bottom;
  7714. break;
  7715. case VK_NEXT:
  7716. rcFocus.top += plv->sizeClient.cy;
  7717. vk = VK_UP;
  7718. break;
  7719. case VK_PRIOR:
  7720. vk = VK_DOWN;
  7721. rcFocus.top -= plv->sizeClient.cy;
  7722. break;
  7723. default:
  7724. return -1; // Out of range
  7725. }
  7726. return ListView_IFindNearestItem(plv, rcFocus.left, rcFocus.top, vk);
  7727. }
  7728. }
  7729. int NEAR ListView_OnGetNextItem(LV* plv, int i, UINT flags)
  7730. {
  7731. int iStart = i;
  7732. int cItemMax = ListView_Count(plv);
  7733. // Note that -1 is a valid starting point
  7734. if (i < -1 || i >= cItemMax)
  7735. return -1;
  7736. if (ListView_IsOwnerData( plv ))
  7737. {
  7738. if (flags & (LVNI_CUT | LVNI_DROPHILITED | LVNI_PREVIOUS))
  7739. {
  7740. return( -1 );
  7741. }
  7742. }
  7743. if (flags & LVNI_FOCUSED)
  7744. {
  7745. // we know which item is focused, jump right to it.
  7746. // but we have to mimick the code below exactly for compat:
  7747. // if directional bits are set, they take precedence.
  7748. if (!(flags & (LVNI_ABOVE | LVNI_BELOW | LVNI_TORIGHT | LVNI_TOLEFT)))
  7749. {
  7750. // there are no more focused items after iFocus
  7751. if (i >= plv->iFocus)
  7752. return -1;
  7753. // subtract one here -- we increment it below
  7754. i = plv->iFocus - 1;
  7755. }
  7756. }
  7757. while (TRUE)
  7758. {
  7759. // BUGBUG: does anyone call this now???
  7760. if (flags & (LVNI_ABOVE | LVNI_BELOW | LVNI_TORIGHT | LVNI_TOLEFT))
  7761. {
  7762. UINT vk;
  7763. if (flags & LVNI_ABOVE)
  7764. vk = VK_UP;
  7765. else if (flags & LVNI_BELOW)
  7766. vk = VK_DOWN;
  7767. else if (flags & LVNI_TORIGHT)
  7768. vk = VK_RIGHT;
  7769. else
  7770. vk = VK_LEFT;
  7771. if (i != -1)
  7772. i = ListView_Arrow(plv, i, vk);
  7773. if (i == -1)
  7774. return i;
  7775. }
  7776. else
  7777. {
  7778. i++;
  7779. if (i == cItemMax)
  7780. return -1;
  7781. }
  7782. // See if any other restrictions are set
  7783. if (flags & ~(LVNI_ABOVE | LVNI_BELOW | LVNI_TORIGHT | LVNI_TOLEFT))
  7784. {
  7785. WORD wItemState;
  7786. if (ListView_IsOwnerData( plv ))
  7787. {
  7788. if (flags & LVNI_FOCUSED)
  7789. {
  7790. // we check LVNI_FOCUSED before the loop, so i == iFocus
  7791. ASSERT(i == plv->iFocus && i != -1);
  7792. if (flags & LVNI_SELECTED)
  7793. {
  7794. if (plv->plvrangeSel->lpVtbl->IsSelected(plv->plvrangeSel, i ) != S_OK)
  7795. {
  7796. i = -1;
  7797. }
  7798. }
  7799. }
  7800. else if (flags & LVNI_SELECTED)
  7801. {
  7802. i = max( i, 0 );
  7803. plv->plvrangeSel->lpVtbl->NextSelected(plv->plvrangeSel, i, &i );
  7804. }
  7805. else
  7806. {
  7807. i = -1;
  7808. }
  7809. }
  7810. else
  7811. {
  7812. {
  7813. LISTITEM FAR* pitem = ListView_FastGetItemPtr(plv, i);
  7814. wItemState = pitem->state;
  7815. }
  7816. // for LVNI_FOCUSED, we start at the LVIS_FOCUSED element, if we're
  7817. // not on that element, one of the below continues was hit, so
  7818. // we'll never find the element. bail out early.
  7819. if ((flags & LVNI_FOCUSED) && !(wItemState & LVIS_FOCUSED))
  7820. {
  7821. ASSERT(i == plv->iFocus || i == plv->iFocus+1);
  7822. return(-1);
  7823. }
  7824. if (((flags & LVNI_SELECTED) && !(wItemState & LVIS_SELECTED)) ||
  7825. ((flags & LVNI_CUT) && !(wItemState & LVIS_CUT)) ||
  7826. ((flags & LVNI_DROPHILITED) && !(wItemState & LVIS_DROPHILITED))) {
  7827. if (i != iStart)
  7828. continue;
  7829. else {
  7830. // we've looped and we can't find anything to fit this criteria
  7831. return -1;
  7832. }
  7833. }
  7834. }
  7835. }
  7836. return i;
  7837. }
  7838. }
  7839. int NEAR ListView_CompareString(LV* plv, int i, LPCTSTR pszFind, UINT flags, int iLen)
  7840. {
  7841. // BUGBUG: non protected globals
  7842. int cb;
  7843. TCHAR ach[CCHLABELMAX];
  7844. LV_ITEM item;
  7845. ASSERT(!ListView_IsOwnerData(plv));
  7846. ASSERT(pszFind);
  7847. item.iItem = i;
  7848. item.iSubItem = 0;
  7849. item.mask = LVIF_TEXT;
  7850. item.pszText = ach;
  7851. item.cchTextMax = ARRAYSIZE(ach);
  7852. ListView_OnGetItem(plv, &item);
  7853. if (!(flags & (LVFI_PARTIAL | LVFI_SUBSTRING)))
  7854. return lstrcmpi(item.pszText, pszFind);
  7855. // REVIEW: LVFI_SUBSTRING is not really implemented yet.
  7856. cb = lstrlen(pszFind);
  7857. if (iLen && (cb > iLen)) {
  7858. cb = iLen;
  7859. }
  7860. //
  7861. // If the sub strings not equal then return the ordering based
  7862. // on the entire string.
  7863. //
  7864. return IntlStrEqNI(item.pszText, pszFind, cb) ? 0 : lstrcmp(item.pszText, pszFind);
  7865. }
  7866. int NEAR ListView_OnFindItemA(LV* plv, int iStart, LV_FINDINFOA * plvfi) {
  7867. LPWSTR pszW = NULL;
  7868. LPCSTR pszC = NULL;
  7869. int iRet;
  7870. //HACK ALERT -- this code assumes that LV_FINDINFOA is exactly the same
  7871. // as LV_FINDINFOW except for the pointer to the string.
  7872. ASSERT(sizeof(LV_FINDINFOA) == sizeof(LV_FINDINFOW));
  7873. if (!plvfi)
  7874. return -1;
  7875. if (!(plvfi->flags & LVFI_PARAM) && !(plvfi->flags & LVFI_NEARESTXY)) {
  7876. pszC = plvfi->psz;
  7877. if ((pszW = ProduceWFromA(plv->ci.uiCodePage, pszC)) == NULL)
  7878. return -1;
  7879. plvfi->psz = (LPSTR)pszW;
  7880. }
  7881. iRet = ListView_OnFindItem(plv, iStart, (const LV_FINDINFO FAR *)plvfi);
  7882. if (pszW != NULL) {
  7883. plvfi->psz = pszC;
  7884. FreeProducedString(pszW);
  7885. }
  7886. return iRet;
  7887. }
  7888. int NEAR ListView_OnFindItem(LV* plv, int iStart, const LV_FINDINFO FAR* plvfi)
  7889. {
  7890. int i;
  7891. int j;
  7892. int cItem;
  7893. UINT flags;
  7894. if (!plvfi)
  7895. return -1;
  7896. if (plvfi->flags & LVFI_NEARESTXY) {
  7897. if (ListView_IsIconView(plv) || ListView_IsSmallView(plv)) {
  7898. return ListView_IFindNearestItem(plv, plvfi->pt.x, plvfi->pt.y, plvfi->vkDirection);
  7899. } else
  7900. return -1;
  7901. }
  7902. // Note that -1 is a valid starting point
  7903. if (iStart < -1 || iStart >= ListView_Count(plv))
  7904. return -1;
  7905. if (ListView_IsOwnerData( plv ))
  7906. {
  7907. // call back to owner for search
  7908. return( (int) ListView_RequestFindItem( plv, plvfi, iStart + 1 ) );
  7909. }
  7910. else
  7911. {
  7912. flags = plvfi->flags;
  7913. i = iStart;
  7914. cItem = ListView_Count(plv);
  7915. if (flags & LVFI_PARAM)
  7916. {
  7917. LPARAM lParam = plvfi->lParam;
  7918. // Linear search with wraparound...
  7919. //
  7920. for (j = cItem; j-- != 0; )
  7921. {
  7922. ++i;
  7923. if (i == cItem) {
  7924. if (flags & LVFI_WRAP)
  7925. i = 0;
  7926. else
  7927. break;
  7928. }
  7929. if (ListView_FastGetItemPtr(plv, i)->lParam == lParam)
  7930. return i;
  7931. }
  7932. }
  7933. else // if (flags & (LVFI_STRING | LVFI_SUBSTRING | LVFI_PARTIAL))
  7934. {
  7935. LPCTSTR pszFind = plvfi->psz;
  7936. if (!pszFind)
  7937. return -1;
  7938. if (plv->ci.style & (LVS_SORTASCENDING | LVS_SORTDESCENDING))
  7939. return ListView_LookupString(plv, pszFind, flags, i + 1);
  7940. for (j = cItem; j-- != 0; )
  7941. {
  7942. ++i;
  7943. if (i == cItem) {
  7944. if (flags & LVFI_WRAP)
  7945. i = 0;
  7946. else
  7947. break;
  7948. }
  7949. if (ListView_CompareString(plv,
  7950. i,
  7951. pszFind,
  7952. (flags & (LVFI_PARTIAL | LVFI_SUBSTRING)), 0) == 0)
  7953. {
  7954. return i;
  7955. }
  7956. }
  7957. }
  7958. }
  7959. return -1;
  7960. }
  7961. BOOL NEAR ListView_OnGetItemRect(LV* plv, int i, RECT FAR* prc)
  7962. {
  7963. LPRECT pRects[LVIR_MAX];
  7964. // validate parameters
  7965. if (!ListView_IsValidItemNumber(plv, i))
  7966. {
  7967. RIPMSG(0, "LVM_GETITEMRECT: invalid index %d", i);
  7968. return FALSE;
  7969. }
  7970. if (!prc || prc->left >= LVIR_MAX || prc->left < 0)
  7971. {
  7972. RIPMSG(0, "LVM_GETITEMRECT: invalid rect pointer");
  7973. return FALSE;
  7974. }
  7975. pRects[0] = NULL;
  7976. pRects[1] = NULL;
  7977. pRects[2] = NULL;
  7978. pRects[3] = NULL;
  7979. pRects[prc->left] = prc;
  7980. ListView_GetRects(plv, i, pRects[LVIR_ICON], pRects[LVIR_LABEL],
  7981. pRects[LVIR_BOUNDS], pRects[LVIR_SELECTBOUNDS]);
  7982. return TRUE;
  7983. }
  7984. //
  7985. // in:
  7986. // plv
  7987. // iItem MUST be a valid item index (in range)
  7988. // out:
  7989. // prcIcon icon bounding rect
  7990. // prcLabel label text bounding rect, for details this is the first column
  7991. // prcBounds entire item (all text and icon), including columns in details
  7992. // prcSelectionBounds union of icon and label rects, does NOT include columns
  7993. // in details view
  7994. // BUGBUG raymondc - Need to pass an HDC parameter for measurement
  7995. // since sometimes we do this while painting
  7996. void NEAR ListView_GetRects(LV* plv, int iItem,
  7997. RECT FAR* prcIcon, RECT FAR* prcLabel, RECT FAR* prcBounds,
  7998. RECT FAR* prcSelectBounds)
  7999. {
  8000. ASSERT(plv);
  8001. if (ListView_IsReportView(plv))
  8002. {
  8003. ListView_RGetRects(plv, iItem, prcIcon, prcLabel, prcBounds, prcSelectBounds);
  8004. }
  8005. else if (ListView_IsListView(plv))
  8006. {
  8007. ListView_LGetRects(plv, iItem, prcIcon, prcLabel, prcBounds, prcSelectBounds);
  8008. }
  8009. else
  8010. {
  8011. if (ListView_IsOwnerData( plv ))
  8012. {
  8013. RECT rcIcon;
  8014. RECT rcTextBounds;
  8015. LISTITEM item;
  8016. if (ListView_IsIconView(plv))
  8017. ListView_IGetRectsOwnerData(plv, iItem, &rcIcon, &rcTextBounds, &item, FALSE);
  8018. else if (ListView_IsSmallView(plv))
  8019. ListView_SGetRectsOwnerData(plv, iItem, &rcIcon, &rcTextBounds, &item, FALSE);
  8020. if (prcIcon)
  8021. *prcIcon = rcIcon;
  8022. if (prcLabel)
  8023. *prcLabel = rcTextBounds;
  8024. if (prcBounds)
  8025. UnionRect(prcBounds, &rcIcon, &rcTextBounds);
  8026. if (prcSelectBounds)
  8027. UnionRect(prcSelectBounds, &rcIcon, &rcTextBounds);
  8028. }
  8029. else
  8030. {
  8031. if (iItem >= ListView_Count(plv))
  8032. {
  8033. return;
  8034. }
  8035. else
  8036. {
  8037. LISTITEM FAR *pitem = ListView_FastGetItemPtr(plv, iItem);
  8038. if (pitem->cyFoldedLabel == SRECOMPUTE)
  8039. {
  8040. ListView_RecomputeLabelSize(plv, pitem, iItem, NULL, FALSE);
  8041. }
  8042. _ListView_GetRectsFromItem(plv, ListView_IsSmallView(plv), pitem,
  8043. prcIcon, prcLabel, prcBounds, prcSelectBounds);
  8044. }
  8045. }
  8046. }
  8047. }
  8048. void NEAR ListView_GetRectsOwnerData(LV* plv, int iItem,
  8049. RECT FAR* prcIcon, RECT FAR* prcLabel, RECT FAR* prcBounds,
  8050. RECT FAR* prcSelectBounds, LISTITEM* pitem)
  8051. {
  8052. ASSERT(plv);
  8053. ASSERT(ListView_IsOwnerData(plv));
  8054. if (ListView_IsReportView(plv))
  8055. {
  8056. ListView_RGetRects(plv, iItem, prcIcon, prcLabel, prcBounds,
  8057. prcSelectBounds);
  8058. }
  8059. else if (ListView_IsListView(plv))
  8060. {
  8061. ListView_LGetRects(plv, iItem, prcIcon, prcLabel, prcBounds,
  8062. prcSelectBounds);
  8063. }
  8064. else
  8065. {
  8066. RECT rcIcon;
  8067. RECT rcTextBounds;
  8068. if (ListView_IsIconView(plv))
  8069. ListView_IGetRectsOwnerData(plv, iItem, &rcIcon, &rcTextBounds, pitem, TRUE);
  8070. else if (ListView_IsSmallView(plv))
  8071. ListView_SGetRectsOwnerData(plv, iItem, &rcIcon, &rcTextBounds, pitem, TRUE);
  8072. // Don't need to check for folding here, as will have been handled in user data
  8073. // rectangle fetching functions.
  8074. if (prcIcon)
  8075. *prcIcon = rcIcon;
  8076. if (prcLabel)
  8077. *prcLabel = rcTextBounds;
  8078. if (prcBounds)
  8079. UnionRect(prcBounds, &rcIcon, &rcTextBounds);
  8080. if (prcSelectBounds)
  8081. UnionRect(prcSelectBounds, &rcIcon, &rcTextBounds);
  8082. }
  8083. }
  8084. BOOL NEAR ListView_OnRedrawItems(LV* plv, int iFirst, int iLast)
  8085. {
  8086. int iCount = ListView_Count(plv);
  8087. if (iFirst < iCount) {
  8088. if (iLast >= iCount)
  8089. iLast = iCount - 1;
  8090. while (iFirst <= iLast)
  8091. ListView_InvalidateItem(plv, iFirst++, FALSE, RDW_INVALIDATE | RDW_ERASE);
  8092. }
  8093. return TRUE;
  8094. }
  8095. // fSelectionOnly use the selection bounds only, ie. don't include
  8096. // columns in invalidation if in details view
  8097. //
  8098. void NEAR ListView_InvalidateItemEx(LV* plv, int iItem, BOOL fSelectionOnly,
  8099. UINT fRedraw, UINT maskChanged)
  8100. {
  8101. RECT rc;
  8102. LPRECT prcIcon;
  8103. LPRECT prcLabel;
  8104. LPRECT prcBounds;
  8105. LPRECT prcSelectBounds;
  8106. if (iItem == -1)
  8107. return;
  8108. prcIcon = prcLabel = prcBounds = prcSelectBounds = NULL;
  8109. // if we're in owner draw mode, and there's been a new font,
  8110. // we don't really know what the selection bounds is, so always use the bounds
  8111. // in that case... unless we're in fullrowselect mode
  8112. if (ListView_IsOwnerData(plv) && plv->flags & LVF_CUSTOMFONT &&
  8113. !ListView_FullRowSelect(plv)) {
  8114. fSelectionOnly = FALSE;
  8115. }
  8116. // if we're owner draw, there's no such thing as selection only
  8117. if (plv->ci.style & LVS_OWNERDRAWFIXED)
  8118. fSelectionOnly = FALSE;
  8119. if (fSelectionOnly) {
  8120. // In report mode non-fullrowselect,
  8121. // we have to use the full label rectangle rather
  8122. // than just the selection bounds, since the stuff outside the
  8123. // rectangle might need redrawing, too.
  8124. if (ListView_IsReportView(plv) && !ListView_FullRowSelect(plv))
  8125. prcLabel = &rc;
  8126. else
  8127. prcSelectBounds = &rc;
  8128. } else {
  8129. // if _only_the_text_ or _only_the_image_ changed then limit the redraw
  8130. switch (maskChanged) {
  8131. case LVIF_IMAGE:
  8132. prcIcon = &rc;
  8133. break;
  8134. case LVIF_TEXT:
  8135. prcLabel = &rc;
  8136. break;
  8137. default:
  8138. prcBounds = &rc;
  8139. break;
  8140. }
  8141. }
  8142. if (ListView_RedrawEnabled(plv)) {
  8143. ListView_GetRects(plv, iItem,
  8144. prcIcon, prcLabel, prcBounds, prcSelectBounds);
  8145. if (RECTS_IN_SIZE(plv->sizeClient, rc))
  8146. {
  8147. if (plv->exStyle & LVS_EX_BORDERSELECT)
  8148. InflateRect(&rc, 4 + g_cxIconMargin, 4 + g_cyIconMargin); // account for selection border and seperation since drawing otside of icon
  8149. RedrawWindow(plv->ci.hwnd, &rc, NULL, fRedraw);
  8150. }
  8151. } else {
  8152. // if we're not visible, we'll get a full
  8153. // erase bk when we do become visible, so only do this stuff when
  8154. // we're on setredraw false
  8155. if (!(plv->flags & LVF_REDRAW)) {
  8156. // if we're invalidating that's new (thus hasn't been painted yet)
  8157. // blow it off
  8158. if ((plv->iFirstChangedNoRedraw != -1) &&
  8159. (iItem >= plv->iFirstChangedNoRedraw)) {
  8160. return;
  8161. }
  8162. ListView_GetRects(plv, iItem,
  8163. prcIcon, prcLabel, prcBounds, prcSelectBounds);
  8164. // if it had the erase bit, add it to our region
  8165. if (RECTS_IN_SIZE(plv->sizeClient, rc)) {
  8166. HRGN hrgn = CreateRectRgnIndirect(&rc);
  8167. ListView_InvalidateRegion(plv, hrgn);
  8168. if (fRedraw & RDW_ERASE)
  8169. plv->flags |= LVF_ERASE;
  8170. }
  8171. }
  8172. }
  8173. }
  8174. // this returns BF_* flags to indicate which if any edge the item I is touching
  8175. // or crossing...
  8176. UINT LV_IsItemOnViewEdge(LV* plv, LISTITEM* pitem)
  8177. {
  8178. RECT rcItem;
  8179. RECT rcView = plv->rcView;
  8180. UINT uRet = 0;
  8181. // the view rect is enlarged a bit to allow for a little space around
  8182. // the text (see ListView_Recompute())
  8183. rcView.bottom -= g_cyEdge;
  8184. rcView.right -= g_cxEdge;
  8185. _ListView_GetRectsFromItem(plv, ListView_IsSmallView(plv), pitem,
  8186. NULL, NULL, &rcItem, NULL);
  8187. // translate from window coordinates to listview coordinate
  8188. OffsetRect(&rcItem, plv->ptOrigin.x, plv->ptOrigin.y);
  8189. if (rcItem.right >= rcView.right)
  8190. uRet |= BF_RIGHT;
  8191. if (rcItem.left <= rcView.left)
  8192. uRet |= BF_LEFT;
  8193. if (rcItem.top <= rcView.top)
  8194. uRet |= BF_TOP;
  8195. if (rcItem.bottom >= rcView.bottom)
  8196. uRet |= BF_BOTTOM;
  8197. return uRet;
  8198. }
  8199. void LV_AdjustViewRectOnMove(LV* plv, LISTITEM *pitem, int x, int y)
  8200. {
  8201. plv->iFreeSlot = -1; // The "free slot" cache is no good once an item moves
  8202. // if we have to recompute anyways, don't bother
  8203. if (!ListView_IsOwnerData( plv )) {
  8204. if ((plv->rcView.left != RECOMPUTE) &&
  8205. x != RECOMPUTE && y != RECOMPUTE &&
  8206. pitem->cyFoldedLabel != SRECOMPUTE) {
  8207. RECT rcAfter;
  8208. RECT rcView = plv->rcView;
  8209. // the view rect is enlarged a bit to allow for a little space around
  8210. // the text (see ListView_Recompute())
  8211. rcView.bottom -= g_cyEdge;
  8212. rcView.right -= g_cxEdge;
  8213. if (pitem->pt.x != RECOMPUTE) {
  8214. UINT uEdges;
  8215. uEdges = LV_IsItemOnViewEdge(plv, pitem);
  8216. pitem->pt.x = x;
  8217. pitem->pt.y = y;
  8218. // before and after the move, they need to be touching the
  8219. // same edges or not at all
  8220. if (uEdges != LV_IsItemOnViewEdge(plv, pitem)) {
  8221. goto FullRecompute;
  8222. }
  8223. } else {
  8224. // if the position wasn't set before
  8225. // we just need to find out what it is afterwards and
  8226. // enlarge the view... we don't need to shrink it
  8227. pitem->pt.x = x;
  8228. pitem->pt.y = y;
  8229. }
  8230. _ListView_GetRectsFromItem(plv, ListView_IsSmallView(plv), pitem,
  8231. NULL, NULL, &rcAfter, NULL);
  8232. // translate from window coordinates to listview coordinate
  8233. OffsetRect(&rcAfter, plv->ptOrigin.x, plv->ptOrigin.y);
  8234. // if we make it here, we just have to make sure the new view rect
  8235. // encompases this new item
  8236. UnionRect(&rcView, &rcView, &rcAfter);
  8237. rcView.right += g_cxEdge;
  8238. rcView.bottom += g_cyEdge;
  8239. DebugMsg(TF_LISTVIEW, TEXT("Score! (%d %d %d %d) was (%d %d %d %d)"),
  8240. rcView.left, rcView.top, rcView.right, rcView.bottom,
  8241. plv->rcView.left, plv->rcView.top, plv->rcView.right, plv->rcView.bottom);
  8242. plv->rcView = rcView;
  8243. } else {
  8244. FullRecompute:
  8245. plv->rcView.left = RECOMPUTE;
  8246. }
  8247. }
  8248. DebugMsg(TF_LISTVIEW, TEXT("LV -- AdjustViewRect pitem %d -- (%x, %x)"),
  8249. pitem,
  8250. pitem->pt.x, pitem->pt.y);
  8251. pitem->pt.x = x;
  8252. pitem->pt.y = y;
  8253. // Compute the workarea of this item if applicable
  8254. ListView_FindWorkArea(plv, pitem->pt, &(pitem->iWorkArea));
  8255. }
  8256. BOOL NEAR ListView_OnSetItemPosition(LV* plv, int i, int x, int y)
  8257. {
  8258. LISTITEM FAR* pitem;
  8259. if (ListView_IsListView(plv))
  8260. return FALSE;
  8261. if (ListView_IsOwnerData( plv ))
  8262. {
  8263. RIPMSG(0, "LVM_SETITEMPOSITION: Invalid for owner-data listview");
  8264. return FALSE;
  8265. }
  8266. pitem = ListView_GetItemPtr(plv, i);
  8267. if (!pitem)
  8268. return FALSE;
  8269. //
  8270. // this is a hack to fix a bug in OLE drag/drop loop
  8271. //
  8272. if (x >= 0xF000 && x < 0x10000)
  8273. {
  8274. DebugMsg(TF_LISTVIEW, TEXT("LV -- On SetItemPosition fixing truncated negative number 0x%08X"), x);
  8275. x = x - 0x10000;
  8276. }
  8277. if (y >= 0xF000 && y < 0x10000)
  8278. {
  8279. DebugMsg(TF_LISTVIEW, TEXT("LV -- On SetItemPosition fixing truncated negative number 0x%08X"), y);
  8280. y = y - 0x10000;
  8281. }
  8282. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  8283. if (pitem->cyFoldedLabel == SRECOMPUTE)
  8284. {
  8285. ListView_RecomputeLabelSize(plv, pitem, i, NULL, FALSE);
  8286. }
  8287. // erase old
  8288. if (y != pitem->pt.y || x != pitem->pt.x) {
  8289. // Don't invalidate if it hasn't got a position yet
  8290. if (pitem->pt.y != RECOMPUTE) {
  8291. ListView_InvalidateItem(plv, i, FALSE, RDW_INVALIDATE | RDW_ERASE);
  8292. } else if (plv->uUnplaced) {
  8293. // this means an unplaced item got placed
  8294. plv->uUnplaced--;
  8295. if (!plv->uUnplaced) {
  8296. MSG msg;
  8297. // if this is now 0, pull out the postmessage
  8298. PeekMessage(&msg, plv->ci.hwnd, LVMI_PLACEITEMS, LVMI_PLACEITEMS, PM_REMOVE);
  8299. }
  8300. }
  8301. if (y == RECOMPUTE) {
  8302. // if they're setting the new position to be a "any open spot" post that we
  8303. // need to calc this later
  8304. if (!plv->uUnplaced) {
  8305. PostMessage(plv->ci.hwnd, LVMI_PLACEITEMS, 0, 0);
  8306. }
  8307. plv->uUnplaced++;
  8308. }
  8309. }
  8310. DebugMsg(TF_LISTVIEW, TEXT("LV -- On SetItemPosition %d %d %d %d -- (%x, %x)"),
  8311. plv->rcView.left, plv->rcView.top, plv->rcView.right, plv->rcView.bottom,
  8312. pitem->pt.x, pitem->pt.y);
  8313. LV_AdjustViewRectOnMove(plv, pitem, x, y);
  8314. // and draw at new position
  8315. ListView_RecalcRegion(plv, FALSE, TRUE);
  8316. ListView_InvalidateItem(plv, i, FALSE, RDW_INVALIDATE);
  8317. // If autoarrange is turned on, do it now...
  8318. if (ListView_RedrawEnabled(plv)) {
  8319. if (plv->ci.style & LVS_AUTOARRANGE)
  8320. ListView_OnArrange(plv, LVA_DEFAULT);
  8321. else
  8322. ListView_UpdateScrollBars(plv);
  8323. }
  8324. return TRUE;
  8325. }
  8326. BOOL NEAR ListView_OnGetItemPosition(LV* plv, int i, POINT FAR* ppt)
  8327. {
  8328. LISTITEM FAR* pitem;
  8329. //
  8330. // This needs to handle all views as it is used to figure out
  8331. // where the item is during drag and drop and the like
  8332. //
  8333. if (!ppt)
  8334. {
  8335. RIPMSG(0, "LVM_GETITEMPOSITION: Invalid ppt = NULL");
  8336. return FALSE;
  8337. }
  8338. if (ListView_IsListView(plv) || ListView_IsReportView(plv)
  8339. || ListView_IsOwnerData( plv ))
  8340. {
  8341. RECT rcIcon;
  8342. ListView_GetRects(plv, i, &rcIcon, NULL, NULL, NULL);
  8343. ppt->x = rcIcon.left;
  8344. ppt->y = rcIcon.top;
  8345. } else {
  8346. pitem = ListView_GetItemPtr(plv, i);
  8347. if (!pitem)
  8348. return FALSE;
  8349. if (pitem->pt.x == RECOMPUTE)
  8350. ListView_Recompute(plv);
  8351. ppt->x = pitem->pt.x;
  8352. ppt->y = pitem->pt.y;
  8353. }
  8354. return TRUE;
  8355. }
  8356. BOOL NEAR ListView_OnGetOrigin(LV* plv, POINT FAR* ppt)
  8357. {
  8358. if (!ppt) {
  8359. DebugMsg(DM_ERROR, TEXT("ListView_OnGetOrigin: ppt is NULL"));
  8360. return FALSE;
  8361. }
  8362. if (ListView_IsListView(plv) || ListView_IsReportView(plv))
  8363. return FALSE;
  8364. *ppt = plv->ptOrigin;
  8365. return TRUE;
  8366. }
  8367. int NEAR ListView_OnGetStringWidthA(LV* plv, LPCSTR psz, HDC hdc) {
  8368. LPWSTR pszW = NULL;
  8369. int iRet;
  8370. if (!psz)
  8371. return 0;
  8372. if ((psz != NULL) && (pszW = ProduceWFromA(plv->ci.uiCodePage, psz)) == NULL)
  8373. return 0;
  8374. iRet = ListView_OnGetStringWidth(plv, pszW, hdc);
  8375. FreeProducedString(pszW);
  8376. return iRet;
  8377. }
  8378. int NEAR ListView_OnGetStringWidth(LV* plv, LPCTSTR psz, HDC hdc)
  8379. {
  8380. SIZE siz;
  8381. HDC hdcFree = NULL;
  8382. HFONT hfontPrev;
  8383. if (!psz || psz == LPSTR_TEXTCALLBACK)
  8384. return 0;
  8385. if (!hdc) {
  8386. hdcFree = hdc = GetDC(plv->ci.hwnd);
  8387. hfontPrev = SelectFont(hdc, plv->hfontLabel);
  8388. }
  8389. GetTextExtentPoint(hdc, psz, lstrlen(psz), &siz);
  8390. if (hdcFree) {
  8391. SelectFont(hdc, hfontPrev);
  8392. ReleaseDC(plv->ci.hwnd, hdcFree);
  8393. }
  8394. return siz.cx;
  8395. }
  8396. int NEAR ListView_OnGetColumnWidth(LV* plv, int iCol)
  8397. {
  8398. if (ListView_IsReportView(plv))
  8399. return ListView_RGetColumnWidth(plv, iCol);
  8400. else if (ListView_IsListView(plv))
  8401. return plv->cxItem;
  8402. return 0;
  8403. }
  8404. BOOL FAR PASCAL ListView_ISetColumnWidth(LV* plv, int iCol, int cx, BOOL fExplicit)
  8405. {
  8406. if (ListView_IsListView(plv))
  8407. {
  8408. if (iCol != 0 || cx <= 0)
  8409. return FALSE;
  8410. // if it's different and this is an explicit set, or we've never set it explicitly
  8411. if (plv->cxItem != cx && (fExplicit || !(plv->flags & LVF_COLSIZESET)))
  8412. {
  8413. // REVIEW: Should optimize what gets invalidated here...
  8414. plv->cxItem = cx;
  8415. if (fExplicit)
  8416. plv->flags |= LVF_COLSIZESET; // Set the fact that we explictly set size!.
  8417. if (ListView_IsLabelTip(plv))
  8418. {
  8419. // A truncated label may have been exposed or vice versa.
  8420. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  8421. }
  8422. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  8423. ListView_UpdateScrollBars(plv);
  8424. }
  8425. return TRUE;
  8426. }
  8427. else if (ListView_IsReportView(plv))
  8428. {
  8429. if (ListView_IsLabelTip(plv))
  8430. {
  8431. // A truncated label may have been exposed or vice versa.
  8432. ListView_InvalidateTTLastHit(plv, plv->iTTLastHit);
  8433. }
  8434. return ListView_RSetColumnWidth(plv, iCol, cx);
  8435. } else {
  8436. if (cx && plv->cxItem != cx && (fExplicit || !(plv->flags & LVF_COLSIZESET)))
  8437. {
  8438. // REVIEW: Should optimize what gets invalidated here...
  8439. plv->cxItem = cx;
  8440. if (fExplicit)
  8441. plv->flags |= LVF_COLSIZESET; // Set the fact that we explictly set size!.
  8442. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  8443. ListView_UpdateScrollBars(plv);
  8444. }
  8445. // BUG-FOR-BUG COMPATIBILITY: IE4 accidentally returned FALSE here.
  8446. }
  8447. return FALSE;
  8448. }
  8449. void NEAR ListView_Redraw(LV* plv, HDC hdc, RECT FAR* prcClip)
  8450. {
  8451. int i;
  8452. int cItem = ListView_Count(plv);
  8453. DWORD dwType = plv->ci.style & LVS_TYPEMASK;
  8454. NMCUSTOMDRAW nmcd;
  8455. LVDRAWITEM lvdi;
  8456. SetBkMode(hdc, TRANSPARENT);
  8457. SelectFont(hdc, plv->hfontLabel);
  8458. nmcd.hdc = hdc;
  8459. /// not implemented yet
  8460. //if (ptb->ci.hwnd == GetFocus())
  8461. //nmcd.uItemState = CDIS_FOCUS;
  8462. //else
  8463. nmcd.uItemState = 0;
  8464. nmcd.lItemlParam = 0;
  8465. nmcd.rc = *prcClip;
  8466. plv->ci.dwCustom = CICustomDrawNotify(&plv->ci, CDDS_PREPAINT, &nmcd);
  8467. if (!(plv->ci.dwCustom & CDRF_SKIPDEFAULT)) {
  8468. // Just before doing any painting, see if the region is up to date...
  8469. ListView_RecalcRegion(plv, FALSE, TRUE);
  8470. //
  8471. // For list view and report view, we can save a lot of time
  8472. // by calculating the index of the first item that may need
  8473. // painting...
  8474. //
  8475. switch (dwType) {
  8476. case LVS_REPORT:
  8477. i = ListView_RYHitTest(plv, prcClip->top);
  8478. cItem = ListView_RYHitTest(plv, prcClip->bottom) + 1;
  8479. break;
  8480. case LVS_LIST:
  8481. i = ListView_LCalcViewItem(plv, prcClip->left, prcClip->top );
  8482. cItem = ListView_LCalcViewItem( plv, prcClip->right, prcClip->bottom ) + 1;
  8483. break;
  8484. default:
  8485. if (ListView_IsOwnerData( plv ))
  8486. {
  8487. ListView_CalcMinMaxIndex( plv, prcClip, &i, &cItem );
  8488. break;
  8489. }
  8490. else
  8491. {
  8492. // REVIEW: we can keep a flag which tracks whether the view is
  8493. // presently in pre-arranged order and bypass Zorder when it is
  8494. i = 0; // Icon views no such hint
  8495. }
  8496. }
  8497. if (i < 0)
  8498. i = 0;
  8499. cItem = min( ListView_Count( plv ), cItem );
  8500. if (ListView_IsOwnerData( plv ) && (cItem > i))
  8501. {
  8502. ListView_NotifyCacheHint( plv, i, cItem-1 );
  8503. ListView_LazyCreateWinEvents( plv, i, cItem-1);
  8504. }
  8505. lvdi.plv = plv;
  8506. lvdi.nmcd.nmcd.hdc = hdc;
  8507. lvdi.prcClip = prcClip;
  8508. lvdi.pitem = NULL;
  8509. for (; i < cItem; i++)
  8510. {
  8511. BOOL bSuccess;
  8512. int i2;
  8513. if ((dwType == LVS_ICON || dwType == LVS_SMALLICON)
  8514. && (!ListView_IsOwnerData(plv)))
  8515. {
  8516. LISTITEM FAR *pitem;
  8517. // Icon views: Draw back-to-front mapped through
  8518. // Z-order array for proper Z order appearance - If autoarrange
  8519. // is on, we don't need to do this as our arrange code is setup
  8520. // to not overlap items!
  8521. //
  8522. // For the cases where we might have overlap, we sped this up,
  8523. // by converting the hdpaZorder into a list of indexes instead
  8524. // of pointers. This ovoids the costly convert pointer to
  8525. // index call.
  8526. //
  8527. i2 = (int)(UINT_PTR)DPA_FastGetPtr(plv->hdpaZOrder, (cItem - 1) -i);
  8528. //
  8529. // do a fast clip check on the item so we dont even try to
  8530. // draw it unless it is visible
  8531. //
  8532. // for small icon view we cant clip on the left without
  8533. // getting the text
  8534. //
  8535. // for large icon view we cant clip on the top without
  8536. // getting the text
  8537. //
  8538. // for large icon view in NOLABELWRAP mode, we can't clip
  8539. // on the top without getting the text, nor can we clip to
  8540. // the left or right in case the text is long.
  8541. //
  8542. // we can always clip to the bottom
  8543. //
  8544. pitem = ListView_FastGetItemPtr(plv, i2);
  8545. if (pitem->pt.x != RECOMPUTE)
  8546. {
  8547. if (pitem->pt.y - plv->ptOrigin.y > prcClip->bottom)
  8548. continue;
  8549. if (dwType == LVS_SMALLICON)
  8550. {
  8551. if (pitem->pt.x - plv->ptOrigin.x - plv->cxState > prcClip->right)
  8552. continue;
  8553. if (pitem->pt.y + plv->cyItem - plv->ptOrigin.y < prcClip->top)
  8554. continue;
  8555. }
  8556. else if (!(plv->ci.style & LVS_NOLABELWRAP))
  8557. {
  8558. if (pitem->pt.x - plv->cxIconSpacing - plv->ptOrigin.x > prcClip->right)
  8559. continue;
  8560. if (pitem->pt.x + plv->cxIconSpacing - plv->ptOrigin.x < prcClip->left)
  8561. continue;
  8562. }
  8563. }
  8564. }
  8565. else
  8566. i2 = i;
  8567. plv->iItemDrawing = i2;
  8568. lvdi.nmcd.nmcd.dwItemSpec = i2;
  8569. // these may get changed
  8570. lvdi.lpptOrg = NULL;
  8571. lvdi.flags = 0;
  8572. lvdi.nmcd.clrText = plv->clrText;
  8573. lvdi.nmcd.clrTextBk = plv->clrTextBk;
  8574. bSuccess = ListView_DrawItem(&lvdi);
  8575. if (!bSuccess) {
  8576. break;
  8577. }
  8578. }
  8579. if ((dwType == LVS_ICON || dwType == LVS_SMALLICON)
  8580. && (ListView_IsOwnerData(plv)) &&
  8581. plv->iFocus != -1) {
  8582. // since there's no zorder in ownerdata, we explicitly draw the focus guy last (again)
  8583. // so that it'll appear on top
  8584. // we may potentially want to do this for all items that are selected
  8585. plv->iItemDrawing = plv->iFocus;
  8586. lvdi.nmcd.nmcd.dwItemSpec = plv->iItemDrawing;
  8587. // these may get changed
  8588. lvdi.lpptOrg = NULL;
  8589. lvdi.flags = 0;
  8590. lvdi.nmcd.clrText = plv->clrText;
  8591. lvdi.nmcd.clrTextBk = plv->clrTextBk;
  8592. ListView_DrawItem(&lvdi);
  8593. }
  8594. // this is an NT5/Memphis feature.
  8595. if (ListView_Count(plv) == 0)
  8596. {
  8597. // there're no items in this view
  8598. // check if we need to display some text in this case.
  8599. if (ListView_GetEmptyText(plv))
  8600. {
  8601. RECT rcClip;
  8602. UINT flags = 0;
  8603. // Put some edging between the text and the border of the
  8604. // window so we don't slam up against the border.
  8605. // This keeps DBCS from looking horrid.
  8606. rcClip.left = g_cxEdge;
  8607. rcClip.top = g_cyEdge;
  8608. if (plv->dwExStyle & WS_EX_RTLREADING)
  8609. {
  8610. flags |= SHDT_RTLREADING;
  8611. }
  8612. // if its a report view && we have a header then move the text down
  8613. if (ListView_IsReportView(plv) && (!(plv->ci.style & LVS_NOCOLUMNHEADER)))
  8614. {
  8615. rcClip.top += plv->cyItem;
  8616. }
  8617. // Note: Use the full sizeClient.cx as the right margin
  8618. // in case pszEmptyText is wider than the client rectangle.
  8619. rcClip.left -= (int)plv->ptlRptOrigin.x;
  8620. rcClip.right = plv->sizeClient.cx;
  8621. rcClip.bottom = rcClip.top + plv->cyItem;
  8622. SHDrawText(hdc, plv->pszEmptyText,
  8623. &rcClip, LVCFMT_LEFT, flags,
  8624. plv->cyLabelChar, plv->cxEllipses,
  8625. plv->clrText, plv->clrBk);
  8626. }
  8627. }
  8628. plv->iItemDrawing = -1;
  8629. // post painting.... this is to do any extra (non item) painting
  8630. // such a grid lines
  8631. switch (dwType) {
  8632. case LVS_REPORT:
  8633. ListView_RAfterRedraw(plv, hdc);
  8634. break;
  8635. }
  8636. // notify parent afterwards if they want us to
  8637. if (plv->ci.dwCustom & CDRF_NOTIFYPOSTPAINT) {
  8638. CICustomDrawNotify(&plv->ci, CDDS_POSTPAINT, &nmcd);
  8639. }
  8640. }
  8641. }
  8642. BOOL NEAR ListView_DrawItem(PLVDRAWITEM plvdi)
  8643. {
  8644. BOOL bRet = TRUE;
  8645. UINT state;
  8646. if (!ListView_IsOwnerData( plvdi->plv )) {
  8647. plvdi->pitem = ListView_FastGetItemPtr(plvdi->plv, plvdi->nmcd.nmcd.dwItemSpec);
  8648. }
  8649. // notify on custom draw then do it!
  8650. plvdi->nmcd.nmcd.uItemState = 0;
  8651. plvdi->nmcd.nmcd.lItemlParam = (plvdi->pitem)? plvdi->pitem->lParam : 0;
  8652. if (!(plvdi->flags & LVDI_NOWAYFOCUS))
  8653. {
  8654. if (plvdi->plv->flags & LVF_FOCUSED) {
  8655. // if we're ownerdraw or asked to callback, go
  8656. // fetch the state
  8657. if (!plvdi->pitem || (plvdi->plv->stateCallbackMask & (LVIS_SELECTED | LVIS_FOCUSED))) {
  8658. state = (WORD) ListView_OnGetItemState(plvdi->plv, (int) plvdi->nmcd.nmcd.dwItemSpec,
  8659. LVIS_SELECTED | LVIS_FOCUSED);
  8660. } else {
  8661. state = plvdi->pitem->state;
  8662. }
  8663. if (state & LVIS_FOCUSED) {
  8664. plvdi->nmcd.nmcd.uItemState |= CDIS_FOCUS;
  8665. }
  8666. if (state & LVIS_SELECTED) {
  8667. plvdi->nmcd.nmcd.uItemState |= CDIS_SELECTED;
  8668. }
  8669. }
  8670. // NOTE: This is a bug. We should set CDIS_SELECTED only if the item
  8671. // really is selected. But this bug has existed forever so who knows
  8672. // what apps are relying on it. Standard workaround is for the client
  8673. // to do a GetItemState and reconfirm the LVIS_SELECTED flag.
  8674. // That's what we do in ListView_DrawImageEx.
  8675. if (plvdi->plv->ci.style & LVS_SHOWSELALWAYS) {
  8676. plvdi->nmcd.nmcd.uItemState |= CDIS_SELECTED;
  8677. }
  8678. }
  8679. if (!(CCGetUIState(&(plvdi->plv->ci)) & UISF_HIDEFOCUS))
  8680. {
  8681. plvdi->nmcd.nmcd.uItemState |= CDIS_SHOWKEYBOARDCUES;
  8682. }
  8683. plvdi->nmcd.clrText = plvdi->plv->clrText;
  8684. plvdi->nmcd.clrTextBk = (plvdi->plv->ci.style & WS_DISABLED ? plvdi->plv->clrBk : plvdi->plv->clrTextBk);
  8685. // PERF: if we decide to keep LVIS_EX_TWOCLICKACTIVATE, then we can
  8686. // call ListView_OnGetItem for LVIF_TEXT | LVIF_IMAGE | LVIF_STATE
  8687. // and pass the intem info into _ListView_DrawItem below.
  8688. if (plvdi->plv->iHot == (int)plvdi->nmcd.nmcd.dwItemSpec) {
  8689. // Handle the HOT case
  8690. if(plvdi->plv->clrHotlight != CLR_DEFAULT)
  8691. plvdi->nmcd.clrText = plvdi->plv->clrHotlight;
  8692. else
  8693. plvdi->nmcd.clrText = GetSysColor(COLOR_HOTLIGHT);
  8694. // ie4 bug 47635: if hotlight color is the same as the background
  8695. // color you don't see the text -- slam to a visible color in this case.
  8696. if (plvdi->nmcd.clrText == plvdi->nmcd.clrTextBk)
  8697. {
  8698. if (COLORISLIGHT(plvdi->nmcd.clrTextBk))
  8699. plvdi->nmcd.clrText = 0x000000; // black
  8700. else
  8701. plvdi->nmcd.clrText = 0xFFFFFF; // white
  8702. }
  8703. if ((plvdi->plv->exStyle & LVS_EX_ONECLICKACTIVATE) ||
  8704. ((plvdi->plv->exStyle & LVS_EX_TWOCLICKACTIVATE) &&
  8705. ListView_OnGetItemState(plvdi->plv, (int) plvdi->nmcd.nmcd.dwItemSpec, LVIS_SELECTED))) {
  8706. if ((plvdi->plv->exStyle & LVS_EX_UNDERLINEHOT) &&
  8707. (plvdi->plv->hFontHot))
  8708. SelectFont(plvdi->nmcd.nmcd.hdc, plvdi->plv->hFontHot);
  8709. else
  8710. SelectFont(plvdi->nmcd.nmcd.hdc, plvdi->plv->hfontLabel);
  8711. plvdi->nmcd.nmcd.uItemState |= CDIS_HOT;
  8712. }
  8713. } else if ((plvdi->plv->exStyle & LVS_EX_ONECLICKACTIVATE) ||
  8714. ((plvdi->plv->exStyle & LVS_EX_TWOCLICKACTIVATE) &&
  8715. ListView_OnGetItemState(plvdi->plv, (int) plvdi->nmcd.nmcd.dwItemSpec, LVIS_SELECTED))) {
  8716. // Handle the non-hot webview case
  8717. if ((plvdi->plv->exStyle & LVS_EX_UNDERLINECOLD) && (plvdi->plv->hFontHot))
  8718. SelectFont(plvdi->nmcd.nmcd.hdc, plvdi->plv->hFontHot);
  8719. else
  8720. SelectFont(plvdi->nmcd.nmcd.hdc, plvdi->plv->hfontLabel);
  8721. } else {
  8722. // Handle the non-webview case
  8723. SelectFont(plvdi->nmcd.nmcd.hdc, plvdi->plv->hfontLabel);
  8724. }
  8725. plvdi->dwCustom = CICustomDrawNotify(&plvdi->plv->ci, CDDS_ITEMPREPAINT, &plvdi->nmcd.nmcd);
  8726. plvdi->flags &= ~(LVDI_FOCUS | LVDI_SELECTED);
  8727. if (plvdi->nmcd.nmcd.uItemState & CDIS_FOCUS)
  8728. plvdi->flags |= LVDI_FOCUS;
  8729. if (plvdi->nmcd.nmcd.uItemState & CDIS_SELECTED) {
  8730. if (plvdi->plv->flags & LVF_FOCUSED)
  8731. plvdi->flags |= LVDI_SELECTED;
  8732. else
  8733. plvdi->flags |= LVDI_SELECTNOFOCUS;
  8734. if (plvdi->plv->iHot == (int)plvdi->nmcd.nmcd.dwItemSpec)
  8735. plvdi->flags |= LVDI_HOTSELECTED;
  8736. }
  8737. if (!(plvdi->dwCustom & CDRF_SKIPDEFAULT)) {
  8738. if (!ListView_IsOwnerData( plvdi->plv )) {
  8739. #ifdef DEBUG_NEWFONT
  8740. if ((plvdi->nmcd.nmcd.dwItemSpec % 3) == 0) {
  8741. plvdi->dwCustom |= CDRF_NEWFONT;
  8742. SelectObject(plvdi->nmcd.nmcd.hdc, GetStockObject(SYSTEM_FONT));
  8743. }
  8744. #endif
  8745. if (plvdi->dwCustom & CDRF_NEWFONT) {
  8746. ListView_RecomputeLabelSize(plvdi->plv, plvdi->pitem, (int) plvdi->nmcd.nmcd.dwItemSpec, plvdi->nmcd.nmcd.hdc, FALSE);
  8747. }
  8748. }
  8749. bRet = _ListView_DrawItem(plvdi);
  8750. if (plvdi->dwCustom & CDRF_NOTIFYPOSTPAINT) {
  8751. plvdi->nmcd.iSubItem = 0;
  8752. CICustomDrawNotify(&plvdi->plv->ci, CDDS_ITEMPOSTPAINT, &plvdi->nmcd.nmcd);
  8753. }
  8754. if (plvdi->dwCustom & CDRF_NEWFONT) {
  8755. SelectObject(plvdi->nmcd.nmcd.hdc, plvdi->plv->hfontLabel);
  8756. plvdi->plv->flags |= LVF_CUSTOMFONT;
  8757. }
  8758. }
  8759. return bRet;
  8760. }
  8761. // NOTE: this function requires a properly selected font.
  8762. //
  8763. void WINAPI SHDrawText(HDC hdc, LPCTSTR pszText, RECT FAR* prc, int fmt,
  8764. UINT flags, int cyChar, int cxEllipses, COLORREF clrText, COLORREF clrTextBk)
  8765. {
  8766. int cchText;
  8767. COLORREF clrSave, clrSaveBk = 0;
  8768. RECT rc;
  8769. UINT uETOFlags = 0;
  8770. BOOL fForeOnly = FALSE;
  8771. TCHAR ach[CCHLABELMAX + CCHELLIPSES];
  8772. int align;
  8773. // REVIEW: Performance idea:
  8774. // We could cache the currently selected text color
  8775. // so we don't have to set and restore it each time
  8776. // when the color is the same.
  8777. //
  8778. if (!pszText)
  8779. return;
  8780. if (IsRectEmpty(prc))
  8781. return;
  8782. if (flags & SHDT_RTLREADING)
  8783. {
  8784. align = GetTextAlign(hdc);
  8785. SetTextAlign(hdc, align | TA_RTLREADING);
  8786. }
  8787. rc = *prc;
  8788. // If needed, add in a little extra margin...
  8789. //
  8790. if (flags & SHDT_EXTRAMARGIN)
  8791. {
  8792. rc.left += g_cxLabelMargin * 3;
  8793. rc.right -= g_cxLabelMargin * 3;
  8794. }
  8795. else
  8796. {
  8797. rc.left += g_cxLabelMargin;
  8798. rc.right -= g_cxLabelMargin;
  8799. }
  8800. if ((rc.left >= rc.right) && !(flags & (SHDT_SELECTED | SHDT_DESELECTED | SHDT_SELECTNOFOCUS)))
  8801. return;
  8802. if ((flags & SHDT_ELLIPSES) &&
  8803. ListView_NeedsEllipses(hdc, pszText, &rc, &cchText, cxEllipses))
  8804. {
  8805. // In some cases cchText was comming back bigger than
  8806. // ARRYASIZE(ach), so we need to make sure we don't overflow the buffer
  8807. // if cchText is too big for the buffer, truncate it down to size
  8808. if (cchText >= ARRAYSIZE(ach) - CCHELLIPSES)
  8809. cchText = ARRAYSIZE(ach) - CCHELLIPSES - 1;
  8810. hmemcpy(ach, pszText, cchText * sizeof(TCHAR));
  8811. StringCchCopy(ach+cchText, ARRAYSIZE(ach)-cchText, c_szEllipses);
  8812. pszText = ach;
  8813. // Left-justify, in case there's no room for all of ellipses
  8814. //
  8815. fmt = LVCFMT_LEFT;
  8816. cchText += CCHELLIPSES;
  8817. }
  8818. else
  8819. {
  8820. cchText = lstrlen(pszText);
  8821. }
  8822. if (((clrTextBk == CLR_NONE) && !(flags & (SHDT_SELECTED | SHDT_SELECTNOFOCUS))) || (flags & SHDT_TRANSPARENT))
  8823. {
  8824. fForeOnly = TRUE;
  8825. clrSave = SetTextColor(hdc, (flags & SHDT_TRANSPARENT) ? 0 : clrText);
  8826. }
  8827. else
  8828. {
  8829. HBRUSH hbrUse = NULL;
  8830. HBRUSH hbrDelete = NULL;
  8831. uETOFlags |= ETO_OPAQUE;
  8832. if (flags & SHDT_SELECTED)
  8833. {
  8834. clrText = g_clrHighlightText;
  8835. clrTextBk = (flags & SHDT_HOTSELECTED) ? GetSysColor(COLOR_HOTLIGHT) : g_clrHighlight;
  8836. if (flags & SHDT_DRAWTEXT)
  8837. hbrUse = (flags & SHDT_HOTSELECTED) ? GetSysColorBrush(COLOR_HOTLIGHT) : g_hbrHighlight;
  8838. }
  8839. else if (flags & SHDT_SELECTNOFOCUS)
  8840. {
  8841. if ((clrTextBk == CLR_DEFAULT ? g_clrWindow : clrTextBk) == g_clrBtnFace)
  8842. {
  8843. // if the text background color in this mode is the same as the current
  8844. // background, use the color highlight text so that you can actually see somehting
  8845. clrText = g_clrHighlightText;
  8846. clrTextBk = g_clrHighlight;
  8847. if (flags & SHDT_DRAWTEXT)
  8848. hbrUse = g_hbrHighlight;
  8849. }
  8850. else
  8851. {
  8852. clrText = g_clrBtnText;
  8853. clrTextBk = g_clrBtnFace;
  8854. if (flags & SHDT_DRAWTEXT)
  8855. hbrUse = g_hbrBtnFace;
  8856. }
  8857. #ifdef LVDEBUG
  8858. if (GetAsyncKeyState(VK_CONTROL) < 0)
  8859. clrText = g_clrBtnHighlight;
  8860. #endif
  8861. }
  8862. else if (clrText == CLR_DEFAULT && clrTextBk == CLR_DEFAULT)
  8863. {
  8864. clrText = g_clrWindowText;
  8865. clrTextBk = g_clrWindow;
  8866. if ( ( flags & (SHDT_DRAWTEXT | SHDT_DESELECTED) ) ==
  8867. (SHDT_DRAWTEXT | SHDT_DESELECTED) )
  8868. {
  8869. hbrUse = g_hbrWindow;
  8870. }
  8871. }
  8872. else
  8873. {
  8874. if (clrText == CLR_DEFAULT)
  8875. clrText = g_clrWindowText;
  8876. if (clrTextBk == CLR_DEFAULT)
  8877. clrTextBk = g_clrWindow;
  8878. if ( ( flags & (SHDT_DRAWTEXT | SHDT_DESELECTED) ) ==
  8879. (SHDT_DRAWTEXT | SHDT_DESELECTED) )
  8880. {
  8881. hbrUse = CreateSolidBrush(GetNearestColor(hdc, clrTextBk));
  8882. if (hbrUse)
  8883. {
  8884. hbrDelete = hbrUse;
  8885. }
  8886. else
  8887. hbrUse = GetStockObject( WHITE_BRUSH );
  8888. }
  8889. }
  8890. // now set it
  8891. clrSave = SetTextColor(hdc, clrText);
  8892. clrSaveBk = SetBkColor(hdc, clrTextBk);
  8893. if (hbrUse) {
  8894. FillRect(hdc, prc, hbrUse);
  8895. if (hbrDelete)
  8896. DeleteObject(hbrDelete);
  8897. }
  8898. }
  8899. // If we want the item to display as if it was depressed, we will
  8900. // offset the text rectangle down and to the left
  8901. if (flags & SHDT_DEPRESSED)
  8902. OffsetRect(&rc, g_cxBorder, g_cyBorder);
  8903. if (flags & SHDT_DRAWTEXT)
  8904. {
  8905. UINT uDTFlags = DT_LVWRAP | DT_END_ELLIPSIS;
  8906. if (flags & SHDT_DTELLIPSIS)
  8907. uDTFlags |= DT_WORD_ELLIPSIS;
  8908. if ( !( flags & SHDT_CLIPPED ) )
  8909. uDTFlags |= DT_NOCLIP;
  8910. if (flags & SHDT_NODBCSBREAK)
  8911. uDTFlags |= DT_NOFULLWIDTHCHARBREAK;
  8912. DrawText(hdc, pszText, cchText, &rc, uDTFlags );
  8913. }
  8914. else
  8915. {
  8916. if (fmt != LVCFMT_LEFT)
  8917. {
  8918. SIZE siz;
  8919. GetTextExtentPoint(hdc, pszText, cchText, &siz);
  8920. if (fmt == LVCFMT_CENTER)
  8921. rc.left = (rc.left + rc.right - siz.cx) / 2;
  8922. else // fmt == LVCFMT_RIGHT
  8923. rc.left = rc.right - siz.cx;
  8924. }
  8925. // Center vertically in case the bitmap (to the left) is larger than
  8926. // the height of one line
  8927. rc.top += (rc.bottom - rc.top - cyChar) / 2;
  8928. if ( flags & SHDT_CLIPPED )
  8929. uETOFlags |= ETO_CLIPPED;
  8930. ExtTextOut(hdc, rc.left, rc.top, uETOFlags, prc, pszText, cchText, NULL);
  8931. }
  8932. if (flags & (SHDT_SELECTED | SHDT_DESELECTED | SHDT_TRANSPARENT))
  8933. {
  8934. SetTextColor(hdc, clrSave);
  8935. if (!fForeOnly)
  8936. SetBkColor(hdc, clrSaveBk);
  8937. }
  8938. if (flags & SHDT_RTLREADING)
  8939. {
  8940. SetTextAlign(hdc, align);
  8941. }
  8942. }
  8943. /*----------------------------------------------------------------
  8944. ** Create an imagelist to be used for dragging.
  8945. **
  8946. ** 1) create mask and image bitmap matching the select bounds size
  8947. ** 2) draw the text to both bitmaps (in black for now)
  8948. ** 3) create an imagelist with these bitmaps
  8949. ** 4) make a dithered copy of the image onto the new imagelist
  8950. **----------------------------------------------------------------*/
  8951. HIMAGELIST NEAR ListView_OnCreateDragImage(LV *plv, int iItem, LPPOINT lpptUpLeft)
  8952. {
  8953. HWND hwndLV = plv->ci.hwnd;
  8954. RECT rcBounds, rcImage, rcLabel;
  8955. HDC hdcMem = NULL;
  8956. HBITMAP hbmImage = NULL;
  8957. HBITMAP hbmMask = NULL;
  8958. HBITMAP hbmOld;
  8959. HIMAGELIST himl = NULL;
  8960. int dx, dy;
  8961. HIMAGELIST himlSrc;
  8962. LV_ITEM item;
  8963. POINT ptOrg;
  8964. LVDRAWITEM lvdi;
  8965. RECT rcSelBounds;
  8966. BOOL bMirroredWnd = (plv->ci.dwExStyle&RTL_MIRRORED_WINDOW);
  8967. if (!lpptUpLeft)
  8968. return NULL;
  8969. if (iItem >= ListView_Count(plv))
  8970. return NULL;
  8971. if (plv->iHot == iItem) {
  8972. ListView_OnSetHotItem(plv, -1);
  8973. UpdateWindow(plv->ci.hwnd);
  8974. }
  8975. ListView_GetRects(plv, iItem, &rcImage, &rcLabel, &rcBounds, &rcSelBounds);
  8976. if (ListView_IsIconView(plv)) {
  8977. ListView_UnfoldRects(plv, iItem, &rcImage, &rcLabel,
  8978. &rcBounds, &rcSelBounds);
  8979. InflateRect(&rcImage, -g_cxIconMargin, -g_cyIconMargin);
  8980. }
  8981. // chop off any extra filler above icon
  8982. ptOrg.x = rcBounds.left - rcSelBounds.left;
  8983. ptOrg.y = rcBounds.top - rcImage.top;
  8984. dx = rcSelBounds.right - rcSelBounds.left;
  8985. dy = rcSelBounds.bottom - rcImage.top;
  8986. lpptUpLeft->x = rcSelBounds.left;
  8987. lpptUpLeft->y = rcImage.top;
  8988. if (!(hdcMem = CreateCompatibleDC(NULL)))
  8989. goto CDI_Exit;
  8990. if (!(hbmImage = CreateColorBitmap(dx, dy)))
  8991. goto CDI_Exit;
  8992. if (!(hbmMask = CreateMonoBitmap(dx, dy)))
  8993. goto CDI_Exit;
  8994. //
  8995. // Mirror the memory DC so that the transition from
  8996. // mirrored(memDC)->non-mirrored(imagelist DCs)->mirrored(screenDC)
  8997. // is consistent. [samera]
  8998. //
  8999. if (bMirroredWnd)
  9000. {
  9001. SET_DC_RTL_MIRRORED(hdcMem);
  9002. }
  9003. // prepare for drawing the item
  9004. SelectObject(hdcMem, plv->hfontLabel);
  9005. SetBkMode(hdcMem, TRANSPARENT);
  9006. lvdi.plv = plv;
  9007. lvdi.nmcd.nmcd.dwItemSpec = iItem;
  9008. lvdi.pitem = NULL; // make sure it is null as Owner data uses this to trigger things...
  9009. lvdi.nmcd.nmcd.hdc = hdcMem;
  9010. lvdi.lpptOrg = &ptOrg;
  9011. lvdi.prcClip = NULL;
  9012. lvdi.flags = LVDI_NOIMAGE | LVDI_TRANSTEXT | LVDI_NOWAYFOCUS | LVDI_UNFOLDED;
  9013. /*
  9014. ** draw the text to both bitmaps
  9015. */
  9016. hbmOld = SelectObject(hdcMem, hbmImage);
  9017. // fill image with black for transparency
  9018. PatBlt(hdcMem, 0, 0, dx, dy, BLACKNESS);
  9019. ListView_DrawItem(&lvdi);
  9020. if (bMirroredWnd)
  9021. MirrorBitmapInDC(hdcMem, hbmImage);
  9022. lvdi.flags = LVDI_NOIMAGE | LVDI_TRANSTEXT | LVDI_NOWAYFOCUS | LVDI_UNFOLDED;
  9023. SelectObject(hdcMem, hbmMask);
  9024. // fill mask with white for transparency
  9025. PatBlt(hdcMem, 0, 0, dx, dy, WHITENESS);
  9026. ListView_DrawItem(&lvdi);
  9027. if (bMirroredWnd)
  9028. MirrorBitmapInDC(hdcMem, hbmMask);
  9029. // unselect objects that we used
  9030. SelectObject(hdcMem, hbmOld);
  9031. SelectObject(hdcMem, g_hfontSystem);
  9032. himlSrc = ListView_OnGetImageList(plv, !(ListView_IsIconView(plv)));
  9033. /*
  9034. ** make an image list that for now only has the text
  9035. ** we use ImageList_Clone so we get a imagelist that
  9036. ** the same color depth as our own imagelist
  9037. */
  9038. if (!(himl = ImageList_Clone(himlSrc, dx, dy, ILC_MASK, 1, 0)))
  9039. goto CDI_Exit;
  9040. ImageList_SetBkColor(himl, CLR_NONE);
  9041. ImageList_Add(himl, hbmImage, hbmMask);
  9042. /*
  9043. ** make a dithered copy of the image part onto our bitmaps
  9044. ** (need both bitmap and mask to be dithered)
  9045. */
  9046. if (himlSrc)
  9047. {
  9048. item.iItem = iItem;
  9049. item.iSubItem = 0;
  9050. item.mask = LVIF_IMAGE |LVIF_STATE;
  9051. item.stateMask = LVIS_OVERLAYMASK;
  9052. ListView_OnGetItem(plv, &item);
  9053. ImageList_CopyDitherImage(himl, 0, rcImage.left - rcSelBounds.left, 0, himlSrc, item.iImage, ((plv->ci.dwExStyle & dwExStyleRTLMirrorWnd) ? ILD_MIRROR : 0L) | (item.state & LVIS_OVERLAYMASK) );
  9054. }
  9055. CDI_Exit:
  9056. if (hdcMem)
  9057. DeleteObject(hdcMem);
  9058. if (hbmImage)
  9059. DeleteObject(hbmImage);
  9060. if (hbmMask)
  9061. DeleteObject(hbmMask);
  9062. return himl;
  9063. }
  9064. //-------------------------------------------------------------------
  9065. // ListView_OnGetTopIndex -- Gets the index of the first visible item
  9066. // For list view and report view this calculates the actual index
  9067. // for iconic views it alway returns 0
  9068. //
  9069. int NEAR ListView_OnGetTopIndex(LV* plv)
  9070. {
  9071. if (ListView_IsReportView(plv))
  9072. return (int)((plv->ptlRptOrigin.y) / plv->cyItem);
  9073. else if (ListView_IsListView(plv))
  9074. return (plv->xOrigin / plv->cxItem) * plv->cItemCol;
  9075. else
  9076. return(0);
  9077. }
  9078. //-------------------------------------------------------------------
  9079. // ListView_OnGetCountPerPage -- Gets the count of items that will fit
  9080. // on a page For list view and report view this calculates the
  9081. // count depending on the size of the window and for Iconic views it
  9082. // will always return the count of items in the list view.
  9083. //
  9084. int NEAR ListView_OnGetCountPerPage(LV* plv)
  9085. {
  9086. if (ListView_IsReportView(plv))
  9087. return (plv->sizeClient.cy - plv->yTop) / plv->cyItem;
  9088. else if (ListView_IsListView(plv))
  9089. return ((plv->sizeClient.cx)/ plv->cxItem)
  9090. * plv->cItemCol;
  9091. else
  9092. return (ListView_Count(plv));
  9093. }
  9094. /*----------------------------------------------------------------------------
  9095. / ListView_InvalidateFoldedItem implementation
  9096. / -----------------------------
  9097. / Purpose:
  9098. / Provides support for invalidating items within list views.
  9099. /
  9100. / Notes:
  9101. / Copes with invalidating the extra region in the list view that requires
  9102. / us to erase the background. Design to optimise out the ERASURE of the
  9103. / background.
  9104. /
  9105. / For details on the API see ListView_InvalidateItem.
  9106. /
  9107. / In:
  9108. / plv->ListView structure to work with
  9109. / iItem = item number
  9110. / bSrelectionOnly = refesh the selection
  9111. / fRedraw = Flags for RedrawWindow
  9112. / Out:
  9113. / -
  9114. /----------------------------------------------------------------------------*/
  9115. void NEAR ListView_InvalidateFoldedItem(LV* plv, int iItem, BOOL fSelectionOnly, UINT fRedraw)
  9116. {
  9117. ListView_InvalidateItem( plv, iItem, fSelectionOnly, fRedraw );
  9118. if ( ListView_IsIconView(plv) &&
  9119. ( !ListView_IsItemUnfolded(plv, iItem) || (fRedraw & RDW_ERASE) ) )
  9120. {
  9121. RECT rcLabel;
  9122. if (ListView_GetUnfoldedRect(plv, iItem, &rcLabel))
  9123. {
  9124. RedrawWindow(plv->ci.hwnd, &rcLabel, NULL, fRedraw|RDW_ERASE);
  9125. }
  9126. }
  9127. }
  9128. /*----------------------------------------------------------------------------
  9129. / ListView_UnfoldedRects implementation
  9130. / ----------------------
  9131. / Purpose:
  9132. / Having previously called get rects, then call this function to ensure
  9133. / that they are correctly unfolded.
  9134. /
  9135. / Notes:
  9136. / -
  9137. /
  9138. / In:
  9139. / plv-> list view to unfold on
  9140. / iItem = item number
  9141. / prcIcon -> icon bounding box
  9142. / prcLabel -> rectangle for the label structure
  9143. / prcBounds -> bounds rectangle / == NULL for none / These are currently the same for large icons
  9144. / prcSelectBounds -> selection bounds / == NULL /
  9145. / Out: TRUE if unfolding the item was worth anything
  9146. / -
  9147. /----------------------------------------------------------------------------*/
  9148. BOOL NEAR ListView_UnfoldRects(LV* plv, int iItem,
  9149. RECT * prcIcon, RECT * prcLabel,
  9150. RECT * prcBounds, RECT * prcSelectBounds)
  9151. {
  9152. LISTITEM item;
  9153. LISTITEM FAR* pitem = &item;
  9154. BOOL fRc = FALSE;
  9155. if (!ListView_IsIconView(plv))
  9156. return fRc;
  9157. // If we have a label pointer then expand as required
  9158. // nb - different paths for owner data
  9159. if ( prcLabel )
  9160. {
  9161. if ( !ListView_IsOwnerData(plv) )
  9162. {
  9163. pitem = ListView_GetItemPtr(plv, iItem);
  9164. if (!EVAL(pitem)) {
  9165. // DavidShi was able to get us into here with an invalid
  9166. // item number during a delete notification. So if the
  9167. // item number is invalid, just return a blank rectangle
  9168. // instead of faulting.
  9169. SetRectEmpty(prcLabel);
  9170. goto doneLabel;
  9171. }
  9172. }
  9173. else
  9174. {
  9175. ListView_RecomputeLabelSize( plv, pitem, iItem, NULL, FALSE );
  9176. }
  9177. if (prcLabel->bottom != prcLabel->top + max(pitem->cyUnfoldedLabel, pitem->cyFoldedLabel))
  9178. fRc = TRUE;
  9179. prcLabel->bottom = prcLabel->top + pitem->cyUnfoldedLabel;
  9180. }
  9181. doneLabel:
  9182. // Build the unions if required
  9183. if ( prcBounds && prcIcon && prcLabel )
  9184. {
  9185. UnionRect( prcBounds, prcIcon, prcLabel );
  9186. }
  9187. if ( prcSelectBounds && prcIcon && prcLabel )
  9188. {
  9189. UnionRect( prcSelectBounds, prcIcon, prcLabel );
  9190. }
  9191. return fRc;
  9192. }