3 * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4 * Copyright 1998,1999 Alex Priem <alexp@sci.kun.nl>
5 * Copyright 1999 Sylvain St-Germain
6 * Copyright 2002 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Note that TREEVIEW_INFO * and HTREEITEM are the same thing.
26 * Note2: If item's text == LPSTR_TEXTCALLBACKA we allocate buffer
27 * of size TEXT_CALLBACK_SIZE in DoSetItem.
28 * We use callbackMask to keep track of fields to be updated.
31 * missing notifications: TVN_GETINFOTIP, TVN_KEYDOWN,
32 * TVN_SETDISPINFO, TVN_SINGLEEXPAND
34 * missing styles: TVS_FULLROWSELECT, TVS_INFOTIP, TVS_RTLREADING,
36 * missing item styles: TVIS_CUT, TVIS_EXPANDPARTIAL
38 * Make the insertion mark look right.
39 * Scroll (instead of repaint) as much as possible.
43 #include "wine/port.h"
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
63 #include "wine/unicode.h"
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(treeview);
68 /* internal structures */
70 typedef struct _TREEITEM /* HTREEITEM is a _TREEINFO *. */
81 int iIntegral; /* item height multiplier (1 is normal) */
82 int iLevel; /* indentation level:0=root level */
83 HTREEITEM parent; /* handle to parent or 0 if at root */
84 HTREEITEM firstChild; /* handle to first child or 0 if no child */
86 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
87 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
93 LONG textWidth; /* horizontal text extent for pszText */
94 LONG visibleOrder; /* visible ordering, 0 is first visible item */
98 typedef struct tagTREEVIEW_INFO
101 HWND hwndNotify; /* Owner window to send notifications to */
104 UINT uInternalStatus;
106 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
107 INT cdmode; /* last custom draw setting */
108 UINT uScrollTime; /* max. time for scrolling in milliseconds */
109 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
111 UINT uItemHeight; /* item height */
114 LONG clientWidth; /* width of control window */
115 LONG clientHeight; /* height of control window */
117 LONG treeWidth; /* width of visible tree items */
118 LONG treeHeight; /* height of visible tree items */
120 UINT uIndent; /* indentation in pixels */
121 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
122 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
123 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
124 HTREEITEM editItem; /* item being edited with builtin edit box */
126 HTREEITEM firstVisible; /* handle to first visible item */
127 LONG maxVisibleOrder;
128 HTREEITEM dropItem; /* handle to item selected by drag cursor */
129 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
130 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
131 HIMAGELIST dragList; /* Bitmap of dragged item */
136 COLORREF clrInsertMark;
140 HFONT hUnderlineFont;
145 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
146 BOOL bIgnoreEditKillFocus;
149 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
150 HIMAGELIST himlNormal;
151 int normalImageHeight;
152 int normalImageWidth;
153 HIMAGELIST himlState;
154 int stateImageHeight;
158 DWORD lastKeyPressTimestamp;
160 INT nSearchParamLength;
161 WCHAR szSearchParam[ MAX_PATH ];
165 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
166 #define KEY_DELAY 450
168 /* bitflags for infoPtr->uInternalStatus */
170 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
171 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
172 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
173 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
174 #define TV_RDRAG 0x10 /* ditto Rbutton */
175 #define TV_RDRAGGING 0x20
177 /* bitflags for infoPtr->timer */
179 #define TV_EDIT_TIMER 2
180 #define TV_EDIT_TIMER_SET 2
182 #define TEXT_CALLBACK_SIZE 260
184 #define TREEVIEW_LEFT_MARGIN 8
186 #define MINIMUM_INDENT 19
188 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
190 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
191 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
192 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
194 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
195 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
196 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
197 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
199 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
202 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
205 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
207 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
208 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
209 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
210 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
211 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
212 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
213 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
215 /* Random Utilities *****************************************************/
216 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
218 /* Returns the treeview private data if hwnd is a treeview.
219 * Otherwise returns an undefined value. */
220 static inline TREEVIEW_INFO *
221 TREEVIEW_GetInfoPtr(HWND hwnd)
223 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
226 /* Don't call this. Nothing wants an item index. */
228 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
230 return DPA_GetPtrIndex(infoPtr->items, handle);
233 /* Checks if item has changed and needs to be redrawn */
234 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
235 const TVITEMEXW *tvChange)
237 /* Number of children has changed */
238 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
241 /* Image has changed and it's not a callback */
242 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
243 tiNew->iImage != I_IMAGECALLBACK)
246 /* Selected image has changed and it's not a callback */
247 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
248 tiNew->iSelectedImage != I_IMAGECALLBACK)
251 /* Text has changed and it's not a callback */
252 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
253 tiNew->pszText != LPSTR_TEXTCALLBACKW)
256 /* Indent has changed */
257 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
260 /* Item state has changed */
261 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
267 /***************************************************************************
268 * This method checks that handle is an item for this tree.
271 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
273 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
275 TRACE("invalid item %p\n", handle);
283 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
287 GetObjectW(hOrigFont, sizeof(font), &font);
288 font.lfWeight = FW_BOLD;
289 return CreateFontIndirectW(&font);
293 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
297 GetObjectW(hOrigFont, sizeof(font), &font);
298 font.lfUnderline = TRUE;
299 return CreateFontIndirectW(&font);
303 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
305 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
306 return infoPtr->hUnderlineFont;
307 if (item->state & TVIS_BOLD)
308 return infoPtr->hBoldFont;
309 return infoPtr->hFont;
312 /* for trace/debugging purposes only */
314 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
316 if (item == NULL) return "<null item>";
317 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
318 if (item->pszText == NULL) return "<null>";
319 return debugstr_w(item->pszText);
322 /* An item is not a child of itself. */
324 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
328 child = child->parent;
329 if (child == parent) return TRUE;
330 } while (child != NULL);
336 /* Tree Traversal *******************************************************/
338 /***************************************************************************
339 * This method returns the last expanded sibling or child child item
342 static TREEVIEW_ITEM *
343 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
348 while (wineItem->lastChild)
350 if (wineItem->state & TVIS_EXPANDED)
351 wineItem = wineItem->lastChild;
356 if (wineItem == infoPtr->root)
362 /***************************************************************************
363 * This method returns the previous non-hidden item in the list not
364 * considering the tree hierarchy.
366 static TREEVIEW_ITEM *
367 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
369 if (tvItem->prevSibling)
371 /* This item has a prevSibling, get the last item in the sibling's tree. */
372 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
374 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
375 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
381 /* this item does not have a prevSibling, get the parent */
382 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
387 /***************************************************************************
388 * This method returns the next physical item in the treeview not
389 * considering the tree hierarchy.
391 static TREEVIEW_ITEM *
392 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
395 * If this item has children and is expanded, return the first child
397 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
399 return tvItem->firstChild;
404 * try to get the sibling
406 if (tvItem->nextSibling)
407 return tvItem->nextSibling;
410 * Otherwise, get the parent's sibling.
412 while (tvItem->parent)
414 tvItem = tvItem->parent;
416 if (tvItem->nextSibling)
417 return tvItem->nextSibling;
423 /***************************************************************************
424 * This method returns the nth item starting at the given item. It returns
425 * the last item (or first) we we run out of items.
427 * Will scroll backward if count is <0.
428 * forward if count is >0.
430 static TREEVIEW_ITEM *
431 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
434 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
435 TREEVIEW_ITEM *previousItem;
437 assert(wineItem != NULL);
441 next_item = TREEVIEW_GetNextListItem;
446 next_item = TREEVIEW_GetPrevListItem;
453 previousItem = wineItem;
454 wineItem = next_item(infoPtr, wineItem);
456 } while (--count && wineItem != NULL);
459 return wineItem ? wineItem : previousItem;
462 /* Notifications ************************************************************/
464 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
466 if (!infoPtr->bNtfUnicode) {
468 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
469 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
470 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
471 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
472 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
473 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
474 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
475 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
476 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
477 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
478 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
479 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
486 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
488 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
489 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
493 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
496 HWND hwnd = infoPtr->hwnd;
499 nmhdr.hwndFrom = hwnd;
500 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
501 nmhdr.code = get_notifycode(infoPtr, code);
503 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
507 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
510 tvItem->hItem = item;
511 tvItem->state = item->state;
512 tvItem->stateMask = 0;
513 tvItem->iImage = item->iImage;
514 tvItem->iSelectedImage = item->iSelectedImage;
515 tvItem->cChildren = item->cChildren;
516 tvItem->lParam = item->lParam;
520 if (!infoPtr->bNtfUnicode)
522 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
523 tvItem->pszText = Alloc (tvItem->cchTextMax);
524 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
528 tvItem->cchTextMax = item->cchTextMax;
529 tvItem->pszText = item->pszText;
534 tvItem->cchTextMax = 0;
535 tvItem->pszText = NULL;
540 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
541 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
543 HWND hwnd = infoPtr->hwnd;
547 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
548 code, action, oldItem, newItem);
550 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
552 nmhdr.hdr.hwndFrom = hwnd;
553 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
554 nmhdr.hdr.code = get_notifycode(infoPtr, code);
555 nmhdr.action = action;
558 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
561 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
566 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
567 if (!infoPtr->bNtfUnicode)
569 Free(nmhdr.itemOld.pszText);
570 Free(nmhdr.itemNew.pszText);
576 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
577 HTREEITEM dragItem, POINT pt)
579 HWND hwnd = infoPtr->hwnd;
582 TRACE("code:%d dragitem:%p\n", code, dragItem);
584 nmhdr.hdr.hwndFrom = hwnd;
585 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
586 nmhdr.hdr.code = get_notifycode(infoPtr, code);
588 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
589 nmhdr.itemNew.hItem = dragItem;
590 nmhdr.itemNew.state = dragItem->state;
591 nmhdr.itemNew.lParam = dragItem->lParam;
593 nmhdr.ptDrag.x = pt.x;
594 nmhdr.ptDrag.y = pt.y;
596 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
601 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
604 HWND hwnd = infoPtr->hwnd;
605 NMTVCUSTOMDRAW nmcdhdr;
608 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
610 nmcd = &nmcdhdr.nmcd;
611 nmcd->hdr.hwndFrom = hwnd;
612 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
613 nmcd->hdr.code = NM_CUSTOMDRAW;
614 nmcd->dwDrawStage = dwDrawStage;
617 nmcd->dwItemSpec = 0;
618 nmcd->uItemState = 0;
619 nmcd->lItemlParam = 0;
620 nmcdhdr.clrText = infoPtr->clrText;
621 nmcdhdr.clrTextBk = infoPtr->clrBk;
624 return (BOOL)TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
629 /* FIXME: need to find out when the flags in uItemState need to be set */
632 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
633 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
634 NMTVCUSTOMDRAW *nmcdhdr)
636 HWND hwnd = infoPtr->hwnd;
639 DWORD_PTR dwItemSpec;
643 dwDrawStage = CDDS_ITEM | uItemDrawState;
644 dwItemSpec = (DWORD_PTR)wineItem;
646 if (wineItem->state & TVIS_SELECTED)
647 uItemState |= CDIS_SELECTED;
648 if (wineItem == infoPtr->selectedItem)
649 uItemState |= CDIS_FOCUS;
650 if (wineItem == infoPtr->hotItem)
651 uItemState |= CDIS_HOT;
653 nmcd = &nmcdhdr->nmcd;
654 nmcd->hdr.hwndFrom = hwnd;
655 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
656 nmcd->hdr.code = NM_CUSTOMDRAW;
657 nmcd->dwDrawStage = dwDrawStage;
659 nmcd->rc = wineItem->rect;
660 nmcd->dwItemSpec = dwItemSpec;
661 nmcd->uItemState = uItemState;
662 nmcd->lItemlParam = wineItem->lParam;
663 nmcdhdr->iLevel = wineItem->iLevel;
665 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
666 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
667 nmcd->uItemState, nmcd->lItemlParam);
669 retval = TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
675 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
677 HWND hwnd = infoPtr->hwnd;
681 tvdi.hdr.hwndFrom = hwnd;
682 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
683 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
685 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
686 &tvdi.item, editItem);
688 ret = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
690 if (!infoPtr->bNtfUnicode)
691 Free(tvdi.item.pszText);
697 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
700 NMTVDISPINFOEXW callback;
701 HWND hwnd = infoPtr->hwnd;
703 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
704 mask &= wineItem->callbackMask;
706 if (mask == 0) return;
708 callback.hdr.hwndFrom = hwnd;
709 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
710 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
712 /* 'state' always contains valid value, as well as 'lParam'.
713 * All other parameters are uninitialized.
715 callback.item.pszText = wineItem->pszText;
716 callback.item.cchTextMax = wineItem->cchTextMax;
717 callback.item.mask = mask;
718 callback.item.hItem = wineItem;
719 callback.item.state = wineItem->state;
720 callback.item.lParam = wineItem->lParam;
722 /* If text is changed we need to recalculate textWidth */
723 if (mask & TVIF_TEXT)
724 wineItem->textWidth = 0;
726 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
728 /* It may have changed due to a call to SetItem. */
729 mask &= wineItem->callbackMask;
731 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
733 /* Instead of copying text into our buffer user specified its own */
734 if (!infoPtr->bNtfUnicode) {
737 int len = MultiByteToWideChar( CP_ACP, 0,
738 (LPSTR)callback.item.pszText, -1,
740 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
741 newText = ReAlloc(wineItem->pszText, buflen);
743 TRACE("returned str %s, len=%d, buflen=%d\n",
744 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
748 wineItem->pszText = newText;
749 MultiByteToWideChar( CP_ACP, 0,
750 (LPSTR)callback.item.pszText, -1,
751 wineItem->pszText, buflen/sizeof(WCHAR));
752 wineItem->cchTextMax = buflen/sizeof(WCHAR);
754 /* If ReAlloc fails we have nothing to do, but keep original text */
757 int len = max(lstrlenW(callback.item.pszText) + 1,
759 LPWSTR newText = ReAlloc(wineItem->pszText, len);
761 TRACE("returned wstr %s, len=%d\n",
762 debugstr_w(callback.item.pszText), len);
766 wineItem->pszText = newText;
767 strcpyW(wineItem->pszText, callback.item.pszText);
768 wineItem->cchTextMax = len;
770 /* If ReAlloc fails we have nothing to do, but keep original text */
773 else if (mask & TVIF_TEXT) {
774 /* User put text into our buffer, that is ok unless A string */
775 if (!infoPtr->bNtfUnicode) {
777 LPWSTR oldText = NULL;
779 int len = MultiByteToWideChar( CP_ACP, 0,
780 (LPSTR)callback.item.pszText, -1,
782 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
783 newText = Alloc(buflen);
785 TRACE("same buffer str %s, len=%d, buflen=%d\n",
786 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
790 oldText = wineItem->pszText;
791 wineItem->pszText = newText;
792 MultiByteToWideChar( CP_ACP, 0,
793 (LPSTR)callback.item.pszText, -1,
794 wineItem->pszText, buflen/sizeof(WCHAR));
795 wineItem->cchTextMax = buflen/sizeof(WCHAR);
801 if (mask & TVIF_IMAGE)
802 wineItem->iImage = callback.item.iImage;
804 if (mask & TVIF_SELECTEDIMAGE)
805 wineItem->iSelectedImage = callback.item.iSelectedImage;
807 if (mask & TVIF_CHILDREN)
808 wineItem->cChildren = callback.item.cChildren;
810 /* These members are now permanently set. */
811 if (callback.item.mask & TVIF_DI_SETITEM)
812 wineItem->callbackMask &= ~callback.item.mask;
815 /***************************************************************************
816 * This function uses cChildren field to decide whether the item has
818 * Note: if this returns TRUE, the child items may not actually exist,
819 * they could be virtual.
821 * Just use wineItem->firstChild to check for physical children.
824 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
826 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
828 return wineItem->cChildren > 0;
831 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
835 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
837 if (nCommand != NF_REQUERY) return 0;
839 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
840 TRACE("format=%d\n", format);
842 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
844 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
849 /* Item Position ********************************************************/
851 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
853 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
855 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
856 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
859 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
861 item->stateOffset = item->linesOffset + infoPtr->uIndent;
862 item->imageOffset = item->stateOffset
863 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
864 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
868 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
874 /* DRAW's OM docker creates items like this */
875 if (item->pszText == NULL)
887 hdc = GetDC(infoPtr->hwnd);
888 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
891 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
892 item->textWidth = sz.cx;
896 SelectObject(hdc, hOldFont);
902 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
904 item->rect.top = infoPtr->uItemHeight *
905 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
907 item->rect.bottom = item->rect.top
908 + infoPtr->uItemHeight * item->iIntegral - 1;
911 item->rect.right = infoPtr->clientWidth;
914 /* We know that only items after start need their order updated. */
916 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
923 start = infoPtr->root->firstChild;
927 order = start->visibleOrder;
929 for (item = start; item != NULL;
930 item = TREEVIEW_GetNextListItem(infoPtr, item))
932 if (!ISVISIBLE(item) && order > 0)
933 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
934 item->visibleOrder = order;
935 order += item->iIntegral;
938 infoPtr->maxVisibleOrder = order;
940 for (item = start; item != NULL;
941 item = TREEVIEW_GetNextListItem(infoPtr, item))
943 TREEVIEW_ComputeItemRect(infoPtr, item);
948 /* Update metrics of all items in selected subtree.
949 * root must be expanded
952 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
954 TREEVIEW_ITEM *sibling;
958 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
961 root->state &= ~TVIS_EXPANDED;
962 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
963 root->state |= TVIS_EXPANDED;
965 hdc = GetDC(infoPtr->hwnd);
966 hOldFont = SelectObject(hdc, infoPtr->hFont);
968 for (; root != sibling;
969 root = TREEVIEW_GetNextListItem(infoPtr, root))
971 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
973 if (root->callbackMask & TVIF_TEXT)
974 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
976 if (root->textWidth == 0)
978 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
979 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
983 SelectObject(hdc, hOldFont);
984 ReleaseDC(infoPtr->hwnd, hdc);
987 /* Item Allocation **********************************************************/
989 static TREEVIEW_ITEM *
990 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
992 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
997 /* I_IMAGENONE would make more sense but this is neither what is
998 * documented (MSDN doesn't specify) nor what Windows actually does
999 * (it sets it to zero)... and I can so imagine an application using
1000 * inc/dec to toggle the images. */
1001 newItem->iImage = 0;
1002 newItem->iSelectedImage = 0;
1004 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1013 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1014 * free item->pszText. */
1016 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1018 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1019 if (infoPtr->selectedItem == item)
1020 infoPtr->selectedItem = NULL;
1021 if (infoPtr->hotItem == item)
1022 infoPtr->hotItem = NULL;
1023 if (infoPtr->focusedItem == item)
1024 infoPtr->focusedItem = NULL;
1025 if (infoPtr->firstVisible == item)
1026 infoPtr->firstVisible = NULL;
1027 if (infoPtr->dropItem == item)
1028 infoPtr->dropItem = NULL;
1029 if (infoPtr->insertMarkItem == item)
1030 infoPtr->insertMarkItem = NULL;
1035 /* Item Insertion *******************************************************/
1037 /***************************************************************************
1038 * This method inserts newItem before sibling as a child of parent.
1039 * sibling can be NULL, but only if parent has no children.
1042 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1043 TREEVIEW_ITEM *parent)
1045 assert(parent != NULL);
1047 if (sibling != NULL)
1049 assert(sibling->parent == parent);
1051 if (sibling->prevSibling != NULL)
1052 sibling->prevSibling->nextSibling = newItem;
1054 newItem->prevSibling = sibling->prevSibling;
1055 sibling->prevSibling = newItem;
1058 newItem->prevSibling = NULL;
1060 newItem->nextSibling = sibling;
1062 if (parent->firstChild == sibling)
1063 parent->firstChild = newItem;
1065 if (parent->lastChild == NULL)
1066 parent->lastChild = newItem;
1069 /***************************************************************************
1070 * This method inserts newItem after sibling as a child of parent.
1071 * sibling can be NULL, but only if parent has no children.
1074 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1075 TREEVIEW_ITEM *parent)
1077 assert(parent != NULL);
1079 if (sibling != NULL)
1081 assert(sibling->parent == parent);
1083 if (sibling->nextSibling != NULL)
1084 sibling->nextSibling->prevSibling = newItem;
1086 newItem->nextSibling = sibling->nextSibling;
1087 sibling->nextSibling = newItem;
1090 newItem->nextSibling = NULL;
1092 newItem->prevSibling = sibling;
1094 if (parent->lastChild == sibling)
1095 parent->lastChild = newItem;
1097 if (parent->firstChild == NULL)
1098 parent->firstChild = newItem;
1102 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1103 const TVITEMEXW *tvItem, BOOL isW)
1105 UINT callbackClear = 0;
1106 UINT callbackSet = 0;
1108 TRACE("item %p\n", wineItem);
1109 /* Do this first in case it fails. */
1110 if (tvItem->mask & TVIF_TEXT)
1112 wineItem->textWidth = 0; /* force width recalculation */
1113 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1118 len = lstrlenW(tvItem->pszText) + 1;
1120 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1122 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1124 if (newText == NULL) return FALSE;
1126 callbackClear |= TVIF_TEXT;
1128 wineItem->pszText = newText;
1129 wineItem->cchTextMax = len;
1131 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1133 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1134 wineItem->pszText, len);
1136 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1140 callbackSet |= TVIF_TEXT;
1142 wineItem->pszText = ReAlloc(wineItem->pszText,
1143 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1144 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1145 TRACE("setting callback, item %p\n", wineItem);
1149 if (tvItem->mask & TVIF_CHILDREN)
1151 wineItem->cChildren = tvItem->cChildren;
1153 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1154 callbackSet |= TVIF_CHILDREN;
1156 callbackClear |= TVIF_CHILDREN;
1159 if (tvItem->mask & TVIF_IMAGE)
1161 wineItem->iImage = tvItem->iImage;
1163 if (wineItem->iImage == I_IMAGECALLBACK)
1164 callbackSet |= TVIF_IMAGE;
1166 callbackClear |= TVIF_IMAGE;
1169 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1171 wineItem->iSelectedImage = tvItem->iSelectedImage;
1173 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1174 callbackSet |= TVIF_SELECTEDIMAGE;
1176 callbackClear |= TVIF_SELECTEDIMAGE;
1179 if (tvItem->mask & TVIF_PARAM)
1180 wineItem->lParam = tvItem->lParam;
1182 /* If the application sets TVIF_INTEGRAL without
1183 * supplying a TVITEMEX structure, it's toast. */
1184 if (tvItem->mask & TVIF_INTEGRAL)
1185 wineItem->iIntegral = tvItem->iIntegral;
1187 if (tvItem->mask & TVIF_STATE)
1189 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1191 wineItem->state &= ~tvItem->stateMask;
1192 wineItem->state |= (tvItem->state & tvItem->stateMask);
1195 wineItem->callbackMask |= callbackSet;
1196 wineItem->callbackMask &= ~callbackClear;
1201 /* Note that the new item is pre-zeroed. */
1203 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1205 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1206 HTREEITEM insertAfter;
1207 TREEVIEW_ITEM *newItem, *parentItem;
1208 BOOL bTextUpdated = FALSE;
1210 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1212 parentItem = infoPtr->root;
1216 parentItem = ptdi->hParent;
1218 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1220 WARN("invalid parent %p\n", parentItem);
1225 insertAfter = ptdi->hInsertAfter;
1227 /* Validate this now for convenience. */
1228 switch ((DWORD_PTR)insertAfter)
1230 case (DWORD_PTR)TVI_FIRST:
1231 case (DWORD_PTR)TVI_LAST:
1232 case (DWORD_PTR)TVI_SORT:
1236 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1237 insertAfter->parent != parentItem)
1239 WARN("invalid insert after %p\n", insertAfter);
1240 insertAfter = TVI_LAST;
1244 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1245 (tvItem->mask & TVIF_TEXT)
1246 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1247 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1250 newItem = TREEVIEW_AllocateItem(infoPtr);
1251 if (newItem == NULL)
1254 newItem->parent = parentItem;
1255 newItem->iIntegral = 1;
1256 newItem->visibleOrder = -1;
1258 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1261 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1263 infoPtr->uNumItems++;
1265 switch ((DWORD_PTR)insertAfter)
1267 case (DWORD_PTR)TVI_FIRST:
1269 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1270 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1271 if (infoPtr->firstVisible == originalFirst)
1272 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1276 case (DWORD_PTR)TVI_LAST:
1277 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1280 /* hInsertAfter names a specific item we want to insert after */
1282 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1285 case (DWORD_PTR)TVI_SORT:
1287 TREEVIEW_ITEM *aChild;
1288 TREEVIEW_ITEM *previousChild = NULL;
1289 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1290 BOOL bItemInserted = FALSE;
1292 aChild = parentItem->firstChild;
1294 bTextUpdated = TRUE;
1295 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1297 /* Iterate the parent children to see where we fit in */
1298 while (aChild != NULL)
1302 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1303 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1305 if (comp < 0) /* we are smaller than the current one */
1307 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1308 if (infoPtr->firstVisible == originalFirst &&
1309 aChild == originalFirst)
1310 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1311 bItemInserted = TRUE;
1314 else if (comp > 0) /* we are bigger than the current one */
1316 previousChild = aChild;
1318 /* This will help us to exit if there is no more sibling */
1319 aChild = (aChild->nextSibling == 0)
1321 : aChild->nextSibling;
1323 /* Look at the next item */
1329 * An item with this name is already existing, therefore,
1330 * we add after the one we found
1332 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1333 bItemInserted = TRUE;
1339 * we reach the end of the child list and the item has not
1340 * yet been inserted, therefore, insert it after the last child.
1342 if ((!bItemInserted) && (aChild == NULL))
1343 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1350 TRACE("new item %p; parent %p, mask %x\n", newItem,
1351 newItem->parent, tvItem->mask);
1353 newItem->iLevel = newItem->parent->iLevel + 1;
1355 if (newItem->parent->cChildren == 0)
1356 newItem->parent->cChildren = 1;
1358 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1360 if (STATEIMAGEINDEX(newItem->state) == 0)
1361 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1364 if (infoPtr->firstVisible == NULL)
1365 infoPtr->firstVisible = newItem;
1367 TREEVIEW_VerifyTree(infoPtr);
1369 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1371 if (parentItem == infoPtr->root ||
1372 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1374 TREEVIEW_ITEM *item;
1375 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1377 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1378 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1381 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1383 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1384 TREEVIEW_UpdateScrollBars(infoPtr);
1386 * if the item was inserted in a visible part of the tree,
1387 * invalidate it, as well as those after it
1389 for (item = newItem;
1391 item = TREEVIEW_GetNextListItem(infoPtr, item))
1392 TREEVIEW_Invalidate(infoPtr, item);
1396 /* refresh treeview if newItem is the first item inserted under parentItem */
1397 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1399 /* parent got '+' - update it */
1400 TREEVIEW_Invalidate(infoPtr, parentItem);
1404 return (LRESULT)newItem;
1407 /* Item Deletion ************************************************************/
1409 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1412 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1414 TREEVIEW_ITEM *kill = parentItem->firstChild;
1416 while (kill != NULL)
1418 TREEVIEW_ITEM *next = kill->nextSibling;
1420 TREEVIEW_RemoveItem(infoPtr, kill);
1425 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1426 assert(parentItem->firstChild == NULL);
1427 assert(parentItem->lastChild == NULL);
1431 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1433 TREEVIEW_ITEM *parentItem = item->parent;
1435 assert(item != NULL);
1436 assert(item->parent != NULL); /* i.e. it must not be the root */
1438 if (parentItem->firstChild == item)
1439 parentItem->firstChild = item->nextSibling;
1441 if (parentItem->lastChild == item)
1442 parentItem->lastChild = item->prevSibling;
1444 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1445 && parentItem->cChildren > 0)
1446 parentItem->cChildren = 0;
1448 if (item->prevSibling)
1449 item->prevSibling->nextSibling = item->nextSibling;
1451 if (item->nextSibling)
1452 item->nextSibling->prevSibling = item->prevSibling;
1456 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1458 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1460 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1461 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1463 if (wineItem->firstChild)
1464 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1466 TREEVIEW_UnlinkItem(wineItem);
1468 infoPtr->uNumItems--;
1470 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1471 Free(wineItem->pszText);
1473 TREEVIEW_FreeItem(infoPtr, wineItem);
1477 /* Empty out the tree. */
1479 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1481 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1483 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1487 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1489 TREEVIEW_ITEM *newSelection = NULL;
1490 TREEVIEW_ITEM *newFirstVisible = NULL;
1491 TREEVIEW_ITEM *parent, *prev = NULL;
1492 BOOL visible = FALSE;
1494 if (wineItem == TVI_ROOT)
1496 TRACE("TVI_ROOT\n");
1497 parent = infoPtr->root;
1498 newSelection = NULL;
1500 TREEVIEW_RemoveTree(infoPtr);
1504 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1507 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1508 parent = wineItem->parent;
1510 if (ISVISIBLE(wineItem))
1512 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1516 if (infoPtr->selectedItem != NULL
1517 && (wineItem == infoPtr->selectedItem
1518 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1520 if (wineItem->nextSibling)
1521 newSelection = wineItem->nextSibling;
1522 else if (wineItem->parent != infoPtr->root)
1523 newSelection = wineItem->parent;
1525 newSelection = wineItem->prevSibling;
1526 TRACE("newSelection = %p\n", newSelection);
1529 if (infoPtr->firstVisible == wineItem)
1531 if (wineItem->nextSibling)
1532 newFirstVisible = wineItem->nextSibling;
1533 else if (wineItem->prevSibling)
1534 newFirstVisible = wineItem->prevSibling;
1535 else if (wineItem->parent != infoPtr->root)
1536 newFirstVisible = wineItem->parent;
1537 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1540 newFirstVisible = infoPtr->firstVisible;
1542 TREEVIEW_RemoveItem(infoPtr, wineItem);
1545 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1546 if (!infoPtr->selectedItem && newSelection)
1548 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1549 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1552 /* Validate insertMark dropItem.
1553 * hotItem ??? - used for comparison only.
1555 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1556 infoPtr->insertMarkItem = 0;
1558 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1559 infoPtr->dropItem = 0;
1561 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1562 newFirstVisible = infoPtr->root->firstChild;
1564 TREEVIEW_VerifyTree(infoPtr);
1566 if (!infoPtr->bRedraw) return TRUE;
1570 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1571 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1572 TREEVIEW_UpdateScrollBars(infoPtr);
1573 TREEVIEW_Invalidate(infoPtr, NULL);
1575 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1577 /* parent lost '+/-' - update it */
1578 TREEVIEW_Invalidate(infoPtr, parent);
1585 /* Get/Set Messages *********************************************************/
1587 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1589 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1591 if (infoPtr->bRedraw)
1593 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1594 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1595 TREEVIEW_UpdateScrollBars(infoPtr);
1596 TREEVIEW_Invalidate(infoPtr, NULL);
1602 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1605 return infoPtr->uIndent;
1609 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1613 if (newIndent < MINIMUM_INDENT)
1614 newIndent = MINIMUM_INDENT;
1616 if (infoPtr->uIndent != newIndent)
1618 infoPtr->uIndent = newIndent;
1619 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1620 TREEVIEW_UpdateScrollBars(infoPtr);
1621 TREEVIEW_Invalidate(infoPtr, NULL);
1629 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1632 return (LRESULT)infoPtr->hwndToolTip;
1636 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1641 prevToolTip = infoPtr->hwndToolTip;
1642 infoPtr->hwndToolTip = hwndTT;
1644 return (LRESULT)prevToolTip;
1648 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1650 BOOL rc = infoPtr->bNtfUnicode;
1651 infoPtr->bNtfUnicode = fUnicode;
1656 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1658 return infoPtr->bNtfUnicode;
1662 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1664 return infoPtr->uScrollTime;
1668 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1670 UINT uOldScrollTime = infoPtr->uScrollTime;
1672 infoPtr->uScrollTime = min(uScrollTime, 100);
1674 return uOldScrollTime;
1679 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1686 return (LRESULT)infoPtr->himlNormal;
1689 return (LRESULT)infoPtr->himlState;
1696 #define TVHEIGHT_MIN 16
1697 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1699 /* Compute the natural height for items. */
1701 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1705 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1708 /* Height is the maximum of:
1709 * 16 (a hack because our fonts are tiny), and
1710 * The text height + border & margin, and
1711 * The size of the normal image list
1713 GetTextMetricsW(hdc, &tm);
1714 SelectObject(hdc, hOldFont);
1717 height = TVHEIGHT_MIN;
1718 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1719 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1720 if (height < infoPtr->normalImageHeight)
1721 height = infoPtr->normalImageHeight;
1723 /* Round down, unless we support odd ("non even") heights. */
1724 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1731 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1733 HIMAGELIST himlOld = 0;
1734 int oldWidth = infoPtr->normalImageWidth;
1735 int oldHeight = infoPtr->normalImageHeight;
1738 TRACE("%lx,%p\n", wParam, himlNew);
1743 himlOld = infoPtr->himlNormal;
1744 infoPtr->himlNormal = himlNew;
1746 if (himlNew != NULL)
1747 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1748 &infoPtr->normalImageHeight);
1751 infoPtr->normalImageWidth = 0;
1752 infoPtr->normalImageHeight = 0;
1758 himlOld = infoPtr->himlState;
1759 infoPtr->himlState = himlNew;
1761 if (himlNew != NULL)
1762 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1763 &infoPtr->stateImageHeight);
1766 infoPtr->stateImageWidth = 0;
1767 infoPtr->stateImageHeight = 0;
1773 if (oldWidth != infoPtr->normalImageWidth ||
1774 oldHeight != infoPtr->normalImageHeight)
1776 BOOL bRecalcVisible = FALSE;
1778 if (oldHeight != infoPtr->normalImageHeight &&
1779 !infoPtr->bHeightSet)
1781 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1782 bRecalcVisible = TRUE;
1785 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1786 infoPtr->normalImageWidth != infoPtr->uIndent)
1788 infoPtr->uIndent = infoPtr->normalImageWidth;
1789 bRecalcVisible = TRUE;
1793 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1795 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1796 TREEVIEW_UpdateScrollBars(infoPtr);
1799 TREEVIEW_Invalidate(infoPtr, NULL);
1801 return (LRESULT)himlOld;
1805 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1807 INT prevHeight = infoPtr->uItemHeight;
1809 TRACE("%d\n", newHeight);
1810 if (newHeight == -1)
1812 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1813 infoPtr->bHeightSet = FALSE;
1817 infoPtr->uItemHeight = newHeight;
1818 infoPtr->bHeightSet = TRUE;
1821 /* Round down, unless we support odd ("non even") heights. */
1822 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1823 infoPtr->uItemHeight &= ~1;
1825 if (infoPtr->uItemHeight != prevHeight)
1827 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1828 TREEVIEW_UpdateScrollBars(infoPtr);
1829 TREEVIEW_Invalidate(infoPtr, NULL);
1836 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1839 return infoPtr->uItemHeight;
1844 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1846 TRACE("%p\n", infoPtr->hFont);
1847 return (LRESULT)infoPtr->hFont;
1852 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1856 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1862 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1864 UINT uHeight = infoPtr->uItemHeight;
1866 TRACE("%p %i\n", hFont, bRedraw);
1868 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1870 DeleteObject(infoPtr->hBoldFont);
1871 DeleteObject(infoPtr->hUnderlineFont);
1872 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1873 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1875 if (!infoPtr->bHeightSet)
1876 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1878 if (uHeight != infoPtr->uItemHeight)
1879 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1881 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1883 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1884 TREEVIEW_UpdateScrollBars(infoPtr);
1887 TREEVIEW_Invalidate(infoPtr, NULL);
1894 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1897 return (LRESULT)infoPtr->clrLine;
1901 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1903 COLORREF prevColor = infoPtr->clrLine;
1906 infoPtr->clrLine = color;
1907 return (LRESULT)prevColor;
1912 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1915 return (LRESULT)infoPtr->clrText;
1919 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1921 COLORREF prevColor = infoPtr->clrText;
1924 infoPtr->clrText = color;
1926 if (infoPtr->clrText != prevColor)
1927 TREEVIEW_Invalidate(infoPtr, NULL);
1929 return (LRESULT)prevColor;
1934 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1937 return (LRESULT)infoPtr->clrBk;
1941 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1943 COLORREF prevColor = infoPtr->clrBk;
1946 infoPtr->clrBk = newColor;
1948 if (newColor != prevColor)
1949 TREEVIEW_Invalidate(infoPtr, NULL);
1951 return (LRESULT)prevColor;
1956 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1959 return (LRESULT)infoPtr->clrInsertMark;
1963 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1965 COLORREF prevColor = infoPtr->clrInsertMark;
1967 TRACE("%x\n", color);
1968 infoPtr->clrInsertMark = color;
1970 return (LRESULT)prevColor;
1975 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1977 TRACE("%d %p\n", wParam, item);
1979 if (!TREEVIEW_ValidItem(infoPtr, item))
1982 infoPtr->insertBeforeorAfter = wParam;
1983 infoPtr->insertMarkItem = item;
1985 TREEVIEW_Invalidate(infoPtr, NULL);
1991 /************************************************************************
1992 * Some serious braindamage here. lParam is a pointer to both the
1993 * input HTREEITEM and the output RECT.
1996 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
1998 TREEVIEW_ITEM *wineItem;
1999 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2003 * validate parameters
2009 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2013 * If wParam is TRUE return the text size otherwise return
2014 * the whole item size
2018 /* Windows does not send TVN_GETDISPINFO here. */
2020 lpRect->top = wineItem->rect.top;
2021 lpRect->bottom = wineItem->rect.bottom;
2023 lpRect->left = wineItem->textOffset;
2024 if (!wineItem->textWidth)
2025 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2027 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2031 *lpRect = wineItem->rect;
2034 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2039 static inline LRESULT
2040 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2042 /* Surprise! This does not take integral height into account. */
2043 return infoPtr->clientHeight / infoPtr->uItemHeight;
2048 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2050 TREEVIEW_ITEM *wineItem;
2052 wineItem = tvItem->hItem;
2053 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2056 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2058 if (tvItem->mask & TVIF_CHILDREN)
2060 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2061 FIXME("I_CHILDRENCALLBACK not supported\n");
2062 tvItem->cChildren = wineItem->cChildren;
2065 if (tvItem->mask & TVIF_HANDLE)
2066 tvItem->hItem = wineItem;
2068 if (tvItem->mask & TVIF_IMAGE)
2069 tvItem->iImage = wineItem->iImage;
2071 if (tvItem->mask & TVIF_INTEGRAL)
2072 tvItem->iIntegral = wineItem->iIntegral;
2074 /* undocumented: windows ignores TVIF_PARAM and
2075 * * always sets lParam
2077 tvItem->lParam = wineItem->lParam;
2079 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2080 tvItem->iSelectedImage = wineItem->iSelectedImage;
2082 if (tvItem->mask & TVIF_STATE)
2083 /* Careful here - Windows ignores the stateMask when you get the state
2084 That contradicts the documentation, but makes more common sense, masking
2085 retrieval in this way seems overkill */
2086 tvItem->state = wineItem->state;
2088 if (tvItem->mask & TVIF_TEXT)
2090 if (wineItem->pszText == NULL)
2092 if (tvItem->cchTextMax > 0)
2093 tvItem->pszText[0] = '\0';
2097 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2099 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2100 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2104 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2109 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2111 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2112 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2116 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2117 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2121 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2122 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2127 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2128 * which is wrong. */
2130 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2132 TREEVIEW_ITEM *wineItem;
2133 TREEVIEW_ITEM originalItem;
2135 wineItem = tvItem->hItem;
2137 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2140 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2143 /* store the original item values */
2144 originalItem = *wineItem;
2146 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2149 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2150 if ((tvItem->mask & TVIF_TEXT
2151 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2152 && ISVISIBLE(wineItem))
2154 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2155 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2158 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2160 /* The refresh updates everything, but we can't wait until then. */
2161 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2163 /* if any of the item's values changed and it's not a callback, redraw the item */
2164 if (item_changed(&originalItem, wineItem, tvItem))
2166 if (tvItem->mask & TVIF_INTEGRAL)
2168 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2169 TREEVIEW_UpdateScrollBars(infoPtr);
2171 TREEVIEW_Invalidate(infoPtr, NULL);
2175 TREEVIEW_UpdateScrollBars(infoPtr);
2176 TREEVIEW_Invalidate(infoPtr, wineItem);
2185 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2189 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2192 return (wineItem->state & mask);
2196 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2198 TREEVIEW_ITEM *retval;
2202 /* handle all the global data here */
2205 case TVGN_CHILD: /* Special case: child of 0 is root */
2210 retval = infoPtr->root->firstChild;
2214 retval = infoPtr->selectedItem;
2217 case TVGN_FIRSTVISIBLE:
2218 retval = infoPtr->firstVisible;
2221 case TVGN_DROPHILITE:
2222 retval = infoPtr->dropItem;
2225 case TVGN_LASTVISIBLE:
2226 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2232 TRACE("flags:%x, returns %p\n", which, retval);
2233 return (LRESULT)retval;
2236 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2238 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2244 retval = wineItem->nextSibling;
2247 retval = wineItem->prevSibling;
2250 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2253 retval = wineItem->firstChild;
2255 case TVGN_NEXTVISIBLE:
2256 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2258 case TVGN_PREVIOUSVISIBLE:
2259 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2262 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2266 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2267 return (LRESULT)retval;
2272 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2274 TRACE(" %d\n", infoPtr->uNumItems);
2275 return (LRESULT)infoPtr->uNumItems;
2279 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2281 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2283 static const unsigned int state_table[] = { 0, 2, 1 };
2287 state = STATEIMAGEINDEX(item->state);
2288 TRACE("state:%x\n", state);
2289 item->state &= ~TVIS_STATEIMAGEMASK;
2292 state = state_table[state];
2294 item->state |= INDEXTOSTATEIMAGEMASK(state);
2296 TRACE("state:%x\n", state);
2297 TREEVIEW_Invalidate(infoPtr, item);
2302 /* Painting *************************************************************/
2304 /* Draw the lines and expand button for an item. Also draws one section
2305 * of the line from item's parent to item's parent's next sibling. */
2307 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2309 LONG centerx, centery;
2310 BOOL lar = ((infoPtr->dwStyle
2311 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2314 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2316 if (!lar && item->iLevel == 0)
2319 hbr = CreateSolidBrush(clrBk);
2320 hbrOld = SelectObject(hdc, hbr);
2322 centerx = (item->linesOffset + item->stateOffset) / 2;
2323 centery = (item->rect.top + item->rect.bottom) / 2;
2325 if (infoPtr->dwStyle & TVS_HASLINES)
2327 HPEN hOldPen, hNewPen;
2331 /* Get a dotted grey pen */
2332 lb.lbStyle = BS_SOLID;
2333 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2334 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2335 hOldPen = SelectObject(hdc, hNewPen);
2337 /* Make sure the center is on a dot (using +2 instead
2338 * of +1 gives us pixel-by-pixel compat with native) */
2339 centery = (centery + 2) & ~1;
2341 MoveToEx(hdc, item->stateOffset, centery, NULL);
2342 LineTo(hdc, centerx - 1, centery);
2344 if (item->prevSibling || item->parent != infoPtr->root)
2346 MoveToEx(hdc, centerx, item->rect.top, NULL);
2347 LineTo(hdc, centerx, centery);
2350 if (item->nextSibling)
2352 MoveToEx(hdc, centerx, centery, NULL);
2353 LineTo(hdc, centerx, item->rect.bottom + 1);
2356 /* Draw the line from our parent to its next sibling. */
2357 parent = item->parent;
2358 while (parent != infoPtr->root)
2360 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2362 if (parent->nextSibling
2363 /* skip top-levels unless TVS_LINESATROOT */
2364 && parent->stateOffset > parent->linesOffset)
2366 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2367 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2370 parent = parent->parent;
2373 SelectObject(hdc, hOldPen);
2374 DeleteObject(hNewPen);
2378 * Display the (+/-) signs
2381 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2383 if (item->cChildren)
2385 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2388 RECT glyphRect = item->rect;
2389 glyphRect.left = item->linesOffset;
2390 glyphRect.right = item->stateOffset;
2391 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2392 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2397 LONG height = item->rect.bottom - item->rect.top;
2398 LONG width = item->stateOffset - item->linesOffset;
2399 LONG rectsize = min(height, width) / 4;
2400 /* plussize = ceil(rectsize * 3/4) */
2401 LONG plussize = (rectsize + 1) * 3 / 4;
2403 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2404 HPEN old_pen = SelectObject(hdc, new_pen);
2406 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2407 centerx + rectsize + 2, centery + rectsize + 2);
2409 SelectObject(hdc, old_pen);
2410 DeleteObject(new_pen);
2412 /* draw +/- signs with current text color */
2413 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2414 old_pen = SelectObject(hdc, new_pen);
2416 if (height < 18 || width < 18)
2418 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2419 LineTo(hdc, centerx + plussize, centery);
2421 if (!(item->state & TVIS_EXPANDED))
2423 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2424 LineTo(hdc, centerx, centery + plussize);
2429 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2430 centerx + plussize, centery + 2);
2432 if (!(item->state & TVIS_EXPANDED))
2434 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2435 centerx + 2, centery + plussize);
2436 SetPixel(hdc, centerx - 1, centery, clrBk);
2437 SetPixel(hdc, centerx + 1, centery, clrBk);
2441 SelectObject(hdc, old_pen);
2442 DeleteObject(new_pen);
2446 SelectObject(hdc, hbrOld);
2451 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2455 COLORREF oldTextColor, oldTextBkColor;
2457 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2458 NMTVCUSTOMDRAW nmcdhdr;
2460 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2462 /* - If item is drop target or it is selected and window is in focus -
2463 * use blue background (COLOR_HIGHLIGHT).
2464 * - If item is selected, window is not in focus, but it has style
2465 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2466 * - Otherwise - use background color
2468 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2469 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2470 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2472 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2474 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2475 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2479 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2480 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2485 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2486 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2487 nmcdhdr.clrText = comctl32_color.clrHighlight;
2489 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2492 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2494 /* The custom draw handler can query the text rectangle,
2496 /* should already be known, set to 0 when changed */
2497 if (!wineItem->textWidth)
2498 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2502 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2504 cditem = TREEVIEW_SendCustomDrawItemNotify
2505 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2506 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2508 if (cditem & CDRF_SKIPDEFAULT)
2510 SelectObject(hdc, hOldFont);
2515 if (cditem & CDRF_NEWFONT)
2516 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2518 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2520 /* Set colors. Custom draw handler can change these so we do this after it. */
2521 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2522 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2524 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2527 * Display the images associated with this item
2532 /* State images are displayed to the left of the Normal image
2533 * image number is in state; zero should be `display no image'.
2535 imageIndex = STATEIMAGEINDEX(wineItem->state);
2537 if (infoPtr->himlState && imageIndex)
2539 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2540 wineItem->stateOffset,
2541 centery - infoPtr->stateImageHeight / 2,
2545 /* Now, draw the normal image; can be either selected or
2546 * non-selected image.
2549 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2551 /* The item is currently selected */
2552 imageIndex = wineItem->iSelectedImage;
2556 /* The item is not selected */
2557 imageIndex = wineItem->iImage;
2560 if (infoPtr->himlNormal)
2562 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2564 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2565 wineItem->imageOffset,
2566 centery - infoPtr->normalImageHeight / 2,
2567 ILD_NORMAL | ovlIdx);
2573 * Display the text associated with this item
2576 /* Don't paint item's text if it's being edited */
2577 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2579 if (wineItem->pszText)
2583 rcText.top = wineItem->rect.top;
2584 rcText.bottom = wineItem->rect.bottom;
2585 rcText.left = wineItem->textOffset;
2586 rcText.right = rcText.left + wineItem->textWidth + 4;
2588 TRACE("drawing text %s at (%s)\n",
2589 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2592 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2593 ETO_CLIPPED | ETO_OPAQUE,
2596 lstrlenW(wineItem->pszText),
2599 /* Draw the box around the selected item */
2600 if ((wineItem == infoPtr->selectedItem) && inFocus)
2602 DrawFocusRect(hdc,&rcText);
2608 /* Draw insertion mark if necessary */
2610 if (infoPtr->insertMarkItem)
2611 TRACE("item:%d,mark:%p\n",
2612 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2613 infoPtr->insertMarkItem);
2615 if (wineItem == infoPtr->insertMarkItem)
2617 HPEN hNewPen, hOldPen;
2621 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2622 hOldPen = SelectObject(hdc, hNewPen);
2624 if (infoPtr->insertBeforeorAfter)
2625 offset = wineItem->rect.bottom - 1;
2627 offset = wineItem->rect.top + 1;
2629 left = wineItem->textOffset - 2;
2630 right = wineItem->textOffset + wineItem->textWidth + 2;
2632 MoveToEx(hdc, left, offset - 3, NULL);
2633 LineTo(hdc, left, offset + 4);
2635 MoveToEx(hdc, left, offset, NULL);
2636 LineTo(hdc, right + 1, offset);
2638 MoveToEx(hdc, right, offset + 3, NULL);
2639 LineTo(hdc, right, offset - 4);
2641 SelectObject(hdc, hOldPen);
2642 DeleteObject(hNewPen);
2645 if (cditem & CDRF_NOTIFYPOSTPAINT)
2647 cditem = TREEVIEW_SendCustomDrawItemNotify
2648 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2649 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2652 /* Restore the hdc state */
2653 SetTextColor(hdc, oldTextColor);
2654 SetBkColor(hdc, oldTextBkColor);
2655 SelectObject(hdc, hOldFont);
2658 /* Computes treeHeight and treeWidth and updates the scroll bars.
2661 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2663 TREEVIEW_ITEM *wineItem;
2664 HWND hwnd = infoPtr->hwnd;
2668 LONG scrollX = infoPtr->scrollX;
2670 infoPtr->treeWidth = 0;
2671 infoPtr->treeHeight = 0;
2673 /* We iterate through all visible items in order to get the tree height
2675 wineItem = infoPtr->root->firstChild;
2677 while (wineItem != NULL)
2679 if (ISVISIBLE(wineItem))
2681 /* actually we draw text at textOffset + 2 */
2682 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2683 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2685 /* This is scroll-adjusted, but we fix this below. */
2686 infoPtr->treeHeight = wineItem->rect.bottom;
2689 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2692 /* Fix the scroll adjusted treeHeight and treeWidth. */
2693 if (infoPtr->root->firstChild)
2694 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2696 infoPtr->treeWidth += infoPtr->scrollX;
2698 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2700 /* Adding one scroll bar may take up enough space that it forces us
2701 * to add the other as well. */
2702 if (infoPtr->treeHeight > infoPtr->clientHeight)
2706 if (infoPtr->treeWidth
2707 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2710 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2713 if (!vert && horz && infoPtr->treeHeight
2714 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2717 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2719 si.cbSize = sizeof(SCROLLINFO);
2720 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2725 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2726 if ( si.nPage && NULL != infoPtr->firstVisible)
2728 si.nPos = infoPtr->firstVisible->visibleOrder;
2729 si.nMax = infoPtr->maxVisibleOrder - 1;
2731 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2733 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2734 ShowScrollBar(hwnd, SB_VERT, TRUE);
2735 infoPtr->uInternalStatus |= TV_VSCROLL;
2739 if (infoPtr->uInternalStatus & TV_VSCROLL)
2740 ShowScrollBar(hwnd, SB_VERT, FALSE);
2741 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2746 if (infoPtr->uInternalStatus & TV_VSCROLL)
2747 ShowScrollBar(hwnd, SB_VERT, FALSE);
2748 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2753 si.nPage = infoPtr->clientWidth;
2754 si.nPos = infoPtr->scrollX;
2755 si.nMax = infoPtr->treeWidth - 1;
2757 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2759 si.nPos = si.nMax - max( si.nPage-1, 0 );
2763 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2764 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2765 infoPtr->uInternalStatus |= TV_HSCROLL;
2767 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2768 TREEVIEW_HScroll(infoPtr,
2769 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2773 if (infoPtr->uInternalStatus & TV_HSCROLL)
2774 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2775 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2778 if (infoPtr->scrollX != 0)
2780 TREEVIEW_HScroll(infoPtr,
2781 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2786 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2790 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2793 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2795 hBrush = CreateSolidBrush(clrBk);
2796 FillRect(hdc, rc, hBrush);
2797 DeleteObject(hBrush);
2800 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2802 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2806 TRACE("%p\n", infoPtr);
2808 GetClientRect(infoPtr->hwnd, &rect);
2809 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2815 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2817 HWND hwnd = infoPtr->hwnd;
2819 TREEVIEW_ITEM *wineItem;
2821 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2823 TRACE("empty window\n");
2827 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2830 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2832 ReleaseDC(hwnd, hdc);
2836 for (wineItem = infoPtr->root->firstChild;
2838 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2840 if (ISVISIBLE(wineItem))
2842 /* Avoid unneeded calculations */
2843 if (wineItem->rect.top > rect.bottom)
2845 if (wineItem->rect.bottom < rect.top)
2848 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2852 TREEVIEW_UpdateScrollBars(infoPtr);
2854 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2856 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2860 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2862 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2866 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2869 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2871 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2875 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2886 GetClientRect(infoPtr->hwnd, &rc);
2890 hdc = BeginPaint(infoPtr->hwnd, &ps);
2893 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2896 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2897 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2900 EndPaint(infoPtr->hwnd, &ps);
2906 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2908 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2910 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2913 if (options & PRF_ERASEBKGND)
2914 TREEVIEW_EraseBackground(infoPtr, hdc);
2916 if (options & PRF_CLIENT)
2919 GetClientRect(infoPtr->hwnd, &rc);
2920 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2926 /* Sorting **************************************************************/
2928 /***************************************************************************
2929 * Forward the DPA local callback to the treeview owner callback
2932 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2933 const TVSORTCB *pCallBackSort)
2935 /* Forward the call to the client-defined callback */
2936 return pCallBackSort->lpfnCompare(first->lParam,
2938 pCallBackSort->lParam);
2941 /***************************************************************************
2942 * Treeview native sort routine: sort on item text.
2945 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2946 const TREEVIEW_INFO *infoPtr)
2948 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2949 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2951 if(first->pszText && second->pszText)
2952 return lstrcmpiW(first->pszText, second->pszText);
2953 else if(first->pszText)
2955 else if(second->pszText)
2961 /* Returns the number of physical children belonging to item. */
2963 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2968 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2974 /* Returns a DPA containing a pointer to each physical child of item in
2975 * sibling order. If item has no children, an empty DPA is returned. */
2977 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
2979 HTREEITEM child = item->firstChild;
2981 HDPA list = DPA_Create(8);
2982 if (list == 0) return NULL;
2984 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2986 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
2996 /***************************************************************************
2997 * Setup the treeview structure with regards of the sort method
2998 * and sort the children of the TV item specified in lParam
2999 * fRecurse: currently unused. Should be zero.
3000 * parent: if pSort!=NULL, should equal pSort->hParent.
3001 * otherwise, item which child items are to be sorted.
3002 * pSort: sort method info. if NULL, sort on item text.
3003 * if non-NULL, sort on item's lParam content, and let the
3004 * application decide what that means. See also TVM_SORTCHILDRENCB.
3008 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3012 PFNDPACOMPARE pfnCompare;
3015 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3016 if (parent == TVI_ROOT || parent == NULL)
3017 parent = infoPtr->root;
3019 /* Check for a valid handle to the parent item */
3020 if (!TREEVIEW_ValidItem(infoPtr, parent))
3022 ERR("invalid item hParent=%p\n", parent);
3028 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3029 lpCompare = (LPARAM)pSort;
3033 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3034 lpCompare = (LPARAM)infoPtr;
3037 cChildren = TREEVIEW_CountChildren(parent);
3039 /* Make sure there is something to sort */
3042 /* TREEVIEW_ITEM rechaining */
3045 HTREEITEM nextItem = 0;
3046 HTREEITEM prevItem = 0;
3048 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3050 if (sortList == NULL)
3053 /* let DPA sort the list */
3054 DPA_Sort(sortList, pfnCompare, lpCompare);
3056 /* The order of DPA entries has been changed, so fixup the
3057 * nextSibling and prevSibling pointers. */
3059 item = DPA_GetPtr(sortList, count++);
3060 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3062 /* link the two current item together */
3063 item->nextSibling = nextItem;
3064 nextItem->prevSibling = item;
3066 if (prevItem == NULL)
3068 /* this is the first item, update the parent */
3069 parent->firstChild = item;
3070 item->prevSibling = NULL;
3074 /* fix the back chaining */
3075 item->prevSibling = prevItem;
3078 /* get ready for the next one */
3083 /* the last item is pointed to by item and never has a sibling */
3084 item->nextSibling = NULL;
3085 parent->lastChild = item;
3087 DPA_Destroy(sortList);
3089 TREEVIEW_VerifyTree(infoPtr);
3091 if (parent->state & TVIS_EXPANDED)
3093 int visOrder = infoPtr->firstVisible->visibleOrder;
3095 if (parent == infoPtr->root)
3096 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3098 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3100 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3102 TREEVIEW_ITEM *item;
3104 for (item = infoPtr->root->firstChild; item != NULL;
3105 item = TREEVIEW_GetNextListItem(infoPtr, item))
3107 if (item->visibleOrder == visOrder)
3111 if (!item) item = parent->firstChild;
3112 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3115 TREEVIEW_Invalidate(infoPtr, NULL);
3124 /***************************************************************************
3125 * Setup the treeview structure with regards of the sort method
3126 * and sort the children of the TV item specified in lParam
3129 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3131 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3135 /***************************************************************************
3136 * Sort the children of the TV item specified in lParam.
3139 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3141 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3145 /* Expansion/Collapse ***************************************************/
3148 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3151 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3152 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3153 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3158 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3161 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3162 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3163 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3168 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3169 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3171 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3172 BOOL bRemoveChildren, BOOL bUser)
3174 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3175 BOOL bSetSelection, bSetFirstVisible;
3177 LONG scrollDist = 0;
3178 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3180 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3182 if (!(wineItem->state & TVIS_EXPANDED))
3185 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3186 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3188 if (wineItem->firstChild == NULL)
3191 wineItem->state &= ~TVIS_EXPANDED;
3193 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3194 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3196 bSetSelection = (infoPtr->selectedItem != NULL
3197 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3199 bSetFirstVisible = (infoPtr->firstVisible != NULL
3200 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3205 if (tmpItem->nextSibling)
3207 nextItem = tmpItem->nextSibling;
3210 tmpItem = tmpItem->parent;
3214 scrollDist = nextItem->rect.top;
3216 if (bRemoveChildren)
3218 INT old_cChildren = wineItem->cChildren;
3219 TRACE("TVE_COLLAPSERESET\n");
3220 wineItem->state &= ~TVIS_EXPANDEDONCE;
3221 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3222 wineItem->cChildren = old_cChildren;
3225 if (wineItem->firstChild)
3227 TREEVIEW_ITEM *item, *sibling;
3229 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3231 for (item = wineItem->firstChild; item != sibling;
3232 item = TREEVIEW_GetNextListItem(infoPtr, item))
3234 item->visibleOrder = -1;
3238 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3241 scrollDist = -(scrollDist - nextItem->rect.top);
3245 /* Don't call DoSelectItem, it sends notifications. */
3246 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3247 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3248 wineItem->state |= TVIS_SELECTED;
3249 infoPtr->selectedItem = wineItem;
3252 TREEVIEW_UpdateScrollBars(infoPtr);
3254 scrollRect.left = 0;
3255 scrollRect.right = infoPtr->clientWidth;
3256 scrollRect.bottom = infoPtr->clientHeight;
3260 scrollRect.top = nextItem->rect.top;
3262 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3263 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3264 TREEVIEW_Invalidate(infoPtr, wineItem);
3266 scrollRect.top = wineItem->rect.top;
3267 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3270 TREEVIEW_SetFirstVisible(infoPtr,
3271 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3278 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3279 BOOL bExpandPartial, BOOL bUser)
3282 LONG orgNextTop = 0;
3284 TREEVIEW_ITEM *nextItem, *tmpItem;
3288 if (wineItem->state & TVIS_EXPANDED)
3291 tmpItem = wineItem; nextItem = NULL;
3294 if (tmpItem->nextSibling)
3296 nextItem = tmpItem->nextSibling;
3299 tmpItem = tmpItem->parent;
3303 orgNextTop = nextItem->rect.top;
3305 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3307 if (bUser || ((wineItem->cChildren != 0) &&
3308 !(wineItem->state & TVIS_EXPANDEDONCE)))
3310 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3312 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3316 if (!wineItem->firstChild)
3319 wineItem->state |= TVIS_EXPANDED;
3320 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3321 wineItem->state |= TVIS_EXPANDEDONCE;
3325 if (!wineItem->firstChild)
3328 /* this item has already been expanded */
3329 wineItem->state |= TVIS_EXPANDED;
3333 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3335 if (ISVISIBLE(wineItem))
3337 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3338 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3339 TREEVIEW_UpdateScrollBars(infoPtr);
3341 scrollRect.left = 0;
3342 scrollRect.bottom = infoPtr->treeHeight;
3343 scrollRect.right = infoPtr->clientWidth;
3346 scrollDist = nextItem->rect.top - orgNextTop;
3347 scrollRect.top = orgNextTop;
3349 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3350 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3351 TREEVIEW_Invalidate (infoPtr, wineItem);
3353 scrollRect.top = wineItem->rect.top;
3354 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3357 /* Scroll up so that as many children as possible are visible.
3358 * This fails when expanding causes an HScroll bar to appear, but we
3359 * don't know that yet, so the last item is obscured. */
3360 if (wineItem->firstChild != NULL)
3362 int nChildren = wineItem->lastChild->visibleOrder
3363 - wineItem->firstChild->visibleOrder + 1;
3365 int visible_pos = wineItem->visibleOrder
3366 - infoPtr->firstVisible->visibleOrder;
3368 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3370 if (visible_pos > 0 && nChildren > rows_below)
3372 int scroll = nChildren - rows_below;
3374 if (scroll > visible_pos)
3375 scroll = visible_pos;
3379 TREEVIEW_ITEM *newFirstVisible
3380 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3384 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3394 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3398 if (wineItem->state & TVIS_EXPANDED)
3399 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3401 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3405 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3407 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3409 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3411 if (TREEVIEW_HasChildren(infoPtr, item))
3412 TREEVIEW_ExpandAll(infoPtr, item);
3416 /* Note:If the specified item is the child of a collapsed parent item,
3417 the parent's list of child items is (recursively) expanded to reveal the
3418 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3419 know if it also applies here.
3423 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3425 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3428 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3429 TREEVIEW_ItemName(wineItem), flag,
3430 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3432 switch (flag & TVE_TOGGLE)
3435 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3439 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3443 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3450 /* Hit-Testing **********************************************************/
3452 static TREEVIEW_ITEM *
3453 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3455 TREEVIEW_ITEM *wineItem;
3458 if (!infoPtr->firstVisible)
3461 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3463 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3464 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3466 if (row >= wineItem->visibleOrder
3467 && row < wineItem->visibleOrder + wineItem->iIntegral)
3475 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3477 TREEVIEW_ITEM *wineItem;
3483 GetClientRect(infoPtr->hwnd, &rect);
3490 status |= TVHT_TOLEFT;
3492 else if (x > rect.right)
3494 status |= TVHT_TORIGHT;
3499 status |= TVHT_ABOVE;
3501 else if (y > rect.bottom)
3503 status |= TVHT_BELOW;
3508 lpht->flags = status;
3512 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3515 lpht->flags = TVHT_NOWHERE;
3519 if (x >= wineItem->textOffset + wineItem->textWidth)
3521 lpht->flags = TVHT_ONITEMRIGHT;
3523 else if (x >= wineItem->textOffset)
3525 lpht->flags = TVHT_ONITEMLABEL;
3527 else if (x >= wineItem->imageOffset)
3529 lpht->flags = TVHT_ONITEMICON;
3531 else if (x >= wineItem->stateOffset)
3533 lpht->flags = TVHT_ONITEMSTATEICON;
3535 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3537 lpht->flags = TVHT_ONITEMBUTTON;
3541 lpht->flags = TVHT_ONITEMINDENT;
3544 lpht->hItem = wineItem;
3545 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3547 return (LRESULT)wineItem;
3550 /* Item Label Editing ***************************************************/
3553 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3555 return (LRESULT)infoPtr->hwndEdit;
3558 static LRESULT CALLBACK
3559 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3561 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3562 BOOL bCancel = FALSE;
3568 TRACE("WM_PAINT start\n");
3569 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3571 TRACE("WM_PAINT done\n");
3575 if (infoPtr->bIgnoreEditKillFocus)
3581 WNDPROC editProc = infoPtr->wpEditOrig;
3582 infoPtr->wpEditOrig = 0;
3583 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3584 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3588 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3591 if (wParam == VK_ESCAPE)
3596 else if (wParam == VK_RETURN)
3603 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3606 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3607 /* eg. Using a messagebox */
3609 infoPtr->bIgnoreEditKillFocus = TRUE;
3610 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3611 infoPtr->bIgnoreEditKillFocus = FALSE;
3617 /* should handle edit control messages here */
3620 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3622 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3624 switch (HIWORD(wParam))
3629 * Adjust the edit window size
3632 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3633 HDC hdc = GetDC(infoPtr->hwndEdit);
3635 HFONT hFont, hOldFont = 0;
3637 TRACE("edit=%p\n", infoPtr->hwndEdit);
3639 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3641 infoPtr->bLabelChanged = TRUE;
3643 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3645 /* Select font to get the right dimension of the string */
3646 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3650 hOldFont = SelectObject(hdc, hFont);
3653 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3655 TEXTMETRICW textMetric;
3657 /* Add Extra spacing for the next character */
3658 GetTextMetricsW(hdc, &textMetric);
3659 sz.cx += (textMetric.tmMaxCharWidth * 2);
3661 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3663 infoPtr->clientWidth - editItem->textOffset + 2);
3665 SetWindowPos(infoPtr->hwndEdit,
3670 editItem->rect.bottom - editItem->rect.top + 3,
3671 SWP_NOMOVE | SWP_DRAWFRAME);
3676 SelectObject(hdc, hOldFont);
3679 ReleaseDC(infoPtr->hwnd, hdc);
3683 /* apparently we should respect passed handle value */
3684 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3686 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3690 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3697 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3699 HWND hwnd = infoPtr->hwnd;
3702 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3705 TEXTMETRICW textMetric;
3707 TRACE("%p %p\n", hwnd, hItem);
3708 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3711 if (infoPtr->hwndEdit)
3712 return infoPtr->hwndEdit;
3714 infoPtr->bLabelChanged = FALSE;
3716 /* make edit item visible */
3717 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3719 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3722 /* Select the font to get appropriate metric dimensions */
3723 if (infoPtr->hFont != 0)
3725 hOldFont = SelectObject(hdc, infoPtr->hFont);
3728 /* Get string length in pixels */
3730 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3733 GetTextExtentPoint32A(hdc, "", 0, &sz);
3735 /* Add Extra spacing for the next character */
3736 GetTextMetricsW(hdc, &textMetric);
3737 sz.cx += (textMetric.tmMaxCharWidth * 2);
3739 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3740 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3742 if (infoPtr->hFont != 0)
3744 SelectObject(hdc, hOldFont);
3747 ReleaseDC(hwnd, hdc);
3749 infoPtr->editItem = hItem;
3751 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3754 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3755 WS_CLIPSIBLINGS | ES_WANTRETURN |
3756 ES_LEFT, hItem->textOffset - 2,
3757 hItem->rect.top - 1, sz.cx + 3,
3758 hItem->rect.bottom -
3759 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3760 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3762 infoPtr->hwndEdit = hwndEdit;
3764 /* Get a 2D border. */
3765 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3766 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3767 SetWindowLongW(hwndEdit, GWL_STYLE,
3768 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3770 SendMessageW(hwndEdit, WM_SETFONT,
3771 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3773 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3775 TREEVIEW_Edit_SubclassProc);
3777 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3779 DestroyWindow(hwndEdit);
3780 infoPtr->hwndEdit = 0;
3781 infoPtr->editItem = NULL;
3786 SetWindowTextW(hwndEdit, hItem->pszText);
3789 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3790 ShowWindow(hwndEdit, SW_SHOW);
3797 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3799 HWND hwnd = infoPtr->hwnd;
3800 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3803 WCHAR tmpText[1024] = { '\0' };
3804 WCHAR *newText = tmpText;
3807 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3809 tvdi.hdr.hwndFrom = hwnd;
3810 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3811 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3813 tvdi.item.hItem = editedItem;
3814 tvdi.item.state = editedItem->state;
3815 tvdi.item.lParam = editedItem->lParam;
3819 if (!infoPtr->bNtfUnicode)
3820 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3822 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3824 if (iLength >= 1023)
3826 ERR("Insufficient space to retrieve new item label\n");
3829 tvdi.item.mask = TVIF_TEXT;
3830 tvdi.item.pszText = tmpText;
3831 tvdi.item.cchTextMax = iLength + 1;
3835 tvdi.item.pszText = NULL;
3836 tvdi.item.cchTextMax = 0;
3839 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3841 if (!bCancel && bCommit) /* Apply the changes */
3843 if (!infoPtr->bNtfUnicode)
3845 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3846 newText = Alloc(len * sizeof(WCHAR));
3847 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3851 if (strcmpW(newText, editedItem->pszText) != 0)
3853 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3856 ERR("OutOfMemory, cannot allocate space for label\n");
3857 if(newText != tmpText) Free(newText);
3858 DestroyWindow(infoPtr->hwndEdit);
3859 infoPtr->hwndEdit = 0;
3860 infoPtr->editItem = NULL;
3865 editedItem->pszText = ptr;
3866 editedItem->cchTextMax = iLength + 1;
3867 strcpyW(editedItem->pszText, newText);
3868 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3871 if(newText != tmpText) Free(newText);
3874 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3875 DestroyWindow(infoPtr->hwndEdit);
3876 infoPtr->hwndEdit = 0;
3877 infoPtr->editItem = NULL;
3882 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3884 if (wParam != TV_EDIT_TIMER)
3886 ERR("got unknown timer\n");
3890 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3891 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3893 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3899 /* Mouse Tracking/Drag **************************************************/
3901 /***************************************************************************
3902 * This is quite unusual piece of code, but that's how it's implemented in
3906 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3908 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3909 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3913 r.top = pt.y - cyDrag;
3914 r.left = pt.x - cxDrag;
3915 r.bottom = pt.y + cyDrag;
3916 r.right = pt.x + cxDrag;
3918 SetCapture(infoPtr->hwnd);
3922 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3924 if (msg.message == WM_MOUSEMOVE)
3926 pt.x = (short)LOWORD(msg.lParam);
3927 pt.y = (short)HIWORD(msg.lParam);
3928 if (PtInRect(&r, pt))
3936 else if (msg.message >= WM_LBUTTONDOWN &&
3937 msg.message <= WM_RBUTTONDBLCLK)
3939 if (msg.message == WM_RBUTTONUP)
3940 TREEVIEW_RButtonUp(infoPtr, &pt);
3944 DispatchMessageW(&msg);
3947 if (GetCapture() != infoPtr->hwnd)
3957 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3959 TREEVIEW_ITEM *wineItem;
3963 SetFocus(infoPtr->hwnd);
3965 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3967 /* If there is pending 'edit label' event - kill it now */
3968 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3971 hit.pt.x = (short)LOWORD(lParam);
3972 hit.pt.y = (short)HIWORD(lParam);
3974 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3977 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3979 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3983 case TVHT_ONITEMRIGHT:
3984 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3987 case TVHT_ONITEMINDENT:
3988 if (!(infoPtr->dwStyle & TVS_HASLINES))
3994 int level = hit.pt.x / infoPtr->uIndent;
3995 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
3997 while (wineItem->iLevel > level)
3999 wineItem = wineItem->parent;
4005 case TVHT_ONITEMLABEL:
4006 case TVHT_ONITEMICON:
4007 case TVHT_ONITEMBUTTON:
4008 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4011 case TVHT_ONITEMSTATEICON:
4012 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4013 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4015 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4024 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4026 HWND hwnd = infoPtr->hwnd;
4028 BOOL bTrack, bDoLabelEdit;
4030 /* If Edit control is active - kill it and return.
4031 * The best way to do it is to set focus to itself.
4032 * Edit control subclassed procedure will automatically call
4035 if (infoPtr->hwndEdit)
4041 ht.pt.x = (short)LOWORD(lParam);
4042 ht.pt.y = (short)HIWORD(lParam);
4044 TREEVIEW_HitTest(infoPtr, &ht);
4045 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4047 /* update focusedItem and redraw both items */
4048 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4050 infoPtr->focusedItem = ht.hItem;
4051 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4052 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4055 bTrack = (ht.flags & TVHT_ONITEM)
4056 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4059 * If the style allows editing and the node is already selected
4060 * and the click occurred on the item label...
4062 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4063 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4065 /* Send NM_CLICK right away */
4067 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4070 if (ht.flags & TVHT_ONITEMBUTTON)
4072 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4076 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4077 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4079 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4080 infoPtr->dropItem = ht.hItem;
4082 /* clean up focusedItem as we dragged and won't select this item */
4083 if(infoPtr->focusedItem)
4085 /* refresh the item that was focused */
4086 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4087 infoPtr->focusedItem = NULL;
4089 /* refresh the selected item to return the filled background */
4090 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4097 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4102 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4103 KillTimer(hwnd, TV_EDIT_TIMER);
4105 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4106 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4108 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4111 * if we are TVS_SINGLEEXPAND then we want this single click to
4112 * do a bunch of things.
4114 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4115 (infoPtr->hwndEdit == 0))
4117 TREEVIEW_ITEM *SelItem;
4120 * Send the notification
4122 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4125 * Close the previous selection all the way to the root
4126 * as long as the new selection is not a child
4128 if((infoPtr->selectedItem)
4129 && (infoPtr->selectedItem != ht.hItem))
4131 BOOL closeit = TRUE;
4134 /* determine if the hitItem is a child of the currently selected item */
4135 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4137 closeit = (SelItem != infoPtr->selectedItem);
4138 SelItem = SelItem->parent;
4143 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4144 SelItem = infoPtr->selectedItem;
4146 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4148 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4149 SelItem = SelItem->parent;
4155 * Expand the current item
4157 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4160 /* Select the current item */
4161 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4163 else if (ht.flags & TVHT_ONITEMSTATEICON)
4165 /* TVS_CHECKBOXES requires us to toggle the current state */
4166 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4167 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4177 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4181 if (infoPtr->hwndEdit)
4183 SetFocus(infoPtr->hwnd);
4187 ht.pt.x = (short)LOWORD(lParam);
4188 ht.pt.y = (short)HIWORD(lParam);
4190 TREEVIEW_HitTest(infoPtr, &ht);
4192 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4196 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4197 infoPtr->dropItem = ht.hItem;
4202 SetFocus(infoPtr->hwnd);
4203 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4210 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4216 TREEVIEW_HitTest(infoPtr, &ht);
4220 /* Change to screen coordinate for WM_CONTEXTMENU */
4221 ClientToScreen(infoPtr->hwnd, &ht.pt);
4223 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4224 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4225 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4232 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4234 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4238 HBITMAP hbmp, hOldbmp;
4245 if (!(infoPtr->himlNormal))
4248 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4251 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4253 hwtop = GetDesktopWindow();
4254 htopdc = GetDC(hwtop);
4255 hdc = CreateCompatibleDC(htopdc);
4257 hOldFont = SelectObject(hdc, infoPtr->hFont);
4259 if (dragItem->pszText)
4260 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4263 GetTextExtentPoint32A(hdc, "", 0, &size);
4265 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4266 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4267 hOldbmp = SelectObject(hdc, hbmp);
4269 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4274 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4275 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4279 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4280 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4283 /* draw item text */
4285 SetRect(&rc, cx, 0, size.cx, size.cy);
4287 if (dragItem->pszText)
4288 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4291 SelectObject(hdc, hOldFont);
4292 SelectObject(hdc, hOldbmp);
4294 ImageList_Add(infoPtr->dragList, hbmp, 0);
4298 ReleaseDC(hwtop, htopdc);
4300 return (LRESULT)infoPtr->dragList;
4303 /* Selection ************************************************************/
4306 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4309 TREEVIEW_ITEM *prevSelect;
4311 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4313 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4314 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4315 newSelect ? newSelect->state : 0);
4317 /* reset and redraw focusedItem if focusedItem was set so we don't */
4318 /* have to worry about the previously focused item when we set a new one */
4319 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4320 infoPtr->focusedItem = NULL;
4325 prevSelect = infoPtr->selectedItem;
4327 if (prevSelect == newSelect) {
4328 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4332 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4335 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4341 prevSelect->state &= ~TVIS_SELECTED;
4343 newSelect->state |= TVIS_SELECTED;
4345 infoPtr->selectedItem = newSelect;
4347 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4349 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4350 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4352 TREEVIEW_SendTreeviewNotify(infoPtr,
4355 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4360 case TVGN_DROPHILITE:
4361 prevSelect = infoPtr->dropItem;
4364 prevSelect->state &= ~TVIS_DROPHILITED;
4366 infoPtr->dropItem = newSelect;
4369 newSelect->state |= TVIS_DROPHILITED;
4371 TREEVIEW_Invalidate(infoPtr, prevSelect);
4372 TREEVIEW_Invalidate(infoPtr, newSelect);
4375 case TVGN_FIRSTVISIBLE:
4376 if (newSelect != NULL)
4378 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4379 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4380 TREEVIEW_Invalidate(infoPtr, NULL);
4385 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4389 /* FIXME: handle NM_KILLFOCUS etc */
4391 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4393 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4396 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4398 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4404 /*************************************************************************
4405 * TREEVIEW_ProcessLetterKeys
4407 * Processes keyboard messages generated by pressing the letter keys
4409 * What this does is perform a case insensitive search from the
4410 * current position with the following quirks:
4411 * - If two chars or more are pressed in quick succession we search
4412 * for the corresponding string (e.g. 'abc').
4413 * - If there is a delay we wipe away the current search string and
4414 * restart with just that char.
4415 * - If the user keeps pressing the same character, whether slowly or
4416 * fast, so that the search string is entirely composed of this
4417 * character ('aaaaa' for instance), then we search for first item
4418 * that starting with that character.
4419 * - If the user types the above character in quick succession, then
4420 * we must also search for the corresponding string ('aaaaa'), and
4421 * go to that string if there is a match.
4429 * - The current implementation has a list of characters it will
4430 * accept and it ignores everything else. In particular it will
4431 * ignore accentuated characters which seems to match what
4432 * Windows does. But I'm not sure it makes sense to follow
4434 * - We don't sound a beep when the search fails.
4435 * - The search should start from the focused item, not from the selected
4436 * item. One reason for this is to allow for multiple selections in trees.
4437 * But currently infoPtr->focusedItem does not seem very usable.
4441 * TREEVIEW_ProcessLetterKeys
4443 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4446 HTREEITEM endidx,idx;
4448 WCHAR buffer[MAX_PATH];
4449 DWORD timestamp,elapsed;
4451 /* simple parameter checking */
4452 if (!charCode || !keyData) return 0;
4454 /* only allow the valid WM_CHARs through */
4455 if (!isalnum(charCode) &&
4456 charCode != '.' && charCode != '`' && charCode != '!' &&
4457 charCode != '@' && charCode != '#' && charCode != '$' &&
4458 charCode != '%' && charCode != '^' && charCode != '&' &&
4459 charCode != '*' && charCode != '(' && charCode != ')' &&
4460 charCode != '-' && charCode != '_' && charCode != '+' &&
4461 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4462 charCode != '}' && charCode != '[' && charCode != '{' &&
4463 charCode != '/' && charCode != '?' && charCode != '>' &&
4464 charCode != '<' && charCode != ',' && charCode != '~')
4467 /* compute how much time elapsed since last keypress */
4468 timestamp = GetTickCount();
4469 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4470 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4472 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4475 /* update the search parameters */
4476 infoPtr->lastKeyPressTimestamp=timestamp;
4477 if (elapsed < KEY_DELAY) {
4478 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4479 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4481 if (infoPtr->charCode != charCode) {
4482 infoPtr->charCode=charCode=0;
4485 infoPtr->charCode=charCode;
4486 infoPtr->szSearchParam[0]=charCode;
4487 infoPtr->nSearchParamLength=1;
4488 /* Redundant with the 1 char string */
4492 /* and search from the current position */
4494 if (infoPtr->selectedItem != NULL) {
4495 endidx=infoPtr->selectedItem;
4496 /* if looking for single character match,
4497 * then we must always move forward
4499 if (infoPtr->nSearchParamLength == 1)
4500 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4505 idx=infoPtr->root->firstChild;
4508 /* At the end point, sort out wrapping */
4511 /* If endidx is null, stop at the last item (ie top to bottom) */
4515 /* Otherwise, start again at the very beginning */
4516 idx=infoPtr->root->firstChild;
4518 /* But if we are stopping on the first child, end now! */
4519 if (idx == endidx) break;
4523 ZeroMemory(&item, sizeof(item));
4524 item.mask = TVIF_TEXT;
4526 item.pszText = buffer;
4527 item.cchTextMax = sizeof(buffer);
4528 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4530 /* check for a match */
4531 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4534 } else if ( (charCode != 0) && (nItem == NULL) &&
4535 (nItem != infoPtr->selectedItem) &&
4536 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4537 /* This would work but we must keep looking for a longer match */
4540 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4541 } while (idx != endidx);
4543 if (nItem != NULL) {
4544 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4545 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4552 /* Scrolling ************************************************************/
4555 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4558 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4559 HTREEITEM newFirstVisible = NULL;
4560 int visible_pos = -1;
4562 if (!TREEVIEW_ValidItem(infoPtr, item))
4565 if (!ISVISIBLE(item))
4567 /* Expand parents as necessary. */
4570 /* see if we are trying to ensure that root is visible */
4571 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4572 parent = item->parent;
4574 parent = item; /* this item is the topmost item */
4576 while (parent != infoPtr->root)
4578 if (!(parent->state & TVIS_EXPANDED))
4579 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4581 parent = parent->parent;
4585 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4587 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4588 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4590 if (hasFirstVisible)
4591 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4593 if (visible_pos < 0)
4595 /* item is before the start of the list: put it at the top. */
4596 newFirstVisible = item;
4598 else if (visible_pos >= viscount
4599 /* Sometimes, before we are displayed, GVC is 0, causing us to
4600 * spuriously scroll up. */
4601 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4603 /* item is past the end of the list. */
4604 int scroll = visible_pos - viscount;
4606 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4612 /* Scroll window so item's text is visible as much as possible */
4613 /* Calculation of amount of extra space is taken from EditLabel code */
4615 TEXTMETRICW textMetric;
4616 HDC hdc = GetWindowDC(infoPtr->hwnd);
4618 x = item->textWidth;
4620 GetTextMetricsW(hdc, &textMetric);
4621 ReleaseDC(infoPtr->hwnd, hdc);
4623 x += (textMetric.tmMaxCharWidth * 2);
4624 x = max(x, textMetric.tmMaxCharWidth * 3);
4626 if (item->textOffset < 0)
4627 pos = item->textOffset;
4628 else if (item->textOffset + x > infoPtr->clientWidth)
4630 if (x > infoPtr->clientWidth)
4631 pos = item->textOffset;
4633 pos = item->textOffset + x - infoPtr->clientWidth;
4638 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4641 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4643 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4652 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4653 TREEVIEW_ITEM *newFirstVisible,
4654 BOOL bUpdateScrollPos)
4658 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4660 if (newFirstVisible != NULL)
4662 /* Prevent an empty gap from appearing at the bottom... */
4663 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4664 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4668 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4671 /* ... unless we just don't have enough items. */
4672 if (newFirstVisible == NULL)
4673 newFirstVisible = infoPtr->root->firstChild;
4677 if (infoPtr->firstVisible != newFirstVisible)
4679 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4681 infoPtr->firstVisible = newFirstVisible;
4682 TREEVIEW_Invalidate(infoPtr, NULL);
4686 TREEVIEW_ITEM *item;
4687 int scroll = infoPtr->uItemHeight *
4688 (infoPtr->firstVisible->visibleOrder
4689 - newFirstVisible->visibleOrder);
4691 infoPtr->firstVisible = newFirstVisible;
4693 for (item = infoPtr->root->firstChild; item != NULL;
4694 item = TREEVIEW_GetNextListItem(infoPtr, item))
4696 item->rect.top += scroll;
4697 item->rect.bottom += scroll;
4700 if (bUpdateScrollPos)
4701 SetScrollPos(infoPtr->hwnd, SB_VERT,
4702 newFirstVisible->visibleOrder, TRUE);
4704 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4709 /************************************************************************
4710 * VScroll is always in units of visible items. i.e. we always have a
4711 * visible item aligned to the top of the control. (Unless we have no
4715 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4717 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4718 TREEVIEW_ITEM *newFirstVisible = NULL;
4720 int nScrollCode = LOWORD(wParam);
4722 TRACE("wp %lx\n", wParam);
4724 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4727 if (!oldFirstVisible)
4729 assert(infoPtr->root->firstChild == NULL);
4733 switch (nScrollCode)
4736 newFirstVisible = infoPtr->root->firstChild;
4740 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4744 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4748 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4752 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4753 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4757 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4758 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4762 case SB_THUMBPOSITION:
4763 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4764 infoPtr->root->firstChild,
4765 (LONG)(SHORT)HIWORD(wParam));
4772 if (newFirstVisible != NULL)
4774 if (newFirstVisible != oldFirstVisible)
4775 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4776 nScrollCode != SB_THUMBTRACK);
4777 else if (nScrollCode == SB_THUMBPOSITION)
4778 SetScrollPos(infoPtr->hwnd, SB_VERT,
4779 newFirstVisible->visibleOrder, TRUE);
4786 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4789 int scrollX = infoPtr->scrollX;
4790 int nScrollCode = LOWORD(wParam);
4792 TRACE("wp %lx\n", wParam);
4794 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4797 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4798 /* shall never occur */
4805 switch (nScrollCode)
4808 scrollX -= infoPtr->uItemHeight;
4811 scrollX += infoPtr->uItemHeight;
4814 scrollX -= infoPtr->clientWidth;
4817 scrollX += infoPtr->clientWidth;
4821 case SB_THUMBPOSITION:
4822 scrollX = (int)(SHORT)HIWORD(wParam);
4829 if (scrollX > maxWidth)
4831 else if (scrollX < 0)
4835 if (scrollX != infoPtr->scrollX)
4837 TREEVIEW_ITEM *item;
4838 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4840 for (item = infoPtr->root->firstChild; item != NULL;
4841 item = TREEVIEW_GetNextListItem(infoPtr, item))
4843 item->linesOffset += scroll_pixels;
4844 item->stateOffset += scroll_pixels;
4845 item->imageOffset += scroll_pixels;
4846 item->textOffset += scroll_pixels;
4849 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4850 infoPtr->scrollX = scrollX;
4851 UpdateWindow(infoPtr->hwnd);
4854 if (nScrollCode != SB_THUMBTRACK)
4855 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4861 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4864 UINT pulScrollLines = 3;
4866 if (wParam & (MK_SHIFT | MK_CONTROL))
4867 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4869 if (infoPtr->firstVisible == NULL)
4872 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4874 gcWheelDelta = -(short)HIWORD(wParam);
4875 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4877 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4879 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4880 int maxDy = infoPtr->maxVisibleOrder;
4888 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4893 /* Create/Destroy *******************************************************/
4896 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4899 HBITMAP hbm, hbmOld;
4903 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4905 hdcScreen = GetDC(0);
4907 hdc = CreateCompatibleDC(hdcScreen);
4908 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4909 hbmOld = SelectObject(hdc, hbm);
4911 SetRect(&rc, 0, 0, 48, 16);
4912 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4914 SetRect(&rc, 18, 2, 30, 14);
4915 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4916 DFCS_BUTTONCHECK|DFCS_FLAT);
4918 SetRect(&rc, 34, 2, 46, 14);
4919 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4920 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4922 SelectObject(hdc, hbmOld);
4923 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4924 comctl32_color.clrWindow);
4925 TRACE("checkbox index %d\n", nIndex);
4929 ReleaseDC(0, hdcScreen);
4931 infoPtr->stateImageWidth = 16;
4932 infoPtr->stateImageHeight = 16;
4936 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4939 TREEVIEW_INFO *infoPtr;
4942 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4944 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4946 if (infoPtr == NULL)
4948 ERR("could not allocate info memory!\n");
4952 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4954 infoPtr->hwnd = hwnd;
4955 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4957 infoPtr->uNumItems = 0;
4958 infoPtr->cdmode = 0;
4959 infoPtr->uScrollTime = 300; /* milliseconds */
4960 infoPtr->bRedraw = TRUE;
4962 GetClientRect(hwnd, &rcClient);
4964 /* No scroll bars yet. */
4965 infoPtr->clientWidth = rcClient.right;
4966 infoPtr->clientHeight = rcClient.bottom;
4967 infoPtr->uInternalStatus = 0;
4969 infoPtr->treeWidth = 0;
4970 infoPtr->treeHeight = 0;
4972 infoPtr->uIndent = MINIMUM_INDENT;
4973 infoPtr->selectedItem = NULL;
4974 infoPtr->focusedItem = NULL;
4975 infoPtr->hotItem = NULL;
4976 infoPtr->editItem = NULL;
4977 infoPtr->firstVisible = NULL;
4978 infoPtr->maxVisibleOrder = 0;
4979 infoPtr->dropItem = NULL;
4980 infoPtr->insertMarkItem = NULL;
4981 infoPtr->insertBeforeorAfter = 0;
4984 infoPtr->scrollX = 0;
4986 infoPtr->clrBk = CLR_NONE; /* use system color */
4987 infoPtr->clrText = CLR_NONE; /* use system color */
4988 infoPtr->clrLine = CLR_DEFAULT;
4989 infoPtr->clrInsertMark = CLR_DEFAULT;
4993 infoPtr->hwndEdit = NULL;
4994 infoPtr->wpEditOrig = NULL;
4995 infoPtr->bIgnoreEditKillFocus = FALSE;
4996 infoPtr->bLabelChanged = FALSE;
4998 infoPtr->himlNormal = NULL;
4999 infoPtr->himlState = NULL;
5000 infoPtr->normalImageWidth = 0;
5001 infoPtr->normalImageHeight = 0;
5002 infoPtr->stateImageWidth = 0;
5003 infoPtr->stateImageHeight = 0;
5005 infoPtr->items = DPA_Create(16);
5007 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5008 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5009 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5010 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5011 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5013 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5015 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5016 infoPtr->root->state = TVIS_EXPANDED;
5017 infoPtr->root->iLevel = -1;
5018 infoPtr->root->visibleOrder = -1;
5020 infoPtr->hwndNotify = lpcs->hwndParent;
5021 infoPtr->hwndToolTip = 0;
5023 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5025 /* Determine what type of notify should be issued */
5026 /* sets infoPtr->bNtfUnicode */
5027 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5029 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5030 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5031 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5034 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5035 initialize_checkboxes(infoPtr);
5037 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5038 ShowScrollBar(hwnd, SB_VERT, FALSE);
5039 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5041 OpenThemeData (hwnd, themeClass);
5048 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5052 /* free item data */
5053 TREEVIEW_RemoveTree(infoPtr);
5054 /* root isn't freed with other items */
5055 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5056 DPA_Destroy(infoPtr->items);
5058 /* tool tip is automatically destroyed: we are its owner */
5060 /* Restore original wndproc */
5061 if (infoPtr->hwndEdit)
5062 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5063 (DWORD_PTR)infoPtr->wpEditOrig);
5065 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5067 /* Deassociate treeview from the window before doing anything drastic. */
5068 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5071 DeleteObject(infoPtr->hDefaultFont);
5072 DeleteObject(infoPtr->hBoldFont);
5073 DeleteObject(infoPtr->hUnderlineFont);
5079 /* Miscellaneous Messages ***********************************************/
5082 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5090 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5091 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5092 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5093 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5094 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5095 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5096 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5097 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5098 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5102 if (key >= VK_PRIOR && key <= VK_DOWN)
5104 unsigned char code = scroll[key - VK_PRIOR].code;
5106 (((code & (1 << 7)) == (SB_HORZ << 7))
5108 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5114 /************************************************************************
5117 * VK_UP Move selection to the previous non-hidden item.
5118 * VK_DOWN Move selection to the next non-hidden item.
5119 * VK_HOME Move selection to the first item.
5120 * VK_END Move selection to the last item.
5121 * VK_LEFT If expanded then collapse, otherwise move to parent.
5122 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5124 * VK_SUBTRACT Collapse.
5125 * VK_MULTIPLY Expand all.
5126 * VK_PRIOR Move up GetVisibleCount items.
5127 * VK_NEXT Move down GetVisibleCount items.
5128 * VK_BACK Move to parent.
5129 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5132 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5134 /* If it is non-NULL and different, it will be selected and visible. */
5135 TREEVIEW_ITEM *newSelection = NULL;
5137 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5139 TRACE("%lx\n", wParam);
5141 if (prevItem == NULL)
5144 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5145 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5150 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5152 newSelection = infoPtr->root->firstChild;
5156 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5160 newSelection = infoPtr->root->firstChild;
5164 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5168 if (prevItem->state & TVIS_EXPANDED)
5170 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5172 else if (prevItem->parent != infoPtr->root)
5174 newSelection = prevItem->parent;
5179 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5181 if (!(prevItem->state & TVIS_EXPANDED))
5182 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5185 newSelection = prevItem->firstChild;
5192 TREEVIEW_ExpandAll(infoPtr, prevItem);
5196 if (!(prevItem->state & TVIS_EXPANDED))
5197 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5201 if (prevItem->state & TVIS_EXPANDED)
5202 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5207 = TREEVIEW_GetListItem(infoPtr, prevItem,
5208 -TREEVIEW_GetVisibleCount(infoPtr));
5213 = TREEVIEW_GetListItem(infoPtr, prevItem,
5214 TREEVIEW_GetVisibleCount(infoPtr));
5218 newSelection = prevItem->parent;
5219 if (newSelection == infoPtr->root)
5220 newSelection = NULL;
5224 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5225 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5229 if (newSelection && newSelection != prevItem)
5231 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5234 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5242 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5244 /* remove hot effect from item */
5245 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5246 infoPtr->hotItem = NULL;
5252 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5255 TRACKMOUSEEVENT trackinfo;
5256 TREEVIEW_ITEM * item;
5258 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5260 /* fill in the TRACKMOUSEEVENT struct */
5261 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5262 trackinfo.dwFlags = TME_QUERY;
5263 trackinfo.hwndTrack = infoPtr->hwnd;
5265 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5266 _TrackMouseEvent(&trackinfo);
5268 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5269 if(!(trackinfo.dwFlags & TME_LEAVE))
5271 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5272 trackinfo.hwndTrack = infoPtr->hwnd;
5273 /* do it as fast as possible, minimal systimer latency will be used */
5274 trackinfo.dwHoverTime = 1;
5276 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5277 /* and can properly deactivate the hot item */
5278 _TrackMouseEvent(&trackinfo);
5281 pt.x = (short)LOWORD(lParam);
5282 pt.y = (short)HIWORD(lParam);
5284 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5286 if (item != infoPtr->hotItem)
5288 /* redraw old hot item */
5289 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5290 infoPtr->hotItem = item;
5291 /* redraw new hot item */
5292 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5298 /* Draw themed border */
5299 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5301 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5305 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5306 cyEdge = GetSystemMetrics (SM_CYEDGE);
5309 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5311 GetWindowRect(infoPtr->hwnd, &r);
5313 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5314 r.right - cxEdge, r.bottom - cyEdge);
5315 if (region != (HRGN)1)
5316 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5317 OffsetRect(&r, -r.left, -r.top);
5319 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5320 OffsetRect(&r, -r.left, -r.top);
5322 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5323 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5324 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5325 ReleaseDC(infoPtr->hwnd, dc);
5327 /* Call default proc to get the scrollbars etc. painted */
5328 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5334 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5336 LPNMHDR lpnmh = (LPNMHDR)lParam;
5338 if (lpnmh->code == PGN_CALCSIZE) {
5339 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5341 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5342 lppgc->iWidth = infoPtr->treeWidth;
5343 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5344 infoPtr->treeWidth, infoPtr->clientWidth);
5347 lppgc->iHeight = infoPtr->treeHeight;
5348 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5349 infoPtr->treeHeight, infoPtr->clientHeight);
5353 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5357 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5359 if (wParam == SIZE_RESTORED)
5361 infoPtr->clientWidth = (short)LOWORD(lParam);
5362 infoPtr->clientHeight = (short)HIWORD(lParam);
5364 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5365 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5366 TREEVIEW_UpdateScrollBars(infoPtr);
5370 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5373 TREEVIEW_Invalidate(infoPtr, NULL);
5378 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5380 TRACE("(%lx %lx)\n", wParam, lParam);
5382 if (wParam == GWL_STYLE)
5384 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5386 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5388 if (dwNewStyle & TVS_CHECKBOXES)
5390 initialize_checkboxes(infoPtr);
5391 TRACE("checkboxes enabled\n");
5395 FIXME("tried to disable checkboxes\n");
5399 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5401 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5403 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5404 TRACE("tooltips enabled\n");
5408 DestroyWindow(infoPtr->hwndToolTip);
5409 infoPtr->hwndToolTip = 0;
5410 TRACE("tooltips disabled\n");
5414 infoPtr->dwStyle = dwNewStyle;
5417 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5418 TREEVIEW_UpdateScrollBars(infoPtr);
5419 TREEVIEW_Invalidate(infoPtr, NULL);
5425 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5428 TREEVIEW_ITEM * item;
5432 ScreenToClient(infoPtr->hwnd, &pt);
5434 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5436 memset(&nmmouse, 0, sizeof(nmmouse));
5437 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5438 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5439 nmmouse.hdr.code = NM_SETCURSOR;
5442 nmmouse.dwItemSpec = (DWORD_PTR)item;
5443 nmmouse.dwItemData = item->lParam;
5447 nmmouse.dwHitInfo = lParam;
5448 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, (LPARAM)&nmmouse))
5451 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5453 SetCursor(infoPtr->hcurHand);
5457 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5461 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5465 if (!infoPtr->selectedItem)
5467 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5471 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5472 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5477 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5481 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5482 UpdateWindow(infoPtr->hwnd);
5483 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5487 /* update theme after a WM_THEMECHANGED message */
5488 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5490 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5491 CloseThemeData (theme);
5492 OpenThemeData (infoPtr->hwnd, themeClass);
5497 static LRESULT WINAPI
5498 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5500 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5502 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5504 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5507 if (uMsg == WM_CREATE)
5508 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5515 case TVM_CREATEDRAGIMAGE:
5516 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5518 case TVM_DELETEITEM:
5519 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5521 case TVM_EDITLABELA:
5522 case TVM_EDITLABELW:
5523 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5525 case TVM_ENDEDITLABELNOW:
5526 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5528 case TVM_ENSUREVISIBLE:
5529 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5532 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5534 case TVM_GETBKCOLOR:
5535 return TREEVIEW_GetBkColor(infoPtr);
5538 return TREEVIEW_GetCount(infoPtr);
5540 case TVM_GETEDITCONTROL:
5541 return TREEVIEW_GetEditControl(infoPtr);
5543 case TVM_GETIMAGELIST:
5544 return TREEVIEW_GetImageList(infoPtr, wParam);
5547 return TREEVIEW_GetIndent(infoPtr);
5549 case TVM_GETINSERTMARKCOLOR:
5550 return TREEVIEW_GetInsertMarkColor(infoPtr);
5552 case TVM_GETISEARCHSTRINGA:
5553 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5556 case TVM_GETISEARCHSTRINGW:
5557 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5562 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5563 uMsg == TVM_GETITEMW);
5564 case TVM_GETITEMHEIGHT:
5565 return TREEVIEW_GetItemHeight(infoPtr);
5567 case TVM_GETITEMRECT:
5568 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5570 case TVM_GETITEMSTATE:
5571 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5573 case TVM_GETLINECOLOR:
5574 return TREEVIEW_GetLineColor(infoPtr);
5576 case TVM_GETNEXTITEM:
5577 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5579 case TVM_GETSCROLLTIME:
5580 return TREEVIEW_GetScrollTime(infoPtr);
5582 case TVM_GETTEXTCOLOR:
5583 return TREEVIEW_GetTextColor(infoPtr);
5585 case TVM_GETTOOLTIPS:
5586 return TREEVIEW_GetToolTips(infoPtr);
5588 case TVM_GETUNICODEFORMAT:
5589 return TREEVIEW_GetUnicodeFormat(infoPtr);
5591 case TVM_GETVISIBLECOUNT:
5592 return TREEVIEW_GetVisibleCount(infoPtr);
5595 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5597 case TVM_INSERTITEMA:
5598 case TVM_INSERTITEMW:
5599 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5600 uMsg == TVM_INSERTITEMW);
5601 case TVM_SELECTITEM:
5602 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5604 case TVM_SETBKCOLOR:
5605 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5607 case TVM_SETIMAGELIST:
5608 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5611 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5613 case TVM_SETINSERTMARK:
5614 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5616 case TVM_SETINSERTMARKCOLOR:
5617 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5621 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5622 uMsg == TVM_SETITEMW);
5623 case TVM_SETLINECOLOR:
5624 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5626 case TVM_SETITEMHEIGHT:
5627 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5629 case TVM_SETSCROLLTIME:
5630 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5632 case TVM_SETTEXTCOLOR:
5633 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5635 case TVM_SETTOOLTIPS:
5636 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5638 case TVM_SETUNICODEFORMAT:
5639 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5641 case TVM_SORTCHILDREN:
5642 return TREEVIEW_SortChildren(infoPtr, lParam);
5644 case TVM_SORTCHILDRENCB:
5645 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5648 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5651 return TREEVIEW_Command(infoPtr, wParam, lParam);
5654 return TREEVIEW_Destroy(infoPtr);
5659 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5662 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5665 return TREEVIEW_GetFont(infoPtr);
5668 return TREEVIEW_HScroll(infoPtr, wParam);
5671 return TREEVIEW_KeyDown(infoPtr, wParam);
5674 return TREEVIEW_KillFocus(infoPtr);
5676 case WM_LBUTTONDBLCLK:
5677 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5679 case WM_LBUTTONDOWN:
5680 return TREEVIEW_LButtonDown(infoPtr, lParam);
5682 /* WM_MBUTTONDOWN */
5685 return TREEVIEW_MouseLeave(infoPtr);
5688 return TREEVIEW_MouseMove(infoPtr, lParam);
5690 case WM_NCLBUTTONDOWN:
5691 if (infoPtr->hwndEdit)
5692 SetFocus(infoPtr->hwnd);
5696 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5699 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5701 case WM_NOTIFYFORMAT:
5702 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5704 case WM_PRINTCLIENT:
5705 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5708 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5710 case WM_RBUTTONDOWN:
5711 return TREEVIEW_RButtonDown(infoPtr, lParam);
5714 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5717 return TREEVIEW_SetFocus(infoPtr);
5720 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5723 return TREEVIEW_SetRedraw(infoPtr, wParam);
5726 return TREEVIEW_Size(infoPtr, wParam, lParam);
5728 case WM_STYLECHANGED:
5729 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5731 case WM_SYSCOLORCHANGE:
5732 COMCTL32_RefreshSysColors();
5738 return TREEVIEW_HandleTimer(infoPtr, wParam);
5740 case WM_THEMECHANGED:
5741 return TREEVIEW_ThemeChanged (infoPtr);
5744 return TREEVIEW_VScroll(infoPtr, wParam);
5746 /* WM_WININICHANGE */
5749 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5752 TRACE("drawItem\n");
5756 /* This mostly catches MFC and Delphi messages. :( */
5757 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5758 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5760 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5765 /* Class Registration ***************************************************/
5768 TREEVIEW_Register(void)
5774 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5775 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5776 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5777 wndClass.cbClsExtra = 0;
5778 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5780 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5781 wndClass.hbrBackground = 0;
5782 wndClass.lpszClassName = WC_TREEVIEWW;
5784 RegisterClassW(&wndClass);
5789 TREEVIEW_Unregister(void)
5791 UnregisterClassW(WC_TREEVIEWW, NULL);
5795 /* Tree Verification ****************************************************/
5798 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5800 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5801 TREEVIEW_ITEM *item)
5803 assert(infoPtr != NULL);
5804 assert(item != NULL);
5806 /* both NULL, or both non-null */
5807 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5809 assert(item->firstChild != item);
5810 assert(item->lastChild != item);
5812 if (item->firstChild)
5814 assert(item->firstChild->parent == item);
5815 assert(item->firstChild->prevSibling == NULL);
5818 if (item->lastChild)
5820 assert(item->lastChild->parent == item);
5821 assert(item->lastChild->nextSibling == NULL);
5824 assert(item->nextSibling != item);
5825 if (item->nextSibling)
5827 assert(item->nextSibling->parent == item->parent);
5828 assert(item->nextSibling->prevSibling == item);
5831 assert(item->prevSibling != item);
5832 if (item->prevSibling)
5834 assert(item->prevSibling->parent == item->parent);
5835 assert(item->prevSibling->nextSibling == item);
5840 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5842 assert(item != NULL);
5844 assert(item->parent != NULL);
5845 assert(item->parent != item);
5846 assert(item->iLevel == item->parent->iLevel + 1);
5848 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5850 TREEVIEW_VerifyItemCommon(infoPtr, item);
5852 TREEVIEW_VerifyChildren(infoPtr, item);
5856 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5858 TREEVIEW_ITEM *child;
5859 assert(item != NULL);
5861 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5862 TREEVIEW_VerifyItem(infoPtr, child);
5866 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5868 TREEVIEW_ITEM *root = infoPtr->root;
5870 assert(root != NULL);
5871 assert(root->iLevel == -1);
5872 assert(root->parent == NULL);
5873 assert(root->prevSibling == NULL);
5875 TREEVIEW_VerifyItemCommon(infoPtr, root);
5877 TREEVIEW_VerifyChildren(infoPtr, root);
5881 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5883 if (!TRACE_ON(treeview)) return;
5885 assert(infoPtr != NULL);
5886 TREEVIEW_VerifyRoot(infoPtr);