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 = (WORD)I_IMAGENONE;
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 if (tvItem->mask & TVIF_STATEEX)
1213 FIXME("New extended state: %x\n", tvItem->uStateEx);
1216 wineItem->callbackMask |= callbackSet;
1217 wineItem->callbackMask &= ~callbackClear;
1222 /* Note that the new item is pre-zeroed. */
1224 TREEVIEW_InsertItemT(TREEVIEW_INFO *infoPtr, const TVINSERTSTRUCTW *ptdi, BOOL isW)
1226 const TVITEMEXW *tvItem = &ptdi->u.itemex;
1227 HTREEITEM insertAfter;
1228 TREEVIEW_ITEM *newItem, *parentItem;
1229 BOOL bTextUpdated = FALSE;
1231 if (ptdi->hParent == TVI_ROOT || ptdi->hParent == 0)
1233 parentItem = infoPtr->root;
1237 parentItem = ptdi->hParent;
1239 if (!TREEVIEW_ValidItem(infoPtr, parentItem))
1241 WARN("invalid parent %p\n", parentItem);
1246 insertAfter = ptdi->hInsertAfter;
1248 /* Validate this now for convenience. */
1249 switch ((DWORD_PTR)insertAfter)
1251 case (DWORD_PTR)TVI_FIRST:
1252 case (DWORD_PTR)TVI_LAST:
1253 case (DWORD_PTR)TVI_SORT:
1257 if (!TREEVIEW_ValidItem(infoPtr, insertAfter) ||
1258 insertAfter->parent != parentItem)
1260 WARN("invalid insert after %p\n", insertAfter);
1261 insertAfter = TVI_LAST;
1265 TRACE("parent %p position %p: %s\n", parentItem, insertAfter,
1266 (tvItem->mask & TVIF_TEXT)
1267 ? ((tvItem->pszText == LPSTR_TEXTCALLBACKW) ? "<callback>"
1268 : (isW ? debugstr_w(tvItem->pszText) : debugstr_a((LPSTR)tvItem->pszText)))
1271 newItem = TREEVIEW_AllocateItem(infoPtr);
1272 if (newItem == NULL)
1275 newItem->parent = parentItem;
1276 newItem->iIntegral = 1;
1277 newItem->visibleOrder = -1;
1279 if (!TREEVIEW_DoSetItemT(infoPtr, newItem, tvItem, isW))
1282 /* After this point, nothing can fail. (Except for TVI_SORT.) */
1284 infoPtr->uNumItems++;
1286 switch ((DWORD_PTR)insertAfter)
1288 case (DWORD_PTR)TVI_FIRST:
1290 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1291 TREEVIEW_InsertBefore(newItem, parentItem->firstChild, parentItem);
1292 if (infoPtr->firstVisible == originalFirst)
1293 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1297 case (DWORD_PTR)TVI_LAST:
1298 TREEVIEW_InsertAfter(newItem, parentItem->lastChild, parentItem);
1301 /* hInsertAfter names a specific item we want to insert after */
1303 TREEVIEW_InsertAfter(newItem, insertAfter, insertAfter->parent);
1306 case (DWORD_PTR)TVI_SORT:
1308 TREEVIEW_ITEM *aChild;
1309 TREEVIEW_ITEM *previousChild = NULL;
1310 TREEVIEW_ITEM *originalFirst = parentItem->firstChild;
1311 BOOL bItemInserted = FALSE;
1313 aChild = parentItem->firstChild;
1315 bTextUpdated = TRUE;
1316 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1318 /* Iterate the parent children to see where we fit in */
1319 while (aChild != NULL)
1323 TREEVIEW_UpdateDispInfo(infoPtr, aChild, TVIF_TEXT);
1324 comp = lstrcmpW(newItem->pszText, aChild->pszText);
1326 if (comp < 0) /* we are smaller than the current one */
1328 TREEVIEW_InsertBefore(newItem, aChild, parentItem);
1329 if (infoPtr->firstVisible == originalFirst &&
1330 aChild == originalFirst)
1331 TREEVIEW_SetFirstVisible(infoPtr, newItem, TRUE);
1332 bItemInserted = TRUE;
1335 else if (comp > 0) /* we are bigger than the current one */
1337 previousChild = aChild;
1339 /* This will help us to exit if there is no more sibling */
1340 aChild = (aChild->nextSibling == 0)
1342 : aChild->nextSibling;
1344 /* Look at the next item */
1350 * An item with this name is already existing, therefore,
1351 * we add after the one we found
1353 TREEVIEW_InsertAfter(newItem, aChild, parentItem);
1354 bItemInserted = TRUE;
1360 * we reach the end of the child list and the item has not
1361 * yet been inserted, therefore, insert it after the last child.
1363 if ((!bItemInserted) && (aChild == NULL))
1364 TREEVIEW_InsertAfter(newItem, previousChild, parentItem);
1371 TRACE("new item %p; parent %p, mask %x\n", newItem,
1372 newItem->parent, tvItem->mask);
1374 newItem->iLevel = newItem->parent->iLevel + 1;
1376 if (newItem->parent->cChildren == 0)
1377 newItem->parent->cChildren = 1;
1379 if (infoPtr->dwStyle & TVS_CHECKBOXES)
1381 if (STATEIMAGEINDEX(newItem->state) == 0)
1382 newItem->state |= INDEXTOSTATEIMAGEMASK(1);
1385 if (infoPtr->firstVisible == NULL)
1386 infoPtr->firstVisible = newItem;
1388 TREEVIEW_VerifyTree(infoPtr);
1390 if (!infoPtr->bRedraw) return (LRESULT)newItem;
1392 if (parentItem == infoPtr->root ||
1393 (ISVISIBLE(parentItem) && parentItem->state & TVIS_EXPANDED))
1395 TREEVIEW_ITEM *item;
1396 TREEVIEW_ITEM *prev = TREEVIEW_GetPrevListItem(infoPtr, newItem);
1398 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1399 TREEVIEW_ComputeItemInternalMetrics(infoPtr, newItem);
1402 TREEVIEW_UpdateDispInfo(infoPtr, newItem, TVIF_TEXT);
1404 TREEVIEW_ComputeTextWidth(infoPtr, newItem, 0);
1405 TREEVIEW_UpdateScrollBars(infoPtr);
1407 * if the item was inserted in a visible part of the tree,
1408 * invalidate it, as well as those after it
1410 for (item = newItem;
1412 item = TREEVIEW_GetNextListItem(infoPtr, item))
1413 TREEVIEW_Invalidate(infoPtr, item);
1417 /* refresh treeview if newItem is the first item inserted under parentItem */
1418 if (ISVISIBLE(parentItem) && newItem->prevSibling == newItem->nextSibling)
1420 /* parent got '+' - update it */
1421 TREEVIEW_Invalidate(infoPtr, parentItem);
1425 return (LRESULT)newItem;
1428 /* Item Deletion ************************************************************/
1430 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem);
1433 TREEVIEW_RemoveAllChildren(TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *parentItem)
1435 TREEVIEW_ITEM *kill = parentItem->firstChild;
1437 while (kill != NULL)
1439 TREEVIEW_ITEM *next = kill->nextSibling;
1441 TREEVIEW_RemoveItem(infoPtr, kill);
1446 assert(parentItem->cChildren <= 0); /* I_CHILDRENCALLBACK or 0 */
1447 assert(parentItem->firstChild == NULL);
1448 assert(parentItem->lastChild == NULL);
1452 TREEVIEW_UnlinkItem(const TREEVIEW_ITEM *item)
1454 TREEVIEW_ITEM *parentItem = item->parent;
1456 assert(item != NULL);
1457 assert(item->parent != NULL); /* i.e. it must not be the root */
1459 if (parentItem->firstChild == item)
1460 parentItem->firstChild = item->nextSibling;
1462 if (parentItem->lastChild == item)
1463 parentItem->lastChild = item->prevSibling;
1465 if (parentItem->firstChild == NULL && parentItem->lastChild == NULL
1466 && parentItem->cChildren > 0)
1467 parentItem->cChildren = 0;
1469 if (item->prevSibling)
1470 item->prevSibling->nextSibling = item->nextSibling;
1472 if (item->nextSibling)
1473 item->nextSibling->prevSibling = item->prevSibling;
1477 TREEVIEW_RemoveItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
1479 TRACE("%p, (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1481 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_DELETEITEMW, TVC_UNKNOWN,
1482 TVIF_HANDLE | TVIF_PARAM, wineItem, 0);
1484 if (wineItem->firstChild)
1485 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
1487 TREEVIEW_UnlinkItem(wineItem);
1489 infoPtr->uNumItems--;
1491 if (wineItem->pszText != LPSTR_TEXTCALLBACKW)
1492 Free(wineItem->pszText);
1494 TREEVIEW_FreeItem(infoPtr, wineItem);
1498 /* Empty out the tree. */
1500 TREEVIEW_RemoveTree(TREEVIEW_INFO *infoPtr)
1502 TREEVIEW_RemoveAllChildren(infoPtr, infoPtr->root);
1504 assert(infoPtr->uNumItems == 0); /* root isn't counted in uNumItems */
1508 TREEVIEW_DeleteItem(TREEVIEW_INFO *infoPtr, HTREEITEM wineItem)
1510 TREEVIEW_ITEM *newSelection = NULL;
1511 TREEVIEW_ITEM *newFirstVisible = NULL;
1512 TREEVIEW_ITEM *parent, *prev = NULL;
1513 BOOL visible = FALSE;
1515 if (wineItem == TVI_ROOT)
1517 TRACE("TVI_ROOT\n");
1518 parent = infoPtr->root;
1519 newSelection = NULL;
1521 TREEVIEW_RemoveTree(infoPtr);
1525 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
1528 TRACE("%p (%s)\n", wineItem, TREEVIEW_ItemName(wineItem));
1529 parent = wineItem->parent;
1531 if (ISVISIBLE(wineItem))
1533 prev = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
1537 if (infoPtr->selectedItem != NULL
1538 && (wineItem == infoPtr->selectedItem
1539 || TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem)))
1541 if (wineItem->nextSibling)
1542 newSelection = wineItem->nextSibling;
1543 else if (wineItem->parent != infoPtr->root)
1544 newSelection = wineItem->parent;
1546 newSelection = wineItem->prevSibling;
1547 TRACE("newSelection = %p\n", newSelection);
1550 if (infoPtr->firstVisible == wineItem)
1552 if (wineItem->nextSibling)
1553 newFirstVisible = wineItem->nextSibling;
1554 else if (wineItem->prevSibling)
1555 newFirstVisible = wineItem->prevSibling;
1556 else if (wineItem->parent != infoPtr->root)
1557 newFirstVisible = wineItem->parent;
1558 TREEVIEW_SetFirstVisible(infoPtr, NULL, TRUE);
1561 newFirstVisible = infoPtr->firstVisible;
1563 TREEVIEW_RemoveItem(infoPtr, wineItem);
1566 /* Don't change if somebody else already has (infoPtr->selectedItem is cleared by FreeItem). */
1567 if (!infoPtr->selectedItem && newSelection)
1569 if (TREEVIEW_ValidItem(infoPtr, newSelection))
1570 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection, TVC_UNKNOWN);
1573 /* Validate insertMark dropItem.
1574 * hotItem ??? - used for comparison only.
1576 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->insertMarkItem))
1577 infoPtr->insertMarkItem = 0;
1579 if (!TREEVIEW_ValidItem(infoPtr, infoPtr->dropItem))
1580 infoPtr->dropItem = 0;
1582 if (!TREEVIEW_ValidItem(infoPtr, newFirstVisible))
1583 newFirstVisible = infoPtr->root->firstChild;
1585 TREEVIEW_VerifyTree(infoPtr);
1587 if (!infoPtr->bRedraw) return TRUE;
1591 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
1592 TREEVIEW_RecalculateVisibleOrder(infoPtr, prev);
1593 TREEVIEW_UpdateScrollBars(infoPtr);
1594 TREEVIEW_Invalidate(infoPtr, NULL);
1596 else if (ISVISIBLE(parent) && !TREEVIEW_HasChildren(infoPtr, parent))
1598 /* parent lost '+/-' - update it */
1599 TREEVIEW_Invalidate(infoPtr, parent);
1606 /* Get/Set Messages *********************************************************/
1608 TREEVIEW_SetRedraw(TREEVIEW_INFO* infoPtr, WPARAM wParam)
1610 infoPtr->bRedraw = wParam ? TRUE : FALSE;
1612 if (infoPtr->bRedraw)
1614 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1615 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1616 TREEVIEW_UpdateScrollBars(infoPtr);
1617 TREEVIEW_Invalidate(infoPtr, NULL);
1623 TREEVIEW_GetIndent(const TREEVIEW_INFO *infoPtr)
1626 return infoPtr->uIndent;
1630 TREEVIEW_SetIndent(TREEVIEW_INFO *infoPtr, UINT newIndent)
1634 if (newIndent < MINIMUM_INDENT)
1635 newIndent = MINIMUM_INDENT;
1637 if (infoPtr->uIndent != newIndent)
1639 infoPtr->uIndent = newIndent;
1640 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1641 TREEVIEW_UpdateScrollBars(infoPtr);
1642 TREEVIEW_Invalidate(infoPtr, NULL);
1650 TREEVIEW_GetToolTips(const TREEVIEW_INFO *infoPtr)
1653 return (LRESULT)infoPtr->hwndToolTip;
1657 TREEVIEW_SetToolTips(TREEVIEW_INFO *infoPtr, HWND hwndTT)
1662 prevToolTip = infoPtr->hwndToolTip;
1663 infoPtr->hwndToolTip = hwndTT;
1665 return (LRESULT)prevToolTip;
1669 TREEVIEW_SetUnicodeFormat(TREEVIEW_INFO *infoPtr, BOOL fUnicode)
1671 BOOL rc = infoPtr->bNtfUnicode;
1672 infoPtr->bNtfUnicode = fUnicode;
1677 TREEVIEW_GetUnicodeFormat(const TREEVIEW_INFO *infoPtr)
1679 return infoPtr->bNtfUnicode;
1683 TREEVIEW_GetScrollTime(const TREEVIEW_INFO *infoPtr)
1685 return infoPtr->uScrollTime;
1689 TREEVIEW_SetScrollTime(TREEVIEW_INFO *infoPtr, UINT uScrollTime)
1691 UINT uOldScrollTime = infoPtr->uScrollTime;
1693 infoPtr->uScrollTime = min(uScrollTime, 100);
1695 return uOldScrollTime;
1700 TREEVIEW_GetImageList(const TREEVIEW_INFO *infoPtr, WPARAM wParam)
1707 return (LRESULT)infoPtr->himlNormal;
1710 return (LRESULT)infoPtr->himlState;
1717 #define TVHEIGHT_MIN 16
1718 #define TVHEIGHT_FONT_ADJUST 3 /* 2 for focus border + 1 for margin some apps assume */
1720 /* Compute the natural height for items. */
1722 TREEVIEW_NaturalHeight(const TREEVIEW_INFO *infoPtr)
1726 HFONT hOldFont = SelectObject(hdc, infoPtr->hFont);
1729 /* Height is the maximum of:
1730 * 16 (a hack because our fonts are tiny), and
1731 * The text height + border & margin, and
1732 * The size of the normal image list
1734 GetTextMetricsW(hdc, &tm);
1735 SelectObject(hdc, hOldFont);
1738 height = TVHEIGHT_MIN;
1739 if (height < tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST)
1740 height = tm.tmHeight + tm.tmExternalLeading + TVHEIGHT_FONT_ADJUST;
1741 if (height < infoPtr->normalImageHeight)
1742 height = infoPtr->normalImageHeight;
1744 /* Round down, unless we support odd ("non even") heights. */
1745 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1752 TREEVIEW_SetImageList(TREEVIEW_INFO *infoPtr, WPARAM wParam, HIMAGELIST himlNew)
1754 HIMAGELIST himlOld = 0;
1755 int oldWidth = infoPtr->normalImageWidth;
1756 int oldHeight = infoPtr->normalImageHeight;
1759 TRACE("%lx,%p\n", wParam, himlNew);
1764 himlOld = infoPtr->himlNormal;
1765 infoPtr->himlNormal = himlNew;
1767 if (himlNew != NULL)
1768 ImageList_GetIconSize(himlNew, &infoPtr->normalImageWidth,
1769 &infoPtr->normalImageHeight);
1772 infoPtr->normalImageWidth = 0;
1773 infoPtr->normalImageHeight = 0;
1779 himlOld = infoPtr->himlState;
1780 infoPtr->himlState = himlNew;
1782 if (himlNew != NULL)
1783 ImageList_GetIconSize(himlNew, &infoPtr->stateImageWidth,
1784 &infoPtr->stateImageHeight);
1787 infoPtr->stateImageWidth = 0;
1788 infoPtr->stateImageHeight = 0;
1794 if (oldWidth != infoPtr->normalImageWidth ||
1795 oldHeight != infoPtr->normalImageHeight)
1797 BOOL bRecalcVisible = FALSE;
1799 if (oldHeight != infoPtr->normalImageHeight &&
1800 !infoPtr->bHeightSet)
1802 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1803 bRecalcVisible = TRUE;
1806 if (infoPtr->normalImageWidth > MINIMUM_INDENT &&
1807 infoPtr->normalImageWidth != infoPtr->uIndent)
1809 infoPtr->uIndent = infoPtr->normalImageWidth;
1810 bRecalcVisible = TRUE;
1814 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1816 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1817 TREEVIEW_UpdateScrollBars(infoPtr);
1820 TREEVIEW_Invalidate(infoPtr, NULL);
1822 return (LRESULT)himlOld;
1826 TREEVIEW_SetItemHeight(TREEVIEW_INFO *infoPtr, INT newHeight)
1828 INT prevHeight = infoPtr->uItemHeight;
1830 TRACE("%d\n", newHeight);
1831 if (newHeight == -1)
1833 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1834 infoPtr->bHeightSet = FALSE;
1838 infoPtr->uItemHeight = newHeight;
1839 infoPtr->bHeightSet = TRUE;
1842 /* Round down, unless we support odd ("non even") heights. */
1843 if (!(infoPtr->dwStyle & TVS_NONEVENHEIGHT))
1844 infoPtr->uItemHeight &= ~1;
1846 if (infoPtr->uItemHeight != prevHeight)
1848 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1849 TREEVIEW_UpdateScrollBars(infoPtr);
1850 TREEVIEW_Invalidate(infoPtr, NULL);
1857 TREEVIEW_GetItemHeight(const TREEVIEW_INFO *infoPtr)
1860 return infoPtr->uItemHeight;
1865 TREEVIEW_GetFont(const TREEVIEW_INFO *infoPtr)
1867 TRACE("%p\n", infoPtr->hFont);
1868 return (LRESULT)infoPtr->hFont;
1873 TREEVIEW_ResetTextWidth(LPVOID pItem, LPVOID unused)
1877 ((TREEVIEW_ITEM *)pItem)->textWidth = 0;
1883 TREEVIEW_SetFont(TREEVIEW_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
1885 UINT uHeight = infoPtr->uItemHeight;
1887 TRACE("%p %i\n", hFont, bRedraw);
1889 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
1891 DeleteObject(infoPtr->hBoldFont);
1892 DeleteObject(infoPtr->hUnderlineFont);
1893 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
1894 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
1896 if (!infoPtr->bHeightSet)
1897 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
1899 if (uHeight != infoPtr->uItemHeight)
1900 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
1902 DPA_EnumCallback(infoPtr->items, TREEVIEW_ResetTextWidth, 0);
1904 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
1905 TREEVIEW_UpdateScrollBars(infoPtr);
1908 TREEVIEW_Invalidate(infoPtr, NULL);
1915 TREEVIEW_GetLineColor(const TREEVIEW_INFO *infoPtr)
1918 return (LRESULT)infoPtr->clrLine;
1922 TREEVIEW_SetLineColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1924 COLORREF prevColor = infoPtr->clrLine;
1927 infoPtr->clrLine = color;
1928 return (LRESULT)prevColor;
1933 TREEVIEW_GetTextColor(const TREEVIEW_INFO *infoPtr)
1936 return (LRESULT)infoPtr->clrText;
1940 TREEVIEW_SetTextColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1942 COLORREF prevColor = infoPtr->clrText;
1945 infoPtr->clrText = color;
1947 if (infoPtr->clrText != prevColor)
1948 TREEVIEW_Invalidate(infoPtr, NULL);
1950 return (LRESULT)prevColor;
1955 TREEVIEW_GetBkColor(const TREEVIEW_INFO *infoPtr)
1958 return (LRESULT)infoPtr->clrBk;
1962 TREEVIEW_SetBkColor(TREEVIEW_INFO *infoPtr, COLORREF newColor)
1964 COLORREF prevColor = infoPtr->clrBk;
1967 infoPtr->clrBk = newColor;
1969 if (newColor != prevColor)
1970 TREEVIEW_Invalidate(infoPtr, NULL);
1972 return (LRESULT)prevColor;
1977 TREEVIEW_GetInsertMarkColor(const TREEVIEW_INFO *infoPtr)
1980 return (LRESULT)infoPtr->clrInsertMark;
1984 TREEVIEW_SetInsertMarkColor(TREEVIEW_INFO *infoPtr, COLORREF color)
1986 COLORREF prevColor = infoPtr->clrInsertMark;
1988 TRACE("%x\n", color);
1989 infoPtr->clrInsertMark = color;
1991 return (LRESULT)prevColor;
1996 TREEVIEW_SetInsertMark(TREEVIEW_INFO *infoPtr, BOOL wParam, HTREEITEM item)
1998 TRACE("%d %p\n", wParam, item);
2000 if (!TREEVIEW_ValidItem(infoPtr, item))
2003 infoPtr->insertBeforeorAfter = wParam;
2004 infoPtr->insertMarkItem = item;
2006 TREEVIEW_Invalidate(infoPtr, NULL);
2012 /************************************************************************
2013 * Some serious braindamage here. lParam is a pointer to both the
2014 * input HTREEITEM and the output RECT.
2017 TREEVIEW_GetItemRect(const TREEVIEW_INFO *infoPtr, BOOL fTextRect, LPRECT lpRect)
2019 TREEVIEW_ITEM *wineItem;
2020 const HTREEITEM *pItem = (HTREEITEM *)lpRect;
2024 * validate parameters
2030 if (!TREEVIEW_ValidItem(infoPtr, wineItem) || !ISVISIBLE(wineItem))
2034 * If wParam is TRUE return the text size otherwise return
2035 * the whole item size
2039 /* Windows does not send TVN_GETDISPINFO here. */
2041 lpRect->top = wineItem->rect.top;
2042 lpRect->bottom = wineItem->rect.bottom;
2044 lpRect->left = wineItem->textOffset;
2045 if (!wineItem->textWidth)
2046 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2048 lpRect->right = wineItem->textOffset + wineItem->textWidth + 4;
2052 *lpRect = wineItem->rect;
2055 TRACE("%s [%s]\n", fTextRect ? "text" : "item", wine_dbgstr_rect(lpRect));
2060 static inline LRESULT
2061 TREEVIEW_GetVisibleCount(const TREEVIEW_INFO *infoPtr)
2063 /* Surprise! This does not take integral height into account. */
2064 return infoPtr->clientHeight / infoPtr->uItemHeight;
2069 TREEVIEW_GetItemT(const TREEVIEW_INFO *infoPtr, LPTVITEMEXW tvItem, BOOL isW)
2071 TREEVIEW_ITEM *wineItem;
2073 wineItem = tvItem->hItem;
2074 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2077 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, tvItem->mask);
2079 if (tvItem->mask & TVIF_CHILDREN)
2081 if (wineItem->cChildren==I_CHILDRENCALLBACK)
2082 FIXME("I_CHILDRENCALLBACK not supported\n");
2083 tvItem->cChildren = wineItem->cChildren;
2086 if (tvItem->mask & TVIF_HANDLE)
2087 tvItem->hItem = wineItem;
2089 if (tvItem->mask & TVIF_IMAGE)
2090 tvItem->iImage = wineItem->iImage;
2092 if (tvItem->mask & TVIF_INTEGRAL)
2093 tvItem->iIntegral = wineItem->iIntegral;
2095 /* undocumented: windows ignores TVIF_PARAM and
2096 * * always sets lParam
2098 tvItem->lParam = wineItem->lParam;
2100 if (tvItem->mask & TVIF_SELECTEDIMAGE)
2101 tvItem->iSelectedImage = wineItem->iSelectedImage;
2103 if (tvItem->mask & TVIF_EXPANDEDIMAGE)
2104 tvItem->iExpandedImage = wineItem->iExpandedImage;
2106 if (tvItem->mask & TVIF_STATE)
2107 /* Careful here - Windows ignores the stateMask when you get the state
2108 That contradicts the documentation, but makes more common sense, masking
2109 retrieval in this way seems overkill */
2110 tvItem->state = wineItem->state;
2112 if (tvItem->mask & TVIF_TEXT)
2114 if (wineItem->pszText == NULL)
2116 if (tvItem->cchTextMax > 0)
2117 tvItem->pszText[0] = '\0';
2121 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2123 tvItem->pszText = LPSTR_TEXTCALLBACKW;
2124 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2128 lstrcpynW(tvItem->pszText, wineItem->pszText, tvItem->cchTextMax);
2133 if (wineItem->pszText == LPSTR_TEXTCALLBACKW)
2135 tvItem->pszText = (LPWSTR)LPSTR_TEXTCALLBACKA;
2136 FIXME(" GetItem called with LPSTR_TEXTCALLBACK\n");
2140 WideCharToMultiByte(CP_ACP, 0, wineItem->pszText, -1,
2141 (LPSTR)tvItem->pszText, tvItem->cchTextMax, NULL, NULL);
2145 TRACE("item <%p>, txt %p, img %p, mask %x\n",
2146 wineItem, tvItem->pszText, &tvItem->iImage, tvItem->mask);
2151 /* Beware MSDN Library Visual Studio 6.0. It says -1 on failure, 0 on success,
2152 * which is wrong. */
2154 TREEVIEW_SetItemT(TREEVIEW_INFO *infoPtr, const TVITEMEXW *tvItem, BOOL isW)
2156 TREEVIEW_ITEM *wineItem;
2157 TREEVIEW_ITEM originalItem;
2159 wineItem = tvItem->hItem;
2161 TRACE("item %d,mask %x\n", TREEVIEW_GetItemIndex(infoPtr, wineItem),
2164 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2167 /* store the original item values */
2168 originalItem = *wineItem;
2170 if (!TREEVIEW_DoSetItemT(infoPtr, wineItem, tvItem, isW))
2173 /* If the text or TVIS_BOLD was changed, and it is visible, recalculate. */
2174 if ((tvItem->mask & TVIF_TEXT
2175 || (tvItem->mask & TVIF_STATE && tvItem->stateMask & TVIS_BOLD))
2176 && ISVISIBLE(wineItem))
2178 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, TVIF_TEXT);
2179 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, 0);
2182 if (tvItem->mask != 0 && ISVISIBLE(wineItem))
2184 /* The refresh updates everything, but we can't wait until then. */
2185 TREEVIEW_ComputeItemInternalMetrics(infoPtr, wineItem);
2187 /* if any of the item's values changed and it's not a callback, redraw the item */
2188 if (item_changed(&originalItem, wineItem, tvItem))
2190 if (tvItem->mask & TVIF_INTEGRAL)
2192 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
2193 TREEVIEW_UpdateScrollBars(infoPtr);
2195 TREEVIEW_Invalidate(infoPtr, NULL);
2199 TREEVIEW_UpdateScrollBars(infoPtr);
2200 TREEVIEW_Invalidate(infoPtr, wineItem);
2209 TREEVIEW_GetItemState(const TREEVIEW_INFO *infoPtr, HTREEITEM wineItem, UINT mask)
2213 if (!wineItem || !TREEVIEW_ValidItem(infoPtr, wineItem))
2216 return (wineItem->state & mask);
2220 TREEVIEW_GetNextItem(const TREEVIEW_INFO *infoPtr, UINT which, HTREEITEM wineItem)
2222 TREEVIEW_ITEM *retval;
2226 /* handle all the global data here */
2229 case TVGN_CHILD: /* Special case: child of 0 is root */
2234 retval = infoPtr->root->firstChild;
2238 retval = infoPtr->selectedItem;
2241 case TVGN_FIRSTVISIBLE:
2242 retval = infoPtr->firstVisible;
2245 case TVGN_DROPHILITE:
2246 retval = infoPtr->dropItem;
2249 case TVGN_LASTVISIBLE:
2250 retval = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
2256 TRACE("flags:%x, returns %p\n", which, retval);
2257 return (LRESULT)retval;
2260 if (wineItem == TVI_ROOT) wineItem = infoPtr->root;
2262 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
2268 retval = wineItem->nextSibling;
2271 retval = wineItem->prevSibling;
2274 retval = (wineItem->parent != infoPtr->root) ? wineItem->parent : NULL;
2277 retval = wineItem->firstChild;
2279 case TVGN_NEXTVISIBLE:
2280 retval = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2282 case TVGN_PREVIOUSVISIBLE:
2283 retval = TREEVIEW_GetPrevListItem(infoPtr, wineItem);
2286 TRACE("Unknown msg %x,item %p\n", which, wineItem);
2290 TRACE("flags:%x, item %p;returns %p\n", which, wineItem, retval);
2291 return (LRESULT)retval;
2296 TREEVIEW_GetCount(const TREEVIEW_INFO *infoPtr)
2298 TRACE(" %d\n", infoPtr->uNumItems);
2299 return (LRESULT)infoPtr->uNumItems;
2303 TREEVIEW_ToggleItemState(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
2305 if (infoPtr->dwStyle & TVS_CHECKBOXES)
2307 static const unsigned int state_table[] = { 0, 2, 1 };
2311 state = STATEIMAGEINDEX(item->state);
2312 TRACE("state:%x\n", state);
2313 item->state &= ~TVIS_STATEIMAGEMASK;
2316 state = state_table[state];
2318 item->state |= INDEXTOSTATEIMAGEMASK(state);
2320 TRACE("state:%x\n", state);
2321 TREEVIEW_Invalidate(infoPtr, item);
2326 /* Painting *************************************************************/
2328 /* Draw the lines and expand button for an item. Also draws one section
2329 * of the line from item's parent to item's parent's next sibling. */
2331 TREEVIEW_DrawItemLines(const TREEVIEW_INFO *infoPtr, HDC hdc, const TREEVIEW_ITEM *item)
2333 LONG centerx, centery;
2334 BOOL lar = ((infoPtr->dwStyle
2335 & (TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS))
2338 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2340 if (!lar && item->iLevel == 0)
2343 hbr = CreateSolidBrush(clrBk);
2344 hbrOld = SelectObject(hdc, hbr);
2346 centerx = (item->linesOffset + item->stateOffset) / 2;
2347 centery = (item->rect.top + item->rect.bottom) / 2;
2349 if (infoPtr->dwStyle & TVS_HASLINES)
2351 HPEN hOldPen, hNewPen;
2355 /* Get a dotted grey pen */
2356 lb.lbStyle = BS_SOLID;
2357 lb.lbColor = GETLINECOLOR(infoPtr->clrLine);
2358 hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
2359 hOldPen = SelectObject(hdc, hNewPen);
2361 /* Make sure the center is on a dot (using +2 instead
2362 * of +1 gives us pixel-by-pixel compat with native) */
2363 centery = (centery + 2) & ~1;
2365 MoveToEx(hdc, item->stateOffset, centery, NULL);
2366 LineTo(hdc, centerx - 1, centery);
2368 if (item->prevSibling || item->parent != infoPtr->root)
2370 MoveToEx(hdc, centerx, item->rect.top, NULL);
2371 LineTo(hdc, centerx, centery);
2374 if (item->nextSibling)
2376 MoveToEx(hdc, centerx, centery, NULL);
2377 LineTo(hdc, centerx, item->rect.bottom + 1);
2380 /* Draw the line from our parent to its next sibling. */
2381 parent = item->parent;
2382 while (parent != infoPtr->root)
2384 int pcenterx = (parent->linesOffset + parent->stateOffset) / 2;
2386 if (parent->nextSibling
2387 /* skip top-levels unless TVS_LINESATROOT */
2388 && parent->stateOffset > parent->linesOffset)
2390 MoveToEx(hdc, pcenterx, item->rect.top, NULL);
2391 LineTo(hdc, pcenterx, item->rect.bottom + 1);
2394 parent = parent->parent;
2397 SelectObject(hdc, hOldPen);
2398 DeleteObject(hNewPen);
2402 * Display the (+/-) signs
2405 if (infoPtr->dwStyle & TVS_HASBUTTONS)
2407 if (item->cChildren)
2409 HTHEME theme = GetWindowTheme(infoPtr->hwnd);
2412 RECT glyphRect = item->rect;
2413 glyphRect.left = item->linesOffset;
2414 glyphRect.right = item->stateOffset;
2415 DrawThemeBackground (theme, hdc, TVP_GLYPH,
2416 (item->state & TVIS_EXPANDED) ? GLPS_OPENED : GLPS_CLOSED,
2421 LONG height = item->rect.bottom - item->rect.top;
2422 LONG width = item->stateOffset - item->linesOffset;
2423 LONG rectsize = min(height, width) / 4;
2424 /* plussize = ceil(rectsize * 3/4) */
2425 LONG plussize = (rectsize + 1) * 3 / 4;
2427 HPEN new_pen = CreatePen(PS_SOLID, 0, GETLINECOLOR(infoPtr->clrLine));
2428 HPEN old_pen = SelectObject(hdc, new_pen);
2430 Rectangle(hdc, centerx - rectsize - 1, centery - rectsize - 1,
2431 centerx + rectsize + 2, centery + rectsize + 2);
2433 SelectObject(hdc, old_pen);
2434 DeleteObject(new_pen);
2436 /* draw +/- signs with current text color */
2437 new_pen = CreatePen(PS_SOLID, 0, GETTXTCOLOR(infoPtr->clrText));
2438 old_pen = SelectObject(hdc, new_pen);
2440 if (height < 18 || width < 18)
2442 MoveToEx(hdc, centerx - plussize + 1, centery, NULL);
2443 LineTo(hdc, centerx + plussize, centery);
2445 if (!(item->state & TVIS_EXPANDED))
2447 MoveToEx(hdc, centerx, centery - plussize + 1, NULL);
2448 LineTo(hdc, centerx, centery + plussize);
2453 Rectangle(hdc, centerx - plussize + 1, centery - 1,
2454 centerx + plussize, centery + 2);
2456 if (!(item->state & TVIS_EXPANDED))
2458 Rectangle(hdc, centerx - 1, centery - plussize + 1,
2459 centerx + 2, centery + plussize);
2460 SetPixel(hdc, centerx - 1, centery, clrBk);
2461 SetPixel(hdc, centerx + 1, centery, clrBk);
2465 SelectObject(hdc, old_pen);
2466 DeleteObject(new_pen);
2470 SelectObject(hdc, hbrOld);
2475 TREEVIEW_DrawItem(const TREEVIEW_INFO *infoPtr, HDC hdc, TREEVIEW_ITEM *wineItem)
2479 COLORREF oldTextColor, oldTextBkColor;
2481 BOOL inFocus = (GetFocus() == infoPtr->hwnd);
2482 NMTVCUSTOMDRAW nmcdhdr;
2484 TREEVIEW_UpdateDispInfo(infoPtr, wineItem, CALLBACK_MASK_ALL);
2486 /* - If item is drop target or it is selected and window is in focus -
2487 * use blue background (COLOR_HIGHLIGHT).
2488 * - If item is selected, window is not in focus, but it has style
2489 * TVS_SHOWSELALWAYS - use grey background (COLOR_BTNFACE)
2490 * - Otherwise - use background color
2492 if ((wineItem->state & TVIS_DROPHILITED) || ((wineItem == infoPtr->focusedItem) && !(wineItem->state & TVIS_SELECTED)) ||
2493 ((wineItem->state & TVIS_SELECTED) && (!infoPtr->focusedItem) &&
2494 (inFocus || (infoPtr->dwStyle & TVS_SHOWSELALWAYS))))
2496 if ((wineItem->state & TVIS_DROPHILITED) || inFocus)
2498 nmcdhdr.clrTextBk = comctl32_color.clrHighlight;
2499 nmcdhdr.clrText = comctl32_color.clrHighlightText;
2503 nmcdhdr.clrTextBk = comctl32_color.clrBtnFace;
2504 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2509 nmcdhdr.clrTextBk = GETBKCOLOR(infoPtr->clrBk);
2510 if ((infoPtr->dwStyle & TVS_TRACKSELECT) && (wineItem == infoPtr->hotItem))
2511 nmcdhdr.clrText = comctl32_color.clrHighlight;
2513 nmcdhdr.clrText = GETTXTCOLOR(infoPtr->clrText);
2516 hOldFont = SelectObject(hdc, TREEVIEW_FontForItem(infoPtr, wineItem));
2518 /* The custom draw handler can query the text rectangle,
2520 /* should already be known, set to 0 when changed */
2521 if (!wineItem->textWidth)
2522 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2526 if (infoPtr->cdmode & CDRF_NOTIFYITEMDRAW)
2528 cditem = TREEVIEW_SendCustomDrawItemNotify
2529 (infoPtr, hdc, wineItem, CDDS_ITEMPREPAINT, &nmcdhdr);
2530 TRACE("prepaint:cditem-app returns 0x%x\n", cditem);
2532 if (cditem & CDRF_SKIPDEFAULT)
2534 SelectObject(hdc, hOldFont);
2539 if (cditem & CDRF_NEWFONT)
2540 TREEVIEW_ComputeTextWidth(infoPtr, wineItem, hdc);
2542 TREEVIEW_DrawItemLines(infoPtr, hdc, wineItem);
2544 /* Set colors. Custom draw handler can change these so we do this after it. */
2545 oldTextColor = SetTextColor(hdc, nmcdhdr.clrText);
2546 oldTextBkColor = SetBkColor(hdc, nmcdhdr.clrTextBk);
2548 centery = (wineItem->rect.top + wineItem->rect.bottom) / 2;
2551 * Display the images associated with this item
2556 /* State images are displayed to the left of the Normal image
2557 * image number is in state; zero should be `display no image'.
2559 imageIndex = STATEIMAGEINDEX(wineItem->state);
2561 if (infoPtr->himlState && imageIndex)
2563 ImageList_Draw(infoPtr->himlState, imageIndex, hdc,
2564 wineItem->stateOffset,
2565 centery - infoPtr->stateImageHeight / 2,
2569 /* Now, draw the normal image; can be either selected,
2570 * non-selected or expanded image.
2573 if ((wineItem->state & TVIS_SELECTED) && (wineItem->iSelectedImage >= 0))
2575 /* The item is currently selected */
2576 imageIndex = wineItem->iSelectedImage;
2578 else if ((wineItem->state & TVIS_EXPANDED) && (wineItem->iExpandedImage != (WORD)I_IMAGENONE))
2580 /* The item is currently not selected but expanded */
2581 imageIndex = wineItem->iExpandedImage;
2585 /* The item is not selected and not expanded */
2586 imageIndex = wineItem->iImage;
2589 if (infoPtr->himlNormal)
2591 int ovlIdx = wineItem->state & TVIS_OVERLAYMASK;
2593 ImageList_Draw(infoPtr->himlNormal, imageIndex, hdc,
2594 wineItem->imageOffset,
2595 centery - infoPtr->normalImageHeight / 2,
2596 ILD_NORMAL | ovlIdx);
2602 * Display the text associated with this item
2605 /* Don't paint item's text if it's being edited */
2606 if (!infoPtr->hwndEdit || (infoPtr->selectedItem != wineItem))
2608 if (wineItem->pszText)
2612 rcText.top = wineItem->rect.top;
2613 rcText.bottom = wineItem->rect.bottom;
2614 rcText.left = wineItem->textOffset;
2615 rcText.right = rcText.left + wineItem->textWidth + 4;
2617 TRACE("drawing text %s at (%s)\n",
2618 debugstr_w(wineItem->pszText), wine_dbgstr_rect(&rcText));
2621 ExtTextOutW(hdc, rcText.left + 2, rcText.top + 1,
2622 ETO_CLIPPED | ETO_OPAQUE,
2625 lstrlenW(wineItem->pszText),
2628 /* Draw the box around the selected item */
2629 if ((wineItem == infoPtr->selectedItem) && inFocus)
2631 DrawFocusRect(hdc,&rcText);
2637 /* Draw insertion mark if necessary */
2639 if (infoPtr->insertMarkItem)
2640 TRACE("item:%d,mark:%p\n",
2641 TREEVIEW_GetItemIndex(infoPtr, wineItem),
2642 infoPtr->insertMarkItem);
2644 if (wineItem == infoPtr->insertMarkItem)
2646 HPEN hNewPen, hOldPen;
2650 hNewPen = CreatePen(PS_SOLID, 2, GETINSCOLOR(infoPtr->clrInsertMark));
2651 hOldPen = SelectObject(hdc, hNewPen);
2653 if (infoPtr->insertBeforeorAfter)
2654 offset = wineItem->rect.bottom - 1;
2656 offset = wineItem->rect.top + 1;
2658 left = wineItem->textOffset - 2;
2659 right = wineItem->textOffset + wineItem->textWidth + 2;
2661 MoveToEx(hdc, left, offset - 3, NULL);
2662 LineTo(hdc, left, offset + 4);
2664 MoveToEx(hdc, left, offset, NULL);
2665 LineTo(hdc, right + 1, offset);
2667 MoveToEx(hdc, right, offset + 3, NULL);
2668 LineTo(hdc, right, offset - 4);
2670 SelectObject(hdc, hOldPen);
2671 DeleteObject(hNewPen);
2674 if (cditem & CDRF_NOTIFYPOSTPAINT)
2676 cditem = TREEVIEW_SendCustomDrawItemNotify
2677 (infoPtr, hdc, wineItem, CDDS_ITEMPOSTPAINT, &nmcdhdr);
2678 TRACE("postpaint:cditem-app returns 0x%x\n", cditem);
2681 /* Restore the hdc state */
2682 SetTextColor(hdc, oldTextColor);
2683 SetBkColor(hdc, oldTextBkColor);
2684 SelectObject(hdc, hOldFont);
2687 /* Computes treeHeight and treeWidth and updates the scroll bars.
2690 TREEVIEW_UpdateScrollBars(TREEVIEW_INFO *infoPtr)
2692 TREEVIEW_ITEM *wineItem;
2693 HWND hwnd = infoPtr->hwnd;
2697 LONG scrollX = infoPtr->scrollX;
2699 infoPtr->treeWidth = 0;
2700 infoPtr->treeHeight = 0;
2702 /* We iterate through all visible items in order to get the tree height
2704 wineItem = infoPtr->root->firstChild;
2706 while (wineItem != NULL)
2708 if (ISVISIBLE(wineItem))
2710 /* actually we draw text at textOffset + 2 */
2711 if (2+wineItem->textOffset+wineItem->textWidth > infoPtr->treeWidth)
2712 infoPtr->treeWidth = wineItem->textOffset+wineItem->textWidth+2;
2714 /* This is scroll-adjusted, but we fix this below. */
2715 infoPtr->treeHeight = wineItem->rect.bottom;
2718 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem);
2721 /* Fix the scroll adjusted treeHeight and treeWidth. */
2722 if (infoPtr->root->firstChild)
2723 infoPtr->treeHeight -= infoPtr->root->firstChild->rect.top;
2725 infoPtr->treeWidth += infoPtr->scrollX;
2727 if (infoPtr->dwStyle & TVS_NOSCROLL) return;
2729 /* Adding one scroll bar may take up enough space that it forces us
2730 * to add the other as well. */
2731 if (infoPtr->treeHeight > infoPtr->clientHeight)
2735 if (infoPtr->treeWidth
2736 > infoPtr->clientWidth - GetSystemMetrics(SM_CXVSCROLL))
2739 else if (infoPtr->treeWidth > infoPtr->clientWidth || infoPtr->scrollX > 0)
2742 if (!vert && horz && infoPtr->treeHeight
2743 > infoPtr->clientHeight - GetSystemMetrics(SM_CYVSCROLL))
2746 if (horz && (infoPtr->dwStyle & TVS_NOHSCROLL)) horz = FALSE;
2748 si.cbSize = sizeof(SCROLLINFO);
2749 si.fMask = SIF_POS|SIF_RANGE|SIF_PAGE;
2754 si.nPage = TREEVIEW_GetVisibleCount(infoPtr);
2755 if ( si.nPage && NULL != infoPtr->firstVisible)
2757 si.nPos = infoPtr->firstVisible->visibleOrder;
2758 si.nMax = infoPtr->maxVisibleOrder - 1;
2760 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2762 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
2763 ShowScrollBar(hwnd, SB_VERT, TRUE);
2764 infoPtr->uInternalStatus |= TV_VSCROLL;
2768 if (infoPtr->uInternalStatus & TV_VSCROLL)
2769 ShowScrollBar(hwnd, SB_VERT, FALSE);
2770 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2775 if (infoPtr->uInternalStatus & TV_VSCROLL)
2776 ShowScrollBar(hwnd, SB_VERT, FALSE);
2777 infoPtr->uInternalStatus &= ~TV_VSCROLL;
2782 si.nPage = infoPtr->clientWidth;
2783 si.nPos = infoPtr->scrollX;
2784 si.nMax = infoPtr->treeWidth - 1;
2786 if (si.nPos > si.nMax - max( si.nPage-1, 0 ))
2788 si.nPos = si.nMax - max( si.nPage-1, 0 );
2792 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
2793 ShowScrollBar(hwnd, SB_HORZ, TRUE);
2794 infoPtr->uInternalStatus |= TV_HSCROLL;
2796 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2797 TREEVIEW_HScroll(infoPtr,
2798 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2802 if (infoPtr->uInternalStatus & TV_HSCROLL)
2803 ShowScrollBar(hwnd, SB_HORZ, FALSE);
2804 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2807 if (infoPtr->scrollX != 0)
2809 TREEVIEW_HScroll(infoPtr,
2810 MAKEWPARAM(SB_THUMBPOSITION, scrollX));
2815 infoPtr->uInternalStatus &= ~TV_HSCROLL;
2819 TREEVIEW_FillBkgnd(const TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2822 COLORREF clrBk = GETBKCOLOR(infoPtr->clrBk);
2824 hBrush = CreateSolidBrush(clrBk);
2825 FillRect(hdc, rc, hBrush);
2826 DeleteObject(hBrush);
2829 /* CtrlSpy doesn't mention this, but CorelDRAW's object manager needs it. */
2831 TREEVIEW_EraseBackground(const TREEVIEW_INFO *infoPtr, HDC hdc)
2835 TRACE("%p\n", infoPtr);
2837 GetClientRect(infoPtr->hwnd, &rect);
2838 TREEVIEW_FillBkgnd(infoPtr, hdc, &rect);
2844 TREEVIEW_Refresh(TREEVIEW_INFO *infoPtr, HDC hdc, const RECT *rc)
2846 HWND hwnd = infoPtr->hwnd;
2848 TREEVIEW_ITEM *wineItem;
2850 if (infoPtr->clientHeight == 0 || infoPtr->clientWidth == 0)
2852 TRACE("empty window\n");
2856 infoPtr->cdmode = TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_PREPAINT,
2859 if (infoPtr->cdmode == CDRF_SKIPDEFAULT)
2861 ReleaseDC(hwnd, hdc);
2865 for (wineItem = infoPtr->root->firstChild;
2867 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
2869 if (ISVISIBLE(wineItem))
2871 /* Avoid unneeded calculations */
2872 if (wineItem->rect.top > rect.bottom)
2874 if (wineItem->rect.bottom < rect.top)
2877 TREEVIEW_DrawItem(infoPtr, hdc, wineItem);
2881 TREEVIEW_UpdateScrollBars(infoPtr);
2883 if (infoPtr->cdmode & CDRF_NOTIFYPOSTPAINT)
2885 TREEVIEW_SendCustomDrawNotify(infoPtr, CDDS_POSTPAINT, hdc, rect);
2889 TREEVIEW_InvalidateItem(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2891 if (item) InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2895 TREEVIEW_Invalidate(const TREEVIEW_INFO *infoPtr, const TREEVIEW_ITEM *item)
2898 InvalidateRect(infoPtr->hwnd, &item->rect, TRUE);
2900 InvalidateRect(infoPtr->hwnd, NULL, TRUE);
2904 TREEVIEW_Paint(TREEVIEW_INFO *infoPtr, HDC hdc_ref)
2910 TRACE("(%p %p)\n", infoPtr, hdc_ref);
2915 GetClientRect(infoPtr->hwnd, &rc);
2919 hdc = BeginPaint(infoPtr->hwnd, &ps);
2922 TREEVIEW_FillBkgnd(infoPtr, hdc, &rc);
2925 if(infoPtr->bRedraw) /* WM_SETREDRAW sets bRedraw */
2926 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2929 EndPaint(infoPtr->hwnd, &ps);
2935 TREEVIEW_PrintClient(TREEVIEW_INFO *infoPtr, HDC hdc, DWORD options)
2937 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2939 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwnd))
2942 if (options & PRF_ERASEBKGND)
2943 TREEVIEW_EraseBackground(infoPtr, hdc);
2945 if (options & PRF_CLIENT)
2948 GetClientRect(infoPtr->hwnd, &rc);
2949 TREEVIEW_Refresh(infoPtr, hdc, &rc);
2955 /* Sorting **************************************************************/
2957 /***************************************************************************
2958 * Forward the DPA local callback to the treeview owner callback
2961 TREEVIEW_CallBackCompare(const TREEVIEW_ITEM *first, const TREEVIEW_ITEM *second,
2962 const TVSORTCB *pCallBackSort)
2964 /* Forward the call to the client-defined callback */
2965 return pCallBackSort->lpfnCompare(first->lParam,
2967 pCallBackSort->lParam);
2970 /***************************************************************************
2971 * Treeview native sort routine: sort on item text.
2974 TREEVIEW_SortOnName(TREEVIEW_ITEM *first, TREEVIEW_ITEM *second,
2975 const TREEVIEW_INFO *infoPtr)
2977 TREEVIEW_UpdateDispInfo(infoPtr, first, TVIF_TEXT);
2978 TREEVIEW_UpdateDispInfo(infoPtr, second, TVIF_TEXT);
2980 if(first->pszText && second->pszText)
2981 return lstrcmpiW(first->pszText, second->pszText);
2982 else if(first->pszText)
2984 else if(second->pszText)
2990 /* Returns the number of physical children belonging to item. */
2992 TREEVIEW_CountChildren(const TREEVIEW_ITEM *item)
2997 for (hti = item->firstChild; hti != NULL; hti = hti->nextSibling)
3003 /* Returns a DPA containing a pointer to each physical child of item in
3004 * sibling order. If item has no children, an empty DPA is returned. */
3006 TREEVIEW_BuildChildDPA(const TREEVIEW_ITEM *item)
3008 HTREEITEM child = item->firstChild;
3010 HDPA list = DPA_Create(8);
3011 if (list == 0) return NULL;
3013 for (child = item->firstChild; child != NULL; child = child->nextSibling)
3015 if (DPA_InsertPtr(list, INT_MAX, child) == -1)
3025 /***************************************************************************
3026 * Setup the treeview structure with regards of the sort method
3027 * and sort the children of the TV item specified in lParam
3028 * fRecurse: currently unused. Should be zero.
3029 * parent: if pSort!=NULL, should equal pSort->hParent.
3030 * otherwise, item which child items are to be sorted.
3031 * pSort: sort method info. if NULL, sort on item text.
3032 * if non-NULL, sort on item's lParam content, and let the
3033 * application decide what that means. See also TVM_SORTCHILDRENCB.
3037 TREEVIEW_Sort(TREEVIEW_INFO *infoPtr, HTREEITEM parent,
3041 PFNDPACOMPARE pfnCompare;
3044 /* undocumented feature: TVI_ROOT or NULL means `sort the whole tree' */
3045 if (parent == TVI_ROOT || parent == NULL)
3046 parent = infoPtr->root;
3048 /* Check for a valid handle to the parent item */
3049 if (!TREEVIEW_ValidItem(infoPtr, parent))
3051 ERR("invalid item hParent=%p\n", parent);
3057 pfnCompare = (PFNDPACOMPARE)TREEVIEW_CallBackCompare;
3058 lpCompare = (LPARAM)pSort;
3062 pfnCompare = (PFNDPACOMPARE)TREEVIEW_SortOnName;
3063 lpCompare = (LPARAM)infoPtr;
3066 cChildren = TREEVIEW_CountChildren(parent);
3068 /* Make sure there is something to sort */
3071 /* TREEVIEW_ITEM rechaining */
3074 HTREEITEM nextItem = 0;
3075 HTREEITEM prevItem = 0;
3077 HDPA sortList = TREEVIEW_BuildChildDPA(parent);
3079 if (sortList == NULL)
3082 /* let DPA sort the list */
3083 DPA_Sort(sortList, pfnCompare, lpCompare);
3085 /* The order of DPA entries has been changed, so fixup the
3086 * nextSibling and prevSibling pointers. */
3088 item = DPA_GetPtr(sortList, count++);
3089 while ((nextItem = DPA_GetPtr(sortList, count++)) != NULL)
3091 /* link the two current item together */
3092 item->nextSibling = nextItem;
3093 nextItem->prevSibling = item;
3095 if (prevItem == NULL)
3097 /* this is the first item, update the parent */
3098 parent->firstChild = item;
3099 item->prevSibling = NULL;
3103 /* fix the back chaining */
3104 item->prevSibling = prevItem;
3107 /* get ready for the next one */
3112 /* the last item is pointed to by item and never has a sibling */
3113 item->nextSibling = NULL;
3114 parent->lastChild = item;
3116 DPA_Destroy(sortList);
3118 TREEVIEW_VerifyTree(infoPtr);
3120 if (parent->state & TVIS_EXPANDED)
3122 int visOrder = infoPtr->firstVisible->visibleOrder;
3124 if (parent == infoPtr->root)
3125 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
3127 TREEVIEW_RecalculateVisibleOrder(infoPtr, parent);
3129 if (TREEVIEW_IsChildOf(parent, infoPtr->firstVisible))
3131 TREEVIEW_ITEM *item;
3133 for (item = infoPtr->root->firstChild; item != NULL;
3134 item = TREEVIEW_GetNextListItem(infoPtr, item))
3136 if (item->visibleOrder == visOrder)
3140 if (!item) item = parent->firstChild;
3141 TREEVIEW_SetFirstVisible(infoPtr, item, FALSE);
3144 TREEVIEW_Invalidate(infoPtr, NULL);
3153 /***************************************************************************
3154 * Setup the treeview structure with regards of the sort method
3155 * and sort the children of the TV item specified in lParam
3158 TREEVIEW_SortChildrenCB(TREEVIEW_INFO *infoPtr, LPTVSORTCB pSort)
3160 return TREEVIEW_Sort(infoPtr, pSort->hParent, pSort);
3164 /***************************************************************************
3165 * Sort the children of the TV item specified in lParam.
3168 TREEVIEW_SortChildren(TREEVIEW_INFO *infoPtr, LPARAM lParam)
3170 return TREEVIEW_Sort(infoPtr, (HTREEITEM)lParam, NULL);
3174 /* Expansion/Collapse ***************************************************/
3177 TREEVIEW_SendExpanding(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3180 return !TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDINGW, action,
3181 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3182 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3187 TREEVIEW_SendExpanded(const TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3190 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_ITEMEXPANDEDW, action,
3191 TVIF_HANDLE | TVIF_STATE | TVIF_PARAM
3192 | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
3197 /* This corresponds to TVM_EXPAND with TVE_COLLAPSE.
3198 * bRemoveChildren corresponds to TVE_COLLAPSERESET. */
3200 TREEVIEW_Collapse(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3201 BOOL bRemoveChildren, BOOL bUser)
3203 UINT action = TVE_COLLAPSE | (bRemoveChildren ? TVE_COLLAPSERESET : 0);
3204 BOOL bSetSelection, bSetFirstVisible;
3206 LONG scrollDist = 0;
3207 TREEVIEW_ITEM *nextItem = NULL, *tmpItem;
3209 TRACE("TVE_COLLAPSE %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3211 if (!(wineItem->state & TVIS_EXPANDED))
3214 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3215 TREEVIEW_SendExpanding(infoPtr, wineItem, action);
3217 if (wineItem->firstChild == NULL)
3220 wineItem->state &= ~TVIS_EXPANDED;
3222 if (bUser || !(wineItem->state & TVIS_EXPANDEDONCE))
3223 TREEVIEW_SendExpanded(infoPtr, wineItem, action);
3225 bSetSelection = (infoPtr->selectedItem != NULL
3226 && TREEVIEW_IsChildOf(wineItem, infoPtr->selectedItem));
3228 bSetFirstVisible = (infoPtr->firstVisible != NULL
3229 && TREEVIEW_IsChildOf(wineItem, infoPtr->firstVisible));
3234 if (tmpItem->nextSibling)
3236 nextItem = tmpItem->nextSibling;
3239 tmpItem = tmpItem->parent;
3243 scrollDist = nextItem->rect.top;
3245 if (bRemoveChildren)
3247 INT old_cChildren = wineItem->cChildren;
3248 TRACE("TVE_COLLAPSERESET\n");
3249 wineItem->state &= ~TVIS_EXPANDEDONCE;
3250 TREEVIEW_RemoveAllChildren(infoPtr, wineItem);
3251 wineItem->cChildren = old_cChildren;
3254 if (wineItem->firstChild)
3256 TREEVIEW_ITEM *item, *sibling;
3258 sibling = TREEVIEW_GetNextListItem(infoPtr, wineItem);
3260 for (item = wineItem->firstChild; item != sibling;
3261 item = TREEVIEW_GetNextListItem(infoPtr, item))
3263 item->visibleOrder = -1;
3267 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3270 scrollDist = -(scrollDist - nextItem->rect.top);
3274 /* Don't call DoSelectItem, it sends notifications. */
3275 if (TREEVIEW_ValidItem(infoPtr, infoPtr->selectedItem))
3276 infoPtr->selectedItem->state &= ~TVIS_SELECTED;
3277 wineItem->state |= TVIS_SELECTED;
3278 infoPtr->selectedItem = wineItem;
3281 TREEVIEW_UpdateScrollBars(infoPtr);
3283 scrollRect.left = 0;
3284 scrollRect.right = infoPtr->clientWidth;
3285 scrollRect.bottom = infoPtr->clientHeight;
3289 scrollRect.top = nextItem->rect.top;
3291 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, &scrollRect,
3292 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3293 TREEVIEW_Invalidate(infoPtr, wineItem);
3295 scrollRect.top = wineItem->rect.top;
3296 InvalidateRect(infoPtr->hwnd, &scrollRect, TRUE);
3299 TREEVIEW_SetFirstVisible(infoPtr,
3300 bSetFirstVisible ? wineItem : infoPtr->firstVisible,
3307 TREEVIEW_Expand(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem,
3308 BOOL partial, BOOL user)
3311 LONG orgNextTop = 0;
3313 TREEVIEW_ITEM *nextItem, *tmpItem;
3314 BOOL sendsNotifications;
3316 TRACE("(%p, %p, partial=%d, %d\n", infoPtr, wineItem, partial, user);
3318 if (wineItem->state & TVIS_EXPANDED)
3321 tmpItem = wineItem; nextItem = NULL;
3324 if (tmpItem->nextSibling)
3326 nextItem = tmpItem->nextSibling;
3329 tmpItem = tmpItem->parent;
3333 orgNextTop = nextItem->rect.top;
3335 TRACE("TVE_EXPAND %p %s\n", wineItem, TREEVIEW_ItemName(wineItem));
3337 sendsNotifications = user || ((wineItem->cChildren != 0) &&
3338 !(wineItem->state & TVIS_EXPANDEDONCE));
3339 if (sendsNotifications)
3341 if (!TREEVIEW_SendExpanding(infoPtr, wineItem, TVE_EXPAND))
3343 TRACE(" TVN_ITEMEXPANDING returned TRUE, exiting...\n");
3347 if (!wineItem->firstChild)
3350 wineItem->state |= TVIS_EXPANDED;
3353 FIXME("TVE_EXPANDPARTIAL not implemented\n");
3355 if (ISVISIBLE(wineItem))
3357 TREEVIEW_RecalculateVisibleOrder(infoPtr, wineItem);
3358 TREEVIEW_UpdateSubTree(infoPtr, wineItem);
3359 TREEVIEW_UpdateScrollBars(infoPtr);
3361 scrollRect.left = 0;
3362 scrollRect.bottom = infoPtr->treeHeight;
3363 scrollRect.right = infoPtr->clientWidth;
3366 scrollDist = nextItem->rect.top - orgNextTop;
3367 scrollRect.top = orgNextTop;
3369 ScrollWindowEx (infoPtr->hwnd, 0, scrollDist, &scrollRect, NULL,
3370 NULL, NULL, SW_ERASE | SW_INVALIDATE);
3371 TREEVIEW_Invalidate (infoPtr, wineItem);
3373 scrollRect.top = wineItem->rect.top;
3374 InvalidateRect(infoPtr->hwnd, &scrollRect, FALSE);
3377 /* Scroll up so that as many children as possible are visible.
3378 * This fails when expanding causes an HScroll bar to appear, but we
3379 * don't know that yet, so the last item is obscured. */
3380 if (wineItem->firstChild != NULL)
3382 int nChildren = wineItem->lastChild->visibleOrder
3383 - wineItem->firstChild->visibleOrder + 1;
3385 int visible_pos = wineItem->visibleOrder
3386 - infoPtr->firstVisible->visibleOrder;
3388 int rows_below = TREEVIEW_GetVisibleCount(infoPtr) - visible_pos - 1;
3390 if (visible_pos > 0 && nChildren > rows_below)
3392 int scroll = nChildren - rows_below;
3394 if (scroll > visible_pos)
3395 scroll = visible_pos;
3399 TREEVIEW_ITEM *newFirstVisible
3400 = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
3404 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
3410 if (sendsNotifications) {
3411 TREEVIEW_SendExpanded(infoPtr, wineItem, TVE_EXPAND);
3412 wineItem->state |= TVIS_EXPANDEDONCE;
3418 /* Handler for TVS_SINGLEEXPAND behaviour. Used on response
3419 to mouse messages and TVM_SELECTITEM.
3421 selection - previously selected item, used to collapse a part of a tree
3422 item - new selected item
3424 static void TREEVIEW_SingleExpand(TREEVIEW_INFO *infoPtr,
3425 HTREEITEM selection, HTREEITEM item)
3427 TREEVIEW_ITEM *SelItem;
3429 if ((infoPtr->dwStyle & TVS_SINGLEEXPAND) == 0 || infoPtr->hwndEdit) return;
3431 TREEVIEW_SendTreeviewNotify(infoPtr, TVN_SINGLEEXPAND, TVC_UNKNOWN, TVIF_HANDLE | TVIF_PARAM, item, 0);
3434 * Close the previous selection all the way to the root
3435 * as long as the new selection is not a child
3437 if(selection && (selection != item))
3439 BOOL closeit = TRUE;
3442 /* determine if the hitItem is a child of the currently selected item */
3443 while(closeit && SelItem && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3444 (SelItem->parent != infoPtr->root))
3446 closeit = (SelItem != selection);
3447 SelItem = SelItem->parent;
3452 if(TREEVIEW_ValidItem(infoPtr, selection))
3453 SelItem = selection;
3455 while(SelItem && (SelItem != item) && TREEVIEW_ValidItem(infoPtr, SelItem) &&
3456 SelItem->parent != infoPtr->root)
3458 TREEVIEW_Collapse(infoPtr, SelItem, FALSE, FALSE);
3459 SelItem = SelItem->parent;
3465 * Expand the current item
3467 TREEVIEW_Expand(infoPtr, item, FALSE, FALSE);
3471 TREEVIEW_Toggle(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem, BOOL bUser)
3475 if (wineItem->state & TVIS_EXPANDED)
3476 return TREEVIEW_Collapse(infoPtr, wineItem, FALSE, bUser);
3478 return TREEVIEW_Expand(infoPtr, wineItem, FALSE, bUser);
3482 TREEVIEW_ExpandAll(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
3484 TREEVIEW_Expand(infoPtr, item, FALSE, TRUE);
3486 for (item = item->firstChild; item != NULL; item = item->nextSibling)
3488 if (TREEVIEW_HasChildren(infoPtr, item))
3489 TREEVIEW_ExpandAll(infoPtr, item);
3493 /* Note:If the specified item is the child of a collapsed parent item,
3494 the parent's list of child items is (recursively) expanded to reveal the
3495 specified item. This is mentioned for TREEVIEW_SelectItem; don't
3496 know if it also applies here.
3500 TREEVIEW_ExpandMsg(TREEVIEW_INFO *infoPtr, UINT flag, HTREEITEM wineItem)
3502 if (!TREEVIEW_ValidItem(infoPtr, wineItem))
3505 TRACE("For (%s) item:%d, flags 0x%x, state:%d\n",
3506 TREEVIEW_ItemName(wineItem), TREEVIEW_GetItemIndex(infoPtr, wineItem),
3507 flag, wineItem->state);
3509 switch (flag & TVE_TOGGLE)
3512 return TREEVIEW_Collapse(infoPtr, wineItem, flag & TVE_COLLAPSERESET,
3516 return TREEVIEW_Expand(infoPtr, wineItem, flag & TVE_EXPANDPARTIAL,
3520 return TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
3527 /* Hit-Testing **********************************************************/
3529 static TREEVIEW_ITEM *
3530 TREEVIEW_HitTestPoint(const TREEVIEW_INFO *infoPtr, POINT pt)
3532 TREEVIEW_ITEM *wineItem;
3535 if (!infoPtr->firstVisible)
3538 row = pt.y / infoPtr->uItemHeight + infoPtr->firstVisible->visibleOrder;
3540 for (wineItem = infoPtr->firstVisible; wineItem != NULL;
3541 wineItem = TREEVIEW_GetNextListItem(infoPtr, wineItem))
3543 if (row >= wineItem->visibleOrder
3544 && row < wineItem->visibleOrder + wineItem->iIntegral)
3552 TREEVIEW_HitTest(const TREEVIEW_INFO *infoPtr, LPTVHITTESTINFO lpht)
3554 TREEVIEW_ITEM *wineItem;
3560 GetClientRect(infoPtr->hwnd, &rect);
3567 status |= TVHT_TOLEFT;
3569 else if (x > rect.right)
3571 status |= TVHT_TORIGHT;
3576 status |= TVHT_ABOVE;
3578 else if (y > rect.bottom)
3580 status |= TVHT_BELOW;
3585 lpht->flags = status;
3589 wineItem = TREEVIEW_HitTestPoint(infoPtr, lpht->pt);
3592 lpht->flags = TVHT_NOWHERE;
3596 if (x >= wineItem->textOffset + wineItem->textWidth)
3598 lpht->flags = TVHT_ONITEMRIGHT;
3600 else if (x >= wineItem->textOffset)
3602 lpht->flags = TVHT_ONITEMLABEL;
3604 else if (x >= wineItem->imageOffset)
3606 lpht->flags = TVHT_ONITEMICON;
3608 else if (x >= wineItem->stateOffset)
3610 lpht->flags = TVHT_ONITEMSTATEICON;
3612 else if (x >= wineItem->linesOffset && infoPtr->dwStyle & TVS_HASBUTTONS)
3614 lpht->flags = TVHT_ONITEMBUTTON;
3618 lpht->flags = TVHT_ONITEMINDENT;
3621 lpht->hItem = wineItem;
3622 TRACE("(%d,%d):result %x\n", lpht->pt.x, lpht->pt.y, lpht->flags);
3624 return (LRESULT)wineItem;
3627 /* Item Label Editing ***************************************************/
3630 TREEVIEW_GetEditControl(const TREEVIEW_INFO *infoPtr)
3632 return (LRESULT)infoPtr->hwndEdit;
3635 static LRESULT CALLBACK
3636 TREEVIEW_Edit_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3638 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(GetParent(hwnd));
3639 BOOL bCancel = FALSE;
3645 TRACE("WM_PAINT start\n");
3646 rc = CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam,
3648 TRACE("WM_PAINT done\n");
3652 if (infoPtr->bIgnoreEditKillFocus)
3658 WNDPROC editProc = infoPtr->wpEditOrig;
3659 infoPtr->wpEditOrig = 0;
3660 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
3661 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
3665 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
3668 if (wParam == VK_ESCAPE)
3673 else if (wParam == VK_RETURN)
3680 return CallWindowProcW(infoPtr->wpEditOrig, hwnd, uMsg, wParam, lParam);
3683 /* Processing TVN_ENDLABELEDIT message could kill the focus */
3684 /* eg. Using a messagebox */
3686 infoPtr->bIgnoreEditKillFocus = TRUE;
3687 TREEVIEW_EndEditLabelNow(infoPtr, bCancel || !infoPtr->bLabelChanged);
3688 infoPtr->bIgnoreEditKillFocus = FALSE;
3694 /* should handle edit control messages here */
3697 TREEVIEW_Command(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3699 TRACE("code=%x, id=%x, handle=%lx\n", HIWORD(wParam), LOWORD(wParam), lParam);
3701 switch (HIWORD(wParam))
3706 * Adjust the edit window size
3709 TREEVIEW_ITEM *editItem = infoPtr->editItem;
3710 HDC hdc = GetDC(infoPtr->hwndEdit);
3712 HFONT hFont, hOldFont = 0;
3714 TRACE("edit=%p\n", infoPtr->hwndEdit);
3716 if (!IsWindow(infoPtr->hwndEdit) || !hdc) return FALSE;
3718 infoPtr->bLabelChanged = TRUE;
3720 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
3722 /* Select font to get the right dimension of the string */
3723 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
3727 hOldFont = SelectObject(hdc, hFont);
3730 if (GetTextExtentPoint32W(hdc, buffer, strlenW(buffer), &sz))
3732 TEXTMETRICW textMetric;
3734 /* Add Extra spacing for the next character */
3735 GetTextMetricsW(hdc, &textMetric);
3736 sz.cx += (textMetric.tmMaxCharWidth * 2);
3738 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3740 infoPtr->clientWidth - editItem->textOffset + 2);
3742 SetWindowPos(infoPtr->hwndEdit,
3747 editItem->rect.bottom - editItem->rect.top + 3,
3748 SWP_NOMOVE | SWP_DRAWFRAME);
3753 SelectObject(hdc, hOldFont);
3756 ReleaseDC(infoPtr->hwnd, hdc);
3760 /* apparently we should respect passed handle value */
3761 if (infoPtr->hwndEdit != (HWND)lParam) return FALSE;
3763 TREEVIEW_EndEditLabelNow(infoPtr, FALSE);
3767 return SendMessageW(infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
3774 TREEVIEW_EditLabel(TREEVIEW_INFO *infoPtr, HTREEITEM hItem)
3776 HWND hwnd = infoPtr->hwnd;
3779 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
3782 TEXTMETRICW textMetric;
3784 TRACE("%p %p\n", hwnd, hItem);
3785 if (!TREEVIEW_ValidItem(infoPtr, hItem))
3788 if (infoPtr->hwndEdit)
3789 return infoPtr->hwndEdit;
3791 infoPtr->bLabelChanged = FALSE;
3793 /* make edit item visible */
3794 TREEVIEW_EnsureVisible(infoPtr, hItem, TRUE);
3796 TREEVIEW_UpdateDispInfo(infoPtr, hItem, TVIF_TEXT);
3799 /* Select the font to get appropriate metric dimensions */
3800 if (infoPtr->hFont != 0)
3802 hOldFont = SelectObject(hdc, infoPtr->hFont);
3805 /* Get string length in pixels */
3807 GetTextExtentPoint32W(hdc, hItem->pszText, strlenW(hItem->pszText),
3810 GetTextExtentPoint32A(hdc, "", 0, &sz);
3812 /* Add Extra spacing for the next character */
3813 GetTextMetricsW(hdc, &textMetric);
3814 sz.cx += (textMetric.tmMaxCharWidth * 2);
3816 sz.cx = max(sz.cx, textMetric.tmMaxCharWidth * 3);
3817 sz.cx = min(sz.cx, infoPtr->clientWidth - hItem->textOffset + 2);
3819 if (infoPtr->hFont != 0)
3821 SelectObject(hdc, hOldFont);
3824 ReleaseDC(hwnd, hdc);
3826 infoPtr->editItem = hItem;
3828 hwndEdit = CreateWindowExW(WS_EX_LEFT,
3831 WS_CHILD | WS_BORDER | ES_AUTOHSCROLL |
3832 WS_CLIPSIBLINGS | ES_WANTRETURN |
3833 ES_LEFT, hItem->textOffset - 2,
3834 hItem->rect.top - 1, sz.cx + 3,
3835 hItem->rect.bottom -
3836 hItem->rect.top + 3, hwnd, 0, hinst, 0);
3837 /* FIXME: (HMENU)IDTVEDIT,pcs->hInstance,0); */
3839 infoPtr->hwndEdit = hwndEdit;
3841 /* Get a 2D border. */
3842 SetWindowLongW(hwndEdit, GWL_EXSTYLE,
3843 GetWindowLongW(hwndEdit, GWL_EXSTYLE) & ~WS_EX_CLIENTEDGE);
3844 SetWindowLongW(hwndEdit, GWL_STYLE,
3845 GetWindowLongW(hwndEdit, GWL_STYLE) | WS_BORDER);
3847 SendMessageW(hwndEdit, WM_SETFONT,
3848 (WPARAM)TREEVIEW_FontForItem(infoPtr, hItem), FALSE);
3850 infoPtr->wpEditOrig = (WNDPROC)SetWindowLongPtrW(hwndEdit, GWLP_WNDPROC,
3852 TREEVIEW_Edit_SubclassProc);
3854 if (TREEVIEW_BeginLabelEditNotify(infoPtr, hItem))
3856 DestroyWindow(hwndEdit);
3857 infoPtr->hwndEdit = 0;
3858 infoPtr->editItem = NULL;
3863 SetWindowTextW(hwndEdit, hItem->pszText);
3866 SendMessageW(hwndEdit, EM_SETSEL, 0, -1);
3867 ShowWindow(hwndEdit, SW_SHOW);
3874 TREEVIEW_EndEditLabelNow(TREEVIEW_INFO *infoPtr, BOOL bCancel)
3876 HWND hwnd = infoPtr->hwnd;
3877 TREEVIEW_ITEM *editedItem = infoPtr->editItem;
3880 WCHAR tmpText[1024] = { '\0' };
3881 WCHAR *newText = tmpText;
3884 if (!IsWindow(infoPtr->hwndEdit)) return FALSE;
3886 tvdi.hdr.hwndFrom = hwnd;
3887 tvdi.hdr.idFrom = GetWindowLongPtrW(hwnd, GWLP_ID);
3888 tvdi.hdr.code = get_notifycode(infoPtr, TVN_ENDLABELEDITW);
3890 tvdi.item.hItem = editedItem;
3891 tvdi.item.state = editedItem->state;
3892 tvdi.item.lParam = editedItem->lParam;
3896 if (!infoPtr->bNtfUnicode)
3897 iLength = GetWindowTextA(infoPtr->hwndEdit, (LPSTR)tmpText, 1023);
3899 iLength = GetWindowTextW(infoPtr->hwndEdit, tmpText, 1023);
3901 if (iLength >= 1023)
3903 ERR("Insufficient space to retrieve new item label\n");
3906 tvdi.item.mask = TVIF_TEXT;
3907 tvdi.item.pszText = tmpText;
3908 tvdi.item.cchTextMax = iLength + 1;
3912 tvdi.item.pszText = NULL;
3913 tvdi.item.cchTextMax = 0;
3916 bCommit = TREEVIEW_SendRealNotify(infoPtr, tvdi.hdr.idFrom, (LPARAM)&tvdi);
3918 if (!bCancel && bCommit) /* Apply the changes */
3920 if (!infoPtr->bNtfUnicode)
3922 DWORD len = MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, NULL, 0 );
3923 newText = Alloc(len * sizeof(WCHAR));
3924 MultiByteToWideChar( CP_ACP, 0, (LPSTR)tmpText, -1, newText, len );
3928 if (strcmpW(newText, editedItem->pszText) != 0)
3930 WCHAR *ptr = ReAlloc(editedItem->pszText, sizeof(WCHAR)*(iLength + 1));
3933 ERR("OutOfMemory, cannot allocate space for label\n");
3934 if(newText != tmpText) Free(newText);
3935 DestroyWindow(infoPtr->hwndEdit);
3936 infoPtr->hwndEdit = 0;
3937 infoPtr->editItem = NULL;
3942 editedItem->pszText = ptr;
3943 editedItem->cchTextMax = iLength + 1;
3944 strcpyW(editedItem->pszText, newText);
3945 TREEVIEW_ComputeTextWidth(infoPtr, editedItem, 0);
3948 if(newText != tmpText) Free(newText);
3951 ShowWindow(infoPtr->hwndEdit, SW_HIDE);
3952 DestroyWindow(infoPtr->hwndEdit);
3953 infoPtr->hwndEdit = 0;
3954 infoPtr->editItem = NULL;
3959 TREEVIEW_HandleTimer(TREEVIEW_INFO *infoPtr, WPARAM wParam)
3961 if (wParam != TV_EDIT_TIMER)
3963 ERR("got unknown timer\n");
3967 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
3968 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
3970 TREEVIEW_EditLabel(infoPtr, infoPtr->selectedItem);
3976 /* Mouse Tracking/Drag **************************************************/
3978 /***************************************************************************
3979 * This is quite unusual piece of code, but that's how it's implemented in
3983 TREEVIEW_TrackMouse(const TREEVIEW_INFO *infoPtr, POINT pt)
3985 INT cxDrag = GetSystemMetrics(SM_CXDRAG);
3986 INT cyDrag = GetSystemMetrics(SM_CYDRAG);
3990 r.top = pt.y - cyDrag;
3991 r.left = pt.x - cxDrag;
3992 r.bottom = pt.y + cyDrag;
3993 r.right = pt.x + cxDrag;
3995 SetCapture(infoPtr->hwnd);
3999 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
4001 if (msg.message == WM_MOUSEMOVE)
4003 pt.x = (short)LOWORD(msg.lParam);
4004 pt.y = (short)HIWORD(msg.lParam);
4005 if (PtInRect(&r, pt))
4013 else if (msg.message >= WM_LBUTTONDOWN &&
4014 msg.message <= WM_RBUTTONDBLCLK)
4016 if (msg.message == WM_RBUTTONUP)
4017 TREEVIEW_RButtonUp(infoPtr, &pt);
4021 DispatchMessageW(&msg);
4024 if (GetCapture() != infoPtr->hwnd)
4034 TREEVIEW_LButtonDoubleClick(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4036 TREEVIEW_ITEM *wineItem;
4040 SetFocus(infoPtr->hwnd);
4042 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4044 /* If there is pending 'edit label' event - kill it now */
4045 KillTimer(infoPtr->hwnd, TV_EDIT_TIMER);
4048 hit.pt.x = (short)LOWORD(lParam);
4049 hit.pt.y = (short)HIWORD(lParam);
4051 wineItem = (TREEVIEW_ITEM *)TREEVIEW_HitTest(infoPtr, &hit);
4054 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, wineItem));
4056 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_DBLCLK) == FALSE)
4060 case TVHT_ONITEMRIGHT:
4061 /* FIXME: we should not have sent NM_DBLCLK in this case. */
4064 case TVHT_ONITEMINDENT:
4065 if (!(infoPtr->dwStyle & TVS_HASLINES))
4071 int level = hit.pt.x / infoPtr->uIndent;
4072 if (!(infoPtr->dwStyle & TVS_LINESATROOT)) level++;
4074 while (wineItem->iLevel > level)
4076 wineItem = wineItem->parent;
4082 case TVHT_ONITEMLABEL:
4083 case TVHT_ONITEMICON:
4084 case TVHT_ONITEMBUTTON:
4085 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4088 case TVHT_ONITEMSTATEICON:
4089 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4090 TREEVIEW_ToggleItemState(infoPtr, wineItem);
4092 TREEVIEW_Toggle(infoPtr, wineItem, TRUE);
4101 TREEVIEW_LButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4103 HWND hwnd = infoPtr->hwnd;
4105 BOOL bTrack, bDoLabelEdit;
4107 /* If Edit control is active - kill it and return.
4108 * The best way to do it is to set focus to itself.
4109 * Edit control subclassed procedure will automatically call
4112 if (infoPtr->hwndEdit)
4118 ht.pt.x = (short)LOWORD(lParam);
4119 ht.pt.y = (short)HIWORD(lParam);
4121 TREEVIEW_HitTest(infoPtr, &ht);
4122 TRACE("item %d\n", TREEVIEW_GetItemIndex(infoPtr, ht.hItem));
4124 /* update focusedItem and redraw both items */
4125 if(ht.hItem && (ht.flags & TVHT_ONITEM))
4127 infoPtr->focusedItem = ht.hItem;
4128 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4129 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4132 bTrack = (ht.flags & TVHT_ONITEM)
4133 && !(infoPtr->dwStyle & TVS_DISABLEDRAGDROP);
4136 * If the style allows editing and the node is already selected
4137 * and the click occurred on the item label...
4139 bDoLabelEdit = (infoPtr->dwStyle & TVS_EDITLABELS) &&
4140 (ht.flags & TVHT_ONITEMLABEL) && (infoPtr->selectedItem == ht.hItem);
4142 /* Send NM_CLICK right away */
4144 if (TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4147 if (ht.flags & TVHT_ONITEMBUTTON)
4149 TREEVIEW_Toggle(infoPtr, ht.hItem, TRUE);
4153 { /* if TREEVIEW_TrackMouse == 1 dragging occurred and the cursor left the dragged item's rectangle */
4154 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4156 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINDRAGW, ht.hItem, ht.pt);
4157 infoPtr->dropItem = ht.hItem;
4159 /* clean up focusedItem as we dragged and won't select this item */
4160 if(infoPtr->focusedItem)
4162 /* refresh the item that was focused */
4163 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4164 infoPtr->focusedItem = NULL;
4166 /* refresh the selected item to return the filled background */
4167 TREEVIEW_InvalidateItem(infoPtr, infoPtr->selectedItem);
4174 if (bTrack && TREEVIEW_SendSimpleNotify(infoPtr, NM_CLICK))
4179 if (infoPtr->Timer & TV_EDIT_TIMER_SET)
4180 KillTimer(hwnd, TV_EDIT_TIMER);
4182 SetTimer(hwnd, TV_EDIT_TIMER, GetDoubleClickTime(), 0);
4183 infoPtr->Timer |= TV_EDIT_TIMER_SET;
4185 else if (ht.flags & (TVHT_ONITEMICON|TVHT_ONITEMLABEL)) /* select the item if the hit was inside of the icon or text */
4187 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4189 /* Select the current item */
4190 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, ht.hItem, TVC_BYMOUSE);
4191 TREEVIEW_SingleExpand(infoPtr, selection, ht.hItem);
4193 else if (ht.flags & TVHT_ONITEMSTATEICON)
4195 /* TVS_CHECKBOXES requires us to toggle the current state */
4196 if (infoPtr->dwStyle & TVS_CHECKBOXES)
4197 TREEVIEW_ToggleItemState(infoPtr, ht.hItem);
4207 TREEVIEW_RButtonDown(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4211 if (infoPtr->hwndEdit)
4213 SetFocus(infoPtr->hwnd);
4217 ht.pt.x = (short)LOWORD(lParam);
4218 ht.pt.y = (short)HIWORD(lParam);
4220 TREEVIEW_HitTest(infoPtr, &ht);
4222 if (TREEVIEW_TrackMouse(infoPtr, ht.pt))
4226 TREEVIEW_SendTreeviewDnDNotify(infoPtr, TVN_BEGINRDRAGW, ht.hItem, ht.pt);
4227 infoPtr->dropItem = ht.hItem;
4232 SetFocus(infoPtr->hwnd);
4233 TREEVIEW_SendSimpleNotify(infoPtr, NM_RCLICK);
4240 TREEVIEW_RButtonUp(const TREEVIEW_INFO *infoPtr, const POINT *pPt)
4246 TREEVIEW_HitTest(infoPtr, &ht);
4250 /* Change to screen coordinate for WM_CONTEXTMENU */
4251 ClientToScreen(infoPtr->hwnd, &ht.pt);
4253 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
4254 SendMessageW(infoPtr->hwnd, WM_CONTEXTMENU,
4255 (WPARAM)infoPtr->hwnd, MAKELPARAM(ht.pt.x, ht.pt.y));
4262 TREEVIEW_CreateDragImage(TREEVIEW_INFO *infoPtr, LPARAM lParam)
4264 TREEVIEW_ITEM *dragItem = (HTREEITEM)lParam;
4268 HBITMAP hbmp, hOldbmp;
4275 if (!(infoPtr->himlNormal))
4278 if (!dragItem || !TREEVIEW_ValidItem(infoPtr, dragItem))
4281 TREEVIEW_UpdateDispInfo(infoPtr, dragItem, TVIF_TEXT);
4283 hwtop = GetDesktopWindow();
4284 htopdc = GetDC(hwtop);
4285 hdc = CreateCompatibleDC(htopdc);
4287 hOldFont = SelectObject(hdc, infoPtr->hFont);
4289 if (dragItem->pszText)
4290 GetTextExtentPoint32W(hdc, dragItem->pszText, strlenW(dragItem->pszText),
4293 GetTextExtentPoint32A(hdc, "", 0, &size);
4295 TRACE("%d %d %s\n", size.cx, size.cy, debugstr_w(dragItem->pszText));
4296 hbmp = CreateCompatibleBitmap(htopdc, size.cx, size.cy);
4297 hOldbmp = SelectObject(hdc, hbmp);
4299 ImageList_GetIconSize(infoPtr->himlNormal, &cx, &cy);
4304 infoPtr->dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4305 ImageList_Draw(infoPtr->himlNormal, dragItem->iImage, hdc, 0, 0,
4309 ImageList_GetImageInfo (infoPtr->himlNormal, dragItem->hItem, &iminfo);
4310 ImageList_AddMasked (infoPtr->dragList, iminfo.hbmImage, CLR_DEFAULT);
4313 /* draw item text */
4315 SetRect(&rc, cx, 0, size.cx, size.cy);
4317 if (dragItem->pszText)
4318 DrawTextW(hdc, dragItem->pszText, strlenW(dragItem->pszText), &rc,
4321 SelectObject(hdc, hOldFont);
4322 SelectObject(hdc, hOldbmp);
4324 ImageList_Add(infoPtr->dragList, hbmp, 0);
4328 ReleaseDC(hwtop, htopdc);
4330 return (LRESULT)infoPtr->dragList;
4333 /* Selection ************************************************************/
4336 TREEVIEW_DoSelectItem(TREEVIEW_INFO *infoPtr, INT action, HTREEITEM newSelect,
4339 TREEVIEW_ITEM *prevSelect;
4341 assert(newSelect == NULL || TREEVIEW_ValidItem(infoPtr, newSelect));
4343 TRACE("Entering item %p (%s), flag %x, cause %x, state %d\n",
4344 newSelect, TREEVIEW_ItemName(newSelect), action, cause,
4345 newSelect ? newSelect->state : 0);
4347 /* reset and redraw focusedItem if focusedItem was set so we don't */
4348 /* have to worry about the previously focused item when we set a new one */
4349 TREEVIEW_InvalidateItem(infoPtr, infoPtr->focusedItem);
4350 infoPtr->focusedItem = NULL;
4355 prevSelect = infoPtr->selectedItem;
4357 if (prevSelect == newSelect) {
4358 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4362 if (TREEVIEW_SendTreeviewNotify(infoPtr,
4365 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4371 prevSelect->state &= ~TVIS_SELECTED;
4373 newSelect->state |= TVIS_SELECTED;
4375 infoPtr->selectedItem = newSelect;
4377 TREEVIEW_EnsureVisible(infoPtr, infoPtr->selectedItem, FALSE);
4379 TREEVIEW_InvalidateItem(infoPtr, prevSelect);
4380 TREEVIEW_InvalidateItem(infoPtr, newSelect);
4382 TREEVIEW_SendTreeviewNotify(infoPtr,
4385 TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM,
4390 case TVGN_DROPHILITE:
4391 prevSelect = infoPtr->dropItem;
4394 prevSelect->state &= ~TVIS_DROPHILITED;
4396 infoPtr->dropItem = newSelect;
4399 newSelect->state |= TVIS_DROPHILITED;
4401 TREEVIEW_Invalidate(infoPtr, prevSelect);
4402 TREEVIEW_Invalidate(infoPtr, newSelect);
4405 case TVGN_FIRSTVISIBLE:
4406 if (newSelect != NULL)
4408 TREEVIEW_EnsureVisible(infoPtr, newSelect, FALSE);
4409 TREEVIEW_SetFirstVisible(infoPtr, newSelect, TRUE);
4410 TREEVIEW_Invalidate(infoPtr, NULL);
4415 TRACE("Leaving state %d\n", newSelect ? newSelect->state : 0);
4419 /* FIXME: handle NM_KILLFOCUS etc */
4421 TREEVIEW_SelectItem(TREEVIEW_INFO *infoPtr, INT wParam, HTREEITEM item)
4423 TREEVIEW_ITEM *selection = infoPtr->selectedItem;
4425 if (item && !TREEVIEW_ValidItem(infoPtr, item))
4428 TRACE("%p (%s) %d\n", item, TREEVIEW_ItemName(item), wParam);
4430 if (!TREEVIEW_DoSelectItem(infoPtr, wParam, item, TVC_UNKNOWN))
4433 TREEVIEW_SingleExpand(infoPtr, selection, item);
4438 /*************************************************************************
4439 * TREEVIEW_ProcessLetterKeys
4441 * Processes keyboard messages generated by pressing the letter keys
4443 * What this does is perform a case insensitive search from the
4444 * current position with the following quirks:
4445 * - If two chars or more are pressed in quick succession we search
4446 * for the corresponding string (e.g. 'abc').
4447 * - If there is a delay we wipe away the current search string and
4448 * restart with just that char.
4449 * - If the user keeps pressing the same character, whether slowly or
4450 * fast, so that the search string is entirely composed of this
4451 * character ('aaaaa' for instance), then we search for first item
4452 * that starting with that character.
4453 * - If the user types the above character in quick succession, then
4454 * we must also search for the corresponding string ('aaaaa'), and
4455 * go to that string if there is a match.
4463 * - The current implementation has a list of characters it will
4464 * accept and it ignores everything else. In particular it will
4465 * ignore accentuated characters which seems to match what
4466 * Windows does. But I'm not sure it makes sense to follow
4468 * - We don't sound a beep when the search fails.
4469 * - The search should start from the focused item, not from the selected
4470 * item. One reason for this is to allow for multiple selections in trees.
4471 * But currently infoPtr->focusedItem does not seem very usable.
4475 * TREEVIEW_ProcessLetterKeys
4477 static INT TREEVIEW_ProcessLetterKeys(TREEVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
4480 HTREEITEM endidx,idx;
4482 WCHAR buffer[MAX_PATH];
4483 DWORD timestamp,elapsed;
4485 /* simple parameter checking */
4486 if (!charCode || !keyData) return 0;
4488 /* only allow the valid WM_CHARs through */
4489 if (!isalnum(charCode) &&
4490 charCode != '.' && charCode != '`' && charCode != '!' &&
4491 charCode != '@' && charCode != '#' && charCode != '$' &&
4492 charCode != '%' && charCode != '^' && charCode != '&' &&
4493 charCode != '*' && charCode != '(' && charCode != ')' &&
4494 charCode != '-' && charCode != '_' && charCode != '+' &&
4495 charCode != '=' && charCode != '\\'&& charCode != ']' &&
4496 charCode != '}' && charCode != '[' && charCode != '{' &&
4497 charCode != '/' && charCode != '?' && charCode != '>' &&
4498 charCode != '<' && charCode != ',' && charCode != '~')
4501 /* compute how much time elapsed since last keypress */
4502 timestamp = GetTickCount();
4503 if (timestamp > infoPtr->lastKeyPressTimestamp) {
4504 elapsed=timestamp-infoPtr->lastKeyPressTimestamp;
4506 elapsed=infoPtr->lastKeyPressTimestamp-timestamp;
4509 /* update the search parameters */
4510 infoPtr->lastKeyPressTimestamp=timestamp;
4511 if (elapsed < KEY_DELAY) {
4512 if (infoPtr->nSearchParamLength < sizeof(infoPtr->szSearchParam) / sizeof(WCHAR)) {
4513 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
4515 if (infoPtr->charCode != charCode) {
4516 infoPtr->charCode=charCode=0;
4519 infoPtr->charCode=charCode;
4520 infoPtr->szSearchParam[0]=charCode;
4521 infoPtr->nSearchParamLength=1;
4522 /* Redundant with the 1 char string */
4526 /* and search from the current position */
4528 if (infoPtr->selectedItem != NULL) {
4529 endidx=infoPtr->selectedItem;
4530 /* if looking for single character match,
4531 * then we must always move forward
4533 if (infoPtr->nSearchParamLength == 1)
4534 idx=TREEVIEW_GetNextListItem(infoPtr,endidx);
4539 idx=infoPtr->root->firstChild;
4542 /* At the end point, sort out wrapping */
4545 /* If endidx is null, stop at the last item (ie top to bottom) */
4549 /* Otherwise, start again at the very beginning */
4550 idx=infoPtr->root->firstChild;
4552 /* But if we are stopping on the first child, end now! */
4553 if (idx == endidx) break;
4557 ZeroMemory(&item, sizeof(item));
4558 item.mask = TVIF_TEXT;
4560 item.pszText = buffer;
4561 item.cchTextMax = sizeof(buffer);
4562 TREEVIEW_GetItemT( infoPtr, &item, TRUE );
4564 /* check for a match */
4565 if (strncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
4568 } else if ( (charCode != 0) && (nItem == NULL) &&
4569 (nItem != infoPtr->selectedItem) &&
4570 (strncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
4571 /* This would work but we must keep looking for a longer match */
4574 idx=TREEVIEW_GetNextListItem(infoPtr,idx);
4575 } while (idx != endidx);
4577 if (nItem != NULL) {
4578 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, nItem, TVC_BYKEYBOARD)) {
4579 TREEVIEW_EnsureVisible(infoPtr, nItem, FALSE);
4586 /* Scrolling ************************************************************/
4589 TREEVIEW_EnsureVisible(TREEVIEW_INFO *infoPtr, HTREEITEM item, BOOL bHScroll)
4592 BOOL hasFirstVisible = infoPtr->firstVisible != NULL;
4593 HTREEITEM newFirstVisible = NULL;
4594 int visible_pos = -1;
4596 if (!TREEVIEW_ValidItem(infoPtr, item))
4599 if (!ISVISIBLE(item))
4601 /* Expand parents as necessary. */
4604 /* see if we are trying to ensure that root is visible */
4605 if((item != infoPtr->root) && TREEVIEW_ValidItem(infoPtr, item))
4606 parent = item->parent;
4608 parent = item; /* this item is the topmost item */
4610 while (parent != infoPtr->root)
4612 if (!(parent->state & TVIS_EXPANDED))
4613 TREEVIEW_Expand(infoPtr, parent, FALSE, FALSE);
4615 parent = parent->parent;
4619 viscount = TREEVIEW_GetVisibleCount(infoPtr);
4621 TRACE("%p (%s) %d - %d viscount(%d)\n", item, TREEVIEW_ItemName(item), item->visibleOrder,
4622 hasFirstVisible ? infoPtr->firstVisible->visibleOrder : -1, viscount);
4624 if (hasFirstVisible)
4625 visible_pos = item->visibleOrder - infoPtr->firstVisible->visibleOrder;
4627 if (visible_pos < 0)
4629 /* item is before the start of the list: put it at the top. */
4630 newFirstVisible = item;
4632 else if (visible_pos >= viscount
4633 /* Sometimes, before we are displayed, GVC is 0, causing us to
4634 * spuriously scroll up. */
4635 && visible_pos > 0 && !(infoPtr->dwStyle & TVS_NOSCROLL) )
4637 /* item is past the end of the list. */
4638 int scroll = visible_pos - viscount;
4640 newFirstVisible = TREEVIEW_GetListItem(infoPtr, infoPtr->firstVisible,
4646 /* Scroll window so item's text is visible as much as possible */
4647 /* Calculation of amount of extra space is taken from EditLabel code */
4649 TEXTMETRICW textMetric;
4650 HDC hdc = GetWindowDC(infoPtr->hwnd);
4652 x = item->textWidth;
4654 GetTextMetricsW(hdc, &textMetric);
4655 ReleaseDC(infoPtr->hwnd, hdc);
4657 x += (textMetric.tmMaxCharWidth * 2);
4658 x = max(x, textMetric.tmMaxCharWidth * 3);
4660 if (item->textOffset < 0)
4661 pos = item->textOffset;
4662 else if (item->textOffset + x > infoPtr->clientWidth)
4664 if (x > infoPtr->clientWidth)
4665 pos = item->textOffset;
4667 pos = item->textOffset + x - infoPtr->clientWidth;
4672 TREEVIEW_HScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, infoPtr->scrollX + pos));
4675 if (newFirstVisible != NULL && newFirstVisible != infoPtr->firstVisible)
4677 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible, TRUE);
4686 TREEVIEW_SetFirstVisible(TREEVIEW_INFO *infoPtr,
4687 TREEVIEW_ITEM *newFirstVisible,
4688 BOOL bUpdateScrollPos)
4692 TRACE("%p: %s\n", newFirstVisible, TREEVIEW_ItemName(newFirstVisible));
4694 if (newFirstVisible != NULL)
4696 /* Prevent an empty gap from appearing at the bottom... */
4697 gap_size = TREEVIEW_GetVisibleCount(infoPtr)
4698 - infoPtr->maxVisibleOrder + newFirstVisible->visibleOrder;
4702 newFirstVisible = TREEVIEW_GetListItem(infoPtr, newFirstVisible,
4705 /* ... unless we just don't have enough items. */
4706 if (newFirstVisible == NULL)
4707 newFirstVisible = infoPtr->root->firstChild;
4711 if (infoPtr->firstVisible != newFirstVisible)
4713 if (infoPtr->firstVisible == NULL || newFirstVisible == NULL)
4715 infoPtr->firstVisible = newFirstVisible;
4716 TREEVIEW_Invalidate(infoPtr, NULL);
4720 TREEVIEW_ITEM *item;
4721 int scroll = infoPtr->uItemHeight *
4722 (infoPtr->firstVisible->visibleOrder
4723 - newFirstVisible->visibleOrder);
4725 infoPtr->firstVisible = newFirstVisible;
4727 for (item = infoPtr->root->firstChild; item != NULL;
4728 item = TREEVIEW_GetNextListItem(infoPtr, item))
4730 item->rect.top += scroll;
4731 item->rect.bottom += scroll;
4734 if (bUpdateScrollPos)
4735 SetScrollPos(infoPtr->hwnd, SB_VERT,
4736 newFirstVisible->visibleOrder, TRUE);
4738 ScrollWindowEx(infoPtr->hwnd, 0, scroll, NULL, NULL, NULL, NULL, SW_ERASE | SW_INVALIDATE);
4743 /************************************************************************
4744 * VScroll is always in units of visible items. i.e. we always have a
4745 * visible item aligned to the top of the control. (Unless we have no
4749 TREEVIEW_VScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4751 TREEVIEW_ITEM *oldFirstVisible = infoPtr->firstVisible;
4752 TREEVIEW_ITEM *newFirstVisible = NULL;
4754 int nScrollCode = LOWORD(wParam);
4756 TRACE("wp %lx\n", wParam);
4758 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
4761 if (!oldFirstVisible)
4763 assert(infoPtr->root->firstChild == NULL);
4767 switch (nScrollCode)
4770 newFirstVisible = infoPtr->root->firstChild;
4774 newFirstVisible = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
4778 newFirstVisible = TREEVIEW_GetPrevListItem(infoPtr, oldFirstVisible);
4782 newFirstVisible = TREEVIEW_GetNextListItem(infoPtr, oldFirstVisible);
4786 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4787 -max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4791 newFirstVisible = TREEVIEW_GetListItem(infoPtr, oldFirstVisible,
4792 max(1, TREEVIEW_GetVisibleCount(infoPtr)));
4796 case SB_THUMBPOSITION:
4797 newFirstVisible = TREEVIEW_GetListItem(infoPtr,
4798 infoPtr->root->firstChild,
4799 (LONG)(SHORT)HIWORD(wParam));
4806 if (newFirstVisible != NULL)
4808 if (newFirstVisible != oldFirstVisible)
4809 TREEVIEW_SetFirstVisible(infoPtr, newFirstVisible,
4810 nScrollCode != SB_THUMBTRACK);
4811 else if (nScrollCode == SB_THUMBPOSITION)
4812 SetScrollPos(infoPtr->hwnd, SB_VERT,
4813 newFirstVisible->visibleOrder, TRUE);
4820 TREEVIEW_HScroll(TREEVIEW_INFO *infoPtr, WPARAM wParam)
4823 int scrollX = infoPtr->scrollX;
4824 int nScrollCode = LOWORD(wParam);
4826 TRACE("wp %lx\n", wParam);
4828 if (!(infoPtr->uInternalStatus & TV_HSCROLL))
4831 maxWidth = infoPtr->treeWidth - infoPtr->clientWidth;
4832 /* shall never occur */
4839 switch (nScrollCode)
4842 scrollX -= infoPtr->uItemHeight;
4845 scrollX += infoPtr->uItemHeight;
4848 scrollX -= infoPtr->clientWidth;
4851 scrollX += infoPtr->clientWidth;
4855 case SB_THUMBPOSITION:
4856 scrollX = (int)(SHORT)HIWORD(wParam);
4863 if (scrollX > maxWidth)
4865 else if (scrollX < 0)
4869 if (scrollX != infoPtr->scrollX)
4871 TREEVIEW_ITEM *item;
4872 LONG scroll_pixels = infoPtr->scrollX - scrollX;
4874 for (item = infoPtr->root->firstChild; item != NULL;
4875 item = TREEVIEW_GetNextListItem(infoPtr, item))
4877 item->linesOffset += scroll_pixels;
4878 item->stateOffset += scroll_pixels;
4879 item->imageOffset += scroll_pixels;
4880 item->textOffset += scroll_pixels;
4883 ScrollWindow(infoPtr->hwnd, scroll_pixels, 0, NULL, NULL);
4884 infoPtr->scrollX = scrollX;
4885 UpdateWindow(infoPtr->hwnd);
4888 if (nScrollCode != SB_THUMBTRACK)
4889 SetScrollPos(infoPtr->hwnd, SB_HORZ, scrollX, TRUE);
4895 TREEVIEW_MouseWheel(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
4898 UINT pulScrollLines = 3;
4900 if (wParam & (MK_SHIFT | MK_CONTROL))
4901 return DefWindowProcW(infoPtr->hwnd, WM_MOUSEWHEEL, wParam, lParam);
4903 if (infoPtr->firstVisible == NULL)
4906 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &pulScrollLines, 0);
4908 gcWheelDelta = -(short)HIWORD(wParam);
4909 pulScrollLines *= (gcWheelDelta / WHEEL_DELTA);
4911 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4913 int newDy = infoPtr->firstVisible->visibleOrder + pulScrollLines;
4914 int maxDy = infoPtr->maxVisibleOrder;
4922 TREEVIEW_VScroll(infoPtr, MAKEWPARAM(SB_THUMBPOSITION, newDy));
4927 /* Create/Destroy *******************************************************/
4930 initialize_checkboxes(TREEVIEW_INFO *infoPtr)
4933 HBITMAP hbm, hbmOld;
4937 infoPtr->himlState = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 3, 0);
4939 hdcScreen = GetDC(0);
4941 hdc = CreateCompatibleDC(hdcScreen);
4942 hbm = CreateCompatibleBitmap(hdcScreen, 48, 16);
4943 hbmOld = SelectObject(hdc, hbm);
4945 SetRect(&rc, 0, 0, 48, 16);
4946 FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
4948 SetRect(&rc, 18, 2, 30, 14);
4949 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4950 DFCS_BUTTONCHECK|DFCS_FLAT);
4952 SetRect(&rc, 34, 2, 46, 14);
4953 DrawFrameControl(hdc, &rc, DFC_BUTTON,
4954 DFCS_BUTTONCHECK|DFCS_FLAT|DFCS_CHECKED);
4956 SelectObject(hdc, hbmOld);
4957 nIndex = ImageList_AddMasked(infoPtr->himlState, hbm,
4958 comctl32_color.clrWindow);
4959 TRACE("checkbox index %d\n", nIndex);
4963 ReleaseDC(0, hdcScreen);
4965 infoPtr->stateImageWidth = 16;
4966 infoPtr->stateImageHeight = 16;
4970 TREEVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
4973 TREEVIEW_INFO *infoPtr;
4976 TRACE("wnd %p, style %x\n", hwnd, GetWindowLongW(hwnd, GWL_STYLE));
4978 infoPtr = Alloc(sizeof(TREEVIEW_INFO));
4980 if (infoPtr == NULL)
4982 ERR("could not allocate info memory!\n");
4986 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
4988 infoPtr->hwnd = hwnd;
4989 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
4991 infoPtr->uNumItems = 0;
4992 infoPtr->cdmode = 0;
4993 infoPtr->uScrollTime = 300; /* milliseconds */
4994 infoPtr->bRedraw = TRUE;
4996 GetClientRect(hwnd, &rcClient);
4998 /* No scroll bars yet. */
4999 infoPtr->clientWidth = rcClient.right;
5000 infoPtr->clientHeight = rcClient.bottom;
5001 infoPtr->uInternalStatus = 0;
5003 infoPtr->treeWidth = 0;
5004 infoPtr->treeHeight = 0;
5006 infoPtr->uIndent = MINIMUM_INDENT;
5007 infoPtr->selectedItem = NULL;
5008 infoPtr->focusedItem = NULL;
5009 infoPtr->hotItem = NULL;
5010 infoPtr->editItem = NULL;
5011 infoPtr->firstVisible = NULL;
5012 infoPtr->maxVisibleOrder = 0;
5013 infoPtr->dropItem = NULL;
5014 infoPtr->insertMarkItem = NULL;
5015 infoPtr->insertBeforeorAfter = 0;
5018 infoPtr->scrollX = 0;
5020 infoPtr->clrBk = CLR_NONE; /* use system color */
5021 infoPtr->clrText = CLR_NONE; /* use system color */
5022 infoPtr->clrLine = CLR_DEFAULT;
5023 infoPtr->clrInsertMark = CLR_DEFAULT;
5027 infoPtr->hwndEdit = NULL;
5028 infoPtr->wpEditOrig = NULL;
5029 infoPtr->bIgnoreEditKillFocus = FALSE;
5030 infoPtr->bLabelChanged = FALSE;
5032 infoPtr->himlNormal = NULL;
5033 infoPtr->himlState = NULL;
5034 infoPtr->normalImageWidth = 0;
5035 infoPtr->normalImageHeight = 0;
5036 infoPtr->stateImageWidth = 0;
5037 infoPtr->stateImageHeight = 0;
5039 infoPtr->items = DPA_Create(16);
5041 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
5042 infoPtr->hFont = infoPtr->hDefaultFont = CreateFontIndirectW(&lf);
5043 infoPtr->hBoldFont = TREEVIEW_CreateBoldFont(infoPtr->hFont);
5044 infoPtr->hUnderlineFont = TREEVIEW_CreateUnderlineFont(infoPtr->hFont);
5045 infoPtr->hcurHand = LoadCursorW(NULL, (LPWSTR)IDC_HAND);
5047 infoPtr->uItemHeight = TREEVIEW_NaturalHeight(infoPtr);
5049 infoPtr->root = TREEVIEW_AllocateItem(infoPtr);
5050 infoPtr->root->state = TVIS_EXPANDED;
5051 infoPtr->root->iLevel = -1;
5052 infoPtr->root->visibleOrder = -1;
5054 infoPtr->hwndNotify = lpcs->hwndParent;
5055 infoPtr->hwndToolTip = 0;
5057 infoPtr->bNtfUnicode = IsWindowUnicode (hwnd);
5059 /* Determine what type of notify should be issued */
5060 /* sets infoPtr->bNtfUnicode */
5061 TREEVIEW_NotifyFormat(infoPtr, infoPtr->hwndNotify, NF_REQUERY);
5063 if (!(infoPtr->dwStyle & TVS_NOTOOLTIPS))
5064 infoPtr->hwndToolTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
5065 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
5068 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5069 initialize_checkboxes(infoPtr);
5071 /* Make sure actual scrollbar state is consistent with uInternalStatus */
5072 ShowScrollBar(hwnd, SB_VERT, FALSE);
5073 ShowScrollBar(hwnd, SB_HORZ, FALSE);
5075 OpenThemeData (hwnd, themeClass);
5082 TREEVIEW_Destroy(TREEVIEW_INFO *infoPtr)
5086 /* free item data */
5087 TREEVIEW_RemoveTree(infoPtr);
5088 /* root isn't freed with other items */
5089 TREEVIEW_FreeItem(infoPtr, infoPtr->root);
5090 DPA_Destroy(infoPtr->items);
5092 /* tool tip is automatically destroyed: we are its owner */
5094 /* Restore original wndproc */
5095 if (infoPtr->hwndEdit)
5096 SetWindowLongPtrW(infoPtr->hwndEdit, GWLP_WNDPROC,
5097 (DWORD_PTR)infoPtr->wpEditOrig);
5099 CloseThemeData (GetWindowTheme (infoPtr->hwnd));
5101 /* Deassociate treeview from the window before doing anything drastic. */
5102 SetWindowLongPtrW(infoPtr->hwnd, 0, 0);
5105 DeleteObject(infoPtr->hDefaultFont);
5106 DeleteObject(infoPtr->hBoldFont);
5107 DeleteObject(infoPtr->hUnderlineFont);
5113 /* Miscellaneous Messages ***********************************************/
5116 TREEVIEW_ScrollKeyDown(TREEVIEW_INFO *infoPtr, WPARAM key)
5124 #define SCROLL_ENTRY(dir, code) { ((dir) << 7) | (code) }
5125 SCROLL_ENTRY(SB_VERT, SB_PAGEUP), /* VK_PRIOR */
5126 SCROLL_ENTRY(SB_VERT, SB_PAGEDOWN), /* VK_NEXT */
5127 SCROLL_ENTRY(SB_VERT, SB_BOTTOM), /* VK_END */
5128 SCROLL_ENTRY(SB_VERT, SB_TOP), /* VK_HOME */
5129 SCROLL_ENTRY(SB_HORZ, SB_LINEUP), /* VK_LEFT */
5130 SCROLL_ENTRY(SB_VERT, SB_LINEUP), /* VK_UP */
5131 SCROLL_ENTRY(SB_HORZ, SB_LINEDOWN), /* VK_RIGHT */
5132 SCROLL_ENTRY(SB_VERT, SB_LINEDOWN) /* VK_DOWN */
5136 if (key >= VK_PRIOR && key <= VK_DOWN)
5138 unsigned char code = scroll[key - VK_PRIOR].code;
5140 (((code & (1 << 7)) == (SB_HORZ << 7))
5142 : TREEVIEW_VScroll)(infoPtr, code & 0x7F);
5148 /************************************************************************
5151 * VK_UP Move selection to the previous non-hidden item.
5152 * VK_DOWN Move selection to the next non-hidden item.
5153 * VK_HOME Move selection to the first item.
5154 * VK_END Move selection to the last item.
5155 * VK_LEFT If expanded then collapse, otherwise move to parent.
5156 * VK_RIGHT If collapsed then expand, otherwise move to first child.
5158 * VK_SUBTRACT Collapse.
5159 * VK_MULTIPLY Expand all.
5160 * VK_PRIOR Move up GetVisibleCount items.
5161 * VK_NEXT Move down GetVisibleCount items.
5162 * VK_BACK Move to parent.
5163 * CTRL-Left,Right,Up,Down,PgUp,PgDown,Home,End: Scroll without changing selection
5166 TREEVIEW_KeyDown(TREEVIEW_INFO *infoPtr, WPARAM wParam)
5168 /* If it is non-NULL and different, it will be selected and visible. */
5169 TREEVIEW_ITEM *newSelection = NULL;
5171 TREEVIEW_ITEM *prevItem = infoPtr->selectedItem;
5173 TRACE("%lx\n", wParam);
5175 if (prevItem == NULL)
5178 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
5179 return TREEVIEW_ScrollKeyDown(infoPtr, wParam);
5184 newSelection = TREEVIEW_GetPrevListItem(infoPtr, prevItem);
5186 newSelection = infoPtr->root->firstChild;
5190 newSelection = TREEVIEW_GetNextListItem(infoPtr, prevItem);
5194 newSelection = infoPtr->root->firstChild;
5198 newSelection = TREEVIEW_GetLastListItem(infoPtr, infoPtr->root);
5202 if (prevItem->state & TVIS_EXPANDED)
5204 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5206 else if (prevItem->parent != infoPtr->root)
5208 newSelection = prevItem->parent;
5213 if (TREEVIEW_HasChildren(infoPtr, prevItem))
5215 if (!(prevItem->state & TVIS_EXPANDED))
5216 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5219 newSelection = prevItem->firstChild;
5226 TREEVIEW_ExpandAll(infoPtr, prevItem);
5230 if (!(prevItem->state & TVIS_EXPANDED))
5231 TREEVIEW_Expand(infoPtr, prevItem, FALSE, TRUE);
5235 if (prevItem->state & TVIS_EXPANDED)
5236 TREEVIEW_Collapse(infoPtr, prevItem, FALSE, TRUE);
5241 = TREEVIEW_GetListItem(infoPtr, prevItem,
5242 -TREEVIEW_GetVisibleCount(infoPtr));
5247 = TREEVIEW_GetListItem(infoPtr, prevItem,
5248 TREEVIEW_GetVisibleCount(infoPtr));
5252 newSelection = prevItem->parent;
5253 if (newSelection == infoPtr->root)
5254 newSelection = NULL;
5258 if (infoPtr->dwStyle & TVS_CHECKBOXES)
5259 TREEVIEW_ToggleItemState(infoPtr, prevItem);
5263 if (newSelection && newSelection != prevItem)
5265 if (TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, newSelection,
5268 TREEVIEW_EnsureVisible(infoPtr, newSelection, FALSE);
5276 TREEVIEW_MouseLeave (TREEVIEW_INFO * infoPtr)
5278 /* remove hot effect from item */
5279 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5280 infoPtr->hotItem = NULL;
5286 TREEVIEW_MouseMove (TREEVIEW_INFO * infoPtr, LPARAM lParam)
5289 TRACKMOUSEEVENT trackinfo;
5290 TREEVIEW_ITEM * item;
5292 if (!(infoPtr->dwStyle & TVS_TRACKSELECT)) return 0;
5294 /* fill in the TRACKMOUSEEVENT struct */
5295 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
5296 trackinfo.dwFlags = TME_QUERY;
5297 trackinfo.hwndTrack = infoPtr->hwnd;
5299 /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
5300 _TrackMouseEvent(&trackinfo);
5302 /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
5303 if(!(trackinfo.dwFlags & TME_LEAVE))
5305 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
5306 trackinfo.hwndTrack = infoPtr->hwnd;
5307 /* do it as fast as possible, minimal systimer latency will be used */
5308 trackinfo.dwHoverTime = 1;
5310 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
5311 /* and can properly deactivate the hot item */
5312 _TrackMouseEvent(&trackinfo);
5315 pt.x = (short)LOWORD(lParam);
5316 pt.y = (short)HIWORD(lParam);
5318 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5320 if (item != infoPtr->hotItem)
5322 /* redraw old hot item */
5323 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5324 infoPtr->hotItem = item;
5325 /* redraw new hot item */
5326 TREEVIEW_InvalidateItem(infoPtr, infoPtr->hotItem);
5332 /* Draw themed border */
5333 static BOOL TREEVIEW_NCPaint (const TREEVIEW_INFO *infoPtr, HRGN region, LPARAM lParam)
5335 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5339 int cxEdge = GetSystemMetrics (SM_CXEDGE),
5340 cyEdge = GetSystemMetrics (SM_CYEDGE);
5343 return DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)region, lParam);
5345 GetWindowRect(infoPtr->hwnd, &r);
5347 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
5348 r.right - cxEdge, r.bottom - cyEdge);
5349 if (region != (HRGN)1)
5350 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
5351 OffsetRect(&r, -r.left, -r.top);
5353 dc = GetDCEx(infoPtr->hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
5354 OffsetRect(&r, -r.left, -r.top);
5356 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
5357 DrawThemeParentBackground(infoPtr->hwnd, dc, &r);
5358 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
5359 ReleaseDC(infoPtr->hwnd, dc);
5361 /* Call default proc to get the scrollbars etc. painted */
5362 DefWindowProcW (infoPtr->hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
5368 TREEVIEW_Notify(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5370 LPNMHDR lpnmh = (LPNMHDR)lParam;
5372 if (lpnmh->code == PGN_CALCSIZE) {
5373 LPNMPGCALCSIZE lppgc = (LPNMPGCALCSIZE)lParam;
5375 if (lppgc->dwFlag == PGF_CALCWIDTH) {
5376 lppgc->iWidth = infoPtr->treeWidth;
5377 TRACE("got PGN_CALCSIZE, returning horz size = %d, client=%d\n",
5378 infoPtr->treeWidth, infoPtr->clientWidth);
5381 lppgc->iHeight = infoPtr->treeHeight;
5382 TRACE("got PGN_CALCSIZE, returning vert size = %d, client=%d\n",
5383 infoPtr->treeHeight, infoPtr->clientHeight);
5387 return DefWindowProcW(infoPtr->hwnd, WM_NOTIFY, wParam, lParam);
5391 TREEVIEW_Size(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5393 if (wParam == SIZE_RESTORED)
5395 infoPtr->clientWidth = (short)LOWORD(lParam);
5396 infoPtr->clientHeight = (short)HIWORD(lParam);
5398 TREEVIEW_RecalculateVisibleOrder(infoPtr, NULL);
5399 TREEVIEW_SetFirstVisible(infoPtr, infoPtr->firstVisible, TRUE);
5400 TREEVIEW_UpdateScrollBars(infoPtr);
5404 FIXME("WM_SIZE flag %lx %lx not handled\n", wParam, lParam);
5407 TREEVIEW_Invalidate(infoPtr, NULL);
5412 TREEVIEW_StyleChanged(TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5414 TRACE("(%lx %lx)\n", wParam, lParam);
5416 if (wParam == GWL_STYLE)
5418 DWORD dwNewStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
5420 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_CHECKBOXES)
5422 if (dwNewStyle & TVS_CHECKBOXES)
5424 initialize_checkboxes(infoPtr);
5425 TRACE("checkboxes enabled\n");
5429 FIXME("tried to disable checkboxes\n");
5433 if ((infoPtr->dwStyle ^ dwNewStyle) & TVS_NOTOOLTIPS)
5435 if (infoPtr->dwStyle & TVS_NOTOOLTIPS)
5437 infoPtr->hwndToolTip = COMCTL32_CreateToolTip(infoPtr->hwnd);
5438 TRACE("tooltips enabled\n");
5442 DestroyWindow(infoPtr->hwndToolTip);
5443 infoPtr->hwndToolTip = 0;
5444 TRACE("tooltips disabled\n");
5448 infoPtr->dwStyle = dwNewStyle;
5451 TREEVIEW_UpdateSubTree(infoPtr, infoPtr->root);
5452 TREEVIEW_UpdateScrollBars(infoPtr);
5453 TREEVIEW_Invalidate(infoPtr, NULL);
5459 TREEVIEW_SetCursor(const TREEVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
5462 TREEVIEW_ITEM * item;
5466 ScreenToClient(infoPtr->hwnd, &pt);
5468 item = TREEVIEW_HitTestPoint(infoPtr, pt);
5470 memset(&nmmouse, 0, sizeof(nmmouse));
5471 nmmouse.hdr.hwndFrom = infoPtr->hwnd;
5472 nmmouse.hdr.idFrom = GetWindowLongPtrW(infoPtr->hwnd, GWLP_ID);
5473 nmmouse.hdr.code = NM_SETCURSOR;
5476 nmmouse.dwItemSpec = (DWORD_PTR)item;
5477 nmmouse.dwItemData = item->lParam;
5481 nmmouse.dwHitInfo = lParam;
5482 if (TREEVIEW_SendRealNotify(infoPtr, nmmouse.hdr.idFrom, (LPARAM)&nmmouse))
5485 if (item && (infoPtr->dwStyle & TVS_TRACKSELECT))
5487 SetCursor(infoPtr->hcurHand);
5491 return DefWindowProcW(infoPtr->hwnd, WM_SETCURSOR, wParam, lParam);
5495 TREEVIEW_SetFocus(TREEVIEW_INFO *infoPtr)
5499 if (!infoPtr->selectedItem)
5501 TREEVIEW_DoSelectItem(infoPtr, TVGN_CARET, infoPtr->firstVisible,
5505 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5506 TREEVIEW_SendSimpleNotify(infoPtr, NM_SETFOCUS);
5511 TREEVIEW_KillFocus(const TREEVIEW_INFO *infoPtr)
5515 TREEVIEW_Invalidate(infoPtr, infoPtr->selectedItem);
5516 UpdateWindow(infoPtr->hwnd);
5517 TREEVIEW_SendSimpleNotify(infoPtr, NM_KILLFOCUS);
5521 /* update theme after a WM_THEMECHANGED message */
5522 static LRESULT TREEVIEW_ThemeChanged(const TREEVIEW_INFO *infoPtr)
5524 HTHEME theme = GetWindowTheme (infoPtr->hwnd);
5525 CloseThemeData (theme);
5526 OpenThemeData (infoPtr->hwnd, themeClass);
5531 static LRESULT WINAPI
5532 TREEVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5534 TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(hwnd);
5536 TRACE("hwnd %p msg %04x wp=%08lx lp=%08lx\n", hwnd, uMsg, wParam, lParam);
5538 if (infoPtr) TREEVIEW_VerifyTree(infoPtr);
5541 if (uMsg == WM_CREATE)
5542 TREEVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
5549 case TVM_CREATEDRAGIMAGE:
5550 return TREEVIEW_CreateDragImage(infoPtr, lParam);
5552 case TVM_DELETEITEM:
5553 return TREEVIEW_DeleteItem(infoPtr, (HTREEITEM)lParam);
5555 case TVM_EDITLABELA:
5556 case TVM_EDITLABELW:
5557 return (LRESULT)TREEVIEW_EditLabel(infoPtr, (HTREEITEM)lParam);
5559 case TVM_ENDEDITLABELNOW:
5560 return TREEVIEW_EndEditLabelNow(infoPtr, (BOOL)wParam);
5562 case TVM_ENSUREVISIBLE:
5563 return TREEVIEW_EnsureVisible(infoPtr, (HTREEITEM)lParam, TRUE);
5566 return TREEVIEW_ExpandMsg(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5568 case TVM_GETBKCOLOR:
5569 return TREEVIEW_GetBkColor(infoPtr);
5572 return TREEVIEW_GetCount(infoPtr);
5574 case TVM_GETEDITCONTROL:
5575 return TREEVIEW_GetEditControl(infoPtr);
5577 case TVM_GETIMAGELIST:
5578 return TREEVIEW_GetImageList(infoPtr, wParam);
5581 return TREEVIEW_GetIndent(infoPtr);
5583 case TVM_GETINSERTMARKCOLOR:
5584 return TREEVIEW_GetInsertMarkColor(infoPtr);
5586 case TVM_GETISEARCHSTRINGA:
5587 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGA\n");
5590 case TVM_GETISEARCHSTRINGW:
5591 FIXME("Unimplemented msg TVM_GETISEARCHSTRINGW\n");
5596 return TREEVIEW_GetItemT(infoPtr, (LPTVITEMEXW)lParam,
5597 uMsg == TVM_GETITEMW);
5598 case TVM_GETITEMHEIGHT:
5599 return TREEVIEW_GetItemHeight(infoPtr);
5601 case TVM_GETITEMRECT:
5602 return TREEVIEW_GetItemRect(infoPtr, (BOOL)wParam, (LPRECT)lParam);
5604 case TVM_GETITEMSTATE:
5605 return TREEVIEW_GetItemState(infoPtr, (HTREEITEM)wParam, (UINT)lParam);
5607 case TVM_GETLINECOLOR:
5608 return TREEVIEW_GetLineColor(infoPtr);
5610 case TVM_GETNEXTITEM:
5611 return TREEVIEW_GetNextItem(infoPtr, (UINT)wParam, (HTREEITEM)lParam);
5613 case TVM_GETSCROLLTIME:
5614 return TREEVIEW_GetScrollTime(infoPtr);
5616 case TVM_GETTEXTCOLOR:
5617 return TREEVIEW_GetTextColor(infoPtr);
5619 case TVM_GETTOOLTIPS:
5620 return TREEVIEW_GetToolTips(infoPtr);
5622 case TVM_GETUNICODEFORMAT:
5623 return TREEVIEW_GetUnicodeFormat(infoPtr);
5625 case TVM_GETVISIBLECOUNT:
5626 return TREEVIEW_GetVisibleCount(infoPtr);
5629 return TREEVIEW_HitTest(infoPtr, (LPTVHITTESTINFO)lParam);
5631 case TVM_INSERTITEMA:
5632 case TVM_INSERTITEMW:
5633 return TREEVIEW_InsertItemT(infoPtr, (LPTVINSERTSTRUCTW)lParam,
5634 uMsg == TVM_INSERTITEMW);
5635 case TVM_SELECTITEM:
5636 return TREEVIEW_SelectItem(infoPtr, (INT)wParam, (HTREEITEM)lParam);
5638 case TVM_SETBKCOLOR:
5639 return TREEVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
5641 case TVM_SETIMAGELIST:
5642 return TREEVIEW_SetImageList(infoPtr, wParam, (HIMAGELIST)lParam);
5645 return TREEVIEW_SetIndent(infoPtr, (UINT)wParam);
5647 case TVM_SETINSERTMARK:
5648 return TREEVIEW_SetInsertMark(infoPtr, (BOOL)wParam, (HTREEITEM)lParam);
5650 case TVM_SETINSERTMARKCOLOR:
5651 return TREEVIEW_SetInsertMarkColor(infoPtr, (COLORREF)lParam);
5655 return TREEVIEW_SetItemT(infoPtr, (LPTVITEMEXW)lParam,
5656 uMsg == TVM_SETITEMW);
5657 case TVM_SETLINECOLOR:
5658 return TREEVIEW_SetLineColor(infoPtr, (COLORREF)lParam);
5660 case TVM_SETITEMHEIGHT:
5661 return TREEVIEW_SetItemHeight(infoPtr, (INT)(SHORT)wParam);
5663 case TVM_SETSCROLLTIME:
5664 return TREEVIEW_SetScrollTime(infoPtr, (UINT)wParam);
5666 case TVM_SETTEXTCOLOR:
5667 return TREEVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
5669 case TVM_SETTOOLTIPS:
5670 return TREEVIEW_SetToolTips(infoPtr, (HWND)wParam);
5672 case TVM_SETUNICODEFORMAT:
5673 return TREEVIEW_SetUnicodeFormat(infoPtr, (BOOL)wParam);
5675 case TVM_SORTCHILDREN:
5676 return TREEVIEW_SortChildren(infoPtr, lParam);
5678 case TVM_SORTCHILDRENCB:
5679 return TREEVIEW_SortChildrenCB(infoPtr, (LPTVSORTCB)lParam);
5682 return TREEVIEW_ProcessLetterKeys(infoPtr, wParam, lParam);
5685 return TREEVIEW_Command(infoPtr, wParam, lParam);
5688 return TREEVIEW_Destroy(infoPtr);
5693 return TREEVIEW_EraseBackground(infoPtr, (HDC)wParam);
5696 return DLGC_WANTARROWS | DLGC_WANTCHARS;
5699 return TREEVIEW_GetFont(infoPtr);
5702 return TREEVIEW_HScroll(infoPtr, wParam);
5705 return TREEVIEW_KeyDown(infoPtr, wParam);
5708 return TREEVIEW_KillFocus(infoPtr);
5710 case WM_LBUTTONDBLCLK:
5711 return TREEVIEW_LButtonDoubleClick(infoPtr, lParam);
5713 case WM_LBUTTONDOWN:
5714 return TREEVIEW_LButtonDown(infoPtr, lParam);
5716 /* WM_MBUTTONDOWN */
5719 return TREEVIEW_MouseLeave(infoPtr);
5722 return TREEVIEW_MouseMove(infoPtr, lParam);
5724 case WM_NCLBUTTONDOWN:
5725 if (infoPtr->hwndEdit)
5726 SetFocus(infoPtr->hwnd);
5730 return TREEVIEW_NCPaint (infoPtr, (HRGN)wParam, lParam);
5733 return TREEVIEW_Notify(infoPtr, wParam, lParam);
5735 case WM_NOTIFYFORMAT:
5736 return TREEVIEW_NotifyFormat(infoPtr, (HWND)wParam, (UINT)lParam);
5738 case WM_PRINTCLIENT:
5739 return TREEVIEW_PrintClient(infoPtr, (HDC)wParam, lParam);
5742 return TREEVIEW_Paint(infoPtr, (HDC)wParam);
5744 case WM_RBUTTONDOWN:
5745 return TREEVIEW_RButtonDown(infoPtr, lParam);
5748 return TREEVIEW_SetCursor(infoPtr, wParam, lParam);
5751 return TREEVIEW_SetFocus(infoPtr);
5754 return TREEVIEW_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
5757 return TREEVIEW_SetRedraw(infoPtr, wParam);
5760 return TREEVIEW_Size(infoPtr, wParam, lParam);
5762 case WM_STYLECHANGED:
5763 return TREEVIEW_StyleChanged(infoPtr, wParam, lParam);
5765 case WM_SYSCOLORCHANGE:
5766 COMCTL32_RefreshSysColors();
5772 return TREEVIEW_HandleTimer(infoPtr, wParam);
5774 case WM_THEMECHANGED:
5775 return TREEVIEW_ThemeChanged (infoPtr);
5778 return TREEVIEW_VScroll(infoPtr, wParam);
5780 /* WM_WININICHANGE */
5783 return TREEVIEW_MouseWheel(infoPtr, wParam, lParam);
5786 TRACE("drawItem\n");
5790 /* This mostly catches MFC and Delphi messages. :( */
5791 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
5792 TRACE("Unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
5794 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
5799 /* Class Registration ***************************************************/
5802 TREEVIEW_Register(void)
5808 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
5809 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
5810 wndClass.lpfnWndProc = TREEVIEW_WindowProc;
5811 wndClass.cbClsExtra = 0;
5812 wndClass.cbWndExtra = sizeof(TREEVIEW_INFO *);
5814 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
5815 wndClass.hbrBackground = 0;
5816 wndClass.lpszClassName = WC_TREEVIEWW;
5818 RegisterClassW(&wndClass);
5823 TREEVIEW_Unregister(void)
5825 UnregisterClassW(WC_TREEVIEWW, NULL);
5829 /* Tree Verification ****************************************************/
5832 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item);
5834 static inline void TREEVIEW_VerifyItemCommon(TREEVIEW_INFO *infoPtr,
5835 TREEVIEW_ITEM *item)
5837 assert(infoPtr != NULL);
5838 assert(item != NULL);
5840 /* both NULL, or both non-null */
5841 assert((item->firstChild == NULL) == (item->lastChild == NULL));
5843 assert(item->firstChild != item);
5844 assert(item->lastChild != item);
5846 if (item->firstChild)
5848 assert(item->firstChild->parent == item);
5849 assert(item->firstChild->prevSibling == NULL);
5852 if (item->lastChild)
5854 assert(item->lastChild->parent == item);
5855 assert(item->lastChild->nextSibling == NULL);
5858 assert(item->nextSibling != item);
5859 if (item->nextSibling)
5861 assert(item->nextSibling->parent == item->parent);
5862 assert(item->nextSibling->prevSibling == item);
5865 assert(item->prevSibling != item);
5866 if (item->prevSibling)
5868 assert(item->prevSibling->parent == item->parent);
5869 assert(item->prevSibling->nextSibling == item);
5874 TREEVIEW_VerifyItem(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5876 assert(item != NULL);
5878 assert(item->parent != NULL);
5879 assert(item->parent != item);
5880 assert(item->iLevel == item->parent->iLevel + 1);
5882 assert(DPA_GetPtrIndex(infoPtr->items, item) != -1);
5884 TREEVIEW_VerifyItemCommon(infoPtr, item);
5886 TREEVIEW_VerifyChildren(infoPtr, item);
5890 TREEVIEW_VerifyChildren(TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *item)
5892 TREEVIEW_ITEM *child;
5893 assert(item != NULL);
5895 for (child = item->firstChild; child != NULL; child = child->nextSibling)
5896 TREEVIEW_VerifyItem(infoPtr, child);
5900 TREEVIEW_VerifyRoot(TREEVIEW_INFO *infoPtr)
5902 TREEVIEW_ITEM *root = infoPtr->root;
5904 assert(root != NULL);
5905 assert(root->iLevel == -1);
5906 assert(root->parent == NULL);
5907 assert(root->prevSibling == NULL);
5909 TREEVIEW_VerifyItemCommon(infoPtr, root);
5911 TREEVIEW_VerifyChildren(infoPtr, root);
5915 TREEVIEW_VerifyTree(TREEVIEW_INFO *infoPtr)
5917 if (!TRACE_ON(treeview)) return;
5919 assert(infoPtr != NULL);
5920 TREEVIEW_VerifyRoot(infoPtr);