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 *. */
82 int iIntegral; /* item height multiplier (1 is normal) */
83 int iLevel; /* indentation level:0=root level */
84 HTREEITEM parent; /* handle to parent or 0 if at root */
85 HTREEITEM firstChild; /* handle to first child or 0 if no child */
87 HTREEITEM prevSibling; /* handle to prev item in list, 0 if first */
88 HTREEITEM nextSibling; /* handle to next item in list, 0 if last */
94 LONG textWidth; /* horizontal text extent for pszText */
95 LONG visibleOrder; /* visible ordering, 0 is first visible item */
99 typedef struct tagTREEVIEW_INFO
102 HWND hwndNotify; /* Owner window to send notifications to */
105 UINT uInternalStatus;
107 UINT uNumItems; /* number of valid TREEVIEW_ITEMs */
108 INT cdmode; /* last custom draw setting */
109 UINT uScrollTime; /* max. time for scrolling in milliseconds */
110 BOOL bRedraw; /* if FALSE we validate but don't redraw in TREEVIEW_Paint() */
112 UINT uItemHeight; /* item height */
115 LONG clientWidth; /* width of control window */
116 LONG clientHeight; /* height of control window */
118 LONG treeWidth; /* width of visible tree items */
119 LONG treeHeight; /* height of visible tree items */
121 UINT uIndent; /* indentation in pixels */
122 HTREEITEM selectedItem; /* handle to selected item or 0 if none */
123 HTREEITEM hotItem; /* handle currently under cursor, 0 if none */
124 HTREEITEM focusedItem; /* item that was under the cursor when WM_LBUTTONDOWN was received */
125 HTREEITEM editItem; /* item being edited with builtin edit box */
127 HTREEITEM firstVisible; /* handle to first visible item */
128 LONG maxVisibleOrder;
129 HTREEITEM dropItem; /* handle to item selected by drag cursor */
130 HTREEITEM insertMarkItem; /* item after which insertion mark is placed */
131 BOOL insertBeforeorAfter; /* flag used by TVM_SETINSERTMARK */
132 HIMAGELIST dragList; /* Bitmap of dragged item */
137 COLORREF clrInsertMark;
141 HFONT hUnderlineFont;
146 WNDPROC wpEditOrig; /* orig window proc for subclassing edit */
147 BOOL bIgnoreEditKillFocus;
150 BOOL bNtfUnicode; /* TRUE if should send NOTIFY with W */
151 HIMAGELIST himlNormal;
152 int normalImageHeight;
153 int normalImageWidth;
154 HIMAGELIST himlState;
155 int stateImageHeight;
159 DWORD lastKeyPressTimestamp;
161 INT nSearchParamLength;
162 WCHAR szSearchParam[ MAX_PATH ];
166 /******** Defines that TREEVIEW_ProcessLetterKeys uses ****************/
167 #define KEY_DELAY 450
169 /* bitflags for infoPtr->uInternalStatus */
171 #define TV_HSCROLL 0x01 /* treeview too large to fit in window */
172 #define TV_VSCROLL 0x02 /* (horizontal/vertical) */
173 #define TV_LDRAG 0x04 /* Lbutton pushed to start drag */
174 #define TV_LDRAGGING 0x08 /* Lbutton pushed, mouse moved. */
175 #define TV_RDRAG 0x10 /* ditto Rbutton */
176 #define TV_RDRAGGING 0x20
178 /* bitflags for infoPtr->timer */
180 #define TV_EDIT_TIMER 2
181 #define TV_EDIT_TIMER_SET 2
183 #define TEXT_CALLBACK_SIZE 260
185 #define TREEVIEW_LEFT_MARGIN 8
187 #define MINIMUM_INDENT 19
189 #define CALLBACK_MASK_ALL (TVIF_TEXT|TVIF_CHILDREN|TVIF_IMAGE|TVIF_SELECTEDIMAGE)
191 #define STATEIMAGEINDEX(x) (((x) >> 12) & 0x0f)
192 #define OVERLAYIMAGEINDEX(x) (((x) >> 8) & 0x0f)
193 #define ISVISIBLE(x) ((x)->visibleOrder >= 0)
195 #define GETLINECOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrGrayText : (x))
196 #define GETBKCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindow : (x))
197 #define GETTXTCOLOR(x) ((x) == CLR_NONE ? comctl32_color.clrWindowText : (x))
198 #define GETINSCOLOR(x) ((x) == CLR_DEFAULT ? comctl32_color.clrBtnText : (x))
200 static const WCHAR themeClass[] = { 'T','r','e','e','v','i','e','w',0 };
203 typedef VOID (*TREEVIEW_ItemEnumFunc)(TREEVIEW_INFO *, TREEVIEW_ITEM *,LPVOID);
206 static VOID TREEVIEW_Invalidate(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
208 static LRESULT TREEVIEW_DoSelectItem(TREEVIEW_INFO *, INT, HTREEITEM, INT);
209 static VOID TREEVIEW_SetFirstVisible(TREEVIEW_INFO *, TREEVIEW_ITEM *, BOOL);
210 static LRESULT TREEVIEW_EnsureVisible(TREEVIEW_INFO *, HTREEITEM, BOOL);
211 static LRESULT TREEVIEW_RButtonUp(const TREEVIEW_INFO *, const POINT *);
212 static LRESULT TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel);
213 static VOID TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr);
214 static LRESULT TREEVIEW_HScroll(TREEVIEW_INFO *, WPARAM);
216 /* Random Utilities *****************************************************/
217 static void TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr);
219 /* Returns the treeview private data if hwnd is a treeview.
220 * Otherwise returns an undefined value. */
221 static inline TREEVIEW_INFO *
222 TREEVIEW_GetInfoPtr(HWND hwnd)
224 return (TREEVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
227 /* Don't call this. Nothing wants an item index. */
229 TREEVIEW_GetItemIndex(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
231 return DPA_GetPtrIndex(infoPtr->items, handle);
234 /* Checks if item has changed and needs to be redrawn */
235 static inline BOOL item_changed (const TREEVIEW_ITEM *tiOld, const TREEVIEW_ITEM *tiNew,
236 const TVITEMEXW *tvChange)
238 /* Number of children has changed */
239 if ((tvChange->mask & TVIF_CHILDREN) && (tiOld->cChildren != tiNew->cChildren))
242 /* Image has changed and it's not a callback */
243 if ((tvChange->mask & TVIF_IMAGE) && (tiOld->iImage != tiNew->iImage) &&
244 tiNew->iImage != I_IMAGECALLBACK)
247 /* Selected image has changed and it's not a callback */
248 if ((tvChange->mask & TVIF_SELECTEDIMAGE) && (tiOld->iSelectedImage != tiNew->iSelectedImage) &&
249 tiNew->iSelectedImage != I_IMAGECALLBACK)
252 if ((tvChange->mask & TVIF_EXPANDEDIMAGE) && (tiOld->iExpandedImage != tiNew->iExpandedImage) &&
253 tiNew->iExpandedImage != I_IMAGECALLBACK)
256 /* Text has changed and it's not a callback */
257 if ((tvChange->mask & TVIF_TEXT) && (tiOld->pszText != tiNew->pszText) &&
258 tiNew->pszText != LPSTR_TEXTCALLBACKW)
261 /* Indent has changed */
262 if ((tvChange->mask & TVIF_INTEGRAL) && (tiOld->iIntegral != tiNew->iIntegral))
265 /* Item state has changed */
266 if ((tvChange->mask & TVIF_STATE) && ((tiOld->state ^ tiNew->state) & tvChange->stateMask ))
272 /***************************************************************************
273 * This method checks that handle is an item for this tree.
276 TREEVIEW_ValidItem(const TREEVIEW_INFO *infoPtr, HTREEITEM handle)
278 if (TREEVIEW_GetItemIndex(infoPtr, handle) == -1)
280 TRACE("invalid item %p\n", handle);
288 TREEVIEW_CreateBoldFont(HFONT hOrigFont)
292 GetObjectW(hOrigFont, sizeof(font), &font);
293 font.lfWeight = FW_BOLD;
294 return CreateFontIndirectW(&font);
298 TREEVIEW_CreateUnderlineFont(HFONT hOrigFont)
302 GetObjectW(hOrigFont, sizeof(font), &font);
303 font.lfUnderline = TRUE;
304 return CreateFontIndirectW(&font);
308 TREEVIEW_FontForItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
310 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (item == infoPtr->hotItem))
311 return infoPtr->hUnderlineFont;
312 if (item->state & TVIS_BOLD)
313 return infoPtr->hBoldFont;
314 return infoPtr->hFont;
317 /* for trace/debugging purposes only */
319 TREEVIEW_ItemName(const TREEVIEW_ITEM *item)
321 if (item == NULL) return "<null item>";
322 if (item->pszText == LPSTR_TEXTCALLBACKW) return "<callback>";
323 if (item->pszText == NULL) return "<null>";
324 return debugstr_w(item->pszText);
327 /* An item is not a child of itself. */
329 TREEVIEW_IsChildOf(const TREEVIEW_ITEM *parent, const TREEVIEW_ITEM *child)
333 child = child->parent;
334 if (child == parent) return TRUE;
335 } while (child != NULL);
341 /* Tree Traversal *******************************************************/
343 /***************************************************************************
344 * This method returns the last expanded sibling or child child item
347 static TREEVIEW_ITEM *
348 TREEVIEW_GetLastListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
353 while (wineItem->lastChild)
355 if (wineItem->state & TVIS_EXPANDED)
356 wineItem = wineItem->lastChild;
361 if (wineItem == infoPtr->root)
367 /***************************************************************************
368 * This method returns the previous non-hidden item in the list not
369 * considering the tree hierarchy.
371 static TREEVIEW_ITEM *
372 TREEVIEW_GetPrevListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
374 if (tvItem->prevSibling)
376 /* This item has a prevSibling, get the last item in the sibling's tree. */
377 TREEVIEW_ITEM *upItem = tvItem->prevSibling;
379 if ((upItem->state & TVIS_EXPANDED) && upItem->lastChild != NULL)
380 return TREEVIEW_GetLastListItem(infoPtr, upItem->lastChild);
386 /* this item does not have a prevSibling, get the parent */
387 return (tvItem->parent != infoPtr->root) ? tvItem->parent : NULL;
392 /***************************************************************************
393 * This method returns the next physical item in the treeview not
394 * considering the tree hierarchy.
396 static TREEVIEW_ITEM *
397 TREEVIEW_GetNextListItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *tvItem)
400 * If this item has children and is expanded, return the first child
402 if ((tvItem->state & TVIS_EXPANDED) && tvItem->firstChild != NULL)
404 return tvItem->firstChild;
409 * try to get the sibling
411 if (tvItem->nextSibling)
412 return tvItem->nextSibling;
415 * Otherwise, get the parent's sibling.
417 while (tvItem->parent)
419 tvItem = tvItem->parent;
421 if (tvItem->nextSibling)
422 return tvItem->nextSibling;
428 /***************************************************************************
429 * This method returns the nth item starting at the given item. It returns
430 * the last item (or first) we we run out of items.
432 * Will scroll backward if count is <0.
433 * forward if count is >0.
435 static TREEVIEW_ITEM *
436 TREEVIEW_GetListItem(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
439 TREEVIEW_ITEM *(*next_item)(const TREEVIEW_INFO *, const TREEVIEW_ITEM *);
440 TREEVIEW_ITEM *previousItem;
442 assert(wineItem != NULL);
446 next_item = TREEVIEW_GetNextListItem;
451 next_item = TREEVIEW_GetPrevListItem;
458 previousItem = wineItem;
459 wineItem = next_item(infoPtr, wineItem);
461 } while (--count && wineItem != NULL);
464 return wineItem ? wineItem : previousItem;
467 /* Notifications ************************************************************/
469 static INT get_notifycode(const TREEVIEW_INFO *infoPtr, INT code)
471 if (!infoPtr->bNtfUnicode) {
473 case TVN_SELCHANGINGW: return TVN_SELCHANGINGA;
474 case TVN_SELCHANGEDW: return TVN_SELCHANGEDA;
475 case TVN_GETDISPINFOW: return TVN_GETDISPINFOA;
476 case TVN_SETDISPINFOW: return TVN_SETDISPINFOA;
477 case TVN_ITEMEXPANDINGW: return TVN_ITEMEXPANDINGA;
478 case TVN_ITEMEXPANDEDW: return TVN_ITEMEXPANDEDA;
479 case TVN_BEGINDRAGW: return TVN_BEGINDRAGA;
480 case TVN_BEGINRDRAGW: return TVN_BEGINRDRAGA;
481 case TVN_DELETEITEMW: return TVN_DELETEITEMA;
482 case TVN_BEGINLABELEDITW: return TVN_BEGINLABELEDITA;
483 case TVN_ENDLABELEDITW: return TVN_ENDLABELEDITA;
484 case TVN_GETINFOTIPW: return TVN_GETINFOTIPA;
491 TREEVIEW_SendRealNotify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
493 TRACE("wParam=%ld, lParam=%ld\n", wParam, lParam);
494 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, wParam, lParam);
498 TREEVIEW_SendSimpleNotify(const TREEVIEW_INFO *infoPtr, UINT code)
501 HWND hwnd = infoPtr->hwnd;
504 nmhdr.hwndFrom = hwnd;
505 nmhdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
506 nmhdr.code = get_notifycode(infoPtr, code);
508 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.idFrom, (LPARAM)&nmhdr);
512 TREEVIEW_TVItemFromItem(const TREEVIEW_INFO *infoPtr, UINT mask, TVITEMW *tvItem, TREEVIEW_ITEM *item)
515 tvItem->hItem = item;
516 tvItem->state = item->state;
517 tvItem->stateMask = 0;
518 tvItem->iImage = item->iImage;
519 tvItem->iSelectedImage = item->iSelectedImage;
520 tvItem->cChildren = item->cChildren;
521 tvItem->lParam = item->lParam;
525 if (!infoPtr->bNtfUnicode)
527 tvItem->cchTextMax = WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, NULL, 0, NULL, NULL );
528 tvItem->pszText = Alloc (tvItem->cchTextMax);
529 WideCharToMultiByte( CP_ACP, 0, item->pszText, -1, (LPSTR)tvItem->pszText, tvItem->cchTextMax, 0, 0 );
533 tvItem->cchTextMax = item->cchTextMax;
534 tvItem->pszText = item->pszText;
539 tvItem->cchTextMax = 0;
540 tvItem->pszText = NULL;
545 TREEVIEW_SendTreeviewNotify(const TREEVIEW_INFO *infoPtr, UINT code, UINT action,
546 UINT mask, HTREEITEM oldItem, HTREEITEM newItem)
548 HWND hwnd = infoPtr->hwnd;
552 TRACE("code:%d action:%x olditem:%p newitem:%p\n",
553 code, action, oldItem, newItem);
555 ZeroMemory(&nmhdr, sizeof(NMTREEVIEWW));
557 nmhdr.hdr.hwndFrom = hwnd;
558 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
559 nmhdr.hdr.code = get_notifycode(infoPtr, code);
560 nmhdr.action = action;
563 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemOld, oldItem);
566 TREEVIEW_TVItemFromItem(infoPtr, mask, &nmhdr.itemNew, newItem);
571 ret = TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
572 if (!infoPtr->bNtfUnicode)
574 Free(nmhdr.itemOld.pszText);
575 Free(nmhdr.itemNew.pszText);
581 TREEVIEW_SendTreeviewDnDNotify(const TREEVIEW_INFO *infoPtr, UINT code,
582 HTREEITEM dragItem, POINT pt)
584 HWND hwnd = infoPtr->hwnd;
587 TRACE("code:%d dragitem:%p\n", code, dragItem);
589 nmhdr.hdr.hwndFrom = hwnd;
590 nmhdr.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
591 nmhdr.hdr.code = get_notifycode(infoPtr, code);
593 nmhdr.itemNew.mask = TVIF_STATE | TVIF_PARAM | TVIF_HANDLE;
594 nmhdr.itemNew.hItem = dragItem;
595 nmhdr.itemNew.state = dragItem->state;
596 nmhdr.itemNew.lParam = dragItem->lParam;
598 nmhdr.ptDrag.x = pt.x;
599 nmhdr.ptDrag.y = pt.y;
601 return TREEVIEW_SendRealNotify(infoPtr, nmhdr.hdr.idFrom, (LPARAM)&nmhdr);
606 TREEVIEW_SendCustomDrawNotify(const TREEVIEW_INFO *infoPtr, DWORD dwDrawStage,
609 HWND hwnd = infoPtr->hwnd;
610 NMTVCUSTOMDRAW nmcdhdr;
613 TRACE("drawstage:%x hdc:%p\n", dwDrawStage, hdc);
615 nmcd = &nmcdhdr.nmcd;
616 nmcd->hdr.hwndFrom = hwnd;
617 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
618 nmcd->hdr.code = NM_CUSTOMDRAW;
619 nmcd->dwDrawStage = dwDrawStage;
622 nmcd->dwItemSpec = 0;
623 nmcd->uItemState = 0;
624 nmcd->lItemlParam = 0;
625 nmcdhdr.clrText = infoPtr->clrText;
626 nmcdhdr.clrTextBk = infoPtr->clrBk;
629 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)&nmcdhdr);
634 /* FIXME: need to find out when the flags in uItemState need to be set */
637 TREEVIEW_SendCustomDrawItemNotify(const TREEVIEW_INFO *infoPtr, HDC hdc,
638 TREEVIEW_ITEM *wineItem, UINT uItemDrawState,
639 NMTVCUSTOMDRAW *nmcdhdr)
641 HWND hwnd = infoPtr->hwnd;
644 DWORD_PTR dwItemSpec;
647 dwDrawStage = CDDS_ITEM | uItemDrawState;
648 dwItemSpec = (DWORD_PTR)wineItem;
650 if (wineItem->state & TVIS_SELECTED)
651 uItemState |= CDIS_SELECTED;
652 if (wineItem == infoPtr->selectedItem)
653 uItemState |= CDIS_FOCUS;
654 if (wineItem == infoPtr->hotItem)
655 uItemState |= CDIS_HOT;
657 nmcd = &nmcdhdr->nmcd;
658 nmcd->hdr.hwndFrom = hwnd;
659 nmcd->hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
660 nmcd->hdr.code = NM_CUSTOMDRAW;
661 nmcd->dwDrawStage = dwDrawStage;
663 nmcd->rc = wineItem->rect;
664 nmcd->dwItemSpec = dwItemSpec;
665 nmcd->uItemState = uItemState;
666 nmcd->lItemlParam = wineItem->lParam;
667 nmcdhdr->iLevel = wineItem->iLevel;
669 TRACE("drawstage:%x hdc:%p item:%lx, itemstate:%x, lItemlParam:%lx\n",
670 nmcd->dwDrawStage, nmcd->hdc, nmcd->dwItemSpec,
671 nmcd->uItemState, nmcd->lItemlParam);
673 return TREEVIEW_SendRealNotify(infoPtr, nmcd->hdr.idFrom, (LPARAM)nmcdhdr);
677 TREEVIEW_BeginLabelEditNotify(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *editItem)
679 HWND hwnd = infoPtr->hwnd;
683 tvdi.hdr.hwndFrom = hwnd;
684 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
685 tvdi.hdr.code = get_notifycode(infoPtr, TVN_BEGINLABELEDITW);
687 TREEVIEW_TVItemFromItem(infoPtr, TVIF_HANDLE | TVIF_STATE | TVIF_PARAM | TVIF_TEXT,
688 &tvdi.item, editItem);
690 ret = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
692 if (!infoPtr->bNtfUnicode)
693 Free(tvdi.item.pszText);
699 TREEVIEW_UpdateDispInfo(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
702 NMTVDISPINFOEXW callback;
703 HWND hwnd = infoPtr->hwnd;
705 TRACE("mask %x callbackMask %x\n", mask, wineItem->callbackMask);
706 mask &= wineItem->callbackMask;
708 if (mask == 0) return;
710 callback.hdr.hwndFrom = hwnd;
711 callback.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
712 callback.hdr.code = get_notifycode(infoPtr, TVN_GETDISPINFOW);
714 /* 'state' always contains valid value, as well as 'lParam'.
715 * All other parameters are uninitialized.
717 callback.item.pszText = wineItem->pszText;
718 callback.item.cchTextMax = wineItem->cchTextMax;
719 callback.item.mask = mask;
720 callback.item.hItem = wineItem;
721 callback.item.state = wineItem->state;
722 callback.item.lParam = wineItem->lParam;
724 /* If text is changed we need to recalculate textWidth */
725 if (mask & TVIF_TEXT)
726 wineItem->textWidth = 0;
728 TREEVIEW_SendRealNotify(infoPtr, callback.hdr.idFrom, (LPARAM)&callback);
730 /* It may have changed due to a call to SetItem. */
731 mask &= wineItem->callbackMask;
733 if ((mask & TVIF_TEXT) && callback.item.pszText != wineItem->pszText)
735 /* Instead of copying text into our buffer user specified its own */
736 if (!infoPtr->bNtfUnicode) {
739 int len = MultiByteToWideChar( CP_ACP, 0,
740 (LPSTR)callback.item.pszText, -1,
742 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
743 newText = ReAlloc(wineItem->pszText, buflen);
745 TRACE("returned str %s, len=%d, buflen=%d\n",
746 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
750 wineItem->pszText = newText;
751 MultiByteToWideChar( CP_ACP, 0,
752 (LPSTR)callback.item.pszText, -1,
753 wineItem->pszText, buflen/sizeof(WCHAR));
754 wineItem->cchTextMax = buflen/sizeof(WCHAR);
756 /* If ReAlloc fails we have nothing to do, but keep original text */
759 int len = max(lstrlenW(callback.item.pszText) + 1,
761 LPWSTR newText = ReAlloc(wineItem->pszText, len);
763 TRACE("returned wstr %s, len=%d\n",
764 debugstr_w(callback.item.pszText), len);
768 wineItem->pszText = newText;
769 strcpyW(wineItem->pszText, callback.item.pszText);
770 wineItem->cchTextMax = len;
772 /* If ReAlloc fails we have nothing to do, but keep original text */
775 else if (mask & TVIF_TEXT) {
776 /* User put text into our buffer, that is ok unless A string */
777 if (!infoPtr->bNtfUnicode) {
779 LPWSTR oldText = NULL;
781 int len = MultiByteToWideChar( CP_ACP, 0,
782 (LPSTR)callback.item.pszText, -1,
784 buflen = max((len)*sizeof(WCHAR), TEXT_CALLBACK_SIZE);
785 newText = Alloc(buflen);
787 TRACE("same buffer str %s, len=%d, buflen=%d\n",
788 debugstr_a((LPSTR)callback.item.pszText), len, buflen);
792 oldText = wineItem->pszText;
793 wineItem->pszText = newText;
794 MultiByteToWideChar( CP_ACP, 0,
795 (LPSTR)callback.item.pszText, -1,
796 wineItem->pszText, buflen/sizeof(WCHAR));
797 wineItem->cchTextMax = buflen/sizeof(WCHAR);
803 if (mask & TVIF_IMAGE)
804 wineItem->iImage = callback.item.iImage;
806 if (mask & TVIF_SELECTEDIMAGE)
807 wineItem->iSelectedImage = callback.item.iSelectedImage;
809 if (mask & TVIF_EXPANDEDIMAGE)
810 wineItem->iExpandedImage = callback.item.iExpandedImage;
812 if (mask & TVIF_CHILDREN)
813 wineItem->cChildren = callback.item.cChildren;
815 /* These members are now permanently set. */
816 if (callback.item.mask & TVIF_DI_SETITEM)
817 wineItem->callbackMask &= ~callback.item.mask;
820 /***************************************************************************
821 * This function uses cChildren field to decide whether the item has
823 * Note: if this returns TRUE, the child items may not actually exist,
824 * they could be virtual.
826 * Just use wineItem->firstChild to check for physical children.
829 TREEVIEW_HasChildren(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
831 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_CHILDREN);
833 return wineItem->cChildren > 0;
836 static INT TREEVIEW_NotifyFormat (TREEVIEW_INFO *infoPtr, HWND hwndFrom, UINT nCommand)
840 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
842 if (nCommand != NF_REQUERY) return 0;
844 format = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwnd, NF_QUERY);
845 TRACE("format=%d\n", format);
847 if (format != NFR_ANSI && format != NFR_UNICODE) return 0;
849 infoPtr->bNtfUnicode = (format == NFR_UNICODE);
854 /* Item Position ********************************************************/
856 /* Compute linesOffset, stateOffset, imageOffset, textOffset of an item. */
858 TREEVIEW_ComputeItemInternalMetrics(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
860 /* has TVS_LINESATROOT and (TVS_HASLINES|TVS_HASBUTTONS) */
861 BOOL lar = ((infoPtr->dwStyle & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
864 item->linesOffset = infoPtr->uIndent * (lar ? item->iLevel : item->iLevel - 1)
866 item->stateOffset = item->linesOffset + infoPtr->uIndent;
867 item->imageOffset = item->stateOffset
868 + (STATEIMAGEINDEX(item->state) ? infoPtr->stateImageWidth : 0);
869 item->textOffset = item->imageOffset + infoPtr->normalImageWidth;
873 TREEVIEW_ComputeTextWidth(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item, HDC hDC)
879 /* DRAW's OM docker creates items like this */
880 if (item->pszText == NULL)
892 hdc = GetDC(infoPtr->hwnd);
893 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, item));
896 GetTextExtentPoint32W(hdc, item->pszText, strlenW(item->pszText), &sz);
897 item->textWidth = sz.cx;
901 SelectObject(hdc, hOldFont);
907 TREEVIEW_ComputeItemRect(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
909 item->rect.top = infoPtr->uItemHeight *
910 (item->visibleOrder - infoPtr->firstVisible->visibleOrder);
912 item->rect.bottom = item->rect.top
913 + infoPtr->uItemHeight * item->iIntegral - 1;
916 item->rect.right = infoPtr->clientWidth;
919 /* We know that only items after start need their order updated. */
921 TREEVIEW_RecalculateVisibleOrder(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *start)
928 start = infoPtr->root->firstChild;
932 order = start->visibleOrder;
934 for (item = start; item != NULL;
935 item = TREEVIEW_GetNextListItem(infoPtr, item))
937 if (!ISVISIBLE(item) && order > 0)
938 TREEVIEW_ComputeItemInternalMetrics(infoPtr, item);
939 item->visibleOrder = order;
940 order += item->iIntegral;
943 infoPtr->maxVisibleOrder = order;
945 for (item = start; item != NULL;
946 item = TREEVIEW_GetNextListItem(infoPtr, item))
948 TREEVIEW_ComputeItemRect(infoPtr, item);
953 /* Update metrics of all items in selected subtree.
954 * root must be expanded
957 TREEVIEW_UpdateSubTree(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *root)
959 TREEVIEW_ITEM *sibling;
963 if (!root->firstChild || !(root->state & TVIS_EXPANDED))
966 root->state &= ~TVIS_EXPANDED;
967 sibling = TREEVIEW_GetNextListItem(infoPtr, root);
968 root->state |= TVIS_EXPANDED;
970 hdc = GetDC(infoPtr->hwnd);
971 hOldFont = SelectObject(hdc, infoPtr->hFont);
973 for (; root != sibling;
974 root = TREEVIEW_GetNextListItem(infoPtr, root))
976 TREEVIEW_ComputeItemInternalMetrics(infoPtr, root);
978 if (root->callbackMask & TVIF_TEXT)
979 TREEVIEW_UpdateDispInfo(infoPtr, root, TVIF_TEXT);
981 if (root->textWidth == 0)
983 SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, root));
984 TREEVIEW_ComputeTextWidth(infoPtr, root, hdc);
988 SelectObject(hdc, hOldFont);
989 ReleaseDC(infoPtr->hwnd, hdc);
992 /* Item Allocation **********************************************************/
994 static TREEVIEW_ITEM *
995 TREEVIEW_AllocateItem(const TREEVIEW_INFO *infoPtr)
997 TREEVIEW_ITEM *newItem = Alloc(sizeof(TREEVIEW_ITEM));
1002 /* I_IMAGENONE would make more sense but this is neither what is
1003 * documented (MSDN doesn't specify) nor what Windows actually does
1004 * (it sets it to zero)... and I can so imagine an application using
1005 * inc/dec to toggle the images. */
1006 newItem->iImage = 0;
1007 newItem->iSelectedImage = 0;
1008 newItem->iExpandedImage = 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_EXPANDEDIMAGE)
1187 wineItem->iExpandedImage = tvItem->iExpandedImage;
1189 if (wineItem->iExpandedImage == I_IMAGECALLBACK)
1190 callbackSet |= TVIF_EXPANDEDIMAGE;
1192 callbackClear |= TVIF_EXPANDEDIMAGE;
1195 if (tvItem->mask & TVIF_PARAM)
1196 wineItem->lParam = tvItem->lParam;
1198 /* If the application sets TVIF_INTEGRAL without
1199 * supplying a TVITEMEX structure, it's toast. */
1200 if (tvItem->mask & TVIF_INTEGRAL)
1201 wineItem->iIntegral = tvItem->iIntegral;
1203 if (tvItem->mask & TVIF_STATE)
1205 TRACE("prevstate,state,mask:%x,%x,%x\n", wineItem->state, tvItem->state,
1207 wineItem->state &= ~tvItem->stateMask;
1208 wineItem->state |= (tvItem->state & tvItem->stateMask);
1211 wineItem->callbackMask |= callbackSet;
1212 wineItem->callbackMask &= ~callbackClear;
1217 /* Note that the new item is pre-zeroed. */
1219 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1221 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1222 HTREEITEM insertAfter;
1223 TREEVIEW_ITEM *newItem, *parentItem;
1224 BOOL bTextUpdated = FALSE;
1226 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1228 parentItem = infoPtr->root;
1232 parentItem = ptdi->hParent;
1234 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1236 WARN("invalid parent %p\n", parentItem);
1241 insertAfter = ptdi->hInsertAfter;
1243 /* Validate this now for convenience. */
1244 switch ((DWORD_PTR)insertAfter)
1246 case (DWORD_PTR)TVI_FIRST:
1247 case (DWORD_PTR)TVI_LAST:
1248 case (DWORD_PTR)TVI_SORT:
1252 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1253 insertAfter->parent != parentItem)
1255 WARN("invalid insert after %p\n", insertAfter);
1256 insertAfter = TVI_LAST;
1260 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1261 (tvItem->mask & TVIF_TEXT)
1262 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1263 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1266 newItem = TREEVIEW_AllocateItem(infoPtr);
1267 if (newItem == NULL)
1270 newItem->parent = parentItem;
1271 newItem->iIntegral = 1;
1272 newItem->visibleOrder = -1;
1274 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1277 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1279 infoPtr->uNumItems++;
1281 switch ((DWORD_PTR)insertAfter)
1283 case (DWORD_PTR)TVI_FIRST:
1285 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1286 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1287 if (infoPtr->firstVisible == originalFirst)
1288 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1292 case (DWORD_PTR)TVI_LAST:
1293 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1296 /* hInsertAfter names a specific item we want to insert after */
1298 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1301 case (DWORD_PTR)TVI_SORT:
1303 TREEVIEW_ITEM *aChild;
1304 TREEVIEW_ITEM *previousChild = NULL;
1305 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1306 BOOL bItemInserted = FALSE;
1308 aChild = parentItem->firstChild;
1310 bTextUpdated = TRUE;
1311 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1313 /* Iterate the parent children to see where we fit in */
1314 while (aChild != NULL)
1318 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1319 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1321 if (comp < 0) /* we are smaller than the current one */
1323 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1324 if (infoPtr->firstVisible == originalFirst &&
1325 aChild == originalFirst)
1326 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1327 bItemInserted = TRUE;
1330 else if (comp > 0) /* we are bigger than the current one */
1332 previousChild = aChild;
1334 /* This will help us to exit if there is no more sibling */
1335 aChild = (aChild->nextSibling == 0)
1337 : aChild->nextSibling;
1339 /* Look at the next item */
1345 * An item with this name is already existing, therefore,
1346 * we add after the one we found
1348 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1349 bItemInserted = TRUE;
1355 * we reach the end of the child list and the item has not
1356 * yet been inserted, therefore, insert it after the last child.
1358 if ((!bItemInserted) && (aChild == NULL))
1359 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1366 TRACE("new item %p; parent %p, mask %x\n", newItem,
1367 newItem->parent, tvItem->mask);
1369 newItem->iLevel = newItem->parent->iLevel + 1;
1371 if (newItem->parent->cChildren == 0)
1372 newItem->parent->cChildren = 1;
1374 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1376 if (STATEIMAGEINDEX(newItem->state) == 0)
1377 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1380 if (infoPtr->firstVisible == NULL)
1381 infoPtr->firstVisible = newItem;
1383 TREEVIEW_VerifyTree(infoPtr);
1385 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1387 if (parentItem == infoPtr->root ||
1388 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1390 TREEVIEW_ITEM *item;
1391 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1393 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1394 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1397 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1399 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1400 TREEVIEW_UpdateScrollBars(infoPtr);
1402 * if the item was inserted in a visible part of the tree,
1403 * invalidate it, as well as those after it
1405 for (item = newItem;
1407 item = TREEVIEW_GetNextListItem(infoPtr, item))
1408 TREEVIEW_Invalidate(infoPtr, item);
1412 /* refresh treeview if newItem is the first item inserted under parentItem */
1413 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1415 /* parent got '+' - update it */
1416 TREEVIEW_Invalidate(infoPtr, parentItem);
1420 return (LRESULT)newItem;
1423 /* Item Deletion ************************************************************/
1425 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1428 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1430 TREEVIEW_ITEM *kill = parentItem->firstChild;
1432 while (kill != NULL)
1434 TREEVIEW_ITEM *next = kill->nextSibling;
1436 TREEVIEW_RemoveItem(infoPtr, kill);
1441 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1442 assert(parentItem->firstChild == NULL);
1443 assert(parentItem->lastChild == NULL);
1447 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1449 TREEVIEW_ITEM *parentItem = item->parent;
1451 assert(item != NULL);
1452 assert(item->parent != NULL); /* i.e. it must not be the root */
1454 if (parentItem->firstChild == item)
1455 parentItem->firstChild = item->nextSibling;
1457 if (parentItem->lastChild == item)
1458 parentItem->lastChild = item->prevSibling;
1460 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1461 && parentItem->cChildren > 0)
1462 parentItem->cChildren = 0;
1464 if (item->prevSibling)
1465 item->prevSibling->nextSibling = item->nextSibling;
1467 if (item->nextSibling)
1468 item->nextSibling->prevSibling = item->prevSibling;
1472 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1474 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1476 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1477 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1479 if (wineItem->firstChild)
1480 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1482 TREEVIEW_UnlinkItem(wineItem);
1484 infoPtr->uNumItems--;
1486 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1487 Free(wineItem->pszText);
1489 TREEVIEW_FreeItem(infoPtr, wineItem);
1493 /* Empty out the tree. */
1495 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1497 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1499 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1503 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1505 TREEVIEW_ITEM *newSelection = NULL;
1506 TREEVIEW_ITEM *newFirstVisible = NULL;
1507 TREEVIEW_ITEM *parent, *prev = NULL;
1508 BOOL visible = FALSE;
1510 if (wineItem == TVI_ROOT)
1512 TRACE("TVI_ROOT\n");
1513 parent = infoPtr->root;
1514 newSelection = NULL;
1516 TREEVIEW_RemoveTree(infoPtr);
1520 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1523 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1524 parent = wineItem->parent;
1526 if (ISVISIBLE(wineItem))
1528 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1532 if (infoPtr->selectedItem != NULL
1533 && (wineItem == infoPtr->selectedItem
1534 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1536 if (wineItem->nextSibling)
1537 newSelection = wineItem->nextSibling;
1538 else if (wineItem->parent != infoPtr->root)
1539 newSelection = wineItem->parent;
1541 newSelection = wineItem->prevSibling;
1542 TRACE("newSelection = %p\n", newSelection);
1545 if (infoPtr->firstVisible == wineItem)
1547 if (wineItem->nextSibling)
1548 newFirstVisible = wineItem->nextSibling;
1549 else if (wineItem->prevSibling)
1550 newFirstVisible = wineItem->prevSibling;
1551 else if (wineItem->parent != infoPtr->root)
1552 newFirstVisible = wineItem->parent;
1553 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1556 newFirstVisible = infoPtr->firstVisible;
1558 TREEVIEW_RemoveItem(infoPtr, wineItem);
1561 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1562 if (!infoPtr->selectedItem && newSelection)
1564 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1565 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1568 /* Validate insertMark dropItem.
1569 * hotItem ??? - used for comparison only.
1571 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1572 infoPtr->insertMarkItem = 0;
1574 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1575 infoPtr->dropItem = 0;
1577 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1578 newFirstVisible = infoPtr->root->firstChild;
1580 TREEVIEW_VerifyTree(infoPtr);
1582 if (!infoPtr->bRedraw) return TRUE;
1586 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1587 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1588 TREEVIEW_UpdateScrollBars(infoPtr);
1589 TREEVIEW_Invalidate(infoPtr, NULL);
1591 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1593 /* parent lost '+/-' - update it */
1594 TREEVIEW_Invalidate(infoPtr, parent);
1601 /* Get/Set Messages *********************************************************/
1603 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1605 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1607 if (infoPtr->bRedraw)
1609 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1610 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1611 TREEVIEW_UpdateScrollBars(infoPtr);
1612 TREEVIEW_Invalidate(infoPtr, NULL);
1618 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1621 return infoPtr->uIndent;
1625 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1629 if (newIndent < MINIMUM_INDENT)
1630 newIndent = MINIMUM_INDENT;
1632 if (infoPtr->uIndent != newIndent)
1634 infoPtr->uIndent = newIndent;
1635 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1636 TREEVIEW_UpdateScrollBars(infoPtr);
1637 TREEVIEW_Invalidate(infoPtr, NULL);
1645 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1648 return (LRESULT)infoPtr->hwndToolTip;
1652 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1657 prevToolTip = infoPtr->hwndToolTip;
1658 infoPtr->hwndToolTip = hwndTT;
1660 return (LRESULT)prevToolTip;
1664 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1666 BOOL rc = infoPtr->bNtfUnicode;
1667 infoPtr->bNtfUnicode = fUnicode;
1672 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1674 return infoPtr->bNtfUnicode;
1678 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1680 return infoPtr->uScrollTime;
1684 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1686 UINT uOldScrollTime = infoPtr->uScrollTime;
1688 infoPtr->uScrollTime = min(uScrollTime, 100);
1690 return uOldScrollTime;
1695 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1702 return (LRESULT)infoPtr->himlNormal;
1705 return (LRESULT)infoPtr->himlState;
1712 #define TVHEIGHT_MIN 16
1713 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1715 /* Compute the natural height for items. */
1717 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1721 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1724 /* Height is the maximum of:
1725 * 16 (a hack because our fonts are tiny), and
1726 * The text height + border & margin, and
1727 * The size of the normal image list
1729 GetTextMetricsW(hdc, &tm);
1730 SelectObject(hdc, hOldFont);
1733 height = TVHEIGHT_MIN;
1734 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1735 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1736 if (height < infoPtr->normalImageHeight)
1737 height = infoPtr->normalImageHeight;
1739 /* Round down, unless we support odd ("non even") heights. */
1740 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1747 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1749 HIMAGELIST himlOld = 0;
1750 int oldWidth = infoPtr->normalImageWidth;
1751 int oldHeight = infoPtr->normalImageHeight;
1754 TRACE("%lx,%p\n", wParam, himlNew);
1759 himlOld = infoPtr->himlNormal;
1760 infoPtr->himlNormal = himlNew;
1762 if (himlNew != NULL)
1763 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1764 &infoPtr->normalImageHeight);
1767 infoPtr->normalImageWidth = 0;
1768 infoPtr->normalImageHeight = 0;
1774 himlOld = infoPtr->himlState;
1775 infoPtr->himlState = himlNew;
1777 if (himlNew != NULL)
1778 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1779 &infoPtr->stateImageHeight);
1782 infoPtr->stateImageWidth = 0;
1783 infoPtr->stateImageHeight = 0;
1789 if (oldWidth != infoPtr->normalImageWidth ||
1790 oldHeight != infoPtr->normalImageHeight)
1792 BOOL bRecalcVisible = FALSE;
1794 if (oldHeight != infoPtr->normalImageHeight &&
1795 !infoPtr->bHeightSet)
1797 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1798 bRecalcVisible = TRUE;
1801 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1802 infoPtr->normalImageWidth != infoPtr->uIndent)
1804 infoPtr->uIndent = infoPtr->normalImageWidth;
1805 bRecalcVisible = TRUE;
1809 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1811 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1812 TREEVIEW_UpdateScrollBars(infoPtr);
1815 TREEVIEW_Invalidate(infoPtr, NULL);
1817 return (LRESULT)himlOld;
1821 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1823 INT prevHeight = infoPtr->uItemHeight;
1825 TRACE("%d\n", newHeight);
1826 if (newHeight == -1)
1828 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1829 infoPtr->bHeightSet = FALSE;
1833 infoPtr->uItemHeight = newHeight;
1834 infoPtr->bHeightSet = TRUE;
1837 /* Round down, unless we support odd ("non even") heights. */
1838 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1839 infoPtr->uItemHeight &= ~1;
1841 if (infoPtr->uItemHeight != prevHeight)
1843 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1844 TREEVIEW_UpdateScrollBars(infoPtr);
1845 TREEVIEW_Invalidate(infoPtr, NULL);
1852 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1855 return infoPtr->uItemHeight;
1860 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1862 TRACE("%p\n", infoPtr->hFont);
1863 return (LRESULT)infoPtr->hFont;
1868 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1872 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1878 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1880 UINT uHeight = infoPtr->uItemHeight;
1882 TRACE("%p %i\n", hFont, bRedraw);
1884 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1886 DeleteObject(infoPtr->hBoldFont);
1887 DeleteObject(infoPtr->hUnderlineFont);
1888 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1889 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1891 if (!infoPtr->bHeightSet)
1892 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1894 if (uHeight != infoPtr->uItemHeight)
1895 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1897 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1899 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1900 TREEVIEW_UpdateScrollBars(infoPtr);
1903 TREEVIEW_Invalidate(infoPtr, NULL);
1910 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1913 return (LRESULT)infoPtr->clrLine;
1917 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1919 COLORREF prevColor = infoPtr->clrLine;
1922 infoPtr->clrLine = color;
1923 return (LRESULT)prevColor;
1928 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1931 return (LRESULT)infoPtr->clrText;
1935 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1937 COLORREF prevColor = infoPtr->clrText;
1940 infoPtr->clrText = color;
1942 if (infoPtr->clrText != prevColor)
1943 TREEVIEW_Invalidate(infoPtr, NULL);
1945 return (LRESULT)prevColor;
1950 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1953 return (LRESULT)infoPtr->clrBk;
1957 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1959 COLORREF prevColor = infoPtr->clrBk;
1962 infoPtr->clrBk = newColor;
1964 if (newColor != prevColor)
1965 TREEVIEW_Invalidate(infoPtr, NULL);
1967 return (LRESULT)prevColor;
1972 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1975 return (LRESULT)infoPtr->clrInsertMark;
1979 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1981 COLORREF prevColor = infoPtr->clrInsertMark;
1983 TRACE("%x\n", color);
1984 infoPtr->clrInsertMark = color;
1986 return (LRESULT)prevColor;
1991 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1993 TRACE("%d %p\n", wParam, item);
1995 if (!TREEVIEW_ValidItem(infoPtr, item))
1998 infoPtr->insertBeforeorAfter = wParam;
1999 infoPtr->insertMarkItem = item;
2001 TREEVIEW_Invalidate(infoPtr, NULL);
2007 /************************************************************************
2008 * Some serious braindamage here. lParam is a pointer to both the
2009 * input HTREEITEM and the output RECT.
2012 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2014 TREEVIEW_ITEM *wineItem;
2015 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2019 * validate parameters
2025 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2029 * If wParam is TRUE return the text size otherwise return
2030 * the whole item size
2034 /* Windows does not send TVN_GETDISPINFO here. */
2036 lpRect->top = wineItem->rect.top;
2037 lpRect->bottom = wineItem->rect.bottom;
2039 lpRect->left = wineItem->textOffset;
2040 if (!wineItem->textWidth)
2041 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2043 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2047 *lpRect = wineItem->rect;
2050 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2055 static inline LRESULT
2056 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2058 /* Surprise! This does not take integral height into account. */
2059 return infoPtr->clientHeight / infoPtr->uItemHeight;
2064 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2066 TREEVIEW_ITEM *wineItem;
2068 wineItem = tvItem->hItem;
2069 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2072 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2074 if (tvItem->mask & TVIF_CHILDREN)
2076 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2077 FIXME("I_CHILDRENCALLBACK not supported\n");
2078 tvItem->cChildren = wineItem->cChildren;
2081 if (tvItem->mask & TVIF_HANDLE)
2082 tvItem->hItem = wineItem;
2084 if (tvItem->mask & TVIF_IMAGE)
2085 tvItem->iImage = wineItem->iImage;
2087 if (tvItem->mask & TVIF_INTEGRAL)
2088 tvItem->iIntegral = wineItem->iIntegral;
2090 /* undocumented: windows ignores TVIF_PARAM and
2091 * * always sets lParam
2093 tvItem->lParam = wineItem->lParam;
2095 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2096 tvItem->iSelectedImage = wineItem->iSelectedImage;
2098 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2099 tvItem->iExpandedImage = wineItem->iExpandedImage;
2101 if (tvItem->mask & TVIF_STATE)
2102 /* Careful here - Windows ignores the stateMask when you get the state
2103 That contradicts the documentation, but makes more common sense, masking
2104 retrieval in this way seems overkill */
2105 tvItem->state = wineItem->state;
2107 if (tvItem->mask & TVIF_TEXT)
2109 if (wineItem->pszText == NULL)
2111 if (tvItem->cchTextMax > 0)
2112 tvItem->pszText[0] = '\0';
2116 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2118 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2119 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2123 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2128 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2130 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2131 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2135 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2136 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2140 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2141 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2146 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2147 * which is wrong. */
2149 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2151 TREEVIEW_ITEM *wineItem;
2152 TREEVIEW_ITEM originalItem;
2154 wineItem = tvItem->hItem;
2156 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2159 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2162 /* store the original item values */
2163 originalItem = *wineItem;
2165 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2168 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2169 if ((tvItem->mask & TVIF_TEXT
2170 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2171 && ISVISIBLE(wineItem))
2173 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2174 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2177 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2179 /* The refresh updates everything, but we can't wait until then. */
2180 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2182 /* if any of the item's values changed and it's not a callback, redraw the item */
2183 if (item_changed(&originalItem, wineItem, tvItem))
2185 if (tvItem->mask & TVIF_INTEGRAL)
2187 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2188 TREEVIEW_UpdateScrollBars(infoPtr);
2190 TREEVIEW_Invalidate(infoPtr, NULL);
2194 TREEVIEW_UpdateScrollBars(infoPtr);
2195 TREEVIEW_Invalidate(infoPtr, wineItem);
2204 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2208 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2211 return (wineItem->state & mask);
2215 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2217 TREEVIEW_ITEM *retval;
2221 /* handle all the global data here */
2224 case TVGN_CHILD: /* Special case: child of 0 is root */
2229 retval = infoPtr->root->firstChild;
2233 retval = infoPtr->selectedItem;
2236 case TVGN_FIRSTVISIBLE:
2237 retval = infoPtr->firstVisible;
2240 case TVGN_DROPHILITE:
2241 retval = infoPtr->dropItem;
2244 case TVGN_LASTVISIBLE:
2245 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2251 TRACE("flags:%x, returns %p\n", which, retval);
2252 return (LRESULT)retval;
2255 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2257 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2263 retval = wineItem->nextSibling;
2266 retval = wineItem->prevSibling;
2269 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2272 retval = wineItem->firstChild;
2274 case TVGN_NEXTVISIBLE:
2275 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2277 case TVGN_PREVIOUSVISIBLE:
2278 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2281 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2285 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2286 return (LRESULT)retval;
2291 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2293 TRACE(" %d\n", infoPtr->uNumItems);
2294 return (LRESULT)infoPtr->uNumItems;
2298 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2300 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2302 static const unsigned int state_table[] = { 0, 2, 1 };
2306 state = STATEIMAGEINDEX(item->state);
2307 TRACE("state:%x\n", state);
2308 item->state &= ~TVIS_STATEIMAGEMASK;
2311 state = state_table[state];
2313 item->state |= INDEXTOSTATEIMAGEMASK(state);
2315 TRACE("state:%x\n", state);
2316 TREEVIEW_Invalidate(infoPtr, item);
2321 /* Painting *************************************************************/
2323 /* Draw the lines and expand button for an item. Also draws one section
2324 * of the line from item's parent to item's parent's next sibling. */
2326 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2328 LONG centerx, centery;
2329 BOOL lar = ((infoPtr->dwStyle
2330 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2333 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2335 if (!lar && item->iLevel == 0)
2338 hbr = CreateSolidBrush(clrBk);
2339 hbrOld = SelectObject(hdc, hbr);
2341 centerx = (item->linesOffset + item->stateOffset) / 2;
2342 centery = (item->rect.top + item->rect.bottom) / 2;
2344 if (infoPtr->dwStyle & TVS_HASLINES)
2346 HPEN hOldPen, hNewPen;
2350 /* Get a dotted grey pen */
2351 lb.lbStyle = BS_SOLID;
2352 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2353 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2354 hOldPen = SelectObject(hdc, hNewPen);
2356 /* Make sure the center is on a dot (using +2 instead
2357 * of +1 gives us pixel-by-pixel compat with native) */
2358 centery = (centery + 2) & ~1;
2360 MoveToEx(hdc, item->stateOffset, centery, NULL);
2361 LineTo(hdc, centerx - 1, centery);
2363 if (item->prevSibling || item->parent != infoPtr->root)
2365 MoveToEx(hdc, centerx, item->rect.top, NULL);
2366 LineTo(hdc, centerx, centery);
2369 if (item->nextSibling)
2371 MoveToEx(hdc, centerx, centery, NULL);
2372 LineTo(hdc, centerx, item->rect.bottom + 1);
2375 /* Draw the line from our parent to its next sibling. */
2376 parent = item->parent;
2377 while (parent != infoPtr->root)
2379 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2381 if (parent->nextSibling
2382 /* skip top-levels unless TVS_LINESATROOT */
2383 && parent->stateOffset > parent->linesOffset)
2385 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2386 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2389 parent = parent->parent;
2392 SelectObject(hdc, hOldPen);
2393 DeleteObject(hNewPen);
2397 * Display the (+/-) signs
2400 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2402 if (item->cChildren)
2404 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2407 RECT glyphRect = item->rect;
2408 glyphRect.left = item->linesOffset;
2409 glyphRect.right = item->stateOffset;
2410 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2411 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2416 LONG height = item->rect.bottom - item->rect.top;
2417 LONG width = item->stateOffset - item->linesOffset;
2418 LONG rectsize = min(height, width) / 4;
2419 /* plussize = ceil(rectsize * 3/4) */
2420 LONG plussize = (rectsize + 1) * 3 / 4;
2422 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2423 HPEN old_pen = SelectObject(hdc, new_pen);
2425 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2426 centerx + rectsize + 2, centery + rectsize + 2);
2428 SelectObject(hdc, old_pen);
2429 DeleteObject(new_pen);
2431 /* draw +/- signs with current text color */
2432 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2433 old_pen = SelectObject(hdc, new_pen);
2435 if (height < 18 || width < 18)
2437 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2438 LineTo(hdc, centerx + plussize, centery);
2440 if (!(item->state & TVIS_EXPANDED))
2442 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2443 LineTo(hdc, centerx, centery + plussize);
2448 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2449 centerx + plussize, centery + 2);
2451 if (!(item->state & TVIS_EXPANDED))
2453 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2454 centerx + 2, centery + plussize);
2455 SetPixel(hdc, centerx - 1, centery, clrBk);
2456 SetPixel(hdc, centerx + 1, centery, clrBk);
2460 SelectObject(hdc, old_pen);
2461 DeleteObject(new_pen);
2465 SelectObject(hdc, hbrOld);
2470 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2474 COLORREF oldTextColor, oldTextBkColor;
2476 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2477 NMTVCUSTOMDRAW nmcdhdr;
2479 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2481 /* - If item is drop target or it is selected and window is in focus -
2482 * use blue background (COLOR_HIGHLIGHT).
2483 * - If item is selected, window is not in focus, but it has style
2484 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2485 * - Otherwise - use background color
2487 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2488 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2489 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2491 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2493 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2494 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2498 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2499 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2504 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2505 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2506 nmcdhdr.clrText = comctl32_color.clrHighlight;
2508 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2511 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2513 /* The custom draw handler can query the text rectangle,
2515 /* should already be known, set to 0 when changed */
2516 if (!wineItem->textWidth)
2517 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2521 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2523 cditem = TREEVIEW_SendCustomDrawItemNotify
2524 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2525 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2527 if (cditem & CDRF_SKIPDEFAULT)
2529 SelectObject(hdc, hOldFont);
2534 if (cditem & CDRF_NEWFONT)
2535 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2537 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2539 /* Set colors. Custom draw handler can change these so we do this after it. */
2540 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2541 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2543 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2546 * Display the images associated with this item
2551 /* State images are displayed to the left of the Normal image
2552 * image number is in state; zero should be `display no image'.
2554 imageIndex = STATEIMAGEINDEX(wineItem->state);
2556 if (infoPtr->himlState && imageIndex)
2558 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2559 wineItem->stateOffset,
2560 centery - infoPtr->stateImageHeight / 2,
2564 /* Now, draw the normal image; can be either selected,
2565 * non-selected or expanded image.
2568 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2570 /* The item is currently selected */
2571 imageIndex = wineItem->iSelectedImage;
2573 else if ((wineItem->state & TVIS_EXPANDED) && (wineItem->iExpandedImage >= 0))
2575 /* The item is currently not selected but expanded */
2576 imageIndex = wineItem->iExpandedImage;
2580 /* The item is not selected and not expanded */
2581 imageIndex = wineItem->iImage;
2584 if (infoPtr->himlNormal)
2586 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2588 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2589 wineItem->imageOffset,
2590 centery - infoPtr->normalImageHeight / 2,
2591 ILD_NORMAL | ovlIdx);
2597 * Display the text associated with this item
2600 /* Don't paint item's text if it's being edited */
2601 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2603 if (wineItem->pszText)
2607 rcText.top = wineItem->rect.top;
2608 rcText.bottom = wineItem->rect.bottom;
2609 rcText.left = wineItem->textOffset;
2610 rcText.right = rcText.left + wineItem->textWidth + 4;
2612 TRACE("drawing text %s at (%s)\n",
2613 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2616 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2617 ETO_CLIPPED | ETO_OPAQUE,
2620 lstrlenW(wineItem->pszText),
2623 /* Draw the box around the selected item */
2624 if ((wineItem == infoPtr->selectedItem) && inFocus)
2626 DrawFocusRect(hdc,&rcText);
2632 /* Draw insertion mark if necessary */
2634 if (infoPtr->insertMarkItem)
2635 TRACE("item:%d,mark:%p\n",
2636 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2637 infoPtr->insertMarkItem);
2639 if (wineItem == infoPtr->insertMarkItem)
2641 HPEN hNewPen, hOldPen;
2645 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2646 hOldPen = SelectObject(hdc, hNewPen);
2648 if (infoPtr->insertBeforeorAfter)
2649 offset = wineItem->rect.bottom - 1;
2651 offset = wineItem->rect.top + 1;
2653 left = wineItem->textOffset - 2;
2654 right = wineItem->textOffset + wineItem->textWidth + 2;
2656 MoveToEx(hdc, left, offset - 3, NULL);
2657 LineTo(hdc, left, offset + 4);
2659 MoveToEx(hdc, left, offset, NULL);
2660 LineTo(hdc, right + 1, offset);
2662 MoveToEx(hdc, right, offset + 3, NULL);
2663 LineTo(hdc, right, offset - 4);
2665 SelectObject(hdc, hOldPen);
2666 DeleteObject(hNewPen);
2669 if (cditem & CDRF_NOTIFYPOSTPAINT)
2671 cditem = TREEVIEW_SendCustomDrawItemNotify
2672 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2673 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2676 /* Restore the hdc state */
2677 SetTextColor(hdc, oldTextColor);
2678 SetBkColor(hdc, oldTextBkColor);
2679 SelectObject(hdc, hOldFont);
2682 /* Computes treeHeight and treeWidth and updates the scroll bars.
2685 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2687 TREEVIEW_ITEM *wineItem;
2688 HWND hwnd = infoPtr->hwnd;
2692 LONG scrollX = infoPtr->scrollX;
2694 infoPtr->treeWidth = 0;
2695 infoPtr->treeHeight = 0;
2697 /* We iterate through all visible items in order to get the tree height
2699 wineItem = infoPtr->root->firstChild;
2701 while (wineItem != NULL)
2703 if (ISVISIBLE(wineItem))
2705 /* actually we draw text at textOffset + 2 */
2706 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2707 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2709 /* This is scroll-adjusted, but we fix this below. */
2710 infoPtr->treeHeight = wineItem->rect.bottom;
2713 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2716 /* Fix the scroll adjusted treeHeight and treeWidth. */
2717 if (infoPtr->root->firstChild)
2718 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2720 infoPtr->treeWidth += infoPtr->scrollX;
2722 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2724 /* Adding one scroll bar may take up enough space that it forces us
2725 * to add the other as well. */
2726 if (infoPtr->treeHeight > infoPtr->clientHeight)
2730 if (infoPtr->treeWidth
2731 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2734 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2737 if (!vert && horz && infoPtr->treeHeight
2738 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2741 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2743 si.cbSize = sizeof(SCROLLINFO);
2744 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2749 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2750 if ( si.nPage && NULL != infoPtr->firstVisible)
2752 si.nPos = infoPtr->firstVisible->visibleOrder;
2753 si.nMax = infoPtr->maxVisibleOrder - 1;
2755 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2757 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2758 ShowScrollBar(hwnd, SB_VERT, TRUE);
2759 infoPtr->uInternalStatus |= TV_VSCROLL;
2763 if (infoPtr->uInternalStatus & TV_VSCROLL)
2764 ShowScrollBar(hwnd, SB_VERT, FALSE);
2765 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2770 if (infoPtr->uInternalStatus & TV_VSCROLL)
2771 ShowScrollBar(hwnd, SB_VERT, FALSE);
2772 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2777 si.nPage = infoPtr->clientWidth;
2778 si.nPos = infoPtr->scrollX;
2779 si.nMax = infoPtr->treeWidth - 1;
2781 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2783 si.nPos = si.nMax - max( si.nPage-1, 0 );
2787 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2788 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2789 infoPtr->uInternalStatus |= TV_HSCROLL;
2791 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2792 TREEVIEW_HScroll(infoPtr,
2793 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2797 if (infoPtr->uInternalStatus & TV_HSCROLL)
2798 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2799 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2802 if (infoPtr->scrollX != 0)
2804 TREEVIEW_HScroll(infoPtr,
2805 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2810 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2814 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2817 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2819 hBrush = CreateSolidBrush(clrBk);
2820 FillRect(hdc, rc, hBrush);
2821 DeleteObject(hBrush);
2824 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2826 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2830 TRACE("%p\n", infoPtr);
2832 GetClientRect(infoPtr->hwnd, &rect);
2833 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2839 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2841 HWND hwnd = infoPtr->hwnd;
2843 TREEVIEW_ITEM *wineItem;
2845 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2847 TRACE("empty window\n");
2851 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2854 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2856 ReleaseDC(hwnd, hdc);
2860 for (wineItem = infoPtr->root->firstChild;
2862 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2864 if (ISVISIBLE(wineItem))
2866 /* Avoid unneeded calculations */
2867 if (wineItem->rect.top > rect.bottom)
2869 if (wineItem->rect.bottom < rect.top)
2872 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2876 TREEVIEW_UpdateScrollBars(infoPtr);
2878 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2880 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2884 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2886 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2890 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2893 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2895 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2899 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2905 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2910 GetClientRect(infoPtr->hwnd, &rc);
2914 hdc = BeginPaint(infoPtr->hwnd, &ps);
2917 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2920 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2921 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2924 EndPaint(infoPtr->hwnd, &ps);
2930 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2932 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2934 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2937 if (options & PRF_ERASEBKGND)
2938 TREEVIEW_EraseBackground(infoPtr, hdc);
2940 if (options & PRF_CLIENT)
2943 GetClientRect(infoPtr->hwnd, &rc);
2944 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2950 /* Sorting **************************************************************/
2952 /***************************************************************************
2953 * Forward the DPA local callback to the treeview owner callback
2956 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2957 const TVSORTCB *pCallBackSort)
2959 /* Forward the call to the client-defined callback */
2960 return pCallBackSort->lpfnCompare(first->lParam,
2962 pCallBackSort->lParam);
2965 /***************************************************************************
2966 * Treeview native sort routine: sort on item text.
2969 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2970 const TREEVIEW_INFO *infoPtr)
2972 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2973 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2975 if(first->pszText && second->pszText)
2976 return lstrcmpiW(first->pszText, second->pszText);
2977 else if(first->pszText)
2979 else if(second->pszText)
2985 /* Returns the number of physical children belonging to item. */
2987 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2992 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
2998 /* Returns a DPA containing a pointer to each physical child of item in
2999 * sibling order. If item has no children, an empty DPA is returned. */
3001 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3003 HTREEITEM child = item->firstChild;
3005 HDPA list = DPA_Create(8);
3006 if (list == 0) return NULL;
3008 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3010 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3020 /***************************************************************************
3021 * Setup the treeview structure with regards of the sort method
3022 * and sort the children of the TV item specified in lParam
3023 * fRecurse: currently unused. Should be zero.
3024 * parent: if pSort!=NULL, should equal pSort->hParent.
3025 * otherwise, item which child items are to be sorted.
3026 * pSort: sort method info. if NULL, sort on item text.
3027 * if non-NULL, sort on item's lParam content, and let the
3028 * application decide what that means. See also TVM_SORTCHILDRENCB.
3032 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3036 PFNDPACOMPARE pfnCompare;
3039 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3040 if (parent == TVI_ROOT || parent == NULL)
3041 parent = infoPtr->root;
3043 /* Check for a valid handle to the parent item */
3044 if (!TREEVIEW_ValidItem(infoPtr, parent))
3046 ERR("invalid item hParent=%p\n", parent);
3052 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3053 lpCompare = (LPARAM)pSort;
3057 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3058 lpCompare = (LPARAM)infoPtr;
3061 cChildren = TREEVIEW_CountChildren(parent);
3063 /* Make sure there is something to sort */
3066 /* TREEVIEW_ITEM rechaining */
3069 HTREEITEM nextItem = 0;
3070 HTREEITEM prevItem = 0;
3072 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3074 if (sortList == NULL)
3077 /* let DPA sort the list */
3078 DPA_Sort(sortList, pfnCompare, lpCompare);
3080 /* The order of DPA entries has been changed, so fixup the
3081 * nextSibling and prevSibling pointers. */
3083 item = DPA_GetPtr(sortList, count++);
3084 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3086 /* link the two current item together */
3087 item->nextSibling = nextItem;
3088 nextItem->prevSibling = item;
3090 if (prevItem == NULL)
3092 /* this is the first item, update the parent */
3093 parent->firstChild = item;
3094 item->prevSibling = NULL;
3098 /* fix the back chaining */
3099 item->prevSibling = prevItem;
3102 /* get ready for the next one */
3107 /* the last item is pointed to by item and never has a sibling */
3108 item->nextSibling = NULL;
3109 parent->lastChild = item;
3111 DPA_Destroy(sortList);
3113 TREEVIEW_VerifyTree(infoPtr);
3115 if (parent->state & TVIS_EXPANDED)
3117 int visOrder = infoPtr->firstVisible->visibleOrder;
3119 if (parent == infoPtr->root)
3120 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3122 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3124 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3126 TREEVIEW_ITEM *item;
3128 for (item = infoPtr->root->firstChild; item != NULL;
3129 item = TREEVIEW_GetNextListItem(infoPtr, item))
3131 if (item->visibleOrder == visOrder)
3135 if (!item) item = parent->firstChild;
3136 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3139 TREEVIEW_Invalidate(infoPtr, NULL);
3148 /***************************************************************************
3149 * Setup the treeview structure with regards of the sort method
3150 * and sort the children of the TV item specified in lParam
3153 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3155 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3159 /***************************************************************************
3160 * Sort the children of the TV item specified in lParam.
3163 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3165 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3169 /* Expansion/Collapse ***************************************************/
3172 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3175 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3176 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3177 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3182 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3185 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3186 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3187 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3192 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3193 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3195 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3196 BOOL bRemoveChildren, BOOL bUser)
3198 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3199 BOOL bSetSelection, bSetFirstVisible;
3201 LONG scrollDist = 0;
3202 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3204 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3206 if (!(wineItem->state & TVIS_EXPANDED))
3209 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3210 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3212 if (wineItem->firstChild == NULL)
3215 wineItem->state &= ~TVIS_EXPANDED;
3217 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3218 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3220 bSetSelection = (infoPtr->selectedItem != NULL
3221 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3223 bSetFirstVisible = (infoPtr->firstVisible != NULL
3224 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3229 if (tmpItem->nextSibling)
3231 nextItem = tmpItem->nextSibling;
3234 tmpItem = tmpItem->parent;
3238 scrollDist = nextItem->rect.top;
3240 if (bRemoveChildren)
3242 INT old_cChildren = wineItem->cChildren;
3243 TRACE("TVE_COLLAPSERESET\n");
3244 wineItem->state &= ~TVIS_EXPANDEDONCE;
3245 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3246 wineItem->cChildren = old_cChildren;
3249 if (wineItem->firstChild)
3251 TREEVIEW_ITEM *item, *sibling;
3253 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3255 for (item = wineItem->firstChild; item != sibling;
3256 item = TREEVIEW_GetNextListItem(infoPtr, item))
3258 item->visibleOrder = -1;
3262 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3265 scrollDist = -(scrollDist - nextItem->rect.top);
3269 /* Don't call DoSelectItem, it sends notifications. */
3270 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3271 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3272 wineItem->state |= TVIS_SELECTED;
3273 infoPtr->selectedItem = wineItem;
3276 TREEVIEW_UpdateScrollBars(infoPtr);
3278 scrollRect.left = 0;
3279 scrollRect.right = infoPtr->clientWidth;
3280 scrollRect.bottom = infoPtr->clientHeight;
3284 scrollRect.top = nextItem->rect.top;
3286 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3287 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3288 TREEVIEW_Invalidate(infoPtr, wineItem);
3290 scrollRect.top = wineItem->rect.top;
3291 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3294 TREEVIEW_SetFirstVisible(infoPtr,
3295 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3302 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3303 BOOL bExpandPartial, BOOL bUser)
3306 LONG orgNextTop = 0;
3308 TREEVIEW_ITEM *nextItem, *tmpItem;
3309 BOOL sendsNotifications;
3311 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, wineItem, bExpandPartial, bUser);
3313 if (wineItem->state & TVIS_EXPANDED)
3316 tmpItem = wineItem; nextItem = NULL;
3319 if (tmpItem->nextSibling)
3321 nextItem = tmpItem->nextSibling;
3324 tmpItem = tmpItem->parent;
3328 orgNextTop = nextItem->rect.top;
3330 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3332 sendsNotifications = bUser || ((wineItem->cChildren != 0) &&
3333 !(wineItem->state & TVIS_EXPANDEDONCE));
3334 if (sendsNotifications)
3336 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3338 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3342 if (!wineItem->firstChild)
3345 wineItem->state |= TVIS_EXPANDED;
3348 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3350 if (ISVISIBLE(wineItem))
3352 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3353 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3354 TREEVIEW_UpdateScrollBars(infoPtr);
3356 scrollRect.left = 0;
3357 scrollRect.bottom = infoPtr->treeHeight;
3358 scrollRect.right = infoPtr->clientWidth;
3361 scrollDist = nextItem->rect.top - orgNextTop;
3362 scrollRect.top = orgNextTop;
3364 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3365 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3366 TREEVIEW_Invalidate (infoPtr, wineItem);
3368 scrollRect.top = wineItem->rect.top;
3369 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3372 /* Scroll up so that as many children as possible are visible.
3373 * This fails when expanding causes an HScroll bar to appear, but we
3374 * don't know that yet, so the last item is obscured. */
3375 if (wineItem->firstChild != NULL)
3377 int nChildren = wineItem->lastChild->visibleOrder
3378 - wineItem->firstChild->visibleOrder + 1;
3380 int visible_pos = wineItem->visibleOrder
3381 - infoPtr->firstVisible->visibleOrder;
3383 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3385 if (visible_pos > 0 && nChildren > rows_below)
3387 int scroll = nChildren - rows_below;
3389 if (scroll > visible_pos)
3390 scroll = visible_pos;
3394 TREEVIEW_ITEM *newFirstVisible
3395 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3399 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3405 if (sendsNotifications) {
3406 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3407 wineItem->state |= TVIS_EXPANDEDONCE;
3414 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3418 if (wineItem->state & TVIS_EXPANDED)
3419 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3421 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3425 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3427 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3429 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3431 if (TREEVIEW_HasChildren(infoPtr, item))
3432 TREEVIEW_ExpandAll(infoPtr, item);
3436 /* Note:If the specified item is the child of a collapsed parent item,
3437 the parent's list of child items is (recursively) expanded to reveal the
3438 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3439 know if it also applies here.
3443 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3445 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3448 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3449 TREEVIEW_ItemName(wineItem), TREEVIEW_GetItemIndex(infoPtr, wineItem),
3450 flag, wineItem->state);
3452 switch (flag & TVE_TOGGLE)
3455 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3459 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3463 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3470 /* Hit-Testing **********************************************************/
3472 static TREEVIEW_ITEM *
3473 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3475 TREEVIEW_ITEM *wineItem;
3478 if (!infoPtr->firstVisible)
3481 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3483 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3484 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3486 if (row >= wineItem->visibleOrder
3487 && row < wineItem->visibleOrder + wineItem->iIntegral)
3495 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3497 TREEVIEW_ITEM *wineItem;
3503 GetClientRect(infoPtr->hwnd, &rect);
3510 status |= TVHT_TOLEFT;
3512 else if (x > rect.right)
3514 status |= TVHT_TORIGHT;
3519 status |= TVHT_ABOVE;
3521 else if (y > rect.bottom)
3523 status |= TVHT_BELOW;
3528 lpht->flags = status;
3532 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3535 lpht->flags = TVHT_NOWHERE;
3539 if (x >= wineItem->textOffset + wineItem->textWidth)
3541 lpht->flags = TVHT_ONITEMRIGHT;
3543 else if (x >= wineItem->textOffset)
3545 lpht->flags = TVHT_ONITEMLABEL;
3547 else if (x >= wineItem->imageOffset)
3549 lpht->flags = TVHT_ONITEMICON;
3551 else if (x >= wineItem->stateOffset)
3553 lpht->flags = TVHT_ONITEMSTATEICON;
3555 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3557 lpht->flags = TVHT_ONITEMBUTTON;
3561 lpht->flags = TVHT_ONITEMINDENT;
3564 lpht->hItem = wineItem;
3565 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3567 return (LRESULT)wineItem;
3570 /* Item Label Editing ***************************************************/
3573 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3575 return (LRESULT)infoPtr->hwndEdit;
3578 static LRESULT CALLBACK
3579 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3581 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3582 BOOL bCancel = FALSE;
3588 TRACE("WM_PAINT start\n");
3589 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3591 TRACE("WM_PAINT done\n");
3595 if (infoPtr->bIgnoreEditKillFocus)
3601 WNDPROC editProc = infoPtr->wpEditOrig;
3602 infoPtr->wpEditOrig = 0;
3603 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3604 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3608 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3611 if (wParam == VK_ESCAPE)
3616 else if (wParam == VK_RETURN)
3623 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3626 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3627 /* eg. Using a messagebox */
3629 infoPtr->bIgnoreEditKillFocus = TRUE;
3630 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3631 infoPtr->bIgnoreEditKillFocus = FALSE;
3637 /* should handle edit control messages here */
3640 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3642 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3644 switch (HIWORD(wParam))
3649 * Adjust the edit window size
3652 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3653 HDC hdc = GetDC(infoPtr->hwndEdit);
3655 HFONT hFont, hOldFont = 0;
3657 TRACE("edit=%p\n", infoPtr->hwndEdit);
3659 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3661 infoPtr->bLabelChanged = TRUE;
3663 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3665 /* Select font to get the right dimension of the string */
3666 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3670 hOldFont = SelectObject(hdc, hFont);
3673 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3675 TEXTMETRICW textMetric;
3677 /* Add Extra spacing for the next character */
3678 GetTextMetricsW(hdc, &textMetric);
3679 sz.cx += (textMetric.tmMaxCharWidth * 2);
3681 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3683 infoPtr->clientWidth - editItem->textOffset + 2);
3685 SetWindowPos(infoPtr->hwndEdit,
3690 editItem->rect.bottom - editItem->rect.top + 3,
3691 SWP_NOMOVE | SWP_DRAWFRAME);
3696 SelectObject(hdc, hOldFont);
3699 ReleaseDC(infoPtr->hwnd, hdc);
3703 /* apparently we should respect passed handle value */
3704 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3706 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3710 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3717 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3719 HWND hwnd = infoPtr->hwnd;
3722 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3725 TEXTMETRICW textMetric;
3727 TRACE("%p %p\n", hwnd, hItem);
3728 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3731 if (infoPtr->hwndEdit)
3732 return infoPtr->hwndEdit;
3734 infoPtr->bLabelChanged = FALSE;
3736 /* make edit item visible */
3737 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3739 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3742 /* Select the font to get appropriate metric dimensions */
3743 if (infoPtr->hFont != 0)
3745 hOldFont = SelectObject(hdc, infoPtr->hFont);
3748 /* Get string length in pixels */
3750 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3753 GetTextExtentPoint32A(hdc, "", 0, &sz);
3755 /* Add Extra spacing for the next character */
3756 GetTextMetricsW(hdc, &textMetric);
3757 sz.cx += (textMetric.tmMaxCharWidth * 2);
3759 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3760 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3762 if (infoPtr->hFont != 0)
3764 SelectObject(hdc, hOldFont);
3767 ReleaseDC(hwnd, hdc);
3769 infoPtr->editItem = hItem;
3771 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3774 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3775 WS_CLIPSIBLINGS | ES_WANTRETURN |
3776 ES_LEFT, hItem->textOffset - 2,
3777 hItem->rect.top - 1, sz.cx + 3,
3778 hItem->rect.bottom -
3779 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3780 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3782 infoPtr->hwndEdit = hwndEdit;
3784 /* Get a 2D border. */
3785 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3786 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3787 SetWindowLongW(hwndEdit, GWL_STYLE,
3788 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3790 SendMessageW(hwndEdit, WM_SETFONT,
3791 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3793 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3795 TREEVIEW_Edit_SubclassProc);
3797 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3799 DestroyWindow(hwndEdit);
3800 infoPtr->hwndEdit = 0;
3801 infoPtr->editItem = NULL;
3806 SetWindowTextW(hwndEdit, hItem->pszText);
3809 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3810 ShowWindow(hwndEdit, SW_SHOW);
3817 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3819 HWND hwnd = infoPtr->hwnd;
3820 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3823 WCHAR tmpText[1024] = { '\0' };
3824 WCHAR *newText = tmpText;
3827 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3829 tvdi.hdr.hwndFrom = hwnd;
3830 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3831 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3833 tvdi.item.hItem = editedItem;
3834 tvdi.item.state = editedItem->state;
3835 tvdi.item.lParam = editedItem->lParam;
3839 if (!infoPtr->bNtfUnicode)
3840 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3842 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3844 if (iLength >= 1023)
3846 ERR("Insufficient space to retrieve new item label\n");
3849 tvdi.item.mask = TVIF_TEXT;
3850 tvdi.item.pszText = tmpText;
3851 tvdi.item.cchTextMax = iLength + 1;
3855 tvdi.item.pszText = NULL;
3856 tvdi.item.cchTextMax = 0;
3859 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3861 if (!bCancel && bCommit) /* Apply the changes */
3863 if (!infoPtr->bNtfUnicode)
3865 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3866 newText = Alloc(len * sizeof(WCHAR));
3867 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3871 if (strcmpW(newText, editedItem->pszText) != 0)
3873 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3876 ERR("OutOfMemory, cannot allocate space for label\n");
3877 if(newText != tmpText) Free(newText);
3878 DestroyWindow(infoPtr->hwndEdit);
3879 infoPtr->hwndEdit = 0;
3880 infoPtr->editItem = NULL;
3885 editedItem->pszText = ptr;
3886 editedItem->cchTextMax = iLength + 1;
3887 strcpyW(editedItem->pszText, newText);
3888 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3891 if(newText != tmpText) Free(newText);
3894 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3895 DestroyWindow(infoPtr->hwndEdit);
3896 infoPtr->hwndEdit = 0;
3897 infoPtr->editItem = NULL;
3902 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3904 if (wParam != TV_EDIT_TIMER)
3906 ERR("got unknown timer\n");
3910 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3911 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3913 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3919 /* Mouse Tracking/Drag **************************************************/
3921 /***************************************************************************
3922 * This is quite unusual piece of code, but that's how it's implemented in
3926 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3928 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3929 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3933 r.top = pt.y - cyDrag;
3934 r.left = pt.x - cxDrag;
3935 r.bottom = pt.y + cyDrag;
3936 r.right = pt.x + cxDrag;
3938 SetCapture(infoPtr->hwnd);
3942 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
3944 if (msg.message == WM_MOUSEMOVE)
3946 pt.x = (short)LOWORD(msg.lParam);
3947 pt.y = (short)HIWORD(msg.lParam);
3948 if (PtInRect(&r, pt))
3956 else if (msg.message >= WM_LBUTTONDOWN &&
3957 msg.message <= WM_RBUTTONDBLCLK)
3959 if (msg.message == WM_RBUTTONUP)
3960 TREEVIEW_RButtonUp(infoPtr, &pt);
3964 DispatchMessageW(&msg);
3967 if (GetCapture() != infoPtr->hwnd)
3977 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3979 TREEVIEW_ITEM *wineItem;
3983 SetFocus(infoPtr->hwnd);
3985 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
3987 /* If there is pending 'edit label' event - kill it now */
3988 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3991 hit.pt.x = (short)LOWORD(lParam);
3992 hit.pt.y = (short)HIWORD(lParam);
3994 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
3997 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
3999 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4003 case TVHT_ONITEMRIGHT:
4004 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4007 case TVHT_ONITEMINDENT:
4008 if (!(infoPtr->dwStyle & TVS_HASLINES))
4014 int level = hit.pt.x / infoPtr->uIndent;
4015 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4017 while (wineItem->iLevel > level)
4019 wineItem = wineItem->parent;
4025 case TVHT_ONITEMLABEL:
4026 case TVHT_ONITEMICON:
4027 case TVHT_ONITEMBUTTON:
4028 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4031 case TVHT_ONITEMSTATEICON:
4032 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4033 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4035 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4044 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4046 HWND hwnd = infoPtr->hwnd;
4048 BOOL bTrack, bDoLabelEdit;
4050 /* If Edit control is active - kill it and return.
4051 * The best way to do it is to set focus to itself.
4052 * Edit control subclassed procedure will automatically call
4055 if (infoPtr->hwndEdit)
4061 ht.pt.x = (short)LOWORD(lParam);
4062 ht.pt.y = (short)HIWORD(lParam);
4064 TREEVIEW_HitTest(infoPtr, &ht);
4065 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4067 /* update focusedItem and redraw both items */
4068 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4070 infoPtr->focusedItem = ht.hItem;
4071 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4072 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4075 bTrack = (ht.flags & TVHT_ONITEM)
4076 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4079 * If the style allows editing and the node is already selected
4080 * and the click occurred on the item label...
4082 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4083 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4085 /* Send NM_CLICK right away */
4087 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4090 if (ht.flags & TVHT_ONITEMBUTTON)
4092 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4096 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4097 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4099 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4100 infoPtr->dropItem = ht.hItem;
4102 /* clean up focusedItem as we dragged and won't select this item */
4103 if(infoPtr->focusedItem)
4105 /* refresh the item that was focused */
4106 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4107 infoPtr->focusedItem = NULL;
4109 /* refresh the selected item to return the filled background */
4110 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4117 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4122 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4123 KillTimer(hwnd, TV_EDIT_TIMER);
4125 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4126 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4128 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4131 * if we are TVS_SINGLEEXPAND then we want this single click to
4132 * do a bunch of things.
4134 if((infoPtr->dwStyle & TVS_SINGLEEXPAND) &&
4135 (infoPtr->hwndEdit == 0))
4137 TREEVIEW_ITEM *SelItem;
4140 * Send the notification
4142 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, ht.hItem, 0);
4145 * Close the previous selection all the way to the root
4146 * as long as the new selection is not a child
4148 if((infoPtr->selectedItem)
4149 && (infoPtr->selectedItem != ht.hItem))
4151 BOOL closeit = TRUE;
4154 /* determine if the hitItem is a child of the currently selected item */
4155 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4157 closeit = (SelItem != infoPtr->selectedItem);
4158 SelItem = SelItem->parent;
4163 if(TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
4164 SelItem = infoPtr->selectedItem;
4166 while(SelItem && (SelItem != ht.hItem) && TREEVIEW_ValidItem(infoPtr, SelItem) && (SelItem != infoPtr->root))
4168 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
4169 SelItem = SelItem->parent;
4175 * Expand the current item
4177 TREEVIEW_Expand(infoPtr, ht.hItem, TVE_TOGGLE, FALSE);
4180 /* Select the current item */
4181 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4183 else if (ht.flags & TVHT_ONITEMSTATEICON)
4185 /* TVS_CHECKBOXES requires us to toggle the current state */
4186 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4187 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4197 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4201 if (infoPtr->hwndEdit)
4203 SetFocus(infoPtr->hwnd);
4207 ht.pt.x = (short)LOWORD(lParam);
4208 ht.pt.y = (short)HIWORD(lParam);
4210 TREEVIEW_HitTest(infoPtr, &ht);
4212 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4216 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4217 infoPtr->dropItem = ht.hItem;
4222 SetFocus(infoPtr->hwnd);
4223 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4230 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4236 TREEVIEW_HitTest(infoPtr, &ht);
4240 /* Change to screen coordinate for WM_CONTEXTMENU */
4241 ClientToScreen(infoPtr->hwnd, &ht.pt);
4243 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4244 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4245 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4252 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4254 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4258 HBITMAP hbmp, hOldbmp;
4265 if (!(infoPtr->himlNormal))
4268 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4271 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4273 hwtop = GetDesktopWindow();
4274 htopdc = GetDC(hwtop);
4275 hdc = CreateCompatibleDC(htopdc);
4277 hOldFont = SelectObject(hdc, infoPtr->hFont);
4279 if (dragItem->pszText)
4280 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4283 GetTextExtentPoint32A(hdc, "", 0, &size);
4285 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4286 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4287 hOldbmp = SelectObject(hdc, hbmp);
4289 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4294 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4295 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4299 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4300 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4303 /* draw item text */
4305 SetRect(&rc, cx, 0, size.cx, size.cy);
4307 if (dragItem->pszText)
4308 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4311 SelectObject(hdc, hOldFont);
4312 SelectObject(hdc, hOldbmp);
4314 ImageList_Add(infoPtr->dragList, hbmp, 0);
4318 ReleaseDC(hwtop, htopdc);
4320 return (LRESULT)infoPtr->dragList;
4323 /* Selection ************************************************************/
4326 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4329 TREEVIEW_ITEM *prevSelect;
4331 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4333 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4334 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4335 newSelect ? newSelect->state : 0);
4337 /* reset and redraw focusedItem if focusedItem was set so we don't */
4338 /* have to worry about the previously focused item when we set a new one */
4339 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4340 infoPtr->focusedItem = NULL;
4345 prevSelect = infoPtr->selectedItem;
4347 if (prevSelect == newSelect) {
4348 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4352 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4355 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4361 prevSelect->state &= ~TVIS_SELECTED;
4363 newSelect->state |= TVIS_SELECTED;
4365 infoPtr->selectedItem = newSelect;
4367 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4369 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4370 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4372 TREEVIEW_SendTreeviewNotify(infoPtr,
4375 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4380 case TVGN_DROPHILITE:
4381 prevSelect = infoPtr->dropItem;
4384 prevSelect->state &= ~TVIS_DROPHILITED;
4386 infoPtr->dropItem = newSelect;
4389 newSelect->state |= TVIS_DROPHILITED;
4391 TREEVIEW_Invalidate(infoPtr, prevSelect);
4392 TREEVIEW_Invalidate(infoPtr, newSelect);
4395 case TVGN_FIRSTVISIBLE:
4396 if (newSelect != NULL)
4398 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4399 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4400 TREEVIEW_Invalidate(infoPtr, NULL);
4405 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4409 /* FIXME: handle NM_KILLFOCUS etc */
4411 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4413 if (item != NULL && !TREEVIEW_ValidItem(infoPtr, item))
4416 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4418 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4424 /*************************************************************************
4425 * TREEVIEW_ProcessLetterKeys
4427 * Processes keyboard messages generated by pressing the letter keys
4429 * What this does is perform a case insensitive search from the
4430 * current position with the following quirks:
4431 * - If two chars or more are pressed in quick succession we search
4432 * for the corresponding string (e.g. 'abc').
4433 * - If there is a delay we wipe away the current search string and
4434 * restart with just that char.
4435 * - If the user keeps pressing the same character, whether slowly or
4436 * fast, so that the search string is entirely composed of this
4437 * character ('aaaaa' for instance), then we search for first item
4438 * that starting with that character.
4439 * - If the user types the above character in quick succession, then
4440 * we must also search for the corresponding string ('aaaaa'), and
4441 * go to that string if there is a match.
4449 * - The current implementation has a list of characters it will
4450 * accept and it ignores everything else. In particular it will
4451 * ignore accentuated characters which seems to match what
4452 * Windows does. But I'm not sure it makes sense to follow
4454 * - We don't sound a beep when the search fails.
4455 * - The search should start from the focused item, not from the selected
4456 * item. One reason for this is to allow for multiple selections in trees.
4457 * But currently infoPtr->focusedItem does not seem very usable.
4461 * TREEVIEW_ProcessLetterKeys
4463 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4466 HTREEITEM endidx,idx;
4468 WCHAR buffer[MAX_PATH];
4469 DWORD timestamp,elapsed;
4471 /* simple parameter checking */
4472 if (!charCode || !keyData) return 0;
4474 /* only allow the valid WM_CHARs through */
4475 if (!isalnum(charCode) &&
4476 charCode != '.' && charCode != '`' && charCode != '!' &&
4477 charCode != '@' && charCode != '#' && charCode != '$' &&
4478 charCode != '%' && charCode != '^' && charCode != '&' &&
4479 charCode != '*' && charCode != '(' && charCode != ')' &&
4480 charCode != '-' && charCode != '_' && charCode != '+' &&
4481 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4482 charCode != '}' && charCode != '[' && charCode != '{' &&
4483 charCode != '/' && charCode != '?' && charCode != '>' &&
4484 charCode != '<' && charCode != ',' && charCode != '~')
4487 /* compute how much time elapsed since last keypress */
4488 timestamp = GetTickCount();
4489 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4490 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4492 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4495 /* update the search parameters */
4496 infoPtr->lastKeyPressTimestamp=timestamp;
4497 if (elapsed < KEY_DELAY) {
4498 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4499 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4501 if (infoPtr->charCode != charCode) {
4502 infoPtr->charCode=charCode=0;
4505 infoPtr->charCode=charCode;
4506 infoPtr->szSearchParam[0]=charCode;
4507 infoPtr->nSearchParamLength=1;
4508 /* Redundant with the 1 char string */
4512 /* and search from the current position */
4514 if (infoPtr->selectedItem != NULL) {
4515 endidx=infoPtr->selectedItem;
4516 /* if looking for single character match,
4517 * then we must always move forward
4519 if (infoPtr->nSearchParamLength == 1)
4520 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4525 idx=infoPtr->root->firstChild;
4528 /* At the end point, sort out wrapping */
4531 /* If endidx is null, stop at the last item (ie top to bottom) */
4535 /* Otherwise, start again at the very beginning */
4536 idx=infoPtr->root->firstChild;
4538 /* But if we are stopping on the first child, end now! */
4539 if (idx == endidx) break;
4543 ZeroMemory(&item, sizeof(item));
4544 item.mask = TVIF_TEXT;
4546 item.pszText = buffer;
4547 item.cchTextMax = sizeof(buffer);
4548 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4550 /* check for a match */
4551 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4554 } else if ( (charCode != 0) && (nItem == NULL) &&
4555 (nItem != infoPtr->selectedItem) &&
4556 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4557 /* This would work but we must keep looking for a longer match */
4560 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4561 } while (idx != endidx);
4563 if (nItem != NULL) {
4564 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4565 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4572 /* Scrolling ************************************************************/
4575 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4578 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4579 HTREEITEM newFirstVisible = NULL;
4580 int visible_pos = -1;
4582 if (!TREEVIEW_ValidItem(infoPtr, item))
4585 if (!ISVISIBLE(item))
4587 /* Expand parents as necessary. */
4590 /* see if we are trying to ensure that root is visible */
4591 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4592 parent = item->parent;
4594 parent = item; /* this item is the topmost item */
4596 while (parent != infoPtr->root)
4598 if (!(parent->state & TVIS_EXPANDED))
4599 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4601 parent = parent->parent;
4605 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4607 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4608 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4610 if (hasFirstVisible)
4611 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4613 if (visible_pos < 0)
4615 /* item is before the start of the list: put it at the top. */
4616 newFirstVisible = item;
4618 else if (visible_pos >= viscount
4619 /* Sometimes, before we are displayed, GVC is 0, causing us to
4620 * spuriously scroll up. */
4621 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4623 /* item is past the end of the list. */
4624 int scroll = visible_pos - viscount;
4626 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4632 /* Scroll window so item's text is visible as much as possible */
4633 /* Calculation of amount of extra space is taken from EditLabel code */
4635 TEXTMETRICW textMetric;
4636 HDC hdc = GetWindowDC(infoPtr->hwnd);
4638 x = item->textWidth;
4640 GetTextMetricsW(hdc, &textMetric);
4641 ReleaseDC(infoPtr->hwnd, hdc);
4643 x += (textMetric.tmMaxCharWidth * 2);
4644 x = max(x, textMetric.tmMaxCharWidth * 3);
4646 if (item->textOffset < 0)
4647 pos = item->textOffset;
4648 else if (item->textOffset + x > infoPtr->clientWidth)
4650 if (x > infoPtr->clientWidth)
4651 pos = item->textOffset;
4653 pos = item->textOffset + x - infoPtr->clientWidth;
4658 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4661 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4663 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4672 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4673 TREEVIEW_ITEM *newFirstVisible,
4674 BOOL bUpdateScrollPos)
4678 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4680 if (newFirstVisible != NULL)
4682 /* Prevent an empty gap from appearing at the bottom... */
4683 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4684 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4688 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4691 /* ... unless we just don't have enough items. */
4692 if (newFirstVisible == NULL)
4693 newFirstVisible = infoPtr->root->firstChild;
4697 if (infoPtr->firstVisible != newFirstVisible)
4699 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4701 infoPtr->firstVisible = newFirstVisible;
4702 TREEVIEW_Invalidate(infoPtr, NULL);
4706 TREEVIEW_ITEM *item;
4707 int scroll = infoPtr->uItemHeight *
4708 (infoPtr->firstVisible->visibleOrder
4709 - newFirstVisible->visibleOrder);
4711 infoPtr->firstVisible = newFirstVisible;
4713 for (item = infoPtr->root->firstChild; item != NULL;
4714 item = TREEVIEW_GetNextListItem(infoPtr, item))
4716 item->rect.top += scroll;
4717 item->rect.bottom += scroll;
4720 if (bUpdateScrollPos)
4721 SetScrollPos(infoPtr->hwnd, SB_VERT,
4722 newFirstVisible->visibleOrder, TRUE);
4724 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4729 /************************************************************************
4730 * VScroll is always in units of visible items. i.e. we always have a
4731 * visible item aligned to the top of the control. (Unless we have no
4735 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4737 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4738 TREEVIEW_ITEM *newFirstVisible = NULL;
4740 int nScrollCode = LOWORD(wParam);
4742 TRACE("wp %lx\n", wParam);
4744 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4747 if (!oldFirstVisible)
4749 assert(infoPtr->root->firstChild == NULL);
4753 switch (nScrollCode)
4756 newFirstVisible = infoPtr->root->firstChild;
4760 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4764 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4768 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4772 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4773 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4777 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4778 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4782 case SB_THUMBPOSITION:
4783 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4784 infoPtr->root->firstChild,
4785 (LONG)(SHORT)HIWORD(wParam));
4792 if (newFirstVisible != NULL)
4794 if (newFirstVisible != oldFirstVisible)
4795 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4796 nScrollCode != SB_THUMBTRACK);
4797 else if (nScrollCode == SB_THUMBPOSITION)
4798 SetScrollPos(infoPtr->hwnd, SB_VERT,
4799 newFirstVisible->visibleOrder, TRUE);
4806 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4809 int scrollX = infoPtr->scrollX;
4810 int nScrollCode = LOWORD(wParam);
4812 TRACE("wp %lx\n", wParam);
4814 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4817 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4818 /* shall never occur */
4825 switch (nScrollCode)
4828 scrollX -= infoPtr->uItemHeight;
4831 scrollX += infoPtr->uItemHeight;
4834 scrollX -= infoPtr->clientWidth;
4837 scrollX += infoPtr->clientWidth;
4841 case SB_THUMBPOSITION:
4842 scrollX = (int)(SHORT)HIWORD(wParam);
4849 if (scrollX > maxWidth)
4851 else if (scrollX < 0)
4855 if (scrollX != infoPtr->scrollX)
4857 TREEVIEW_ITEM *item;
4858 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4860 for (item = infoPtr->root->firstChild; item != NULL;
4861 item = TREEVIEW_GetNextListItem(infoPtr, item))
4863 item->linesOffset += scroll_pixels;
4864 item->stateOffset += scroll_pixels;
4865 item->imageOffset += scroll_pixels;
4866 item->textOffset += scroll_pixels;
4869 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4870 infoPtr->scrollX = scrollX;
4871 UpdateWindow(infoPtr->hwnd);
4874 if (nScrollCode != SB_THUMBTRACK)
4875 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4881 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4884 UINT pulScrollLines = 3;
4886 if (wParam & (MK_SHIFT | MK_CONTROL))
4887 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4889 if (infoPtr->firstVisible == NULL)
4892 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4894 gcWheelDelta = -(short)HIWORD(wParam);
4895 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4897 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4899 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4900 int maxDy = infoPtr->maxVisibleOrder;
4908 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4913 /* Create/Destroy *******************************************************/
4916 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4919 HBITMAP hbm, hbmOld;
4923 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4925 hdcScreen = GetDC(0);
4927 hdc = CreateCompatibleDC(hdcScreen);
4928 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4929 hbmOld = SelectObject(hdc, hbm);
4931 SetRect(&rc, 0, 0, 48, 16);
4932 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4934 SetRect(&rc, 18, 2, 30, 14);
4935 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4936 DFCS_BUTTONCHECK|DFCS_FLAT);
4938 SetRect(&rc, 34, 2, 46, 14);
4939 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4940 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4942 SelectObject(hdc, hbmOld);
4943 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4944 comctl32_color.clrWindow);
4945 TRACE("checkbox index %d\n", nIndex);
4949 ReleaseDC(0, hdcScreen);
4951 infoPtr->stateImageWidth = 16;
4952 infoPtr->stateImageHeight = 16;
4956 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4959 TREEVIEW_INFO *infoPtr;
4962 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4964 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4966 if (infoPtr == NULL)
4968 ERR("could not allocate info memory!\n");
4972 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4974 infoPtr->hwnd = hwnd;
4975 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4977 infoPtr->uNumItems = 0;
4978 infoPtr->cdmode = 0;
4979 infoPtr->uScrollTime = 300; /* milliseconds */
4980 infoPtr->bRedraw = TRUE;
4982 GetClientRect(hwnd, &rcClient);
4984 /* No scroll bars yet. */
4985 infoPtr->clientWidth = rcClient.right;
4986 infoPtr->clientHeight = rcClient.bottom;
4987 infoPtr->uInternalStatus = 0;
4989 infoPtr->treeWidth = 0;
4990 infoPtr->treeHeight = 0;
4992 infoPtr->uIndent = MINIMUM_INDENT;
4993 infoPtr->selectedItem = NULL;
4994 infoPtr->focusedItem = NULL;
4995 infoPtr->hotItem = NULL;
4996 infoPtr->editItem = NULL;
4997 infoPtr->firstVisible = NULL;
4998 infoPtr->maxVisibleOrder = 0;
4999 infoPtr->dropItem = NULL;
5000 infoPtr->insertMarkItem = NULL;
5001 infoPtr->insertBeforeorAfter = 0;
5004 infoPtr->scrollX = 0;
5006 infoPtr->clrBk = CLR_NONE; /* use system color */
5007 infoPtr->clrText = CLR_NONE; /* use system color */
5008 infoPtr->clrLine = CLR_DEFAULT;
5009 infoPtr->clrInsertMark = CLR_DEFAULT;
5013 infoPtr->hwndEdit = NULL;
5014 infoPtr->wpEditOrig = NULL;
5015 infoPtr->bIgnoreEditKillFocus = FALSE;
5016 infoPtr->bLabelChanged = FALSE;
5018 infoPtr->himlNormal = NULL;
5019 infoPtr->himlState = NULL;
5020 infoPtr->normalImageWidth = 0;
5021 infoPtr->normalImageHeight = 0;
5022 infoPtr->stateImageWidth = 0;
5023 infoPtr->stateImageHeight = 0;
5025 infoPtr->items = DPA_Create(16);
5027 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5028 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5029 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5030 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5031 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5033 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5035 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5036 infoPtr->root->state = TVIS_EXPANDED;
5037 infoPtr->root->iLevel = -1;
5038 infoPtr->root->visibleOrder = -1;
5040 infoPtr->hwndNotify = lpcs->hwndParent;
5041 infoPtr->hwndToolTip = 0;
5043 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5045 /* Determine what type of notify should be issued */
5046 /* sets infoPtr->bNtfUnicode */
5047 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5049 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5050 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5051 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5054 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5055 initialize_checkboxes(infoPtr);
5057 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5058 ShowScrollBar(hwnd, SB_VERT, FALSE);
5059 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5061 OpenThemeData (hwnd, themeClass);
5068 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5072 /* free item data */
5073 TREEVIEW_RemoveTree(infoPtr);
5074 /* root isn't freed with other items */
5075 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5076 DPA_Destroy(infoPtr->items);
5078 /* tool tip is automatically destroyed: we are its owner */
5080 /* Restore original wndproc */
5081 if (infoPtr->hwndEdit)
5082 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5083 (DWORD_PTR)infoPtr->wpEditOrig);
5085 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5087 /* Deassociate treeview from the window before doing anything drastic. */
5088 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5091 DeleteObject(infoPtr->hDefaultFont);
5092 DeleteObject(infoPtr->hBoldFont);
5093 DeleteObject(infoPtr->hUnderlineFont);
5099 /* Miscellaneous Messages ***********************************************/
5102 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5110 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5111 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5112 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5113 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5114 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5115 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5116 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5117 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5118 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5122 if (key >= VK_PRIOR && key <= VK_DOWN)
5124 unsigned char code = scroll[key - VK_PRIOR].code;
5126 (((code & (1 << 7)) == (SB_HORZ << 7))
5128 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5134 /************************************************************************
5137 * VK_UP Move selection to the previous non-hidden item.
5138 * VK_DOWN Move selection to the next non-hidden item.
5139 * VK_HOME Move selection to the first item.
5140 * VK_END Move selection to the last item.
5141 * VK_LEFT If expanded then collapse, otherwise move to parent.
5142 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5144 * VK_SUBTRACT Collapse.
5145 * VK_MULTIPLY Expand all.
5146 * VK_PRIOR Move up GetVisibleCount items.
5147 * VK_NEXT Move down GetVisibleCount items.
5148 * VK_BACK Move to parent.
5149 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5152 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5154 /* If it is non-NULL and different, it will be selected and visible. */
5155 TREEVIEW_ITEM *newSelection = NULL;
5157 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5159 TRACE("%lx\n", wParam);
5161 if (prevItem == NULL)
5164 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5165 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5170 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5172 newSelection = infoPtr->root->firstChild;
5176 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5180 newSelection = infoPtr->root->firstChild;
5184 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5188 if (prevItem->state & TVIS_EXPANDED)
5190 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5192 else if (prevItem->parent != infoPtr->root)
5194 newSelection = prevItem->parent;
5199 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5201 if (!(prevItem->state & TVIS_EXPANDED))
5202 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5205 newSelection = prevItem->firstChild;
5212 TREEVIEW_ExpandAll(infoPtr, prevItem);
5216 if (!(prevItem->state & TVIS_EXPANDED))
5217 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5221 if (prevItem->state & TVIS_EXPANDED)
5222 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5227 = TREEVIEW_GetListItem(infoPtr, prevItem,
5228 -TREEVIEW_GetVisibleCount(infoPtr));
5233 = TREEVIEW_GetListItem(infoPtr, prevItem,
5234 TREEVIEW_GetVisibleCount(infoPtr));
5238 newSelection = prevItem->parent;
5239 if (newSelection == infoPtr->root)
5240 newSelection = NULL;
5244 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5245 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5249 if (newSelection && newSelection != prevItem)
5251 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5254 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5262 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5264 /* remove hot effect from item */
5265 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5266 infoPtr->hotItem = NULL;
5272 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5275 TRACKMOUSEEVENT trackinfo;
5276 TREEVIEW_ITEM * item;
5278 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5280 /* fill in the TRACKMOUSEEVENT struct */
5281 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5282 trackinfo.dwFlags = TME_QUERY;
5283 trackinfo.hwndTrack = infoPtr->hwnd;
5285 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5286 _TrackMouseEvent(&trackinfo);
5288 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5289 if(!(trackinfo.dwFlags & TME_LEAVE))
5291 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5292 trackinfo.hwndTrack = infoPtr->hwnd;
5293 /* do it as fast as possible, minimal systimer latency will be used */
5294 trackinfo.dwHoverTime = 1;
5296 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5297 /* and can properly deactivate the hot item */
5298 _TrackMouseEvent(&trackinfo);
5301 pt.x = (short)LOWORD(lParam);
5302 pt.y = (short)HIWORD(lParam);
5304 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5306 if (item != infoPtr->hotItem)
5308 /* redraw old hot item */
5309 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5310 infoPtr->hotItem = item;
5311 /* redraw new hot item */
5312 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5318 /* Draw themed border */
5319 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5321 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5325 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5326 cyEdge = GetSystemMetrics (SM_CYEDGE);
5329 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5331 GetWindowRect(infoPtr->hwnd, &r);
5333 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5334 r.right - cxEdge, r.bottom - cyEdge);
5335 if (region != (HRGN)1)
5336 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5337 OffsetRect(&r, -r.left, -r.top);
5339 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5340 OffsetRect(&r, -r.left, -r.top);
5342 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5343 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5344 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5345 ReleaseDC(infoPtr->hwnd, dc);
5347 /* Call default proc to get the scrollbars etc. painted */
5348 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5354 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5356 LPNMHDR lpnmh = (LPNMHDR)lParam;
5358 if (lpnmh->code == PGN_CALCSIZE) {
5359 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5361 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5362 lppgc->iWidth = infoPtr->treeWidth;
5363 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5364 infoPtr->treeWidth, infoPtr->clientWidth);
5367 lppgc->iHeight = infoPtr->treeHeight;
5368 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5369 infoPtr->treeHeight, infoPtr->clientHeight);
5373 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5377 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5379 if (wParam == SIZE_RESTORED)
5381 infoPtr->clientWidth = (short)LOWORD(lParam);
5382 infoPtr->clientHeight = (short)HIWORD(lParam);
5384 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5385 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5386 TREEVIEW_UpdateScrollBars(infoPtr);
5390 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5393 TREEVIEW_Invalidate(infoPtr, NULL);
5398 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5400 TRACE("(%lx %lx)\n", wParam, lParam);
5402 if (wParam == GWL_STYLE)
5404 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5406 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5408 if (dwNewStyle & TVS_CHECKBOXES)
5410 initialize_checkboxes(infoPtr);
5411 TRACE("checkboxes enabled\n");
5415 FIXME("tried to disable checkboxes\n");
5419 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5421 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5423 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5424 TRACE("tooltips enabled\n");
5428 DestroyWindow(infoPtr->hwndToolTip);
5429 infoPtr->hwndToolTip = 0;
5430 TRACE("tooltips disabled\n");
5434 infoPtr->dwStyle = dwNewStyle;
5437 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5438 TREEVIEW_UpdateScrollBars(infoPtr);
5439 TREEVIEW_Invalidate(infoPtr, NULL);
5445 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5448 TREEVIEW_ITEM * item;
5452 ScreenToClient(infoPtr->hwnd, &pt);
5454 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5456 memset(&nmmouse, 0, sizeof(nmmouse));
5457 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5458 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5459 nmmouse.hdr.code = NM_SETCURSOR;
5462 nmmouse.dwItemSpec = (DWORD_PTR)item;
5463 nmmouse.dwItemData = item->lParam;
5467 nmmouse.dwHitInfo = lParam;
5468 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, (LPARAM)&nmmouse))
5471 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5473 SetCursor(infoPtr->hcurHand);
5477 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5481 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5485 if (!infoPtr->selectedItem)
5487 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5491 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5492 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5497 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5501 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5502 UpdateWindow(infoPtr->hwnd);
5503 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5507 /* update theme after a WM_THEMECHANGED message */
5508 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5510 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5511 CloseThemeData (theme);
5512 OpenThemeData (infoPtr->hwnd, themeClass);
5517 static LRESULT WINAPI
5518 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5520 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5522 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5524 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5527 if (uMsg == WM_CREATE)
5528 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5535 case TVM_CREATEDRAGIMAGE:
5536 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5538 case TVM_DELETEITEM:
5539 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5541 case TVM_EDITLABELA:
5542 case TVM_EDITLABELW:
5543 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5545 case TVM_ENDEDITLABELNOW:
5546 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5548 case TVM_ENSUREVISIBLE:
5549 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5552 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5554 case TVM_GETBKCOLOR:
5555 return TREEVIEW_GetBkColor(infoPtr);
5558 return TREEVIEW_GetCount(infoPtr);
5560 case TVM_GETEDITCONTROL:
5561 return TREEVIEW_GetEditControl(infoPtr);
5563 case TVM_GETIMAGELIST:
5564 return TREEVIEW_GetImageList(infoPtr, wParam);
5567 return TREEVIEW_GetIndent(infoPtr);
5569 case TVM_GETINSERTMARKCOLOR:
5570 return TREEVIEW_GetInsertMarkColor(infoPtr);
5572 case TVM_GETISEARCHSTRINGA:
5573 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5576 case TVM_GETISEARCHSTRINGW:
5577 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5582 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5583 uMsg == TVM_GETITEMW);
5584 case TVM_GETITEMHEIGHT:
5585 return TREEVIEW_GetItemHeight(infoPtr);
5587 case TVM_GETITEMRECT:
5588 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5590 case TVM_GETITEMSTATE:
5591 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5593 case TVM_GETLINECOLOR:
5594 return TREEVIEW_GetLineColor(infoPtr);
5596 case TVM_GETNEXTITEM:
5597 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5599 case TVM_GETSCROLLTIME:
5600 return TREEVIEW_GetScrollTime(infoPtr);
5602 case TVM_GETTEXTCOLOR:
5603 return TREEVIEW_GetTextColor(infoPtr);
5605 case TVM_GETTOOLTIPS:
5606 return TREEVIEW_GetToolTips(infoPtr);
5608 case TVM_GETUNICODEFORMAT:
5609 return TREEVIEW_GetUnicodeFormat(infoPtr);
5611 case TVM_GETVISIBLECOUNT:
5612 return TREEVIEW_GetVisibleCount(infoPtr);
5615 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5617 case TVM_INSERTITEMA:
5618 case TVM_INSERTITEMW:
5619 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5620 uMsg == TVM_INSERTITEMW);
5621 case TVM_SELECTITEM:
5622 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5624 case TVM_SETBKCOLOR:
5625 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5627 case TVM_SETIMAGELIST:
5628 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5631 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5633 case TVM_SETINSERTMARK:
5634 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5636 case TVM_SETINSERTMARKCOLOR:
5637 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5641 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5642 uMsg == TVM_SETITEMW);
5643 case TVM_SETLINECOLOR:
5644 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5646 case TVM_SETITEMHEIGHT:
5647 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5649 case TVM_SETSCROLLTIME:
5650 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5652 case TVM_SETTEXTCOLOR:
5653 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5655 case TVM_SETTOOLTIPS:
5656 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5658 case TVM_SETUNICODEFORMAT:
5659 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5661 case TVM_SORTCHILDREN:
5662 return TREEVIEW_SortChildren(infoPtr, lParam);
5664 case TVM_SORTCHILDRENCB:
5665 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5668 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5671 return TREEVIEW_Command(infoPtr, wParam, lParam);
5674 return TREEVIEW_Destroy(infoPtr);
5679 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5682 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5685 return TREEVIEW_GetFont(infoPtr);
5688 return TREEVIEW_HScroll(infoPtr, wParam);
5691 return TREEVIEW_KeyDown(infoPtr, wParam);
5694 return TREEVIEW_KillFocus(infoPtr);
5696 case WM_LBUTTONDBLCLK:
5697 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5699 case WM_LBUTTONDOWN:
5700 return TREEVIEW_LButtonDown(infoPtr, lParam);
5702 /* WM_MBUTTONDOWN */
5705 return TREEVIEW_MouseLeave(infoPtr);
5708 return TREEVIEW_MouseMove(infoPtr, lParam);
5710 case WM_NCLBUTTONDOWN:
5711 if (infoPtr->hwndEdit)
5712 SetFocus(infoPtr->hwnd);
5716 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5719 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5721 case WM_NOTIFYFORMAT:
5722 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5724 case WM_PRINTCLIENT:
5725 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5728 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5730 case WM_RBUTTONDOWN:
5731 return TREEVIEW_RButtonDown(infoPtr, lParam);
5734 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5737 return TREEVIEW_SetFocus(infoPtr);
5740 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5743 return TREEVIEW_SetRedraw(infoPtr, wParam);
5746 return TREEVIEW_Size(infoPtr, wParam, lParam);
5748 case WM_STYLECHANGED:
5749 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5751 case WM_SYSCOLORCHANGE:
5752 COMCTL32_RefreshSysColors();
5758 return TREEVIEW_HandleTimer(infoPtr, wParam);
5760 case WM_THEMECHANGED:
5761 return TREEVIEW_ThemeChanged (infoPtr);
5764 return TREEVIEW_VScroll(infoPtr, wParam);
5766 /* WM_WININICHANGE */
5769 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5772 TRACE("drawItem\n");
5776 /* This mostly catches MFC and Delphi messages. :( */
5777 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5778 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5780 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5785 /* Class Registration ***************************************************/
5788 TREEVIEW_Register(void)
5794 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5795 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5796 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5797 wndClass.cbClsExtra = 0;
5798 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5800 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5801 wndClass.hbrBackground = 0;
5802 wndClass.lpszClassName = WC_TREEVIEWW;
5804 RegisterClassW(&wndClass);
5809 TREEVIEW_Unregister(void)
5811 UnregisterClassW(WC_TREEVIEWW, NULL);
5815 /* Tree Verification ****************************************************/
5818 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5820 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5821 TREEVIEW_ITEM *item)
5823 assert(infoPtr != NULL);
5824 assert(item != NULL);
5826 /* both NULL, or both non-null */
5827 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5829 assert(item->firstChild != item);
5830 assert(item->lastChild != item);
5832 if (item->firstChild)
5834 assert(item->firstChild->parent == item);
5835 assert(item->firstChild->prevSibling == NULL);
5838 if (item->lastChild)
5840 assert(item->lastChild->parent == item);
5841 assert(item->lastChild->nextSibling == NULL);
5844 assert(item->nextSibling != item);
5845 if (item->nextSibling)
5847 assert(item->nextSibling->parent == item->parent);
5848 assert(item->nextSibling->prevSibling == item);
5851 assert(item->prevSibling != item);
5852 if (item->prevSibling)
5854 assert(item->prevSibling->parent == item->parent);
5855 assert(item->prevSibling->nextSibling == item);
5860 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5862 assert(item != NULL);
5864 assert(item->parent != NULL);
5865 assert(item->parent != item);
5866 assert(item->iLevel == item->parent->iLevel + 1);
5868 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5870 TREEVIEW_VerifyItemCommon(infoPtr, item);
5872 TREEVIEW_VerifyChildren(infoPtr, item);
5876 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5878 TREEVIEW_ITEM *child;
5879 assert(item != NULL);
5881 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5882 TREEVIEW_VerifyItem(infoPtr, child);
5886 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5888 TREEVIEW_ITEM *root = infoPtr->root;
5890 assert(root != NULL);
5891 assert(root->iLevel == -1);
5892 assert(root->parent == NULL);
5893 assert(root->prevSibling == NULL);
5895 TREEVIEW_VerifyItemCommon(infoPtr, root);
5897 TREEVIEW_VerifyChildren(infoPtr, root);
5901 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5903 if (!TRACE_ON(treeview)) return;
5905 assert(infoPtr != NULL);
5906 TREEVIEW_VerifyRoot(infoPtr);