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 /* Same effect, different optimisation. */
857 BOOL lar = ((infoPtr->dwStyle & TVS_LINESATROOT)
858 && (infoPtr->dwStyle & (TVS_HASLINES|TVS_HASBUTTONS)));
860 BOOL lar = ((infoPtr->dwStyle
861 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
865 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
867 item->stateOffset = item->linesOffset + infoPtr->uIndent;
868 item->imageOffset = item->stateOffset
869 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
870 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
874 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
880 /* DRAW's OM docker creates items like this */
881 if (item->pszText == NULL)
893 hdc = GetDC(infoPtr->hwnd);
894 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
897 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
898 item->textWidth = sz.cx;
902 SelectObject(hdc, hOldFont);
908 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
910 item->rect.top = infoPtr->uItemHeight *
911 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
913 item->rect.bottom = item->rect.top
914 + infoPtr->uItemHeight * item->iIntegral - 1;
917 item->rect.right = infoPtr->clientWidth;
920 /* We know that only items after start need their order updated. */
922 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
929 start = infoPtr->root->firstChild;
933 order = start->visibleOrder;
935 for (item = start; item != NULL;
936 item = TREEVIEW_GetNextListItem(infoPtr, item))
938 if (!ISVISIBLE(item) && order > 0)
939 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
940 item->visibleOrder = order;
941 order += item->iIntegral;
944 infoPtr->maxVisibleOrder = order;
946 for (item = start; item != NULL;
947 item = TREEVIEW_GetNextListItem(infoPtr, item))
949 TREEVIEW_ComputeItemRect(infoPtr, item);
954 /* Update metrics of all items in selected subtree.
955 * root must be expanded
958 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
960 TREEVIEW_ITEM *sibling;
964 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
967 root->state &= ~TVIS_EXPANDED;
968 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
969 root->state |= TVIS_EXPANDED;
971 hdc = GetDC(infoPtr->hwnd);
972 hOldFont = SelectObject(hdc, infoPtr->hFont);
974 for (; root != sibling;
975 root = TREEVIEW_GetNextListItem(infoPtr, root))
977 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
979 if (root->callbackMask & TVIF_TEXT)
980 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
982 if (root->textWidth == 0)
984 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
985 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
989 SelectObject(hdc, hOldFont);
990 ReleaseDC(infoPtr->hwnd, hdc);
993 /* Item Allocation **********************************************************/
995 static TREEVIEW_ITEM *
996 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
998 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1003 /* I_IMAGENONE would make more sense but this is neither what is
1004 * documented (MSDN doesn't specify) nor what Windows actually does
1005 * (it sets it to zero)... and I can so imagine an application using
1006 * inc/dec to toggle the images. */
1007 newItem->iImage = 0;
1008 newItem->iSelectedImage = 0;
1010 if (DPA_InsertPtr(infoPtr->items, INT_MAX, newItem) == -1)
1019 /* Exact opposite of TREEVIEW_AllocateItem. In particular, it does not
1020 * free item->pszText. */
1022 TREEVIEW_FreeItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
1024 DPA_DeletePtr(infoPtr->items, DPA_GetPtrIndex(infoPtr->items, item));
1025 if (infoPtr->selectedItem == item)
1026 infoPtr->selectedItem = NULL;
1027 if (infoPtr->hotItem == item)
1028 infoPtr->hotItem = NULL;
1029 if (infoPtr->focusedItem == item)
1030 infoPtr->focusedItem = NULL;
1031 if (infoPtr->firstVisible == item)
1032 infoPtr->firstVisible = NULL;
1033 if (infoPtr->dropItem == item)
1034 infoPtr->dropItem = NULL;
1035 if (infoPtr->insertMarkItem == item)
1036 infoPtr->insertMarkItem = NULL;
1041 /* Item Insertion *******************************************************/
1043 /***************************************************************************
1044 * This method inserts newItem before sibling as a child of parent.
1045 * sibling can be NULL, but only if parent has no children.
1048 TREEVIEW_InsertBefore(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1049 TREEVIEW_ITEM *parent)
1051 assert(parent != NULL);
1053 if (sibling != NULL)
1055 assert(sibling->parent == parent);
1057 if (sibling->prevSibling != NULL)
1058 sibling->prevSibling->nextSibling = newItem;
1060 newItem->prevSibling = sibling->prevSibling;
1061 sibling->prevSibling = newItem;
1064 newItem->prevSibling = NULL;
1066 newItem->nextSibling = sibling;
1068 if (parent->firstChild == sibling)
1069 parent->firstChild = newItem;
1071 if (parent->lastChild == NULL)
1072 parent->lastChild = newItem;
1075 /***************************************************************************
1076 * This method inserts newItem after sibling as a child of parent.
1077 * sibling can be NULL, but only if parent has no children.
1080 TREEVIEW_InsertAfter(TREEVIEW_ITEM *newItem, TREEVIEW_ITEM *sibling,
1081 TREEVIEW_ITEM *parent)
1083 assert(parent != NULL);
1085 if (sibling != NULL)
1087 assert(sibling->parent == parent);
1089 if (sibling->nextSibling != NULL)
1090 sibling->nextSibling->prevSibling = newItem;
1092 newItem->nextSibling = sibling->nextSibling;
1093 sibling->nextSibling = newItem;
1096 newItem->nextSibling = NULL;
1098 newItem->prevSibling = sibling;
1100 if (parent->lastChild == sibling)
1101 parent->lastChild = newItem;
1103 if (parent->firstChild == NULL)
1104 parent->firstChild = newItem;
1108 TREEVIEW_DoSetItemT(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
1109 const TVITEMEXW *tvItem, BOOL isW)
1111 UINT callbackClear = 0;
1112 UINT callbackSet = 0;
1114 TRACE("item %p\n", wineItem);
1115 /* Do this first in case it fails. */
1116 if (tvItem->mask & TVIF_TEXT)
1118 wineItem->textWidth = 0; /* force width recalculation */
1119 if (tvItem->pszText != LPSTR_TEXTCALLBACKW && tvItem->pszText != NULL) /* covers != TEXTCALLBACKA too, and undocumented: pszText of NULL also means TEXTCALLBACK */
1124 len = lstrlenW(tvItem->pszText) + 1;
1126 len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1, NULL, 0);
1128 newText = ReAlloc(wineItem->pszText, len * sizeof(WCHAR));
1130 if (newText == NULL) return FALSE;
1132 callbackClear |= TVIF_TEXT;
1134 wineItem->pszText = newText;
1135 wineItem->cchTextMax = len;
1137 lstrcpynW(wineItem->pszText, tvItem->pszText, len);
1139 MultiByteToWideChar(CP_ACP, 0, (LPSTR)tvItem->pszText, -1,
1140 wineItem->pszText, len);
1142 TRACE("setting text %s, item %p\n", debugstr_w(wineItem->pszText), wineItem);
1146 callbackSet |= TVIF_TEXT;
1148 wineItem->pszText = ReAlloc(wineItem->pszText,
1149 TEXT_CALLBACK_SIZE * sizeof(WCHAR));
1150 wineItem->cchTextMax = TEXT_CALLBACK_SIZE;
1151 TRACE("setting callback, item %p\n", wineItem);
1155 if (tvItem->mask & TVIF_CHILDREN)
1157 wineItem->cChildren = tvItem->cChildren;
1159 if (wineItem->cChildren == I_CHILDRENCALLBACK)
1160 callbackSet |= TVIF_CHILDREN;
1162 callbackClear |= TVIF_CHILDREN;
1165 if (tvItem->mask & TVIF_IMAGE)
1167 wineItem->iImage = tvItem->iImage;
1169 if (wineItem->iImage == I_IMAGECALLBACK)
1170 callbackSet |= TVIF_IMAGE;
1172 callbackClear |= TVIF_IMAGE;
1175 if (tvItem->mask & TVIF_SELECTEDIMAGE)
1177 wineItem->iSelectedImage = tvItem->iSelectedImage;
1179 if (wineItem->iSelectedImage == I_IMAGECALLBACK)
1180 callbackSet |= TVIF_SELECTEDIMAGE;
1182 callbackClear |= TVIF_SELECTEDIMAGE;
1185 if (tvItem->mask & TVIF_PARAM)
1186 wineItem->lParam = tvItem->lParam;
1188 /* If the application sets TVIF_INTEGRAL without
1189 * supplying a TVITEMEX structure, it's toast. */
1190 if (tvItem->mask & TVIF_INTEGRAL)
1191 wineItem->iIntegral = tvItem->iIntegral;
1193 if (tvItem->mask & TVIF_STATE)
1195 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1197 wineItem->state &= ~tvItem->stateMask;
1198 wineItem->state |= (tvItem->state & tvItem->stateMask);
1201 wineItem->callbackMask |= callbackSet;
1202 wineItem->callbackMask &= ~callbackClear;
1207 /* Note that the new item is pre-zeroed. */
1209 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1211 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1212 HTREEITEM insertAfter;
1213 TREEVIEW_ITEM *newItem, *parentItem;
1214 BOOL bTextUpdated = FALSE;
1216 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1218 parentItem = infoPtr->root;
1222 parentItem = ptdi->hParent;
1224 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1226 WARN("invalid parent %p\n", parentItem);
1231 insertAfter = ptdi->hInsertAfter;
1233 /* Validate this now for convenience. */
1234 switch ((DWORD_PTR)insertAfter)
1236 case (DWORD_PTR)TVI_FIRST:
1237 case (DWORD_PTR)TVI_LAST:
1238 case (DWORD_PTR)TVI_SORT:
1242 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1243 insertAfter->parent != parentItem)
1245 WARN("invalid insert after %p\n", insertAfter);
1246 insertAfter = TVI_LAST;
1250 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1251 (tvItem->mask & TVIF_TEXT)
1252 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1253 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1256 newItem = TREEVIEW_AllocateItem(infoPtr);
1257 if (newItem == NULL)
1260 newItem->parent = parentItem;
1261 newItem->iIntegral = 1;
1262 newItem->visibleOrder = -1;
1264 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1267 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1269 infoPtr->uNumItems++;
1271 switch ((DWORD_PTR)insertAfter)
1273 case (DWORD_PTR)TVI_FIRST:
1275 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1276 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1277 if (infoPtr->firstVisible == originalFirst)
1278 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1282 case (DWORD_PTR)TVI_LAST:
1283 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1286 /* hInsertAfter names a specific item we want to insert after */
1288 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1291 case (DWORD_PTR)TVI_SORT:
1293 TREEVIEW_ITEM *aChild;
1294 TREEVIEW_ITEM *previousChild = NULL;
1295 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1296 BOOL bItemInserted = FALSE;
1298 aChild = parentItem->firstChild;
1300 bTextUpdated = TRUE;
1301 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1303 /* Iterate the parent children to see where we fit in */
1304 while (aChild != NULL)
1308 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1309 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1311 if (comp < 0) /* we are smaller than the current one */
1313 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1314 if (infoPtr->firstVisible == originalFirst &&
1315 aChild == originalFirst)
1316 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1317 bItemInserted = TRUE;
1320 else if (comp > 0) /* we are bigger than the current one */
1322 previousChild = aChild;
1324 /* This will help us to exit if there is no more sibling */
1325 aChild = (aChild->nextSibling == 0)
1327 : aChild->nextSibling;
1329 /* Look at the next item */
1335 * An item with this name is already existing, therefore,
1336 * we add after the one we found
1338 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1339 bItemInserted = TRUE;
1345 * we reach the end of the child list and the item has not
1346 * yet been inserted, therefore, insert it after the last child.
1348 if ((!bItemInserted) && (aChild == NULL))
1349 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1356 TRACE("new item %p; parent %p, mask %x\n", newItem,
1357 newItem->parent, tvItem->mask);
1359 newItem->iLevel = newItem->parent->iLevel + 1;
1361 if (newItem->parent->cChildren == 0)
1362 newItem->parent->cChildren = 1;
1364 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1366 if (STATEIMAGEINDEX(newItem->state) == 0)
1367 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1370 if (infoPtr->firstVisible == NULL)
1371 infoPtr->firstVisible = newItem;
1373 TREEVIEW_VerifyTree(infoPtr);
1375 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1377 if (parentItem == infoPtr->root ||
1378 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1380 TREEVIEW_ITEM *item;
1381 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1383 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1384 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1387 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1389 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1390 TREEVIEW_UpdateScrollBars(infoPtr);
1392 * if the item was inserted in a visible part of the tree,
1393 * invalidate it, as well as those after it
1395 for (item = newItem;
1397 item = TREEVIEW_GetNextListItem(infoPtr, item))
1398 TREEVIEW_Invalidate(infoPtr, item);
1402 /* refresh treeview if newItem is the first item inserted under parentItem */
1403 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1405 /* parent got '+' - update it */
1406 TREEVIEW_Invalidate(infoPtr, parentItem);
1410 return (LRESULT)newItem;
1413 /* Item Deletion ************************************************************/
1415 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1418 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1420 TREEVIEW_ITEM *kill = parentItem->firstChild;
1422 while (kill != NULL)
1424 TREEVIEW_ITEM *next = kill->nextSibling;
1426 TREEVIEW_RemoveItem(infoPtr, kill);
1431 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1432 assert(parentItem->firstChild == NULL);
1433 assert(parentItem->lastChild == NULL);
1437 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1439 TREEVIEW_ITEM *parentItem = item->parent;
1441 assert(item != NULL);
1442 assert(item->parent != NULL); /* i.e. it must not be the root */
1444 if (parentItem->firstChild == item)
1445 parentItem->firstChild = item->nextSibling;
1447 if (parentItem->lastChild == item)
1448 parentItem->lastChild = item->prevSibling;
1450 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1451 && parentItem->cChildren > 0)
1452 parentItem->cChildren = 0;
1454 if (item->prevSibling)
1455 item->prevSibling->nextSibling = item->nextSibling;
1457 if (item->nextSibling)
1458 item->nextSibling->prevSibling = item->prevSibling;
1462 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1464 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1466 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1467 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1469 if (wineItem->firstChild)
1470 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1472 TREEVIEW_UnlinkItem(wineItem);
1474 infoPtr->uNumItems--;
1476 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1477 Free(wineItem->pszText);
1479 TREEVIEW_FreeItem(infoPtr, wineItem);
1483 /* Empty out the tree. */
1485 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1487 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1489 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1493 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1495 TREEVIEW_ITEM *newSelection = NULL;
1496 TREEVIEW_ITEM *newFirstVisible = NULL;
1497 TREEVIEW_ITEM *parent, *prev = NULL;
1498 BOOL visible = FALSE;
1500 if (wineItem == TVI_ROOT)
1502 TRACE("TVI_ROOT\n");
1503 parent = infoPtr->root;
1504 newSelection = NULL;
1506 TREEVIEW_RemoveTree(infoPtr);
1510 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1513 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1514 parent = wineItem->parent;
1516 if (ISVISIBLE(wineItem))
1518 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1522 if (infoPtr->selectedItem != NULL
1523 && (wineItem == infoPtr->selectedItem
1524 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1526 if (wineItem->nextSibling)
1527 newSelection = wineItem->nextSibling;
1528 else if (wineItem->parent != infoPtr->root)
1529 newSelection = wineItem->parent;
1531 newSelection = wineItem->prevSibling;
1532 TRACE("newSelection = %p\n", newSelection);
1535 if (infoPtr->firstVisible == wineItem)
1537 if (wineItem->nextSibling)
1538 newFirstVisible = wineItem->nextSibling;
1539 else if (wineItem->prevSibling)
1540 newFirstVisible = wineItem->prevSibling;
1541 else if (wineItem->parent != infoPtr->root)
1542 newFirstVisible = wineItem->parent;
1543 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1546 newFirstVisible = infoPtr->firstVisible;
1548 TREEVIEW_RemoveItem(infoPtr, wineItem);
1551 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1552 if (!infoPtr->selectedItem && newSelection)
1554 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1555 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1558 /* Validate insertMark dropItem.
1559 * hotItem ??? - used for comparison only.
1561 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1562 infoPtr->insertMarkItem = 0;
1564 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1565 infoPtr->dropItem = 0;
1567 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1568 newFirstVisible = infoPtr->root->firstChild;
1570 TREEVIEW_VerifyTree(infoPtr);
1572 if (!infoPtr->bRedraw) return TRUE;
1576 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1577 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1578 TREEVIEW_UpdateScrollBars(infoPtr);
1579 TREEVIEW_Invalidate(infoPtr, NULL);
1581 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1583 /* parent lost '+/-' - update it */
1584 TREEVIEW_Invalidate(infoPtr, parent);
1591 /* Get/Set Messages *********************************************************/
1593 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1595 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1597 if (infoPtr->bRedraw)
1599 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1600 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1601 TREEVIEW_UpdateScrollBars(infoPtr);
1602 TREEVIEW_Invalidate(infoPtr, NULL);
1608 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1611 return infoPtr->uIndent;
1615 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1619 if (newIndent < MINIMUM_INDENT)
1620 newIndent = MINIMUM_INDENT;
1622 if (infoPtr->uIndent != newIndent)
1624 infoPtr->uIndent = newIndent;
1625 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1626 TREEVIEW_UpdateScrollBars(infoPtr);
1627 TREEVIEW_Invalidate(infoPtr, NULL);
1635 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1638 return (LRESULT)infoPtr->hwndToolTip;
1642 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1647 prevToolTip = infoPtr->hwndToolTip;
1648 infoPtr->hwndToolTip = hwndTT;
1650 return (LRESULT)prevToolTip;
1654 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1656 BOOL rc = infoPtr->bNtfUnicode;
1657 infoPtr->bNtfUnicode = fUnicode;
1662 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1664 return infoPtr->bNtfUnicode;
1668 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1670 return infoPtr->uScrollTime;
1674 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1676 UINT uOldScrollTime = infoPtr->uScrollTime;
1678 infoPtr->uScrollTime = min(uScrollTime, 100);
1680 return uOldScrollTime;
1685 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1692 return (LRESULT)infoPtr->himlNormal;
1695 return (LRESULT)infoPtr->himlState;
1702 #define TVHEIGHT_MIN 16
1703 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1705 /* Compute the natural height for items. */
1707 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1711 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1714 /* Height is the maximum of:
1715 * 16 (a hack because our fonts are tiny), and
1716 * The text height + border & margin, and
1717 * The size of the normal image list
1719 GetTextMetricsW(hdc, &tm);
1720 SelectObject(hdc, hOldFont);
1723 height = TVHEIGHT_MIN;
1724 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1725 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1726 if (height < infoPtr->normalImageHeight)
1727 height = infoPtr->normalImageHeight;
1729 /* Round down, unless we support odd ("non even") heights. */
1730 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1737 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1739 HIMAGELIST himlOld = 0;
1740 int oldWidth = infoPtr->normalImageWidth;
1741 int oldHeight = infoPtr->normalImageHeight;
1744 TRACE("%lx,%p\n", wParam, himlNew);
1749 himlOld = infoPtr->himlNormal;
1750 infoPtr->himlNormal = himlNew;
1752 if (himlNew != NULL)
1753 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1754 &infoPtr->normalImageHeight);
1757 infoPtr->normalImageWidth = 0;
1758 infoPtr->normalImageHeight = 0;
1764 himlOld = infoPtr->himlState;
1765 infoPtr->himlState = himlNew;
1767 if (himlNew != NULL)
1768 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1769 &infoPtr->stateImageHeight);
1772 infoPtr->stateImageWidth = 0;
1773 infoPtr->stateImageHeight = 0;
1779 if (oldWidth != infoPtr->normalImageWidth ||
1780 oldHeight != infoPtr->normalImageHeight)
1782 BOOL bRecalcVisible = FALSE;
1784 if (oldHeight != infoPtr->normalImageHeight &&
1785 !infoPtr->bHeightSet)
1787 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1788 bRecalcVisible = TRUE;
1791 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1792 infoPtr->normalImageWidth != infoPtr->uIndent)
1794 infoPtr->uIndent = infoPtr->normalImageWidth;
1795 bRecalcVisible = TRUE;
1799 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1801 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1802 TREEVIEW_UpdateScrollBars(infoPtr);
1805 TREEVIEW_Invalidate(infoPtr, NULL);
1807 return (LRESULT)himlOld;
1811 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1813 INT prevHeight = infoPtr->uItemHeight;
1815 TRACE("%d\n", newHeight);
1816 if (newHeight == -1)
1818 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1819 infoPtr->bHeightSet = FALSE;
1823 infoPtr->uItemHeight = newHeight;
1824 infoPtr->bHeightSet = TRUE;
1827 /* Round down, unless we support odd ("non even") heights. */
1828 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1829 infoPtr->uItemHeight &= ~1;
1831 if (infoPtr->uItemHeight != prevHeight)
1833 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1834 TREEVIEW_UpdateScrollBars(infoPtr);
1835 TREEVIEW_Invalidate(infoPtr, NULL);
1842 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1845 return infoPtr->uItemHeight;
1850 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1852 TRACE("%p\n", infoPtr->hFont);
1853 return (LRESULT)infoPtr->hFont;
1858 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1862 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1868 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1870 UINT uHeight = infoPtr->uItemHeight;
1872 TRACE("%p %i\n", hFont, bRedraw);
1874 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1876 DeleteObject(infoPtr->hBoldFont);
1877 DeleteObject(infoPtr->hUnderlineFont);
1878 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1879 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1881 if (!infoPtr->bHeightSet)
1882 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1884 if (uHeight != infoPtr->uItemHeight)
1885 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1887 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1889 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1890 TREEVIEW_UpdateScrollBars(infoPtr);
1893 TREEVIEW_Invalidate(infoPtr, NULL);
1900 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1903 return (LRESULT)infoPtr->clrLine;
1907 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1909 COLORREF prevColor = infoPtr->clrLine;
1912 infoPtr->clrLine = color;
1913 return (LRESULT)prevColor;
1918 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1921 return (LRESULT)infoPtr->clrText;
1925 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1927 COLORREF prevColor = infoPtr->clrText;
1930 infoPtr->clrText = color;
1932 if (infoPtr->clrText != prevColor)
1933 TREEVIEW_Invalidate(infoPtr, NULL);
1935 return (LRESULT)prevColor;
1940 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1943 return (LRESULT)infoPtr->clrBk;
1947 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1949 COLORREF prevColor = infoPtr->clrBk;
1952 infoPtr->clrBk = newColor;
1954 if (newColor != prevColor)
1955 TREEVIEW_Invalidate(infoPtr, NULL);
1957 return (LRESULT)prevColor;
1962 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1965 return (LRESULT)infoPtr->clrInsertMark;
1969 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1971 COLORREF prevColor = infoPtr->clrInsertMark;
1973 TRACE("%x\n", color);
1974 infoPtr->clrInsertMark = color;
1976 return (LRESULT)prevColor;
1981 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1983 TRACE("%d %p\n", wParam, item);
1985 if (!TREEVIEW_ValidItem(infoPtr, item))
1988 infoPtr->insertBeforeorAfter = wParam;
1989 infoPtr->insertMarkItem = item;
1991 TREEVIEW_Invalidate(infoPtr, NULL);
1997 /************************************************************************
1998 * Some serious braindamage here. lParam is a pointer to both the
1999 * input HTREEITEM and the output RECT.
2002 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2004 TREEVIEW_ITEM *wineItem;
2005 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2009 * validate parameters
2015 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2019 * If wParam is TRUE return the text size otherwise return
2020 * the whole item size
2024 /* Windows does not send TVN_GETDISPINFO here. */
2026 lpRect->top = wineItem->rect.top;
2027 lpRect->bottom = wineItem->rect.bottom;
2029 lpRect->left = wineItem->textOffset;
2030 if (!wineItem->textWidth)
2031 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2033 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2037 *lpRect = wineItem->rect;
2040 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2045 static inline LRESULT
2046 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2048 /* Surprise! This does not take integral height into account. */
2049 return infoPtr->clientHeight / infoPtr->uItemHeight;
2054 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2056 TREEVIEW_ITEM *wineItem;
2058 wineItem = tvItem->hItem;
2059 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2062 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2064 if (tvItem->mask & TVIF_CHILDREN)
2066 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2067 FIXME("I_CHILDRENCALLBACK not supported\n");
2068 tvItem->cChildren = wineItem->cChildren;
2071 if (tvItem->mask & TVIF_HANDLE)
2072 tvItem->hItem = wineItem;
2074 if (tvItem->mask & TVIF_IMAGE)
2075 tvItem->iImage = wineItem->iImage;
2077 if (tvItem->mask & TVIF_INTEGRAL)
2078 tvItem->iIntegral = wineItem->iIntegral;
2080 /* undocumented: windows ignores TVIF_PARAM and
2081 * * always sets lParam
2083 tvItem->lParam = wineItem->lParam;
2085 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2086 tvItem->iSelectedImage = wineItem->iSelectedImage;
2088 if (tvItem->mask & TVIF_STATE)
2089 /* Careful here - Windows ignores the stateMask when you get the state
2090 That contradicts the documentation, but makes more common sense, masking
2091 retrieval in this way seems overkill */
2092 tvItem->state = wineItem->state;
2094 if (tvItem->mask & TVIF_TEXT)
2096 if (wineItem->pszText == NULL)
2098 if (tvItem->cchTextMax > 0)
2099 tvItem->pszText[0] = '\0';
2103 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2105 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2106 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2110 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2115 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2117 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2118 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2122 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2123 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2127 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2128 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2133 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2134 * which is wrong. */
2136 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2138 TREEVIEW_ITEM *wineItem;
2139 TREEVIEW_ITEM originalItem;
2141 wineItem = tvItem->hItem;
2143 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2146 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2149 /* store the original item values */
2150 originalItem = *wineItem;
2152 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2155 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2156 if ((tvItem->mask & TVIF_TEXT
2157 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2158 && ISVISIBLE(wineItem))
2160 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2161 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2164 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2166 /* The refresh updates everything, but we can't wait until then. */
2167 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2169 /* if any of the item's values changed and it's not a callback, redraw the item */
2170 if (item_changed(&originalItem, wineItem, tvItem))
2172 if (tvItem->mask & TVIF_INTEGRAL)
2174 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2175 TREEVIEW_UpdateScrollBars(infoPtr);
2177 TREEVIEW_Invalidate(infoPtr, NULL);
2181 TREEVIEW_UpdateScrollBars(infoPtr);
2182 TREEVIEW_Invalidate(infoPtr, wineItem);
2191 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2195 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2198 return (wineItem->state & mask);
2202 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2204 TREEVIEW_ITEM *retval;
2208 /* handle all the global data here */
2211 case TVGN_CHILD: /* Special case: child of 0 is root */
2216 retval = infoPtr->root->firstChild;
2220 retval = infoPtr->selectedItem;
2223 case TVGN_FIRSTVISIBLE:
2224 retval = infoPtr->firstVisible;
2227 case TVGN_DROPHILITE:
2228 retval = infoPtr->dropItem;
2231 case TVGN_LASTVISIBLE:
2232 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2238 TRACE("flags:%x, returns %p\n", which, retval);
2239 return (LRESULT)retval;
2242 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2244 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2250 retval = wineItem->nextSibling;
2253 retval = wineItem->prevSibling;
2256 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2259 retval = wineItem->firstChild;
2261 case TVGN_NEXTVISIBLE:
2262 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2264 case TVGN_PREVIOUSVISIBLE:
2265 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2268 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2272 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2273 return (LRESULT)retval;
2278 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2280 TRACE(" %d\n", infoPtr->uNumItems);
2281 return (LRESULT)infoPtr->uNumItems;
2285 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2287 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2289 static const unsigned int state_table[] = { 0, 2, 1 };
2293 state = STATEIMAGEINDEX(item->state);
2294 TRACE("state:%x\n", state);
2295 item->state &= ~TVIS_STATEIMAGEMASK;
2298 state = state_table[state];
2300 item->state |= INDEXTOSTATEIMAGEMASK(state);
2302 TRACE("state:%x\n", state);
2303 TREEVIEW_Invalidate(infoPtr, item);
2308 /* Painting *************************************************************/
2310 /* Draw the lines and expand button for an item. Also draws one section
2311 * of the line from item's parent to item's parent's next sibling. */
2313 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2315 LONG centerx, centery;
2316 BOOL lar = ((infoPtr->dwStyle
2317 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2320 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2322 if (!lar && item->iLevel == 0)
2325 hbr = CreateSolidBrush(clrBk);
2326 hbrOld = SelectObject(hdc, hbr);
2328 centerx = (item->linesOffset + item->stateOffset) / 2;
2329 centery = (item->rect.top + item->rect.bottom) / 2;
2331 if (infoPtr->dwStyle & TVS_HASLINES)
2333 HPEN hOldPen, hNewPen;
2337 /* Get a dotted grey pen */
2338 lb.lbStyle = BS_SOLID;
2339 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2340 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2341 hOldPen = SelectObject(hdc, hNewPen);
2343 /* Make sure the center is on a dot (using +2 instead
2344 * of +1 gives us pixel-by-pixel compat with native) */
2345 centery = (centery + 2) & ~1;
2347 MoveToEx(hdc, item->stateOffset, centery, NULL);
2348 LineTo(hdc, centerx - 1, centery);
2350 if (item->prevSibling || item->parent != infoPtr->root)
2352 MoveToEx(hdc, centerx, item->rect.top, NULL);
2353 LineTo(hdc, centerx, centery);
2356 if (item->nextSibling)
2358 MoveToEx(hdc, centerx, centery, NULL);
2359 LineTo(hdc, centerx, item->rect.bottom + 1);
2362 /* Draw the line from our parent to its next sibling. */
2363 parent = item->parent;
2364 while (parent != infoPtr->root)
2366 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2368 if (parent->nextSibling
2369 /* skip top-levels unless TVS_LINESATROOT */
2370 && parent->stateOffset > parent->linesOffset)
2372 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2373 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2376 parent = parent->parent;
2379 SelectObject(hdc, hOldPen);
2380 DeleteObject(hNewPen);
2384 * Display the (+/-) signs
2387 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2389 if (item->cChildren)
2391 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2394 RECT glyphRect = item->rect;
2395 glyphRect.left = item->linesOffset;
2396 glyphRect.right = item->stateOffset;
2397 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2398 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2403 LONG height = item->rect.bottom - item->rect.top;
2404 LONG width = item->stateOffset - item->linesOffset;
2405 LONG rectsize = min(height, width) / 4;
2406 /* plussize = ceil(rectsize * 3/4) */
2407 LONG plussize = (rectsize + 1) * 3 / 4;
2409 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2410 HPEN old_pen = SelectObject(hdc, new_pen);
2412 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2413 centerx + rectsize + 2, centery + rectsize + 2);
2415 SelectObject(hdc, old_pen);
2416 DeleteObject(new_pen);
2418 /* draw +/- signs with current text color */
2419 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2420 old_pen = SelectObject(hdc, new_pen);
2422 if (height < 18 || width < 18)
2424 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2425 LineTo(hdc, centerx + plussize, centery);
2427 if (!(item->state & TVIS_EXPANDED))
2429 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2430 LineTo(hdc, centerx, centery + plussize);
2435 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2436 centerx + plussize, centery + 2);
2438 if (!(item->state & TVIS_EXPANDED))
2440 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2441 centerx + 2, centery + plussize);
2442 SetPixel(hdc, centerx - 1, centery, clrBk);
2443 SetPixel(hdc, centerx + 1, centery, clrBk);
2447 SelectObject(hdc, old_pen);
2448 DeleteObject(new_pen);
2452 SelectObject(hdc, hbrOld);
2457 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2461 COLORREF oldTextColor, oldTextBkColor;
2463 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2464 NMTVCUSTOMDRAW nmcdhdr;
2466 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2468 /* - If item is drop target or it is selected and window is in focus -
2469 * use blue background (COLOR_HIGHLIGHT).
2470 * - If item is selected, window is not in focus, but it has style
2471 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2472 * - Otherwise - use background color
2474 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2475 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2476 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2478 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2480 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2481 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2485 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2486 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2491 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2492 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2493 nmcdhdr.clrText = comctl32_color.clrHighlight;
2495 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2498 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2500 /* The custom draw handler can query the text rectangle,
2502 /* should already be known, set to 0 when changed */
2503 if (!wineItem->textWidth)
2504 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2508 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2510 cditem = TREEVIEW_SendCustomDrawItemNotify
2511 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2512 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2514 if (cditem & CDRF_SKIPDEFAULT)
2516 SelectObject(hdc, hOldFont);
2521 if (cditem & CDRF_NEWFONT)
2522 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2524 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2526 /* Set colors. Custom draw handler can change these so we do this after it. */
2527 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2528 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2530 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2533 * Display the images associated with this item
2538 /* State images are displayed to the left of the Normal image
2539 * image number is in state; zero should be `display no image'.
2541 imageIndex = STATEIMAGEINDEX(wineItem->state);
2543 if (infoPtr->himlState && imageIndex)
2545 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2546 wineItem->stateOffset,
2547 centery - infoPtr->stateImageHeight / 2,
2551 /* Now, draw the normal image; can be either selected or
2552 * non-selected image.
2555 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2557 /* The item is currently selected */
2558 imageIndex = wineItem->iSelectedImage;
2562 /* The item is not selected */
2563 imageIndex = wineItem->iImage;
2566 if (infoPtr->himlNormal)
2568 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2570 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2571 wineItem->imageOffset,
2572 centery - infoPtr->normalImageHeight / 2,
2573 ILD_NORMAL | ovlIdx);
2579 * Display the text associated with this item
2582 /* Don't paint item's text if it's being edited */
2583 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2585 if (wineItem->pszText)
2589 rcText.top = wineItem->rect.top;
2590 rcText.bottom = wineItem->rect.bottom;
2591 rcText.left = wineItem->textOffset;
2592 rcText.right = rcText.left + wineItem->textWidth + 4;
2594 TRACE("drawing text %s at (%s)\n",
2595 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2598 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2599 ETO_CLIPPED | ETO_OPAQUE,
2602 lstrlenW(wineItem->pszText),
2605 /* Draw the box around the selected item */
2606 if ((wineItem == infoPtr->selectedItem) && inFocus)
2608 DrawFocusRect(hdc,&rcText);
2614 /* Draw insertion mark if necessary */
2616 if (infoPtr->insertMarkItem)
2617 TRACE("item:%d,mark:%p\n",
2618 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2619 infoPtr->insertMarkItem);
2621 if (wineItem == infoPtr->insertMarkItem)
2623 HPEN hNewPen, hOldPen;
2627 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2628 hOldPen = SelectObject(hdc, hNewPen);
2630 if (infoPtr->insertBeforeorAfter)
2631 offset = wineItem->rect.bottom - 1;
2633 offset = wineItem->rect.top + 1;
2635 left = wineItem->textOffset - 2;
2636 right = wineItem->textOffset + wineItem->textWidth + 2;
2638 MoveToEx(hdc, left, offset - 3, NULL);
2639 LineTo(hdc, left, offset + 4);
2641 MoveToEx(hdc, left, offset, NULL);
2642 LineTo(hdc, right + 1, offset);
2644 MoveToEx(hdc, right, offset + 3, NULL);
2645 LineTo(hdc, right, offset - 4);
2647 SelectObject(hdc, hOldPen);
2648 DeleteObject(hNewPen);
2651 if (cditem & CDRF_NOTIFYPOSTPAINT)
2653 cditem = TREEVIEW_SendCustomDrawItemNotify
2654 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2655 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2658 /* Restore the hdc state */
2659 SetTextColor(hdc, oldTextColor);
2660 SetBkColor(hdc, oldTextBkColor);
2661 SelectObject(hdc, hOldFont);
2664 /* Computes treeHeight and treeWidth and updates the scroll bars.
2667 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2669 TREEVIEW_ITEM *wineItem;
2670 HWND hwnd = infoPtr->hwnd;
2674 LONG scrollX = infoPtr->scrollX;
2676 infoPtr->treeWidth = 0;
2677 infoPtr->treeHeight = 0;
2679 /* We iterate through all visible items in order to get the tree height
2681 wineItem = infoPtr->root->firstChild;
2683 while (wineItem != NULL)
2685 if (ISVISIBLE(wineItem))
2687 /* actually we draw text at textOffset + 2 */
2688 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2689 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2691 /* This is scroll-adjusted, but we fix this below. */
2692 infoPtr->treeHeight = wineItem->rect.bottom;
2695 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2698 /* Fix the scroll adjusted treeHeight and treeWidth. */
2699 if (infoPtr->root->firstChild)
2700 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2702 infoPtr->treeWidth += infoPtr->scrollX;
2704 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2706 /* Adding one scroll bar may take up enough space that it forces us
2707 * to add the other as well. */
2708 if (infoPtr->treeHeight > infoPtr->clientHeight)
2712 if (infoPtr->treeWidth
2713 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2716 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2719 if (!vert && horz && infoPtr->treeHeight
2720 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2723 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2725 si.cbSize = sizeof(SCROLLINFO);
2726 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2731 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2732 if ( si.nPage && NULL != infoPtr->firstVisible)
2734 si.nPos = infoPtr->firstVisible->visibleOrder;
2735 si.nMax = infoPtr->maxVisibleOrder - 1;
2737 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2739 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2740 ShowScrollBar(hwnd, SB_VERT, TRUE);
2741 infoPtr->uInternalStatus |= TV_VSCROLL;
2745 if (infoPtr->uInternalStatus & TV_VSCROLL)
2746 ShowScrollBar(hwnd, SB_VERT, FALSE);
2747 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2752 if (infoPtr->uInternalStatus & TV_VSCROLL)
2753 ShowScrollBar(hwnd, SB_VERT, FALSE);
2754 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2759 si.nPage = infoPtr->clientWidth;
2760 si.nPos = infoPtr->scrollX;
2761 si.nMax = infoPtr->treeWidth - 1;
2763 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2765 si.nPos = si.nMax - max( si.nPage-1, 0 );
2769 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2770 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2771 infoPtr->uInternalStatus |= TV_HSCROLL;
2773 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2774 TREEVIEW_HScroll(infoPtr,
2775 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2779 if (infoPtr->uInternalStatus & TV_HSCROLL)
2780 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2781 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2784 if (infoPtr->scrollX != 0)
2786 TREEVIEW_HScroll(infoPtr,
2787 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2792 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2796 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2799 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2801 hBrush = CreateSolidBrush(clrBk);
2802 FillRect(hdc, rc, hBrush);
2803 DeleteObject(hBrush);
2806 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2808 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2812 TRACE("%p\n", infoPtr);
2814 GetClientRect(infoPtr->hwnd, &rect);
2815 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2821 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2823 HWND hwnd = infoPtr->hwnd;
2825 TREEVIEW_ITEM *wineItem;
2827 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2829 TRACE("empty window\n");
2833 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2836 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2838 ReleaseDC(hwnd, hdc);
2842 for (wineItem = infoPtr->root->firstChild;
2844 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2846 if (ISVISIBLE(wineItem))
2848 /* Avoid unneeded calculations */
2849 if (wineItem->rect.top > rect.bottom)
2851 if (wineItem->rect.bottom < rect.top)
2854 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2858 TREEVIEW_UpdateScrollBars(infoPtr);
2860 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2862 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2866 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2868 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2872 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2875 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2877 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2881 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2892 GetClientRect(infoPtr->hwnd, &rc);
2896 hdc = BeginPaint(infoPtr->hwnd, &ps);
2899 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2902 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2903 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2906 EndPaint(infoPtr->hwnd, &ps);
2912 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2914 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2916 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2919 if (options & PRF_ERASEBKGND)
2920 TREEVIEW_EraseBackground(infoPtr, hdc);
2922 if (options & PRF_CLIENT)
2925 GetClientRect(infoPtr->hwnd, &rc);
2926 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2932 /* Sorting **************************************************************/
2934 /***************************************************************************
2935 * Forward the DPA local callback to the treeview owner callback
2938 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2939 const TVSORTCB *pCallBackSort)
2941 /* Forward the call to the client-defined callback */
2942 return pCallBackSort->lpfnCompare(first->lParam,
2944 pCallBackSort->lParam);
2947 /***************************************************************************
2948 * Treeview native sort routine: sort on item text.
2951 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2952 const TREEVIEW_INFO *infoPtr)
2954 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2955 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2957 if(first->pszText && second->pszText)
2958 return lstrcmpiW(first->pszText, second->pszText);
2959 else if(first->pszText)
2961 else if(second->pszText)
2967 /* Returns the number of physical children belonging to item. */
2969 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2974 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2980 /* Returns a DPA containing a pointer to each physical child of item in
2981 * sibling order. If item has no children, an empty DPA is returned. */
2983 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
2985 HTREEITEM child = item->firstChild;
2987 HDPA list = DPA_Create(8);
2988 if (list == 0) return NULL;
2990 for (child = item->firstChild; child != NULL; child = child->nextSibling)
2992 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3002 /***************************************************************************
3003 * Setup the treeview structure with regards of the sort method
3004 * and sort the children of the TV item specified in lParam
3005 * fRecurse: currently unused. Should be zero.
3006 * parent: if pSort!=NULL, should equal pSort->hParent.
3007 * otherwise, item which child items are to be sorted.
3008 * pSort: sort method info. if NULL, sort on item text.
3009 * if non-NULL, sort on item's lParam content, and let the
3010 * application decide what that means. See also TVM_SORTCHILDRENCB.
3014 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3018 PFNDPACOMPARE pfnCompare;
3021 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3022 if (parent == TVI_ROOT || parent == NULL)
3023 parent = infoPtr->root;
3025 /* Check for a valid handle to the parent item */
3026 if (!TREEVIEW_ValidItem(infoPtr, parent))
3028 ERR("invalid item hParent=%p\n", parent);
3034 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3035 lpCompare = (LPARAM)pSort;
3039 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3040 lpCompare = (LPARAM)infoPtr;
3043 cChildren = TREEVIEW_CountChildren(parent);
3045 /* Make sure there is something to sort */
3048 /* TREEVIEW_ITEM rechaining */
3051 HTREEITEM nextItem = 0;
3052 HTREEITEM prevItem = 0;
3054 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3056 if (sortList == NULL)
3059 /* let DPA sort the list */
3060 DPA_Sort(sortList, pfnCompare, lpCompare);
3062 /* The order of DPA entries has been changed, so fixup the
3063 * nextSibling and prevSibling pointers. */
3065 item = DPA_GetPtr(sortList, count++);
3066 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3068 /* link the two current item together */
3069 item->nextSibling = nextItem;
3070 nextItem->prevSibling = item;
3072 if (prevItem == NULL)
3074 /* this is the first item, update the parent */
3075 parent->firstChild = item;
3076 item->prevSibling = NULL;
3080 /* fix the back chaining */
3081 item->prevSibling = prevItem;
3084 /* get ready for the next one */
3089 /* the last item is pointed to by item and never has a sibling */
3090 item->nextSibling = NULL;
3091 parent->lastChild = item;
3093 DPA_Destroy(sortList);
3095 TREEVIEW_VerifyTree(infoPtr);
3097 if (parent->state & TVIS_EXPANDED)
3099 int visOrder = infoPtr->firstVisible->visibleOrder;
3101 if (parent == infoPtr->root)
3102 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3104 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3106 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3108 TREEVIEW_ITEM *item;
3110 for (item = infoPtr->root->firstChild; item != NULL;
3111 item = TREEVIEW_GetNextListItem(infoPtr, item))
3113 if (item->visibleOrder == visOrder)
3117 if (!item) item = parent->firstChild;
3118 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3121 TREEVIEW_Invalidate(infoPtr, NULL);
3130 /***************************************************************************
3131 * Setup the treeview structure with regards of the sort method
3132 * and sort the children of the TV item specified in lParam
3135 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3137 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3141 /***************************************************************************
3142 * Sort the children of the TV item specified in lParam.
3145 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3147 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3151 /* Expansion/Collapse ***************************************************/
3154 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3157 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3158 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3159 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3164 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3167 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3168 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3169 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3174 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3175 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3177 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3178 BOOL bRemoveChildren, BOOL bUser)
3180 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3181 BOOL bSetSelection, bSetFirstVisible;
3183 LONG scrollDist = 0;
3184 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3186 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3188 if (!(wineItem->state & TVIS_EXPANDED))
3191 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3192 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3194 if (wineItem->firstChild == NULL)
3197 wineItem->state &= ~TVIS_EXPANDED;
3199 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3200 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3202 bSetSelection = (infoPtr->selectedItem != NULL
3203 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3205 bSetFirstVisible = (infoPtr->firstVisible != NULL
3206 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3211 if (tmpItem->nextSibling)
3213 nextItem = tmpItem->nextSibling;
3216 tmpItem = tmpItem->parent;
3220 scrollDist = nextItem->rect.top;
3222 if (bRemoveChildren)
3224 INT old_cChildren = wineItem->cChildren;
3225 TRACE("TVE_COLLAPSERESET\n");
3226 wineItem->state &= ~TVIS_EXPANDEDONCE;
3227 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3228 wineItem->cChildren = old_cChildren;
3231 if (wineItem->firstChild)
3233 TREEVIEW_ITEM *item, *sibling;
3235 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3237 for (item = wineItem->firstChild; item != sibling;
3238 item = TREEVIEW_GetNextListItem(infoPtr, item))
3240 item->visibleOrder = -1;
3244 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3247 scrollDist = -(scrollDist - nextItem->rect.top);
3251 /* Don't call DoSelectItem, it sends notifications. */
3252 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3253 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3254 wineItem->state |= TVIS_SELECTED;
3255 infoPtr->selectedItem = wineItem;
3258 TREEVIEW_UpdateScrollBars(infoPtr);
3260 scrollRect.left = 0;
3261 scrollRect.right = infoPtr->clientWidth;
3262 scrollRect.bottom = infoPtr->clientHeight;
3266 scrollRect.top = nextItem->rect.top;
3268 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3269 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3270 TREEVIEW_Invalidate(infoPtr, wineItem);
3272 scrollRect.top = wineItem->rect.top;
3273 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3276 TREEVIEW_SetFirstVisible(infoPtr,
3277 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3284 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3285 BOOL bExpandPartial, BOOL bUser)
3288 LONG orgNextTop = 0;
3290 TREEVIEW_ITEM *nextItem, *tmpItem;
3294 if (wineItem->state & TVIS_EXPANDED)
3297 tmpItem = wineItem; nextItem = NULL;
3300 if (tmpItem->nextSibling)
3302 nextItem = tmpItem->nextSibling;
3305 tmpItem = tmpItem->parent;
3309 orgNextTop = nextItem->rect.top;
3311 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3313 if (bUser || ((wineItem->cChildren != 0) &&
3314 !(wineItem->state & TVIS_EXPANDEDONCE)))
3316 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3318 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3322 if (!wineItem->firstChild)
3325 wineItem->state |= TVIS_EXPANDED;
3326 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3327 wineItem->state |= TVIS_EXPANDEDONCE;
3331 if (!wineItem->firstChild)
3334 /* this item has already been expanded */
3335 wineItem->state |= TVIS_EXPANDED;
3339 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3341 if (ISVISIBLE(wineItem))
3343 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3344 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3345 TREEVIEW_UpdateScrollBars(infoPtr);
3347 scrollRect.left = 0;
3348 scrollRect.bottom = infoPtr->treeHeight;
3349 scrollRect.right = infoPtr->clientWidth;
3352 scrollDist = nextItem->rect.top - orgNextTop;
3353 scrollRect.top = orgNextTop;
3355 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3356 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3357 TREEVIEW_Invalidate (infoPtr, wineItem);
3359 scrollRect.top = wineItem->rect.top;
3360 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3363 /* Scroll up so that as many children as possible are visible.
3364 * This fails when expanding causes an HScroll bar to appear, but we
3365 * don't know that yet, so the last item is obscured. */
3366 if (wineItem->firstChild != NULL)
3368 int nChildren = wineItem->lastChild->visibleOrder
3369 - wineItem->firstChild->visibleOrder + 1;
3371 int visible_pos = wineItem->visibleOrder
3372 - infoPtr->firstVisible->visibleOrder;
3374 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3376 if (visible_pos > 0 && nChildren > rows_below)
3378 int scroll = nChildren - rows_below;
3380 if (scroll > visible_pos)
3381 scroll = visible_pos;
3385 TREEVIEW_ITEM *newFirstVisible
3386 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3390 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3400 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3404 if (wineItem->state & TVIS_EXPANDED)
3405 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3407 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3411 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3413 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3415 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3417 if (TREEVIEW_HasChildren(infoPtr, item))
3418 TREEVIEW_ExpandAll(infoPtr, item);
3422 /* Note:If the specified item is the child of a collapsed parent item,
3423 the parent's list of child items is (recursively) expanded to reveal the
3424 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3425 know if it also applies here.
3429 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3431 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3434 TRACE("For (%s) item:%d, flags %x, state:%d\n",
3435 TREEVIEW_ItemName(wineItem), flag,
3436 TREEVIEW_GetItemIndex(infoPtr, wineItem), wineItem->state);
3438 switch (flag & TVE_TOGGLE)
3441 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3445 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3449 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3456 TRACE("Exiting, Item %p state is now %d...\n", wineItem, wineItem->state);
3460 /* Hit-Testing **********************************************************/
3462 static TREEVIEW_ITEM *
3463 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3465 TREEVIEW_ITEM *wineItem;
3468 if (!infoPtr->firstVisible)
3471 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3473 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3474 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3476 if (row >= wineItem->visibleOrder
3477 && row < wineItem->visibleOrder + wineItem->iIntegral)
3485 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3487 TREEVIEW_ITEM *wineItem;
3493 GetClientRect(infoPtr->hwnd, &rect);
3500 status |= TVHT_TOLEFT;
3502 else if (x > rect.right)
3504 status |= TVHT_TORIGHT;
3509 status |= TVHT_ABOVE;
3511 else if (y > rect.bottom)
3513 status |= TVHT_BELOW;
3518 lpht->flags = status;
3522 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3525 lpht->flags = TVHT_NOWHERE;
3529 if (x >= wineItem->textOffset + wineItem->textWidth)
3531 lpht->flags = TVHT_ONITEMRIGHT;
3533 else if (x >= wineItem->textOffset)
3535 lpht->flags = TVHT_ONITEMLABEL;
3537 else if (x >= wineItem->imageOffset)
3539 lpht->flags = TVHT_ONITEMICON;
3541 else if (x >= wineItem->stateOffset)
3543 lpht->flags = TVHT_ONITEMSTATEICON;
3545 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3547 lpht->flags = TVHT_ONITEMBUTTON;
3551 lpht->flags = TVHT_ONITEMINDENT;
3554 lpht->hItem = wineItem;
3555 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3557 return (LRESULT)wineItem;
3560 /* Item Label Editing ***************************************************/
3563 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3565 return (LRESULT)infoPtr->hwndEdit;
3568 static LRESULT CALLBACK
3569 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3571 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3572 BOOL bCancel = FALSE;
3578 TRACE("WM_PAINT start\n");
3579 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3581 TRACE("WM_PAINT done\n");
3585 if (infoPtr->bIgnoreEditKillFocus)
3591 WNDPROC editProc = infoPtr->wpEditOrig;
3592 infoPtr->wpEditOrig = 0;
3593 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3594 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3598 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3601 if (wParam == VK_ESCAPE)
3606 else if (wParam == VK_RETURN)
3613 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3616 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3617 /* eg. Using a messagebox */
3619 infoPtr->bIgnoreEditKillFocus = TRUE;
3620 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3621 infoPtr->bIgnoreEditKillFocus = FALSE;
3627 /* should handle edit control messages here */
3630 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3632 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3634 switch (HIWORD(wParam))
3639 * Adjust the edit window size
3642 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3643 HDC hdc = GetDC(infoPtr->hwndEdit);
3645 HFONT hFont, hOldFont = 0;
3647 TRACE("edit=%p\n", infoPtr->hwndEdit);
3649 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3651 infoPtr->bLabelChanged = TRUE;
3653 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3655 /* Select font to get the right dimension of the string */
3656 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3660 hOldFont = SelectObject(hdc, hFont);
3663 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3665 TEXTMETRICW textMetric;
3667 /* Add Extra spacing for the next character */
3668 GetTextMetricsW(hdc, &textMetric);
3669 sz.cx += (textMetric.tmMaxCharWidth * 2);
3671 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3673 infoPtr->clientWidth - editItem->textOffset + 2);
3675 SetWindowPos(infoPtr->hwndEdit,
3680 editItem->rect.bottom - editItem->rect.top + 3,
3681 SWP_NOMOVE | SWP_DRAWFRAME);
3686 SelectObject(hdc, hOldFont);
3689 ReleaseDC(infoPtr->hwnd, hdc);
3693 /* apparently we should respect passed handle value */
3694 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3696 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3700 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3707 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3709 HWND hwnd = infoPtr->hwnd;
3712 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3715 TEXTMETRICW textMetric;
3717 TRACE("%p %p\n", hwnd, hItem);
3718 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3721 if (infoPtr->hwndEdit)
3722 return infoPtr->hwndEdit;
3724 infoPtr->bLabelChanged = FALSE;
3726 /* make edit item visible */
3727 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3729 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3732 /* Select the font to get appropriate metric dimensions */
3733 if (infoPtr->hFont != 0)
3735 hOldFont = SelectObject(hdc, infoPtr->hFont);
3738 /* Get string length in pixels */
3740 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3743 GetTextExtentPoint32A(hdc, "", 0, &sz);
3745 /* Add Extra spacing for the next character */
3746 GetTextMetricsW(hdc, &textMetric);
3747 sz.cx += (textMetric.tmMaxCharWidth * 2);
3749 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3750 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3752 if (infoPtr->hFont != 0)
3754 SelectObject(hdc, hOldFont);
3757 ReleaseDC(hwnd, hdc);
3759 infoPtr->editItem = hItem;
3761 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3764 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3765 WS_CLIPSIBLINGS | ES_WANTRETURN |
3766 ES_LEFT, hItem->textOffset - 2,
3767 hItem->rect.top - 1, sz.cx + 3,
3768 hItem->rect.bottom -
3769 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3770 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3772 infoPtr->hwndEdit = hwndEdit;
3774 /* Get a 2D border. */
3775 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3776 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3777 SetWindowLongW(hwndEdit, GWL_STYLE,
3778 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3780 SendMessageW(hwndEdit, WM_SETFONT,
3781 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3783 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3785 TREEVIEW_Edit_SubclassProc);
3787 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3789 DestroyWindow(hwndEdit);
3790 infoPtr->hwndEdit = 0;
3791 infoPtr->editItem = NULL;
3796 SetWindowTextW(hwndEdit, hItem->pszText);
3799 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3800 ShowWindow(hwndEdit, SW_SHOW);
3807 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3809 HWND hwnd = infoPtr->hwnd;
3810 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3813 WCHAR tmpText[1024] = { '\0' };
3814 WCHAR *newText = tmpText;
3817 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3819 tvdi.hdr.hwndFrom = hwnd;
3820 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3821 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3823 tvdi.item.hItem = editedItem;
3824 tvdi.item.state = editedItem->state;
3825 tvdi.item.lParam = editedItem->lParam;
3829 if (!infoPtr->bNtfUnicode)
3830 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3832 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3834 if (iLength >= 1023)
3836 ERR("Insufficient space to retrieve new item label\n");
3839 tvdi.item.mask = TVIF_TEXT;
3840 tvdi.item.pszText = tmpText;
3841 tvdi.item.cchTextMax = iLength + 1;
3845 tvdi.item.pszText = NULL;
3846 tvdi.item.cchTextMax = 0;
3849 bCommit = (BOOL)TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3851 if (!bCancel && bCommit) /* Apply the changes */
3853 if (!infoPtr->bNtfUnicode)
3855 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3856 newText = Alloc(len * sizeof(WCHAR));
3857 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3861 if (strcmpW(newText, editedItem->pszText) != 0)
3863 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3866 ERR("OutOfMemory, cannot allocate space for label\n");
3867 if(newText != tmpText) Free(newText);
3868 DestroyWindow(infoPtr->hwndEdit);
3869 infoPtr->hwndEdit = 0;
3870 infoPtr->editItem = NULL;
3875 editedItem->pszText = ptr;
3876 editedItem->cchTextMax = iLength + 1;
3877 strcpyW(editedItem->pszText, newText);
3878 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3881 if(newText != tmpText) Free(newText);
3884 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3885 DestroyWindow(infoPtr->hwndEdit);
3886 infoPtr->hwndEdit = 0;
3887 infoPtr->editItem = NULL;
3892 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3894 if (wParam != TV_EDIT_TIMER)
3896 ERR("got unknown timer\n");
3900 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3901 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3903 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3909 /* Mouse Tracking/Drag **************************************************/
3911 /***************************************************************************
3912 * This is quite unusual piece of code, but that's how it's implemented in
3916 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3918 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3919 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3923 r.top = pt.y - cyDrag;
3924 r.left = pt.x - cxDrag;
3925 r.bottom = pt.y + cyDrag;
3926 r.right = pt.x + cxDrag;
3928 SetCapture(infoPtr->hwnd);
3932 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3934 if (msg.message == WM_MOUSEMOVE)
3936 pt.x = (short)LOWORD(msg.lParam);
3937 pt.y = (short)HIWORD(msg.lParam);
3938 if (PtInRect(&r, pt))
3946 else if (msg.message >= WM_LBUTTONDOWN &&
3947 msg.message <= WM_RBUTTONDBLCLK)
3949 if (msg.message == WM_RBUTTONUP)
3950 TREEVIEW_RButtonUp(infoPtr, &pt);
3954 DispatchMessageW(&msg);
3957 if (GetCapture() != infoPtr->hwnd)
3967 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3969 TREEVIEW_ITEM *wineItem;
3973 SetFocus(infoPtr->hwnd);
3975 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3977 /* If there is pending 'edit label' event - kill it now */
3978 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3981 hit.pt.x = (short)LOWORD(lParam);
3982 hit.pt.y = (short)HIWORD(lParam);
3984 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3987 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3989 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
3993 case TVHT_ONITEMRIGHT:
3994 /* FIXME: we should not have sent NM_DBLCLK in this case. */
3997 case TVHT_ONITEMINDENT:
3998 if (!(infoPtr->dwStyle & TVS_HASLINES))
4004 int level = hit.pt.x / infoPtr->uIndent;
4005 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4007 while (wineItem->iLevel > level)
4009 wineItem = wineItem->parent;
4015 case TVHT_ONITEMLABEL:
4016 case TVHT_ONITEMICON:
4017 case TVHT_ONITEMBUTTON:
4018 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4021 case TVHT_ONITEMSTATEICON:
4022 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4023 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4025 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4034 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4036 HWND hwnd = infoPtr->hwnd;
4038 BOOL bTrack, bDoLabelEdit;
4040 /* If Edit control is active - kill it and return.
4041 * The best way to do it is to set focus to itself.
4042 * Edit control subclassed procedure will automatically call
4045 if (infoPtr->hwndEdit)
4051 ht.pt.x = (short)LOWORD(lParam);
4052 ht.pt.y = (short)HIWORD(lParam);
4054 TREEVIEW_HitTest(infoPtr, &ht);
4055 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4057 /* update focusedItem and redraw both items */
4058 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4060 infoPtr->focusedItem = ht.hItem;
4061 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4062 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4065 bTrack = (ht.flags & TVHT_ONITEM)
4066 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4069 * If the style allows editing and the node is already selected
4070 * and the click occurred on the item label...
4072 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4073 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4075 /* Send NM_CLICK right away */
4077 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4080 if (ht.flags & TVHT_ONITEMBUTTON)
4082 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4086 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4087 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4089 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4090 infoPtr->dropItem = ht.hItem;
4092 /* clean up focusedItem as we dragged and won't select this item */
4093 if(infoPtr->focusedItem)
4095 /* refresh the item that was focused */
4096 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4097 infoPtr->focusedItem = NULL;
4099 /* refresh the selected item to return the filled background */
4100 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4107 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4112 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4113 KillTimer(hwnd, TV_EDIT_TIMER);
4115 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4116 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4118 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4121 * if we are TVS_SINGLEEXPAND then we want this single click to
4122 * do a bunch of things.
4124 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4125 (infoPtr->hwndEdit == 0))
4127 TREEVIEW_ITEM *SelItem;
4130 * Send the notification
4132 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4135 * Close the previous selection all the way to the root
4136 * as long as the new selection is not a child
4138 if((infoPtr->selectedItem)
4139 && (infoPtr->selectedItem != ht.hItem))
4141 BOOL closeit = TRUE;
4144 /* determine if the hitItem is a child of the currently selected item */
4145 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4147 closeit = (SelItem != infoPtr->selectedItem);
4148 SelItem = SelItem->parent;
4153 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4154 SelItem = infoPtr->selectedItem;
4156 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4158 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4159 SelItem = SelItem->parent;
4165 * Expand the current item
4167 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4170 /* Select the current item */
4171 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4173 else if (ht.flags & TVHT_ONITEMSTATEICON)
4175 /* TVS_CHECKBOXES requires us to toggle the current state */
4176 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4177 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4187 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4191 if (infoPtr->hwndEdit)
4193 SetFocus(infoPtr->hwnd);
4197 ht.pt.x = (short)LOWORD(lParam);
4198 ht.pt.y = (short)HIWORD(lParam);
4200 TREEVIEW_HitTest(infoPtr, &ht);
4202 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4206 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4207 infoPtr->dropItem = ht.hItem;
4212 SetFocus(infoPtr->hwnd);
4213 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4220 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4226 TREEVIEW_HitTest(infoPtr, &ht);
4230 /* Change to screen coordinate for WM_CONTEXTMENU */
4231 ClientToScreen(infoPtr->hwnd, &ht.pt);
4233 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4234 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4235 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4242 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4244 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4248 HBITMAP hbmp, hOldbmp;
4255 if (!(infoPtr->himlNormal))
4258 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4261 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4263 hwtop = GetDesktopWindow();
4264 htopdc = GetDC(hwtop);
4265 hdc = CreateCompatibleDC(htopdc);
4267 hOldFont = SelectObject(hdc, infoPtr->hFont);
4269 if (dragItem->pszText)
4270 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4273 GetTextExtentPoint32A(hdc, "", 0, &size);
4275 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4276 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4277 hOldbmp = SelectObject(hdc, hbmp);
4279 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4284 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4285 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4289 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4290 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4293 /* draw item text */
4295 SetRect(&rc, cx, 0, size.cx, size.cy);
4297 if (dragItem->pszText)
4298 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4301 SelectObject(hdc, hOldFont);
4302 SelectObject(hdc, hOldbmp);
4304 ImageList_Add(infoPtr->dragList, hbmp, 0);
4308 ReleaseDC(hwtop, htopdc);
4310 return (LRESULT)infoPtr->dragList;
4313 /* Selection ************************************************************/
4316 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4319 TREEVIEW_ITEM *prevSelect;
4321 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4323 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4324 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4325 newSelect ? newSelect->state : 0);
4327 /* reset and redraw focusedItem if focusedItem was set so we don't */
4328 /* have to worry about the previously focused item when we set a new one */
4329 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4330 infoPtr->focusedItem = NULL;
4335 prevSelect = infoPtr->selectedItem;
4337 if (prevSelect == newSelect) {
4338 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4342 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4345 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4351 prevSelect->state &= ~TVIS_SELECTED;
4353 newSelect->state |= TVIS_SELECTED;
4355 infoPtr->selectedItem = newSelect;
4357 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4359 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4360 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4362 TREEVIEW_SendTreeviewNotify(infoPtr,
4365 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4370 case TVGN_DROPHILITE:
4371 prevSelect = infoPtr->dropItem;
4374 prevSelect->state &= ~TVIS_DROPHILITED;
4376 infoPtr->dropItem = newSelect;
4379 newSelect->state |= TVIS_DROPHILITED;
4381 TREEVIEW_Invalidate(infoPtr, prevSelect);
4382 TREEVIEW_Invalidate(infoPtr, newSelect);
4385 case TVGN_FIRSTVISIBLE:
4386 if (newSelect != NULL)
4388 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4389 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4390 TREEVIEW_Invalidate(infoPtr, NULL);
4395 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4399 /* FIXME: handle NM_KILLFOCUS etc */
4401 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4403 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4406 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4408 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4414 /*************************************************************************
4415 * TREEVIEW_ProcessLetterKeys
4417 * Processes keyboard messages generated by pressing the letter keys
4419 * What this does is perform a case insensitive search from the
4420 * current position with the following quirks:
4421 * - If two chars or more are pressed in quick succession we search
4422 * for the corresponding string (e.g. 'abc').
4423 * - If there is a delay we wipe away the current search string and
4424 * restart with just that char.
4425 * - If the user keeps pressing the same character, whether slowly or
4426 * fast, so that the search string is entirely composed of this
4427 * character ('aaaaa' for instance), then we search for first item
4428 * that starting with that character.
4429 * - If the user types the above character in quick succession, then
4430 * we must also search for the corresponding string ('aaaaa'), and
4431 * go to that string if there is a match.
4439 * - The current implementation has a list of characters it will
4440 * accept and it ignores everything else. In particular it will
4441 * ignore accentuated characters which seems to match what
4442 * Windows does. But I'm not sure it makes sense to follow
4444 * - We don't sound a beep when the search fails.
4445 * - The search should start from the focused item, not from the selected
4446 * item. One reason for this is to allow for multiple selections in trees.
4447 * But currently infoPtr->focusedItem does not seem very usable.
4451 * TREEVIEW_ProcessLetterKeys
4453 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4456 HTREEITEM endidx,idx;
4458 WCHAR buffer[MAX_PATH];
4459 DWORD timestamp,elapsed;
4461 /* simple parameter checking */
4462 if (!charCode || !keyData) return 0;
4464 /* only allow the valid WM_CHARs through */
4465 if (!isalnum(charCode) &&
4466 charCode != '.' && charCode != '`' && charCode != '!' &&
4467 charCode != '@' && charCode != '#' && charCode != '$' &&
4468 charCode != '%' && charCode != '^' && charCode != '&' &&
4469 charCode != '*' && charCode != '(' && charCode != ')' &&
4470 charCode != '-' && charCode != '_' && charCode != '+' &&
4471 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4472 charCode != '}' && charCode != '[' && charCode != '{' &&
4473 charCode != '/' && charCode != '?' && charCode != '>' &&
4474 charCode != '<' && charCode != ',' && charCode != '~')
4477 /* compute how much time elapsed since last keypress */
4478 timestamp = GetTickCount();
4479 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4480 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4482 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4485 /* update the search parameters */
4486 infoPtr->lastKeyPressTimestamp=timestamp;
4487 if (elapsed < KEY_DELAY) {
4488 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4489 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4491 if (infoPtr->charCode != charCode) {
4492 infoPtr->charCode=charCode=0;
4495 infoPtr->charCode=charCode;
4496 infoPtr->szSearchParam[0]=charCode;
4497 infoPtr->nSearchParamLength=1;
4498 /* Redundant with the 1 char string */
4502 /* and search from the current position */
4504 if (infoPtr->selectedItem != NULL) {
4505 endidx=infoPtr->selectedItem;
4506 /* if looking for single character match,
4507 * then we must always move forward
4509 if (infoPtr->nSearchParamLength == 1)
4510 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4515 idx=infoPtr->root->firstChild;
4518 /* At the end point, sort out wrapping */
4521 /* If endidx is null, stop at the last item (ie top to bottom) */
4525 /* Otherwise, start again at the very beginning */
4526 idx=infoPtr->root->firstChild;
4528 /* But if we are stopping on the first child, end now! */
4529 if (idx == endidx) break;
4533 ZeroMemory(&item, sizeof(item));
4534 item.mask = TVIF_TEXT;
4536 item.pszText = buffer;
4537 item.cchTextMax = sizeof(buffer);
4538 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4540 /* check for a match */
4541 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4544 } else if ( (charCode != 0) && (nItem == NULL) &&
4545 (nItem != infoPtr->selectedItem) &&
4546 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4547 /* This would work but we must keep looking for a longer match */
4550 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4551 } while (idx != endidx);
4553 if (nItem != NULL) {
4554 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4555 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4562 /* Scrolling ************************************************************/
4565 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4568 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4569 HTREEITEM newFirstVisible = NULL;
4570 int visible_pos = -1;
4572 if (!TREEVIEW_ValidItem(infoPtr, item))
4575 if (!ISVISIBLE(item))
4577 /* Expand parents as necessary. */
4580 /* see if we are trying to ensure that root is visible */
4581 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4582 parent = item->parent;
4584 parent = item; /* this item is the topmost item */
4586 while (parent != infoPtr->root)
4588 if (!(parent->state & TVIS_EXPANDED))
4589 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4591 parent = parent->parent;
4595 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4597 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4598 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4600 if (hasFirstVisible)
4601 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4603 if (visible_pos < 0)
4605 /* item is before the start of the list: put it at the top. */
4606 newFirstVisible = item;
4608 else if (visible_pos >= viscount
4609 /* Sometimes, before we are displayed, GVC is 0, causing us to
4610 * spuriously scroll up. */
4611 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4613 /* item is past the end of the list. */
4614 int scroll = visible_pos - viscount;
4616 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4622 /* Scroll window so item's text is visible as much as possible */
4623 /* Calculation of amount of extra space is taken from EditLabel code */
4625 TEXTMETRICW textMetric;
4626 HDC hdc = GetWindowDC(infoPtr->hwnd);
4628 x = item->textWidth;
4630 GetTextMetricsW(hdc, &textMetric);
4631 ReleaseDC(infoPtr->hwnd, hdc);
4633 x += (textMetric.tmMaxCharWidth * 2);
4634 x = max(x, textMetric.tmMaxCharWidth * 3);
4636 if (item->textOffset < 0)
4637 pos = item->textOffset;
4638 else if (item->textOffset + x > infoPtr->clientWidth)
4640 if (x > infoPtr->clientWidth)
4641 pos = item->textOffset;
4643 pos = item->textOffset + x - infoPtr->clientWidth;
4648 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4651 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4653 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4662 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4663 TREEVIEW_ITEM *newFirstVisible,
4664 BOOL bUpdateScrollPos)
4668 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4670 if (newFirstVisible != NULL)
4672 /* Prevent an empty gap from appearing at the bottom... */
4673 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4674 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4678 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4681 /* ... unless we just don't have enough items. */
4682 if (newFirstVisible == NULL)
4683 newFirstVisible = infoPtr->root->firstChild;
4687 if (infoPtr->firstVisible != newFirstVisible)
4689 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4691 infoPtr->firstVisible = newFirstVisible;
4692 TREEVIEW_Invalidate(infoPtr, NULL);
4696 TREEVIEW_ITEM *item;
4697 int scroll = infoPtr->uItemHeight *
4698 (infoPtr->firstVisible->visibleOrder
4699 - newFirstVisible->visibleOrder);
4701 infoPtr->firstVisible = newFirstVisible;
4703 for (item = infoPtr->root->firstChild; item != NULL;
4704 item = TREEVIEW_GetNextListItem(infoPtr, item))
4706 item->rect.top += scroll;
4707 item->rect.bottom += scroll;
4710 if (bUpdateScrollPos)
4711 SetScrollPos(infoPtr->hwnd, SB_VERT,
4712 newFirstVisible->visibleOrder, TRUE);
4714 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4719 /************************************************************************
4720 * VScroll is always in units of visible items. i.e. we always have a
4721 * visible item aligned to the top of the control. (Unless we have no
4725 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4727 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4728 TREEVIEW_ITEM *newFirstVisible = NULL;
4730 int nScrollCode = LOWORD(wParam);
4732 TRACE("wp %lx\n", wParam);
4734 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4737 if (!oldFirstVisible)
4739 assert(infoPtr->root->firstChild == NULL);
4743 switch (nScrollCode)
4746 newFirstVisible = infoPtr->root->firstChild;
4750 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4754 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4758 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4762 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4763 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4767 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4768 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4772 case SB_THUMBPOSITION:
4773 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4774 infoPtr->root->firstChild,
4775 (LONG)(SHORT)HIWORD(wParam));
4782 if (newFirstVisible != NULL)
4784 if (newFirstVisible != oldFirstVisible)
4785 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4786 nScrollCode != SB_THUMBTRACK);
4787 else if (nScrollCode == SB_THUMBPOSITION)
4788 SetScrollPos(infoPtr->hwnd, SB_VERT,
4789 newFirstVisible->visibleOrder, TRUE);
4796 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4799 int scrollX = infoPtr->scrollX;
4800 int nScrollCode = LOWORD(wParam);
4802 TRACE("wp %lx\n", wParam);
4804 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4807 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4808 /* shall never occur */
4815 switch (nScrollCode)
4818 scrollX -= infoPtr->uItemHeight;
4821 scrollX += infoPtr->uItemHeight;
4824 scrollX -= infoPtr->clientWidth;
4827 scrollX += infoPtr->clientWidth;
4831 case SB_THUMBPOSITION:
4832 scrollX = (int)(SHORT)HIWORD(wParam);
4839 if (scrollX > maxWidth)
4841 else if (scrollX < 0)
4845 if (scrollX != infoPtr->scrollX)
4847 TREEVIEW_ITEM *item;
4848 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4850 for (item = infoPtr->root->firstChild; item != NULL;
4851 item = TREEVIEW_GetNextListItem(infoPtr, item))
4853 item->linesOffset += scroll_pixels;
4854 item->stateOffset += scroll_pixels;
4855 item->imageOffset += scroll_pixels;
4856 item->textOffset += scroll_pixels;
4859 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4860 infoPtr->scrollX = scrollX;
4861 UpdateWindow(infoPtr->hwnd);
4864 if (nScrollCode != SB_THUMBTRACK)
4865 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4871 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4874 UINT pulScrollLines = 3;
4876 if (wParam & (MK_SHIFT | MK_CONTROL))
4877 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4879 if (infoPtr->firstVisible == NULL)
4882 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4884 gcWheelDelta = -(short)HIWORD(wParam);
4885 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4887 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4889 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4890 int maxDy = infoPtr->maxVisibleOrder;
4898 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4903 /* Create/Destroy *******************************************************/
4906 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4909 HBITMAP hbm, hbmOld;
4913 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4915 hdcScreen = GetDC(0);
4917 hdc = CreateCompatibleDC(hdcScreen);
4918 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4919 hbmOld = SelectObject(hdc, hbm);
4921 SetRect(&rc, 0, 0, 48, 16);
4922 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4924 SetRect(&rc, 18, 2, 30, 14);
4925 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4926 DFCS_BUTTONCHECK|DFCS_FLAT);
4928 SetRect(&rc, 34, 2, 46, 14);
4929 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4930 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4932 SelectObject(hdc, hbmOld);
4933 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4934 comctl32_color.clrWindow);
4935 TRACE("checkbox index %d\n", nIndex);
4939 ReleaseDC(0, hdcScreen);
4941 infoPtr->stateImageWidth = 16;
4942 infoPtr->stateImageHeight = 16;
4946 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4949 TREEVIEW_INFO *infoPtr;
4952 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4954 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4956 if (infoPtr == NULL)
4958 ERR("could not allocate info memory!\n");
4962 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4964 infoPtr->hwnd = hwnd;
4965 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4967 infoPtr->uNumItems = 0;
4968 infoPtr->cdmode = 0;
4969 infoPtr->uScrollTime = 300; /* milliseconds */
4970 infoPtr->bRedraw = TRUE;
4972 GetClientRect(hwnd, &rcClient);
4974 /* No scroll bars yet. */
4975 infoPtr->clientWidth = rcClient.right;
4976 infoPtr->clientHeight = rcClient.bottom;
4977 infoPtr->uInternalStatus = 0;
4979 infoPtr->treeWidth = 0;
4980 infoPtr->treeHeight = 0;
4982 infoPtr->uIndent = MINIMUM_INDENT;
4983 infoPtr->selectedItem = NULL;
4984 infoPtr->focusedItem = NULL;
4985 infoPtr->hotItem = NULL;
4986 infoPtr->editItem = NULL;
4987 infoPtr->firstVisible = NULL;
4988 infoPtr->maxVisibleOrder = 0;
4989 infoPtr->dropItem = NULL;
4990 infoPtr->insertMarkItem = NULL;
4991 infoPtr->insertBeforeorAfter = 0;
4994 infoPtr->scrollX = 0;
4996 infoPtr->clrBk = CLR_NONE; /* use system color */
4997 infoPtr->clrText = CLR_NONE; /* use system color */
4998 infoPtr->clrLine = CLR_DEFAULT;
4999 infoPtr->clrInsertMark = CLR_DEFAULT;
5003 infoPtr->hwndEdit = NULL;
5004 infoPtr->wpEditOrig = NULL;
5005 infoPtr->bIgnoreEditKillFocus = FALSE;
5006 infoPtr->bLabelChanged = FALSE;
5008 infoPtr->himlNormal = NULL;
5009 infoPtr->himlState = NULL;
5010 infoPtr->normalImageWidth = 0;
5011 infoPtr->normalImageHeight = 0;
5012 infoPtr->stateImageWidth = 0;
5013 infoPtr->stateImageHeight = 0;
5015 infoPtr->items = DPA_Create(16);
5017 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5018 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5019 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5020 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5021 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5023 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5025 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5026 infoPtr->root->state = TVIS_EXPANDED;
5027 infoPtr->root->iLevel = -1;
5028 infoPtr->root->visibleOrder = -1;
5030 infoPtr->hwndNotify = lpcs->hwndParent;
5032 infoPtr->bTransparent = ( GetWindowLongW( hwnd, GWL_STYLE) & TBSTYLE_FLAT);
5035 infoPtr->hwndToolTip = 0;
5037 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5039 /* Determine what type of notify should be issued */
5040 /* sets infoPtr->bNtfUnicode */
5041 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5043 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5044 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5045 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5048 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5049 initialize_checkboxes(infoPtr);
5051 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5052 ShowScrollBar(hwnd, SB_VERT, FALSE);
5053 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5055 OpenThemeData (hwnd, themeClass);
5062 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5066 /* free item data */
5067 TREEVIEW_RemoveTree(infoPtr);
5068 /* root isn't freed with other items */
5069 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5070 DPA_Destroy(infoPtr->items);
5072 /* tool tip is automatically destroyed: we are its owner */
5074 /* Restore original wndproc */
5075 if (infoPtr->hwndEdit)
5076 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5077 (DWORD_PTR)infoPtr->wpEditOrig);
5079 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5081 /* Deassociate treeview from the window before doing anything drastic. */
5082 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5085 DeleteObject(infoPtr->hDefaultFont);
5086 DeleteObject(infoPtr->hBoldFont);
5087 DeleteObject(infoPtr->hUnderlineFont);
5093 /* Miscellaneous Messages ***********************************************/
5096 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5104 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5105 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5106 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5107 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5108 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5109 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5110 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5111 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5112 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5116 if (key >= VK_PRIOR && key <= VK_DOWN)
5118 unsigned char code = scroll[key - VK_PRIOR].code;
5120 (((code & (1 << 7)) == (SB_HORZ << 7))
5122 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5128 /************************************************************************
5131 * VK_UP Move selection to the previous non-hidden item.
5132 * VK_DOWN Move selection to the next non-hidden item.
5133 * VK_HOME Move selection to the first item.
5134 * VK_END Move selection to the last item.
5135 * VK_LEFT If expanded then collapse, otherwise move to parent.
5136 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5138 * VK_SUBTRACT Collapse.
5139 * VK_MULTIPLY Expand all.
5140 * VK_PRIOR Move up GetVisibleCount items.
5141 * VK_NEXT Move down GetVisibleCount items.
5142 * VK_BACK Move to parent.
5143 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5146 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5148 /* If it is non-NULL and different, it will be selected and visible. */
5149 TREEVIEW_ITEM *newSelection = NULL;
5151 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5153 TRACE("%lx\n", wParam);
5155 if (prevItem == NULL)
5158 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5159 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5164 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5166 newSelection = infoPtr->root->firstChild;
5170 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5174 newSelection = infoPtr->root->firstChild;
5178 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5182 if (prevItem->state & TVIS_EXPANDED)
5184 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5186 else if (prevItem->parent != infoPtr->root)
5188 newSelection = prevItem->parent;
5193 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5195 if (!(prevItem->state & TVIS_EXPANDED))
5196 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5199 newSelection = prevItem->firstChild;
5206 TREEVIEW_ExpandAll(infoPtr, prevItem);
5210 if (!(prevItem->state & TVIS_EXPANDED))
5211 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5215 if (prevItem->state & TVIS_EXPANDED)
5216 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5221 = TREEVIEW_GetListItem(infoPtr, prevItem,
5222 -TREEVIEW_GetVisibleCount(infoPtr));
5227 = TREEVIEW_GetListItem(infoPtr, prevItem,
5228 TREEVIEW_GetVisibleCount(infoPtr));
5232 newSelection = prevItem->parent;
5233 if (newSelection == infoPtr->root)
5234 newSelection = NULL;
5238 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5239 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5243 if (newSelection && newSelection != prevItem)
5245 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5248 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5256 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5258 /* remove hot effect from item */
5259 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5260 infoPtr->hotItem = NULL;
5266 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5269 TRACKMOUSEEVENT trackinfo;
5270 TREEVIEW_ITEM * item;
5272 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5274 /* fill in the TRACKMOUSEEVENT struct */
5275 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5276 trackinfo.dwFlags = TME_QUERY;
5277 trackinfo.hwndTrack = infoPtr->hwnd;
5279 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5280 _TrackMouseEvent(&trackinfo);
5282 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5283 if(!(trackinfo.dwFlags & TME_LEAVE))
5285 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5286 trackinfo.hwndTrack = infoPtr->hwnd;
5287 /* do it as fast as possible, minimal systimer latency will be used */
5288 trackinfo.dwHoverTime = 1;
5290 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5291 /* and can properly deactivate the hot item */
5292 _TrackMouseEvent(&trackinfo);
5295 pt.x = (short)LOWORD(lParam);
5296 pt.y = (short)HIWORD(lParam);
5298 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5300 if (item != infoPtr->hotItem)
5302 /* redraw old hot item */
5303 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5304 infoPtr->hotItem = item;
5305 /* redraw new hot item */
5306 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5312 /* Draw themed border */
5313 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5315 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5319 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5320 cyEdge = GetSystemMetrics (SM_CYEDGE);
5323 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5325 GetWindowRect(infoPtr->hwnd, &r);
5327 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5328 r.right - cxEdge, r.bottom - cyEdge);
5329 if (region != (HRGN)1)
5330 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5331 OffsetRect(&r, -r.left, -r.top);
5333 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5334 OffsetRect(&r, -r.left, -r.top);
5336 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5337 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5338 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5339 ReleaseDC(infoPtr->hwnd, dc);
5341 /* Call default proc to get the scrollbars etc. painted */
5342 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5348 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5350 LPNMHDR lpnmh = (LPNMHDR)lParam;
5352 if (lpnmh->code == PGN_CALCSIZE) {
5353 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5355 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5356 lppgc->iWidth = infoPtr->treeWidth;
5357 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5358 infoPtr->treeWidth, infoPtr->clientWidth);
5361 lppgc->iHeight = infoPtr->treeHeight;
5362 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5363 infoPtr->treeHeight, infoPtr->clientHeight);
5367 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5371 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5373 if (wParam == SIZE_RESTORED)
5375 infoPtr->clientWidth = (short)LOWORD(lParam);
5376 infoPtr->clientHeight = (short)HIWORD(lParam);
5378 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5379 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5380 TREEVIEW_UpdateScrollBars(infoPtr);
5384 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5387 TREEVIEW_Invalidate(infoPtr, NULL);
5392 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5394 TRACE("(%lx %lx)\n", wParam, lParam);
5396 if (wParam == GWL_STYLE)
5398 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5400 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5402 if (dwNewStyle & TVS_CHECKBOXES)
5404 initialize_checkboxes(infoPtr);
5405 TRACE("checkboxes enabled\n");
5409 FIXME("tried to disable checkboxes\n");
5413 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5415 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5417 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5418 TRACE("tooltips enabled\n");
5422 DestroyWindow(infoPtr->hwndToolTip);
5423 infoPtr->hwndToolTip = 0;
5424 TRACE("tooltips disabled\n");
5428 infoPtr->dwStyle = dwNewStyle;
5431 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5432 TREEVIEW_UpdateScrollBars(infoPtr);
5433 TREEVIEW_Invalidate(infoPtr, NULL);
5439 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5442 TREEVIEW_ITEM * item;
5446 ScreenToClient(infoPtr->hwnd, &pt);
5448 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5450 memset(&nmmouse, 0, sizeof(nmmouse));
5451 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5452 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5453 nmmouse.hdr.code = NM_SETCURSOR;
5456 nmmouse.dwItemSpec = (DWORD_PTR)item;
5457 nmmouse.dwItemData = item->lParam;
5461 nmmouse.dwHitInfo = lParam;
5462 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, (LPARAM)&nmmouse))
5465 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5467 SetCursor(infoPtr->hcurHand);
5471 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5475 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5479 if (!infoPtr->selectedItem)
5481 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5485 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5486 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5491 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5495 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5496 UpdateWindow(infoPtr->hwnd);
5497 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5501 /* update theme after a WM_THEMECHANGED message */
5502 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5504 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5505 CloseThemeData (theme);
5506 OpenThemeData (infoPtr->hwnd, themeClass);
5511 static LRESULT WINAPI
5512 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5514 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5516 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5518 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5521 if (uMsg == WM_CREATE)
5522 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5529 case TVM_CREATEDRAGIMAGE:
5530 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5532 case TVM_DELETEITEM:
5533 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5535 case TVM_EDITLABELA:
5536 case TVM_EDITLABELW:
5537 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5539 case TVM_ENDEDITLABELNOW:
5540 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5542 case TVM_ENSUREVISIBLE:
5543 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5546 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5548 case TVM_GETBKCOLOR:
5549 return TREEVIEW_GetBkColor(infoPtr);
5552 return TREEVIEW_GetCount(infoPtr);
5554 case TVM_GETEDITCONTROL:
5555 return TREEVIEW_GetEditControl(infoPtr);
5557 case TVM_GETIMAGELIST:
5558 return TREEVIEW_GetImageList(infoPtr, wParam);
5561 return TREEVIEW_GetIndent(infoPtr);
5563 case TVM_GETINSERTMARKCOLOR:
5564 return TREEVIEW_GetInsertMarkColor(infoPtr);
5566 case TVM_GETISEARCHSTRINGA:
5567 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5570 case TVM_GETISEARCHSTRINGW:
5571 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5576 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5577 uMsg == TVM_GETITEMW);
5578 case TVM_GETITEMHEIGHT:
5579 return TREEVIEW_GetItemHeight(infoPtr);
5581 case TVM_GETITEMRECT:
5582 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5584 case TVM_GETITEMSTATE:
5585 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5587 case TVM_GETLINECOLOR:
5588 return TREEVIEW_GetLineColor(infoPtr);
5590 case TVM_GETNEXTITEM:
5591 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5593 case TVM_GETSCROLLTIME:
5594 return TREEVIEW_GetScrollTime(infoPtr);
5596 case TVM_GETTEXTCOLOR:
5597 return TREEVIEW_GetTextColor(infoPtr);
5599 case TVM_GETTOOLTIPS:
5600 return TREEVIEW_GetToolTips(infoPtr);
5602 case TVM_GETUNICODEFORMAT:
5603 return TREEVIEW_GetUnicodeFormat(infoPtr);
5605 case TVM_GETVISIBLECOUNT:
5606 return TREEVIEW_GetVisibleCount(infoPtr);
5609 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5611 case TVM_INSERTITEMA:
5612 case TVM_INSERTITEMW:
5613 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5614 uMsg == TVM_INSERTITEMW);
5615 case TVM_SELECTITEM:
5616 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5618 case TVM_SETBKCOLOR:
5619 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5621 case TVM_SETIMAGELIST:
5622 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5625 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5627 case TVM_SETINSERTMARK:
5628 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5630 case TVM_SETINSERTMARKCOLOR:
5631 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5635 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5636 uMsg == TVM_SETITEMW);
5637 case TVM_SETLINECOLOR:
5638 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5640 case TVM_SETITEMHEIGHT:
5641 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5643 case TVM_SETSCROLLTIME:
5644 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5646 case TVM_SETTEXTCOLOR:
5647 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5649 case TVM_SETTOOLTIPS:
5650 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5652 case TVM_SETUNICODEFORMAT:
5653 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5655 case TVM_SORTCHILDREN:
5656 return TREEVIEW_SortChildren(infoPtr, lParam);
5658 case TVM_SORTCHILDRENCB:
5659 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5662 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5665 return TREEVIEW_Command(infoPtr, wParam, lParam);
5668 return TREEVIEW_Destroy(infoPtr);
5673 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5676 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5679 return TREEVIEW_GetFont(infoPtr);
5682 return TREEVIEW_HScroll(infoPtr, wParam);
5685 return TREEVIEW_KeyDown(infoPtr, wParam);
5688 return TREEVIEW_KillFocus(infoPtr);
5690 case WM_LBUTTONDBLCLK:
5691 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5693 case WM_LBUTTONDOWN:
5694 return TREEVIEW_LButtonDown(infoPtr, lParam);
5696 /* WM_MBUTTONDOWN */
5699 return TREEVIEW_MouseLeave(infoPtr);
5702 return TREEVIEW_MouseMove(infoPtr, lParam);
5704 case WM_NCLBUTTONDOWN:
5705 if (infoPtr->hwndEdit)
5706 SetFocus(infoPtr->hwnd);
5710 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5713 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5715 case WM_NOTIFYFORMAT:
5716 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5718 case WM_PRINTCLIENT:
5719 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5722 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5724 case WM_RBUTTONDOWN:
5725 return TREEVIEW_RButtonDown(infoPtr, lParam);
5728 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5731 return TREEVIEW_SetFocus(infoPtr);
5734 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5737 return TREEVIEW_SetRedraw(infoPtr, wParam);
5740 return TREEVIEW_Size(infoPtr, wParam, lParam);
5742 case WM_STYLECHANGED:
5743 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5745 case WM_SYSCOLORCHANGE:
5746 COMCTL32_RefreshSysColors();
5752 return TREEVIEW_HandleTimer(infoPtr, wParam);
5754 case WM_THEMECHANGED:
5755 return TREEVIEW_ThemeChanged (infoPtr);
5758 return TREEVIEW_VScroll(infoPtr, wParam);
5760 /* WM_WININICHANGE */
5763 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5766 TRACE("drawItem\n");
5770 /* This mostly catches MFC and Delphi messages. :( */
5771 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5772 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5774 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5779 /* Class Registration ***************************************************/
5782 TREEVIEW_Register(void)
5788 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5789 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5790 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5791 wndClass.cbClsExtra = 0;
5792 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5794 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5795 wndClass.hbrBackground = 0;
5796 wndClass.lpszClassName = WC_TREEVIEWW;
5798 RegisterClassW(&wndClass);
5803 TREEVIEW_Unregister(void)
5805 UnregisterClassW(WC_TREEVIEWW, NULL);
5809 /* Tree Verification ****************************************************/
5812 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5814 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5815 TREEVIEW_ITEM *item)
5817 assert(infoPtr != NULL);
5818 assert(item != NULL);
5820 /* both NULL, or both non-null */
5821 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5823 assert(item->firstChild != item);
5824 assert(item->lastChild != item);
5826 if (item->firstChild)
5828 assert(item->firstChild->parent == item);
5829 assert(item->firstChild->prevSibling == NULL);
5832 if (item->lastChild)
5834 assert(item->lastChild->parent == item);
5835 assert(item->lastChild->nextSibling == NULL);
5838 assert(item->nextSibling != item);
5839 if (item->nextSibling)
5841 assert(item->nextSibling->parent == item->parent);
5842 assert(item->nextSibling->prevSibling == item);
5845 assert(item->prevSibling != item);
5846 if (item->prevSibling)
5848 assert(item->prevSibling->parent == item->parent);
5849 assert(item->prevSibling->nextSibling == item);
5854 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5856 assert(item != NULL);
5858 assert(item->parent != NULL);
5859 assert(item->parent != item);
5860 assert(item->iLevel == item->parent->iLevel + 1);
5862 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5864 TREEVIEW_VerifyItemCommon(infoPtr, item);
5866 TREEVIEW_VerifyChildren(infoPtr, item);
5870 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5872 TREEVIEW_ITEM *child;
5873 assert(item != NULL);
5875 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5876 TREEVIEW_VerifyItem(infoPtr, child);
5880 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5882 TREEVIEW_ITEM *root = infoPtr->root;
5884 assert(root != NULL);
5885 assert(root->iLevel == -1);
5886 assert(root->parent == NULL);
5887 assert(root->prevSibling == NULL);
5889 TREEVIEW_VerifyItemCommon(infoPtr, root);
5891 TREEVIEW_VerifyChildren(infoPtr, root);
5895 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5897 if (!TRACE_ON(treeview)) return;
5899 assert(infoPtr != NULL);
5900 TREEVIEW_VerifyRoot(infoPtr);