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.

2377 lines
72 KiB

  1. // report view stuff (details)
  2. #include "ctlspriv.h"
  3. #include "listview.h"
  4. #include <limits.h>
  5. void ListView_RGetRectsEx(LV* plv, int iItem, int iSubItem, LPRECT prcIcon, LPRECT prcLabel);
  6. int ListView_RXHitTest(LV* plv, int x);
  7. void NEAR PASCAL ListView_RInitialize(LV* plv, BOOL fInval)
  8. {
  9. MEASUREITEMSTRUCT mi;
  10. if (plv && (plv->ci.style & LVS_OWNERDRAWFIXED)) {
  11. int iOld = plv->cyItem;
  12. mi.CtlType = ODT_LISTVIEW;
  13. mi.CtlID = GetDlgCtrlID(plv->ci.hwnd);
  14. mi.itemHeight = plv->cyItem; // default
  15. SendMessage(plv->ci.hwndParent, WM_MEASUREITEM, mi.CtlID, (LPARAM)(MEASUREITEMSTRUCT FAR *)&mi);
  16. plv->cyItem = max(mi.itemHeight, 1); // never let app set height=0 or we fault-o-rama!
  17. if (fInval && (iOld != plv->cyItem)) {
  18. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
  19. }
  20. }
  21. }
  22. DWORD ListView_RApproximateViewRect(LV* plv, int iCount, int iWidth, int iHeight)
  23. {
  24. RECT rc;
  25. ListView_RGetRects(plv, iCount, NULL, NULL, &rc, NULL);
  26. rc.bottom += plv->ptlRptOrigin.y;
  27. rc.right += plv->ptlRptOrigin.x;
  28. return MAKELONG(rc.right, rc.bottom);
  29. }
  30. void CCDrawRect(HDC hdc, int x, int y, int dx, int dy)
  31. {
  32. RECT rc;
  33. rc.left = x;
  34. rc.top = y;
  35. rc.right = x + dx;
  36. rc.bottom = y + dy;
  37. ExtTextOut(hdc,0,0,ETO_OPAQUE,&rc,NULL,0,NULL);
  38. }
  39. void NEAR PASCAL ListView_RAfterRedraw(LV* plv, HDC hdc)
  40. {
  41. if (plv->exStyle & LVS_EX_GRIDLINES) {
  42. int i;
  43. int x;
  44. COLORREF clrBk;
  45. clrBk = SetBkColor(hdc, g_clrBtnFace);
  46. x = -plv->ptlRptOrigin.x;
  47. for (i = 0 ; (i < plv->cCol) && (x < plv->sizeClient.cx); i++) {
  48. HD_ITEM hitem;
  49. hitem.mask = HDI_WIDTH;
  50. Header_GetItem(plv->hwndHdr,
  51. SendMessage(plv->hwndHdr, HDM_ORDERTOINDEX, i, 0),
  52. &hitem);
  53. x += hitem.cxy;
  54. if (x > 0) {
  55. CCDrawRect(hdc, x, 0, g_cxBorder, plv->sizeClient.cy);
  56. }
  57. }
  58. for (x = plv->yTop - 1; (x < plv->sizeClient.cy); x += plv->cyItem) {
  59. CCDrawRect(hdc, 0, x, plv->sizeClient.cx, g_cxBorder);
  60. }
  61. SetBkColor(hdc, clrBk);
  62. }
  63. }
  64. //
  65. // Internal function to Get the CXLabel, taking into account if the listview
  66. // has no item data and also if RECOMPUTE needs to happen.
  67. //
  68. SHORT NEAR PASCAL ListView_RGetCXLabel(LV* plv, int i, LISTITEM FAR* pitem,
  69. HDC hdc, BOOL fUseItem)
  70. {
  71. SHORT cxLabel = SRECOMPUTE;
  72. if (!ListView_IsOwnerData( plv )) {
  73. cxLabel = pitem->cxSingleLabel;
  74. }
  75. if (cxLabel == SRECOMPUTE)
  76. {
  77. LISTITEM item;
  78. if (!pitem)
  79. {
  80. ASSERT(!fUseItem)
  81. pitem = &item;
  82. fUseItem = FALSE;
  83. }
  84. ListView_RecomputeLabelSize(plv, pitem, i, hdc, fUseItem);
  85. cxLabel = pitem->cxSingleLabel;
  86. }
  87. // add on the space around the label taken up by the select rect
  88. cxLabel += 2*g_cxLabelMargin;
  89. return(cxLabel);
  90. }
  91. //
  92. // Returns FALSE if no more items to draw.
  93. //
  94. BOOL ListView_RDrawItem(PLVDRAWITEM plvdi)
  95. {
  96. BOOL fDrawFocusRect = FALSE;
  97. RECT rcIcon;
  98. RECT rcLabel;
  99. RECT rcBounds;
  100. RECT rcT;
  101. LV* plv = plvdi->plv;
  102. int iCol = 0;
  103. LVITEM item;
  104. HDITEM hitem;
  105. TCHAR ach[CCHLABELMAX];
  106. UINT fText = 0;
  107. UINT uSubItemFlags;
  108. int iIndex = 0;
  109. int xOffset = 0;
  110. int yOffset = 0;
  111. ListView_RGetRects(plv, (int)plvdi->nmcd.nmcd.dwItemSpec, NULL, NULL, &rcBounds, NULL);
  112. if (rcBounds.bottom <= plv->yTop)
  113. return TRUE;
  114. if (plvdi->prcClip)
  115. {
  116. if (rcBounds.top >= plvdi->prcClip->bottom)
  117. return FALSE; // no more items need painting.
  118. // Probably this condition won't happen very often...
  119. if (!IntersectRect(&rcT, &rcBounds, plvdi->prcClip))
  120. return TRUE;
  121. }
  122. // REVIEW: this would be faster if we did the GetClientRect
  123. // outside the loop.
  124. //
  125. if (rcBounds.top >= plv->sizeClient.cy)
  126. return FALSE;
  127. if (plvdi->lpptOrg)
  128. {
  129. xOffset = plvdi->lpptOrg->x - rcBounds.left;
  130. yOffset = plvdi->lpptOrg->y - rcBounds.top;
  131. OffsetRect(&rcBounds, xOffset, yOffset);
  132. }
  133. item.iItem = (int)plvdi->nmcd.nmcd.dwItemSpec;
  134. item.stateMask = LVIS_ALL;
  135. // for first ListView_OnGetItem call
  136. item.state = 0;
  137. if (plv->ci.style & LVS_OWNERDRAWFIXED) {
  138. goto SendOwnerDraw;
  139. }
  140. SetRectEmpty(&rcT);
  141. for (; iCol < plv->cCol; iCol++)
  142. {
  143. DWORD dwCustom = 0;
  144. UINT uImageFlags;
  145. iIndex = (int) SendMessage(plv->hwndHdr, HDM_ORDERTOINDEX, iCol, 0);
  146. SendOwnerDraw:
  147. if (iIndex == 0) {
  148. item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_INDENT;
  149. } else {
  150. // Next time through, we only want text for subitems...
  151. item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
  152. }
  153. item.iImage = -1;
  154. item.iSubItem = iIndex;
  155. item.pszText = ach;
  156. item.cchTextMax = ARRAYSIZE(ach);
  157. ListView_OnGetItem(plv, &item);
  158. uSubItemFlags = plvdi->flags;
  159. if (iIndex == 0) {
  160. // if it's owner draw, send off a message and return.
  161. // do this after we've collected state information above though
  162. if (plv->ci.style & LVS_OWNERDRAWFIXED) {
  163. DRAWITEMSTRUCT di = {0};
  164. di.CtlType = ODT_LISTVIEW;
  165. di.CtlID = GetDlgCtrlID(plv->ci.hwnd);
  166. di.itemID = (int)plvdi->nmcd.nmcd.dwItemSpec;
  167. di.itemAction = ODA_DRAWENTIRE;
  168. di.hwndItem = plv->ci.hwnd;
  169. di.hDC = plvdi->nmcd.nmcd.hdc;
  170. di.rcItem = rcBounds;
  171. if (plvdi->pitem)
  172. di.itemData = plvdi->pitem->lParam;
  173. if (item.state & LVIS_FOCUSED) {
  174. di.itemState |= ODS_FOCUS;
  175. }
  176. if (item.state & LVIS_SELECTED) {
  177. di.itemState |= ODS_SELECTED;
  178. }
  179. SendMessage(plv->ci.hwndParent, WM_DRAWITEM, di.CtlID,
  180. (LPARAM)(DRAWITEMSTRUCT FAR *)&di);
  181. return TRUE;
  182. }
  183. }
  184. hitem.mask = HDI_WIDTH | HDI_FORMAT;
  185. Header_GetItem(plv->hwndHdr, iIndex, &hitem);
  186. // first get the rects...
  187. ListView_RGetRectsEx(plv, (int)plvdi->nmcd.nmcd.dwItemSpec, iIndex, &rcIcon, &rcLabel);
  188. OffsetRect(&rcIcon, xOffset, yOffset);
  189. OffsetRect(&rcLabel, xOffset, yOffset);
  190. if (plvdi->dwCustom & CDRF_NOTIFYSUBITEMDRAW) {
  191. RECT rcTemp;
  192. UINT uItemStateOld = plvdi->nmcd.nmcd.uItemState, uItemStateNew;
  193. SendMessage(plv->hwndHdr, HDM_GETITEMRECT, iIndex, (LPARAM)&rcTemp);
  194. plvdi->nmcd.nmcd.rc.left = rcTemp.left;
  195. plvdi->nmcd.nmcd.rc.right = rcTemp.right;
  196. plvdi->nmcd.iSubItem = iIndex;
  197. // Note: IE4 didn't reset nmcd.clrText or nmcd.clrTextBk
  198. // before each subitem. This is arguably a bug, but we
  199. // shipped that way so I guess we're stuck with it.
  200. dwCustom = CICustomDrawNotify(&plvdi->plv->ci, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &plvdi->nmcd.nmcd);
  201. uItemStateNew = plvdi->nmcd.nmcd.uItemState;
  202. plvdi->nmcd.nmcd.uItemState = uItemStateOld;
  203. if (dwCustom & CDRF_SKIPDEFAULT)
  204. continue;
  205. uSubItemFlags &= ~(LVDI_FOCUS | LVDI_SELECTED | LVDI_SELECTNOFOCUS | LVDI_HOTSELECTED);
  206. if (uItemStateNew & CDIS_FOCUS)
  207. uSubItemFlags |= LVDI_FOCUS;
  208. if (uItemStateNew & CDIS_SELECTED) {
  209. if (plvdi->plv->flags & LVF_FOCUSED)
  210. uSubItemFlags |= LVDI_SELECTED;
  211. else
  212. uSubItemFlags |= LVDI_SELECTNOFOCUS;
  213. if (plvdi->plv->iHot == (int)plvdi->nmcd.nmcd.dwItemSpec)
  214. uSubItemFlags |= LVDI_HOTSELECTED;
  215. }
  216. }
  217. if (iIndex != 0)
  218. {
  219. // for right now, add this in because the get rects for
  220. // non 0 doesn't account for the icon (yet)
  221. if (item.iImage != -1)
  222. rcLabel.left += plv->cxSmIcon;
  223. }
  224. uImageFlags = uSubItemFlags;
  225. if (item.iImage == -1) {
  226. if (iIndex != 0)
  227. // just use ListView_DrawImage to get the fText
  228. uImageFlags |= LVDI_NOIMAGE;
  229. }
  230. else if (ListView_FullRowSelect(plv) && iCol)
  231. {
  232. int iLeft = rcIcon.left;
  233. int iRight = rcIcon.right;
  234. // for full row select when the icon is not in the first column,
  235. // we need to explicitly paint the background so focus rect
  236. // remnants aren't left behind (jeffbog -- 07/09/96)
  237. /// need to deal with the state image if index == 0.
  238. // all the otehr columns don't have state images.
  239. if (iIndex == 0) {
  240. rcIcon.left -= plv->cxState + g_cxEdge;
  241. }
  242. rcIcon.right = rcLabel.right;
  243. FillRect(plvdi->nmcd.nmcd.hdc, &rcIcon, plv->hbrBk);
  244. rcIcon.left = iLeft;
  245. rcIcon.right = iRight;
  246. }
  247. fText = ListView_DrawImageEx(plv, &item, plvdi->nmcd.nmcd.hdc,
  248. rcIcon.left, rcIcon.top, uSubItemFlags, rcLabel.right);
  249. if (ListView_FullRowSelect(plv) && (uSubItemFlags & LVDI_FOCUS)) {
  250. // if we're doing a full row selection, collect the union
  251. // of the labels for the focus rect
  252. UnionRect(&rcT, &rcT, &rcLabel);
  253. }
  254. if (item.pszText)
  255. {
  256. int xLabelRight = rcLabel.right;
  257. UINT textflags;
  258. // give all but the first columns extra margins so
  259. // left and right justified things don't stick together
  260. textflags = (iIndex == 0) ? SHDT_ELLIPSES : SHDT_ELLIPSES | SHDT_EXTRAMARGIN;
  261. // rectangle limited to the size of the string
  262. textflags |= fText;
  263. if ((!ListView_FullRowSelect(plv)) &&
  264. ((fText & (SHDT_SELECTED | SHDT_SELECTNOFOCUS)) || (item.state & LVIS_FOCUSED)))
  265. {
  266. int cxLabel;
  267. // if selected or focused, the rectangle is more
  268. // meaningful and should correspond to the string
  269. //
  270. if (iIndex == 0) {
  271. LISTITEM litem;
  272. LISTITEM FAR *pitem = plvdi->pitem;
  273. if (!pitem) {
  274. pitem = &litem;
  275. litem.pszText = item.pszText;
  276. }
  277. cxLabel = ListView_RGetCXLabel(plv, (int)plvdi->nmcd.nmcd.dwItemSpec, pitem, plvdi->nmcd.nmcd.hdc, TRUE);
  278. } else {
  279. // add g_cxLabelMargin * 6 because we use SHDT_EXTRAMARGIN
  280. // on iIndex != 0
  281. // and if you look inside shdrawtext, there are 6 cxlabelmargins added...
  282. cxLabel = ListView_OnGetStringWidth(plv, item.pszText, plvdi->nmcd.nmcd.hdc) + g_cxLabelMargin * 6;
  283. }
  284. if (rcLabel.right > rcLabel.left + cxLabel)
  285. {
  286. rcLabel.right = rcLabel.left + cxLabel;
  287. }
  288. }
  289. if ((iIndex != 0) || (plv->iEdit != (int)plvdi->nmcd.nmcd.dwItemSpec))
  290. {
  291. COLORREF clrText;
  292. HFONT hFontTemp = NULL;
  293. int cxEllipses;
  294. clrText = plvdi->nmcd.clrText;
  295. if ((clrText == GetSysColor(COLOR_HOTLIGHT)) ||
  296. ((plv->exStyle & LVS_EX_UNDERLINEHOT) &&
  297. ((plv->exStyle & LVS_EX_ONECLICKACTIVATE) ||
  298. ((plvdi->plv->exStyle & LVS_EX_TWOCLICKACTIVATE) &&
  299. ListView_OnGetItemState(plvdi->plv, (int) plvdi->nmcd.nmcd.dwItemSpec, LVIS_SELECTED))))) {
  300. if (iIndex != 0 && !ListView_FullRowSelect(plv)) {
  301. hFontTemp = SelectFont(plvdi->nmcd.nmcd.hdc, plv->hfontLabel);
  302. if (hFontTemp != plv->hFontHot) {
  303. // they've overridden... leave it.
  304. SelectFont(plvdi->nmcd.nmcd.hdc, hFontTemp);
  305. hFontTemp = NULL;
  306. }
  307. clrText = plv->clrText;
  308. }
  309. }
  310. if ((textflags & SHDT_SELECTED) && (uSubItemFlags & LVDI_HOTSELECTED))
  311. textflags |= SHDT_HOTSELECTED;
  312. //TraceMsg(TF_LISTVIEW, "LISTVIEW: SHDrawText called. style = %lx, WS_DISABLED = %lx, plvdi->clrBk = %lx, plvdi->nmcd.clrTextBk = %lx", (DWORD)plv->ci.style, (DWORD)WS_DISABLED, plvdi->clrBk, plvdi->nmcd.clrTextBk);
  313. if( plv->dwExStyle & WS_EX_RTLREADING)
  314. {
  315. //
  316. // temp hack for the find.files to see if LtoR/RtoL mixing
  317. // works. if ok, we'll take this out and make that lv ownerdraw
  318. //
  319. if ((item.pszText[0] != '\xfd') && (item.pszText[lstrlen(item.pszText)-1] != '\xfd'))
  320. textflags |= SHDT_RTLREADING;
  321. }
  322. //
  323. // If the app customized the font, we need to get the new
  324. // ellipsis size. We could try to optimize not doing this
  325. // if ellipses aren't needed, but tough. That's what you
  326. // get if you use customdraw.
  327. //
  328. if ((plvdi->dwCustom | dwCustom) & CDRF_NEWFONT)
  329. {
  330. SIZE siz;
  331. GetTextExtentPoint(plvdi->nmcd.nmcd.hdc, c_szEllipses, CCHELLIPSES, &siz);
  332. cxEllipses = siz.cx;
  333. }
  334. else
  335. cxEllipses = plv->cxEllipses;
  336. SHDrawText(plvdi->nmcd.nmcd.hdc, item.pszText, &rcLabel,
  337. hitem.fmt & HDF_JUSTIFYMASK, textflags,
  338. plv->cyLabelChar, cxEllipses,
  339. clrText, plvdi->nmcd.clrTextBk);
  340. // draw a focus rect on the first column of a focus item
  341. if ((uSubItemFlags & LVDI_FOCUS) && (item.state & LVIS_FOCUSED)
  342. && !(CCGetUIState(&(plvdi->plv->ci)) & UISF_HIDEFOCUS)
  343. )
  344. {
  345. if (ListView_FullRowSelect(plv)) {
  346. fDrawFocusRect = TRUE;
  347. } else {
  348. DrawFocusRect(plvdi->nmcd.nmcd.hdc, &rcLabel);
  349. }
  350. }
  351. // If we didn't SHDrawText into the full label rectangle
  352. // (because the selection or focus rectangle seemed more
  353. // applicable), draw emptiness into the unused part so
  354. // garbage doesn't show through. Use SHDrawText so we
  355. // draw in exactly the same way that the rest of the code does.
  356. if (rcLabel.right < xLabelRight)
  357. {
  358. rcLabel.left = rcLabel.right;
  359. rcLabel.right = xLabelRight;
  360. SHDrawText(plvdi->nmcd.nmcd.hdc, c_szNULL, &rcLabel,
  361. LVCFMT_LEFT,
  362. textflags & SHDT_TRANSPARENT,
  363. plv->cyLabelChar, cxEllipses,
  364. clrText, plvdi->nmcd.clrTextBk);
  365. }
  366. // restore the font
  367. if (hFontTemp)
  368. SelectFont(plvdi->nmcd.nmcd.hdc, hFontTemp);
  369. }
  370. }
  371. if (dwCustom & CDRF_NOTIFYPOSTPAINT) {
  372. CICustomDrawNotify(&plvdi->plv->ci, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &plvdi->nmcd.nmcd);
  373. }
  374. }
  375. if (fDrawFocusRect) {
  376. DrawFocusRect(plvdi->nmcd.nmcd.hdc, &rcT);
  377. }
  378. return TRUE;
  379. }
  380. #ifndef HDS_FULLDRAG
  381. #define HDS_FULLDRAG 0x0080
  382. #endif
  383. BOOL_PTR NEAR ListView_CreateHeader(LV* plv)
  384. {
  385. // enable drag drop always here... just fail the notify
  386. // if the bit in listview isn't set
  387. DWORD dwStyle = HDS_HORZ | WS_CHILD | HDS_DRAGDROP;
  388. if (plv->ci.style & LVS_NOCOLUMNHEADER)
  389. dwStyle |= HDS_HIDDEN;
  390. if (!(plv->ci.style & LVS_NOSORTHEADER))
  391. dwStyle |= HDS_BUTTONS;
  392. dwStyle |= HDS_FULLDRAG;
  393. plv->hwndHdr = CreateWindowEx(0L, c_szHeaderClass, // WC_HEADER,
  394. NULL, dwStyle, 0, 0, 0, 0, plv->ci.hwnd, (HMENU)LVID_HEADER, GetWindowInstance(plv->ci.hwnd), NULL);
  395. if (plv->hwndHdr) {
  396. #ifdef POST_BETA
  397. NMLVHEADERCREATED nmhc;
  398. nmhc.hwndHdr = plv->hwndHdr;
  399. // some apps blow up if a notify is sent before the control is fully created.
  400. CCSendNotify(&plv->ci, LVN_HEADERCREATED, &nmhc.hdr);
  401. plv->hwndHdr = nmhc.hwndHdr;
  402. #endif
  403. FORWARD_WM_SETFONT(plv->hwndHdr, plv->hfontLabel, FALSE, SendMessage);
  404. if (plv->himlSmall)
  405. SendMessage(plv->hwndHdr, HDM_SETIMAGELIST, 0, (LPARAM)plv->himlSmall);
  406. }
  407. return (BOOL_PTR)plv->hwndHdr;
  408. }
  409. int NEAR ListView_OnInsertColumnA(LV* plv, int iCol, LV_COLUMNA * pcol) {
  410. LPWSTR pszW = NULL;
  411. LPSTR pszC = NULL;
  412. int iRet;
  413. //HACK ALERT -- this code assumes that LV_COLUMNA is exactly the same
  414. // as LV_COLUMNW except for the pointer to the string.
  415. ASSERT(sizeof(LV_COLUMNA) == sizeof(LV_COLUMNW));
  416. if (!pcol)
  417. return -1;
  418. if ((pcol->mask & LVCF_TEXT) && (pcol->pszText != NULL)) {
  419. pszC = pcol->pszText;
  420. if ((pszW = ProduceWFromA(plv->ci.uiCodePage, pszC)) == NULL)
  421. {
  422. // NT's IE4 returned -1, so we keep doing it in IE5.
  423. return -1;
  424. } else {
  425. pcol->pszText = (LPSTR)pszW;
  426. }
  427. }
  428. iRet = ListView_OnInsertColumn(plv, iCol, (const LV_COLUMN FAR*) pcol);
  429. if (pszW != NULL) {
  430. pcol->pszText = pszC;
  431. FreeProducedString(pszW);
  432. }
  433. return iRet;
  434. }
  435. int NEAR ListView_OnInsertColumn(LV* plv, int iCol, const LV_COLUMN FAR* pcol)
  436. {
  437. int idpa = -1;
  438. HD_ITEM item;
  439. ASSERT(LVCFMT_LEFT == HDF_LEFT);
  440. ASSERT(LVCFMT_RIGHT == HDF_RIGHT);
  441. ASSERT(LVCFMT_CENTER == HDF_CENTER);
  442. if (iCol < 0 || !pcol)
  443. return -1;
  444. if (!plv->hwndHdr && !ListView_CreateHeader(plv))
  445. return -1;
  446. item.mask = (HDI_WIDTH | HDI_HEIGHT | HDI_FORMAT | HDI_LPARAM);
  447. if (pcol->mask & LVCF_IMAGE) {
  448. // do this only if this bit is set so that we don't fault on
  449. // old binaries
  450. item.iImage = pcol->iImage;
  451. item.mask |= HDI_IMAGE;
  452. }
  453. if (pcol->mask & LVCF_TEXT) {
  454. item.pszText = pcol->pszText;
  455. item.mask |= HDI_TEXT;
  456. }
  457. if (pcol->mask & LVCF_ORDER) {
  458. item.iOrder = pcol->iOrder;
  459. item.mask |= HDI_ORDER;
  460. }
  461. item.cxy = pcol->mask & LVCF_WIDTH ? pcol->cx : 10; // some random default
  462. item.fmt = ((pcol->mask & LVCF_FMT) && (iCol > 0)) ? pcol->fmt : LVCFMT_LEFT;
  463. item.hbm = NULL;
  464. item.lParam = pcol->mask & LVCF_SUBITEM ? pcol->iSubItem : 0;
  465. // Column 0 refers to the item list. If we've already added a
  466. // column, make sure there are plv->cCol - 1 subitem ptr slots
  467. // in hdpaSubItems...
  468. //
  469. if (plv->cCol > 0)
  470. {
  471. if (!plv->hdpaSubItems)
  472. {
  473. plv->hdpaSubItems = DPA_CreateEx(8, plv->hheap);
  474. if (!plv->hdpaSubItems)
  475. return -1;
  476. }
  477. // WARNING: the max(0, iCol-1) was min in Win95, which was
  478. // just wrong. hopefully(!) no one has relied on this brokeness
  479. // if so, we may have to version switch it.
  480. idpa = DPA_InsertPtr(plv->hdpaSubItems, max(0, iCol - 1), NULL);
  481. if (idpa == -1)
  482. return -1;
  483. }
  484. iCol = Header_InsertItem(plv->hwndHdr, iCol, &item);
  485. if (iCol == -1)
  486. {
  487. if (plv->hdpaSubItems && (idpa != -1))
  488. DPA_DeletePtr(plv->hdpaSubItems, idpa);
  489. return -1;
  490. }
  491. plv->xTotalColumnWidth = RECOMPUTE;
  492. plv->cCol++;
  493. ListView_UpdateScrollBars(plv);
  494. if (ListView_IsReportView(plv) && ListView_RedrawEnabled(plv)) {
  495. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
  496. }
  497. return iCol;
  498. }
  499. int NEAR ListView_FreeColumnData(LPVOID d, LPVOID p)
  500. {
  501. PLISTSUBITEM plsi = (PLISTSUBITEM)d;
  502. ListView_FreeSubItem(plsi);
  503. return 1;
  504. }
  505. BOOL NEAR ListView_OnDeleteColumn(LV* plv, int iCol)
  506. {
  507. if (iCol < 0 || iCol >= plv->cCol) // validate column index
  508. {
  509. RIPMSG(0, "LVM_DELETECOLUMN: Invalid column index: %d", iCol);
  510. return FALSE;
  511. }
  512. if (plv->hdpaSubItems)
  513. {
  514. int iDeleteColumn = -1; // Default to "can't delete column".
  515. if (0 < iCol) {
  516. iDeleteColumn = iCol; // Deleting col 1+. Simple delete OK.
  517. }
  518. else if (5 <= plv->ci.iVersion) {
  519. //
  520. // Version 5 and later can delete column 0. Prior versions can't.
  521. //
  522. if (1 < plv->cCol && !ListView_IsOwnerData(plv)) {
  523. // if deleting column 0,
  524. // we have to do something a little special...
  525. // set all item 0 strings to what column 1 has and
  526. // delete column 1
  527. int i;
  528. int iCount = ListView_Count(plv);
  529. for (i = 0; i < iCount; i++) {
  530. LISTSUBITEM lsi;
  531. LVITEM lvi;
  532. ListView_GetSubItem(plv, i, 1, &lsi);
  533. lvi.iSubItem = 0;
  534. lvi.iItem = i;
  535. lvi.mask = LVIF_TEXT | LVIF_IMAGE;
  536. lvi.iImage = lsi.iImage;
  537. lvi.pszText = lsi.pszText;
  538. lvi.state = lsi.state;
  539. lvi.stateMask = 0xffffffff;
  540. ListView_OnSetItem(plv, &lvi);
  541. }
  542. iDeleteColumn = 1;
  543. }
  544. }
  545. if (0 < iDeleteColumn) {
  546. HDPA hdpa = (HDPA)DPA_DeletePtr(plv->hdpaSubItems, iDeleteColumn - 1);
  547. DPA_DestroyCallback(hdpa, ListView_FreeColumnData, 0);
  548. }
  549. }
  550. if (!Header_DeleteItem(plv->hwndHdr, iCol))
  551. return FALSE;
  552. plv->cCol--;
  553. plv->xTotalColumnWidth = RECOMPUTE;
  554. ListView_UpdateScrollBars(plv);
  555. if (ListView_IsReportView(plv) && ListView_RedrawEnabled(plv)) {
  556. RedrawWindow(plv->ci.hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
  557. }
  558. return TRUE;
  559. }
  560. int NEAR ListView_RGetColumnWidth(LV* plv, int iCol)
  561. {
  562. HD_ITEM item;
  563. item.mask = HDI_WIDTH;
  564. Header_GetItem(plv->hwndHdr, iCol, &item);
  565. return item.cxy;
  566. }
  567. // The FakeCustomDraw functions are used when you want the customdraw client
  568. // to set up a HDC so you can do stuff like GetTextExtent.
  569. //
  570. // Usage:
  571. //
  572. // LVFAKEDRAW lvfd;
  573. // LV_ITEM item;
  574. // ListView_BeginFakeCustomDraw(plv, &lvfd, &item);
  575. // for each item you care about {
  576. // item.iItem = iItem;
  577. // item.iItem = iSubItem;
  578. // item.lParam = <item lParam>; // use ListView_OnGetItem to get it
  579. // ListView_BeginFakeItemDraw(&lvfd);
  580. // <party on the HDC in lvfd.nmcd.nmcd.hdc>
  581. // ListView_EndFakeItemDraw(&lvfd);
  582. // }
  583. // ListView_EndFakeCustomDraw(&lvfd);
  584. //
  585. void ListView_BeginFakeCustomDraw(LV* plv, PLVFAKEDRAW plvfd, LV_ITEM *pitem)
  586. {
  587. plvfd->nmcd.nmcd.hdc = GetDC(plv->ci.hwnd);
  588. plvfd->nmcd.nmcd.uItemState = 0;
  589. plvfd->nmcd.nmcd.dwItemSpec = 0;
  590. plvfd->nmcd.nmcd.lItemlParam = 0;
  591. plvfd->hfontPrev = SelectFont(plvfd->nmcd.nmcd.hdc, plv->hfontLabel);
  592. //
  593. // Since we aren't actually painting anything, we pass an empty
  594. // paint rectangle. Gosh, I hope no app faults when it sees an
  595. // empty paint rectangle.
  596. //
  597. SetRectEmpty(&plvfd->nmcd.nmcd.rc);
  598. plvfd->plv = plv;
  599. plvfd->dwCustomPrev = plv->ci.dwCustom;
  600. plvfd->pitem = pitem;
  601. plv->ci.dwCustom = CIFakeCustomDrawNotify(&plv->ci, CDDS_PREPAINT, &plvfd->nmcd.nmcd);
  602. }
  603. DWORD ListView_BeginFakeItemDraw(PLVFAKEDRAW plvfd)
  604. {
  605. LV *plv = plvfd->plv;
  606. LV_ITEM *pitem;
  607. // Early-out: If client doesn't use CustomDraw, then stop immediately.
  608. if (!(plv->ci.dwCustom & CDRF_NOTIFYITEMDRAW))
  609. return CDRF_DODEFAULT;
  610. pitem = plvfd->pitem;
  611. // Note that if the client says CDRF_SKIPDEFAULT (i.e., is owner-draw)
  612. // we measure the item anyway, because that's what IE4 did.
  613. // Make sure we do have the lParam. Office will fault if you give
  614. // bogus lParams during customdraw callbacks.
  615. plvfd->nmcd.nmcd.dwItemSpec = pitem->iItem;
  616. if (ListView_IsOwnerData(plv))
  617. {
  618. // OwnerData always gets lItemlParam = 0
  619. ASSERT(plvfd->nmcd.nmcd.lItemlParam == 0); // should still be 0
  620. } else {
  621. ASSERT(pitem->mask & LVIF_PARAM);
  622. plvfd->nmcd.nmcd.lItemlParam = pitem->lParam;
  623. }
  624. if (!(plv->ci.dwCustom & CDRF_SKIPDEFAULT)) {
  625. plvfd->nmcd.iSubItem = 0;
  626. plvfd->dwCustomItem = CIFakeCustomDrawNotify(&plv->ci, CDDS_ITEMPREPAINT, &plvfd->nmcd.nmcd);
  627. } else {
  628. plvfd->dwCustomItem = CDRF_DODEFAULT;
  629. }
  630. //
  631. // Only report view supports sub-items.
  632. //
  633. if (!ListView_IsReportView(plv))
  634. plvfd->dwCustomItem &= ~CDRF_NOTIFYSUBITEMDRAW;
  635. if (plvfd->dwCustomItem & CDRF_NOTIFYSUBITEMDRAW) {
  636. plvfd->nmcd.iSubItem = pitem->iSubItem;
  637. plvfd->dwCustomSubItem = CIFakeCustomDrawNotify(&plv->ci, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &plvfd->nmcd.nmcd);
  638. } else {
  639. plvfd->dwCustomSubItem = CDRF_DODEFAULT;
  640. }
  641. return plvfd->dwCustomItem | plvfd->dwCustomSubItem;
  642. }
  643. void ListView_EndFakeItemDraw(PLVFAKEDRAW plvfd)
  644. {
  645. LV *plv = plvfd->plv;
  646. // Early-out: If client doesn't use CustomDraw, then stop immediately.
  647. if (!(plv->ci.dwCustom & CDRF_NOTIFYITEMDRAW))
  648. return;
  649. if (!(plvfd->dwCustomSubItem & CDRF_SKIPDEFAULT) &&
  650. (plvfd->dwCustomSubItem & CDRF_NOTIFYPOSTPAINT)) {
  651. ASSERT(plvfd->dwCustomItem & CDRF_NOTIFYSUBITEMDRAW);
  652. ASSERT(plvfd->nmcd.iSubItem == plvfd->pitem->iSubItem);
  653. CIFakeCustomDrawNotify(&plv->ci, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &plvfd->nmcd.nmcd);
  654. }
  655. if ((plvfd->dwCustomItem | plvfd->dwCustomSubItem) & CDRF_NEWFONT) // App changed font, so
  656. SelectFont(plvfd->nmcd.nmcd.hdc, plv->hfontLabel); // restore default font
  657. if (!(plvfd->dwCustomItem & CDRF_SKIPDEFAULT) &&
  658. (plvfd->dwCustomItem & CDRF_NOTIFYPOSTPAINT)) {
  659. plvfd->nmcd.iSubItem = 0;
  660. CIFakeCustomDrawNotify(&plv->ci, CDDS_ITEMPOSTPAINT, &plvfd->nmcd.nmcd);
  661. }
  662. }
  663. void ListView_EndFakeCustomDraw(PLVFAKEDRAW plvfd)
  664. {
  665. LV *plv = plvfd->plv;
  666. // notify parent afterwards if they want us to
  667. if (!(plv->ci.dwCustom & CDRF_SKIPDEFAULT) &&
  668. plv->ci.dwCustom & CDRF_NOTIFYPOSTPAINT) {
  669. CIFakeCustomDrawNotify(&plv->ci, CDDS_POSTPAINT, &plvfd->nmcd.nmcd);
  670. }
  671. // Restore previous state
  672. plv->ci.dwCustom = plvfd->dwCustomPrev;
  673. SelectObject(plvfd->nmcd.nmcd.hdc, plvfd->hfontPrev);
  674. ReleaseDC(plv->ci.hwnd, plvfd->nmcd.nmcd.hdc);
  675. }
  676. BOOL NEAR PASCAL hasVertScroll
  677. (
  678. LV* plv
  679. )
  680. {
  681. RECT rcClient;
  682. RECT rcBounds;
  683. int cColVis;
  684. BOOL fHorSB;
  685. // Get the horizontal bounds of the items.
  686. ListView_GetClientRect(plv, &rcClient, FALSE, NULL);
  687. ListView_RGetRects(plv, 0, NULL, NULL, &rcBounds, NULL);
  688. fHorSB = (rcBounds.right - rcBounds.left > rcClient.right);
  689. cColVis = (rcClient.bottom - plv->yTop -
  690. (fHorSB ? ListView_GetCyScrollbar(plv) : 0)) / plv->cyItem;
  691. // check to see if we need a vert scrollbar
  692. if ((int)cColVis < ListView_Count(plv))
  693. return(TRUE);
  694. else
  695. return(FALSE);
  696. }
  697. BOOL NEAR ListView_RSetColumnWidth(LV* plv, int iCol, int cx)
  698. {
  699. HD_ITEM item;
  700. HD_ITEM colitem;
  701. SIZE siz;
  702. LV_ITEM lviItem;
  703. int i;
  704. int ItemWidth = 0;
  705. int HeaderWidth = 0;
  706. TCHAR szLabel[CCHLABELMAX + 4]; // CCHLABLEMAX == MAX_PATH
  707. int iBegin;
  708. int iEnd;
  709. // Should we compute the width based on the widest string?
  710. // If we do, include the Width of the Label, and if this is the
  711. // Last column, set the width so the right side is at the list view's right edge
  712. if (cx <= LVSCW_AUTOSIZE)
  713. {
  714. LVFAKEDRAW lvfd; // in case client uses customdraw
  715. if (cx == LVSCW_AUTOSIZE_USEHEADER)
  716. {
  717. // Special Cases:
  718. // 1) There is only 1 column. Set the width to the width of the listview
  719. // 2) This is the rightmost column, set the width so the right edge of the
  720. // column coinsides with to right edge of the list view.
  721. if (plv->cCol == 1)
  722. {
  723. RECT rcClient;
  724. ListView_GetClientRect(plv, &rcClient, FALSE, NULL);
  725. HeaderWidth = rcClient.right - rcClient.left;
  726. }
  727. else if (iCol == (plv->cCol-1))
  728. {
  729. // BUGBUG This will only work if the listview as NOT
  730. // been previously horizontally scrolled
  731. RECT rcClient;
  732. RECT rcHeader;
  733. ListView_GetClientRect(plv, &rcClient, FALSE, NULL);
  734. if (!Header_GetItemRect(plv->hwndHdr, plv->cCol - 2, &rcHeader))
  735. rcHeader.right = 0;
  736. // Is if visible
  737. if (rcHeader.right < (rcClient.right-rcClient.left))
  738. {
  739. HeaderWidth = (rcClient.right-rcClient.left) - rcHeader.right;
  740. }
  741. }
  742. // If we have a header width, then is is one of these special ones, so
  743. // we need to account for a vert scroll bar since we are using Client values
  744. if (HeaderWidth && hasVertScroll(plv))
  745. {
  746. HeaderWidth -= g_cxVScroll;
  747. }
  748. // Get the Width of the label.
  749. // We assume that the app hasn't changed any attributes
  750. // of the header control - still has default font, margins, etc.
  751. colitem.mask = HDI_TEXT | HDI_FORMAT;
  752. colitem.pszText = szLabel;
  753. colitem.cchTextMax = ARRAYSIZE(szLabel);
  754. if (Header_GetItem(plv->hwndHdr, iCol, &colitem))
  755. {
  756. HDC hdc = GetDC(plv->ci.hwnd);
  757. HFONT hfPrev = SelectFont(hdc, plv->hfontLabel);
  758. GetTextExtentPoint(hdc, colitem.pszText,
  759. lstrlen(colitem.pszText), &siz);
  760. siz.cx += 2 * (3 * g_cxLabelMargin); // phd->iTextMargin
  761. if (colitem.fmt & HDF_IMAGE)
  762. {
  763. siz.cx += plv->cxSmIcon;
  764. siz.cx += 2 * (3 * g_cxLabelMargin); // pdh->iBmMargin
  765. }
  766. HeaderWidth = max(HeaderWidth, siz.cx);
  767. SelectFont(hdc, hfPrev);
  768. ReleaseDC(plv->ci.hwnd, hdc);
  769. }
  770. }
  771. iBegin = 0;
  772. iEnd = ListView_Count( plv );
  773. //
  774. // Loop for each item in the view
  775. //
  776. if (ListView_IsOwnerData( plv ))
  777. {
  778. iBegin = (int)((plv->ptlRptOrigin.y - plv->yTop)
  779. / plv->cyItem);
  780. iEnd = (int)((plv->ptlRptOrigin.y + plv->sizeClient.cy - plv->yTop)
  781. / plv->cyItem) + 1;
  782. iBegin = max( 0, iBegin );
  783. iEnd = max(iEnd, iBegin + 1);
  784. iEnd = min( iEnd, ListView_Count( plv ) );
  785. ListView_NotifyCacheHint( plv, iBegin, iEnd-1 );
  786. }
  787. //
  788. // To obtain the widths of the strings, we have to pretend that
  789. // we are painting them, in case the custom-draw client wants
  790. // to play with fonts (e.g., Athena).
  791. //
  792. ListView_BeginFakeCustomDraw(plv, &lvfd, &lviItem);
  793. //
  794. // If column 0, then we also need to take indent into account.
  795. //
  796. lviItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  797. if (iCol == 0) {
  798. lviItem.mask |= LVIF_INDENT;
  799. }
  800. // Loop for each item in the List
  801. for (i = iBegin; i < iEnd; i++)
  802. {
  803. lviItem.iImage = -1;
  804. lviItem.iItem = i;
  805. lviItem.iSubItem = iCol;
  806. lviItem.pszText = szLabel;
  807. lviItem.cchTextMax = ARRAYSIZE(szLabel);
  808. lviItem.iIndent = 0;
  809. lviItem.stateMask = 0;
  810. ListView_OnGetItem(plv, &lviItem);
  811. // If there is a Text item, get its width
  812. if (lviItem.pszText || (lviItem.iImage != -1))
  813. {
  814. if (lviItem.pszText) {
  815. ListView_BeginFakeItemDraw(&lvfd);
  816. GetTextExtentPoint(lvfd.nmcd.nmcd.hdc, lviItem.pszText,
  817. lstrlen(lviItem.pszText), &siz);
  818. ListView_EndFakeItemDraw(&lvfd);
  819. } else {
  820. siz.cx = 0;
  821. }
  822. if (lviItem.iImage != -1)
  823. siz.cx += plv->cxSmIcon + g_cxEdge;
  824. siz.cx += lviItem.iIndent * plv->cxSmIcon;
  825. ItemWidth = max(ItemWidth, siz.cx);
  826. }
  827. }
  828. ListView_EndFakeCustomDraw(&lvfd);
  829. // Adjust by a reasonable border amount.
  830. // If col 0, add 2*g_cxLabelMargin + g_szSmIcon.
  831. // Otherwise add 6*g_cxLabelMargin.
  832. // These amounts are based on Margins added automatically
  833. // to the ListView in ShDrawText.
  834. // BUGBUG ListView Report format currently assumes and makes
  835. // room for a Small Icon.
  836. if (iCol == 0)
  837. {
  838. ItemWidth += plv->cxState + g_cxEdge;
  839. ItemWidth += 2*g_cxLabelMargin;
  840. }
  841. else
  842. {
  843. ItemWidth += 6*g_cxLabelMargin;
  844. }
  845. TraceMsg(TF_LISTVIEW, "ListView: HeaderWidth:%d ItemWidth:%d", HeaderWidth, ItemWidth);
  846. item.cxy = max(HeaderWidth, ItemWidth);
  847. }
  848. else
  849. {
  850. // Use supplied width
  851. item.cxy = cx;
  852. }
  853. plv->xTotalColumnWidth = RECOMPUTE;
  854. item.mask = HDI_WIDTH;
  855. return Header_SetItem(plv->hwndHdr, iCol, &item);
  856. }
  857. BOOL NEAR ListView_OnGetColumnA(LV* plv, int iCol, LV_COLUMNA FAR* pcol) {
  858. LPWSTR pszW = NULL;
  859. LPSTR pszC = NULL;
  860. BOOL fRet;
  861. //HACK ALERT -- this code assumes that LV_COLUMNA is exactly the same
  862. // as LV_COLUMNW except for the pointer to the string.
  863. ASSERT(sizeof(LV_COLUMNA) == sizeof(LV_COLUMNW))
  864. if (!pcol) return FALSE;
  865. if ((pcol->mask & LVCF_TEXT) && (pcol->pszText != NULL)) {
  866. pszC = pcol->pszText;
  867. pszW = LocalAlloc(LMEM_FIXED, pcol->cchTextMax * sizeof(WCHAR));
  868. if (pszW == NULL)
  869. return FALSE;
  870. pcol->pszText = (LPSTR)pszW;
  871. }
  872. fRet = ListView_OnGetColumn(plv, iCol, (LV_COLUMN FAR*) pcol);
  873. if (pszW != NULL) {
  874. if (fRet && pcol->cchTextMax)
  875. ConvertWToAN(plv->ci.uiCodePage, pszC, pcol->cchTextMax, pszW, -1);
  876. pcol->pszText = pszC;
  877. LocalFree(pszW);
  878. }
  879. return fRet;
  880. }
  881. BOOL NEAR ListView_OnGetColumn(LV* plv, int iCol, LV_COLUMN FAR* pcol)
  882. {
  883. HD_ITEM item;
  884. UINT mask;
  885. if (!pcol) {
  886. RIPMSG(0, "LVM_GETCOLUMN: Invalid pcol = NULL");
  887. return FALSE;
  888. }
  889. mask = pcol->mask;
  890. if (!mask)
  891. return TRUE;
  892. item.mask = HDI_FORMAT | HDI_WIDTH | HDI_LPARAM | HDI_ORDER | HDI_IMAGE;
  893. if (mask & LVCF_TEXT)
  894. {
  895. if (pcol->pszText)
  896. {
  897. item.mask |= HDI_TEXT;
  898. item.pszText = pcol->pszText;
  899. item.cchTextMax = pcol->cchTextMax;
  900. } else {
  901. // For compatibility reasons, we don't fail the call if they
  902. // pass NULL.
  903. RIPMSG(0, "LVM_GETCOLUMN: Invalid pcol->pszText = NULL");
  904. }
  905. }
  906. if (!Header_GetItem(plv->hwndHdr, iCol, &item))
  907. {
  908. RIPMSG(0, "LVM_GETCOLUMN: Invalid column number %d", iCol);
  909. return FALSE;
  910. }
  911. if (mask & LVCF_SUBITEM)
  912. pcol->iSubItem = (int)item.lParam;
  913. if (mask & LVCF_ORDER)
  914. pcol->iOrder = (int)item.iOrder;
  915. if (mask & LVCF_IMAGE)
  916. pcol->iImage = item.iImage;
  917. if (mask & LVCF_FMT)
  918. pcol->fmt = item.fmt;
  919. if (mask & LVCF_WIDTH)
  920. pcol->cx = item.cxy;
  921. return TRUE;
  922. }
  923. BOOL NEAR ListView_OnSetColumnA(LV* plv, int iCol, LV_COLUMNA FAR* pcol) {
  924. LPWSTR pszW = NULL;
  925. LPSTR pszC = NULL;
  926. BOOL fRet;
  927. //HACK ALERT -- this code assumes that LV_COLUMNA is exactly the same
  928. // as LV_COLUMNW except for the pointer to the string.
  929. ASSERT(sizeof(LV_COLUMNA) == sizeof(LV_COLUMNW));
  930. if (!pcol) return FALSE;
  931. if ((pcol->mask & LVCF_TEXT) && (pcol->pszText != NULL)) {
  932. pszC = pcol->pszText;
  933. if ((pszW = ProduceWFromA(plv->ci.uiCodePage, pszC)) == NULL)
  934. return FALSE;
  935. pcol->pszText = (LPSTR)pszW;
  936. }
  937. fRet = ListView_OnSetColumn(plv, iCol, (const LV_COLUMN FAR*) pcol);
  938. if (pszW != NULL) {
  939. pcol->pszText = pszC;
  940. FreeProducedString(pszW);
  941. }
  942. return fRet;
  943. }
  944. BOOL NEAR ListView_OnSetColumn(LV* plv, int iCol, const LV_COLUMN FAR* pcol)
  945. {
  946. HD_ITEM item;
  947. UINT mask;
  948. if (!pcol) return FALSE;
  949. mask = pcol->mask;
  950. if (!mask)
  951. return TRUE;
  952. item.mask = 0;
  953. if (mask & LVCF_SUBITEM)
  954. {
  955. item.mask |= HDI_LPARAM;
  956. item.lParam = iCol;
  957. }
  958. if (mask & LVCF_FMT)
  959. {
  960. item.mask |= HDI_FORMAT;
  961. item.fmt = (pcol->fmt | HDF_STRING);
  962. }
  963. if (mask & LVCF_WIDTH)
  964. {
  965. item.mask |= HDI_WIDTH;
  966. item.cxy = pcol->cx;
  967. }
  968. if (mask & LVCF_TEXT)
  969. {
  970. RIPMSG(pcol->pszText != NULL, "LVM_SETCOLUMN: LV_COLUMN.pszText should not be NULL");
  971. item.mask |= HDI_TEXT;
  972. item.pszText = pcol->pszText;
  973. item.cchTextMax = 0;
  974. }
  975. if (mask & LVCF_IMAGE)
  976. {
  977. item.mask |= HDI_IMAGE;
  978. item.iImage = pcol->iImage;
  979. }
  980. if (mask & LVCF_ORDER)
  981. {
  982. item.mask |= HDI_ORDER;
  983. item.iOrder = pcol->iOrder;
  984. }
  985. plv->xTotalColumnWidth = RECOMPUTE;
  986. return Header_SetItem(plv->hwndHdr, iCol, &item);
  987. }
  988. BOOL NEAR ListView_SetSubItem(LV* plv, const LV_ITEM FAR* plvi)
  989. {
  990. LISTSUBITEM lsi;
  991. BOOL fChanged = FALSE;
  992. int i;
  993. int idpa;
  994. HDPA hdpa;
  995. if (plvi->mask & ~(LVIF_DI_SETITEM | LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))
  996. {
  997. RIPMSG(0, "ListView: Invalid mask: %04x", plvi->mask);
  998. return FALSE;
  999. }
  1000. if (!(plvi->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE)))
  1001. return TRUE;
  1002. i = plvi->iItem;
  1003. if (!ListView_IsValidItemNumber(plv, i))
  1004. {
  1005. RIPMSG(0, "LVM_SETITEM: Invalid iItem: %d", plvi->iItem);
  1006. return FALSE;
  1007. }
  1008. // sub item indices are 1-based...
  1009. //
  1010. idpa = plvi->iSubItem - 1;
  1011. if (idpa < 0 || idpa >= plv->cCol - 1)
  1012. {
  1013. RIPMSG(0, "LVM_SETITEM: Invalid iSubItem: %d", plvi->iSubItem);
  1014. return FALSE;
  1015. }
  1016. hdpa = ListView_GetSubItemDPA(plv, idpa);
  1017. if (!hdpa)
  1018. {
  1019. hdpa = DPA_CreateEx(LV_HDPA_GROW, plv->hheap);
  1020. if (!hdpa)
  1021. return FALSE;
  1022. DPA_SetPtr(plv->hdpaSubItems, idpa, (void FAR*)hdpa);
  1023. }
  1024. ListView_GetSubItem(plv, i, plvi->iSubItem, &lsi);
  1025. if (plvi->mask & LVIF_TEXT) {
  1026. if (lsi.pszText != plvi->pszText) {
  1027. Str_Set(&lsi.pszText, plvi->pszText);
  1028. fChanged = TRUE;
  1029. }
  1030. }
  1031. if (plvi->mask & LVIF_IMAGE) {
  1032. if (plvi->iImage != lsi.iImage) {
  1033. lsi.iImage = (short) plvi->iImage;
  1034. fChanged = TRUE;
  1035. }
  1036. }
  1037. if (plvi->mask & LVIF_STATE) {
  1038. DWORD dwChange;
  1039. dwChange = (lsi.state ^ plvi->state ) & plvi->stateMask;
  1040. if (dwChange) {
  1041. lsi.state ^= dwChange;
  1042. fChanged = TRUE;
  1043. }
  1044. }
  1045. if (fChanged) {
  1046. PLISTSUBITEM plsiReal = DPA_GetPtr(hdpa, i);
  1047. if (!plsiReal) {
  1048. plsiReal = LocalAlloc(LPTR, sizeof(LISTSUBITEM));
  1049. if (!plsiReal) {
  1050. // fail! bail out
  1051. return FALSE;
  1052. }
  1053. }
  1054. *plsiReal = lsi;
  1055. if (!DPA_SetPtr(hdpa, i, (void FAR*)plsiReal)) {
  1056. ListView_FreeSubItem(plsiReal);
  1057. return FALSE;
  1058. }
  1059. }
  1060. // all's well... let's invalidate this
  1061. if (ListView_IsReportView(plv)) {
  1062. RECT rc;
  1063. ListView_RGetRectsEx(plv, plvi->iItem, plvi->iSubItem, NULL, &rc);
  1064. RedrawWindow(plv->ci.hwnd, &rc, NULL, RDW_ERASE | RDW_INVALIDATE);
  1065. }
  1066. return TRUE;
  1067. }
  1068. int ListView_RDestroyColumn(LPVOID d, LPVOID p)
  1069. {
  1070. HDPA hdpa = (HDPA)d;
  1071. DPA_DestroyCallback(hdpa, ListView_FreeColumnData, 0);
  1072. return 1;
  1073. }
  1074. void NEAR ListView_RDestroy(LV* plv)
  1075. {
  1076. DPA_DestroyCallback(plv->hdpaSubItems, ListView_RDestroyColumn, 0);
  1077. plv->hdpaSubItems = NULL;
  1078. }
  1079. VOID NEAR ListView_RHeaderTrack(LV* plv, HD_NOTIFY FAR * pnm)
  1080. {
  1081. // We want to update to show where the column header will be.
  1082. HDC hdc;
  1083. RECT rcBounds;
  1084. // Statics needed from call to call
  1085. static int s_xLast = -32767;
  1086. hdc = GetDC(plv->ci.hwnd);
  1087. if (hdc == NULL)
  1088. return;
  1089. //
  1090. // First undraw the last marker we drew.
  1091. //
  1092. if (s_xLast > 0)
  1093. {
  1094. PatBlt(hdc, s_xLast, plv->yTop, g_cxBorder, plv->sizeClient.cy - plv->yTop, PATINVERT);
  1095. }
  1096. if (pnm->hdr.code == HDN_ENDTRACK)
  1097. {
  1098. s_xLast = -32767; // Some large negative number...
  1099. }
  1100. else
  1101. {
  1102. RECT rc;
  1103. //
  1104. // First we need to calculate the X location of the column
  1105. // To do this, we will need to know where this column begins
  1106. // Note: We need the bounding rects to help us know the origin.
  1107. ListView_GetRects(plv, 0, NULL, NULL, &rcBounds, NULL);
  1108. if (!Header_GetItemRect(plv->hwndHdr, pnm->iItem, &rc)) {
  1109. rc.left = 0;
  1110. }
  1111. rcBounds.left += rc.left;
  1112. // Draw the new line...
  1113. s_xLast = rcBounds.left + pnm->pitem->cxy;
  1114. PatBlt(hdc, s_xLast, plv->yTop, g_cxBorder, plv->sizeClient.cy - plv->yTop, PATINVERT);
  1115. }
  1116. ReleaseDC(plv->ci.hwnd, hdc);
  1117. }
  1118. // try to use scrollwindow to adjust the columns rather than erasing
  1119. // and redrawing.
  1120. void NEAR PASCAL ListView_AdjustColumn(LV * plv, int iWidth)
  1121. {
  1122. int x;
  1123. RECT rcClip;
  1124. int dx = iWidth - plv->iSelOldWidth;
  1125. if (iWidth == plv->iSelOldWidth)
  1126. return;
  1127. // find the x coord of the left side of the iCol
  1128. // use rcClip as a temporary...
  1129. if (!Header_GetItemRect(plv->hwndHdr, plv->iSelCol, &rcClip)) {
  1130. x = 0;
  1131. } else {
  1132. x = rcClip.left;
  1133. }
  1134. x -= plv->ptlRptOrigin.x;
  1135. // compute the area to the right of the adjusted column
  1136. GetWindowRect(plv->hwndHdr, &rcClip);
  1137. rcClip.left = x;
  1138. rcClip.top = RECTHEIGHT(rcClip);
  1139. rcClip.right = plv->sizeClient.cx;
  1140. rcClip.bottom = plv->sizeClient.cy;
  1141. if ((plv->pImgCtx == NULL) && (plv->clrBk != CLR_NONE) &&
  1142. (plv->clrTextBk != CLR_NONE))
  1143. {
  1144. //
  1145. // We have a solid color background,
  1146. // so we can smooth scroll the right side columns.
  1147. //
  1148. SMOOTHSCROLLINFO si =
  1149. {
  1150. sizeof(si),
  1151. 0,
  1152. plv->ci.hwnd,
  1153. dx,
  1154. 0,
  1155. NULL,
  1156. &rcClip,
  1157. NULL,
  1158. NULL,
  1159. SW_ERASE | SW_INVALIDATE,
  1160. };
  1161. rcClip.left += min(plv->iSelOldWidth, iWidth);
  1162. SmoothScrollWindow(&si);
  1163. // if we shrunk, invalidate the right most edge because
  1164. // there might be junk there
  1165. if (iWidth < plv->iSelOldWidth) {
  1166. rcClip.right = rcClip.left + g_cxEdge;
  1167. InvalidateRect(plv->ci.hwnd, &rcClip, TRUE);
  1168. }
  1169. plv->xTotalColumnWidth = RECOMPUTE;
  1170. // adjust clipping rect to only redraw the adjusted column
  1171. rcClip.left = x;
  1172. rcClip.right = max(rcClip.left, x+iWidth);
  1173. // Make the rectangle origin-based because ListView_UpdateScrollBars
  1174. // may scroll us around.
  1175. OffsetRect(&rcClip, plv->ptlRptOrigin.x, plv->ptlRptOrigin.y);
  1176. ListView_UpdateScrollBars(plv);
  1177. // Okay, now convert it back to client coordinates
  1178. OffsetRect(&rcClip, -plv->ptlRptOrigin.x, -plv->ptlRptOrigin.y);
  1179. // call update because scrollwindowex might have erased the far right
  1180. // we don't want this invalidate to then enlarge the region
  1181. // and end up erasing everything.
  1182. UpdateWindow(plv->ci.hwnd);
  1183. RedrawWindow(plv->ci.hwnd, &rcClip, NULL,
  1184. RDW_INVALIDATE | RDW_UPDATENOW);
  1185. }
  1186. else
  1187. {
  1188. //
  1189. // We don't have a solid color background,
  1190. // erase and redraw the adjusted column and
  1191. // everything to the right (sigh).
  1192. //
  1193. plv->xTotalColumnWidth = RECOMPUTE;
  1194. ListView_UpdateScrollBars(plv);
  1195. rcClip.left = x;
  1196. RedrawWindow(plv->ci.hwnd, &rcClip, NULL,
  1197. RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
  1198. }
  1199. }
  1200. BOOL ListView_ForwardHeaderNotify(LV* plv, HD_NOTIFY FAR *pnm)
  1201. {
  1202. return BOOLFROMPTR(SendNotifyEx(plv->ci.hwndParent, pnm->hdr.hwndFrom, pnm->hdr.code,
  1203. (NMHDR FAR *)pnm, plv->ci.bUnicode));
  1204. }
  1205. LRESULT ListView_HeaderNotify(LV* plv, HD_NOTIFY *pnm)
  1206. {
  1207. LRESULT lres = 0;
  1208. switch (pnm->hdr.code)
  1209. {
  1210. case HDN_BEGINDRAG:
  1211. if (!(plv->exStyle & LVS_EX_HEADERDRAGDROP))
  1212. return TRUE;
  1213. return ListView_ForwardHeaderNotify(plv, pnm);
  1214. case HDN_ENDDRAG:
  1215. if (pnm->pitem->iOrder != -1) {
  1216. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  1217. return ListView_ForwardHeaderNotify(plv, pnm);
  1218. }
  1219. goto DoDefault;
  1220. case HDN_ITEMCHANGING:
  1221. if (pnm->pitem->mask & HDI_WIDTH) {
  1222. HD_ITEM hitem;
  1223. hitem.mask = HDI_WIDTH;
  1224. Header_GetItem(plv->hwndHdr, pnm->iItem, &hitem);
  1225. plv->iSelCol = pnm->iItem;
  1226. plv->iSelOldWidth = hitem.cxy;
  1227. TraceMsg(TF_LISTVIEW, "HDN_ITEMCHANGING %d %d", hitem.cxy, pnm->pitem->cxy);
  1228. return ListView_ForwardHeaderNotify(plv, pnm);
  1229. }
  1230. else if (pnm->pitem->mask & HDI_FILTER) {
  1231. return ListView_ForwardHeaderNotify(plv, pnm);
  1232. }
  1233. goto DoDefault;
  1234. case HDN_ITEMCHANGED:
  1235. if (pnm->pitem->mask & HDI_WIDTH)
  1236. {
  1237. ListView_DismissEdit(plv, FALSE);
  1238. if (pnm->iItem == plv->iSelCol) {
  1239. // Must do this even if there are no items, since
  1240. // we have to redo the scrollbar, and the client
  1241. // may have custom-drawn gridlines or something.
  1242. ListView_AdjustColumn(plv, pnm->pitem->cxy);
  1243. } else {
  1244. // sanity check. we got confused, so redraw all
  1245. RedrawWindow(plv->ci.hwnd, NULL, NULL,
  1246. RDW_ERASE | RDW_INVALIDATE);
  1247. }
  1248. plv->iSelCol = -1;
  1249. lres = ListView_ForwardHeaderNotify(plv, pnm);
  1250. }
  1251. else if (pnm->pitem->mask & HDI_FILTER) {
  1252. lres = ListView_ForwardHeaderNotify(plv, pnm);
  1253. } else
  1254. goto DoDefault;
  1255. break;
  1256. case HDN_ITEMCLICK:
  1257. //
  1258. // BUGBUG:: Need to pass this and other HDN_ notifications back to
  1259. // parent. Should we simply pass up the HDN notifications
  1260. // or should we define equivlent LVN_ notifications...
  1261. //
  1262. // Pass column number in iSubItem, not iItem...
  1263. //
  1264. ListView_DismissEdit(plv, FALSE);
  1265. ListView_Notify(plv, -1, pnm->iItem, LVN_COLUMNCLICK);
  1266. lres = ListView_ForwardHeaderNotify(plv, pnm);
  1267. SetFocus(plv->ci.hwnd);
  1268. break;
  1269. case HDN_TRACK:
  1270. case HDN_ENDTRACK:
  1271. ListView_DismissEdit(plv, FALSE);
  1272. ListView_RHeaderTrack(plv, pnm);
  1273. lres = ListView_ForwardHeaderNotify(plv, pnm);
  1274. SetFocus(plv->ci.hwnd);
  1275. break;
  1276. case HDN_DIVIDERDBLCLICK:
  1277. ListView_DismissEdit(plv, FALSE);
  1278. ListView_RSetColumnWidth(plv, pnm->iItem, -1);
  1279. lres = ListView_ForwardHeaderNotify(plv, pnm);
  1280. SetFocus(plv->ci.hwnd);
  1281. break;
  1282. case HDN_FILTERCHANGE:
  1283. case HDN_FILTERBTNCLICK:
  1284. return ListView_ForwardHeaderNotify(plv, pnm);
  1285. case NM_RCLICK:
  1286. return (UINT)SendNotifyEx(plv->ci.hwndParent, plv->hwndHdr, NM_RCLICK, NULL, plv->ci.bUnicode);
  1287. default:
  1288. DoDefault:
  1289. if (plv->ci.iVersion >= 5)
  1290. return ListView_ForwardHeaderNotify(plv, pnm);
  1291. break;
  1292. }
  1293. // in v < 5 we always returned 0
  1294. // but for newer clients we'd like to have them deal with the notify
  1295. if (plv->ci.iVersion >= 5)
  1296. return lres;
  1297. return 0;
  1298. }
  1299. /*----------------------------------------------------------------
  1300. ** Check for a hit in a report view.
  1301. **
  1302. ** a hit only counts if it's on the icon or the string in the first
  1303. ** column. so we gotta figure out what this means exactly. yuck.
  1304. **
  1305. ** BONUS FEATURE: If piSubItem is non-NULL, then we also hit-test
  1306. ** against subitems. But if we find nothing, we return iSubItem = 0
  1307. ** for compatibility with the other hit-test functions.
  1308. **----------------------------------------------------------------*/
  1309. int NEAR ListView_RItemHitTest(LV* plv, int x, int y, UINT FAR* pflags, int *piSubItem)
  1310. {
  1311. int iHit;
  1312. int i, iSub;
  1313. UINT flags;
  1314. RECT rcLabel;
  1315. RECT rcIcon;
  1316. if (piSubItem)
  1317. *piSubItem = 0;
  1318. flags = LVHT_NOWHERE;
  1319. iHit = -1;
  1320. i = ListView_RYHitTest(plv, y);
  1321. if (ListView_IsValidItemNumber(plv, i))
  1322. {
  1323. if (plv->ci.style & LVS_OWNERDRAWFIXED) {
  1324. flags = LVHT_ONITEM;
  1325. iHit = i;
  1326. } else {
  1327. RECT rcSelect;
  1328. ListView_GetRects(plv, i, &rcIcon, &rcLabel, NULL, &rcSelect);
  1329. // is the hit in the first column?
  1330. if ((x < rcIcon.left - g_cxEdge) && x > (rcIcon.left - plv->cxState))
  1331. {
  1332. iHit = i;
  1333. flags = LVHT_ONITEMSTATEICON;
  1334. }
  1335. else if ((x >= rcIcon.left) && (x < rcIcon.right))
  1336. {
  1337. iHit = i;
  1338. flags = LVHT_ONITEMICON;
  1339. }
  1340. else if (x >= rcLabel.left && (x < rcSelect.right))
  1341. {
  1342. iHit = i;
  1343. flags = LVHT_ONITEMLABEL;
  1344. if (ListView_FullRowSelect(plv)) {
  1345. // this is kinda funky... in full row select mode
  1346. // we're only really on the label if x is <= rcLabel.left + cxLabel
  1347. // because GetRects returns a label rect of the full column width
  1348. // and rcSelect has the full row in FullRowSelect mode
  1349. // (it has the label only width in non-fullrow select mode.
  1350. //
  1351. // go figure..
  1352. //
  1353. int cxLabel;
  1354. LISTITEM FAR* pitem = NULL;
  1355. if (!ListView_IsOwnerData( plv ))
  1356. {
  1357. pitem = ListView_FastGetItemPtr(plv, i);
  1358. }
  1359. cxLabel = ListView_RGetCXLabel(plv, i, pitem, NULL, FALSE);
  1360. if (x >= min(rcLabel.left + cxLabel, rcLabel.right)) {
  1361. if (!piSubItem)
  1362. flags = LVHT_ONITEM;
  1363. else
  1364. goto CheckSubItem;
  1365. }
  1366. }
  1367. } else if (x < rcSelect.right && ListView_FullRowSelect(plv)) {
  1368. // we can fall into this case if columns have been re-ordered
  1369. iHit = i;
  1370. flags = LVHT_ONITEM;
  1371. } else if (piSubItem) {
  1372. CheckSubItem:
  1373. iSub = ListView_RXHitTest(plv, x);
  1374. if (iSub >= 0) {
  1375. iHit = i;
  1376. *piSubItem = iSub;
  1377. // Flags still say LVHT_NOWHERE
  1378. }
  1379. }
  1380. }
  1381. }
  1382. *pflags = flags;
  1383. return iHit;
  1384. }
  1385. void ListView_GetSubItem(LV* plv, int i, int iSubItem, PLISTSUBITEM plsi)
  1386. {
  1387. HDPA hdpa;
  1388. PLISTSUBITEM plsiSrc = NULL;
  1389. ASSERT( !ListView_IsOwnerData( plv ));
  1390. // Sub items are indexed starting at 1...
  1391. //
  1392. RIPMSG(iSubItem > 0 && iSubItem < plv->cCol, "ListView: Invalid iSubItem: %d", iSubItem);
  1393. #ifdef DEBUG
  1394. // Avoid the assert in DPA_GetPtr if somebdy tries to get a subitem
  1395. // when no columns have been added. We already RIP'd above.
  1396. hdpa = plv->cCol ? ListView_GetSubItemDPA(plv, iSubItem - 1) : NULL;
  1397. #else
  1398. hdpa = ListView_GetSubItemDPA(plv, iSubItem - 1);
  1399. #endif
  1400. if (hdpa) {
  1401. plsiSrc = DPA_GetPtr(hdpa, i);
  1402. }
  1403. if (plsiSrc) {
  1404. *plsi = *plsiSrc;
  1405. } else {
  1406. // item data exists.. give defaults
  1407. plsi->pszText = LPSTR_TEXTCALLBACK;
  1408. plsi->iImage = I_IMAGECALLBACK;
  1409. plsi->state = 0;
  1410. }
  1411. }
  1412. // this will return the rect of a subitem as requested.
  1413. void ListView_RGetRectsEx(LV* plv, int iItem, int iSubItem, LPRECT prcIcon, LPRECT prcLabel)
  1414. {
  1415. int x;
  1416. int y;
  1417. LONG ly;
  1418. RECT rcLabel;
  1419. RECT rcIcon;
  1420. RECT rcHeader;
  1421. if (iSubItem == 0) {
  1422. ListView_RGetRects(plv, iItem, prcIcon, prcLabel, NULL, NULL);
  1423. return;
  1424. }
  1425. // otherwise it's just the header's column right and left and the item's height
  1426. ly = (LONG)iItem * plv->cyItem - plv->ptlRptOrigin.y + plv->yTop;
  1427. x = - (int)plv->ptlRptOrigin.x;
  1428. //
  1429. // Need to check for y overflow into rectangle structure
  1430. // if so we need to return something reasonable...
  1431. // For now will simply set it to the max or min that will fit...
  1432. //
  1433. if (ly >= (INT_MAX - plv->cyItem))
  1434. y = INT_MAX - plv->cyItem;
  1435. else if ( ly < INT_MIN)
  1436. y = INT_MIN;
  1437. else
  1438. y = (int)ly;
  1439. ASSERT(iSubItem < plv->cCol);
  1440. Header_GetItemRect(plv->hwndHdr, iSubItem, &rcHeader);
  1441. rcLabel.left = x + rcHeader.left;
  1442. rcLabel.right = x + rcHeader.right;
  1443. rcLabel.top = y;
  1444. rcLabel.bottom = rcLabel.top + plv->cyItem;
  1445. rcIcon = rcLabel;
  1446. rcIcon.right = rcIcon.left + plv->cxSmIcon;
  1447. if (SELECTOROF(prcIcon))
  1448. *prcIcon = rcIcon;
  1449. if (SELECTOROF(prcLabel))
  1450. *prcLabel = rcLabel;
  1451. }
  1452. int ListView_RGetTotalColumnWidth(LV* plv)
  1453. {
  1454. if (plv->xTotalColumnWidth == RECOMPUTE)
  1455. {
  1456. plv->xTotalColumnWidth = 0;
  1457. if (plv->cCol) {
  1458. RECT rcLabel;
  1459. int iIndex;
  1460. // find the right edge of the last ordered item to get the total column width
  1461. iIndex = (int) SendMessage(plv->hwndHdr, HDM_ORDERTOINDEX, plv->cCol - 1, 0);
  1462. Header_GetItemRect(plv->hwndHdr, iIndex, &rcLabel);
  1463. plv->xTotalColumnWidth = rcLabel.right;
  1464. }
  1465. }
  1466. return plv->xTotalColumnWidth;
  1467. }
  1468. // get the rects for report view
  1469. void NEAR ListView_RGetRects(LV* plv, int iItem, RECT FAR* prcIcon,
  1470. RECT FAR* prcLabel, RECT FAR* prcBounds, RECT FAR* prcSelectBounds)
  1471. {
  1472. RECT rcIcon;
  1473. RECT rcLabel;
  1474. int x;
  1475. int y;
  1476. LONG ly;
  1477. LVITEM lvitem;
  1478. BOOL fItemSpecific = (prcIcon || prcLabel || prcSelectBounds);
  1479. // use long math for cases where we have lots-o-items
  1480. ly = (LONG)iItem * plv->cyItem - plv->ptlRptOrigin.y + plv->yTop;
  1481. x = - (int)plv->ptlRptOrigin.x;
  1482. //
  1483. // Need to check for y overflow into rectangle structure
  1484. // if so we need to return something reasonable...
  1485. // For now will simply set it to the max or min that will fit...
  1486. //
  1487. if (ly >= (INT_MAX - plv->cyItem))
  1488. y = INT_MAX - plv->cyItem;
  1489. else
  1490. y = (int)ly;
  1491. if (ListView_Count(plv) && fItemSpecific) {
  1492. // move this over by the indent level as well
  1493. lvitem.mask = LVIF_INDENT;
  1494. lvitem.iItem = iItem;
  1495. lvitem.iSubItem = 0;
  1496. ListView_OnGetItem(plv, &lvitem);
  1497. } else {
  1498. lvitem.iIndent = 0;
  1499. }
  1500. rcIcon.left = x + plv->cxState + (lvitem.iIndent * plv->cxSmIcon) + g_cxEdge;
  1501. rcIcon.right = rcIcon.left + plv->cxSmIcon;
  1502. rcIcon.top = y;
  1503. rcIcon.bottom = rcIcon.top + plv->cyItem;
  1504. rcLabel.left = rcIcon.right;
  1505. rcLabel.top = rcIcon.top;
  1506. rcLabel.bottom = rcIcon.bottom;
  1507. //
  1508. // The label is assumed to be the first column.
  1509. //
  1510. rcLabel.right = x;
  1511. if (plv->cCol > 0 && fItemSpecific)
  1512. {
  1513. RECT rc;
  1514. Header_GetItemRect(plv->hwndHdr, 0, &rc);
  1515. rcLabel.right = x + rc.right;
  1516. rcLabel.left += rc.left;
  1517. rcIcon.left += rc.left;
  1518. rcIcon.right += rc.left;
  1519. }
  1520. if (SELECTOROF(prcIcon))
  1521. *prcIcon = rcIcon;
  1522. // Save away the label bounds.
  1523. if (SELECTOROF(prcLabel)) {
  1524. *prcLabel = rcLabel;
  1525. }
  1526. // See if they also want the Selection bounds of the item
  1527. if (prcSelectBounds)
  1528. {
  1529. if (ListView_FullRowSelect(plv)) {
  1530. prcSelectBounds->left = x;
  1531. prcSelectBounds->top = y;
  1532. prcSelectBounds->bottom = rcLabel.bottom;
  1533. prcSelectBounds->right = prcSelectBounds->left + ListView_RGetTotalColumnWidth(plv);
  1534. } else {
  1535. int cxLabel;
  1536. LISTITEM FAR* pitem = NULL;
  1537. if (!ListView_IsOwnerData( plv ))
  1538. {
  1539. pitem = ListView_FastGetItemPtr(plv, iItem);
  1540. }
  1541. cxLabel = ListView_RGetCXLabel(plv, iItem, pitem, NULL, FALSE);
  1542. *prcSelectBounds = rcIcon;
  1543. prcSelectBounds->right = rcLabel.left + cxLabel;
  1544. if (prcSelectBounds->right > rcLabel.right)
  1545. prcSelectBounds->right = rcLabel.right;
  1546. }
  1547. }
  1548. // And also the Total bounds
  1549. //
  1550. // and now for the complete bounds...
  1551. //
  1552. if (SELECTOROF(prcBounds))
  1553. {
  1554. prcBounds->left = x;
  1555. prcBounds->top = y;
  1556. prcBounds->bottom = rcLabel.bottom;
  1557. prcBounds->right = prcBounds->left + ListView_RGetTotalColumnWidth(plv);
  1558. }
  1559. }
  1560. BOOL ListView_OnGetSubItemRect(LV* plv, int iItem, LPRECT lprc)
  1561. {
  1562. LPRECT pRects[LVIR_MAX];
  1563. RECT rcTemp;
  1564. int iSubItem;
  1565. int iCode;
  1566. if (!lprc)
  1567. return FALSE;
  1568. iSubItem = lprc->top;
  1569. iCode = lprc->left;
  1570. if (iSubItem == 0) {
  1571. return ListView_OnGetItemRect(plv, iItem, lprc);
  1572. }
  1573. if (!ListView_IsReportView(plv) ||
  1574. (iCode != LVIR_BOUNDS && iCode != LVIR_ICON && iCode != LVIR_LABEL)) {
  1575. return FALSE;
  1576. }
  1577. pRects[0] = NULL;
  1578. pRects[1] = &rcTemp; // LVIR_ICON
  1579. pRects[2] = &rcTemp; // LVIR_LABEL
  1580. pRects[3] = NULL;
  1581. if (iCode != LVIR_BOUNDS) {
  1582. pRects[iCode] = lprc;
  1583. } else {
  1584. // choose either
  1585. pRects[LVIR_ICON] = lprc;
  1586. }
  1587. ListView_RGetRectsEx(plv, iItem, iSubItem,
  1588. pRects[LVIR_ICON], pRects[LVIR_LABEL]);
  1589. if (iCode == LVIR_BOUNDS) {
  1590. UnionRect(lprc, lprc, &rcTemp);
  1591. }
  1592. return TRUE;
  1593. }
  1594. int ListView_RXHitTest(LV* plv, int x)
  1595. {
  1596. int iSubItem;
  1597. for (iSubItem = plv->cCol - 1; iSubItem >= 0; iSubItem--) {
  1598. RECT rc;
  1599. // see if its in this rect,
  1600. if (!Header_GetItemRect(plv->hwndHdr, iSubItem, &rc))
  1601. return -1;
  1602. OffsetRect(&rc, -plv->ptlRptOrigin.x, 0);
  1603. if (rc.left <= x && x < rc.right) {
  1604. break;
  1605. }
  1606. }
  1607. return iSubItem;
  1608. }
  1609. int ListView_OnSubItemHitTest(LV* plv, LPLVHITTESTINFO plvhti)
  1610. {
  1611. int i = -1;
  1612. int iSubItem = 0;
  1613. UINT uFlags = LVHT_NOWHERE;
  1614. if (!plvhti) {
  1615. return -1;
  1616. }
  1617. if (ListView_IsReportView(plv)) {
  1618. iSubItem = ListView_RXHitTest(plv, plvhti->pt.x);
  1619. if (iSubItem == -1) {
  1620. goto Bail;
  1621. }
  1622. }
  1623. if (iSubItem == 0) {
  1624. // if we're in column 0, just hand it off to the old stuff
  1625. ListView_OnHitTest(plv, plvhti);
  1626. plvhti->iSubItem = 0;
  1627. return plvhti->iItem;
  1628. }
  1629. if (!ListView_IsReportView(plv)) {
  1630. goto Bail;
  1631. }
  1632. i = ListView_RYHitTest(plv, plvhti->pt.y);
  1633. if (i < ListView_Count(plv)) {
  1634. RECT rcIcon, rcLabel;
  1635. if (i != -1) {
  1636. ListView_RGetRectsEx(plv, i, iSubItem, &rcIcon, &rcLabel);
  1637. if (plvhti->pt.x >= rcIcon.left && plvhti->pt.x <= rcIcon.right) {
  1638. uFlags = LVHT_ONITEMICON;
  1639. } else if (plvhti->pt.x >= rcLabel.left && plvhti->pt.x <= rcLabel.right){
  1640. uFlags = LVHT_ONITEMLABEL;
  1641. } else
  1642. uFlags = LVHT_ONITEM;
  1643. }
  1644. } else {
  1645. i = -1;
  1646. }
  1647. Bail:
  1648. plvhti->iItem = i;
  1649. plvhti->iSubItem = iSubItem;
  1650. plvhti->flags = uFlags;
  1651. return plvhti->iItem;
  1652. }
  1653. // BUGBUG: this is duplicate code with all the other views!
  1654. // See whether entire string will fit in *prc; if not, compute number of chars
  1655. // that will fit, including ellipses. Returns length of string in *pcchDraw.
  1656. //
  1657. BOOL NEAR ListView_NeedsEllipses(HDC hdc, LPCTSTR pszText, RECT FAR* prc, int FAR* pcchDraw, int cxEllipses)
  1658. {
  1659. int cchText;
  1660. int cxRect;
  1661. int ichMin, ichMax, ichMid;
  1662. SIZE siz;
  1663. cxRect = prc->right - prc->left;
  1664. cchText = lstrlen(pszText);
  1665. if (cchText == 0)
  1666. {
  1667. *pcchDraw = cchText;
  1668. return FALSE;
  1669. }
  1670. GetTextExtentPoint(hdc, pszText, cchText, &siz);
  1671. if (siz.cx <= cxRect)
  1672. {
  1673. *pcchDraw = cchText;
  1674. return FALSE;
  1675. }
  1676. cxRect -= cxEllipses;
  1677. // If no room for ellipses, always show first character.
  1678. //
  1679. ichMax = 1;
  1680. if (cxRect > 0)
  1681. {
  1682. // Binary search to find character that will fit
  1683. ichMin = 0;
  1684. ichMax = cchText;
  1685. while (ichMin < ichMax)
  1686. {
  1687. // Be sure to round up, to make sure we make progress in
  1688. // the loop if ichMax == ichMin + 1.
  1689. //
  1690. ichMid = (ichMin + ichMax + 1) / 2;
  1691. GetTextExtentPoint(hdc, &pszText[ichMin], ichMid - ichMin, &siz);
  1692. if (siz.cx < cxRect)
  1693. {
  1694. ichMin = ichMid;
  1695. cxRect -= siz.cx;
  1696. }
  1697. else if (siz.cx > cxRect)
  1698. {
  1699. ichMax = ichMid - 1;
  1700. }
  1701. else
  1702. {
  1703. // Exact match up up to ichMid: just exit.
  1704. //
  1705. ichMax = ichMid;
  1706. break;
  1707. }
  1708. }
  1709. // Make sure we always show at least the first character...
  1710. //
  1711. if (ichMax < 1)
  1712. ichMax = 1;
  1713. }
  1714. *pcchDraw = ichMax;
  1715. return TRUE;
  1716. }
  1717. void NEAR ListView_RUpdateScrollBars(LV* plv)
  1718. {
  1719. HD_LAYOUT layout;
  1720. RECT rcClient;
  1721. RECT rcBounds;
  1722. WINDOWPOS wpos;
  1723. int cColVis, cyColVis, iNewPos, iyDelta = 0, ixDelta = 0;
  1724. BOOL fHorSB, fReupdate = FALSE;
  1725. SCROLLINFO si;
  1726. ListView_GetClientRect(plv, &rcClient, FALSE, NULL);
  1727. if (!plv->hwndHdr)
  1728. ListView_CreateHeader(plv);
  1729. if (!plv->hwndHdr)
  1730. TraceMsg(TF_WARNING, "ListView_RUpdateScrollBars could not create hwndHdr");
  1731. layout.pwpos = &wpos;
  1732. // For now lets try to handle scrolling the header by setting
  1733. // its window pos.
  1734. rcClient.left -= (int)plv->ptlRptOrigin.x;
  1735. layout.prc = &rcClient;
  1736. Header_Layout(plv->hwndHdr, &layout);
  1737. rcClient.left += (int)plv->ptlRptOrigin.x; // Move it back over!
  1738. SetWindowPos(plv->hwndHdr, wpos.hwndInsertAfter, wpos.x, wpos.y,
  1739. wpos.cx, wpos.cy, wpos.flags | SWP_SHOWWINDOW);
  1740. // Get the horizontal bounds of the items.
  1741. ListView_RGetRects(plv, 0, NULL, NULL, &rcBounds, NULL);
  1742. // If v3 or better, take a cyEdge off the top
  1743. if (plv->ci.iVersion >= 3)
  1744. rcClient.top += g_cyEdge;
  1745. plv->yTop = rcClient.top;
  1746. // fHorSB = Do I need a horizontal scrollbar?
  1747. // cyColVis = number of pixels per screenful
  1748. fHorSB = (rcBounds.right - rcBounds.left > rcClient.right); // First guess.
  1749. cyColVis = rcClient.bottom - rcClient.top -
  1750. (fHorSB ? ListView_GetCyScrollbar(plv) : 0);
  1751. // If screen can't fit the entire listview...
  1752. if (cyColVis < ListView_Count(plv) * plv->cyItem) {
  1753. //then we're going to have a vertical scrollbar.. make sure our horizontal count is correct
  1754. rcClient.right -= ListView_GetCxScrollbar(plv);
  1755. if (!fHorSB) {
  1756. // if we previously thought we weren't going to have a scrollbar, we could be wrong..
  1757. // since the vertical bar shrunk our area
  1758. fHorSB = (rcBounds.right - rcBounds.left > rcClient.right); // First guess.
  1759. cyColVis = rcClient.bottom - rcClient.top -
  1760. (fHorSB ? ListView_GetCyScrollbar(plv) : 0);
  1761. }
  1762. }
  1763. // cColVis = number of completely visible items per screenful
  1764. cColVis = cyColVis / plv->cyItem;
  1765. si.cbSize = sizeof(SCROLLINFO);
  1766. si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
  1767. si.nPos = (int)(plv->ptlRptOrigin.y / plv->cyItem);
  1768. si.nPage = cColVis;
  1769. si.nMin = 0;
  1770. si.nMax = ListView_Count(plv) - 1;
  1771. ListView_SetScrollInfo(plv, SB_VERT, &si, TRUE);
  1772. // make sure our position and page doesn't hang over max
  1773. if ((si.nPos > (int)si.nMax - (int)si.nPage + 1) && si.nPos > 0) {
  1774. iNewPos = (int)si.nMax - (int)si.nPage + 1;
  1775. if (iNewPos < 0) iNewPos = 0;
  1776. if (iNewPos != si.nPos) {
  1777. iyDelta = iNewPos - (int)si.nPos;
  1778. fReupdate = TRUE;
  1779. }
  1780. }
  1781. si.nPos = (int)plv->ptlRptOrigin.x;
  1782. si.nPage = rcClient.right - rcClient.left;
  1783. // We need to subtract 1 here because nMax is 0 based, and nPage is the actual
  1784. // number of page pixels. So, if nPage and nMax are the same we will get a
  1785. // horz scroll, since there is 1 more pixel than the page can show, but... rcBounds
  1786. // is like rcRect, and is the actual number of pixels for the whole thing, so
  1787. // we need to set nMax so that: nMax - 0 == rcBounds.right - rcBound.left
  1788. si.nMax = rcBounds.right - rcBounds.left - 1;
  1789. ListView_SetScrollInfo(plv, SB_HORZ, &si, TRUE);
  1790. // SWP_FRAMECHANGED redraws the background if the client
  1791. // area has changed (taking into account scrollbars and
  1792. // the Header window). SetScrollInfo does this automatically
  1793. // when it creates a scrollbar - we do it ourselves when
  1794. // there is no scrollbar.
  1795. if ((UINT)si.nPage > (UINT)si.nMax &&
  1796. ((plv->pImgCtx && plv->fImgCtxComplete) || plv->hbmBkImage))
  1797. SetWindowPos(plv->ci.hwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
  1798. // make sure our position and page doesn't hang over max
  1799. if ((si.nPos + (LONG)si.nPage - 1 > si.nMax) && si.nPos > 0) {
  1800. iNewPos = (int)si.nMax - (int)si.nPage + 1;
  1801. if (iNewPos < 0) iNewPos = 0;
  1802. if (iNewPos != si.nPos) {
  1803. ixDelta = iNewPos - (int)si.nPos;
  1804. fReupdate = TRUE;
  1805. }
  1806. }
  1807. if (fReupdate) {
  1808. // we shouldn't recurse because the second time through, si.nPos >0
  1809. ListView_RScroll2(plv, ixDelta, iyDelta, 0);
  1810. ListView_RUpdateScrollBars(plv);
  1811. TraceMsg(TF_LISTVIEW, "LISTVIEW: ERROR: We had to recurse!");
  1812. }
  1813. }
  1814. //
  1815. // We need a smoothscroll callback so our background image draws
  1816. // at the correct origin. If we don't have a background image,
  1817. // then this work is superfluous but not harmful either.
  1818. //
  1819. int CALLBACK ListView_RScroll2_SmoothScroll(
  1820. HWND hwnd,
  1821. int dx,
  1822. int dy,
  1823. CONST RECT *prcScroll,
  1824. CONST RECT *prcClip,
  1825. HRGN hrgnUpdate,
  1826. LPRECT prcUpdate,
  1827. UINT flags)
  1828. {
  1829. LV* plv = ListView_GetPtr(hwnd);
  1830. if (plv)
  1831. {
  1832. plv->ptlRptOrigin.x -= dx;
  1833. plv->ptlRptOrigin.y -= dy;
  1834. }
  1835. // Now do what SmoothScrollWindow would've done if we weren't
  1836. // a callback
  1837. return ScrollWindowEx(hwnd, dx, dy, prcScroll, prcClip, hrgnUpdate, prcUpdate, flags);
  1838. }
  1839. void FAR PASCAL ListView_RScroll2(LV* plv, int dx, int dy, UINT uSmooth)
  1840. {
  1841. LONG ldy;
  1842. if (dx | dy)
  1843. {
  1844. RECT rc;
  1845. GetClientRect(plv->ci.hwnd, &rc);
  1846. rc.top = plv->yTop;
  1847. // We can not do a simple multiply here as we may run into
  1848. // a case where this will overflow an int..
  1849. ldy = (LONG)dy * plv->cyItem;
  1850. // handle case where dy is large (greater than int...)
  1851. if ((ldy > rc.bottom) || (ldy < -rc.bottom)) {
  1852. InvalidateRect(plv->ci.hwnd, NULL, TRUE);
  1853. plv->ptlRptOrigin.x += dx;
  1854. plv->ptlRptOrigin.y += ldy;
  1855. } else {
  1856. SMOOTHSCROLLINFO si;
  1857. si.cbSize = sizeof(si);
  1858. si.fMask = SSIF_SCROLLPROC;
  1859. si.hwnd = plv->ci.hwnd;
  1860. si.dx = -dx;
  1861. si.dy = (int)-ldy;
  1862. si.lprcSrc = NULL;
  1863. si.lprcClip = &rc;
  1864. si.hrgnUpdate = NULL;
  1865. si.lprcUpdate = NULL;
  1866. si.fuScroll =SW_INVALIDATE | SW_ERASE | uSmooth;
  1867. si.pfnScrollProc = ListView_RScroll2_SmoothScroll;
  1868. SmoothScrollWindow(&si);
  1869. /// this causes horrible flicker/repaint on deletes.
  1870. // if this is a problem with UI scrolling, we'll have to pass through a
  1871. // flag when to use this
  1872. ///UpdateWindow(plv->ci.hwnd);
  1873. }
  1874. // if Horizontal scrolling, we should update the location of the
  1875. // left hand edge of the window...
  1876. //
  1877. if (dx != 0)
  1878. {
  1879. RECT rcHdr;
  1880. GetWindowRect(plv->hwndHdr, &rcHdr);
  1881. MapWindowRect(HWND_DESKTOP, plv->ci.hwnd, &rcHdr);
  1882. SetWindowPos(plv->hwndHdr, NULL, rcHdr.left - dx, rcHdr.top,
  1883. rcHdr.right - rcHdr.left + dx,
  1884. rcHdr.bottom - rcHdr.top,
  1885. SWP_NOZORDER | SWP_NOACTIVATE);
  1886. }
  1887. }
  1888. }
  1889. //-------------------------------------------------------------------
  1890. // Make sure that specified item is visible for report view.
  1891. // Must handle Large number of items...
  1892. BOOL NEAR ListView_ROnEnsureVisible(LV* plv, int iItem, BOOL fPartialOK)
  1893. {
  1894. LONG dy;
  1895. LONG yTop;
  1896. LONG lyTop;
  1897. yTop = plv->yTop;
  1898. // lyTop = where our item is right now
  1899. lyTop = (LONG)iItem * plv->cyItem - plv->ptlRptOrigin.y + plv->yTop;
  1900. // If visible below yTop and our bottom is visible above client bottom,
  1901. // then we're happy.
  1902. if ((lyTop >= (LONG)yTop) &&
  1903. ((lyTop + plv->cyItem) <= (LONG)plv->sizeClient.cy))
  1904. return(TRUE); // we are visible
  1905. dy = lyTop - yTop;
  1906. if (dy >= 0)
  1907. {
  1908. // dy = how many pixels we need to scroll to come into view
  1909. dy = lyTop + plv->cyItem - plv->sizeClient.cy;
  1910. if (dy < 0)
  1911. dy = 0;
  1912. }
  1913. if (dy)
  1914. {
  1915. int iRound = ((dy > 0) ? 1 : -1) * (plv->cyItem - 1);
  1916. // Now convert into the number of items to scroll...
  1917. dy = (dy + iRound) / plv->cyItem;
  1918. ListView_RScroll2(plv, 0, (int)dy, 0);
  1919. if (ListView_RedrawEnabled(plv)) {
  1920. ListView_UpdateScrollBars(plv);
  1921. } else {
  1922. ListView_DeleteHrgnInval(plv);
  1923. plv->hrgnInval = (HRGN)ENTIRE_REGION;
  1924. plv->flags |= LVF_ERASE;
  1925. }
  1926. }
  1927. return TRUE;
  1928. }
  1929. void NEAR ListView_ROnScroll(LV* plv, UINT code, int posNew, UINT sb)
  1930. {
  1931. int cLine;
  1932. cLine = (sb == SB_VERT) ? 1 : plv->cxLabelChar;
  1933. ListView_ComOnScroll(plv, code, posNew, sb, cLine, -1);
  1934. }
  1935. int NEAR ListView_RGetScrollUnitsPerLine(LV* plv, UINT sb)
  1936. {
  1937. int cLine;
  1938. cLine = (sb == SB_VERT) ? 1 : plv->cxLabelChar;
  1939. return cLine;
  1940. }